Partial implementation of C++11 thread_local.
[official-gcc.git] / gcc / cp / parser.c
blob52a152d1925cdd69ba85e2727dc734836439f223
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 } non_integral_constant;
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 } name_lookup_error;
125 /* The various kinds of required token */
126 typedef enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 } required_token;
181 /* Prototypes. */
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
230 (cp_token *);
232 static tree cp_literal_operator_id
233 (const char *);
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
239 /* Variables. */
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
254 static void
255 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
259 unsigned i, nprinted;
260 cp_token *token;
261 bool do_print;
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265 if (buffer == NULL)
266 return;
268 if (num == 0)
269 num = VEC_length (cp_token, buffer);
271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
274 if (start_token > VEC_address (cp_token, buffer))
276 cp_lexer_print_token (file, &VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284 if (token == start_token)
285 do_print = true;
287 if (!do_print)
288 continue;
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
294 cp_lexer_print_token (file, token);
296 if (token == curr_token)
297 fprintf (file, "]]");
299 switch (token->type)
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
308 default:
309 fputc (' ', file);
313 if (i == num && i < VEC_length (cp_token, buffer))
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, &VEC_last (cp_token, buffer));
319 fprintf (file, "\n");
323 /* Dump all tokens in BUFFER to stderr. */
325 void
326 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
328 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
332 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
333 description for T. */
335 static void
336 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
338 if (t)
340 fprintf (file, "%s: ", desc);
341 print_node_brief (file, "", t, 0);
346 /* Dump parser context C to FILE. */
348 static void
349 cp_debug_print_context (FILE *file, cp_parser_context *c)
351 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
352 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
353 print_node_brief (file, "", c->object_type, 0);
354 fprintf (file, "}\n");
358 /* Print the stack of parsing contexts to FILE starting with FIRST. */
360 static void
361 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363 unsigned i;
364 cp_parser_context *c;
366 fprintf (file, "Parsing context stack:\n");
367 for (i = 0, c = first; c; c = c->next, i++)
369 fprintf (file, "\t#%u: ", i);
370 cp_debug_print_context (file, c);
375 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
377 static void
378 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380 if (flag)
381 fprintf (file, "%s: true\n", desc);
385 /* Print an unparsed function entry UF to FILE. */
387 static void
388 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390 unsigned i;
391 cp_default_arg_entry *default_arg_fn;
392 tree fn;
394 fprintf (file, "\tFunctions with default args:\n");
395 for (i = 0;
396 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
397 default_arg_fn);
398 i++)
400 fprintf (file, "\t\tClass type: ");
401 print_node_brief (file, "", default_arg_fn->class_type, 0);
402 fprintf (file, "\t\tDeclaration: ");
403 print_node_brief (file, "", default_arg_fn->decl, 0);
404 fprintf (file, "\n");
407 fprintf (file, "\n\tFunctions with definitions that require "
408 "post-processing\n\t\t");
409 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
411 print_node_brief (file, "", fn, 0);
412 fprintf (file, " ");
414 fprintf (file, "\n");
416 fprintf (file, "\n\tNon-static data members with initializers that require "
417 "post-processing\n\t\t");
418 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
420 print_node_brief (file, "", fn, 0);
421 fprintf (file, " ");
423 fprintf (file, "\n");
427 /* Print the stack of unparsed member functions S to FILE. */
429 static void
430 cp_debug_print_unparsed_queues (FILE *file,
431 VEC(cp_unparsed_functions_entry, gc) *s)
433 unsigned i;
434 cp_unparsed_functions_entry *uf;
436 fprintf (file, "Unparsed functions\n");
437 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
439 fprintf (file, "#%u:\n", i);
440 cp_debug_print_unparsed_function (file, uf);
445 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
446 the given PARSER. If FILE is NULL, the output is printed on stderr. */
448 static void
449 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
451 cp_token *next_token, *first_token, *start_token;
453 if (file == NULL)
454 file = stderr;
456 next_token = parser->lexer->next_token;
457 first_token = VEC_address (cp_token, parser->lexer->buffer);
458 start_token = (next_token > first_token + window_size / 2)
459 ? next_token - window_size / 2
460 : first_token;
461 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
462 next_token);
466 /* Dump debugging information for the given PARSER. If FILE is NULL,
467 the output is printed on stderr. */
469 void
470 cp_debug_parser (FILE *file, cp_parser *parser)
472 const size_t window_size = 20;
473 cp_token *token;
474 expanded_location eloc;
476 if (file == NULL)
477 file = stderr;
479 fprintf (file, "Parser state\n\n");
480 fprintf (file, "Number of tokens: %u\n",
481 VEC_length (cp_token, parser->lexer->buffer));
482 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
483 cp_debug_print_tree_if_set (file, "Object scope",
484 parser->object_scope);
485 cp_debug_print_tree_if_set (file, "Qualifying scope",
486 parser->qualifying_scope);
487 cp_debug_print_context_stack (file, parser->context);
488 cp_debug_print_flag (file, "Allow GNU extensions",
489 parser->allow_gnu_extensions_p);
490 cp_debug_print_flag (file, "'>' token is greater-than",
491 parser->greater_than_is_operator_p);
492 cp_debug_print_flag (file, "Default args allowed in current "
493 "parameter list", parser->default_arg_ok_p);
494 cp_debug_print_flag (file, "Parsing integral constant-expression",
495 parser->integral_constant_expression_p);
496 cp_debug_print_flag (file, "Allow non-constant expression in current "
497 "constant-expression",
498 parser->allow_non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Seen non-constant expression",
500 parser->non_integral_constant_expression_p);
501 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
502 "current context",
503 parser->local_variables_forbidden_p);
504 cp_debug_print_flag (file, "In unbraced linkage specification",
505 parser->in_unbraced_linkage_specification_p);
506 cp_debug_print_flag (file, "Parsing a declarator",
507 parser->in_declarator_p);
508 cp_debug_print_flag (file, "In template argument list",
509 parser->in_template_argument_list_p);
510 cp_debug_print_flag (file, "Parsing an iteration statement",
511 parser->in_statement & IN_ITERATION_STMT);
512 cp_debug_print_flag (file, "Parsing a switch statement",
513 parser->in_statement & IN_SWITCH_STMT);
514 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
515 parser->in_statement & IN_OMP_BLOCK);
516 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
517 parser->in_statement & IN_OMP_FOR);
518 cp_debug_print_flag (file, "Parsing an if statement",
519 parser->in_statement & IN_IF_STMT);
520 cp_debug_print_flag (file, "Parsing a type-id in an expression "
521 "context", parser->in_type_id_in_expr_p);
522 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
523 parser->implicit_extern_c);
524 cp_debug_print_flag (file, "String expressions should be translated "
525 "to execution character set",
526 parser->translate_strings_p);
527 cp_debug_print_flag (file, "Parsing function body outside of a "
528 "local class", parser->in_function_body);
529 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
530 parser->colon_corrects_to_scope_p);
531 if (parser->type_definition_forbidden_message)
532 fprintf (file, "Error message for forbidden type definitions: %s\n",
533 parser->type_definition_forbidden_message);
534 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
535 fprintf (file, "Number of class definitions in progress: %u\n",
536 parser->num_classes_being_defined);
537 fprintf (file, "Number of template parameter lists for the current "
538 "declaration: %u\n", parser->num_template_parameter_lists);
539 cp_debug_parser_tokens (file, parser, window_size);
540 token = parser->lexer->next_token;
541 fprintf (file, "Next token to parse:\n");
542 fprintf (file, "\tToken: ");
543 cp_lexer_print_token (file, token);
544 eloc = expand_location (token->location);
545 fprintf (file, "\n\tFile: %s\n", eloc.file);
546 fprintf (file, "\tLine: %d\n", eloc.line);
547 fprintf (file, "\tColumn: %d\n", eloc.column);
551 /* Allocate memory for a new lexer object and return it. */
553 static cp_lexer *
554 cp_lexer_alloc (void)
556 cp_lexer *lexer;
558 c_common_no_more_pch ();
560 /* Allocate the memory. */
561 lexer = ggc_alloc_cleared_cp_lexer ();
563 /* Initially we are not debugging. */
564 lexer->debugging_p = false;
566 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
567 CP_SAVED_TOKEN_STACK);
569 /* Create the buffer. */
570 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
572 return lexer;
576 /* Create a new main C++ lexer, the lexer that gets tokens from the
577 preprocessor. */
579 static cp_lexer *
580 cp_lexer_new_main (void)
582 cp_lexer *lexer;
583 cp_token token;
585 /* It's possible that parsing the first pragma will load a PCH file,
586 which is a GC collection point. So we have to do that before
587 allocating any memory. */
588 cp_parser_initial_pragma (&token);
590 lexer = cp_lexer_alloc ();
592 /* Put the first token in the buffer. */
593 VEC_quick_push (cp_token, lexer->buffer, token);
595 /* Get the remaining tokens from the preprocessor. */
596 while (token.type != CPP_EOF)
598 cp_lexer_get_preprocessor_token (lexer, &token);
599 VEC_safe_push (cp_token, gc, lexer->buffer, token);
602 lexer->last_token = VEC_address (cp_token, lexer->buffer)
603 + VEC_length (cp_token, lexer->buffer)
604 - 1;
605 lexer->next_token = VEC_length (cp_token, lexer->buffer)
606 ? VEC_address (cp_token, lexer->buffer)
607 : &eof_token;
609 /* Subsequent preprocessor diagnostics should use compiler
610 diagnostic functions to get the compiler source location. */
611 done_lexing = true;
613 gcc_assert (!lexer->next_token->purged_p);
614 return lexer;
617 /* Create a new lexer whose token stream is primed with the tokens in
618 CACHE. When these tokens are exhausted, no new tokens will be read. */
620 static cp_lexer *
621 cp_lexer_new_from_tokens (cp_token_cache *cache)
623 cp_token *first = cache->first;
624 cp_token *last = cache->last;
625 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
627 /* We do not own the buffer. */
628 lexer->buffer = NULL;
629 lexer->next_token = first == last ? &eof_token : first;
630 lexer->last_token = last;
632 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
633 CP_SAVED_TOKEN_STACK);
635 /* Initially we are not debugging. */
636 lexer->debugging_p = false;
638 gcc_assert (!lexer->next_token->purged_p);
639 return lexer;
642 /* Frees all resources associated with LEXER. */
644 static void
645 cp_lexer_destroy (cp_lexer *lexer)
647 VEC_free (cp_token, gc, lexer->buffer);
648 VEC_free (cp_token_position, heap, lexer->saved_tokens);
649 ggc_free (lexer);
652 /* Returns nonzero if debugging information should be output. */
654 static inline bool
655 cp_lexer_debugging_p (cp_lexer *lexer)
657 return lexer->debugging_p;
661 static inline cp_token_position
662 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
664 gcc_assert (!previous_p || lexer->next_token != &eof_token);
666 return lexer->next_token - previous_p;
669 static inline cp_token *
670 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
672 return pos;
675 static inline void
676 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
678 lexer->next_token = cp_lexer_token_at (lexer, pos);
681 static inline cp_token_position
682 cp_lexer_previous_token_position (cp_lexer *lexer)
684 if (lexer->next_token == &eof_token)
685 return lexer->last_token - 1;
686 else
687 return cp_lexer_token_position (lexer, true);
690 static inline cp_token *
691 cp_lexer_previous_token (cp_lexer *lexer)
693 cp_token_position tp = cp_lexer_previous_token_position (lexer);
695 return cp_lexer_token_at (lexer, tp);
698 /* nonzero if we are presently saving tokens. */
700 static inline int
701 cp_lexer_saving_tokens (const cp_lexer* lexer)
703 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 /* Store the next token from the preprocessor in *TOKEN. Return true
707 if we reach EOF. If LEXER is NULL, assume we are handling an
708 initial #pragma pch_preprocess, and thus want the lexer to return
709 processed strings. */
711 static void
712 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
714 static int is_extern_c = 0;
716 /* Get a new token from the preprocessor. */
717 token->type
718 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
719 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
720 token->keyword = RID_MAX;
721 token->pragma_kind = PRAGMA_NONE;
722 token->purged_p = false;
724 /* On some systems, some header files are surrounded by an
725 implicit extern "C" block. Set a flag in the token if it
726 comes from such a header. */
727 is_extern_c += pending_lang_change;
728 pending_lang_change = 0;
729 token->implicit_extern_c = is_extern_c > 0;
731 /* Check to see if this token is a keyword. */
732 if (token->type == CPP_NAME)
734 if (C_IS_RESERVED_WORD (token->u.value))
736 /* Mark this token as a keyword. */
737 token->type = CPP_KEYWORD;
738 /* Record which keyword. */
739 token->keyword = C_RID_CODE (token->u.value);
741 else
743 if (warn_cxx0x_compat
744 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
745 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
747 /* Warn about the C++0x keyword (but still treat it as
748 an identifier). */
749 warning (OPT_Wc__0x_compat,
750 "identifier %qE is a keyword in C++11",
751 token->u.value);
753 /* Clear out the C_RID_CODE so we don't warn about this
754 particular identifier-turned-keyword again. */
755 C_SET_RID_CODE (token->u.value, RID_MAX);
758 token->ambiguous_p = false;
759 token->keyword = RID_MAX;
762 else if (token->type == CPP_AT_NAME)
764 /* This only happens in Objective-C++; it must be a keyword. */
765 token->type = CPP_KEYWORD;
766 switch (C_RID_CODE (token->u.value))
768 /* Replace 'class' with '@class', 'private' with '@private',
769 etc. This prevents confusion with the C++ keyword
770 'class', and makes the tokens consistent with other
771 Objective-C 'AT' keywords. For example '@class' is
772 reported as RID_AT_CLASS which is consistent with
773 '@synchronized', which is reported as
774 RID_AT_SYNCHRONIZED.
776 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
777 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
778 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
779 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
780 case RID_THROW: token->keyword = RID_AT_THROW; break;
781 case RID_TRY: token->keyword = RID_AT_TRY; break;
782 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
783 default: token->keyword = C_RID_CODE (token->u.value);
786 else if (token->type == CPP_PRAGMA)
788 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
789 token->pragma_kind = ((enum pragma_kind)
790 TREE_INT_CST_LOW (token->u.value));
791 token->u.value = NULL_TREE;
795 /* Update the globals input_location and the input file stack from TOKEN. */
796 static inline void
797 cp_lexer_set_source_position_from_token (cp_token *token)
799 if (token->type != CPP_EOF)
801 input_location = token->location;
805 /* Return a pointer to the next token in the token stream, but do not
806 consume it. */
808 static inline cp_token *
809 cp_lexer_peek_token (cp_lexer *lexer)
811 if (cp_lexer_debugging_p (lexer))
813 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
814 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
815 putc ('\n', cp_lexer_debug_stream);
817 return lexer->next_token;
820 /* Return true if the next token has the indicated TYPE. */
822 static inline bool
823 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
825 return cp_lexer_peek_token (lexer)->type == type;
828 /* Return true if the next token does not have the indicated TYPE. */
830 static inline bool
831 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
833 return !cp_lexer_next_token_is (lexer, type);
836 /* Return true if the next token is the indicated KEYWORD. */
838 static inline bool
839 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
841 return cp_lexer_peek_token (lexer)->keyword == keyword;
844 /* Return true if the next token is not the indicated KEYWORD. */
846 static inline bool
847 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
849 return cp_lexer_peek_token (lexer)->keyword != keyword;
852 /* Return true if the next token is a keyword for a decl-specifier. */
854 static bool
855 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
857 cp_token *token;
859 token = cp_lexer_peek_token (lexer);
860 switch (token->keyword)
862 /* auto specifier: storage-class-specifier in C++,
863 simple-type-specifier in C++0x. */
864 case RID_AUTO:
865 /* Storage classes. */
866 case RID_REGISTER:
867 case RID_STATIC:
868 case RID_EXTERN:
869 case RID_MUTABLE:
870 case RID_THREAD:
871 /* Elaborated type specifiers. */
872 case RID_ENUM:
873 case RID_CLASS:
874 case RID_STRUCT:
875 case RID_UNION:
876 case RID_TYPENAME:
877 /* Simple type specifiers. */
878 case RID_CHAR:
879 case RID_CHAR16:
880 case RID_CHAR32:
881 case RID_WCHAR:
882 case RID_BOOL:
883 case RID_SHORT:
884 case RID_INT:
885 case RID_LONG:
886 case RID_INT128:
887 case RID_SIGNED:
888 case RID_UNSIGNED:
889 case RID_FLOAT:
890 case RID_DOUBLE:
891 case RID_VOID:
892 /* GNU extensions. */
893 case RID_ATTRIBUTE:
894 case RID_TYPEOF:
895 /* C++0x extensions. */
896 case RID_DECLTYPE:
897 case RID_UNDERLYING_TYPE:
898 return true;
900 default:
901 return false;
905 /* Returns TRUE iff the token T begins a decltype type. */
907 static bool
908 token_is_decltype (cp_token *t)
910 return (t->keyword == RID_DECLTYPE
911 || t->type == CPP_DECLTYPE);
914 /* Returns TRUE iff the next token begins a decltype type. */
916 static bool
917 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
919 cp_token *t = cp_lexer_peek_token (lexer);
920 return token_is_decltype (t);
923 /* Return a pointer to the Nth token in the token stream. If N is 1,
924 then this is precisely equivalent to cp_lexer_peek_token (except
925 that it is not inline). One would like to disallow that case, but
926 there is one case (cp_parser_nth_token_starts_template_id) where
927 the caller passes a variable for N and it might be 1. */
929 static cp_token *
930 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
932 cp_token *token;
934 /* N is 1-based, not zero-based. */
935 gcc_assert (n > 0);
937 if (cp_lexer_debugging_p (lexer))
938 fprintf (cp_lexer_debug_stream,
939 "cp_lexer: peeking ahead %ld at token: ", (long)n);
941 --n;
942 token = lexer->next_token;
943 gcc_assert (!n || token != &eof_token);
944 while (n != 0)
946 ++token;
947 if (token == lexer->last_token)
949 token = &eof_token;
950 break;
953 if (!token->purged_p)
954 --n;
957 if (cp_lexer_debugging_p (lexer))
959 cp_lexer_print_token (cp_lexer_debug_stream, token);
960 putc ('\n', cp_lexer_debug_stream);
963 return token;
966 /* Return the next token, and advance the lexer's next_token pointer
967 to point to the next non-purged token. */
969 static cp_token *
970 cp_lexer_consume_token (cp_lexer* lexer)
972 cp_token *token = lexer->next_token;
974 gcc_assert (token != &eof_token);
975 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
979 lexer->next_token++;
980 if (lexer->next_token == lexer->last_token)
982 lexer->next_token = &eof_token;
983 break;
987 while (lexer->next_token->purged_p);
989 cp_lexer_set_source_position_from_token (token);
991 /* Provide debugging output. */
992 if (cp_lexer_debugging_p (lexer))
994 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
995 cp_lexer_print_token (cp_lexer_debug_stream, token);
996 putc ('\n', cp_lexer_debug_stream);
999 return token;
1002 /* Permanently remove the next token from the token stream, and
1003 advance the next_token pointer to refer to the next non-purged
1004 token. */
1006 static void
1007 cp_lexer_purge_token (cp_lexer *lexer)
1009 cp_token *tok = lexer->next_token;
1011 gcc_assert (tok != &eof_token);
1012 tok->purged_p = true;
1013 tok->location = UNKNOWN_LOCATION;
1014 tok->u.value = NULL_TREE;
1015 tok->keyword = RID_MAX;
1019 tok++;
1020 if (tok == lexer->last_token)
1022 tok = &eof_token;
1023 break;
1026 while (tok->purged_p);
1027 lexer->next_token = tok;
1030 /* Permanently remove all tokens after TOK, up to, but not
1031 including, the token that will be returned next by
1032 cp_lexer_peek_token. */
1034 static void
1035 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1037 cp_token *peek = lexer->next_token;
1039 if (peek == &eof_token)
1040 peek = lexer->last_token;
1042 gcc_assert (tok < peek);
1044 for ( tok += 1; tok != peek; tok += 1)
1046 tok->purged_p = true;
1047 tok->location = UNKNOWN_LOCATION;
1048 tok->u.value = NULL_TREE;
1049 tok->keyword = RID_MAX;
1053 /* Begin saving tokens. All tokens consumed after this point will be
1054 preserved. */
1056 static void
1057 cp_lexer_save_tokens (cp_lexer* lexer)
1059 /* Provide debugging output. */
1060 if (cp_lexer_debugging_p (lexer))
1061 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1063 VEC_safe_push (cp_token_position, heap,
1064 lexer->saved_tokens, lexer->next_token);
1067 /* Commit to the portion of the token stream most recently saved. */
1069 static void
1070 cp_lexer_commit_tokens (cp_lexer* lexer)
1072 /* Provide debugging output. */
1073 if (cp_lexer_debugging_p (lexer))
1074 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1076 VEC_pop (cp_token_position, lexer->saved_tokens);
1079 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1080 to the token stream. Stop saving tokens. */
1082 static void
1083 cp_lexer_rollback_tokens (cp_lexer* lexer)
1085 /* Provide debugging output. */
1086 if (cp_lexer_debugging_p (lexer))
1087 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1089 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 /* Print a representation of the TOKEN on the STREAM. */
1094 static void
1095 cp_lexer_print_token (FILE * stream, cp_token *token)
1097 /* We don't use cpp_type2name here because the parser defines
1098 a few tokens of its own. */
1099 static const char *const token_names[] = {
1100 /* cpplib-defined token types */
1101 #define OP(e, s) #e,
1102 #define TK(e, s) #e,
1103 TTYPE_TABLE
1104 #undef OP
1105 #undef TK
1106 /* C++ parser token types - see "Manifest constants", above. */
1107 "KEYWORD",
1108 "TEMPLATE_ID",
1109 "NESTED_NAME_SPECIFIER",
1112 /* For some tokens, print the associated data. */
1113 switch (token->type)
1115 case CPP_KEYWORD:
1116 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1117 For example, `struct' is mapped to an INTEGER_CST. */
1118 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1119 break;
1120 /* else fall through */
1121 case CPP_NAME:
1122 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1123 break;
1125 case CPP_STRING:
1126 case CPP_STRING16:
1127 case CPP_STRING32:
1128 case CPP_WSTRING:
1129 case CPP_UTF8STRING:
1130 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1131 break;
1133 case CPP_NUMBER:
1134 print_generic_expr (stream, token->u.value, 0);
1135 break;
1137 default:
1138 /* If we have a name for the token, print it out. Otherwise, we
1139 simply give the numeric code. */
1140 if (token->type < ARRAY_SIZE(token_names))
1141 fputs (token_names[token->type], stream);
1142 else
1143 fprintf (stream, "[%d]", token->type);
1144 break;
1148 /* Start emitting debugging information. */
1150 static void
1151 cp_lexer_start_debugging (cp_lexer* lexer)
1153 lexer->debugging_p = true;
1154 cp_lexer_debug_stream = stderr;
1157 /* Stop emitting debugging information. */
1159 static void
1160 cp_lexer_stop_debugging (cp_lexer* lexer)
1162 lexer->debugging_p = false;
1163 cp_lexer_debug_stream = NULL;
1166 /* Create a new cp_token_cache, representing a range of tokens. */
1168 static cp_token_cache *
1169 cp_token_cache_new (cp_token *first, cp_token *last)
1171 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1172 cache->first = first;
1173 cache->last = last;
1174 return cache;
1178 /* Decl-specifiers. */
1180 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1182 static void
1183 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1185 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 /* Declarators. */
1190 /* Nothing other than the parser should be creating declarators;
1191 declarators are a semi-syntactic representation of C++ entities.
1192 Other parts of the front end that need to create entities (like
1193 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1195 static cp_declarator *make_call_declarator
1196 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1197 static cp_declarator *make_array_declarator
1198 (cp_declarator *, tree);
1199 static cp_declarator *make_pointer_declarator
1200 (cp_cv_quals, cp_declarator *, tree);
1201 static cp_declarator *make_reference_declarator
1202 (cp_cv_quals, cp_declarator *, bool, tree);
1203 static cp_parameter_declarator *make_parameter_declarator
1204 (cp_decl_specifier_seq *, cp_declarator *, tree);
1205 static cp_declarator *make_ptrmem_declarator
1206 (cp_cv_quals, tree, cp_declarator *, tree);
1208 /* An erroneous declarator. */
1209 static cp_declarator *cp_error_declarator;
1211 /* The obstack on which declarators and related data structures are
1212 allocated. */
1213 static struct obstack declarator_obstack;
1215 /* Alloc BYTES from the declarator memory pool. */
1217 static inline void *
1218 alloc_declarator (size_t bytes)
1220 return obstack_alloc (&declarator_obstack, bytes);
1223 /* Allocate a declarator of the indicated KIND. Clear fields that are
1224 common to all declarators. */
1226 static cp_declarator *
1227 make_declarator (cp_declarator_kind kind)
1229 cp_declarator *declarator;
1231 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1232 declarator->kind = kind;
1233 declarator->attributes = NULL_TREE;
1234 declarator->std_attributes = NULL_TREE;
1235 declarator->declarator = NULL;
1236 declarator->parameter_pack_p = false;
1237 declarator->id_loc = UNKNOWN_LOCATION;
1239 return declarator;
1242 /* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1246 is, if any. */
1248 static cp_declarator *
1249 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250 special_function_kind sfk)
1252 cp_declarator *declarator;
1254 /* It is valid to write:
1256 class C { void f(); };
1257 typedef C D;
1258 void D::f();
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1263 well. */
1264 if (qualifying_scope && TYPE_P (qualifying_scope))
1265 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271 declarator = make_declarator (cdk_id);
1272 declarator->u.id.qualifying_scope = qualifying_scope;
1273 declarator->u.id.unqualified_name = unqualified_name;
1274 declarator->u.id.sfk = sfk;
1276 return declarator;
1279 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. ATTRIBUTES represent the attributes that
1282 appertain to the pointer or reference. */
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1286 tree attributes)
1288 cp_declarator *declarator;
1290 declarator = make_declarator (cdk_pointer);
1291 declarator->declarator = target;
1292 declarator->u.pointer.qualifiers = cv_qualifiers;
1293 declarator->u.pointer.class_type = NULL_TREE;
1294 if (target)
1296 declarator->id_loc = target->id_loc;
1297 declarator->parameter_pack_p = target->parameter_pack_p;
1298 target->parameter_pack_p = false;
1300 else
1301 declarator->parameter_pack_p = false;
1303 declarator->std_attributes = attributes;
1305 return declarator;
1308 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1309 represent the attributes that appertain to the pointer or
1310 reference. */
1312 cp_declarator *
1313 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1314 bool rvalue_ref, tree attributes)
1316 cp_declarator *declarator;
1318 declarator = make_declarator (cdk_reference);
1319 declarator->declarator = target;
1320 declarator->u.reference.qualifiers = cv_qualifiers;
1321 declarator->u.reference.rvalue_ref = rvalue_ref;
1322 if (target)
1324 declarator->id_loc = target->id_loc;
1325 declarator->parameter_pack_p = target->parameter_pack_p;
1326 target->parameter_pack_p = false;
1328 else
1329 declarator->parameter_pack_p = false;
1331 declarator->std_attributes = attributes;
1333 return declarator;
1336 /* Like make_pointer_declarator -- but for a pointer to a non-static
1337 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1338 appertain to the pointer or reference. */
1340 cp_declarator *
1341 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1342 cp_declarator *pointee,
1343 tree attributes)
1345 cp_declarator *declarator;
1347 declarator = make_declarator (cdk_ptrmem);
1348 declarator->declarator = pointee;
1349 declarator->u.pointer.qualifiers = cv_qualifiers;
1350 declarator->u.pointer.class_type = class_type;
1352 if (pointee)
1354 declarator->parameter_pack_p = pointee->parameter_pack_p;
1355 pointee->parameter_pack_p = false;
1357 else
1358 declarator->parameter_pack_p = false;
1360 declarator->std_attributes = attributes;
1362 return declarator;
1365 /* Make a declarator for the function given by TARGET, with the
1366 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1367 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1368 indicates what exceptions can be thrown. */
1370 cp_declarator *
1371 make_call_declarator (cp_declarator *target,
1372 tree parms,
1373 cp_cv_quals cv_qualifiers,
1374 cp_virt_specifiers virt_specifiers,
1375 tree exception_specification,
1376 tree late_return_type)
1378 cp_declarator *declarator;
1380 declarator = make_declarator (cdk_function);
1381 declarator->declarator = target;
1382 declarator->u.function.parameters = parms;
1383 declarator->u.function.qualifiers = cv_qualifiers;
1384 declarator->u.function.virt_specifiers = virt_specifiers;
1385 declarator->u.function.exception_specification = exception_specification;
1386 declarator->u.function.late_return_type = late_return_type;
1387 if (target)
1389 declarator->id_loc = target->id_loc;
1390 declarator->parameter_pack_p = target->parameter_pack_p;
1391 target->parameter_pack_p = false;
1393 else
1394 declarator->parameter_pack_p = false;
1396 return declarator;
1399 /* Make a declarator for an array of BOUNDS elements, each of which is
1400 defined by ELEMENT. */
1402 cp_declarator *
1403 make_array_declarator (cp_declarator *element, tree bounds)
1405 cp_declarator *declarator;
1407 declarator = make_declarator (cdk_array);
1408 declarator->declarator = element;
1409 declarator->u.array.bounds = bounds;
1410 if (element)
1412 declarator->id_loc = element->id_loc;
1413 declarator->parameter_pack_p = element->parameter_pack_p;
1414 element->parameter_pack_p = false;
1416 else
1417 declarator->parameter_pack_p = false;
1419 return declarator;
1422 /* Determine whether the declarator we've seen so far can be a
1423 parameter pack, when followed by an ellipsis. */
1424 static bool
1425 declarator_can_be_parameter_pack (cp_declarator *declarator)
1427 /* Search for a declarator name, or any other declarator that goes
1428 after the point where the ellipsis could appear in a parameter
1429 pack. If we find any of these, then this declarator can not be
1430 made into a parameter pack. */
1431 bool found = false;
1432 while (declarator && !found)
1434 switch ((int)declarator->kind)
1436 case cdk_id:
1437 case cdk_array:
1438 found = true;
1439 break;
1441 case cdk_error:
1442 return true;
1444 default:
1445 declarator = declarator->declarator;
1446 break;
1450 return !found;
1453 cp_parameter_declarator *no_parameters;
1455 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1456 DECLARATOR and DEFAULT_ARGUMENT. */
1458 cp_parameter_declarator *
1459 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1460 cp_declarator *declarator,
1461 tree default_argument)
1463 cp_parameter_declarator *parameter;
1465 parameter = ((cp_parameter_declarator *)
1466 alloc_declarator (sizeof (cp_parameter_declarator)));
1467 parameter->next = NULL;
1468 if (decl_specifiers)
1469 parameter->decl_specifiers = *decl_specifiers;
1470 else
1471 clear_decl_specs (&parameter->decl_specifiers);
1472 parameter->declarator = declarator;
1473 parameter->default_argument = default_argument;
1474 parameter->ellipsis_p = false;
1476 return parameter;
1479 /* Returns true iff DECLARATOR is a declaration for a function. */
1481 static bool
1482 function_declarator_p (const cp_declarator *declarator)
1484 while (declarator)
1486 if (declarator->kind == cdk_function
1487 && declarator->declarator->kind == cdk_id)
1488 return true;
1489 if (declarator->kind == cdk_id
1490 || declarator->kind == cdk_error)
1491 return false;
1492 declarator = declarator->declarator;
1494 return false;
1497 /* The parser. */
1499 /* Overview
1500 --------
1502 A cp_parser parses the token stream as specified by the C++
1503 grammar. Its job is purely parsing, not semantic analysis. For
1504 example, the parser breaks the token stream into declarators,
1505 expressions, statements, and other similar syntactic constructs.
1506 It does not check that the types of the expressions on either side
1507 of an assignment-statement are compatible, or that a function is
1508 not declared with a parameter of type `void'.
1510 The parser invokes routines elsewhere in the compiler to perform
1511 semantic analysis and to build up the abstract syntax tree for the
1512 code processed.
1514 The parser (and the template instantiation code, which is, in a
1515 way, a close relative of parsing) are the only parts of the
1516 compiler that should be calling push_scope and pop_scope, or
1517 related functions. The parser (and template instantiation code)
1518 keeps track of what scope is presently active; everything else
1519 should simply honor that. (The code that generates static
1520 initializers may also need to set the scope, in order to check
1521 access control correctly when emitting the initializers.)
1523 Methodology
1524 -----------
1526 The parser is of the standard recursive-descent variety. Upcoming
1527 tokens in the token stream are examined in order to determine which
1528 production to use when parsing a non-terminal. Some C++ constructs
1529 require arbitrary look ahead to disambiguate. For example, it is
1530 impossible, in the general case, to tell whether a statement is an
1531 expression or declaration without scanning the entire statement.
1532 Therefore, the parser is capable of "parsing tentatively." When the
1533 parser is not sure what construct comes next, it enters this mode.
1534 Then, while we attempt to parse the construct, the parser queues up
1535 error messages, rather than issuing them immediately, and saves the
1536 tokens it consumes. If the construct is parsed successfully, the
1537 parser "commits", i.e., it issues any queued error messages and
1538 the tokens that were being preserved are permanently discarded.
1539 If, however, the construct is not parsed successfully, the parser
1540 rolls back its state completely so that it can resume parsing using
1541 a different alternative.
1543 Future Improvements
1544 -------------------
1546 The performance of the parser could probably be improved substantially.
1547 We could often eliminate the need to parse tentatively by looking ahead
1548 a little bit. In some places, this approach might not entirely eliminate
1549 the need to parse tentatively, but it might still speed up the average
1550 case. */
1552 /* Flags that are passed to some parsing functions. These values can
1553 be bitwise-ored together. */
1555 enum
1557 /* No flags. */
1558 CP_PARSER_FLAGS_NONE = 0x0,
1559 /* The construct is optional. If it is not present, then no error
1560 should be issued. */
1561 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1562 /* When parsing a type-specifier, treat user-defined type-names
1563 as non-type identifiers. */
1564 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1565 /* When parsing a type-specifier, do not try to parse a class-specifier
1566 or enum-specifier. */
1567 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1568 /* When parsing a decl-specifier-seq, only allow type-specifier or
1569 constexpr. */
1570 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1573 /* This type is used for parameters and variables which hold
1574 combinations of the above flags. */
1575 typedef int cp_parser_flags;
1577 /* The different kinds of declarators we want to parse. */
1579 typedef enum cp_parser_declarator_kind
1581 /* We want an abstract declarator. */
1582 CP_PARSER_DECLARATOR_ABSTRACT,
1583 /* We want a named declarator. */
1584 CP_PARSER_DECLARATOR_NAMED,
1585 /* We don't mind, but the name must be an unqualified-id. */
1586 CP_PARSER_DECLARATOR_EITHER
1587 } cp_parser_declarator_kind;
1589 /* The precedence values used to parse binary expressions. The minimum value
1590 of PREC must be 1, because zero is reserved to quickly discriminate
1591 binary operators from other tokens. */
1593 enum cp_parser_prec
1595 PREC_NOT_OPERATOR,
1596 PREC_LOGICAL_OR_EXPRESSION,
1597 PREC_LOGICAL_AND_EXPRESSION,
1598 PREC_INCLUSIVE_OR_EXPRESSION,
1599 PREC_EXCLUSIVE_OR_EXPRESSION,
1600 PREC_AND_EXPRESSION,
1601 PREC_EQUALITY_EXPRESSION,
1602 PREC_RELATIONAL_EXPRESSION,
1603 PREC_SHIFT_EXPRESSION,
1604 PREC_ADDITIVE_EXPRESSION,
1605 PREC_MULTIPLICATIVE_EXPRESSION,
1606 PREC_PM_EXPRESSION,
1607 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1610 /* A mapping from a token type to a corresponding tree node type, with a
1611 precedence value. */
1613 typedef struct cp_parser_binary_operations_map_node
1615 /* The token type. */
1616 enum cpp_ttype token_type;
1617 /* The corresponding tree code. */
1618 enum tree_code tree_type;
1619 /* The precedence of this operator. */
1620 enum cp_parser_prec prec;
1621 } cp_parser_binary_operations_map_node;
1623 typedef struct cp_parser_expression_stack_entry
1625 /* Left hand side of the binary operation we are currently
1626 parsing. */
1627 tree lhs;
1628 /* Original tree code for left hand side, if it was a binary
1629 expression itself (used for -Wparentheses). */
1630 enum tree_code lhs_type;
1631 /* Tree code for the binary operation we are parsing. */
1632 enum tree_code tree_type;
1633 /* Precedence of the binary operation we are parsing. */
1634 enum cp_parser_prec prec;
1635 /* Location of the binary operation we are parsing. */
1636 location_t loc;
1637 } cp_parser_expression_stack_entry;
1639 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1640 entries because precedence levels on the stack are monotonically
1641 increasing. */
1642 typedef struct cp_parser_expression_stack_entry
1643 cp_parser_expression_stack[NUM_PREC_VALUES];
1645 /* Prototypes. */
1647 /* Constructors and destructors. */
1649 static cp_parser_context *cp_parser_context_new
1650 (cp_parser_context *);
1652 /* Class variables. */
1654 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1656 /* The operator-precedence table used by cp_parser_binary_expression.
1657 Transformed into an associative array (binops_by_token) by
1658 cp_parser_new. */
1660 static const cp_parser_binary_operations_map_node binops[] = {
1661 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1662 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1664 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1665 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1666 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1668 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1669 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1671 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1672 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1674 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1675 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1676 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1677 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1679 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1680 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1682 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1684 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1686 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1688 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1690 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1693 /* The same as binops, but initialized by cp_parser_new so that
1694 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1695 for speed. */
1696 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1698 /* Constructors and destructors. */
1700 /* Construct a new context. The context below this one on the stack
1701 is given by NEXT. */
1703 static cp_parser_context *
1704 cp_parser_context_new (cp_parser_context* next)
1706 cp_parser_context *context;
1708 /* Allocate the storage. */
1709 if (cp_parser_context_free_list != NULL)
1711 /* Pull the first entry from the free list. */
1712 context = cp_parser_context_free_list;
1713 cp_parser_context_free_list = context->next;
1714 memset (context, 0, sizeof (*context));
1716 else
1717 context = ggc_alloc_cleared_cp_parser_context ();
1719 /* No errors have occurred yet in this context. */
1720 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1721 /* If this is not the bottommost context, copy information that we
1722 need from the previous context. */
1723 if (next)
1725 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1726 expression, then we are parsing one in this context, too. */
1727 context->object_type = next->object_type;
1728 /* Thread the stack. */
1729 context->next = next;
1732 return context;
1735 /* Managing the unparsed function queues. */
1737 #define unparsed_funs_with_default_args \
1738 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1739 #define unparsed_funs_with_definitions \
1740 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1741 #define unparsed_nsdmis \
1742 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1744 static void
1745 push_unparsed_function_queues (cp_parser *parser)
1747 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1748 VEC_safe_push (cp_unparsed_functions_entry, gc, parser->unparsed_queues, e);
1751 static void
1752 pop_unparsed_function_queues (cp_parser *parser)
1754 release_tree_vector (unparsed_funs_with_definitions);
1755 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1758 /* Prototypes. */
1760 /* Constructors and destructors. */
1762 static cp_parser *cp_parser_new
1763 (void);
1765 /* Routines to parse various constructs.
1767 Those that return `tree' will return the error_mark_node (rather
1768 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1769 Sometimes, they will return an ordinary node if error-recovery was
1770 attempted, even though a parse error occurred. So, to check
1771 whether or not a parse error occurred, you should always use
1772 cp_parser_error_occurred. If the construct is optional (indicated
1773 either by an `_opt' in the name of the function that does the
1774 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1775 the construct is not present. */
1777 /* Lexical conventions [gram.lex] */
1779 static tree cp_parser_identifier
1780 (cp_parser *);
1781 static tree cp_parser_string_literal
1782 (cp_parser *, bool, bool);
1783 static tree cp_parser_userdef_char_literal
1784 (cp_parser *);
1785 static tree cp_parser_userdef_string_literal
1786 (cp_token *);
1787 static tree cp_parser_userdef_numeric_literal
1788 (cp_parser *);
1790 /* Basic concepts [gram.basic] */
1792 static bool cp_parser_translation_unit
1793 (cp_parser *);
1795 /* Expressions [gram.expr] */
1797 static tree cp_parser_primary_expression
1798 (cp_parser *, bool, bool, bool, cp_id_kind *);
1799 static tree cp_parser_id_expression
1800 (cp_parser *, bool, bool, bool *, bool, bool);
1801 static tree cp_parser_unqualified_id
1802 (cp_parser *, bool, bool, bool, bool);
1803 static tree cp_parser_nested_name_specifier_opt
1804 (cp_parser *, bool, bool, bool, bool);
1805 static tree cp_parser_nested_name_specifier
1806 (cp_parser *, bool, bool, bool, bool);
1807 static tree cp_parser_qualifying_entity
1808 (cp_parser *, bool, bool, bool, bool, bool);
1809 static tree cp_parser_postfix_expression
1810 (cp_parser *, bool, bool, bool, cp_id_kind *);
1811 static tree cp_parser_postfix_open_square_expression
1812 (cp_parser *, tree, bool);
1813 static tree cp_parser_postfix_dot_deref_expression
1814 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1815 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1816 (cp_parser *, int, bool, bool, bool *);
1817 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1818 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1819 static void cp_parser_pseudo_destructor_name
1820 (cp_parser *, tree *, tree *);
1821 static tree cp_parser_unary_expression
1822 (cp_parser *, bool, bool, cp_id_kind *);
1823 static enum tree_code cp_parser_unary_operator
1824 (cp_token *);
1825 static tree cp_parser_new_expression
1826 (cp_parser *);
1827 static VEC(tree,gc) *cp_parser_new_placement
1828 (cp_parser *);
1829 static tree cp_parser_new_type_id
1830 (cp_parser *, tree *);
1831 static cp_declarator *cp_parser_new_declarator_opt
1832 (cp_parser *);
1833 static cp_declarator *cp_parser_direct_new_declarator
1834 (cp_parser *);
1835 static VEC(tree,gc) *cp_parser_new_initializer
1836 (cp_parser *);
1837 static tree cp_parser_delete_expression
1838 (cp_parser *);
1839 static tree cp_parser_cast_expression
1840 (cp_parser *, bool, bool, cp_id_kind *);
1841 static tree cp_parser_binary_expression
1842 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1843 static tree cp_parser_question_colon_clause
1844 (cp_parser *, tree);
1845 static tree cp_parser_assignment_expression
1846 (cp_parser *, bool, cp_id_kind *);
1847 static enum tree_code cp_parser_assignment_operator_opt
1848 (cp_parser *);
1849 static tree cp_parser_expression
1850 (cp_parser *, bool, cp_id_kind *);
1851 static tree cp_parser_constant_expression
1852 (cp_parser *, bool, bool *);
1853 static tree cp_parser_builtin_offsetof
1854 (cp_parser *);
1855 static tree cp_parser_lambda_expression
1856 (cp_parser *);
1857 static void cp_parser_lambda_introducer
1858 (cp_parser *, tree);
1859 static bool cp_parser_lambda_declarator_opt
1860 (cp_parser *, tree);
1861 static void cp_parser_lambda_body
1862 (cp_parser *, tree);
1864 /* Statements [gram.stmt.stmt] */
1866 static void cp_parser_statement
1867 (cp_parser *, tree, bool, bool *);
1868 static void cp_parser_label_for_labeled_statement
1869 (cp_parser *, tree);
1870 static tree cp_parser_expression_statement
1871 (cp_parser *, tree);
1872 static tree cp_parser_compound_statement
1873 (cp_parser *, tree, bool, bool);
1874 static void cp_parser_statement_seq_opt
1875 (cp_parser *, tree);
1876 static tree cp_parser_selection_statement
1877 (cp_parser *, bool *);
1878 static tree cp_parser_condition
1879 (cp_parser *);
1880 static tree cp_parser_iteration_statement
1881 (cp_parser *);
1882 static bool cp_parser_for_init_statement
1883 (cp_parser *, tree *decl);
1884 static tree cp_parser_for
1885 (cp_parser *);
1886 static tree cp_parser_c_for
1887 (cp_parser *, tree, tree);
1888 static tree cp_parser_range_for
1889 (cp_parser *, tree, tree, tree);
1890 static void do_range_for_auto_deduction
1891 (tree, tree);
1892 static tree cp_parser_perform_range_for_lookup
1893 (tree, tree *, tree *);
1894 static tree cp_parser_range_for_member_function
1895 (tree, tree);
1896 static tree cp_parser_jump_statement
1897 (cp_parser *);
1898 static void cp_parser_declaration_statement
1899 (cp_parser *);
1901 static tree cp_parser_implicitly_scoped_statement
1902 (cp_parser *, bool *);
1903 static void cp_parser_already_scoped_statement
1904 (cp_parser *);
1906 /* Declarations [gram.dcl.dcl] */
1908 static void cp_parser_declaration_seq_opt
1909 (cp_parser *);
1910 static void cp_parser_declaration
1911 (cp_parser *);
1912 static void cp_parser_block_declaration
1913 (cp_parser *, bool);
1914 static void cp_parser_simple_declaration
1915 (cp_parser *, bool, tree *);
1916 static void cp_parser_decl_specifier_seq
1917 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1918 static tree cp_parser_storage_class_specifier_opt
1919 (cp_parser *);
1920 static tree cp_parser_function_specifier_opt
1921 (cp_parser *, cp_decl_specifier_seq *);
1922 static tree cp_parser_type_specifier
1923 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1924 int *, bool *);
1925 static tree cp_parser_simple_type_specifier
1926 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1927 static tree cp_parser_type_name
1928 (cp_parser *);
1929 static tree cp_parser_nonclass_name
1930 (cp_parser* parser);
1931 static tree cp_parser_elaborated_type_specifier
1932 (cp_parser *, bool, bool);
1933 static tree cp_parser_enum_specifier
1934 (cp_parser *);
1935 static void cp_parser_enumerator_list
1936 (cp_parser *, tree);
1937 static void cp_parser_enumerator_definition
1938 (cp_parser *, tree);
1939 static tree cp_parser_namespace_name
1940 (cp_parser *);
1941 static void cp_parser_namespace_definition
1942 (cp_parser *);
1943 static void cp_parser_namespace_body
1944 (cp_parser *);
1945 static tree cp_parser_qualified_namespace_specifier
1946 (cp_parser *);
1947 static void cp_parser_namespace_alias_definition
1948 (cp_parser *);
1949 static bool cp_parser_using_declaration
1950 (cp_parser *, bool);
1951 static void cp_parser_using_directive
1952 (cp_parser *);
1953 static tree cp_parser_alias_declaration
1954 (cp_parser *);
1955 static void cp_parser_asm_definition
1956 (cp_parser *);
1957 static void cp_parser_linkage_specification
1958 (cp_parser *);
1959 static void cp_parser_static_assert
1960 (cp_parser *, bool);
1961 static tree cp_parser_decltype
1962 (cp_parser *);
1964 /* Declarators [gram.dcl.decl] */
1966 static tree cp_parser_init_declarator
1967 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1968 static cp_declarator *cp_parser_declarator
1969 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1970 static cp_declarator *cp_parser_direct_declarator
1971 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1972 static enum tree_code cp_parser_ptr_operator
1973 (cp_parser *, tree *, cp_cv_quals *, tree *);
1974 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1975 (cp_parser *);
1976 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1977 (cp_parser *);
1978 static tree cp_parser_late_return_type_opt
1979 (cp_parser *, cp_cv_quals);
1980 static tree cp_parser_declarator_id
1981 (cp_parser *, bool);
1982 static tree cp_parser_type_id
1983 (cp_parser *);
1984 static tree cp_parser_template_type_arg
1985 (cp_parser *);
1986 static tree cp_parser_trailing_type_id (cp_parser *);
1987 static tree cp_parser_type_id_1
1988 (cp_parser *, bool, bool);
1989 static void cp_parser_type_specifier_seq
1990 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1991 static tree cp_parser_parameter_declaration_clause
1992 (cp_parser *);
1993 static tree cp_parser_parameter_declaration_list
1994 (cp_parser *, bool *);
1995 static cp_parameter_declarator *cp_parser_parameter_declaration
1996 (cp_parser *, bool, bool *);
1997 static tree cp_parser_default_argument
1998 (cp_parser *, bool);
1999 static void cp_parser_function_body
2000 (cp_parser *, bool);
2001 static tree cp_parser_initializer
2002 (cp_parser *, bool *, bool *);
2003 static tree cp_parser_initializer_clause
2004 (cp_parser *, bool *);
2005 static tree cp_parser_braced_list
2006 (cp_parser*, bool*);
2007 static VEC(constructor_elt,gc) *cp_parser_initializer_list
2008 (cp_parser *, bool *);
2010 static bool cp_parser_ctor_initializer_opt_and_function_body
2011 (cp_parser *, bool);
2013 /* Classes [gram.class] */
2015 static tree cp_parser_class_name
2016 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2017 static tree cp_parser_class_specifier
2018 (cp_parser *);
2019 static tree cp_parser_class_head
2020 (cp_parser *, bool *);
2021 static enum tag_types cp_parser_class_key
2022 (cp_parser *);
2023 static void cp_parser_member_specification_opt
2024 (cp_parser *);
2025 static void cp_parser_member_declaration
2026 (cp_parser *);
2027 static tree cp_parser_pure_specifier
2028 (cp_parser *);
2029 static tree cp_parser_constant_initializer
2030 (cp_parser *);
2032 /* Derived classes [gram.class.derived] */
2034 static tree cp_parser_base_clause
2035 (cp_parser *);
2036 static tree cp_parser_base_specifier
2037 (cp_parser *);
2039 /* Special member functions [gram.special] */
2041 static tree cp_parser_conversion_function_id
2042 (cp_parser *);
2043 static tree cp_parser_conversion_type_id
2044 (cp_parser *);
2045 static cp_declarator *cp_parser_conversion_declarator_opt
2046 (cp_parser *);
2047 static bool cp_parser_ctor_initializer_opt
2048 (cp_parser *);
2049 static void cp_parser_mem_initializer_list
2050 (cp_parser *);
2051 static tree cp_parser_mem_initializer
2052 (cp_parser *);
2053 static tree cp_parser_mem_initializer_id
2054 (cp_parser *);
2056 /* Overloading [gram.over] */
2058 static tree cp_parser_operator_function_id
2059 (cp_parser *);
2060 static tree cp_parser_operator
2061 (cp_parser *);
2063 /* Templates [gram.temp] */
2065 static void cp_parser_template_declaration
2066 (cp_parser *, bool);
2067 static tree cp_parser_template_parameter_list
2068 (cp_parser *);
2069 static tree cp_parser_template_parameter
2070 (cp_parser *, bool *, bool *);
2071 static tree cp_parser_type_parameter
2072 (cp_parser *, bool *);
2073 static tree cp_parser_template_id
2074 (cp_parser *, bool, bool, enum tag_types, bool);
2075 static tree cp_parser_template_name
2076 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2077 static tree cp_parser_template_argument_list
2078 (cp_parser *);
2079 static tree cp_parser_template_argument
2080 (cp_parser *);
2081 static void cp_parser_explicit_instantiation
2082 (cp_parser *);
2083 static void cp_parser_explicit_specialization
2084 (cp_parser *);
2086 /* Exception handling [gram.exception] */
2088 static tree cp_parser_try_block
2089 (cp_parser *);
2090 static bool cp_parser_function_try_block
2091 (cp_parser *);
2092 static void cp_parser_handler_seq
2093 (cp_parser *);
2094 static void cp_parser_handler
2095 (cp_parser *);
2096 static tree cp_parser_exception_declaration
2097 (cp_parser *);
2098 static tree cp_parser_throw_expression
2099 (cp_parser *);
2100 static tree cp_parser_exception_specification_opt
2101 (cp_parser *);
2102 static tree cp_parser_type_id_list
2103 (cp_parser *);
2105 /* GNU Extensions */
2107 static tree cp_parser_asm_specification_opt
2108 (cp_parser *);
2109 static tree cp_parser_asm_operand_list
2110 (cp_parser *);
2111 static tree cp_parser_asm_clobber_list
2112 (cp_parser *);
2113 static tree cp_parser_asm_label_list
2114 (cp_parser *);
2115 static bool cp_next_tokens_can_be_attribute_p
2116 (cp_parser *);
2117 static bool cp_next_tokens_can_be_gnu_attribute_p
2118 (cp_parser *);
2119 static bool cp_next_tokens_can_be_std_attribute_p
2120 (cp_parser *);
2121 static bool cp_nth_tokens_can_be_std_attribute_p
2122 (cp_parser *, size_t);
2123 static bool cp_nth_tokens_can_be_gnu_attribute_p
2124 (cp_parser *, size_t);
2125 static bool cp_nth_tokens_can_be_attribute_p
2126 (cp_parser *, size_t);
2127 static tree cp_parser_attributes_opt
2128 (cp_parser *);
2129 static tree cp_parser_gnu_attributes_opt
2130 (cp_parser *);
2131 static tree cp_parser_gnu_attribute_list
2132 (cp_parser *);
2133 static tree cp_parser_std_attribute
2134 (cp_parser *);
2135 static tree cp_parser_std_attribute_spec
2136 (cp_parser *);
2137 static tree cp_parser_std_attribute_spec_seq
2138 (cp_parser *);
2139 static bool cp_parser_extension_opt
2140 (cp_parser *, int *);
2141 static void cp_parser_label_declaration
2142 (cp_parser *);
2144 /* Transactional Memory Extensions */
2146 static tree cp_parser_transaction
2147 (cp_parser *, enum rid);
2148 static tree cp_parser_transaction_expression
2149 (cp_parser *, enum rid);
2150 static bool cp_parser_function_transaction
2151 (cp_parser *, enum rid);
2152 static tree cp_parser_transaction_cancel
2153 (cp_parser *);
2155 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2156 static bool cp_parser_pragma
2157 (cp_parser *, enum pragma_context);
2159 /* Objective-C++ Productions */
2161 static tree cp_parser_objc_message_receiver
2162 (cp_parser *);
2163 static tree cp_parser_objc_message_args
2164 (cp_parser *);
2165 static tree cp_parser_objc_message_expression
2166 (cp_parser *);
2167 static tree cp_parser_objc_encode_expression
2168 (cp_parser *);
2169 static tree cp_parser_objc_defs_expression
2170 (cp_parser *);
2171 static tree cp_parser_objc_protocol_expression
2172 (cp_parser *);
2173 static tree cp_parser_objc_selector_expression
2174 (cp_parser *);
2175 static tree cp_parser_objc_expression
2176 (cp_parser *);
2177 static bool cp_parser_objc_selector_p
2178 (enum cpp_ttype);
2179 static tree cp_parser_objc_selector
2180 (cp_parser *);
2181 static tree cp_parser_objc_protocol_refs_opt
2182 (cp_parser *);
2183 static void cp_parser_objc_declaration
2184 (cp_parser *, tree);
2185 static tree cp_parser_objc_statement
2186 (cp_parser *);
2187 static bool cp_parser_objc_valid_prefix_attributes
2188 (cp_parser *, tree *);
2189 static void cp_parser_objc_at_property_declaration
2190 (cp_parser *) ;
2191 static void cp_parser_objc_at_synthesize_declaration
2192 (cp_parser *) ;
2193 static void cp_parser_objc_at_dynamic_declaration
2194 (cp_parser *) ;
2195 static tree cp_parser_objc_struct_declaration
2196 (cp_parser *) ;
2198 /* Utility Routines */
2200 static tree cp_parser_lookup_name
2201 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2202 static tree cp_parser_lookup_name_simple
2203 (cp_parser *, tree, location_t);
2204 static tree cp_parser_maybe_treat_template_as_class
2205 (tree, bool);
2206 static bool cp_parser_check_declarator_template_parameters
2207 (cp_parser *, cp_declarator *, location_t);
2208 static bool cp_parser_check_template_parameters
2209 (cp_parser *, unsigned, location_t, cp_declarator *);
2210 static tree cp_parser_simple_cast_expression
2211 (cp_parser *);
2212 static tree cp_parser_global_scope_opt
2213 (cp_parser *, bool);
2214 static bool cp_parser_constructor_declarator_p
2215 (cp_parser *, bool);
2216 static tree cp_parser_function_definition_from_specifiers_and_declarator
2217 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2218 static tree cp_parser_function_definition_after_declarator
2219 (cp_parser *, bool);
2220 static void cp_parser_template_declaration_after_export
2221 (cp_parser *, bool);
2222 static void cp_parser_perform_template_parameter_access_checks
2223 (VEC (deferred_access_check,gc)*);
2224 static tree cp_parser_single_declaration
2225 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2226 static tree cp_parser_functional_cast
2227 (cp_parser *, tree);
2228 static tree cp_parser_save_member_function_body
2229 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2230 static tree cp_parser_save_nsdmi
2231 (cp_parser *);
2232 static tree cp_parser_enclosed_template_argument_list
2233 (cp_parser *);
2234 static void cp_parser_save_default_args
2235 (cp_parser *, tree);
2236 static void cp_parser_late_parsing_for_member
2237 (cp_parser *, tree);
2238 static tree cp_parser_late_parse_one_default_arg
2239 (cp_parser *, tree, tree, tree);
2240 static void cp_parser_late_parsing_nsdmi
2241 (cp_parser *, tree);
2242 static void cp_parser_late_parsing_default_args
2243 (cp_parser *, tree);
2244 static tree cp_parser_sizeof_operand
2245 (cp_parser *, enum rid);
2246 static tree cp_parser_trait_expr
2247 (cp_parser *, enum rid);
2248 static bool cp_parser_declares_only_class_p
2249 (cp_parser *);
2250 static void cp_parser_set_storage_class
2251 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2252 static void cp_parser_set_decl_spec_type
2253 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2254 static void set_and_check_decl_spec_loc
2255 (cp_decl_specifier_seq *decl_specs,
2256 cp_decl_spec ds, cp_token *);
2257 static bool cp_parser_friend_p
2258 (const cp_decl_specifier_seq *);
2259 static void cp_parser_required_error
2260 (cp_parser *, required_token, bool);
2261 static cp_token *cp_parser_require
2262 (cp_parser *, enum cpp_ttype, required_token);
2263 static cp_token *cp_parser_require_keyword
2264 (cp_parser *, enum rid, required_token);
2265 static bool cp_parser_token_starts_function_definition_p
2266 (cp_token *);
2267 static bool cp_parser_next_token_starts_class_definition_p
2268 (cp_parser *);
2269 static bool cp_parser_next_token_ends_template_argument_p
2270 (cp_parser *);
2271 static bool cp_parser_nth_token_starts_template_argument_list_p
2272 (cp_parser *, size_t);
2273 static enum tag_types cp_parser_token_is_class_key
2274 (cp_token *);
2275 static void cp_parser_check_class_key
2276 (enum tag_types, tree type);
2277 static void cp_parser_check_access_in_redeclaration
2278 (tree type, location_t location);
2279 static bool cp_parser_optional_template_keyword
2280 (cp_parser *);
2281 static void cp_parser_pre_parsed_nested_name_specifier
2282 (cp_parser *);
2283 static bool cp_parser_cache_group
2284 (cp_parser *, enum cpp_ttype, unsigned);
2285 static tree cp_parser_cache_defarg
2286 (cp_parser *parser, bool nsdmi);
2287 static void cp_parser_parse_tentatively
2288 (cp_parser *);
2289 static void cp_parser_commit_to_tentative_parse
2290 (cp_parser *);
2291 static void cp_parser_abort_tentative_parse
2292 (cp_parser *);
2293 static bool cp_parser_parse_definitely
2294 (cp_parser *);
2295 static inline bool cp_parser_parsing_tentatively
2296 (cp_parser *);
2297 static bool cp_parser_uncommitted_to_tentative_parse_p
2298 (cp_parser *);
2299 static void cp_parser_error
2300 (cp_parser *, const char *);
2301 static void cp_parser_name_lookup_error
2302 (cp_parser *, tree, tree, name_lookup_error, location_t);
2303 static bool cp_parser_simulate_error
2304 (cp_parser *);
2305 static bool cp_parser_check_type_definition
2306 (cp_parser *);
2307 static void cp_parser_check_for_definition_in_return_type
2308 (cp_declarator *, tree, location_t type_location);
2309 static void cp_parser_check_for_invalid_template_id
2310 (cp_parser *, tree, enum tag_types, location_t location);
2311 static bool cp_parser_non_integral_constant_expression
2312 (cp_parser *, non_integral_constant);
2313 static void cp_parser_diagnose_invalid_type_name
2314 (cp_parser *, tree, tree, location_t);
2315 static bool cp_parser_parse_and_diagnose_invalid_type_name
2316 (cp_parser *);
2317 static int cp_parser_skip_to_closing_parenthesis
2318 (cp_parser *, bool, bool, bool);
2319 static void cp_parser_skip_to_end_of_statement
2320 (cp_parser *);
2321 static void cp_parser_consume_semicolon_at_end_of_statement
2322 (cp_parser *);
2323 static void cp_parser_skip_to_end_of_block_or_statement
2324 (cp_parser *);
2325 static bool cp_parser_skip_to_closing_brace
2326 (cp_parser *);
2327 static void cp_parser_skip_to_end_of_template_parameter_list
2328 (cp_parser *);
2329 static void cp_parser_skip_to_pragma_eol
2330 (cp_parser*, cp_token *);
2331 static bool cp_parser_error_occurred
2332 (cp_parser *);
2333 static bool cp_parser_allow_gnu_extensions_p
2334 (cp_parser *);
2335 static bool cp_parser_is_pure_string_literal
2336 (cp_token *);
2337 static bool cp_parser_is_string_literal
2338 (cp_token *);
2339 static bool cp_parser_is_keyword
2340 (cp_token *, enum rid);
2341 static tree cp_parser_make_typename_type
2342 (cp_parser *, tree, tree, location_t location);
2343 static cp_declarator * cp_parser_make_indirect_declarator
2344 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2346 /* Returns nonzero if we are parsing tentatively. */
2348 static inline bool
2349 cp_parser_parsing_tentatively (cp_parser* parser)
2351 return parser->context->next != NULL;
2354 /* Returns nonzero if TOKEN is a string literal. */
2356 static bool
2357 cp_parser_is_pure_string_literal (cp_token* token)
2359 return (token->type == CPP_STRING ||
2360 token->type == CPP_STRING16 ||
2361 token->type == CPP_STRING32 ||
2362 token->type == CPP_WSTRING ||
2363 token->type == CPP_UTF8STRING);
2366 /* Returns nonzero if TOKEN is a string literal
2367 of a user-defined string literal. */
2369 static bool
2370 cp_parser_is_string_literal (cp_token* token)
2372 return (cp_parser_is_pure_string_literal (token) ||
2373 token->type == CPP_STRING_USERDEF ||
2374 token->type == CPP_STRING16_USERDEF ||
2375 token->type == CPP_STRING32_USERDEF ||
2376 token->type == CPP_WSTRING_USERDEF ||
2377 token->type == CPP_UTF8STRING_USERDEF);
2380 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2382 static bool
2383 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2385 return token->keyword == keyword;
2388 /* If not parsing tentatively, issue a diagnostic of the form
2389 FILE:LINE: MESSAGE before TOKEN
2390 where TOKEN is the next token in the input stream. MESSAGE
2391 (specified by the caller) is usually of the form "expected
2392 OTHER-TOKEN". */
2394 static void
2395 cp_parser_error (cp_parser* parser, const char* gmsgid)
2397 if (!cp_parser_simulate_error (parser))
2399 cp_token *token = cp_lexer_peek_token (parser->lexer);
2400 /* This diagnostic makes more sense if it is tagged to the line
2401 of the token we just peeked at. */
2402 cp_lexer_set_source_position_from_token (token);
2404 if (token->type == CPP_PRAGMA)
2406 error_at (token->location,
2407 "%<#pragma%> is not allowed here");
2408 cp_parser_skip_to_pragma_eol (parser, token);
2409 return;
2412 c_parse_error (gmsgid,
2413 /* Because c_parser_error does not understand
2414 CPP_KEYWORD, keywords are treated like
2415 identifiers. */
2416 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2417 token->u.value, token->flags);
2421 /* Issue an error about name-lookup failing. NAME is the
2422 IDENTIFIER_NODE DECL is the result of
2423 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2424 the thing that we hoped to find. */
2426 static void
2427 cp_parser_name_lookup_error (cp_parser* parser,
2428 tree name,
2429 tree decl,
2430 name_lookup_error desired,
2431 location_t location)
2433 /* If name lookup completely failed, tell the user that NAME was not
2434 declared. */
2435 if (decl == error_mark_node)
2437 if (parser->scope && parser->scope != global_namespace)
2438 error_at (location, "%<%E::%E%> has not been declared",
2439 parser->scope, name);
2440 else if (parser->scope == global_namespace)
2441 error_at (location, "%<::%E%> has not been declared", name);
2442 else if (parser->object_scope
2443 && !CLASS_TYPE_P (parser->object_scope))
2444 error_at (location, "request for member %qE in non-class type %qT",
2445 name, parser->object_scope);
2446 else if (parser->object_scope)
2447 error_at (location, "%<%T::%E%> has not been declared",
2448 parser->object_scope, name);
2449 else
2450 error_at (location, "%qE has not been declared", name);
2452 else if (parser->scope && parser->scope != global_namespace)
2454 switch (desired)
2456 case NLE_TYPE:
2457 error_at (location, "%<%E::%E%> is not a type",
2458 parser->scope, name);
2459 break;
2460 case NLE_CXX98:
2461 error_at (location, "%<%E::%E%> is not a class or namespace",
2462 parser->scope, name);
2463 break;
2464 case NLE_NOT_CXX98:
2465 error_at (location,
2466 "%<%E::%E%> is not a class, namespace, or enumeration",
2467 parser->scope, name);
2468 break;
2469 default:
2470 gcc_unreachable ();
2474 else if (parser->scope == global_namespace)
2476 switch (desired)
2478 case NLE_TYPE:
2479 error_at (location, "%<::%E%> is not a type", name);
2480 break;
2481 case NLE_CXX98:
2482 error_at (location, "%<::%E%> is not a class or namespace", name);
2483 break;
2484 case NLE_NOT_CXX98:
2485 error_at (location,
2486 "%<::%E%> is not a class, namespace, or enumeration",
2487 name);
2488 break;
2489 default:
2490 gcc_unreachable ();
2493 else
2495 switch (desired)
2497 case NLE_TYPE:
2498 error_at (location, "%qE is not a type", name);
2499 break;
2500 case NLE_CXX98:
2501 error_at (location, "%qE is not a class or namespace", name);
2502 break;
2503 case NLE_NOT_CXX98:
2504 error_at (location,
2505 "%qE is not a class, namespace, or enumeration", name);
2506 break;
2507 default:
2508 gcc_unreachable ();
2513 /* If we are parsing tentatively, remember that an error has occurred
2514 during this tentative parse. Returns true if the error was
2515 simulated; false if a message should be issued by the caller. */
2517 static bool
2518 cp_parser_simulate_error (cp_parser* parser)
2520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2522 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2523 return true;
2525 return false;
2528 /* This function is called when a type is defined. If type
2529 definitions are forbidden at this point, an error message is
2530 issued. */
2532 static bool
2533 cp_parser_check_type_definition (cp_parser* parser)
2535 /* If types are forbidden here, issue a message. */
2536 if (parser->type_definition_forbidden_message)
2538 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2539 in the message need to be interpreted. */
2540 error (parser->type_definition_forbidden_message);
2541 return false;
2543 return true;
2546 /* This function is called when the DECLARATOR is processed. The TYPE
2547 was a type defined in the decl-specifiers. If it is invalid to
2548 define a type in the decl-specifiers for DECLARATOR, an error is
2549 issued. TYPE_LOCATION is the location of TYPE and is used
2550 for error reporting. */
2552 static void
2553 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2554 tree type, location_t type_location)
2556 /* [dcl.fct] forbids type definitions in return types.
2557 Unfortunately, it's not easy to know whether or not we are
2558 processing a return type until after the fact. */
2559 while (declarator
2560 && (declarator->kind == cdk_pointer
2561 || declarator->kind == cdk_reference
2562 || declarator->kind == cdk_ptrmem))
2563 declarator = declarator->declarator;
2564 if (declarator
2565 && declarator->kind == cdk_function)
2567 error_at (type_location,
2568 "new types may not be defined in a return type");
2569 inform (type_location,
2570 "(perhaps a semicolon is missing after the definition of %qT)",
2571 type);
2575 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2576 "<" in any valid C++ program. If the next token is indeed "<",
2577 issue a message warning the user about what appears to be an
2578 invalid attempt to form a template-id. LOCATION is the location
2579 of the type-specifier (TYPE) */
2581 static void
2582 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2583 tree type,
2584 enum tag_types tag_type,
2585 location_t location)
2587 cp_token_position start = 0;
2589 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2591 if (TYPE_P (type))
2592 error_at (location, "%qT is not a template", type);
2593 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2595 if (tag_type != none_type)
2596 error_at (location, "%qE is not a class template", type);
2597 else
2598 error_at (location, "%qE is not a template", type);
2600 else
2601 error_at (location, "invalid template-id");
2602 /* Remember the location of the invalid "<". */
2603 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2604 start = cp_lexer_token_position (parser->lexer, true);
2605 /* Consume the "<". */
2606 cp_lexer_consume_token (parser->lexer);
2607 /* Parse the template arguments. */
2608 cp_parser_enclosed_template_argument_list (parser);
2609 /* Permanently remove the invalid template arguments so that
2610 this error message is not issued again. */
2611 if (start)
2612 cp_lexer_purge_tokens_after (parser->lexer, start);
2616 /* If parsing an integral constant-expression, issue an error message
2617 about the fact that THING appeared and return true. Otherwise,
2618 return false. In either case, set
2619 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2621 static bool
2622 cp_parser_non_integral_constant_expression (cp_parser *parser,
2623 non_integral_constant thing)
2625 parser->non_integral_constant_expression_p = true;
2626 if (parser->integral_constant_expression_p)
2628 if (!parser->allow_non_integral_constant_expression_p)
2630 const char *msg = NULL;
2631 switch (thing)
2633 case NIC_FLOAT:
2634 error ("floating-point literal "
2635 "cannot appear in a constant-expression");
2636 return true;
2637 case NIC_CAST:
2638 error ("a cast to a type other than an integral or "
2639 "enumeration type cannot appear in a "
2640 "constant-expression");
2641 return true;
2642 case NIC_TYPEID:
2643 error ("%<typeid%> operator "
2644 "cannot appear in a constant-expression");
2645 return true;
2646 case NIC_NCC:
2647 error ("non-constant compound literals "
2648 "cannot appear in a constant-expression");
2649 return true;
2650 case NIC_FUNC_CALL:
2651 error ("a function call "
2652 "cannot appear in a constant-expression");
2653 return true;
2654 case NIC_INC:
2655 error ("an increment "
2656 "cannot appear in a constant-expression");
2657 return true;
2658 case NIC_DEC:
2659 error ("an decrement "
2660 "cannot appear in a constant-expression");
2661 return true;
2662 case NIC_ARRAY_REF:
2663 error ("an array reference "
2664 "cannot appear in a constant-expression");
2665 return true;
2666 case NIC_ADDR_LABEL:
2667 error ("the address of a label "
2668 "cannot appear in a constant-expression");
2669 return true;
2670 case NIC_OVERLOADED:
2671 error ("calls to overloaded operators "
2672 "cannot appear in a constant-expression");
2673 return true;
2674 case NIC_ASSIGNMENT:
2675 error ("an assignment cannot appear in a constant-expression");
2676 return true;
2677 case NIC_COMMA:
2678 error ("a comma operator "
2679 "cannot appear in a constant-expression");
2680 return true;
2681 case NIC_CONSTRUCTOR:
2682 error ("a call to a constructor "
2683 "cannot appear in a constant-expression");
2684 return true;
2685 case NIC_TRANSACTION:
2686 error ("a transaction expression "
2687 "cannot appear in a constant-expression");
2688 return true;
2689 case NIC_THIS:
2690 msg = "this";
2691 break;
2692 case NIC_FUNC_NAME:
2693 msg = "__FUNCTION__";
2694 break;
2695 case NIC_PRETTY_FUNC:
2696 msg = "__PRETTY_FUNCTION__";
2697 break;
2698 case NIC_C99_FUNC:
2699 msg = "__func__";
2700 break;
2701 case NIC_VA_ARG:
2702 msg = "va_arg";
2703 break;
2704 case NIC_ARROW:
2705 msg = "->";
2706 break;
2707 case NIC_POINT:
2708 msg = ".";
2709 break;
2710 case NIC_STAR:
2711 msg = "*";
2712 break;
2713 case NIC_ADDR:
2714 msg = "&";
2715 break;
2716 case NIC_PREINCREMENT:
2717 msg = "++";
2718 break;
2719 case NIC_PREDECREMENT:
2720 msg = "--";
2721 break;
2722 case NIC_NEW:
2723 msg = "new";
2724 break;
2725 case NIC_DEL:
2726 msg = "delete";
2727 break;
2728 default:
2729 gcc_unreachable ();
2731 if (msg)
2732 error ("%qs cannot appear in a constant-expression", msg);
2733 return true;
2736 return false;
2739 /* Emit a diagnostic for an invalid type name. SCOPE is the
2740 qualifying scope (or NULL, if none) for ID. This function commits
2741 to the current active tentative parse, if any. (Otherwise, the
2742 problematic construct might be encountered again later, resulting
2743 in duplicate error messages.) LOCATION is the location of ID. */
2745 static void
2746 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2747 tree scope, tree id,
2748 location_t location)
2750 tree decl, old_scope;
2751 cp_parser_commit_to_tentative_parse (parser);
2752 /* Try to lookup the identifier. */
2753 old_scope = parser->scope;
2754 parser->scope = scope;
2755 decl = cp_parser_lookup_name_simple (parser, id, location);
2756 parser->scope = old_scope;
2757 /* If the lookup found a template-name, it means that the user forgot
2758 to specify an argument list. Emit a useful error message. */
2759 if (TREE_CODE (decl) == TEMPLATE_DECL)
2760 error_at (location,
2761 "invalid use of template-name %qE without an argument list",
2762 decl);
2763 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2764 error_at (location, "invalid use of destructor %qD as a type", id);
2765 else if (TREE_CODE (decl) == TYPE_DECL)
2766 /* Something like 'unsigned A a;' */
2767 error_at (location, "invalid combination of multiple type-specifiers");
2768 else if (!parser->scope)
2770 /* Issue an error message. */
2771 error_at (location, "%qE does not name a type", id);
2772 /* If we're in a template class, it's possible that the user was
2773 referring to a type from a base class. For example:
2775 template <typename T> struct A { typedef T X; };
2776 template <typename T> struct B : public A<T> { X x; };
2778 The user should have said "typename A<T>::X". */
2779 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2780 inform (location, "C++11 %<constexpr%> only available with "
2781 "-std=c++11 or -std=gnu++11");
2782 else if (processing_template_decl && current_class_type
2783 && TYPE_BINFO (current_class_type))
2785 tree b;
2787 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2789 b = TREE_CHAIN (b))
2791 tree base_type = BINFO_TYPE (b);
2792 if (CLASS_TYPE_P (base_type)
2793 && dependent_type_p (base_type))
2795 tree field;
2796 /* Go from a particular instantiation of the
2797 template (which will have an empty TYPE_FIELDs),
2798 to the main version. */
2799 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2800 for (field = TYPE_FIELDS (base_type);
2801 field;
2802 field = DECL_CHAIN (field))
2803 if (TREE_CODE (field) == TYPE_DECL
2804 && DECL_NAME (field) == id)
2806 inform (location,
2807 "(perhaps %<typename %T::%E%> was intended)",
2808 BINFO_TYPE (b), id);
2809 break;
2811 if (field)
2812 break;
2817 /* Here we diagnose qualified-ids where the scope is actually correct,
2818 but the identifier does not resolve to a valid type name. */
2819 else if (parser->scope != error_mark_node)
2821 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2822 error_at (location, "%qE in namespace %qE does not name a type",
2823 id, parser->scope);
2824 else if (CLASS_TYPE_P (parser->scope)
2825 && constructor_name_p (id, parser->scope))
2827 /* A<T>::A<T>() */
2828 error_at (location, "%<%T::%E%> names the constructor, not"
2829 " the type", parser->scope, id);
2830 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2831 error_at (location, "and %qT has no template constructors",
2832 parser->scope);
2834 else if (TYPE_P (parser->scope)
2835 && dependent_scope_p (parser->scope))
2836 error_at (location, "need %<typename%> before %<%T::%E%> because "
2837 "%qT is a dependent scope",
2838 parser->scope, id, parser->scope);
2839 else if (TYPE_P (parser->scope))
2840 error_at (location, "%qE in %q#T does not name a type",
2841 id, parser->scope);
2842 else
2843 gcc_unreachable ();
2847 /* Check for a common situation where a type-name should be present,
2848 but is not, and issue a sensible error message. Returns true if an
2849 invalid type-name was detected.
2851 The situation handled by this function are variable declarations of the
2852 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2853 Usually, `ID' should name a type, but if we got here it means that it
2854 does not. We try to emit the best possible error message depending on
2855 how exactly the id-expression looks like. */
2857 static bool
2858 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2860 tree id;
2861 cp_token *token = cp_lexer_peek_token (parser->lexer);
2863 /* Avoid duplicate error about ambiguous lookup. */
2864 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2866 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2867 if (next->type == CPP_NAME && next->ambiguous_p)
2868 goto out;
2871 cp_parser_parse_tentatively (parser);
2872 id = cp_parser_id_expression (parser,
2873 /*template_keyword_p=*/false,
2874 /*check_dependency_p=*/true,
2875 /*template_p=*/NULL,
2876 /*declarator_p=*/true,
2877 /*optional_p=*/false);
2878 /* If the next token is a (, this is a function with no explicit return
2879 type, i.e. constructor, destructor or conversion op. */
2880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2881 || TREE_CODE (id) == TYPE_DECL)
2883 cp_parser_abort_tentative_parse (parser);
2884 return false;
2886 if (!cp_parser_parse_definitely (parser))
2887 return false;
2889 /* Emit a diagnostic for the invalid type. */
2890 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2891 id, token->location);
2892 out:
2893 /* If we aren't in the middle of a declarator (i.e. in a
2894 parameter-declaration-clause), skip to the end of the declaration;
2895 there's no point in trying to process it. */
2896 if (!parser->in_declarator_p)
2897 cp_parser_skip_to_end_of_block_or_statement (parser);
2898 return true;
2901 /* Consume tokens up to, and including, the next non-nested closing `)'.
2902 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2903 are doing error recovery. Returns -1 if OR_COMMA is true and we
2904 found an unnested comma. */
2906 static int
2907 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2908 bool recovering,
2909 bool or_comma,
2910 bool consume_paren)
2912 unsigned paren_depth = 0;
2913 unsigned brace_depth = 0;
2914 unsigned square_depth = 0;
2916 if (recovering && !or_comma
2917 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2918 return 0;
2920 while (true)
2922 cp_token * token = cp_lexer_peek_token (parser->lexer);
2924 switch (token->type)
2926 case CPP_EOF:
2927 case CPP_PRAGMA_EOL:
2928 /* If we've run out of tokens, then there is no closing `)'. */
2929 return 0;
2931 /* This is good for lambda expression capture-lists. */
2932 case CPP_OPEN_SQUARE:
2933 ++square_depth;
2934 break;
2935 case CPP_CLOSE_SQUARE:
2936 if (!square_depth--)
2937 return 0;
2938 break;
2940 case CPP_SEMICOLON:
2941 /* This matches the processing in skip_to_end_of_statement. */
2942 if (!brace_depth)
2943 return 0;
2944 break;
2946 case CPP_OPEN_BRACE:
2947 ++brace_depth;
2948 break;
2949 case CPP_CLOSE_BRACE:
2950 if (!brace_depth--)
2951 return 0;
2952 break;
2954 case CPP_COMMA:
2955 if (recovering && or_comma && !brace_depth && !paren_depth
2956 && !square_depth)
2957 return -1;
2958 break;
2960 case CPP_OPEN_PAREN:
2961 if (!brace_depth)
2962 ++paren_depth;
2963 break;
2965 case CPP_CLOSE_PAREN:
2966 if (!brace_depth && !paren_depth--)
2968 if (consume_paren)
2969 cp_lexer_consume_token (parser->lexer);
2970 return 1;
2972 break;
2974 default:
2975 break;
2978 /* Consume the token. */
2979 cp_lexer_consume_token (parser->lexer);
2983 /* Consume tokens until we reach the end of the current statement.
2984 Normally, that will be just before consuming a `;'. However, if a
2985 non-nested `}' comes first, then we stop before consuming that. */
2987 static void
2988 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2990 unsigned nesting_depth = 0;
2992 while (true)
2994 cp_token *token = cp_lexer_peek_token (parser->lexer);
2996 switch (token->type)
2998 case CPP_EOF:
2999 case CPP_PRAGMA_EOL:
3000 /* If we've run out of tokens, stop. */
3001 return;
3003 case CPP_SEMICOLON:
3004 /* If the next token is a `;', we have reached the end of the
3005 statement. */
3006 if (!nesting_depth)
3007 return;
3008 break;
3010 case CPP_CLOSE_BRACE:
3011 /* If this is a non-nested '}', stop before consuming it.
3012 That way, when confronted with something like:
3014 { 3 + }
3016 we stop before consuming the closing '}', even though we
3017 have not yet reached a `;'. */
3018 if (nesting_depth == 0)
3019 return;
3021 /* If it is the closing '}' for a block that we have
3022 scanned, stop -- but only after consuming the token.
3023 That way given:
3025 void f g () { ... }
3026 typedef int I;
3028 we will stop after the body of the erroneously declared
3029 function, but before consuming the following `typedef'
3030 declaration. */
3031 if (--nesting_depth == 0)
3033 cp_lexer_consume_token (parser->lexer);
3034 return;
3037 case CPP_OPEN_BRACE:
3038 ++nesting_depth;
3039 break;
3041 default:
3042 break;
3045 /* Consume the token. */
3046 cp_lexer_consume_token (parser->lexer);
3050 /* This function is called at the end of a statement or declaration.
3051 If the next token is a semicolon, it is consumed; otherwise, error
3052 recovery is attempted. */
3054 static void
3055 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3057 /* Look for the trailing `;'. */
3058 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3060 /* If there is additional (erroneous) input, skip to the end of
3061 the statement. */
3062 cp_parser_skip_to_end_of_statement (parser);
3063 /* If the next token is now a `;', consume it. */
3064 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3065 cp_lexer_consume_token (parser->lexer);
3069 /* Skip tokens until we have consumed an entire block, or until we
3070 have consumed a non-nested `;'. */
3072 static void
3073 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3075 int nesting_depth = 0;
3077 while (nesting_depth >= 0)
3079 cp_token *token = cp_lexer_peek_token (parser->lexer);
3081 switch (token->type)
3083 case CPP_EOF:
3084 case CPP_PRAGMA_EOL:
3085 /* If we've run out of tokens, stop. */
3086 return;
3088 case CPP_SEMICOLON:
3089 /* Stop if this is an unnested ';'. */
3090 if (!nesting_depth)
3091 nesting_depth = -1;
3092 break;
3094 case CPP_CLOSE_BRACE:
3095 /* Stop if this is an unnested '}', or closes the outermost
3096 nesting level. */
3097 nesting_depth--;
3098 if (nesting_depth < 0)
3099 return;
3100 if (!nesting_depth)
3101 nesting_depth = -1;
3102 break;
3104 case CPP_OPEN_BRACE:
3105 /* Nest. */
3106 nesting_depth++;
3107 break;
3109 default:
3110 break;
3113 /* Consume the token. */
3114 cp_lexer_consume_token (parser->lexer);
3118 /* Skip tokens until a non-nested closing curly brace is the next
3119 token, or there are no more tokens. Return true in the first case,
3120 false otherwise. */
3122 static bool
3123 cp_parser_skip_to_closing_brace (cp_parser *parser)
3125 unsigned nesting_depth = 0;
3127 while (true)
3129 cp_token *token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3133 case CPP_EOF:
3134 case CPP_PRAGMA_EOL:
3135 /* If we've run out of tokens, stop. */
3136 return false;
3138 case CPP_CLOSE_BRACE:
3139 /* If the next token is a non-nested `}', then we have reached
3140 the end of the current block. */
3141 if (nesting_depth-- == 0)
3142 return true;
3143 break;
3145 case CPP_OPEN_BRACE:
3146 /* If it the next token is a `{', then we are entering a new
3147 block. Consume the entire block. */
3148 ++nesting_depth;
3149 break;
3151 default:
3152 break;
3155 /* Consume the token. */
3156 cp_lexer_consume_token (parser->lexer);
3160 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3161 parameter is the PRAGMA token, allowing us to purge the entire pragma
3162 sequence. */
3164 static void
3165 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3167 cp_token *token;
3169 parser->lexer->in_pragma = false;
3172 token = cp_lexer_consume_token (parser->lexer);
3173 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3175 /* Ensure that the pragma is not parsed again. */
3176 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3179 /* Require pragma end of line, resyncing with it as necessary. The
3180 arguments are as for cp_parser_skip_to_pragma_eol. */
3182 static void
3183 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3185 parser->lexer->in_pragma = false;
3186 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3190 /* This is a simple wrapper around make_typename_type. When the id is
3191 an unresolved identifier node, we can provide a superior diagnostic
3192 using cp_parser_diagnose_invalid_type_name. */
3194 static tree
3195 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3196 tree id, location_t id_location)
3198 tree result;
3199 if (TREE_CODE (id) == IDENTIFIER_NODE)
3201 result = make_typename_type (scope, id, typename_type,
3202 /*complain=*/tf_none);
3203 if (result == error_mark_node)
3204 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3205 return result;
3207 return make_typename_type (scope, id, typename_type, tf_error);
3210 /* This is a wrapper around the
3211 make_{pointer,ptrmem,reference}_declarator functions that decides
3212 which one to call based on the CODE and CLASS_TYPE arguments. The
3213 CODE argument should be one of the values returned by
3214 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3215 appertain to the pointer or reference. */
3217 static cp_declarator *
3218 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3219 cp_cv_quals cv_qualifiers,
3220 cp_declarator *target,
3221 tree attributes)
3223 if (code == ERROR_MARK)
3224 return cp_error_declarator;
3226 if (code == INDIRECT_REF)
3227 if (class_type == NULL_TREE)
3228 return make_pointer_declarator (cv_qualifiers, target, attributes);
3229 else
3230 return make_ptrmem_declarator (cv_qualifiers, class_type,
3231 target, attributes);
3232 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3233 return make_reference_declarator (cv_qualifiers, target,
3234 false, attributes);
3235 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236 return make_reference_declarator (cv_qualifiers, target,
3237 true, attributes);
3238 gcc_unreachable ();
3241 /* Create a new C++ parser. */
3243 static cp_parser *
3244 cp_parser_new (void)
3246 cp_parser *parser;
3247 cp_lexer *lexer;
3248 unsigned i;
3250 /* cp_lexer_new_main is called before doing GC allocation because
3251 cp_lexer_new_main might load a PCH file. */
3252 lexer = cp_lexer_new_main ();
3254 /* Initialize the binops_by_token so that we can get the tree
3255 directly from the token. */
3256 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3257 binops_by_token[binops[i].token_type] = binops[i];
3259 parser = ggc_alloc_cleared_cp_parser ();
3260 parser->lexer = lexer;
3261 parser->context = cp_parser_context_new (NULL);
3263 /* For now, we always accept GNU extensions. */
3264 parser->allow_gnu_extensions_p = 1;
3266 /* The `>' token is a greater-than operator, not the end of a
3267 template-id. */
3268 parser->greater_than_is_operator_p = true;
3270 parser->default_arg_ok_p = true;
3272 /* We are not parsing a constant-expression. */
3273 parser->integral_constant_expression_p = false;
3274 parser->allow_non_integral_constant_expression_p = false;
3275 parser->non_integral_constant_expression_p = false;
3277 /* Local variable names are not forbidden. */
3278 parser->local_variables_forbidden_p = false;
3280 /* We are not processing an `extern "C"' declaration. */
3281 parser->in_unbraced_linkage_specification_p = false;
3283 /* We are not processing a declarator. */
3284 parser->in_declarator_p = false;
3286 /* We are not processing a template-argument-list. */
3287 parser->in_template_argument_list_p = false;
3289 /* We are not in an iteration statement. */
3290 parser->in_statement = 0;
3292 /* We are not in a switch statement. */
3293 parser->in_switch_statement_p = false;
3295 /* We are not parsing a type-id inside an expression. */
3296 parser->in_type_id_in_expr_p = false;
3298 /* Declarations aren't implicitly extern "C". */
3299 parser->implicit_extern_c = false;
3301 /* String literals should be translated to the execution character set. */
3302 parser->translate_strings_p = true;
3304 /* We are not parsing a function body. */
3305 parser->in_function_body = false;
3307 /* We can correct until told otherwise. */
3308 parser->colon_corrects_to_scope_p = true;
3310 /* The unparsed function queue is empty. */
3311 push_unparsed_function_queues (parser);
3313 /* There are no classes being defined. */
3314 parser->num_classes_being_defined = 0;
3316 /* No template parameters apply. */
3317 parser->num_template_parameter_lists = 0;
3319 return parser;
3322 /* Create a cp_lexer structure which will emit the tokens in CACHE
3323 and push it onto the parser's lexer stack. This is used for delayed
3324 parsing of in-class method bodies and default arguments, and should
3325 not be confused with tentative parsing. */
3326 static void
3327 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3330 lexer->next = parser->lexer;
3331 parser->lexer = lexer;
3333 /* Move the current source position to that of the first token in the
3334 new lexer. */
3335 cp_lexer_set_source_position_from_token (lexer->next_token);
3338 /* Pop the top lexer off the parser stack. This is never used for the
3339 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3340 static void
3341 cp_parser_pop_lexer (cp_parser *parser)
3343 cp_lexer *lexer = parser->lexer;
3344 parser->lexer = lexer->next;
3345 cp_lexer_destroy (lexer);
3347 /* Put the current source position back where it was before this
3348 lexer was pushed. */
3349 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3352 /* Lexical conventions [gram.lex] */
3354 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3355 identifier. */
3357 static tree
3358 cp_parser_identifier (cp_parser* parser)
3360 cp_token *token;
3362 /* Look for the identifier. */
3363 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3364 /* Return the value. */
3365 return token ? token->u.value : error_mark_node;
3368 /* Parse a sequence of adjacent string constants. Returns a
3369 TREE_STRING representing the combined, nul-terminated string
3370 constant. If TRANSLATE is true, translate the string to the
3371 execution character set. If WIDE_OK is true, a wide string is
3372 invalid here.
3374 C++98 [lex.string] says that if a narrow string literal token is
3375 adjacent to a wide string literal token, the behavior is undefined.
3376 However, C99 6.4.5p4 says that this results in a wide string literal.
3377 We follow C99 here, for consistency with the C front end.
3379 This code is largely lifted from lex_string() in c-lex.c.
3381 FUTURE: ObjC++ will need to handle @-strings here. */
3382 static tree
3383 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 tree value;
3386 size_t count;
3387 struct obstack str_ob;
3388 cpp_string str, istr, *strs;
3389 cp_token *tok;
3390 enum cpp_ttype type, curr_type;
3391 int have_suffix_p = 0;
3392 tree string_tree;
3393 tree suffix_id = NULL_TREE;
3394 bool curr_tok_is_userdef_p = false;
3396 tok = cp_lexer_peek_token (parser->lexer);
3397 if (!cp_parser_is_string_literal (tok))
3399 cp_parser_error (parser, "expected string-literal");
3400 return error_mark_node;
3403 if (cpp_userdef_string_p (tok->type))
3405 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3406 curr_type = cpp_userdef_string_remove_type (tok->type);
3407 curr_tok_is_userdef_p = true;
3409 else
3411 string_tree = tok->u.value;
3412 curr_type = tok->type;
3414 type = curr_type;
3416 /* Try to avoid the overhead of creating and destroying an obstack
3417 for the common case of just one string. */
3418 if (!cp_parser_is_string_literal
3419 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421 cp_lexer_consume_token (parser->lexer);
3423 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3424 str.len = TREE_STRING_LENGTH (string_tree);
3425 count = 1;
3427 if (curr_tok_is_userdef_p)
3429 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3430 have_suffix_p = 1;
3431 curr_type = cpp_userdef_string_remove_type (tok->type);
3433 else
3434 curr_type = tok->type;
3436 strs = &str;
3438 else
3440 gcc_obstack_init (&str_ob);
3441 count = 0;
3445 cp_lexer_consume_token (parser->lexer);
3446 count++;
3447 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3448 str.len = TREE_STRING_LENGTH (string_tree);
3450 if (curr_tok_is_userdef_p)
3452 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3453 if (have_suffix_p == 0)
3455 suffix_id = curr_suffix_id;
3456 have_suffix_p = 1;
3458 else if (have_suffix_p == 1
3459 && curr_suffix_id != suffix_id)
3461 error ("inconsistent user-defined literal suffixes"
3462 " %qD and %qD in string literal",
3463 suffix_id, curr_suffix_id);
3464 have_suffix_p = -1;
3466 curr_type = cpp_userdef_string_remove_type (tok->type);
3468 else
3469 curr_type = tok->type;
3471 if (type != curr_type)
3473 if (type == CPP_STRING)
3474 type = curr_type;
3475 else if (curr_type != CPP_STRING)
3476 error_at (tok->location,
3477 "unsupported non-standard concatenation "
3478 "of string literals");
3481 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483 tok = cp_lexer_peek_token (parser->lexer);
3484 if (cpp_userdef_string_p (tok->type))
3486 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3487 curr_type = cpp_userdef_string_remove_type (tok->type);
3488 curr_tok_is_userdef_p = true;
3490 else
3492 string_tree = tok->u.value;
3493 curr_type = tok->type;
3494 curr_tok_is_userdef_p = false;
3497 while (cp_parser_is_string_literal (tok));
3499 strs = (cpp_string *) obstack_finish (&str_ob);
3502 if (type != CPP_STRING && !wide_ok)
3504 cp_parser_error (parser, "a wide string is invalid in this context");
3505 type = CPP_STRING;
3508 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3509 (parse_in, strs, count, &istr, type))
3511 value = build_string (istr.len, (const char *)istr.text);
3512 free (CONST_CAST (unsigned char *, istr.text));
3514 switch (type)
3516 default:
3517 case CPP_STRING:
3518 case CPP_UTF8STRING:
3519 TREE_TYPE (value) = char_array_type_node;
3520 break;
3521 case CPP_STRING16:
3522 TREE_TYPE (value) = char16_array_type_node;
3523 break;
3524 case CPP_STRING32:
3525 TREE_TYPE (value) = char32_array_type_node;
3526 break;
3527 case CPP_WSTRING:
3528 TREE_TYPE (value) = wchar_array_type_node;
3529 break;
3532 value = fix_string_type (value);
3534 if (have_suffix_p)
3536 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3537 tok->u.value = literal;
3538 return cp_parser_userdef_string_literal (tok);
3541 else
3542 /* cpp_interpret_string has issued an error. */
3543 value = error_mark_node;
3545 if (count > 1)
3546 obstack_free (&str_ob, 0);
3548 return value;
3551 /* Look up a literal operator with the name and the exact arguments. */
3553 static tree
3554 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 tree decl, fns;
3557 decl = lookup_name (name);
3558 if (!decl || !is_overloaded_fn (decl))
3559 return error_mark_node;
3561 for (fns = decl; fns; fns = OVL_NEXT (fns))
3563 unsigned int ix;
3564 bool found = true;
3565 tree fn = OVL_CURRENT (fns);
3566 tree argtypes = NULL_TREE;
3567 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3568 if (argtypes != NULL_TREE)
3570 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3571 ++ix, argtypes = TREE_CHAIN (argtypes))
3573 tree targ = TREE_VALUE (argtypes);
3574 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3575 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3576 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3577 if ((ptr || arr || !same_type_p (targ, tparm))
3578 && (!ptr || !arr
3579 || !same_type_p (TREE_TYPE (targ),
3580 TREE_TYPE (tparm))))
3581 found = false;
3583 if (found
3584 && ix == VEC_length (tree, args)
3585 /* May be this should be sufficient_parms_p instead,
3586 depending on how exactly should user-defined literals
3587 work in presence of default arguments on the literal
3588 operator parameters. */
3589 && argtypes == void_list_node)
3590 return fn;
3594 return error_mark_node;
3597 /* Parse a user-defined char constant. Returns a call to a user-defined
3598 literal operator taking the character as an argument. */
3600 static tree
3601 cp_parser_userdef_char_literal (cp_parser *parser)
3603 cp_token *token = cp_lexer_consume_token (parser->lexer);
3604 tree literal = token->u.value;
3605 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3606 tree value = USERDEF_LITERAL_VALUE (literal);
3607 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3608 tree decl, result;
3610 /* Build up a call to the user-defined operator */
3611 /* Lookup the name we got back from the id-expression. */
3612 VEC(tree,gc) *args = make_tree_vector ();
3613 VEC_safe_push (tree, gc, args, value);
3614 decl = lookup_literal_operator (name, args);
3615 if (!decl || decl == error_mark_node)
3617 error ("unable to find character literal operator %qD with %qT argument",
3618 name, TREE_TYPE (value));
3619 release_tree_vector (args);
3620 return error_mark_node;
3622 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3623 release_tree_vector (args);
3624 if (result != error_mark_node)
3625 return result;
3627 error ("unable to find character literal operator %qD with %qT argument",
3628 name, TREE_TYPE (value));
3629 return error_mark_node;
3632 /* A subroutine of cp_parser_userdef_numeric_literal to
3633 create a char... template parameter pack from a string node. */
3635 static tree
3636 make_char_string_pack (tree value)
3638 tree charvec;
3639 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3640 const char *str = TREE_STRING_POINTER (value);
3641 int i, len = TREE_STRING_LENGTH (value) - 1;
3642 tree argvec = make_tree_vec (1);
3644 /* Fill in CHARVEC with all of the parameters. */
3645 charvec = make_tree_vec (len);
3646 for (i = 0; i < len; ++i)
3647 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3649 /* Build the argument packs. */
3650 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3651 TREE_TYPE (argpack) = char_type_node;
3653 TREE_VEC_ELT (argvec, 0) = argpack;
3655 return argvec;
3658 /* Parse a user-defined numeric constant. returns a call to a user-defined
3659 literal operator. */
3661 static tree
3662 cp_parser_userdef_numeric_literal (cp_parser *parser)
3664 cp_token *token = cp_lexer_consume_token (parser->lexer);
3665 tree literal = token->u.value;
3666 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3667 tree value = USERDEF_LITERAL_VALUE (literal);
3668 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3669 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3670 tree decl, result;
3671 VEC(tree,gc) *args;
3673 /* Look for a literal operator taking the exact type of numeric argument
3674 as the literal value. */
3675 args = make_tree_vector ();
3676 VEC_safe_push (tree, gc, args, value);
3677 decl = lookup_literal_operator (name, args);
3678 if (decl && decl != error_mark_node)
3680 result = finish_call_expr (decl, &args, false, true, tf_none);
3681 if (result != error_mark_node)
3683 release_tree_vector (args);
3684 return result;
3687 release_tree_vector (args);
3689 /* If the numeric argument didn't work, look for a raw literal
3690 operator taking a const char* argument consisting of the number
3691 in string format. */
3692 args = make_tree_vector ();
3693 VEC_safe_push (tree, gc, args, num_string);
3694 decl = lookup_literal_operator (name, args);
3695 if (decl && decl != error_mark_node)
3697 result = finish_call_expr (decl, &args, false, true, tf_none);
3698 if (result != error_mark_node)
3700 release_tree_vector (args);
3701 return result;
3704 release_tree_vector (args);
3706 /* If the raw literal didn't work, look for a non-type template
3707 function with parameter pack char.... Call the function with
3708 template parameter characters representing the number. */
3709 args = make_tree_vector ();
3710 decl = lookup_literal_operator (name, args);
3711 if (decl && decl != error_mark_node)
3713 tree tmpl_args = make_char_string_pack (num_string);
3714 decl = lookup_template_function (decl, tmpl_args);
3715 result = finish_call_expr (decl, &args, false, true, tf_none);
3716 if (result != error_mark_node)
3718 release_tree_vector (args);
3719 return result;
3722 release_tree_vector (args);
3724 error ("unable to find numeric literal operator %qD", name);
3725 return error_mark_node;
3728 /* Parse a user-defined string constant. Returns a call to a user-defined
3729 literal operator taking a character pointer and the length of the string
3730 as arguments. */
3732 static tree
3733 cp_parser_userdef_string_literal (cp_token *token)
3735 tree literal = token->u.value;
3736 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3737 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3738 tree value = USERDEF_LITERAL_VALUE (literal);
3739 int len = TREE_STRING_LENGTH (value)
3740 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3741 tree decl, result;
3743 /* Build up a call to the user-defined operator */
3744 /* Lookup the name we got back from the id-expression. */
3745 VEC(tree,gc) *args = make_tree_vector ();
3746 VEC_safe_push (tree, gc, args, value);
3747 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3748 decl = lookup_name (name);
3749 if (!decl || decl == error_mark_node)
3751 error ("unable to find string literal operator %qD", name);
3752 release_tree_vector (args);
3753 return error_mark_node;
3755 result = finish_call_expr (decl, &args, false, true, tf_none);
3756 release_tree_vector (args);
3757 if (result != error_mark_node)
3758 return result;
3760 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3761 name, TREE_TYPE (value), size_type_node);
3762 return error_mark_node;
3766 /* Basic concepts [gram.basic] */
3768 /* Parse a translation-unit.
3770 translation-unit:
3771 declaration-seq [opt]
3773 Returns TRUE if all went well. */
3775 static bool
3776 cp_parser_translation_unit (cp_parser* parser)
3778 /* The address of the first non-permanent object on the declarator
3779 obstack. */
3780 static void *declarator_obstack_base;
3782 bool success;
3784 /* Create the declarator obstack, if necessary. */
3785 if (!cp_error_declarator)
3787 gcc_obstack_init (&declarator_obstack);
3788 /* Create the error declarator. */
3789 cp_error_declarator = make_declarator (cdk_error);
3790 /* Create the empty parameter list. */
3791 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3792 /* Remember where the base of the declarator obstack lies. */
3793 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3796 cp_parser_declaration_seq_opt (parser);
3798 /* If there are no tokens left then all went well. */
3799 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3801 /* Get rid of the token array; we don't need it any more. */
3802 cp_lexer_destroy (parser->lexer);
3803 parser->lexer = NULL;
3805 /* This file might have been a context that's implicitly extern
3806 "C". If so, pop the lang context. (Only relevant for PCH.) */
3807 if (parser->implicit_extern_c)
3809 pop_lang_context ();
3810 parser->implicit_extern_c = false;
3813 /* Finish up. */
3814 finish_translation_unit ();
3816 success = true;
3818 else
3820 cp_parser_error (parser, "expected declaration");
3821 success = false;
3824 /* Make sure the declarator obstack was fully cleaned up. */
3825 gcc_assert (obstack_next_free (&declarator_obstack)
3826 == declarator_obstack_base);
3828 /* All went well. */
3829 return success;
3832 /* Expressions [gram.expr] */
3834 /* Parse a primary-expression.
3836 primary-expression:
3837 literal
3838 this
3839 ( expression )
3840 id-expression
3842 GNU Extensions:
3844 primary-expression:
3845 ( compound-statement )
3846 __builtin_va_arg ( assignment-expression , type-id )
3847 __builtin_offsetof ( type-id , offsetof-expression )
3849 C++ Extensions:
3850 __has_nothrow_assign ( type-id )
3851 __has_nothrow_constructor ( type-id )
3852 __has_nothrow_copy ( type-id )
3853 __has_trivial_assign ( type-id )
3854 __has_trivial_constructor ( type-id )
3855 __has_trivial_copy ( type-id )
3856 __has_trivial_destructor ( type-id )
3857 __has_virtual_destructor ( type-id )
3858 __is_abstract ( type-id )
3859 __is_base_of ( type-id , type-id )
3860 __is_class ( type-id )
3861 __is_convertible_to ( type-id , type-id )
3862 __is_empty ( type-id )
3863 __is_enum ( type-id )
3864 __is_final ( type-id )
3865 __is_literal_type ( type-id )
3866 __is_pod ( type-id )
3867 __is_polymorphic ( type-id )
3868 __is_std_layout ( type-id )
3869 __is_trivial ( type-id )
3870 __is_union ( type-id )
3872 Objective-C++ Extension:
3874 primary-expression:
3875 objc-expression
3877 literal:
3878 __null
3880 ADDRESS_P is true iff this expression was immediately preceded by
3881 "&" and therefore might denote a pointer-to-member. CAST_P is true
3882 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3883 true iff this expression is a template argument.
3885 Returns a representation of the expression. Upon return, *IDK
3886 indicates what kind of id-expression (if any) was present. */
3888 static tree
3889 cp_parser_primary_expression (cp_parser *parser,
3890 bool address_p,
3891 bool cast_p,
3892 bool template_arg_p,
3893 cp_id_kind *idk)
3895 cp_token *token = NULL;
3897 /* Assume the primary expression is not an id-expression. */
3898 *idk = CP_ID_KIND_NONE;
3900 /* Peek at the next token. */
3901 token = cp_lexer_peek_token (parser->lexer);
3902 switch (token->type)
3904 /* literal:
3905 integer-literal
3906 character-literal
3907 floating-literal
3908 string-literal
3909 boolean-literal
3910 pointer-literal
3911 user-defined-literal */
3912 case CPP_CHAR:
3913 case CPP_CHAR16:
3914 case CPP_CHAR32:
3915 case CPP_WCHAR:
3916 case CPP_NUMBER:
3917 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3918 return cp_parser_userdef_numeric_literal (parser);
3919 token = cp_lexer_consume_token (parser->lexer);
3920 if (TREE_CODE (token->u.value) == FIXED_CST)
3922 error_at (token->location,
3923 "fixed-point types not supported in C++");
3924 return error_mark_node;
3926 /* Floating-point literals are only allowed in an integral
3927 constant expression if they are cast to an integral or
3928 enumeration type. */
3929 if (TREE_CODE (token->u.value) == REAL_CST
3930 && parser->integral_constant_expression_p
3931 && pedantic)
3933 /* CAST_P will be set even in invalid code like "int(2.7 +
3934 ...)". Therefore, we have to check that the next token
3935 is sure to end the cast. */
3936 if (cast_p)
3938 cp_token *next_token;
3940 next_token = cp_lexer_peek_token (parser->lexer);
3941 if (/* The comma at the end of an
3942 enumerator-definition. */
3943 next_token->type != CPP_COMMA
3944 /* The curly brace at the end of an enum-specifier. */
3945 && next_token->type != CPP_CLOSE_BRACE
3946 /* The end of a statement. */
3947 && next_token->type != CPP_SEMICOLON
3948 /* The end of the cast-expression. */
3949 && next_token->type != CPP_CLOSE_PAREN
3950 /* The end of an array bound. */
3951 && next_token->type != CPP_CLOSE_SQUARE
3952 /* The closing ">" in a template-argument-list. */
3953 && (next_token->type != CPP_GREATER
3954 || parser->greater_than_is_operator_p)
3955 /* C++0x only: A ">>" treated like two ">" tokens,
3956 in a template-argument-list. */
3957 && (next_token->type != CPP_RSHIFT
3958 || (cxx_dialect == cxx98)
3959 || parser->greater_than_is_operator_p))
3960 cast_p = false;
3963 /* If we are within a cast, then the constraint that the
3964 cast is to an integral or enumeration type will be
3965 checked at that point. If we are not within a cast, then
3966 this code is invalid. */
3967 if (!cast_p)
3968 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3970 return token->u.value;
3972 case CPP_CHAR_USERDEF:
3973 case CPP_CHAR16_USERDEF:
3974 case CPP_CHAR32_USERDEF:
3975 case CPP_WCHAR_USERDEF:
3976 return cp_parser_userdef_char_literal (parser);
3978 case CPP_STRING:
3979 case CPP_STRING16:
3980 case CPP_STRING32:
3981 case CPP_WSTRING:
3982 case CPP_UTF8STRING:
3983 case CPP_STRING_USERDEF:
3984 case CPP_STRING16_USERDEF:
3985 case CPP_STRING32_USERDEF:
3986 case CPP_WSTRING_USERDEF:
3987 case CPP_UTF8STRING_USERDEF:
3988 /* ??? Should wide strings be allowed when parser->translate_strings_p
3989 is false (i.e. in attributes)? If not, we can kill the third
3990 argument to cp_parser_string_literal. */
3991 return cp_parser_string_literal (parser,
3992 parser->translate_strings_p,
3993 true);
3995 case CPP_OPEN_PAREN:
3997 tree expr;
3998 bool saved_greater_than_is_operator_p;
4000 /* Consume the `('. */
4001 cp_lexer_consume_token (parser->lexer);
4002 /* Within a parenthesized expression, a `>' token is always
4003 the greater-than operator. */
4004 saved_greater_than_is_operator_p
4005 = parser->greater_than_is_operator_p;
4006 parser->greater_than_is_operator_p = true;
4007 /* If we see `( { ' then we are looking at the beginning of
4008 a GNU statement-expression. */
4009 if (cp_parser_allow_gnu_extensions_p (parser)
4010 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4012 /* Statement-expressions are not allowed by the standard. */
4013 pedwarn (token->location, OPT_Wpedantic,
4014 "ISO C++ forbids braced-groups within expressions");
4016 /* And they're not allowed outside of a function-body; you
4017 cannot, for example, write:
4019 int i = ({ int j = 3; j + 1; });
4021 at class or namespace scope. */
4022 if (!parser->in_function_body
4023 || parser->in_template_argument_list_p)
4025 error_at (token->location,
4026 "statement-expressions are not allowed outside "
4027 "functions nor in template-argument lists");
4028 cp_parser_skip_to_end_of_block_or_statement (parser);
4029 expr = error_mark_node;
4031 else
4033 /* Start the statement-expression. */
4034 expr = begin_stmt_expr ();
4035 /* Parse the compound-statement. */
4036 cp_parser_compound_statement (parser, expr, false, false);
4037 /* Finish up. */
4038 expr = finish_stmt_expr (expr, false);
4041 else
4043 /* Parse the parenthesized expression. */
4044 expr = cp_parser_expression (parser, cast_p, idk);
4045 /* Let the front end know that this expression was
4046 enclosed in parentheses. This matters in case, for
4047 example, the expression is of the form `A::B', since
4048 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4049 not. */
4050 finish_parenthesized_expr (expr);
4051 /* DR 705: Wrapping an unqualified name in parentheses
4052 suppresses arg-dependent lookup. We want to pass back
4053 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4054 (c++/37862), but none of the others. */
4055 if (*idk != CP_ID_KIND_QUALIFIED)
4056 *idk = CP_ID_KIND_NONE;
4058 /* The `>' token might be the end of a template-id or
4059 template-parameter-list now. */
4060 parser->greater_than_is_operator_p
4061 = saved_greater_than_is_operator_p;
4062 /* Consume the `)'. */
4063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4064 cp_parser_skip_to_end_of_statement (parser);
4066 return expr;
4069 case CPP_OPEN_SQUARE:
4070 if (c_dialect_objc ())
4071 /* We have an Objective-C++ message. */
4072 return cp_parser_objc_expression (parser);
4074 tree lam = cp_parser_lambda_expression (parser);
4075 /* Don't warn about a failed tentative parse. */
4076 if (cp_parser_error_occurred (parser))
4077 return error_mark_node;
4078 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4079 return lam;
4082 case CPP_OBJC_STRING:
4083 if (c_dialect_objc ())
4084 /* We have an Objective-C++ string literal. */
4085 return cp_parser_objc_expression (parser);
4086 cp_parser_error (parser, "expected primary-expression");
4087 return error_mark_node;
4089 case CPP_KEYWORD:
4090 switch (token->keyword)
4092 /* These two are the boolean literals. */
4093 case RID_TRUE:
4094 cp_lexer_consume_token (parser->lexer);
4095 return boolean_true_node;
4096 case RID_FALSE:
4097 cp_lexer_consume_token (parser->lexer);
4098 return boolean_false_node;
4100 /* The `__null' literal. */
4101 case RID_NULL:
4102 cp_lexer_consume_token (parser->lexer);
4103 return null_node;
4105 /* The `nullptr' literal. */
4106 case RID_NULLPTR:
4107 cp_lexer_consume_token (parser->lexer);
4108 return nullptr_node;
4110 /* Recognize the `this' keyword. */
4111 case RID_THIS:
4112 cp_lexer_consume_token (parser->lexer);
4113 if (parser->local_variables_forbidden_p)
4115 error_at (token->location,
4116 "%<this%> may not be used in this context");
4117 return error_mark_node;
4119 /* Pointers cannot appear in constant-expressions. */
4120 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4121 return error_mark_node;
4122 return finish_this_expr ();
4124 /* The `operator' keyword can be the beginning of an
4125 id-expression. */
4126 case RID_OPERATOR:
4127 goto id_expression;
4129 case RID_FUNCTION_NAME:
4130 case RID_PRETTY_FUNCTION_NAME:
4131 case RID_C99_FUNCTION_NAME:
4133 non_integral_constant name;
4135 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4136 __func__ are the names of variables -- but they are
4137 treated specially. Therefore, they are handled here,
4138 rather than relying on the generic id-expression logic
4139 below. Grammatically, these names are id-expressions.
4141 Consume the token. */
4142 token = cp_lexer_consume_token (parser->lexer);
4144 switch (token->keyword)
4146 case RID_FUNCTION_NAME:
4147 name = NIC_FUNC_NAME;
4148 break;
4149 case RID_PRETTY_FUNCTION_NAME:
4150 name = NIC_PRETTY_FUNC;
4151 break;
4152 case RID_C99_FUNCTION_NAME:
4153 name = NIC_C99_FUNC;
4154 break;
4155 default:
4156 gcc_unreachable ();
4159 if (cp_parser_non_integral_constant_expression (parser, name))
4160 return error_mark_node;
4162 /* Look up the name. */
4163 return finish_fname (token->u.value);
4166 case RID_VA_ARG:
4168 tree expression;
4169 tree type;
4170 source_location type_location;
4172 /* The `__builtin_va_arg' construct is used to handle
4173 `va_arg'. Consume the `__builtin_va_arg' token. */
4174 cp_lexer_consume_token (parser->lexer);
4175 /* Look for the opening `('. */
4176 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4177 /* Now, parse the assignment-expression. */
4178 expression = cp_parser_assignment_expression (parser,
4179 /*cast_p=*/false, NULL);
4180 /* Look for the `,'. */
4181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4182 type_location = cp_lexer_peek_token (parser->lexer)->location;
4183 /* Parse the type-id. */
4184 type = cp_parser_type_id (parser);
4185 /* Look for the closing `)'. */
4186 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4187 /* Using `va_arg' in a constant-expression is not
4188 allowed. */
4189 if (cp_parser_non_integral_constant_expression (parser,
4190 NIC_VA_ARG))
4191 return error_mark_node;
4192 return build_x_va_arg (type_location, expression, type);
4195 case RID_OFFSETOF:
4196 return cp_parser_builtin_offsetof (parser);
4198 case RID_HAS_NOTHROW_ASSIGN:
4199 case RID_HAS_NOTHROW_CONSTRUCTOR:
4200 case RID_HAS_NOTHROW_COPY:
4201 case RID_HAS_TRIVIAL_ASSIGN:
4202 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4203 case RID_HAS_TRIVIAL_COPY:
4204 case RID_HAS_TRIVIAL_DESTRUCTOR:
4205 case RID_HAS_VIRTUAL_DESTRUCTOR:
4206 case RID_IS_ABSTRACT:
4207 case RID_IS_BASE_OF:
4208 case RID_IS_CLASS:
4209 case RID_IS_CONVERTIBLE_TO:
4210 case RID_IS_EMPTY:
4211 case RID_IS_ENUM:
4212 case RID_IS_FINAL:
4213 case RID_IS_LITERAL_TYPE:
4214 case RID_IS_POD:
4215 case RID_IS_POLYMORPHIC:
4216 case RID_IS_STD_LAYOUT:
4217 case RID_IS_TRIVIAL:
4218 case RID_IS_UNION:
4219 return cp_parser_trait_expr (parser, token->keyword);
4221 /* Objective-C++ expressions. */
4222 case RID_AT_ENCODE:
4223 case RID_AT_PROTOCOL:
4224 case RID_AT_SELECTOR:
4225 return cp_parser_objc_expression (parser);
4227 case RID_TEMPLATE:
4228 if (parser->in_function_body
4229 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4230 == CPP_LESS))
4232 error_at (token->location,
4233 "a template declaration cannot appear at block scope");
4234 cp_parser_skip_to_end_of_block_or_statement (parser);
4235 return error_mark_node;
4237 default:
4238 cp_parser_error (parser, "expected primary-expression");
4239 return error_mark_node;
4242 /* An id-expression can start with either an identifier, a
4243 `::' as the beginning of a qualified-id, or the "operator"
4244 keyword. */
4245 case CPP_NAME:
4246 case CPP_SCOPE:
4247 case CPP_TEMPLATE_ID:
4248 case CPP_NESTED_NAME_SPECIFIER:
4250 tree id_expression;
4251 tree decl;
4252 const char *error_msg;
4253 bool template_p;
4254 bool done;
4255 cp_token *id_expr_token;
4257 id_expression:
4258 /* Parse the id-expression. */
4259 id_expression
4260 = cp_parser_id_expression (parser,
4261 /*template_keyword_p=*/false,
4262 /*check_dependency_p=*/true,
4263 &template_p,
4264 /*declarator_p=*/false,
4265 /*optional_p=*/false);
4266 if (id_expression == error_mark_node)
4267 return error_mark_node;
4268 id_expr_token = token;
4269 token = cp_lexer_peek_token (parser->lexer);
4270 done = (token->type != CPP_OPEN_SQUARE
4271 && token->type != CPP_OPEN_PAREN
4272 && token->type != CPP_DOT
4273 && token->type != CPP_DEREF
4274 && token->type != CPP_PLUS_PLUS
4275 && token->type != CPP_MINUS_MINUS);
4276 /* If we have a template-id, then no further lookup is
4277 required. If the template-id was for a template-class, we
4278 will sometimes have a TYPE_DECL at this point. */
4279 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4280 || TREE_CODE (id_expression) == TYPE_DECL)
4281 decl = id_expression;
4282 /* Look up the name. */
4283 else
4285 tree ambiguous_decls;
4287 /* If we already know that this lookup is ambiguous, then
4288 we've already issued an error message; there's no reason
4289 to check again. */
4290 if (id_expr_token->type == CPP_NAME
4291 && id_expr_token->ambiguous_p)
4293 cp_parser_simulate_error (parser);
4294 return error_mark_node;
4297 decl = cp_parser_lookup_name (parser, id_expression,
4298 none_type,
4299 template_p,
4300 /*is_namespace=*/false,
4301 /*check_dependency=*/true,
4302 &ambiguous_decls,
4303 id_expr_token->location);
4304 /* If the lookup was ambiguous, an error will already have
4305 been issued. */
4306 if (ambiguous_decls)
4307 return error_mark_node;
4309 /* In Objective-C++, we may have an Objective-C 2.0
4310 dot-syntax for classes here. */
4311 if (c_dialect_objc ()
4312 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4313 && TREE_CODE (decl) == TYPE_DECL
4314 && objc_is_class_name (decl))
4316 tree component;
4317 cp_lexer_consume_token (parser->lexer);
4318 component = cp_parser_identifier (parser);
4319 if (component == error_mark_node)
4320 return error_mark_node;
4322 return objc_build_class_component_ref (id_expression, component);
4325 /* In Objective-C++, an instance variable (ivar) may be preferred
4326 to whatever cp_parser_lookup_name() found. */
4327 decl = objc_lookup_ivar (decl, id_expression);
4329 /* If name lookup gives us a SCOPE_REF, then the
4330 qualifying scope was dependent. */
4331 if (TREE_CODE (decl) == SCOPE_REF)
4333 /* At this point, we do not know if DECL is a valid
4334 integral constant expression. We assume that it is
4335 in fact such an expression, so that code like:
4337 template <int N> struct A {
4338 int a[B<N>::i];
4341 is accepted. At template-instantiation time, we
4342 will check that B<N>::i is actually a constant. */
4343 return decl;
4345 /* Check to see if DECL is a local variable in a context
4346 where that is forbidden. */
4347 if (parser->local_variables_forbidden_p
4348 && local_variable_p (decl))
4350 /* It might be that we only found DECL because we are
4351 trying to be generous with pre-ISO scoping rules.
4352 For example, consider:
4354 int i;
4355 void g() {
4356 for (int i = 0; i < 10; ++i) {}
4357 extern void f(int j = i);
4360 Here, name look up will originally find the out
4361 of scope `i'. We need to issue a warning message,
4362 but then use the global `i'. */
4363 decl = check_for_out_of_scope_variable (decl);
4364 if (local_variable_p (decl))
4366 error_at (id_expr_token->location,
4367 "local variable %qD may not appear in this context",
4368 decl);
4369 return error_mark_node;
4374 decl = (finish_id_expression
4375 (id_expression, decl, parser->scope,
4376 idk,
4377 parser->integral_constant_expression_p,
4378 parser->allow_non_integral_constant_expression_p,
4379 &parser->non_integral_constant_expression_p,
4380 template_p, done, address_p,
4381 template_arg_p,
4382 &error_msg,
4383 id_expr_token->location));
4384 if (error_msg)
4385 cp_parser_error (parser, error_msg);
4386 return decl;
4389 /* Anything else is an error. */
4390 default:
4391 cp_parser_error (parser, "expected primary-expression");
4392 return error_mark_node;
4396 /* Parse an id-expression.
4398 id-expression:
4399 unqualified-id
4400 qualified-id
4402 qualified-id:
4403 :: [opt] nested-name-specifier template [opt] unqualified-id
4404 :: identifier
4405 :: operator-function-id
4406 :: template-id
4408 Return a representation of the unqualified portion of the
4409 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4410 a `::' or nested-name-specifier.
4412 Often, if the id-expression was a qualified-id, the caller will
4413 want to make a SCOPE_REF to represent the qualified-id. This
4414 function does not do this in order to avoid wastefully creating
4415 SCOPE_REFs when they are not required.
4417 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4418 `template' keyword.
4420 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4421 uninstantiated templates.
4423 If *TEMPLATE_P is non-NULL, it is set to true iff the
4424 `template' keyword is used to explicitly indicate that the entity
4425 named is a template.
4427 If DECLARATOR_P is true, the id-expression is appearing as part of
4428 a declarator, rather than as part of an expression. */
4430 static tree
4431 cp_parser_id_expression (cp_parser *parser,
4432 bool template_keyword_p,
4433 bool check_dependency_p,
4434 bool *template_p,
4435 bool declarator_p,
4436 bool optional_p)
4438 bool global_scope_p;
4439 bool nested_name_specifier_p;
4441 /* Assume the `template' keyword was not used. */
4442 if (template_p)
4443 *template_p = template_keyword_p;
4445 /* Look for the optional `::' operator. */
4446 global_scope_p
4447 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4448 != NULL_TREE);
4449 /* Look for the optional nested-name-specifier. */
4450 nested_name_specifier_p
4451 = (cp_parser_nested_name_specifier_opt (parser,
4452 /*typename_keyword_p=*/false,
4453 check_dependency_p,
4454 /*type_p=*/false,
4455 declarator_p)
4456 != NULL_TREE);
4457 /* If there is a nested-name-specifier, then we are looking at
4458 the first qualified-id production. */
4459 if (nested_name_specifier_p)
4461 tree saved_scope;
4462 tree saved_object_scope;
4463 tree saved_qualifying_scope;
4464 tree unqualified_id;
4465 bool is_template;
4467 /* See if the next token is the `template' keyword. */
4468 if (!template_p)
4469 template_p = &is_template;
4470 *template_p = cp_parser_optional_template_keyword (parser);
4471 /* Name lookup we do during the processing of the
4472 unqualified-id might obliterate SCOPE. */
4473 saved_scope = parser->scope;
4474 saved_object_scope = parser->object_scope;
4475 saved_qualifying_scope = parser->qualifying_scope;
4476 /* Process the final unqualified-id. */
4477 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4478 check_dependency_p,
4479 declarator_p,
4480 /*optional_p=*/false);
4481 /* Restore the SAVED_SCOPE for our caller. */
4482 parser->scope = saved_scope;
4483 parser->object_scope = saved_object_scope;
4484 parser->qualifying_scope = saved_qualifying_scope;
4486 return unqualified_id;
4488 /* Otherwise, if we are in global scope, then we are looking at one
4489 of the other qualified-id productions. */
4490 else if (global_scope_p)
4492 cp_token *token;
4493 tree id;
4495 /* Peek at the next token. */
4496 token = cp_lexer_peek_token (parser->lexer);
4498 /* If it's an identifier, and the next token is not a "<", then
4499 we can avoid the template-id case. This is an optimization
4500 for this common case. */
4501 if (token->type == CPP_NAME
4502 && !cp_parser_nth_token_starts_template_argument_list_p
4503 (parser, 2))
4504 return cp_parser_identifier (parser);
4506 cp_parser_parse_tentatively (parser);
4507 /* Try a template-id. */
4508 id = cp_parser_template_id (parser,
4509 /*template_keyword_p=*/false,
4510 /*check_dependency_p=*/true,
4511 none_type,
4512 declarator_p);
4513 /* If that worked, we're done. */
4514 if (cp_parser_parse_definitely (parser))
4515 return id;
4517 /* Peek at the next token. (Changes in the token buffer may
4518 have invalidated the pointer obtained above.) */
4519 token = cp_lexer_peek_token (parser->lexer);
4521 switch (token->type)
4523 case CPP_NAME:
4524 return cp_parser_identifier (parser);
4526 case CPP_KEYWORD:
4527 if (token->keyword == RID_OPERATOR)
4528 return cp_parser_operator_function_id (parser);
4529 /* Fall through. */
4531 default:
4532 cp_parser_error (parser, "expected id-expression");
4533 return error_mark_node;
4536 else
4537 return cp_parser_unqualified_id (parser, template_keyword_p,
4538 /*check_dependency_p=*/true,
4539 declarator_p,
4540 optional_p);
4543 /* Parse an unqualified-id.
4545 unqualified-id:
4546 identifier
4547 operator-function-id
4548 conversion-function-id
4549 ~ class-name
4550 template-id
4552 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4553 keyword, in a construct like `A::template ...'.
4555 Returns a representation of unqualified-id. For the `identifier'
4556 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4557 production a BIT_NOT_EXPR is returned; the operand of the
4558 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4559 other productions, see the documentation accompanying the
4560 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4561 names are looked up in uninstantiated templates. If DECLARATOR_P
4562 is true, the unqualified-id is appearing as part of a declarator,
4563 rather than as part of an expression. */
4565 static tree
4566 cp_parser_unqualified_id (cp_parser* parser,
4567 bool template_keyword_p,
4568 bool check_dependency_p,
4569 bool declarator_p,
4570 bool optional_p)
4572 cp_token *token;
4574 /* Peek at the next token. */
4575 token = cp_lexer_peek_token (parser->lexer);
4577 switch (token->type)
4579 case CPP_NAME:
4581 tree id;
4583 /* We don't know yet whether or not this will be a
4584 template-id. */
4585 cp_parser_parse_tentatively (parser);
4586 /* Try a template-id. */
4587 id = cp_parser_template_id (parser, template_keyword_p,
4588 check_dependency_p,
4589 none_type,
4590 declarator_p);
4591 /* If it worked, we're done. */
4592 if (cp_parser_parse_definitely (parser))
4593 return id;
4594 /* Otherwise, it's an ordinary identifier. */
4595 return cp_parser_identifier (parser);
4598 case CPP_TEMPLATE_ID:
4599 return cp_parser_template_id (parser, template_keyword_p,
4600 check_dependency_p,
4601 none_type,
4602 declarator_p);
4604 case CPP_COMPL:
4606 tree type_decl;
4607 tree qualifying_scope;
4608 tree object_scope;
4609 tree scope;
4610 bool done;
4612 /* Consume the `~' token. */
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Parse the class-name. The standard, as written, seems to
4615 say that:
4617 template <typename T> struct S { ~S (); };
4618 template <typename T> S<T>::~S() {}
4620 is invalid, since `~' must be followed by a class-name, but
4621 `S<T>' is dependent, and so not known to be a class.
4622 That's not right; we need to look in uninstantiated
4623 templates. A further complication arises from:
4625 template <typename T> void f(T t) {
4626 t.T::~T();
4629 Here, it is not possible to look up `T' in the scope of `T'
4630 itself. We must look in both the current scope, and the
4631 scope of the containing complete expression.
4633 Yet another issue is:
4635 struct S {
4636 int S;
4637 ~S();
4640 S::~S() {}
4642 The standard does not seem to say that the `S' in `~S'
4643 should refer to the type `S' and not the data member
4644 `S::S'. */
4646 /* DR 244 says that we look up the name after the "~" in the
4647 same scope as we looked up the qualifying name. That idea
4648 isn't fully worked out; it's more complicated than that. */
4649 scope = parser->scope;
4650 object_scope = parser->object_scope;
4651 qualifying_scope = parser->qualifying_scope;
4653 /* Check for invalid scopes. */
4654 if (scope == error_mark_node)
4656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4657 cp_lexer_consume_token (parser->lexer);
4658 return error_mark_node;
4660 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4662 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4663 error_at (token->location,
4664 "scope %qT before %<~%> is not a class-name",
4665 scope);
4666 cp_parser_simulate_error (parser);
4667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4668 cp_lexer_consume_token (parser->lexer);
4669 return error_mark_node;
4671 gcc_assert (!scope || TYPE_P (scope));
4673 /* If the name is of the form "X::~X" it's OK even if X is a
4674 typedef. */
4675 token = cp_lexer_peek_token (parser->lexer);
4676 if (scope
4677 && token->type == CPP_NAME
4678 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4679 != CPP_LESS)
4680 && (token->u.value == TYPE_IDENTIFIER (scope)
4681 || (CLASS_TYPE_P (scope)
4682 && constructor_name_p (token->u.value, scope))))
4684 cp_lexer_consume_token (parser->lexer);
4685 return build_nt (BIT_NOT_EXPR, scope);
4688 /* If there was an explicit qualification (S::~T), first look
4689 in the scope given by the qualification (i.e., S).
4691 Note: in the calls to cp_parser_class_name below we pass
4692 typename_type so that lookup finds the injected-class-name
4693 rather than the constructor. */
4694 done = false;
4695 type_decl = NULL_TREE;
4696 if (scope)
4698 cp_parser_parse_tentatively (parser);
4699 type_decl = cp_parser_class_name (parser,
4700 /*typename_keyword_p=*/false,
4701 /*template_keyword_p=*/false,
4702 typename_type,
4703 /*check_dependency=*/false,
4704 /*class_head_p=*/false,
4705 declarator_p);
4706 if (cp_parser_parse_definitely (parser))
4707 done = true;
4709 /* In "N::S::~S", look in "N" as well. */
4710 if (!done && scope && qualifying_scope)
4712 cp_parser_parse_tentatively (parser);
4713 parser->scope = qualifying_scope;
4714 parser->object_scope = NULL_TREE;
4715 parser->qualifying_scope = NULL_TREE;
4716 type_decl
4717 = cp_parser_class_name (parser,
4718 /*typename_keyword_p=*/false,
4719 /*template_keyword_p=*/false,
4720 typename_type,
4721 /*check_dependency=*/false,
4722 /*class_head_p=*/false,
4723 declarator_p);
4724 if (cp_parser_parse_definitely (parser))
4725 done = true;
4727 /* In "p->S::~T", look in the scope given by "*p" as well. */
4728 else if (!done && object_scope)
4730 cp_parser_parse_tentatively (parser);
4731 parser->scope = object_scope;
4732 parser->object_scope = NULL_TREE;
4733 parser->qualifying_scope = NULL_TREE;
4734 type_decl
4735 = cp_parser_class_name (parser,
4736 /*typename_keyword_p=*/false,
4737 /*template_keyword_p=*/false,
4738 typename_type,
4739 /*check_dependency=*/false,
4740 /*class_head_p=*/false,
4741 declarator_p);
4742 if (cp_parser_parse_definitely (parser))
4743 done = true;
4745 /* Look in the surrounding context. */
4746 if (!done)
4748 parser->scope = NULL_TREE;
4749 parser->object_scope = NULL_TREE;
4750 parser->qualifying_scope = NULL_TREE;
4751 if (processing_template_decl)
4752 cp_parser_parse_tentatively (parser);
4753 type_decl
4754 = cp_parser_class_name (parser,
4755 /*typename_keyword_p=*/false,
4756 /*template_keyword_p=*/false,
4757 typename_type,
4758 /*check_dependency=*/false,
4759 /*class_head_p=*/false,
4760 declarator_p);
4761 if (processing_template_decl
4762 && ! cp_parser_parse_definitely (parser))
4764 /* We couldn't find a type with this name, so just accept
4765 it and check for a match at instantiation time. */
4766 type_decl = cp_parser_identifier (parser);
4767 if (type_decl != error_mark_node)
4768 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4769 return type_decl;
4772 /* If an error occurred, assume that the name of the
4773 destructor is the same as the name of the qualifying
4774 class. That allows us to keep parsing after running
4775 into ill-formed destructor names. */
4776 if (type_decl == error_mark_node && scope)
4777 return build_nt (BIT_NOT_EXPR, scope);
4778 else if (type_decl == error_mark_node)
4779 return error_mark_node;
4781 /* Check that destructor name and scope match. */
4782 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4785 error_at (token->location,
4786 "declaration of %<~%T%> as member of %qT",
4787 type_decl, scope);
4788 cp_parser_simulate_error (parser);
4789 return error_mark_node;
4792 /* [class.dtor]
4794 A typedef-name that names a class shall not be used as the
4795 identifier in the declarator for a destructor declaration. */
4796 if (declarator_p
4797 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4798 && !DECL_SELF_REFERENCE_P (type_decl)
4799 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4800 error_at (token->location,
4801 "typedef-name %qD used as destructor declarator",
4802 type_decl);
4804 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4807 case CPP_KEYWORD:
4808 if (token->keyword == RID_OPERATOR)
4810 tree id;
4812 /* This could be a template-id, so we try that first. */
4813 cp_parser_parse_tentatively (parser);
4814 /* Try a template-id. */
4815 id = cp_parser_template_id (parser, template_keyword_p,
4816 /*check_dependency_p=*/true,
4817 none_type,
4818 declarator_p);
4819 /* If that worked, we're done. */
4820 if (cp_parser_parse_definitely (parser))
4821 return id;
4822 /* We still don't know whether we're looking at an
4823 operator-function-id or a conversion-function-id. */
4824 cp_parser_parse_tentatively (parser);
4825 /* Try an operator-function-id. */
4826 id = cp_parser_operator_function_id (parser);
4827 /* If that didn't work, try a conversion-function-id. */
4828 if (!cp_parser_parse_definitely (parser))
4829 id = cp_parser_conversion_function_id (parser);
4830 else if (UDLIT_OPER_P (id))
4832 /* 17.6.3.3.5 */
4833 const char *name = UDLIT_OP_SUFFIX (id);
4834 if (name[0] != '_' && !in_system_header)
4835 warning (0, "literal operator suffixes not preceded by %<_%>"
4836 " are reserved for future standardization");
4839 return id;
4841 /* Fall through. */
4843 default:
4844 if (optional_p)
4845 return NULL_TREE;
4846 cp_parser_error (parser, "expected unqualified-id");
4847 return error_mark_node;
4851 /* Parse an (optional) nested-name-specifier.
4853 nested-name-specifier: [C++98]
4854 class-or-namespace-name :: nested-name-specifier [opt]
4855 class-or-namespace-name :: template nested-name-specifier [opt]
4857 nested-name-specifier: [C++0x]
4858 type-name ::
4859 namespace-name ::
4860 nested-name-specifier identifier ::
4861 nested-name-specifier template [opt] simple-template-id ::
4863 PARSER->SCOPE should be set appropriately before this function is
4864 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4865 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4866 in name lookups.
4868 Sets PARSER->SCOPE to the class (TYPE) or namespace
4869 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4870 it unchanged if there is no nested-name-specifier. Returns the new
4871 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4873 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4874 part of a declaration and/or decl-specifier. */
4876 static tree
4877 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4878 bool typename_keyword_p,
4879 bool check_dependency_p,
4880 bool type_p,
4881 bool is_declaration)
4883 bool success = false;
4884 cp_token_position start = 0;
4885 cp_token *token;
4887 /* Remember where the nested-name-specifier starts. */
4888 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4890 start = cp_lexer_token_position (parser->lexer, false);
4891 push_deferring_access_checks (dk_deferred);
4894 while (true)
4896 tree new_scope;
4897 tree old_scope;
4898 tree saved_qualifying_scope;
4899 bool template_keyword_p;
4901 /* Spot cases that cannot be the beginning of a
4902 nested-name-specifier. */
4903 token = cp_lexer_peek_token (parser->lexer);
4905 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4906 the already parsed nested-name-specifier. */
4907 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4909 /* Grab the nested-name-specifier and continue the loop. */
4910 cp_parser_pre_parsed_nested_name_specifier (parser);
4911 /* If we originally encountered this nested-name-specifier
4912 with IS_DECLARATION set to false, we will not have
4913 resolved TYPENAME_TYPEs, so we must do so here. */
4914 if (is_declaration
4915 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4917 new_scope = resolve_typename_type (parser->scope,
4918 /*only_current_p=*/false);
4919 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4920 parser->scope = new_scope;
4922 success = true;
4923 continue;
4926 /* Spot cases that cannot be the beginning of a
4927 nested-name-specifier. On the second and subsequent times
4928 through the loop, we look for the `template' keyword. */
4929 if (success && token->keyword == RID_TEMPLATE)
4931 /* A template-id can start a nested-name-specifier. */
4932 else if (token->type == CPP_TEMPLATE_ID)
4934 /* DR 743: decltype can be used in a nested-name-specifier. */
4935 else if (token_is_decltype (token))
4937 else
4939 /* If the next token is not an identifier, then it is
4940 definitely not a type-name or namespace-name. */
4941 if (token->type != CPP_NAME)
4942 break;
4943 /* If the following token is neither a `<' (to begin a
4944 template-id), nor a `::', then we are not looking at a
4945 nested-name-specifier. */
4946 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4948 if (token->type == CPP_COLON
4949 && parser->colon_corrects_to_scope_p
4950 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4952 error_at (token->location,
4953 "found %<:%> in nested-name-specifier, expected %<::%>");
4954 token->type = CPP_SCOPE;
4957 if (token->type != CPP_SCOPE
4958 && !cp_parser_nth_token_starts_template_argument_list_p
4959 (parser, 2))
4960 break;
4963 /* The nested-name-specifier is optional, so we parse
4964 tentatively. */
4965 cp_parser_parse_tentatively (parser);
4967 /* Look for the optional `template' keyword, if this isn't the
4968 first time through the loop. */
4969 if (success)
4970 template_keyword_p = cp_parser_optional_template_keyword (parser);
4971 else
4972 template_keyword_p = false;
4974 /* Save the old scope since the name lookup we are about to do
4975 might destroy it. */
4976 old_scope = parser->scope;
4977 saved_qualifying_scope = parser->qualifying_scope;
4978 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4979 look up names in "X<T>::I" in order to determine that "Y" is
4980 a template. So, if we have a typename at this point, we make
4981 an effort to look through it. */
4982 if (is_declaration
4983 && !typename_keyword_p
4984 && parser->scope
4985 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4986 parser->scope = resolve_typename_type (parser->scope,
4987 /*only_current_p=*/false);
4988 /* Parse the qualifying entity. */
4989 new_scope
4990 = cp_parser_qualifying_entity (parser,
4991 typename_keyword_p,
4992 template_keyword_p,
4993 check_dependency_p,
4994 type_p,
4995 is_declaration);
4996 /* Look for the `::' token. */
4997 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4999 /* If we found what we wanted, we keep going; otherwise, we're
5000 done. */
5001 if (!cp_parser_parse_definitely (parser))
5003 bool error_p = false;
5005 /* Restore the OLD_SCOPE since it was valid before the
5006 failed attempt at finding the last
5007 class-or-namespace-name. */
5008 parser->scope = old_scope;
5009 parser->qualifying_scope = saved_qualifying_scope;
5011 /* If the next token is a decltype, and the one after that is a
5012 `::', then the decltype has failed to resolve to a class or
5013 enumeration type. Give this error even when parsing
5014 tentatively since it can't possibly be valid--and we're going
5015 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5016 won't get another chance.*/
5017 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5018 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5019 == CPP_SCOPE))
5021 token = cp_lexer_consume_token (parser->lexer);
5022 error_at (token->location, "decltype evaluates to %qT, "
5023 "which is not a class or enumeration type",
5024 token->u.value);
5025 parser->scope = error_mark_node;
5026 error_p = true;
5027 /* As below. */
5028 success = true;
5029 cp_lexer_consume_token (parser->lexer);
5032 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5033 break;
5034 /* If the next token is an identifier, and the one after
5035 that is a `::', then any valid interpretation would have
5036 found a class-or-namespace-name. */
5037 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5038 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5039 == CPP_SCOPE)
5040 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5041 != CPP_COMPL))
5043 token = cp_lexer_consume_token (parser->lexer);
5044 if (!error_p)
5046 if (!token->ambiguous_p)
5048 tree decl;
5049 tree ambiguous_decls;
5051 decl = cp_parser_lookup_name (parser, token->u.value,
5052 none_type,
5053 /*is_template=*/false,
5054 /*is_namespace=*/false,
5055 /*check_dependency=*/true,
5056 &ambiguous_decls,
5057 token->location);
5058 if (TREE_CODE (decl) == TEMPLATE_DECL)
5059 error_at (token->location,
5060 "%qD used without template parameters",
5061 decl);
5062 else if (ambiguous_decls)
5064 error_at (token->location,
5065 "reference to %qD is ambiguous",
5066 token->u.value);
5067 print_candidates (ambiguous_decls);
5068 decl = error_mark_node;
5070 else
5072 if (cxx_dialect != cxx98)
5073 cp_parser_name_lookup_error
5074 (parser, token->u.value, decl, NLE_NOT_CXX98,
5075 token->location);
5076 else
5077 cp_parser_name_lookup_error
5078 (parser, token->u.value, decl, NLE_CXX98,
5079 token->location);
5082 parser->scope = error_mark_node;
5083 error_p = true;
5084 /* Treat this as a successful nested-name-specifier
5085 due to:
5087 [basic.lookup.qual]
5089 If the name found is not a class-name (clause
5090 _class_) or namespace-name (_namespace.def_), the
5091 program is ill-formed. */
5092 success = true;
5094 cp_lexer_consume_token (parser->lexer);
5096 break;
5098 /* We've found one valid nested-name-specifier. */
5099 success = true;
5100 /* Name lookup always gives us a DECL. */
5101 if (TREE_CODE (new_scope) == TYPE_DECL)
5102 new_scope = TREE_TYPE (new_scope);
5103 /* Uses of "template" must be followed by actual templates. */
5104 if (template_keyword_p
5105 && !(CLASS_TYPE_P (new_scope)
5106 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5107 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5108 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5109 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5110 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5111 == TEMPLATE_ID_EXPR)))
5112 permerror (input_location, TYPE_P (new_scope)
5113 ? G_("%qT is not a template")
5114 : G_("%qD is not a template"),
5115 new_scope);
5116 /* If it is a class scope, try to complete it; we are about to
5117 be looking up names inside the class. */
5118 if (TYPE_P (new_scope)
5119 /* Since checking types for dependency can be expensive,
5120 avoid doing it if the type is already complete. */
5121 && !COMPLETE_TYPE_P (new_scope)
5122 /* Do not try to complete dependent types. */
5123 && !dependent_type_p (new_scope))
5125 new_scope = complete_type (new_scope);
5126 /* If it is a typedef to current class, use the current
5127 class instead, as the typedef won't have any names inside
5128 it yet. */
5129 if (!COMPLETE_TYPE_P (new_scope)
5130 && currently_open_class (new_scope))
5131 new_scope = TYPE_MAIN_VARIANT (new_scope);
5133 /* Make sure we look in the right scope the next time through
5134 the loop. */
5135 parser->scope = new_scope;
5138 /* If parsing tentatively, replace the sequence of tokens that makes
5139 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5140 token. That way, should we re-parse the token stream, we will
5141 not have to repeat the effort required to do the parse, nor will
5142 we issue duplicate error messages. */
5143 if (success && start)
5145 cp_token *token;
5147 token = cp_lexer_token_at (parser->lexer, start);
5148 /* Reset the contents of the START token. */
5149 token->type = CPP_NESTED_NAME_SPECIFIER;
5150 /* Retrieve any deferred checks. Do not pop this access checks yet
5151 so the memory will not be reclaimed during token replacing below. */
5152 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5153 token->u.tree_check_value->value = parser->scope;
5154 token->u.tree_check_value->checks = get_deferred_access_checks ();
5155 token->u.tree_check_value->qualifying_scope =
5156 parser->qualifying_scope;
5157 token->keyword = RID_MAX;
5159 /* Purge all subsequent tokens. */
5160 cp_lexer_purge_tokens_after (parser->lexer, start);
5163 if (start)
5164 pop_to_parent_deferring_access_checks ();
5166 return success ? parser->scope : NULL_TREE;
5169 /* Parse a nested-name-specifier. See
5170 cp_parser_nested_name_specifier_opt for details. This function
5171 behaves identically, except that it will an issue an error if no
5172 nested-name-specifier is present. */
5174 static tree
5175 cp_parser_nested_name_specifier (cp_parser *parser,
5176 bool typename_keyword_p,
5177 bool check_dependency_p,
5178 bool type_p,
5179 bool is_declaration)
5181 tree scope;
5183 /* Look for the nested-name-specifier. */
5184 scope = cp_parser_nested_name_specifier_opt (parser,
5185 typename_keyword_p,
5186 check_dependency_p,
5187 type_p,
5188 is_declaration);
5189 /* If it was not present, issue an error message. */
5190 if (!scope)
5192 cp_parser_error (parser, "expected nested-name-specifier");
5193 parser->scope = NULL_TREE;
5196 return scope;
5199 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5200 this is either a class-name or a namespace-name (which corresponds
5201 to the class-or-namespace-name production in the grammar). For
5202 C++0x, it can also be a type-name that refers to an enumeration
5203 type or a simple-template-id.
5205 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5206 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5207 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5208 TYPE_P is TRUE iff the next name should be taken as a class-name,
5209 even the same name is declared to be another entity in the same
5210 scope.
5212 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5213 specified by the class-or-namespace-name. If neither is found the
5214 ERROR_MARK_NODE is returned. */
5216 static tree
5217 cp_parser_qualifying_entity (cp_parser *parser,
5218 bool typename_keyword_p,
5219 bool template_keyword_p,
5220 bool check_dependency_p,
5221 bool type_p,
5222 bool is_declaration)
5224 tree saved_scope;
5225 tree saved_qualifying_scope;
5226 tree saved_object_scope;
5227 tree scope;
5228 bool only_class_p;
5229 bool successful_parse_p;
5231 /* DR 743: decltype can appear in a nested-name-specifier. */
5232 if (cp_lexer_next_token_is_decltype (parser->lexer))
5234 scope = cp_parser_decltype (parser);
5235 if (TREE_CODE (scope) != ENUMERAL_TYPE
5236 && !MAYBE_CLASS_TYPE_P (scope))
5238 cp_parser_simulate_error (parser);
5239 return error_mark_node;
5241 if (TYPE_NAME (scope))
5242 scope = TYPE_NAME (scope);
5243 return scope;
5246 /* Before we try to parse the class-name, we must save away the
5247 current PARSER->SCOPE since cp_parser_class_name will destroy
5248 it. */
5249 saved_scope = parser->scope;
5250 saved_qualifying_scope = parser->qualifying_scope;
5251 saved_object_scope = parser->object_scope;
5252 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5253 there is no need to look for a namespace-name. */
5254 only_class_p = template_keyword_p
5255 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5256 if (!only_class_p)
5257 cp_parser_parse_tentatively (parser);
5258 scope = cp_parser_class_name (parser,
5259 typename_keyword_p,
5260 template_keyword_p,
5261 type_p ? class_type : none_type,
5262 check_dependency_p,
5263 /*class_head_p=*/false,
5264 is_declaration);
5265 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5266 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5267 if (!only_class_p
5268 && cxx_dialect != cxx98
5269 && !successful_parse_p)
5271 /* Restore the saved scope. */
5272 parser->scope = saved_scope;
5273 parser->qualifying_scope = saved_qualifying_scope;
5274 parser->object_scope = saved_object_scope;
5276 /* Parse tentatively. */
5277 cp_parser_parse_tentatively (parser);
5279 /* Parse a type-name */
5280 scope = cp_parser_type_name (parser);
5282 /* "If the name found does not designate a namespace or a class,
5283 enumeration, or dependent type, the program is ill-formed."
5285 We cover classes and dependent types above and namespaces below,
5286 so this code is only looking for enums. */
5287 if (!scope || TREE_CODE (scope) != TYPE_DECL
5288 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5289 cp_parser_simulate_error (parser);
5291 successful_parse_p = cp_parser_parse_definitely (parser);
5293 /* If that didn't work, try for a namespace-name. */
5294 if (!only_class_p && !successful_parse_p)
5296 /* Restore the saved scope. */
5297 parser->scope = saved_scope;
5298 parser->qualifying_scope = saved_qualifying_scope;
5299 parser->object_scope = saved_object_scope;
5300 /* If we are not looking at an identifier followed by the scope
5301 resolution operator, then this is not part of a
5302 nested-name-specifier. (Note that this function is only used
5303 to parse the components of a nested-name-specifier.) */
5304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5305 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5306 return error_mark_node;
5307 scope = cp_parser_namespace_name (parser);
5310 return scope;
5313 /* Parse a postfix-expression.
5315 postfix-expression:
5316 primary-expression
5317 postfix-expression [ expression ]
5318 postfix-expression ( expression-list [opt] )
5319 simple-type-specifier ( expression-list [opt] )
5320 typename :: [opt] nested-name-specifier identifier
5321 ( expression-list [opt] )
5322 typename :: [opt] nested-name-specifier template [opt] template-id
5323 ( expression-list [opt] )
5324 postfix-expression . template [opt] id-expression
5325 postfix-expression -> template [opt] id-expression
5326 postfix-expression . pseudo-destructor-name
5327 postfix-expression -> pseudo-destructor-name
5328 postfix-expression ++
5329 postfix-expression --
5330 dynamic_cast < type-id > ( expression )
5331 static_cast < type-id > ( expression )
5332 reinterpret_cast < type-id > ( expression )
5333 const_cast < type-id > ( expression )
5334 typeid ( expression )
5335 typeid ( type-id )
5337 GNU Extension:
5339 postfix-expression:
5340 ( type-id ) { initializer-list , [opt] }
5342 This extension is a GNU version of the C99 compound-literal
5343 construct. (The C99 grammar uses `type-name' instead of `type-id',
5344 but they are essentially the same concept.)
5346 If ADDRESS_P is true, the postfix expression is the operand of the
5347 `&' operator. CAST_P is true if this expression is the target of a
5348 cast.
5350 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5351 class member access expressions [expr.ref].
5353 Returns a representation of the expression. */
5355 static tree
5356 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5357 bool member_access_only_p,
5358 cp_id_kind * pidk_return)
5360 cp_token *token;
5361 enum rid keyword;
5362 cp_id_kind idk = CP_ID_KIND_NONE;
5363 tree postfix_expression = NULL_TREE;
5364 bool is_member_access = false;
5366 /* Peek at the next token. */
5367 token = cp_lexer_peek_token (parser->lexer);
5368 /* Some of the productions are determined by keywords. */
5369 keyword = token->keyword;
5370 switch (keyword)
5372 case RID_DYNCAST:
5373 case RID_STATCAST:
5374 case RID_REINTCAST:
5375 case RID_CONSTCAST:
5377 tree type;
5378 tree expression;
5379 const char *saved_message;
5381 /* All of these can be handled in the same way from the point
5382 of view of parsing. Begin by consuming the token
5383 identifying the cast. */
5384 cp_lexer_consume_token (parser->lexer);
5386 /* New types cannot be defined in the cast. */
5387 saved_message = parser->type_definition_forbidden_message;
5388 parser->type_definition_forbidden_message
5389 = G_("types may not be defined in casts");
5391 /* Look for the opening `<'. */
5392 cp_parser_require (parser, CPP_LESS, RT_LESS);
5393 /* Parse the type to which we are casting. */
5394 type = cp_parser_type_id (parser);
5395 /* Look for the closing `>'. */
5396 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5397 /* Restore the old message. */
5398 parser->type_definition_forbidden_message = saved_message;
5400 /* And the expression which is being cast. */
5401 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5402 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5403 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5405 /* Only type conversions to integral or enumeration types
5406 can be used in constant-expressions. */
5407 if (!cast_valid_in_integral_constant_expression_p (type)
5408 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5409 return error_mark_node;
5411 switch (keyword)
5413 case RID_DYNCAST:
5414 postfix_expression
5415 = build_dynamic_cast (type, expression, tf_warning_or_error);
5416 break;
5417 case RID_STATCAST:
5418 postfix_expression
5419 = build_static_cast (type, expression, tf_warning_or_error);
5420 break;
5421 case RID_REINTCAST:
5422 postfix_expression
5423 = build_reinterpret_cast (type, expression,
5424 tf_warning_or_error);
5425 break;
5426 case RID_CONSTCAST:
5427 postfix_expression
5428 = build_const_cast (type, expression, tf_warning_or_error);
5429 break;
5430 default:
5431 gcc_unreachable ();
5434 break;
5436 case RID_TYPEID:
5438 tree type;
5439 const char *saved_message;
5440 bool saved_in_type_id_in_expr_p;
5442 /* Consume the `typeid' token. */
5443 cp_lexer_consume_token (parser->lexer);
5444 /* Look for the `(' token. */
5445 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5446 /* Types cannot be defined in a `typeid' expression. */
5447 saved_message = parser->type_definition_forbidden_message;
5448 parser->type_definition_forbidden_message
5449 = G_("types may not be defined in a %<typeid%> expression");
5450 /* We can't be sure yet whether we're looking at a type-id or an
5451 expression. */
5452 cp_parser_parse_tentatively (parser);
5453 /* Try a type-id first. */
5454 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5455 parser->in_type_id_in_expr_p = true;
5456 type = cp_parser_type_id (parser);
5457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5458 /* Look for the `)' token. Otherwise, we can't be sure that
5459 we're not looking at an expression: consider `typeid (int
5460 (3))', for example. */
5461 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5462 /* If all went well, simply lookup the type-id. */
5463 if (cp_parser_parse_definitely (parser))
5464 postfix_expression = get_typeid (type);
5465 /* Otherwise, fall back to the expression variant. */
5466 else
5468 tree expression;
5470 /* Look for an expression. */
5471 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5472 /* Compute its typeid. */
5473 postfix_expression = build_typeid (expression);
5474 /* Look for the `)' token. */
5475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5477 /* Restore the saved message. */
5478 parser->type_definition_forbidden_message = saved_message;
5479 /* `typeid' may not appear in an integral constant expression. */
5480 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5481 return error_mark_node;
5483 break;
5485 case RID_TYPENAME:
5487 tree type;
5488 /* The syntax permitted here is the same permitted for an
5489 elaborated-type-specifier. */
5490 type = cp_parser_elaborated_type_specifier (parser,
5491 /*is_friend=*/false,
5492 /*is_declaration=*/false);
5493 postfix_expression = cp_parser_functional_cast (parser, type);
5495 break;
5497 case RID_BUILTIN_SHUFFLE:
5499 VEC(tree,gc)* vec;
5500 unsigned int i;
5501 tree p;
5502 location_t loc = token->location;
5504 cp_lexer_consume_token (parser->lexer);
5505 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5506 /*cast_p=*/false, /*allow_expansion_p=*/true,
5507 /*non_constant_p=*/NULL);
5508 if (vec == NULL)
5509 return error_mark_node;
5511 FOR_EACH_VEC_ELT (tree, vec, i, p)
5512 mark_exp_read (p);
5514 if (VEC_length (tree, vec) == 2)
5515 return
5516 c_build_vec_perm_expr
5517 (loc, VEC_index (tree, vec, 0),
5518 NULL_TREE, VEC_index (tree, vec, 1));
5520 else if (VEC_length (tree, vec) == 3)
5521 return
5522 c_build_vec_perm_expr
5523 (loc, VEC_index (tree, vec, 0),
5524 VEC_index (tree, vec, 1),
5525 VEC_index (tree, vec, 2));
5526 else
5528 error_at (loc, "wrong number of arguments to "
5529 "%<__builtin_shuffle%>");
5530 return error_mark_node;
5532 break;
5535 default:
5537 tree type;
5539 /* If the next thing is a simple-type-specifier, we may be
5540 looking at a functional cast. We could also be looking at
5541 an id-expression. So, we try the functional cast, and if
5542 that doesn't work we fall back to the primary-expression. */
5543 cp_parser_parse_tentatively (parser);
5544 /* Look for the simple-type-specifier. */
5545 type = cp_parser_simple_type_specifier (parser,
5546 /*decl_specs=*/NULL,
5547 CP_PARSER_FLAGS_NONE);
5548 /* Parse the cast itself. */
5549 if (!cp_parser_error_occurred (parser))
5550 postfix_expression
5551 = cp_parser_functional_cast (parser, type);
5552 /* If that worked, we're done. */
5553 if (cp_parser_parse_definitely (parser))
5554 break;
5556 /* If the functional-cast didn't work out, try a
5557 compound-literal. */
5558 if (cp_parser_allow_gnu_extensions_p (parser)
5559 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5561 VEC(constructor_elt,gc) *initializer_list = NULL;
5562 bool saved_in_type_id_in_expr_p;
5564 cp_parser_parse_tentatively (parser);
5565 /* Consume the `('. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Parse the type. */
5568 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5569 parser->in_type_id_in_expr_p = true;
5570 type = cp_parser_type_id (parser);
5571 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5572 /* Look for the `)'. */
5573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5574 /* Look for the `{'. */
5575 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5576 /* If things aren't going well, there's no need to
5577 keep going. */
5578 if (!cp_parser_error_occurred (parser))
5580 bool non_constant_p;
5581 /* Parse the initializer-list. */
5582 initializer_list
5583 = cp_parser_initializer_list (parser, &non_constant_p);
5584 /* Allow a trailing `,'. */
5585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5586 cp_lexer_consume_token (parser->lexer);
5587 /* Look for the final `}'. */
5588 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5590 /* If that worked, we're definitely looking at a
5591 compound-literal expression. */
5592 if (cp_parser_parse_definitely (parser))
5594 /* Warn the user that a compound literal is not
5595 allowed in standard C++. */
5596 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5597 /* For simplicity, we disallow compound literals in
5598 constant-expressions. We could
5599 allow compound literals of integer type, whose
5600 initializer was a constant, in constant
5601 expressions. Permitting that usage, as a further
5602 extension, would not change the meaning of any
5603 currently accepted programs. (Of course, as
5604 compound literals are not part of ISO C++, the
5605 standard has nothing to say.) */
5606 if (cp_parser_non_integral_constant_expression (parser,
5607 NIC_NCC))
5609 postfix_expression = error_mark_node;
5610 break;
5612 /* Form the representation of the compound-literal. */
5613 postfix_expression
5614 = (finish_compound_literal
5615 (type, build_constructor (init_list_type_node,
5616 initializer_list),
5617 tf_warning_or_error));
5618 break;
5622 /* It must be a primary-expression. */
5623 postfix_expression
5624 = cp_parser_primary_expression (parser, address_p, cast_p,
5625 /*template_arg_p=*/false,
5626 &idk);
5628 break;
5631 /* Keep looping until the postfix-expression is complete. */
5632 while (true)
5634 if (idk == CP_ID_KIND_UNQUALIFIED
5635 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5636 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5637 /* It is not a Koenig lookup function call. */
5638 postfix_expression
5639 = unqualified_name_lookup_error (postfix_expression);
5641 /* Peek at the next token. */
5642 token = cp_lexer_peek_token (parser->lexer);
5644 switch (token->type)
5646 case CPP_OPEN_SQUARE:
5647 if (cp_next_tokens_can_be_std_attribute_p (parser))
5649 cp_parser_error (parser,
5650 "two consecutive %<[%> shall "
5651 "only introduce an attribute");
5652 return error_mark_node;
5654 postfix_expression
5655 = cp_parser_postfix_open_square_expression (parser,
5656 postfix_expression,
5657 false);
5658 idk = CP_ID_KIND_NONE;
5659 is_member_access = false;
5660 break;
5662 case CPP_OPEN_PAREN:
5663 /* postfix-expression ( expression-list [opt] ) */
5665 bool koenig_p;
5666 bool is_builtin_constant_p;
5667 bool saved_integral_constant_expression_p = false;
5668 bool saved_non_integral_constant_expression_p = false;
5669 VEC(tree,gc) *args;
5671 is_member_access = false;
5673 is_builtin_constant_p
5674 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5675 if (is_builtin_constant_p)
5677 /* The whole point of __builtin_constant_p is to allow
5678 non-constant expressions to appear as arguments. */
5679 saved_integral_constant_expression_p
5680 = parser->integral_constant_expression_p;
5681 saved_non_integral_constant_expression_p
5682 = parser->non_integral_constant_expression_p;
5683 parser->integral_constant_expression_p = false;
5685 args = (cp_parser_parenthesized_expression_list
5686 (parser, non_attr,
5687 /*cast_p=*/false, /*allow_expansion_p=*/true,
5688 /*non_constant_p=*/NULL));
5689 if (is_builtin_constant_p)
5691 parser->integral_constant_expression_p
5692 = saved_integral_constant_expression_p;
5693 parser->non_integral_constant_expression_p
5694 = saved_non_integral_constant_expression_p;
5697 if (args == NULL)
5699 postfix_expression = error_mark_node;
5700 break;
5703 /* Function calls are not permitted in
5704 constant-expressions. */
5705 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5706 && cp_parser_non_integral_constant_expression (parser,
5707 NIC_FUNC_CALL))
5709 postfix_expression = error_mark_node;
5710 release_tree_vector (args);
5711 break;
5714 koenig_p = false;
5715 if (idk == CP_ID_KIND_UNQUALIFIED
5716 || idk == CP_ID_KIND_TEMPLATE_ID)
5718 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5720 if (!VEC_empty (tree, args))
5722 koenig_p = true;
5723 if (!any_type_dependent_arguments_p (args))
5724 postfix_expression
5725 = perform_koenig_lookup (postfix_expression, args,
5726 /*include_std=*/false,
5727 tf_warning_or_error);
5729 else
5730 postfix_expression
5731 = unqualified_fn_lookup_error (postfix_expression);
5733 /* We do not perform argument-dependent lookup if
5734 normal lookup finds a non-function, in accordance
5735 with the expected resolution of DR 218. */
5736 else if (!VEC_empty (tree, args)
5737 && is_overloaded_fn (postfix_expression))
5739 tree fn = get_first_fn (postfix_expression);
5740 fn = STRIP_TEMPLATE (fn);
5742 /* Do not do argument dependent lookup if regular
5743 lookup finds a member function or a block-scope
5744 function declaration. [basic.lookup.argdep]/3 */
5745 if (!DECL_FUNCTION_MEMBER_P (fn)
5746 && !DECL_LOCAL_FUNCTION_P (fn))
5748 koenig_p = true;
5749 if (!any_type_dependent_arguments_p (args))
5750 postfix_expression
5751 = perform_koenig_lookup (postfix_expression, args,
5752 /*include_std=*/false,
5753 tf_warning_or_error);
5758 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5760 tree instance = TREE_OPERAND (postfix_expression, 0);
5761 tree fn = TREE_OPERAND (postfix_expression, 1);
5763 if (processing_template_decl
5764 && (type_dependent_expression_p (instance)
5765 || (!BASELINK_P (fn)
5766 && TREE_CODE (fn) != FIELD_DECL)
5767 || type_dependent_expression_p (fn)
5768 || any_type_dependent_arguments_p (args)))
5770 postfix_expression
5771 = build_nt_call_vec (postfix_expression, args);
5772 release_tree_vector (args);
5773 break;
5776 if (BASELINK_P (fn))
5778 postfix_expression
5779 = (build_new_method_call
5780 (instance, fn, &args, NULL_TREE,
5781 (idk == CP_ID_KIND_QUALIFIED
5782 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5783 : LOOKUP_NORMAL),
5784 /*fn_p=*/NULL,
5785 tf_warning_or_error));
5787 else
5788 postfix_expression
5789 = finish_call_expr (postfix_expression, &args,
5790 /*disallow_virtual=*/false,
5791 /*koenig_p=*/false,
5792 tf_warning_or_error);
5794 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5795 || TREE_CODE (postfix_expression) == MEMBER_REF
5796 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5797 postfix_expression = (build_offset_ref_call_from_tree
5798 (postfix_expression, &args,
5799 tf_warning_or_error));
5800 else if (idk == CP_ID_KIND_QUALIFIED)
5801 /* A call to a static class member, or a namespace-scope
5802 function. */
5803 postfix_expression
5804 = finish_call_expr (postfix_expression, &args,
5805 /*disallow_virtual=*/true,
5806 koenig_p,
5807 tf_warning_or_error);
5808 else
5809 /* All other function calls. */
5810 postfix_expression
5811 = finish_call_expr (postfix_expression, &args,
5812 /*disallow_virtual=*/false,
5813 koenig_p,
5814 tf_warning_or_error);
5816 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5817 idk = CP_ID_KIND_NONE;
5819 release_tree_vector (args);
5821 break;
5823 case CPP_DOT:
5824 case CPP_DEREF:
5825 /* postfix-expression . template [opt] id-expression
5826 postfix-expression . pseudo-destructor-name
5827 postfix-expression -> template [opt] id-expression
5828 postfix-expression -> pseudo-destructor-name */
5830 /* Consume the `.' or `->' operator. */
5831 cp_lexer_consume_token (parser->lexer);
5833 postfix_expression
5834 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5835 postfix_expression,
5836 false, &idk,
5837 token->location);
5839 is_member_access = true;
5840 break;
5842 case CPP_PLUS_PLUS:
5843 /* postfix-expression ++ */
5844 /* Consume the `++' token. */
5845 cp_lexer_consume_token (parser->lexer);
5846 /* Generate a representation for the complete expression. */
5847 postfix_expression
5848 = finish_increment_expr (postfix_expression,
5849 POSTINCREMENT_EXPR);
5850 /* Increments may not appear in constant-expressions. */
5851 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5852 postfix_expression = error_mark_node;
5853 idk = CP_ID_KIND_NONE;
5854 is_member_access = false;
5855 break;
5857 case CPP_MINUS_MINUS:
5858 /* postfix-expression -- */
5859 /* Consume the `--' token. */
5860 cp_lexer_consume_token (parser->lexer);
5861 /* Generate a representation for the complete expression. */
5862 postfix_expression
5863 = finish_increment_expr (postfix_expression,
5864 POSTDECREMENT_EXPR);
5865 /* Decrements may not appear in constant-expressions. */
5866 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5867 postfix_expression = error_mark_node;
5868 idk = CP_ID_KIND_NONE;
5869 is_member_access = false;
5870 break;
5872 default:
5873 if (pidk_return != NULL)
5874 * pidk_return = idk;
5875 if (member_access_only_p)
5876 return is_member_access? postfix_expression : error_mark_node;
5877 else
5878 return postfix_expression;
5882 /* We should never get here. */
5883 gcc_unreachable ();
5884 return error_mark_node;
5887 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5888 by cp_parser_builtin_offsetof. We're looking for
5890 postfix-expression [ expression ]
5891 postfix-expression [ braced-init-list ] (C++11)
5893 FOR_OFFSETOF is set if we're being called in that context, which
5894 changes how we deal with integer constant expressions. */
5896 static tree
5897 cp_parser_postfix_open_square_expression (cp_parser *parser,
5898 tree postfix_expression,
5899 bool for_offsetof)
5901 tree index;
5902 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5904 /* Consume the `[' token. */
5905 cp_lexer_consume_token (parser->lexer);
5907 /* Parse the index expression. */
5908 /* ??? For offsetof, there is a question of what to allow here. If
5909 offsetof is not being used in an integral constant expression context,
5910 then we *could* get the right answer by computing the value at runtime.
5911 If we are in an integral constant expression context, then we might
5912 could accept any constant expression; hard to say without analysis.
5913 Rather than open the barn door too wide right away, allow only integer
5914 constant expressions here. */
5915 if (for_offsetof)
5916 index = cp_parser_constant_expression (parser, false, NULL);
5917 else
5919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5921 bool expr_nonconst_p;
5922 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5923 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5925 else
5926 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5929 /* Look for the closing `]'. */
5930 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5932 /* Build the ARRAY_REF. */
5933 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5935 /* When not doing offsetof, array references are not permitted in
5936 constant-expressions. */
5937 if (!for_offsetof
5938 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5939 postfix_expression = error_mark_node;
5941 return postfix_expression;
5944 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5945 by cp_parser_builtin_offsetof. We're looking for
5947 postfix-expression . template [opt] id-expression
5948 postfix-expression . pseudo-destructor-name
5949 postfix-expression -> template [opt] id-expression
5950 postfix-expression -> pseudo-destructor-name
5952 FOR_OFFSETOF is set if we're being called in that context. That sorta
5953 limits what of the above we'll actually accept, but nevermind.
5954 TOKEN_TYPE is the "." or "->" token, which will already have been
5955 removed from the stream. */
5957 static tree
5958 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5959 enum cpp_ttype token_type,
5960 tree postfix_expression,
5961 bool for_offsetof, cp_id_kind *idk,
5962 location_t location)
5964 tree name;
5965 bool dependent_p;
5966 bool pseudo_destructor_p;
5967 tree scope = NULL_TREE;
5969 /* If this is a `->' operator, dereference the pointer. */
5970 if (token_type == CPP_DEREF)
5971 postfix_expression = build_x_arrow (location, postfix_expression,
5972 tf_warning_or_error);
5973 /* Check to see whether or not the expression is type-dependent. */
5974 dependent_p = type_dependent_expression_p (postfix_expression);
5975 /* The identifier following the `->' or `.' is not qualified. */
5976 parser->scope = NULL_TREE;
5977 parser->qualifying_scope = NULL_TREE;
5978 parser->object_scope = NULL_TREE;
5979 *idk = CP_ID_KIND_NONE;
5981 /* Enter the scope corresponding to the type of the object
5982 given by the POSTFIX_EXPRESSION. */
5983 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5985 scope = TREE_TYPE (postfix_expression);
5986 /* According to the standard, no expression should ever have
5987 reference type. Unfortunately, we do not currently match
5988 the standard in this respect in that our internal representation
5989 of an expression may have reference type even when the standard
5990 says it does not. Therefore, we have to manually obtain the
5991 underlying type here. */
5992 scope = non_reference (scope);
5993 /* The type of the POSTFIX_EXPRESSION must be complete. */
5994 if (scope == unknown_type_node)
5996 error_at (location, "%qE does not have class type",
5997 postfix_expression);
5998 scope = NULL_TREE;
6000 /* Unlike the object expression in other contexts, *this is not
6001 required to be of complete type for purposes of class member
6002 access (5.2.5) outside the member function body. */
6003 else if (scope != current_class_ref
6004 && !(processing_template_decl && scope == current_class_type))
6005 scope = complete_type_or_else (scope, NULL_TREE);
6006 /* Let the name lookup machinery know that we are processing a
6007 class member access expression. */
6008 parser->context->object_type = scope;
6009 /* If something went wrong, we want to be able to discern that case,
6010 as opposed to the case where there was no SCOPE due to the type
6011 of expression being dependent. */
6012 if (!scope)
6013 scope = error_mark_node;
6014 /* If the SCOPE was erroneous, make the various semantic analysis
6015 functions exit quickly -- and without issuing additional error
6016 messages. */
6017 if (scope == error_mark_node)
6018 postfix_expression = error_mark_node;
6021 /* Assume this expression is not a pseudo-destructor access. */
6022 pseudo_destructor_p = false;
6024 /* If the SCOPE is a scalar type, then, if this is a valid program,
6025 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6026 is type dependent, it can be pseudo-destructor-name or something else.
6027 Try to parse it as pseudo-destructor-name first. */
6028 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6030 tree s;
6031 tree type;
6033 cp_parser_parse_tentatively (parser);
6034 /* Parse the pseudo-destructor-name. */
6035 s = NULL_TREE;
6036 cp_parser_pseudo_destructor_name (parser, &s, &type);
6037 if (dependent_p
6038 && (cp_parser_error_occurred (parser)
6039 || TREE_CODE (type) != TYPE_DECL
6040 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6041 cp_parser_abort_tentative_parse (parser);
6042 else if (cp_parser_parse_definitely (parser))
6044 pseudo_destructor_p = true;
6045 postfix_expression
6046 = finish_pseudo_destructor_expr (postfix_expression,
6047 s, TREE_TYPE (type));
6051 if (!pseudo_destructor_p)
6053 /* If the SCOPE is not a scalar type, we are looking at an
6054 ordinary class member access expression, rather than a
6055 pseudo-destructor-name. */
6056 bool template_p;
6057 cp_token *token = cp_lexer_peek_token (parser->lexer);
6058 /* Parse the id-expression. */
6059 name = (cp_parser_id_expression
6060 (parser,
6061 cp_parser_optional_template_keyword (parser),
6062 /*check_dependency_p=*/true,
6063 &template_p,
6064 /*declarator_p=*/false,
6065 /*optional_p=*/false));
6066 /* In general, build a SCOPE_REF if the member name is qualified.
6067 However, if the name was not dependent and has already been
6068 resolved; there is no need to build the SCOPE_REF. For example;
6070 struct X { void f(); };
6071 template <typename T> void f(T* t) { t->X::f(); }
6073 Even though "t" is dependent, "X::f" is not and has been resolved
6074 to a BASELINK; there is no need to include scope information. */
6076 /* But we do need to remember that there was an explicit scope for
6077 virtual function calls. */
6078 if (parser->scope)
6079 *idk = CP_ID_KIND_QUALIFIED;
6081 /* If the name is a template-id that names a type, we will get a
6082 TYPE_DECL here. That is invalid code. */
6083 if (TREE_CODE (name) == TYPE_DECL)
6085 error_at (token->location, "invalid use of %qD", name);
6086 postfix_expression = error_mark_node;
6088 else
6090 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6092 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6094 error_at (token->location, "%<%D::%D%> is not a class member",
6095 parser->scope, name);
6096 postfix_expression = error_mark_node;
6098 else
6099 name = build_qualified_name (/*type=*/NULL_TREE,
6100 parser->scope,
6101 name,
6102 template_p);
6103 parser->scope = NULL_TREE;
6104 parser->qualifying_scope = NULL_TREE;
6105 parser->object_scope = NULL_TREE;
6107 if (parser->scope && name && BASELINK_P (name))
6108 adjust_result_of_qualified_name_lookup
6109 (name, parser->scope, scope);
6110 postfix_expression
6111 = finish_class_member_access_expr (postfix_expression, name,
6112 template_p,
6113 tf_warning_or_error);
6117 /* We no longer need to look up names in the scope of the object on
6118 the left-hand side of the `.' or `->' operator. */
6119 parser->context->object_type = NULL_TREE;
6121 /* Outside of offsetof, these operators may not appear in
6122 constant-expressions. */
6123 if (!for_offsetof
6124 && (cp_parser_non_integral_constant_expression
6125 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6126 postfix_expression = error_mark_node;
6128 return postfix_expression;
6131 /* Parse a parenthesized expression-list.
6133 expression-list:
6134 assignment-expression
6135 expression-list, assignment-expression
6137 attribute-list:
6138 expression-list
6139 identifier
6140 identifier, expression-list
6142 CAST_P is true if this expression is the target of a cast.
6144 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6145 argument pack.
6147 Returns a vector of trees. Each element is a representation of an
6148 assignment-expression. NULL is returned if the ( and or ) are
6149 missing. An empty, but allocated, vector is returned on no
6150 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6151 if we are parsing an attribute list for an attribute that wants a
6152 plain identifier argument, normal_attr for an attribute that wants
6153 an expression, or non_attr if we aren't parsing an attribute list. If
6154 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6155 not all of the expressions in the list were constant. */
6157 static VEC(tree,gc) *
6158 cp_parser_parenthesized_expression_list (cp_parser* parser,
6159 int is_attribute_list,
6160 bool cast_p,
6161 bool allow_expansion_p,
6162 bool *non_constant_p)
6164 VEC(tree,gc) *expression_list;
6165 bool fold_expr_p = is_attribute_list != non_attr;
6166 tree identifier = NULL_TREE;
6167 bool saved_greater_than_is_operator_p;
6169 /* Assume all the expressions will be constant. */
6170 if (non_constant_p)
6171 *non_constant_p = false;
6173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6174 return NULL;
6176 expression_list = make_tree_vector ();
6178 /* Within a parenthesized expression, a `>' token is always
6179 the greater-than operator. */
6180 saved_greater_than_is_operator_p
6181 = parser->greater_than_is_operator_p;
6182 parser->greater_than_is_operator_p = true;
6184 /* Consume expressions until there are no more. */
6185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6186 while (true)
6188 tree expr;
6190 /* At the beginning of attribute lists, check to see if the
6191 next token is an identifier. */
6192 if (is_attribute_list == id_attr
6193 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6195 cp_token *token;
6197 /* Consume the identifier. */
6198 token = cp_lexer_consume_token (parser->lexer);
6199 /* Save the identifier. */
6200 identifier = token->u.value;
6202 else
6204 bool expr_non_constant_p;
6206 /* Parse the next assignment-expression. */
6207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6209 /* A braced-init-list. */
6210 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6211 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6212 if (non_constant_p && expr_non_constant_p)
6213 *non_constant_p = true;
6215 else if (non_constant_p)
6217 expr = (cp_parser_constant_expression
6218 (parser, /*allow_non_constant_p=*/true,
6219 &expr_non_constant_p));
6220 if (expr_non_constant_p)
6221 *non_constant_p = true;
6223 else
6224 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6226 if (fold_expr_p)
6227 expr = fold_non_dependent_expr (expr);
6229 /* If we have an ellipsis, then this is an expression
6230 expansion. */
6231 if (allow_expansion_p
6232 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6234 /* Consume the `...'. */
6235 cp_lexer_consume_token (parser->lexer);
6237 /* Build the argument pack. */
6238 expr = make_pack_expansion (expr);
6241 /* Add it to the list. We add error_mark_node
6242 expressions to the list, so that we can still tell if
6243 the correct form for a parenthesized expression-list
6244 is found. That gives better errors. */
6245 VEC_safe_push (tree, gc, expression_list, expr);
6247 if (expr == error_mark_node)
6248 goto skip_comma;
6251 /* After the first item, attribute lists look the same as
6252 expression lists. */
6253 is_attribute_list = non_attr;
6255 get_comma:;
6256 /* If the next token isn't a `,', then we are done. */
6257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6258 break;
6260 /* Otherwise, consume the `,' and keep going. */
6261 cp_lexer_consume_token (parser->lexer);
6264 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6266 int ending;
6268 skip_comma:;
6269 /* We try and resync to an unnested comma, as that will give the
6270 user better diagnostics. */
6271 ending = cp_parser_skip_to_closing_parenthesis (parser,
6272 /*recovering=*/true,
6273 /*or_comma=*/true,
6274 /*consume_paren=*/true);
6275 if (ending < 0)
6276 goto get_comma;
6277 if (!ending)
6279 parser->greater_than_is_operator_p
6280 = saved_greater_than_is_operator_p;
6281 return NULL;
6285 parser->greater_than_is_operator_p
6286 = saved_greater_than_is_operator_p;
6288 if (identifier)
6289 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6291 return expression_list;
6294 /* Parse a pseudo-destructor-name.
6296 pseudo-destructor-name:
6297 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6298 :: [opt] nested-name-specifier template template-id :: ~ type-name
6299 :: [opt] nested-name-specifier [opt] ~ type-name
6301 If either of the first two productions is used, sets *SCOPE to the
6302 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6303 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6304 or ERROR_MARK_NODE if the parse fails. */
6306 static void
6307 cp_parser_pseudo_destructor_name (cp_parser* parser,
6308 tree* scope,
6309 tree* type)
6311 bool nested_name_specifier_p;
6313 /* Assume that things will not work out. */
6314 *type = error_mark_node;
6316 /* Look for the optional `::' operator. */
6317 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6318 /* Look for the optional nested-name-specifier. */
6319 nested_name_specifier_p
6320 = (cp_parser_nested_name_specifier_opt (parser,
6321 /*typename_keyword_p=*/false,
6322 /*check_dependency_p=*/true,
6323 /*type_p=*/false,
6324 /*is_declaration=*/false)
6325 != NULL_TREE);
6326 /* Now, if we saw a nested-name-specifier, we might be doing the
6327 second production. */
6328 if (nested_name_specifier_p
6329 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6331 /* Consume the `template' keyword. */
6332 cp_lexer_consume_token (parser->lexer);
6333 /* Parse the template-id. */
6334 cp_parser_template_id (parser,
6335 /*template_keyword_p=*/true,
6336 /*check_dependency_p=*/false,
6337 class_type,
6338 /*is_declaration=*/true);
6339 /* Look for the `::' token. */
6340 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6342 /* If the next token is not a `~', then there might be some
6343 additional qualification. */
6344 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6346 /* At this point, we're looking for "type-name :: ~". The type-name
6347 must not be a class-name, since this is a pseudo-destructor. So,
6348 it must be either an enum-name, or a typedef-name -- both of which
6349 are just identifiers. So, we peek ahead to check that the "::"
6350 and "~" tokens are present; if they are not, then we can avoid
6351 calling type_name. */
6352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6353 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6354 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6356 cp_parser_error (parser, "non-scalar type");
6357 return;
6360 /* Look for the type-name. */
6361 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6362 if (*scope == error_mark_node)
6363 return;
6365 /* Look for the `::' token. */
6366 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6368 else
6369 *scope = NULL_TREE;
6371 /* Look for the `~'. */
6372 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6374 /* Once we see the ~, this has to be a pseudo-destructor. */
6375 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6376 cp_parser_commit_to_tentative_parse (parser);
6378 /* Look for the type-name again. We are not responsible for
6379 checking that it matches the first type-name. */
6380 *type = cp_parser_nonclass_name (parser);
6383 /* Parse a unary-expression.
6385 unary-expression:
6386 postfix-expression
6387 ++ cast-expression
6388 -- cast-expression
6389 unary-operator cast-expression
6390 sizeof unary-expression
6391 sizeof ( type-id )
6392 alignof ( type-id ) [C++0x]
6393 new-expression
6394 delete-expression
6396 GNU Extensions:
6398 unary-expression:
6399 __extension__ cast-expression
6400 __alignof__ unary-expression
6401 __alignof__ ( type-id )
6402 alignof unary-expression [C++0x]
6403 __real__ cast-expression
6404 __imag__ cast-expression
6405 && identifier
6407 ADDRESS_P is true iff the unary-expression is appearing as the
6408 operand of the `&' operator. CAST_P is true if this expression is
6409 the target of a cast.
6411 Returns a representation of the expression. */
6413 static tree
6414 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6415 cp_id_kind * pidk)
6417 cp_token *token;
6418 enum tree_code unary_operator;
6420 /* Peek at the next token. */
6421 token = cp_lexer_peek_token (parser->lexer);
6422 /* Some keywords give away the kind of expression. */
6423 if (token->type == CPP_KEYWORD)
6425 enum rid keyword = token->keyword;
6427 switch (keyword)
6429 case RID_ALIGNOF:
6430 case RID_SIZEOF:
6432 tree operand, ret;
6433 enum tree_code op;
6434 location_t first_loc;
6436 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6437 /* Consume the token. */
6438 cp_lexer_consume_token (parser->lexer);
6439 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6440 /* Parse the operand. */
6441 operand = cp_parser_sizeof_operand (parser, keyword);
6443 if (TYPE_P (operand))
6444 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6445 else
6447 /* ISO C++ defines alignof only with types, not with
6448 expressions. So pedwarn if alignof is used with a non-
6449 type expression. However, __alignof__ is ok. */
6450 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6451 pedwarn (token->location, OPT_Wpedantic,
6452 "ISO C++ does not allow %<alignof%> "
6453 "with a non-type");
6455 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6457 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6458 SIZEOF_EXPR with the original operand. */
6459 if (op == SIZEOF_EXPR && ret != error_mark_node)
6461 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6463 if (!processing_template_decl && TYPE_P (operand))
6465 ret = build_min (SIZEOF_EXPR, size_type_node,
6466 build1 (NOP_EXPR, operand,
6467 error_mark_node));
6468 SIZEOF_EXPR_TYPE_P (ret) = 1;
6470 else
6471 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6472 TREE_SIDE_EFFECTS (ret) = 0;
6473 TREE_READONLY (ret) = 1;
6475 SET_EXPR_LOCATION (ret, first_loc);
6477 return ret;
6480 case RID_NEW:
6481 return cp_parser_new_expression (parser);
6483 case RID_DELETE:
6484 return cp_parser_delete_expression (parser);
6486 case RID_EXTENSION:
6488 /* The saved value of the PEDANTIC flag. */
6489 int saved_pedantic;
6490 tree expr;
6492 /* Save away the PEDANTIC flag. */
6493 cp_parser_extension_opt (parser, &saved_pedantic);
6494 /* Parse the cast-expression. */
6495 expr = cp_parser_simple_cast_expression (parser);
6496 /* Restore the PEDANTIC flag. */
6497 pedantic = saved_pedantic;
6499 return expr;
6502 case RID_REALPART:
6503 case RID_IMAGPART:
6505 tree expression;
6507 /* Consume the `__real__' or `__imag__' token. */
6508 cp_lexer_consume_token (parser->lexer);
6509 /* Parse the cast-expression. */
6510 expression = cp_parser_simple_cast_expression (parser);
6511 /* Create the complete representation. */
6512 return build_x_unary_op (token->location,
6513 (keyword == RID_REALPART
6514 ? REALPART_EXPR : IMAGPART_EXPR),
6515 expression,
6516 tf_warning_or_error);
6518 break;
6520 case RID_TRANSACTION_ATOMIC:
6521 case RID_TRANSACTION_RELAXED:
6522 return cp_parser_transaction_expression (parser, keyword);
6524 case RID_NOEXCEPT:
6526 tree expr;
6527 const char *saved_message;
6528 bool saved_integral_constant_expression_p;
6529 bool saved_non_integral_constant_expression_p;
6530 bool saved_greater_than_is_operator_p;
6532 cp_lexer_consume_token (parser->lexer);
6533 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6535 saved_message = parser->type_definition_forbidden_message;
6536 parser->type_definition_forbidden_message
6537 = G_("types may not be defined in %<noexcept%> expressions");
6539 saved_integral_constant_expression_p
6540 = parser->integral_constant_expression_p;
6541 saved_non_integral_constant_expression_p
6542 = parser->non_integral_constant_expression_p;
6543 parser->integral_constant_expression_p = false;
6545 saved_greater_than_is_operator_p
6546 = parser->greater_than_is_operator_p;
6547 parser->greater_than_is_operator_p = true;
6549 ++cp_unevaluated_operand;
6550 ++c_inhibit_evaluation_warnings;
6551 expr = cp_parser_expression (parser, false, NULL);
6552 --c_inhibit_evaluation_warnings;
6553 --cp_unevaluated_operand;
6555 parser->greater_than_is_operator_p
6556 = saved_greater_than_is_operator_p;
6558 parser->integral_constant_expression_p
6559 = saved_integral_constant_expression_p;
6560 parser->non_integral_constant_expression_p
6561 = saved_non_integral_constant_expression_p;
6563 parser->type_definition_forbidden_message = saved_message;
6565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6566 return finish_noexcept_expr (expr, tf_warning_or_error);
6569 default:
6570 break;
6574 /* Look for the `:: new' and `:: delete', which also signal the
6575 beginning of a new-expression, or delete-expression,
6576 respectively. If the next token is `::', then it might be one of
6577 these. */
6578 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6580 enum rid keyword;
6582 /* See if the token after the `::' is one of the keywords in
6583 which we're interested. */
6584 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6585 /* If it's `new', we have a new-expression. */
6586 if (keyword == RID_NEW)
6587 return cp_parser_new_expression (parser);
6588 /* Similarly, for `delete'. */
6589 else if (keyword == RID_DELETE)
6590 return cp_parser_delete_expression (parser);
6593 /* Look for a unary operator. */
6594 unary_operator = cp_parser_unary_operator (token);
6595 /* The `++' and `--' operators can be handled similarly, even though
6596 they are not technically unary-operators in the grammar. */
6597 if (unary_operator == ERROR_MARK)
6599 if (token->type == CPP_PLUS_PLUS)
6600 unary_operator = PREINCREMENT_EXPR;
6601 else if (token->type == CPP_MINUS_MINUS)
6602 unary_operator = PREDECREMENT_EXPR;
6603 /* Handle the GNU address-of-label extension. */
6604 else if (cp_parser_allow_gnu_extensions_p (parser)
6605 && token->type == CPP_AND_AND)
6607 tree identifier;
6608 tree expression;
6609 location_t loc = token->location;
6611 /* Consume the '&&' token. */
6612 cp_lexer_consume_token (parser->lexer);
6613 /* Look for the identifier. */
6614 identifier = cp_parser_identifier (parser);
6615 /* Create an expression representing the address. */
6616 expression = finish_label_address_expr (identifier, loc);
6617 if (cp_parser_non_integral_constant_expression (parser,
6618 NIC_ADDR_LABEL))
6619 expression = error_mark_node;
6620 return expression;
6623 if (unary_operator != ERROR_MARK)
6625 tree cast_expression;
6626 tree expression = error_mark_node;
6627 non_integral_constant non_constant_p = NIC_NONE;
6628 location_t loc = token->location;
6630 /* Consume the operator token. */
6631 token = cp_lexer_consume_token (parser->lexer);
6632 /* Parse the cast-expression. */
6633 cast_expression
6634 = cp_parser_cast_expression (parser,
6635 unary_operator == ADDR_EXPR,
6636 /*cast_p=*/false, pidk);
6637 /* Now, build an appropriate representation. */
6638 switch (unary_operator)
6640 case INDIRECT_REF:
6641 non_constant_p = NIC_STAR;
6642 expression = build_x_indirect_ref (loc, cast_expression,
6643 RO_UNARY_STAR,
6644 tf_warning_or_error);
6645 break;
6647 case ADDR_EXPR:
6648 non_constant_p = NIC_ADDR;
6649 /* Fall through. */
6650 case BIT_NOT_EXPR:
6651 expression = build_x_unary_op (loc, unary_operator,
6652 cast_expression,
6653 tf_warning_or_error);
6654 break;
6656 case PREINCREMENT_EXPR:
6657 case PREDECREMENT_EXPR:
6658 non_constant_p = unary_operator == PREINCREMENT_EXPR
6659 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6660 /* Fall through. */
6661 case UNARY_PLUS_EXPR:
6662 case NEGATE_EXPR:
6663 case TRUTH_NOT_EXPR:
6664 expression = finish_unary_op_expr (loc, unary_operator,
6665 cast_expression);
6666 break;
6668 default:
6669 gcc_unreachable ();
6672 if (non_constant_p != NIC_NONE
6673 && cp_parser_non_integral_constant_expression (parser,
6674 non_constant_p))
6675 expression = error_mark_node;
6677 return expression;
6680 return cp_parser_postfix_expression (parser, address_p, cast_p,
6681 /*member_access_only_p=*/false,
6682 pidk);
6685 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6686 unary-operator, the corresponding tree code is returned. */
6688 static enum tree_code
6689 cp_parser_unary_operator (cp_token* token)
6691 switch (token->type)
6693 case CPP_MULT:
6694 return INDIRECT_REF;
6696 case CPP_AND:
6697 return ADDR_EXPR;
6699 case CPP_PLUS:
6700 return UNARY_PLUS_EXPR;
6702 case CPP_MINUS:
6703 return NEGATE_EXPR;
6705 case CPP_NOT:
6706 return TRUTH_NOT_EXPR;
6708 case CPP_COMPL:
6709 return BIT_NOT_EXPR;
6711 default:
6712 return ERROR_MARK;
6716 /* Parse a new-expression.
6718 new-expression:
6719 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6720 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6722 Returns a representation of the expression. */
6724 static tree
6725 cp_parser_new_expression (cp_parser* parser)
6727 bool global_scope_p;
6728 VEC(tree,gc) *placement;
6729 tree type;
6730 VEC(tree,gc) *initializer;
6731 tree nelts = NULL_TREE;
6732 tree ret;
6734 /* Look for the optional `::' operator. */
6735 global_scope_p
6736 = (cp_parser_global_scope_opt (parser,
6737 /*current_scope_valid_p=*/false)
6738 != NULL_TREE);
6739 /* Look for the `new' operator. */
6740 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6741 /* There's no easy way to tell a new-placement from the
6742 `( type-id )' construct. */
6743 cp_parser_parse_tentatively (parser);
6744 /* Look for a new-placement. */
6745 placement = cp_parser_new_placement (parser);
6746 /* If that didn't work out, there's no new-placement. */
6747 if (!cp_parser_parse_definitely (parser))
6749 if (placement != NULL)
6750 release_tree_vector (placement);
6751 placement = NULL;
6754 /* If the next token is a `(', then we have a parenthesized
6755 type-id. */
6756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6758 cp_token *token;
6759 const char *saved_message = parser->type_definition_forbidden_message;
6761 /* Consume the `('. */
6762 cp_lexer_consume_token (parser->lexer);
6764 /* Parse the type-id. */
6765 parser->type_definition_forbidden_message
6766 = G_("types may not be defined in a new-expression");
6767 type = cp_parser_type_id (parser);
6768 parser->type_definition_forbidden_message = saved_message;
6770 /* Look for the closing `)'. */
6771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6772 token = cp_lexer_peek_token (parser->lexer);
6773 /* There should not be a direct-new-declarator in this production,
6774 but GCC used to allowed this, so we check and emit a sensible error
6775 message for this case. */
6776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6778 error_at (token->location,
6779 "array bound forbidden after parenthesized type-id");
6780 inform (token->location,
6781 "try removing the parentheses around the type-id");
6782 cp_parser_direct_new_declarator (parser);
6785 /* Otherwise, there must be a new-type-id. */
6786 else
6787 type = cp_parser_new_type_id (parser, &nelts);
6789 /* If the next token is a `(' or '{', then we have a new-initializer. */
6790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6791 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6792 initializer = cp_parser_new_initializer (parser);
6793 else
6794 initializer = NULL;
6796 /* A new-expression may not appear in an integral constant
6797 expression. */
6798 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6799 ret = error_mark_node;
6800 else
6802 /* Create a representation of the new-expression. */
6803 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6804 tf_warning_or_error);
6807 if (placement != NULL)
6808 release_tree_vector (placement);
6809 if (initializer != NULL)
6810 release_tree_vector (initializer);
6812 return ret;
6815 /* Parse a new-placement.
6817 new-placement:
6818 ( expression-list )
6820 Returns the same representation as for an expression-list. */
6822 static VEC(tree,gc) *
6823 cp_parser_new_placement (cp_parser* parser)
6825 VEC(tree,gc) *expression_list;
6827 /* Parse the expression-list. */
6828 expression_list = (cp_parser_parenthesized_expression_list
6829 (parser, non_attr, /*cast_p=*/false,
6830 /*allow_expansion_p=*/true,
6831 /*non_constant_p=*/NULL));
6833 return expression_list;
6836 /* Parse a new-type-id.
6838 new-type-id:
6839 type-specifier-seq new-declarator [opt]
6841 Returns the TYPE allocated. If the new-type-id indicates an array
6842 type, *NELTS is set to the number of elements in the last array
6843 bound; the TYPE will not include the last array bound. */
6845 static tree
6846 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6848 cp_decl_specifier_seq type_specifier_seq;
6849 cp_declarator *new_declarator;
6850 cp_declarator *declarator;
6851 cp_declarator *outer_declarator;
6852 const char *saved_message;
6854 /* The type-specifier sequence must not contain type definitions.
6855 (It cannot contain declarations of new types either, but if they
6856 are not definitions we will catch that because they are not
6857 complete.) */
6858 saved_message = parser->type_definition_forbidden_message;
6859 parser->type_definition_forbidden_message
6860 = G_("types may not be defined in a new-type-id");
6861 /* Parse the type-specifier-seq. */
6862 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6863 /*is_trailing_return=*/false,
6864 &type_specifier_seq);
6865 /* Restore the old message. */
6866 parser->type_definition_forbidden_message = saved_message;
6868 if (type_specifier_seq.type == error_mark_node)
6869 return error_mark_node;
6871 /* Parse the new-declarator. */
6872 new_declarator = cp_parser_new_declarator_opt (parser);
6874 /* Determine the number of elements in the last array dimension, if
6875 any. */
6876 *nelts = NULL_TREE;
6877 /* Skip down to the last array dimension. */
6878 declarator = new_declarator;
6879 outer_declarator = NULL;
6880 while (declarator && (declarator->kind == cdk_pointer
6881 || declarator->kind == cdk_ptrmem))
6883 outer_declarator = declarator;
6884 declarator = declarator->declarator;
6886 while (declarator
6887 && declarator->kind == cdk_array
6888 && declarator->declarator
6889 && declarator->declarator->kind == cdk_array)
6891 outer_declarator = declarator;
6892 declarator = declarator->declarator;
6895 if (declarator && declarator->kind == cdk_array)
6897 *nelts = declarator->u.array.bounds;
6898 if (*nelts == error_mark_node)
6899 *nelts = integer_one_node;
6901 if (outer_declarator)
6902 outer_declarator->declarator = declarator->declarator;
6903 else
6904 new_declarator = NULL;
6907 return groktypename (&type_specifier_seq, new_declarator, false);
6910 /* Parse an (optional) new-declarator.
6912 new-declarator:
6913 ptr-operator new-declarator [opt]
6914 direct-new-declarator
6916 Returns the declarator. */
6918 static cp_declarator *
6919 cp_parser_new_declarator_opt (cp_parser* parser)
6921 enum tree_code code;
6922 tree type, std_attributes = NULL_TREE;
6923 cp_cv_quals cv_quals;
6925 /* We don't know if there's a ptr-operator next, or not. */
6926 cp_parser_parse_tentatively (parser);
6927 /* Look for a ptr-operator. */
6928 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6929 /* If that worked, look for more new-declarators. */
6930 if (cp_parser_parse_definitely (parser))
6932 cp_declarator *declarator;
6934 /* Parse another optional declarator. */
6935 declarator = cp_parser_new_declarator_opt (parser);
6937 declarator = cp_parser_make_indirect_declarator
6938 (code, type, cv_quals, declarator, std_attributes);
6940 return declarator;
6943 /* If the next token is a `[', there is a direct-new-declarator. */
6944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6945 return cp_parser_direct_new_declarator (parser);
6947 return NULL;
6950 /* Parse a direct-new-declarator.
6952 direct-new-declarator:
6953 [ expression ]
6954 direct-new-declarator [constant-expression]
6958 static cp_declarator *
6959 cp_parser_direct_new_declarator (cp_parser* parser)
6961 cp_declarator *declarator = NULL;
6963 while (true)
6965 tree expression;
6966 cp_token *token;
6968 /* Look for the opening `['. */
6969 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6971 token = cp_lexer_peek_token (parser->lexer);
6972 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6973 /* The standard requires that the expression have integral
6974 type. DR 74 adds enumeration types. We believe that the
6975 real intent is that these expressions be handled like the
6976 expression in a `switch' condition, which also allows
6977 classes with a single conversion to integral or
6978 enumeration type. */
6979 if (!processing_template_decl)
6981 expression
6982 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6983 expression,
6984 /*complain=*/true);
6985 if (!expression)
6987 error_at (token->location,
6988 "expression in new-declarator must have integral "
6989 "or enumeration type");
6990 expression = error_mark_node;
6994 /* Look for the closing `]'. */
6995 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6997 /* Add this bound to the declarator. */
6998 declarator = make_array_declarator (declarator, expression);
7000 /* If the next token is not a `[', then there are no more
7001 bounds. */
7002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7003 break;
7006 return declarator;
7009 /* Parse a new-initializer.
7011 new-initializer:
7012 ( expression-list [opt] )
7013 braced-init-list
7015 Returns a representation of the expression-list. */
7017 static VEC(tree,gc) *
7018 cp_parser_new_initializer (cp_parser* parser)
7020 VEC(tree,gc) *expression_list;
7022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7024 tree t;
7025 bool expr_non_constant_p;
7026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7027 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7028 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7029 expression_list = make_tree_vector_single (t);
7031 else
7032 expression_list = (cp_parser_parenthesized_expression_list
7033 (parser, non_attr, /*cast_p=*/false,
7034 /*allow_expansion_p=*/true,
7035 /*non_constant_p=*/NULL));
7037 return expression_list;
7040 /* Parse a delete-expression.
7042 delete-expression:
7043 :: [opt] delete cast-expression
7044 :: [opt] delete [ ] cast-expression
7046 Returns a representation of the expression. */
7048 static tree
7049 cp_parser_delete_expression (cp_parser* parser)
7051 bool global_scope_p;
7052 bool array_p;
7053 tree expression;
7055 /* Look for the optional `::' operator. */
7056 global_scope_p
7057 = (cp_parser_global_scope_opt (parser,
7058 /*current_scope_valid_p=*/false)
7059 != NULL_TREE);
7060 /* Look for the `delete' keyword. */
7061 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7062 /* See if the array syntax is in use. */
7063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7065 /* Consume the `[' token. */
7066 cp_lexer_consume_token (parser->lexer);
7067 /* Look for the `]' token. */
7068 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7069 /* Remember that this is the `[]' construct. */
7070 array_p = true;
7072 else
7073 array_p = false;
7075 /* Parse the cast-expression. */
7076 expression = cp_parser_simple_cast_expression (parser);
7078 /* A delete-expression may not appear in an integral constant
7079 expression. */
7080 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7081 return error_mark_node;
7083 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7084 tf_warning_or_error);
7087 /* Returns true if TOKEN may start a cast-expression and false
7088 otherwise. */
7090 static bool
7091 cp_parser_token_starts_cast_expression (cp_token *token)
7093 switch (token->type)
7095 case CPP_COMMA:
7096 case CPP_SEMICOLON:
7097 case CPP_QUERY:
7098 case CPP_COLON:
7099 case CPP_CLOSE_SQUARE:
7100 case CPP_CLOSE_PAREN:
7101 case CPP_CLOSE_BRACE:
7102 case CPP_DOT:
7103 case CPP_DOT_STAR:
7104 case CPP_DEREF:
7105 case CPP_DEREF_STAR:
7106 case CPP_DIV:
7107 case CPP_MOD:
7108 case CPP_LSHIFT:
7109 case CPP_RSHIFT:
7110 case CPP_LESS:
7111 case CPP_GREATER:
7112 case CPP_LESS_EQ:
7113 case CPP_GREATER_EQ:
7114 case CPP_EQ_EQ:
7115 case CPP_NOT_EQ:
7116 case CPP_EQ:
7117 case CPP_MULT_EQ:
7118 case CPP_DIV_EQ:
7119 case CPP_MOD_EQ:
7120 case CPP_PLUS_EQ:
7121 case CPP_MINUS_EQ:
7122 case CPP_RSHIFT_EQ:
7123 case CPP_LSHIFT_EQ:
7124 case CPP_AND_EQ:
7125 case CPP_XOR_EQ:
7126 case CPP_OR_EQ:
7127 case CPP_XOR:
7128 case CPP_OR:
7129 case CPP_OR_OR:
7130 case CPP_EOF:
7131 return false;
7133 /* '[' may start a primary-expression in obj-c++. */
7134 case CPP_OPEN_SQUARE:
7135 return c_dialect_objc ();
7137 default:
7138 return true;
7142 /* Parse a cast-expression.
7144 cast-expression:
7145 unary-expression
7146 ( type-id ) cast-expression
7148 ADDRESS_P is true iff the unary-expression is appearing as the
7149 operand of the `&' operator. CAST_P is true if this expression is
7150 the target of a cast.
7152 Returns a representation of the expression. */
7154 static tree
7155 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7156 cp_id_kind * pidk)
7158 /* If it's a `(', then we might be looking at a cast. */
7159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7161 tree type = NULL_TREE;
7162 tree expr = NULL_TREE;
7163 bool compound_literal_p;
7164 const char *saved_message;
7166 /* There's no way to know yet whether or not this is a cast.
7167 For example, `(int (3))' is a unary-expression, while `(int)
7168 3' is a cast. So, we resort to parsing tentatively. */
7169 cp_parser_parse_tentatively (parser);
7170 /* Types may not be defined in a cast. */
7171 saved_message = parser->type_definition_forbidden_message;
7172 parser->type_definition_forbidden_message
7173 = G_("types may not be defined in casts");
7174 /* Consume the `('. */
7175 cp_lexer_consume_token (parser->lexer);
7176 /* A very tricky bit is that `(struct S) { 3 }' is a
7177 compound-literal (which we permit in C++ as an extension).
7178 But, that construct is not a cast-expression -- it is a
7179 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7180 is legal; if the compound-literal were a cast-expression,
7181 you'd need an extra set of parentheses.) But, if we parse
7182 the type-id, and it happens to be a class-specifier, then we
7183 will commit to the parse at that point, because we cannot
7184 undo the action that is done when creating a new class. So,
7185 then we cannot back up and do a postfix-expression.
7187 Therefore, we scan ahead to the closing `)', and check to see
7188 if the token after the `)' is a `{'. If so, we are not
7189 looking at a cast-expression.
7191 Save tokens so that we can put them back. */
7192 cp_lexer_save_tokens (parser->lexer);
7193 /* Skip tokens until the next token is a closing parenthesis.
7194 If we find the closing `)', and the next token is a `{', then
7195 we are looking at a compound-literal. */
7196 compound_literal_p
7197 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7198 /*consume_paren=*/true)
7199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7200 /* Roll back the tokens we skipped. */
7201 cp_lexer_rollback_tokens (parser->lexer);
7202 /* If we were looking at a compound-literal, simulate an error
7203 so that the call to cp_parser_parse_definitely below will
7204 fail. */
7205 if (compound_literal_p)
7206 cp_parser_simulate_error (parser);
7207 else
7209 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7210 parser->in_type_id_in_expr_p = true;
7211 /* Look for the type-id. */
7212 type = cp_parser_type_id (parser);
7213 /* Look for the closing `)'. */
7214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7215 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7218 /* Restore the saved message. */
7219 parser->type_definition_forbidden_message = saved_message;
7221 /* At this point this can only be either a cast or a
7222 parenthesized ctor such as `(T ())' that looks like a cast to
7223 function returning T. */
7224 if (!cp_parser_error_occurred (parser)
7225 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7226 (parser->lexer)))
7228 cp_parser_parse_definitely (parser);
7229 expr = cp_parser_cast_expression (parser,
7230 /*address_p=*/false,
7231 /*cast_p=*/true, pidk);
7233 /* Warn about old-style casts, if so requested. */
7234 if (warn_old_style_cast
7235 && !in_system_header
7236 && !VOID_TYPE_P (type)
7237 && current_lang_name != lang_name_c)
7238 warning (OPT_Wold_style_cast, "use of old-style cast");
7240 /* Only type conversions to integral or enumeration types
7241 can be used in constant-expressions. */
7242 if (!cast_valid_in_integral_constant_expression_p (type)
7243 && cp_parser_non_integral_constant_expression (parser,
7244 NIC_CAST))
7245 return error_mark_node;
7247 /* Perform the cast. */
7248 expr = build_c_cast (input_location, type, expr);
7249 return expr;
7251 else
7252 cp_parser_abort_tentative_parse (parser);
7255 /* If we get here, then it's not a cast, so it must be a
7256 unary-expression. */
7257 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7260 /* Parse a binary expression of the general form:
7262 pm-expression:
7263 cast-expression
7264 pm-expression .* cast-expression
7265 pm-expression ->* cast-expression
7267 multiplicative-expression:
7268 pm-expression
7269 multiplicative-expression * pm-expression
7270 multiplicative-expression / pm-expression
7271 multiplicative-expression % pm-expression
7273 additive-expression:
7274 multiplicative-expression
7275 additive-expression + multiplicative-expression
7276 additive-expression - multiplicative-expression
7278 shift-expression:
7279 additive-expression
7280 shift-expression << additive-expression
7281 shift-expression >> additive-expression
7283 relational-expression:
7284 shift-expression
7285 relational-expression < shift-expression
7286 relational-expression > shift-expression
7287 relational-expression <= shift-expression
7288 relational-expression >= shift-expression
7290 GNU Extension:
7292 relational-expression:
7293 relational-expression <? shift-expression
7294 relational-expression >? shift-expression
7296 equality-expression:
7297 relational-expression
7298 equality-expression == relational-expression
7299 equality-expression != relational-expression
7301 and-expression:
7302 equality-expression
7303 and-expression & equality-expression
7305 exclusive-or-expression:
7306 and-expression
7307 exclusive-or-expression ^ and-expression
7309 inclusive-or-expression:
7310 exclusive-or-expression
7311 inclusive-or-expression | exclusive-or-expression
7313 logical-and-expression:
7314 inclusive-or-expression
7315 logical-and-expression && inclusive-or-expression
7317 logical-or-expression:
7318 logical-and-expression
7319 logical-or-expression || logical-and-expression
7321 All these are implemented with a single function like:
7323 binary-expression:
7324 simple-cast-expression
7325 binary-expression <token> binary-expression
7327 CAST_P is true if this expression is the target of a cast.
7329 The binops_by_token map is used to get the tree codes for each <token> type.
7330 binary-expressions are associated according to a precedence table. */
7332 #define TOKEN_PRECEDENCE(token) \
7333 (((token->type == CPP_GREATER \
7334 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7335 && !parser->greater_than_is_operator_p) \
7336 ? PREC_NOT_OPERATOR \
7337 : binops_by_token[token->type].prec)
7339 static tree
7340 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7341 bool no_toplevel_fold_p,
7342 enum cp_parser_prec prec,
7343 cp_id_kind * pidk)
7345 cp_parser_expression_stack stack;
7346 cp_parser_expression_stack_entry *sp = &stack[0];
7347 cp_parser_expression_stack_entry current;
7348 tree rhs;
7349 cp_token *token;
7350 enum tree_code rhs_type;
7351 enum cp_parser_prec new_prec, lookahead_prec;
7352 tree overload;
7354 /* Parse the first expression. */
7355 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7356 cast_p, pidk);
7357 current.lhs_type = ERROR_MARK;
7358 current.prec = prec;
7360 if (cp_parser_error_occurred (parser))
7361 return error_mark_node;
7363 for (;;)
7365 /* Get an operator token. */
7366 token = cp_lexer_peek_token (parser->lexer);
7368 if (warn_cxx0x_compat
7369 && token->type == CPP_RSHIFT
7370 && !parser->greater_than_is_operator_p)
7372 if (warning_at (token->location, OPT_Wc__0x_compat,
7373 "%<>>%> operator is treated"
7374 " as two right angle brackets in C++11"))
7375 inform (token->location,
7376 "suggest parentheses around %<>>%> expression");
7379 new_prec = TOKEN_PRECEDENCE (token);
7381 /* Popping an entry off the stack means we completed a subexpression:
7382 - either we found a token which is not an operator (`>' where it is not
7383 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7384 will happen repeatedly;
7385 - or, we found an operator which has lower priority. This is the case
7386 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7387 parsing `3 * 4'. */
7388 if (new_prec <= current.prec)
7390 if (sp == stack)
7391 break;
7392 else
7393 goto pop;
7396 get_rhs:
7397 current.tree_type = binops_by_token[token->type].tree_type;
7398 current.loc = token->location;
7400 /* We used the operator token. */
7401 cp_lexer_consume_token (parser->lexer);
7403 /* For "false && x" or "true || x", x will never be executed;
7404 disable warnings while evaluating it. */
7405 if (current.tree_type == TRUTH_ANDIF_EXPR)
7406 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7407 else if (current.tree_type == TRUTH_ORIF_EXPR)
7408 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7410 /* Extract another operand. It may be the RHS of this expression
7411 or the LHS of a new, higher priority expression. */
7412 rhs = cp_parser_simple_cast_expression (parser);
7413 rhs_type = ERROR_MARK;
7415 /* Get another operator token. Look up its precedence to avoid
7416 building a useless (immediately popped) stack entry for common
7417 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7418 token = cp_lexer_peek_token (parser->lexer);
7419 lookahead_prec = TOKEN_PRECEDENCE (token);
7420 if (lookahead_prec > new_prec)
7422 /* ... and prepare to parse the RHS of the new, higher priority
7423 expression. Since precedence levels on the stack are
7424 monotonically increasing, we do not have to care about
7425 stack overflows. */
7426 *sp = current;
7427 ++sp;
7428 current.lhs = rhs;
7429 current.lhs_type = rhs_type;
7430 current.prec = new_prec;
7431 new_prec = lookahead_prec;
7432 goto get_rhs;
7434 pop:
7435 lookahead_prec = new_prec;
7436 /* If the stack is not empty, we have parsed into LHS the right side
7437 (`4' in the example above) of an expression we had suspended.
7438 We can use the information on the stack to recover the LHS (`3')
7439 from the stack together with the tree code (`MULT_EXPR'), and
7440 the precedence of the higher level subexpression
7441 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7442 which will be used to actually build the additive expression. */
7443 rhs = current.lhs;
7444 rhs_type = current.lhs_type;
7445 --sp;
7446 current = *sp;
7449 /* Undo the disabling of warnings done above. */
7450 if (current.tree_type == TRUTH_ANDIF_EXPR)
7451 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7452 else if (current.tree_type == TRUTH_ORIF_EXPR)
7453 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7455 overload = NULL;
7456 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7457 ERROR_MARK for everything that is not a binary expression.
7458 This makes warn_about_parentheses miss some warnings that
7459 involve unary operators. For unary expressions we should
7460 pass the correct tree_code unless the unary expression was
7461 surrounded by parentheses.
7463 if (no_toplevel_fold_p
7464 && lookahead_prec <= current.prec
7465 && sp == stack
7466 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7467 current.lhs = build2 (current.tree_type, boolean_type_node,
7468 current.lhs, rhs);
7469 else
7470 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7471 current.lhs, current.lhs_type,
7472 rhs, rhs_type, &overload,
7473 tf_warning_or_error);
7474 current.lhs_type = current.tree_type;
7476 /* If the binary operator required the use of an overloaded operator,
7477 then this expression cannot be an integral constant-expression.
7478 An overloaded operator can be used even if both operands are
7479 otherwise permissible in an integral constant-expression if at
7480 least one of the operands is of enumeration type. */
7482 if (overload
7483 && cp_parser_non_integral_constant_expression (parser,
7484 NIC_OVERLOADED))
7485 return error_mark_node;
7488 return current.lhs;
7492 /* Parse the `? expression : assignment-expression' part of a
7493 conditional-expression. The LOGICAL_OR_EXPR is the
7494 logical-or-expression that started the conditional-expression.
7495 Returns a representation of the entire conditional-expression.
7497 This routine is used by cp_parser_assignment_expression.
7499 ? expression : assignment-expression
7501 GNU Extensions:
7503 ? : assignment-expression */
7505 static tree
7506 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7508 tree expr;
7509 tree assignment_expr;
7510 struct cp_token *token;
7511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7513 /* Consume the `?' token. */
7514 cp_lexer_consume_token (parser->lexer);
7515 token = cp_lexer_peek_token (parser->lexer);
7516 if (cp_parser_allow_gnu_extensions_p (parser)
7517 && token->type == CPP_COLON)
7519 pedwarn (token->location, OPT_Wpedantic,
7520 "ISO C++ does not allow ?: with omitted middle operand");
7521 /* Implicit true clause. */
7522 expr = NULL_TREE;
7523 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7524 warn_for_omitted_condop (token->location, logical_or_expr);
7526 else
7528 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7529 parser->colon_corrects_to_scope_p = false;
7530 /* Parse the expression. */
7531 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7532 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7533 c_inhibit_evaluation_warnings +=
7534 ((logical_or_expr == truthvalue_true_node)
7535 - (logical_or_expr == truthvalue_false_node));
7536 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7539 /* The next token should be a `:'. */
7540 cp_parser_require (parser, CPP_COLON, RT_COLON);
7541 /* Parse the assignment-expression. */
7542 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7543 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7545 /* Build the conditional-expression. */
7546 return build_x_conditional_expr (loc, logical_or_expr,
7547 expr,
7548 assignment_expr,
7549 tf_warning_or_error);
7552 /* Parse an assignment-expression.
7554 assignment-expression:
7555 conditional-expression
7556 logical-or-expression assignment-operator assignment_expression
7557 throw-expression
7559 CAST_P is true if this expression is the target of a cast.
7561 Returns a representation for the expression. */
7563 static tree
7564 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7565 cp_id_kind * pidk)
7567 tree expr;
7569 /* If the next token is the `throw' keyword, then we're looking at
7570 a throw-expression. */
7571 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7572 expr = cp_parser_throw_expression (parser);
7573 /* Otherwise, it must be that we are looking at a
7574 logical-or-expression. */
7575 else
7577 /* Parse the binary expressions (logical-or-expression). */
7578 expr = cp_parser_binary_expression (parser, cast_p, false,
7579 PREC_NOT_OPERATOR, pidk);
7580 /* If the next token is a `?' then we're actually looking at a
7581 conditional-expression. */
7582 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7583 return cp_parser_question_colon_clause (parser, expr);
7584 else
7586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7588 /* If it's an assignment-operator, we're using the second
7589 production. */
7590 enum tree_code assignment_operator
7591 = cp_parser_assignment_operator_opt (parser);
7592 if (assignment_operator != ERROR_MARK)
7594 bool non_constant_p;
7595 location_t saved_input_location;
7597 /* Parse the right-hand side of the assignment. */
7598 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7600 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7601 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7603 /* An assignment may not appear in a
7604 constant-expression. */
7605 if (cp_parser_non_integral_constant_expression (parser,
7606 NIC_ASSIGNMENT))
7607 return error_mark_node;
7608 /* Build the assignment expression. Its default
7609 location is the location of the '=' token. */
7610 saved_input_location = input_location;
7611 input_location = loc;
7612 expr = build_x_modify_expr (loc, expr,
7613 assignment_operator,
7614 rhs,
7615 tf_warning_or_error);
7616 input_location = saved_input_location;
7621 return expr;
7624 /* Parse an (optional) assignment-operator.
7626 assignment-operator: one of
7627 = *= /= %= += -= >>= <<= &= ^= |=
7629 GNU Extension:
7631 assignment-operator: one of
7632 <?= >?=
7634 If the next token is an assignment operator, the corresponding tree
7635 code is returned, and the token is consumed. For example, for
7636 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7637 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7638 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7639 operator, ERROR_MARK is returned. */
7641 static enum tree_code
7642 cp_parser_assignment_operator_opt (cp_parser* parser)
7644 enum tree_code op;
7645 cp_token *token;
7647 /* Peek at the next token. */
7648 token = cp_lexer_peek_token (parser->lexer);
7650 switch (token->type)
7652 case CPP_EQ:
7653 op = NOP_EXPR;
7654 break;
7656 case CPP_MULT_EQ:
7657 op = MULT_EXPR;
7658 break;
7660 case CPP_DIV_EQ:
7661 op = TRUNC_DIV_EXPR;
7662 break;
7664 case CPP_MOD_EQ:
7665 op = TRUNC_MOD_EXPR;
7666 break;
7668 case CPP_PLUS_EQ:
7669 op = PLUS_EXPR;
7670 break;
7672 case CPP_MINUS_EQ:
7673 op = MINUS_EXPR;
7674 break;
7676 case CPP_RSHIFT_EQ:
7677 op = RSHIFT_EXPR;
7678 break;
7680 case CPP_LSHIFT_EQ:
7681 op = LSHIFT_EXPR;
7682 break;
7684 case CPP_AND_EQ:
7685 op = BIT_AND_EXPR;
7686 break;
7688 case CPP_XOR_EQ:
7689 op = BIT_XOR_EXPR;
7690 break;
7692 case CPP_OR_EQ:
7693 op = BIT_IOR_EXPR;
7694 break;
7696 default:
7697 /* Nothing else is an assignment operator. */
7698 op = ERROR_MARK;
7701 /* If it was an assignment operator, consume it. */
7702 if (op != ERROR_MARK)
7703 cp_lexer_consume_token (parser->lexer);
7705 return op;
7708 /* Parse an expression.
7710 expression:
7711 assignment-expression
7712 expression , assignment-expression
7714 CAST_P is true if this expression is the target of a cast.
7716 Returns a representation of the expression. */
7718 static tree
7719 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7721 tree expression = NULL_TREE;
7722 location_t loc = UNKNOWN_LOCATION;
7724 while (true)
7726 tree assignment_expression;
7728 /* Parse the next assignment-expression. */
7729 assignment_expression
7730 = cp_parser_assignment_expression (parser, cast_p, pidk);
7731 /* If this is the first assignment-expression, we can just
7732 save it away. */
7733 if (!expression)
7734 expression = assignment_expression;
7735 else
7736 expression = build_x_compound_expr (loc, expression,
7737 assignment_expression,
7738 tf_warning_or_error);
7739 /* If the next token is not a comma, then we are done with the
7740 expression. */
7741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7742 break;
7743 /* Consume the `,'. */
7744 loc = cp_lexer_peek_token (parser->lexer)->location;
7745 cp_lexer_consume_token (parser->lexer);
7746 /* A comma operator cannot appear in a constant-expression. */
7747 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7748 expression = error_mark_node;
7751 return expression;
7754 /* Parse a constant-expression.
7756 constant-expression:
7757 conditional-expression
7759 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7760 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7761 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7762 is false, NON_CONSTANT_P should be NULL. */
7764 static tree
7765 cp_parser_constant_expression (cp_parser* parser,
7766 bool allow_non_constant_p,
7767 bool *non_constant_p)
7769 bool saved_integral_constant_expression_p;
7770 bool saved_allow_non_integral_constant_expression_p;
7771 bool saved_non_integral_constant_expression_p;
7772 tree expression;
7774 /* It might seem that we could simply parse the
7775 conditional-expression, and then check to see if it were
7776 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7777 one that the compiler can figure out is constant, possibly after
7778 doing some simplifications or optimizations. The standard has a
7779 precise definition of constant-expression, and we must honor
7780 that, even though it is somewhat more restrictive.
7782 For example:
7784 int i[(2, 3)];
7786 is not a legal declaration, because `(2, 3)' is not a
7787 constant-expression. The `,' operator is forbidden in a
7788 constant-expression. However, GCC's constant-folding machinery
7789 will fold this operation to an INTEGER_CST for `3'. */
7791 /* Save the old settings. */
7792 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7793 saved_allow_non_integral_constant_expression_p
7794 = parser->allow_non_integral_constant_expression_p;
7795 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7796 /* We are now parsing a constant-expression. */
7797 parser->integral_constant_expression_p = true;
7798 parser->allow_non_integral_constant_expression_p
7799 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7800 parser->non_integral_constant_expression_p = false;
7801 /* Although the grammar says "conditional-expression", we parse an
7802 "assignment-expression", which also permits "throw-expression"
7803 and the use of assignment operators. In the case that
7804 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7805 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7806 actually essential that we look for an assignment-expression.
7807 For example, cp_parser_initializer_clauses uses this function to
7808 determine whether a particular assignment-expression is in fact
7809 constant. */
7810 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7811 /* Restore the old settings. */
7812 parser->integral_constant_expression_p
7813 = saved_integral_constant_expression_p;
7814 parser->allow_non_integral_constant_expression_p
7815 = saved_allow_non_integral_constant_expression_p;
7816 if (cxx_dialect >= cxx0x)
7818 /* Require an rvalue constant expression here; that's what our
7819 callers expect. Reference constant expressions are handled
7820 separately in e.g. cp_parser_template_argument. */
7821 bool is_const = potential_rvalue_constant_expression (expression);
7822 parser->non_integral_constant_expression_p = !is_const;
7823 if (!is_const && !allow_non_constant_p)
7824 require_potential_rvalue_constant_expression (expression);
7826 if (allow_non_constant_p)
7827 *non_constant_p = parser->non_integral_constant_expression_p;
7828 parser->non_integral_constant_expression_p
7829 = saved_non_integral_constant_expression_p;
7831 return expression;
7834 /* Parse __builtin_offsetof.
7836 offsetof-expression:
7837 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7839 offsetof-member-designator:
7840 id-expression
7841 | offsetof-member-designator "." id-expression
7842 | offsetof-member-designator "[" expression "]"
7843 | offsetof-member-designator "->" id-expression */
7845 static tree
7846 cp_parser_builtin_offsetof (cp_parser *parser)
7848 int save_ice_p, save_non_ice_p;
7849 tree type, expr;
7850 cp_id_kind dummy;
7851 cp_token *token;
7853 /* We're about to accept non-integral-constant things, but will
7854 definitely yield an integral constant expression. Save and
7855 restore these values around our local parsing. */
7856 save_ice_p = parser->integral_constant_expression_p;
7857 save_non_ice_p = parser->non_integral_constant_expression_p;
7859 /* Consume the "__builtin_offsetof" token. */
7860 cp_lexer_consume_token (parser->lexer);
7861 /* Consume the opening `('. */
7862 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7863 /* Parse the type-id. */
7864 type = cp_parser_type_id (parser);
7865 /* Look for the `,'. */
7866 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7867 token = cp_lexer_peek_token (parser->lexer);
7869 /* Build the (type *)null that begins the traditional offsetof macro. */
7870 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7871 tf_warning_or_error);
7873 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7874 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7875 true, &dummy, token->location);
7876 while (true)
7878 token = cp_lexer_peek_token (parser->lexer);
7879 switch (token->type)
7881 case CPP_OPEN_SQUARE:
7882 /* offsetof-member-designator "[" expression "]" */
7883 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7884 break;
7886 case CPP_DEREF:
7887 /* offsetof-member-designator "->" identifier */
7888 expr = grok_array_decl (token->location, expr, integer_zero_node);
7889 /* FALLTHRU */
7891 case CPP_DOT:
7892 /* offsetof-member-designator "." identifier */
7893 cp_lexer_consume_token (parser->lexer);
7894 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7895 expr, true, &dummy,
7896 token->location);
7897 break;
7899 case CPP_CLOSE_PAREN:
7900 /* Consume the ")" token. */
7901 cp_lexer_consume_token (parser->lexer);
7902 goto success;
7904 default:
7905 /* Error. We know the following require will fail, but
7906 that gives the proper error message. */
7907 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7908 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7909 expr = error_mark_node;
7910 goto failure;
7914 success:
7915 /* If we're processing a template, we can't finish the semantics yet.
7916 Otherwise we can fold the entire expression now. */
7917 if (processing_template_decl)
7918 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7919 else
7920 expr = finish_offsetof (expr);
7922 failure:
7923 parser->integral_constant_expression_p = save_ice_p;
7924 parser->non_integral_constant_expression_p = save_non_ice_p;
7926 return expr;
7929 /* Parse a trait expression.
7931 Returns a representation of the expression, the underlying type
7932 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7934 static tree
7935 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7937 cp_trait_kind kind;
7938 tree type1, type2 = NULL_TREE;
7939 bool binary = false;
7940 cp_decl_specifier_seq decl_specs;
7942 switch (keyword)
7944 case RID_HAS_NOTHROW_ASSIGN:
7945 kind = CPTK_HAS_NOTHROW_ASSIGN;
7946 break;
7947 case RID_HAS_NOTHROW_CONSTRUCTOR:
7948 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7949 break;
7950 case RID_HAS_NOTHROW_COPY:
7951 kind = CPTK_HAS_NOTHROW_COPY;
7952 break;
7953 case RID_HAS_TRIVIAL_ASSIGN:
7954 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7955 break;
7956 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7957 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7958 break;
7959 case RID_HAS_TRIVIAL_COPY:
7960 kind = CPTK_HAS_TRIVIAL_COPY;
7961 break;
7962 case RID_HAS_TRIVIAL_DESTRUCTOR:
7963 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7964 break;
7965 case RID_HAS_VIRTUAL_DESTRUCTOR:
7966 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7967 break;
7968 case RID_IS_ABSTRACT:
7969 kind = CPTK_IS_ABSTRACT;
7970 break;
7971 case RID_IS_BASE_OF:
7972 kind = CPTK_IS_BASE_OF;
7973 binary = true;
7974 break;
7975 case RID_IS_CLASS:
7976 kind = CPTK_IS_CLASS;
7977 break;
7978 case RID_IS_CONVERTIBLE_TO:
7979 kind = CPTK_IS_CONVERTIBLE_TO;
7980 binary = true;
7981 break;
7982 case RID_IS_EMPTY:
7983 kind = CPTK_IS_EMPTY;
7984 break;
7985 case RID_IS_ENUM:
7986 kind = CPTK_IS_ENUM;
7987 break;
7988 case RID_IS_FINAL:
7989 kind = CPTK_IS_FINAL;
7990 break;
7991 case RID_IS_LITERAL_TYPE:
7992 kind = CPTK_IS_LITERAL_TYPE;
7993 break;
7994 case RID_IS_POD:
7995 kind = CPTK_IS_POD;
7996 break;
7997 case RID_IS_POLYMORPHIC:
7998 kind = CPTK_IS_POLYMORPHIC;
7999 break;
8000 case RID_IS_STD_LAYOUT:
8001 kind = CPTK_IS_STD_LAYOUT;
8002 break;
8003 case RID_IS_TRIVIAL:
8004 kind = CPTK_IS_TRIVIAL;
8005 break;
8006 case RID_IS_UNION:
8007 kind = CPTK_IS_UNION;
8008 break;
8009 case RID_UNDERLYING_TYPE:
8010 kind = CPTK_UNDERLYING_TYPE;
8011 break;
8012 case RID_BASES:
8013 kind = CPTK_BASES;
8014 break;
8015 case RID_DIRECT_BASES:
8016 kind = CPTK_DIRECT_BASES;
8017 break;
8018 default:
8019 gcc_unreachable ();
8022 /* Consume the token. */
8023 cp_lexer_consume_token (parser->lexer);
8025 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8027 type1 = cp_parser_type_id (parser);
8029 if (type1 == error_mark_node)
8030 return error_mark_node;
8032 /* Build a trivial decl-specifier-seq. */
8033 clear_decl_specs (&decl_specs);
8034 decl_specs.type = type1;
8036 /* Call grokdeclarator to figure out what type this is. */
8037 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8038 /*initialized=*/0, /*attrlist=*/NULL);
8040 if (binary)
8042 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8044 type2 = cp_parser_type_id (parser);
8046 if (type2 == error_mark_node)
8047 return error_mark_node;
8049 /* Build a trivial decl-specifier-seq. */
8050 clear_decl_specs (&decl_specs);
8051 decl_specs.type = type2;
8053 /* Call grokdeclarator to figure out what type this is. */
8054 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8055 /*initialized=*/0, /*attrlist=*/NULL);
8058 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8060 /* Complete the trait expression, which may mean either processing
8061 the trait expr now or saving it for template instantiation. */
8062 switch(kind)
8064 case CPTK_UNDERLYING_TYPE:
8065 return finish_underlying_type (type1);
8066 case CPTK_BASES:
8067 return finish_bases (type1, false);
8068 case CPTK_DIRECT_BASES:
8069 return finish_bases (type1, true);
8070 default:
8071 return finish_trait_expr (kind, type1, type2);
8075 /* Lambdas that appear in variable initializer or default argument scope
8076 get that in their mangling, so we need to record it. We might as well
8077 use the count for function and namespace scopes as well. */
8078 static GTY(()) tree lambda_scope;
8079 static GTY(()) int lambda_count;
8080 typedef struct GTY(()) tree_int
8082 tree t;
8083 int i;
8084 } tree_int;
8085 DEF_VEC_O(tree_int);
8086 DEF_VEC_ALLOC_O(tree_int,gc);
8087 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8089 static void
8090 start_lambda_scope (tree decl)
8092 tree_int ti;
8093 gcc_assert (decl);
8094 /* Once we're inside a function, we ignore other scopes and just push
8095 the function again so that popping works properly. */
8096 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8097 decl = current_function_decl;
8098 ti.t = lambda_scope;
8099 ti.i = lambda_count;
8100 VEC_safe_push (tree_int, gc, lambda_scope_stack, ti);
8101 if (lambda_scope != decl)
8103 /* Don't reset the count if we're still in the same function. */
8104 lambda_scope = decl;
8105 lambda_count = 0;
8109 static void
8110 record_lambda_scope (tree lambda)
8112 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8113 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8116 static void
8117 finish_lambda_scope (void)
8119 tree_int *p = &VEC_last (tree_int, lambda_scope_stack);
8120 if (lambda_scope != p->t)
8122 lambda_scope = p->t;
8123 lambda_count = p->i;
8125 VEC_pop (tree_int, lambda_scope_stack);
8128 /* Parse a lambda expression.
8130 lambda-expression:
8131 lambda-introducer lambda-declarator [opt] compound-statement
8133 Returns a representation of the expression. */
8135 static tree
8136 cp_parser_lambda_expression (cp_parser* parser)
8138 tree lambda_expr = build_lambda_expr ();
8139 tree type;
8140 bool ok;
8142 LAMBDA_EXPR_LOCATION (lambda_expr)
8143 = cp_lexer_peek_token (parser->lexer)->location;
8145 if (cp_unevaluated_operand)
8146 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8147 "lambda-expression in unevaluated context");
8149 /* We may be in the middle of deferred access check. Disable
8150 it now. */
8151 push_deferring_access_checks (dk_no_deferred);
8153 cp_parser_lambda_introducer (parser, lambda_expr);
8155 type = begin_lambda_type (lambda_expr);
8156 if (type == error_mark_node)
8157 return error_mark_node;
8159 record_lambda_scope (lambda_expr);
8161 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8162 determine_visibility (TYPE_NAME (type));
8164 /* Now that we've started the type, add the capture fields for any
8165 explicit captures. */
8166 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8169 /* Inside the class, surrounding template-parameter-lists do not apply. */
8170 unsigned int saved_num_template_parameter_lists
8171 = parser->num_template_parameter_lists;
8172 unsigned char in_statement = parser->in_statement;
8173 bool in_switch_statement_p = parser->in_switch_statement_p;
8175 parser->num_template_parameter_lists = 0;
8176 parser->in_statement = 0;
8177 parser->in_switch_statement_p = false;
8179 /* By virtue of defining a local class, a lambda expression has access to
8180 the private variables of enclosing classes. */
8182 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8184 if (ok)
8185 cp_parser_lambda_body (parser, lambda_expr);
8186 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8187 cp_parser_skip_to_end_of_block_or_statement (parser);
8189 /* The capture list was built up in reverse order; fix that now. */
8191 tree newlist = NULL_TREE;
8192 tree elt, next;
8194 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8195 elt; elt = next)
8197 next = TREE_CHAIN (elt);
8198 TREE_CHAIN (elt) = newlist;
8199 newlist = elt;
8201 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8204 if (ok)
8205 maybe_add_lambda_conv_op (type);
8207 type = finish_struct (type, /*attributes=*/NULL_TREE);
8209 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8210 parser->in_statement = in_statement;
8211 parser->in_switch_statement_p = in_switch_statement_p;
8214 pop_deferring_access_checks ();
8216 /* This field is only used during parsing of the lambda. */
8217 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8219 /* This lambda shouldn't have any proxies left at this point. */
8220 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8221 /* And now that we're done, push proxies for an enclosing lambda. */
8222 insert_pending_capture_proxies ();
8224 if (ok)
8225 return build_lambda_object (lambda_expr);
8226 else
8227 return error_mark_node;
8230 /* Parse the beginning of a lambda expression.
8232 lambda-introducer:
8233 [ lambda-capture [opt] ]
8235 LAMBDA_EXPR is the current representation of the lambda expression. */
8237 static void
8238 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8240 /* Need commas after the first capture. */
8241 bool first = true;
8243 /* Eat the leading `['. */
8244 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8246 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8247 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8248 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8249 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8250 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8253 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8255 cp_lexer_consume_token (parser->lexer);
8256 first = false;
8259 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8261 cp_token* capture_token;
8262 tree capture_id;
8263 tree capture_init_expr;
8264 cp_id_kind idk = CP_ID_KIND_NONE;
8265 bool explicit_init_p = false;
8267 enum capture_kind_type
8269 BY_COPY,
8270 BY_REFERENCE
8272 enum capture_kind_type capture_kind = BY_COPY;
8274 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8276 error ("expected end of capture-list");
8277 return;
8280 if (first)
8281 first = false;
8282 else
8283 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8285 /* Possibly capture `this'. */
8286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8289 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8290 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8291 "with by-copy capture default");
8292 cp_lexer_consume_token (parser->lexer);
8293 add_capture (lambda_expr,
8294 /*id=*/this_identifier,
8295 /*initializer=*/finish_this_expr(),
8296 /*by_reference_p=*/false,
8297 explicit_init_p);
8298 continue;
8301 /* Remember whether we want to capture as a reference or not. */
8302 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8304 capture_kind = BY_REFERENCE;
8305 cp_lexer_consume_token (parser->lexer);
8308 /* Get the identifier. */
8309 capture_token = cp_lexer_peek_token (parser->lexer);
8310 capture_id = cp_parser_identifier (parser);
8312 if (capture_id == error_mark_node)
8313 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8314 delimiters, but I modified this to stop on unnested ']' as well. It
8315 was already changed to stop on unnested '}', so the
8316 "closing_parenthesis" name is no more misleading with my change. */
8318 cp_parser_skip_to_closing_parenthesis (parser,
8319 /*recovering=*/true,
8320 /*or_comma=*/true,
8321 /*consume_paren=*/true);
8322 break;
8325 /* Find the initializer for this capture. */
8326 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8328 /* An explicit expression exists. */
8329 cp_lexer_consume_token (parser->lexer);
8330 pedwarn (input_location, OPT_Wpedantic,
8331 "ISO C++ does not allow initializers "
8332 "in lambda expression capture lists");
8333 capture_init_expr = cp_parser_assignment_expression (parser,
8334 /*cast_p=*/true,
8335 &idk);
8336 explicit_init_p = true;
8338 else
8340 const char* error_msg;
8342 /* Turn the identifier into an id-expression. */
8343 capture_init_expr
8344 = cp_parser_lookup_name
8345 (parser,
8346 capture_id,
8347 none_type,
8348 /*is_template=*/false,
8349 /*is_namespace=*/false,
8350 /*check_dependency=*/true,
8351 /*ambiguous_decls=*/NULL,
8352 capture_token->location);
8354 if (capture_init_expr == error_mark_node)
8356 unqualified_name_lookup_error (capture_id);
8357 continue;
8359 else if (DECL_P (capture_init_expr)
8360 && (TREE_CODE (capture_init_expr) != VAR_DECL
8361 && TREE_CODE (capture_init_expr) != PARM_DECL))
8363 error_at (capture_token->location,
8364 "capture of non-variable %qD ",
8365 capture_init_expr);
8366 inform (0, "%q+#D declared here", capture_init_expr);
8367 continue;
8369 if (TREE_CODE (capture_init_expr) == VAR_DECL
8370 && decl_storage_duration (capture_init_expr) != dk_auto)
8372 pedwarn (capture_token->location, 0, "capture of variable "
8373 "%qD with non-automatic storage duration",
8374 capture_init_expr);
8375 inform (0, "%q+#D declared here", capture_init_expr);
8376 continue;
8379 capture_init_expr
8380 = finish_id_expression
8381 (capture_id,
8382 capture_init_expr,
8383 parser->scope,
8384 &idk,
8385 /*integral_constant_expression_p=*/false,
8386 /*allow_non_integral_constant_expression_p=*/false,
8387 /*non_integral_constant_expression_p=*/NULL,
8388 /*template_p=*/false,
8389 /*done=*/true,
8390 /*address_p=*/false,
8391 /*template_arg_p=*/false,
8392 &error_msg,
8393 capture_token->location);
8396 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8397 && !explicit_init_p)
8399 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8400 && capture_kind == BY_COPY)
8401 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8402 "of %qD redundant with by-copy capture default",
8403 capture_id);
8404 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8405 && capture_kind == BY_REFERENCE)
8406 pedwarn (capture_token->location, 0, "explicit by-reference "
8407 "capture of %qD redundant with by-reference capture "
8408 "default", capture_id);
8411 add_capture (lambda_expr,
8412 capture_id,
8413 capture_init_expr,
8414 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8415 explicit_init_p);
8418 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8421 /* Parse the (optional) middle of a lambda expression.
8423 lambda-declarator:
8424 ( parameter-declaration-clause [opt] )
8425 attribute-specifier [opt]
8426 mutable [opt]
8427 exception-specification [opt]
8428 lambda-return-type-clause [opt]
8430 LAMBDA_EXPR is the current representation of the lambda expression. */
8432 static bool
8433 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8435 /* 5.1.1.4 of the standard says:
8436 If a lambda-expression does not include a lambda-declarator, it is as if
8437 the lambda-declarator were ().
8438 This means an empty parameter list, no attributes, and no exception
8439 specification. */
8440 tree param_list = void_list_node;
8441 tree attributes = NULL_TREE;
8442 tree exception_spec = NULL_TREE;
8443 tree t;
8445 /* The lambda-declarator is optional, but must begin with an opening
8446 parenthesis if present. */
8447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8449 cp_lexer_consume_token (parser->lexer);
8451 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8453 /* Parse parameters. */
8454 param_list = cp_parser_parameter_declaration_clause (parser);
8456 /* Default arguments shall not be specified in the
8457 parameter-declaration-clause of a lambda-declarator. */
8458 for (t = param_list; t; t = TREE_CHAIN (t))
8459 if (TREE_PURPOSE (t))
8460 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8461 "default argument specified for lambda parameter");
8463 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8465 attributes = cp_parser_attributes_opt (parser);
8467 /* Parse optional `mutable' keyword. */
8468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8470 cp_lexer_consume_token (parser->lexer);
8471 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8474 /* Parse optional exception specification. */
8475 exception_spec = cp_parser_exception_specification_opt (parser);
8477 /* Parse optional trailing return type. */
8478 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8480 cp_lexer_consume_token (parser->lexer);
8481 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8484 /* The function parameters must be in scope all the way until after the
8485 trailing-return-type in case of decltype. */
8486 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8487 pop_binding (DECL_NAME (t), t);
8489 leave_scope ();
8492 /* Create the function call operator.
8494 Messing with declarators like this is no uglier than building up the
8495 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8496 other code. */
8498 cp_decl_specifier_seq return_type_specs;
8499 cp_declarator* declarator;
8500 tree fco;
8501 int quals;
8502 void *p;
8504 clear_decl_specs (&return_type_specs);
8505 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8506 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8507 else
8508 /* Maybe we will deduce the return type later. */
8509 return_type_specs.type = make_auto ();
8511 p = obstack_alloc (&declarator_obstack, 0);
8513 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8514 sfk_none);
8516 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8517 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8518 declarator = make_call_declarator (declarator, param_list, quals,
8519 VIRT_SPEC_UNSPECIFIED,
8520 exception_spec,
8521 /*late_return_type=*/NULL_TREE);
8522 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8524 fco = grokmethod (&return_type_specs,
8525 declarator,
8526 attributes);
8527 if (fco != error_mark_node)
8529 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8530 DECL_ARTIFICIAL (fco) = 1;
8531 /* Give the object parameter a different name. */
8532 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8535 finish_member_declaration (fco);
8537 obstack_free (&declarator_obstack, p);
8539 return (fco != error_mark_node);
8543 /* Parse the body of a lambda expression, which is simply
8545 compound-statement
8547 but which requires special handling.
8548 LAMBDA_EXPR is the current representation of the lambda expression. */
8550 static void
8551 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8553 bool nested = (current_function_decl != NULL_TREE);
8554 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8555 if (nested)
8556 push_function_context ();
8557 else
8558 /* Still increment function_depth so that we don't GC in the
8559 middle of an expression. */
8560 ++function_depth;
8561 /* Clear this in case we're in the middle of a default argument. */
8562 parser->local_variables_forbidden_p = false;
8564 /* Finish the function call operator
8565 - class_specifier
8566 + late_parsing_for_member
8567 + function_definition_after_declarator
8568 + ctor_initializer_opt_and_function_body */
8570 tree fco = lambda_function (lambda_expr);
8571 tree body;
8572 bool done = false;
8573 tree compound_stmt;
8574 tree cap;
8576 /* Let the front end know that we are going to be defining this
8577 function. */
8578 start_preparsed_function (fco,
8579 NULL_TREE,
8580 SF_PRE_PARSED | SF_INCLASS_INLINE);
8582 start_lambda_scope (fco);
8583 body = begin_function_body ();
8585 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8586 goto out;
8588 /* Push the proxies for any explicit captures. */
8589 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8590 cap = TREE_CHAIN (cap))
8591 build_capture_proxy (TREE_PURPOSE (cap));
8593 compound_stmt = begin_compound_stmt (0);
8595 /* 5.1.1.4 of the standard says:
8596 If a lambda-expression does not include a trailing-return-type, it
8597 is as if the trailing-return-type denotes the following type:
8598 * if the compound-statement is of the form
8599 { return attribute-specifier [opt] expression ; }
8600 the type of the returned expression after lvalue-to-rvalue
8601 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8602 (_conv.array_ 4.2), and function-to-pointer conversion
8603 (_conv.func_ 4.3);
8604 * otherwise, void. */
8606 /* In a lambda that has neither a lambda-return-type-clause
8607 nor a deducible form, errors should be reported for return statements
8608 in the body. Since we used void as the placeholder return type, parsing
8609 the body as usual will give such desired behavior. */
8610 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8611 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8612 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8614 tree expr = NULL_TREE;
8615 cp_id_kind idk = CP_ID_KIND_NONE;
8617 /* Parse tentatively in case there's more after the initial return
8618 statement. */
8619 cp_parser_parse_tentatively (parser);
8621 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8623 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8625 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8626 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8628 if (cp_parser_parse_definitely (parser))
8630 if (!processing_template_decl)
8631 apply_deduced_return_type (fco, lambda_return_type (expr));
8633 /* Will get error here if type not deduced yet. */
8634 finish_return_stmt (expr);
8636 done = true;
8640 if (!done)
8642 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8643 cp_parser_label_declaration (parser);
8644 cp_parser_statement_seq_opt (parser, NULL_TREE);
8645 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8648 finish_compound_stmt (compound_stmt);
8650 out:
8651 finish_function_body (body);
8652 finish_lambda_scope ();
8654 /* Finish the function and generate code for it if necessary. */
8655 expand_or_defer_fn (finish_function (/*inline*/2));
8658 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8659 if (nested)
8660 pop_function_context();
8661 else
8662 --function_depth;
8665 /* Statements [gram.stmt.stmt] */
8667 /* Parse a statement.
8669 statement:
8670 labeled-statement
8671 expression-statement
8672 compound-statement
8673 selection-statement
8674 iteration-statement
8675 jump-statement
8676 declaration-statement
8677 try-block
8679 C++11:
8681 statement:
8682 labeled-statement
8683 attribute-specifier-seq (opt) expression-statement
8684 attribute-specifier-seq (opt) compound-statement
8685 attribute-specifier-seq (opt) selection-statement
8686 attribute-specifier-seq (opt) iteration-statement
8687 attribute-specifier-seq (opt) jump-statement
8688 declaration-statement
8689 attribute-specifier-seq (opt) try-block
8691 TM Extension:
8693 statement:
8694 atomic-statement
8696 IN_COMPOUND is true when the statement is nested inside a
8697 cp_parser_compound_statement; this matters for certain pragmas.
8699 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8700 is a (possibly labeled) if statement which is not enclosed in braces
8701 and has an else clause. This is used to implement -Wparentheses. */
8703 static void
8704 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8705 bool in_compound, bool *if_p)
8707 tree statement, std_attrs = NULL_TREE;
8708 cp_token *token;
8709 location_t statement_location, attrs_location;
8711 restart:
8712 if (if_p != NULL)
8713 *if_p = false;
8714 /* There is no statement yet. */
8715 statement = NULL_TREE;
8717 cp_lexer_save_tokens (parser->lexer);
8718 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8719 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8721 /* Peek at the next token. */
8722 token = cp_lexer_peek_token (parser->lexer);
8723 /* Remember the location of the first token in the statement. */
8724 statement_location = token->location;
8725 /* If this is a keyword, then that will often determine what kind of
8726 statement we have. */
8727 if (token->type == CPP_KEYWORD)
8729 enum rid keyword = token->keyword;
8731 switch (keyword)
8733 case RID_CASE:
8734 case RID_DEFAULT:
8735 /* Looks like a labeled-statement with a case label.
8736 Parse the label, and then use tail recursion to parse
8737 the statement. */
8738 cp_parser_label_for_labeled_statement (parser, std_attrs);
8739 goto restart;
8741 case RID_IF:
8742 case RID_SWITCH:
8743 statement = cp_parser_selection_statement (parser, if_p);
8744 break;
8746 case RID_WHILE:
8747 case RID_DO:
8748 case RID_FOR:
8749 statement = cp_parser_iteration_statement (parser);
8750 break;
8752 case RID_BREAK:
8753 case RID_CONTINUE:
8754 case RID_RETURN:
8755 case RID_GOTO:
8756 statement = cp_parser_jump_statement (parser);
8757 break;
8759 /* Objective-C++ exception-handling constructs. */
8760 case RID_AT_TRY:
8761 case RID_AT_CATCH:
8762 case RID_AT_FINALLY:
8763 case RID_AT_SYNCHRONIZED:
8764 case RID_AT_THROW:
8765 statement = cp_parser_objc_statement (parser);
8766 break;
8768 case RID_TRY:
8769 statement = cp_parser_try_block (parser);
8770 break;
8772 case RID_NAMESPACE:
8773 /* This must be a namespace alias definition. */
8774 cp_parser_declaration_statement (parser);
8775 return;
8777 case RID_TRANSACTION_ATOMIC:
8778 case RID_TRANSACTION_RELAXED:
8779 statement = cp_parser_transaction (parser, keyword);
8780 break;
8781 case RID_TRANSACTION_CANCEL:
8782 statement = cp_parser_transaction_cancel (parser);
8783 break;
8785 default:
8786 /* It might be a keyword like `int' that can start a
8787 declaration-statement. */
8788 break;
8791 else if (token->type == CPP_NAME)
8793 /* If the next token is a `:', then we are looking at a
8794 labeled-statement. */
8795 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8796 if (token->type == CPP_COLON)
8798 /* Looks like a labeled-statement with an ordinary label.
8799 Parse the label, and then use tail recursion to parse
8800 the statement. */
8802 cp_parser_label_for_labeled_statement (parser, std_attrs);
8803 goto restart;
8806 /* Anything that starts with a `{' must be a compound-statement. */
8807 else if (token->type == CPP_OPEN_BRACE)
8808 statement = cp_parser_compound_statement (parser, NULL, false, false);
8809 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8810 a statement all its own. */
8811 else if (token->type == CPP_PRAGMA)
8813 /* Only certain OpenMP pragmas are attached to statements, and thus
8814 are considered statements themselves. All others are not. In
8815 the context of a compound, accept the pragma as a "statement" and
8816 return so that we can check for a close brace. Otherwise we
8817 require a real statement and must go back and read one. */
8818 if (in_compound)
8819 cp_parser_pragma (parser, pragma_compound);
8820 else if (!cp_parser_pragma (parser, pragma_stmt))
8821 goto restart;
8822 return;
8824 else if (token->type == CPP_EOF)
8826 cp_parser_error (parser, "expected statement");
8827 return;
8830 /* Everything else must be a declaration-statement or an
8831 expression-statement. Try for the declaration-statement
8832 first, unless we are looking at a `;', in which case we know that
8833 we have an expression-statement. */
8834 if (!statement)
8836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8838 if (std_attrs != NULL_TREE)
8840 /* Attributes should be parsed as part of the the
8841 declaration, so let's un-parse them. */
8842 cp_lexer_rollback_tokens (parser->lexer);
8843 std_attrs = NULL_TREE;
8846 cp_parser_parse_tentatively (parser);
8847 /* Try to parse the declaration-statement. */
8848 cp_parser_declaration_statement (parser);
8849 /* If that worked, we're done. */
8850 if (cp_parser_parse_definitely (parser))
8851 return;
8853 /* Look for an expression-statement instead. */
8854 statement = cp_parser_expression_statement (parser, in_statement_expr);
8857 /* Set the line number for the statement. */
8858 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8859 SET_EXPR_LOCATION (statement, statement_location);
8861 /* Note that for now, we don't do anything with c++11 statements
8862 parsed at this level. */
8863 if (std_attrs != NULL_TREE)
8864 warning_at (attrs_location,
8865 OPT_Wattributes,
8866 "attributes at the beginning of statement are ignored");
8869 /* Parse the label for a labeled-statement, i.e.
8871 identifier :
8872 case constant-expression :
8873 default :
8875 GNU Extension:
8876 case constant-expression ... constant-expression : statement
8878 When a label is parsed without errors, the label is added to the
8879 parse tree by the finish_* functions, so this function doesn't
8880 have to return the label. */
8882 static void
8883 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
8885 cp_token *token;
8886 tree label = NULL_TREE;
8887 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8889 /* The next token should be an identifier. */
8890 token = cp_lexer_peek_token (parser->lexer);
8891 if (token->type != CPP_NAME
8892 && token->type != CPP_KEYWORD)
8894 cp_parser_error (parser, "expected labeled-statement");
8895 return;
8898 parser->colon_corrects_to_scope_p = false;
8899 switch (token->keyword)
8901 case RID_CASE:
8903 tree expr, expr_hi;
8904 cp_token *ellipsis;
8906 /* Consume the `case' token. */
8907 cp_lexer_consume_token (parser->lexer);
8908 /* Parse the constant-expression. */
8909 expr = cp_parser_constant_expression (parser,
8910 /*allow_non_constant_p=*/false,
8911 NULL);
8913 ellipsis = cp_lexer_peek_token (parser->lexer);
8914 if (ellipsis->type == CPP_ELLIPSIS)
8916 /* Consume the `...' token. */
8917 cp_lexer_consume_token (parser->lexer);
8918 expr_hi =
8919 cp_parser_constant_expression (parser,
8920 /*allow_non_constant_p=*/false,
8921 NULL);
8922 /* We don't need to emit warnings here, as the common code
8923 will do this for us. */
8925 else
8926 expr_hi = NULL_TREE;
8928 if (parser->in_switch_statement_p)
8929 finish_case_label (token->location, expr, expr_hi);
8930 else
8931 error_at (token->location,
8932 "case label %qE not within a switch statement",
8933 expr);
8935 break;
8937 case RID_DEFAULT:
8938 /* Consume the `default' token. */
8939 cp_lexer_consume_token (parser->lexer);
8941 if (parser->in_switch_statement_p)
8942 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8943 else
8944 error_at (token->location, "case label not within a switch statement");
8945 break;
8947 default:
8948 /* Anything else must be an ordinary label. */
8949 label = finish_label_stmt (cp_parser_identifier (parser));
8950 break;
8953 /* Require the `:' token. */
8954 cp_parser_require (parser, CPP_COLON, RT_COLON);
8956 /* An ordinary label may optionally be followed by attributes.
8957 However, this is only permitted if the attributes are then
8958 followed by a semicolon. This is because, for backward
8959 compatibility, when parsing
8960 lab: __attribute__ ((unused)) int i;
8961 we want the attribute to attach to "i", not "lab". */
8962 if (label != NULL_TREE
8963 && cp_next_tokens_can_be_gnu_attribute_p (parser))
8965 tree attrs;
8966 cp_parser_parse_tentatively (parser);
8967 attrs = cp_parser_gnu_attributes_opt (parser);
8968 if (attrs == NULL_TREE
8969 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8970 cp_parser_abort_tentative_parse (parser);
8971 else if (!cp_parser_parse_definitely (parser))
8973 else
8974 attributes = chainon (attributes, attrs);
8977 if (attributes != NULL_TREE)
8978 cplus_decl_attributes (&label, attributes, 0);
8980 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8983 /* Parse an expression-statement.
8985 expression-statement:
8986 expression [opt] ;
8988 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8989 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8990 indicates whether this expression-statement is part of an
8991 expression statement. */
8993 static tree
8994 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8996 tree statement = NULL_TREE;
8997 cp_token *token = cp_lexer_peek_token (parser->lexer);
8999 /* If the next token is a ';', then there is no expression
9000 statement. */
9001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9002 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9004 /* Give a helpful message for "A<T>::type t;" and the like. */
9005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9006 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9008 if (TREE_CODE (statement) == SCOPE_REF)
9009 error_at (token->location, "need %<typename%> before %qE because "
9010 "%qT is a dependent scope",
9011 statement, TREE_OPERAND (statement, 0));
9012 else if (is_overloaded_fn (statement)
9013 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9015 /* A::A a; */
9016 tree fn = get_first_fn (statement);
9017 error_at (token->location,
9018 "%<%T::%D%> names the constructor, not the type",
9019 DECL_CONTEXT (fn), DECL_NAME (fn));
9023 /* Consume the final `;'. */
9024 cp_parser_consume_semicolon_at_end_of_statement (parser);
9026 if (in_statement_expr
9027 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9028 /* This is the final expression statement of a statement
9029 expression. */
9030 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9031 else if (statement)
9032 statement = finish_expr_stmt (statement);
9033 else
9034 finish_stmt ();
9036 return statement;
9039 /* Parse a compound-statement.
9041 compound-statement:
9042 { statement-seq [opt] }
9044 GNU extension:
9046 compound-statement:
9047 { label-declaration-seq [opt] statement-seq [opt] }
9049 label-declaration-seq:
9050 label-declaration
9051 label-declaration-seq label-declaration
9053 Returns a tree representing the statement. */
9055 static tree
9056 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9057 bool in_try, bool function_body)
9059 tree compound_stmt;
9061 /* Consume the `{'. */
9062 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9063 return error_mark_node;
9064 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9065 && !function_body)
9066 pedwarn (input_location, OPT_Wpedantic,
9067 "compound-statement in constexpr function");
9068 /* Begin the compound-statement. */
9069 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9070 /* If the next keyword is `__label__' we have a label declaration. */
9071 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9072 cp_parser_label_declaration (parser);
9073 /* Parse an (optional) statement-seq. */
9074 cp_parser_statement_seq_opt (parser, in_statement_expr);
9075 /* Finish the compound-statement. */
9076 finish_compound_stmt (compound_stmt);
9077 /* Consume the `}'. */
9078 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9080 return compound_stmt;
9083 /* Parse an (optional) statement-seq.
9085 statement-seq:
9086 statement
9087 statement-seq [opt] statement */
9089 static void
9090 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9092 /* Scan statements until there aren't any more. */
9093 while (true)
9095 cp_token *token = cp_lexer_peek_token (parser->lexer);
9097 /* If we are looking at a `}', then we have run out of
9098 statements; the same is true if we have reached the end
9099 of file, or have stumbled upon a stray '@end'. */
9100 if (token->type == CPP_CLOSE_BRACE
9101 || token->type == CPP_EOF
9102 || token->type == CPP_PRAGMA_EOL
9103 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9104 break;
9106 /* If we are in a compound statement and find 'else' then
9107 something went wrong. */
9108 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9110 if (parser->in_statement & IN_IF_STMT)
9111 break;
9112 else
9114 token = cp_lexer_consume_token (parser->lexer);
9115 error_at (token->location, "%<else%> without a previous %<if%>");
9119 /* Parse the statement. */
9120 cp_parser_statement (parser, in_statement_expr, true, NULL);
9124 /* Parse a selection-statement.
9126 selection-statement:
9127 if ( condition ) statement
9128 if ( condition ) statement else statement
9129 switch ( condition ) statement
9131 Returns the new IF_STMT or SWITCH_STMT.
9133 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9134 is a (possibly labeled) if statement which is not enclosed in
9135 braces and has an else clause. This is used to implement
9136 -Wparentheses. */
9138 static tree
9139 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9141 cp_token *token;
9142 enum rid keyword;
9144 if (if_p != NULL)
9145 *if_p = false;
9147 /* Peek at the next token. */
9148 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9150 /* See what kind of keyword it is. */
9151 keyword = token->keyword;
9152 switch (keyword)
9154 case RID_IF:
9155 case RID_SWITCH:
9157 tree statement;
9158 tree condition;
9160 /* Look for the `('. */
9161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9163 cp_parser_skip_to_end_of_statement (parser);
9164 return error_mark_node;
9167 /* Begin the selection-statement. */
9168 if (keyword == RID_IF)
9169 statement = begin_if_stmt ();
9170 else
9171 statement = begin_switch_stmt ();
9173 /* Parse the condition. */
9174 condition = cp_parser_condition (parser);
9175 /* Look for the `)'. */
9176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9177 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9178 /*consume_paren=*/true);
9180 if (keyword == RID_IF)
9182 bool nested_if;
9183 unsigned char in_statement;
9185 /* Add the condition. */
9186 finish_if_stmt_cond (condition, statement);
9188 /* Parse the then-clause. */
9189 in_statement = parser->in_statement;
9190 parser->in_statement |= IN_IF_STMT;
9191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9194 add_stmt (build_empty_stmt (loc));
9195 cp_lexer_consume_token (parser->lexer);
9196 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9197 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9198 "empty body in an %<if%> statement");
9199 nested_if = false;
9201 else
9202 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9203 parser->in_statement = in_statement;
9205 finish_then_clause (statement);
9207 /* If the next token is `else', parse the else-clause. */
9208 if (cp_lexer_next_token_is_keyword (parser->lexer,
9209 RID_ELSE))
9211 /* Consume the `else' keyword. */
9212 cp_lexer_consume_token (parser->lexer);
9213 begin_else_clause (statement);
9214 /* Parse the else-clause. */
9215 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9217 location_t loc;
9218 loc = cp_lexer_peek_token (parser->lexer)->location;
9219 warning_at (loc,
9220 OPT_Wempty_body, "suggest braces around "
9221 "empty body in an %<else%> statement");
9222 add_stmt (build_empty_stmt (loc));
9223 cp_lexer_consume_token (parser->lexer);
9225 else
9226 cp_parser_implicitly_scoped_statement (parser, NULL);
9228 finish_else_clause (statement);
9230 /* If we are currently parsing a then-clause, then
9231 IF_P will not be NULL. We set it to true to
9232 indicate that this if statement has an else clause.
9233 This may trigger the Wparentheses warning below
9234 when we get back up to the parent if statement. */
9235 if (if_p != NULL)
9236 *if_p = true;
9238 else
9240 /* This if statement does not have an else clause. If
9241 NESTED_IF is true, then the then-clause is an if
9242 statement which does have an else clause. We warn
9243 about the potential ambiguity. */
9244 if (nested_if)
9245 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9246 "suggest explicit braces to avoid ambiguous"
9247 " %<else%>");
9250 /* Now we're all done with the if-statement. */
9251 finish_if_stmt (statement);
9253 else
9255 bool in_switch_statement_p;
9256 unsigned char in_statement;
9258 /* Add the condition. */
9259 finish_switch_cond (condition, statement);
9261 /* Parse the body of the switch-statement. */
9262 in_switch_statement_p = parser->in_switch_statement_p;
9263 in_statement = parser->in_statement;
9264 parser->in_switch_statement_p = true;
9265 parser->in_statement |= IN_SWITCH_STMT;
9266 cp_parser_implicitly_scoped_statement (parser, NULL);
9267 parser->in_switch_statement_p = in_switch_statement_p;
9268 parser->in_statement = in_statement;
9270 /* Now we're all done with the switch-statement. */
9271 finish_switch_stmt (statement);
9274 return statement;
9276 break;
9278 default:
9279 cp_parser_error (parser, "expected selection-statement");
9280 return error_mark_node;
9284 /* Parse a condition.
9286 condition:
9287 expression
9288 type-specifier-seq declarator = initializer-clause
9289 type-specifier-seq declarator braced-init-list
9291 GNU Extension:
9293 condition:
9294 type-specifier-seq declarator asm-specification [opt]
9295 attributes [opt] = assignment-expression
9297 Returns the expression that should be tested. */
9299 static tree
9300 cp_parser_condition (cp_parser* parser)
9302 cp_decl_specifier_seq type_specifiers;
9303 const char *saved_message;
9304 int declares_class_or_enum;
9306 /* Try the declaration first. */
9307 cp_parser_parse_tentatively (parser);
9308 /* New types are not allowed in the type-specifier-seq for a
9309 condition. */
9310 saved_message = parser->type_definition_forbidden_message;
9311 parser->type_definition_forbidden_message
9312 = G_("types may not be defined in conditions");
9313 /* Parse the type-specifier-seq. */
9314 cp_parser_decl_specifier_seq (parser,
9315 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9316 &type_specifiers,
9317 &declares_class_or_enum);
9318 /* Restore the saved message. */
9319 parser->type_definition_forbidden_message = saved_message;
9320 /* If all is well, we might be looking at a declaration. */
9321 if (!cp_parser_error_occurred (parser))
9323 tree decl;
9324 tree asm_specification;
9325 tree attributes;
9326 cp_declarator *declarator;
9327 tree initializer = NULL_TREE;
9329 /* Parse the declarator. */
9330 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9331 /*ctor_dtor_or_conv_p=*/NULL,
9332 /*parenthesized_p=*/NULL,
9333 /*member_p=*/false);
9334 /* Parse the attributes. */
9335 attributes = cp_parser_attributes_opt (parser);
9336 /* Parse the asm-specification. */
9337 asm_specification = cp_parser_asm_specification_opt (parser);
9338 /* If the next token is not an `=' or '{', then we might still be
9339 looking at an expression. For example:
9341 if (A(a).x)
9343 looks like a decl-specifier-seq and a declarator -- but then
9344 there is no `=', so this is an expression. */
9345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9346 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9347 cp_parser_simulate_error (parser);
9349 /* If we did see an `=' or '{', then we are looking at a declaration
9350 for sure. */
9351 if (cp_parser_parse_definitely (parser))
9353 tree pushed_scope;
9354 bool non_constant_p;
9355 bool flags = LOOKUP_ONLYCONVERTING;
9357 /* Create the declaration. */
9358 decl = start_decl (declarator, &type_specifiers,
9359 /*initialized_p=*/true,
9360 attributes, /*prefix_attributes=*/NULL_TREE,
9361 &pushed_scope);
9363 /* Parse the initializer. */
9364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9366 initializer = cp_parser_braced_list (parser, &non_constant_p);
9367 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9368 flags = 0;
9370 else
9372 /* Consume the `='. */
9373 cp_parser_require (parser, CPP_EQ, RT_EQ);
9374 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9376 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9377 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9379 /* Process the initializer. */
9380 cp_finish_decl (decl,
9381 initializer, !non_constant_p,
9382 asm_specification,
9383 flags);
9385 if (pushed_scope)
9386 pop_scope (pushed_scope);
9388 return convert_from_reference (decl);
9391 /* If we didn't even get past the declarator successfully, we are
9392 definitely not looking at a declaration. */
9393 else
9394 cp_parser_abort_tentative_parse (parser);
9396 /* Otherwise, we are looking at an expression. */
9397 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9400 /* Parses a for-statement or range-for-statement until the closing ')',
9401 not included. */
9403 static tree
9404 cp_parser_for (cp_parser *parser)
9406 tree init, scope, decl;
9407 bool is_range_for;
9409 /* Begin the for-statement. */
9410 scope = begin_for_scope (&init);
9412 /* Parse the initialization. */
9413 is_range_for = cp_parser_for_init_statement (parser, &decl);
9415 if (is_range_for)
9416 return cp_parser_range_for (parser, scope, init, decl);
9417 else
9418 return cp_parser_c_for (parser, scope, init);
9421 static tree
9422 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9424 /* Normal for loop */
9425 tree condition = NULL_TREE;
9426 tree expression = NULL_TREE;
9427 tree stmt;
9429 stmt = begin_for_stmt (scope, init);
9430 /* The for-init-statement has already been parsed in
9431 cp_parser_for_init_statement, so no work is needed here. */
9432 finish_for_init_stmt (stmt);
9434 /* If there's a condition, process it. */
9435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9436 condition = cp_parser_condition (parser);
9437 finish_for_cond (condition, stmt);
9438 /* Look for the `;'. */
9439 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9441 /* If there's an expression, process it. */
9442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9443 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9444 finish_for_expr (expression, stmt);
9446 return stmt;
9449 /* Tries to parse a range-based for-statement:
9451 range-based-for:
9452 decl-specifier-seq declarator : expression
9454 The decl-specifier-seq declarator and the `:' are already parsed by
9455 cp_parser_for_init_statement. If processing_template_decl it returns a
9456 newly created RANGE_FOR_STMT; if not, it is converted to a
9457 regular FOR_STMT. */
9459 static tree
9460 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9462 tree stmt, range_expr;
9464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9466 bool expr_non_constant_p;
9467 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9469 else
9470 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9472 /* If in template, STMT is converted to a normal for-statement
9473 at instantiation. If not, it is done just ahead. */
9474 if (processing_template_decl)
9476 if (check_for_bare_parameter_packs (range_expr))
9477 range_expr = error_mark_node;
9478 stmt = begin_range_for_stmt (scope, init);
9479 finish_range_for_decl (stmt, range_decl, range_expr);
9480 if (!type_dependent_expression_p (range_expr)
9481 /* do_auto_deduction doesn't mess with template init-lists. */
9482 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9483 do_range_for_auto_deduction (range_decl, range_expr);
9485 else
9487 stmt = begin_for_stmt (scope, init);
9488 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9490 return stmt;
9493 /* Subroutine of cp_convert_range_for: given the initializer expression,
9494 builds up the range temporary. */
9496 static tree
9497 build_range_temp (tree range_expr)
9499 tree range_type, range_temp;
9501 /* Find out the type deduced by the declaration
9502 `auto &&__range = range_expr'. */
9503 range_type = cp_build_reference_type (make_auto (), true);
9504 range_type = do_auto_deduction (range_type, range_expr,
9505 type_uses_auto (range_type));
9507 /* Create the __range variable. */
9508 range_temp = build_decl (input_location, VAR_DECL,
9509 get_identifier ("__for_range"), range_type);
9510 TREE_USED (range_temp) = 1;
9511 DECL_ARTIFICIAL (range_temp) = 1;
9513 return range_temp;
9516 /* Used by cp_parser_range_for in template context: we aren't going to
9517 do a full conversion yet, but we still need to resolve auto in the
9518 type of the for-range-declaration if present. This is basically
9519 a shortcut version of cp_convert_range_for. */
9521 static void
9522 do_range_for_auto_deduction (tree decl, tree range_expr)
9524 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9525 if (auto_node)
9527 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9528 range_temp = convert_from_reference (build_range_temp (range_expr));
9529 iter_type = (cp_parser_perform_range_for_lookup
9530 (range_temp, &begin_dummy, &end_dummy));
9531 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9532 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9533 tf_warning_or_error);
9534 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9535 iter_decl, auto_node);
9539 /* Converts a range-based for-statement into a normal
9540 for-statement, as per the definition.
9542 for (RANGE_DECL : RANGE_EXPR)
9543 BLOCK
9545 should be equivalent to:
9548 auto &&__range = RANGE_EXPR;
9549 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9550 __begin != __end;
9551 ++__begin)
9553 RANGE_DECL = *__begin;
9554 BLOCK
9558 If RANGE_EXPR is an array:
9559 BEGIN_EXPR = __range
9560 END_EXPR = __range + ARRAY_SIZE(__range)
9561 Else if RANGE_EXPR has a member 'begin' or 'end':
9562 BEGIN_EXPR = __range.begin()
9563 END_EXPR = __range.end()
9564 Else:
9565 BEGIN_EXPR = begin(__range)
9566 END_EXPR = end(__range);
9568 If __range has a member 'begin' but not 'end', or vice versa, we must
9569 still use the second alternative (it will surely fail, however).
9570 When calling begin()/end() in the third alternative we must use
9571 argument dependent lookup, but always considering 'std' as an associated
9572 namespace. */
9574 tree
9575 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9577 tree begin, end;
9578 tree iter_type, begin_expr, end_expr;
9579 tree condition, expression;
9581 if (range_decl == error_mark_node || range_expr == error_mark_node)
9582 /* If an error happened previously do nothing or else a lot of
9583 unhelpful errors would be issued. */
9584 begin_expr = end_expr = iter_type = error_mark_node;
9585 else
9587 tree range_temp = build_range_temp (range_expr);
9588 pushdecl (range_temp);
9589 cp_finish_decl (range_temp, range_expr,
9590 /*is_constant_init*/false, NULL_TREE,
9591 LOOKUP_ONLYCONVERTING);
9593 range_temp = convert_from_reference (range_temp);
9594 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9595 &begin_expr, &end_expr);
9598 /* The new for initialization statement. */
9599 begin = build_decl (input_location, VAR_DECL,
9600 get_identifier ("__for_begin"), iter_type);
9601 TREE_USED (begin) = 1;
9602 DECL_ARTIFICIAL (begin) = 1;
9603 pushdecl (begin);
9604 cp_finish_decl (begin, begin_expr,
9605 /*is_constant_init*/false, NULL_TREE,
9606 LOOKUP_ONLYCONVERTING);
9608 end = build_decl (input_location, VAR_DECL,
9609 get_identifier ("__for_end"), iter_type);
9610 TREE_USED (end) = 1;
9611 DECL_ARTIFICIAL (end) = 1;
9612 pushdecl (end);
9613 cp_finish_decl (end, end_expr,
9614 /*is_constant_init*/false, NULL_TREE,
9615 LOOKUP_ONLYCONVERTING);
9617 finish_for_init_stmt (statement);
9619 /* The new for condition. */
9620 condition = build_x_binary_op (input_location, NE_EXPR,
9621 begin, ERROR_MARK,
9622 end, ERROR_MARK,
9623 NULL, tf_warning_or_error);
9624 finish_for_cond (condition, statement);
9626 /* The new increment expression. */
9627 expression = finish_unary_op_expr (input_location,
9628 PREINCREMENT_EXPR, begin);
9629 finish_for_expr (expression, statement);
9631 /* The declaration is initialized with *__begin inside the loop body. */
9632 cp_finish_decl (range_decl,
9633 build_x_indirect_ref (input_location, begin, RO_NULL,
9634 tf_warning_or_error),
9635 /*is_constant_init*/false, NULL_TREE,
9636 LOOKUP_ONLYCONVERTING);
9638 return statement;
9641 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9642 We need to solve both at the same time because the method used
9643 depends on the existence of members begin or end.
9644 Returns the type deduced for the iterator expression. */
9646 static tree
9647 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9649 if (error_operand_p (range))
9651 *begin = *end = error_mark_node;
9652 return error_mark_node;
9655 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9657 error ("range-based %<for%> expression of type %qT "
9658 "has incomplete type", TREE_TYPE (range));
9659 *begin = *end = error_mark_node;
9660 return error_mark_node;
9662 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9664 /* If RANGE is an array, we will use pointer arithmetic. */
9665 *begin = range;
9666 *end = build_binary_op (input_location, PLUS_EXPR,
9667 range,
9668 array_type_nelts_top (TREE_TYPE (range)),
9670 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9672 else
9674 /* If it is not an array, we must do a bit of magic. */
9675 tree id_begin, id_end;
9676 tree member_begin, member_end;
9678 *begin = *end = error_mark_node;
9680 id_begin = get_identifier ("begin");
9681 id_end = get_identifier ("end");
9682 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9683 /*protect=*/2, /*want_type=*/false,
9684 tf_warning_or_error);
9685 member_end = lookup_member (TREE_TYPE (range), id_end,
9686 /*protect=*/2, /*want_type=*/false,
9687 tf_warning_or_error);
9689 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9691 /* Use the member functions. */
9692 if (member_begin != NULL_TREE)
9693 *begin = cp_parser_range_for_member_function (range, id_begin);
9694 else
9695 error ("range-based %<for%> expression of type %qT has an "
9696 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9698 if (member_end != NULL_TREE)
9699 *end = cp_parser_range_for_member_function (range, id_end);
9700 else
9701 error ("range-based %<for%> expression of type %qT has a "
9702 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9704 else
9706 /* Use global functions with ADL. */
9707 VEC(tree,gc) *vec;
9708 vec = make_tree_vector ();
9710 VEC_safe_push (tree, gc, vec, range);
9712 member_begin = perform_koenig_lookup (id_begin, vec,
9713 /*include_std=*/true,
9714 tf_warning_or_error);
9715 *begin = finish_call_expr (member_begin, &vec, false, true,
9716 tf_warning_or_error);
9717 member_end = perform_koenig_lookup (id_end, vec,
9718 /*include_std=*/true,
9719 tf_warning_or_error);
9720 *end = finish_call_expr (member_end, &vec, false, true,
9721 tf_warning_or_error);
9723 release_tree_vector (vec);
9726 /* Last common checks. */
9727 if (*begin == error_mark_node || *end == error_mark_node)
9729 /* If one of the expressions is an error do no more checks. */
9730 *begin = *end = error_mark_node;
9731 return error_mark_node;
9733 else
9735 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9736 /* The unqualified type of the __begin and __end temporaries should
9737 be the same, as required by the multiple auto declaration. */
9738 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9739 error ("inconsistent begin/end types in range-based %<for%> "
9740 "statement: %qT and %qT",
9741 TREE_TYPE (*begin), TREE_TYPE (*end));
9742 return iter_type;
9747 /* Helper function for cp_parser_perform_range_for_lookup.
9748 Builds a tree for RANGE.IDENTIFIER(). */
9750 static tree
9751 cp_parser_range_for_member_function (tree range, tree identifier)
9753 tree member, res;
9754 VEC(tree,gc) *vec;
9756 member = finish_class_member_access_expr (range, identifier,
9757 false, tf_warning_or_error);
9758 if (member == error_mark_node)
9759 return error_mark_node;
9761 vec = make_tree_vector ();
9762 res = finish_call_expr (member, &vec,
9763 /*disallow_virtual=*/false,
9764 /*koenig_p=*/false,
9765 tf_warning_or_error);
9766 release_tree_vector (vec);
9767 return res;
9770 /* Parse an iteration-statement.
9772 iteration-statement:
9773 while ( condition ) statement
9774 do statement while ( expression ) ;
9775 for ( for-init-statement condition [opt] ; expression [opt] )
9776 statement
9778 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9780 static tree
9781 cp_parser_iteration_statement (cp_parser* parser)
9783 cp_token *token;
9784 enum rid keyword;
9785 tree statement;
9786 unsigned char in_statement;
9788 /* Peek at the next token. */
9789 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9790 if (!token)
9791 return error_mark_node;
9793 /* Remember whether or not we are already within an iteration
9794 statement. */
9795 in_statement = parser->in_statement;
9797 /* See what kind of keyword it is. */
9798 keyword = token->keyword;
9799 switch (keyword)
9801 case RID_WHILE:
9803 tree condition;
9805 /* Begin the while-statement. */
9806 statement = begin_while_stmt ();
9807 /* Look for the `('. */
9808 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9809 /* Parse the condition. */
9810 condition = cp_parser_condition (parser);
9811 finish_while_stmt_cond (condition, statement);
9812 /* Look for the `)'. */
9813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9814 /* Parse the dependent statement. */
9815 parser->in_statement = IN_ITERATION_STMT;
9816 cp_parser_already_scoped_statement (parser);
9817 parser->in_statement = in_statement;
9818 /* We're done with the while-statement. */
9819 finish_while_stmt (statement);
9821 break;
9823 case RID_DO:
9825 tree expression;
9827 /* Begin the do-statement. */
9828 statement = begin_do_stmt ();
9829 /* Parse the body of the do-statement. */
9830 parser->in_statement = IN_ITERATION_STMT;
9831 cp_parser_implicitly_scoped_statement (parser, NULL);
9832 parser->in_statement = in_statement;
9833 finish_do_body (statement);
9834 /* Look for the `while' keyword. */
9835 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9836 /* Look for the `('. */
9837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9838 /* Parse the expression. */
9839 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9840 /* We're done with the do-statement. */
9841 finish_do_stmt (expression, statement);
9842 /* Look for the `)'. */
9843 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9844 /* Look for the `;'. */
9845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9847 break;
9849 case RID_FOR:
9851 /* Look for the `('. */
9852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9854 statement = cp_parser_for (parser);
9856 /* Look for the `)'. */
9857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9859 /* Parse the body of the for-statement. */
9860 parser->in_statement = IN_ITERATION_STMT;
9861 cp_parser_already_scoped_statement (parser);
9862 parser->in_statement = in_statement;
9864 /* We're done with the for-statement. */
9865 finish_for_stmt (statement);
9867 break;
9869 default:
9870 cp_parser_error (parser, "expected iteration-statement");
9871 statement = error_mark_node;
9872 break;
9875 return statement;
9878 /* Parse a for-init-statement or the declarator of a range-based-for.
9879 Returns true if a range-based-for declaration is seen.
9881 for-init-statement:
9882 expression-statement
9883 simple-declaration */
9885 static bool
9886 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9888 /* If the next token is a `;', then we have an empty
9889 expression-statement. Grammatically, this is also a
9890 simple-declaration, but an invalid one, because it does not
9891 declare anything. Therefore, if we did not handle this case
9892 specially, we would issue an error message about an invalid
9893 declaration. */
9894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9896 bool is_range_for = false;
9897 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9899 parser->colon_corrects_to_scope_p = false;
9901 /* We're going to speculatively look for a declaration, falling back
9902 to an expression, if necessary. */
9903 cp_parser_parse_tentatively (parser);
9904 /* Parse the declaration. */
9905 cp_parser_simple_declaration (parser,
9906 /*function_definition_allowed_p=*/false,
9907 decl);
9908 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9909 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9911 /* It is a range-for, consume the ':' */
9912 cp_lexer_consume_token (parser->lexer);
9913 is_range_for = true;
9914 if (cxx_dialect < cxx0x)
9916 error_at (cp_lexer_peek_token (parser->lexer)->location,
9917 "range-based %<for%> loops are not allowed "
9918 "in C++98 mode");
9919 *decl = error_mark_node;
9922 else
9923 /* The ';' is not consumed yet because we told
9924 cp_parser_simple_declaration not to. */
9925 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9927 if (cp_parser_parse_definitely (parser))
9928 return is_range_for;
9929 /* If the tentative parse failed, then we shall need to look for an
9930 expression-statement. */
9932 /* If we are here, it is an expression-statement. */
9933 cp_parser_expression_statement (parser, NULL_TREE);
9934 return false;
9937 /* Parse a jump-statement.
9939 jump-statement:
9940 break ;
9941 continue ;
9942 return expression [opt] ;
9943 return braced-init-list ;
9944 goto identifier ;
9946 GNU extension:
9948 jump-statement:
9949 goto * expression ;
9951 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9953 static tree
9954 cp_parser_jump_statement (cp_parser* parser)
9956 tree statement = error_mark_node;
9957 cp_token *token;
9958 enum rid keyword;
9959 unsigned char in_statement;
9961 /* Peek at the next token. */
9962 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9963 if (!token)
9964 return error_mark_node;
9966 /* See what kind of keyword it is. */
9967 keyword = token->keyword;
9968 switch (keyword)
9970 case RID_BREAK:
9971 in_statement = parser->in_statement & ~IN_IF_STMT;
9972 switch (in_statement)
9974 case 0:
9975 error_at (token->location, "break statement not within loop or switch");
9976 break;
9977 default:
9978 gcc_assert ((in_statement & IN_SWITCH_STMT)
9979 || in_statement == IN_ITERATION_STMT);
9980 statement = finish_break_stmt ();
9981 break;
9982 case IN_OMP_BLOCK:
9983 error_at (token->location, "invalid exit from OpenMP structured block");
9984 break;
9985 case IN_OMP_FOR:
9986 error_at (token->location, "break statement used with OpenMP for loop");
9987 break;
9989 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9990 break;
9992 case RID_CONTINUE:
9993 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9995 case 0:
9996 error_at (token->location, "continue statement not within a loop");
9997 break;
9998 case IN_ITERATION_STMT:
9999 case IN_OMP_FOR:
10000 statement = finish_continue_stmt ();
10001 break;
10002 case IN_OMP_BLOCK:
10003 error_at (token->location, "invalid exit from OpenMP structured block");
10004 break;
10005 default:
10006 gcc_unreachable ();
10008 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10009 break;
10011 case RID_RETURN:
10013 tree expr;
10014 bool expr_non_constant_p;
10016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10018 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10019 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10021 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10022 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10023 else
10024 /* If the next token is a `;', then there is no
10025 expression. */
10026 expr = NULL_TREE;
10027 /* Build the return-statement. */
10028 statement = finish_return_stmt (expr);
10029 /* Look for the final `;'. */
10030 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10032 break;
10034 case RID_GOTO:
10035 /* Create the goto-statement. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10038 /* Issue a warning about this use of a GNU extension. */
10039 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10040 /* Consume the '*' token. */
10041 cp_lexer_consume_token (parser->lexer);
10042 /* Parse the dependent expression. */
10043 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10045 else
10046 finish_goto_stmt (cp_parser_identifier (parser));
10047 /* Look for the final `;'. */
10048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10049 break;
10051 default:
10052 cp_parser_error (parser, "expected jump-statement");
10053 break;
10056 return statement;
10059 /* Parse a declaration-statement.
10061 declaration-statement:
10062 block-declaration */
10064 static void
10065 cp_parser_declaration_statement (cp_parser* parser)
10067 void *p;
10069 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10070 p = obstack_alloc (&declarator_obstack, 0);
10072 /* Parse the block-declaration. */
10073 cp_parser_block_declaration (parser, /*statement_p=*/true);
10075 /* Free any declarators allocated. */
10076 obstack_free (&declarator_obstack, p);
10078 /* Finish off the statement. */
10079 finish_stmt ();
10082 /* Some dependent statements (like `if (cond) statement'), are
10083 implicitly in their own scope. In other words, if the statement is
10084 a single statement (as opposed to a compound-statement), it is
10085 none-the-less treated as if it were enclosed in braces. Any
10086 declarations appearing in the dependent statement are out of scope
10087 after control passes that point. This function parses a statement,
10088 but ensures that is in its own scope, even if it is not a
10089 compound-statement.
10091 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10092 is a (possibly labeled) if statement which is not enclosed in
10093 braces and has an else clause. This is used to implement
10094 -Wparentheses.
10096 Returns the new statement. */
10098 static tree
10099 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10101 tree statement;
10103 if (if_p != NULL)
10104 *if_p = false;
10106 /* Mark if () ; with a special NOP_EXPR. */
10107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10110 cp_lexer_consume_token (parser->lexer);
10111 statement = add_stmt (build_empty_stmt (loc));
10113 /* if a compound is opened, we simply parse the statement directly. */
10114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10115 statement = cp_parser_compound_statement (parser, NULL, false, false);
10116 /* If the token is not a `{', then we must take special action. */
10117 else
10119 /* Create a compound-statement. */
10120 statement = begin_compound_stmt (0);
10121 /* Parse the dependent-statement. */
10122 cp_parser_statement (parser, NULL_TREE, false, if_p);
10123 /* Finish the dummy compound-statement. */
10124 finish_compound_stmt (statement);
10127 /* Return the statement. */
10128 return statement;
10131 /* For some dependent statements (like `while (cond) statement'), we
10132 have already created a scope. Therefore, even if the dependent
10133 statement is a compound-statement, we do not want to create another
10134 scope. */
10136 static void
10137 cp_parser_already_scoped_statement (cp_parser* parser)
10139 /* If the token is a `{', then we must take special action. */
10140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10141 cp_parser_statement (parser, NULL_TREE, false, NULL);
10142 else
10144 /* Avoid calling cp_parser_compound_statement, so that we
10145 don't create a new scope. Do everything else by hand. */
10146 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10147 /* If the next keyword is `__label__' we have a label declaration. */
10148 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10149 cp_parser_label_declaration (parser);
10150 /* Parse an (optional) statement-seq. */
10151 cp_parser_statement_seq_opt (parser, NULL_TREE);
10152 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10156 /* Declarations [gram.dcl.dcl] */
10158 /* Parse an optional declaration-sequence.
10160 declaration-seq:
10161 declaration
10162 declaration-seq declaration */
10164 static void
10165 cp_parser_declaration_seq_opt (cp_parser* parser)
10167 while (true)
10169 cp_token *token;
10171 token = cp_lexer_peek_token (parser->lexer);
10173 if (token->type == CPP_CLOSE_BRACE
10174 || token->type == CPP_EOF
10175 || token->type == CPP_PRAGMA_EOL)
10176 break;
10178 if (token->type == CPP_SEMICOLON)
10180 /* A declaration consisting of a single semicolon is
10181 invalid. Allow it unless we're being pedantic. */
10182 cp_lexer_consume_token (parser->lexer);
10183 if (!in_system_header)
10184 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10185 continue;
10188 /* If we're entering or exiting a region that's implicitly
10189 extern "C", modify the lang context appropriately. */
10190 if (!parser->implicit_extern_c && token->implicit_extern_c)
10192 push_lang_context (lang_name_c);
10193 parser->implicit_extern_c = true;
10195 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10197 pop_lang_context ();
10198 parser->implicit_extern_c = false;
10201 if (token->type == CPP_PRAGMA)
10203 /* A top-level declaration can consist solely of a #pragma.
10204 A nested declaration cannot, so this is done here and not
10205 in cp_parser_declaration. (A #pragma at block scope is
10206 handled in cp_parser_statement.) */
10207 cp_parser_pragma (parser, pragma_external);
10208 continue;
10211 /* Parse the declaration itself. */
10212 cp_parser_declaration (parser);
10216 /* Parse a declaration.
10218 declaration:
10219 block-declaration
10220 function-definition
10221 template-declaration
10222 explicit-instantiation
10223 explicit-specialization
10224 linkage-specification
10225 namespace-definition
10227 GNU extension:
10229 declaration:
10230 __extension__ declaration */
10232 static void
10233 cp_parser_declaration (cp_parser* parser)
10235 cp_token token1;
10236 cp_token token2;
10237 int saved_pedantic;
10238 void *p;
10239 tree attributes = NULL_TREE;
10241 /* Check for the `__extension__' keyword. */
10242 if (cp_parser_extension_opt (parser, &saved_pedantic))
10244 /* Parse the qualified declaration. */
10245 cp_parser_declaration (parser);
10246 /* Restore the PEDANTIC flag. */
10247 pedantic = saved_pedantic;
10249 return;
10252 /* Try to figure out what kind of declaration is present. */
10253 token1 = *cp_lexer_peek_token (parser->lexer);
10255 if (token1.type != CPP_EOF)
10256 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10257 else
10259 token2.type = CPP_EOF;
10260 token2.keyword = RID_MAX;
10263 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10264 p = obstack_alloc (&declarator_obstack, 0);
10266 /* If the next token is `extern' and the following token is a string
10267 literal, then we have a linkage specification. */
10268 if (token1.keyword == RID_EXTERN
10269 && cp_parser_is_pure_string_literal (&token2))
10270 cp_parser_linkage_specification (parser);
10271 /* If the next token is `template', then we have either a template
10272 declaration, an explicit instantiation, or an explicit
10273 specialization. */
10274 else if (token1.keyword == RID_TEMPLATE)
10276 /* `template <>' indicates a template specialization. */
10277 if (token2.type == CPP_LESS
10278 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10279 cp_parser_explicit_specialization (parser);
10280 /* `template <' indicates a template declaration. */
10281 else if (token2.type == CPP_LESS)
10282 cp_parser_template_declaration (parser, /*member_p=*/false);
10283 /* Anything else must be an explicit instantiation. */
10284 else
10285 cp_parser_explicit_instantiation (parser);
10287 /* If the next token is `export', then we have a template
10288 declaration. */
10289 else if (token1.keyword == RID_EXPORT)
10290 cp_parser_template_declaration (parser, /*member_p=*/false);
10291 /* If the next token is `extern', 'static' or 'inline' and the one
10292 after that is `template', we have a GNU extended explicit
10293 instantiation directive. */
10294 else if (cp_parser_allow_gnu_extensions_p (parser)
10295 && (token1.keyword == RID_EXTERN
10296 || token1.keyword == RID_STATIC
10297 || token1.keyword == RID_INLINE)
10298 && token2.keyword == RID_TEMPLATE)
10299 cp_parser_explicit_instantiation (parser);
10300 /* If the next token is `namespace', check for a named or unnamed
10301 namespace definition. */
10302 else if (token1.keyword == RID_NAMESPACE
10303 && (/* A named namespace definition. */
10304 (token2.type == CPP_NAME
10305 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10306 != CPP_EQ))
10307 /* An unnamed namespace definition. */
10308 || token2.type == CPP_OPEN_BRACE
10309 || token2.keyword == RID_ATTRIBUTE))
10310 cp_parser_namespace_definition (parser);
10311 /* An inline (associated) namespace definition. */
10312 else if (token1.keyword == RID_INLINE
10313 && token2.keyword == RID_NAMESPACE)
10314 cp_parser_namespace_definition (parser);
10315 /* Objective-C++ declaration/definition. */
10316 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10317 cp_parser_objc_declaration (parser, NULL_TREE);
10318 else if (c_dialect_objc ()
10319 && token1.keyword == RID_ATTRIBUTE
10320 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10321 cp_parser_objc_declaration (parser, attributes);
10322 /* We must have either a block declaration or a function
10323 definition. */
10324 else
10325 /* Try to parse a block-declaration, or a function-definition. */
10326 cp_parser_block_declaration (parser, /*statement_p=*/false);
10328 /* Free any declarators allocated. */
10329 obstack_free (&declarator_obstack, p);
10332 /* Parse a block-declaration.
10334 block-declaration:
10335 simple-declaration
10336 asm-definition
10337 namespace-alias-definition
10338 using-declaration
10339 using-directive
10341 GNU Extension:
10343 block-declaration:
10344 __extension__ block-declaration
10346 C++0x Extension:
10348 block-declaration:
10349 static_assert-declaration
10351 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10352 part of a declaration-statement. */
10354 static void
10355 cp_parser_block_declaration (cp_parser *parser,
10356 bool statement_p)
10358 cp_token *token1;
10359 int saved_pedantic;
10361 /* Check for the `__extension__' keyword. */
10362 if (cp_parser_extension_opt (parser, &saved_pedantic))
10364 /* Parse the qualified declaration. */
10365 cp_parser_block_declaration (parser, statement_p);
10366 /* Restore the PEDANTIC flag. */
10367 pedantic = saved_pedantic;
10369 return;
10372 /* Peek at the next token to figure out which kind of declaration is
10373 present. */
10374 token1 = cp_lexer_peek_token (parser->lexer);
10376 /* If the next keyword is `asm', we have an asm-definition. */
10377 if (token1->keyword == RID_ASM)
10379 if (statement_p)
10380 cp_parser_commit_to_tentative_parse (parser);
10381 cp_parser_asm_definition (parser);
10383 /* If the next keyword is `namespace', we have a
10384 namespace-alias-definition. */
10385 else if (token1->keyword == RID_NAMESPACE)
10386 cp_parser_namespace_alias_definition (parser);
10387 /* If the next keyword is `using', we have a
10388 using-declaration, a using-directive, or an alias-declaration. */
10389 else if (token1->keyword == RID_USING)
10391 cp_token *token2;
10393 if (statement_p)
10394 cp_parser_commit_to_tentative_parse (parser);
10395 /* If the token after `using' is `namespace', then we have a
10396 using-directive. */
10397 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10398 if (token2->keyword == RID_NAMESPACE)
10399 cp_parser_using_directive (parser);
10400 /* If the second token after 'using' is '=', then we have an
10401 alias-declaration. */
10402 else if (cxx_dialect >= cxx0x
10403 && token2->type == CPP_NAME
10404 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10405 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10406 cp_parser_alias_declaration (parser);
10407 /* Otherwise, it's a using-declaration. */
10408 else
10409 cp_parser_using_declaration (parser,
10410 /*access_declaration_p=*/false);
10412 /* If the next keyword is `__label__' we have a misplaced label
10413 declaration. */
10414 else if (token1->keyword == RID_LABEL)
10416 cp_lexer_consume_token (parser->lexer);
10417 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10418 cp_parser_skip_to_end_of_statement (parser);
10419 /* If the next token is now a `;', consume it. */
10420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10421 cp_lexer_consume_token (parser->lexer);
10423 /* If the next token is `static_assert' we have a static assertion. */
10424 else if (token1->keyword == RID_STATIC_ASSERT)
10425 cp_parser_static_assert (parser, /*member_p=*/false);
10426 /* Anything else must be a simple-declaration. */
10427 else
10428 cp_parser_simple_declaration (parser, !statement_p,
10429 /*maybe_range_for_decl*/NULL);
10432 /* Parse a simple-declaration.
10434 simple-declaration:
10435 decl-specifier-seq [opt] init-declarator-list [opt] ;
10437 init-declarator-list:
10438 init-declarator
10439 init-declarator-list , init-declarator
10441 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10442 function-definition as a simple-declaration.
10444 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10445 parsed declaration if it is an uninitialized single declarator not followed
10446 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10447 if present, will not be consumed. */
10449 static void
10450 cp_parser_simple_declaration (cp_parser* parser,
10451 bool function_definition_allowed_p,
10452 tree *maybe_range_for_decl)
10454 cp_decl_specifier_seq decl_specifiers;
10455 int declares_class_or_enum;
10456 bool saw_declarator;
10458 if (maybe_range_for_decl)
10459 *maybe_range_for_decl = NULL_TREE;
10461 /* Defer access checks until we know what is being declared; the
10462 checks for names appearing in the decl-specifier-seq should be
10463 done as if we were in the scope of the thing being declared. */
10464 push_deferring_access_checks (dk_deferred);
10466 /* Parse the decl-specifier-seq. We have to keep track of whether
10467 or not the decl-specifier-seq declares a named class or
10468 enumeration type, since that is the only case in which the
10469 init-declarator-list is allowed to be empty.
10471 [dcl.dcl]
10473 In a simple-declaration, the optional init-declarator-list can be
10474 omitted only when declaring a class or enumeration, that is when
10475 the decl-specifier-seq contains either a class-specifier, an
10476 elaborated-type-specifier, or an enum-specifier. */
10477 cp_parser_decl_specifier_seq (parser,
10478 CP_PARSER_FLAGS_OPTIONAL,
10479 &decl_specifiers,
10480 &declares_class_or_enum);
10481 /* We no longer need to defer access checks. */
10482 stop_deferring_access_checks ();
10484 /* In a block scope, a valid declaration must always have a
10485 decl-specifier-seq. By not trying to parse declarators, we can
10486 resolve the declaration/expression ambiguity more quickly. */
10487 if (!function_definition_allowed_p
10488 && !decl_specifiers.any_specifiers_p)
10490 cp_parser_error (parser, "expected declaration");
10491 goto done;
10494 /* If the next two tokens are both identifiers, the code is
10495 erroneous. The usual cause of this situation is code like:
10497 T t;
10499 where "T" should name a type -- but does not. */
10500 if (!decl_specifiers.any_type_specifiers_p
10501 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10503 /* If parsing tentatively, we should commit; we really are
10504 looking at a declaration. */
10505 cp_parser_commit_to_tentative_parse (parser);
10506 /* Give up. */
10507 goto done;
10510 /* If we have seen at least one decl-specifier, and the next token
10511 is not a parenthesis, then we must be looking at a declaration.
10512 (After "int (" we might be looking at a functional cast.) */
10513 if (decl_specifiers.any_specifiers_p
10514 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10515 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10516 && !cp_parser_error_occurred (parser))
10517 cp_parser_commit_to_tentative_parse (parser);
10519 /* Keep going until we hit the `;' at the end of the simple
10520 declaration. */
10521 saw_declarator = false;
10522 while (cp_lexer_next_token_is_not (parser->lexer,
10523 CPP_SEMICOLON))
10525 cp_token *token;
10526 bool function_definition_p;
10527 tree decl;
10529 if (saw_declarator)
10531 /* If we are processing next declarator, coma is expected */
10532 token = cp_lexer_peek_token (parser->lexer);
10533 gcc_assert (token->type == CPP_COMMA);
10534 cp_lexer_consume_token (parser->lexer);
10535 if (maybe_range_for_decl)
10536 *maybe_range_for_decl = error_mark_node;
10538 else
10539 saw_declarator = true;
10541 /* Parse the init-declarator. */
10542 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10543 /*checks=*/NULL,
10544 function_definition_allowed_p,
10545 /*member_p=*/false,
10546 declares_class_or_enum,
10547 &function_definition_p,
10548 maybe_range_for_decl);
10549 /* If an error occurred while parsing tentatively, exit quickly.
10550 (That usually happens when in the body of a function; each
10551 statement is treated as a declaration-statement until proven
10552 otherwise.) */
10553 if (cp_parser_error_occurred (parser))
10554 goto done;
10555 /* Handle function definitions specially. */
10556 if (function_definition_p)
10558 /* If the next token is a `,', then we are probably
10559 processing something like:
10561 void f() {}, *p;
10563 which is erroneous. */
10564 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10566 cp_token *token = cp_lexer_peek_token (parser->lexer);
10567 error_at (token->location,
10568 "mixing"
10569 " declarations and function-definitions is forbidden");
10571 /* Otherwise, we're done with the list of declarators. */
10572 else
10574 pop_deferring_access_checks ();
10575 return;
10578 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10579 *maybe_range_for_decl = decl;
10580 /* The next token should be either a `,' or a `;'. */
10581 token = cp_lexer_peek_token (parser->lexer);
10582 /* If it's a `,', there are more declarators to come. */
10583 if (token->type == CPP_COMMA)
10584 /* will be consumed next time around */;
10585 /* If it's a `;', we are done. */
10586 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10587 break;
10588 /* Anything else is an error. */
10589 else
10591 /* If we have already issued an error message we don't need
10592 to issue another one. */
10593 if (decl != error_mark_node
10594 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10595 cp_parser_error (parser, "expected %<,%> or %<;%>");
10596 /* Skip tokens until we reach the end of the statement. */
10597 cp_parser_skip_to_end_of_statement (parser);
10598 /* If the next token is now a `;', consume it. */
10599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10600 cp_lexer_consume_token (parser->lexer);
10601 goto done;
10603 /* After the first time around, a function-definition is not
10604 allowed -- even if it was OK at first. For example:
10606 int i, f() {}
10608 is not valid. */
10609 function_definition_allowed_p = false;
10612 /* Issue an error message if no declarators are present, and the
10613 decl-specifier-seq does not itself declare a class or
10614 enumeration. */
10615 if (!saw_declarator)
10617 if (cp_parser_declares_only_class_p (parser))
10618 shadow_tag (&decl_specifiers);
10619 /* Perform any deferred access checks. */
10620 perform_deferred_access_checks (tf_warning_or_error);
10623 /* Consume the `;'. */
10624 if (!maybe_range_for_decl)
10625 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10627 done:
10628 pop_deferring_access_checks ();
10631 /* Parse a decl-specifier-seq.
10633 decl-specifier-seq:
10634 decl-specifier-seq [opt] decl-specifier
10635 decl-specifier attribute-specifier-seq [opt] (C++11)
10637 decl-specifier:
10638 storage-class-specifier
10639 type-specifier
10640 function-specifier
10641 friend
10642 typedef
10644 GNU Extension:
10646 decl-specifier:
10647 attributes
10649 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10651 The parser flags FLAGS is used to control type-specifier parsing.
10653 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10654 flags:
10656 1: one of the decl-specifiers is an elaborated-type-specifier
10657 (i.e., a type declaration)
10658 2: one of the decl-specifiers is an enum-specifier or a
10659 class-specifier (i.e., a type definition)
10663 static void
10664 cp_parser_decl_specifier_seq (cp_parser* parser,
10665 cp_parser_flags flags,
10666 cp_decl_specifier_seq *decl_specs,
10667 int* declares_class_or_enum)
10669 bool constructor_possible_p = !parser->in_declarator_p;
10670 bool found_decl_spec = false;
10671 cp_token *start_token = NULL;
10672 cp_decl_spec ds;
10674 /* Clear DECL_SPECS. */
10675 clear_decl_specs (decl_specs);
10677 /* Assume no class or enumeration type is declared. */
10678 *declares_class_or_enum = 0;
10680 /* Keep reading specifiers until there are no more to read. */
10681 while (true)
10683 bool constructor_p;
10684 cp_token *token;
10685 ds = ds_last;
10687 /* Peek at the next token. */
10688 token = cp_lexer_peek_token (parser->lexer);
10690 /* Save the first token of the decl spec list for error
10691 reporting. */
10692 if (!start_token)
10693 start_token = token;
10694 /* Handle attributes. */
10695 if (cp_next_tokens_can_be_attribute_p (parser))
10697 /* Parse the attributes. */
10698 tree attrs = cp_parser_attributes_opt (parser);
10700 /* In a sequence of declaration specifiers, c++11 attributes
10701 appertain to the type that precede them. In that case
10702 [dcl.spec]/1 says:
10704 The attribute-specifier-seq affects the type only for
10705 the declaration it appears in, not other declarations
10706 involving the same type.
10708 But for now let's force the user to position the
10709 attribute either at the beginning of the declaration or
10710 after the declarator-id, which would clearly mean that it
10711 applies to the declarator. */
10712 if (cxx11_attribute_p (attrs))
10714 if (!found_decl_spec)
10715 /* The c++11 attribute is at the beginning of the
10716 declaration. It appertains to the entity being
10717 declared. */;
10718 else
10720 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10722 /* This is an attribute following a
10723 class-specifier. */
10724 if (decl_specs->type_definition_p)
10725 warn_misplaced_attr_for_class_type (token->location,
10726 decl_specs->type);
10727 attrs = NULL_TREE;
10729 else
10731 decl_specs->std_attributes
10732 = chainon (decl_specs->std_attributes,
10733 attrs);
10734 if (decl_specs->locations[ds_std_attribute] == 0)
10735 decl_specs->locations[ds_std_attribute] = token->location;
10737 continue;
10741 decl_specs->attributes
10742 = chainon (decl_specs->attributes,
10743 attrs);
10744 if (decl_specs->locations[ds_attribute] == 0)
10745 decl_specs->locations[ds_attribute] = token->location;
10746 continue;
10748 /* Assume we will find a decl-specifier keyword. */
10749 found_decl_spec = true;
10750 /* If the next token is an appropriate keyword, we can simply
10751 add it to the list. */
10752 switch (token->keyword)
10754 /* decl-specifier:
10755 friend
10756 constexpr */
10757 case RID_FRIEND:
10758 if (!at_class_scope_p ())
10760 error_at (token->location, "%<friend%> used outside of class");
10761 cp_lexer_purge_token (parser->lexer);
10763 else
10765 ds = ds_friend;
10766 /* Consume the token. */
10767 cp_lexer_consume_token (parser->lexer);
10769 break;
10771 case RID_CONSTEXPR:
10772 ds = ds_constexpr;
10773 cp_lexer_consume_token (parser->lexer);
10774 break;
10776 /* function-specifier:
10777 inline
10778 virtual
10779 explicit */
10780 case RID_INLINE:
10781 case RID_VIRTUAL:
10782 case RID_EXPLICIT:
10783 cp_parser_function_specifier_opt (parser, decl_specs);
10784 break;
10786 /* decl-specifier:
10787 typedef */
10788 case RID_TYPEDEF:
10789 ds = ds_typedef;
10790 /* Consume the token. */
10791 cp_lexer_consume_token (parser->lexer);
10792 /* A constructor declarator cannot appear in a typedef. */
10793 constructor_possible_p = false;
10794 /* The "typedef" keyword can only occur in a declaration; we
10795 may as well commit at this point. */
10796 cp_parser_commit_to_tentative_parse (parser);
10798 if (decl_specs->storage_class != sc_none)
10799 decl_specs->conflicting_specifiers_p = true;
10800 break;
10802 /* storage-class-specifier:
10803 auto
10804 register
10805 static
10806 extern
10807 mutable
10809 GNU Extension:
10810 thread */
10811 case RID_AUTO:
10812 if (cxx_dialect == cxx98)
10814 /* Consume the token. */
10815 cp_lexer_consume_token (parser->lexer);
10817 /* Complain about `auto' as a storage specifier, if
10818 we're complaining about C++0x compatibility. */
10819 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10820 " changes meaning in C++11; please remove it");
10822 /* Set the storage class anyway. */
10823 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10824 token);
10826 else
10827 /* C++0x auto type-specifier. */
10828 found_decl_spec = false;
10829 break;
10831 case RID_REGISTER:
10832 case RID_STATIC:
10833 case RID_EXTERN:
10834 case RID_MUTABLE:
10835 /* Consume the token. */
10836 cp_lexer_consume_token (parser->lexer);
10837 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10838 token);
10839 break;
10840 case RID_THREAD:
10841 /* Consume the token. */
10842 ds = ds_thread;
10843 cp_lexer_consume_token (parser->lexer);
10844 break;
10846 default:
10847 /* We did not yet find a decl-specifier yet. */
10848 found_decl_spec = false;
10849 break;
10852 if (found_decl_spec
10853 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10854 && token->keyword != RID_CONSTEXPR)
10855 error ("decl-specifier invalid in condition");
10857 if (ds != ds_last)
10858 set_and_check_decl_spec_loc (decl_specs, ds, token);
10860 /* Constructors are a special case. The `S' in `S()' is not a
10861 decl-specifier; it is the beginning of the declarator. */
10862 constructor_p
10863 = (!found_decl_spec
10864 && constructor_possible_p
10865 && (cp_parser_constructor_declarator_p
10866 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10868 /* If we don't have a DECL_SPEC yet, then we must be looking at
10869 a type-specifier. */
10870 if (!found_decl_spec && !constructor_p)
10872 int decl_spec_declares_class_or_enum;
10873 bool is_cv_qualifier;
10874 tree type_spec;
10876 type_spec
10877 = cp_parser_type_specifier (parser, flags,
10878 decl_specs,
10879 /*is_declaration=*/true,
10880 &decl_spec_declares_class_or_enum,
10881 &is_cv_qualifier);
10882 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10884 /* If this type-specifier referenced a user-defined type
10885 (a typedef, class-name, etc.), then we can't allow any
10886 more such type-specifiers henceforth.
10888 [dcl.spec]
10890 The longest sequence of decl-specifiers that could
10891 possibly be a type name is taken as the
10892 decl-specifier-seq of a declaration. The sequence shall
10893 be self-consistent as described below.
10895 [dcl.type]
10897 As a general rule, at most one type-specifier is allowed
10898 in the complete decl-specifier-seq of a declaration. The
10899 only exceptions are the following:
10901 -- const or volatile can be combined with any other
10902 type-specifier.
10904 -- signed or unsigned can be combined with char, long,
10905 short, or int.
10907 -- ..
10909 Example:
10911 typedef char* Pc;
10912 void g (const int Pc);
10914 Here, Pc is *not* part of the decl-specifier seq; it's
10915 the declarator. Therefore, once we see a type-specifier
10916 (other than a cv-qualifier), we forbid any additional
10917 user-defined types. We *do* still allow things like `int
10918 int' to be considered a decl-specifier-seq, and issue the
10919 error message later. */
10920 if (type_spec && !is_cv_qualifier)
10921 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10922 /* A constructor declarator cannot follow a type-specifier. */
10923 if (type_spec)
10925 constructor_possible_p = false;
10926 found_decl_spec = true;
10927 if (!is_cv_qualifier)
10928 decl_specs->any_type_specifiers_p = true;
10932 /* If we still do not have a DECL_SPEC, then there are no more
10933 decl-specifiers. */
10934 if (!found_decl_spec)
10935 break;
10937 decl_specs->any_specifiers_p = true;
10938 /* After we see one decl-specifier, further decl-specifiers are
10939 always optional. */
10940 flags |= CP_PARSER_FLAGS_OPTIONAL;
10943 /* Don't allow a friend specifier with a class definition. */
10944 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10945 && (*declares_class_or_enum & 2))
10946 error_at (decl_specs->locations[ds_friend],
10947 "class definition may not be declared a friend");
10950 /* Parse an (optional) storage-class-specifier.
10952 storage-class-specifier:
10953 auto
10954 register
10955 static
10956 extern
10957 mutable
10959 GNU Extension:
10961 storage-class-specifier:
10962 thread
10964 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10966 static tree
10967 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10969 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10971 case RID_AUTO:
10972 if (cxx_dialect != cxx98)
10973 return NULL_TREE;
10974 /* Fall through for C++98. */
10976 case RID_REGISTER:
10977 case RID_STATIC:
10978 case RID_EXTERN:
10979 case RID_MUTABLE:
10980 case RID_THREAD:
10981 /* Consume the token. */
10982 return cp_lexer_consume_token (parser->lexer)->u.value;
10984 default:
10985 return NULL_TREE;
10989 /* Parse an (optional) function-specifier.
10991 function-specifier:
10992 inline
10993 virtual
10994 explicit
10996 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10997 Updates DECL_SPECS, if it is non-NULL. */
10999 static tree
11000 cp_parser_function_specifier_opt (cp_parser* parser,
11001 cp_decl_specifier_seq *decl_specs)
11003 cp_token *token = cp_lexer_peek_token (parser->lexer);
11004 switch (token->keyword)
11006 case RID_INLINE:
11007 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11008 break;
11010 case RID_VIRTUAL:
11011 /* 14.5.2.3 [temp.mem]
11013 A member function template shall not be virtual. */
11014 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11015 error_at (token->location, "templates may not be %<virtual%>");
11016 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11017 break;
11019 case RID_EXPLICIT:
11020 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11021 break;
11023 default:
11024 return NULL_TREE;
11027 /* Consume the token. */
11028 return cp_lexer_consume_token (parser->lexer)->u.value;
11031 /* Parse a linkage-specification.
11033 linkage-specification:
11034 extern string-literal { declaration-seq [opt] }
11035 extern string-literal declaration */
11037 static void
11038 cp_parser_linkage_specification (cp_parser* parser)
11040 tree linkage;
11042 /* Look for the `extern' keyword. */
11043 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11045 /* Look for the string-literal. */
11046 linkage = cp_parser_string_literal (parser, false, false);
11048 /* Transform the literal into an identifier. If the literal is a
11049 wide-character string, or contains embedded NULs, then we can't
11050 handle it as the user wants. */
11051 if (strlen (TREE_STRING_POINTER (linkage))
11052 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11054 cp_parser_error (parser, "invalid linkage-specification");
11055 /* Assume C++ linkage. */
11056 linkage = lang_name_cplusplus;
11058 else
11059 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11061 /* We're now using the new linkage. */
11062 push_lang_context (linkage);
11064 /* If the next token is a `{', then we're using the first
11065 production. */
11066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11068 /* Consume the `{' token. */
11069 cp_lexer_consume_token (parser->lexer);
11070 /* Parse the declarations. */
11071 cp_parser_declaration_seq_opt (parser);
11072 /* Look for the closing `}'. */
11073 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11075 /* Otherwise, there's just one declaration. */
11076 else
11078 bool saved_in_unbraced_linkage_specification_p;
11080 saved_in_unbraced_linkage_specification_p
11081 = parser->in_unbraced_linkage_specification_p;
11082 parser->in_unbraced_linkage_specification_p = true;
11083 cp_parser_declaration (parser);
11084 parser->in_unbraced_linkage_specification_p
11085 = saved_in_unbraced_linkage_specification_p;
11088 /* We're done with the linkage-specification. */
11089 pop_lang_context ();
11092 /* Parse a static_assert-declaration.
11094 static_assert-declaration:
11095 static_assert ( constant-expression , string-literal ) ;
11097 If MEMBER_P, this static_assert is a class member. */
11099 static void
11100 cp_parser_static_assert(cp_parser *parser, bool member_p)
11102 tree condition;
11103 tree message;
11104 cp_token *token;
11105 location_t saved_loc;
11106 bool dummy;
11108 /* Peek at the `static_assert' token so we can keep track of exactly
11109 where the static assertion started. */
11110 token = cp_lexer_peek_token (parser->lexer);
11111 saved_loc = token->location;
11113 /* Look for the `static_assert' keyword. */
11114 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11115 RT_STATIC_ASSERT))
11116 return;
11118 /* We know we are in a static assertion; commit to any tentative
11119 parse. */
11120 if (cp_parser_parsing_tentatively (parser))
11121 cp_parser_commit_to_tentative_parse (parser);
11123 /* Parse the `(' starting the static assertion condition. */
11124 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11126 /* Parse the constant-expression. Allow a non-constant expression
11127 here in order to give better diagnostics in finish_static_assert. */
11128 condition =
11129 cp_parser_constant_expression (parser,
11130 /*allow_non_constant_p=*/true,
11131 /*non_constant_p=*/&dummy);
11133 /* Parse the separating `,'. */
11134 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11136 /* Parse the string-literal message. */
11137 message = cp_parser_string_literal (parser,
11138 /*translate=*/false,
11139 /*wide_ok=*/true);
11141 /* A `)' completes the static assertion. */
11142 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11143 cp_parser_skip_to_closing_parenthesis (parser,
11144 /*recovering=*/true,
11145 /*or_comma=*/false,
11146 /*consume_paren=*/true);
11148 /* A semicolon terminates the declaration. */
11149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11151 /* Complete the static assertion, which may mean either processing
11152 the static assert now or saving it for template instantiation. */
11153 finish_static_assert (condition, message, saved_loc, member_p);
11156 /* Parse a `decltype' type. Returns the type.
11158 simple-type-specifier:
11159 decltype ( expression ) */
11161 static tree
11162 cp_parser_decltype (cp_parser *parser)
11164 tree expr;
11165 bool id_expression_or_member_access_p = false;
11166 const char *saved_message;
11167 bool saved_integral_constant_expression_p;
11168 bool saved_non_integral_constant_expression_p;
11169 cp_token *id_expr_start_token;
11170 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11172 if (start_token->type == CPP_DECLTYPE)
11174 /* Already parsed. */
11175 cp_lexer_consume_token (parser->lexer);
11176 return start_token->u.value;
11179 /* Look for the `decltype' token. */
11180 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11181 return error_mark_node;
11183 /* Types cannot be defined in a `decltype' expression. Save away the
11184 old message. */
11185 saved_message = parser->type_definition_forbidden_message;
11187 /* And create the new one. */
11188 parser->type_definition_forbidden_message
11189 = G_("types may not be defined in %<decltype%> expressions");
11191 /* The restrictions on constant-expressions do not apply inside
11192 decltype expressions. */
11193 saved_integral_constant_expression_p
11194 = parser->integral_constant_expression_p;
11195 saved_non_integral_constant_expression_p
11196 = parser->non_integral_constant_expression_p;
11197 parser->integral_constant_expression_p = false;
11199 /* Do not actually evaluate the expression. */
11200 ++cp_unevaluated_operand;
11202 /* Do not warn about problems with the expression. */
11203 ++c_inhibit_evaluation_warnings;
11205 /* Parse the opening `('. */
11206 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11207 return error_mark_node;
11209 /* First, try parsing an id-expression. */
11210 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11211 cp_parser_parse_tentatively (parser);
11212 expr = cp_parser_id_expression (parser,
11213 /*template_keyword_p=*/false,
11214 /*check_dependency_p=*/true,
11215 /*template_p=*/NULL,
11216 /*declarator_p=*/false,
11217 /*optional_p=*/false);
11219 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11221 bool non_integral_constant_expression_p = false;
11222 tree id_expression = expr;
11223 cp_id_kind idk;
11224 const char *error_msg;
11226 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11227 /* Lookup the name we got back from the id-expression. */
11228 expr = cp_parser_lookup_name (parser, expr,
11229 none_type,
11230 /*is_template=*/false,
11231 /*is_namespace=*/false,
11232 /*check_dependency=*/true,
11233 /*ambiguous_decls=*/NULL,
11234 id_expr_start_token->location);
11236 if (expr
11237 && expr != error_mark_node
11238 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11239 && TREE_CODE (expr) != TYPE_DECL
11240 && (TREE_CODE (expr) != BIT_NOT_EXPR
11241 || !TYPE_P (TREE_OPERAND (expr, 0)))
11242 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11244 /* Complete lookup of the id-expression. */
11245 expr = (finish_id_expression
11246 (id_expression, expr, parser->scope, &idk,
11247 /*integral_constant_expression_p=*/false,
11248 /*allow_non_integral_constant_expression_p=*/true,
11249 &non_integral_constant_expression_p,
11250 /*template_p=*/false,
11251 /*done=*/true,
11252 /*address_p=*/false,
11253 /*template_arg_p=*/false,
11254 &error_msg,
11255 id_expr_start_token->location));
11257 if (expr == error_mark_node)
11258 /* We found an id-expression, but it was something that we
11259 should not have found. This is an error, not something
11260 we can recover from, so note that we found an
11261 id-expression and we'll recover as gracefully as
11262 possible. */
11263 id_expression_or_member_access_p = true;
11266 if (expr
11267 && expr != error_mark_node
11268 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11269 /* We have an id-expression. */
11270 id_expression_or_member_access_p = true;
11273 if (!id_expression_or_member_access_p)
11275 /* Abort the id-expression parse. */
11276 cp_parser_abort_tentative_parse (parser);
11278 /* Parsing tentatively, again. */
11279 cp_parser_parse_tentatively (parser);
11281 /* Parse a class member access. */
11282 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11283 /*cast_p=*/false,
11284 /*member_access_only_p=*/true, NULL);
11286 if (expr
11287 && expr != error_mark_node
11288 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11289 /* We have an id-expression. */
11290 id_expression_or_member_access_p = true;
11293 if (id_expression_or_member_access_p)
11294 /* We have parsed the complete id-expression or member access. */
11295 cp_parser_parse_definitely (parser);
11296 else
11298 bool saved_greater_than_is_operator_p;
11300 /* Abort our attempt to parse an id-expression or member access
11301 expression. */
11302 cp_parser_abort_tentative_parse (parser);
11304 /* Within a parenthesized expression, a `>' token is always
11305 the greater-than operator. */
11306 saved_greater_than_is_operator_p
11307 = parser->greater_than_is_operator_p;
11308 parser->greater_than_is_operator_p = true;
11310 /* Parse a full expression. */
11311 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11313 /* The `>' token might be the end of a template-id or
11314 template-parameter-list now. */
11315 parser->greater_than_is_operator_p
11316 = saved_greater_than_is_operator_p;
11319 /* Go back to evaluating expressions. */
11320 --cp_unevaluated_operand;
11321 --c_inhibit_evaluation_warnings;
11323 /* Restore the old message and the integral constant expression
11324 flags. */
11325 parser->type_definition_forbidden_message = saved_message;
11326 parser->integral_constant_expression_p
11327 = saved_integral_constant_expression_p;
11328 parser->non_integral_constant_expression_p
11329 = saved_non_integral_constant_expression_p;
11331 /* Parse to the closing `)'. */
11332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11334 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11335 /*consume_paren=*/true);
11336 return error_mark_node;
11339 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11340 tf_warning_or_error);
11342 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11343 it again. */
11344 start_token->type = CPP_DECLTYPE;
11345 start_token->u.value = expr;
11346 start_token->keyword = RID_MAX;
11347 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11349 return expr;
11352 /* Special member functions [gram.special] */
11354 /* Parse a conversion-function-id.
11356 conversion-function-id:
11357 operator conversion-type-id
11359 Returns an IDENTIFIER_NODE representing the operator. */
11361 static tree
11362 cp_parser_conversion_function_id (cp_parser* parser)
11364 tree type;
11365 tree saved_scope;
11366 tree saved_qualifying_scope;
11367 tree saved_object_scope;
11368 tree pushed_scope = NULL_TREE;
11370 /* Look for the `operator' token. */
11371 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11372 return error_mark_node;
11373 /* When we parse the conversion-type-id, the current scope will be
11374 reset. However, we need that information in able to look up the
11375 conversion function later, so we save it here. */
11376 saved_scope = parser->scope;
11377 saved_qualifying_scope = parser->qualifying_scope;
11378 saved_object_scope = parser->object_scope;
11379 /* We must enter the scope of the class so that the names of
11380 entities declared within the class are available in the
11381 conversion-type-id. For example, consider:
11383 struct S {
11384 typedef int I;
11385 operator I();
11388 S::operator I() { ... }
11390 In order to see that `I' is a type-name in the definition, we
11391 must be in the scope of `S'. */
11392 if (saved_scope)
11393 pushed_scope = push_scope (saved_scope);
11394 /* Parse the conversion-type-id. */
11395 type = cp_parser_conversion_type_id (parser);
11396 /* Leave the scope of the class, if any. */
11397 if (pushed_scope)
11398 pop_scope (pushed_scope);
11399 /* Restore the saved scope. */
11400 parser->scope = saved_scope;
11401 parser->qualifying_scope = saved_qualifying_scope;
11402 parser->object_scope = saved_object_scope;
11403 /* If the TYPE is invalid, indicate failure. */
11404 if (type == error_mark_node)
11405 return error_mark_node;
11406 return mangle_conv_op_name_for_type (type);
11409 /* Parse a conversion-type-id:
11411 conversion-type-id:
11412 type-specifier-seq conversion-declarator [opt]
11414 Returns the TYPE specified. */
11416 static tree
11417 cp_parser_conversion_type_id (cp_parser* parser)
11419 tree attributes;
11420 cp_decl_specifier_seq type_specifiers;
11421 cp_declarator *declarator;
11422 tree type_specified;
11424 /* Parse the attributes. */
11425 attributes = cp_parser_attributes_opt (parser);
11426 /* Parse the type-specifiers. */
11427 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11428 /*is_trailing_return=*/false,
11429 &type_specifiers);
11430 /* If that didn't work, stop. */
11431 if (type_specifiers.type == error_mark_node)
11432 return error_mark_node;
11433 /* Parse the conversion-declarator. */
11434 declarator = cp_parser_conversion_declarator_opt (parser);
11436 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11437 /*initialized=*/0, &attributes);
11438 if (attributes)
11439 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11441 /* Don't give this error when parsing tentatively. This happens to
11442 work because we always parse this definitively once. */
11443 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11444 && type_uses_auto (type_specified))
11446 if (cxx_dialect < cxx1y)
11448 error ("invalid use of %<auto%> in conversion operator");
11449 return error_mark_node;
11451 else if (template_parm_scope_p ())
11452 warning (0, "use of %<auto%> in member template "
11453 "conversion operator can never be deduced");
11456 return type_specified;
11459 /* Parse an (optional) conversion-declarator.
11461 conversion-declarator:
11462 ptr-operator conversion-declarator [opt]
11466 static cp_declarator *
11467 cp_parser_conversion_declarator_opt (cp_parser* parser)
11469 enum tree_code code;
11470 tree class_type, std_attributes = NULL_TREE;
11471 cp_cv_quals cv_quals;
11473 /* We don't know if there's a ptr-operator next, or not. */
11474 cp_parser_parse_tentatively (parser);
11475 /* Try the ptr-operator. */
11476 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11477 &std_attributes);
11478 /* If it worked, look for more conversion-declarators. */
11479 if (cp_parser_parse_definitely (parser))
11481 cp_declarator *declarator;
11483 /* Parse another optional declarator. */
11484 declarator = cp_parser_conversion_declarator_opt (parser);
11486 declarator = cp_parser_make_indirect_declarator
11487 (code, class_type, cv_quals, declarator, std_attributes);
11489 return declarator;
11492 return NULL;
11495 /* Parse an (optional) ctor-initializer.
11497 ctor-initializer:
11498 : mem-initializer-list
11500 Returns TRUE iff the ctor-initializer was actually present. */
11502 static bool
11503 cp_parser_ctor_initializer_opt (cp_parser* parser)
11505 /* If the next token is not a `:', then there is no
11506 ctor-initializer. */
11507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11509 /* Do default initialization of any bases and members. */
11510 if (DECL_CONSTRUCTOR_P (current_function_decl))
11511 finish_mem_initializers (NULL_TREE);
11513 return false;
11516 /* Consume the `:' token. */
11517 cp_lexer_consume_token (parser->lexer);
11518 /* And the mem-initializer-list. */
11519 cp_parser_mem_initializer_list (parser);
11521 return true;
11524 /* Parse a mem-initializer-list.
11526 mem-initializer-list:
11527 mem-initializer ... [opt]
11528 mem-initializer ... [opt] , mem-initializer-list */
11530 static void
11531 cp_parser_mem_initializer_list (cp_parser* parser)
11533 tree mem_initializer_list = NULL_TREE;
11534 tree target_ctor = error_mark_node;
11535 cp_token *token = cp_lexer_peek_token (parser->lexer);
11537 /* Let the semantic analysis code know that we are starting the
11538 mem-initializer-list. */
11539 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11540 error_at (token->location,
11541 "only constructors take member initializers");
11543 /* Loop through the list. */
11544 while (true)
11546 tree mem_initializer;
11548 token = cp_lexer_peek_token (parser->lexer);
11549 /* Parse the mem-initializer. */
11550 mem_initializer = cp_parser_mem_initializer (parser);
11551 /* If the next token is a `...', we're expanding member initializers. */
11552 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11554 /* Consume the `...'. */
11555 cp_lexer_consume_token (parser->lexer);
11557 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11558 can be expanded but members cannot. */
11559 if (mem_initializer != error_mark_node
11560 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11562 error_at (token->location,
11563 "cannot expand initializer for member %<%D%>",
11564 TREE_PURPOSE (mem_initializer));
11565 mem_initializer = error_mark_node;
11568 /* Construct the pack expansion type. */
11569 if (mem_initializer != error_mark_node)
11570 mem_initializer = make_pack_expansion (mem_initializer);
11572 if (target_ctor != error_mark_node
11573 && mem_initializer != error_mark_node)
11575 error ("mem-initializer for %qD follows constructor delegation",
11576 TREE_PURPOSE (mem_initializer));
11577 mem_initializer = error_mark_node;
11579 /* Look for a target constructor. */
11580 if (mem_initializer != error_mark_node
11581 && TYPE_P (TREE_PURPOSE (mem_initializer))
11582 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11584 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11585 if (mem_initializer_list)
11587 error ("constructor delegation follows mem-initializer for %qD",
11588 TREE_PURPOSE (mem_initializer_list));
11589 mem_initializer = error_mark_node;
11591 target_ctor = mem_initializer;
11593 /* Add it to the list, unless it was erroneous. */
11594 if (mem_initializer != error_mark_node)
11596 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11597 mem_initializer_list = mem_initializer;
11599 /* If the next token is not a `,', we're done. */
11600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11601 break;
11602 /* Consume the `,' token. */
11603 cp_lexer_consume_token (parser->lexer);
11606 /* Perform semantic analysis. */
11607 if (DECL_CONSTRUCTOR_P (current_function_decl))
11608 finish_mem_initializers (mem_initializer_list);
11611 /* Parse a mem-initializer.
11613 mem-initializer:
11614 mem-initializer-id ( expression-list [opt] )
11615 mem-initializer-id braced-init-list
11617 GNU extension:
11619 mem-initializer:
11620 ( expression-list [opt] )
11622 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11623 class) or FIELD_DECL (for a non-static data member) to initialize;
11624 the TREE_VALUE is the expression-list. An empty initialization
11625 list is represented by void_list_node. */
11627 static tree
11628 cp_parser_mem_initializer (cp_parser* parser)
11630 tree mem_initializer_id;
11631 tree expression_list;
11632 tree member;
11633 cp_token *token = cp_lexer_peek_token (parser->lexer);
11635 /* Find out what is being initialized. */
11636 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11638 permerror (token->location,
11639 "anachronistic old-style base class initializer");
11640 mem_initializer_id = NULL_TREE;
11642 else
11644 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11645 if (mem_initializer_id == error_mark_node)
11646 return mem_initializer_id;
11648 member = expand_member_init (mem_initializer_id);
11649 if (member && !DECL_P (member))
11650 in_base_initializer = 1;
11652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11654 bool expr_non_constant_p;
11655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11656 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11657 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11658 expression_list = build_tree_list (NULL_TREE, expression_list);
11660 else
11662 VEC(tree,gc)* vec;
11663 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11664 /*cast_p=*/false,
11665 /*allow_expansion_p=*/true,
11666 /*non_constant_p=*/NULL);
11667 if (vec == NULL)
11668 return error_mark_node;
11669 expression_list = build_tree_list_vec (vec);
11670 release_tree_vector (vec);
11673 if (expression_list == error_mark_node)
11674 return error_mark_node;
11675 if (!expression_list)
11676 expression_list = void_type_node;
11678 in_base_initializer = 0;
11680 return member ? build_tree_list (member, expression_list) : error_mark_node;
11683 /* Parse a mem-initializer-id.
11685 mem-initializer-id:
11686 :: [opt] nested-name-specifier [opt] class-name
11687 identifier
11689 Returns a TYPE indicating the class to be initializer for the first
11690 production. Returns an IDENTIFIER_NODE indicating the data member
11691 to be initialized for the second production. */
11693 static tree
11694 cp_parser_mem_initializer_id (cp_parser* parser)
11696 bool global_scope_p;
11697 bool nested_name_specifier_p;
11698 bool template_p = false;
11699 tree id;
11701 cp_token *token = cp_lexer_peek_token (parser->lexer);
11703 /* `typename' is not allowed in this context ([temp.res]). */
11704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11706 error_at (token->location,
11707 "keyword %<typename%> not allowed in this context (a qualified "
11708 "member initializer is implicitly a type)");
11709 cp_lexer_consume_token (parser->lexer);
11711 /* Look for the optional `::' operator. */
11712 global_scope_p
11713 = (cp_parser_global_scope_opt (parser,
11714 /*current_scope_valid_p=*/false)
11715 != NULL_TREE);
11716 /* Look for the optional nested-name-specifier. The simplest way to
11717 implement:
11719 [temp.res]
11721 The keyword `typename' is not permitted in a base-specifier or
11722 mem-initializer; in these contexts a qualified name that
11723 depends on a template-parameter is implicitly assumed to be a
11724 type name.
11726 is to assume that we have seen the `typename' keyword at this
11727 point. */
11728 nested_name_specifier_p
11729 = (cp_parser_nested_name_specifier_opt (parser,
11730 /*typename_keyword_p=*/true,
11731 /*check_dependency_p=*/true,
11732 /*type_p=*/true,
11733 /*is_declaration=*/true)
11734 != NULL_TREE);
11735 if (nested_name_specifier_p)
11736 template_p = cp_parser_optional_template_keyword (parser);
11737 /* If there is a `::' operator or a nested-name-specifier, then we
11738 are definitely looking for a class-name. */
11739 if (global_scope_p || nested_name_specifier_p)
11740 return cp_parser_class_name (parser,
11741 /*typename_keyword_p=*/true,
11742 /*template_keyword_p=*/template_p,
11743 typename_type,
11744 /*check_dependency_p=*/true,
11745 /*class_head_p=*/false,
11746 /*is_declaration=*/true);
11747 /* Otherwise, we could also be looking for an ordinary identifier. */
11748 cp_parser_parse_tentatively (parser);
11749 /* Try a class-name. */
11750 id = cp_parser_class_name (parser,
11751 /*typename_keyword_p=*/true,
11752 /*template_keyword_p=*/false,
11753 none_type,
11754 /*check_dependency_p=*/true,
11755 /*class_head_p=*/false,
11756 /*is_declaration=*/true);
11757 /* If we found one, we're done. */
11758 if (cp_parser_parse_definitely (parser))
11759 return id;
11760 /* Otherwise, look for an ordinary identifier. */
11761 return cp_parser_identifier (parser);
11764 /* Overloading [gram.over] */
11766 /* Parse an operator-function-id.
11768 operator-function-id:
11769 operator operator
11771 Returns an IDENTIFIER_NODE for the operator which is a
11772 human-readable spelling of the identifier, e.g., `operator +'. */
11774 static tree
11775 cp_parser_operator_function_id (cp_parser* parser)
11777 /* Look for the `operator' keyword. */
11778 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11779 return error_mark_node;
11780 /* And then the name of the operator itself. */
11781 return cp_parser_operator (parser);
11784 /* Return an identifier node for a user-defined literal operator.
11785 The suffix identifier is chained to the operator name identifier. */
11787 static tree
11788 cp_literal_operator_id (const char* name)
11790 tree identifier;
11791 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11792 + strlen (name) + 10);
11793 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11794 identifier = get_identifier (buffer);
11795 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11797 return identifier;
11800 /* Parse an operator.
11802 operator:
11803 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11804 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11805 || ++ -- , ->* -> () []
11807 GNU Extensions:
11809 operator:
11810 <? >? <?= >?=
11812 Returns an IDENTIFIER_NODE for the operator which is a
11813 human-readable spelling of the identifier, e.g., `operator +'. */
11815 static tree
11816 cp_parser_operator (cp_parser* parser)
11818 tree id = NULL_TREE;
11819 cp_token *token;
11821 /* Peek at the next token. */
11822 token = cp_lexer_peek_token (parser->lexer);
11823 /* Figure out which operator we have. */
11824 switch (token->type)
11826 case CPP_KEYWORD:
11828 enum tree_code op;
11830 /* The keyword should be either `new' or `delete'. */
11831 if (token->keyword == RID_NEW)
11832 op = NEW_EXPR;
11833 else if (token->keyword == RID_DELETE)
11834 op = DELETE_EXPR;
11835 else
11836 break;
11838 /* Consume the `new' or `delete' token. */
11839 cp_lexer_consume_token (parser->lexer);
11841 /* Peek at the next token. */
11842 token = cp_lexer_peek_token (parser->lexer);
11843 /* If it's a `[' token then this is the array variant of the
11844 operator. */
11845 if (token->type == CPP_OPEN_SQUARE)
11847 /* Consume the `[' token. */
11848 cp_lexer_consume_token (parser->lexer);
11849 /* Look for the `]' token. */
11850 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11851 id = ansi_opname (op == NEW_EXPR
11852 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11854 /* Otherwise, we have the non-array variant. */
11855 else
11856 id = ansi_opname (op);
11858 return id;
11861 case CPP_PLUS:
11862 id = ansi_opname (PLUS_EXPR);
11863 break;
11865 case CPP_MINUS:
11866 id = ansi_opname (MINUS_EXPR);
11867 break;
11869 case CPP_MULT:
11870 id = ansi_opname (MULT_EXPR);
11871 break;
11873 case CPP_DIV:
11874 id = ansi_opname (TRUNC_DIV_EXPR);
11875 break;
11877 case CPP_MOD:
11878 id = ansi_opname (TRUNC_MOD_EXPR);
11879 break;
11881 case CPP_XOR:
11882 id = ansi_opname (BIT_XOR_EXPR);
11883 break;
11885 case CPP_AND:
11886 id = ansi_opname (BIT_AND_EXPR);
11887 break;
11889 case CPP_OR:
11890 id = ansi_opname (BIT_IOR_EXPR);
11891 break;
11893 case CPP_COMPL:
11894 id = ansi_opname (BIT_NOT_EXPR);
11895 break;
11897 case CPP_NOT:
11898 id = ansi_opname (TRUTH_NOT_EXPR);
11899 break;
11901 case CPP_EQ:
11902 id = ansi_assopname (NOP_EXPR);
11903 break;
11905 case CPP_LESS:
11906 id = ansi_opname (LT_EXPR);
11907 break;
11909 case CPP_GREATER:
11910 id = ansi_opname (GT_EXPR);
11911 break;
11913 case CPP_PLUS_EQ:
11914 id = ansi_assopname (PLUS_EXPR);
11915 break;
11917 case CPP_MINUS_EQ:
11918 id = ansi_assopname (MINUS_EXPR);
11919 break;
11921 case CPP_MULT_EQ:
11922 id = ansi_assopname (MULT_EXPR);
11923 break;
11925 case CPP_DIV_EQ:
11926 id = ansi_assopname (TRUNC_DIV_EXPR);
11927 break;
11929 case CPP_MOD_EQ:
11930 id = ansi_assopname (TRUNC_MOD_EXPR);
11931 break;
11933 case CPP_XOR_EQ:
11934 id = ansi_assopname (BIT_XOR_EXPR);
11935 break;
11937 case CPP_AND_EQ:
11938 id = ansi_assopname (BIT_AND_EXPR);
11939 break;
11941 case CPP_OR_EQ:
11942 id = ansi_assopname (BIT_IOR_EXPR);
11943 break;
11945 case CPP_LSHIFT:
11946 id = ansi_opname (LSHIFT_EXPR);
11947 break;
11949 case CPP_RSHIFT:
11950 id = ansi_opname (RSHIFT_EXPR);
11951 break;
11953 case CPP_LSHIFT_EQ:
11954 id = ansi_assopname (LSHIFT_EXPR);
11955 break;
11957 case CPP_RSHIFT_EQ:
11958 id = ansi_assopname (RSHIFT_EXPR);
11959 break;
11961 case CPP_EQ_EQ:
11962 id = ansi_opname (EQ_EXPR);
11963 break;
11965 case CPP_NOT_EQ:
11966 id = ansi_opname (NE_EXPR);
11967 break;
11969 case CPP_LESS_EQ:
11970 id = ansi_opname (LE_EXPR);
11971 break;
11973 case CPP_GREATER_EQ:
11974 id = ansi_opname (GE_EXPR);
11975 break;
11977 case CPP_AND_AND:
11978 id = ansi_opname (TRUTH_ANDIF_EXPR);
11979 break;
11981 case CPP_OR_OR:
11982 id = ansi_opname (TRUTH_ORIF_EXPR);
11983 break;
11985 case CPP_PLUS_PLUS:
11986 id = ansi_opname (POSTINCREMENT_EXPR);
11987 break;
11989 case CPP_MINUS_MINUS:
11990 id = ansi_opname (PREDECREMENT_EXPR);
11991 break;
11993 case CPP_COMMA:
11994 id = ansi_opname (COMPOUND_EXPR);
11995 break;
11997 case CPP_DEREF_STAR:
11998 id = ansi_opname (MEMBER_REF);
11999 break;
12001 case CPP_DEREF:
12002 id = ansi_opname (COMPONENT_REF);
12003 break;
12005 case CPP_OPEN_PAREN:
12006 /* Consume the `('. */
12007 cp_lexer_consume_token (parser->lexer);
12008 /* Look for the matching `)'. */
12009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12010 return ansi_opname (CALL_EXPR);
12012 case CPP_OPEN_SQUARE:
12013 /* Consume the `['. */
12014 cp_lexer_consume_token (parser->lexer);
12015 /* Look for the matching `]'. */
12016 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12017 return ansi_opname (ARRAY_REF);
12019 case CPP_STRING:
12020 if (cxx_dialect == cxx98)
12021 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12022 if (TREE_STRING_LENGTH (token->u.value) > 2)
12024 error ("expected empty string after %<operator%> keyword");
12025 return error_mark_node;
12027 /* Consume the string. */
12028 cp_lexer_consume_token (parser->lexer);
12029 /* Look for the suffix identifier. */
12030 token = cp_lexer_peek_token (parser->lexer);
12031 if (token->type == CPP_NAME)
12033 id = cp_parser_identifier (parser);
12034 if (id != error_mark_node)
12036 const char *name = IDENTIFIER_POINTER (id);
12037 return cp_literal_operator_id (name);
12040 else
12042 error ("expected suffix identifier");
12043 return error_mark_node;
12046 case CPP_STRING_USERDEF:
12047 error ("missing space between %<\"\"%> and suffix identifier");
12048 return error_mark_node;
12050 default:
12051 /* Anything else is an error. */
12052 break;
12055 /* If we have selected an identifier, we need to consume the
12056 operator token. */
12057 if (id)
12058 cp_lexer_consume_token (parser->lexer);
12059 /* Otherwise, no valid operator name was present. */
12060 else
12062 cp_parser_error (parser, "expected operator");
12063 id = error_mark_node;
12066 return id;
12069 /* Parse a template-declaration.
12071 template-declaration:
12072 export [opt] template < template-parameter-list > declaration
12074 If MEMBER_P is TRUE, this template-declaration occurs within a
12075 class-specifier.
12077 The grammar rule given by the standard isn't correct. What
12078 is really meant is:
12080 template-declaration:
12081 export [opt] template-parameter-list-seq
12082 decl-specifier-seq [opt] init-declarator [opt] ;
12083 export [opt] template-parameter-list-seq
12084 function-definition
12086 template-parameter-list-seq:
12087 template-parameter-list-seq [opt]
12088 template < template-parameter-list > */
12090 static void
12091 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12093 /* Check for `export'. */
12094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12096 /* Consume the `export' token. */
12097 cp_lexer_consume_token (parser->lexer);
12098 /* Warn that we do not support `export'. */
12099 warning (0, "keyword %<export%> not implemented, and will be ignored");
12102 cp_parser_template_declaration_after_export (parser, member_p);
12105 /* Parse a template-parameter-list.
12107 template-parameter-list:
12108 template-parameter
12109 template-parameter-list , template-parameter
12111 Returns a TREE_LIST. Each node represents a template parameter.
12112 The nodes are connected via their TREE_CHAINs. */
12114 static tree
12115 cp_parser_template_parameter_list (cp_parser* parser)
12117 tree parameter_list = NULL_TREE;
12119 begin_template_parm_list ();
12121 /* The loop below parses the template parms. We first need to know
12122 the total number of template parms to be able to compute proper
12123 canonical types of each dependent type. So after the loop, when
12124 we know the total number of template parms,
12125 end_template_parm_list computes the proper canonical types and
12126 fixes up the dependent types accordingly. */
12127 while (true)
12129 tree parameter;
12130 bool is_non_type;
12131 bool is_parameter_pack;
12132 location_t parm_loc;
12134 /* Parse the template-parameter. */
12135 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12136 parameter = cp_parser_template_parameter (parser,
12137 &is_non_type,
12138 &is_parameter_pack);
12139 /* Add it to the list. */
12140 if (parameter != error_mark_node)
12141 parameter_list = process_template_parm (parameter_list,
12142 parm_loc,
12143 parameter,
12144 is_non_type,
12145 is_parameter_pack);
12146 else
12148 tree err_parm = build_tree_list (parameter, parameter);
12149 parameter_list = chainon (parameter_list, err_parm);
12152 /* If the next token is not a `,', we're done. */
12153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12154 break;
12155 /* Otherwise, consume the `,' token. */
12156 cp_lexer_consume_token (parser->lexer);
12159 return end_template_parm_list (parameter_list);
12162 /* Parse a template-parameter.
12164 template-parameter:
12165 type-parameter
12166 parameter-declaration
12168 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12169 the parameter. The TREE_PURPOSE is the default value, if any.
12170 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12171 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12172 set to true iff this parameter is a parameter pack. */
12174 static tree
12175 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12176 bool *is_parameter_pack)
12178 cp_token *token;
12179 cp_parameter_declarator *parameter_declarator;
12180 cp_declarator *id_declarator;
12181 tree parm;
12183 /* Assume it is a type parameter or a template parameter. */
12184 *is_non_type = false;
12185 /* Assume it not a parameter pack. */
12186 *is_parameter_pack = false;
12187 /* Peek at the next token. */
12188 token = cp_lexer_peek_token (parser->lexer);
12189 /* If it is `class' or `template', we have a type-parameter. */
12190 if (token->keyword == RID_TEMPLATE)
12191 return cp_parser_type_parameter (parser, is_parameter_pack);
12192 /* If it is `class' or `typename' we do not know yet whether it is a
12193 type parameter or a non-type parameter. Consider:
12195 template <typename T, typename T::X X> ...
12199 template <class C, class D*> ...
12201 Here, the first parameter is a type parameter, and the second is
12202 a non-type parameter. We can tell by looking at the token after
12203 the identifier -- if it is a `,', `=', or `>' then we have a type
12204 parameter. */
12205 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12207 /* Peek at the token after `class' or `typename'. */
12208 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12209 /* If it's an ellipsis, we have a template type parameter
12210 pack. */
12211 if (token->type == CPP_ELLIPSIS)
12212 return cp_parser_type_parameter (parser, is_parameter_pack);
12213 /* If it's an identifier, skip it. */
12214 if (token->type == CPP_NAME)
12215 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12216 /* Now, see if the token looks like the end of a template
12217 parameter. */
12218 if (token->type == CPP_COMMA
12219 || token->type == CPP_EQ
12220 || token->type == CPP_GREATER)
12221 return cp_parser_type_parameter (parser, is_parameter_pack);
12224 /* Otherwise, it is a non-type parameter.
12226 [temp.param]
12228 When parsing a default template-argument for a non-type
12229 template-parameter, the first non-nested `>' is taken as the end
12230 of the template parameter-list rather than a greater-than
12231 operator. */
12232 *is_non_type = true;
12233 parameter_declarator
12234 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12235 /*parenthesized_p=*/NULL);
12237 /* If the parameter declaration is marked as a parameter pack, set
12238 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12239 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12240 grokdeclarator. */
12241 if (parameter_declarator
12242 && parameter_declarator->declarator
12243 && parameter_declarator->declarator->parameter_pack_p)
12245 *is_parameter_pack = true;
12246 parameter_declarator->declarator->parameter_pack_p = false;
12249 /* If the next token is an ellipsis, and we don't already have it
12250 marked as a parameter pack, then we have a parameter pack (that
12251 has no declarator). */
12252 if (!*is_parameter_pack
12253 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12254 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12256 /* Consume the `...'. */
12257 cp_lexer_consume_token (parser->lexer);
12258 maybe_warn_variadic_templates ();
12260 *is_parameter_pack = true;
12262 /* We might end up with a pack expansion as the type of the non-type
12263 template parameter, in which case this is a non-type template
12264 parameter pack. */
12265 else if (parameter_declarator
12266 && parameter_declarator->decl_specifiers.type
12267 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12269 *is_parameter_pack = true;
12270 parameter_declarator->decl_specifiers.type =
12271 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12274 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12276 /* Parameter packs cannot have default arguments. However, a
12277 user may try to do so, so we'll parse them and give an
12278 appropriate diagnostic here. */
12280 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12282 /* Find the name of the parameter pack. */
12283 id_declarator = parameter_declarator->declarator;
12284 while (id_declarator && id_declarator->kind != cdk_id)
12285 id_declarator = id_declarator->declarator;
12287 if (id_declarator && id_declarator->kind == cdk_id)
12288 error_at (start_token->location,
12289 "template parameter pack %qD cannot have a default argument",
12290 id_declarator->u.id.unqualified_name);
12291 else
12292 error_at (start_token->location,
12293 "template parameter pack cannot have a default argument");
12295 /* Parse the default argument, but throw away the result. */
12296 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12299 parm = grokdeclarator (parameter_declarator->declarator,
12300 &parameter_declarator->decl_specifiers,
12301 TPARM, /*initialized=*/0,
12302 /*attrlist=*/NULL);
12303 if (parm == error_mark_node)
12304 return error_mark_node;
12306 return build_tree_list (parameter_declarator->default_argument, parm);
12309 /* Parse a type-parameter.
12311 type-parameter:
12312 class identifier [opt]
12313 class identifier [opt] = type-id
12314 typename identifier [opt]
12315 typename identifier [opt] = type-id
12316 template < template-parameter-list > class identifier [opt]
12317 template < template-parameter-list > class identifier [opt]
12318 = id-expression
12320 GNU Extension (variadic templates):
12322 type-parameter:
12323 class ... identifier [opt]
12324 typename ... identifier [opt]
12326 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12327 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12328 the declaration of the parameter.
12330 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12332 static tree
12333 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12335 cp_token *token;
12336 tree parameter;
12338 /* Look for a keyword to tell us what kind of parameter this is. */
12339 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12340 if (!token)
12341 return error_mark_node;
12343 switch (token->keyword)
12345 case RID_CLASS:
12346 case RID_TYPENAME:
12348 tree identifier;
12349 tree default_argument;
12351 /* If the next token is an ellipsis, we have a template
12352 argument pack. */
12353 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12355 /* Consume the `...' token. */
12356 cp_lexer_consume_token (parser->lexer);
12357 maybe_warn_variadic_templates ();
12359 *is_parameter_pack = true;
12362 /* If the next token is an identifier, then it names the
12363 parameter. */
12364 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12365 identifier = cp_parser_identifier (parser);
12366 else
12367 identifier = NULL_TREE;
12369 /* Create the parameter. */
12370 parameter = finish_template_type_parm (class_type_node, identifier);
12372 /* If the next token is an `=', we have a default argument. */
12373 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12375 /* Consume the `=' token. */
12376 cp_lexer_consume_token (parser->lexer);
12377 /* Parse the default-argument. */
12378 push_deferring_access_checks (dk_no_deferred);
12379 default_argument = cp_parser_type_id (parser);
12381 /* Template parameter packs cannot have default
12382 arguments. */
12383 if (*is_parameter_pack)
12385 if (identifier)
12386 error_at (token->location,
12387 "template parameter pack %qD cannot have a "
12388 "default argument", identifier);
12389 else
12390 error_at (token->location,
12391 "template parameter packs cannot have "
12392 "default arguments");
12393 default_argument = NULL_TREE;
12395 pop_deferring_access_checks ();
12397 else
12398 default_argument = NULL_TREE;
12400 /* Create the combined representation of the parameter and the
12401 default argument. */
12402 parameter = build_tree_list (default_argument, parameter);
12404 break;
12406 case RID_TEMPLATE:
12408 tree identifier;
12409 tree default_argument;
12411 /* Look for the `<'. */
12412 cp_parser_require (parser, CPP_LESS, RT_LESS);
12413 /* Parse the template-parameter-list. */
12414 cp_parser_template_parameter_list (parser);
12415 /* Look for the `>'. */
12416 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12417 /* Look for the `class' keyword. */
12418 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12419 /* If the next token is an ellipsis, we have a template
12420 argument pack. */
12421 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12423 /* Consume the `...' token. */
12424 cp_lexer_consume_token (parser->lexer);
12425 maybe_warn_variadic_templates ();
12427 *is_parameter_pack = true;
12429 /* If the next token is an `=', then there is a
12430 default-argument. If the next token is a `>', we are at
12431 the end of the parameter-list. If the next token is a `,',
12432 then we are at the end of this parameter. */
12433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12434 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12435 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12437 identifier = cp_parser_identifier (parser);
12438 /* Treat invalid names as if the parameter were nameless. */
12439 if (identifier == error_mark_node)
12440 identifier = NULL_TREE;
12442 else
12443 identifier = NULL_TREE;
12445 /* Create the template parameter. */
12446 parameter = finish_template_template_parm (class_type_node,
12447 identifier);
12449 /* If the next token is an `=', then there is a
12450 default-argument. */
12451 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12453 bool is_template;
12455 /* Consume the `='. */
12456 cp_lexer_consume_token (parser->lexer);
12457 /* Parse the id-expression. */
12458 push_deferring_access_checks (dk_no_deferred);
12459 /* save token before parsing the id-expression, for error
12460 reporting */
12461 token = cp_lexer_peek_token (parser->lexer);
12462 default_argument
12463 = cp_parser_id_expression (parser,
12464 /*template_keyword_p=*/false,
12465 /*check_dependency_p=*/true,
12466 /*template_p=*/&is_template,
12467 /*declarator_p=*/false,
12468 /*optional_p=*/false);
12469 if (TREE_CODE (default_argument) == TYPE_DECL)
12470 /* If the id-expression was a template-id that refers to
12471 a template-class, we already have the declaration here,
12472 so no further lookup is needed. */
12474 else
12475 /* Look up the name. */
12476 default_argument
12477 = cp_parser_lookup_name (parser, default_argument,
12478 none_type,
12479 /*is_template=*/is_template,
12480 /*is_namespace=*/false,
12481 /*check_dependency=*/true,
12482 /*ambiguous_decls=*/NULL,
12483 token->location);
12484 /* See if the default argument is valid. */
12485 default_argument
12486 = check_template_template_default_arg (default_argument);
12488 /* Template parameter packs cannot have default
12489 arguments. */
12490 if (*is_parameter_pack)
12492 if (identifier)
12493 error_at (token->location,
12494 "template parameter pack %qD cannot "
12495 "have a default argument",
12496 identifier);
12497 else
12498 error_at (token->location, "template parameter packs cannot "
12499 "have default arguments");
12500 default_argument = NULL_TREE;
12502 pop_deferring_access_checks ();
12504 else
12505 default_argument = NULL_TREE;
12507 /* Create the combined representation of the parameter and the
12508 default argument. */
12509 parameter = build_tree_list (default_argument, parameter);
12511 break;
12513 default:
12514 gcc_unreachable ();
12515 break;
12518 return parameter;
12521 /* Parse a template-id.
12523 template-id:
12524 template-name < template-argument-list [opt] >
12526 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12527 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12528 returned. Otherwise, if the template-name names a function, or set
12529 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12530 names a class, returns a TYPE_DECL for the specialization.
12532 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12533 uninstantiated templates. */
12535 static tree
12536 cp_parser_template_id (cp_parser *parser,
12537 bool template_keyword_p,
12538 bool check_dependency_p,
12539 enum tag_types tag_type,
12540 bool is_declaration)
12542 int i;
12543 tree templ;
12544 tree arguments;
12545 tree template_id;
12546 cp_token_position start_of_id = 0;
12547 deferred_access_check *chk;
12548 VEC (deferred_access_check,gc) *access_check;
12549 cp_token *next_token = NULL, *next_token_2 = NULL;
12550 bool is_identifier;
12552 /* If the next token corresponds to a template-id, there is no need
12553 to reparse it. */
12554 next_token = cp_lexer_peek_token (parser->lexer);
12555 if (next_token->type == CPP_TEMPLATE_ID)
12557 struct tree_check *check_value;
12559 /* Get the stored value. */
12560 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12561 /* Perform any access checks that were deferred. */
12562 access_check = check_value->checks;
12563 if (access_check)
12565 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12566 perform_or_defer_access_check (chk->binfo,
12567 chk->decl,
12568 chk->diag_decl,
12569 tf_warning_or_error);
12571 /* Return the stored value. */
12572 return check_value->value;
12575 /* Avoid performing name lookup if there is no possibility of
12576 finding a template-id. */
12577 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12578 || (next_token->type == CPP_NAME
12579 && !cp_parser_nth_token_starts_template_argument_list_p
12580 (parser, 2)))
12582 cp_parser_error (parser, "expected template-id");
12583 return error_mark_node;
12586 /* Remember where the template-id starts. */
12587 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12588 start_of_id = cp_lexer_token_position (parser->lexer, false);
12590 push_deferring_access_checks (dk_deferred);
12592 /* Parse the template-name. */
12593 is_identifier = false;
12594 templ = cp_parser_template_name (parser, template_keyword_p,
12595 check_dependency_p,
12596 is_declaration,
12597 tag_type,
12598 &is_identifier);
12599 if (templ == error_mark_node || is_identifier)
12601 pop_deferring_access_checks ();
12602 return templ;
12605 /* If we find the sequence `[:' after a template-name, it's probably
12606 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12607 parse correctly the argument list. */
12608 next_token = cp_lexer_peek_token (parser->lexer);
12609 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12610 if (next_token->type == CPP_OPEN_SQUARE
12611 && next_token->flags & DIGRAPH
12612 && next_token_2->type == CPP_COLON
12613 && !(next_token_2->flags & PREV_WHITE))
12615 cp_parser_parse_tentatively (parser);
12616 /* Change `:' into `::'. */
12617 next_token_2->type = CPP_SCOPE;
12618 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12619 CPP_LESS. */
12620 cp_lexer_consume_token (parser->lexer);
12622 /* Parse the arguments. */
12623 arguments = cp_parser_enclosed_template_argument_list (parser);
12624 if (!cp_parser_parse_definitely (parser))
12626 /* If we couldn't parse an argument list, then we revert our changes
12627 and return simply an error. Maybe this is not a template-id
12628 after all. */
12629 next_token_2->type = CPP_COLON;
12630 cp_parser_error (parser, "expected %<<%>");
12631 pop_deferring_access_checks ();
12632 return error_mark_node;
12634 /* Otherwise, emit an error about the invalid digraph, but continue
12635 parsing because we got our argument list. In C++11 do not emit
12636 any error, per 2.5/3. */
12637 if (cxx_dialect < cxx0x
12638 && permerror (next_token->location,
12639 "%<<::%> cannot begin a template-argument list"))
12641 static bool hint = false;
12642 inform (next_token->location,
12643 "%<<:%> is an alternate spelling for %<[%>."
12644 " Insert whitespace between %<<%> and %<::%>");
12645 if (!hint && !flag_permissive)
12647 inform (next_token->location, "(if you use %<-fpermissive%> "
12648 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12649 "accept your code)");
12650 hint = true;
12654 else
12656 /* Look for the `<' that starts the template-argument-list. */
12657 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12659 pop_deferring_access_checks ();
12660 return error_mark_node;
12662 /* Parse the arguments. */
12663 arguments = cp_parser_enclosed_template_argument_list (parser);
12666 /* Build a representation of the specialization. */
12667 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12668 template_id = build_min_nt_loc (next_token->location,
12669 TEMPLATE_ID_EXPR,
12670 templ, arguments);
12671 else if (DECL_TYPE_TEMPLATE_P (templ)
12672 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12674 bool entering_scope;
12675 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12676 template (rather than some instantiation thereof) only if
12677 is not nested within some other construct. For example, in
12678 "template <typename T> void f(T) { A<T>::", A<T> is just an
12679 instantiation of A. */
12680 entering_scope = (template_parm_scope_p ()
12681 && cp_lexer_next_token_is (parser->lexer,
12682 CPP_SCOPE));
12683 template_id
12684 = finish_template_type (templ, arguments, entering_scope);
12686 else
12688 /* If it's not a class-template or a template-template, it should be
12689 a function-template. */
12690 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12691 || TREE_CODE (templ) == OVERLOAD
12692 || BASELINK_P (templ)));
12694 template_id = lookup_template_function (templ, arguments);
12697 /* If parsing tentatively, replace the sequence of tokens that makes
12698 up the template-id with a CPP_TEMPLATE_ID token. That way,
12699 should we re-parse the token stream, we will not have to repeat
12700 the effort required to do the parse, nor will we issue duplicate
12701 error messages about problems during instantiation of the
12702 template. */
12703 if (start_of_id)
12705 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12707 /* Reset the contents of the START_OF_ID token. */
12708 token->type = CPP_TEMPLATE_ID;
12709 /* Retrieve any deferred checks. Do not pop this access checks yet
12710 so the memory will not be reclaimed during token replacing below. */
12711 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12712 token->u.tree_check_value->value = template_id;
12713 token->u.tree_check_value->checks = get_deferred_access_checks ();
12714 token->keyword = RID_MAX;
12716 /* Purge all subsequent tokens. */
12717 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12719 /* ??? Can we actually assume that, if template_id ==
12720 error_mark_node, we will have issued a diagnostic to the
12721 user, as opposed to simply marking the tentative parse as
12722 failed? */
12723 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12724 error_at (token->location, "parse error in template argument list");
12727 pop_deferring_access_checks ();
12728 return template_id;
12731 /* Parse a template-name.
12733 template-name:
12734 identifier
12736 The standard should actually say:
12738 template-name:
12739 identifier
12740 operator-function-id
12742 A defect report has been filed about this issue.
12744 A conversion-function-id cannot be a template name because they cannot
12745 be part of a template-id. In fact, looking at this code:
12747 a.operator K<int>()
12749 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12750 It is impossible to call a templated conversion-function-id with an
12751 explicit argument list, since the only allowed template parameter is
12752 the type to which it is converting.
12754 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12755 `template' keyword, in a construction like:
12757 T::template f<3>()
12759 In that case `f' is taken to be a template-name, even though there
12760 is no way of knowing for sure.
12762 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12763 name refers to a set of overloaded functions, at least one of which
12764 is a template, or an IDENTIFIER_NODE with the name of the template,
12765 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12766 names are looked up inside uninstantiated templates. */
12768 static tree
12769 cp_parser_template_name (cp_parser* parser,
12770 bool template_keyword_p,
12771 bool check_dependency_p,
12772 bool is_declaration,
12773 enum tag_types tag_type,
12774 bool *is_identifier)
12776 tree identifier;
12777 tree decl;
12778 tree fns;
12779 cp_token *token = cp_lexer_peek_token (parser->lexer);
12781 /* If the next token is `operator', then we have either an
12782 operator-function-id or a conversion-function-id. */
12783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12785 /* We don't know whether we're looking at an
12786 operator-function-id or a conversion-function-id. */
12787 cp_parser_parse_tentatively (parser);
12788 /* Try an operator-function-id. */
12789 identifier = cp_parser_operator_function_id (parser);
12790 /* If that didn't work, try a conversion-function-id. */
12791 if (!cp_parser_parse_definitely (parser))
12793 cp_parser_error (parser, "expected template-name");
12794 return error_mark_node;
12797 /* Look for the identifier. */
12798 else
12799 identifier = cp_parser_identifier (parser);
12801 /* If we didn't find an identifier, we don't have a template-id. */
12802 if (identifier == error_mark_node)
12803 return error_mark_node;
12805 /* If the name immediately followed the `template' keyword, then it
12806 is a template-name. However, if the next token is not `<', then
12807 we do not treat it as a template-name, since it is not being used
12808 as part of a template-id. This enables us to handle constructs
12809 like:
12811 template <typename T> struct S { S(); };
12812 template <typename T> S<T>::S();
12814 correctly. We would treat `S' as a template -- if it were `S<T>'
12815 -- but we do not if there is no `<'. */
12817 if (processing_template_decl
12818 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12820 /* In a declaration, in a dependent context, we pretend that the
12821 "template" keyword was present in order to improve error
12822 recovery. For example, given:
12824 template <typename T> void f(T::X<int>);
12826 we want to treat "X<int>" as a template-id. */
12827 if (is_declaration
12828 && !template_keyword_p
12829 && parser->scope && TYPE_P (parser->scope)
12830 && check_dependency_p
12831 && dependent_scope_p (parser->scope)
12832 /* Do not do this for dtors (or ctors), since they never
12833 need the template keyword before their name. */
12834 && !constructor_name_p (identifier, parser->scope))
12836 cp_token_position start = 0;
12838 /* Explain what went wrong. */
12839 error_at (token->location, "non-template %qD used as template",
12840 identifier);
12841 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12842 parser->scope, identifier);
12843 /* If parsing tentatively, find the location of the "<" token. */
12844 if (cp_parser_simulate_error (parser))
12845 start = cp_lexer_token_position (parser->lexer, true);
12846 /* Parse the template arguments so that we can issue error
12847 messages about them. */
12848 cp_lexer_consume_token (parser->lexer);
12849 cp_parser_enclosed_template_argument_list (parser);
12850 /* Skip tokens until we find a good place from which to
12851 continue parsing. */
12852 cp_parser_skip_to_closing_parenthesis (parser,
12853 /*recovering=*/true,
12854 /*or_comma=*/true,
12855 /*consume_paren=*/false);
12856 /* If parsing tentatively, permanently remove the
12857 template argument list. That will prevent duplicate
12858 error messages from being issued about the missing
12859 "template" keyword. */
12860 if (start)
12861 cp_lexer_purge_tokens_after (parser->lexer, start);
12862 if (is_identifier)
12863 *is_identifier = true;
12864 return identifier;
12867 /* If the "template" keyword is present, then there is generally
12868 no point in doing name-lookup, so we just return IDENTIFIER.
12869 But, if the qualifying scope is non-dependent then we can
12870 (and must) do name-lookup normally. */
12871 if (template_keyword_p
12872 && (!parser->scope
12873 || (TYPE_P (parser->scope)
12874 && dependent_type_p (parser->scope))))
12875 return identifier;
12878 /* Look up the name. */
12879 decl = cp_parser_lookup_name (parser, identifier,
12880 tag_type,
12881 /*is_template=*/true,
12882 /*is_namespace=*/false,
12883 check_dependency_p,
12884 /*ambiguous_decls=*/NULL,
12885 token->location);
12887 /* If DECL is a template, then the name was a template-name. */
12888 if (TREE_CODE (decl) == TEMPLATE_DECL)
12890 else
12892 tree fn = NULL_TREE;
12894 /* The standard does not explicitly indicate whether a name that
12895 names a set of overloaded declarations, some of which are
12896 templates, is a template-name. However, such a name should
12897 be a template-name; otherwise, there is no way to form a
12898 template-id for the overloaded templates. */
12899 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12900 if (TREE_CODE (fns) == OVERLOAD)
12901 for (fn = fns; fn; fn = OVL_NEXT (fn))
12902 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12903 break;
12905 if (!fn)
12907 /* The name does not name a template. */
12908 cp_parser_error (parser, "expected template-name");
12909 return error_mark_node;
12913 /* If DECL is dependent, and refers to a function, then just return
12914 its name; we will look it up again during template instantiation. */
12915 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12917 tree scope = ovl_scope (decl);
12918 if (TYPE_P (scope) && dependent_type_p (scope))
12919 return identifier;
12922 return decl;
12925 /* Parse a template-argument-list.
12927 template-argument-list:
12928 template-argument ... [opt]
12929 template-argument-list , template-argument ... [opt]
12931 Returns a TREE_VEC containing the arguments. */
12933 static tree
12934 cp_parser_template_argument_list (cp_parser* parser)
12936 tree fixed_args[10];
12937 unsigned n_args = 0;
12938 unsigned alloced = 10;
12939 tree *arg_ary = fixed_args;
12940 tree vec;
12941 bool saved_in_template_argument_list_p;
12942 bool saved_ice_p;
12943 bool saved_non_ice_p;
12945 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12946 parser->in_template_argument_list_p = true;
12947 /* Even if the template-id appears in an integral
12948 constant-expression, the contents of the argument list do
12949 not. */
12950 saved_ice_p = parser->integral_constant_expression_p;
12951 parser->integral_constant_expression_p = false;
12952 saved_non_ice_p = parser->non_integral_constant_expression_p;
12953 parser->non_integral_constant_expression_p = false;
12955 /* Parse the arguments. */
12958 tree argument;
12960 if (n_args)
12961 /* Consume the comma. */
12962 cp_lexer_consume_token (parser->lexer);
12964 /* Parse the template-argument. */
12965 argument = cp_parser_template_argument (parser);
12967 /* If the next token is an ellipsis, we're expanding a template
12968 argument pack. */
12969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12971 if (argument == error_mark_node)
12973 cp_token *token = cp_lexer_peek_token (parser->lexer);
12974 error_at (token->location,
12975 "expected parameter pack before %<...%>");
12977 /* Consume the `...' token. */
12978 cp_lexer_consume_token (parser->lexer);
12980 /* Make the argument into a TYPE_PACK_EXPANSION or
12981 EXPR_PACK_EXPANSION. */
12982 argument = make_pack_expansion (argument);
12985 if (n_args == alloced)
12987 alloced *= 2;
12989 if (arg_ary == fixed_args)
12991 arg_ary = XNEWVEC (tree, alloced);
12992 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12994 else
12995 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12997 arg_ary[n_args++] = argument;
12999 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13001 vec = make_tree_vec (n_args);
13003 while (n_args--)
13004 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13006 if (arg_ary != fixed_args)
13007 free (arg_ary);
13008 parser->non_integral_constant_expression_p = saved_non_ice_p;
13009 parser->integral_constant_expression_p = saved_ice_p;
13010 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13011 #ifdef ENABLE_CHECKING
13012 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13013 #endif
13014 return vec;
13017 /* Parse a template-argument.
13019 template-argument:
13020 assignment-expression
13021 type-id
13022 id-expression
13024 The representation is that of an assignment-expression, type-id, or
13025 id-expression -- except that the qualified id-expression is
13026 evaluated, so that the value returned is either a DECL or an
13027 OVERLOAD.
13029 Although the standard says "assignment-expression", it forbids
13030 throw-expressions or assignments in the template argument.
13031 Therefore, we use "conditional-expression" instead. */
13033 static tree
13034 cp_parser_template_argument (cp_parser* parser)
13036 tree argument;
13037 bool template_p;
13038 bool address_p;
13039 bool maybe_type_id = false;
13040 cp_token *token = NULL, *argument_start_token = NULL;
13041 location_t loc = 0;
13042 cp_id_kind idk;
13044 /* There's really no way to know what we're looking at, so we just
13045 try each alternative in order.
13047 [temp.arg]
13049 In a template-argument, an ambiguity between a type-id and an
13050 expression is resolved to a type-id, regardless of the form of
13051 the corresponding template-parameter.
13053 Therefore, we try a type-id first. */
13054 cp_parser_parse_tentatively (parser);
13055 argument = cp_parser_template_type_arg (parser);
13056 /* If there was no error parsing the type-id but the next token is a
13057 '>>', our behavior depends on which dialect of C++ we're
13058 parsing. In C++98, we probably found a typo for '> >'. But there
13059 are type-id which are also valid expressions. For instance:
13061 struct X { int operator >> (int); };
13062 template <int V> struct Foo {};
13063 Foo<X () >> 5> r;
13065 Here 'X()' is a valid type-id of a function type, but the user just
13066 wanted to write the expression "X() >> 5". Thus, we remember that we
13067 found a valid type-id, but we still try to parse the argument as an
13068 expression to see what happens.
13070 In C++0x, the '>>' will be considered two separate '>'
13071 tokens. */
13072 if (!cp_parser_error_occurred (parser)
13073 && cxx_dialect == cxx98
13074 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13076 maybe_type_id = true;
13077 cp_parser_abort_tentative_parse (parser);
13079 else
13081 /* If the next token isn't a `,' or a `>', then this argument wasn't
13082 really finished. This means that the argument is not a valid
13083 type-id. */
13084 if (!cp_parser_next_token_ends_template_argument_p (parser))
13085 cp_parser_error (parser, "expected template-argument");
13086 /* If that worked, we're done. */
13087 if (cp_parser_parse_definitely (parser))
13088 return argument;
13090 /* We're still not sure what the argument will be. */
13091 cp_parser_parse_tentatively (parser);
13092 /* Try a template. */
13093 argument_start_token = cp_lexer_peek_token (parser->lexer);
13094 argument = cp_parser_id_expression (parser,
13095 /*template_keyword_p=*/false,
13096 /*check_dependency_p=*/true,
13097 &template_p,
13098 /*declarator_p=*/false,
13099 /*optional_p=*/false);
13100 /* If the next token isn't a `,' or a `>', then this argument wasn't
13101 really finished. */
13102 if (!cp_parser_next_token_ends_template_argument_p (parser))
13103 cp_parser_error (parser, "expected template-argument");
13104 if (!cp_parser_error_occurred (parser))
13106 /* Figure out what is being referred to. If the id-expression
13107 was for a class template specialization, then we will have a
13108 TYPE_DECL at this point. There is no need to do name lookup
13109 at this point in that case. */
13110 if (TREE_CODE (argument) != TYPE_DECL)
13111 argument = cp_parser_lookup_name (parser, argument,
13112 none_type,
13113 /*is_template=*/template_p,
13114 /*is_namespace=*/false,
13115 /*check_dependency=*/true,
13116 /*ambiguous_decls=*/NULL,
13117 argument_start_token->location);
13118 if (TREE_CODE (argument) != TEMPLATE_DECL
13119 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13120 cp_parser_error (parser, "expected template-name");
13122 if (cp_parser_parse_definitely (parser))
13123 return argument;
13124 /* It must be a non-type argument. There permitted cases are given
13125 in [temp.arg.nontype]:
13127 -- an integral constant-expression of integral or enumeration
13128 type; or
13130 -- the name of a non-type template-parameter; or
13132 -- the name of an object or function with external linkage...
13134 -- the address of an object or function with external linkage...
13136 -- a pointer to member... */
13137 /* Look for a non-type template parameter. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13140 cp_parser_parse_tentatively (parser);
13141 argument = cp_parser_primary_expression (parser,
13142 /*address_p=*/false,
13143 /*cast_p=*/false,
13144 /*template_arg_p=*/true,
13145 &idk);
13146 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13147 || !cp_parser_next_token_ends_template_argument_p (parser))
13148 cp_parser_simulate_error (parser);
13149 if (cp_parser_parse_definitely (parser))
13150 return argument;
13153 /* If the next token is "&", the argument must be the address of an
13154 object or function with external linkage. */
13155 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13156 if (address_p)
13158 loc = cp_lexer_peek_token (parser->lexer)->location;
13159 cp_lexer_consume_token (parser->lexer);
13161 /* See if we might have an id-expression. */
13162 token = cp_lexer_peek_token (parser->lexer);
13163 if (token->type == CPP_NAME
13164 || token->keyword == RID_OPERATOR
13165 || token->type == CPP_SCOPE
13166 || token->type == CPP_TEMPLATE_ID
13167 || token->type == CPP_NESTED_NAME_SPECIFIER)
13169 cp_parser_parse_tentatively (parser);
13170 argument = cp_parser_primary_expression (parser,
13171 address_p,
13172 /*cast_p=*/false,
13173 /*template_arg_p=*/true,
13174 &idk);
13175 if (cp_parser_error_occurred (parser)
13176 || !cp_parser_next_token_ends_template_argument_p (parser))
13177 cp_parser_abort_tentative_parse (parser);
13178 else
13180 tree probe;
13182 if (TREE_CODE (argument) == INDIRECT_REF)
13184 gcc_assert (REFERENCE_REF_P (argument));
13185 argument = TREE_OPERAND (argument, 0);
13188 /* If we're in a template, we represent a qualified-id referring
13189 to a static data member as a SCOPE_REF even if the scope isn't
13190 dependent so that we can check access control later. */
13191 probe = argument;
13192 if (TREE_CODE (probe) == SCOPE_REF)
13193 probe = TREE_OPERAND (probe, 1);
13194 if (TREE_CODE (probe) == VAR_DECL)
13196 /* A variable without external linkage might still be a
13197 valid constant-expression, so no error is issued here
13198 if the external-linkage check fails. */
13199 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13200 cp_parser_simulate_error (parser);
13202 else if (is_overloaded_fn (argument))
13203 /* All overloaded functions are allowed; if the external
13204 linkage test does not pass, an error will be issued
13205 later. */
13207 else if (address_p
13208 && (TREE_CODE (argument) == OFFSET_REF
13209 || TREE_CODE (argument) == SCOPE_REF))
13210 /* A pointer-to-member. */
13212 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13214 else
13215 cp_parser_simulate_error (parser);
13217 if (cp_parser_parse_definitely (parser))
13219 if (address_p)
13220 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13221 tf_warning_or_error);
13222 return argument;
13226 /* If the argument started with "&", there are no other valid
13227 alternatives at this point. */
13228 if (address_p)
13230 cp_parser_error (parser, "invalid non-type template argument");
13231 return error_mark_node;
13234 /* If the argument wasn't successfully parsed as a type-id followed
13235 by '>>', the argument can only be a constant expression now.
13236 Otherwise, we try parsing the constant-expression tentatively,
13237 because the argument could really be a type-id. */
13238 if (maybe_type_id)
13239 cp_parser_parse_tentatively (parser);
13240 argument = cp_parser_constant_expression (parser,
13241 /*allow_non_constant_p=*/false,
13242 /*non_constant_p=*/NULL);
13243 argument = fold_non_dependent_expr (argument);
13244 if (!maybe_type_id)
13245 return argument;
13246 if (!cp_parser_next_token_ends_template_argument_p (parser))
13247 cp_parser_error (parser, "expected template-argument");
13248 if (cp_parser_parse_definitely (parser))
13249 return argument;
13250 /* We did our best to parse the argument as a non type-id, but that
13251 was the only alternative that matched (albeit with a '>' after
13252 it). We can assume it's just a typo from the user, and a
13253 diagnostic will then be issued. */
13254 return cp_parser_template_type_arg (parser);
13257 /* Parse an explicit-instantiation.
13259 explicit-instantiation:
13260 template declaration
13262 Although the standard says `declaration', what it really means is:
13264 explicit-instantiation:
13265 template decl-specifier-seq [opt] declarator [opt] ;
13267 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13268 supposed to be allowed. A defect report has been filed about this
13269 issue.
13271 GNU Extension:
13273 explicit-instantiation:
13274 storage-class-specifier template
13275 decl-specifier-seq [opt] declarator [opt] ;
13276 function-specifier template
13277 decl-specifier-seq [opt] declarator [opt] ; */
13279 static void
13280 cp_parser_explicit_instantiation (cp_parser* parser)
13282 int declares_class_or_enum;
13283 cp_decl_specifier_seq decl_specifiers;
13284 tree extension_specifier = NULL_TREE;
13286 timevar_push (TV_TEMPLATE_INST);
13288 /* Look for an (optional) storage-class-specifier or
13289 function-specifier. */
13290 if (cp_parser_allow_gnu_extensions_p (parser))
13292 extension_specifier
13293 = cp_parser_storage_class_specifier_opt (parser);
13294 if (!extension_specifier)
13295 extension_specifier
13296 = cp_parser_function_specifier_opt (parser,
13297 /*decl_specs=*/NULL);
13300 /* Look for the `template' keyword. */
13301 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13302 /* Let the front end know that we are processing an explicit
13303 instantiation. */
13304 begin_explicit_instantiation ();
13305 /* [temp.explicit] says that we are supposed to ignore access
13306 control while processing explicit instantiation directives. */
13307 push_deferring_access_checks (dk_no_check);
13308 /* Parse a decl-specifier-seq. */
13309 cp_parser_decl_specifier_seq (parser,
13310 CP_PARSER_FLAGS_OPTIONAL,
13311 &decl_specifiers,
13312 &declares_class_or_enum);
13313 /* If there was exactly one decl-specifier, and it declared a class,
13314 and there's no declarator, then we have an explicit type
13315 instantiation. */
13316 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13318 tree type;
13320 type = check_tag_decl (&decl_specifiers,
13321 /*explicit_type_instantiation_p=*/true);
13322 /* Turn access control back on for names used during
13323 template instantiation. */
13324 pop_deferring_access_checks ();
13325 if (type)
13326 do_type_instantiation (type, extension_specifier,
13327 /*complain=*/tf_error);
13329 else
13331 cp_declarator *declarator;
13332 tree decl;
13334 /* Parse the declarator. */
13335 declarator
13336 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13337 /*ctor_dtor_or_conv_p=*/NULL,
13338 /*parenthesized_p=*/NULL,
13339 /*member_p=*/false);
13340 if (declares_class_or_enum & 2)
13341 cp_parser_check_for_definition_in_return_type (declarator,
13342 decl_specifiers.type,
13343 decl_specifiers.locations[ds_type_spec]);
13344 if (declarator != cp_error_declarator)
13346 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13347 permerror (decl_specifiers.locations[ds_inline],
13348 "explicit instantiation shall not use"
13349 " %<inline%> specifier");
13350 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13351 permerror (decl_specifiers.locations[ds_constexpr],
13352 "explicit instantiation shall not use"
13353 " %<constexpr%> specifier");
13355 decl = grokdeclarator (declarator, &decl_specifiers,
13356 NORMAL, 0, &decl_specifiers.attributes);
13357 /* Turn access control back on for names used during
13358 template instantiation. */
13359 pop_deferring_access_checks ();
13360 /* Do the explicit instantiation. */
13361 do_decl_instantiation (decl, extension_specifier);
13363 else
13365 pop_deferring_access_checks ();
13366 /* Skip the body of the explicit instantiation. */
13367 cp_parser_skip_to_end_of_statement (parser);
13370 /* We're done with the instantiation. */
13371 end_explicit_instantiation ();
13373 cp_parser_consume_semicolon_at_end_of_statement (parser);
13375 timevar_pop (TV_TEMPLATE_INST);
13378 /* Parse an explicit-specialization.
13380 explicit-specialization:
13381 template < > declaration
13383 Although the standard says `declaration', what it really means is:
13385 explicit-specialization:
13386 template <> decl-specifier [opt] init-declarator [opt] ;
13387 template <> function-definition
13388 template <> explicit-specialization
13389 template <> template-declaration */
13391 static void
13392 cp_parser_explicit_specialization (cp_parser* parser)
13394 bool need_lang_pop;
13395 cp_token *token = cp_lexer_peek_token (parser->lexer);
13397 /* Look for the `template' keyword. */
13398 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13399 /* Look for the `<'. */
13400 cp_parser_require (parser, CPP_LESS, RT_LESS);
13401 /* Look for the `>'. */
13402 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13403 /* We have processed another parameter list. */
13404 ++parser->num_template_parameter_lists;
13405 /* [temp]
13407 A template ... explicit specialization ... shall not have C
13408 linkage. */
13409 if (current_lang_name == lang_name_c)
13411 error_at (token->location, "template specialization with C linkage");
13412 /* Give it C++ linkage to avoid confusing other parts of the
13413 front end. */
13414 push_lang_context (lang_name_cplusplus);
13415 need_lang_pop = true;
13417 else
13418 need_lang_pop = false;
13419 /* Let the front end know that we are beginning a specialization. */
13420 if (!begin_specialization ())
13422 end_specialization ();
13423 return;
13426 /* If the next keyword is `template', we need to figure out whether
13427 or not we're looking a template-declaration. */
13428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13430 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13431 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13432 cp_parser_template_declaration_after_export (parser,
13433 /*member_p=*/false);
13434 else
13435 cp_parser_explicit_specialization (parser);
13437 else
13438 /* Parse the dependent declaration. */
13439 cp_parser_single_declaration (parser,
13440 /*checks=*/NULL,
13441 /*member_p=*/false,
13442 /*explicit_specialization_p=*/true,
13443 /*friend_p=*/NULL);
13444 /* We're done with the specialization. */
13445 end_specialization ();
13446 /* For the erroneous case of a template with C linkage, we pushed an
13447 implicit C++ linkage scope; exit that scope now. */
13448 if (need_lang_pop)
13449 pop_lang_context ();
13450 /* We're done with this parameter list. */
13451 --parser->num_template_parameter_lists;
13454 /* Parse a type-specifier.
13456 type-specifier:
13457 simple-type-specifier
13458 class-specifier
13459 enum-specifier
13460 elaborated-type-specifier
13461 cv-qualifier
13463 GNU Extension:
13465 type-specifier:
13466 __complex__
13468 Returns a representation of the type-specifier. For a
13469 class-specifier, enum-specifier, or elaborated-type-specifier, a
13470 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13472 The parser flags FLAGS is used to control type-specifier parsing.
13474 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13475 in a decl-specifier-seq.
13477 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13478 class-specifier, enum-specifier, or elaborated-type-specifier, then
13479 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13480 if a type is declared; 2 if it is defined. Otherwise, it is set to
13481 zero.
13483 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13484 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13485 is set to FALSE. */
13487 static tree
13488 cp_parser_type_specifier (cp_parser* parser,
13489 cp_parser_flags flags,
13490 cp_decl_specifier_seq *decl_specs,
13491 bool is_declaration,
13492 int* declares_class_or_enum,
13493 bool* is_cv_qualifier)
13495 tree type_spec = NULL_TREE;
13496 cp_token *token;
13497 enum rid keyword;
13498 cp_decl_spec ds = ds_last;
13500 /* Assume this type-specifier does not declare a new type. */
13501 if (declares_class_or_enum)
13502 *declares_class_or_enum = 0;
13503 /* And that it does not specify a cv-qualifier. */
13504 if (is_cv_qualifier)
13505 *is_cv_qualifier = false;
13506 /* Peek at the next token. */
13507 token = cp_lexer_peek_token (parser->lexer);
13509 /* If we're looking at a keyword, we can use that to guide the
13510 production we choose. */
13511 keyword = token->keyword;
13512 switch (keyword)
13514 case RID_ENUM:
13515 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13516 goto elaborated_type_specifier;
13518 /* Look for the enum-specifier. */
13519 type_spec = cp_parser_enum_specifier (parser);
13520 /* If that worked, we're done. */
13521 if (type_spec)
13523 if (declares_class_or_enum)
13524 *declares_class_or_enum = 2;
13525 if (decl_specs)
13526 cp_parser_set_decl_spec_type (decl_specs,
13527 type_spec,
13528 token,
13529 /*type_definition_p=*/true);
13530 return type_spec;
13532 else
13533 goto elaborated_type_specifier;
13535 /* Any of these indicate either a class-specifier, or an
13536 elaborated-type-specifier. */
13537 case RID_CLASS:
13538 case RID_STRUCT:
13539 case RID_UNION:
13540 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13541 goto elaborated_type_specifier;
13543 /* Parse tentatively so that we can back up if we don't find a
13544 class-specifier. */
13545 cp_parser_parse_tentatively (parser);
13546 /* Look for the class-specifier. */
13547 type_spec = cp_parser_class_specifier (parser);
13548 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13549 /* If that worked, we're done. */
13550 if (cp_parser_parse_definitely (parser))
13552 if (declares_class_or_enum)
13553 *declares_class_or_enum = 2;
13554 if (decl_specs)
13555 cp_parser_set_decl_spec_type (decl_specs,
13556 type_spec,
13557 token,
13558 /*type_definition_p=*/true);
13559 return type_spec;
13562 /* Fall through. */
13563 elaborated_type_specifier:
13564 /* We're declaring (not defining) a class or enum. */
13565 if (declares_class_or_enum)
13566 *declares_class_or_enum = 1;
13568 /* Fall through. */
13569 case RID_TYPENAME:
13570 /* Look for an elaborated-type-specifier. */
13571 type_spec
13572 = (cp_parser_elaborated_type_specifier
13573 (parser,
13574 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13575 is_declaration));
13576 if (decl_specs)
13577 cp_parser_set_decl_spec_type (decl_specs,
13578 type_spec,
13579 token,
13580 /*type_definition_p=*/false);
13581 return type_spec;
13583 case RID_CONST:
13584 ds = ds_const;
13585 if (is_cv_qualifier)
13586 *is_cv_qualifier = true;
13587 break;
13589 case RID_VOLATILE:
13590 ds = ds_volatile;
13591 if (is_cv_qualifier)
13592 *is_cv_qualifier = true;
13593 break;
13595 case RID_RESTRICT:
13596 ds = ds_restrict;
13597 if (is_cv_qualifier)
13598 *is_cv_qualifier = true;
13599 break;
13601 case RID_COMPLEX:
13602 /* The `__complex__' keyword is a GNU extension. */
13603 ds = ds_complex;
13604 break;
13606 default:
13607 break;
13610 /* Handle simple keywords. */
13611 if (ds != ds_last)
13613 if (decl_specs)
13615 set_and_check_decl_spec_loc (decl_specs, ds, token);
13616 decl_specs->any_specifiers_p = true;
13618 return cp_lexer_consume_token (parser->lexer)->u.value;
13621 /* If we do not already have a type-specifier, assume we are looking
13622 at a simple-type-specifier. */
13623 type_spec = cp_parser_simple_type_specifier (parser,
13624 decl_specs,
13625 flags);
13627 /* If we didn't find a type-specifier, and a type-specifier was not
13628 optional in this context, issue an error message. */
13629 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13631 cp_parser_error (parser, "expected type specifier");
13632 return error_mark_node;
13635 return type_spec;
13638 /* Parse a simple-type-specifier.
13640 simple-type-specifier:
13641 :: [opt] nested-name-specifier [opt] type-name
13642 :: [opt] nested-name-specifier template template-id
13643 char
13644 wchar_t
13645 bool
13646 short
13648 long
13649 signed
13650 unsigned
13651 float
13652 double
13653 void
13655 C++0x Extension:
13657 simple-type-specifier:
13658 auto
13659 decltype ( expression )
13660 char16_t
13661 char32_t
13662 __underlying_type ( type-id )
13664 GNU Extension:
13666 simple-type-specifier:
13667 __int128
13668 __typeof__ unary-expression
13669 __typeof__ ( type-id )
13671 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13672 appropriately updated. */
13674 static tree
13675 cp_parser_simple_type_specifier (cp_parser* parser,
13676 cp_decl_specifier_seq *decl_specs,
13677 cp_parser_flags flags)
13679 tree type = NULL_TREE;
13680 cp_token *token;
13682 /* Peek at the next token. */
13683 token = cp_lexer_peek_token (parser->lexer);
13685 /* If we're looking at a keyword, things are easy. */
13686 switch (token->keyword)
13688 case RID_CHAR:
13689 if (decl_specs)
13690 decl_specs->explicit_char_p = true;
13691 type = char_type_node;
13692 break;
13693 case RID_CHAR16:
13694 type = char16_type_node;
13695 break;
13696 case RID_CHAR32:
13697 type = char32_type_node;
13698 break;
13699 case RID_WCHAR:
13700 type = wchar_type_node;
13701 break;
13702 case RID_BOOL:
13703 type = boolean_type_node;
13704 break;
13705 case RID_SHORT:
13706 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13707 type = short_integer_type_node;
13708 break;
13709 case RID_INT:
13710 if (decl_specs)
13711 decl_specs->explicit_int_p = true;
13712 type = integer_type_node;
13713 break;
13714 case RID_INT128:
13715 if (!int128_integer_type_node)
13716 break;
13717 if (decl_specs)
13718 decl_specs->explicit_int128_p = true;
13719 type = int128_integer_type_node;
13720 break;
13721 case RID_LONG:
13722 if (decl_specs)
13723 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13724 type = long_integer_type_node;
13725 break;
13726 case RID_SIGNED:
13727 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13728 type = integer_type_node;
13729 break;
13730 case RID_UNSIGNED:
13731 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13732 type = unsigned_type_node;
13733 break;
13734 case RID_FLOAT:
13735 type = float_type_node;
13736 break;
13737 case RID_DOUBLE:
13738 type = double_type_node;
13739 break;
13740 case RID_VOID:
13741 type = void_type_node;
13742 break;
13744 case RID_AUTO:
13745 maybe_warn_cpp0x (CPP0X_AUTO);
13746 type = make_auto ();
13747 break;
13749 case RID_DECLTYPE:
13750 /* Since DR 743, decltype can either be a simple-type-specifier by
13751 itself or begin a nested-name-specifier. Parsing it will replace
13752 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13753 handling below decide what to do. */
13754 cp_parser_decltype (parser);
13755 cp_lexer_set_token_position (parser->lexer, token);
13756 break;
13758 case RID_TYPEOF:
13759 /* Consume the `typeof' token. */
13760 cp_lexer_consume_token (parser->lexer);
13761 /* Parse the operand to `typeof'. */
13762 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13763 /* If it is not already a TYPE, take its type. */
13764 if (!TYPE_P (type))
13765 type = finish_typeof (type);
13767 if (decl_specs)
13768 cp_parser_set_decl_spec_type (decl_specs, type,
13769 token,
13770 /*type_definition_p=*/false);
13772 return type;
13774 case RID_UNDERLYING_TYPE:
13775 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13776 if (decl_specs)
13777 cp_parser_set_decl_spec_type (decl_specs, type,
13778 token,
13779 /*type_definition_p=*/false);
13781 return type;
13783 case RID_BASES:
13784 case RID_DIRECT_BASES:
13785 type = cp_parser_trait_expr (parser, token->keyword);
13786 if (decl_specs)
13787 cp_parser_set_decl_spec_type (decl_specs, type,
13788 token,
13789 /*type_definition_p=*/false);
13790 return type;
13791 default:
13792 break;
13795 /* If token is an already-parsed decltype not followed by ::,
13796 it's a simple-type-specifier. */
13797 if (token->type == CPP_DECLTYPE
13798 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13800 type = token->u.value;
13801 if (decl_specs)
13802 cp_parser_set_decl_spec_type (decl_specs, type,
13803 token,
13804 /*type_definition_p=*/false);
13805 cp_lexer_consume_token (parser->lexer);
13806 return type;
13809 /* If the type-specifier was for a built-in type, we're done. */
13810 if (type)
13812 /* Record the type. */
13813 if (decl_specs
13814 && (token->keyword != RID_SIGNED
13815 && token->keyword != RID_UNSIGNED
13816 && token->keyword != RID_SHORT
13817 && token->keyword != RID_LONG))
13818 cp_parser_set_decl_spec_type (decl_specs,
13819 type,
13820 token,
13821 /*type_definition_p=*/false);
13822 if (decl_specs)
13823 decl_specs->any_specifiers_p = true;
13825 /* Consume the token. */
13826 cp_lexer_consume_token (parser->lexer);
13828 /* There is no valid C++ program where a non-template type is
13829 followed by a "<". That usually indicates that the user thought
13830 that the type was a template. */
13831 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13832 token->location);
13834 return TYPE_NAME (type);
13837 /* The type-specifier must be a user-defined type. */
13838 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13840 bool qualified_p;
13841 bool global_p;
13843 /* Don't gobble tokens or issue error messages if this is an
13844 optional type-specifier. */
13845 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13846 cp_parser_parse_tentatively (parser);
13848 /* Look for the optional `::' operator. */
13849 global_p
13850 = (cp_parser_global_scope_opt (parser,
13851 /*current_scope_valid_p=*/false)
13852 != NULL_TREE);
13853 /* Look for the nested-name specifier. */
13854 qualified_p
13855 = (cp_parser_nested_name_specifier_opt (parser,
13856 /*typename_keyword_p=*/false,
13857 /*check_dependency_p=*/true,
13858 /*type_p=*/false,
13859 /*is_declaration=*/false)
13860 != NULL_TREE);
13861 token = cp_lexer_peek_token (parser->lexer);
13862 /* If we have seen a nested-name-specifier, and the next token
13863 is `template', then we are using the template-id production. */
13864 if (parser->scope
13865 && cp_parser_optional_template_keyword (parser))
13867 /* Look for the template-id. */
13868 type = cp_parser_template_id (parser,
13869 /*template_keyword_p=*/true,
13870 /*check_dependency_p=*/true,
13871 none_type,
13872 /*is_declaration=*/false);
13873 /* If the template-id did not name a type, we are out of
13874 luck. */
13875 if (TREE_CODE (type) != TYPE_DECL)
13877 cp_parser_error (parser, "expected template-id for type");
13878 type = NULL_TREE;
13881 /* Otherwise, look for a type-name. */
13882 else
13883 type = cp_parser_type_name (parser);
13884 /* Keep track of all name-lookups performed in class scopes. */
13885 if (type
13886 && !global_p
13887 && !qualified_p
13888 && TREE_CODE (type) == TYPE_DECL
13889 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13890 maybe_note_name_used_in_class (DECL_NAME (type), type);
13891 /* If it didn't work out, we don't have a TYPE. */
13892 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13893 && !cp_parser_parse_definitely (parser))
13894 type = NULL_TREE;
13895 if (type && decl_specs)
13896 cp_parser_set_decl_spec_type (decl_specs, type,
13897 token,
13898 /*type_definition_p=*/false);
13901 /* If we didn't get a type-name, issue an error message. */
13902 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13904 cp_parser_error (parser, "expected type-name");
13905 return error_mark_node;
13908 if (type && type != error_mark_node)
13910 /* See if TYPE is an Objective-C type, and if so, parse and
13911 accept any protocol references following it. Do this before
13912 the cp_parser_check_for_invalid_template_id() call, because
13913 Objective-C types can be followed by '<...>' which would
13914 enclose protocol names rather than template arguments, and so
13915 everything is fine. */
13916 if (c_dialect_objc () && !parser->scope
13917 && (objc_is_id (type) || objc_is_class_name (type)))
13919 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13920 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13922 /* Clobber the "unqualified" type previously entered into
13923 DECL_SPECS with the new, improved protocol-qualified version. */
13924 if (decl_specs)
13925 decl_specs->type = qual_type;
13927 return qual_type;
13930 /* There is no valid C++ program where a non-template type is
13931 followed by a "<". That usually indicates that the user
13932 thought that the type was a template. */
13933 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13934 none_type,
13935 token->location);
13938 return type;
13941 /* Parse a type-name.
13943 type-name:
13944 class-name
13945 enum-name
13946 typedef-name
13947 simple-template-id [in c++0x]
13949 enum-name:
13950 identifier
13952 typedef-name:
13953 identifier
13955 Returns a TYPE_DECL for the type. */
13957 static tree
13958 cp_parser_type_name (cp_parser* parser)
13960 tree type_decl;
13962 /* We can't know yet whether it is a class-name or not. */
13963 cp_parser_parse_tentatively (parser);
13964 /* Try a class-name. */
13965 type_decl = cp_parser_class_name (parser,
13966 /*typename_keyword_p=*/false,
13967 /*template_keyword_p=*/false,
13968 none_type,
13969 /*check_dependency_p=*/true,
13970 /*class_head_p=*/false,
13971 /*is_declaration=*/false);
13972 /* If it's not a class-name, keep looking. */
13973 if (!cp_parser_parse_definitely (parser))
13975 if (cxx_dialect < cxx0x)
13976 /* It must be a typedef-name or an enum-name. */
13977 return cp_parser_nonclass_name (parser);
13979 cp_parser_parse_tentatively (parser);
13980 /* It is either a simple-template-id representing an
13981 instantiation of an alias template... */
13982 type_decl = cp_parser_template_id (parser,
13983 /*template_keyword_p=*/false,
13984 /*check_dependency_p=*/false,
13985 none_type,
13986 /*is_declaration=*/false);
13987 /* Note that this must be an instantiation of an alias template
13988 because [temp.names]/6 says:
13990 A template-id that names an alias template specialization
13991 is a type-name.
13993 Whereas [temp.names]/7 says:
13995 A simple-template-id that names a class template
13996 specialization is a class-name. */
13997 if (type_decl != NULL_TREE
13998 && TREE_CODE (type_decl) == TYPE_DECL
13999 && TYPE_DECL_ALIAS_P (type_decl))
14000 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14001 else
14002 cp_parser_simulate_error (parser);
14004 if (!cp_parser_parse_definitely (parser))
14005 /* ... Or a typedef-name or an enum-name. */
14006 return cp_parser_nonclass_name (parser);
14009 return type_decl;
14012 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14014 enum-name:
14015 identifier
14017 typedef-name:
14018 identifier
14020 Returns a TYPE_DECL for the type. */
14022 static tree
14023 cp_parser_nonclass_name (cp_parser* parser)
14025 tree type_decl;
14026 tree identifier;
14028 cp_token *token = cp_lexer_peek_token (parser->lexer);
14029 identifier = cp_parser_identifier (parser);
14030 if (identifier == error_mark_node)
14031 return error_mark_node;
14033 /* Look up the type-name. */
14034 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14036 if (TREE_CODE (type_decl) == USING_DECL)
14038 if (!DECL_DEPENDENT_P (type_decl))
14039 type_decl = strip_using_decl (type_decl);
14040 else if (USING_DECL_TYPENAME_P (type_decl))
14042 /* We have found a type introduced by a using
14043 declaration at class scope that refers to a dependent
14044 type.
14046 using typename :: [opt] nested-name-specifier unqualified-id ;
14048 type_decl = make_typename_type (TREE_TYPE (type_decl),
14049 DECL_NAME (type_decl),
14050 typename_type, tf_error);
14051 if (type_decl != error_mark_node)
14052 type_decl = TYPE_NAME (type_decl);
14056 if (TREE_CODE (type_decl) != TYPE_DECL
14057 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14059 /* See if this is an Objective-C type. */
14060 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14061 tree type = objc_get_protocol_qualified_type (identifier, protos);
14062 if (type)
14063 type_decl = TYPE_NAME (type);
14066 /* Issue an error if we did not find a type-name. */
14067 if (TREE_CODE (type_decl) != TYPE_DECL
14068 /* In Objective-C, we have the complication that class names are
14069 normally type names and start declarations (eg, the
14070 "NSObject" in "NSObject *object;"), but can be used in an
14071 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14072 is an expression. So, a classname followed by a dot is not a
14073 valid type-name. */
14074 || (objc_is_class_name (TREE_TYPE (type_decl))
14075 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14077 if (!cp_parser_simulate_error (parser))
14078 cp_parser_name_lookup_error (parser, identifier, type_decl,
14079 NLE_TYPE, token->location);
14080 return error_mark_node;
14082 /* Remember that the name was used in the definition of the
14083 current class so that we can check later to see if the
14084 meaning would have been different after the class was
14085 entirely defined. */
14086 else if (type_decl != error_mark_node
14087 && !parser->scope)
14088 maybe_note_name_used_in_class (identifier, type_decl);
14090 return type_decl;
14093 /* Parse an elaborated-type-specifier. Note that the grammar given
14094 here incorporates the resolution to DR68.
14096 elaborated-type-specifier:
14097 class-key :: [opt] nested-name-specifier [opt] identifier
14098 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14099 enum-key :: [opt] nested-name-specifier [opt] identifier
14100 typename :: [opt] nested-name-specifier identifier
14101 typename :: [opt] nested-name-specifier template [opt]
14102 template-id
14104 GNU extension:
14106 elaborated-type-specifier:
14107 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14108 class-key attributes :: [opt] nested-name-specifier [opt]
14109 template [opt] template-id
14110 enum attributes :: [opt] nested-name-specifier [opt] identifier
14112 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14113 declared `friend'. If IS_DECLARATION is TRUE, then this
14114 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14115 something is being declared.
14117 Returns the TYPE specified. */
14119 static tree
14120 cp_parser_elaborated_type_specifier (cp_parser* parser,
14121 bool is_friend,
14122 bool is_declaration)
14124 enum tag_types tag_type;
14125 tree identifier;
14126 tree type = NULL_TREE;
14127 tree attributes = NULL_TREE;
14128 tree globalscope;
14129 cp_token *token = NULL;
14131 /* See if we're looking at the `enum' keyword. */
14132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14134 /* Consume the `enum' token. */
14135 cp_lexer_consume_token (parser->lexer);
14136 /* Remember that it's an enumeration type. */
14137 tag_type = enum_type;
14138 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14139 enums) is used here. */
14140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14141 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14143 pedwarn (input_location, 0, "elaborated-type-specifier "
14144 "for a scoped enum must not use the %<%D%> keyword",
14145 cp_lexer_peek_token (parser->lexer)->u.value);
14146 /* Consume the `struct' or `class' and parse it anyway. */
14147 cp_lexer_consume_token (parser->lexer);
14149 /* Parse the attributes. */
14150 attributes = cp_parser_attributes_opt (parser);
14152 /* Or, it might be `typename'. */
14153 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14154 RID_TYPENAME))
14156 /* Consume the `typename' token. */
14157 cp_lexer_consume_token (parser->lexer);
14158 /* Remember that it's a `typename' type. */
14159 tag_type = typename_type;
14161 /* Otherwise it must be a class-key. */
14162 else
14164 tag_type = cp_parser_class_key (parser);
14165 if (tag_type == none_type)
14166 return error_mark_node;
14167 /* Parse the attributes. */
14168 attributes = cp_parser_attributes_opt (parser);
14171 /* Look for the `::' operator. */
14172 globalscope = cp_parser_global_scope_opt (parser,
14173 /*current_scope_valid_p=*/false);
14174 /* Look for the nested-name-specifier. */
14175 if (tag_type == typename_type && !globalscope)
14177 if (!cp_parser_nested_name_specifier (parser,
14178 /*typename_keyword_p=*/true,
14179 /*check_dependency_p=*/true,
14180 /*type_p=*/true,
14181 is_declaration))
14182 return error_mark_node;
14184 else
14185 /* Even though `typename' is not present, the proposed resolution
14186 to Core Issue 180 says that in `class A<T>::B', `B' should be
14187 considered a type-name, even if `A<T>' is dependent. */
14188 cp_parser_nested_name_specifier_opt (parser,
14189 /*typename_keyword_p=*/true,
14190 /*check_dependency_p=*/true,
14191 /*type_p=*/true,
14192 is_declaration);
14193 /* For everything but enumeration types, consider a template-id.
14194 For an enumeration type, consider only a plain identifier. */
14195 if (tag_type != enum_type)
14197 bool template_p = false;
14198 tree decl;
14200 /* Allow the `template' keyword. */
14201 template_p = cp_parser_optional_template_keyword (parser);
14202 /* If we didn't see `template', we don't know if there's a
14203 template-id or not. */
14204 if (!template_p)
14205 cp_parser_parse_tentatively (parser);
14206 /* Parse the template-id. */
14207 token = cp_lexer_peek_token (parser->lexer);
14208 decl = cp_parser_template_id (parser, template_p,
14209 /*check_dependency_p=*/true,
14210 tag_type,
14211 is_declaration);
14212 /* If we didn't find a template-id, look for an ordinary
14213 identifier. */
14214 if (!template_p && !cp_parser_parse_definitely (parser))
14216 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14217 in effect, then we must assume that, upon instantiation, the
14218 template will correspond to a class. */
14219 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14220 && tag_type == typename_type)
14221 type = make_typename_type (parser->scope, decl,
14222 typename_type,
14223 /*complain=*/tf_error);
14224 /* If the `typename' keyword is in effect and DECL is not a type
14225 decl. Then type is non existant. */
14226 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14227 type = NULL_TREE;
14228 else
14229 type = check_elaborated_type_specifier (tag_type, decl,
14230 /*allow_template_p=*/true);
14233 if (!type)
14235 token = cp_lexer_peek_token (parser->lexer);
14236 identifier = cp_parser_identifier (parser);
14238 if (identifier == error_mark_node)
14240 parser->scope = NULL_TREE;
14241 return error_mark_node;
14244 /* For a `typename', we needn't call xref_tag. */
14245 if (tag_type == typename_type
14246 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14247 return cp_parser_make_typename_type (parser, parser->scope,
14248 identifier,
14249 token->location);
14250 /* Look up a qualified name in the usual way. */
14251 if (parser->scope)
14253 tree decl;
14254 tree ambiguous_decls;
14256 decl = cp_parser_lookup_name (parser, identifier,
14257 tag_type,
14258 /*is_template=*/false,
14259 /*is_namespace=*/false,
14260 /*check_dependency=*/true,
14261 &ambiguous_decls,
14262 token->location);
14264 /* If the lookup was ambiguous, an error will already have been
14265 issued. */
14266 if (ambiguous_decls)
14267 return error_mark_node;
14269 /* If we are parsing friend declaration, DECL may be a
14270 TEMPLATE_DECL tree node here. However, we need to check
14271 whether this TEMPLATE_DECL results in valid code. Consider
14272 the following example:
14274 namespace N {
14275 template <class T> class C {};
14277 class X {
14278 template <class T> friend class N::C; // #1, valid code
14280 template <class T> class Y {
14281 friend class N::C; // #2, invalid code
14284 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14285 name lookup of `N::C'. We see that friend declaration must
14286 be template for the code to be valid. Note that
14287 processing_template_decl does not work here since it is
14288 always 1 for the above two cases. */
14290 decl = (cp_parser_maybe_treat_template_as_class
14291 (decl, /*tag_name_p=*/is_friend
14292 && parser->num_template_parameter_lists));
14294 if (TREE_CODE (decl) != TYPE_DECL)
14296 cp_parser_diagnose_invalid_type_name (parser,
14297 parser->scope,
14298 identifier,
14299 token->location);
14300 return error_mark_node;
14303 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14305 bool allow_template = (parser->num_template_parameter_lists
14306 || DECL_SELF_REFERENCE_P (decl));
14307 type = check_elaborated_type_specifier (tag_type, decl,
14308 allow_template);
14310 if (type == error_mark_node)
14311 return error_mark_node;
14314 /* Forward declarations of nested types, such as
14316 class C1::C2;
14317 class C1::C2::C3;
14319 are invalid unless all components preceding the final '::'
14320 are complete. If all enclosing types are complete, these
14321 declarations become merely pointless.
14323 Invalid forward declarations of nested types are errors
14324 caught elsewhere in parsing. Those that are pointless arrive
14325 here. */
14327 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14328 && !is_friend && !processing_explicit_instantiation)
14329 warning (0, "declaration %qD does not declare anything", decl);
14331 type = TREE_TYPE (decl);
14333 else
14335 /* An elaborated-type-specifier sometimes introduces a new type and
14336 sometimes names an existing type. Normally, the rule is that it
14337 introduces a new type only if there is not an existing type of
14338 the same name already in scope. For example, given:
14340 struct S {};
14341 void f() { struct S s; }
14343 the `struct S' in the body of `f' is the same `struct S' as in
14344 the global scope; the existing definition is used. However, if
14345 there were no global declaration, this would introduce a new
14346 local class named `S'.
14348 An exception to this rule applies to the following code:
14350 namespace N { struct S; }
14352 Here, the elaborated-type-specifier names a new type
14353 unconditionally; even if there is already an `S' in the
14354 containing scope this declaration names a new type.
14355 This exception only applies if the elaborated-type-specifier
14356 forms the complete declaration:
14358 [class.name]
14360 A declaration consisting solely of `class-key identifier ;' is
14361 either a redeclaration of the name in the current scope or a
14362 forward declaration of the identifier as a class name. It
14363 introduces the name into the current scope.
14365 We are in this situation precisely when the next token is a `;'.
14367 An exception to the exception is that a `friend' declaration does
14368 *not* name a new type; i.e., given:
14370 struct S { friend struct T; };
14372 `T' is not a new type in the scope of `S'.
14374 Also, `new struct S' or `sizeof (struct S)' never results in the
14375 definition of a new type; a new type can only be declared in a
14376 declaration context. */
14378 tag_scope ts;
14379 bool template_p;
14381 if (is_friend)
14382 /* Friends have special name lookup rules. */
14383 ts = ts_within_enclosing_non_class;
14384 else if (is_declaration
14385 && cp_lexer_next_token_is (parser->lexer,
14386 CPP_SEMICOLON))
14387 /* This is a `class-key identifier ;' */
14388 ts = ts_current;
14389 else
14390 ts = ts_global;
14392 template_p =
14393 (parser->num_template_parameter_lists
14394 && (cp_parser_next_token_starts_class_definition_p (parser)
14395 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14396 /* An unqualified name was used to reference this type, so
14397 there were no qualifying templates. */
14398 if (!cp_parser_check_template_parameters (parser,
14399 /*num_templates=*/0,
14400 token->location,
14401 /*declarator=*/NULL))
14402 return error_mark_node;
14403 type = xref_tag (tag_type, identifier, ts, template_p);
14407 if (type == error_mark_node)
14408 return error_mark_node;
14410 /* Allow attributes on forward declarations of classes. */
14411 if (attributes)
14413 if (TREE_CODE (type) == TYPENAME_TYPE)
14414 warning (OPT_Wattributes,
14415 "attributes ignored on uninstantiated type");
14416 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14417 && ! processing_explicit_instantiation)
14418 warning (OPT_Wattributes,
14419 "attributes ignored on template instantiation");
14420 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14421 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14422 else
14423 warning (OPT_Wattributes,
14424 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14427 if (tag_type != enum_type)
14429 /* Indicate whether this class was declared as a `class' or as a
14430 `struct'. */
14431 if (TREE_CODE (type) == RECORD_TYPE)
14432 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14433 cp_parser_check_class_key (tag_type, type);
14436 /* A "<" cannot follow an elaborated type specifier. If that
14437 happens, the user was probably trying to form a template-id. */
14438 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14439 token->location);
14441 return type;
14444 /* Parse an enum-specifier.
14446 enum-specifier:
14447 enum-head { enumerator-list [opt] }
14448 enum-head { enumerator-list , } [C++0x]
14450 enum-head:
14451 enum-key identifier [opt] enum-base [opt]
14452 enum-key nested-name-specifier identifier enum-base [opt]
14454 enum-key:
14455 enum
14456 enum class [C++0x]
14457 enum struct [C++0x]
14459 enum-base: [C++0x]
14460 : type-specifier-seq
14462 opaque-enum-specifier:
14463 enum-key identifier enum-base [opt] ;
14465 GNU Extensions:
14466 enum-key attributes[opt] identifier [opt] enum-base [opt]
14467 { enumerator-list [opt] }attributes[opt]
14468 enum-key attributes[opt] identifier [opt] enum-base [opt]
14469 { enumerator-list, }attributes[opt] [C++0x]
14471 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14472 if the token stream isn't an enum-specifier after all. */
14474 static tree
14475 cp_parser_enum_specifier (cp_parser* parser)
14477 tree identifier;
14478 tree type = NULL_TREE;
14479 tree prev_scope;
14480 tree nested_name_specifier = NULL_TREE;
14481 tree attributes;
14482 bool scoped_enum_p = false;
14483 bool has_underlying_type = false;
14484 bool nested_being_defined = false;
14485 bool new_value_list = false;
14486 bool is_new_type = false;
14487 bool is_anonymous = false;
14488 tree underlying_type = NULL_TREE;
14489 cp_token *type_start_token = NULL;
14490 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14492 parser->colon_corrects_to_scope_p = false;
14494 /* Parse tentatively so that we can back up if we don't find a
14495 enum-specifier. */
14496 cp_parser_parse_tentatively (parser);
14498 /* Caller guarantees that the current token is 'enum', an identifier
14499 possibly follows, and the token after that is an opening brace.
14500 If we don't have an identifier, fabricate an anonymous name for
14501 the enumeration being defined. */
14502 cp_lexer_consume_token (parser->lexer);
14504 /* Parse the "class" or "struct", which indicates a scoped
14505 enumeration type in C++0x. */
14506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14507 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14509 if (cxx_dialect < cxx0x)
14510 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14512 /* Consume the `struct' or `class' token. */
14513 cp_lexer_consume_token (parser->lexer);
14515 scoped_enum_p = true;
14518 attributes = cp_parser_attributes_opt (parser);
14520 /* Clear the qualification. */
14521 parser->scope = NULL_TREE;
14522 parser->qualifying_scope = NULL_TREE;
14523 parser->object_scope = NULL_TREE;
14525 /* Figure out in what scope the declaration is being placed. */
14526 prev_scope = current_scope ();
14528 type_start_token = cp_lexer_peek_token (parser->lexer);
14530 push_deferring_access_checks (dk_no_check);
14531 nested_name_specifier
14532 = cp_parser_nested_name_specifier_opt (parser,
14533 /*typename_keyword_p=*/true,
14534 /*check_dependency_p=*/false,
14535 /*type_p=*/false,
14536 /*is_declaration=*/false);
14538 if (nested_name_specifier)
14540 tree name;
14542 identifier = cp_parser_identifier (parser);
14543 name = cp_parser_lookup_name (parser, identifier,
14544 enum_type,
14545 /*is_template=*/false,
14546 /*is_namespace=*/false,
14547 /*check_dependency=*/true,
14548 /*ambiguous_decls=*/NULL,
14549 input_location);
14550 if (name)
14552 type = TREE_TYPE (name);
14553 if (TREE_CODE (type) == TYPENAME_TYPE)
14555 /* Are template enums allowed in ISO? */
14556 if (template_parm_scope_p ())
14557 pedwarn (type_start_token->location, OPT_Wpedantic,
14558 "%qD is an enumeration template", name);
14559 /* ignore a typename reference, for it will be solved by name
14560 in start_enum. */
14561 type = NULL_TREE;
14564 else
14565 error_at (type_start_token->location,
14566 "%qD is not an enumerator-name", identifier);
14568 else
14570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14571 identifier = cp_parser_identifier (parser);
14572 else
14574 identifier = make_anon_name ();
14575 is_anonymous = true;
14578 pop_deferring_access_checks ();
14580 /* Check for the `:' that denotes a specified underlying type in C++0x.
14581 Note that a ':' could also indicate a bitfield width, however. */
14582 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14584 cp_decl_specifier_seq type_specifiers;
14586 /* Consume the `:'. */
14587 cp_lexer_consume_token (parser->lexer);
14589 /* Parse the type-specifier-seq. */
14590 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14591 /*is_trailing_return=*/false,
14592 &type_specifiers);
14594 /* At this point this is surely not elaborated type specifier. */
14595 if (!cp_parser_parse_definitely (parser))
14596 return NULL_TREE;
14598 if (cxx_dialect < cxx0x)
14599 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14601 has_underlying_type = true;
14603 /* If that didn't work, stop. */
14604 if (type_specifiers.type != error_mark_node)
14606 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14607 /*initialized=*/0, NULL);
14608 if (underlying_type == error_mark_node)
14609 underlying_type = NULL_TREE;
14613 /* Look for the `{' but don't consume it yet. */
14614 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14616 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14618 cp_parser_error (parser, "expected %<{%>");
14619 if (has_underlying_type)
14621 type = NULL_TREE;
14622 goto out;
14625 /* An opaque-enum-specifier must have a ';' here. */
14626 if ((scoped_enum_p || underlying_type)
14627 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14629 cp_parser_error (parser, "expected %<;%> or %<{%>");
14630 if (has_underlying_type)
14632 type = NULL_TREE;
14633 goto out;
14638 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14639 return NULL_TREE;
14641 if (nested_name_specifier)
14643 if (CLASS_TYPE_P (nested_name_specifier))
14645 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14646 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14647 push_scope (nested_name_specifier);
14649 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14651 push_nested_namespace (nested_name_specifier);
14655 /* Issue an error message if type-definitions are forbidden here. */
14656 if (!cp_parser_check_type_definition (parser))
14657 type = error_mark_node;
14658 else
14659 /* Create the new type. We do this before consuming the opening
14660 brace so the enum will be recorded as being on the line of its
14661 tag (or the 'enum' keyword, if there is no tag). */
14662 type = start_enum (identifier, type, underlying_type,
14663 scoped_enum_p, &is_new_type);
14665 /* If the next token is not '{' it is an opaque-enum-specifier or an
14666 elaborated-type-specifier. */
14667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14669 timevar_push (TV_PARSE_ENUM);
14670 if (nested_name_specifier)
14672 /* The following catches invalid code such as:
14673 enum class S<int>::E { A, B, C }; */
14674 if (!processing_specialization
14675 && CLASS_TYPE_P (nested_name_specifier)
14676 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14677 error_at (type_start_token->location, "cannot add an enumerator "
14678 "list to a template instantiation");
14680 /* If that scope does not contain the scope in which the
14681 class was originally declared, the program is invalid. */
14682 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14684 if (at_namespace_scope_p ())
14685 error_at (type_start_token->location,
14686 "declaration of %qD in namespace %qD which does not "
14687 "enclose %qD",
14688 type, prev_scope, nested_name_specifier);
14689 else
14690 error_at (type_start_token->location,
14691 "declaration of %qD in %qD which does not enclose %qD",
14692 type, prev_scope, nested_name_specifier);
14693 type = error_mark_node;
14697 if (scoped_enum_p)
14698 begin_scope (sk_scoped_enum, type);
14700 /* Consume the opening brace. */
14701 cp_lexer_consume_token (parser->lexer);
14703 if (type == error_mark_node)
14704 ; /* Nothing to add */
14705 else if (OPAQUE_ENUM_P (type)
14706 || (cxx_dialect > cxx98 && processing_specialization))
14708 new_value_list = true;
14709 SET_OPAQUE_ENUM_P (type, false);
14710 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14712 else
14714 error_at (type_start_token->location, "multiple definition of %q#T", type);
14715 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14716 "previous definition here");
14717 type = error_mark_node;
14720 if (type == error_mark_node)
14721 cp_parser_skip_to_end_of_block_or_statement (parser);
14722 /* If the next token is not '}', then there are some enumerators. */
14723 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14724 cp_parser_enumerator_list (parser, type);
14726 /* Consume the final '}'. */
14727 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14729 if (scoped_enum_p)
14730 finish_scope ();
14731 timevar_pop (TV_PARSE_ENUM);
14733 else
14735 /* If a ';' follows, then it is an opaque-enum-specifier
14736 and additional restrictions apply. */
14737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14739 if (is_anonymous)
14740 error_at (type_start_token->location,
14741 "opaque-enum-specifier without name");
14742 else if (nested_name_specifier)
14743 error_at (type_start_token->location,
14744 "opaque-enum-specifier must use a simple identifier");
14748 /* Look for trailing attributes to apply to this enumeration, and
14749 apply them if appropriate. */
14750 if (cp_parser_allow_gnu_extensions_p (parser))
14752 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14753 trailing_attr = chainon (trailing_attr, attributes);
14754 cplus_decl_attributes (&type,
14755 trailing_attr,
14756 (int) ATTR_FLAG_TYPE_IN_PLACE);
14759 /* Finish up the enumeration. */
14760 if (type != error_mark_node)
14762 if (new_value_list)
14763 finish_enum_value_list (type);
14764 if (is_new_type)
14765 finish_enum (type);
14768 if (nested_name_specifier)
14770 if (CLASS_TYPE_P (nested_name_specifier))
14772 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14773 pop_scope (nested_name_specifier);
14775 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14777 pop_nested_namespace (nested_name_specifier);
14780 out:
14781 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14782 return type;
14785 /* Parse an enumerator-list. The enumerators all have the indicated
14786 TYPE.
14788 enumerator-list:
14789 enumerator-definition
14790 enumerator-list , enumerator-definition */
14792 static void
14793 cp_parser_enumerator_list (cp_parser* parser, tree type)
14795 while (true)
14797 /* Parse an enumerator-definition. */
14798 cp_parser_enumerator_definition (parser, type);
14800 /* If the next token is not a ',', we've reached the end of
14801 the list. */
14802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14803 break;
14804 /* Otherwise, consume the `,' and keep going. */
14805 cp_lexer_consume_token (parser->lexer);
14806 /* If the next token is a `}', there is a trailing comma. */
14807 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14809 if (cxx_dialect < cxx0x && !in_system_header)
14810 pedwarn (input_location, OPT_Wpedantic,
14811 "comma at end of enumerator list");
14812 break;
14817 /* Parse an enumerator-definition. The enumerator has the indicated
14818 TYPE.
14820 enumerator-definition:
14821 enumerator
14822 enumerator = constant-expression
14824 enumerator:
14825 identifier */
14827 static void
14828 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14830 tree identifier;
14831 tree value;
14832 location_t loc;
14834 /* Save the input location because we are interested in the location
14835 of the identifier and not the location of the explicit value. */
14836 loc = cp_lexer_peek_token (parser->lexer)->location;
14838 /* Look for the identifier. */
14839 identifier = cp_parser_identifier (parser);
14840 if (identifier == error_mark_node)
14841 return;
14843 /* If the next token is an '=', then there is an explicit value. */
14844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14846 /* Consume the `=' token. */
14847 cp_lexer_consume_token (parser->lexer);
14848 /* Parse the value. */
14849 value = cp_parser_constant_expression (parser,
14850 /*allow_non_constant_p=*/false,
14851 NULL);
14853 else
14854 value = NULL_TREE;
14856 /* If we are processing a template, make sure the initializer of the
14857 enumerator doesn't contain any bare template parameter pack. */
14858 if (check_for_bare_parameter_packs (value))
14859 value = error_mark_node;
14861 /* integral_constant_value will pull out this expression, so make sure
14862 it's folded as appropriate. */
14863 value = fold_non_dependent_expr (value);
14865 /* Create the enumerator. */
14866 build_enumerator (identifier, value, type, loc);
14869 /* Parse a namespace-name.
14871 namespace-name:
14872 original-namespace-name
14873 namespace-alias
14875 Returns the NAMESPACE_DECL for the namespace. */
14877 static tree
14878 cp_parser_namespace_name (cp_parser* parser)
14880 tree identifier;
14881 tree namespace_decl;
14883 cp_token *token = cp_lexer_peek_token (parser->lexer);
14885 /* Get the name of the namespace. */
14886 identifier = cp_parser_identifier (parser);
14887 if (identifier == error_mark_node)
14888 return error_mark_node;
14890 /* Look up the identifier in the currently active scope. Look only
14891 for namespaces, due to:
14893 [basic.lookup.udir]
14895 When looking up a namespace-name in a using-directive or alias
14896 definition, only namespace names are considered.
14898 And:
14900 [basic.lookup.qual]
14902 During the lookup of a name preceding the :: scope resolution
14903 operator, object, function, and enumerator names are ignored.
14905 (Note that cp_parser_qualifying_entity only calls this
14906 function if the token after the name is the scope resolution
14907 operator.) */
14908 namespace_decl = cp_parser_lookup_name (parser, identifier,
14909 none_type,
14910 /*is_template=*/false,
14911 /*is_namespace=*/true,
14912 /*check_dependency=*/true,
14913 /*ambiguous_decls=*/NULL,
14914 token->location);
14915 /* If it's not a namespace, issue an error. */
14916 if (namespace_decl == error_mark_node
14917 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14919 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14920 error_at (token->location, "%qD is not a namespace-name", identifier);
14921 cp_parser_error (parser, "expected namespace-name");
14922 namespace_decl = error_mark_node;
14925 return namespace_decl;
14928 /* Parse a namespace-definition.
14930 namespace-definition:
14931 named-namespace-definition
14932 unnamed-namespace-definition
14934 named-namespace-definition:
14935 original-namespace-definition
14936 extension-namespace-definition
14938 original-namespace-definition:
14939 namespace identifier { namespace-body }
14941 extension-namespace-definition:
14942 namespace original-namespace-name { namespace-body }
14944 unnamed-namespace-definition:
14945 namespace { namespace-body } */
14947 static void
14948 cp_parser_namespace_definition (cp_parser* parser)
14950 tree identifier, attribs;
14951 bool has_visibility;
14952 bool is_inline;
14954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14956 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14957 is_inline = true;
14958 cp_lexer_consume_token (parser->lexer);
14960 else
14961 is_inline = false;
14963 /* Look for the `namespace' keyword. */
14964 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14966 /* Get the name of the namespace. We do not attempt to distinguish
14967 between an original-namespace-definition and an
14968 extension-namespace-definition at this point. The semantic
14969 analysis routines are responsible for that. */
14970 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14971 identifier = cp_parser_identifier (parser);
14972 else
14973 identifier = NULL_TREE;
14975 /* Parse any specified attributes. */
14976 attribs = cp_parser_attributes_opt (parser);
14978 /* Look for the `{' to start the namespace. */
14979 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14980 /* Start the namespace. */
14981 push_namespace (identifier);
14983 /* "inline namespace" is equivalent to a stub namespace definition
14984 followed by a strong using directive. */
14985 if (is_inline)
14987 tree name_space = current_namespace;
14988 /* Set up namespace association. */
14989 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14990 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14991 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14992 /* Import the contents of the inline namespace. */
14993 pop_namespace ();
14994 do_using_directive (name_space);
14995 push_namespace (identifier);
14998 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15000 /* Parse the body of the namespace. */
15001 cp_parser_namespace_body (parser);
15003 if (has_visibility)
15004 pop_visibility (1);
15006 /* Finish the namespace. */
15007 pop_namespace ();
15008 /* Look for the final `}'. */
15009 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15012 /* Parse a namespace-body.
15014 namespace-body:
15015 declaration-seq [opt] */
15017 static void
15018 cp_parser_namespace_body (cp_parser* parser)
15020 cp_parser_declaration_seq_opt (parser);
15023 /* Parse a namespace-alias-definition.
15025 namespace-alias-definition:
15026 namespace identifier = qualified-namespace-specifier ; */
15028 static void
15029 cp_parser_namespace_alias_definition (cp_parser* parser)
15031 tree identifier;
15032 tree namespace_specifier;
15034 cp_token *token = cp_lexer_peek_token (parser->lexer);
15036 /* Look for the `namespace' keyword. */
15037 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15038 /* Look for the identifier. */
15039 identifier = cp_parser_identifier (parser);
15040 if (identifier == error_mark_node)
15041 return;
15042 /* Look for the `=' token. */
15043 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15044 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15046 error_at (token->location, "%<namespace%> definition is not allowed here");
15047 /* Skip the definition. */
15048 cp_lexer_consume_token (parser->lexer);
15049 if (cp_parser_skip_to_closing_brace (parser))
15050 cp_lexer_consume_token (parser->lexer);
15051 return;
15053 cp_parser_require (parser, CPP_EQ, RT_EQ);
15054 /* Look for the qualified-namespace-specifier. */
15055 namespace_specifier
15056 = cp_parser_qualified_namespace_specifier (parser);
15057 /* Look for the `;' token. */
15058 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15060 /* Register the alias in the symbol table. */
15061 do_namespace_alias (identifier, namespace_specifier);
15064 /* Parse a qualified-namespace-specifier.
15066 qualified-namespace-specifier:
15067 :: [opt] nested-name-specifier [opt] namespace-name
15069 Returns a NAMESPACE_DECL corresponding to the specified
15070 namespace. */
15072 static tree
15073 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15075 /* Look for the optional `::'. */
15076 cp_parser_global_scope_opt (parser,
15077 /*current_scope_valid_p=*/false);
15079 /* Look for the optional nested-name-specifier. */
15080 cp_parser_nested_name_specifier_opt (parser,
15081 /*typename_keyword_p=*/false,
15082 /*check_dependency_p=*/true,
15083 /*type_p=*/false,
15084 /*is_declaration=*/true);
15086 return cp_parser_namespace_name (parser);
15089 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15090 access declaration.
15092 using-declaration:
15093 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15094 using :: unqualified-id ;
15096 access-declaration:
15097 qualified-id ;
15101 static bool
15102 cp_parser_using_declaration (cp_parser* parser,
15103 bool access_declaration_p)
15105 cp_token *token;
15106 bool typename_p = false;
15107 bool global_scope_p;
15108 tree decl;
15109 tree identifier;
15110 tree qscope;
15111 int oldcount = errorcount;
15112 cp_token *diag_token = NULL;
15114 if (access_declaration_p)
15116 diag_token = cp_lexer_peek_token (parser->lexer);
15117 cp_parser_parse_tentatively (parser);
15119 else
15121 /* Look for the `using' keyword. */
15122 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15124 /* Peek at the next token. */
15125 token = cp_lexer_peek_token (parser->lexer);
15126 /* See if it's `typename'. */
15127 if (token->keyword == RID_TYPENAME)
15129 /* Remember that we've seen it. */
15130 typename_p = true;
15131 /* Consume the `typename' token. */
15132 cp_lexer_consume_token (parser->lexer);
15136 /* Look for the optional global scope qualification. */
15137 global_scope_p
15138 = (cp_parser_global_scope_opt (parser,
15139 /*current_scope_valid_p=*/false)
15140 != NULL_TREE);
15142 /* If we saw `typename', or didn't see `::', then there must be a
15143 nested-name-specifier present. */
15144 if (typename_p || !global_scope_p)
15145 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15146 /*check_dependency_p=*/true,
15147 /*type_p=*/false,
15148 /*is_declaration=*/true);
15149 /* Otherwise, we could be in either of the two productions. In that
15150 case, treat the nested-name-specifier as optional. */
15151 else
15152 qscope = cp_parser_nested_name_specifier_opt (parser,
15153 /*typename_keyword_p=*/false,
15154 /*check_dependency_p=*/true,
15155 /*type_p=*/false,
15156 /*is_declaration=*/true);
15157 if (!qscope)
15158 qscope = global_namespace;
15160 if (access_declaration_p && cp_parser_error_occurred (parser))
15161 /* Something has already gone wrong; there's no need to parse
15162 further. Since an error has occurred, the return value of
15163 cp_parser_parse_definitely will be false, as required. */
15164 return cp_parser_parse_definitely (parser);
15166 token = cp_lexer_peek_token (parser->lexer);
15167 /* Parse the unqualified-id. */
15168 identifier = cp_parser_unqualified_id (parser,
15169 /*template_keyword_p=*/false,
15170 /*check_dependency_p=*/true,
15171 /*declarator_p=*/true,
15172 /*optional_p=*/false);
15174 if (access_declaration_p)
15176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15177 cp_parser_simulate_error (parser);
15178 if (!cp_parser_parse_definitely (parser))
15179 return false;
15182 /* The function we call to handle a using-declaration is different
15183 depending on what scope we are in. */
15184 if (qscope == error_mark_node || identifier == error_mark_node)
15186 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15187 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15188 /* [namespace.udecl]
15190 A using declaration shall not name a template-id. */
15191 error_at (token->location,
15192 "a template-id may not appear in a using-declaration");
15193 else
15195 if (at_class_scope_p ())
15197 /* Create the USING_DECL. */
15198 decl = do_class_using_decl (parser->scope, identifier);
15200 if (decl && typename_p)
15201 USING_DECL_TYPENAME_P (decl) = 1;
15203 if (check_for_bare_parameter_packs (decl))
15204 return false;
15205 else
15206 /* Add it to the list of members in this class. */
15207 finish_member_declaration (decl);
15209 else
15211 decl = cp_parser_lookup_name_simple (parser,
15212 identifier,
15213 token->location);
15214 if (decl == error_mark_node)
15215 cp_parser_name_lookup_error (parser, identifier,
15216 decl, NLE_NULL,
15217 token->location);
15218 else if (check_for_bare_parameter_packs (decl))
15219 return false;
15220 else if (!at_namespace_scope_p ())
15221 do_local_using_decl (decl, qscope, identifier);
15222 else
15223 do_toplevel_using_decl (decl, qscope, identifier);
15227 /* Look for the final `;'. */
15228 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15230 if (access_declaration_p && errorcount == oldcount)
15231 warning_at (diag_token->location, OPT_Wdeprecated,
15232 "access declarations are deprecated "
15233 "in favour of using-declarations; "
15234 "suggestion: add the %<using%> keyword");
15236 return true;
15239 /* Parse an alias-declaration.
15241 alias-declaration:
15242 using identifier attribute-specifier-seq [opt] = type-id */
15244 static tree
15245 cp_parser_alias_declaration (cp_parser* parser)
15247 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15248 location_t id_location;
15249 cp_declarator *declarator;
15250 cp_decl_specifier_seq decl_specs;
15251 bool member_p;
15252 const char *saved_message = NULL;
15254 /* Look for the `using' keyword. */
15255 cp_token *using_token
15256 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15257 if (using_token == NULL)
15258 return error_mark_node;
15260 id_location = cp_lexer_peek_token (parser->lexer)->location;
15261 id = cp_parser_identifier (parser);
15262 if (id == error_mark_node)
15263 return error_mark_node;
15265 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15266 attributes = cp_parser_attributes_opt (parser);
15267 if (attributes == error_mark_node)
15268 return error_mark_node;
15270 cp_parser_require (parser, CPP_EQ, RT_EQ);
15272 if (cp_parser_error_occurred (parser))
15273 return error_mark_node;
15275 /* Now we are going to parse the type-id of the declaration. */
15278 [dcl.type]/3 says:
15280 "A type-specifier-seq shall not define a class or enumeration
15281 unless it appears in the type-id of an alias-declaration (7.1.3) that
15282 is not the declaration of a template-declaration."
15284 In other words, if we currently are in an alias template, the
15285 type-id should not define a type.
15287 So let's set parser->type_definition_forbidden_message in that
15288 case; cp_parser_check_type_definition (called by
15289 cp_parser_class_specifier) will then emit an error if a type is
15290 defined in the type-id. */
15291 if (parser->num_template_parameter_lists)
15293 saved_message = parser->type_definition_forbidden_message;
15294 parser->type_definition_forbidden_message =
15295 G_("types may not be defined in alias template declarations");
15298 type = cp_parser_type_id (parser);
15300 /* Restore the error message if need be. */
15301 if (parser->num_template_parameter_lists)
15302 parser->type_definition_forbidden_message = saved_message;
15304 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15306 if (cp_parser_error_occurred (parser))
15307 return error_mark_node;
15309 /* A typedef-name can also be introduced by an alias-declaration. The
15310 identifier following the using keyword becomes a typedef-name. It has
15311 the same semantics as if it were introduced by the typedef
15312 specifier. In particular, it does not define a new type and it shall
15313 not appear in the type-id. */
15315 clear_decl_specs (&decl_specs);
15316 decl_specs.type = type;
15317 if (attributes != NULL_TREE)
15319 decl_specs.attributes = attributes;
15320 set_and_check_decl_spec_loc (&decl_specs,
15321 ds_attribute,
15322 attrs_token);
15324 set_and_check_decl_spec_loc (&decl_specs,
15325 ds_typedef,
15326 using_token);
15327 set_and_check_decl_spec_loc (&decl_specs,
15328 ds_alias,
15329 using_token);
15331 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15332 declarator->id_loc = id_location;
15334 member_p = at_class_scope_p ();
15335 if (member_p)
15336 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15337 NULL_TREE, attributes);
15338 else
15339 decl = start_decl (declarator, &decl_specs, 0,
15340 attributes, NULL_TREE, &pushed_scope);
15341 if (decl == error_mark_node)
15342 return decl;
15344 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15346 if (pushed_scope)
15347 pop_scope (pushed_scope);
15349 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15350 added into the symbol table; otherwise, return the TYPE_DECL. */
15351 if (DECL_LANG_SPECIFIC (decl)
15352 && DECL_TEMPLATE_INFO (decl)
15353 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15355 decl = DECL_TI_TEMPLATE (decl);
15356 if (member_p)
15357 check_member_template (decl);
15360 return decl;
15363 /* Parse a using-directive.
15365 using-directive:
15366 using namespace :: [opt] nested-name-specifier [opt]
15367 namespace-name ; */
15369 static void
15370 cp_parser_using_directive (cp_parser* parser)
15372 tree namespace_decl;
15373 tree attribs;
15375 /* Look for the `using' keyword. */
15376 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15377 /* And the `namespace' keyword. */
15378 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15379 /* Look for the optional `::' operator. */
15380 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15381 /* And the optional nested-name-specifier. */
15382 cp_parser_nested_name_specifier_opt (parser,
15383 /*typename_keyword_p=*/false,
15384 /*check_dependency_p=*/true,
15385 /*type_p=*/false,
15386 /*is_declaration=*/true);
15387 /* Get the namespace being used. */
15388 namespace_decl = cp_parser_namespace_name (parser);
15389 /* And any specified attributes. */
15390 attribs = cp_parser_attributes_opt (parser);
15391 /* Update the symbol table. */
15392 parse_using_directive (namespace_decl, attribs);
15393 /* Look for the final `;'. */
15394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15397 /* Parse an asm-definition.
15399 asm-definition:
15400 asm ( string-literal ) ;
15402 GNU Extension:
15404 asm-definition:
15405 asm volatile [opt] ( string-literal ) ;
15406 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15407 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15408 : asm-operand-list [opt] ) ;
15409 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15410 : asm-operand-list [opt]
15411 : asm-clobber-list [opt] ) ;
15412 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15413 : asm-clobber-list [opt]
15414 : asm-goto-list ) ; */
15416 static void
15417 cp_parser_asm_definition (cp_parser* parser)
15419 tree string;
15420 tree outputs = NULL_TREE;
15421 tree inputs = NULL_TREE;
15422 tree clobbers = NULL_TREE;
15423 tree labels = NULL_TREE;
15424 tree asm_stmt;
15425 bool volatile_p = false;
15426 bool extended_p = false;
15427 bool invalid_inputs_p = false;
15428 bool invalid_outputs_p = false;
15429 bool goto_p = false;
15430 required_token missing = RT_NONE;
15432 /* Look for the `asm' keyword. */
15433 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15434 /* See if the next token is `volatile'. */
15435 if (cp_parser_allow_gnu_extensions_p (parser)
15436 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15438 /* Remember that we saw the `volatile' keyword. */
15439 volatile_p = true;
15440 /* Consume the token. */
15441 cp_lexer_consume_token (parser->lexer);
15443 if (cp_parser_allow_gnu_extensions_p (parser)
15444 && parser->in_function_body
15445 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15447 /* Remember that we saw the `goto' keyword. */
15448 goto_p = true;
15449 /* Consume the token. */
15450 cp_lexer_consume_token (parser->lexer);
15452 /* Look for the opening `('. */
15453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15454 return;
15455 /* Look for the string. */
15456 string = cp_parser_string_literal (parser, false, false);
15457 if (string == error_mark_node)
15459 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15460 /*consume_paren=*/true);
15461 return;
15464 /* If we're allowing GNU extensions, check for the extended assembly
15465 syntax. Unfortunately, the `:' tokens need not be separated by
15466 a space in C, and so, for compatibility, we tolerate that here
15467 too. Doing that means that we have to treat the `::' operator as
15468 two `:' tokens. */
15469 if (cp_parser_allow_gnu_extensions_p (parser)
15470 && parser->in_function_body
15471 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15472 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15474 bool inputs_p = false;
15475 bool clobbers_p = false;
15476 bool labels_p = false;
15478 /* The extended syntax was used. */
15479 extended_p = true;
15481 /* Look for outputs. */
15482 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15484 /* Consume the `:'. */
15485 cp_lexer_consume_token (parser->lexer);
15486 /* Parse the output-operands. */
15487 if (cp_lexer_next_token_is_not (parser->lexer,
15488 CPP_COLON)
15489 && cp_lexer_next_token_is_not (parser->lexer,
15490 CPP_SCOPE)
15491 && cp_lexer_next_token_is_not (parser->lexer,
15492 CPP_CLOSE_PAREN)
15493 && !goto_p)
15494 outputs = cp_parser_asm_operand_list (parser);
15496 if (outputs == error_mark_node)
15497 invalid_outputs_p = true;
15499 /* If the next token is `::', there are no outputs, and the
15500 next token is the beginning of the inputs. */
15501 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15502 /* The inputs are coming next. */
15503 inputs_p = true;
15505 /* Look for inputs. */
15506 if (inputs_p
15507 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15509 /* Consume the `:' or `::'. */
15510 cp_lexer_consume_token (parser->lexer);
15511 /* Parse the output-operands. */
15512 if (cp_lexer_next_token_is_not (parser->lexer,
15513 CPP_COLON)
15514 && cp_lexer_next_token_is_not (parser->lexer,
15515 CPP_SCOPE)
15516 && cp_lexer_next_token_is_not (parser->lexer,
15517 CPP_CLOSE_PAREN))
15518 inputs = cp_parser_asm_operand_list (parser);
15520 if (inputs == error_mark_node)
15521 invalid_inputs_p = true;
15523 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15524 /* The clobbers are coming next. */
15525 clobbers_p = true;
15527 /* Look for clobbers. */
15528 if (clobbers_p
15529 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15531 clobbers_p = true;
15532 /* Consume the `:' or `::'. */
15533 cp_lexer_consume_token (parser->lexer);
15534 /* Parse the clobbers. */
15535 if (cp_lexer_next_token_is_not (parser->lexer,
15536 CPP_COLON)
15537 && cp_lexer_next_token_is_not (parser->lexer,
15538 CPP_CLOSE_PAREN))
15539 clobbers = cp_parser_asm_clobber_list (parser);
15541 else if (goto_p
15542 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15543 /* The labels are coming next. */
15544 labels_p = true;
15546 /* Look for labels. */
15547 if (labels_p
15548 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15550 labels_p = true;
15551 /* Consume the `:' or `::'. */
15552 cp_lexer_consume_token (parser->lexer);
15553 /* Parse the labels. */
15554 labels = cp_parser_asm_label_list (parser);
15557 if (goto_p && !labels_p)
15558 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15560 else if (goto_p)
15561 missing = RT_COLON_SCOPE;
15563 /* Look for the closing `)'. */
15564 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15565 missing ? missing : RT_CLOSE_PAREN))
15566 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15567 /*consume_paren=*/true);
15568 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15570 if (!invalid_inputs_p && !invalid_outputs_p)
15572 /* Create the ASM_EXPR. */
15573 if (parser->in_function_body)
15575 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15576 inputs, clobbers, labels);
15577 /* If the extended syntax was not used, mark the ASM_EXPR. */
15578 if (!extended_p)
15580 tree temp = asm_stmt;
15581 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15582 temp = TREE_OPERAND (temp, 0);
15584 ASM_INPUT_P (temp) = 1;
15587 else
15588 add_asm_node (string);
15592 /* Declarators [gram.dcl.decl] */
15594 /* Parse an init-declarator.
15596 init-declarator:
15597 declarator initializer [opt]
15599 GNU Extension:
15601 init-declarator:
15602 declarator asm-specification [opt] attributes [opt] initializer [opt]
15604 function-definition:
15605 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15606 function-body
15607 decl-specifier-seq [opt] declarator function-try-block
15609 GNU Extension:
15611 function-definition:
15612 __extension__ function-definition
15614 TM Extension:
15616 function-definition:
15617 decl-specifier-seq [opt] declarator function-transaction-block
15619 The DECL_SPECIFIERS apply to this declarator. Returns a
15620 representation of the entity declared. If MEMBER_P is TRUE, then
15621 this declarator appears in a class scope. The new DECL created by
15622 this declarator is returned.
15624 The CHECKS are access checks that should be performed once we know
15625 what entity is being declared (and, therefore, what classes have
15626 befriended it).
15628 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15629 for a function-definition here as well. If the declarator is a
15630 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15631 be TRUE upon return. By that point, the function-definition will
15632 have been completely parsed.
15634 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15635 is FALSE.
15637 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15638 parsed declaration if it is an uninitialized single declarator not followed
15639 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15640 if present, will not be consumed. If returned, this declarator will be
15641 created with SD_INITIALIZED but will not call cp_finish_decl. */
15643 static tree
15644 cp_parser_init_declarator (cp_parser* parser,
15645 cp_decl_specifier_seq *decl_specifiers,
15646 VEC (deferred_access_check,gc)* checks,
15647 bool function_definition_allowed_p,
15648 bool member_p,
15649 int declares_class_or_enum,
15650 bool* function_definition_p,
15651 tree* maybe_range_for_decl)
15653 cp_token *token = NULL, *asm_spec_start_token = NULL,
15654 *attributes_start_token = NULL;
15655 cp_declarator *declarator;
15656 tree prefix_attributes;
15657 tree attributes = NULL;
15658 tree asm_specification;
15659 tree initializer;
15660 tree decl = NULL_TREE;
15661 tree scope;
15662 int is_initialized;
15663 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15664 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15665 "(...)". */
15666 enum cpp_ttype initialization_kind;
15667 bool is_direct_init = false;
15668 bool is_non_constant_init;
15669 int ctor_dtor_or_conv_p;
15670 bool friend_p;
15671 tree pushed_scope = NULL_TREE;
15672 bool range_for_decl_p = false;
15674 /* Gather the attributes that were provided with the
15675 decl-specifiers. */
15676 prefix_attributes = decl_specifiers->attributes;
15678 /* Assume that this is not the declarator for a function
15679 definition. */
15680 if (function_definition_p)
15681 *function_definition_p = false;
15683 /* Defer access checks while parsing the declarator; we cannot know
15684 what names are accessible until we know what is being
15685 declared. */
15686 resume_deferring_access_checks ();
15688 /* Parse the declarator. */
15689 token = cp_lexer_peek_token (parser->lexer);
15690 declarator
15691 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15692 &ctor_dtor_or_conv_p,
15693 /*parenthesized_p=*/NULL,
15694 member_p);
15695 /* Gather up the deferred checks. */
15696 stop_deferring_access_checks ();
15698 /* If the DECLARATOR was erroneous, there's no need to go
15699 further. */
15700 if (declarator == cp_error_declarator)
15701 return error_mark_node;
15703 /* Check that the number of template-parameter-lists is OK. */
15704 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15705 token->location))
15706 return error_mark_node;
15708 if (declares_class_or_enum & 2)
15709 cp_parser_check_for_definition_in_return_type (declarator,
15710 decl_specifiers->type,
15711 decl_specifiers->locations[ds_type_spec]);
15713 /* Figure out what scope the entity declared by the DECLARATOR is
15714 located in. `grokdeclarator' sometimes changes the scope, so
15715 we compute it now. */
15716 scope = get_scope_of_declarator (declarator);
15718 /* Perform any lookups in the declared type which were thought to be
15719 dependent, but are not in the scope of the declarator. */
15720 decl_specifiers->type
15721 = maybe_update_decl_type (decl_specifiers->type, scope);
15723 /* If we're allowing GNU extensions, look for an
15724 asm-specification. */
15725 if (cp_parser_allow_gnu_extensions_p (parser))
15727 /* Look for an asm-specification. */
15728 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15729 asm_specification = cp_parser_asm_specification_opt (parser);
15731 else
15732 asm_specification = NULL_TREE;
15734 /* Look for attributes. */
15735 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15736 attributes = cp_parser_attributes_opt (parser);
15738 /* Peek at the next token. */
15739 token = cp_lexer_peek_token (parser->lexer);
15740 /* Check to see if the token indicates the start of a
15741 function-definition. */
15742 if (function_declarator_p (declarator)
15743 && cp_parser_token_starts_function_definition_p (token))
15745 if (!function_definition_allowed_p)
15747 /* If a function-definition should not appear here, issue an
15748 error message. */
15749 cp_parser_error (parser,
15750 "a function-definition is not allowed here");
15751 return error_mark_node;
15753 else
15755 location_t func_brace_location
15756 = cp_lexer_peek_token (parser->lexer)->location;
15758 /* Neither attributes nor an asm-specification are allowed
15759 on a function-definition. */
15760 if (asm_specification)
15761 error_at (asm_spec_start_token->location,
15762 "an asm-specification is not allowed "
15763 "on a function-definition");
15764 if (attributes)
15765 error_at (attributes_start_token->location,
15766 "attributes are not allowed on a function-definition");
15767 /* This is a function-definition. */
15768 *function_definition_p = true;
15770 /* Parse the function definition. */
15771 if (member_p)
15772 decl = cp_parser_save_member_function_body (parser,
15773 decl_specifiers,
15774 declarator,
15775 prefix_attributes);
15776 else
15777 decl
15778 = (cp_parser_function_definition_from_specifiers_and_declarator
15779 (parser, decl_specifiers, prefix_attributes, declarator));
15781 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15783 /* This is where the prologue starts... */
15784 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15785 = func_brace_location;
15788 return decl;
15792 /* [dcl.dcl]
15794 Only in function declarations for constructors, destructors, and
15795 type conversions can the decl-specifier-seq be omitted.
15797 We explicitly postpone this check past the point where we handle
15798 function-definitions because we tolerate function-definitions
15799 that are missing their return types in some modes. */
15800 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15802 cp_parser_error (parser,
15803 "expected constructor, destructor, or type conversion");
15804 return error_mark_node;
15807 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15808 if (token->type == CPP_EQ
15809 || token->type == CPP_OPEN_PAREN
15810 || token->type == CPP_OPEN_BRACE)
15812 is_initialized = SD_INITIALIZED;
15813 initialization_kind = token->type;
15814 if (maybe_range_for_decl)
15815 *maybe_range_for_decl = error_mark_node;
15817 if (token->type == CPP_EQ
15818 && function_declarator_p (declarator))
15820 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15821 if (t2->keyword == RID_DEFAULT)
15822 is_initialized = SD_DEFAULTED;
15823 else if (t2->keyword == RID_DELETE)
15824 is_initialized = SD_DELETED;
15827 else
15829 /* If the init-declarator isn't initialized and isn't followed by a
15830 `,' or `;', it's not a valid init-declarator. */
15831 if (token->type != CPP_COMMA
15832 && token->type != CPP_SEMICOLON)
15834 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15835 range_for_decl_p = true;
15836 else
15838 cp_parser_error (parser, "expected initializer");
15839 return error_mark_node;
15842 is_initialized = SD_UNINITIALIZED;
15843 initialization_kind = CPP_EOF;
15846 /* Because start_decl has side-effects, we should only call it if we
15847 know we're going ahead. By this point, we know that we cannot
15848 possibly be looking at any other construct. */
15849 cp_parser_commit_to_tentative_parse (parser);
15851 /* If the decl specifiers were bad, issue an error now that we're
15852 sure this was intended to be a declarator. Then continue
15853 declaring the variable(s), as int, to try to cut down on further
15854 errors. */
15855 if (decl_specifiers->any_specifiers_p
15856 && decl_specifiers->type == error_mark_node)
15858 cp_parser_error (parser, "invalid type in declaration");
15859 decl_specifiers->type = integer_type_node;
15862 /* Check to see whether or not this declaration is a friend. */
15863 friend_p = cp_parser_friend_p (decl_specifiers);
15865 /* Enter the newly declared entry in the symbol table. If we're
15866 processing a declaration in a class-specifier, we wait until
15867 after processing the initializer. */
15868 if (!member_p)
15870 if (parser->in_unbraced_linkage_specification_p)
15871 decl_specifiers->storage_class = sc_extern;
15872 decl = start_decl (declarator, decl_specifiers,
15873 range_for_decl_p? SD_INITIALIZED : is_initialized,
15874 attributes, prefix_attributes,
15875 &pushed_scope);
15876 /* Adjust location of decl if declarator->id_loc is more appropriate:
15877 set, and decl wasn't merged with another decl, in which case its
15878 location would be different from input_location, and more accurate. */
15879 if (DECL_P (decl)
15880 && declarator->id_loc != UNKNOWN_LOCATION
15881 && DECL_SOURCE_LOCATION (decl) == input_location)
15882 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15884 else if (scope)
15885 /* Enter the SCOPE. That way unqualified names appearing in the
15886 initializer will be looked up in SCOPE. */
15887 pushed_scope = push_scope (scope);
15889 /* Perform deferred access control checks, now that we know in which
15890 SCOPE the declared entity resides. */
15891 if (!member_p && decl)
15893 tree saved_current_function_decl = NULL_TREE;
15895 /* If the entity being declared is a function, pretend that we
15896 are in its scope. If it is a `friend', it may have access to
15897 things that would not otherwise be accessible. */
15898 if (TREE_CODE (decl) == FUNCTION_DECL)
15900 saved_current_function_decl = current_function_decl;
15901 current_function_decl = decl;
15904 /* Perform access checks for template parameters. */
15905 cp_parser_perform_template_parameter_access_checks (checks);
15907 /* Perform the access control checks for the declarator and the
15908 decl-specifiers. */
15909 perform_deferred_access_checks (tf_warning_or_error);
15911 /* Restore the saved value. */
15912 if (TREE_CODE (decl) == FUNCTION_DECL)
15913 current_function_decl = saved_current_function_decl;
15916 /* Parse the initializer. */
15917 initializer = NULL_TREE;
15918 is_direct_init = false;
15919 is_non_constant_init = true;
15920 if (is_initialized)
15922 if (function_declarator_p (declarator))
15924 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15925 if (initialization_kind == CPP_EQ)
15926 initializer = cp_parser_pure_specifier (parser);
15927 else
15929 /* If the declaration was erroneous, we don't really
15930 know what the user intended, so just silently
15931 consume the initializer. */
15932 if (decl != error_mark_node)
15933 error_at (initializer_start_token->location,
15934 "initializer provided for function");
15935 cp_parser_skip_to_closing_parenthesis (parser,
15936 /*recovering=*/true,
15937 /*or_comma=*/false,
15938 /*consume_paren=*/true);
15941 else
15943 /* We want to record the extra mangling scope for in-class
15944 initializers of class members and initializers of static data
15945 member templates. The former involves deferring
15946 parsing of the initializer until end of class as with default
15947 arguments. So right here we only handle the latter. */
15948 if (!member_p && processing_template_decl)
15949 start_lambda_scope (decl);
15950 initializer = cp_parser_initializer (parser,
15951 &is_direct_init,
15952 &is_non_constant_init);
15953 if (!member_p && processing_template_decl)
15954 finish_lambda_scope ();
15955 if (initializer == error_mark_node)
15956 cp_parser_skip_to_end_of_statement (parser);
15960 /* The old parser allows attributes to appear after a parenthesized
15961 initializer. Mark Mitchell proposed removing this functionality
15962 on the GCC mailing lists on 2002-08-13. This parser accepts the
15963 attributes -- but ignores them. */
15964 if (cp_parser_allow_gnu_extensions_p (parser)
15965 && initialization_kind == CPP_OPEN_PAREN)
15966 if (cp_parser_attributes_opt (parser))
15967 warning (OPT_Wattributes,
15968 "attributes after parenthesized initializer ignored");
15970 /* For an in-class declaration, use `grokfield' to create the
15971 declaration. */
15972 if (member_p)
15974 if (pushed_scope)
15976 pop_scope (pushed_scope);
15977 pushed_scope = NULL_TREE;
15979 decl = grokfield (declarator, decl_specifiers,
15980 initializer, !is_non_constant_init,
15981 /*asmspec=*/NULL_TREE,
15982 prefix_attributes);
15983 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15984 cp_parser_save_default_args (parser, decl);
15987 /* Finish processing the declaration. But, skip member
15988 declarations. */
15989 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15991 cp_finish_decl (decl,
15992 initializer, !is_non_constant_init,
15993 asm_specification,
15994 /* If the initializer is in parentheses, then this is
15995 a direct-initialization, which means that an
15996 `explicit' constructor is OK. Otherwise, an
15997 `explicit' constructor cannot be used. */
15998 ((is_direct_init || !is_initialized)
15999 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16001 else if ((cxx_dialect != cxx98) && friend_p
16002 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16003 /* Core issue #226 (C++0x only): A default template-argument
16004 shall not be specified in a friend class template
16005 declaration. */
16006 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16007 /*is_partial=*/false, /*is_friend_decl=*/1);
16009 if (!friend_p && pushed_scope)
16010 pop_scope (pushed_scope);
16012 return decl;
16015 /* Parse a declarator.
16017 declarator:
16018 direct-declarator
16019 ptr-operator declarator
16021 abstract-declarator:
16022 ptr-operator abstract-declarator [opt]
16023 direct-abstract-declarator
16025 GNU Extensions:
16027 declarator:
16028 attributes [opt] direct-declarator
16029 attributes [opt] ptr-operator declarator
16031 abstract-declarator:
16032 attributes [opt] ptr-operator abstract-declarator [opt]
16033 attributes [opt] direct-abstract-declarator
16035 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16036 detect constructor, destructor or conversion operators. It is set
16037 to -1 if the declarator is a name, and +1 if it is a
16038 function. Otherwise it is set to zero. Usually you just want to
16039 test for >0, but internally the negative value is used.
16041 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16042 a decl-specifier-seq unless it declares a constructor, destructor,
16043 or conversion. It might seem that we could check this condition in
16044 semantic analysis, rather than parsing, but that makes it difficult
16045 to handle something like `f()'. We want to notice that there are
16046 no decl-specifiers, and therefore realize that this is an
16047 expression, not a declaration.)
16049 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16050 the declarator is a direct-declarator of the form "(...)".
16052 MEMBER_P is true iff this declarator is a member-declarator. */
16054 static cp_declarator *
16055 cp_parser_declarator (cp_parser* parser,
16056 cp_parser_declarator_kind dcl_kind,
16057 int* ctor_dtor_or_conv_p,
16058 bool* parenthesized_p,
16059 bool member_p)
16061 cp_declarator *declarator;
16062 enum tree_code code;
16063 cp_cv_quals cv_quals;
16064 tree class_type;
16065 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16067 /* Assume this is not a constructor, destructor, or type-conversion
16068 operator. */
16069 if (ctor_dtor_or_conv_p)
16070 *ctor_dtor_or_conv_p = 0;
16072 if (cp_parser_allow_gnu_extensions_p (parser))
16073 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16075 /* Check for the ptr-operator production. */
16076 cp_parser_parse_tentatively (parser);
16077 /* Parse the ptr-operator. */
16078 code = cp_parser_ptr_operator (parser,
16079 &class_type,
16080 &cv_quals,
16081 &std_attributes);
16083 /* If that worked, then we have a ptr-operator. */
16084 if (cp_parser_parse_definitely (parser))
16086 /* If a ptr-operator was found, then this declarator was not
16087 parenthesized. */
16088 if (parenthesized_p)
16089 *parenthesized_p = true;
16090 /* The dependent declarator is optional if we are parsing an
16091 abstract-declarator. */
16092 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16093 cp_parser_parse_tentatively (parser);
16095 /* Parse the dependent declarator. */
16096 declarator = cp_parser_declarator (parser, dcl_kind,
16097 /*ctor_dtor_or_conv_p=*/NULL,
16098 /*parenthesized_p=*/NULL,
16099 /*member_p=*/false);
16101 /* If we are parsing an abstract-declarator, we must handle the
16102 case where the dependent declarator is absent. */
16103 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16104 && !cp_parser_parse_definitely (parser))
16105 declarator = NULL;
16107 declarator = cp_parser_make_indirect_declarator
16108 (code, class_type, cv_quals, declarator, std_attributes);
16110 /* Everything else is a direct-declarator. */
16111 else
16113 if (parenthesized_p)
16114 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16115 CPP_OPEN_PAREN);
16116 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16117 ctor_dtor_or_conv_p,
16118 member_p);
16121 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16122 declarator->attributes = gnu_attributes;
16123 return declarator;
16126 /* Parse a direct-declarator or direct-abstract-declarator.
16128 direct-declarator:
16129 declarator-id
16130 direct-declarator ( parameter-declaration-clause )
16131 cv-qualifier-seq [opt]
16132 exception-specification [opt]
16133 direct-declarator [ constant-expression [opt] ]
16134 ( declarator )
16136 direct-abstract-declarator:
16137 direct-abstract-declarator [opt]
16138 ( parameter-declaration-clause )
16139 cv-qualifier-seq [opt]
16140 exception-specification [opt]
16141 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16142 ( abstract-declarator )
16144 Returns a representation of the declarator. DCL_KIND is
16145 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16146 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16147 we are parsing a direct-declarator. It is
16148 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16149 of ambiguity we prefer an abstract declarator, as per
16150 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16151 cp_parser_declarator. */
16153 static cp_declarator *
16154 cp_parser_direct_declarator (cp_parser* parser,
16155 cp_parser_declarator_kind dcl_kind,
16156 int* ctor_dtor_or_conv_p,
16157 bool member_p)
16159 cp_token *token;
16160 cp_declarator *declarator = NULL;
16161 tree scope = NULL_TREE;
16162 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16163 bool saved_in_declarator_p = parser->in_declarator_p;
16164 bool first = true;
16165 tree pushed_scope = NULL_TREE;
16167 while (true)
16169 /* Peek at the next token. */
16170 token = cp_lexer_peek_token (parser->lexer);
16171 if (token->type == CPP_OPEN_PAREN)
16173 /* This is either a parameter-declaration-clause, or a
16174 parenthesized declarator. When we know we are parsing a
16175 named declarator, it must be a parenthesized declarator
16176 if FIRST is true. For instance, `(int)' is a
16177 parameter-declaration-clause, with an omitted
16178 direct-abstract-declarator. But `((*))', is a
16179 parenthesized abstract declarator. Finally, when T is a
16180 template parameter `(T)' is a
16181 parameter-declaration-clause, and not a parenthesized
16182 named declarator.
16184 We first try and parse a parameter-declaration-clause,
16185 and then try a nested declarator (if FIRST is true).
16187 It is not an error for it not to be a
16188 parameter-declaration-clause, even when FIRST is
16189 false. Consider,
16191 int i (int);
16192 int i (3);
16194 The first is the declaration of a function while the
16195 second is the definition of a variable, including its
16196 initializer.
16198 Having seen only the parenthesis, we cannot know which of
16199 these two alternatives should be selected. Even more
16200 complex are examples like:
16202 int i (int (a));
16203 int i (int (3));
16205 The former is a function-declaration; the latter is a
16206 variable initialization.
16208 Thus again, we try a parameter-declaration-clause, and if
16209 that fails, we back out and return. */
16211 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16213 tree params;
16214 unsigned saved_num_template_parameter_lists;
16215 bool is_declarator = false;
16216 tree t;
16218 /* In a member-declarator, the only valid interpretation
16219 of a parenthesis is the start of a
16220 parameter-declaration-clause. (It is invalid to
16221 initialize a static data member with a parenthesized
16222 initializer; only the "=" form of initialization is
16223 permitted.) */
16224 if (!member_p)
16225 cp_parser_parse_tentatively (parser);
16227 /* Consume the `('. */
16228 cp_lexer_consume_token (parser->lexer);
16229 if (first)
16231 /* If this is going to be an abstract declarator, we're
16232 in a declarator and we can't have default args. */
16233 parser->default_arg_ok_p = false;
16234 parser->in_declarator_p = true;
16237 /* Inside the function parameter list, surrounding
16238 template-parameter-lists do not apply. */
16239 saved_num_template_parameter_lists
16240 = parser->num_template_parameter_lists;
16241 parser->num_template_parameter_lists = 0;
16243 begin_scope (sk_function_parms, NULL_TREE);
16245 /* Parse the parameter-declaration-clause. */
16246 params = cp_parser_parameter_declaration_clause (parser);
16248 parser->num_template_parameter_lists
16249 = saved_num_template_parameter_lists;
16251 /* Consume the `)'. */
16252 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16254 /* If all went well, parse the cv-qualifier-seq and the
16255 exception-specification. */
16256 if (member_p || cp_parser_parse_definitely (parser))
16258 cp_cv_quals cv_quals;
16259 cp_virt_specifiers virt_specifiers;
16260 tree exception_specification;
16261 tree late_return;
16262 tree attrs;
16264 is_declarator = true;
16266 if (ctor_dtor_or_conv_p)
16267 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16268 first = false;
16270 /* Parse the cv-qualifier-seq. */
16271 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16272 /* And the exception-specification. */
16273 exception_specification
16274 = cp_parser_exception_specification_opt (parser);
16276 attrs = cp_parser_std_attribute_spec_seq (parser);
16278 late_return = (cp_parser_late_return_type_opt
16279 (parser, member_p ? cv_quals : -1));
16281 /* Parse the virt-specifier-seq. */
16282 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16284 /* Create the function-declarator. */
16285 declarator = make_call_declarator (declarator,
16286 params,
16287 cv_quals,
16288 virt_specifiers,
16289 exception_specification,
16290 late_return);
16291 declarator->std_attributes = attrs;
16292 /* Any subsequent parameter lists are to do with
16293 return type, so are not those of the declared
16294 function. */
16295 parser->default_arg_ok_p = false;
16298 /* Remove the function parms from scope. */
16299 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16300 pop_binding (DECL_NAME (t), t);
16301 leave_scope();
16303 if (is_declarator)
16304 /* Repeat the main loop. */
16305 continue;
16308 /* If this is the first, we can try a parenthesized
16309 declarator. */
16310 if (first)
16312 bool saved_in_type_id_in_expr_p;
16314 parser->default_arg_ok_p = saved_default_arg_ok_p;
16315 parser->in_declarator_p = saved_in_declarator_p;
16317 /* Consume the `('. */
16318 cp_lexer_consume_token (parser->lexer);
16319 /* Parse the nested declarator. */
16320 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16321 parser->in_type_id_in_expr_p = true;
16322 declarator
16323 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16324 /*parenthesized_p=*/NULL,
16325 member_p);
16326 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16327 first = false;
16328 /* Expect a `)'. */
16329 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16330 declarator = cp_error_declarator;
16331 if (declarator == cp_error_declarator)
16332 break;
16334 goto handle_declarator;
16336 /* Otherwise, we must be done. */
16337 else
16338 break;
16340 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16341 && token->type == CPP_OPEN_SQUARE
16342 && !cp_next_tokens_can_be_attribute_p (parser))
16344 /* Parse an array-declarator. */
16345 tree bounds, attrs;
16347 if (ctor_dtor_or_conv_p)
16348 *ctor_dtor_or_conv_p = 0;
16350 first = false;
16351 parser->default_arg_ok_p = false;
16352 parser->in_declarator_p = true;
16353 /* Consume the `['. */
16354 cp_lexer_consume_token (parser->lexer);
16355 /* Peek at the next token. */
16356 token = cp_lexer_peek_token (parser->lexer);
16357 /* If the next token is `]', then there is no
16358 constant-expression. */
16359 if (token->type != CPP_CLOSE_SQUARE)
16361 bool non_constant_p;
16363 bounds
16364 = cp_parser_constant_expression (parser,
16365 /*allow_non_constant=*/true,
16366 &non_constant_p);
16367 if (!non_constant_p)
16368 /* OK */;
16369 else if (error_operand_p (bounds))
16370 /* Already gave an error. */;
16371 else if (!parser->in_function_body
16372 || current_binding_level->kind == sk_function_parms)
16374 /* Normally, the array bound must be an integral constant
16375 expression. However, as an extension, we allow VLAs
16376 in function scopes as long as they aren't part of a
16377 parameter declaration. */
16378 cp_parser_error (parser,
16379 "array bound is not an integer constant");
16380 bounds = error_mark_node;
16382 else if (processing_template_decl)
16384 /* Remember this wasn't a constant-expression. */
16385 bounds = build_nop (TREE_TYPE (bounds), bounds);
16386 TREE_SIDE_EFFECTS (bounds) = 1;
16389 else
16390 bounds = NULL_TREE;
16391 /* Look for the closing `]'. */
16392 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16394 declarator = cp_error_declarator;
16395 break;
16398 attrs = cp_parser_std_attribute_spec_seq (parser);
16399 declarator = make_array_declarator (declarator, bounds);
16400 declarator->std_attributes = attrs;
16402 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16405 tree qualifying_scope;
16406 tree unqualified_name;
16407 tree attrs;
16408 special_function_kind sfk;
16409 bool abstract_ok;
16410 bool pack_expansion_p = false;
16411 cp_token *declarator_id_start_token;
16413 /* Parse a declarator-id */
16414 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16415 if (abstract_ok)
16417 cp_parser_parse_tentatively (parser);
16419 /* If we see an ellipsis, we should be looking at a
16420 parameter pack. */
16421 if (token->type == CPP_ELLIPSIS)
16423 /* Consume the `...' */
16424 cp_lexer_consume_token (parser->lexer);
16426 pack_expansion_p = true;
16430 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16431 unqualified_name
16432 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16433 qualifying_scope = parser->scope;
16434 if (abstract_ok)
16436 bool okay = false;
16438 if (!unqualified_name && pack_expansion_p)
16440 /* Check whether an error occurred. */
16441 okay = !cp_parser_error_occurred (parser);
16443 /* We already consumed the ellipsis to mark a
16444 parameter pack, but we have no way to report it,
16445 so abort the tentative parse. We will be exiting
16446 immediately anyway. */
16447 cp_parser_abort_tentative_parse (parser);
16449 else
16450 okay = cp_parser_parse_definitely (parser);
16452 if (!okay)
16453 unqualified_name = error_mark_node;
16454 else if (unqualified_name
16455 && (qualifying_scope
16456 || (TREE_CODE (unqualified_name)
16457 != IDENTIFIER_NODE)))
16459 cp_parser_error (parser, "expected unqualified-id");
16460 unqualified_name = error_mark_node;
16464 if (!unqualified_name)
16465 return NULL;
16466 if (unqualified_name == error_mark_node)
16468 declarator = cp_error_declarator;
16469 pack_expansion_p = false;
16470 declarator->parameter_pack_p = false;
16471 break;
16474 attrs = cp_parser_std_attribute_spec_seq (parser);
16476 if (qualifying_scope && at_namespace_scope_p ()
16477 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16479 /* In the declaration of a member of a template class
16480 outside of the class itself, the SCOPE will sometimes
16481 be a TYPENAME_TYPE. For example, given:
16483 template <typename T>
16484 int S<T>::R::i = 3;
16486 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16487 this context, we must resolve S<T>::R to an ordinary
16488 type, rather than a typename type.
16490 The reason we normally avoid resolving TYPENAME_TYPEs
16491 is that a specialization of `S' might render
16492 `S<T>::R' not a type. However, if `S' is
16493 specialized, then this `i' will not be used, so there
16494 is no harm in resolving the types here. */
16495 tree type;
16497 /* Resolve the TYPENAME_TYPE. */
16498 type = resolve_typename_type (qualifying_scope,
16499 /*only_current_p=*/false);
16500 /* If that failed, the declarator is invalid. */
16501 if (TREE_CODE (type) == TYPENAME_TYPE)
16503 if (typedef_variant_p (type))
16504 error_at (declarator_id_start_token->location,
16505 "cannot define member of dependent typedef "
16506 "%qT", type);
16507 else
16508 error_at (declarator_id_start_token->location,
16509 "%<%T::%E%> is not a type",
16510 TYPE_CONTEXT (qualifying_scope),
16511 TYPE_IDENTIFIER (qualifying_scope));
16513 qualifying_scope = type;
16516 sfk = sfk_none;
16518 if (unqualified_name)
16520 tree class_type;
16522 if (qualifying_scope
16523 && CLASS_TYPE_P (qualifying_scope))
16524 class_type = qualifying_scope;
16525 else
16526 class_type = current_class_type;
16528 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16530 tree name_type = TREE_TYPE (unqualified_name);
16531 if (class_type && same_type_p (name_type, class_type))
16533 if (qualifying_scope
16534 && CLASSTYPE_USE_TEMPLATE (name_type))
16536 error_at (declarator_id_start_token->location,
16537 "invalid use of constructor as a template");
16538 inform (declarator_id_start_token->location,
16539 "use %<%T::%D%> instead of %<%T::%D%> to "
16540 "name the constructor in a qualified name",
16541 class_type,
16542 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16543 class_type, name_type);
16544 declarator = cp_error_declarator;
16545 break;
16547 else
16548 unqualified_name = constructor_name (class_type);
16550 else
16552 /* We do not attempt to print the declarator
16553 here because we do not have enough
16554 information about its original syntactic
16555 form. */
16556 cp_parser_error (parser, "invalid declarator");
16557 declarator = cp_error_declarator;
16558 break;
16562 if (class_type)
16564 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16565 sfk = sfk_destructor;
16566 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16567 sfk = sfk_conversion;
16568 else if (/* There's no way to declare a constructor
16569 for an anonymous type, even if the type
16570 got a name for linkage purposes. */
16571 !TYPE_WAS_ANONYMOUS (class_type)
16572 && constructor_name_p (unqualified_name,
16573 class_type))
16575 unqualified_name = constructor_name (class_type);
16576 sfk = sfk_constructor;
16578 else if (is_overloaded_fn (unqualified_name)
16579 && DECL_CONSTRUCTOR_P (get_first_fn
16580 (unqualified_name)))
16581 sfk = sfk_constructor;
16583 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16584 *ctor_dtor_or_conv_p = -1;
16587 declarator = make_id_declarator (qualifying_scope,
16588 unqualified_name,
16589 sfk);
16590 declarator->std_attributes = attrs;
16591 declarator->id_loc = token->location;
16592 declarator->parameter_pack_p = pack_expansion_p;
16594 if (pack_expansion_p)
16595 maybe_warn_variadic_templates ();
16598 handle_declarator:;
16599 scope = get_scope_of_declarator (declarator);
16600 if (scope)
16601 /* Any names that appear after the declarator-id for a
16602 member are looked up in the containing scope. */
16603 pushed_scope = push_scope (scope);
16604 parser->in_declarator_p = true;
16605 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16606 || (declarator && declarator->kind == cdk_id))
16607 /* Default args are only allowed on function
16608 declarations. */
16609 parser->default_arg_ok_p = saved_default_arg_ok_p;
16610 else
16611 parser->default_arg_ok_p = false;
16613 first = false;
16615 /* We're done. */
16616 else
16617 break;
16620 /* For an abstract declarator, we might wind up with nothing at this
16621 point. That's an error; the declarator is not optional. */
16622 if (!declarator)
16623 cp_parser_error (parser, "expected declarator");
16625 /* If we entered a scope, we must exit it now. */
16626 if (pushed_scope)
16627 pop_scope (pushed_scope);
16629 parser->default_arg_ok_p = saved_default_arg_ok_p;
16630 parser->in_declarator_p = saved_in_declarator_p;
16632 return declarator;
16635 /* Parse a ptr-operator.
16637 ptr-operator:
16638 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16639 * cv-qualifier-seq [opt]
16641 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16642 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16644 GNU Extension:
16646 ptr-operator:
16647 & cv-qualifier-seq [opt]
16649 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16650 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16651 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16652 filled in with the TYPE containing the member. *CV_QUALS is
16653 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16654 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16655 Note that the tree codes returned by this function have nothing
16656 to do with the types of trees that will be eventually be created
16657 to represent the pointer or reference type being parsed. They are
16658 just constants with suggestive names. */
16659 static enum tree_code
16660 cp_parser_ptr_operator (cp_parser* parser,
16661 tree* type,
16662 cp_cv_quals *cv_quals,
16663 tree *attributes)
16665 enum tree_code code = ERROR_MARK;
16666 cp_token *token;
16667 tree attrs = NULL_TREE;
16669 /* Assume that it's not a pointer-to-member. */
16670 *type = NULL_TREE;
16671 /* And that there are no cv-qualifiers. */
16672 *cv_quals = TYPE_UNQUALIFIED;
16674 /* Peek at the next token. */
16675 token = cp_lexer_peek_token (parser->lexer);
16677 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16678 if (token->type == CPP_MULT)
16679 code = INDIRECT_REF;
16680 else if (token->type == CPP_AND)
16681 code = ADDR_EXPR;
16682 else if ((cxx_dialect != cxx98) &&
16683 token->type == CPP_AND_AND) /* C++0x only */
16684 code = NON_LVALUE_EXPR;
16686 if (code != ERROR_MARK)
16688 /* Consume the `*', `&' or `&&'. */
16689 cp_lexer_consume_token (parser->lexer);
16691 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16692 `&', if we are allowing GNU extensions. (The only qualifier
16693 that can legally appear after `&' is `restrict', but that is
16694 enforced during semantic analysis. */
16695 if (code == INDIRECT_REF
16696 || cp_parser_allow_gnu_extensions_p (parser))
16697 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16699 attrs = cp_parser_std_attribute_spec_seq (parser);
16700 if (attributes != NULL)
16701 *attributes = attrs;
16703 else
16705 /* Try the pointer-to-member case. */
16706 cp_parser_parse_tentatively (parser);
16707 /* Look for the optional `::' operator. */
16708 cp_parser_global_scope_opt (parser,
16709 /*current_scope_valid_p=*/false);
16710 /* Look for the nested-name specifier. */
16711 token = cp_lexer_peek_token (parser->lexer);
16712 cp_parser_nested_name_specifier (parser,
16713 /*typename_keyword_p=*/false,
16714 /*check_dependency_p=*/true,
16715 /*type_p=*/false,
16716 /*is_declaration=*/false);
16717 /* If we found it, and the next token is a `*', then we are
16718 indeed looking at a pointer-to-member operator. */
16719 if (!cp_parser_error_occurred (parser)
16720 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16722 /* Indicate that the `*' operator was used. */
16723 code = INDIRECT_REF;
16725 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16726 error_at (token->location, "%qD is a namespace", parser->scope);
16727 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16728 error_at (token->location, "cannot form pointer to member of "
16729 "non-class %q#T", parser->scope);
16730 else
16732 /* The type of which the member is a member is given by the
16733 current SCOPE. */
16734 *type = parser->scope;
16735 /* The next name will not be qualified. */
16736 parser->scope = NULL_TREE;
16737 parser->qualifying_scope = NULL_TREE;
16738 parser->object_scope = NULL_TREE;
16739 /* Look for optional c++11 attributes. */
16740 attrs = cp_parser_std_attribute_spec_seq (parser);
16741 if (attributes != NULL)
16742 *attributes = attrs;
16743 /* Look for the optional cv-qualifier-seq. */
16744 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16747 /* If that didn't work we don't have a ptr-operator. */
16748 if (!cp_parser_parse_definitely (parser))
16749 cp_parser_error (parser, "expected ptr-operator");
16752 return code;
16755 /* Parse an (optional) cv-qualifier-seq.
16757 cv-qualifier-seq:
16758 cv-qualifier cv-qualifier-seq [opt]
16760 cv-qualifier:
16761 const
16762 volatile
16764 GNU Extension:
16766 cv-qualifier:
16767 __restrict__
16769 Returns a bitmask representing the cv-qualifiers. */
16771 static cp_cv_quals
16772 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16774 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16776 while (true)
16778 cp_token *token;
16779 cp_cv_quals cv_qualifier;
16781 /* Peek at the next token. */
16782 token = cp_lexer_peek_token (parser->lexer);
16783 /* See if it's a cv-qualifier. */
16784 switch (token->keyword)
16786 case RID_CONST:
16787 cv_qualifier = TYPE_QUAL_CONST;
16788 break;
16790 case RID_VOLATILE:
16791 cv_qualifier = TYPE_QUAL_VOLATILE;
16792 break;
16794 case RID_RESTRICT:
16795 cv_qualifier = TYPE_QUAL_RESTRICT;
16796 break;
16798 default:
16799 cv_qualifier = TYPE_UNQUALIFIED;
16800 break;
16803 if (!cv_qualifier)
16804 break;
16806 if (cv_quals & cv_qualifier)
16808 error_at (token->location, "duplicate cv-qualifier");
16809 cp_lexer_purge_token (parser->lexer);
16811 else
16813 cp_lexer_consume_token (parser->lexer);
16814 cv_quals |= cv_qualifier;
16818 return cv_quals;
16821 /* Parse an (optional) virt-specifier-seq.
16823 virt-specifier-seq:
16824 virt-specifier virt-specifier-seq [opt]
16826 virt-specifier:
16827 override
16828 final
16830 Returns a bitmask representing the virt-specifiers. */
16832 static cp_virt_specifiers
16833 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16835 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16837 while (true)
16839 cp_token *token;
16840 cp_virt_specifiers virt_specifier;
16842 /* Peek at the next token. */
16843 token = cp_lexer_peek_token (parser->lexer);
16844 /* See if it's a virt-specifier-qualifier. */
16845 if (token->type != CPP_NAME)
16846 break;
16847 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16849 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16850 virt_specifier = VIRT_SPEC_OVERRIDE;
16852 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16854 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16855 virt_specifier = VIRT_SPEC_FINAL;
16857 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16859 virt_specifier = VIRT_SPEC_FINAL;
16861 else
16862 break;
16864 if (virt_specifiers & virt_specifier)
16866 error_at (token->location, "duplicate virt-specifier");
16867 cp_lexer_purge_token (parser->lexer);
16869 else
16871 cp_lexer_consume_token (parser->lexer);
16872 virt_specifiers |= virt_specifier;
16875 return virt_specifiers;
16878 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16879 is in scope even though it isn't real. */
16881 static void
16882 inject_this_parameter (tree ctype, cp_cv_quals quals)
16884 tree this_parm;
16886 if (current_class_ptr)
16888 /* We don't clear this between NSDMIs. Is it already what we want? */
16889 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16890 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16891 && cp_type_quals (type) == quals)
16892 return;
16895 this_parm = build_this_parm (ctype, quals);
16896 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16897 current_class_ptr = NULL_TREE;
16898 current_class_ref
16899 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16900 current_class_ptr = this_parm;
16903 /* Parse a late-specified return type, if any. This is not a separate
16904 non-terminal, but part of a function declarator, which looks like
16906 -> trailing-type-specifier-seq abstract-declarator(opt)
16908 Returns the type indicated by the type-id.
16910 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16911 function. */
16913 static tree
16914 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16916 cp_token *token;
16917 tree type;
16919 /* Peek at the next token. */
16920 token = cp_lexer_peek_token (parser->lexer);
16921 /* A late-specified return type is indicated by an initial '->'. */
16922 if (token->type != CPP_DEREF)
16923 return NULL_TREE;
16925 /* Consume the ->. */
16926 cp_lexer_consume_token (parser->lexer);
16928 if (quals >= 0)
16930 /* DR 1207: 'this' is in scope in the trailing return type. */
16931 gcc_assert (current_class_ptr == NULL_TREE);
16932 inject_this_parameter (current_class_type, quals);
16935 type = cp_parser_trailing_type_id (parser);
16937 if (quals >= 0)
16938 current_class_ptr = current_class_ref = NULL_TREE;
16940 return type;
16943 /* Parse a declarator-id.
16945 declarator-id:
16946 id-expression
16947 :: [opt] nested-name-specifier [opt] type-name
16949 In the `id-expression' case, the value returned is as for
16950 cp_parser_id_expression if the id-expression was an unqualified-id.
16951 If the id-expression was a qualified-id, then a SCOPE_REF is
16952 returned. The first operand is the scope (either a NAMESPACE_DECL
16953 or TREE_TYPE), but the second is still just a representation of an
16954 unqualified-id. */
16956 static tree
16957 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16959 tree id;
16960 /* The expression must be an id-expression. Assume that qualified
16961 names are the names of types so that:
16963 template <class T>
16964 int S<T>::R::i = 3;
16966 will work; we must treat `S<T>::R' as the name of a type.
16967 Similarly, assume that qualified names are templates, where
16968 required, so that:
16970 template <class T>
16971 int S<T>::R<T>::i = 3;
16973 will work, too. */
16974 id = cp_parser_id_expression (parser,
16975 /*template_keyword_p=*/false,
16976 /*check_dependency_p=*/false,
16977 /*template_p=*/NULL,
16978 /*declarator_p=*/true,
16979 optional_p);
16980 if (id && BASELINK_P (id))
16981 id = BASELINK_FUNCTIONS (id);
16982 return id;
16985 /* Parse a type-id.
16987 type-id:
16988 type-specifier-seq abstract-declarator [opt]
16990 Returns the TYPE specified. */
16992 static tree
16993 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16994 bool is_trailing_return)
16996 cp_decl_specifier_seq type_specifier_seq;
16997 cp_declarator *abstract_declarator;
16999 /* Parse the type-specifier-seq. */
17000 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17001 is_trailing_return,
17002 &type_specifier_seq);
17003 if (type_specifier_seq.type == error_mark_node)
17004 return error_mark_node;
17006 /* There might or might not be an abstract declarator. */
17007 cp_parser_parse_tentatively (parser);
17008 /* Look for the declarator. */
17009 abstract_declarator
17010 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17011 /*parenthesized_p=*/NULL,
17012 /*member_p=*/false);
17013 /* Check to see if there really was a declarator. */
17014 if (!cp_parser_parse_definitely (parser))
17015 abstract_declarator = NULL;
17017 if (type_specifier_seq.type
17018 && type_uses_auto (type_specifier_seq.type))
17020 /* A type-id with type 'auto' is only ok if the abstract declarator
17021 is a function declarator with a late-specified return type. */
17022 if (abstract_declarator
17023 && abstract_declarator->kind == cdk_function
17024 && abstract_declarator->u.function.late_return_type)
17025 /* OK */;
17026 else
17028 error ("invalid use of %<auto%>");
17029 return error_mark_node;
17033 return groktypename (&type_specifier_seq, abstract_declarator,
17034 is_template_arg);
17037 static tree cp_parser_type_id (cp_parser *parser)
17039 return cp_parser_type_id_1 (parser, false, false);
17042 static tree cp_parser_template_type_arg (cp_parser *parser)
17044 tree r;
17045 const char *saved_message = parser->type_definition_forbidden_message;
17046 parser->type_definition_forbidden_message
17047 = G_("types may not be defined in template arguments");
17048 r = cp_parser_type_id_1 (parser, true, false);
17049 parser->type_definition_forbidden_message = saved_message;
17050 return r;
17053 static tree cp_parser_trailing_type_id (cp_parser *parser)
17055 return cp_parser_type_id_1 (parser, false, true);
17058 /* Parse a type-specifier-seq.
17060 type-specifier-seq:
17061 type-specifier type-specifier-seq [opt]
17063 GNU extension:
17065 type-specifier-seq:
17066 attributes type-specifier-seq [opt]
17068 If IS_DECLARATION is true, we are at the start of a "condition" or
17069 exception-declaration, so we might be followed by a declarator-id.
17071 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17072 i.e. we've just seen "->".
17074 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17076 static void
17077 cp_parser_type_specifier_seq (cp_parser* parser,
17078 bool is_declaration,
17079 bool is_trailing_return,
17080 cp_decl_specifier_seq *type_specifier_seq)
17082 bool seen_type_specifier = false;
17083 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17084 cp_token *start_token = NULL;
17086 /* Clear the TYPE_SPECIFIER_SEQ. */
17087 clear_decl_specs (type_specifier_seq);
17089 /* In the context of a trailing return type, enum E { } is an
17090 elaborated-type-specifier followed by a function-body, not an
17091 enum-specifier. */
17092 if (is_trailing_return)
17093 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17095 /* Parse the type-specifiers and attributes. */
17096 while (true)
17098 tree type_specifier;
17099 bool is_cv_qualifier;
17101 /* Check for attributes first. */
17102 if (cp_next_tokens_can_be_attribute_p (parser))
17104 type_specifier_seq->attributes =
17105 chainon (type_specifier_seq->attributes,
17106 cp_parser_attributes_opt (parser));
17107 continue;
17110 /* record the token of the beginning of the type specifier seq,
17111 for error reporting purposes*/
17112 if (!start_token)
17113 start_token = cp_lexer_peek_token (parser->lexer);
17115 /* Look for the type-specifier. */
17116 type_specifier = cp_parser_type_specifier (parser,
17117 flags,
17118 type_specifier_seq,
17119 /*is_declaration=*/false,
17120 NULL,
17121 &is_cv_qualifier);
17122 if (!type_specifier)
17124 /* If the first type-specifier could not be found, this is not a
17125 type-specifier-seq at all. */
17126 if (!seen_type_specifier)
17128 cp_parser_error (parser, "expected type-specifier");
17129 type_specifier_seq->type = error_mark_node;
17130 return;
17132 /* If subsequent type-specifiers could not be found, the
17133 type-specifier-seq is complete. */
17134 break;
17137 seen_type_specifier = true;
17138 /* The standard says that a condition can be:
17140 type-specifier-seq declarator = assignment-expression
17142 However, given:
17144 struct S {};
17145 if (int S = ...)
17147 we should treat the "S" as a declarator, not as a
17148 type-specifier. The standard doesn't say that explicitly for
17149 type-specifier-seq, but it does say that for
17150 decl-specifier-seq in an ordinary declaration. Perhaps it
17151 would be clearer just to allow a decl-specifier-seq here, and
17152 then add a semantic restriction that if any decl-specifiers
17153 that are not type-specifiers appear, the program is invalid. */
17154 if (is_declaration && !is_cv_qualifier)
17155 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17159 /* Parse a parameter-declaration-clause.
17161 parameter-declaration-clause:
17162 parameter-declaration-list [opt] ... [opt]
17163 parameter-declaration-list , ...
17165 Returns a representation for the parameter declarations. A return
17166 value of NULL indicates a parameter-declaration-clause consisting
17167 only of an ellipsis. */
17169 static tree
17170 cp_parser_parameter_declaration_clause (cp_parser* parser)
17172 tree parameters;
17173 cp_token *token;
17174 bool ellipsis_p;
17175 bool is_error;
17177 /* Peek at the next token. */
17178 token = cp_lexer_peek_token (parser->lexer);
17179 /* Check for trivial parameter-declaration-clauses. */
17180 if (token->type == CPP_ELLIPSIS)
17182 /* Consume the `...' token. */
17183 cp_lexer_consume_token (parser->lexer);
17184 return NULL_TREE;
17186 else if (token->type == CPP_CLOSE_PAREN)
17187 /* There are no parameters. */
17189 #ifndef NO_IMPLICIT_EXTERN_C
17190 if (in_system_header && current_class_type == NULL
17191 && current_lang_name == lang_name_c)
17192 return NULL_TREE;
17193 else
17194 #endif
17195 return void_list_node;
17197 /* Check for `(void)', too, which is a special case. */
17198 else if (token->keyword == RID_VOID
17199 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17200 == CPP_CLOSE_PAREN))
17202 /* Consume the `void' token. */
17203 cp_lexer_consume_token (parser->lexer);
17204 /* There are no parameters. */
17205 return void_list_node;
17208 /* Parse the parameter-declaration-list. */
17209 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17210 /* If a parse error occurred while parsing the
17211 parameter-declaration-list, then the entire
17212 parameter-declaration-clause is erroneous. */
17213 if (is_error)
17214 return NULL;
17216 /* Peek at the next token. */
17217 token = cp_lexer_peek_token (parser->lexer);
17218 /* If it's a `,', the clause should terminate with an ellipsis. */
17219 if (token->type == CPP_COMMA)
17221 /* Consume the `,'. */
17222 cp_lexer_consume_token (parser->lexer);
17223 /* Expect an ellipsis. */
17224 ellipsis_p
17225 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17227 /* It might also be `...' if the optional trailing `,' was
17228 omitted. */
17229 else if (token->type == CPP_ELLIPSIS)
17231 /* Consume the `...' token. */
17232 cp_lexer_consume_token (parser->lexer);
17233 /* And remember that we saw it. */
17234 ellipsis_p = true;
17236 else
17237 ellipsis_p = false;
17239 /* Finish the parameter list. */
17240 if (!ellipsis_p)
17241 parameters = chainon (parameters, void_list_node);
17243 return parameters;
17246 /* Parse a parameter-declaration-list.
17248 parameter-declaration-list:
17249 parameter-declaration
17250 parameter-declaration-list , parameter-declaration
17252 Returns a representation of the parameter-declaration-list, as for
17253 cp_parser_parameter_declaration_clause. However, the
17254 `void_list_node' is never appended to the list. Upon return,
17255 *IS_ERROR will be true iff an error occurred. */
17257 static tree
17258 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17260 tree parameters = NULL_TREE;
17261 tree *tail = &parameters;
17262 bool saved_in_unbraced_linkage_specification_p;
17263 int index = 0;
17265 /* Assume all will go well. */
17266 *is_error = false;
17267 /* The special considerations that apply to a function within an
17268 unbraced linkage specifications do not apply to the parameters
17269 to the function. */
17270 saved_in_unbraced_linkage_specification_p
17271 = parser->in_unbraced_linkage_specification_p;
17272 parser->in_unbraced_linkage_specification_p = false;
17274 /* Look for more parameters. */
17275 while (true)
17277 cp_parameter_declarator *parameter;
17278 tree decl = error_mark_node;
17279 bool parenthesized_p = false;
17280 /* Parse the parameter. */
17281 parameter
17282 = cp_parser_parameter_declaration (parser,
17283 /*template_parm_p=*/false,
17284 &parenthesized_p);
17286 /* We don't know yet if the enclosing context is deprecated, so wait
17287 and warn in grokparms if appropriate. */
17288 deprecated_state = DEPRECATED_SUPPRESS;
17290 if (parameter)
17291 decl = grokdeclarator (parameter->declarator,
17292 &parameter->decl_specifiers,
17293 PARM,
17294 parameter->default_argument != NULL_TREE,
17295 &parameter->decl_specifiers.attributes);
17297 deprecated_state = DEPRECATED_NORMAL;
17299 /* If a parse error occurred parsing the parameter declaration,
17300 then the entire parameter-declaration-list is erroneous. */
17301 if (decl == error_mark_node)
17303 *is_error = true;
17304 parameters = error_mark_node;
17305 break;
17308 if (parameter->decl_specifiers.attributes)
17309 cplus_decl_attributes (&decl,
17310 parameter->decl_specifiers.attributes,
17312 if (DECL_NAME (decl))
17313 decl = pushdecl (decl);
17315 if (decl != error_mark_node)
17317 retrofit_lang_decl (decl);
17318 DECL_PARM_INDEX (decl) = ++index;
17319 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17322 /* Add the new parameter to the list. */
17323 *tail = build_tree_list (parameter->default_argument, decl);
17324 tail = &TREE_CHAIN (*tail);
17326 /* Peek at the next token. */
17327 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17328 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17329 /* These are for Objective-C++ */
17330 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17331 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17332 /* The parameter-declaration-list is complete. */
17333 break;
17334 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17336 cp_token *token;
17338 /* Peek at the next token. */
17339 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17340 /* If it's an ellipsis, then the list is complete. */
17341 if (token->type == CPP_ELLIPSIS)
17342 break;
17343 /* Otherwise, there must be more parameters. Consume the
17344 `,'. */
17345 cp_lexer_consume_token (parser->lexer);
17346 /* When parsing something like:
17348 int i(float f, double d)
17350 we can tell after seeing the declaration for "f" that we
17351 are not looking at an initialization of a variable "i",
17352 but rather at the declaration of a function "i".
17354 Due to the fact that the parsing of template arguments
17355 (as specified to a template-id) requires backtracking we
17356 cannot use this technique when inside a template argument
17357 list. */
17358 if (!parser->in_template_argument_list_p
17359 && !parser->in_type_id_in_expr_p
17360 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17361 /* However, a parameter-declaration of the form
17362 "foat(f)" (which is a valid declaration of a
17363 parameter "f") can also be interpreted as an
17364 expression (the conversion of "f" to "float"). */
17365 && !parenthesized_p)
17366 cp_parser_commit_to_tentative_parse (parser);
17368 else
17370 cp_parser_error (parser, "expected %<,%> or %<...%>");
17371 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17372 cp_parser_skip_to_closing_parenthesis (parser,
17373 /*recovering=*/true,
17374 /*or_comma=*/false,
17375 /*consume_paren=*/false);
17376 break;
17380 parser->in_unbraced_linkage_specification_p
17381 = saved_in_unbraced_linkage_specification_p;
17383 return parameters;
17386 /* Parse a parameter declaration.
17388 parameter-declaration:
17389 decl-specifier-seq ... [opt] declarator
17390 decl-specifier-seq declarator = assignment-expression
17391 decl-specifier-seq ... [opt] abstract-declarator [opt]
17392 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17394 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17395 declares a template parameter. (In that case, a non-nested `>'
17396 token encountered during the parsing of the assignment-expression
17397 is not interpreted as a greater-than operator.)
17399 Returns a representation of the parameter, or NULL if an error
17400 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17401 true iff the declarator is of the form "(p)". */
17403 static cp_parameter_declarator *
17404 cp_parser_parameter_declaration (cp_parser *parser,
17405 bool template_parm_p,
17406 bool *parenthesized_p)
17408 int declares_class_or_enum;
17409 cp_decl_specifier_seq decl_specifiers;
17410 cp_declarator *declarator;
17411 tree default_argument;
17412 cp_token *token = NULL, *declarator_token_start = NULL;
17413 const char *saved_message;
17415 /* In a template parameter, `>' is not an operator.
17417 [temp.param]
17419 When parsing a default template-argument for a non-type
17420 template-parameter, the first non-nested `>' is taken as the end
17421 of the template parameter-list rather than a greater-than
17422 operator. */
17424 /* Type definitions may not appear in parameter types. */
17425 saved_message = parser->type_definition_forbidden_message;
17426 parser->type_definition_forbidden_message
17427 = G_("types may not be defined in parameter types");
17429 /* Parse the declaration-specifiers. */
17430 cp_parser_decl_specifier_seq (parser,
17431 CP_PARSER_FLAGS_NONE,
17432 &decl_specifiers,
17433 &declares_class_or_enum);
17435 /* Complain about missing 'typename' or other invalid type names. */
17436 if (!decl_specifiers.any_type_specifiers_p)
17437 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17439 /* If an error occurred, there's no reason to attempt to parse the
17440 rest of the declaration. */
17441 if (cp_parser_error_occurred (parser))
17443 parser->type_definition_forbidden_message = saved_message;
17444 return NULL;
17447 /* Peek at the next token. */
17448 token = cp_lexer_peek_token (parser->lexer);
17450 /* If the next token is a `)', `,', `=', `>', or `...', then there
17451 is no declarator. However, when variadic templates are enabled,
17452 there may be a declarator following `...'. */
17453 if (token->type == CPP_CLOSE_PAREN
17454 || token->type == CPP_COMMA
17455 || token->type == CPP_EQ
17456 || token->type == CPP_GREATER)
17458 declarator = NULL;
17459 if (parenthesized_p)
17460 *parenthesized_p = false;
17462 /* Otherwise, there should be a declarator. */
17463 else
17465 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17466 parser->default_arg_ok_p = false;
17468 /* After seeing a decl-specifier-seq, if the next token is not a
17469 "(", there is no possibility that the code is a valid
17470 expression. Therefore, if parsing tentatively, we commit at
17471 this point. */
17472 if (!parser->in_template_argument_list_p
17473 /* In an expression context, having seen:
17475 (int((char ...
17477 we cannot be sure whether we are looking at a
17478 function-type (taking a "char" as a parameter) or a cast
17479 of some object of type "char" to "int". */
17480 && !parser->in_type_id_in_expr_p
17481 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17482 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17483 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17484 cp_parser_commit_to_tentative_parse (parser);
17485 /* Parse the declarator. */
17486 declarator_token_start = token;
17487 declarator = cp_parser_declarator (parser,
17488 CP_PARSER_DECLARATOR_EITHER,
17489 /*ctor_dtor_or_conv_p=*/NULL,
17490 parenthesized_p,
17491 /*member_p=*/false);
17492 parser->default_arg_ok_p = saved_default_arg_ok_p;
17493 /* After the declarator, allow more attributes. */
17494 decl_specifiers.attributes
17495 = chainon (decl_specifiers.attributes,
17496 cp_parser_attributes_opt (parser));
17499 /* If the next token is an ellipsis, and we have not seen a
17500 declarator name, and the type of the declarator contains parameter
17501 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17502 a parameter pack expansion expression. Otherwise, leave the
17503 ellipsis for a C-style variadic function. */
17504 token = cp_lexer_peek_token (parser->lexer);
17505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17507 tree type = decl_specifiers.type;
17509 if (type && DECL_P (type))
17510 type = TREE_TYPE (type);
17512 if (type
17513 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17514 && declarator_can_be_parameter_pack (declarator)
17515 && (!declarator || !declarator->parameter_pack_p)
17516 && uses_parameter_packs (type))
17518 /* Consume the `...'. */
17519 cp_lexer_consume_token (parser->lexer);
17520 maybe_warn_variadic_templates ();
17522 /* Build a pack expansion type */
17523 if (declarator)
17524 declarator->parameter_pack_p = true;
17525 else
17526 decl_specifiers.type = make_pack_expansion (type);
17530 /* The restriction on defining new types applies only to the type
17531 of the parameter, not to the default argument. */
17532 parser->type_definition_forbidden_message = saved_message;
17534 /* If the next token is `=', then process a default argument. */
17535 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17537 token = cp_lexer_peek_token (parser->lexer);
17538 /* If we are defining a class, then the tokens that make up the
17539 default argument must be saved and processed later. */
17540 if (!template_parm_p && at_class_scope_p ()
17541 && TYPE_BEING_DEFINED (current_class_type)
17542 && !LAMBDA_TYPE_P (current_class_type))
17543 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17544 /* Outside of a class definition, we can just parse the
17545 assignment-expression. */
17546 else
17547 default_argument
17548 = cp_parser_default_argument (parser, template_parm_p);
17550 if (!parser->default_arg_ok_p)
17552 if (flag_permissive)
17553 warning (0, "deprecated use of default argument for parameter of non-function");
17554 else
17556 error_at (token->location,
17557 "default arguments are only "
17558 "permitted for function parameters");
17559 default_argument = NULL_TREE;
17562 else if ((declarator && declarator->parameter_pack_p)
17563 || (decl_specifiers.type
17564 && PACK_EXPANSION_P (decl_specifiers.type)))
17566 /* Find the name of the parameter pack. */
17567 cp_declarator *id_declarator = declarator;
17568 while (id_declarator && id_declarator->kind != cdk_id)
17569 id_declarator = id_declarator->declarator;
17571 if (id_declarator && id_declarator->kind == cdk_id)
17572 error_at (declarator_token_start->location,
17573 template_parm_p
17574 ? G_("template parameter pack %qD "
17575 "cannot have a default argument")
17576 : G_("parameter pack %qD cannot have "
17577 "a default argument"),
17578 id_declarator->u.id.unqualified_name);
17579 else
17580 error_at (declarator_token_start->location,
17581 template_parm_p
17582 ? G_("template parameter pack cannot have "
17583 "a default argument")
17584 : G_("parameter pack cannot have a "
17585 "default argument"));
17587 default_argument = NULL_TREE;
17590 else
17591 default_argument = NULL_TREE;
17593 return make_parameter_declarator (&decl_specifiers,
17594 declarator,
17595 default_argument);
17598 /* Parse a default argument and return it.
17600 TEMPLATE_PARM_P is true if this is a default argument for a
17601 non-type template parameter. */
17602 static tree
17603 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17605 tree default_argument = NULL_TREE;
17606 bool saved_greater_than_is_operator_p;
17607 bool saved_local_variables_forbidden_p;
17608 bool non_constant_p, is_direct_init;
17610 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17611 set correctly. */
17612 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17613 parser->greater_than_is_operator_p = !template_parm_p;
17614 /* Local variable names (and the `this' keyword) may not
17615 appear in a default argument. */
17616 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17617 parser->local_variables_forbidden_p = true;
17618 /* Parse the assignment-expression. */
17619 if (template_parm_p)
17620 push_deferring_access_checks (dk_no_deferred);
17621 default_argument
17622 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17623 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17624 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17625 if (template_parm_p)
17626 pop_deferring_access_checks ();
17627 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17628 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17630 return default_argument;
17633 /* Parse a function-body.
17635 function-body:
17636 compound_statement */
17638 static void
17639 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17641 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17644 /* Parse a ctor-initializer-opt followed by a function-body. Return
17645 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17646 is true we are parsing a function-try-block. */
17648 static bool
17649 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17650 bool in_function_try_block)
17652 tree body, list;
17653 bool ctor_initializer_p;
17654 const bool check_body_p =
17655 DECL_CONSTRUCTOR_P (current_function_decl)
17656 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17657 tree last = NULL;
17659 /* Begin the function body. */
17660 body = begin_function_body ();
17661 /* Parse the optional ctor-initializer. */
17662 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17664 /* If we're parsing a constexpr constructor definition, we need
17665 to check that the constructor body is indeed empty. However,
17666 before we get to cp_parser_function_body lot of junk has been
17667 generated, so we can't just check that we have an empty block.
17668 Rather we take a snapshot of the outermost block, and check whether
17669 cp_parser_function_body changed its state. */
17670 if (check_body_p)
17672 list = cur_stmt_list;
17673 if (STATEMENT_LIST_TAIL (list))
17674 last = STATEMENT_LIST_TAIL (list)->stmt;
17676 /* Parse the function-body. */
17677 cp_parser_function_body (parser, in_function_try_block);
17678 if (check_body_p)
17679 check_constexpr_ctor_body (last, list);
17680 /* Finish the function body. */
17681 finish_function_body (body);
17683 return ctor_initializer_p;
17686 /* Parse an initializer.
17688 initializer:
17689 = initializer-clause
17690 ( expression-list )
17692 Returns an expression representing the initializer. If no
17693 initializer is present, NULL_TREE is returned.
17695 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17696 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17697 set to TRUE if there is no initializer present. If there is an
17698 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17699 is set to true; otherwise it is set to false. */
17701 static tree
17702 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17703 bool* non_constant_p)
17705 cp_token *token;
17706 tree init;
17708 /* Peek at the next token. */
17709 token = cp_lexer_peek_token (parser->lexer);
17711 /* Let our caller know whether or not this initializer was
17712 parenthesized. */
17713 *is_direct_init = (token->type != CPP_EQ);
17714 /* Assume that the initializer is constant. */
17715 *non_constant_p = false;
17717 if (token->type == CPP_EQ)
17719 /* Consume the `='. */
17720 cp_lexer_consume_token (parser->lexer);
17721 /* Parse the initializer-clause. */
17722 init = cp_parser_initializer_clause (parser, non_constant_p);
17724 else if (token->type == CPP_OPEN_PAREN)
17726 VEC(tree,gc) *vec;
17727 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17728 /*cast_p=*/false,
17729 /*allow_expansion_p=*/true,
17730 non_constant_p);
17731 if (vec == NULL)
17732 return error_mark_node;
17733 init = build_tree_list_vec (vec);
17734 release_tree_vector (vec);
17736 else if (token->type == CPP_OPEN_BRACE)
17738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17739 init = cp_parser_braced_list (parser, non_constant_p);
17740 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17742 else
17744 /* Anything else is an error. */
17745 cp_parser_error (parser, "expected initializer");
17746 init = error_mark_node;
17749 return init;
17752 /* Parse an initializer-clause.
17754 initializer-clause:
17755 assignment-expression
17756 braced-init-list
17758 Returns an expression representing the initializer.
17760 If the `assignment-expression' production is used the value
17761 returned is simply a representation for the expression.
17763 Otherwise, calls cp_parser_braced_list. */
17765 static tree
17766 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17768 tree initializer;
17770 /* Assume the expression is constant. */
17771 *non_constant_p = false;
17773 /* If it is not a `{', then we are looking at an
17774 assignment-expression. */
17775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17777 initializer
17778 = cp_parser_constant_expression (parser,
17779 /*allow_non_constant_p=*/true,
17780 non_constant_p);
17782 else
17783 initializer = cp_parser_braced_list (parser, non_constant_p);
17785 return initializer;
17788 /* Parse a brace-enclosed initializer list.
17790 braced-init-list:
17791 { initializer-list , [opt] }
17794 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17795 the elements of the initializer-list (or NULL, if the last
17796 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17797 NULL_TREE. There is no way to detect whether or not the optional
17798 trailing `,' was provided. NON_CONSTANT_P is as for
17799 cp_parser_initializer. */
17801 static tree
17802 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17804 tree initializer;
17806 /* Consume the `{' token. */
17807 cp_lexer_consume_token (parser->lexer);
17808 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17809 initializer = make_node (CONSTRUCTOR);
17810 /* If it's not a `}', then there is a non-trivial initializer. */
17811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17813 /* Parse the initializer list. */
17814 CONSTRUCTOR_ELTS (initializer)
17815 = cp_parser_initializer_list (parser, non_constant_p);
17816 /* A trailing `,' token is allowed. */
17817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17818 cp_lexer_consume_token (parser->lexer);
17820 /* Now, there should be a trailing `}'. */
17821 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17822 TREE_TYPE (initializer) = init_list_type_node;
17823 return initializer;
17826 /* Parse an initializer-list.
17828 initializer-list:
17829 initializer-clause ... [opt]
17830 initializer-list , initializer-clause ... [opt]
17832 GNU Extension:
17834 initializer-list:
17835 designation initializer-clause ...[opt]
17836 initializer-list , designation initializer-clause ...[opt]
17838 designation:
17839 . identifier =
17840 identifier :
17841 [ constant-expression ] =
17843 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17844 for the initializer. If the INDEX of the elt is non-NULL, it is the
17845 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17846 as for cp_parser_initializer. */
17848 static VEC(constructor_elt,gc) *
17849 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17851 VEC(constructor_elt,gc) *v = NULL;
17853 /* Assume all of the expressions are constant. */
17854 *non_constant_p = false;
17856 /* Parse the rest of the list. */
17857 while (true)
17859 cp_token *token;
17860 tree designator;
17861 tree initializer;
17862 bool clause_non_constant_p;
17864 /* If the next token is an identifier and the following one is a
17865 colon, we are looking at the GNU designated-initializer
17866 syntax. */
17867 if (cp_parser_allow_gnu_extensions_p (parser)
17868 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17869 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17871 /* Warn the user that they are using an extension. */
17872 pedwarn (input_location, OPT_Wpedantic,
17873 "ISO C++ does not allow designated initializers");
17874 /* Consume the identifier. */
17875 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17876 /* Consume the `:'. */
17877 cp_lexer_consume_token (parser->lexer);
17879 /* Also handle the C99 syntax, '. id ='. */
17880 else if (cp_parser_allow_gnu_extensions_p (parser)
17881 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17882 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17883 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17885 /* Warn the user that they are using an extension. */
17886 pedwarn (input_location, OPT_Wpedantic,
17887 "ISO C++ does not allow C99 designated initializers");
17888 /* Consume the `.'. */
17889 cp_lexer_consume_token (parser->lexer);
17890 /* Consume the identifier. */
17891 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17892 /* Consume the `='. */
17893 cp_lexer_consume_token (parser->lexer);
17895 /* Also handle C99 array designators, '[ const ] ='. */
17896 else if (cp_parser_allow_gnu_extensions_p (parser)
17897 && !c_dialect_objc ()
17898 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17900 /* In C++11, [ could start a lambda-introducer. */
17901 cp_parser_parse_tentatively (parser);
17902 cp_lexer_consume_token (parser->lexer);
17903 designator = cp_parser_constant_expression (parser, false, NULL);
17904 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17905 cp_parser_require (parser, CPP_EQ, RT_EQ);
17906 if (!cp_parser_parse_definitely (parser))
17907 designator = NULL_TREE;
17909 else
17910 designator = NULL_TREE;
17912 /* Parse the initializer. */
17913 initializer = cp_parser_initializer_clause (parser,
17914 &clause_non_constant_p);
17915 /* If any clause is non-constant, so is the entire initializer. */
17916 if (clause_non_constant_p)
17917 *non_constant_p = true;
17919 /* If we have an ellipsis, this is an initializer pack
17920 expansion. */
17921 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17923 /* Consume the `...'. */
17924 cp_lexer_consume_token (parser->lexer);
17926 /* Turn the initializer into an initializer expansion. */
17927 initializer = make_pack_expansion (initializer);
17930 /* Add it to the vector. */
17931 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17933 /* If the next token is not a comma, we have reached the end of
17934 the list. */
17935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17936 break;
17938 /* Peek at the next token. */
17939 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17940 /* If the next token is a `}', then we're still done. An
17941 initializer-clause can have a trailing `,' after the
17942 initializer-list and before the closing `}'. */
17943 if (token->type == CPP_CLOSE_BRACE)
17944 break;
17946 /* Consume the `,' token. */
17947 cp_lexer_consume_token (parser->lexer);
17950 return v;
17953 /* Classes [gram.class] */
17955 /* Parse a class-name.
17957 class-name:
17958 identifier
17959 template-id
17961 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17962 to indicate that names looked up in dependent types should be
17963 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17964 keyword has been used to indicate that the name that appears next
17965 is a template. TAG_TYPE indicates the explicit tag given before
17966 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17967 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17968 is the class being defined in a class-head.
17970 Returns the TYPE_DECL representing the class. */
17972 static tree
17973 cp_parser_class_name (cp_parser *parser,
17974 bool typename_keyword_p,
17975 bool template_keyword_p,
17976 enum tag_types tag_type,
17977 bool check_dependency_p,
17978 bool class_head_p,
17979 bool is_declaration)
17981 tree decl;
17982 tree scope;
17983 bool typename_p;
17984 cp_token *token;
17985 tree identifier = NULL_TREE;
17987 /* All class-names start with an identifier. */
17988 token = cp_lexer_peek_token (parser->lexer);
17989 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17991 cp_parser_error (parser, "expected class-name");
17992 return error_mark_node;
17995 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17996 to a template-id, so we save it here. */
17997 scope = parser->scope;
17998 if (scope == error_mark_node)
17999 return error_mark_node;
18001 /* Any name names a type if we're following the `typename' keyword
18002 in a qualified name where the enclosing scope is type-dependent. */
18003 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18004 && dependent_type_p (scope));
18005 /* Handle the common case (an identifier, but not a template-id)
18006 efficiently. */
18007 if (token->type == CPP_NAME
18008 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18010 cp_token *identifier_token;
18011 bool ambiguous_p;
18013 /* Look for the identifier. */
18014 identifier_token = cp_lexer_peek_token (parser->lexer);
18015 ambiguous_p = identifier_token->ambiguous_p;
18016 identifier = cp_parser_identifier (parser);
18017 /* If the next token isn't an identifier, we are certainly not
18018 looking at a class-name. */
18019 if (identifier == error_mark_node)
18020 decl = error_mark_node;
18021 /* If we know this is a type-name, there's no need to look it
18022 up. */
18023 else if (typename_p)
18024 decl = identifier;
18025 else
18027 tree ambiguous_decls;
18028 /* If we already know that this lookup is ambiguous, then
18029 we've already issued an error message; there's no reason
18030 to check again. */
18031 if (ambiguous_p)
18033 cp_parser_simulate_error (parser);
18034 return error_mark_node;
18036 /* If the next token is a `::', then the name must be a type
18037 name.
18039 [basic.lookup.qual]
18041 During the lookup for a name preceding the :: scope
18042 resolution operator, object, function, and enumerator
18043 names are ignored. */
18044 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18045 tag_type = typename_type;
18046 /* Look up the name. */
18047 decl = cp_parser_lookup_name (parser, identifier,
18048 tag_type,
18049 /*is_template=*/false,
18050 /*is_namespace=*/false,
18051 check_dependency_p,
18052 &ambiguous_decls,
18053 identifier_token->location);
18054 if (ambiguous_decls)
18056 if (cp_parser_parsing_tentatively (parser))
18057 cp_parser_simulate_error (parser);
18058 return error_mark_node;
18062 else
18064 /* Try a template-id. */
18065 decl = cp_parser_template_id (parser, template_keyword_p,
18066 check_dependency_p,
18067 tag_type,
18068 is_declaration);
18069 if (decl == error_mark_node)
18070 return error_mark_node;
18073 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18075 /* If this is a typename, create a TYPENAME_TYPE. */
18076 if (typename_p && decl != error_mark_node)
18078 decl = make_typename_type (scope, decl, typename_type,
18079 /*complain=*/tf_error);
18080 if (decl != error_mark_node)
18081 decl = TYPE_NAME (decl);
18084 decl = strip_using_decl (decl);
18086 /* Check to see that it is really the name of a class. */
18087 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18088 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18089 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18090 /* Situations like this:
18092 template <typename T> struct A {
18093 typename T::template X<int>::I i;
18096 are problematic. Is `T::template X<int>' a class-name? The
18097 standard does not seem to be definitive, but there is no other
18098 valid interpretation of the following `::'. Therefore, those
18099 names are considered class-names. */
18101 decl = make_typename_type (scope, decl, tag_type, tf_error);
18102 if (decl != error_mark_node)
18103 decl = TYPE_NAME (decl);
18105 else if (TREE_CODE (decl) != TYPE_DECL
18106 || TREE_TYPE (decl) == error_mark_node
18107 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18108 /* In Objective-C 2.0, a classname followed by '.' starts a
18109 dot-syntax expression, and it's not a type-name. */
18110 || (c_dialect_objc ()
18111 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18112 && objc_is_class_name (decl)))
18113 decl = error_mark_node;
18115 if (decl == error_mark_node)
18116 cp_parser_error (parser, "expected class-name");
18117 else if (identifier && !parser->scope)
18118 maybe_note_name_used_in_class (identifier, decl);
18120 return decl;
18123 /* Parse a class-specifier.
18125 class-specifier:
18126 class-head { member-specification [opt] }
18128 Returns the TREE_TYPE representing the class. */
18130 static tree
18131 cp_parser_class_specifier_1 (cp_parser* parser)
18133 tree type;
18134 tree attributes = NULL_TREE;
18135 bool nested_name_specifier_p;
18136 unsigned saved_num_template_parameter_lists;
18137 bool saved_in_function_body;
18138 unsigned char in_statement;
18139 bool in_switch_statement_p;
18140 bool saved_in_unbraced_linkage_specification_p;
18141 tree old_scope = NULL_TREE;
18142 tree scope = NULL_TREE;
18143 cp_token *closing_brace;
18145 push_deferring_access_checks (dk_no_deferred);
18147 /* Parse the class-head. */
18148 type = cp_parser_class_head (parser,
18149 &nested_name_specifier_p);
18150 /* If the class-head was a semantic disaster, skip the entire body
18151 of the class. */
18152 if (!type)
18154 cp_parser_skip_to_end_of_block_or_statement (parser);
18155 pop_deferring_access_checks ();
18156 return error_mark_node;
18159 /* Look for the `{'. */
18160 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18162 pop_deferring_access_checks ();
18163 return error_mark_node;
18166 /* Issue an error message if type-definitions are forbidden here. */
18167 cp_parser_check_type_definition (parser);
18168 /* Remember that we are defining one more class. */
18169 ++parser->num_classes_being_defined;
18170 /* Inside the class, surrounding template-parameter-lists do not
18171 apply. */
18172 saved_num_template_parameter_lists
18173 = parser->num_template_parameter_lists;
18174 parser->num_template_parameter_lists = 0;
18175 /* We are not in a function body. */
18176 saved_in_function_body = parser->in_function_body;
18177 parser->in_function_body = false;
18178 /* Or in a loop. */
18179 in_statement = parser->in_statement;
18180 parser->in_statement = 0;
18181 /* Or in a switch. */
18182 in_switch_statement_p = parser->in_switch_statement_p;
18183 parser->in_switch_statement_p = false;
18184 /* We are not immediately inside an extern "lang" block. */
18185 saved_in_unbraced_linkage_specification_p
18186 = parser->in_unbraced_linkage_specification_p;
18187 parser->in_unbraced_linkage_specification_p = false;
18189 /* Start the class. */
18190 if (nested_name_specifier_p)
18192 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18193 old_scope = push_inner_scope (scope);
18195 type = begin_class_definition (type);
18197 if (type == error_mark_node)
18198 /* If the type is erroneous, skip the entire body of the class. */
18199 cp_parser_skip_to_closing_brace (parser);
18200 else
18201 /* Parse the member-specification. */
18202 cp_parser_member_specification_opt (parser);
18204 /* Look for the trailing `}'. */
18205 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18206 /* Look for trailing attributes to apply to this class. */
18207 if (cp_parser_allow_gnu_extensions_p (parser))
18208 attributes = cp_parser_gnu_attributes_opt (parser);
18209 if (type != error_mark_node)
18210 type = finish_struct (type, attributes);
18211 if (nested_name_specifier_p)
18212 pop_inner_scope (old_scope, scope);
18214 /* We've finished a type definition. Check for the common syntax
18215 error of forgetting a semicolon after the definition. We need to
18216 be careful, as we can't just check for not-a-semicolon and be done
18217 with it; the user might have typed:
18219 class X { } c = ...;
18220 class X { } *p = ...;
18222 and so forth. Instead, enumerate all the possible tokens that
18223 might follow this production; if we don't see one of them, then
18224 complain and silently insert the semicolon. */
18226 cp_token *token = cp_lexer_peek_token (parser->lexer);
18227 bool want_semicolon = true;
18229 if (cp_next_tokens_can_be_std_attribute_p (parser))
18230 /* Don't try to parse c++11 attributes here. As per the
18231 grammar, that should be a task for
18232 cp_parser_decl_specifier_seq. */
18233 want_semicolon = false;
18235 switch (token->type)
18237 case CPP_NAME:
18238 case CPP_SEMICOLON:
18239 case CPP_MULT:
18240 case CPP_AND:
18241 case CPP_OPEN_PAREN:
18242 case CPP_CLOSE_PAREN:
18243 case CPP_COMMA:
18244 want_semicolon = false;
18245 break;
18247 /* While it's legal for type qualifiers and storage class
18248 specifiers to follow type definitions in the grammar, only
18249 compiler testsuites contain code like that. Assume that if
18250 we see such code, then what we're really seeing is a case
18251 like:
18253 class X { }
18254 const <type> var = ...;
18258 class Y { }
18259 static <type> func (...) ...
18261 i.e. the qualifier or specifier applies to the next
18262 declaration. To do so, however, we need to look ahead one
18263 more token to see if *that* token is a type specifier.
18265 This code could be improved to handle:
18267 class Z { }
18268 static const <type> var = ...; */
18269 case CPP_KEYWORD:
18270 if (keyword_is_decl_specifier (token->keyword))
18272 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18274 /* Handling user-defined types here would be nice, but very
18275 tricky. */
18276 want_semicolon
18277 = (lookahead->type == CPP_KEYWORD
18278 && keyword_begins_type_specifier (lookahead->keyword));
18280 break;
18281 default:
18282 break;
18285 /* If we don't have a type, then something is very wrong and we
18286 shouldn't try to do anything clever. Likewise for not seeing the
18287 closing brace. */
18288 if (closing_brace && TYPE_P (type) && want_semicolon)
18290 cp_token_position prev
18291 = cp_lexer_previous_token_position (parser->lexer);
18292 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18293 location_t loc = prev_token->location;
18295 if (CLASSTYPE_DECLARED_CLASS (type))
18296 error_at (loc, "expected %<;%> after class definition");
18297 else if (TREE_CODE (type) == RECORD_TYPE)
18298 error_at (loc, "expected %<;%> after struct definition");
18299 else if (TREE_CODE (type) == UNION_TYPE)
18300 error_at (loc, "expected %<;%> after union definition");
18301 else
18302 gcc_unreachable ();
18304 /* Unget one token and smash it to look as though we encountered
18305 a semicolon in the input stream. */
18306 cp_lexer_set_token_position (parser->lexer, prev);
18307 token = cp_lexer_peek_token (parser->lexer);
18308 token->type = CPP_SEMICOLON;
18309 token->keyword = RID_MAX;
18313 /* If this class is not itself within the scope of another class,
18314 then we need to parse the bodies of all of the queued function
18315 definitions. Note that the queued functions defined in a class
18316 are not always processed immediately following the
18317 class-specifier for that class. Consider:
18319 struct A {
18320 struct B { void f() { sizeof (A); } };
18323 If `f' were processed before the processing of `A' were
18324 completed, there would be no way to compute the size of `A'.
18325 Note that the nesting we are interested in here is lexical --
18326 not the semantic nesting given by TYPE_CONTEXT. In particular,
18327 for:
18329 struct A { struct B; };
18330 struct A::B { void f() { } };
18332 there is no need to delay the parsing of `A::B::f'. */
18333 if (--parser->num_classes_being_defined == 0)
18335 tree decl;
18336 tree class_type = NULL_TREE;
18337 tree pushed_scope = NULL_TREE;
18338 unsigned ix;
18339 cp_default_arg_entry *e;
18340 tree save_ccp, save_ccr;
18342 /* In a first pass, parse default arguments to the functions.
18343 Then, in a second pass, parse the bodies of the functions.
18344 This two-phased approach handles cases like:
18346 struct S {
18347 void f() { g(); }
18348 void g(int i = 3);
18352 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18353 ix, e)
18355 decl = e->decl;
18356 /* If there are default arguments that have not yet been processed,
18357 take care of them now. */
18358 if (class_type != e->class_type)
18360 if (pushed_scope)
18361 pop_scope (pushed_scope);
18362 class_type = e->class_type;
18363 pushed_scope = push_scope (class_type);
18365 /* Make sure that any template parameters are in scope. */
18366 maybe_begin_member_template_processing (decl);
18367 /* Parse the default argument expressions. */
18368 cp_parser_late_parsing_default_args (parser, decl);
18369 /* Remove any template parameters from the symbol table. */
18370 maybe_end_member_template_processing ();
18372 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18373 /* Now parse any NSDMIs. */
18374 save_ccp = current_class_ptr;
18375 save_ccr = current_class_ref;
18376 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18378 if (class_type != DECL_CONTEXT (decl))
18380 if (pushed_scope)
18381 pop_scope (pushed_scope);
18382 class_type = DECL_CONTEXT (decl);
18383 pushed_scope = push_scope (class_type);
18385 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18386 cp_parser_late_parsing_nsdmi (parser, decl);
18388 VEC_truncate (tree, unparsed_nsdmis, 0);
18389 current_class_ptr = save_ccp;
18390 current_class_ref = save_ccr;
18391 if (pushed_scope)
18392 pop_scope (pushed_scope);
18393 /* Now parse the body of the functions. */
18394 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18395 cp_parser_late_parsing_for_member (parser, decl);
18396 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18399 /* Put back any saved access checks. */
18400 pop_deferring_access_checks ();
18402 /* Restore saved state. */
18403 parser->in_switch_statement_p = in_switch_statement_p;
18404 parser->in_statement = in_statement;
18405 parser->in_function_body = saved_in_function_body;
18406 parser->num_template_parameter_lists
18407 = saved_num_template_parameter_lists;
18408 parser->in_unbraced_linkage_specification_p
18409 = saved_in_unbraced_linkage_specification_p;
18411 return type;
18414 static tree
18415 cp_parser_class_specifier (cp_parser* parser)
18417 tree ret;
18418 timevar_push (TV_PARSE_STRUCT);
18419 ret = cp_parser_class_specifier_1 (parser);
18420 timevar_pop (TV_PARSE_STRUCT);
18421 return ret;
18424 /* Parse a class-head.
18426 class-head:
18427 class-key identifier [opt] base-clause [opt]
18428 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18429 class-key nested-name-specifier [opt] template-id
18430 base-clause [opt]
18432 class-virt-specifier:
18433 final
18435 GNU Extensions:
18436 class-key attributes identifier [opt] base-clause [opt]
18437 class-key attributes nested-name-specifier identifier base-clause [opt]
18438 class-key attributes nested-name-specifier [opt] template-id
18439 base-clause [opt]
18441 Upon return BASES is initialized to the list of base classes (or
18442 NULL, if there are none) in the same form returned by
18443 cp_parser_base_clause.
18445 Returns the TYPE of the indicated class. Sets
18446 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18447 involving a nested-name-specifier was used, and FALSE otherwise.
18449 Returns error_mark_node if this is not a class-head.
18451 Returns NULL_TREE if the class-head is syntactically valid, but
18452 semantically invalid in a way that means we should skip the entire
18453 body of the class. */
18455 static tree
18456 cp_parser_class_head (cp_parser* parser,
18457 bool* nested_name_specifier_p)
18459 tree nested_name_specifier;
18460 enum tag_types class_key;
18461 tree id = NULL_TREE;
18462 tree type = NULL_TREE;
18463 tree attributes;
18464 tree bases;
18465 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18466 bool template_id_p = false;
18467 bool qualified_p = false;
18468 bool invalid_nested_name_p = false;
18469 bool invalid_explicit_specialization_p = false;
18470 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18471 tree pushed_scope = NULL_TREE;
18472 unsigned num_templates;
18473 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18474 /* Assume no nested-name-specifier will be present. */
18475 *nested_name_specifier_p = false;
18476 /* Assume no template parameter lists will be used in defining the
18477 type. */
18478 num_templates = 0;
18479 parser->colon_corrects_to_scope_p = false;
18481 /* Look for the class-key. */
18482 class_key = cp_parser_class_key (parser);
18483 if (class_key == none_type)
18484 return error_mark_node;
18486 /* Parse the attributes. */
18487 attributes = cp_parser_attributes_opt (parser);
18489 /* If the next token is `::', that is invalid -- but sometimes
18490 people do try to write:
18492 struct ::S {};
18494 Handle this gracefully by accepting the extra qualifier, and then
18495 issuing an error about it later if this really is a
18496 class-head. If it turns out just to be an elaborated type
18497 specifier, remain silent. */
18498 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18499 qualified_p = true;
18501 push_deferring_access_checks (dk_no_check);
18503 /* Determine the name of the class. Begin by looking for an
18504 optional nested-name-specifier. */
18505 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18506 nested_name_specifier
18507 = cp_parser_nested_name_specifier_opt (parser,
18508 /*typename_keyword_p=*/false,
18509 /*check_dependency_p=*/false,
18510 /*type_p=*/true,
18511 /*is_declaration=*/false);
18512 /* If there was a nested-name-specifier, then there *must* be an
18513 identifier. */
18514 if (nested_name_specifier)
18516 type_start_token = cp_lexer_peek_token (parser->lexer);
18517 /* Although the grammar says `identifier', it really means
18518 `class-name' or `template-name'. You are only allowed to
18519 define a class that has already been declared with this
18520 syntax.
18522 The proposed resolution for Core Issue 180 says that wherever
18523 you see `class T::X' you should treat `X' as a type-name.
18525 It is OK to define an inaccessible class; for example:
18527 class A { class B; };
18528 class A::B {};
18530 We do not know if we will see a class-name, or a
18531 template-name. We look for a class-name first, in case the
18532 class-name is a template-id; if we looked for the
18533 template-name first we would stop after the template-name. */
18534 cp_parser_parse_tentatively (parser);
18535 type = cp_parser_class_name (parser,
18536 /*typename_keyword_p=*/false,
18537 /*template_keyword_p=*/false,
18538 class_type,
18539 /*check_dependency_p=*/false,
18540 /*class_head_p=*/true,
18541 /*is_declaration=*/false);
18542 /* If that didn't work, ignore the nested-name-specifier. */
18543 if (!cp_parser_parse_definitely (parser))
18545 invalid_nested_name_p = true;
18546 type_start_token = cp_lexer_peek_token (parser->lexer);
18547 id = cp_parser_identifier (parser);
18548 if (id == error_mark_node)
18549 id = NULL_TREE;
18551 /* If we could not find a corresponding TYPE, treat this
18552 declaration like an unqualified declaration. */
18553 if (type == error_mark_node)
18554 nested_name_specifier = NULL_TREE;
18555 /* Otherwise, count the number of templates used in TYPE and its
18556 containing scopes. */
18557 else
18559 tree scope;
18561 for (scope = TREE_TYPE (type);
18562 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18563 scope = (TYPE_P (scope)
18564 ? TYPE_CONTEXT (scope)
18565 : DECL_CONTEXT (scope)))
18566 if (TYPE_P (scope)
18567 && CLASS_TYPE_P (scope)
18568 && CLASSTYPE_TEMPLATE_INFO (scope)
18569 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18570 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18571 ++num_templates;
18574 /* Otherwise, the identifier is optional. */
18575 else
18577 /* We don't know whether what comes next is a template-id,
18578 an identifier, or nothing at all. */
18579 cp_parser_parse_tentatively (parser);
18580 /* Check for a template-id. */
18581 type_start_token = cp_lexer_peek_token (parser->lexer);
18582 id = cp_parser_template_id (parser,
18583 /*template_keyword_p=*/false,
18584 /*check_dependency_p=*/true,
18585 class_key,
18586 /*is_declaration=*/true);
18587 /* If that didn't work, it could still be an identifier. */
18588 if (!cp_parser_parse_definitely (parser))
18590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18592 type_start_token = cp_lexer_peek_token (parser->lexer);
18593 id = cp_parser_identifier (parser);
18595 else
18596 id = NULL_TREE;
18598 else
18600 template_id_p = true;
18601 ++num_templates;
18605 pop_deferring_access_checks ();
18607 if (id)
18609 cp_parser_check_for_invalid_template_id (parser, id,
18610 class_key,
18611 type_start_token->location);
18613 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18615 /* If it's not a `:' or a `{' then we can't really be looking at a
18616 class-head, since a class-head only appears as part of a
18617 class-specifier. We have to detect this situation before calling
18618 xref_tag, since that has irreversible side-effects. */
18619 if (!cp_parser_next_token_starts_class_definition_p (parser))
18621 cp_parser_error (parser, "expected %<{%> or %<:%>");
18622 type = error_mark_node;
18623 goto out;
18626 /* At this point, we're going ahead with the class-specifier, even
18627 if some other problem occurs. */
18628 cp_parser_commit_to_tentative_parse (parser);
18629 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18631 cp_parser_error (parser,
18632 "cannot specify %<override%> for a class");
18633 type = error_mark_node;
18634 goto out;
18636 /* Issue the error about the overly-qualified name now. */
18637 if (qualified_p)
18639 cp_parser_error (parser,
18640 "global qualification of class name is invalid");
18641 type = error_mark_node;
18642 goto out;
18644 else if (invalid_nested_name_p)
18646 cp_parser_error (parser,
18647 "qualified name does not name a class");
18648 type = error_mark_node;
18649 goto out;
18651 else if (nested_name_specifier)
18653 tree scope;
18655 /* Reject typedef-names in class heads. */
18656 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18658 error_at (type_start_token->location,
18659 "invalid class name in declaration of %qD",
18660 type);
18661 type = NULL_TREE;
18662 goto done;
18665 /* Figure out in what scope the declaration is being placed. */
18666 scope = current_scope ();
18667 /* If that scope does not contain the scope in which the
18668 class was originally declared, the program is invalid. */
18669 if (scope && !is_ancestor (scope, nested_name_specifier))
18671 if (at_namespace_scope_p ())
18672 error_at (type_start_token->location,
18673 "declaration of %qD in namespace %qD which does not "
18674 "enclose %qD",
18675 type, scope, nested_name_specifier);
18676 else
18677 error_at (type_start_token->location,
18678 "declaration of %qD in %qD which does not enclose %qD",
18679 type, scope, nested_name_specifier);
18680 type = NULL_TREE;
18681 goto done;
18683 /* [dcl.meaning]
18685 A declarator-id shall not be qualified except for the
18686 definition of a ... nested class outside of its class
18687 ... [or] the definition or explicit instantiation of a
18688 class member of a namespace outside of its namespace. */
18689 if (scope == nested_name_specifier)
18691 permerror (nested_name_specifier_token_start->location,
18692 "extra qualification not allowed");
18693 nested_name_specifier = NULL_TREE;
18694 num_templates = 0;
18697 /* An explicit-specialization must be preceded by "template <>". If
18698 it is not, try to recover gracefully. */
18699 if (at_namespace_scope_p ()
18700 && parser->num_template_parameter_lists == 0
18701 && template_id_p)
18703 error_at (type_start_token->location,
18704 "an explicit specialization must be preceded by %<template <>%>");
18705 invalid_explicit_specialization_p = true;
18706 /* Take the same action that would have been taken by
18707 cp_parser_explicit_specialization. */
18708 ++parser->num_template_parameter_lists;
18709 begin_specialization ();
18711 /* There must be no "return" statements between this point and the
18712 end of this function; set "type "to the correct return value and
18713 use "goto done;" to return. */
18714 /* Make sure that the right number of template parameters were
18715 present. */
18716 if (!cp_parser_check_template_parameters (parser, num_templates,
18717 type_start_token->location,
18718 /*declarator=*/NULL))
18720 /* If something went wrong, there is no point in even trying to
18721 process the class-definition. */
18722 type = NULL_TREE;
18723 goto done;
18726 /* Look up the type. */
18727 if (template_id_p)
18729 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18730 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18731 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18733 error_at (type_start_token->location,
18734 "function template %qD redeclared as a class template", id);
18735 type = error_mark_node;
18737 else
18739 type = TREE_TYPE (id);
18740 type = maybe_process_partial_specialization (type);
18742 if (nested_name_specifier)
18743 pushed_scope = push_scope (nested_name_specifier);
18745 else if (nested_name_specifier)
18747 tree class_type;
18749 /* Given:
18751 template <typename T> struct S { struct T };
18752 template <typename T> struct S<T>::T { };
18754 we will get a TYPENAME_TYPE when processing the definition of
18755 `S::T'. We need to resolve it to the actual type before we
18756 try to define it. */
18757 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18759 class_type = resolve_typename_type (TREE_TYPE (type),
18760 /*only_current_p=*/false);
18761 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18762 type = TYPE_NAME (class_type);
18763 else
18765 cp_parser_error (parser, "could not resolve typename type");
18766 type = error_mark_node;
18770 if (maybe_process_partial_specialization (TREE_TYPE (type))
18771 == error_mark_node)
18773 type = NULL_TREE;
18774 goto done;
18777 class_type = current_class_type;
18778 /* Enter the scope indicated by the nested-name-specifier. */
18779 pushed_scope = push_scope (nested_name_specifier);
18780 /* Get the canonical version of this type. */
18781 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18782 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18783 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18785 type = push_template_decl (type);
18786 if (type == error_mark_node)
18788 type = NULL_TREE;
18789 goto done;
18793 type = TREE_TYPE (type);
18794 *nested_name_specifier_p = true;
18796 else /* The name is not a nested name. */
18798 /* If the class was unnamed, create a dummy name. */
18799 if (!id)
18800 id = make_anon_name ();
18801 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18802 parser->num_template_parameter_lists);
18805 /* Indicate whether this class was declared as a `class' or as a
18806 `struct'. */
18807 if (TREE_CODE (type) == RECORD_TYPE)
18808 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18809 cp_parser_check_class_key (class_key, type);
18811 /* If this type was already complete, and we see another definition,
18812 that's an error. */
18813 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18815 error_at (type_start_token->location, "redefinition of %q#T",
18816 type);
18817 error_at (type_start_token->location, "previous definition of %q+#T",
18818 type);
18819 type = NULL_TREE;
18820 goto done;
18822 else if (type == error_mark_node)
18823 type = NULL_TREE;
18825 if (type)
18827 /* Apply attributes now, before any use of the class as a template
18828 argument in its base list. */
18829 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18830 fixup_attribute_variants (type);
18833 /* We will have entered the scope containing the class; the names of
18834 base classes should be looked up in that context. For example:
18836 struct A { struct B {}; struct C; };
18837 struct A::C : B {};
18839 is valid. */
18841 /* Get the list of base-classes, if there is one. */
18842 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18843 bases = cp_parser_base_clause (parser);
18844 else
18845 bases = NULL_TREE;
18847 /* If we're really defining a class, process the base classes.
18848 If they're invalid, fail. */
18849 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18850 && !xref_basetypes (type, bases))
18851 type = NULL_TREE;
18853 done:
18854 /* Leave the scope given by the nested-name-specifier. We will
18855 enter the class scope itself while processing the members. */
18856 if (pushed_scope)
18857 pop_scope (pushed_scope);
18859 if (invalid_explicit_specialization_p)
18861 end_specialization ();
18862 --parser->num_template_parameter_lists;
18865 if (type)
18866 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18867 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18868 CLASSTYPE_FINAL (type) = 1;
18869 out:
18870 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18871 return type;
18874 /* Parse a class-key.
18876 class-key:
18877 class
18878 struct
18879 union
18881 Returns the kind of class-key specified, or none_type to indicate
18882 error. */
18884 static enum tag_types
18885 cp_parser_class_key (cp_parser* parser)
18887 cp_token *token;
18888 enum tag_types tag_type;
18890 /* Look for the class-key. */
18891 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18892 if (!token)
18893 return none_type;
18895 /* Check to see if the TOKEN is a class-key. */
18896 tag_type = cp_parser_token_is_class_key (token);
18897 if (!tag_type)
18898 cp_parser_error (parser, "expected class-key");
18899 return tag_type;
18902 /* Parse an (optional) member-specification.
18904 member-specification:
18905 member-declaration member-specification [opt]
18906 access-specifier : member-specification [opt] */
18908 static void
18909 cp_parser_member_specification_opt (cp_parser* parser)
18911 while (true)
18913 cp_token *token;
18914 enum rid keyword;
18916 /* Peek at the next token. */
18917 token = cp_lexer_peek_token (parser->lexer);
18918 /* If it's a `}', or EOF then we've seen all the members. */
18919 if (token->type == CPP_CLOSE_BRACE
18920 || token->type == CPP_EOF
18921 || token->type == CPP_PRAGMA_EOL)
18922 break;
18924 /* See if this token is a keyword. */
18925 keyword = token->keyword;
18926 switch (keyword)
18928 case RID_PUBLIC:
18929 case RID_PROTECTED:
18930 case RID_PRIVATE:
18931 /* Consume the access-specifier. */
18932 cp_lexer_consume_token (parser->lexer);
18933 /* Remember which access-specifier is active. */
18934 current_access_specifier = token->u.value;
18935 /* Look for the `:'. */
18936 cp_parser_require (parser, CPP_COLON, RT_COLON);
18937 break;
18939 default:
18940 /* Accept #pragmas at class scope. */
18941 if (token->type == CPP_PRAGMA)
18943 cp_parser_pragma (parser, pragma_external);
18944 break;
18947 /* Otherwise, the next construction must be a
18948 member-declaration. */
18949 cp_parser_member_declaration (parser);
18954 /* Parse a member-declaration.
18956 member-declaration:
18957 decl-specifier-seq [opt] member-declarator-list [opt] ;
18958 function-definition ; [opt]
18959 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18960 using-declaration
18961 template-declaration
18962 alias-declaration
18964 member-declarator-list:
18965 member-declarator
18966 member-declarator-list , member-declarator
18968 member-declarator:
18969 declarator pure-specifier [opt]
18970 declarator constant-initializer [opt]
18971 identifier [opt] : constant-expression
18973 GNU Extensions:
18975 member-declaration:
18976 __extension__ member-declaration
18978 member-declarator:
18979 declarator attributes [opt] pure-specifier [opt]
18980 declarator attributes [opt] constant-initializer [opt]
18981 identifier [opt] attributes [opt] : constant-expression
18983 C++0x Extensions:
18985 member-declaration:
18986 static_assert-declaration */
18988 static void
18989 cp_parser_member_declaration (cp_parser* parser)
18991 cp_decl_specifier_seq decl_specifiers;
18992 tree prefix_attributes;
18993 tree decl;
18994 int declares_class_or_enum;
18995 bool friend_p;
18996 cp_token *token = NULL;
18997 cp_token *decl_spec_token_start = NULL;
18998 cp_token *initializer_token_start = NULL;
18999 int saved_pedantic;
19000 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19002 /* Check for the `__extension__' keyword. */
19003 if (cp_parser_extension_opt (parser, &saved_pedantic))
19005 /* Recurse. */
19006 cp_parser_member_declaration (parser);
19007 /* Restore the old value of the PEDANTIC flag. */
19008 pedantic = saved_pedantic;
19010 return;
19013 /* Check for a template-declaration. */
19014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19016 /* An explicit specialization here is an error condition, and we
19017 expect the specialization handler to detect and report this. */
19018 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19019 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19020 cp_parser_explicit_specialization (parser);
19021 else
19022 cp_parser_template_declaration (parser, /*member_p=*/true);
19024 return;
19027 /* Check for a using-declaration. */
19028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19030 if (cxx_dialect < cxx0x)
19032 /* Parse the using-declaration. */
19033 cp_parser_using_declaration (parser,
19034 /*access_declaration_p=*/false);
19035 return;
19037 else
19039 tree decl;
19040 cp_parser_parse_tentatively (parser);
19041 decl = cp_parser_alias_declaration (parser);
19042 if (cp_parser_parse_definitely (parser))
19043 finish_member_declaration (decl);
19044 else
19045 cp_parser_using_declaration (parser,
19046 /*access_declaration_p=*/false);
19047 return;
19051 /* Check for @defs. */
19052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19054 tree ivar, member;
19055 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19056 ivar = ivar_chains;
19057 while (ivar)
19059 member = ivar;
19060 ivar = TREE_CHAIN (member);
19061 TREE_CHAIN (member) = NULL_TREE;
19062 finish_member_declaration (member);
19064 return;
19067 /* If the next token is `static_assert' we have a static assertion. */
19068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19070 cp_parser_static_assert (parser, /*member_p=*/true);
19071 return;
19074 parser->colon_corrects_to_scope_p = false;
19076 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19077 goto out;
19079 /* Parse the decl-specifier-seq. */
19080 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19081 cp_parser_decl_specifier_seq (parser,
19082 CP_PARSER_FLAGS_OPTIONAL,
19083 &decl_specifiers,
19084 &declares_class_or_enum);
19085 /* Check for an invalid type-name. */
19086 if (!decl_specifiers.any_type_specifiers_p
19087 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19088 goto out;
19089 /* If there is no declarator, then the decl-specifier-seq should
19090 specify a type. */
19091 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19093 /* If there was no decl-specifier-seq, and the next token is a
19094 `;', then we have something like:
19096 struct S { ; };
19098 [class.mem]
19100 Each member-declaration shall declare at least one member
19101 name of the class. */
19102 if (!decl_specifiers.any_specifiers_p)
19104 cp_token *token = cp_lexer_peek_token (parser->lexer);
19105 if (!in_system_header_at (token->location))
19106 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19108 else
19110 tree type;
19112 /* See if this declaration is a friend. */
19113 friend_p = cp_parser_friend_p (&decl_specifiers);
19114 /* If there were decl-specifiers, check to see if there was
19115 a class-declaration. */
19116 type = check_tag_decl (&decl_specifiers,
19117 /*explicit_type_instantiation_p=*/false);
19118 /* Nested classes have already been added to the class, but
19119 a `friend' needs to be explicitly registered. */
19120 if (friend_p)
19122 /* If the `friend' keyword was present, the friend must
19123 be introduced with a class-key. */
19124 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19125 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19126 "in C++03 a class-key must be used "
19127 "when declaring a friend");
19128 /* In this case:
19130 template <typename T> struct A {
19131 friend struct A<T>::B;
19134 A<T>::B will be represented by a TYPENAME_TYPE, and
19135 therefore not recognized by check_tag_decl. */
19136 if (!type)
19138 type = decl_specifiers.type;
19139 if (type && TREE_CODE (type) == TYPE_DECL)
19140 type = TREE_TYPE (type);
19142 if (!type || !TYPE_P (type))
19143 error_at (decl_spec_token_start->location,
19144 "friend declaration does not name a class or "
19145 "function");
19146 else
19147 make_friend_class (current_class_type, type,
19148 /*complain=*/true);
19150 /* If there is no TYPE, an error message will already have
19151 been issued. */
19152 else if (!type || type == error_mark_node)
19154 /* An anonymous aggregate has to be handled specially; such
19155 a declaration really declares a data member (with a
19156 particular type), as opposed to a nested class. */
19157 else if (ANON_AGGR_TYPE_P (type))
19159 /* C++11 9.5/6. */
19160 if (decl_specifiers.storage_class != sc_none)
19161 error_at (decl_spec_token_start->location,
19162 "a storage class on an anonymous aggregate "
19163 "in class scope is not allowed");
19165 /* Remove constructors and such from TYPE, now that we
19166 know it is an anonymous aggregate. */
19167 fixup_anonymous_aggr (type);
19168 /* And make the corresponding data member. */
19169 decl = build_decl (decl_spec_token_start->location,
19170 FIELD_DECL, NULL_TREE, type);
19171 /* Add it to the class. */
19172 finish_member_declaration (decl);
19174 else
19175 cp_parser_check_access_in_redeclaration
19176 (TYPE_NAME (type),
19177 decl_spec_token_start->location);
19180 else
19182 bool assume_semicolon = false;
19184 /* Clear attributes from the decl_specifiers but keep them
19185 around as prefix attributes that apply them to the entity
19186 being declared. */
19187 prefix_attributes = decl_specifiers.attributes;
19188 decl_specifiers.attributes = NULL_TREE;
19190 /* See if these declarations will be friends. */
19191 friend_p = cp_parser_friend_p (&decl_specifiers);
19193 /* Keep going until we hit the `;' at the end of the
19194 declaration. */
19195 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19197 tree attributes = NULL_TREE;
19198 tree first_attribute;
19200 /* Peek at the next token. */
19201 token = cp_lexer_peek_token (parser->lexer);
19203 /* Check for a bitfield declaration. */
19204 if (token->type == CPP_COLON
19205 || (token->type == CPP_NAME
19206 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19207 == CPP_COLON))
19209 tree identifier;
19210 tree width;
19212 /* Get the name of the bitfield. Note that we cannot just
19213 check TOKEN here because it may have been invalidated by
19214 the call to cp_lexer_peek_nth_token above. */
19215 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19216 identifier = cp_parser_identifier (parser);
19217 else
19218 identifier = NULL_TREE;
19220 /* Consume the `:' token. */
19221 cp_lexer_consume_token (parser->lexer);
19222 /* Get the width of the bitfield. */
19223 width
19224 = cp_parser_constant_expression (parser,
19225 /*allow_non_constant=*/false,
19226 NULL);
19228 /* Look for attributes that apply to the bitfield. */
19229 attributes = cp_parser_attributes_opt (parser);
19230 /* Remember which attributes are prefix attributes and
19231 which are not. */
19232 first_attribute = attributes;
19233 /* Combine the attributes. */
19234 attributes = chainon (prefix_attributes, attributes);
19236 /* Create the bitfield declaration. */
19237 decl = grokbitfield (identifier
19238 ? make_id_declarator (NULL_TREE,
19239 identifier,
19240 sfk_none)
19241 : NULL,
19242 &decl_specifiers,
19243 width,
19244 attributes);
19246 else
19248 cp_declarator *declarator;
19249 tree initializer;
19250 tree asm_specification;
19251 int ctor_dtor_or_conv_p;
19253 /* Parse the declarator. */
19254 declarator
19255 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19256 &ctor_dtor_or_conv_p,
19257 /*parenthesized_p=*/NULL,
19258 /*member_p=*/true);
19260 /* If something went wrong parsing the declarator, make sure
19261 that we at least consume some tokens. */
19262 if (declarator == cp_error_declarator)
19264 /* Skip to the end of the statement. */
19265 cp_parser_skip_to_end_of_statement (parser);
19266 /* If the next token is not a semicolon, that is
19267 probably because we just skipped over the body of
19268 a function. So, we consume a semicolon if
19269 present, but do not issue an error message if it
19270 is not present. */
19271 if (cp_lexer_next_token_is (parser->lexer,
19272 CPP_SEMICOLON))
19273 cp_lexer_consume_token (parser->lexer);
19274 goto out;
19277 if (declares_class_or_enum & 2)
19278 cp_parser_check_for_definition_in_return_type
19279 (declarator, decl_specifiers.type,
19280 decl_specifiers.locations[ds_type_spec]);
19282 /* Look for an asm-specification. */
19283 asm_specification = cp_parser_asm_specification_opt (parser);
19284 /* Look for attributes that apply to the declaration. */
19285 attributes = cp_parser_attributes_opt (parser);
19286 /* Remember which attributes are prefix attributes and
19287 which are not. */
19288 first_attribute = attributes;
19289 /* Combine the attributes. */
19290 attributes = chainon (prefix_attributes, attributes);
19292 /* If it's an `=', then we have a constant-initializer or a
19293 pure-specifier. It is not correct to parse the
19294 initializer before registering the member declaration
19295 since the member declaration should be in scope while
19296 its initializer is processed. However, the rest of the
19297 front end does not yet provide an interface that allows
19298 us to handle this correctly. */
19299 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19301 /* In [class.mem]:
19303 A pure-specifier shall be used only in the declaration of
19304 a virtual function.
19306 A member-declarator can contain a constant-initializer
19307 only if it declares a static member of integral or
19308 enumeration type.
19310 Therefore, if the DECLARATOR is for a function, we look
19311 for a pure-specifier; otherwise, we look for a
19312 constant-initializer. When we call `grokfield', it will
19313 perform more stringent semantics checks. */
19314 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19315 if (function_declarator_p (declarator)
19316 || (decl_specifiers.type
19317 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19318 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19319 == FUNCTION_TYPE)))
19320 initializer = cp_parser_pure_specifier (parser);
19321 else if (decl_specifiers.storage_class != sc_static)
19322 initializer = cp_parser_save_nsdmi (parser);
19323 else if (cxx_dialect >= cxx0x)
19325 bool nonconst;
19326 /* Don't require a constant rvalue in C++11, since we
19327 might want a reference constant. We'll enforce
19328 constancy later. */
19329 cp_lexer_consume_token (parser->lexer);
19330 /* Parse the initializer. */
19331 initializer = cp_parser_initializer_clause (parser,
19332 &nonconst);
19334 else
19335 /* Parse the initializer. */
19336 initializer = cp_parser_constant_initializer (parser);
19338 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19339 && !function_declarator_p (declarator))
19341 bool x;
19342 if (decl_specifiers.storage_class != sc_static)
19343 initializer = cp_parser_save_nsdmi (parser);
19344 else
19345 initializer = cp_parser_initializer (parser, &x, &x);
19347 /* Otherwise, there is no initializer. */
19348 else
19349 initializer = NULL_TREE;
19351 /* See if we are probably looking at a function
19352 definition. We are certainly not looking at a
19353 member-declarator. Calling `grokfield' has
19354 side-effects, so we must not do it unless we are sure
19355 that we are looking at a member-declarator. */
19356 if (cp_parser_token_starts_function_definition_p
19357 (cp_lexer_peek_token (parser->lexer)))
19359 /* The grammar does not allow a pure-specifier to be
19360 used when a member function is defined. (It is
19361 possible that this fact is an oversight in the
19362 standard, since a pure function may be defined
19363 outside of the class-specifier. */
19364 if (initializer && initializer_token_start)
19365 error_at (initializer_token_start->location,
19366 "pure-specifier on function-definition");
19367 decl = cp_parser_save_member_function_body (parser,
19368 &decl_specifiers,
19369 declarator,
19370 attributes);
19371 /* If the member was not a friend, declare it here. */
19372 if (!friend_p)
19373 finish_member_declaration (decl);
19374 /* Peek at the next token. */
19375 token = cp_lexer_peek_token (parser->lexer);
19376 /* If the next token is a semicolon, consume it. */
19377 if (token->type == CPP_SEMICOLON)
19378 cp_lexer_consume_token (parser->lexer);
19379 goto out;
19381 else
19382 if (declarator->kind == cdk_function)
19383 declarator->id_loc = token->location;
19384 /* Create the declaration. */
19385 decl = grokfield (declarator, &decl_specifiers,
19386 initializer, /*init_const_expr_p=*/true,
19387 asm_specification,
19388 attributes);
19391 /* Reset PREFIX_ATTRIBUTES. */
19392 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19393 attributes = TREE_CHAIN (attributes);
19394 if (attributes)
19395 TREE_CHAIN (attributes) = NULL_TREE;
19397 /* If there is any qualification still in effect, clear it
19398 now; we will be starting fresh with the next declarator. */
19399 parser->scope = NULL_TREE;
19400 parser->qualifying_scope = NULL_TREE;
19401 parser->object_scope = NULL_TREE;
19402 /* If it's a `,', then there are more declarators. */
19403 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19404 cp_lexer_consume_token (parser->lexer);
19405 /* If the next token isn't a `;', then we have a parse error. */
19406 else if (cp_lexer_next_token_is_not (parser->lexer,
19407 CPP_SEMICOLON))
19409 /* The next token might be a ways away from where the
19410 actual semicolon is missing. Find the previous token
19411 and use that for our error position. */
19412 cp_token *token = cp_lexer_previous_token (parser->lexer);
19413 error_at (token->location,
19414 "expected %<;%> at end of member declaration");
19416 /* Assume that the user meant to provide a semicolon. If
19417 we were to cp_parser_skip_to_end_of_statement, we might
19418 skip to a semicolon inside a member function definition
19419 and issue nonsensical error messages. */
19420 assume_semicolon = true;
19423 if (decl)
19425 /* Add DECL to the list of members. */
19426 if (!friend_p)
19427 finish_member_declaration (decl);
19429 if (TREE_CODE (decl) == FUNCTION_DECL)
19430 cp_parser_save_default_args (parser, decl);
19431 else if (TREE_CODE (decl) == FIELD_DECL
19432 && !DECL_C_BIT_FIELD (decl)
19433 && DECL_INITIAL (decl))
19434 /* Add DECL to the queue of NSDMI to be parsed later. */
19435 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19438 if (assume_semicolon)
19439 goto out;
19443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19444 out:
19445 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19448 /* Parse a pure-specifier.
19450 pure-specifier:
19453 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19454 Otherwise, ERROR_MARK_NODE is returned. */
19456 static tree
19457 cp_parser_pure_specifier (cp_parser* parser)
19459 cp_token *token;
19461 /* Look for the `=' token. */
19462 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19463 return error_mark_node;
19464 /* Look for the `0' token. */
19465 token = cp_lexer_peek_token (parser->lexer);
19467 if (token->type == CPP_EOF
19468 || token->type == CPP_PRAGMA_EOL)
19469 return error_mark_node;
19471 cp_lexer_consume_token (parser->lexer);
19473 /* Accept = default or = delete in c++0x mode. */
19474 if (token->keyword == RID_DEFAULT
19475 || token->keyword == RID_DELETE)
19477 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19478 return token->u.value;
19481 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19482 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19484 cp_parser_error (parser,
19485 "invalid pure specifier (only %<= 0%> is allowed)");
19486 cp_parser_skip_to_end_of_statement (parser);
19487 return error_mark_node;
19489 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19491 error_at (token->location, "templates may not be %<virtual%>");
19492 return error_mark_node;
19495 return integer_zero_node;
19498 /* Parse a constant-initializer.
19500 constant-initializer:
19501 = constant-expression
19503 Returns a representation of the constant-expression. */
19505 static tree
19506 cp_parser_constant_initializer (cp_parser* parser)
19508 /* Look for the `=' token. */
19509 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19510 return error_mark_node;
19512 /* It is invalid to write:
19514 struct S { static const int i = { 7 }; };
19517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19519 cp_parser_error (parser,
19520 "a brace-enclosed initializer is not allowed here");
19521 /* Consume the opening brace. */
19522 cp_lexer_consume_token (parser->lexer);
19523 /* Skip the initializer. */
19524 cp_parser_skip_to_closing_brace (parser);
19525 /* Look for the trailing `}'. */
19526 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19528 return error_mark_node;
19531 return cp_parser_constant_expression (parser,
19532 /*allow_non_constant=*/false,
19533 NULL);
19536 /* Derived classes [gram.class.derived] */
19538 /* Parse a base-clause.
19540 base-clause:
19541 : base-specifier-list
19543 base-specifier-list:
19544 base-specifier ... [opt]
19545 base-specifier-list , base-specifier ... [opt]
19547 Returns a TREE_LIST representing the base-classes, in the order in
19548 which they were declared. The representation of each node is as
19549 described by cp_parser_base_specifier.
19551 In the case that no bases are specified, this function will return
19552 NULL_TREE, not ERROR_MARK_NODE. */
19554 static tree
19555 cp_parser_base_clause (cp_parser* parser)
19557 tree bases = NULL_TREE;
19559 /* Look for the `:' that begins the list. */
19560 cp_parser_require (parser, CPP_COLON, RT_COLON);
19562 /* Scan the base-specifier-list. */
19563 while (true)
19565 cp_token *token;
19566 tree base;
19567 bool pack_expansion_p = false;
19569 /* Look for the base-specifier. */
19570 base = cp_parser_base_specifier (parser);
19571 /* Look for the (optional) ellipsis. */
19572 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19574 /* Consume the `...'. */
19575 cp_lexer_consume_token (parser->lexer);
19577 pack_expansion_p = true;
19580 /* Add BASE to the front of the list. */
19581 if (base && base != error_mark_node)
19583 if (pack_expansion_p)
19584 /* Make this a pack expansion type. */
19585 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19587 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19589 TREE_CHAIN (base) = bases;
19590 bases = base;
19593 /* Peek at the next token. */
19594 token = cp_lexer_peek_token (parser->lexer);
19595 /* If it's not a comma, then the list is complete. */
19596 if (token->type != CPP_COMMA)
19597 break;
19598 /* Consume the `,'. */
19599 cp_lexer_consume_token (parser->lexer);
19602 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19603 base class had a qualified name. However, the next name that
19604 appears is certainly not qualified. */
19605 parser->scope = NULL_TREE;
19606 parser->qualifying_scope = NULL_TREE;
19607 parser->object_scope = NULL_TREE;
19609 return nreverse (bases);
19612 /* Parse a base-specifier.
19614 base-specifier:
19615 :: [opt] nested-name-specifier [opt] class-name
19616 virtual access-specifier [opt] :: [opt] nested-name-specifier
19617 [opt] class-name
19618 access-specifier virtual [opt] :: [opt] nested-name-specifier
19619 [opt] class-name
19621 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19622 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19623 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19624 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19626 static tree
19627 cp_parser_base_specifier (cp_parser* parser)
19629 cp_token *token;
19630 bool done = false;
19631 bool virtual_p = false;
19632 bool duplicate_virtual_error_issued_p = false;
19633 bool duplicate_access_error_issued_p = false;
19634 bool class_scope_p, template_p;
19635 tree access = access_default_node;
19636 tree type;
19638 /* Process the optional `virtual' and `access-specifier'. */
19639 while (!done)
19641 /* Peek at the next token. */
19642 token = cp_lexer_peek_token (parser->lexer);
19643 /* Process `virtual'. */
19644 switch (token->keyword)
19646 case RID_VIRTUAL:
19647 /* If `virtual' appears more than once, issue an error. */
19648 if (virtual_p && !duplicate_virtual_error_issued_p)
19650 cp_parser_error (parser,
19651 "%<virtual%> specified more than once in base-specified");
19652 duplicate_virtual_error_issued_p = true;
19655 virtual_p = true;
19657 /* Consume the `virtual' token. */
19658 cp_lexer_consume_token (parser->lexer);
19660 break;
19662 case RID_PUBLIC:
19663 case RID_PROTECTED:
19664 case RID_PRIVATE:
19665 /* If more than one access specifier appears, issue an
19666 error. */
19667 if (access != access_default_node
19668 && !duplicate_access_error_issued_p)
19670 cp_parser_error (parser,
19671 "more than one access specifier in base-specified");
19672 duplicate_access_error_issued_p = true;
19675 access = ridpointers[(int) token->keyword];
19677 /* Consume the access-specifier. */
19678 cp_lexer_consume_token (parser->lexer);
19680 break;
19682 default:
19683 done = true;
19684 break;
19687 /* It is not uncommon to see programs mechanically, erroneously, use
19688 the 'typename' keyword to denote (dependent) qualified types
19689 as base classes. */
19690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19692 token = cp_lexer_peek_token (parser->lexer);
19693 if (!processing_template_decl)
19694 error_at (token->location,
19695 "keyword %<typename%> not allowed outside of templates");
19696 else
19697 error_at (token->location,
19698 "keyword %<typename%> not allowed in this context "
19699 "(the base class is implicitly a type)");
19700 cp_lexer_consume_token (parser->lexer);
19703 /* Look for the optional `::' operator. */
19704 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19705 /* Look for the nested-name-specifier. The simplest way to
19706 implement:
19708 [temp.res]
19710 The keyword `typename' is not permitted in a base-specifier or
19711 mem-initializer; in these contexts a qualified name that
19712 depends on a template-parameter is implicitly assumed to be a
19713 type name.
19715 is to pretend that we have seen the `typename' keyword at this
19716 point. */
19717 cp_parser_nested_name_specifier_opt (parser,
19718 /*typename_keyword_p=*/true,
19719 /*check_dependency_p=*/true,
19720 typename_type,
19721 /*is_declaration=*/true);
19722 /* If the base class is given by a qualified name, assume that names
19723 we see are type names or templates, as appropriate. */
19724 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19725 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19727 if (!parser->scope
19728 && cp_lexer_next_token_is_decltype (parser->lexer))
19729 /* DR 950 allows decltype as a base-specifier. */
19730 type = cp_parser_decltype (parser);
19731 else
19733 /* Otherwise, look for the class-name. */
19734 type = cp_parser_class_name (parser,
19735 class_scope_p,
19736 template_p,
19737 typename_type,
19738 /*check_dependency_p=*/true,
19739 /*class_head_p=*/false,
19740 /*is_declaration=*/true);
19741 type = TREE_TYPE (type);
19744 if (type == error_mark_node)
19745 return error_mark_node;
19747 return finish_base_specifier (type, access, virtual_p);
19750 /* Exception handling [gram.exception] */
19752 /* Parse an (optional) noexcept-specification.
19754 noexcept-specification:
19755 noexcept ( constant-expression ) [opt]
19757 If no noexcept-specification is present, returns NULL_TREE.
19758 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19759 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19760 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19761 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19762 in which case a boolean condition is returned instead. */
19764 static tree
19765 cp_parser_noexcept_specification_opt (cp_parser* parser,
19766 bool require_constexpr,
19767 bool* consumed_expr,
19768 bool return_cond)
19770 cp_token *token;
19771 const char *saved_message;
19773 /* Peek at the next token. */
19774 token = cp_lexer_peek_token (parser->lexer);
19776 /* Is it a noexcept-specification? */
19777 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19779 tree expr;
19780 cp_lexer_consume_token (parser->lexer);
19782 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19784 cp_lexer_consume_token (parser->lexer);
19786 if (require_constexpr)
19788 /* Types may not be defined in an exception-specification. */
19789 saved_message = parser->type_definition_forbidden_message;
19790 parser->type_definition_forbidden_message
19791 = G_("types may not be defined in an exception-specification");
19793 expr = cp_parser_constant_expression (parser, false, NULL);
19795 /* Restore the saved message. */
19796 parser->type_definition_forbidden_message = saved_message;
19798 else
19800 expr = cp_parser_expression (parser, false, NULL);
19801 *consumed_expr = true;
19804 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19806 else
19808 expr = boolean_true_node;
19809 if (!require_constexpr)
19810 *consumed_expr = false;
19813 /* We cannot build a noexcept-spec right away because this will check
19814 that expr is a constexpr. */
19815 if (!return_cond)
19816 return build_noexcept_spec (expr, tf_warning_or_error);
19817 else
19818 return expr;
19820 else
19821 return NULL_TREE;
19824 /* Parse an (optional) exception-specification.
19826 exception-specification:
19827 throw ( type-id-list [opt] )
19829 Returns a TREE_LIST representing the exception-specification. The
19830 TREE_VALUE of each node is a type. */
19832 static tree
19833 cp_parser_exception_specification_opt (cp_parser* parser)
19835 cp_token *token;
19836 tree type_id_list;
19837 const char *saved_message;
19839 /* Peek at the next token. */
19840 token = cp_lexer_peek_token (parser->lexer);
19842 /* Is it a noexcept-specification? */
19843 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19844 false);
19845 if (type_id_list != NULL_TREE)
19846 return type_id_list;
19848 /* If it's not `throw', then there's no exception-specification. */
19849 if (!cp_parser_is_keyword (token, RID_THROW))
19850 return NULL_TREE;
19852 #if 0
19853 /* Enable this once a lot of code has transitioned to noexcept? */
19854 if (cxx_dialect >= cxx0x && !in_system_header)
19855 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19856 "deprecated in C++0x; use %<noexcept%> instead");
19857 #endif
19859 /* Consume the `throw'. */
19860 cp_lexer_consume_token (parser->lexer);
19862 /* Look for the `('. */
19863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19865 /* Peek at the next token. */
19866 token = cp_lexer_peek_token (parser->lexer);
19867 /* If it's not a `)', then there is a type-id-list. */
19868 if (token->type != CPP_CLOSE_PAREN)
19870 /* Types may not be defined in an exception-specification. */
19871 saved_message = parser->type_definition_forbidden_message;
19872 parser->type_definition_forbidden_message
19873 = G_("types may not be defined in an exception-specification");
19874 /* Parse the type-id-list. */
19875 type_id_list = cp_parser_type_id_list (parser);
19876 /* Restore the saved message. */
19877 parser->type_definition_forbidden_message = saved_message;
19879 else
19880 type_id_list = empty_except_spec;
19882 /* Look for the `)'. */
19883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19885 return type_id_list;
19888 /* Parse an (optional) type-id-list.
19890 type-id-list:
19891 type-id ... [opt]
19892 type-id-list , type-id ... [opt]
19894 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19895 in the order that the types were presented. */
19897 static tree
19898 cp_parser_type_id_list (cp_parser* parser)
19900 tree types = NULL_TREE;
19902 while (true)
19904 cp_token *token;
19905 tree type;
19907 /* Get the next type-id. */
19908 type = cp_parser_type_id (parser);
19909 /* Parse the optional ellipsis. */
19910 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19912 /* Consume the `...'. */
19913 cp_lexer_consume_token (parser->lexer);
19915 /* Turn the type into a pack expansion expression. */
19916 type = make_pack_expansion (type);
19918 /* Add it to the list. */
19919 types = add_exception_specifier (types, type, /*complain=*/1);
19920 /* Peek at the next token. */
19921 token = cp_lexer_peek_token (parser->lexer);
19922 /* If it is not a `,', we are done. */
19923 if (token->type != CPP_COMMA)
19924 break;
19925 /* Consume the `,'. */
19926 cp_lexer_consume_token (parser->lexer);
19929 return nreverse (types);
19932 /* Parse a try-block.
19934 try-block:
19935 try compound-statement handler-seq */
19937 static tree
19938 cp_parser_try_block (cp_parser* parser)
19940 tree try_block;
19942 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19943 try_block = begin_try_block ();
19944 cp_parser_compound_statement (parser, NULL, true, false);
19945 finish_try_block (try_block);
19946 cp_parser_handler_seq (parser);
19947 finish_handler_sequence (try_block);
19949 return try_block;
19952 /* Parse a function-try-block.
19954 function-try-block:
19955 try ctor-initializer [opt] function-body handler-seq */
19957 static bool
19958 cp_parser_function_try_block (cp_parser* parser)
19960 tree compound_stmt;
19961 tree try_block;
19962 bool ctor_initializer_p;
19964 /* Look for the `try' keyword. */
19965 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19966 return false;
19967 /* Let the rest of the front end know where we are. */
19968 try_block = begin_function_try_block (&compound_stmt);
19969 /* Parse the function-body. */
19970 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19971 (parser, /*in_function_try_block=*/true);
19972 /* We're done with the `try' part. */
19973 finish_function_try_block (try_block);
19974 /* Parse the handlers. */
19975 cp_parser_handler_seq (parser);
19976 /* We're done with the handlers. */
19977 finish_function_handler_sequence (try_block, compound_stmt);
19979 return ctor_initializer_p;
19982 /* Parse a handler-seq.
19984 handler-seq:
19985 handler handler-seq [opt] */
19987 static void
19988 cp_parser_handler_seq (cp_parser* parser)
19990 while (true)
19992 cp_token *token;
19994 /* Parse the handler. */
19995 cp_parser_handler (parser);
19996 /* Peek at the next token. */
19997 token = cp_lexer_peek_token (parser->lexer);
19998 /* If it's not `catch' then there are no more handlers. */
19999 if (!cp_parser_is_keyword (token, RID_CATCH))
20000 break;
20004 /* Parse a handler.
20006 handler:
20007 catch ( exception-declaration ) compound-statement */
20009 static void
20010 cp_parser_handler (cp_parser* parser)
20012 tree handler;
20013 tree declaration;
20015 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20016 handler = begin_handler ();
20017 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20018 declaration = cp_parser_exception_declaration (parser);
20019 finish_handler_parms (declaration, handler);
20020 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20021 cp_parser_compound_statement (parser, NULL, false, false);
20022 finish_handler (handler);
20025 /* Parse an exception-declaration.
20027 exception-declaration:
20028 type-specifier-seq declarator
20029 type-specifier-seq abstract-declarator
20030 type-specifier-seq
20033 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20034 ellipsis variant is used. */
20036 static tree
20037 cp_parser_exception_declaration (cp_parser* parser)
20039 cp_decl_specifier_seq type_specifiers;
20040 cp_declarator *declarator;
20041 const char *saved_message;
20043 /* If it's an ellipsis, it's easy to handle. */
20044 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20046 /* Consume the `...' token. */
20047 cp_lexer_consume_token (parser->lexer);
20048 return NULL_TREE;
20051 /* Types may not be defined in exception-declarations. */
20052 saved_message = parser->type_definition_forbidden_message;
20053 parser->type_definition_forbidden_message
20054 = G_("types may not be defined in exception-declarations");
20056 /* Parse the type-specifier-seq. */
20057 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20058 /*is_trailing_return=*/false,
20059 &type_specifiers);
20060 /* If it's a `)', then there is no declarator. */
20061 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20062 declarator = NULL;
20063 else
20064 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20065 /*ctor_dtor_or_conv_p=*/NULL,
20066 /*parenthesized_p=*/NULL,
20067 /*member_p=*/false);
20069 /* Restore the saved message. */
20070 parser->type_definition_forbidden_message = saved_message;
20072 if (!type_specifiers.any_specifiers_p)
20073 return error_mark_node;
20075 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20078 /* Parse a throw-expression.
20080 throw-expression:
20081 throw assignment-expression [opt]
20083 Returns a THROW_EXPR representing the throw-expression. */
20085 static tree
20086 cp_parser_throw_expression (cp_parser* parser)
20088 tree expression;
20089 cp_token* token;
20091 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20092 token = cp_lexer_peek_token (parser->lexer);
20093 /* Figure out whether or not there is an assignment-expression
20094 following the "throw" keyword. */
20095 if (token->type == CPP_COMMA
20096 || token->type == CPP_SEMICOLON
20097 || token->type == CPP_CLOSE_PAREN
20098 || token->type == CPP_CLOSE_SQUARE
20099 || token->type == CPP_CLOSE_BRACE
20100 || token->type == CPP_COLON)
20101 expression = NULL_TREE;
20102 else
20103 expression = cp_parser_assignment_expression (parser,
20104 /*cast_p=*/false, NULL);
20106 return build_throw (expression);
20109 /* GNU Extensions */
20111 /* Parse an (optional) asm-specification.
20113 asm-specification:
20114 asm ( string-literal )
20116 If the asm-specification is present, returns a STRING_CST
20117 corresponding to the string-literal. Otherwise, returns
20118 NULL_TREE. */
20120 static tree
20121 cp_parser_asm_specification_opt (cp_parser* parser)
20123 cp_token *token;
20124 tree asm_specification;
20126 /* Peek at the next token. */
20127 token = cp_lexer_peek_token (parser->lexer);
20128 /* If the next token isn't the `asm' keyword, then there's no
20129 asm-specification. */
20130 if (!cp_parser_is_keyword (token, RID_ASM))
20131 return NULL_TREE;
20133 /* Consume the `asm' token. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* Look for the `('. */
20136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20138 /* Look for the string-literal. */
20139 asm_specification = cp_parser_string_literal (parser, false, false);
20141 /* Look for the `)'. */
20142 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20144 return asm_specification;
20147 /* Parse an asm-operand-list.
20149 asm-operand-list:
20150 asm-operand
20151 asm-operand-list , asm-operand
20153 asm-operand:
20154 string-literal ( expression )
20155 [ string-literal ] string-literal ( expression )
20157 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20158 each node is the expression. The TREE_PURPOSE is itself a
20159 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20160 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20161 is a STRING_CST for the string literal before the parenthesis. Returns
20162 ERROR_MARK_NODE if any of the operands are invalid. */
20164 static tree
20165 cp_parser_asm_operand_list (cp_parser* parser)
20167 tree asm_operands = NULL_TREE;
20168 bool invalid_operands = false;
20170 while (true)
20172 tree string_literal;
20173 tree expression;
20174 tree name;
20176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20178 /* Consume the `[' token. */
20179 cp_lexer_consume_token (parser->lexer);
20180 /* Read the operand name. */
20181 name = cp_parser_identifier (parser);
20182 if (name != error_mark_node)
20183 name = build_string (IDENTIFIER_LENGTH (name),
20184 IDENTIFIER_POINTER (name));
20185 /* Look for the closing `]'. */
20186 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20188 else
20189 name = NULL_TREE;
20190 /* Look for the string-literal. */
20191 string_literal = cp_parser_string_literal (parser, false, false);
20193 /* Look for the `('. */
20194 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20195 /* Parse the expression. */
20196 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20197 /* Look for the `)'. */
20198 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20200 if (name == error_mark_node
20201 || string_literal == error_mark_node
20202 || expression == error_mark_node)
20203 invalid_operands = true;
20205 /* Add this operand to the list. */
20206 asm_operands = tree_cons (build_tree_list (name, string_literal),
20207 expression,
20208 asm_operands);
20209 /* If the next token is not a `,', there are no more
20210 operands. */
20211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20212 break;
20213 /* Consume the `,'. */
20214 cp_lexer_consume_token (parser->lexer);
20217 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20220 /* Parse an asm-clobber-list.
20222 asm-clobber-list:
20223 string-literal
20224 asm-clobber-list , string-literal
20226 Returns a TREE_LIST, indicating the clobbers in the order that they
20227 appeared. The TREE_VALUE of each node is a STRING_CST. */
20229 static tree
20230 cp_parser_asm_clobber_list (cp_parser* parser)
20232 tree clobbers = NULL_TREE;
20234 while (true)
20236 tree string_literal;
20238 /* Look for the string literal. */
20239 string_literal = cp_parser_string_literal (parser, false, false);
20240 /* Add it to the list. */
20241 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20242 /* If the next token is not a `,', then the list is
20243 complete. */
20244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20245 break;
20246 /* Consume the `,' token. */
20247 cp_lexer_consume_token (parser->lexer);
20250 return clobbers;
20253 /* Parse an asm-label-list.
20255 asm-label-list:
20256 identifier
20257 asm-label-list , identifier
20259 Returns a TREE_LIST, indicating the labels in the order that they
20260 appeared. The TREE_VALUE of each node is a label. */
20262 static tree
20263 cp_parser_asm_label_list (cp_parser* parser)
20265 tree labels = NULL_TREE;
20267 while (true)
20269 tree identifier, label, name;
20271 /* Look for the identifier. */
20272 identifier = cp_parser_identifier (parser);
20273 if (!error_operand_p (identifier))
20275 label = lookup_label (identifier);
20276 if (TREE_CODE (label) == LABEL_DECL)
20278 TREE_USED (label) = 1;
20279 check_goto (label);
20280 name = build_string (IDENTIFIER_LENGTH (identifier),
20281 IDENTIFIER_POINTER (identifier));
20282 labels = tree_cons (name, label, labels);
20285 /* If the next token is not a `,', then the list is
20286 complete. */
20287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20288 break;
20289 /* Consume the `,' token. */
20290 cp_lexer_consume_token (parser->lexer);
20293 return nreverse (labels);
20296 /* Return TRUE iff the next tokens in the stream are possibly the
20297 beginning of a GNU extension attribute. */
20299 static bool
20300 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20302 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20305 /* Return TRUE iff the next tokens in the stream are possibly the
20306 beginning of a standard C++-11 attribute. */
20308 static bool
20309 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20311 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20314 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20315 beginning of a standard C++-11 attribute. */
20317 static bool
20318 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20320 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20322 return (cxx_dialect >= cxx0x
20323 && token->type == CPP_OPEN_SQUARE
20324 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20325 && token->type == CPP_OPEN_SQUARE);
20328 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20329 beginning of a GNU extension attribute. */
20331 static bool
20332 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20334 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20336 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20339 /* Return true iff the next tokens can be the beginning of either a
20340 GNU attribute list, or a standard C++11 attribute sequence. */
20342 static bool
20343 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20345 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20346 || cp_next_tokens_can_be_std_attribute_p (parser));
20349 /* Return true iff the next Nth tokens can be the beginning of either
20350 a GNU attribute list, or a standard C++11 attribute sequence. */
20352 static bool
20353 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20355 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20356 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20359 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20360 of GNU attributes, or return NULL. */
20362 static tree
20363 cp_parser_attributes_opt (cp_parser *parser)
20365 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20366 return cp_parser_gnu_attributes_opt (parser);
20367 return cp_parser_std_attribute_spec_seq (parser);
20370 /* Parse an (optional) series of attributes.
20372 attributes:
20373 attributes attribute
20375 attribute:
20376 __attribute__ (( attribute-list [opt] ))
20378 The return value is as for cp_parser_gnu_attribute_list. */
20380 static tree
20381 cp_parser_gnu_attributes_opt (cp_parser* parser)
20383 tree attributes = NULL_TREE;
20385 while (true)
20387 cp_token *token;
20388 tree attribute_list;
20389 bool ok = true;
20391 /* Peek at the next token. */
20392 token = cp_lexer_peek_token (parser->lexer);
20393 /* If it's not `__attribute__', then we're done. */
20394 if (token->keyword != RID_ATTRIBUTE)
20395 break;
20397 /* Consume the `__attribute__' keyword. */
20398 cp_lexer_consume_token (parser->lexer);
20399 /* Look for the two `(' tokens. */
20400 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20401 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20403 /* Peek at the next token. */
20404 token = cp_lexer_peek_token (parser->lexer);
20405 if (token->type != CPP_CLOSE_PAREN)
20406 /* Parse the attribute-list. */
20407 attribute_list = cp_parser_gnu_attribute_list (parser);
20408 else
20409 /* If the next token is a `)', then there is no attribute
20410 list. */
20411 attribute_list = NULL;
20413 /* Look for the two `)' tokens. */
20414 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20415 ok = false;
20416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20417 ok = false;
20418 if (!ok)
20419 cp_parser_skip_to_end_of_statement (parser);
20421 /* Add these new attributes to the list. */
20422 attributes = chainon (attributes, attribute_list);
20425 return attributes;
20428 /* Parse a GNU attribute-list.
20430 attribute-list:
20431 attribute
20432 attribute-list , attribute
20434 attribute:
20435 identifier
20436 identifier ( identifier )
20437 identifier ( identifier , expression-list )
20438 identifier ( expression-list )
20440 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20441 to an attribute. The TREE_PURPOSE of each node is the identifier
20442 indicating which attribute is in use. The TREE_VALUE represents
20443 the arguments, if any. */
20445 static tree
20446 cp_parser_gnu_attribute_list (cp_parser* parser)
20448 tree attribute_list = NULL_TREE;
20449 bool save_translate_strings_p = parser->translate_strings_p;
20451 parser->translate_strings_p = false;
20452 while (true)
20454 cp_token *token;
20455 tree identifier;
20456 tree attribute;
20458 /* Look for the identifier. We also allow keywords here; for
20459 example `__attribute__ ((const))' is legal. */
20460 token = cp_lexer_peek_token (parser->lexer);
20461 if (token->type == CPP_NAME
20462 || token->type == CPP_KEYWORD)
20464 tree arguments = NULL_TREE;
20466 /* Consume the token. */
20467 token = cp_lexer_consume_token (parser->lexer);
20469 /* Save away the identifier that indicates which attribute
20470 this is. */
20471 identifier = (token->type == CPP_KEYWORD)
20472 /* For keywords, use the canonical spelling, not the
20473 parsed identifier. */
20474 ? ridpointers[(int) token->keyword]
20475 : token->u.value;
20477 attribute = build_tree_list (identifier, NULL_TREE);
20479 /* Peek at the next token. */
20480 token = cp_lexer_peek_token (parser->lexer);
20481 /* If it's an `(', then parse the attribute arguments. */
20482 if (token->type == CPP_OPEN_PAREN)
20484 VEC(tree,gc) *vec;
20485 int attr_flag = (attribute_takes_identifier_p (identifier)
20486 ? id_attr : normal_attr);
20487 vec = cp_parser_parenthesized_expression_list
20488 (parser, attr_flag, /*cast_p=*/false,
20489 /*allow_expansion_p=*/false,
20490 /*non_constant_p=*/NULL);
20491 if (vec == NULL)
20492 arguments = error_mark_node;
20493 else
20495 arguments = build_tree_list_vec (vec);
20496 release_tree_vector (vec);
20498 /* Save the arguments away. */
20499 TREE_VALUE (attribute) = arguments;
20502 if (arguments != error_mark_node)
20504 /* Add this attribute to the list. */
20505 TREE_CHAIN (attribute) = attribute_list;
20506 attribute_list = attribute;
20509 token = cp_lexer_peek_token (parser->lexer);
20511 /* Now, look for more attributes. If the next token isn't a
20512 `,', we're done. */
20513 if (token->type != CPP_COMMA)
20514 break;
20516 /* Consume the comma and keep going. */
20517 cp_lexer_consume_token (parser->lexer);
20519 parser->translate_strings_p = save_translate_strings_p;
20521 /* We built up the list in reverse order. */
20522 return nreverse (attribute_list);
20525 /* Parse a standard C++11 attribute.
20527 The returned representation is a TREE_LIST which TREE_PURPOSE is
20528 the scoped name of the attribute, and the TREE_VALUE is its
20529 arguments list.
20531 Note that the scoped name of the attribute is itself a TREE_LIST
20532 which TREE_PURPOSE is the namespace of the attribute, and
20533 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20534 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20535 and which TREE_PURPOSE is directly the attribute name.
20537 Clients of the attribute code should use get_attribute_namespace
20538 and get_attribute_name to get the actual namespace and name of
20539 attributes, regardless of their being GNU or C++11 attributes.
20541 attribute:
20542 attribute-token attribute-argument-clause [opt]
20544 attribute-token:
20545 identifier
20546 attribute-scoped-token
20548 attribute-scoped-token:
20549 attribute-namespace :: identifier
20551 attribute-namespace:
20552 identifier
20554 attribute-argument-clause:
20555 ( balanced-token-seq )
20557 balanced-token-seq:
20558 balanced-token [opt]
20559 balanced-token-seq balanced-token
20561 balanced-token:
20562 ( balanced-token-seq )
20563 [ balanced-token-seq ]
20564 { balanced-token-seq }. */
20566 static tree
20567 cp_parser_std_attribute (cp_parser *parser)
20569 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20570 cp_token *token;
20572 /* First, parse name of the the attribute, a.k.a
20573 attribute-token. */
20575 token = cp_lexer_peek_token (parser->lexer);
20576 if (token->type == CPP_NAME)
20577 attr_id = token->u.value;
20578 else if (token->type == CPP_KEYWORD)
20579 attr_id = ridpointers[(int) token->keyword];
20580 else if (token->flags & NAMED_OP)
20581 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20583 if (attr_id == NULL_TREE)
20584 return NULL_TREE;
20586 cp_lexer_consume_token (parser->lexer);
20588 token = cp_lexer_peek_token (parser->lexer);
20589 if (token->type == CPP_SCOPE)
20591 /* We are seeing a scoped attribute token. */
20593 cp_lexer_consume_token (parser->lexer);
20594 attr_ns = attr_id;
20596 token = cp_lexer_consume_token (parser->lexer);
20597 if (token->type == CPP_NAME)
20598 attr_id = token->u.value;
20599 else if (token->type == CPP_KEYWORD)
20600 attr_id = ridpointers[(int) token->keyword];
20601 else
20603 error_at (token->location,
20604 "expected an identifier for the attribute name");
20605 return error_mark_node;
20607 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20608 NULL_TREE);
20609 token = cp_lexer_peek_token (parser->lexer);
20611 else
20612 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20613 NULL_TREE);
20615 /* Now parse the optional argument clause of the attribute. */
20617 if (token->type != CPP_OPEN_PAREN)
20618 return attribute;
20621 VEC(tree, gc) *vec;
20622 int attr_flag = normal_attr;
20624 if (attr_ns == get_identifier ("gnu")
20625 && attribute_takes_identifier_p (attr_id))
20626 /* A GNU attribute that takes an identifier in parameter. */
20627 attr_flag = id_attr;
20629 vec = cp_parser_parenthesized_expression_list
20630 (parser, attr_flag, /*cast_p=*/false,
20631 /*allow_expansion_p=*/true,
20632 /*non_constant_p=*/NULL);
20633 if (vec == NULL)
20634 arguments = error_mark_node;
20635 else
20637 arguments = build_tree_list_vec (vec);
20638 release_tree_vector (vec);
20641 if (arguments == error_mark_node)
20642 attribute = error_mark_node;
20643 else
20644 TREE_VALUE (attribute) = arguments;
20647 return attribute;
20650 /* Parse a list of standard C++-11 attributes.
20652 attribute-list:
20653 attribute [opt]
20654 attribute-list , attribute[opt]
20655 attribute ...
20656 attribute-list , attribute ...
20659 static tree
20660 cp_parser_std_attribute_list (cp_parser *parser)
20662 tree attributes = NULL_TREE, attribute = NULL_TREE;
20663 cp_token *token = NULL;
20665 while (true)
20667 attribute = cp_parser_std_attribute (parser);
20668 if (attribute == error_mark_node)
20669 break;
20670 if (attribute != NULL_TREE)
20672 TREE_CHAIN (attribute) = attributes;
20673 attributes = attribute;
20675 token = cp_lexer_peek_token (parser->lexer);
20676 if (token->type != CPP_COMMA)
20677 break;
20678 cp_lexer_consume_token (parser->lexer);
20680 attributes = nreverse (attributes);
20681 return attributes;
20684 /* Parse a standard C++-11 attribute specifier.
20686 attribute-specifier:
20687 [ [ attribute-list ] ]
20688 alignment-specifier
20690 alignment-specifier:
20691 alignas ( type-id ... [opt] )
20692 alignas ( alignment-expression ... [opt] ). */
20694 static tree
20695 cp_parser_std_attribute_spec (cp_parser *parser)
20697 tree attributes = NULL_TREE;
20698 cp_token *token = cp_lexer_peek_token (parser->lexer);
20700 if (token->type == CPP_OPEN_SQUARE
20701 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20703 cp_lexer_consume_token (parser->lexer);
20704 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20705 cp_lexer_consume_token (parser->lexer);
20707 attributes = cp_parser_std_attribute_list (parser);
20709 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20710 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20711 cp_parser_skip_to_end_of_statement (parser);
20713 else
20715 tree alignas_expr;
20717 /* Look for an alignment-specifier. */
20719 token = cp_lexer_peek_token (parser->lexer);
20721 if (token->type != CPP_KEYWORD
20722 || token->keyword != RID_ALIGNAS)
20723 return NULL_TREE;
20725 cp_lexer_consume_token (parser->lexer);
20726 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20728 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20730 cp_parser_error (parser, "expected %<(%>");
20731 return error_mark_node;
20734 cp_parser_parse_tentatively (parser);
20735 alignas_expr = cp_parser_type_id (parser);
20737 if (!cp_parser_parse_definitely (parser))
20739 gcc_assert (alignas_expr == error_mark_node
20740 || alignas_expr == NULL_TREE);
20742 alignas_expr =
20743 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20744 /**cp_id_kind=*/NULL);
20745 if (alignas_expr == NULL_TREE
20746 || alignas_expr == error_mark_node)
20747 return alignas_expr;
20750 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20752 cp_parser_error (parser, "expected %<)%>");
20753 return error_mark_node;
20756 alignas_expr = cxx_alignas_expr (alignas_expr);
20758 /* Build the C++-11 representation of an 'aligned'
20759 attribute. */
20760 attributes =
20761 build_tree_list (build_tree_list (get_identifier ("gnu"),
20762 get_identifier ("aligned")),
20763 build_tree_list (NULL_TREE, alignas_expr));
20766 return attributes;
20769 /* Parse a standard C++-11 attribute-specifier-seq.
20771 attribute-specifier-seq:
20772 attribute-specifier-seq [opt] attribute-specifier
20775 static tree
20776 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20778 tree attr_specs = NULL;
20780 while (true)
20782 tree attr_spec = cp_parser_std_attribute_spec (parser);
20783 if (attr_spec == NULL_TREE)
20784 break;
20785 if (attr_spec == error_mark_node)
20786 return error_mark_node;
20788 TREE_CHAIN (attr_spec) = attr_specs;
20789 attr_specs = attr_spec;
20792 attr_specs = nreverse (attr_specs);
20793 return attr_specs;
20796 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20797 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20798 current value of the PEDANTIC flag, regardless of whether or not
20799 the `__extension__' keyword is present. The caller is responsible
20800 for restoring the value of the PEDANTIC flag. */
20802 static bool
20803 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20805 /* Save the old value of the PEDANTIC flag. */
20806 *saved_pedantic = pedantic;
20808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20810 /* Consume the `__extension__' token. */
20811 cp_lexer_consume_token (parser->lexer);
20812 /* We're not being pedantic while the `__extension__' keyword is
20813 in effect. */
20814 pedantic = 0;
20816 return true;
20819 return false;
20822 /* Parse a label declaration.
20824 label-declaration:
20825 __label__ label-declarator-seq ;
20827 label-declarator-seq:
20828 identifier , label-declarator-seq
20829 identifier */
20831 static void
20832 cp_parser_label_declaration (cp_parser* parser)
20834 /* Look for the `__label__' keyword. */
20835 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20837 while (true)
20839 tree identifier;
20841 /* Look for an identifier. */
20842 identifier = cp_parser_identifier (parser);
20843 /* If we failed, stop. */
20844 if (identifier == error_mark_node)
20845 break;
20846 /* Declare it as a label. */
20847 finish_label_decl (identifier);
20848 /* If the next token is a `;', stop. */
20849 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20850 break;
20851 /* Look for the `,' separating the label declarations. */
20852 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20855 /* Look for the final `;'. */
20856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20859 /* Support Functions */
20861 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20862 NAME should have one of the representations used for an
20863 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20864 is returned. If PARSER->SCOPE is a dependent type, then a
20865 SCOPE_REF is returned.
20867 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20868 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20869 was formed. Abstractly, such entities should not be passed to this
20870 function, because they do not need to be looked up, but it is
20871 simpler to check for this special case here, rather than at the
20872 call-sites.
20874 In cases not explicitly covered above, this function returns a
20875 DECL, OVERLOAD, or baselink representing the result of the lookup.
20876 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20877 is returned.
20879 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20880 (e.g., "struct") that was used. In that case bindings that do not
20881 refer to types are ignored.
20883 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20884 ignored.
20886 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20887 are ignored.
20889 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20890 types.
20892 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20893 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20894 NULL_TREE otherwise. */
20896 static tree
20897 cp_parser_lookup_name (cp_parser *parser, tree name,
20898 enum tag_types tag_type,
20899 bool is_template,
20900 bool is_namespace,
20901 bool check_dependency,
20902 tree *ambiguous_decls,
20903 location_t name_location)
20905 tree decl;
20906 tree object_type = parser->context->object_type;
20908 /* Assume that the lookup will be unambiguous. */
20909 if (ambiguous_decls)
20910 *ambiguous_decls = NULL_TREE;
20912 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20913 no longer valid. Note that if we are parsing tentatively, and
20914 the parse fails, OBJECT_TYPE will be automatically restored. */
20915 parser->context->object_type = NULL_TREE;
20917 if (name == error_mark_node)
20918 return error_mark_node;
20920 /* A template-id has already been resolved; there is no lookup to
20921 do. */
20922 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20923 return name;
20924 if (BASELINK_P (name))
20926 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20927 == TEMPLATE_ID_EXPR);
20928 return name;
20931 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20932 it should already have been checked to make sure that the name
20933 used matches the type being destroyed. */
20934 if (TREE_CODE (name) == BIT_NOT_EXPR)
20936 tree type;
20938 /* Figure out to which type this destructor applies. */
20939 if (parser->scope)
20940 type = parser->scope;
20941 else if (object_type)
20942 type = object_type;
20943 else
20944 type = current_class_type;
20945 /* If that's not a class type, there is no destructor. */
20946 if (!type || !CLASS_TYPE_P (type))
20947 return error_mark_node;
20948 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20949 lazily_declare_fn (sfk_destructor, type);
20950 if (!CLASSTYPE_DESTRUCTORS (type))
20951 return error_mark_node;
20952 /* If it was a class type, return the destructor. */
20953 return CLASSTYPE_DESTRUCTORS (type);
20956 /* By this point, the NAME should be an ordinary identifier. If
20957 the id-expression was a qualified name, the qualifying scope is
20958 stored in PARSER->SCOPE at this point. */
20959 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20961 /* Perform the lookup. */
20962 if (parser->scope)
20964 bool dependent_p;
20966 if (parser->scope == error_mark_node)
20967 return error_mark_node;
20969 /* If the SCOPE is dependent, the lookup must be deferred until
20970 the template is instantiated -- unless we are explicitly
20971 looking up names in uninstantiated templates. Even then, we
20972 cannot look up the name if the scope is not a class type; it
20973 might, for example, be a template type parameter. */
20974 dependent_p = (TYPE_P (parser->scope)
20975 && dependent_scope_p (parser->scope));
20976 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20977 && dependent_p)
20978 /* Defer lookup. */
20979 decl = error_mark_node;
20980 else
20982 tree pushed_scope = NULL_TREE;
20984 /* If PARSER->SCOPE is a dependent type, then it must be a
20985 class type, and we must not be checking dependencies;
20986 otherwise, we would have processed this lookup above. So
20987 that PARSER->SCOPE is not considered a dependent base by
20988 lookup_member, we must enter the scope here. */
20989 if (dependent_p)
20990 pushed_scope = push_scope (parser->scope);
20992 /* If the PARSER->SCOPE is a template specialization, it
20993 may be instantiated during name lookup. In that case,
20994 errors may be issued. Even if we rollback the current
20995 tentative parse, those errors are valid. */
20996 decl = lookup_qualified_name (parser->scope, name,
20997 tag_type != none_type,
20998 /*complain=*/true);
21000 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21001 lookup result and the nested-name-specifier nominates a class C:
21002 * if the name specified after the nested-name-specifier, when
21003 looked up in C, is the injected-class-name of C (Clause 9), or
21004 * if the name specified after the nested-name-specifier is the
21005 same as the identifier or the simple-template-id's template-
21006 name in the last component of the nested-name-specifier,
21007 the name is instead considered to name the constructor of
21008 class C. [ Note: for example, the constructor is not an
21009 acceptable lookup result in an elaborated-type-specifier so
21010 the constructor would not be used in place of the
21011 injected-class-name. --end note ] Such a constructor name
21012 shall be used only in the declarator-id of a declaration that
21013 names a constructor or in a using-declaration. */
21014 if (tag_type == none_type
21015 && DECL_SELF_REFERENCE_P (decl)
21016 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21017 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21018 tag_type != none_type,
21019 /*complain=*/true);
21021 /* If we have a single function from a using decl, pull it out. */
21022 if (TREE_CODE (decl) == OVERLOAD
21023 && !really_overloaded_fn (decl))
21024 decl = OVL_FUNCTION (decl);
21026 if (pushed_scope)
21027 pop_scope (pushed_scope);
21030 /* If the scope is a dependent type and either we deferred lookup or
21031 we did lookup but didn't find the name, rememeber the name. */
21032 if (decl == error_mark_node && TYPE_P (parser->scope)
21033 && dependent_type_p (parser->scope))
21035 if (tag_type)
21037 tree type;
21039 /* The resolution to Core Issue 180 says that `struct
21040 A::B' should be considered a type-name, even if `A'
21041 is dependent. */
21042 type = make_typename_type (parser->scope, name, tag_type,
21043 /*complain=*/tf_error);
21044 decl = TYPE_NAME (type);
21046 else if (is_template
21047 && (cp_parser_next_token_ends_template_argument_p (parser)
21048 || cp_lexer_next_token_is (parser->lexer,
21049 CPP_CLOSE_PAREN)))
21050 decl = make_unbound_class_template (parser->scope,
21051 name, NULL_TREE,
21052 /*complain=*/tf_error);
21053 else
21054 decl = build_qualified_name (/*type=*/NULL_TREE,
21055 parser->scope, name,
21056 is_template);
21058 parser->qualifying_scope = parser->scope;
21059 parser->object_scope = NULL_TREE;
21061 else if (object_type)
21063 tree object_decl = NULL_TREE;
21064 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21065 OBJECT_TYPE is not a class. */
21066 if (CLASS_TYPE_P (object_type))
21067 /* If the OBJECT_TYPE is a template specialization, it may
21068 be instantiated during name lookup. In that case, errors
21069 may be issued. Even if we rollback the current tentative
21070 parse, those errors are valid. */
21071 object_decl = lookup_member (object_type,
21072 name,
21073 /*protect=*/0,
21074 tag_type != none_type,
21075 tf_warning_or_error);
21076 /* Look it up in the enclosing context, too. */
21077 decl = lookup_name_real (name, tag_type != none_type,
21078 /*nonclass=*/0,
21079 /*block_p=*/true, is_namespace, 0);
21080 parser->object_scope = object_type;
21081 parser->qualifying_scope = NULL_TREE;
21082 if (object_decl)
21083 decl = object_decl;
21085 else
21087 decl = lookup_name_real (name, tag_type != none_type,
21088 /*nonclass=*/0,
21089 /*block_p=*/true, is_namespace, 0);
21090 parser->qualifying_scope = NULL_TREE;
21091 parser->object_scope = NULL_TREE;
21094 /* If the lookup failed, let our caller know. */
21095 if (!decl || decl == error_mark_node)
21096 return error_mark_node;
21098 /* Pull out the template from an injected-class-name (or multiple). */
21099 if (is_template)
21100 decl = maybe_get_template_decl_from_type_decl (decl);
21102 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21103 if (TREE_CODE (decl) == TREE_LIST)
21105 if (ambiguous_decls)
21106 *ambiguous_decls = decl;
21107 /* The error message we have to print is too complicated for
21108 cp_parser_error, so we incorporate its actions directly. */
21109 if (!cp_parser_simulate_error (parser))
21111 error_at (name_location, "reference to %qD is ambiguous",
21112 name);
21113 print_candidates (decl);
21115 return error_mark_node;
21118 gcc_assert (DECL_P (decl)
21119 || TREE_CODE (decl) == OVERLOAD
21120 || TREE_CODE (decl) == SCOPE_REF
21121 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21122 || BASELINK_P (decl));
21124 /* If we have resolved the name of a member declaration, check to
21125 see if the declaration is accessible. When the name resolves to
21126 set of overloaded functions, accessibility is checked when
21127 overload resolution is done.
21129 During an explicit instantiation, access is not checked at all,
21130 as per [temp.explicit]. */
21131 if (DECL_P (decl))
21132 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21134 maybe_record_typedef_use (decl);
21136 return decl;
21139 /* Like cp_parser_lookup_name, but for use in the typical case where
21140 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21141 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21143 static tree
21144 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21146 return cp_parser_lookup_name (parser, name,
21147 none_type,
21148 /*is_template=*/false,
21149 /*is_namespace=*/false,
21150 /*check_dependency=*/true,
21151 /*ambiguous_decls=*/NULL,
21152 location);
21155 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21156 the current context, return the TYPE_DECL. If TAG_NAME_P is
21157 true, the DECL indicates the class being defined in a class-head,
21158 or declared in an elaborated-type-specifier.
21160 Otherwise, return DECL. */
21162 static tree
21163 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21165 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21166 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21168 struct A {
21169 template <typename T> struct B;
21172 template <typename T> struct A::B {};
21174 Similarly, in an elaborated-type-specifier:
21176 namespace N { struct X{}; }
21178 struct A {
21179 template <typename T> friend struct N::X;
21182 However, if the DECL refers to a class type, and we are in
21183 the scope of the class, then the name lookup automatically
21184 finds the TYPE_DECL created by build_self_reference rather
21185 than a TEMPLATE_DECL. For example, in:
21187 template <class T> struct S {
21188 S s;
21191 there is no need to handle such case. */
21193 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21194 return DECL_TEMPLATE_RESULT (decl);
21196 return decl;
21199 /* If too many, or too few, template-parameter lists apply to the
21200 declarator, issue an error message. Returns TRUE if all went well,
21201 and FALSE otherwise. */
21203 static bool
21204 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21205 cp_declarator *declarator,
21206 location_t declarator_location)
21208 switch (declarator->kind)
21210 case cdk_id:
21212 unsigned num_templates = 0;
21213 tree scope = declarator->u.id.qualifying_scope;
21215 if (scope)
21216 num_templates = num_template_headers_for_class (scope);
21217 else if (TREE_CODE (declarator->u.id.unqualified_name)
21218 == TEMPLATE_ID_EXPR)
21219 /* If the DECLARATOR has the form `X<y>' then it uses one
21220 additional level of template parameters. */
21221 ++num_templates;
21223 return cp_parser_check_template_parameters
21224 (parser, num_templates, declarator_location, declarator);
21227 case cdk_function:
21228 case cdk_array:
21229 case cdk_pointer:
21230 case cdk_reference:
21231 case cdk_ptrmem:
21232 return (cp_parser_check_declarator_template_parameters
21233 (parser, declarator->declarator, declarator_location));
21235 case cdk_error:
21236 return true;
21238 default:
21239 gcc_unreachable ();
21241 return false;
21244 /* NUM_TEMPLATES were used in the current declaration. If that is
21245 invalid, return FALSE and issue an error messages. Otherwise,
21246 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21247 declarator and we can print more accurate diagnostics. */
21249 static bool
21250 cp_parser_check_template_parameters (cp_parser* parser,
21251 unsigned num_templates,
21252 location_t location,
21253 cp_declarator *declarator)
21255 /* If there are the same number of template classes and parameter
21256 lists, that's OK. */
21257 if (parser->num_template_parameter_lists == num_templates)
21258 return true;
21259 /* If there are more, but only one more, then we are referring to a
21260 member template. That's OK too. */
21261 if (parser->num_template_parameter_lists == num_templates + 1)
21262 return true;
21263 /* If there are more template classes than parameter lists, we have
21264 something like:
21266 template <class T> void S<T>::R<T>::f (); */
21267 if (parser->num_template_parameter_lists < num_templates)
21269 if (declarator && !current_function_decl)
21270 error_at (location, "specializing member %<%T::%E%> "
21271 "requires %<template<>%> syntax",
21272 declarator->u.id.qualifying_scope,
21273 declarator->u.id.unqualified_name);
21274 else if (declarator)
21275 error_at (location, "invalid declaration of %<%T::%E%>",
21276 declarator->u.id.qualifying_scope,
21277 declarator->u.id.unqualified_name);
21278 else
21279 error_at (location, "too few template-parameter-lists");
21280 return false;
21282 /* Otherwise, there are too many template parameter lists. We have
21283 something like:
21285 template <class T> template <class U> void S::f(); */
21286 error_at (location, "too many template-parameter-lists");
21287 return false;
21290 /* Parse an optional `::' token indicating that the following name is
21291 from the global namespace. If so, PARSER->SCOPE is set to the
21292 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21293 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21294 Returns the new value of PARSER->SCOPE, if the `::' token is
21295 present, and NULL_TREE otherwise. */
21297 static tree
21298 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21300 cp_token *token;
21302 /* Peek at the next token. */
21303 token = cp_lexer_peek_token (parser->lexer);
21304 /* If we're looking at a `::' token then we're starting from the
21305 global namespace, not our current location. */
21306 if (token->type == CPP_SCOPE)
21308 /* Consume the `::' token. */
21309 cp_lexer_consume_token (parser->lexer);
21310 /* Set the SCOPE so that we know where to start the lookup. */
21311 parser->scope = global_namespace;
21312 parser->qualifying_scope = global_namespace;
21313 parser->object_scope = NULL_TREE;
21315 return parser->scope;
21317 else if (!current_scope_valid_p)
21319 parser->scope = NULL_TREE;
21320 parser->qualifying_scope = NULL_TREE;
21321 parser->object_scope = NULL_TREE;
21324 return NULL_TREE;
21327 /* Returns TRUE if the upcoming token sequence is the start of a
21328 constructor declarator. If FRIEND_P is true, the declarator is
21329 preceded by the `friend' specifier. */
21331 static bool
21332 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21334 bool constructor_p;
21335 tree nested_name_specifier;
21336 cp_token *next_token;
21338 /* The common case is that this is not a constructor declarator, so
21339 try to avoid doing lots of work if at all possible. It's not
21340 valid declare a constructor at function scope. */
21341 if (parser->in_function_body)
21342 return false;
21343 /* And only certain tokens can begin a constructor declarator. */
21344 next_token = cp_lexer_peek_token (parser->lexer);
21345 if (next_token->type != CPP_NAME
21346 && next_token->type != CPP_SCOPE
21347 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21348 && next_token->type != CPP_TEMPLATE_ID)
21349 return false;
21351 /* Parse tentatively; we are going to roll back all of the tokens
21352 consumed here. */
21353 cp_parser_parse_tentatively (parser);
21354 /* Assume that we are looking at a constructor declarator. */
21355 constructor_p = true;
21357 /* Look for the optional `::' operator. */
21358 cp_parser_global_scope_opt (parser,
21359 /*current_scope_valid_p=*/false);
21360 /* Look for the nested-name-specifier. */
21361 nested_name_specifier
21362 = (cp_parser_nested_name_specifier_opt (parser,
21363 /*typename_keyword_p=*/false,
21364 /*check_dependency_p=*/false,
21365 /*type_p=*/false,
21366 /*is_declaration=*/false));
21367 /* Outside of a class-specifier, there must be a
21368 nested-name-specifier. */
21369 if (!nested_name_specifier &&
21370 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21371 || friend_p))
21372 constructor_p = false;
21373 else if (nested_name_specifier == error_mark_node)
21374 constructor_p = false;
21376 /* If we have a class scope, this is easy; DR 147 says that S::S always
21377 names the constructor, and no other qualified name could. */
21378 if (constructor_p && nested_name_specifier
21379 && CLASS_TYPE_P (nested_name_specifier))
21381 tree id = cp_parser_unqualified_id (parser,
21382 /*template_keyword_p=*/false,
21383 /*check_dependency_p=*/false,
21384 /*declarator_p=*/true,
21385 /*optional_p=*/false);
21386 if (is_overloaded_fn (id))
21387 id = DECL_NAME (get_first_fn (id));
21388 if (!constructor_name_p (id, nested_name_specifier))
21389 constructor_p = false;
21391 /* If we still think that this might be a constructor-declarator,
21392 look for a class-name. */
21393 else if (constructor_p)
21395 /* If we have:
21397 template <typename T> struct S {
21398 S();
21401 we must recognize that the nested `S' names a class. */
21402 tree type_decl;
21403 type_decl = cp_parser_class_name (parser,
21404 /*typename_keyword_p=*/false,
21405 /*template_keyword_p=*/false,
21406 none_type,
21407 /*check_dependency_p=*/false,
21408 /*class_head_p=*/false,
21409 /*is_declaration=*/false);
21410 /* If there was no class-name, then this is not a constructor. */
21411 constructor_p = !cp_parser_error_occurred (parser);
21413 /* If we're still considering a constructor, we have to see a `(',
21414 to begin the parameter-declaration-clause, followed by either a
21415 `)', an `...', or a decl-specifier. We need to check for a
21416 type-specifier to avoid being fooled into thinking that:
21418 S (f) (int);
21420 is a constructor. (It is actually a function named `f' that
21421 takes one parameter (of type `int') and returns a value of type
21422 `S'. */
21423 if (constructor_p
21424 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21425 constructor_p = false;
21427 if (constructor_p
21428 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21429 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21430 /* A parameter declaration begins with a decl-specifier,
21431 which is either the "attribute" keyword, a storage class
21432 specifier, or (usually) a type-specifier. */
21433 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21435 tree type;
21436 tree pushed_scope = NULL_TREE;
21437 unsigned saved_num_template_parameter_lists;
21439 /* Names appearing in the type-specifier should be looked up
21440 in the scope of the class. */
21441 if (current_class_type)
21442 type = NULL_TREE;
21443 else
21445 type = TREE_TYPE (type_decl);
21446 if (TREE_CODE (type) == TYPENAME_TYPE)
21448 type = resolve_typename_type (type,
21449 /*only_current_p=*/false);
21450 if (TREE_CODE (type) == TYPENAME_TYPE)
21452 cp_parser_abort_tentative_parse (parser);
21453 return false;
21456 pushed_scope = push_scope (type);
21459 /* Inside the constructor parameter list, surrounding
21460 template-parameter-lists do not apply. */
21461 saved_num_template_parameter_lists
21462 = parser->num_template_parameter_lists;
21463 parser->num_template_parameter_lists = 0;
21465 /* Look for the type-specifier. */
21466 cp_parser_type_specifier (parser,
21467 CP_PARSER_FLAGS_NONE,
21468 /*decl_specs=*/NULL,
21469 /*is_declarator=*/true,
21470 /*declares_class_or_enum=*/NULL,
21471 /*is_cv_qualifier=*/NULL);
21473 parser->num_template_parameter_lists
21474 = saved_num_template_parameter_lists;
21476 /* Leave the scope of the class. */
21477 if (pushed_scope)
21478 pop_scope (pushed_scope);
21480 constructor_p = !cp_parser_error_occurred (parser);
21484 /* We did not really want to consume any tokens. */
21485 cp_parser_abort_tentative_parse (parser);
21487 return constructor_p;
21490 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21491 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21492 they must be performed once we are in the scope of the function.
21494 Returns the function defined. */
21496 static tree
21497 cp_parser_function_definition_from_specifiers_and_declarator
21498 (cp_parser* parser,
21499 cp_decl_specifier_seq *decl_specifiers,
21500 tree attributes,
21501 const cp_declarator *declarator)
21503 tree fn;
21504 bool success_p;
21506 /* Begin the function-definition. */
21507 success_p = start_function (decl_specifiers, declarator, attributes);
21509 /* The things we're about to see are not directly qualified by any
21510 template headers we've seen thus far. */
21511 reset_specialization ();
21513 /* If there were names looked up in the decl-specifier-seq that we
21514 did not check, check them now. We must wait until we are in the
21515 scope of the function to perform the checks, since the function
21516 might be a friend. */
21517 perform_deferred_access_checks (tf_warning_or_error);
21519 if (!success_p)
21521 /* Skip the entire function. */
21522 cp_parser_skip_to_end_of_block_or_statement (parser);
21523 fn = error_mark_node;
21525 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21527 /* Seen already, skip it. An error message has already been output. */
21528 cp_parser_skip_to_end_of_block_or_statement (parser);
21529 fn = current_function_decl;
21530 current_function_decl = NULL_TREE;
21531 /* If this is a function from a class, pop the nested class. */
21532 if (current_class_name)
21533 pop_nested_class ();
21535 else
21537 timevar_id_t tv;
21538 if (DECL_DECLARED_INLINE_P (current_function_decl))
21539 tv = TV_PARSE_INLINE;
21540 else
21541 tv = TV_PARSE_FUNC;
21542 timevar_push (tv);
21543 fn = cp_parser_function_definition_after_declarator (parser,
21544 /*inline_p=*/false);
21545 timevar_pop (tv);
21548 return fn;
21551 /* Parse the part of a function-definition that follows the
21552 declarator. INLINE_P is TRUE iff this function is an inline
21553 function defined within a class-specifier.
21555 Returns the function defined. */
21557 static tree
21558 cp_parser_function_definition_after_declarator (cp_parser* parser,
21559 bool inline_p)
21561 tree fn;
21562 bool ctor_initializer_p = false;
21563 bool saved_in_unbraced_linkage_specification_p;
21564 bool saved_in_function_body;
21565 unsigned saved_num_template_parameter_lists;
21566 cp_token *token;
21568 saved_in_function_body = parser->in_function_body;
21569 parser->in_function_body = true;
21570 /* If the next token is `return', then the code may be trying to
21571 make use of the "named return value" extension that G++ used to
21572 support. */
21573 token = cp_lexer_peek_token (parser->lexer);
21574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21576 /* Consume the `return' keyword. */
21577 cp_lexer_consume_token (parser->lexer);
21578 /* Look for the identifier that indicates what value is to be
21579 returned. */
21580 cp_parser_identifier (parser);
21581 /* Issue an error message. */
21582 error_at (token->location,
21583 "named return values are no longer supported");
21584 /* Skip tokens until we reach the start of the function body. */
21585 while (true)
21587 cp_token *token = cp_lexer_peek_token (parser->lexer);
21588 if (token->type == CPP_OPEN_BRACE
21589 || token->type == CPP_EOF
21590 || token->type == CPP_PRAGMA_EOL)
21591 break;
21592 cp_lexer_consume_token (parser->lexer);
21595 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21596 anything declared inside `f'. */
21597 saved_in_unbraced_linkage_specification_p
21598 = parser->in_unbraced_linkage_specification_p;
21599 parser->in_unbraced_linkage_specification_p = false;
21600 /* Inside the function, surrounding template-parameter-lists do not
21601 apply. */
21602 saved_num_template_parameter_lists
21603 = parser->num_template_parameter_lists;
21604 parser->num_template_parameter_lists = 0;
21606 start_lambda_scope (current_function_decl);
21608 /* If the next token is `try', `__transaction_atomic', or
21609 `__transaction_relaxed`, then we are looking at either function-try-block
21610 or function-transaction-block. Note that all of these include the
21611 function-body. */
21612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21613 ctor_initializer_p = cp_parser_function_transaction (parser,
21614 RID_TRANSACTION_ATOMIC);
21615 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21616 RID_TRANSACTION_RELAXED))
21617 ctor_initializer_p = cp_parser_function_transaction (parser,
21618 RID_TRANSACTION_RELAXED);
21619 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21620 ctor_initializer_p = cp_parser_function_try_block (parser);
21621 else
21622 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21623 (parser, /*in_function_try_block=*/false);
21625 finish_lambda_scope ();
21627 /* Finish the function. */
21628 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21629 (inline_p ? 2 : 0));
21630 /* Generate code for it, if necessary. */
21631 expand_or_defer_fn (fn);
21632 /* Restore the saved values. */
21633 parser->in_unbraced_linkage_specification_p
21634 = saved_in_unbraced_linkage_specification_p;
21635 parser->num_template_parameter_lists
21636 = saved_num_template_parameter_lists;
21637 parser->in_function_body = saved_in_function_body;
21639 return fn;
21642 /* Parse a template-declaration, assuming that the `export' (and
21643 `extern') keywords, if present, has already been scanned. MEMBER_P
21644 is as for cp_parser_template_declaration. */
21646 static void
21647 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21649 tree decl = NULL_TREE;
21650 VEC (deferred_access_check,gc) *checks;
21651 tree parameter_list;
21652 bool friend_p = false;
21653 bool need_lang_pop;
21654 cp_token *token;
21656 /* Look for the `template' keyword. */
21657 token = cp_lexer_peek_token (parser->lexer);
21658 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21659 return;
21661 /* And the `<'. */
21662 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21663 return;
21664 if (at_class_scope_p () && current_function_decl)
21666 /* 14.5.2.2 [temp.mem]
21668 A local class shall not have member templates. */
21669 error_at (token->location,
21670 "invalid declaration of member template in local class");
21671 cp_parser_skip_to_end_of_block_or_statement (parser);
21672 return;
21674 /* [temp]
21676 A template ... shall not have C linkage. */
21677 if (current_lang_name == lang_name_c)
21679 error_at (token->location, "template with C linkage");
21680 /* Give it C++ linkage to avoid confusing other parts of the
21681 front end. */
21682 push_lang_context (lang_name_cplusplus);
21683 need_lang_pop = true;
21685 else
21686 need_lang_pop = false;
21688 /* We cannot perform access checks on the template parameter
21689 declarations until we know what is being declared, just as we
21690 cannot check the decl-specifier list. */
21691 push_deferring_access_checks (dk_deferred);
21693 /* If the next token is `>', then we have an invalid
21694 specialization. Rather than complain about an invalid template
21695 parameter, issue an error message here. */
21696 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21698 cp_parser_error (parser, "invalid explicit specialization");
21699 begin_specialization ();
21700 parameter_list = NULL_TREE;
21702 else
21704 /* Parse the template parameters. */
21705 parameter_list = cp_parser_template_parameter_list (parser);
21708 /* Get the deferred access checks from the parameter list. These
21709 will be checked once we know what is being declared, as for a
21710 member template the checks must be performed in the scope of the
21711 class containing the member. */
21712 checks = get_deferred_access_checks ();
21714 /* Look for the `>'. */
21715 cp_parser_skip_to_end_of_template_parameter_list (parser);
21716 /* We just processed one more parameter list. */
21717 ++parser->num_template_parameter_lists;
21718 /* If the next token is `template', there are more template
21719 parameters. */
21720 if (cp_lexer_next_token_is_keyword (parser->lexer,
21721 RID_TEMPLATE))
21722 cp_parser_template_declaration_after_export (parser, member_p);
21723 else if (cxx_dialect >= cxx0x
21724 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21725 decl = cp_parser_alias_declaration (parser);
21726 else
21728 /* There are no access checks when parsing a template, as we do not
21729 know if a specialization will be a friend. */
21730 push_deferring_access_checks (dk_no_check);
21731 token = cp_lexer_peek_token (parser->lexer);
21732 decl = cp_parser_single_declaration (parser,
21733 checks,
21734 member_p,
21735 /*explicit_specialization_p=*/false,
21736 &friend_p);
21737 pop_deferring_access_checks ();
21739 /* If this is a member template declaration, let the front
21740 end know. */
21741 if (member_p && !friend_p && decl)
21743 if (TREE_CODE (decl) == TYPE_DECL)
21744 cp_parser_check_access_in_redeclaration (decl, token->location);
21746 decl = finish_member_template_decl (decl);
21748 else if (friend_p && decl
21749 && (TREE_CODE (decl) == TYPE_DECL
21750 || DECL_TYPE_TEMPLATE_P (decl)))
21751 make_friend_class (current_class_type, TREE_TYPE (decl),
21752 /*complain=*/true);
21754 /* We are done with the current parameter list. */
21755 --parser->num_template_parameter_lists;
21757 pop_deferring_access_checks ();
21759 /* Finish up. */
21760 finish_template_decl (parameter_list);
21762 /* Check the template arguments for a literal operator template. */
21763 if (decl
21764 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21765 && UDLIT_OPER_P (DECL_NAME (decl)))
21767 bool ok = true;
21768 if (parameter_list == NULL_TREE)
21769 ok = false;
21770 else
21772 int num_parms = TREE_VEC_LENGTH (parameter_list);
21773 if (num_parms != 1)
21774 ok = false;
21775 else
21777 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21778 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21779 if (TREE_TYPE (parm) != char_type_node
21780 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21781 ok = false;
21784 if (!ok)
21785 error ("literal operator template %qD has invalid parameter list."
21786 " Expected non-type template argument pack <char...>",
21787 decl);
21789 /* Register member declarations. */
21790 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21791 finish_member_declaration (decl);
21792 /* For the erroneous case of a template with C linkage, we pushed an
21793 implicit C++ linkage scope; exit that scope now. */
21794 if (need_lang_pop)
21795 pop_lang_context ();
21796 /* If DECL is a function template, we must return to parse it later.
21797 (Even though there is no definition, there might be default
21798 arguments that need handling.) */
21799 if (member_p && decl
21800 && (TREE_CODE (decl) == FUNCTION_DECL
21801 || DECL_FUNCTION_TEMPLATE_P (decl)))
21802 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21805 /* Perform the deferred access checks from a template-parameter-list.
21806 CHECKS is a TREE_LIST of access checks, as returned by
21807 get_deferred_access_checks. */
21809 static void
21810 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21812 ++processing_template_parmlist;
21813 perform_access_checks (checks, tf_warning_or_error);
21814 --processing_template_parmlist;
21817 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21818 `function-definition' sequence that follows a template header.
21819 If MEMBER_P is true, this declaration appears in a class scope.
21821 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21822 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21824 static tree
21825 cp_parser_single_declaration (cp_parser* parser,
21826 VEC (deferred_access_check,gc)* checks,
21827 bool member_p,
21828 bool explicit_specialization_p,
21829 bool* friend_p)
21831 int declares_class_or_enum;
21832 tree decl = NULL_TREE;
21833 cp_decl_specifier_seq decl_specifiers;
21834 bool function_definition_p = false;
21835 cp_token *decl_spec_token_start;
21837 /* This function is only used when processing a template
21838 declaration. */
21839 gcc_assert (innermost_scope_kind () == sk_template_parms
21840 || innermost_scope_kind () == sk_template_spec);
21842 /* Defer access checks until we know what is being declared. */
21843 push_deferring_access_checks (dk_deferred);
21845 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21846 alternative. */
21847 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21848 cp_parser_decl_specifier_seq (parser,
21849 CP_PARSER_FLAGS_OPTIONAL,
21850 &decl_specifiers,
21851 &declares_class_or_enum);
21852 if (friend_p)
21853 *friend_p = cp_parser_friend_p (&decl_specifiers);
21855 /* There are no template typedefs. */
21856 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21858 error_at (decl_spec_token_start->location,
21859 "template declaration of %<typedef%>");
21860 decl = error_mark_node;
21863 /* Gather up the access checks that occurred the
21864 decl-specifier-seq. */
21865 stop_deferring_access_checks ();
21867 /* Check for the declaration of a template class. */
21868 if (declares_class_or_enum)
21870 if (cp_parser_declares_only_class_p (parser))
21872 decl = shadow_tag (&decl_specifiers);
21874 /* In this case:
21876 struct C {
21877 friend template <typename T> struct A<T>::B;
21880 A<T>::B will be represented by a TYPENAME_TYPE, and
21881 therefore not recognized by shadow_tag. */
21882 if (friend_p && *friend_p
21883 && !decl
21884 && decl_specifiers.type
21885 && TYPE_P (decl_specifiers.type))
21886 decl = decl_specifiers.type;
21888 if (decl && decl != error_mark_node)
21889 decl = TYPE_NAME (decl);
21890 else
21891 decl = error_mark_node;
21893 /* Perform access checks for template parameters. */
21894 cp_parser_perform_template_parameter_access_checks (checks);
21898 /* Complain about missing 'typename' or other invalid type names. */
21899 if (!decl_specifiers.any_type_specifiers_p
21900 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21902 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21903 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21904 the rest of this declaration. */
21905 decl = error_mark_node;
21906 goto out;
21909 /* If it's not a template class, try for a template function. If
21910 the next token is a `;', then this declaration does not declare
21911 anything. But, if there were errors in the decl-specifiers, then
21912 the error might well have come from an attempted class-specifier.
21913 In that case, there's no need to warn about a missing declarator. */
21914 if (!decl
21915 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21916 || decl_specifiers.type != error_mark_node))
21918 decl = cp_parser_init_declarator (parser,
21919 &decl_specifiers,
21920 checks,
21921 /*function_definition_allowed_p=*/true,
21922 member_p,
21923 declares_class_or_enum,
21924 &function_definition_p,
21925 NULL);
21927 /* 7.1.1-1 [dcl.stc]
21929 A storage-class-specifier shall not be specified in an explicit
21930 specialization... */
21931 if (decl
21932 && explicit_specialization_p
21933 && decl_specifiers.storage_class != sc_none)
21935 error_at (decl_spec_token_start->location,
21936 "explicit template specialization cannot have a storage class");
21937 decl = error_mark_node;
21940 if (decl && TREE_CODE (decl) == VAR_DECL)
21941 check_template_variable (decl);
21944 /* Look for a trailing `;' after the declaration. */
21945 if (!function_definition_p
21946 && (decl == error_mark_node
21947 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21948 cp_parser_skip_to_end_of_block_or_statement (parser);
21950 out:
21951 pop_deferring_access_checks ();
21953 /* Clear any current qualification; whatever comes next is the start
21954 of something new. */
21955 parser->scope = NULL_TREE;
21956 parser->qualifying_scope = NULL_TREE;
21957 parser->object_scope = NULL_TREE;
21959 return decl;
21962 /* Parse a cast-expression that is not the operand of a unary "&". */
21964 static tree
21965 cp_parser_simple_cast_expression (cp_parser *parser)
21967 return cp_parser_cast_expression (parser, /*address_p=*/false,
21968 /*cast_p=*/false, NULL);
21971 /* Parse a functional cast to TYPE. Returns an expression
21972 representing the cast. */
21974 static tree
21975 cp_parser_functional_cast (cp_parser* parser, tree type)
21977 VEC(tree,gc) *vec;
21978 tree expression_list;
21979 tree cast;
21980 bool nonconst_p;
21982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21985 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21987 if (TREE_CODE (type) == TYPE_DECL)
21988 type = TREE_TYPE (type);
21989 return finish_compound_literal (type, expression_list,
21990 tf_warning_or_error);
21994 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21995 /*cast_p=*/true,
21996 /*allow_expansion_p=*/true,
21997 /*non_constant_p=*/NULL);
21998 if (vec == NULL)
21999 expression_list = error_mark_node;
22000 else
22002 expression_list = build_tree_list_vec (vec);
22003 release_tree_vector (vec);
22006 cast = build_functional_cast (type, expression_list,
22007 tf_warning_or_error);
22008 /* [expr.const]/1: In an integral constant expression "only type
22009 conversions to integral or enumeration type can be used". */
22010 if (TREE_CODE (type) == TYPE_DECL)
22011 type = TREE_TYPE (type);
22012 if (cast != error_mark_node
22013 && !cast_valid_in_integral_constant_expression_p (type)
22014 && cp_parser_non_integral_constant_expression (parser,
22015 NIC_CONSTRUCTOR))
22016 return error_mark_node;
22017 return cast;
22020 /* Save the tokens that make up the body of a member function defined
22021 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22022 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22023 specifiers applied to the declaration. Returns the FUNCTION_DECL
22024 for the member function. */
22026 static tree
22027 cp_parser_save_member_function_body (cp_parser* parser,
22028 cp_decl_specifier_seq *decl_specifiers,
22029 cp_declarator *declarator,
22030 tree attributes)
22032 cp_token *first;
22033 cp_token *last;
22034 tree fn;
22036 /* Create the FUNCTION_DECL. */
22037 fn = grokmethod (decl_specifiers, declarator, attributes);
22038 /* If something went badly wrong, bail out now. */
22039 if (fn == error_mark_node)
22041 /* If there's a function-body, skip it. */
22042 if (cp_parser_token_starts_function_definition_p
22043 (cp_lexer_peek_token (parser->lexer)))
22044 cp_parser_skip_to_end_of_block_or_statement (parser);
22045 return error_mark_node;
22048 /* Remember it, if there default args to post process. */
22049 cp_parser_save_default_args (parser, fn);
22051 /* Save away the tokens that make up the body of the
22052 function. */
22053 first = parser->lexer->next_token;
22054 /* We can have braced-init-list mem-initializers before the fn body. */
22055 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22057 cp_lexer_consume_token (parser->lexer);
22058 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22059 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22061 /* cache_group will stop after an un-nested { } pair, too. */
22062 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22063 break;
22065 /* variadic mem-inits have ... after the ')'. */
22066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22067 cp_lexer_consume_token (parser->lexer);
22070 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22071 /* Handle function try blocks. */
22072 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22073 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22074 last = parser->lexer->next_token;
22076 /* Save away the inline definition; we will process it when the
22077 class is complete. */
22078 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22079 DECL_PENDING_INLINE_P (fn) = 1;
22081 /* We need to know that this was defined in the class, so that
22082 friend templates are handled correctly. */
22083 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22085 /* Add FN to the queue of functions to be parsed later. */
22086 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
22088 return fn;
22091 /* Save the tokens that make up the in-class initializer for a non-static
22092 data member. Returns a DEFAULT_ARG. */
22094 static tree
22095 cp_parser_save_nsdmi (cp_parser* parser)
22097 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22100 /* Parse a template-argument-list, as well as the trailing ">" (but
22101 not the opening "<"). See cp_parser_template_argument_list for the
22102 return value. */
22104 static tree
22105 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22107 tree arguments;
22108 tree saved_scope;
22109 tree saved_qualifying_scope;
22110 tree saved_object_scope;
22111 bool saved_greater_than_is_operator_p;
22112 int saved_unevaluated_operand;
22113 int saved_inhibit_evaluation_warnings;
22115 /* [temp.names]
22117 When parsing a template-id, the first non-nested `>' is taken as
22118 the end of the template-argument-list rather than a greater-than
22119 operator. */
22120 saved_greater_than_is_operator_p
22121 = parser->greater_than_is_operator_p;
22122 parser->greater_than_is_operator_p = false;
22123 /* Parsing the argument list may modify SCOPE, so we save it
22124 here. */
22125 saved_scope = parser->scope;
22126 saved_qualifying_scope = parser->qualifying_scope;
22127 saved_object_scope = parser->object_scope;
22128 /* We need to evaluate the template arguments, even though this
22129 template-id may be nested within a "sizeof". */
22130 saved_unevaluated_operand = cp_unevaluated_operand;
22131 cp_unevaluated_operand = 0;
22132 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22133 c_inhibit_evaluation_warnings = 0;
22134 /* Parse the template-argument-list itself. */
22135 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22136 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22137 arguments = NULL_TREE;
22138 else
22139 arguments = cp_parser_template_argument_list (parser);
22140 /* Look for the `>' that ends the template-argument-list. If we find
22141 a '>>' instead, it's probably just a typo. */
22142 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22144 if (cxx_dialect != cxx98)
22146 /* In C++0x, a `>>' in a template argument list or cast
22147 expression is considered to be two separate `>'
22148 tokens. So, change the current token to a `>', but don't
22149 consume it: it will be consumed later when the outer
22150 template argument list (or cast expression) is parsed.
22151 Note that this replacement of `>' for `>>' is necessary
22152 even if we are parsing tentatively: in the tentative
22153 case, after calling
22154 cp_parser_enclosed_template_argument_list we will always
22155 throw away all of the template arguments and the first
22156 closing `>', either because the template argument list
22157 was erroneous or because we are replacing those tokens
22158 with a CPP_TEMPLATE_ID token. The second `>' (which will
22159 not have been thrown away) is needed either to close an
22160 outer template argument list or to complete a new-style
22161 cast. */
22162 cp_token *token = cp_lexer_peek_token (parser->lexer);
22163 token->type = CPP_GREATER;
22165 else if (!saved_greater_than_is_operator_p)
22167 /* If we're in a nested template argument list, the '>>' has
22168 to be a typo for '> >'. We emit the error message, but we
22169 continue parsing and we push a '>' as next token, so that
22170 the argument list will be parsed correctly. Note that the
22171 global source location is still on the token before the
22172 '>>', so we need to say explicitly where we want it. */
22173 cp_token *token = cp_lexer_peek_token (parser->lexer);
22174 error_at (token->location, "%<>>%> should be %<> >%> "
22175 "within a nested template argument list");
22177 token->type = CPP_GREATER;
22179 else
22181 /* If this is not a nested template argument list, the '>>'
22182 is a typo for '>'. Emit an error message and continue.
22183 Same deal about the token location, but here we can get it
22184 right by consuming the '>>' before issuing the diagnostic. */
22185 cp_token *token = cp_lexer_consume_token (parser->lexer);
22186 error_at (token->location,
22187 "spurious %<>>%>, use %<>%> to terminate "
22188 "a template argument list");
22191 else
22192 cp_parser_skip_to_end_of_template_parameter_list (parser);
22193 /* The `>' token might be a greater-than operator again now. */
22194 parser->greater_than_is_operator_p
22195 = saved_greater_than_is_operator_p;
22196 /* Restore the SAVED_SCOPE. */
22197 parser->scope = saved_scope;
22198 parser->qualifying_scope = saved_qualifying_scope;
22199 parser->object_scope = saved_object_scope;
22200 cp_unevaluated_operand = saved_unevaluated_operand;
22201 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22203 return arguments;
22206 /* MEMBER_FUNCTION is a member function, or a friend. If default
22207 arguments, or the body of the function have not yet been parsed,
22208 parse them now. */
22210 static void
22211 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22213 timevar_push (TV_PARSE_INMETH);
22214 /* If this member is a template, get the underlying
22215 FUNCTION_DECL. */
22216 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22217 member_function = DECL_TEMPLATE_RESULT (member_function);
22219 /* There should not be any class definitions in progress at this
22220 point; the bodies of members are only parsed outside of all class
22221 definitions. */
22222 gcc_assert (parser->num_classes_being_defined == 0);
22223 /* While we're parsing the member functions we might encounter more
22224 classes. We want to handle them right away, but we don't want
22225 them getting mixed up with functions that are currently in the
22226 queue. */
22227 push_unparsed_function_queues (parser);
22229 /* Make sure that any template parameters are in scope. */
22230 maybe_begin_member_template_processing (member_function);
22232 /* If the body of the function has not yet been parsed, parse it
22233 now. */
22234 if (DECL_PENDING_INLINE_P (member_function))
22236 tree function_scope;
22237 cp_token_cache *tokens;
22239 /* The function is no longer pending; we are processing it. */
22240 tokens = DECL_PENDING_INLINE_INFO (member_function);
22241 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22242 DECL_PENDING_INLINE_P (member_function) = 0;
22244 /* If this is a local class, enter the scope of the containing
22245 function. */
22246 function_scope = current_function_decl;
22247 if (function_scope)
22248 push_function_context ();
22250 /* Push the body of the function onto the lexer stack. */
22251 cp_parser_push_lexer_for_tokens (parser, tokens);
22253 /* Let the front end know that we going to be defining this
22254 function. */
22255 start_preparsed_function (member_function, NULL_TREE,
22256 SF_PRE_PARSED | SF_INCLASS_INLINE);
22258 /* Don't do access checking if it is a templated function. */
22259 if (processing_template_decl)
22260 push_deferring_access_checks (dk_no_check);
22262 /* Now, parse the body of the function. */
22263 cp_parser_function_definition_after_declarator (parser,
22264 /*inline_p=*/true);
22266 if (processing_template_decl)
22267 pop_deferring_access_checks ();
22269 /* Leave the scope of the containing function. */
22270 if (function_scope)
22271 pop_function_context ();
22272 cp_parser_pop_lexer (parser);
22275 /* Remove any template parameters from the symbol table. */
22276 maybe_end_member_template_processing ();
22278 /* Restore the queue. */
22279 pop_unparsed_function_queues (parser);
22280 timevar_pop (TV_PARSE_INMETH);
22283 /* If DECL contains any default args, remember it on the unparsed
22284 functions queue. */
22286 static void
22287 cp_parser_save_default_args (cp_parser* parser, tree decl)
22289 tree probe;
22291 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22292 probe;
22293 probe = TREE_CHAIN (probe))
22294 if (TREE_PURPOSE (probe))
22296 cp_default_arg_entry entry = {current_class_type, decl};
22297 VEC_safe_push (cp_default_arg_entry, gc,
22298 unparsed_funs_with_default_args, entry);
22299 break;
22303 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22304 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22305 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22306 from the parameter-type-list. */
22308 static tree
22309 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22310 tree default_arg, tree parmtype)
22312 cp_token_cache *tokens;
22313 tree parsed_arg;
22314 bool dummy;
22316 if (default_arg == error_mark_node)
22317 return error_mark_node;
22319 /* Push the saved tokens for the default argument onto the parser's
22320 lexer stack. */
22321 tokens = DEFARG_TOKENS (default_arg);
22322 cp_parser_push_lexer_for_tokens (parser, tokens);
22324 start_lambda_scope (decl);
22326 /* Parse the default argument. */
22327 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22328 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22329 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22331 finish_lambda_scope ();
22333 if (parsed_arg == error_mark_node)
22334 cp_parser_skip_to_end_of_statement (parser);
22336 if (!processing_template_decl)
22338 /* In a non-template class, check conversions now. In a template,
22339 we'll wait and instantiate these as needed. */
22340 if (TREE_CODE (decl) == PARM_DECL)
22341 parsed_arg = check_default_argument (parmtype, parsed_arg);
22342 else
22344 int flags = LOOKUP_IMPLICIT;
22345 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22346 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22347 flags = LOOKUP_NORMAL;
22348 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22352 /* If the token stream has not been completely used up, then
22353 there was extra junk after the end of the default
22354 argument. */
22355 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22357 if (TREE_CODE (decl) == PARM_DECL)
22358 cp_parser_error (parser, "expected %<,%>");
22359 else
22360 cp_parser_error (parser, "expected %<;%>");
22363 /* Revert to the main lexer. */
22364 cp_parser_pop_lexer (parser);
22366 return parsed_arg;
22369 /* FIELD is a non-static data member with an initializer which we saved for
22370 later; parse it now. */
22372 static void
22373 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22375 tree def;
22377 push_unparsed_function_queues (parser);
22378 def = cp_parser_late_parse_one_default_arg (parser, field,
22379 DECL_INITIAL (field),
22380 NULL_TREE);
22381 pop_unparsed_function_queues (parser);
22383 DECL_INITIAL (field) = def;
22386 /* FN is a FUNCTION_DECL which may contains a parameter with an
22387 unparsed DEFAULT_ARG. Parse the default args now. This function
22388 assumes that the current scope is the scope in which the default
22389 argument should be processed. */
22391 static void
22392 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22394 bool saved_local_variables_forbidden_p;
22395 tree parm, parmdecl;
22397 /* While we're parsing the default args, we might (due to the
22398 statement expression extension) encounter more classes. We want
22399 to handle them right away, but we don't want them getting mixed
22400 up with default args that are currently in the queue. */
22401 push_unparsed_function_queues (parser);
22403 /* Local variable names (and the `this' keyword) may not appear
22404 in a default argument. */
22405 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22406 parser->local_variables_forbidden_p = true;
22408 push_defarg_context (fn);
22410 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22411 parmdecl = DECL_ARGUMENTS (fn);
22412 parm && parm != void_list_node;
22413 parm = TREE_CHAIN (parm),
22414 parmdecl = DECL_CHAIN (parmdecl))
22416 tree default_arg = TREE_PURPOSE (parm);
22417 tree parsed_arg;
22418 VEC(tree,gc) *insts;
22419 tree copy;
22420 unsigned ix;
22422 if (!default_arg)
22423 continue;
22425 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22426 /* This can happen for a friend declaration for a function
22427 already declared with default arguments. */
22428 continue;
22430 parsed_arg
22431 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22432 default_arg,
22433 TREE_VALUE (parm));
22434 if (parsed_arg == error_mark_node)
22436 continue;
22439 TREE_PURPOSE (parm) = parsed_arg;
22441 /* Update any instantiations we've already created. */
22442 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22443 VEC_iterate (tree, insts, ix, copy); ix++)
22444 TREE_PURPOSE (copy) = parsed_arg;
22447 pop_defarg_context ();
22449 /* Make sure no default arg is missing. */
22450 check_default_args (fn);
22452 /* Restore the state of local_variables_forbidden_p. */
22453 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22455 /* Restore the queue. */
22456 pop_unparsed_function_queues (parser);
22459 /* Parse the operand of `sizeof' (or a similar operator). Returns
22460 either a TYPE or an expression, depending on the form of the
22461 input. The KEYWORD indicates which kind of expression we have
22462 encountered. */
22464 static tree
22465 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22467 tree expr = NULL_TREE;
22468 const char *saved_message;
22469 char *tmp;
22470 bool saved_integral_constant_expression_p;
22471 bool saved_non_integral_constant_expression_p;
22472 bool pack_expansion_p = false;
22474 /* Types cannot be defined in a `sizeof' expression. Save away the
22475 old message. */
22476 saved_message = parser->type_definition_forbidden_message;
22477 /* And create the new one. */
22478 tmp = concat ("types may not be defined in %<",
22479 IDENTIFIER_POINTER (ridpointers[keyword]),
22480 "%> expressions", NULL);
22481 parser->type_definition_forbidden_message = tmp;
22483 /* The restrictions on constant-expressions do not apply inside
22484 sizeof expressions. */
22485 saved_integral_constant_expression_p
22486 = parser->integral_constant_expression_p;
22487 saved_non_integral_constant_expression_p
22488 = parser->non_integral_constant_expression_p;
22489 parser->integral_constant_expression_p = false;
22491 /* If it's a `...', then we are computing the length of a parameter
22492 pack. */
22493 if (keyword == RID_SIZEOF
22494 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22496 /* Consume the `...'. */
22497 cp_lexer_consume_token (parser->lexer);
22498 maybe_warn_variadic_templates ();
22500 /* Note that this is an expansion. */
22501 pack_expansion_p = true;
22504 /* Do not actually evaluate the expression. */
22505 ++cp_unevaluated_operand;
22506 ++c_inhibit_evaluation_warnings;
22507 /* If it's a `(', then we might be looking at the type-id
22508 construction. */
22509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22511 tree type;
22512 bool saved_in_type_id_in_expr_p;
22514 /* We can't be sure yet whether we're looking at a type-id or an
22515 expression. */
22516 cp_parser_parse_tentatively (parser);
22517 /* Consume the `('. */
22518 cp_lexer_consume_token (parser->lexer);
22519 /* Parse the type-id. */
22520 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22521 parser->in_type_id_in_expr_p = true;
22522 type = cp_parser_type_id (parser);
22523 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22524 /* Now, look for the trailing `)'. */
22525 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22526 /* If all went well, then we're done. */
22527 if (cp_parser_parse_definitely (parser))
22529 cp_decl_specifier_seq decl_specs;
22531 /* Build a trivial decl-specifier-seq. */
22532 clear_decl_specs (&decl_specs);
22533 decl_specs.type = type;
22535 /* Call grokdeclarator to figure out what type this is. */
22536 expr = grokdeclarator (NULL,
22537 &decl_specs,
22538 TYPENAME,
22539 /*initialized=*/0,
22540 /*attrlist=*/NULL);
22543 else if (pack_expansion_p)
22544 permerror (cp_lexer_peek_token (parser->lexer)->location,
22545 "%<sizeof...%> argument must be surrounded by parentheses");
22547 /* If the type-id production did not work out, then we must be
22548 looking at the unary-expression production. */
22549 if (!expr)
22550 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22551 /*cast_p=*/false, NULL);
22553 if (pack_expansion_p)
22554 /* Build a pack expansion. */
22555 expr = make_pack_expansion (expr);
22557 /* Go back to evaluating expressions. */
22558 --cp_unevaluated_operand;
22559 --c_inhibit_evaluation_warnings;
22561 /* Free the message we created. */
22562 free (tmp);
22563 /* And restore the old one. */
22564 parser->type_definition_forbidden_message = saved_message;
22565 parser->integral_constant_expression_p
22566 = saved_integral_constant_expression_p;
22567 parser->non_integral_constant_expression_p
22568 = saved_non_integral_constant_expression_p;
22570 return expr;
22573 /* If the current declaration has no declarator, return true. */
22575 static bool
22576 cp_parser_declares_only_class_p (cp_parser *parser)
22578 /* If the next token is a `;' or a `,' then there is no
22579 declarator. */
22580 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22581 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22584 /* Update the DECL_SPECS to reflect the storage class indicated by
22585 KEYWORD. */
22587 static void
22588 cp_parser_set_storage_class (cp_parser *parser,
22589 cp_decl_specifier_seq *decl_specs,
22590 enum rid keyword,
22591 cp_token *token)
22593 cp_storage_class storage_class;
22595 if (parser->in_unbraced_linkage_specification_p)
22597 error_at (token->location, "invalid use of %qD in linkage specification",
22598 ridpointers[keyword]);
22599 return;
22601 else if (decl_specs->storage_class != sc_none)
22603 decl_specs->conflicting_specifiers_p = true;
22604 return;
22607 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22608 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22609 && decl_specs->gnu_thread_keyword_p)
22611 pedwarn (decl_specs->locations[ds_thread], 0,
22612 "%<__thread%> before %qD", ridpointers[keyword]);
22615 switch (keyword)
22617 case RID_AUTO:
22618 storage_class = sc_auto;
22619 break;
22620 case RID_REGISTER:
22621 storage_class = sc_register;
22622 break;
22623 case RID_STATIC:
22624 storage_class = sc_static;
22625 break;
22626 case RID_EXTERN:
22627 storage_class = sc_extern;
22628 break;
22629 case RID_MUTABLE:
22630 storage_class = sc_mutable;
22631 break;
22632 default:
22633 gcc_unreachable ();
22635 decl_specs->storage_class = storage_class;
22636 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22638 /* A storage class specifier cannot be applied alongside a typedef
22639 specifier. If there is a typedef specifier present then set
22640 conflicting_specifiers_p which will trigger an error later
22641 on in grokdeclarator. */
22642 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22643 decl_specs->conflicting_specifiers_p = true;
22646 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22647 is true, the type is a class or enum definition. */
22649 static void
22650 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22651 tree type_spec,
22652 cp_token *token,
22653 bool type_definition_p)
22655 decl_specs->any_specifiers_p = true;
22657 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22658 (with, for example, in "typedef int wchar_t;") we remember that
22659 this is what happened. In system headers, we ignore these
22660 declarations so that G++ can work with system headers that are not
22661 C++-safe. */
22662 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22663 && !type_definition_p
22664 && (type_spec == boolean_type_node
22665 || type_spec == char16_type_node
22666 || type_spec == char32_type_node
22667 || type_spec == wchar_type_node)
22668 && (decl_specs->type
22669 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22670 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22671 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22672 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22674 decl_specs->redefined_builtin_type = type_spec;
22675 set_and_check_decl_spec_loc (decl_specs,
22676 ds_redefined_builtin_type_spec,
22677 token);
22678 if (!decl_specs->type)
22680 decl_specs->type = type_spec;
22681 decl_specs->type_definition_p = false;
22682 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22685 else if (decl_specs->type)
22686 decl_specs->multiple_types_p = true;
22687 else
22689 decl_specs->type = type_spec;
22690 decl_specs->type_definition_p = type_definition_p;
22691 decl_specs->redefined_builtin_type = NULL_TREE;
22692 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22696 /* True iff TOKEN is the GNU keyword __thread. */
22698 static bool
22699 token_is__thread (cp_token *token)
22701 gcc_assert (token->keyword == RID_THREAD);
22702 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22705 /* Set the location for a declarator specifier and check if it is
22706 duplicated.
22708 DECL_SPECS is the sequence of declarator specifiers onto which to
22709 set the location.
22711 DS is the single declarator specifier to set which location is to
22712 be set onto the existing sequence of declarators.
22714 LOCATION is the location for the declarator specifier to
22715 consider. */
22717 static void
22718 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22719 cp_decl_spec ds, cp_token *token)
22721 gcc_assert (ds < ds_last);
22723 if (decl_specs == NULL)
22724 return;
22726 source_location location = token->location;
22728 if (decl_specs->locations[ds] == 0)
22730 decl_specs->locations[ds] = location;
22731 if (ds == ds_thread)
22732 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22734 else
22736 if (ds == ds_long)
22738 if (decl_specs->locations[ds_long_long] != 0)
22739 error_at (location,
22740 "%<long long long%> is too long for GCC");
22741 else
22743 decl_specs->locations[ds_long_long] = location;
22744 pedwarn_cxx98 (location,
22745 OPT_Wlong_long,
22746 "ISO C++ 1998 does not support %<long long%>");
22749 else if (ds == ds_thread)
22751 bool gnu = token_is__thread (token);
22752 if (gnu != decl_specs->gnu_thread_keyword_p)
22753 error_at (location,
22754 "both %<__thread%> and %<thread_local%> specified");
22755 else
22756 error_at (location, "duplicate %qD", token->u.value);
22758 else
22760 static const char *const decl_spec_names[] = {
22761 "signed",
22762 "unsigned",
22763 "short",
22764 "long",
22765 "const",
22766 "volatile",
22767 "restrict",
22768 "inline",
22769 "virtual",
22770 "explicit",
22771 "friend",
22772 "typedef",
22773 "using",
22774 "constexpr",
22775 "__complex"
22777 error_at (location,
22778 "duplicate %qs", decl_spec_names[ds]);
22783 /* Return true iff the declarator specifier DS is present in the
22784 sequence of declarator specifiers DECL_SPECS. */
22786 bool
22787 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22788 cp_decl_spec ds)
22790 gcc_assert (ds < ds_last);
22792 if (decl_specs == NULL)
22793 return false;
22795 return decl_specs->locations[ds] != 0;
22798 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22799 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22801 static bool
22802 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22804 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22807 /* Issue an error message indicating that TOKEN_DESC was expected.
22808 If KEYWORD is true, it indicated this function is called by
22809 cp_parser_require_keword and the required token can only be
22810 a indicated keyword. */
22812 static void
22813 cp_parser_required_error (cp_parser *parser,
22814 required_token token_desc,
22815 bool keyword)
22817 switch (token_desc)
22819 case RT_NEW:
22820 cp_parser_error (parser, "expected %<new%>");
22821 return;
22822 case RT_DELETE:
22823 cp_parser_error (parser, "expected %<delete%>");
22824 return;
22825 case RT_RETURN:
22826 cp_parser_error (parser, "expected %<return%>");
22827 return;
22828 case RT_WHILE:
22829 cp_parser_error (parser, "expected %<while%>");
22830 return;
22831 case RT_EXTERN:
22832 cp_parser_error (parser, "expected %<extern%>");
22833 return;
22834 case RT_STATIC_ASSERT:
22835 cp_parser_error (parser, "expected %<static_assert%>");
22836 return;
22837 case RT_DECLTYPE:
22838 cp_parser_error (parser, "expected %<decltype%>");
22839 return;
22840 case RT_OPERATOR:
22841 cp_parser_error (parser, "expected %<operator%>");
22842 return;
22843 case RT_CLASS:
22844 cp_parser_error (parser, "expected %<class%>");
22845 return;
22846 case RT_TEMPLATE:
22847 cp_parser_error (parser, "expected %<template%>");
22848 return;
22849 case RT_NAMESPACE:
22850 cp_parser_error (parser, "expected %<namespace%>");
22851 return;
22852 case RT_USING:
22853 cp_parser_error (parser, "expected %<using%>");
22854 return;
22855 case RT_ASM:
22856 cp_parser_error (parser, "expected %<asm%>");
22857 return;
22858 case RT_TRY:
22859 cp_parser_error (parser, "expected %<try%>");
22860 return;
22861 case RT_CATCH:
22862 cp_parser_error (parser, "expected %<catch%>");
22863 return;
22864 case RT_THROW:
22865 cp_parser_error (parser, "expected %<throw%>");
22866 return;
22867 case RT_LABEL:
22868 cp_parser_error (parser, "expected %<__label__%>");
22869 return;
22870 case RT_AT_TRY:
22871 cp_parser_error (parser, "expected %<@try%>");
22872 return;
22873 case RT_AT_SYNCHRONIZED:
22874 cp_parser_error (parser, "expected %<@synchronized%>");
22875 return;
22876 case RT_AT_THROW:
22877 cp_parser_error (parser, "expected %<@throw%>");
22878 return;
22879 case RT_TRANSACTION_ATOMIC:
22880 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22881 return;
22882 case RT_TRANSACTION_RELAXED:
22883 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22884 return;
22885 default:
22886 break;
22888 if (!keyword)
22890 switch (token_desc)
22892 case RT_SEMICOLON:
22893 cp_parser_error (parser, "expected %<;%>");
22894 return;
22895 case RT_OPEN_PAREN:
22896 cp_parser_error (parser, "expected %<(%>");
22897 return;
22898 case RT_CLOSE_BRACE:
22899 cp_parser_error (parser, "expected %<}%>");
22900 return;
22901 case RT_OPEN_BRACE:
22902 cp_parser_error (parser, "expected %<{%>");
22903 return;
22904 case RT_CLOSE_SQUARE:
22905 cp_parser_error (parser, "expected %<]%>");
22906 return;
22907 case RT_OPEN_SQUARE:
22908 cp_parser_error (parser, "expected %<[%>");
22909 return;
22910 case RT_COMMA:
22911 cp_parser_error (parser, "expected %<,%>");
22912 return;
22913 case RT_SCOPE:
22914 cp_parser_error (parser, "expected %<::%>");
22915 return;
22916 case RT_LESS:
22917 cp_parser_error (parser, "expected %<<%>");
22918 return;
22919 case RT_GREATER:
22920 cp_parser_error (parser, "expected %<>%>");
22921 return;
22922 case RT_EQ:
22923 cp_parser_error (parser, "expected %<=%>");
22924 return;
22925 case RT_ELLIPSIS:
22926 cp_parser_error (parser, "expected %<...%>");
22927 return;
22928 case RT_MULT:
22929 cp_parser_error (parser, "expected %<*%>");
22930 return;
22931 case RT_COMPL:
22932 cp_parser_error (parser, "expected %<~%>");
22933 return;
22934 case RT_COLON:
22935 cp_parser_error (parser, "expected %<:%>");
22936 return;
22937 case RT_COLON_SCOPE:
22938 cp_parser_error (parser, "expected %<:%> or %<::%>");
22939 return;
22940 case RT_CLOSE_PAREN:
22941 cp_parser_error (parser, "expected %<)%>");
22942 return;
22943 case RT_COMMA_CLOSE_PAREN:
22944 cp_parser_error (parser, "expected %<,%> or %<)%>");
22945 return;
22946 case RT_PRAGMA_EOL:
22947 cp_parser_error (parser, "expected end of line");
22948 return;
22949 case RT_NAME:
22950 cp_parser_error (parser, "expected identifier");
22951 return;
22952 case RT_SELECT:
22953 cp_parser_error (parser, "expected selection-statement");
22954 return;
22955 case RT_INTERATION:
22956 cp_parser_error (parser, "expected iteration-statement");
22957 return;
22958 case RT_JUMP:
22959 cp_parser_error (parser, "expected jump-statement");
22960 return;
22961 case RT_CLASS_KEY:
22962 cp_parser_error (parser, "expected class-key");
22963 return;
22964 case RT_CLASS_TYPENAME_TEMPLATE:
22965 cp_parser_error (parser,
22966 "expected %<class%>, %<typename%>, or %<template%>");
22967 return;
22968 default:
22969 gcc_unreachable ();
22972 else
22973 gcc_unreachable ();
22978 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22979 issue an error message indicating that TOKEN_DESC was expected.
22981 Returns the token consumed, if the token had the appropriate type.
22982 Otherwise, returns NULL. */
22984 static cp_token *
22985 cp_parser_require (cp_parser* parser,
22986 enum cpp_ttype type,
22987 required_token token_desc)
22989 if (cp_lexer_next_token_is (parser->lexer, type))
22990 return cp_lexer_consume_token (parser->lexer);
22991 else
22993 /* Output the MESSAGE -- unless we're parsing tentatively. */
22994 if (!cp_parser_simulate_error (parser))
22995 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22996 return NULL;
23000 /* An error message is produced if the next token is not '>'.
23001 All further tokens are skipped until the desired token is
23002 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23004 static void
23005 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23007 /* Current level of '< ... >'. */
23008 unsigned level = 0;
23009 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23010 unsigned nesting_depth = 0;
23012 /* Are we ready, yet? If not, issue error message. */
23013 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23014 return;
23016 /* Skip tokens until the desired token is found. */
23017 while (true)
23019 /* Peek at the next token. */
23020 switch (cp_lexer_peek_token (parser->lexer)->type)
23022 case CPP_LESS:
23023 if (!nesting_depth)
23024 ++level;
23025 break;
23027 case CPP_RSHIFT:
23028 if (cxx_dialect == cxx98)
23029 /* C++0x views the `>>' operator as two `>' tokens, but
23030 C++98 does not. */
23031 break;
23032 else if (!nesting_depth && level-- == 0)
23034 /* We've hit a `>>' where the first `>' closes the
23035 template argument list, and the second `>' is
23036 spurious. Just consume the `>>' and stop; we've
23037 already produced at least one error. */
23038 cp_lexer_consume_token (parser->lexer);
23039 return;
23041 /* Fall through for C++0x, so we handle the second `>' in
23042 the `>>'. */
23044 case CPP_GREATER:
23045 if (!nesting_depth && level-- == 0)
23047 /* We've reached the token we want, consume it and stop. */
23048 cp_lexer_consume_token (parser->lexer);
23049 return;
23051 break;
23053 case CPP_OPEN_PAREN:
23054 case CPP_OPEN_SQUARE:
23055 ++nesting_depth;
23056 break;
23058 case CPP_CLOSE_PAREN:
23059 case CPP_CLOSE_SQUARE:
23060 if (nesting_depth-- == 0)
23061 return;
23062 break;
23064 case CPP_EOF:
23065 case CPP_PRAGMA_EOL:
23066 case CPP_SEMICOLON:
23067 case CPP_OPEN_BRACE:
23068 case CPP_CLOSE_BRACE:
23069 /* The '>' was probably forgotten, don't look further. */
23070 return;
23072 default:
23073 break;
23076 /* Consume this token. */
23077 cp_lexer_consume_token (parser->lexer);
23081 /* If the next token is the indicated keyword, consume it. Otherwise,
23082 issue an error message indicating that TOKEN_DESC was expected.
23084 Returns the token consumed, if the token had the appropriate type.
23085 Otherwise, returns NULL. */
23087 static cp_token *
23088 cp_parser_require_keyword (cp_parser* parser,
23089 enum rid keyword,
23090 required_token token_desc)
23092 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23094 if (token && token->keyword != keyword)
23096 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23097 return NULL;
23100 return token;
23103 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23104 function-definition. */
23106 static bool
23107 cp_parser_token_starts_function_definition_p (cp_token* token)
23109 return (/* An ordinary function-body begins with an `{'. */
23110 token->type == CPP_OPEN_BRACE
23111 /* A ctor-initializer begins with a `:'. */
23112 || token->type == CPP_COLON
23113 /* A function-try-block begins with `try'. */
23114 || token->keyword == RID_TRY
23115 /* A function-transaction-block begins with `__transaction_atomic'
23116 or `__transaction_relaxed'. */
23117 || token->keyword == RID_TRANSACTION_ATOMIC
23118 || token->keyword == RID_TRANSACTION_RELAXED
23119 /* The named return value extension begins with `return'. */
23120 || token->keyword == RID_RETURN);
23123 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23124 definition. */
23126 static bool
23127 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23129 cp_token *token;
23131 token = cp_lexer_peek_token (parser->lexer);
23132 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23135 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23136 C++0x) ending a template-argument. */
23138 static bool
23139 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23141 cp_token *token;
23143 token = cp_lexer_peek_token (parser->lexer);
23144 return (token->type == CPP_COMMA
23145 || token->type == CPP_GREATER
23146 || token->type == CPP_ELLIPSIS
23147 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23150 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23151 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23153 static bool
23154 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23155 size_t n)
23157 cp_token *token;
23159 token = cp_lexer_peek_nth_token (parser->lexer, n);
23160 if (token->type == CPP_LESS)
23161 return true;
23162 /* Check for the sequence `<::' in the original code. It would be lexed as
23163 `[:', where `[' is a digraph, and there is no whitespace before
23164 `:'. */
23165 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23167 cp_token *token2;
23168 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23169 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23170 return true;
23172 return false;
23175 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23176 or none_type otherwise. */
23178 static enum tag_types
23179 cp_parser_token_is_class_key (cp_token* token)
23181 switch (token->keyword)
23183 case RID_CLASS:
23184 return class_type;
23185 case RID_STRUCT:
23186 return record_type;
23187 case RID_UNION:
23188 return union_type;
23190 default:
23191 return none_type;
23195 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23197 static void
23198 cp_parser_check_class_key (enum tag_types class_key, tree type)
23200 if (type == error_mark_node)
23201 return;
23202 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23204 permerror (input_location, "%qs tag used in naming %q#T",
23205 class_key == union_type ? "union"
23206 : class_key == record_type ? "struct" : "class",
23207 type);
23208 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23209 "%q#T was previously declared here", type);
23213 /* Issue an error message if DECL is redeclared with different
23214 access than its original declaration [class.access.spec/3].
23215 This applies to nested classes and nested class templates.
23216 [class.mem/1]. */
23218 static void
23219 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23221 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23222 return;
23224 if ((TREE_PRIVATE (decl)
23225 != (current_access_specifier == access_private_node))
23226 || (TREE_PROTECTED (decl)
23227 != (current_access_specifier == access_protected_node)))
23228 error_at (location, "%qD redeclared with different access", decl);
23231 /* Look for the `template' keyword, as a syntactic disambiguator.
23232 Return TRUE iff it is present, in which case it will be
23233 consumed. */
23235 static bool
23236 cp_parser_optional_template_keyword (cp_parser *parser)
23238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23240 /* The `template' keyword can only be used within templates;
23241 outside templates the parser can always figure out what is a
23242 template and what is not. */
23243 if (!processing_template_decl)
23245 cp_token *token = cp_lexer_peek_token (parser->lexer);
23246 error_at (token->location,
23247 "%<template%> (as a disambiguator) is only allowed "
23248 "within templates");
23249 /* If this part of the token stream is rescanned, the same
23250 error message would be generated. So, we purge the token
23251 from the stream. */
23252 cp_lexer_purge_token (parser->lexer);
23253 return false;
23255 else
23257 /* Consume the `template' keyword. */
23258 cp_lexer_consume_token (parser->lexer);
23259 return true;
23263 return false;
23266 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23267 set PARSER->SCOPE, and perform other related actions. */
23269 static void
23270 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23272 int i;
23273 struct tree_check *check_value;
23274 deferred_access_check *chk;
23275 VEC (deferred_access_check,gc) *checks;
23277 /* Get the stored value. */
23278 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23279 /* Perform any access checks that were deferred. */
23280 checks = check_value->checks;
23281 if (checks)
23283 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
23284 perform_or_defer_access_check (chk->binfo,
23285 chk->decl,
23286 chk->diag_decl, tf_warning_or_error);
23288 /* Set the scope from the stored value. */
23289 parser->scope = check_value->value;
23290 parser->qualifying_scope = check_value->qualifying_scope;
23291 parser->object_scope = NULL_TREE;
23294 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23295 encounter the end of a block before what we were looking for. */
23297 static bool
23298 cp_parser_cache_group (cp_parser *parser,
23299 enum cpp_ttype end,
23300 unsigned depth)
23302 while (true)
23304 cp_token *token = cp_lexer_peek_token (parser->lexer);
23306 /* Abort a parenthesized expression if we encounter a semicolon. */
23307 if ((end == CPP_CLOSE_PAREN || depth == 0)
23308 && token->type == CPP_SEMICOLON)
23309 return true;
23310 /* If we've reached the end of the file, stop. */
23311 if (token->type == CPP_EOF
23312 || (end != CPP_PRAGMA_EOL
23313 && token->type == CPP_PRAGMA_EOL))
23314 return true;
23315 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23316 /* We've hit the end of an enclosing block, so there's been some
23317 kind of syntax error. */
23318 return true;
23320 /* Consume the token. */
23321 cp_lexer_consume_token (parser->lexer);
23322 /* See if it starts a new group. */
23323 if (token->type == CPP_OPEN_BRACE)
23325 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23326 /* In theory this should probably check end == '}', but
23327 cp_parser_save_member_function_body needs it to exit
23328 after either '}' or ')' when called with ')'. */
23329 if (depth == 0)
23330 return false;
23332 else if (token->type == CPP_OPEN_PAREN)
23334 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23335 if (depth == 0 && end == CPP_CLOSE_PAREN)
23336 return false;
23338 else if (token->type == CPP_PRAGMA)
23339 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23340 else if (token->type == end)
23341 return false;
23345 /* Like above, for caching a default argument or NSDMI. Both of these are
23346 terminated by a non-nested comma, but it can be unclear whether or not a
23347 comma is nested in a template argument list unless we do more parsing.
23348 In order to handle this ambiguity, when we encounter a ',' after a '<'
23349 we try to parse what follows as a parameter-declaration-list (in the
23350 case of a default argument) or a member-declarator (in the case of an
23351 NSDMI). If that succeeds, then we stop caching. */
23353 static tree
23354 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23356 unsigned depth = 0;
23357 int maybe_template_id = 0;
23358 cp_token *first_token;
23359 cp_token *token;
23360 tree default_argument;
23362 /* Add tokens until we have processed the entire default
23363 argument. We add the range [first_token, token). */
23364 first_token = cp_lexer_peek_token (parser->lexer);
23365 if (first_token->type == CPP_OPEN_BRACE)
23367 /* For list-initialization, this is straightforward. */
23368 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23369 token = cp_lexer_peek_token (parser->lexer);
23371 else while (true)
23373 bool done = false;
23375 /* Peek at the next token. */
23376 token = cp_lexer_peek_token (parser->lexer);
23377 /* What we do depends on what token we have. */
23378 switch (token->type)
23380 /* In valid code, a default argument must be
23381 immediately followed by a `,' `)', or `...'. */
23382 case CPP_COMMA:
23383 if (depth == 0 && maybe_template_id)
23385 /* If we've seen a '<', we might be in a
23386 template-argument-list. Until Core issue 325 is
23387 resolved, we don't know how this situation ought
23388 to be handled, so try to DTRT. We check whether
23389 what comes after the comma is a valid parameter
23390 declaration list. If it is, then the comma ends
23391 the default argument; otherwise the default
23392 argument continues. */
23393 bool error = false;
23394 tree t;
23396 /* Set ITALP so cp_parser_parameter_declaration_list
23397 doesn't decide to commit to this parse. */
23398 bool saved_italp = parser->in_template_argument_list_p;
23399 parser->in_template_argument_list_p = true;
23401 cp_parser_parse_tentatively (parser);
23402 cp_lexer_consume_token (parser->lexer);
23404 if (nsdmi)
23406 int ctor_dtor_or_conv_p;
23407 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23408 &ctor_dtor_or_conv_p,
23409 /*parenthesized_p=*/NULL,
23410 /*member_p=*/true);
23412 else
23414 begin_scope (sk_function_parms, NULL_TREE);
23415 cp_parser_parameter_declaration_list (parser, &error);
23416 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23417 pop_binding (DECL_NAME (t), t);
23418 leave_scope ();
23420 if (!cp_parser_error_occurred (parser) && !error)
23421 done = true;
23422 cp_parser_abort_tentative_parse (parser);
23424 parser->in_template_argument_list_p = saved_italp;
23425 break;
23427 case CPP_CLOSE_PAREN:
23428 case CPP_ELLIPSIS:
23429 /* If we run into a non-nested `;', `}', or `]',
23430 then the code is invalid -- but the default
23431 argument is certainly over. */
23432 case CPP_SEMICOLON:
23433 case CPP_CLOSE_BRACE:
23434 case CPP_CLOSE_SQUARE:
23435 if (depth == 0)
23436 done = true;
23437 /* Update DEPTH, if necessary. */
23438 else if (token->type == CPP_CLOSE_PAREN
23439 || token->type == CPP_CLOSE_BRACE
23440 || token->type == CPP_CLOSE_SQUARE)
23441 --depth;
23442 break;
23444 case CPP_OPEN_PAREN:
23445 case CPP_OPEN_SQUARE:
23446 case CPP_OPEN_BRACE:
23447 ++depth;
23448 break;
23450 case CPP_LESS:
23451 if (depth == 0)
23452 /* This might be the comparison operator, or it might
23453 start a template argument list. */
23454 ++maybe_template_id;
23455 break;
23457 case CPP_RSHIFT:
23458 if (cxx_dialect == cxx98)
23459 break;
23460 /* Fall through for C++0x, which treats the `>>'
23461 operator like two `>' tokens in certain
23462 cases. */
23464 case CPP_GREATER:
23465 if (depth == 0)
23467 /* This might be an operator, or it might close a
23468 template argument list. But if a previous '<'
23469 started a template argument list, this will have
23470 closed it, so we can't be in one anymore. */
23471 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23472 if (maybe_template_id < 0)
23473 maybe_template_id = 0;
23475 break;
23477 /* If we run out of tokens, issue an error message. */
23478 case CPP_EOF:
23479 case CPP_PRAGMA_EOL:
23480 error_at (token->location, "file ends in default argument");
23481 done = true;
23482 break;
23484 case CPP_NAME:
23485 case CPP_SCOPE:
23486 /* In these cases, we should look for template-ids.
23487 For example, if the default argument is
23488 `X<int, double>()', we need to do name lookup to
23489 figure out whether or not `X' is a template; if
23490 so, the `,' does not end the default argument.
23492 That is not yet done. */
23493 break;
23495 default:
23496 break;
23499 /* If we've reached the end, stop. */
23500 if (done)
23501 break;
23503 /* Add the token to the token block. */
23504 token = cp_lexer_consume_token (parser->lexer);
23507 /* Create a DEFAULT_ARG to represent the unparsed default
23508 argument. */
23509 default_argument = make_node (DEFAULT_ARG);
23510 DEFARG_TOKENS (default_argument)
23511 = cp_token_cache_new (first_token, token);
23512 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23514 return default_argument;
23517 /* Begin parsing tentatively. We always save tokens while parsing
23518 tentatively so that if the tentative parsing fails we can restore the
23519 tokens. */
23521 static void
23522 cp_parser_parse_tentatively (cp_parser* parser)
23524 /* Enter a new parsing context. */
23525 parser->context = cp_parser_context_new (parser->context);
23526 /* Begin saving tokens. */
23527 cp_lexer_save_tokens (parser->lexer);
23528 /* In order to avoid repetitive access control error messages,
23529 access checks are queued up until we are no longer parsing
23530 tentatively. */
23531 push_deferring_access_checks (dk_deferred);
23534 /* Commit to the currently active tentative parse. */
23536 static void
23537 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23539 cp_parser_context *context;
23540 cp_lexer *lexer;
23542 /* Mark all of the levels as committed. */
23543 lexer = parser->lexer;
23544 for (context = parser->context; context->next; context = context->next)
23546 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23547 break;
23548 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23549 while (!cp_lexer_saving_tokens (lexer))
23550 lexer = lexer->next;
23551 cp_lexer_commit_tokens (lexer);
23555 /* Abort the currently active tentative parse. All consumed tokens
23556 will be rolled back, and no diagnostics will be issued. */
23558 static void
23559 cp_parser_abort_tentative_parse (cp_parser* parser)
23561 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23562 || errorcount > 0);
23563 cp_parser_simulate_error (parser);
23564 /* Now, pretend that we want to see if the construct was
23565 successfully parsed. */
23566 cp_parser_parse_definitely (parser);
23569 /* Stop parsing tentatively. If a parse error has occurred, restore the
23570 token stream. Otherwise, commit to the tokens we have consumed.
23571 Returns true if no error occurred; false otherwise. */
23573 static bool
23574 cp_parser_parse_definitely (cp_parser* parser)
23576 bool error_occurred;
23577 cp_parser_context *context;
23579 /* Remember whether or not an error occurred, since we are about to
23580 destroy that information. */
23581 error_occurred = cp_parser_error_occurred (parser);
23582 /* Remove the topmost context from the stack. */
23583 context = parser->context;
23584 parser->context = context->next;
23585 /* If no parse errors occurred, commit to the tentative parse. */
23586 if (!error_occurred)
23588 /* Commit to the tokens read tentatively, unless that was
23589 already done. */
23590 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23591 cp_lexer_commit_tokens (parser->lexer);
23593 pop_to_parent_deferring_access_checks ();
23595 /* Otherwise, if errors occurred, roll back our state so that things
23596 are just as they were before we began the tentative parse. */
23597 else
23599 cp_lexer_rollback_tokens (parser->lexer);
23600 pop_deferring_access_checks ();
23602 /* Add the context to the front of the free list. */
23603 context->next = cp_parser_context_free_list;
23604 cp_parser_context_free_list = context;
23606 return !error_occurred;
23609 /* Returns true if we are parsing tentatively and are not committed to
23610 this tentative parse. */
23612 static bool
23613 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23615 return (cp_parser_parsing_tentatively (parser)
23616 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23619 /* Returns nonzero iff an error has occurred during the most recent
23620 tentative parse. */
23622 static bool
23623 cp_parser_error_occurred (cp_parser* parser)
23625 return (cp_parser_parsing_tentatively (parser)
23626 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23629 /* Returns nonzero if GNU extensions are allowed. */
23631 static bool
23632 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23634 return parser->allow_gnu_extensions_p;
23637 /* Objective-C++ Productions */
23640 /* Parse an Objective-C expression, which feeds into a primary-expression
23641 above.
23643 objc-expression:
23644 objc-message-expression
23645 objc-string-literal
23646 objc-encode-expression
23647 objc-protocol-expression
23648 objc-selector-expression
23650 Returns a tree representation of the expression. */
23652 static tree
23653 cp_parser_objc_expression (cp_parser* parser)
23655 /* Try to figure out what kind of declaration is present. */
23656 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23658 switch (kwd->type)
23660 case CPP_OPEN_SQUARE:
23661 return cp_parser_objc_message_expression (parser);
23663 case CPP_OBJC_STRING:
23664 kwd = cp_lexer_consume_token (parser->lexer);
23665 return objc_build_string_object (kwd->u.value);
23667 case CPP_KEYWORD:
23668 switch (kwd->keyword)
23670 case RID_AT_ENCODE:
23671 return cp_parser_objc_encode_expression (parser);
23673 case RID_AT_PROTOCOL:
23674 return cp_parser_objc_protocol_expression (parser);
23676 case RID_AT_SELECTOR:
23677 return cp_parser_objc_selector_expression (parser);
23679 default:
23680 break;
23682 default:
23683 error_at (kwd->location,
23684 "misplaced %<@%D%> Objective-C++ construct",
23685 kwd->u.value);
23686 cp_parser_skip_to_end_of_block_or_statement (parser);
23689 return error_mark_node;
23692 /* Parse an Objective-C message expression.
23694 objc-message-expression:
23695 [ objc-message-receiver objc-message-args ]
23697 Returns a representation of an Objective-C message. */
23699 static tree
23700 cp_parser_objc_message_expression (cp_parser* parser)
23702 tree receiver, messageargs;
23704 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23705 receiver = cp_parser_objc_message_receiver (parser);
23706 messageargs = cp_parser_objc_message_args (parser);
23707 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23709 return objc_build_message_expr (receiver, messageargs);
23712 /* Parse an objc-message-receiver.
23714 objc-message-receiver:
23715 expression
23716 simple-type-specifier
23718 Returns a representation of the type or expression. */
23720 static tree
23721 cp_parser_objc_message_receiver (cp_parser* parser)
23723 tree rcv;
23725 /* An Objective-C message receiver may be either (1) a type
23726 or (2) an expression. */
23727 cp_parser_parse_tentatively (parser);
23728 rcv = cp_parser_expression (parser, false, NULL);
23730 if (cp_parser_parse_definitely (parser))
23731 return rcv;
23733 rcv = cp_parser_simple_type_specifier (parser,
23734 /*decl_specs=*/NULL,
23735 CP_PARSER_FLAGS_NONE);
23737 return objc_get_class_reference (rcv);
23740 /* Parse the arguments and selectors comprising an Objective-C message.
23742 objc-message-args:
23743 objc-selector
23744 objc-selector-args
23745 objc-selector-args , objc-comma-args
23747 objc-selector-args:
23748 objc-selector [opt] : assignment-expression
23749 objc-selector-args objc-selector [opt] : assignment-expression
23751 objc-comma-args:
23752 assignment-expression
23753 objc-comma-args , assignment-expression
23755 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23756 selector arguments and TREE_VALUE containing a list of comma
23757 arguments. */
23759 static tree
23760 cp_parser_objc_message_args (cp_parser* parser)
23762 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23763 bool maybe_unary_selector_p = true;
23764 cp_token *token = cp_lexer_peek_token (parser->lexer);
23766 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23768 tree selector = NULL_TREE, arg;
23770 if (token->type != CPP_COLON)
23771 selector = cp_parser_objc_selector (parser);
23773 /* Detect if we have a unary selector. */
23774 if (maybe_unary_selector_p
23775 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23776 return build_tree_list (selector, NULL_TREE);
23778 maybe_unary_selector_p = false;
23779 cp_parser_require (parser, CPP_COLON, RT_COLON);
23780 arg = cp_parser_assignment_expression (parser, false, NULL);
23782 sel_args
23783 = chainon (sel_args,
23784 build_tree_list (selector, arg));
23786 token = cp_lexer_peek_token (parser->lexer);
23789 /* Handle non-selector arguments, if any. */
23790 while (token->type == CPP_COMMA)
23792 tree arg;
23794 cp_lexer_consume_token (parser->lexer);
23795 arg = cp_parser_assignment_expression (parser, false, NULL);
23797 addl_args
23798 = chainon (addl_args,
23799 build_tree_list (NULL_TREE, arg));
23801 token = cp_lexer_peek_token (parser->lexer);
23804 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23806 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23807 return build_tree_list (error_mark_node, error_mark_node);
23810 return build_tree_list (sel_args, addl_args);
23813 /* Parse an Objective-C encode expression.
23815 objc-encode-expression:
23816 @encode objc-typename
23818 Returns an encoded representation of the type argument. */
23820 static tree
23821 cp_parser_objc_encode_expression (cp_parser* parser)
23823 tree type;
23824 cp_token *token;
23826 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23827 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23828 token = cp_lexer_peek_token (parser->lexer);
23829 type = complete_type (cp_parser_type_id (parser));
23830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23832 if (!type)
23834 error_at (token->location,
23835 "%<@encode%> must specify a type as an argument");
23836 return error_mark_node;
23839 /* This happens if we find @encode(T) (where T is a template
23840 typename or something dependent on a template typename) when
23841 parsing a template. In that case, we can't compile it
23842 immediately, but we rather create an AT_ENCODE_EXPR which will
23843 need to be instantiated when the template is used.
23845 if (dependent_type_p (type))
23847 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23848 TREE_READONLY (value) = 1;
23849 return value;
23852 return objc_build_encode_expr (type);
23855 /* Parse an Objective-C @defs expression. */
23857 static tree
23858 cp_parser_objc_defs_expression (cp_parser *parser)
23860 tree name;
23862 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23864 name = cp_parser_identifier (parser);
23865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23867 return objc_get_class_ivars (name);
23870 /* Parse an Objective-C protocol expression.
23872 objc-protocol-expression:
23873 @protocol ( identifier )
23875 Returns a representation of the protocol expression. */
23877 static tree
23878 cp_parser_objc_protocol_expression (cp_parser* parser)
23880 tree proto;
23882 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23883 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23884 proto = cp_parser_identifier (parser);
23885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23887 return objc_build_protocol_expr (proto);
23890 /* Parse an Objective-C selector expression.
23892 objc-selector-expression:
23893 @selector ( objc-method-signature )
23895 objc-method-signature:
23896 objc-selector
23897 objc-selector-seq
23899 objc-selector-seq:
23900 objc-selector :
23901 objc-selector-seq objc-selector :
23903 Returns a representation of the method selector. */
23905 static tree
23906 cp_parser_objc_selector_expression (cp_parser* parser)
23908 tree sel_seq = NULL_TREE;
23909 bool maybe_unary_selector_p = true;
23910 cp_token *token;
23911 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23913 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23914 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23915 token = cp_lexer_peek_token (parser->lexer);
23917 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23918 || token->type == CPP_SCOPE)
23920 tree selector = NULL_TREE;
23922 if (token->type != CPP_COLON
23923 || token->type == CPP_SCOPE)
23924 selector = cp_parser_objc_selector (parser);
23926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23927 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23929 /* Detect if we have a unary selector. */
23930 if (maybe_unary_selector_p)
23932 sel_seq = selector;
23933 goto finish_selector;
23935 else
23937 cp_parser_error (parser, "expected %<:%>");
23940 maybe_unary_selector_p = false;
23941 token = cp_lexer_consume_token (parser->lexer);
23943 if (token->type == CPP_SCOPE)
23945 sel_seq
23946 = chainon (sel_seq,
23947 build_tree_list (selector, NULL_TREE));
23948 sel_seq
23949 = chainon (sel_seq,
23950 build_tree_list (NULL_TREE, NULL_TREE));
23952 else
23953 sel_seq
23954 = chainon (sel_seq,
23955 build_tree_list (selector, NULL_TREE));
23957 token = cp_lexer_peek_token (parser->lexer);
23960 finish_selector:
23961 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23963 return objc_build_selector_expr (loc, sel_seq);
23966 /* Parse a list of identifiers.
23968 objc-identifier-list:
23969 identifier
23970 objc-identifier-list , identifier
23972 Returns a TREE_LIST of identifier nodes. */
23974 static tree
23975 cp_parser_objc_identifier_list (cp_parser* parser)
23977 tree identifier;
23978 tree list;
23979 cp_token *sep;
23981 identifier = cp_parser_identifier (parser);
23982 if (identifier == error_mark_node)
23983 return error_mark_node;
23985 list = build_tree_list (NULL_TREE, identifier);
23986 sep = cp_lexer_peek_token (parser->lexer);
23988 while (sep->type == CPP_COMMA)
23990 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23991 identifier = cp_parser_identifier (parser);
23992 if (identifier == error_mark_node)
23993 return list;
23995 list = chainon (list, build_tree_list (NULL_TREE,
23996 identifier));
23997 sep = cp_lexer_peek_token (parser->lexer);
24000 return list;
24003 /* Parse an Objective-C alias declaration.
24005 objc-alias-declaration:
24006 @compatibility_alias identifier identifier ;
24008 This function registers the alias mapping with the Objective-C front end.
24009 It returns nothing. */
24011 static void
24012 cp_parser_objc_alias_declaration (cp_parser* parser)
24014 tree alias, orig;
24016 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24017 alias = cp_parser_identifier (parser);
24018 orig = cp_parser_identifier (parser);
24019 objc_declare_alias (alias, orig);
24020 cp_parser_consume_semicolon_at_end_of_statement (parser);
24023 /* Parse an Objective-C class forward-declaration.
24025 objc-class-declaration:
24026 @class objc-identifier-list ;
24028 The function registers the forward declarations with the Objective-C
24029 front end. It returns nothing. */
24031 static void
24032 cp_parser_objc_class_declaration (cp_parser* parser)
24034 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24035 while (true)
24037 tree id;
24039 id = cp_parser_identifier (parser);
24040 if (id == error_mark_node)
24041 break;
24043 objc_declare_class (id);
24045 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24046 cp_lexer_consume_token (parser->lexer);
24047 else
24048 break;
24050 cp_parser_consume_semicolon_at_end_of_statement (parser);
24053 /* Parse a list of Objective-C protocol references.
24055 objc-protocol-refs-opt:
24056 objc-protocol-refs [opt]
24058 objc-protocol-refs:
24059 < objc-identifier-list >
24061 Returns a TREE_LIST of identifiers, if any. */
24063 static tree
24064 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24066 tree protorefs = NULL_TREE;
24068 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24070 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24071 protorefs = cp_parser_objc_identifier_list (parser);
24072 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24075 return protorefs;
24078 /* Parse a Objective-C visibility specification. */
24080 static void
24081 cp_parser_objc_visibility_spec (cp_parser* parser)
24083 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24085 switch (vis->keyword)
24087 case RID_AT_PRIVATE:
24088 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24089 break;
24090 case RID_AT_PROTECTED:
24091 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24092 break;
24093 case RID_AT_PUBLIC:
24094 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24095 break;
24096 case RID_AT_PACKAGE:
24097 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24098 break;
24099 default:
24100 return;
24103 /* Eat '@private'/'@protected'/'@public'. */
24104 cp_lexer_consume_token (parser->lexer);
24107 /* Parse an Objective-C method type. Return 'true' if it is a class
24108 (+) method, and 'false' if it is an instance (-) method. */
24110 static inline bool
24111 cp_parser_objc_method_type (cp_parser* parser)
24113 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24114 return true;
24115 else
24116 return false;
24119 /* Parse an Objective-C protocol qualifier. */
24121 static tree
24122 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24124 tree quals = NULL_TREE, node;
24125 cp_token *token = cp_lexer_peek_token (parser->lexer);
24127 node = token->u.value;
24129 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24130 && (node == ridpointers [(int) RID_IN]
24131 || node == ridpointers [(int) RID_OUT]
24132 || node == ridpointers [(int) RID_INOUT]
24133 || node == ridpointers [(int) RID_BYCOPY]
24134 || node == ridpointers [(int) RID_BYREF]
24135 || node == ridpointers [(int) RID_ONEWAY]))
24137 quals = tree_cons (NULL_TREE, node, quals);
24138 cp_lexer_consume_token (parser->lexer);
24139 token = cp_lexer_peek_token (parser->lexer);
24140 node = token->u.value;
24143 return quals;
24146 /* Parse an Objective-C typename. */
24148 static tree
24149 cp_parser_objc_typename (cp_parser* parser)
24151 tree type_name = NULL_TREE;
24153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24155 tree proto_quals, cp_type = NULL_TREE;
24157 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24158 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24160 /* An ObjC type name may consist of just protocol qualifiers, in which
24161 case the type shall default to 'id'. */
24162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24164 cp_type = cp_parser_type_id (parser);
24166 /* If the type could not be parsed, an error has already
24167 been produced. For error recovery, behave as if it had
24168 not been specified, which will use the default type
24169 'id'. */
24170 if (cp_type == error_mark_node)
24172 cp_type = NULL_TREE;
24173 /* We need to skip to the closing parenthesis as
24174 cp_parser_type_id() does not seem to do it for
24175 us. */
24176 cp_parser_skip_to_closing_parenthesis (parser,
24177 /*recovering=*/true,
24178 /*or_comma=*/false,
24179 /*consume_paren=*/false);
24183 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24184 type_name = build_tree_list (proto_quals, cp_type);
24187 return type_name;
24190 /* Check to see if TYPE refers to an Objective-C selector name. */
24192 static bool
24193 cp_parser_objc_selector_p (enum cpp_ttype type)
24195 return (type == CPP_NAME || type == CPP_KEYWORD
24196 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24197 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24198 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24199 || type == CPP_XOR || type == CPP_XOR_EQ);
24202 /* Parse an Objective-C selector. */
24204 static tree
24205 cp_parser_objc_selector (cp_parser* parser)
24207 cp_token *token = cp_lexer_consume_token (parser->lexer);
24209 if (!cp_parser_objc_selector_p (token->type))
24211 error_at (token->location, "invalid Objective-C++ selector name");
24212 return error_mark_node;
24215 /* C++ operator names are allowed to appear in ObjC selectors. */
24216 switch (token->type)
24218 case CPP_AND_AND: return get_identifier ("and");
24219 case CPP_AND_EQ: return get_identifier ("and_eq");
24220 case CPP_AND: return get_identifier ("bitand");
24221 case CPP_OR: return get_identifier ("bitor");
24222 case CPP_COMPL: return get_identifier ("compl");
24223 case CPP_NOT: return get_identifier ("not");
24224 case CPP_NOT_EQ: return get_identifier ("not_eq");
24225 case CPP_OR_OR: return get_identifier ("or");
24226 case CPP_OR_EQ: return get_identifier ("or_eq");
24227 case CPP_XOR: return get_identifier ("xor");
24228 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24229 default: return token->u.value;
24233 /* Parse an Objective-C params list. */
24235 static tree
24236 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24238 tree params = NULL_TREE;
24239 bool maybe_unary_selector_p = true;
24240 cp_token *token = cp_lexer_peek_token (parser->lexer);
24242 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24244 tree selector = NULL_TREE, type_name, identifier;
24245 tree parm_attr = NULL_TREE;
24247 if (token->keyword == RID_ATTRIBUTE)
24248 break;
24250 if (token->type != CPP_COLON)
24251 selector = cp_parser_objc_selector (parser);
24253 /* Detect if we have a unary selector. */
24254 if (maybe_unary_selector_p
24255 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24257 params = selector; /* Might be followed by attributes. */
24258 break;
24261 maybe_unary_selector_p = false;
24262 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24264 /* Something went quite wrong. There should be a colon
24265 here, but there is not. Stop parsing parameters. */
24266 break;
24268 type_name = cp_parser_objc_typename (parser);
24269 /* New ObjC allows attributes on parameters too. */
24270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24271 parm_attr = cp_parser_attributes_opt (parser);
24272 identifier = cp_parser_identifier (parser);
24274 params
24275 = chainon (params,
24276 objc_build_keyword_decl (selector,
24277 type_name,
24278 identifier,
24279 parm_attr));
24281 token = cp_lexer_peek_token (parser->lexer);
24284 if (params == NULL_TREE)
24286 cp_parser_error (parser, "objective-c++ method declaration is expected");
24287 return error_mark_node;
24290 /* We allow tail attributes for the method. */
24291 if (token->keyword == RID_ATTRIBUTE)
24293 *attributes = cp_parser_attributes_opt (parser);
24294 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24295 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24296 return params;
24297 cp_parser_error (parser,
24298 "method attributes must be specified at the end");
24299 return error_mark_node;
24302 if (params == NULL_TREE)
24304 cp_parser_error (parser, "objective-c++ method declaration is expected");
24305 return error_mark_node;
24307 return params;
24310 /* Parse the non-keyword Objective-C params. */
24312 static tree
24313 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24314 tree* attributes)
24316 tree params = make_node (TREE_LIST);
24317 cp_token *token = cp_lexer_peek_token (parser->lexer);
24318 *ellipsisp = false; /* Initially, assume no ellipsis. */
24320 while (token->type == CPP_COMMA)
24322 cp_parameter_declarator *parmdecl;
24323 tree parm;
24325 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24326 token = cp_lexer_peek_token (parser->lexer);
24328 if (token->type == CPP_ELLIPSIS)
24330 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24331 *ellipsisp = true;
24332 token = cp_lexer_peek_token (parser->lexer);
24333 break;
24336 /* TODO: parse attributes for tail parameters. */
24337 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24338 parm = grokdeclarator (parmdecl->declarator,
24339 &parmdecl->decl_specifiers,
24340 PARM, /*initialized=*/0,
24341 /*attrlist=*/NULL);
24343 chainon (params, build_tree_list (NULL_TREE, parm));
24344 token = cp_lexer_peek_token (parser->lexer);
24347 /* We allow tail attributes for the method. */
24348 if (token->keyword == RID_ATTRIBUTE)
24350 if (*attributes == NULL_TREE)
24352 *attributes = cp_parser_attributes_opt (parser);
24353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24354 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24355 return params;
24357 else
24358 /* We have an error, but parse the attributes, so that we can
24359 carry on. */
24360 *attributes = cp_parser_attributes_opt (parser);
24362 cp_parser_error (parser,
24363 "method attributes must be specified at the end");
24364 return error_mark_node;
24367 return params;
24370 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24372 static void
24373 cp_parser_objc_interstitial_code (cp_parser* parser)
24375 cp_token *token = cp_lexer_peek_token (parser->lexer);
24377 /* If the next token is `extern' and the following token is a string
24378 literal, then we have a linkage specification. */
24379 if (token->keyword == RID_EXTERN
24380 && cp_parser_is_pure_string_literal
24381 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24382 cp_parser_linkage_specification (parser);
24383 /* Handle #pragma, if any. */
24384 else if (token->type == CPP_PRAGMA)
24385 cp_parser_pragma (parser, pragma_external);
24386 /* Allow stray semicolons. */
24387 else if (token->type == CPP_SEMICOLON)
24388 cp_lexer_consume_token (parser->lexer);
24389 /* Mark methods as optional or required, when building protocols. */
24390 else if (token->keyword == RID_AT_OPTIONAL)
24392 cp_lexer_consume_token (parser->lexer);
24393 objc_set_method_opt (true);
24395 else if (token->keyword == RID_AT_REQUIRED)
24397 cp_lexer_consume_token (parser->lexer);
24398 objc_set_method_opt (false);
24400 else if (token->keyword == RID_NAMESPACE)
24401 cp_parser_namespace_definition (parser);
24402 /* Other stray characters must generate errors. */
24403 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24405 cp_lexer_consume_token (parser->lexer);
24406 error ("stray %qs between Objective-C++ methods",
24407 token->type == CPP_OPEN_BRACE ? "{" : "}");
24409 /* Finally, try to parse a block-declaration, or a function-definition. */
24410 else
24411 cp_parser_block_declaration (parser, /*statement_p=*/false);
24414 /* Parse a method signature. */
24416 static tree
24417 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24419 tree rettype, kwdparms, optparms;
24420 bool ellipsis = false;
24421 bool is_class_method;
24423 is_class_method = cp_parser_objc_method_type (parser);
24424 rettype = cp_parser_objc_typename (parser);
24425 *attributes = NULL_TREE;
24426 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24427 if (kwdparms == error_mark_node)
24428 return error_mark_node;
24429 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24430 if (optparms == error_mark_node)
24431 return error_mark_node;
24433 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24436 static bool
24437 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24439 tree tattr;
24440 cp_lexer_save_tokens (parser->lexer);
24441 tattr = cp_parser_attributes_opt (parser);
24442 gcc_assert (tattr) ;
24444 /* If the attributes are followed by a method introducer, this is not allowed.
24445 Dump the attributes and flag the situation. */
24446 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24447 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24448 return true;
24450 /* Otherwise, the attributes introduce some interstitial code, possibly so
24451 rewind to allow that check. */
24452 cp_lexer_rollback_tokens (parser->lexer);
24453 return false;
24456 /* Parse an Objective-C method prototype list. */
24458 static void
24459 cp_parser_objc_method_prototype_list (cp_parser* parser)
24461 cp_token *token = cp_lexer_peek_token (parser->lexer);
24463 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24465 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24467 tree attributes, sig;
24468 bool is_class_method;
24469 if (token->type == CPP_PLUS)
24470 is_class_method = true;
24471 else
24472 is_class_method = false;
24473 sig = cp_parser_objc_method_signature (parser, &attributes);
24474 if (sig == error_mark_node)
24476 cp_parser_skip_to_end_of_block_or_statement (parser);
24477 token = cp_lexer_peek_token (parser->lexer);
24478 continue;
24480 objc_add_method_declaration (is_class_method, sig, attributes);
24481 cp_parser_consume_semicolon_at_end_of_statement (parser);
24483 else if (token->keyword == RID_AT_PROPERTY)
24484 cp_parser_objc_at_property_declaration (parser);
24485 else if (token->keyword == RID_ATTRIBUTE
24486 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24487 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24488 OPT_Wattributes,
24489 "prefix attributes are ignored for methods");
24490 else
24491 /* Allow for interspersed non-ObjC++ code. */
24492 cp_parser_objc_interstitial_code (parser);
24494 token = cp_lexer_peek_token (parser->lexer);
24497 if (token->type != CPP_EOF)
24498 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24499 else
24500 cp_parser_error (parser, "expected %<@end%>");
24502 objc_finish_interface ();
24505 /* Parse an Objective-C method definition list. */
24507 static void
24508 cp_parser_objc_method_definition_list (cp_parser* parser)
24510 cp_token *token = cp_lexer_peek_token (parser->lexer);
24512 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24514 tree meth;
24516 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24518 cp_token *ptk;
24519 tree sig, attribute;
24520 bool is_class_method;
24521 if (token->type == CPP_PLUS)
24522 is_class_method = true;
24523 else
24524 is_class_method = false;
24525 push_deferring_access_checks (dk_deferred);
24526 sig = cp_parser_objc_method_signature (parser, &attribute);
24527 if (sig == error_mark_node)
24529 cp_parser_skip_to_end_of_block_or_statement (parser);
24530 token = cp_lexer_peek_token (parser->lexer);
24531 continue;
24533 objc_start_method_definition (is_class_method, sig, attribute,
24534 NULL_TREE);
24536 /* For historical reasons, we accept an optional semicolon. */
24537 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24538 cp_lexer_consume_token (parser->lexer);
24540 ptk = cp_lexer_peek_token (parser->lexer);
24541 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24542 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24544 perform_deferred_access_checks (tf_warning_or_error);
24545 stop_deferring_access_checks ();
24546 meth = cp_parser_function_definition_after_declarator (parser,
24547 false);
24548 pop_deferring_access_checks ();
24549 objc_finish_method_definition (meth);
24552 /* The following case will be removed once @synthesize is
24553 completely implemented. */
24554 else if (token->keyword == RID_AT_PROPERTY)
24555 cp_parser_objc_at_property_declaration (parser);
24556 else if (token->keyword == RID_AT_SYNTHESIZE)
24557 cp_parser_objc_at_synthesize_declaration (parser);
24558 else if (token->keyword == RID_AT_DYNAMIC)
24559 cp_parser_objc_at_dynamic_declaration (parser);
24560 else if (token->keyword == RID_ATTRIBUTE
24561 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24562 warning_at (token->location, OPT_Wattributes,
24563 "prefix attributes are ignored for methods");
24564 else
24565 /* Allow for interspersed non-ObjC++ code. */
24566 cp_parser_objc_interstitial_code (parser);
24568 token = cp_lexer_peek_token (parser->lexer);
24571 if (token->type != CPP_EOF)
24572 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24573 else
24574 cp_parser_error (parser, "expected %<@end%>");
24576 objc_finish_implementation ();
24579 /* Parse Objective-C ivars. */
24581 static void
24582 cp_parser_objc_class_ivars (cp_parser* parser)
24584 cp_token *token = cp_lexer_peek_token (parser->lexer);
24586 if (token->type != CPP_OPEN_BRACE)
24587 return; /* No ivars specified. */
24589 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24590 token = cp_lexer_peek_token (parser->lexer);
24592 while (token->type != CPP_CLOSE_BRACE
24593 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24595 cp_decl_specifier_seq declspecs;
24596 int decl_class_or_enum_p;
24597 tree prefix_attributes;
24599 cp_parser_objc_visibility_spec (parser);
24601 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24602 break;
24604 cp_parser_decl_specifier_seq (parser,
24605 CP_PARSER_FLAGS_OPTIONAL,
24606 &declspecs,
24607 &decl_class_or_enum_p);
24609 /* auto, register, static, extern, mutable. */
24610 if (declspecs.storage_class != sc_none)
24612 cp_parser_error (parser, "invalid type for instance variable");
24613 declspecs.storage_class = sc_none;
24616 /* thread_local. */
24617 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24619 cp_parser_error (parser, "invalid type for instance variable");
24620 declspecs.locations[ds_thread] = 0;
24623 /* typedef. */
24624 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24626 cp_parser_error (parser, "invalid type for instance variable");
24627 declspecs.locations[ds_thread] = 0;
24630 prefix_attributes = declspecs.attributes;
24631 declspecs.attributes = NULL_TREE;
24633 /* Keep going until we hit the `;' at the end of the
24634 declaration. */
24635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24637 tree width = NULL_TREE, attributes, first_attribute, decl;
24638 cp_declarator *declarator = NULL;
24639 int ctor_dtor_or_conv_p;
24641 /* Check for a (possibly unnamed) bitfield declaration. */
24642 token = cp_lexer_peek_token (parser->lexer);
24643 if (token->type == CPP_COLON)
24644 goto eat_colon;
24646 if (token->type == CPP_NAME
24647 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24648 == CPP_COLON))
24650 /* Get the name of the bitfield. */
24651 declarator = make_id_declarator (NULL_TREE,
24652 cp_parser_identifier (parser),
24653 sfk_none);
24655 eat_colon:
24656 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24657 /* Get the width of the bitfield. */
24658 width
24659 = cp_parser_constant_expression (parser,
24660 /*allow_non_constant=*/false,
24661 NULL);
24663 else
24665 /* Parse the declarator. */
24666 declarator
24667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24668 &ctor_dtor_or_conv_p,
24669 /*parenthesized_p=*/NULL,
24670 /*member_p=*/false);
24673 /* Look for attributes that apply to the ivar. */
24674 attributes = cp_parser_attributes_opt (parser);
24675 /* Remember which attributes are prefix attributes and
24676 which are not. */
24677 first_attribute = attributes;
24678 /* Combine the attributes. */
24679 attributes = chainon (prefix_attributes, attributes);
24681 if (width)
24682 /* Create the bitfield declaration. */
24683 decl = grokbitfield (declarator, &declspecs,
24684 width,
24685 attributes);
24686 else
24687 decl = grokfield (declarator, &declspecs,
24688 NULL_TREE, /*init_const_expr_p=*/false,
24689 NULL_TREE, attributes);
24691 /* Add the instance variable. */
24692 if (decl != error_mark_node && decl != NULL_TREE)
24693 objc_add_instance_variable (decl);
24695 /* Reset PREFIX_ATTRIBUTES. */
24696 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24697 attributes = TREE_CHAIN (attributes);
24698 if (attributes)
24699 TREE_CHAIN (attributes) = NULL_TREE;
24701 token = cp_lexer_peek_token (parser->lexer);
24703 if (token->type == CPP_COMMA)
24705 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24706 continue;
24708 break;
24711 cp_parser_consume_semicolon_at_end_of_statement (parser);
24712 token = cp_lexer_peek_token (parser->lexer);
24715 if (token->keyword == RID_AT_END)
24716 cp_parser_error (parser, "expected %<}%>");
24718 /* Do not consume the RID_AT_END, so it will be read again as terminating
24719 the @interface of @implementation. */
24720 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24721 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24723 /* For historical reasons, we accept an optional semicolon. */
24724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24725 cp_lexer_consume_token (parser->lexer);
24728 /* Parse an Objective-C protocol declaration. */
24730 static void
24731 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24733 tree proto, protorefs;
24734 cp_token *tok;
24736 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24739 tok = cp_lexer_peek_token (parser->lexer);
24740 error_at (tok->location, "identifier expected after %<@protocol%>");
24741 cp_parser_consume_semicolon_at_end_of_statement (parser);
24742 return;
24745 /* See if we have a forward declaration or a definition. */
24746 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24748 /* Try a forward declaration first. */
24749 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24751 while (true)
24753 tree id;
24755 id = cp_parser_identifier (parser);
24756 if (id == error_mark_node)
24757 break;
24759 objc_declare_protocol (id, attributes);
24761 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24762 cp_lexer_consume_token (parser->lexer);
24763 else
24764 break;
24766 cp_parser_consume_semicolon_at_end_of_statement (parser);
24769 /* Ok, we got a full-fledged definition (or at least should). */
24770 else
24772 proto = cp_parser_identifier (parser);
24773 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24774 objc_start_protocol (proto, protorefs, attributes);
24775 cp_parser_objc_method_prototype_list (parser);
24779 /* Parse an Objective-C superclass or category. */
24781 static void
24782 cp_parser_objc_superclass_or_category (cp_parser *parser,
24783 bool iface_p,
24784 tree *super,
24785 tree *categ, bool *is_class_extension)
24787 cp_token *next = cp_lexer_peek_token (parser->lexer);
24789 *super = *categ = NULL_TREE;
24790 *is_class_extension = false;
24791 if (next->type == CPP_COLON)
24793 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24794 *super = cp_parser_identifier (parser);
24796 else if (next->type == CPP_OPEN_PAREN)
24798 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24800 /* If there is no category name, and this is an @interface, we
24801 have a class extension. */
24802 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24804 *categ = NULL_TREE;
24805 *is_class_extension = true;
24807 else
24808 *categ = cp_parser_identifier (parser);
24810 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24814 /* Parse an Objective-C class interface. */
24816 static void
24817 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24819 tree name, super, categ, protos;
24820 bool is_class_extension;
24822 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24823 name = cp_parser_identifier (parser);
24824 if (name == error_mark_node)
24826 /* It's hard to recover because even if valid @interface stuff
24827 is to follow, we can't compile it (or validate it) if we
24828 don't even know which class it refers to. Let's assume this
24829 was a stray '@interface' token in the stream and skip it.
24831 return;
24833 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24834 &is_class_extension);
24835 protos = cp_parser_objc_protocol_refs_opt (parser);
24837 /* We have either a class or a category on our hands. */
24838 if (categ || is_class_extension)
24839 objc_start_category_interface (name, categ, protos, attributes);
24840 else
24842 objc_start_class_interface (name, super, protos, attributes);
24843 /* Handle instance variable declarations, if any. */
24844 cp_parser_objc_class_ivars (parser);
24845 objc_continue_interface ();
24848 cp_parser_objc_method_prototype_list (parser);
24851 /* Parse an Objective-C class implementation. */
24853 static void
24854 cp_parser_objc_class_implementation (cp_parser* parser)
24856 tree name, super, categ;
24857 bool is_class_extension;
24859 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24860 name = cp_parser_identifier (parser);
24861 if (name == error_mark_node)
24863 /* It's hard to recover because even if valid @implementation
24864 stuff is to follow, we can't compile it (or validate it) if
24865 we don't even know which class it refers to. Let's assume
24866 this was a stray '@implementation' token in the stream and
24867 skip it.
24869 return;
24871 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24872 &is_class_extension);
24874 /* We have either a class or a category on our hands. */
24875 if (categ)
24876 objc_start_category_implementation (name, categ);
24877 else
24879 objc_start_class_implementation (name, super);
24880 /* Handle instance variable declarations, if any. */
24881 cp_parser_objc_class_ivars (parser);
24882 objc_continue_implementation ();
24885 cp_parser_objc_method_definition_list (parser);
24888 /* Consume the @end token and finish off the implementation. */
24890 static void
24891 cp_parser_objc_end_implementation (cp_parser* parser)
24893 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24894 objc_finish_implementation ();
24897 /* Parse an Objective-C declaration. */
24899 static void
24900 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24902 /* Try to figure out what kind of declaration is present. */
24903 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24905 if (attributes)
24906 switch (kwd->keyword)
24908 case RID_AT_ALIAS:
24909 case RID_AT_CLASS:
24910 case RID_AT_END:
24911 error_at (kwd->location, "attributes may not be specified before"
24912 " the %<@%D%> Objective-C++ keyword",
24913 kwd->u.value);
24914 attributes = NULL;
24915 break;
24916 case RID_AT_IMPLEMENTATION:
24917 warning_at (kwd->location, OPT_Wattributes,
24918 "prefix attributes are ignored before %<@%D%>",
24919 kwd->u.value);
24920 attributes = NULL;
24921 default:
24922 break;
24925 switch (kwd->keyword)
24927 case RID_AT_ALIAS:
24928 cp_parser_objc_alias_declaration (parser);
24929 break;
24930 case RID_AT_CLASS:
24931 cp_parser_objc_class_declaration (parser);
24932 break;
24933 case RID_AT_PROTOCOL:
24934 cp_parser_objc_protocol_declaration (parser, attributes);
24935 break;
24936 case RID_AT_INTERFACE:
24937 cp_parser_objc_class_interface (parser, attributes);
24938 break;
24939 case RID_AT_IMPLEMENTATION:
24940 cp_parser_objc_class_implementation (parser);
24941 break;
24942 case RID_AT_END:
24943 cp_parser_objc_end_implementation (parser);
24944 break;
24945 default:
24946 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24947 kwd->u.value);
24948 cp_parser_skip_to_end_of_block_or_statement (parser);
24952 /* Parse an Objective-C try-catch-finally statement.
24954 objc-try-catch-finally-stmt:
24955 @try compound-statement objc-catch-clause-seq [opt]
24956 objc-finally-clause [opt]
24958 objc-catch-clause-seq:
24959 objc-catch-clause objc-catch-clause-seq [opt]
24961 objc-catch-clause:
24962 @catch ( objc-exception-declaration ) compound-statement
24964 objc-finally-clause:
24965 @finally compound-statement
24967 objc-exception-declaration:
24968 parameter-declaration
24969 '...'
24971 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24973 Returns NULL_TREE.
24975 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24976 for C. Keep them in sync. */
24978 static tree
24979 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24981 location_t location;
24982 tree stmt;
24984 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24985 location = cp_lexer_peek_token (parser->lexer)->location;
24986 objc_maybe_warn_exceptions (location);
24987 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24988 node, lest it get absorbed into the surrounding block. */
24989 stmt = push_stmt_list ();
24990 cp_parser_compound_statement (parser, NULL, false, false);
24991 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24993 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24995 cp_parameter_declarator *parm;
24996 tree parameter_declaration = error_mark_node;
24997 bool seen_open_paren = false;
24999 cp_lexer_consume_token (parser->lexer);
25000 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25001 seen_open_paren = true;
25002 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25004 /* We have "@catch (...)" (where the '...' are literally
25005 what is in the code). Skip the '...'.
25006 parameter_declaration is set to NULL_TREE, and
25007 objc_being_catch_clauses() knows that that means
25008 '...'. */
25009 cp_lexer_consume_token (parser->lexer);
25010 parameter_declaration = NULL_TREE;
25012 else
25014 /* We have "@catch (NSException *exception)" or something
25015 like that. Parse the parameter declaration. */
25016 parm = cp_parser_parameter_declaration (parser, false, NULL);
25017 if (parm == NULL)
25018 parameter_declaration = error_mark_node;
25019 else
25020 parameter_declaration = grokdeclarator (parm->declarator,
25021 &parm->decl_specifiers,
25022 PARM, /*initialized=*/0,
25023 /*attrlist=*/NULL);
25025 if (seen_open_paren)
25026 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25027 else
25029 /* If there was no open parenthesis, we are recovering from
25030 an error, and we are trying to figure out what mistake
25031 the user has made. */
25033 /* If there is an immediate closing parenthesis, the user
25034 probably forgot the opening one (ie, they typed "@catch
25035 NSException *e)". Parse the closing parenthesis and keep
25036 going. */
25037 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25038 cp_lexer_consume_token (parser->lexer);
25040 /* If these is no immediate closing parenthesis, the user
25041 probably doesn't know that parenthesis are required at
25042 all (ie, they typed "@catch NSException *e"). So, just
25043 forget about the closing parenthesis and keep going. */
25045 objc_begin_catch_clause (parameter_declaration);
25046 cp_parser_compound_statement (parser, NULL, false, false);
25047 objc_finish_catch_clause ();
25049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25051 cp_lexer_consume_token (parser->lexer);
25052 location = cp_lexer_peek_token (parser->lexer)->location;
25053 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25054 node, lest it get absorbed into the surrounding block. */
25055 stmt = push_stmt_list ();
25056 cp_parser_compound_statement (parser, NULL, false, false);
25057 objc_build_finally_clause (location, pop_stmt_list (stmt));
25060 return objc_finish_try_stmt ();
25063 /* Parse an Objective-C synchronized statement.
25065 objc-synchronized-stmt:
25066 @synchronized ( expression ) compound-statement
25068 Returns NULL_TREE. */
25070 static tree
25071 cp_parser_objc_synchronized_statement (cp_parser *parser)
25073 location_t location;
25074 tree lock, stmt;
25076 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25078 location = cp_lexer_peek_token (parser->lexer)->location;
25079 objc_maybe_warn_exceptions (location);
25080 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25081 lock = cp_parser_expression (parser, false, NULL);
25082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25084 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25085 node, lest it get absorbed into the surrounding block. */
25086 stmt = push_stmt_list ();
25087 cp_parser_compound_statement (parser, NULL, false, false);
25089 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25092 /* Parse an Objective-C throw statement.
25094 objc-throw-stmt:
25095 @throw assignment-expression [opt] ;
25097 Returns a constructed '@throw' statement. */
25099 static tree
25100 cp_parser_objc_throw_statement (cp_parser *parser)
25102 tree expr = NULL_TREE;
25103 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25105 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25108 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25110 cp_parser_consume_semicolon_at_end_of_statement (parser);
25112 return objc_build_throw_stmt (loc, expr);
25115 /* Parse an Objective-C statement. */
25117 static tree
25118 cp_parser_objc_statement (cp_parser * parser)
25120 /* Try to figure out what kind of declaration is present. */
25121 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25123 switch (kwd->keyword)
25125 case RID_AT_TRY:
25126 return cp_parser_objc_try_catch_finally_statement (parser);
25127 case RID_AT_SYNCHRONIZED:
25128 return cp_parser_objc_synchronized_statement (parser);
25129 case RID_AT_THROW:
25130 return cp_parser_objc_throw_statement (parser);
25131 default:
25132 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25133 kwd->u.value);
25134 cp_parser_skip_to_end_of_block_or_statement (parser);
25137 return error_mark_node;
25140 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25141 look ahead to see if an objc keyword follows the attributes. This
25142 is to detect the use of prefix attributes on ObjC @interface and
25143 @protocol. */
25145 static bool
25146 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25148 cp_lexer_save_tokens (parser->lexer);
25149 *attrib = cp_parser_attributes_opt (parser);
25150 gcc_assert (*attrib);
25151 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25153 cp_lexer_commit_tokens (parser->lexer);
25154 return true;
25156 cp_lexer_rollback_tokens (parser->lexer);
25157 return false;
25160 /* This routine is a minimal replacement for
25161 c_parser_struct_declaration () used when parsing the list of
25162 types/names or ObjC++ properties. For example, when parsing the
25163 code
25165 @property (readonly) int a, b, c;
25167 this function is responsible for parsing "int a, int b, int c" and
25168 returning the declarations as CHAIN of DECLs.
25170 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25171 similar parsing. */
25172 static tree
25173 cp_parser_objc_struct_declaration (cp_parser *parser)
25175 tree decls = NULL_TREE;
25176 cp_decl_specifier_seq declspecs;
25177 int decl_class_or_enum_p;
25178 tree prefix_attributes;
25180 cp_parser_decl_specifier_seq (parser,
25181 CP_PARSER_FLAGS_NONE,
25182 &declspecs,
25183 &decl_class_or_enum_p);
25185 if (declspecs.type == error_mark_node)
25186 return error_mark_node;
25188 /* auto, register, static, extern, mutable. */
25189 if (declspecs.storage_class != sc_none)
25191 cp_parser_error (parser, "invalid type for property");
25192 declspecs.storage_class = sc_none;
25195 /* thread_local. */
25196 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25198 cp_parser_error (parser, "invalid type for property");
25199 declspecs.locations[ds_thread] = 0;
25202 /* typedef. */
25203 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25205 cp_parser_error (parser, "invalid type for property");
25206 declspecs.locations[ds_typedef] = 0;
25209 prefix_attributes = declspecs.attributes;
25210 declspecs.attributes = NULL_TREE;
25212 /* Keep going until we hit the `;' at the end of the declaration. */
25213 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25215 tree attributes, first_attribute, decl;
25216 cp_declarator *declarator;
25217 cp_token *token;
25219 /* Parse the declarator. */
25220 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25221 NULL, NULL, false);
25223 /* Look for attributes that apply to the ivar. */
25224 attributes = cp_parser_attributes_opt (parser);
25225 /* Remember which attributes are prefix attributes and
25226 which are not. */
25227 first_attribute = attributes;
25228 /* Combine the attributes. */
25229 attributes = chainon (prefix_attributes, attributes);
25231 decl = grokfield (declarator, &declspecs,
25232 NULL_TREE, /*init_const_expr_p=*/false,
25233 NULL_TREE, attributes);
25235 if (decl == error_mark_node || decl == NULL_TREE)
25236 return error_mark_node;
25238 /* Reset PREFIX_ATTRIBUTES. */
25239 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25240 attributes = TREE_CHAIN (attributes);
25241 if (attributes)
25242 TREE_CHAIN (attributes) = NULL_TREE;
25244 DECL_CHAIN (decl) = decls;
25245 decls = decl;
25247 token = cp_lexer_peek_token (parser->lexer);
25248 if (token->type == CPP_COMMA)
25250 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25251 continue;
25253 else
25254 break;
25256 return decls;
25259 /* Parse an Objective-C @property declaration. The syntax is:
25261 objc-property-declaration:
25262 '@property' objc-property-attributes[opt] struct-declaration ;
25264 objc-property-attributes:
25265 '(' objc-property-attribute-list ')'
25267 objc-property-attribute-list:
25268 objc-property-attribute
25269 objc-property-attribute-list, objc-property-attribute
25271 objc-property-attribute
25272 'getter' = identifier
25273 'setter' = identifier
25274 'readonly'
25275 'readwrite'
25276 'assign'
25277 'retain'
25278 'copy'
25279 'nonatomic'
25281 For example:
25282 @property NSString *name;
25283 @property (readonly) id object;
25284 @property (retain, nonatomic, getter=getTheName) id name;
25285 @property int a, b, c;
25287 PS: This function is identical to
25288 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25289 static void
25290 cp_parser_objc_at_property_declaration (cp_parser *parser)
25292 /* The following variables hold the attributes of the properties as
25293 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25294 seen. When we see an attribute, we set them to 'true' (if they
25295 are boolean properties) or to the identifier (if they have an
25296 argument, ie, for getter and setter). Note that here we only
25297 parse the list of attributes, check the syntax and accumulate the
25298 attributes that we find. objc_add_property_declaration() will
25299 then process the information. */
25300 bool property_assign = false;
25301 bool property_copy = false;
25302 tree property_getter_ident = NULL_TREE;
25303 bool property_nonatomic = false;
25304 bool property_readonly = false;
25305 bool property_readwrite = false;
25306 bool property_retain = false;
25307 tree property_setter_ident = NULL_TREE;
25309 /* 'properties' is the list of properties that we read. Usually a
25310 single one, but maybe more (eg, in "@property int a, b, c;" there
25311 are three). */
25312 tree properties;
25313 location_t loc;
25315 loc = cp_lexer_peek_token (parser->lexer)->location;
25317 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25319 /* Parse the optional attribute list... */
25320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25322 /* Eat the '('. */
25323 cp_lexer_consume_token (parser->lexer);
25325 while (true)
25327 bool syntax_error = false;
25328 cp_token *token = cp_lexer_peek_token (parser->lexer);
25329 enum rid keyword;
25331 if (token->type != CPP_NAME)
25333 cp_parser_error (parser, "expected identifier");
25334 break;
25336 keyword = C_RID_CODE (token->u.value);
25337 cp_lexer_consume_token (parser->lexer);
25338 switch (keyword)
25340 case RID_ASSIGN: property_assign = true; break;
25341 case RID_COPY: property_copy = true; break;
25342 case RID_NONATOMIC: property_nonatomic = true; break;
25343 case RID_READONLY: property_readonly = true; break;
25344 case RID_READWRITE: property_readwrite = true; break;
25345 case RID_RETAIN: property_retain = true; break;
25347 case RID_GETTER:
25348 case RID_SETTER:
25349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25351 if (keyword == RID_GETTER)
25352 cp_parser_error (parser,
25353 "missing %<=%> (after %<getter%> attribute)");
25354 else
25355 cp_parser_error (parser,
25356 "missing %<=%> (after %<setter%> attribute)");
25357 syntax_error = true;
25358 break;
25360 cp_lexer_consume_token (parser->lexer); /* eat the = */
25361 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25363 cp_parser_error (parser, "expected identifier");
25364 syntax_error = true;
25365 break;
25367 if (keyword == RID_SETTER)
25369 if (property_setter_ident != NULL_TREE)
25371 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25372 cp_lexer_consume_token (parser->lexer);
25374 else
25375 property_setter_ident = cp_parser_objc_selector (parser);
25376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25377 cp_parser_error (parser, "setter name must terminate with %<:%>");
25378 else
25379 cp_lexer_consume_token (parser->lexer);
25381 else
25383 if (property_getter_ident != NULL_TREE)
25385 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25386 cp_lexer_consume_token (parser->lexer);
25388 else
25389 property_getter_ident = cp_parser_objc_selector (parser);
25391 break;
25392 default:
25393 cp_parser_error (parser, "unknown property attribute");
25394 syntax_error = true;
25395 break;
25398 if (syntax_error)
25399 break;
25401 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25402 cp_lexer_consume_token (parser->lexer);
25403 else
25404 break;
25407 /* FIXME: "@property (setter, assign);" will generate a spurious
25408 "error: expected ‘)’ before ‘,’ token". This is because
25409 cp_parser_require, unlike the C counterpart, will produce an
25410 error even if we are in error recovery. */
25411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25413 cp_parser_skip_to_closing_parenthesis (parser,
25414 /*recovering=*/true,
25415 /*or_comma=*/false,
25416 /*consume_paren=*/true);
25420 /* ... and the property declaration(s). */
25421 properties = cp_parser_objc_struct_declaration (parser);
25423 if (properties == error_mark_node)
25425 cp_parser_skip_to_end_of_statement (parser);
25426 /* If the next token is now a `;', consume it. */
25427 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25428 cp_lexer_consume_token (parser->lexer);
25429 return;
25432 if (properties == NULL_TREE)
25433 cp_parser_error (parser, "expected identifier");
25434 else
25436 /* Comma-separated properties are chained together in
25437 reverse order; add them one by one. */
25438 properties = nreverse (properties);
25440 for (; properties; properties = TREE_CHAIN (properties))
25441 objc_add_property_declaration (loc, copy_node (properties),
25442 property_readonly, property_readwrite,
25443 property_assign, property_retain,
25444 property_copy, property_nonatomic,
25445 property_getter_ident, property_setter_ident);
25448 cp_parser_consume_semicolon_at_end_of_statement (parser);
25451 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25453 objc-synthesize-declaration:
25454 @synthesize objc-synthesize-identifier-list ;
25456 objc-synthesize-identifier-list:
25457 objc-synthesize-identifier
25458 objc-synthesize-identifier-list, objc-synthesize-identifier
25460 objc-synthesize-identifier
25461 identifier
25462 identifier = identifier
25464 For example:
25465 @synthesize MyProperty;
25466 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25468 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25469 for C. Keep them in sync.
25471 static void
25472 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25474 tree list = NULL_TREE;
25475 location_t loc;
25476 loc = cp_lexer_peek_token (parser->lexer)->location;
25478 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25479 while (true)
25481 tree property, ivar;
25482 property = cp_parser_identifier (parser);
25483 if (property == error_mark_node)
25485 cp_parser_consume_semicolon_at_end_of_statement (parser);
25486 return;
25488 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25490 cp_lexer_consume_token (parser->lexer);
25491 ivar = cp_parser_identifier (parser);
25492 if (ivar == error_mark_node)
25494 cp_parser_consume_semicolon_at_end_of_statement (parser);
25495 return;
25498 else
25499 ivar = NULL_TREE;
25500 list = chainon (list, build_tree_list (ivar, property));
25501 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25502 cp_lexer_consume_token (parser->lexer);
25503 else
25504 break;
25506 cp_parser_consume_semicolon_at_end_of_statement (parser);
25507 objc_add_synthesize_declaration (loc, list);
25510 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25512 objc-dynamic-declaration:
25513 @dynamic identifier-list ;
25515 For example:
25516 @dynamic MyProperty;
25517 @dynamic MyProperty, AnotherProperty;
25519 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25520 for C. Keep them in sync.
25522 static void
25523 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25525 tree list = NULL_TREE;
25526 location_t loc;
25527 loc = cp_lexer_peek_token (parser->lexer)->location;
25529 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25530 while (true)
25532 tree property;
25533 property = cp_parser_identifier (parser);
25534 if (property == error_mark_node)
25536 cp_parser_consume_semicolon_at_end_of_statement (parser);
25537 return;
25539 list = chainon (list, build_tree_list (NULL, property));
25540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25541 cp_lexer_consume_token (parser->lexer);
25542 else
25543 break;
25545 cp_parser_consume_semicolon_at_end_of_statement (parser);
25546 objc_add_dynamic_declaration (loc, list);
25550 /* OpenMP 2.5 parsing routines. */
25552 /* Returns name of the next clause.
25553 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25554 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25555 returned and the token is consumed. */
25557 static pragma_omp_clause
25558 cp_parser_omp_clause_name (cp_parser *parser)
25560 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25562 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25563 result = PRAGMA_OMP_CLAUSE_IF;
25564 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25565 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25566 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25567 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25568 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25570 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25571 const char *p = IDENTIFIER_POINTER (id);
25573 switch (p[0])
25575 case 'c':
25576 if (!strcmp ("collapse", p))
25577 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25578 else if (!strcmp ("copyin", p))
25579 result = PRAGMA_OMP_CLAUSE_COPYIN;
25580 else if (!strcmp ("copyprivate", p))
25581 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25582 break;
25583 case 'f':
25584 if (!strcmp ("final", p))
25585 result = PRAGMA_OMP_CLAUSE_FINAL;
25586 else if (!strcmp ("firstprivate", p))
25587 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25588 break;
25589 case 'l':
25590 if (!strcmp ("lastprivate", p))
25591 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25592 break;
25593 case 'm':
25594 if (!strcmp ("mergeable", p))
25595 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25596 break;
25597 case 'n':
25598 if (!strcmp ("nowait", p))
25599 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25600 else if (!strcmp ("num_threads", p))
25601 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25602 break;
25603 case 'o':
25604 if (!strcmp ("ordered", p))
25605 result = PRAGMA_OMP_CLAUSE_ORDERED;
25606 break;
25607 case 'r':
25608 if (!strcmp ("reduction", p))
25609 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25610 break;
25611 case 's':
25612 if (!strcmp ("schedule", p))
25613 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25614 else if (!strcmp ("shared", p))
25615 result = PRAGMA_OMP_CLAUSE_SHARED;
25616 break;
25617 case 'u':
25618 if (!strcmp ("untied", p))
25619 result = PRAGMA_OMP_CLAUSE_UNTIED;
25620 break;
25624 if (result != PRAGMA_OMP_CLAUSE_NONE)
25625 cp_lexer_consume_token (parser->lexer);
25627 return result;
25630 /* Validate that a clause of the given type does not already exist. */
25632 static void
25633 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25634 const char *name, location_t location)
25636 tree c;
25638 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25639 if (OMP_CLAUSE_CODE (c) == code)
25641 error_at (location, "too many %qs clauses", name);
25642 break;
25646 /* OpenMP 2.5:
25647 variable-list:
25648 identifier
25649 variable-list , identifier
25651 In addition, we match a closing parenthesis. An opening parenthesis
25652 will have been consumed by the caller.
25654 If KIND is nonzero, create the appropriate node and install the decl
25655 in OMP_CLAUSE_DECL and add the node to the head of the list.
25657 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25658 return the list created. */
25660 static tree
25661 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25662 tree list)
25664 cp_token *token;
25665 while (1)
25667 tree name, decl;
25669 token = cp_lexer_peek_token (parser->lexer);
25670 name = cp_parser_id_expression (parser, /*template_p=*/false,
25671 /*check_dependency_p=*/true,
25672 /*template_p=*/NULL,
25673 /*declarator_p=*/false,
25674 /*optional_p=*/false);
25675 if (name == error_mark_node)
25676 goto skip_comma;
25678 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25679 if (decl == error_mark_node)
25680 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25681 token->location);
25682 else if (kind != 0)
25684 tree u = build_omp_clause (token->location, kind);
25685 OMP_CLAUSE_DECL (u) = decl;
25686 OMP_CLAUSE_CHAIN (u) = list;
25687 list = u;
25689 else
25690 list = tree_cons (decl, NULL_TREE, list);
25692 get_comma:
25693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25694 break;
25695 cp_lexer_consume_token (parser->lexer);
25698 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25700 int ending;
25702 /* Try to resync to an unnested comma. Copied from
25703 cp_parser_parenthesized_expression_list. */
25704 skip_comma:
25705 ending = cp_parser_skip_to_closing_parenthesis (parser,
25706 /*recovering=*/true,
25707 /*or_comma=*/true,
25708 /*consume_paren=*/true);
25709 if (ending < 0)
25710 goto get_comma;
25713 return list;
25716 /* Similarly, but expect leading and trailing parenthesis. This is a very
25717 common case for omp clauses. */
25719 static tree
25720 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25722 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25723 return cp_parser_omp_var_list_no_open (parser, kind, list);
25724 return list;
25727 /* OpenMP 3.0:
25728 collapse ( constant-expression ) */
25730 static tree
25731 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25733 tree c, num;
25734 location_t loc;
25735 HOST_WIDE_INT n;
25737 loc = cp_lexer_peek_token (parser->lexer)->location;
25738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25739 return list;
25741 num = cp_parser_constant_expression (parser, false, NULL);
25743 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25745 /*or_comma=*/false,
25746 /*consume_paren=*/true);
25748 if (num == error_mark_node)
25749 return list;
25750 num = fold_non_dependent_expr (num);
25751 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25752 || !host_integerp (num, 0)
25753 || (n = tree_low_cst (num, 0)) <= 0
25754 || (int) n != n)
25756 error_at (loc, "collapse argument needs positive constant integer expression");
25757 return list;
25760 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25761 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25762 OMP_CLAUSE_CHAIN (c) = list;
25763 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25765 return c;
25768 /* OpenMP 2.5:
25769 default ( shared | none ) */
25771 static tree
25772 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25774 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25775 tree c;
25777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25778 return list;
25779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25781 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25782 const char *p = IDENTIFIER_POINTER (id);
25784 switch (p[0])
25786 case 'n':
25787 if (strcmp ("none", p) != 0)
25788 goto invalid_kind;
25789 kind = OMP_CLAUSE_DEFAULT_NONE;
25790 break;
25792 case 's':
25793 if (strcmp ("shared", p) != 0)
25794 goto invalid_kind;
25795 kind = OMP_CLAUSE_DEFAULT_SHARED;
25796 break;
25798 default:
25799 goto invalid_kind;
25802 cp_lexer_consume_token (parser->lexer);
25804 else
25806 invalid_kind:
25807 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25810 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25811 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25812 /*or_comma=*/false,
25813 /*consume_paren=*/true);
25815 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25816 return list;
25818 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25819 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25820 OMP_CLAUSE_CHAIN (c) = list;
25821 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25823 return c;
25826 /* OpenMP 3.1:
25827 final ( expression ) */
25829 static tree
25830 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25832 tree t, c;
25834 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25835 return list;
25837 t = cp_parser_condition (parser);
25839 if (t == error_mark_node
25840 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25841 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25842 /*or_comma=*/false,
25843 /*consume_paren=*/true);
25845 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25847 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25848 OMP_CLAUSE_FINAL_EXPR (c) = t;
25849 OMP_CLAUSE_CHAIN (c) = list;
25851 return c;
25854 /* OpenMP 2.5:
25855 if ( expression ) */
25857 static tree
25858 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25860 tree t, c;
25862 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25863 return list;
25865 t = cp_parser_condition (parser);
25867 if (t == error_mark_node
25868 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25870 /*or_comma=*/false,
25871 /*consume_paren=*/true);
25873 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25875 c = build_omp_clause (location, OMP_CLAUSE_IF);
25876 OMP_CLAUSE_IF_EXPR (c) = t;
25877 OMP_CLAUSE_CHAIN (c) = list;
25879 return c;
25882 /* OpenMP 3.1:
25883 mergeable */
25885 static tree
25886 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25887 tree list, location_t location)
25889 tree c;
25891 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25892 location);
25894 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25895 OMP_CLAUSE_CHAIN (c) = list;
25896 return c;
25899 /* OpenMP 2.5:
25900 nowait */
25902 static tree
25903 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25904 tree list, location_t location)
25906 tree c;
25908 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25910 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25911 OMP_CLAUSE_CHAIN (c) = list;
25912 return c;
25915 /* OpenMP 2.5:
25916 num_threads ( expression ) */
25918 static tree
25919 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25920 location_t location)
25922 tree t, c;
25924 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25925 return list;
25927 t = cp_parser_expression (parser, false, NULL);
25929 if (t == error_mark_node
25930 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25932 /*or_comma=*/false,
25933 /*consume_paren=*/true);
25935 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25936 "num_threads", location);
25938 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25939 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25940 OMP_CLAUSE_CHAIN (c) = list;
25942 return c;
25945 /* OpenMP 2.5:
25946 ordered */
25948 static tree
25949 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
25950 tree list, location_t location)
25952 tree c;
25954 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25955 "ordered", location);
25957 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25958 OMP_CLAUSE_CHAIN (c) = list;
25959 return c;
25962 /* OpenMP 2.5:
25963 reduction ( reduction-operator : variable-list )
25965 reduction-operator:
25966 One of: + * - & ^ | && ||
25968 OpenMP 3.1:
25970 reduction-operator:
25971 One of: + * - & ^ | && || min max */
25973 static tree
25974 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25976 enum tree_code code;
25977 tree nlist, c;
25979 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25980 return list;
25982 switch (cp_lexer_peek_token (parser->lexer)->type)
25984 case CPP_PLUS:
25985 code = PLUS_EXPR;
25986 break;
25987 case CPP_MULT:
25988 code = MULT_EXPR;
25989 break;
25990 case CPP_MINUS:
25991 code = MINUS_EXPR;
25992 break;
25993 case CPP_AND:
25994 code = BIT_AND_EXPR;
25995 break;
25996 case CPP_XOR:
25997 code = BIT_XOR_EXPR;
25998 break;
25999 case CPP_OR:
26000 code = BIT_IOR_EXPR;
26001 break;
26002 case CPP_AND_AND:
26003 code = TRUTH_ANDIF_EXPR;
26004 break;
26005 case CPP_OR_OR:
26006 code = TRUTH_ORIF_EXPR;
26007 break;
26008 case CPP_NAME:
26010 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26011 const char *p = IDENTIFIER_POINTER (id);
26013 if (strcmp (p, "min") == 0)
26015 code = MIN_EXPR;
26016 break;
26018 if (strcmp (p, "max") == 0)
26020 code = MAX_EXPR;
26021 break;
26024 /* FALLTHROUGH */
26025 default:
26026 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26027 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26028 resync_fail:
26029 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26030 /*or_comma=*/false,
26031 /*consume_paren=*/true);
26032 return list;
26034 cp_lexer_consume_token (parser->lexer);
26036 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26037 goto resync_fail;
26039 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26040 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26041 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26043 return nlist;
26046 /* OpenMP 2.5:
26047 schedule ( schedule-kind )
26048 schedule ( schedule-kind , expression )
26050 schedule-kind:
26051 static | dynamic | guided | runtime | auto */
26053 static tree
26054 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26056 tree c, t;
26058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26059 return list;
26061 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26066 const char *p = IDENTIFIER_POINTER (id);
26068 switch (p[0])
26070 case 'd':
26071 if (strcmp ("dynamic", p) != 0)
26072 goto invalid_kind;
26073 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26074 break;
26076 case 'g':
26077 if (strcmp ("guided", p) != 0)
26078 goto invalid_kind;
26079 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26080 break;
26082 case 'r':
26083 if (strcmp ("runtime", p) != 0)
26084 goto invalid_kind;
26085 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26086 break;
26088 default:
26089 goto invalid_kind;
26092 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26093 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26094 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26095 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26096 else
26097 goto invalid_kind;
26098 cp_lexer_consume_token (parser->lexer);
26100 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26102 cp_token *token;
26103 cp_lexer_consume_token (parser->lexer);
26105 token = cp_lexer_peek_token (parser->lexer);
26106 t = cp_parser_assignment_expression (parser, false, NULL);
26108 if (t == error_mark_node)
26109 goto resync_fail;
26110 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26111 error_at (token->location, "schedule %<runtime%> does not take "
26112 "a %<chunk_size%> parameter");
26113 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26114 error_at (token->location, "schedule %<auto%> does not take "
26115 "a %<chunk_size%> parameter");
26116 else
26117 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26120 goto resync_fail;
26122 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26123 goto resync_fail;
26125 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26126 OMP_CLAUSE_CHAIN (c) = list;
26127 return c;
26129 invalid_kind:
26130 cp_parser_error (parser, "invalid schedule kind");
26131 resync_fail:
26132 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26133 /*or_comma=*/false,
26134 /*consume_paren=*/true);
26135 return list;
26138 /* OpenMP 3.0:
26139 untied */
26141 static tree
26142 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26143 tree list, location_t location)
26145 tree c;
26147 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26149 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26150 OMP_CLAUSE_CHAIN (c) = list;
26151 return c;
26154 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26155 is a bitmask in MASK. Return the list of clauses found; the result
26156 of clause default goes in *pdefault. */
26158 static tree
26159 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26160 const char *where, cp_token *pragma_tok)
26162 tree clauses = NULL;
26163 bool first = true;
26164 cp_token *token = NULL;
26166 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26168 pragma_omp_clause c_kind;
26169 const char *c_name;
26170 tree prev = clauses;
26172 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26173 cp_lexer_consume_token (parser->lexer);
26175 token = cp_lexer_peek_token (parser->lexer);
26176 c_kind = cp_parser_omp_clause_name (parser);
26177 first = false;
26179 switch (c_kind)
26181 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26182 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26183 token->location);
26184 c_name = "collapse";
26185 break;
26186 case PRAGMA_OMP_CLAUSE_COPYIN:
26187 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26188 c_name = "copyin";
26189 break;
26190 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26191 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26192 clauses);
26193 c_name = "copyprivate";
26194 break;
26195 case PRAGMA_OMP_CLAUSE_DEFAULT:
26196 clauses = cp_parser_omp_clause_default (parser, clauses,
26197 token->location);
26198 c_name = "default";
26199 break;
26200 case PRAGMA_OMP_CLAUSE_FINAL:
26201 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26202 c_name = "final";
26203 break;
26204 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26205 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26206 clauses);
26207 c_name = "firstprivate";
26208 break;
26209 case PRAGMA_OMP_CLAUSE_IF:
26210 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26211 c_name = "if";
26212 break;
26213 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26214 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26215 clauses);
26216 c_name = "lastprivate";
26217 break;
26218 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26219 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26220 token->location);
26221 c_name = "mergeable";
26222 break;
26223 case PRAGMA_OMP_CLAUSE_NOWAIT:
26224 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26225 c_name = "nowait";
26226 break;
26227 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26228 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26229 token->location);
26230 c_name = "num_threads";
26231 break;
26232 case PRAGMA_OMP_CLAUSE_ORDERED:
26233 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26234 token->location);
26235 c_name = "ordered";
26236 break;
26237 case PRAGMA_OMP_CLAUSE_PRIVATE:
26238 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26239 clauses);
26240 c_name = "private";
26241 break;
26242 case PRAGMA_OMP_CLAUSE_REDUCTION:
26243 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26244 c_name = "reduction";
26245 break;
26246 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26247 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26248 token->location);
26249 c_name = "schedule";
26250 break;
26251 case PRAGMA_OMP_CLAUSE_SHARED:
26252 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26253 clauses);
26254 c_name = "shared";
26255 break;
26256 case PRAGMA_OMP_CLAUSE_UNTIED:
26257 clauses = cp_parser_omp_clause_untied (parser, clauses,
26258 token->location);
26259 c_name = "nowait";
26260 break;
26261 default:
26262 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26263 goto saw_error;
26266 if (((mask >> c_kind) & 1) == 0)
26268 /* Remove the invalid clause(s) from the list to avoid
26269 confusing the rest of the compiler. */
26270 clauses = prev;
26271 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26274 saw_error:
26275 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26276 return finish_omp_clauses (clauses);
26279 /* OpenMP 2.5:
26280 structured-block:
26281 statement
26283 In practice, we're also interested in adding the statement to an
26284 outer node. So it is convenient if we work around the fact that
26285 cp_parser_statement calls add_stmt. */
26287 static unsigned
26288 cp_parser_begin_omp_structured_block (cp_parser *parser)
26290 unsigned save = parser->in_statement;
26292 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26293 This preserves the "not within loop or switch" style error messages
26294 for nonsense cases like
26295 void foo() {
26296 #pragma omp single
26297 break;
26300 if (parser->in_statement)
26301 parser->in_statement = IN_OMP_BLOCK;
26303 return save;
26306 static void
26307 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26309 parser->in_statement = save;
26312 static tree
26313 cp_parser_omp_structured_block (cp_parser *parser)
26315 tree stmt = begin_omp_structured_block ();
26316 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26318 cp_parser_statement (parser, NULL_TREE, false, NULL);
26320 cp_parser_end_omp_structured_block (parser, save);
26321 return finish_omp_structured_block (stmt);
26324 /* OpenMP 2.5:
26325 # pragma omp atomic new-line
26326 expression-stmt
26328 expression-stmt:
26329 x binop= expr | x++ | ++x | x-- | --x
26330 binop:
26331 +, *, -, /, &, ^, |, <<, >>
26333 where x is an lvalue expression with scalar type.
26335 OpenMP 3.1:
26336 # pragma omp atomic new-line
26337 update-stmt
26339 # pragma omp atomic read new-line
26340 read-stmt
26342 # pragma omp atomic write new-line
26343 write-stmt
26345 # pragma omp atomic update new-line
26346 update-stmt
26348 # pragma omp atomic capture new-line
26349 capture-stmt
26351 # pragma omp atomic capture new-line
26352 capture-block
26354 read-stmt:
26355 v = x
26356 write-stmt:
26357 x = expr
26358 update-stmt:
26359 expression-stmt | x = x binop expr
26360 capture-stmt:
26361 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26362 capture-block:
26363 { v = x; update-stmt; } | { update-stmt; v = x; }
26365 where x and v are lvalue expressions with scalar type. */
26367 static void
26368 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26370 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26371 tree rhs1 = NULL_TREE, orig_lhs;
26372 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26373 bool structured_block = false;
26375 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26377 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26378 const char *p = IDENTIFIER_POINTER (id);
26380 if (!strcmp (p, "read"))
26381 code = OMP_ATOMIC_READ;
26382 else if (!strcmp (p, "write"))
26383 code = NOP_EXPR;
26384 else if (!strcmp (p, "update"))
26385 code = OMP_ATOMIC;
26386 else if (!strcmp (p, "capture"))
26387 code = OMP_ATOMIC_CAPTURE_NEW;
26388 else
26389 p = NULL;
26390 if (p)
26391 cp_lexer_consume_token (parser->lexer);
26393 cp_parser_require_pragma_eol (parser, pragma_tok);
26395 switch (code)
26397 case OMP_ATOMIC_READ:
26398 case NOP_EXPR: /* atomic write */
26399 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26400 /*cast_p=*/false, NULL);
26401 if (v == error_mark_node)
26402 goto saw_error;
26403 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26404 goto saw_error;
26405 if (code == NOP_EXPR)
26406 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26407 else
26408 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26409 /*cast_p=*/false, NULL);
26410 if (lhs == error_mark_node)
26411 goto saw_error;
26412 if (code == NOP_EXPR)
26414 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26415 opcode. */
26416 code = OMP_ATOMIC;
26417 rhs = lhs;
26418 lhs = v;
26419 v = NULL_TREE;
26421 goto done;
26422 case OMP_ATOMIC_CAPTURE_NEW:
26423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26425 cp_lexer_consume_token (parser->lexer);
26426 structured_block = true;
26428 else
26430 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26431 /*cast_p=*/false, NULL);
26432 if (v == error_mark_node)
26433 goto saw_error;
26434 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26435 goto saw_error;
26437 default:
26438 break;
26441 restart:
26442 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26443 /*cast_p=*/false, NULL);
26444 orig_lhs = lhs;
26445 switch (TREE_CODE (lhs))
26447 case ERROR_MARK:
26448 goto saw_error;
26450 case POSTINCREMENT_EXPR:
26451 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26452 code = OMP_ATOMIC_CAPTURE_OLD;
26453 /* FALLTHROUGH */
26454 case PREINCREMENT_EXPR:
26455 lhs = TREE_OPERAND (lhs, 0);
26456 opcode = PLUS_EXPR;
26457 rhs = integer_one_node;
26458 break;
26460 case POSTDECREMENT_EXPR:
26461 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26462 code = OMP_ATOMIC_CAPTURE_OLD;
26463 /* FALLTHROUGH */
26464 case PREDECREMENT_EXPR:
26465 lhs = TREE_OPERAND (lhs, 0);
26466 opcode = MINUS_EXPR;
26467 rhs = integer_one_node;
26468 break;
26470 case COMPOUND_EXPR:
26471 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26472 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26473 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26474 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26475 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26476 (TREE_OPERAND (lhs, 1), 0), 0)))
26477 == BOOLEAN_TYPE)
26478 /* Undo effects of boolean_increment for post {in,de}crement. */
26479 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26480 /* FALLTHRU */
26481 case MODIFY_EXPR:
26482 if (TREE_CODE (lhs) == MODIFY_EXPR
26483 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26485 /* Undo effects of boolean_increment. */
26486 if (integer_onep (TREE_OPERAND (lhs, 1)))
26488 /* This is pre or post increment. */
26489 rhs = TREE_OPERAND (lhs, 1);
26490 lhs = TREE_OPERAND (lhs, 0);
26491 opcode = NOP_EXPR;
26492 if (code == OMP_ATOMIC_CAPTURE_NEW
26493 && !structured_block
26494 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26495 code = OMP_ATOMIC_CAPTURE_OLD;
26496 break;
26499 /* FALLTHRU */
26500 default:
26501 switch (cp_lexer_peek_token (parser->lexer)->type)
26503 case CPP_MULT_EQ:
26504 opcode = MULT_EXPR;
26505 break;
26506 case CPP_DIV_EQ:
26507 opcode = TRUNC_DIV_EXPR;
26508 break;
26509 case CPP_PLUS_EQ:
26510 opcode = PLUS_EXPR;
26511 break;
26512 case CPP_MINUS_EQ:
26513 opcode = MINUS_EXPR;
26514 break;
26515 case CPP_LSHIFT_EQ:
26516 opcode = LSHIFT_EXPR;
26517 break;
26518 case CPP_RSHIFT_EQ:
26519 opcode = RSHIFT_EXPR;
26520 break;
26521 case CPP_AND_EQ:
26522 opcode = BIT_AND_EXPR;
26523 break;
26524 case CPP_OR_EQ:
26525 opcode = BIT_IOR_EXPR;
26526 break;
26527 case CPP_XOR_EQ:
26528 opcode = BIT_XOR_EXPR;
26529 break;
26530 case CPP_EQ:
26531 if (structured_block || code == OMP_ATOMIC)
26533 enum cp_parser_prec oprec;
26534 cp_token *token;
26535 cp_lexer_consume_token (parser->lexer);
26536 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26537 /*cast_p=*/false, NULL);
26538 if (rhs1 == error_mark_node)
26539 goto saw_error;
26540 token = cp_lexer_peek_token (parser->lexer);
26541 switch (token->type)
26543 case CPP_SEMICOLON:
26544 if (code == OMP_ATOMIC_CAPTURE_NEW)
26546 code = OMP_ATOMIC_CAPTURE_OLD;
26547 v = lhs;
26548 lhs = NULL_TREE;
26549 lhs1 = rhs1;
26550 rhs1 = NULL_TREE;
26551 cp_lexer_consume_token (parser->lexer);
26552 goto restart;
26554 cp_parser_error (parser,
26555 "invalid form of %<#pragma omp atomic%>");
26556 goto saw_error;
26557 case CPP_MULT:
26558 opcode = MULT_EXPR;
26559 break;
26560 case CPP_DIV:
26561 opcode = TRUNC_DIV_EXPR;
26562 break;
26563 case CPP_PLUS:
26564 opcode = PLUS_EXPR;
26565 break;
26566 case CPP_MINUS:
26567 opcode = MINUS_EXPR;
26568 break;
26569 case CPP_LSHIFT:
26570 opcode = LSHIFT_EXPR;
26571 break;
26572 case CPP_RSHIFT:
26573 opcode = RSHIFT_EXPR;
26574 break;
26575 case CPP_AND:
26576 opcode = BIT_AND_EXPR;
26577 break;
26578 case CPP_OR:
26579 opcode = BIT_IOR_EXPR;
26580 break;
26581 case CPP_XOR:
26582 opcode = BIT_XOR_EXPR;
26583 break;
26584 default:
26585 cp_parser_error (parser,
26586 "invalid operator for %<#pragma omp atomic%>");
26587 goto saw_error;
26589 oprec = TOKEN_PRECEDENCE (token);
26590 gcc_assert (oprec != PREC_NOT_OPERATOR);
26591 if (commutative_tree_code (opcode))
26592 oprec = (enum cp_parser_prec) (oprec - 1);
26593 cp_lexer_consume_token (parser->lexer);
26594 rhs = cp_parser_binary_expression (parser, false, false,
26595 oprec, NULL);
26596 if (rhs == error_mark_node)
26597 goto saw_error;
26598 goto stmt_done;
26600 /* FALLTHROUGH */
26601 default:
26602 cp_parser_error (parser,
26603 "invalid operator for %<#pragma omp atomic%>");
26604 goto saw_error;
26606 cp_lexer_consume_token (parser->lexer);
26608 rhs = cp_parser_expression (parser, false, NULL);
26609 if (rhs == error_mark_node)
26610 goto saw_error;
26611 break;
26613 stmt_done:
26614 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26616 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26617 goto saw_error;
26618 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26619 /*cast_p=*/false, NULL);
26620 if (v == error_mark_node)
26621 goto saw_error;
26622 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26623 goto saw_error;
26624 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26625 /*cast_p=*/false, NULL);
26626 if (lhs1 == error_mark_node)
26627 goto saw_error;
26629 if (structured_block)
26631 cp_parser_consume_semicolon_at_end_of_statement (parser);
26632 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26634 done:
26635 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26636 if (!structured_block)
26637 cp_parser_consume_semicolon_at_end_of_statement (parser);
26638 return;
26640 saw_error:
26641 cp_parser_skip_to_end_of_block_or_statement (parser);
26642 if (structured_block)
26644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26645 cp_lexer_consume_token (parser->lexer);
26646 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26648 cp_parser_skip_to_end_of_block_or_statement (parser);
26649 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26650 cp_lexer_consume_token (parser->lexer);
26656 /* OpenMP 2.5:
26657 # pragma omp barrier new-line */
26659 static void
26660 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26662 cp_parser_require_pragma_eol (parser, pragma_tok);
26663 finish_omp_barrier ();
26666 /* OpenMP 2.5:
26667 # pragma omp critical [(name)] new-line
26668 structured-block */
26670 static tree
26671 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26673 tree stmt, name = NULL;
26675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26677 cp_lexer_consume_token (parser->lexer);
26679 name = cp_parser_identifier (parser);
26681 if (name == error_mark_node
26682 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26684 /*or_comma=*/false,
26685 /*consume_paren=*/true);
26686 if (name == error_mark_node)
26687 name = NULL;
26689 cp_parser_require_pragma_eol (parser, pragma_tok);
26691 stmt = cp_parser_omp_structured_block (parser);
26692 return c_finish_omp_critical (input_location, stmt, name);
26695 /* OpenMP 2.5:
26696 # pragma omp flush flush-vars[opt] new-line
26698 flush-vars:
26699 ( variable-list ) */
26701 static void
26702 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26704 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26705 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26706 cp_parser_require_pragma_eol (parser, pragma_tok);
26708 finish_omp_flush ();
26711 /* Helper function, to parse omp for increment expression. */
26713 static tree
26714 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26716 tree cond = cp_parser_binary_expression (parser, false, true,
26717 PREC_NOT_OPERATOR, NULL);
26718 if (cond == error_mark_node
26719 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26721 cp_parser_skip_to_end_of_statement (parser);
26722 return error_mark_node;
26725 switch (TREE_CODE (cond))
26727 case GT_EXPR:
26728 case GE_EXPR:
26729 case LT_EXPR:
26730 case LE_EXPR:
26731 break;
26732 default:
26733 return error_mark_node;
26736 /* If decl is an iterator, preserve LHS and RHS of the relational
26737 expr until finish_omp_for. */
26738 if (decl
26739 && (type_dependent_expression_p (decl)
26740 || CLASS_TYPE_P (TREE_TYPE (decl))))
26741 return cond;
26743 return build_x_binary_op (input_location, TREE_CODE (cond),
26744 TREE_OPERAND (cond, 0), ERROR_MARK,
26745 TREE_OPERAND (cond, 1), ERROR_MARK,
26746 /*overload=*/NULL, tf_warning_or_error);
26749 /* Helper function, to parse omp for increment expression. */
26751 static tree
26752 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26754 cp_token *token = cp_lexer_peek_token (parser->lexer);
26755 enum tree_code op;
26756 tree lhs, rhs;
26757 cp_id_kind idk;
26758 bool decl_first;
26760 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26762 op = (token->type == CPP_PLUS_PLUS
26763 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26764 cp_lexer_consume_token (parser->lexer);
26765 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26766 if (lhs != decl)
26767 return error_mark_node;
26768 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26771 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26772 if (lhs != decl)
26773 return error_mark_node;
26775 token = cp_lexer_peek_token (parser->lexer);
26776 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26778 op = (token->type == CPP_PLUS_PLUS
26779 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26780 cp_lexer_consume_token (parser->lexer);
26781 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26784 op = cp_parser_assignment_operator_opt (parser);
26785 if (op == ERROR_MARK)
26786 return error_mark_node;
26788 if (op != NOP_EXPR)
26790 rhs = cp_parser_assignment_expression (parser, false, NULL);
26791 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26792 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26795 lhs = cp_parser_binary_expression (parser, false, false,
26796 PREC_ADDITIVE_EXPRESSION, NULL);
26797 token = cp_lexer_peek_token (parser->lexer);
26798 decl_first = lhs == decl;
26799 if (decl_first)
26800 lhs = NULL_TREE;
26801 if (token->type != CPP_PLUS
26802 && token->type != CPP_MINUS)
26803 return error_mark_node;
26807 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26808 cp_lexer_consume_token (parser->lexer);
26809 rhs = cp_parser_binary_expression (parser, false, false,
26810 PREC_ADDITIVE_EXPRESSION, NULL);
26811 token = cp_lexer_peek_token (parser->lexer);
26812 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26814 if (lhs == NULL_TREE)
26816 if (op == PLUS_EXPR)
26817 lhs = rhs;
26818 else
26819 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26820 tf_warning_or_error);
26822 else
26823 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26824 ERROR_MARK, NULL, tf_warning_or_error);
26827 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26829 if (!decl_first)
26831 if (rhs != decl || op == MINUS_EXPR)
26832 return error_mark_node;
26833 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26835 else
26836 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26838 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26841 /* Parse the restricted form of the for statement allowed by OpenMP. */
26843 static tree
26844 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26846 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26847 tree real_decl, initv, condv, incrv, declv;
26848 tree this_pre_body, cl;
26849 location_t loc_first;
26850 bool collapse_err = false;
26851 int i, collapse = 1, nbraces = 0;
26852 VEC(tree,gc) *for_block = make_tree_vector ();
26854 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26855 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26856 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26858 gcc_assert (collapse >= 1);
26860 declv = make_tree_vec (collapse);
26861 initv = make_tree_vec (collapse);
26862 condv = make_tree_vec (collapse);
26863 incrv = make_tree_vec (collapse);
26865 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26867 for (i = 0; i < collapse; i++)
26869 int bracecount = 0;
26870 bool add_private_clause = false;
26871 location_t loc;
26873 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26875 cp_parser_error (parser, "for statement expected");
26876 return NULL;
26878 loc = cp_lexer_consume_token (parser->lexer)->location;
26880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26881 return NULL;
26883 init = decl = real_decl = NULL;
26884 this_pre_body = push_stmt_list ();
26885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26887 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26889 init-expr:
26890 var = lb
26891 integer-type var = lb
26892 random-access-iterator-type var = lb
26893 pointer-type var = lb
26895 cp_decl_specifier_seq type_specifiers;
26897 /* First, try to parse as an initialized declaration. See
26898 cp_parser_condition, from whence the bulk of this is copied. */
26900 cp_parser_parse_tentatively (parser);
26901 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26902 /*is_trailing_return=*/false,
26903 &type_specifiers);
26904 if (cp_parser_parse_definitely (parser))
26906 /* If parsing a type specifier seq succeeded, then this
26907 MUST be a initialized declaration. */
26908 tree asm_specification, attributes;
26909 cp_declarator *declarator;
26911 declarator = cp_parser_declarator (parser,
26912 CP_PARSER_DECLARATOR_NAMED,
26913 /*ctor_dtor_or_conv_p=*/NULL,
26914 /*parenthesized_p=*/NULL,
26915 /*member_p=*/false);
26916 attributes = cp_parser_attributes_opt (parser);
26917 asm_specification = cp_parser_asm_specification_opt (parser);
26919 if (declarator == cp_error_declarator)
26920 cp_parser_skip_to_end_of_statement (parser);
26922 else
26924 tree pushed_scope, auto_node;
26926 decl = start_decl (declarator, &type_specifiers,
26927 SD_INITIALIZED, attributes,
26928 /*prefix_attributes=*/NULL_TREE,
26929 &pushed_scope);
26931 auto_node = type_uses_auto (TREE_TYPE (decl));
26932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26934 if (cp_lexer_next_token_is (parser->lexer,
26935 CPP_OPEN_PAREN))
26936 error ("parenthesized initialization is not allowed in "
26937 "OpenMP %<for%> loop");
26938 else
26939 /* Trigger an error. */
26940 cp_parser_require (parser, CPP_EQ, RT_EQ);
26942 init = error_mark_node;
26943 cp_parser_skip_to_end_of_statement (parser);
26945 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26946 || type_dependent_expression_p (decl)
26947 || auto_node)
26949 bool is_direct_init, is_non_constant_init;
26951 init = cp_parser_initializer (parser,
26952 &is_direct_init,
26953 &is_non_constant_init);
26955 if (auto_node)
26957 TREE_TYPE (decl)
26958 = do_auto_deduction (TREE_TYPE (decl), init,
26959 auto_node);
26961 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26962 && !type_dependent_expression_p (decl))
26963 goto non_class;
26966 cp_finish_decl (decl, init, !is_non_constant_init,
26967 asm_specification,
26968 LOOKUP_ONLYCONVERTING);
26969 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26971 VEC_safe_push (tree, gc, for_block, this_pre_body);
26972 init = NULL_TREE;
26974 else
26975 init = pop_stmt_list (this_pre_body);
26976 this_pre_body = NULL_TREE;
26978 else
26980 /* Consume '='. */
26981 cp_lexer_consume_token (parser->lexer);
26982 init = cp_parser_assignment_expression (parser, false, NULL);
26984 non_class:
26985 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26986 init = error_mark_node;
26987 else
26988 cp_finish_decl (decl, NULL_TREE,
26989 /*init_const_expr_p=*/false,
26990 asm_specification,
26991 LOOKUP_ONLYCONVERTING);
26994 if (pushed_scope)
26995 pop_scope (pushed_scope);
26998 else
27000 cp_id_kind idk;
27001 /* If parsing a type specifier sequence failed, then
27002 this MUST be a simple expression. */
27003 cp_parser_parse_tentatively (parser);
27004 decl = cp_parser_primary_expression (parser, false, false,
27005 false, &idk);
27006 if (!cp_parser_error_occurred (parser)
27007 && decl
27008 && DECL_P (decl)
27009 && CLASS_TYPE_P (TREE_TYPE (decl)))
27011 tree rhs;
27013 cp_parser_parse_definitely (parser);
27014 cp_parser_require (parser, CPP_EQ, RT_EQ);
27015 rhs = cp_parser_assignment_expression (parser, false, NULL);
27016 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27017 decl, NOP_EXPR,
27018 rhs,
27019 tf_warning_or_error));
27020 add_private_clause = true;
27022 else
27024 decl = NULL;
27025 cp_parser_abort_tentative_parse (parser);
27026 init = cp_parser_expression (parser, false, NULL);
27027 if (init)
27029 if (TREE_CODE (init) == MODIFY_EXPR
27030 || TREE_CODE (init) == MODOP_EXPR)
27031 real_decl = TREE_OPERAND (init, 0);
27036 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27037 if (this_pre_body)
27039 this_pre_body = pop_stmt_list (this_pre_body);
27040 if (pre_body)
27042 tree t = pre_body;
27043 pre_body = push_stmt_list ();
27044 add_stmt (t);
27045 add_stmt (this_pre_body);
27046 pre_body = pop_stmt_list (pre_body);
27048 else
27049 pre_body = this_pre_body;
27052 if (decl)
27053 real_decl = decl;
27054 if (par_clauses != NULL && real_decl != NULL_TREE)
27056 tree *c;
27057 for (c = par_clauses; *c ; )
27058 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27059 && OMP_CLAUSE_DECL (*c) == real_decl)
27061 error_at (loc, "iteration variable %qD"
27062 " should not be firstprivate", real_decl);
27063 *c = OMP_CLAUSE_CHAIN (*c);
27065 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27066 && OMP_CLAUSE_DECL (*c) == real_decl)
27068 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27069 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27070 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27071 OMP_CLAUSE_DECL (l) = real_decl;
27072 OMP_CLAUSE_CHAIN (l) = clauses;
27073 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27074 clauses = l;
27075 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27076 CP_OMP_CLAUSE_INFO (*c) = NULL;
27077 add_private_clause = false;
27079 else
27081 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27082 && OMP_CLAUSE_DECL (*c) == real_decl)
27083 add_private_clause = false;
27084 c = &OMP_CLAUSE_CHAIN (*c);
27088 if (add_private_clause)
27090 tree c;
27091 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27093 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27094 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27095 && OMP_CLAUSE_DECL (c) == decl)
27096 break;
27097 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27098 && OMP_CLAUSE_DECL (c) == decl)
27099 error_at (loc, "iteration variable %qD "
27100 "should not be firstprivate",
27101 decl);
27102 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27103 && OMP_CLAUSE_DECL (c) == decl)
27104 error_at (loc, "iteration variable %qD should not be reduction",
27105 decl);
27107 if (c == NULL)
27109 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27110 OMP_CLAUSE_DECL (c) = decl;
27111 c = finish_omp_clauses (c);
27112 if (c)
27114 OMP_CLAUSE_CHAIN (c) = clauses;
27115 clauses = c;
27120 cond = NULL;
27121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27122 cond = cp_parser_omp_for_cond (parser, decl);
27123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27125 incr = NULL;
27126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27128 /* If decl is an iterator, preserve the operator on decl
27129 until finish_omp_for. */
27130 if (real_decl
27131 && ((processing_template_decl
27132 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27133 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27134 incr = cp_parser_omp_for_incr (parser, real_decl);
27135 else
27136 incr = cp_parser_expression (parser, false, NULL);
27137 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27138 SET_EXPR_LOCATION (incr, input_location);
27141 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27142 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27143 /*or_comma=*/false,
27144 /*consume_paren=*/true);
27146 TREE_VEC_ELT (declv, i) = decl;
27147 TREE_VEC_ELT (initv, i) = init;
27148 TREE_VEC_ELT (condv, i) = cond;
27149 TREE_VEC_ELT (incrv, i) = incr;
27151 if (i == collapse - 1)
27152 break;
27154 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27155 in between the collapsed for loops to be still considered perfectly
27156 nested. Hopefully the final version clarifies this.
27157 For now handle (multiple) {'s and empty statements. */
27158 cp_parser_parse_tentatively (parser);
27161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27162 break;
27163 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27165 cp_lexer_consume_token (parser->lexer);
27166 bracecount++;
27168 else if (bracecount
27169 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27170 cp_lexer_consume_token (parser->lexer);
27171 else
27173 loc = cp_lexer_peek_token (parser->lexer)->location;
27174 error_at (loc, "not enough collapsed for loops");
27175 collapse_err = true;
27176 cp_parser_abort_tentative_parse (parser);
27177 declv = NULL_TREE;
27178 break;
27181 while (1);
27183 if (declv)
27185 cp_parser_parse_definitely (parser);
27186 nbraces += bracecount;
27190 /* Note that we saved the original contents of this flag when we entered
27191 the structured block, and so we don't need to re-save it here. */
27192 parser->in_statement = IN_OMP_FOR;
27194 /* Note that the grammar doesn't call for a structured block here,
27195 though the loop as a whole is a structured block. */
27196 body = push_stmt_list ();
27197 cp_parser_statement (parser, NULL_TREE, false, NULL);
27198 body = pop_stmt_list (body);
27200 if (declv == NULL_TREE)
27201 ret = NULL_TREE;
27202 else
27203 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27204 pre_body, clauses);
27206 while (nbraces)
27208 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27210 cp_lexer_consume_token (parser->lexer);
27211 nbraces--;
27213 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27214 cp_lexer_consume_token (parser->lexer);
27215 else
27217 if (!collapse_err)
27219 error_at (cp_lexer_peek_token (parser->lexer)->location,
27220 "collapsed loops not perfectly nested");
27222 collapse_err = true;
27223 cp_parser_statement_seq_opt (parser, NULL);
27224 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27225 break;
27229 while (!VEC_empty (tree, for_block))
27230 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
27231 release_tree_vector (for_block);
27233 return ret;
27236 /* OpenMP 2.5:
27237 #pragma omp for for-clause[optseq] new-line
27238 for-loop */
27240 #define OMP_FOR_CLAUSE_MASK \
27241 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27242 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27243 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27244 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27245 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27246 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27247 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27248 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27250 static tree
27251 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27253 tree clauses, sb, ret;
27254 unsigned int save;
27256 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27257 "#pragma omp for", pragma_tok);
27259 sb = begin_omp_structured_block ();
27260 save = cp_parser_begin_omp_structured_block (parser);
27262 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27264 cp_parser_end_omp_structured_block (parser, save);
27265 add_stmt (finish_omp_structured_block (sb));
27267 return ret;
27270 /* OpenMP 2.5:
27271 # pragma omp master new-line
27272 structured-block */
27274 static tree
27275 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27277 cp_parser_require_pragma_eol (parser, pragma_tok);
27278 return c_finish_omp_master (input_location,
27279 cp_parser_omp_structured_block (parser));
27282 /* OpenMP 2.5:
27283 # pragma omp ordered new-line
27284 structured-block */
27286 static tree
27287 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27290 cp_parser_require_pragma_eol (parser, pragma_tok);
27291 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27294 /* OpenMP 2.5:
27296 section-scope:
27297 { section-sequence }
27299 section-sequence:
27300 section-directive[opt] structured-block
27301 section-sequence section-directive structured-block */
27303 static tree
27304 cp_parser_omp_sections_scope (cp_parser *parser)
27306 tree stmt, substmt;
27307 bool error_suppress = false;
27308 cp_token *tok;
27310 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27311 return NULL_TREE;
27313 stmt = push_stmt_list ();
27315 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27317 unsigned save;
27319 substmt = begin_omp_structured_block ();
27320 save = cp_parser_begin_omp_structured_block (parser);
27322 while (1)
27324 cp_parser_statement (parser, NULL_TREE, false, NULL);
27326 tok = cp_lexer_peek_token (parser->lexer);
27327 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27328 break;
27329 if (tok->type == CPP_CLOSE_BRACE)
27330 break;
27331 if (tok->type == CPP_EOF)
27332 break;
27335 cp_parser_end_omp_structured_block (parser, save);
27336 substmt = finish_omp_structured_block (substmt);
27337 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27338 add_stmt (substmt);
27341 while (1)
27343 tok = cp_lexer_peek_token (parser->lexer);
27344 if (tok->type == CPP_CLOSE_BRACE)
27345 break;
27346 if (tok->type == CPP_EOF)
27347 break;
27349 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27351 cp_lexer_consume_token (parser->lexer);
27352 cp_parser_require_pragma_eol (parser, tok);
27353 error_suppress = false;
27355 else if (!error_suppress)
27357 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27358 error_suppress = true;
27361 substmt = cp_parser_omp_structured_block (parser);
27362 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27363 add_stmt (substmt);
27365 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27367 substmt = pop_stmt_list (stmt);
27369 stmt = make_node (OMP_SECTIONS);
27370 TREE_TYPE (stmt) = void_type_node;
27371 OMP_SECTIONS_BODY (stmt) = substmt;
27373 add_stmt (stmt);
27374 return stmt;
27377 /* OpenMP 2.5:
27378 # pragma omp sections sections-clause[optseq] newline
27379 sections-scope */
27381 #define OMP_SECTIONS_CLAUSE_MASK \
27382 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27383 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27384 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27385 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27386 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27388 static tree
27389 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27391 tree clauses, ret;
27393 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27394 "#pragma omp sections", pragma_tok);
27396 ret = cp_parser_omp_sections_scope (parser);
27397 if (ret)
27398 OMP_SECTIONS_CLAUSES (ret) = clauses;
27400 return ret;
27403 /* OpenMP 2.5:
27404 # pragma parallel parallel-clause new-line
27405 # pragma parallel for parallel-for-clause new-line
27406 # pragma parallel sections parallel-sections-clause new-line */
27408 #define OMP_PARALLEL_CLAUSE_MASK \
27409 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27410 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27411 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27412 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27413 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27414 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27415 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27416 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27418 static tree
27419 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27421 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27422 const char *p_name = "#pragma omp parallel";
27423 tree stmt, clauses, par_clause, ws_clause, block;
27424 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27425 unsigned int save;
27426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27430 cp_lexer_consume_token (parser->lexer);
27431 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27432 p_name = "#pragma omp parallel for";
27433 mask |= OMP_FOR_CLAUSE_MASK;
27434 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27436 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27438 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27439 const char *p = IDENTIFIER_POINTER (id);
27440 if (strcmp (p, "sections") == 0)
27442 cp_lexer_consume_token (parser->lexer);
27443 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27444 p_name = "#pragma omp parallel sections";
27445 mask |= OMP_SECTIONS_CLAUSE_MASK;
27446 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27450 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27451 block = begin_omp_parallel ();
27452 save = cp_parser_begin_omp_structured_block (parser);
27454 switch (p_kind)
27456 case PRAGMA_OMP_PARALLEL:
27457 cp_parser_statement (parser, NULL_TREE, false, NULL);
27458 par_clause = clauses;
27459 break;
27461 case PRAGMA_OMP_PARALLEL_FOR:
27462 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27463 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27464 break;
27466 case PRAGMA_OMP_PARALLEL_SECTIONS:
27467 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27468 stmt = cp_parser_omp_sections_scope (parser);
27469 if (stmt)
27470 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27471 break;
27473 default:
27474 gcc_unreachable ();
27477 cp_parser_end_omp_structured_block (parser, save);
27478 stmt = finish_omp_parallel (par_clause, block);
27479 if (p_kind != PRAGMA_OMP_PARALLEL)
27480 OMP_PARALLEL_COMBINED (stmt) = 1;
27481 return stmt;
27484 /* OpenMP 2.5:
27485 # pragma omp single single-clause[optseq] new-line
27486 structured-block */
27488 #define OMP_SINGLE_CLAUSE_MASK \
27489 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27490 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27491 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27492 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27494 static tree
27495 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27497 tree stmt = make_node (OMP_SINGLE);
27498 TREE_TYPE (stmt) = void_type_node;
27500 OMP_SINGLE_CLAUSES (stmt)
27501 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27502 "#pragma omp single", pragma_tok);
27503 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27505 return add_stmt (stmt);
27508 /* OpenMP 3.0:
27509 # pragma omp task task-clause[optseq] new-line
27510 structured-block */
27512 #define OMP_TASK_CLAUSE_MASK \
27513 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27514 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27515 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27516 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27517 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27518 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27519 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27520 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27522 static tree
27523 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27525 tree clauses, block;
27526 unsigned int save;
27528 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27529 "#pragma omp task", pragma_tok);
27530 block = begin_omp_task ();
27531 save = cp_parser_begin_omp_structured_block (parser);
27532 cp_parser_statement (parser, NULL_TREE, false, NULL);
27533 cp_parser_end_omp_structured_block (parser, save);
27534 return finish_omp_task (clauses, block);
27537 /* OpenMP 3.0:
27538 # pragma omp taskwait new-line */
27540 static void
27541 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27543 cp_parser_require_pragma_eol (parser, pragma_tok);
27544 finish_omp_taskwait ();
27547 /* OpenMP 3.1:
27548 # pragma omp taskyield new-line */
27550 static void
27551 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27553 cp_parser_require_pragma_eol (parser, pragma_tok);
27554 finish_omp_taskyield ();
27557 /* OpenMP 2.5:
27558 # pragma omp threadprivate (variable-list) */
27560 static void
27561 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27563 tree vars;
27565 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27566 cp_parser_require_pragma_eol (parser, pragma_tok);
27568 finish_omp_threadprivate (vars);
27571 /* Main entry point to OpenMP statement pragmas. */
27573 static void
27574 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27576 tree stmt;
27578 switch (pragma_tok->pragma_kind)
27580 case PRAGMA_OMP_ATOMIC:
27581 cp_parser_omp_atomic (parser, pragma_tok);
27582 return;
27583 case PRAGMA_OMP_CRITICAL:
27584 stmt = cp_parser_omp_critical (parser, pragma_tok);
27585 break;
27586 case PRAGMA_OMP_FOR:
27587 stmt = cp_parser_omp_for (parser, pragma_tok);
27588 break;
27589 case PRAGMA_OMP_MASTER:
27590 stmt = cp_parser_omp_master (parser, pragma_tok);
27591 break;
27592 case PRAGMA_OMP_ORDERED:
27593 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27594 break;
27595 case PRAGMA_OMP_PARALLEL:
27596 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27597 break;
27598 case PRAGMA_OMP_SECTIONS:
27599 stmt = cp_parser_omp_sections (parser, pragma_tok);
27600 break;
27601 case PRAGMA_OMP_SINGLE:
27602 stmt = cp_parser_omp_single (parser, pragma_tok);
27603 break;
27604 case PRAGMA_OMP_TASK:
27605 stmt = cp_parser_omp_task (parser, pragma_tok);
27606 break;
27607 default:
27608 gcc_unreachable ();
27611 if (stmt)
27612 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27615 /* Transactional Memory parsing routines. */
27617 /* Parse a transaction attribute.
27619 txn-attribute:
27620 attribute
27621 [ [ identifier ] ]
27623 ??? Simplify this when C++0x bracket attributes are
27624 implemented properly. */
27626 static tree
27627 cp_parser_txn_attribute_opt (cp_parser *parser)
27629 cp_token *token;
27630 tree attr_name, attr = NULL;
27632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27633 return cp_parser_attributes_opt (parser);
27635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27636 return NULL_TREE;
27637 cp_lexer_consume_token (parser->lexer);
27638 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27639 goto error1;
27641 token = cp_lexer_peek_token (parser->lexer);
27642 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27644 token = cp_lexer_consume_token (parser->lexer);
27646 attr_name = (token->type == CPP_KEYWORD
27647 /* For keywords, use the canonical spelling,
27648 not the parsed identifier. */
27649 ? ridpointers[(int) token->keyword]
27650 : token->u.value);
27651 attr = build_tree_list (attr_name, NULL_TREE);
27653 else
27654 cp_parser_error (parser, "expected identifier");
27656 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27657 error1:
27658 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27659 return attr;
27662 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27664 transaction-statement:
27665 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27666 compound-statement
27667 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27670 static tree
27671 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27673 unsigned char old_in = parser->in_transaction;
27674 unsigned char this_in = 1, new_in;
27675 cp_token *token;
27676 tree stmt, attrs, noex;
27678 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27679 || keyword == RID_TRANSACTION_RELAXED);
27680 token = cp_parser_require_keyword (parser, keyword,
27681 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27682 : RT_TRANSACTION_RELAXED));
27683 gcc_assert (token != NULL);
27685 if (keyword == RID_TRANSACTION_RELAXED)
27686 this_in |= TM_STMT_ATTR_RELAXED;
27687 else
27689 attrs = cp_parser_txn_attribute_opt (parser);
27690 if (attrs)
27691 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27694 /* Parse a noexcept specification. */
27695 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27697 /* Keep track if we're in the lexical scope of an outer transaction. */
27698 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27700 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27702 parser->in_transaction = new_in;
27703 cp_parser_compound_statement (parser, NULL, false, false);
27704 parser->in_transaction = old_in;
27706 finish_transaction_stmt (stmt, NULL, this_in, noex);
27708 return stmt;
27711 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27713 transaction-expression:
27714 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27715 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27718 static tree
27719 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27721 unsigned char old_in = parser->in_transaction;
27722 unsigned char this_in = 1;
27723 cp_token *token;
27724 tree expr, noex;
27725 bool noex_expr;
27727 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27728 || keyword == RID_TRANSACTION_RELAXED);
27730 if (!flag_tm)
27731 error (keyword == RID_TRANSACTION_RELAXED
27732 ? G_("%<__transaction_relaxed%> without transactional memory "
27733 "support enabled")
27734 : G_("%<__transaction_atomic%> without transactional memory "
27735 "support enabled"));
27737 token = cp_parser_require_keyword (parser, keyword,
27738 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27739 : RT_TRANSACTION_RELAXED));
27740 gcc_assert (token != NULL);
27742 if (keyword == RID_TRANSACTION_RELAXED)
27743 this_in |= TM_STMT_ATTR_RELAXED;
27745 /* Set this early. This might mean that we allow transaction_cancel in
27746 an expression that we find out later actually has to be a constexpr.
27747 However, we expect that cxx_constant_value will be able to deal with
27748 this; also, if the noexcept has no constexpr, then what we parse next
27749 really is a transaction's body. */
27750 parser->in_transaction = this_in;
27752 /* Parse a noexcept specification. */
27753 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27754 true);
27756 if (!noex || !noex_expr
27757 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27759 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27761 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27762 finish_parenthesized_expr (expr);
27764 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27766 else
27768 /* The only expression that is available got parsed for the noexcept
27769 already. noexcept is true then. */
27770 expr = noex;
27771 noex = boolean_true_node;
27774 expr = build_transaction_expr (token->location, expr, this_in, noex);
27775 parser->in_transaction = old_in;
27777 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27778 return error_mark_node;
27780 return (flag_tm ? expr : error_mark_node);
27783 /* Parse a function-transaction-block.
27785 function-transaction-block:
27786 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27787 function-body
27788 __transaction_atomic txn-attribute[opt] function-try-block
27789 __transaction_relaxed ctor-initializer[opt] function-body
27790 __transaction_relaxed function-try-block
27793 static bool
27794 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27796 unsigned char old_in = parser->in_transaction;
27797 unsigned char new_in = 1;
27798 tree compound_stmt, stmt, attrs;
27799 bool ctor_initializer_p;
27800 cp_token *token;
27802 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27803 || keyword == RID_TRANSACTION_RELAXED);
27804 token = cp_parser_require_keyword (parser, keyword,
27805 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27806 : RT_TRANSACTION_RELAXED));
27807 gcc_assert (token != NULL);
27809 if (keyword == RID_TRANSACTION_RELAXED)
27810 new_in |= TM_STMT_ATTR_RELAXED;
27811 else
27813 attrs = cp_parser_txn_attribute_opt (parser);
27814 if (attrs)
27815 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27818 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27820 parser->in_transaction = new_in;
27822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27823 ctor_initializer_p = cp_parser_function_try_block (parser);
27824 else
27825 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27826 (parser, /*in_function_try_block=*/false);
27828 parser->in_transaction = old_in;
27830 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27832 return ctor_initializer_p;
27835 /* Parse a __transaction_cancel statement.
27837 cancel-statement:
27838 __transaction_cancel txn-attribute[opt] ;
27839 __transaction_cancel txn-attribute[opt] throw-expression ;
27841 ??? Cancel and throw is not yet implemented. */
27843 static tree
27844 cp_parser_transaction_cancel (cp_parser *parser)
27846 cp_token *token;
27847 bool is_outer = false;
27848 tree stmt, attrs;
27850 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27851 RT_TRANSACTION_CANCEL);
27852 gcc_assert (token != NULL);
27854 attrs = cp_parser_txn_attribute_opt (parser);
27855 if (attrs)
27856 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27858 /* ??? Parse cancel-and-throw here. */
27860 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27862 if (!flag_tm)
27864 error_at (token->location, "%<__transaction_cancel%> without "
27865 "transactional memory support enabled");
27866 return error_mark_node;
27868 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27870 error_at (token->location, "%<__transaction_cancel%> within a "
27871 "%<__transaction_relaxed%>");
27872 return error_mark_node;
27874 else if (is_outer)
27876 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27877 && !is_tm_may_cancel_outer (current_function_decl))
27879 error_at (token->location, "outer %<__transaction_cancel%> not "
27880 "within outer %<__transaction_atomic%>");
27881 error_at (token->location,
27882 " or a %<transaction_may_cancel_outer%> function");
27883 return error_mark_node;
27886 else if (parser->in_transaction == 0)
27888 error_at (token->location, "%<__transaction_cancel%> not within "
27889 "%<__transaction_atomic%>");
27890 return error_mark_node;
27893 stmt = build_tm_abort_call (token->location, is_outer);
27894 add_stmt (stmt);
27895 finish_stmt ();
27897 return stmt;
27900 /* The parser. */
27902 static GTY (()) cp_parser *the_parser;
27905 /* Special handling for the first token or line in the file. The first
27906 thing in the file might be #pragma GCC pch_preprocess, which loads a
27907 PCH file, which is a GC collection point. So we need to handle this
27908 first pragma without benefit of an existing lexer structure.
27910 Always returns one token to the caller in *FIRST_TOKEN. This is
27911 either the true first token of the file, or the first token after
27912 the initial pragma. */
27914 static void
27915 cp_parser_initial_pragma (cp_token *first_token)
27917 tree name = NULL;
27919 cp_lexer_get_preprocessor_token (NULL, first_token);
27920 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27921 return;
27923 cp_lexer_get_preprocessor_token (NULL, first_token);
27924 if (first_token->type == CPP_STRING)
27926 name = first_token->u.value;
27928 cp_lexer_get_preprocessor_token (NULL, first_token);
27929 if (first_token->type != CPP_PRAGMA_EOL)
27930 error_at (first_token->location,
27931 "junk at end of %<#pragma GCC pch_preprocess%>");
27933 else
27934 error_at (first_token->location, "expected string literal");
27936 /* Skip to the end of the pragma. */
27937 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27938 cp_lexer_get_preprocessor_token (NULL, first_token);
27940 /* Now actually load the PCH file. */
27941 if (name)
27942 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27944 /* Read one more token to return to our caller. We have to do this
27945 after reading the PCH file in, since its pointers have to be
27946 live. */
27947 cp_lexer_get_preprocessor_token (NULL, first_token);
27950 /* Normal parsing of a pragma token. Here we can (and must) use the
27951 regular lexer. */
27953 static bool
27954 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27956 cp_token *pragma_tok;
27957 unsigned int id;
27959 pragma_tok = cp_lexer_consume_token (parser->lexer);
27960 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27961 parser->lexer->in_pragma = true;
27963 id = pragma_tok->pragma_kind;
27964 switch (id)
27966 case PRAGMA_GCC_PCH_PREPROCESS:
27967 error_at (pragma_tok->location,
27968 "%<#pragma GCC pch_preprocess%> must be first");
27969 break;
27971 case PRAGMA_OMP_BARRIER:
27972 switch (context)
27974 case pragma_compound:
27975 cp_parser_omp_barrier (parser, pragma_tok);
27976 return false;
27977 case pragma_stmt:
27978 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27979 "used in compound statements");
27980 break;
27981 default:
27982 goto bad_stmt;
27984 break;
27986 case PRAGMA_OMP_FLUSH:
27987 switch (context)
27989 case pragma_compound:
27990 cp_parser_omp_flush (parser, pragma_tok);
27991 return false;
27992 case pragma_stmt:
27993 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27994 "used in compound statements");
27995 break;
27996 default:
27997 goto bad_stmt;
27999 break;
28001 case PRAGMA_OMP_TASKWAIT:
28002 switch (context)
28004 case pragma_compound:
28005 cp_parser_omp_taskwait (parser, pragma_tok);
28006 return false;
28007 case pragma_stmt:
28008 error_at (pragma_tok->location,
28009 "%<#pragma omp taskwait%> may only be "
28010 "used in compound statements");
28011 break;
28012 default:
28013 goto bad_stmt;
28015 break;
28017 case PRAGMA_OMP_TASKYIELD:
28018 switch (context)
28020 case pragma_compound:
28021 cp_parser_omp_taskyield (parser, pragma_tok);
28022 return false;
28023 case pragma_stmt:
28024 error_at (pragma_tok->location,
28025 "%<#pragma omp taskyield%> may only be "
28026 "used in compound statements");
28027 break;
28028 default:
28029 goto bad_stmt;
28031 break;
28033 case PRAGMA_OMP_THREADPRIVATE:
28034 cp_parser_omp_threadprivate (parser, pragma_tok);
28035 return false;
28037 case PRAGMA_OMP_ATOMIC:
28038 case PRAGMA_OMP_CRITICAL:
28039 case PRAGMA_OMP_FOR:
28040 case PRAGMA_OMP_MASTER:
28041 case PRAGMA_OMP_ORDERED:
28042 case PRAGMA_OMP_PARALLEL:
28043 case PRAGMA_OMP_SECTIONS:
28044 case PRAGMA_OMP_SINGLE:
28045 case PRAGMA_OMP_TASK:
28046 if (context == pragma_external)
28047 goto bad_stmt;
28048 cp_parser_omp_construct (parser, pragma_tok);
28049 return true;
28051 case PRAGMA_OMP_SECTION:
28052 error_at (pragma_tok->location,
28053 "%<#pragma omp section%> may only be used in "
28054 "%<#pragma omp sections%> construct");
28055 break;
28057 default:
28058 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28059 c_invoke_pragma_handler (id);
28060 break;
28062 bad_stmt:
28063 cp_parser_error (parser, "expected declaration specifiers");
28064 break;
28067 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28068 return false;
28071 /* The interface the pragma parsers have to the lexer. */
28073 enum cpp_ttype
28074 pragma_lex (tree *value)
28076 cp_token *tok;
28077 enum cpp_ttype ret;
28079 tok = cp_lexer_peek_token (the_parser->lexer);
28081 ret = tok->type;
28082 *value = tok->u.value;
28084 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28085 ret = CPP_EOF;
28086 else if (ret == CPP_STRING)
28087 *value = cp_parser_string_literal (the_parser, false, false);
28088 else
28090 cp_lexer_consume_token (the_parser->lexer);
28091 if (ret == CPP_KEYWORD)
28092 ret = CPP_NAME;
28095 return ret;
28099 /* External interface. */
28101 /* Parse one entire translation unit. */
28103 void
28104 c_parse_file (void)
28106 static bool already_called = false;
28108 if (already_called)
28110 sorry ("inter-module optimizations not implemented for C++");
28111 return;
28113 already_called = true;
28115 the_parser = cp_parser_new ();
28116 push_deferring_access_checks (flag_access_control
28117 ? dk_no_deferred : dk_no_check);
28118 cp_parser_translation_unit (the_parser);
28119 the_parser = NULL;
28122 #include "gt-cp-parser.h"