* config/darwin.c (darwin_assemble_visibility): Treat
[official-gcc.git] / gcc / cp / parser.c
blob155b51a180d9f6168283aff464fd8d77733a4ee8
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 *);
1201 static cp_declarator *make_reference_declarator
1202 (cp_cv_quals, cp_declarator *, bool);
1203 static cp_parameter_declarator *make_parameter_declarator
1204 (cp_decl_specifier_seq *, cp_declarator *, tree);
1205 static cp_declarator *make_ptrmem_declarator
1206 (cp_cv_quals, tree, cp_declarator *);
1208 /* An erroneous declarator. */
1209 static cp_declarator *cp_error_declarator;
1211 /* The obstack on which declarators and related data structures are
1212 allocated. */
1213 static struct obstack declarator_obstack;
1215 /* Alloc BYTES from the declarator memory pool. */
1217 static inline void *
1218 alloc_declarator (size_t bytes)
1220 return obstack_alloc (&declarator_obstack, bytes);
1223 /* Allocate a declarator of the indicated KIND. Clear fields that are
1224 common to all declarators. */
1226 static cp_declarator *
1227 make_declarator (cp_declarator_kind kind)
1229 cp_declarator *declarator;
1231 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1232 declarator->kind = kind;
1233 declarator->attributes = NULL_TREE;
1234 declarator->declarator = NULL;
1235 declarator->parameter_pack_p = false;
1236 declarator->id_loc = UNKNOWN_LOCATION;
1238 return declarator;
1241 /* Make a declarator for a generalized identifier. If
1242 QUALIFYING_SCOPE is non-NULL, the identifier is
1243 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1244 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1245 is, if any. */
1247 static cp_declarator *
1248 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1249 special_function_kind sfk)
1251 cp_declarator *declarator;
1253 /* It is valid to write:
1255 class C { void f(); };
1256 typedef C D;
1257 void D::f();
1259 The standard is not clear about whether `typedef const C D' is
1260 legal; as of 2002-09-15 the committee is considering that
1261 question. EDG 3.0 allows that syntax. Therefore, we do as
1262 well. */
1263 if (qualifying_scope && TYPE_P (qualifying_scope))
1264 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1266 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1267 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1268 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1270 declarator = make_declarator (cdk_id);
1271 declarator->u.id.qualifying_scope = qualifying_scope;
1272 declarator->u.id.unqualified_name = unqualified_name;
1273 declarator->u.id.sfk = sfk;
1275 return declarator;
1278 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1279 of modifiers such as const or volatile to apply to the pointer
1280 type, represented as identifiers. */
1282 cp_declarator *
1283 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1285 cp_declarator *declarator;
1287 declarator = make_declarator (cdk_pointer);
1288 declarator->declarator = target;
1289 declarator->u.pointer.qualifiers = cv_qualifiers;
1290 declarator->u.pointer.class_type = NULL_TREE;
1291 if (target)
1293 declarator->id_loc = target->id_loc;
1294 declarator->parameter_pack_p = target->parameter_pack_p;
1295 target->parameter_pack_p = false;
1297 else
1298 declarator->parameter_pack_p = false;
1300 return declarator;
1303 /* Like make_pointer_declarator -- but for references. */
1305 cp_declarator *
1306 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1307 bool rvalue_ref)
1309 cp_declarator *declarator;
1311 declarator = make_declarator (cdk_reference);
1312 declarator->declarator = target;
1313 declarator->u.reference.qualifiers = cv_qualifiers;
1314 declarator->u.reference.rvalue_ref = rvalue_ref;
1315 if (target)
1317 declarator->id_loc = target->id_loc;
1318 declarator->parameter_pack_p = target->parameter_pack_p;
1319 target->parameter_pack_p = false;
1321 else
1322 declarator->parameter_pack_p = false;
1324 return declarator;
1327 /* Like make_pointer_declarator -- but for a pointer to a non-static
1328 member of CLASS_TYPE. */
1330 cp_declarator *
1331 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1332 cp_declarator *pointee)
1334 cp_declarator *declarator;
1336 declarator = make_declarator (cdk_ptrmem);
1337 declarator->declarator = pointee;
1338 declarator->u.pointer.qualifiers = cv_qualifiers;
1339 declarator->u.pointer.class_type = class_type;
1341 if (pointee)
1343 declarator->parameter_pack_p = pointee->parameter_pack_p;
1344 pointee->parameter_pack_p = false;
1346 else
1347 declarator->parameter_pack_p = false;
1349 return declarator;
1352 /* Make a declarator for the function given by TARGET, with the
1353 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1354 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1355 indicates what exceptions can be thrown. */
1357 cp_declarator *
1358 make_call_declarator (cp_declarator *target,
1359 tree parms,
1360 cp_cv_quals cv_qualifiers,
1361 cp_virt_specifiers virt_specifiers,
1362 tree exception_specification,
1363 tree late_return_type)
1365 cp_declarator *declarator;
1367 declarator = make_declarator (cdk_function);
1368 declarator->declarator = target;
1369 declarator->u.function.parameters = parms;
1370 declarator->u.function.qualifiers = cv_qualifiers;
1371 declarator->u.function.virt_specifiers = virt_specifiers;
1372 declarator->u.function.exception_specification = exception_specification;
1373 declarator->u.function.late_return_type = late_return_type;
1374 if (target)
1376 declarator->id_loc = target->id_loc;
1377 declarator->parameter_pack_p = target->parameter_pack_p;
1378 target->parameter_pack_p = false;
1380 else
1381 declarator->parameter_pack_p = false;
1383 return declarator;
1386 /* Make a declarator for an array of BOUNDS elements, each of which is
1387 defined by ELEMENT. */
1389 cp_declarator *
1390 make_array_declarator (cp_declarator *element, tree bounds)
1392 cp_declarator *declarator;
1394 declarator = make_declarator (cdk_array);
1395 declarator->declarator = element;
1396 declarator->u.array.bounds = bounds;
1397 if (element)
1399 declarator->id_loc = element->id_loc;
1400 declarator->parameter_pack_p = element->parameter_pack_p;
1401 element->parameter_pack_p = false;
1403 else
1404 declarator->parameter_pack_p = false;
1406 return declarator;
1409 /* Determine whether the declarator we've seen so far can be a
1410 parameter pack, when followed by an ellipsis. */
1411 static bool
1412 declarator_can_be_parameter_pack (cp_declarator *declarator)
1414 /* Search for a declarator name, or any other declarator that goes
1415 after the point where the ellipsis could appear in a parameter
1416 pack. If we find any of these, then this declarator can not be
1417 made into a parameter pack. */
1418 bool found = false;
1419 while (declarator && !found)
1421 switch ((int)declarator->kind)
1423 case cdk_id:
1424 case cdk_array:
1425 found = true;
1426 break;
1428 case cdk_error:
1429 return true;
1431 default:
1432 declarator = declarator->declarator;
1433 break;
1437 return !found;
1440 cp_parameter_declarator *no_parameters;
1442 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1443 DECLARATOR and DEFAULT_ARGUMENT. */
1445 cp_parameter_declarator *
1446 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1447 cp_declarator *declarator,
1448 tree default_argument)
1450 cp_parameter_declarator *parameter;
1452 parameter = ((cp_parameter_declarator *)
1453 alloc_declarator (sizeof (cp_parameter_declarator)));
1454 parameter->next = NULL;
1455 if (decl_specifiers)
1456 parameter->decl_specifiers = *decl_specifiers;
1457 else
1458 clear_decl_specs (&parameter->decl_specifiers);
1459 parameter->declarator = declarator;
1460 parameter->default_argument = default_argument;
1461 parameter->ellipsis_p = false;
1463 return parameter;
1466 /* Returns true iff DECLARATOR is a declaration for a function. */
1468 static bool
1469 function_declarator_p (const cp_declarator *declarator)
1471 while (declarator)
1473 if (declarator->kind == cdk_function
1474 && declarator->declarator->kind == cdk_id)
1475 return true;
1476 if (declarator->kind == cdk_id
1477 || declarator->kind == cdk_error)
1478 return false;
1479 declarator = declarator->declarator;
1481 return false;
1484 /* The parser. */
1486 /* Overview
1487 --------
1489 A cp_parser parses the token stream as specified by the C++
1490 grammar. Its job is purely parsing, not semantic analysis. For
1491 example, the parser breaks the token stream into declarators,
1492 expressions, statements, and other similar syntactic constructs.
1493 It does not check that the types of the expressions on either side
1494 of an assignment-statement are compatible, or that a function is
1495 not declared with a parameter of type `void'.
1497 The parser invokes routines elsewhere in the compiler to perform
1498 semantic analysis and to build up the abstract syntax tree for the
1499 code processed.
1501 The parser (and the template instantiation code, which is, in a
1502 way, a close relative of parsing) are the only parts of the
1503 compiler that should be calling push_scope and pop_scope, or
1504 related functions. The parser (and template instantiation code)
1505 keeps track of what scope is presently active; everything else
1506 should simply honor that. (The code that generates static
1507 initializers may also need to set the scope, in order to check
1508 access control correctly when emitting the initializers.)
1510 Methodology
1511 -----------
1513 The parser is of the standard recursive-descent variety. Upcoming
1514 tokens in the token stream are examined in order to determine which
1515 production to use when parsing a non-terminal. Some C++ constructs
1516 require arbitrary look ahead to disambiguate. For example, it is
1517 impossible, in the general case, to tell whether a statement is an
1518 expression or declaration without scanning the entire statement.
1519 Therefore, the parser is capable of "parsing tentatively." When the
1520 parser is not sure what construct comes next, it enters this mode.
1521 Then, while we attempt to parse the construct, the parser queues up
1522 error messages, rather than issuing them immediately, and saves the
1523 tokens it consumes. If the construct is parsed successfully, the
1524 parser "commits", i.e., it issues any queued error messages and
1525 the tokens that were being preserved are permanently discarded.
1526 If, however, the construct is not parsed successfully, the parser
1527 rolls back its state completely so that it can resume parsing using
1528 a different alternative.
1530 Future Improvements
1531 -------------------
1533 The performance of the parser could probably be improved substantially.
1534 We could often eliminate the need to parse tentatively by looking ahead
1535 a little bit. In some places, this approach might not entirely eliminate
1536 the need to parse tentatively, but it might still speed up the average
1537 case. */
1539 /* Flags that are passed to some parsing functions. These values can
1540 be bitwise-ored together. */
1542 enum
1544 /* No flags. */
1545 CP_PARSER_FLAGS_NONE = 0x0,
1546 /* The construct is optional. If it is not present, then no error
1547 should be issued. */
1548 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1549 /* When parsing a type-specifier, treat user-defined type-names
1550 as non-type identifiers. */
1551 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1552 /* When parsing a type-specifier, do not try to parse a class-specifier
1553 or enum-specifier. */
1554 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1555 /* When parsing a decl-specifier-seq, only allow type-specifier or
1556 constexpr. */
1557 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 /* This type is used for parameters and variables which hold
1561 combinations of the above flags. */
1562 typedef int cp_parser_flags;
1564 /* The different kinds of declarators we want to parse. */
1566 typedef enum cp_parser_declarator_kind
1568 /* We want an abstract declarator. */
1569 CP_PARSER_DECLARATOR_ABSTRACT,
1570 /* We want a named declarator. */
1571 CP_PARSER_DECLARATOR_NAMED,
1572 /* We don't mind, but the name must be an unqualified-id. */
1573 CP_PARSER_DECLARATOR_EITHER
1574 } cp_parser_declarator_kind;
1576 /* The precedence values used to parse binary expressions. The minimum value
1577 of PREC must be 1, because zero is reserved to quickly discriminate
1578 binary operators from other tokens. */
1580 enum cp_parser_prec
1582 PREC_NOT_OPERATOR,
1583 PREC_LOGICAL_OR_EXPRESSION,
1584 PREC_LOGICAL_AND_EXPRESSION,
1585 PREC_INCLUSIVE_OR_EXPRESSION,
1586 PREC_EXCLUSIVE_OR_EXPRESSION,
1587 PREC_AND_EXPRESSION,
1588 PREC_EQUALITY_EXPRESSION,
1589 PREC_RELATIONAL_EXPRESSION,
1590 PREC_SHIFT_EXPRESSION,
1591 PREC_ADDITIVE_EXPRESSION,
1592 PREC_MULTIPLICATIVE_EXPRESSION,
1593 PREC_PM_EXPRESSION,
1594 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 /* A mapping from a token type to a corresponding tree node type, with a
1598 precedence value. */
1600 typedef struct cp_parser_binary_operations_map_node
1602 /* The token type. */
1603 enum cpp_ttype token_type;
1604 /* The corresponding tree code. */
1605 enum tree_code tree_type;
1606 /* The precedence of this operator. */
1607 enum cp_parser_prec prec;
1608 } cp_parser_binary_operations_map_node;
1610 typedef struct cp_parser_expression_stack_entry
1612 /* Left hand side of the binary operation we are currently
1613 parsing. */
1614 tree lhs;
1615 /* Original tree code for left hand side, if it was a binary
1616 expression itself (used for -Wparentheses). */
1617 enum tree_code lhs_type;
1618 /* Tree code for the binary operation we are parsing. */
1619 enum tree_code tree_type;
1620 /* Precedence of the binary operation we are parsing. */
1621 enum cp_parser_prec prec;
1622 /* Location of the binary operation we are parsing. */
1623 location_t loc;
1624 } cp_parser_expression_stack_entry;
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1628 increasing. */
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack[NUM_PREC_VALUES];
1632 /* Prototypes. */
1634 /* Constructors and destructors. */
1636 static cp_parser_context *cp_parser_context_new
1637 (cp_parser_context *);
1639 /* Class variables. */
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1645 cp_parser_new. */
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1682 for speed. */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1685 /* Constructors and destructors. */
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1693 cp_parser_context *context;
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list != NULL)
1698 /* Pull the first entry from the free list. */
1699 context = cp_parser_context_free_list;
1700 cp_parser_context_free_list = context->next;
1701 memset (context, 0, sizeof (*context));
1703 else
1704 context = ggc_alloc_cleared_cp_parser_context ();
1706 /* No errors have occurred yet in this context. */
1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1710 if (next)
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context->object_type = next->object_type;
1715 /* Thread the stack. */
1716 context->next = next;
1719 return context;
1722 /* Managing the unparsed function queues. */
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1734 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1735 VEC_safe_push (cp_unparsed_functions_entry, gc, parser->unparsed_queues, e);
1738 static void
1739 pop_unparsed_function_queues (cp_parser *parser)
1741 release_tree_vector (unparsed_funs_with_definitions);
1742 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1745 /* Prototypes. */
1747 /* Constructors and destructors. */
1749 static cp_parser *cp_parser_new
1750 (void);
1752 /* Routines to parse various constructs.
1754 Those that return `tree' will return the error_mark_node (rather
1755 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756 Sometimes, they will return an ordinary node if error-recovery was
1757 attempted, even though a parse error occurred. So, to check
1758 whether or not a parse error occurred, you should always use
1759 cp_parser_error_occurred. If the construct is optional (indicated
1760 either by an `_opt' in the name of the function that does the
1761 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762 the construct is not present. */
1764 /* Lexical conventions [gram.lex] */
1766 static tree cp_parser_identifier
1767 (cp_parser *);
1768 static tree cp_parser_string_literal
1769 (cp_parser *, bool, bool);
1770 static tree cp_parser_userdef_char_literal
1771 (cp_parser *);
1772 static tree cp_parser_userdef_string_literal
1773 (cp_token *);
1774 static tree cp_parser_userdef_numeric_literal
1775 (cp_parser *);
1777 /* Basic concepts [gram.basic] */
1779 static bool cp_parser_translation_unit
1780 (cp_parser *);
1782 /* Expressions [gram.expr] */
1784 static tree cp_parser_primary_expression
1785 (cp_parser *, bool, bool, bool, cp_id_kind *);
1786 static tree cp_parser_id_expression
1787 (cp_parser *, bool, bool, bool *, bool, bool);
1788 static tree cp_parser_unqualified_id
1789 (cp_parser *, bool, bool, bool, bool);
1790 static tree cp_parser_nested_name_specifier_opt
1791 (cp_parser *, bool, bool, bool, bool);
1792 static tree cp_parser_nested_name_specifier
1793 (cp_parser *, bool, bool, bool, bool);
1794 static tree cp_parser_qualifying_entity
1795 (cp_parser *, bool, bool, bool, bool, bool);
1796 static tree cp_parser_postfix_expression
1797 (cp_parser *, bool, bool, bool, cp_id_kind *);
1798 static tree cp_parser_postfix_open_square_expression
1799 (cp_parser *, tree, bool);
1800 static tree cp_parser_postfix_dot_deref_expression
1801 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1802 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1803 (cp_parser *, int, bool, bool, bool *);
1804 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1805 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1806 static void cp_parser_pseudo_destructor_name
1807 (cp_parser *, tree *, tree *);
1808 static tree cp_parser_unary_expression
1809 (cp_parser *, bool, bool, cp_id_kind *);
1810 static enum tree_code cp_parser_unary_operator
1811 (cp_token *);
1812 static tree cp_parser_new_expression
1813 (cp_parser *);
1814 static VEC(tree,gc) *cp_parser_new_placement
1815 (cp_parser *);
1816 static tree cp_parser_new_type_id
1817 (cp_parser *, tree *);
1818 static cp_declarator *cp_parser_new_declarator_opt
1819 (cp_parser *);
1820 static cp_declarator *cp_parser_direct_new_declarator
1821 (cp_parser *);
1822 static VEC(tree,gc) *cp_parser_new_initializer
1823 (cp_parser *);
1824 static tree cp_parser_delete_expression
1825 (cp_parser *);
1826 static tree cp_parser_cast_expression
1827 (cp_parser *, bool, bool, cp_id_kind *);
1828 static tree cp_parser_binary_expression
1829 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1830 static tree cp_parser_question_colon_clause
1831 (cp_parser *, tree);
1832 static tree cp_parser_assignment_expression
1833 (cp_parser *, bool, cp_id_kind *);
1834 static enum tree_code cp_parser_assignment_operator_opt
1835 (cp_parser *);
1836 static tree cp_parser_expression
1837 (cp_parser *, bool, cp_id_kind *);
1838 static tree cp_parser_constant_expression
1839 (cp_parser *, bool, bool *);
1840 static tree cp_parser_builtin_offsetof
1841 (cp_parser *);
1842 static tree cp_parser_lambda_expression
1843 (cp_parser *);
1844 static void cp_parser_lambda_introducer
1845 (cp_parser *, tree);
1846 static bool cp_parser_lambda_declarator_opt
1847 (cp_parser *, tree);
1848 static void cp_parser_lambda_body
1849 (cp_parser *, tree);
1851 /* Statements [gram.stmt.stmt] */
1853 static void cp_parser_statement
1854 (cp_parser *, tree, bool, bool *);
1855 static void cp_parser_label_for_labeled_statement
1856 (cp_parser *);
1857 static tree cp_parser_expression_statement
1858 (cp_parser *, tree);
1859 static tree cp_parser_compound_statement
1860 (cp_parser *, tree, bool, bool);
1861 static void cp_parser_statement_seq_opt
1862 (cp_parser *, tree);
1863 static tree cp_parser_selection_statement
1864 (cp_parser *, bool *);
1865 static tree cp_parser_condition
1866 (cp_parser *);
1867 static tree cp_parser_iteration_statement
1868 (cp_parser *);
1869 static bool cp_parser_for_init_statement
1870 (cp_parser *, tree *decl);
1871 static tree cp_parser_for
1872 (cp_parser *);
1873 static tree cp_parser_c_for
1874 (cp_parser *, tree, tree);
1875 static tree cp_parser_range_for
1876 (cp_parser *, tree, tree, tree);
1877 static void do_range_for_auto_deduction
1878 (tree, tree);
1879 static tree cp_parser_perform_range_for_lookup
1880 (tree, tree *, tree *);
1881 static tree cp_parser_range_for_member_function
1882 (tree, tree);
1883 static tree cp_parser_jump_statement
1884 (cp_parser *);
1885 static void cp_parser_declaration_statement
1886 (cp_parser *);
1888 static tree cp_parser_implicitly_scoped_statement
1889 (cp_parser *, bool *);
1890 static void cp_parser_already_scoped_statement
1891 (cp_parser *);
1893 /* Declarations [gram.dcl.dcl] */
1895 static void cp_parser_declaration_seq_opt
1896 (cp_parser *);
1897 static void cp_parser_declaration
1898 (cp_parser *);
1899 static void cp_parser_block_declaration
1900 (cp_parser *, bool);
1901 static void cp_parser_simple_declaration
1902 (cp_parser *, bool, tree *);
1903 static void cp_parser_decl_specifier_seq
1904 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1905 static tree cp_parser_storage_class_specifier_opt
1906 (cp_parser *);
1907 static tree cp_parser_function_specifier_opt
1908 (cp_parser *, cp_decl_specifier_seq *);
1909 static tree cp_parser_type_specifier
1910 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1911 int *, bool *);
1912 static tree cp_parser_simple_type_specifier
1913 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1914 static tree cp_parser_type_name
1915 (cp_parser *);
1916 static tree cp_parser_nonclass_name
1917 (cp_parser* parser);
1918 static tree cp_parser_elaborated_type_specifier
1919 (cp_parser *, bool, bool);
1920 static tree cp_parser_enum_specifier
1921 (cp_parser *);
1922 static void cp_parser_enumerator_list
1923 (cp_parser *, tree);
1924 static void cp_parser_enumerator_definition
1925 (cp_parser *, tree);
1926 static tree cp_parser_namespace_name
1927 (cp_parser *);
1928 static void cp_parser_namespace_definition
1929 (cp_parser *);
1930 static void cp_parser_namespace_body
1931 (cp_parser *);
1932 static tree cp_parser_qualified_namespace_specifier
1933 (cp_parser *);
1934 static void cp_parser_namespace_alias_definition
1935 (cp_parser *);
1936 static bool cp_parser_using_declaration
1937 (cp_parser *, bool);
1938 static void cp_parser_using_directive
1939 (cp_parser *);
1940 static tree cp_parser_alias_declaration
1941 (cp_parser *);
1942 static void cp_parser_asm_definition
1943 (cp_parser *);
1944 static void cp_parser_linkage_specification
1945 (cp_parser *);
1946 static void cp_parser_static_assert
1947 (cp_parser *, bool);
1948 static tree cp_parser_decltype
1949 (cp_parser *);
1951 /* Declarators [gram.dcl.decl] */
1953 static tree cp_parser_init_declarator
1954 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1955 static cp_declarator *cp_parser_declarator
1956 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1957 static cp_declarator *cp_parser_direct_declarator
1958 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1959 static enum tree_code cp_parser_ptr_operator
1960 (cp_parser *, tree *, cp_cv_quals *);
1961 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1962 (cp_parser *);
1963 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1964 (cp_parser *);
1965 static tree cp_parser_late_return_type_opt
1966 (cp_parser *, cp_cv_quals);
1967 static tree cp_parser_declarator_id
1968 (cp_parser *, bool);
1969 static tree cp_parser_type_id
1970 (cp_parser *);
1971 static tree cp_parser_template_type_arg
1972 (cp_parser *);
1973 static tree cp_parser_trailing_type_id (cp_parser *);
1974 static tree cp_parser_type_id_1
1975 (cp_parser *, bool, bool);
1976 static void cp_parser_type_specifier_seq
1977 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1978 static tree cp_parser_parameter_declaration_clause
1979 (cp_parser *);
1980 static tree cp_parser_parameter_declaration_list
1981 (cp_parser *, bool *);
1982 static cp_parameter_declarator *cp_parser_parameter_declaration
1983 (cp_parser *, bool, bool *);
1984 static tree cp_parser_default_argument
1985 (cp_parser *, bool);
1986 static void cp_parser_function_body
1987 (cp_parser *, bool);
1988 static tree cp_parser_initializer
1989 (cp_parser *, bool *, bool *);
1990 static tree cp_parser_initializer_clause
1991 (cp_parser *, bool *);
1992 static tree cp_parser_braced_list
1993 (cp_parser*, bool*);
1994 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1995 (cp_parser *, bool *);
1997 static bool cp_parser_ctor_initializer_opt_and_function_body
1998 (cp_parser *, bool);
2000 /* Classes [gram.class] */
2002 static tree cp_parser_class_name
2003 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2004 static tree cp_parser_class_specifier
2005 (cp_parser *);
2006 static tree cp_parser_class_head
2007 (cp_parser *, bool *);
2008 static enum tag_types cp_parser_class_key
2009 (cp_parser *);
2010 static void cp_parser_member_specification_opt
2011 (cp_parser *);
2012 static void cp_parser_member_declaration
2013 (cp_parser *);
2014 static tree cp_parser_pure_specifier
2015 (cp_parser *);
2016 static tree cp_parser_constant_initializer
2017 (cp_parser *);
2019 /* Derived classes [gram.class.derived] */
2021 static tree cp_parser_base_clause
2022 (cp_parser *);
2023 static tree cp_parser_base_specifier
2024 (cp_parser *);
2026 /* Special member functions [gram.special] */
2028 static tree cp_parser_conversion_function_id
2029 (cp_parser *);
2030 static tree cp_parser_conversion_type_id
2031 (cp_parser *);
2032 static cp_declarator *cp_parser_conversion_declarator_opt
2033 (cp_parser *);
2034 static bool cp_parser_ctor_initializer_opt
2035 (cp_parser *);
2036 static void cp_parser_mem_initializer_list
2037 (cp_parser *);
2038 static tree cp_parser_mem_initializer
2039 (cp_parser *);
2040 static tree cp_parser_mem_initializer_id
2041 (cp_parser *);
2043 /* Overloading [gram.over] */
2045 static tree cp_parser_operator_function_id
2046 (cp_parser *);
2047 static tree cp_parser_operator
2048 (cp_parser *);
2050 /* Templates [gram.temp] */
2052 static void cp_parser_template_declaration
2053 (cp_parser *, bool);
2054 static tree cp_parser_template_parameter_list
2055 (cp_parser *);
2056 static tree cp_parser_template_parameter
2057 (cp_parser *, bool *, bool *);
2058 static tree cp_parser_type_parameter
2059 (cp_parser *, bool *);
2060 static tree cp_parser_template_id
2061 (cp_parser *, bool, bool, enum tag_types, bool);
2062 static tree cp_parser_template_name
2063 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2064 static tree cp_parser_template_argument_list
2065 (cp_parser *);
2066 static tree cp_parser_template_argument
2067 (cp_parser *);
2068 static void cp_parser_explicit_instantiation
2069 (cp_parser *);
2070 static void cp_parser_explicit_specialization
2071 (cp_parser *);
2073 /* Exception handling [gram.exception] */
2075 static tree cp_parser_try_block
2076 (cp_parser *);
2077 static bool cp_parser_function_try_block
2078 (cp_parser *);
2079 static void cp_parser_handler_seq
2080 (cp_parser *);
2081 static void cp_parser_handler
2082 (cp_parser *);
2083 static tree cp_parser_exception_declaration
2084 (cp_parser *);
2085 static tree cp_parser_throw_expression
2086 (cp_parser *);
2087 static tree cp_parser_exception_specification_opt
2088 (cp_parser *);
2089 static tree cp_parser_type_id_list
2090 (cp_parser *);
2092 /* GNU Extensions */
2094 static tree cp_parser_asm_specification_opt
2095 (cp_parser *);
2096 static tree cp_parser_asm_operand_list
2097 (cp_parser *);
2098 static tree cp_parser_asm_clobber_list
2099 (cp_parser *);
2100 static tree cp_parser_asm_label_list
2101 (cp_parser *);
2102 static tree cp_parser_attributes_opt
2103 (cp_parser *);
2104 static tree cp_parser_attribute_list
2105 (cp_parser *);
2106 static bool cp_parser_extension_opt
2107 (cp_parser *, int *);
2108 static void cp_parser_label_declaration
2109 (cp_parser *);
2111 /* Transactional Memory Extensions */
2113 static tree cp_parser_transaction
2114 (cp_parser *, enum rid);
2115 static tree cp_parser_transaction_expression
2116 (cp_parser *, enum rid);
2117 static bool cp_parser_function_transaction
2118 (cp_parser *, enum rid);
2119 static tree cp_parser_transaction_cancel
2120 (cp_parser *);
2122 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2123 static bool cp_parser_pragma
2124 (cp_parser *, enum pragma_context);
2126 /* Objective-C++ Productions */
2128 static tree cp_parser_objc_message_receiver
2129 (cp_parser *);
2130 static tree cp_parser_objc_message_args
2131 (cp_parser *);
2132 static tree cp_parser_objc_message_expression
2133 (cp_parser *);
2134 static tree cp_parser_objc_encode_expression
2135 (cp_parser *);
2136 static tree cp_parser_objc_defs_expression
2137 (cp_parser *);
2138 static tree cp_parser_objc_protocol_expression
2139 (cp_parser *);
2140 static tree cp_parser_objc_selector_expression
2141 (cp_parser *);
2142 static tree cp_parser_objc_expression
2143 (cp_parser *);
2144 static bool cp_parser_objc_selector_p
2145 (enum cpp_ttype);
2146 static tree cp_parser_objc_selector
2147 (cp_parser *);
2148 static tree cp_parser_objc_protocol_refs_opt
2149 (cp_parser *);
2150 static void cp_parser_objc_declaration
2151 (cp_parser *, tree);
2152 static tree cp_parser_objc_statement
2153 (cp_parser *);
2154 static bool cp_parser_objc_valid_prefix_attributes
2155 (cp_parser *, tree *);
2156 static void cp_parser_objc_at_property_declaration
2157 (cp_parser *) ;
2158 static void cp_parser_objc_at_synthesize_declaration
2159 (cp_parser *) ;
2160 static void cp_parser_objc_at_dynamic_declaration
2161 (cp_parser *) ;
2162 static tree cp_parser_objc_struct_declaration
2163 (cp_parser *) ;
2165 /* Utility Routines */
2167 static tree cp_parser_lookup_name
2168 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2169 static tree cp_parser_lookup_name_simple
2170 (cp_parser *, tree, location_t);
2171 static tree cp_parser_maybe_treat_template_as_class
2172 (tree, bool);
2173 static bool cp_parser_check_declarator_template_parameters
2174 (cp_parser *, cp_declarator *, location_t);
2175 static bool cp_parser_check_template_parameters
2176 (cp_parser *, unsigned, location_t, cp_declarator *);
2177 static tree cp_parser_simple_cast_expression
2178 (cp_parser *);
2179 static tree cp_parser_global_scope_opt
2180 (cp_parser *, bool);
2181 static bool cp_parser_constructor_declarator_p
2182 (cp_parser *, bool);
2183 static tree cp_parser_function_definition_from_specifiers_and_declarator
2184 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2185 static tree cp_parser_function_definition_after_declarator
2186 (cp_parser *, bool);
2187 static void cp_parser_template_declaration_after_export
2188 (cp_parser *, bool);
2189 static void cp_parser_perform_template_parameter_access_checks
2190 (VEC (deferred_access_check,gc)*);
2191 static tree cp_parser_single_declaration
2192 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2193 static tree cp_parser_functional_cast
2194 (cp_parser *, tree);
2195 static tree cp_parser_save_member_function_body
2196 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2197 static tree cp_parser_save_nsdmi
2198 (cp_parser *);
2199 static tree cp_parser_enclosed_template_argument_list
2200 (cp_parser *);
2201 static void cp_parser_save_default_args
2202 (cp_parser *, tree);
2203 static void cp_parser_late_parsing_for_member
2204 (cp_parser *, tree);
2205 static tree cp_parser_late_parse_one_default_arg
2206 (cp_parser *, tree, tree, tree);
2207 static void cp_parser_late_parsing_nsdmi
2208 (cp_parser *, tree);
2209 static void cp_parser_late_parsing_default_args
2210 (cp_parser *, tree);
2211 static tree cp_parser_sizeof_operand
2212 (cp_parser *, enum rid);
2213 static tree cp_parser_trait_expr
2214 (cp_parser *, enum rid);
2215 static bool cp_parser_declares_only_class_p
2216 (cp_parser *);
2217 static void cp_parser_set_storage_class
2218 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2219 static void cp_parser_set_decl_spec_type
2220 (cp_decl_specifier_seq *, tree, location_t, bool);
2221 static void set_and_check_decl_spec_loc
2222 (cp_decl_specifier_seq *decl_specs,
2223 cp_decl_spec ds, source_location location);
2224 static bool cp_parser_friend_p
2225 (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227 (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229 (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231 (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233 (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235 (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237 (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239 (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241 (cp_token *);
2242 static void cp_parser_check_class_key
2243 (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245 (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247 (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249 (cp_parser *);
2250 static bool cp_parser_cache_group
2251 (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253 (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2255 (cp_parser *);
2256 static void cp_parser_commit_to_tentative_parse
2257 (cp_parser *);
2258 static void cp_parser_abort_tentative_parse
2259 (cp_parser *);
2260 static bool cp_parser_parse_definitely
2261 (cp_parser *);
2262 static inline bool cp_parser_parsing_tentatively
2263 (cp_parser *);
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2265 (cp_parser *);
2266 static void cp_parser_error
2267 (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269 (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2271 (cp_parser *);
2272 static bool cp_parser_check_type_definition
2273 (cp_parser *);
2274 static void cp_parser_check_for_definition_in_return_type
2275 (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277 (cp_parser *, tree, enum tag_types, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279 (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281 (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2283 (cp_parser *);
2284 static int cp_parser_skip_to_closing_parenthesis
2285 (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2287 (cp_parser *);
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2289 (cp_parser *);
2290 static void cp_parser_skip_to_end_of_block_or_statement
2291 (cp_parser *);
2292 static bool cp_parser_skip_to_closing_brace
2293 (cp_parser *);
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2295 (cp_parser *);
2296 static void cp_parser_skip_to_pragma_eol
2297 (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2299 (cp_parser *);
2300 static bool cp_parser_allow_gnu_extensions_p
2301 (cp_parser *);
2302 static bool cp_parser_is_pure_string_literal
2303 (cp_token *);
2304 static bool cp_parser_is_string_literal
2305 (cp_token *);
2306 static bool cp_parser_is_keyword
2307 (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309 (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2313 /* Returns nonzero if we are parsing tentatively. */
2315 static inline bool
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2318 return parser->context->next != NULL;
2321 /* Returns nonzero if TOKEN is a string literal. */
2323 static bool
2324 cp_parser_is_pure_string_literal (cp_token* token)
2326 return (token->type == CPP_STRING ||
2327 token->type == CPP_STRING16 ||
2328 token->type == CPP_STRING32 ||
2329 token->type == CPP_WSTRING ||
2330 token->type == CPP_UTF8STRING);
2333 /* Returns nonzero if TOKEN is a string literal
2334 of a user-defined string literal. */
2336 static bool
2337 cp_parser_is_string_literal (cp_token* token)
2339 return (cp_parser_is_pure_string_literal (token) ||
2340 token->type == CPP_STRING_USERDEF ||
2341 token->type == CPP_STRING16_USERDEF ||
2342 token->type == CPP_STRING32_USERDEF ||
2343 token->type == CPP_WSTRING_USERDEF ||
2344 token->type == CPP_UTF8STRING_USERDEF);
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2349 static bool
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2352 return token->keyword == keyword;
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356 FILE:LINE: MESSAGE before TOKEN
2357 where TOKEN is the next token in the input stream. MESSAGE
2358 (specified by the caller) is usually of the form "expected
2359 OTHER-TOKEN". */
2361 static void
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2364 if (!cp_parser_simulate_error (parser))
2366 cp_token *token = cp_lexer_peek_token (parser->lexer);
2367 /* This diagnostic makes more sense if it is tagged to the line
2368 of the token we just peeked at. */
2369 cp_lexer_set_source_position_from_token (token);
2371 if (token->type == CPP_PRAGMA)
2373 error_at (token->location,
2374 "%<#pragma%> is not allowed here");
2375 cp_parser_skip_to_pragma_eol (parser, token);
2376 return;
2379 c_parse_error (gmsgid,
2380 /* Because c_parser_error does not understand
2381 CPP_KEYWORD, keywords are treated like
2382 identifiers. */
2383 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384 token->u.value, token->flags);
2388 /* Issue an error about name-lookup failing. NAME is the
2389 IDENTIFIER_NODE DECL is the result of
2390 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2391 the thing that we hoped to find. */
2393 static void
2394 cp_parser_name_lookup_error (cp_parser* parser,
2395 tree name,
2396 tree decl,
2397 name_lookup_error desired,
2398 location_t location)
2400 /* If name lookup completely failed, tell the user that NAME was not
2401 declared. */
2402 if (decl == error_mark_node)
2404 if (parser->scope && parser->scope != global_namespace)
2405 error_at (location, "%<%E::%E%> has not been declared",
2406 parser->scope, name);
2407 else if (parser->scope == global_namespace)
2408 error_at (location, "%<::%E%> has not been declared", name);
2409 else if (parser->object_scope
2410 && !CLASS_TYPE_P (parser->object_scope))
2411 error_at (location, "request for member %qE in non-class type %qT",
2412 name, parser->object_scope);
2413 else if (parser->object_scope)
2414 error_at (location, "%<%T::%E%> has not been declared",
2415 parser->object_scope, name);
2416 else
2417 error_at (location, "%qE has not been declared", name);
2419 else if (parser->scope && parser->scope != global_namespace)
2421 switch (desired)
2423 case NLE_TYPE:
2424 error_at (location, "%<%E::%E%> is not a type",
2425 parser->scope, name);
2426 break;
2427 case NLE_CXX98:
2428 error_at (location, "%<%E::%E%> is not a class or namespace",
2429 parser->scope, name);
2430 break;
2431 case NLE_NOT_CXX98:
2432 error_at (location,
2433 "%<%E::%E%> is not a class, namespace, or enumeration",
2434 parser->scope, name);
2435 break;
2436 default:
2437 gcc_unreachable ();
2441 else if (parser->scope == global_namespace)
2443 switch (desired)
2445 case NLE_TYPE:
2446 error_at (location, "%<::%E%> is not a type", name);
2447 break;
2448 case NLE_CXX98:
2449 error_at (location, "%<::%E%> is not a class or namespace", name);
2450 break;
2451 case NLE_NOT_CXX98:
2452 error_at (location,
2453 "%<::%E%> is not a class, namespace, or enumeration",
2454 name);
2455 break;
2456 default:
2457 gcc_unreachable ();
2460 else
2462 switch (desired)
2464 case NLE_TYPE:
2465 error_at (location, "%qE is not a type", name);
2466 break;
2467 case NLE_CXX98:
2468 error_at (location, "%qE is not a class or namespace", name);
2469 break;
2470 case NLE_NOT_CXX98:
2471 error_at (location,
2472 "%qE is not a class, namespace, or enumeration", name);
2473 break;
2474 default:
2475 gcc_unreachable ();
2480 /* If we are parsing tentatively, remember that an error has occurred
2481 during this tentative parse. Returns true if the error was
2482 simulated; false if a message should be issued by the caller. */
2484 static bool
2485 cp_parser_simulate_error (cp_parser* parser)
2487 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2489 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2490 return true;
2492 return false;
2495 /* This function is called when a type is defined. If type
2496 definitions are forbidden at this point, an error message is
2497 issued. */
2499 static bool
2500 cp_parser_check_type_definition (cp_parser* parser)
2502 /* If types are forbidden here, issue a message. */
2503 if (parser->type_definition_forbidden_message)
2505 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2506 in the message need to be interpreted. */
2507 error (parser->type_definition_forbidden_message);
2508 return false;
2510 return true;
2513 /* This function is called when the DECLARATOR is processed. The TYPE
2514 was a type defined in the decl-specifiers. If it is invalid to
2515 define a type in the decl-specifiers for DECLARATOR, an error is
2516 issued. TYPE_LOCATION is the location of TYPE and is used
2517 for error reporting. */
2519 static void
2520 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2521 tree type, location_t type_location)
2523 /* [dcl.fct] forbids type definitions in return types.
2524 Unfortunately, it's not easy to know whether or not we are
2525 processing a return type until after the fact. */
2526 while (declarator
2527 && (declarator->kind == cdk_pointer
2528 || declarator->kind == cdk_reference
2529 || declarator->kind == cdk_ptrmem))
2530 declarator = declarator->declarator;
2531 if (declarator
2532 && declarator->kind == cdk_function)
2534 error_at (type_location,
2535 "new types may not be defined in a return type");
2536 inform (type_location,
2537 "(perhaps a semicolon is missing after the definition of %qT)",
2538 type);
2542 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2543 "<" in any valid C++ program. If the next token is indeed "<",
2544 issue a message warning the user about what appears to be an
2545 invalid attempt to form a template-id. LOCATION is the location
2546 of the type-specifier (TYPE) */
2548 static void
2549 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2550 tree type,
2551 enum tag_types tag_type,
2552 location_t location)
2554 cp_token_position start = 0;
2556 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2558 if (TYPE_P (type))
2559 error_at (location, "%qT is not a template", type);
2560 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2562 if (tag_type != none_type)
2563 error_at (location, "%qE is not a class template", type);
2564 else
2565 error_at (location, "%qE is not a template", type);
2567 else
2568 error_at (location, "invalid template-id");
2569 /* Remember the location of the invalid "<". */
2570 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2571 start = cp_lexer_token_position (parser->lexer, true);
2572 /* Consume the "<". */
2573 cp_lexer_consume_token (parser->lexer);
2574 /* Parse the template arguments. */
2575 cp_parser_enclosed_template_argument_list (parser);
2576 /* Permanently remove the invalid template arguments so that
2577 this error message is not issued again. */
2578 if (start)
2579 cp_lexer_purge_tokens_after (parser->lexer, start);
2583 /* If parsing an integral constant-expression, issue an error message
2584 about the fact that THING appeared and return true. Otherwise,
2585 return false. In either case, set
2586 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2588 static bool
2589 cp_parser_non_integral_constant_expression (cp_parser *parser,
2590 non_integral_constant thing)
2592 parser->non_integral_constant_expression_p = true;
2593 if (parser->integral_constant_expression_p)
2595 if (!parser->allow_non_integral_constant_expression_p)
2597 const char *msg = NULL;
2598 switch (thing)
2600 case NIC_FLOAT:
2601 error ("floating-point literal "
2602 "cannot appear in a constant-expression");
2603 return true;
2604 case NIC_CAST:
2605 error ("a cast to a type other than an integral or "
2606 "enumeration type cannot appear in a "
2607 "constant-expression");
2608 return true;
2609 case NIC_TYPEID:
2610 error ("%<typeid%> operator "
2611 "cannot appear in a constant-expression");
2612 return true;
2613 case NIC_NCC:
2614 error ("non-constant compound literals "
2615 "cannot appear in a constant-expression");
2616 return true;
2617 case NIC_FUNC_CALL:
2618 error ("a function call "
2619 "cannot appear in a constant-expression");
2620 return true;
2621 case NIC_INC:
2622 error ("an increment "
2623 "cannot appear in a constant-expression");
2624 return true;
2625 case NIC_DEC:
2626 error ("an decrement "
2627 "cannot appear in a constant-expression");
2628 return true;
2629 case NIC_ARRAY_REF:
2630 error ("an array reference "
2631 "cannot appear in a constant-expression");
2632 return true;
2633 case NIC_ADDR_LABEL:
2634 error ("the address of a label "
2635 "cannot appear in a constant-expression");
2636 return true;
2637 case NIC_OVERLOADED:
2638 error ("calls to overloaded operators "
2639 "cannot appear in a constant-expression");
2640 return true;
2641 case NIC_ASSIGNMENT:
2642 error ("an assignment cannot appear in a constant-expression");
2643 return true;
2644 case NIC_COMMA:
2645 error ("a comma operator "
2646 "cannot appear in a constant-expression");
2647 return true;
2648 case NIC_CONSTRUCTOR:
2649 error ("a call to a constructor "
2650 "cannot appear in a constant-expression");
2651 return true;
2652 case NIC_TRANSACTION:
2653 error ("a transaction expression "
2654 "cannot appear in a constant-expression");
2655 return true;
2656 case NIC_THIS:
2657 msg = "this";
2658 break;
2659 case NIC_FUNC_NAME:
2660 msg = "__FUNCTION__";
2661 break;
2662 case NIC_PRETTY_FUNC:
2663 msg = "__PRETTY_FUNCTION__";
2664 break;
2665 case NIC_C99_FUNC:
2666 msg = "__func__";
2667 break;
2668 case NIC_VA_ARG:
2669 msg = "va_arg";
2670 break;
2671 case NIC_ARROW:
2672 msg = "->";
2673 break;
2674 case NIC_POINT:
2675 msg = ".";
2676 break;
2677 case NIC_STAR:
2678 msg = "*";
2679 break;
2680 case NIC_ADDR:
2681 msg = "&";
2682 break;
2683 case NIC_PREINCREMENT:
2684 msg = "++";
2685 break;
2686 case NIC_PREDECREMENT:
2687 msg = "--";
2688 break;
2689 case NIC_NEW:
2690 msg = "new";
2691 break;
2692 case NIC_DEL:
2693 msg = "delete";
2694 break;
2695 default:
2696 gcc_unreachable ();
2698 if (msg)
2699 error ("%qs cannot appear in a constant-expression", msg);
2700 return true;
2703 return false;
2706 /* Emit a diagnostic for an invalid type name. SCOPE is the
2707 qualifying scope (or NULL, if none) for ID. This function commits
2708 to the current active tentative parse, if any. (Otherwise, the
2709 problematic construct might be encountered again later, resulting
2710 in duplicate error messages.) LOCATION is the location of ID. */
2712 static void
2713 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2714 tree scope, tree id,
2715 location_t location)
2717 tree decl, old_scope;
2718 cp_parser_commit_to_tentative_parse (parser);
2719 /* Try to lookup the identifier. */
2720 old_scope = parser->scope;
2721 parser->scope = scope;
2722 decl = cp_parser_lookup_name_simple (parser, id, location);
2723 parser->scope = old_scope;
2724 /* If the lookup found a template-name, it means that the user forgot
2725 to specify an argument list. Emit a useful error message. */
2726 if (TREE_CODE (decl) == TEMPLATE_DECL)
2727 error_at (location,
2728 "invalid use of template-name %qE without an argument list",
2729 decl);
2730 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2731 error_at (location, "invalid use of destructor %qD as a type", id);
2732 else if (TREE_CODE (decl) == TYPE_DECL)
2733 /* Something like 'unsigned A a;' */
2734 error_at (location, "invalid combination of multiple type-specifiers");
2735 else if (!parser->scope)
2737 /* Issue an error message. */
2738 error_at (location, "%qE does not name a type", id);
2739 /* If we're in a template class, it's possible that the user was
2740 referring to a type from a base class. For example:
2742 template <typename T> struct A { typedef T X; };
2743 template <typename T> struct B : public A<T> { X x; };
2745 The user should have said "typename A<T>::X". */
2746 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2747 inform (location, "C++11 %<constexpr%> only available with "
2748 "-std=c++11 or -std=gnu++11");
2749 else if (processing_template_decl && current_class_type
2750 && TYPE_BINFO (current_class_type))
2752 tree b;
2754 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2756 b = TREE_CHAIN (b))
2758 tree base_type = BINFO_TYPE (b);
2759 if (CLASS_TYPE_P (base_type)
2760 && dependent_type_p (base_type))
2762 tree field;
2763 /* Go from a particular instantiation of the
2764 template (which will have an empty TYPE_FIELDs),
2765 to the main version. */
2766 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2767 for (field = TYPE_FIELDS (base_type);
2768 field;
2769 field = DECL_CHAIN (field))
2770 if (TREE_CODE (field) == TYPE_DECL
2771 && DECL_NAME (field) == id)
2773 inform (location,
2774 "(perhaps %<typename %T::%E%> was intended)",
2775 BINFO_TYPE (b), id);
2776 break;
2778 if (field)
2779 break;
2784 /* Here we diagnose qualified-ids where the scope is actually correct,
2785 but the identifier does not resolve to a valid type name. */
2786 else if (parser->scope != error_mark_node)
2788 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2789 error_at (location, "%qE in namespace %qE does not name a type",
2790 id, parser->scope);
2791 else if (CLASS_TYPE_P (parser->scope)
2792 && constructor_name_p (id, parser->scope))
2794 /* A<T>::A<T>() */
2795 error_at (location, "%<%T::%E%> names the constructor, not"
2796 " the type", parser->scope, id);
2797 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2798 error_at (location, "and %qT has no template constructors",
2799 parser->scope);
2801 else if (TYPE_P (parser->scope)
2802 && dependent_scope_p (parser->scope))
2803 error_at (location, "need %<typename%> before %<%T::%E%> because "
2804 "%qT is a dependent scope",
2805 parser->scope, id, parser->scope);
2806 else if (TYPE_P (parser->scope))
2807 error_at (location, "%qE in %q#T does not name a type",
2808 id, parser->scope);
2809 else
2810 gcc_unreachable ();
2814 /* Check for a common situation where a type-name should be present,
2815 but is not, and issue a sensible error message. Returns true if an
2816 invalid type-name was detected.
2818 The situation handled by this function are variable declarations of the
2819 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2820 Usually, `ID' should name a type, but if we got here it means that it
2821 does not. We try to emit the best possible error message depending on
2822 how exactly the id-expression looks like. */
2824 static bool
2825 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2827 tree id;
2828 cp_token *token = cp_lexer_peek_token (parser->lexer);
2830 /* Avoid duplicate error about ambiguous lookup. */
2831 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2833 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2834 if (next->type == CPP_NAME && next->ambiguous_p)
2835 goto out;
2838 cp_parser_parse_tentatively (parser);
2839 id = cp_parser_id_expression (parser,
2840 /*template_keyword_p=*/false,
2841 /*check_dependency_p=*/true,
2842 /*template_p=*/NULL,
2843 /*declarator_p=*/true,
2844 /*optional_p=*/false);
2845 /* If the next token is a (, this is a function with no explicit return
2846 type, i.e. constructor, destructor or conversion op. */
2847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2848 || TREE_CODE (id) == TYPE_DECL)
2850 cp_parser_abort_tentative_parse (parser);
2851 return false;
2853 if (!cp_parser_parse_definitely (parser))
2854 return false;
2856 /* Emit a diagnostic for the invalid type. */
2857 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2858 id, token->location);
2859 out:
2860 /* If we aren't in the middle of a declarator (i.e. in a
2861 parameter-declaration-clause), skip to the end of the declaration;
2862 there's no point in trying to process it. */
2863 if (!parser->in_declarator_p)
2864 cp_parser_skip_to_end_of_block_or_statement (parser);
2865 return true;
2868 /* Consume tokens up to, and including, the next non-nested closing `)'.
2869 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2870 are doing error recovery. Returns -1 if OR_COMMA is true and we
2871 found an unnested comma. */
2873 static int
2874 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2875 bool recovering,
2876 bool or_comma,
2877 bool consume_paren)
2879 unsigned paren_depth = 0;
2880 unsigned brace_depth = 0;
2881 unsigned square_depth = 0;
2883 if (recovering && !or_comma
2884 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2885 return 0;
2887 while (true)
2889 cp_token * token = cp_lexer_peek_token (parser->lexer);
2891 switch (token->type)
2893 case CPP_EOF:
2894 case CPP_PRAGMA_EOL:
2895 /* If we've run out of tokens, then there is no closing `)'. */
2896 return 0;
2898 /* This is good for lambda expression capture-lists. */
2899 case CPP_OPEN_SQUARE:
2900 ++square_depth;
2901 break;
2902 case CPP_CLOSE_SQUARE:
2903 if (!square_depth--)
2904 return 0;
2905 break;
2907 case CPP_SEMICOLON:
2908 /* This matches the processing in skip_to_end_of_statement. */
2909 if (!brace_depth)
2910 return 0;
2911 break;
2913 case CPP_OPEN_BRACE:
2914 ++brace_depth;
2915 break;
2916 case CPP_CLOSE_BRACE:
2917 if (!brace_depth--)
2918 return 0;
2919 break;
2921 case CPP_COMMA:
2922 if (recovering && or_comma && !brace_depth && !paren_depth
2923 && !square_depth)
2924 return -1;
2925 break;
2927 case CPP_OPEN_PAREN:
2928 if (!brace_depth)
2929 ++paren_depth;
2930 break;
2932 case CPP_CLOSE_PAREN:
2933 if (!brace_depth && !paren_depth--)
2935 if (consume_paren)
2936 cp_lexer_consume_token (parser->lexer);
2937 return 1;
2939 break;
2941 default:
2942 break;
2945 /* Consume the token. */
2946 cp_lexer_consume_token (parser->lexer);
2950 /* Consume tokens until we reach the end of the current statement.
2951 Normally, that will be just before consuming a `;'. However, if a
2952 non-nested `}' comes first, then we stop before consuming that. */
2954 static void
2955 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2957 unsigned nesting_depth = 0;
2959 while (true)
2961 cp_token *token = cp_lexer_peek_token (parser->lexer);
2963 switch (token->type)
2965 case CPP_EOF:
2966 case CPP_PRAGMA_EOL:
2967 /* If we've run out of tokens, stop. */
2968 return;
2970 case CPP_SEMICOLON:
2971 /* If the next token is a `;', we have reached the end of the
2972 statement. */
2973 if (!nesting_depth)
2974 return;
2975 break;
2977 case CPP_CLOSE_BRACE:
2978 /* If this is a non-nested '}', stop before consuming it.
2979 That way, when confronted with something like:
2981 { 3 + }
2983 we stop before consuming the closing '}', even though we
2984 have not yet reached a `;'. */
2985 if (nesting_depth == 0)
2986 return;
2988 /* If it is the closing '}' for a block that we have
2989 scanned, stop -- but only after consuming the token.
2990 That way given:
2992 void f g () { ... }
2993 typedef int I;
2995 we will stop after the body of the erroneously declared
2996 function, but before consuming the following `typedef'
2997 declaration. */
2998 if (--nesting_depth == 0)
3000 cp_lexer_consume_token (parser->lexer);
3001 return;
3004 case CPP_OPEN_BRACE:
3005 ++nesting_depth;
3006 break;
3008 default:
3009 break;
3012 /* Consume the token. */
3013 cp_lexer_consume_token (parser->lexer);
3017 /* This function is called at the end of a statement or declaration.
3018 If the next token is a semicolon, it is consumed; otherwise, error
3019 recovery is attempted. */
3021 static void
3022 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3024 /* Look for the trailing `;'. */
3025 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3027 /* If there is additional (erroneous) input, skip to the end of
3028 the statement. */
3029 cp_parser_skip_to_end_of_statement (parser);
3030 /* If the next token is now a `;', consume it. */
3031 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3032 cp_lexer_consume_token (parser->lexer);
3036 /* Skip tokens until we have consumed an entire block, or until we
3037 have consumed a non-nested `;'. */
3039 static void
3040 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3042 int nesting_depth = 0;
3044 while (nesting_depth >= 0)
3046 cp_token *token = cp_lexer_peek_token (parser->lexer);
3048 switch (token->type)
3050 case CPP_EOF:
3051 case CPP_PRAGMA_EOL:
3052 /* If we've run out of tokens, stop. */
3053 return;
3055 case CPP_SEMICOLON:
3056 /* Stop if this is an unnested ';'. */
3057 if (!nesting_depth)
3058 nesting_depth = -1;
3059 break;
3061 case CPP_CLOSE_BRACE:
3062 /* Stop if this is an unnested '}', or closes the outermost
3063 nesting level. */
3064 nesting_depth--;
3065 if (nesting_depth < 0)
3066 return;
3067 if (!nesting_depth)
3068 nesting_depth = -1;
3069 break;
3071 case CPP_OPEN_BRACE:
3072 /* Nest. */
3073 nesting_depth++;
3074 break;
3076 default:
3077 break;
3080 /* Consume the token. */
3081 cp_lexer_consume_token (parser->lexer);
3085 /* Skip tokens until a non-nested closing curly brace is the next
3086 token, or there are no more tokens. Return true in the first case,
3087 false otherwise. */
3089 static bool
3090 cp_parser_skip_to_closing_brace (cp_parser *parser)
3092 unsigned nesting_depth = 0;
3094 while (true)
3096 cp_token *token = cp_lexer_peek_token (parser->lexer);
3098 switch (token->type)
3100 case CPP_EOF:
3101 case CPP_PRAGMA_EOL:
3102 /* If we've run out of tokens, stop. */
3103 return false;
3105 case CPP_CLOSE_BRACE:
3106 /* If the next token is a non-nested `}', then we have reached
3107 the end of the current block. */
3108 if (nesting_depth-- == 0)
3109 return true;
3110 break;
3112 case CPP_OPEN_BRACE:
3113 /* If it the next token is a `{', then we are entering a new
3114 block. Consume the entire block. */
3115 ++nesting_depth;
3116 break;
3118 default:
3119 break;
3122 /* Consume the token. */
3123 cp_lexer_consume_token (parser->lexer);
3127 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3128 parameter is the PRAGMA token, allowing us to purge the entire pragma
3129 sequence. */
3131 static void
3132 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3134 cp_token *token;
3136 parser->lexer->in_pragma = false;
3139 token = cp_lexer_consume_token (parser->lexer);
3140 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3142 /* Ensure that the pragma is not parsed again. */
3143 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3146 /* Require pragma end of line, resyncing with it as necessary. The
3147 arguments are as for cp_parser_skip_to_pragma_eol. */
3149 static void
3150 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3152 parser->lexer->in_pragma = false;
3153 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3154 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3157 /* This is a simple wrapper around make_typename_type. When the id is
3158 an unresolved identifier node, we can provide a superior diagnostic
3159 using cp_parser_diagnose_invalid_type_name. */
3161 static tree
3162 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3163 tree id, location_t id_location)
3165 tree result;
3166 if (TREE_CODE (id) == IDENTIFIER_NODE)
3168 result = make_typename_type (scope, id, typename_type,
3169 /*complain=*/tf_none);
3170 if (result == error_mark_node)
3171 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3172 return result;
3174 return make_typename_type (scope, id, typename_type, tf_error);
3177 /* This is a wrapper around the
3178 make_{pointer,ptrmem,reference}_declarator functions that decides
3179 which one to call based on the CODE and CLASS_TYPE arguments. The
3180 CODE argument should be one of the values returned by
3181 cp_parser_ptr_operator. */
3182 static cp_declarator *
3183 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3184 cp_cv_quals cv_qualifiers,
3185 cp_declarator *target)
3187 if (code == ERROR_MARK)
3188 return cp_error_declarator;
3190 if (code == INDIRECT_REF)
3191 if (class_type == NULL_TREE)
3192 return make_pointer_declarator (cv_qualifiers, target);
3193 else
3194 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3195 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3196 return make_reference_declarator (cv_qualifiers, target, false);
3197 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3198 return make_reference_declarator (cv_qualifiers, target, true);
3199 gcc_unreachable ();
3202 /* Create a new C++ parser. */
3204 static cp_parser *
3205 cp_parser_new (void)
3207 cp_parser *parser;
3208 cp_lexer *lexer;
3209 unsigned i;
3211 /* cp_lexer_new_main is called before doing GC allocation because
3212 cp_lexer_new_main might load a PCH file. */
3213 lexer = cp_lexer_new_main ();
3215 /* Initialize the binops_by_token so that we can get the tree
3216 directly from the token. */
3217 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3218 binops_by_token[binops[i].token_type] = binops[i];
3220 parser = ggc_alloc_cleared_cp_parser ();
3221 parser->lexer = lexer;
3222 parser->context = cp_parser_context_new (NULL);
3224 /* For now, we always accept GNU extensions. */
3225 parser->allow_gnu_extensions_p = 1;
3227 /* The `>' token is a greater-than operator, not the end of a
3228 template-id. */
3229 parser->greater_than_is_operator_p = true;
3231 parser->default_arg_ok_p = true;
3233 /* We are not parsing a constant-expression. */
3234 parser->integral_constant_expression_p = false;
3235 parser->allow_non_integral_constant_expression_p = false;
3236 parser->non_integral_constant_expression_p = false;
3238 /* Local variable names are not forbidden. */
3239 parser->local_variables_forbidden_p = false;
3241 /* We are not processing an `extern "C"' declaration. */
3242 parser->in_unbraced_linkage_specification_p = false;
3244 /* We are not processing a declarator. */
3245 parser->in_declarator_p = false;
3247 /* We are not processing a template-argument-list. */
3248 parser->in_template_argument_list_p = false;
3250 /* We are not in an iteration statement. */
3251 parser->in_statement = 0;
3253 /* We are not in a switch statement. */
3254 parser->in_switch_statement_p = false;
3256 /* We are not parsing a type-id inside an expression. */
3257 parser->in_type_id_in_expr_p = false;
3259 /* Declarations aren't implicitly extern "C". */
3260 parser->implicit_extern_c = false;
3262 /* String literals should be translated to the execution character set. */
3263 parser->translate_strings_p = true;
3265 /* We are not parsing a function body. */
3266 parser->in_function_body = false;
3268 /* We can correct until told otherwise. */
3269 parser->colon_corrects_to_scope_p = true;
3271 /* The unparsed function queue is empty. */
3272 push_unparsed_function_queues (parser);
3274 /* There are no classes being defined. */
3275 parser->num_classes_being_defined = 0;
3277 /* No template parameters apply. */
3278 parser->num_template_parameter_lists = 0;
3280 return parser;
3283 /* Create a cp_lexer structure which will emit the tokens in CACHE
3284 and push it onto the parser's lexer stack. This is used for delayed
3285 parsing of in-class method bodies and default arguments, and should
3286 not be confused with tentative parsing. */
3287 static void
3288 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3290 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3291 lexer->next = parser->lexer;
3292 parser->lexer = lexer;
3294 /* Move the current source position to that of the first token in the
3295 new lexer. */
3296 cp_lexer_set_source_position_from_token (lexer->next_token);
3299 /* Pop the top lexer off the parser stack. This is never used for the
3300 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3301 static void
3302 cp_parser_pop_lexer (cp_parser *parser)
3304 cp_lexer *lexer = parser->lexer;
3305 parser->lexer = lexer->next;
3306 cp_lexer_destroy (lexer);
3308 /* Put the current source position back where it was before this
3309 lexer was pushed. */
3310 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3313 /* Lexical conventions [gram.lex] */
3315 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3316 identifier. */
3318 static tree
3319 cp_parser_identifier (cp_parser* parser)
3321 cp_token *token;
3323 /* Look for the identifier. */
3324 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3325 /* Return the value. */
3326 return token ? token->u.value : error_mark_node;
3329 /* Parse a sequence of adjacent string constants. Returns a
3330 TREE_STRING representing the combined, nul-terminated string
3331 constant. If TRANSLATE is true, translate the string to the
3332 execution character set. If WIDE_OK is true, a wide string is
3333 invalid here.
3335 C++98 [lex.string] says that if a narrow string literal token is
3336 adjacent to a wide string literal token, the behavior is undefined.
3337 However, C99 6.4.5p4 says that this results in a wide string literal.
3338 We follow C99 here, for consistency with the C front end.
3340 This code is largely lifted from lex_string() in c-lex.c.
3342 FUTURE: ObjC++ will need to handle @-strings here. */
3343 static tree
3344 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3346 tree value;
3347 size_t count;
3348 struct obstack str_ob;
3349 cpp_string str, istr, *strs;
3350 cp_token *tok;
3351 enum cpp_ttype type, curr_type;
3352 int have_suffix_p = 0;
3353 tree string_tree;
3354 tree suffix_id = NULL_TREE;
3355 bool curr_tok_is_userdef_p = false;
3357 tok = cp_lexer_peek_token (parser->lexer);
3358 if (!cp_parser_is_string_literal (tok))
3360 cp_parser_error (parser, "expected string-literal");
3361 return error_mark_node;
3364 if (cpp_userdef_string_p (tok->type))
3366 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3367 curr_type = cpp_userdef_string_remove_type (tok->type);
3368 curr_tok_is_userdef_p = true;
3370 else
3372 string_tree = tok->u.value;
3373 curr_type = tok->type;
3375 type = curr_type;
3377 /* Try to avoid the overhead of creating and destroying an obstack
3378 for the common case of just one string. */
3379 if (!cp_parser_is_string_literal
3380 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3382 cp_lexer_consume_token (parser->lexer);
3384 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3385 str.len = TREE_STRING_LENGTH (string_tree);
3386 count = 1;
3388 if (curr_tok_is_userdef_p)
3390 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3391 have_suffix_p = 1;
3392 curr_type = cpp_userdef_string_remove_type (tok->type);
3394 else
3395 curr_type = tok->type;
3397 strs = &str;
3399 else
3401 gcc_obstack_init (&str_ob);
3402 count = 0;
3406 cp_lexer_consume_token (parser->lexer);
3407 count++;
3408 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3409 str.len = TREE_STRING_LENGTH (string_tree);
3411 if (curr_tok_is_userdef_p)
3413 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3414 if (have_suffix_p == 0)
3416 suffix_id = curr_suffix_id;
3417 have_suffix_p = 1;
3419 else if (have_suffix_p == 1
3420 && curr_suffix_id != suffix_id)
3422 error ("inconsistent user-defined literal suffixes"
3423 " %qD and %qD in string literal",
3424 suffix_id, curr_suffix_id);
3425 have_suffix_p = -1;
3427 curr_type = cpp_userdef_string_remove_type (tok->type);
3429 else
3430 curr_type = tok->type;
3432 if (type != curr_type)
3434 if (type == CPP_STRING)
3435 type = curr_type;
3436 else if (curr_type != CPP_STRING)
3437 error_at (tok->location,
3438 "unsupported non-standard concatenation "
3439 "of string literals");
3442 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3444 tok = cp_lexer_peek_token (parser->lexer);
3445 if (cpp_userdef_string_p (tok->type))
3447 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3448 curr_type = cpp_userdef_string_remove_type (tok->type);
3449 curr_tok_is_userdef_p = true;
3451 else
3453 string_tree = tok->u.value;
3454 curr_type = tok->type;
3455 curr_tok_is_userdef_p = false;
3458 while (cp_parser_is_string_literal (tok));
3460 strs = (cpp_string *) obstack_finish (&str_ob);
3463 if (type != CPP_STRING && !wide_ok)
3465 cp_parser_error (parser, "a wide string is invalid in this context");
3466 type = CPP_STRING;
3469 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3470 (parse_in, strs, count, &istr, type))
3472 value = build_string (istr.len, (const char *)istr.text);
3473 free (CONST_CAST (unsigned char *, istr.text));
3475 switch (type)
3477 default:
3478 case CPP_STRING:
3479 case CPP_UTF8STRING:
3480 TREE_TYPE (value) = char_array_type_node;
3481 break;
3482 case CPP_STRING16:
3483 TREE_TYPE (value) = char16_array_type_node;
3484 break;
3485 case CPP_STRING32:
3486 TREE_TYPE (value) = char32_array_type_node;
3487 break;
3488 case CPP_WSTRING:
3489 TREE_TYPE (value) = wchar_array_type_node;
3490 break;
3493 value = fix_string_type (value);
3495 if (have_suffix_p)
3497 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3498 tok->u.value = literal;
3499 return cp_parser_userdef_string_literal (tok);
3502 else
3503 /* cpp_interpret_string has issued an error. */
3504 value = error_mark_node;
3506 if (count > 1)
3507 obstack_free (&str_ob, 0);
3509 return value;
3512 /* Look up a literal operator with the name and the exact arguments. */
3514 static tree
3515 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3517 tree decl, fns;
3518 decl = lookup_name (name);
3519 if (!decl || !is_overloaded_fn (decl))
3520 return error_mark_node;
3522 for (fns = decl; fns; fns = OVL_NEXT (fns))
3524 unsigned int ix;
3525 bool found = true;
3526 tree fn = OVL_CURRENT (fns);
3527 tree argtypes = NULL_TREE;
3528 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3529 if (argtypes != NULL_TREE)
3531 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3532 ++ix, argtypes = TREE_CHAIN (argtypes))
3534 tree targ = TREE_VALUE (argtypes);
3535 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3536 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3537 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3538 if ((ptr || arr || !same_type_p (targ, tparm))
3539 && (!ptr || !arr
3540 || !same_type_p (TREE_TYPE (targ),
3541 TREE_TYPE (tparm))))
3542 found = false;
3544 if (found
3545 && ix == VEC_length (tree, args)
3546 /* May be this should be sufficient_parms_p instead,
3547 depending on how exactly should user-defined literals
3548 work in presence of default arguments on the literal
3549 operator parameters. */
3550 && argtypes == void_list_node)
3551 return fn;
3555 return error_mark_node;
3558 /* Parse a user-defined char constant. Returns a call to a user-defined
3559 literal operator taking the character as an argument. */
3561 static tree
3562 cp_parser_userdef_char_literal (cp_parser *parser)
3564 cp_token *token = cp_lexer_consume_token (parser->lexer);
3565 tree literal = token->u.value;
3566 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3567 tree value = USERDEF_LITERAL_VALUE (literal);
3568 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3569 tree decl, result;
3571 /* Build up a call to the user-defined operator */
3572 /* Lookup the name we got back from the id-expression. */
3573 VEC(tree,gc) *args = make_tree_vector ();
3574 VEC_safe_push (tree, gc, args, value);
3575 decl = lookup_literal_operator (name, args);
3576 if (!decl || decl == error_mark_node)
3578 error ("unable to find character literal operator %qD with %qT argument",
3579 name, TREE_TYPE (value));
3580 release_tree_vector (args);
3581 return error_mark_node;
3583 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3584 release_tree_vector (args);
3585 if (result != error_mark_node)
3586 return result;
3588 error ("unable to find character literal operator %qD with %qT argument",
3589 name, TREE_TYPE (value));
3590 return error_mark_node;
3593 /* A subroutine of cp_parser_userdef_numeric_literal to
3594 create a char... template parameter pack from a string node. */
3596 static tree
3597 make_char_string_pack (tree value)
3599 tree charvec;
3600 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3601 const char *str = TREE_STRING_POINTER (value);
3602 int i, len = TREE_STRING_LENGTH (value) - 1;
3603 tree argvec = make_tree_vec (1);
3605 /* Fill in CHARVEC with all of the parameters. */
3606 charvec = make_tree_vec (len);
3607 for (i = 0; i < len; ++i)
3608 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3610 /* Build the argument packs. */
3611 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3612 TREE_TYPE (argpack) = char_type_node;
3614 TREE_VEC_ELT (argvec, 0) = argpack;
3616 return argvec;
3619 /* Parse a user-defined numeric constant. returns a call to a user-defined
3620 literal operator. */
3622 static tree
3623 cp_parser_userdef_numeric_literal (cp_parser *parser)
3625 cp_token *token = cp_lexer_consume_token (parser->lexer);
3626 tree literal = token->u.value;
3627 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3628 tree value = USERDEF_LITERAL_VALUE (literal);
3629 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3630 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3631 tree decl, result;
3632 VEC(tree,gc) *args;
3634 /* Look for a literal operator taking the exact type of numeric argument
3635 as the literal value. */
3636 args = make_tree_vector ();
3637 VEC_safe_push (tree, gc, args, value);
3638 decl = lookup_literal_operator (name, args);
3639 if (decl && decl != error_mark_node)
3641 result = finish_call_expr (decl, &args, false, true, tf_none);
3642 if (result != error_mark_node)
3644 release_tree_vector (args);
3645 return result;
3648 release_tree_vector (args);
3650 /* If the numeric argument didn't work, look for a raw literal
3651 operator taking a const char* argument consisting of the number
3652 in string format. */
3653 args = make_tree_vector ();
3654 VEC_safe_push (tree, gc, args, num_string);
3655 decl = lookup_literal_operator (name, args);
3656 if (decl && decl != error_mark_node)
3658 result = finish_call_expr (decl, &args, false, true, tf_none);
3659 if (result != error_mark_node)
3661 release_tree_vector (args);
3662 return result;
3665 release_tree_vector (args);
3667 /* If the raw literal didn't work, look for a non-type template
3668 function with parameter pack char.... Call the function with
3669 template parameter characters representing the number. */
3670 args = make_tree_vector ();
3671 decl = lookup_literal_operator (name, args);
3672 if (decl && decl != error_mark_node)
3674 tree tmpl_args = make_char_string_pack (num_string);
3675 decl = lookup_template_function (decl, tmpl_args);
3676 result = finish_call_expr (decl, &args, false, true, tf_none);
3677 if (result != error_mark_node)
3679 release_tree_vector (args);
3680 return result;
3683 release_tree_vector (args);
3685 error ("unable to find numeric literal operator %qD", name);
3686 return error_mark_node;
3689 /* Parse a user-defined string constant. Returns a call to a user-defined
3690 literal operator taking a character pointer and the length of the string
3691 as arguments. */
3693 static tree
3694 cp_parser_userdef_string_literal (cp_token *token)
3696 tree literal = token->u.value;
3697 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3698 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3699 tree value = USERDEF_LITERAL_VALUE (literal);
3700 int len = TREE_STRING_LENGTH (value)
3701 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3702 tree decl, result;
3704 /* Build up a call to the user-defined operator */
3705 /* Lookup the name we got back from the id-expression. */
3706 VEC(tree,gc) *args = make_tree_vector ();
3707 VEC_safe_push (tree, gc, args, value);
3708 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3709 decl = lookup_name (name);
3710 if (!decl || decl == error_mark_node)
3712 error ("unable to find string literal operator %qD", name);
3713 release_tree_vector (args);
3714 return error_mark_node;
3716 result = finish_call_expr (decl, &args, false, true, tf_none);
3717 release_tree_vector (args);
3718 if (result != error_mark_node)
3719 return result;
3721 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3722 name, TREE_TYPE (value), size_type_node);
3723 return error_mark_node;
3727 /* Basic concepts [gram.basic] */
3729 /* Parse a translation-unit.
3731 translation-unit:
3732 declaration-seq [opt]
3734 Returns TRUE if all went well. */
3736 static bool
3737 cp_parser_translation_unit (cp_parser* parser)
3739 /* The address of the first non-permanent object on the declarator
3740 obstack. */
3741 static void *declarator_obstack_base;
3743 bool success;
3745 /* Create the declarator obstack, if necessary. */
3746 if (!cp_error_declarator)
3748 gcc_obstack_init (&declarator_obstack);
3749 /* Create the error declarator. */
3750 cp_error_declarator = make_declarator (cdk_error);
3751 /* Create the empty parameter list. */
3752 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3753 /* Remember where the base of the declarator obstack lies. */
3754 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3757 cp_parser_declaration_seq_opt (parser);
3759 /* If there are no tokens left then all went well. */
3760 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3762 /* Get rid of the token array; we don't need it any more. */
3763 cp_lexer_destroy (parser->lexer);
3764 parser->lexer = NULL;
3766 /* This file might have been a context that's implicitly extern
3767 "C". If so, pop the lang context. (Only relevant for PCH.) */
3768 if (parser->implicit_extern_c)
3770 pop_lang_context ();
3771 parser->implicit_extern_c = false;
3774 /* Finish up. */
3775 finish_translation_unit ();
3777 success = true;
3779 else
3781 cp_parser_error (parser, "expected declaration");
3782 success = false;
3785 /* Make sure the declarator obstack was fully cleaned up. */
3786 gcc_assert (obstack_next_free (&declarator_obstack)
3787 == declarator_obstack_base);
3789 /* All went well. */
3790 return success;
3793 /* Expressions [gram.expr] */
3795 /* Parse a primary-expression.
3797 primary-expression:
3798 literal
3799 this
3800 ( expression )
3801 id-expression
3803 GNU Extensions:
3805 primary-expression:
3806 ( compound-statement )
3807 __builtin_va_arg ( assignment-expression , type-id )
3808 __builtin_offsetof ( type-id , offsetof-expression )
3810 C++ Extensions:
3811 __has_nothrow_assign ( type-id )
3812 __has_nothrow_constructor ( type-id )
3813 __has_nothrow_copy ( type-id )
3814 __has_trivial_assign ( type-id )
3815 __has_trivial_constructor ( type-id )
3816 __has_trivial_copy ( type-id )
3817 __has_trivial_destructor ( type-id )
3818 __has_virtual_destructor ( type-id )
3819 __is_abstract ( type-id )
3820 __is_base_of ( type-id , type-id )
3821 __is_class ( type-id )
3822 __is_convertible_to ( type-id , type-id )
3823 __is_empty ( type-id )
3824 __is_enum ( type-id )
3825 __is_final ( type-id )
3826 __is_literal_type ( type-id )
3827 __is_pod ( type-id )
3828 __is_polymorphic ( type-id )
3829 __is_std_layout ( type-id )
3830 __is_trivial ( type-id )
3831 __is_union ( type-id )
3833 Objective-C++ Extension:
3835 primary-expression:
3836 objc-expression
3838 literal:
3839 __null
3841 ADDRESS_P is true iff this expression was immediately preceded by
3842 "&" and therefore might denote a pointer-to-member. CAST_P is true
3843 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3844 true iff this expression is a template argument.
3846 Returns a representation of the expression. Upon return, *IDK
3847 indicates what kind of id-expression (if any) was present. */
3849 static tree
3850 cp_parser_primary_expression (cp_parser *parser,
3851 bool address_p,
3852 bool cast_p,
3853 bool template_arg_p,
3854 cp_id_kind *idk)
3856 cp_token *token = NULL;
3858 /* Assume the primary expression is not an id-expression. */
3859 *idk = CP_ID_KIND_NONE;
3861 /* Peek at the next token. */
3862 token = cp_lexer_peek_token (parser->lexer);
3863 switch (token->type)
3865 /* literal:
3866 integer-literal
3867 character-literal
3868 floating-literal
3869 string-literal
3870 boolean-literal
3871 pointer-literal
3872 user-defined-literal */
3873 case CPP_CHAR:
3874 case CPP_CHAR16:
3875 case CPP_CHAR32:
3876 case CPP_WCHAR:
3877 case CPP_NUMBER:
3878 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3879 return cp_parser_userdef_numeric_literal (parser);
3880 token = cp_lexer_consume_token (parser->lexer);
3881 if (TREE_CODE (token->u.value) == FIXED_CST)
3883 error_at (token->location,
3884 "fixed-point types not supported in C++");
3885 return error_mark_node;
3887 /* Floating-point literals are only allowed in an integral
3888 constant expression if they are cast to an integral or
3889 enumeration type. */
3890 if (TREE_CODE (token->u.value) == REAL_CST
3891 && parser->integral_constant_expression_p
3892 && pedantic)
3894 /* CAST_P will be set even in invalid code like "int(2.7 +
3895 ...)". Therefore, we have to check that the next token
3896 is sure to end the cast. */
3897 if (cast_p)
3899 cp_token *next_token;
3901 next_token = cp_lexer_peek_token (parser->lexer);
3902 if (/* The comma at the end of an
3903 enumerator-definition. */
3904 next_token->type != CPP_COMMA
3905 /* The curly brace at the end of an enum-specifier. */
3906 && next_token->type != CPP_CLOSE_BRACE
3907 /* The end of a statement. */
3908 && next_token->type != CPP_SEMICOLON
3909 /* The end of the cast-expression. */
3910 && next_token->type != CPP_CLOSE_PAREN
3911 /* The end of an array bound. */
3912 && next_token->type != CPP_CLOSE_SQUARE
3913 /* The closing ">" in a template-argument-list. */
3914 && (next_token->type != CPP_GREATER
3915 || parser->greater_than_is_operator_p)
3916 /* C++0x only: A ">>" treated like two ">" tokens,
3917 in a template-argument-list. */
3918 && (next_token->type != CPP_RSHIFT
3919 || (cxx_dialect == cxx98)
3920 || parser->greater_than_is_operator_p))
3921 cast_p = false;
3924 /* If we are within a cast, then the constraint that the
3925 cast is to an integral or enumeration type will be
3926 checked at that point. If we are not within a cast, then
3927 this code is invalid. */
3928 if (!cast_p)
3929 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3931 return token->u.value;
3933 case CPP_CHAR_USERDEF:
3934 case CPP_CHAR16_USERDEF:
3935 case CPP_CHAR32_USERDEF:
3936 case CPP_WCHAR_USERDEF:
3937 return cp_parser_userdef_char_literal (parser);
3939 case CPP_STRING:
3940 case CPP_STRING16:
3941 case CPP_STRING32:
3942 case CPP_WSTRING:
3943 case CPP_UTF8STRING:
3944 case CPP_STRING_USERDEF:
3945 case CPP_STRING16_USERDEF:
3946 case CPP_STRING32_USERDEF:
3947 case CPP_WSTRING_USERDEF:
3948 case CPP_UTF8STRING_USERDEF:
3949 /* ??? Should wide strings be allowed when parser->translate_strings_p
3950 is false (i.e. in attributes)? If not, we can kill the third
3951 argument to cp_parser_string_literal. */
3952 return cp_parser_string_literal (parser,
3953 parser->translate_strings_p,
3954 true);
3956 case CPP_OPEN_PAREN:
3958 tree expr;
3959 bool saved_greater_than_is_operator_p;
3961 /* Consume the `('. */
3962 cp_lexer_consume_token (parser->lexer);
3963 /* Within a parenthesized expression, a `>' token is always
3964 the greater-than operator. */
3965 saved_greater_than_is_operator_p
3966 = parser->greater_than_is_operator_p;
3967 parser->greater_than_is_operator_p = true;
3968 /* If we see `( { ' then we are looking at the beginning of
3969 a GNU statement-expression. */
3970 if (cp_parser_allow_gnu_extensions_p (parser)
3971 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3973 /* Statement-expressions are not allowed by the standard. */
3974 pedwarn (token->location, OPT_Wpedantic,
3975 "ISO C++ forbids braced-groups within expressions");
3977 /* And they're not allowed outside of a function-body; you
3978 cannot, for example, write:
3980 int i = ({ int j = 3; j + 1; });
3982 at class or namespace scope. */
3983 if (!parser->in_function_body
3984 || parser->in_template_argument_list_p)
3986 error_at (token->location,
3987 "statement-expressions are not allowed outside "
3988 "functions nor in template-argument lists");
3989 cp_parser_skip_to_end_of_block_or_statement (parser);
3990 expr = error_mark_node;
3992 else
3994 /* Start the statement-expression. */
3995 expr = begin_stmt_expr ();
3996 /* Parse the compound-statement. */
3997 cp_parser_compound_statement (parser, expr, false, false);
3998 /* Finish up. */
3999 expr = finish_stmt_expr (expr, false);
4002 else
4004 /* Parse the parenthesized expression. */
4005 expr = cp_parser_expression (parser, cast_p, idk);
4006 /* Let the front end know that this expression was
4007 enclosed in parentheses. This matters in case, for
4008 example, the expression is of the form `A::B', since
4009 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4010 not. */
4011 finish_parenthesized_expr (expr);
4012 /* DR 705: Wrapping an unqualified name in parentheses
4013 suppresses arg-dependent lookup. We want to pass back
4014 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4015 (c++/37862), but none of the others. */
4016 if (*idk != CP_ID_KIND_QUALIFIED)
4017 *idk = CP_ID_KIND_NONE;
4019 /* The `>' token might be the end of a template-id or
4020 template-parameter-list now. */
4021 parser->greater_than_is_operator_p
4022 = saved_greater_than_is_operator_p;
4023 /* Consume the `)'. */
4024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4025 cp_parser_skip_to_end_of_statement (parser);
4027 return expr;
4030 case CPP_OPEN_SQUARE:
4031 if (c_dialect_objc ())
4032 /* We have an Objective-C++ message. */
4033 return cp_parser_objc_expression (parser);
4035 tree lam = cp_parser_lambda_expression (parser);
4036 /* Don't warn about a failed tentative parse. */
4037 if (cp_parser_error_occurred (parser))
4038 return error_mark_node;
4039 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4040 return lam;
4043 case CPP_OBJC_STRING:
4044 if (c_dialect_objc ())
4045 /* We have an Objective-C++ string literal. */
4046 return cp_parser_objc_expression (parser);
4047 cp_parser_error (parser, "expected primary-expression");
4048 return error_mark_node;
4050 case CPP_KEYWORD:
4051 switch (token->keyword)
4053 /* These two are the boolean literals. */
4054 case RID_TRUE:
4055 cp_lexer_consume_token (parser->lexer);
4056 return boolean_true_node;
4057 case RID_FALSE:
4058 cp_lexer_consume_token (parser->lexer);
4059 return boolean_false_node;
4061 /* The `__null' literal. */
4062 case RID_NULL:
4063 cp_lexer_consume_token (parser->lexer);
4064 return null_node;
4066 /* The `nullptr' literal. */
4067 case RID_NULLPTR:
4068 cp_lexer_consume_token (parser->lexer);
4069 return nullptr_node;
4071 /* Recognize the `this' keyword. */
4072 case RID_THIS:
4073 cp_lexer_consume_token (parser->lexer);
4074 if (parser->local_variables_forbidden_p)
4076 error_at (token->location,
4077 "%<this%> may not be used in this context");
4078 return error_mark_node;
4080 /* Pointers cannot appear in constant-expressions. */
4081 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4082 return error_mark_node;
4083 return finish_this_expr ();
4085 /* The `operator' keyword can be the beginning of an
4086 id-expression. */
4087 case RID_OPERATOR:
4088 goto id_expression;
4090 case RID_FUNCTION_NAME:
4091 case RID_PRETTY_FUNCTION_NAME:
4092 case RID_C99_FUNCTION_NAME:
4094 non_integral_constant name;
4096 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4097 __func__ are the names of variables -- but they are
4098 treated specially. Therefore, they are handled here,
4099 rather than relying on the generic id-expression logic
4100 below. Grammatically, these names are id-expressions.
4102 Consume the token. */
4103 token = cp_lexer_consume_token (parser->lexer);
4105 switch (token->keyword)
4107 case RID_FUNCTION_NAME:
4108 name = NIC_FUNC_NAME;
4109 break;
4110 case RID_PRETTY_FUNCTION_NAME:
4111 name = NIC_PRETTY_FUNC;
4112 break;
4113 case RID_C99_FUNCTION_NAME:
4114 name = NIC_C99_FUNC;
4115 break;
4116 default:
4117 gcc_unreachable ();
4120 if (cp_parser_non_integral_constant_expression (parser, name))
4121 return error_mark_node;
4123 /* Look up the name. */
4124 return finish_fname (token->u.value);
4127 case RID_VA_ARG:
4129 tree expression;
4130 tree type;
4131 source_location type_location;
4133 /* The `__builtin_va_arg' construct is used to handle
4134 `va_arg'. Consume the `__builtin_va_arg' token. */
4135 cp_lexer_consume_token (parser->lexer);
4136 /* Look for the opening `('. */
4137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4138 /* Now, parse the assignment-expression. */
4139 expression = cp_parser_assignment_expression (parser,
4140 /*cast_p=*/false, NULL);
4141 /* Look for the `,'. */
4142 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4143 type_location = cp_lexer_peek_token (parser->lexer)->location;
4144 /* Parse the type-id. */
4145 type = cp_parser_type_id (parser);
4146 /* Look for the closing `)'. */
4147 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4148 /* Using `va_arg' in a constant-expression is not
4149 allowed. */
4150 if (cp_parser_non_integral_constant_expression (parser,
4151 NIC_VA_ARG))
4152 return error_mark_node;
4153 return build_x_va_arg (type_location, expression, type);
4156 case RID_OFFSETOF:
4157 return cp_parser_builtin_offsetof (parser);
4159 case RID_HAS_NOTHROW_ASSIGN:
4160 case RID_HAS_NOTHROW_CONSTRUCTOR:
4161 case RID_HAS_NOTHROW_COPY:
4162 case RID_HAS_TRIVIAL_ASSIGN:
4163 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4164 case RID_HAS_TRIVIAL_COPY:
4165 case RID_HAS_TRIVIAL_DESTRUCTOR:
4166 case RID_HAS_VIRTUAL_DESTRUCTOR:
4167 case RID_IS_ABSTRACT:
4168 case RID_IS_BASE_OF:
4169 case RID_IS_CLASS:
4170 case RID_IS_CONVERTIBLE_TO:
4171 case RID_IS_EMPTY:
4172 case RID_IS_ENUM:
4173 case RID_IS_FINAL:
4174 case RID_IS_LITERAL_TYPE:
4175 case RID_IS_POD:
4176 case RID_IS_POLYMORPHIC:
4177 case RID_IS_STD_LAYOUT:
4178 case RID_IS_TRIVIAL:
4179 case RID_IS_UNION:
4180 return cp_parser_trait_expr (parser, token->keyword);
4182 /* Objective-C++ expressions. */
4183 case RID_AT_ENCODE:
4184 case RID_AT_PROTOCOL:
4185 case RID_AT_SELECTOR:
4186 return cp_parser_objc_expression (parser);
4188 case RID_TEMPLATE:
4189 if (parser->in_function_body
4190 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4191 == CPP_LESS))
4193 error_at (token->location,
4194 "a template declaration cannot appear at block scope");
4195 cp_parser_skip_to_end_of_block_or_statement (parser);
4196 return error_mark_node;
4198 default:
4199 cp_parser_error (parser, "expected primary-expression");
4200 return error_mark_node;
4203 /* An id-expression can start with either an identifier, a
4204 `::' as the beginning of a qualified-id, or the "operator"
4205 keyword. */
4206 case CPP_NAME:
4207 case CPP_SCOPE:
4208 case CPP_TEMPLATE_ID:
4209 case CPP_NESTED_NAME_SPECIFIER:
4211 tree id_expression;
4212 tree decl;
4213 const char *error_msg;
4214 bool template_p;
4215 bool done;
4216 cp_token *id_expr_token;
4218 id_expression:
4219 /* Parse the id-expression. */
4220 id_expression
4221 = cp_parser_id_expression (parser,
4222 /*template_keyword_p=*/false,
4223 /*check_dependency_p=*/true,
4224 &template_p,
4225 /*declarator_p=*/false,
4226 /*optional_p=*/false);
4227 if (id_expression == error_mark_node)
4228 return error_mark_node;
4229 id_expr_token = token;
4230 token = cp_lexer_peek_token (parser->lexer);
4231 done = (token->type != CPP_OPEN_SQUARE
4232 && token->type != CPP_OPEN_PAREN
4233 && token->type != CPP_DOT
4234 && token->type != CPP_DEREF
4235 && token->type != CPP_PLUS_PLUS
4236 && token->type != CPP_MINUS_MINUS);
4237 /* If we have a template-id, then no further lookup is
4238 required. If the template-id was for a template-class, we
4239 will sometimes have a TYPE_DECL at this point. */
4240 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4241 || TREE_CODE (id_expression) == TYPE_DECL)
4242 decl = id_expression;
4243 /* Look up the name. */
4244 else
4246 tree ambiguous_decls;
4248 /* If we already know that this lookup is ambiguous, then
4249 we've already issued an error message; there's no reason
4250 to check again. */
4251 if (id_expr_token->type == CPP_NAME
4252 && id_expr_token->ambiguous_p)
4254 cp_parser_simulate_error (parser);
4255 return error_mark_node;
4258 decl = cp_parser_lookup_name (parser, id_expression,
4259 none_type,
4260 template_p,
4261 /*is_namespace=*/false,
4262 /*check_dependency=*/true,
4263 &ambiguous_decls,
4264 id_expr_token->location);
4265 /* If the lookup was ambiguous, an error will already have
4266 been issued. */
4267 if (ambiguous_decls)
4268 return error_mark_node;
4270 /* In Objective-C++, we may have an Objective-C 2.0
4271 dot-syntax for classes here. */
4272 if (c_dialect_objc ()
4273 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4274 && TREE_CODE (decl) == TYPE_DECL
4275 && objc_is_class_name (decl))
4277 tree component;
4278 cp_lexer_consume_token (parser->lexer);
4279 component = cp_parser_identifier (parser);
4280 if (component == error_mark_node)
4281 return error_mark_node;
4283 return objc_build_class_component_ref (id_expression, component);
4286 /* In Objective-C++, an instance variable (ivar) may be preferred
4287 to whatever cp_parser_lookup_name() found. */
4288 decl = objc_lookup_ivar (decl, id_expression);
4290 /* If name lookup gives us a SCOPE_REF, then the
4291 qualifying scope was dependent. */
4292 if (TREE_CODE (decl) == SCOPE_REF)
4294 /* At this point, we do not know if DECL is a valid
4295 integral constant expression. We assume that it is
4296 in fact such an expression, so that code like:
4298 template <int N> struct A {
4299 int a[B<N>::i];
4302 is accepted. At template-instantiation time, we
4303 will check that B<N>::i is actually a constant. */
4304 return decl;
4306 /* Check to see if DECL is a local variable in a context
4307 where that is forbidden. */
4308 if (parser->local_variables_forbidden_p
4309 && local_variable_p (decl))
4311 /* It might be that we only found DECL because we are
4312 trying to be generous with pre-ISO scoping rules.
4313 For example, consider:
4315 int i;
4316 void g() {
4317 for (int i = 0; i < 10; ++i) {}
4318 extern void f(int j = i);
4321 Here, name look up will originally find the out
4322 of scope `i'. We need to issue a warning message,
4323 but then use the global `i'. */
4324 decl = check_for_out_of_scope_variable (decl);
4325 if (local_variable_p (decl))
4327 error_at (id_expr_token->location,
4328 "local variable %qD may not appear in this context",
4329 decl);
4330 return error_mark_node;
4335 decl = (finish_id_expression
4336 (id_expression, decl, parser->scope,
4337 idk,
4338 parser->integral_constant_expression_p,
4339 parser->allow_non_integral_constant_expression_p,
4340 &parser->non_integral_constant_expression_p,
4341 template_p, done, address_p,
4342 template_arg_p,
4343 &error_msg,
4344 id_expr_token->location));
4345 if (error_msg)
4346 cp_parser_error (parser, error_msg);
4347 return decl;
4350 /* Anything else is an error. */
4351 default:
4352 cp_parser_error (parser, "expected primary-expression");
4353 return error_mark_node;
4357 /* Parse an id-expression.
4359 id-expression:
4360 unqualified-id
4361 qualified-id
4363 qualified-id:
4364 :: [opt] nested-name-specifier template [opt] unqualified-id
4365 :: identifier
4366 :: operator-function-id
4367 :: template-id
4369 Return a representation of the unqualified portion of the
4370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4371 a `::' or nested-name-specifier.
4373 Often, if the id-expression was a qualified-id, the caller will
4374 want to make a SCOPE_REF to represent the qualified-id. This
4375 function does not do this in order to avoid wastefully creating
4376 SCOPE_REFs when they are not required.
4378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4379 `template' keyword.
4381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4382 uninstantiated templates.
4384 If *TEMPLATE_P is non-NULL, it is set to true iff the
4385 `template' keyword is used to explicitly indicate that the entity
4386 named is a template.
4388 If DECLARATOR_P is true, the id-expression is appearing as part of
4389 a declarator, rather than as part of an expression. */
4391 static tree
4392 cp_parser_id_expression (cp_parser *parser,
4393 bool template_keyword_p,
4394 bool check_dependency_p,
4395 bool *template_p,
4396 bool declarator_p,
4397 bool optional_p)
4399 bool global_scope_p;
4400 bool nested_name_specifier_p;
4402 /* Assume the `template' keyword was not used. */
4403 if (template_p)
4404 *template_p = template_keyword_p;
4406 /* Look for the optional `::' operator. */
4407 global_scope_p
4408 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4409 != NULL_TREE);
4410 /* Look for the optional nested-name-specifier. */
4411 nested_name_specifier_p
4412 = (cp_parser_nested_name_specifier_opt (parser,
4413 /*typename_keyword_p=*/false,
4414 check_dependency_p,
4415 /*type_p=*/false,
4416 declarator_p)
4417 != NULL_TREE);
4418 /* If there is a nested-name-specifier, then we are looking at
4419 the first qualified-id production. */
4420 if (nested_name_specifier_p)
4422 tree saved_scope;
4423 tree saved_object_scope;
4424 tree saved_qualifying_scope;
4425 tree unqualified_id;
4426 bool is_template;
4428 /* See if the next token is the `template' keyword. */
4429 if (!template_p)
4430 template_p = &is_template;
4431 *template_p = cp_parser_optional_template_keyword (parser);
4432 /* Name lookup we do during the processing of the
4433 unqualified-id might obliterate SCOPE. */
4434 saved_scope = parser->scope;
4435 saved_object_scope = parser->object_scope;
4436 saved_qualifying_scope = parser->qualifying_scope;
4437 /* Process the final unqualified-id. */
4438 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4439 check_dependency_p,
4440 declarator_p,
4441 /*optional_p=*/false);
4442 /* Restore the SAVED_SCOPE for our caller. */
4443 parser->scope = saved_scope;
4444 parser->object_scope = saved_object_scope;
4445 parser->qualifying_scope = saved_qualifying_scope;
4447 return unqualified_id;
4449 /* Otherwise, if we are in global scope, then we are looking at one
4450 of the other qualified-id productions. */
4451 else if (global_scope_p)
4453 cp_token *token;
4454 tree id;
4456 /* Peek at the next token. */
4457 token = cp_lexer_peek_token (parser->lexer);
4459 /* If it's an identifier, and the next token is not a "<", then
4460 we can avoid the template-id case. This is an optimization
4461 for this common case. */
4462 if (token->type == CPP_NAME
4463 && !cp_parser_nth_token_starts_template_argument_list_p
4464 (parser, 2))
4465 return cp_parser_identifier (parser);
4467 cp_parser_parse_tentatively (parser);
4468 /* Try a template-id. */
4469 id = cp_parser_template_id (parser,
4470 /*template_keyword_p=*/false,
4471 /*check_dependency_p=*/true,
4472 none_type,
4473 declarator_p);
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser))
4476 return id;
4478 /* Peek at the next token. (Changes in the token buffer may
4479 have invalidated the pointer obtained above.) */
4480 token = cp_lexer_peek_token (parser->lexer);
4482 switch (token->type)
4484 case CPP_NAME:
4485 return cp_parser_identifier (parser);
4487 case CPP_KEYWORD:
4488 if (token->keyword == RID_OPERATOR)
4489 return cp_parser_operator_function_id (parser);
4490 /* Fall through. */
4492 default:
4493 cp_parser_error (parser, "expected id-expression");
4494 return error_mark_node;
4497 else
4498 return cp_parser_unqualified_id (parser, template_keyword_p,
4499 /*check_dependency_p=*/true,
4500 declarator_p,
4501 optional_p);
4504 /* Parse an unqualified-id.
4506 unqualified-id:
4507 identifier
4508 operator-function-id
4509 conversion-function-id
4510 ~ class-name
4511 template-id
4513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514 keyword, in a construct like `A::template ...'.
4516 Returns a representation of unqualified-id. For the `identifier'
4517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4518 production a BIT_NOT_EXPR is returned; the operand of the
4519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4520 other productions, see the documentation accompanying the
4521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4522 names are looked up in uninstantiated templates. If DECLARATOR_P
4523 is true, the unqualified-id is appearing as part of a declarator,
4524 rather than as part of an expression. */
4526 static tree
4527 cp_parser_unqualified_id (cp_parser* parser,
4528 bool template_keyword_p,
4529 bool check_dependency_p,
4530 bool declarator_p,
4531 bool optional_p)
4533 cp_token *token;
4535 /* Peek at the next token. */
4536 token = cp_lexer_peek_token (parser->lexer);
4538 switch (token->type)
4540 case CPP_NAME:
4542 tree id;
4544 /* We don't know yet whether or not this will be a
4545 template-id. */
4546 cp_parser_parse_tentatively (parser);
4547 /* Try a template-id. */
4548 id = cp_parser_template_id (parser, template_keyword_p,
4549 check_dependency_p,
4550 none_type,
4551 declarator_p);
4552 /* If it worked, we're done. */
4553 if (cp_parser_parse_definitely (parser))
4554 return id;
4555 /* Otherwise, it's an ordinary identifier. */
4556 return cp_parser_identifier (parser);
4559 case CPP_TEMPLATE_ID:
4560 return cp_parser_template_id (parser, template_keyword_p,
4561 check_dependency_p,
4562 none_type,
4563 declarator_p);
4565 case CPP_COMPL:
4567 tree type_decl;
4568 tree qualifying_scope;
4569 tree object_scope;
4570 tree scope;
4571 bool done;
4573 /* Consume the `~' token. */
4574 cp_lexer_consume_token (parser->lexer);
4575 /* Parse the class-name. The standard, as written, seems to
4576 say that:
4578 template <typename T> struct S { ~S (); };
4579 template <typename T> S<T>::~S() {}
4581 is invalid, since `~' must be followed by a class-name, but
4582 `S<T>' is dependent, and so not known to be a class.
4583 That's not right; we need to look in uninstantiated
4584 templates. A further complication arises from:
4586 template <typename T> void f(T t) {
4587 t.T::~T();
4590 Here, it is not possible to look up `T' in the scope of `T'
4591 itself. We must look in both the current scope, and the
4592 scope of the containing complete expression.
4594 Yet another issue is:
4596 struct S {
4597 int S;
4598 ~S();
4601 S::~S() {}
4603 The standard does not seem to say that the `S' in `~S'
4604 should refer to the type `S' and not the data member
4605 `S::S'. */
4607 /* DR 244 says that we look up the name after the "~" in the
4608 same scope as we looked up the qualifying name. That idea
4609 isn't fully worked out; it's more complicated than that. */
4610 scope = parser->scope;
4611 object_scope = parser->object_scope;
4612 qualifying_scope = parser->qualifying_scope;
4614 /* Check for invalid scopes. */
4615 if (scope == error_mark_node)
4617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4618 cp_lexer_consume_token (parser->lexer);
4619 return error_mark_node;
4621 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4624 error_at (token->location,
4625 "scope %qT before %<~%> is not a class-name",
4626 scope);
4627 cp_parser_simulate_error (parser);
4628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4629 cp_lexer_consume_token (parser->lexer);
4630 return error_mark_node;
4632 gcc_assert (!scope || TYPE_P (scope));
4634 /* If the name is of the form "X::~X" it's OK even if X is a
4635 typedef. */
4636 token = cp_lexer_peek_token (parser->lexer);
4637 if (scope
4638 && token->type == CPP_NAME
4639 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4640 != CPP_LESS)
4641 && (token->u.value == TYPE_IDENTIFIER (scope)
4642 || (CLASS_TYPE_P (scope)
4643 && constructor_name_p (token->u.value, scope))))
4645 cp_lexer_consume_token (parser->lexer);
4646 return build_nt (BIT_NOT_EXPR, scope);
4649 /* If there was an explicit qualification (S::~T), first look
4650 in the scope given by the qualification (i.e., S).
4652 Note: in the calls to cp_parser_class_name below we pass
4653 typename_type so that lookup finds the injected-class-name
4654 rather than the constructor. */
4655 done = false;
4656 type_decl = NULL_TREE;
4657 if (scope)
4659 cp_parser_parse_tentatively (parser);
4660 type_decl = cp_parser_class_name (parser,
4661 /*typename_keyword_p=*/false,
4662 /*template_keyword_p=*/false,
4663 typename_type,
4664 /*check_dependency=*/false,
4665 /*class_head_p=*/false,
4666 declarator_p);
4667 if (cp_parser_parse_definitely (parser))
4668 done = true;
4670 /* In "N::S::~S", look in "N" as well. */
4671 if (!done && scope && qualifying_scope)
4673 cp_parser_parse_tentatively (parser);
4674 parser->scope = qualifying_scope;
4675 parser->object_scope = NULL_TREE;
4676 parser->qualifying_scope = NULL_TREE;
4677 type_decl
4678 = cp_parser_class_name (parser,
4679 /*typename_keyword_p=*/false,
4680 /*template_keyword_p=*/false,
4681 typename_type,
4682 /*check_dependency=*/false,
4683 /*class_head_p=*/false,
4684 declarator_p);
4685 if (cp_parser_parse_definitely (parser))
4686 done = true;
4688 /* In "p->S::~T", look in the scope given by "*p" as well. */
4689 else if (!done && object_scope)
4691 cp_parser_parse_tentatively (parser);
4692 parser->scope = object_scope;
4693 parser->object_scope = NULL_TREE;
4694 parser->qualifying_scope = NULL_TREE;
4695 type_decl
4696 = cp_parser_class_name (parser,
4697 /*typename_keyword_p=*/false,
4698 /*template_keyword_p=*/false,
4699 typename_type,
4700 /*check_dependency=*/false,
4701 /*class_head_p=*/false,
4702 declarator_p);
4703 if (cp_parser_parse_definitely (parser))
4704 done = true;
4706 /* Look in the surrounding context. */
4707 if (!done)
4709 parser->scope = NULL_TREE;
4710 parser->object_scope = NULL_TREE;
4711 parser->qualifying_scope = NULL_TREE;
4712 if (processing_template_decl)
4713 cp_parser_parse_tentatively (parser);
4714 type_decl
4715 = cp_parser_class_name (parser,
4716 /*typename_keyword_p=*/false,
4717 /*template_keyword_p=*/false,
4718 typename_type,
4719 /*check_dependency=*/false,
4720 /*class_head_p=*/false,
4721 declarator_p);
4722 if (processing_template_decl
4723 && ! cp_parser_parse_definitely (parser))
4725 /* We couldn't find a type with this name, so just accept
4726 it and check for a match at instantiation time. */
4727 type_decl = cp_parser_identifier (parser);
4728 if (type_decl != error_mark_node)
4729 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4730 return type_decl;
4733 /* If an error occurred, assume that the name of the
4734 destructor is the same as the name of the qualifying
4735 class. That allows us to keep parsing after running
4736 into ill-formed destructor names. */
4737 if (type_decl == error_mark_node && scope)
4738 return build_nt (BIT_NOT_EXPR, scope);
4739 else if (type_decl == error_mark_node)
4740 return error_mark_node;
4742 /* Check that destructor name and scope match. */
4743 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4745 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4746 error_at (token->location,
4747 "declaration of %<~%T%> as member of %qT",
4748 type_decl, scope);
4749 cp_parser_simulate_error (parser);
4750 return error_mark_node;
4753 /* [class.dtor]
4755 A typedef-name that names a class shall not be used as the
4756 identifier in the declarator for a destructor declaration. */
4757 if (declarator_p
4758 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4759 && !DECL_SELF_REFERENCE_P (type_decl)
4760 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4761 error_at (token->location,
4762 "typedef-name %qD used as destructor declarator",
4763 type_decl);
4765 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4768 case CPP_KEYWORD:
4769 if (token->keyword == RID_OPERATOR)
4771 tree id;
4773 /* This could be a template-id, so we try that first. */
4774 cp_parser_parse_tentatively (parser);
4775 /* Try a template-id. */
4776 id = cp_parser_template_id (parser, template_keyword_p,
4777 /*check_dependency_p=*/true,
4778 none_type,
4779 declarator_p);
4780 /* If that worked, we're done. */
4781 if (cp_parser_parse_definitely (parser))
4782 return id;
4783 /* We still don't know whether we're looking at an
4784 operator-function-id or a conversion-function-id. */
4785 cp_parser_parse_tentatively (parser);
4786 /* Try an operator-function-id. */
4787 id = cp_parser_operator_function_id (parser);
4788 /* If that didn't work, try a conversion-function-id. */
4789 if (!cp_parser_parse_definitely (parser))
4790 id = cp_parser_conversion_function_id (parser);
4791 else if (UDLIT_OPER_P (id))
4793 /* 17.6.3.3.5 */
4794 const char *name = UDLIT_OP_SUFFIX (id);
4795 if (name[0] != '_' && !in_system_header)
4796 warning (0, "literal operator suffixes not preceded by %<_%>"
4797 " are reserved for future standardization");
4800 return id;
4802 /* Fall through. */
4804 default:
4805 if (optional_p)
4806 return NULL_TREE;
4807 cp_parser_error (parser, "expected unqualified-id");
4808 return error_mark_node;
4812 /* Parse an (optional) nested-name-specifier.
4814 nested-name-specifier: [C++98]
4815 class-or-namespace-name :: nested-name-specifier [opt]
4816 class-or-namespace-name :: template nested-name-specifier [opt]
4818 nested-name-specifier: [C++0x]
4819 type-name ::
4820 namespace-name ::
4821 nested-name-specifier identifier ::
4822 nested-name-specifier template [opt] simple-template-id ::
4824 PARSER->SCOPE should be set appropriately before this function is
4825 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4826 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4827 in name lookups.
4829 Sets PARSER->SCOPE to the class (TYPE) or namespace
4830 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4831 it unchanged if there is no nested-name-specifier. Returns the new
4832 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4834 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4835 part of a declaration and/or decl-specifier. */
4837 static tree
4838 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4839 bool typename_keyword_p,
4840 bool check_dependency_p,
4841 bool type_p,
4842 bool is_declaration)
4844 bool success = false;
4845 cp_token_position start = 0;
4846 cp_token *token;
4848 /* Remember where the nested-name-specifier starts. */
4849 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4851 start = cp_lexer_token_position (parser->lexer, false);
4852 push_deferring_access_checks (dk_deferred);
4855 while (true)
4857 tree new_scope;
4858 tree old_scope;
4859 tree saved_qualifying_scope;
4860 bool template_keyword_p;
4862 /* Spot cases that cannot be the beginning of a
4863 nested-name-specifier. */
4864 token = cp_lexer_peek_token (parser->lexer);
4866 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4867 the already parsed nested-name-specifier. */
4868 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4870 /* Grab the nested-name-specifier and continue the loop. */
4871 cp_parser_pre_parsed_nested_name_specifier (parser);
4872 /* If we originally encountered this nested-name-specifier
4873 with IS_DECLARATION set to false, we will not have
4874 resolved TYPENAME_TYPEs, so we must do so here. */
4875 if (is_declaration
4876 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4878 new_scope = resolve_typename_type (parser->scope,
4879 /*only_current_p=*/false);
4880 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4881 parser->scope = new_scope;
4883 success = true;
4884 continue;
4887 /* Spot cases that cannot be the beginning of a
4888 nested-name-specifier. On the second and subsequent times
4889 through the loop, we look for the `template' keyword. */
4890 if (success && token->keyword == RID_TEMPLATE)
4892 /* A template-id can start a nested-name-specifier. */
4893 else if (token->type == CPP_TEMPLATE_ID)
4895 /* DR 743: decltype can be used in a nested-name-specifier. */
4896 else if (token_is_decltype (token))
4898 else
4900 /* If the next token is not an identifier, then it is
4901 definitely not a type-name or namespace-name. */
4902 if (token->type != CPP_NAME)
4903 break;
4904 /* If the following token is neither a `<' (to begin a
4905 template-id), nor a `::', then we are not looking at a
4906 nested-name-specifier. */
4907 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4909 if (token->type == CPP_COLON
4910 && parser->colon_corrects_to_scope_p
4911 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4913 error_at (token->location,
4914 "found %<:%> in nested-name-specifier, expected %<::%>");
4915 token->type = CPP_SCOPE;
4918 if (token->type != CPP_SCOPE
4919 && !cp_parser_nth_token_starts_template_argument_list_p
4920 (parser, 2))
4921 break;
4924 /* The nested-name-specifier is optional, so we parse
4925 tentatively. */
4926 cp_parser_parse_tentatively (parser);
4928 /* Look for the optional `template' keyword, if this isn't the
4929 first time through the loop. */
4930 if (success)
4931 template_keyword_p = cp_parser_optional_template_keyword (parser);
4932 else
4933 template_keyword_p = false;
4935 /* Save the old scope since the name lookup we are about to do
4936 might destroy it. */
4937 old_scope = parser->scope;
4938 saved_qualifying_scope = parser->qualifying_scope;
4939 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4940 look up names in "X<T>::I" in order to determine that "Y" is
4941 a template. So, if we have a typename at this point, we make
4942 an effort to look through it. */
4943 if (is_declaration
4944 && !typename_keyword_p
4945 && parser->scope
4946 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4947 parser->scope = resolve_typename_type (parser->scope,
4948 /*only_current_p=*/false);
4949 /* Parse the qualifying entity. */
4950 new_scope
4951 = cp_parser_qualifying_entity (parser,
4952 typename_keyword_p,
4953 template_keyword_p,
4954 check_dependency_p,
4955 type_p,
4956 is_declaration);
4957 /* Look for the `::' token. */
4958 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4960 /* If we found what we wanted, we keep going; otherwise, we're
4961 done. */
4962 if (!cp_parser_parse_definitely (parser))
4964 bool error_p = false;
4966 /* Restore the OLD_SCOPE since it was valid before the
4967 failed attempt at finding the last
4968 class-or-namespace-name. */
4969 parser->scope = old_scope;
4970 parser->qualifying_scope = saved_qualifying_scope;
4972 /* If the next token is a decltype, and the one after that is a
4973 `::', then the decltype has failed to resolve to a class or
4974 enumeration type. Give this error even when parsing
4975 tentatively since it can't possibly be valid--and we're going
4976 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4977 won't get another chance.*/
4978 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4979 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4980 == CPP_SCOPE))
4982 token = cp_lexer_consume_token (parser->lexer);
4983 error_at (token->location, "decltype evaluates to %qT, "
4984 "which is not a class or enumeration type",
4985 token->u.value);
4986 parser->scope = error_mark_node;
4987 error_p = true;
4988 /* As below. */
4989 success = true;
4990 cp_lexer_consume_token (parser->lexer);
4993 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4994 break;
4995 /* If the next token is an identifier, and the one after
4996 that is a `::', then any valid interpretation would have
4997 found a class-or-namespace-name. */
4998 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4999 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5000 == CPP_SCOPE)
5001 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5002 != CPP_COMPL))
5004 token = cp_lexer_consume_token (parser->lexer);
5005 if (!error_p)
5007 if (!token->ambiguous_p)
5009 tree decl;
5010 tree ambiguous_decls;
5012 decl = cp_parser_lookup_name (parser, token->u.value,
5013 none_type,
5014 /*is_template=*/false,
5015 /*is_namespace=*/false,
5016 /*check_dependency=*/true,
5017 &ambiguous_decls,
5018 token->location);
5019 if (TREE_CODE (decl) == TEMPLATE_DECL)
5020 error_at (token->location,
5021 "%qD used without template parameters",
5022 decl);
5023 else if (ambiguous_decls)
5025 error_at (token->location,
5026 "reference to %qD is ambiguous",
5027 token->u.value);
5028 print_candidates (ambiguous_decls);
5029 decl = error_mark_node;
5031 else
5033 if (cxx_dialect != cxx98)
5034 cp_parser_name_lookup_error
5035 (parser, token->u.value, decl, NLE_NOT_CXX98,
5036 token->location);
5037 else
5038 cp_parser_name_lookup_error
5039 (parser, token->u.value, decl, NLE_CXX98,
5040 token->location);
5043 parser->scope = error_mark_node;
5044 error_p = true;
5045 /* Treat this as a successful nested-name-specifier
5046 due to:
5048 [basic.lookup.qual]
5050 If the name found is not a class-name (clause
5051 _class_) or namespace-name (_namespace.def_), the
5052 program is ill-formed. */
5053 success = true;
5055 cp_lexer_consume_token (parser->lexer);
5057 break;
5059 /* We've found one valid nested-name-specifier. */
5060 success = true;
5061 /* Name lookup always gives us a DECL. */
5062 if (TREE_CODE (new_scope) == TYPE_DECL)
5063 new_scope = TREE_TYPE (new_scope);
5064 /* Uses of "template" must be followed by actual templates. */
5065 if (template_keyword_p
5066 && !(CLASS_TYPE_P (new_scope)
5067 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5068 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5069 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5070 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5071 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5072 == TEMPLATE_ID_EXPR)))
5073 permerror (input_location, TYPE_P (new_scope)
5074 ? G_("%qT is not a template")
5075 : G_("%qD is not a template"),
5076 new_scope);
5077 /* If it is a class scope, try to complete it; we are about to
5078 be looking up names inside the class. */
5079 if (TYPE_P (new_scope)
5080 /* Since checking types for dependency can be expensive,
5081 avoid doing it if the type is already complete. */
5082 && !COMPLETE_TYPE_P (new_scope)
5083 /* Do not try to complete dependent types. */
5084 && !dependent_type_p (new_scope))
5086 new_scope = complete_type (new_scope);
5087 /* If it is a typedef to current class, use the current
5088 class instead, as the typedef won't have any names inside
5089 it yet. */
5090 if (!COMPLETE_TYPE_P (new_scope)
5091 && currently_open_class (new_scope))
5092 new_scope = TYPE_MAIN_VARIANT (new_scope);
5094 /* Make sure we look in the right scope the next time through
5095 the loop. */
5096 parser->scope = new_scope;
5099 /* If parsing tentatively, replace the sequence of tokens that makes
5100 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5101 token. That way, should we re-parse the token stream, we will
5102 not have to repeat the effort required to do the parse, nor will
5103 we issue duplicate error messages. */
5104 if (success && start)
5106 cp_token *token;
5108 token = cp_lexer_token_at (parser->lexer, start);
5109 /* Reset the contents of the START token. */
5110 token->type = CPP_NESTED_NAME_SPECIFIER;
5111 /* Retrieve any deferred checks. Do not pop this access checks yet
5112 so the memory will not be reclaimed during token replacing below. */
5113 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5114 token->u.tree_check_value->value = parser->scope;
5115 token->u.tree_check_value->checks = get_deferred_access_checks ();
5116 token->u.tree_check_value->qualifying_scope =
5117 parser->qualifying_scope;
5118 token->keyword = RID_MAX;
5120 /* Purge all subsequent tokens. */
5121 cp_lexer_purge_tokens_after (parser->lexer, start);
5124 if (start)
5125 pop_to_parent_deferring_access_checks ();
5127 return success ? parser->scope : NULL_TREE;
5130 /* Parse a nested-name-specifier. See
5131 cp_parser_nested_name_specifier_opt for details. This function
5132 behaves identically, except that it will an issue an error if no
5133 nested-name-specifier is present. */
5135 static tree
5136 cp_parser_nested_name_specifier (cp_parser *parser,
5137 bool typename_keyword_p,
5138 bool check_dependency_p,
5139 bool type_p,
5140 bool is_declaration)
5142 tree scope;
5144 /* Look for the nested-name-specifier. */
5145 scope = cp_parser_nested_name_specifier_opt (parser,
5146 typename_keyword_p,
5147 check_dependency_p,
5148 type_p,
5149 is_declaration);
5150 /* If it was not present, issue an error message. */
5151 if (!scope)
5153 cp_parser_error (parser, "expected nested-name-specifier");
5154 parser->scope = NULL_TREE;
5157 return scope;
5160 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5161 this is either a class-name or a namespace-name (which corresponds
5162 to the class-or-namespace-name production in the grammar). For
5163 C++0x, it can also be a type-name that refers to an enumeration
5164 type or a simple-template-id.
5166 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5167 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5168 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5169 TYPE_P is TRUE iff the next name should be taken as a class-name,
5170 even the same name is declared to be another entity in the same
5171 scope.
5173 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5174 specified by the class-or-namespace-name. If neither is found the
5175 ERROR_MARK_NODE is returned. */
5177 static tree
5178 cp_parser_qualifying_entity (cp_parser *parser,
5179 bool typename_keyword_p,
5180 bool template_keyword_p,
5181 bool check_dependency_p,
5182 bool type_p,
5183 bool is_declaration)
5185 tree saved_scope;
5186 tree saved_qualifying_scope;
5187 tree saved_object_scope;
5188 tree scope;
5189 bool only_class_p;
5190 bool successful_parse_p;
5192 /* DR 743: decltype can appear in a nested-name-specifier. */
5193 if (cp_lexer_next_token_is_decltype (parser->lexer))
5195 scope = cp_parser_decltype (parser);
5196 if (TREE_CODE (scope) != ENUMERAL_TYPE
5197 && !MAYBE_CLASS_TYPE_P (scope))
5199 cp_parser_simulate_error (parser);
5200 return error_mark_node;
5202 if (TYPE_NAME (scope))
5203 scope = TYPE_NAME (scope);
5204 return scope;
5207 /* Before we try to parse the class-name, we must save away the
5208 current PARSER->SCOPE since cp_parser_class_name will destroy
5209 it. */
5210 saved_scope = parser->scope;
5211 saved_qualifying_scope = parser->qualifying_scope;
5212 saved_object_scope = parser->object_scope;
5213 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5214 there is no need to look for a namespace-name. */
5215 only_class_p = template_keyword_p
5216 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5217 if (!only_class_p)
5218 cp_parser_parse_tentatively (parser);
5219 scope = cp_parser_class_name (parser,
5220 typename_keyword_p,
5221 template_keyword_p,
5222 type_p ? class_type : none_type,
5223 check_dependency_p,
5224 /*class_head_p=*/false,
5225 is_declaration);
5226 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5227 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5228 if (!only_class_p
5229 && cxx_dialect != cxx98
5230 && !successful_parse_p)
5232 /* Restore the saved scope. */
5233 parser->scope = saved_scope;
5234 parser->qualifying_scope = saved_qualifying_scope;
5235 parser->object_scope = saved_object_scope;
5237 /* Parse tentatively. */
5238 cp_parser_parse_tentatively (parser);
5240 /* Parse a type-name */
5241 scope = cp_parser_type_name (parser);
5243 /* "If the name found does not designate a namespace or a class,
5244 enumeration, or dependent type, the program is ill-formed."
5246 We cover classes and dependent types above and namespaces below,
5247 so this code is only looking for enums. */
5248 if (!scope || TREE_CODE (scope) != TYPE_DECL
5249 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5250 cp_parser_simulate_error (parser);
5252 successful_parse_p = cp_parser_parse_definitely (parser);
5254 /* If that didn't work, try for a namespace-name. */
5255 if (!only_class_p && !successful_parse_p)
5257 /* Restore the saved scope. */
5258 parser->scope = saved_scope;
5259 parser->qualifying_scope = saved_qualifying_scope;
5260 parser->object_scope = saved_object_scope;
5261 /* If we are not looking at an identifier followed by the scope
5262 resolution operator, then this is not part of a
5263 nested-name-specifier. (Note that this function is only used
5264 to parse the components of a nested-name-specifier.) */
5265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5266 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5267 return error_mark_node;
5268 scope = cp_parser_namespace_name (parser);
5271 return scope;
5274 /* Parse a postfix-expression.
5276 postfix-expression:
5277 primary-expression
5278 postfix-expression [ expression ]
5279 postfix-expression ( expression-list [opt] )
5280 simple-type-specifier ( expression-list [opt] )
5281 typename :: [opt] nested-name-specifier identifier
5282 ( expression-list [opt] )
5283 typename :: [opt] nested-name-specifier template [opt] template-id
5284 ( expression-list [opt] )
5285 postfix-expression . template [opt] id-expression
5286 postfix-expression -> template [opt] id-expression
5287 postfix-expression . pseudo-destructor-name
5288 postfix-expression -> pseudo-destructor-name
5289 postfix-expression ++
5290 postfix-expression --
5291 dynamic_cast < type-id > ( expression )
5292 static_cast < type-id > ( expression )
5293 reinterpret_cast < type-id > ( expression )
5294 const_cast < type-id > ( expression )
5295 typeid ( expression )
5296 typeid ( type-id )
5298 GNU Extension:
5300 postfix-expression:
5301 ( type-id ) { initializer-list , [opt] }
5303 This extension is a GNU version of the C99 compound-literal
5304 construct. (The C99 grammar uses `type-name' instead of `type-id',
5305 but they are essentially the same concept.)
5307 If ADDRESS_P is true, the postfix expression is the operand of the
5308 `&' operator. CAST_P is true if this expression is the target of a
5309 cast.
5311 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5312 class member access expressions [expr.ref].
5314 Returns a representation of the expression. */
5316 static tree
5317 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5318 bool member_access_only_p,
5319 cp_id_kind * pidk_return)
5321 cp_token *token;
5322 enum rid keyword;
5323 cp_id_kind idk = CP_ID_KIND_NONE;
5324 tree postfix_expression = NULL_TREE;
5325 bool is_member_access = false;
5327 /* Peek at the next token. */
5328 token = cp_lexer_peek_token (parser->lexer);
5329 /* Some of the productions are determined by keywords. */
5330 keyword = token->keyword;
5331 switch (keyword)
5333 case RID_DYNCAST:
5334 case RID_STATCAST:
5335 case RID_REINTCAST:
5336 case RID_CONSTCAST:
5338 tree type;
5339 tree expression;
5340 const char *saved_message;
5342 /* All of these can be handled in the same way from the point
5343 of view of parsing. Begin by consuming the token
5344 identifying the cast. */
5345 cp_lexer_consume_token (parser->lexer);
5347 /* New types cannot be defined in the cast. */
5348 saved_message = parser->type_definition_forbidden_message;
5349 parser->type_definition_forbidden_message
5350 = G_("types may not be defined in casts");
5352 /* Look for the opening `<'. */
5353 cp_parser_require (parser, CPP_LESS, RT_LESS);
5354 /* Parse the type to which we are casting. */
5355 type = cp_parser_type_id (parser);
5356 /* Look for the closing `>'. */
5357 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5358 /* Restore the old message. */
5359 parser->type_definition_forbidden_message = saved_message;
5361 /* And the expression which is being cast. */
5362 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5363 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5364 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5366 /* Only type conversions to integral or enumeration types
5367 can be used in constant-expressions. */
5368 if (!cast_valid_in_integral_constant_expression_p (type)
5369 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5370 return error_mark_node;
5372 switch (keyword)
5374 case RID_DYNCAST:
5375 postfix_expression
5376 = build_dynamic_cast (type, expression, tf_warning_or_error);
5377 break;
5378 case RID_STATCAST:
5379 postfix_expression
5380 = build_static_cast (type, expression, tf_warning_or_error);
5381 break;
5382 case RID_REINTCAST:
5383 postfix_expression
5384 = build_reinterpret_cast (type, expression,
5385 tf_warning_or_error);
5386 break;
5387 case RID_CONSTCAST:
5388 postfix_expression
5389 = build_const_cast (type, expression, tf_warning_or_error);
5390 break;
5391 default:
5392 gcc_unreachable ();
5395 break;
5397 case RID_TYPEID:
5399 tree type;
5400 const char *saved_message;
5401 bool saved_in_type_id_in_expr_p;
5403 /* Consume the `typeid' token. */
5404 cp_lexer_consume_token (parser->lexer);
5405 /* Look for the `(' token. */
5406 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5407 /* Types cannot be defined in a `typeid' expression. */
5408 saved_message = parser->type_definition_forbidden_message;
5409 parser->type_definition_forbidden_message
5410 = G_("types may not be defined in a %<typeid%> expression");
5411 /* We can't be sure yet whether we're looking at a type-id or an
5412 expression. */
5413 cp_parser_parse_tentatively (parser);
5414 /* Try a type-id first. */
5415 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5416 parser->in_type_id_in_expr_p = true;
5417 type = cp_parser_type_id (parser);
5418 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5419 /* Look for the `)' token. Otherwise, we can't be sure that
5420 we're not looking at an expression: consider `typeid (int
5421 (3))', for example. */
5422 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5423 /* If all went well, simply lookup the type-id. */
5424 if (cp_parser_parse_definitely (parser))
5425 postfix_expression = get_typeid (type);
5426 /* Otherwise, fall back to the expression variant. */
5427 else
5429 tree expression;
5431 /* Look for an expression. */
5432 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5433 /* Compute its typeid. */
5434 postfix_expression = build_typeid (expression);
5435 /* Look for the `)' token. */
5436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5438 /* Restore the saved message. */
5439 parser->type_definition_forbidden_message = saved_message;
5440 /* `typeid' may not appear in an integral constant expression. */
5441 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5442 return error_mark_node;
5444 break;
5446 case RID_TYPENAME:
5448 tree type;
5449 /* The syntax permitted here is the same permitted for an
5450 elaborated-type-specifier. */
5451 type = cp_parser_elaborated_type_specifier (parser,
5452 /*is_friend=*/false,
5453 /*is_declaration=*/false);
5454 postfix_expression = cp_parser_functional_cast (parser, type);
5456 break;
5458 case RID_BUILTIN_SHUFFLE:
5460 VEC(tree,gc)* vec;
5461 unsigned int i;
5462 tree p;
5463 location_t loc = token->location;
5465 cp_lexer_consume_token (parser->lexer);
5466 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5467 /*cast_p=*/false, /*allow_expansion_p=*/true,
5468 /*non_constant_p=*/NULL);
5469 if (vec == NULL)
5470 return error_mark_node;
5472 FOR_EACH_VEC_ELT (tree, vec, i, p)
5473 mark_exp_read (p);
5475 if (VEC_length (tree, vec) == 2)
5476 return
5477 c_build_vec_perm_expr
5478 (loc, VEC_index (tree, vec, 0),
5479 NULL_TREE, VEC_index (tree, vec, 1));
5481 else if (VEC_length (tree, vec) == 3)
5482 return
5483 c_build_vec_perm_expr
5484 (loc, VEC_index (tree, vec, 0),
5485 VEC_index (tree, vec, 1),
5486 VEC_index (tree, vec, 2));
5487 else
5489 error_at (loc, "wrong number of arguments to "
5490 "%<__builtin_shuffle%>");
5491 return error_mark_node;
5493 break;
5496 default:
5498 tree type;
5500 /* If the next thing is a simple-type-specifier, we may be
5501 looking at a functional cast. We could also be looking at
5502 an id-expression. So, we try the functional cast, and if
5503 that doesn't work we fall back to the primary-expression. */
5504 cp_parser_parse_tentatively (parser);
5505 /* Look for the simple-type-specifier. */
5506 type = cp_parser_simple_type_specifier (parser,
5507 /*decl_specs=*/NULL,
5508 CP_PARSER_FLAGS_NONE);
5509 /* Parse the cast itself. */
5510 if (!cp_parser_error_occurred (parser))
5511 postfix_expression
5512 = cp_parser_functional_cast (parser, type);
5513 /* If that worked, we're done. */
5514 if (cp_parser_parse_definitely (parser))
5515 break;
5517 /* If the functional-cast didn't work out, try a
5518 compound-literal. */
5519 if (cp_parser_allow_gnu_extensions_p (parser)
5520 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5522 VEC(constructor_elt,gc) *initializer_list = NULL;
5523 bool saved_in_type_id_in_expr_p;
5525 cp_parser_parse_tentatively (parser);
5526 /* Consume the `('. */
5527 cp_lexer_consume_token (parser->lexer);
5528 /* Parse the type. */
5529 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5530 parser->in_type_id_in_expr_p = true;
5531 type = cp_parser_type_id (parser);
5532 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5533 /* Look for the `)'. */
5534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5535 /* Look for the `{'. */
5536 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5537 /* If things aren't going well, there's no need to
5538 keep going. */
5539 if (!cp_parser_error_occurred (parser))
5541 bool non_constant_p;
5542 /* Parse the initializer-list. */
5543 initializer_list
5544 = cp_parser_initializer_list (parser, &non_constant_p);
5545 /* Allow a trailing `,'. */
5546 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5547 cp_lexer_consume_token (parser->lexer);
5548 /* Look for the final `}'. */
5549 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5551 /* If that worked, we're definitely looking at a
5552 compound-literal expression. */
5553 if (cp_parser_parse_definitely (parser))
5555 /* Warn the user that a compound literal is not
5556 allowed in standard C++. */
5557 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5558 /* For simplicity, we disallow compound literals in
5559 constant-expressions. We could
5560 allow compound literals of integer type, whose
5561 initializer was a constant, in constant
5562 expressions. Permitting that usage, as a further
5563 extension, would not change the meaning of any
5564 currently accepted programs. (Of course, as
5565 compound literals are not part of ISO C++, the
5566 standard has nothing to say.) */
5567 if (cp_parser_non_integral_constant_expression (parser,
5568 NIC_NCC))
5570 postfix_expression = error_mark_node;
5571 break;
5573 /* Form the representation of the compound-literal. */
5574 postfix_expression
5575 = (finish_compound_literal
5576 (type, build_constructor (init_list_type_node,
5577 initializer_list),
5578 tf_warning_or_error));
5579 break;
5583 /* It must be a primary-expression. */
5584 postfix_expression
5585 = cp_parser_primary_expression (parser, address_p, cast_p,
5586 /*template_arg_p=*/false,
5587 &idk);
5589 break;
5592 /* Keep looping until the postfix-expression is complete. */
5593 while (true)
5595 if (idk == CP_ID_KIND_UNQUALIFIED
5596 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5597 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5598 /* It is not a Koenig lookup function call. */
5599 postfix_expression
5600 = unqualified_name_lookup_error (postfix_expression);
5602 /* Peek at the next token. */
5603 token = cp_lexer_peek_token (parser->lexer);
5605 switch (token->type)
5607 case CPP_OPEN_SQUARE:
5608 postfix_expression
5609 = cp_parser_postfix_open_square_expression (parser,
5610 postfix_expression,
5611 false);
5612 idk = CP_ID_KIND_NONE;
5613 is_member_access = false;
5614 break;
5616 case CPP_OPEN_PAREN:
5617 /* postfix-expression ( expression-list [opt] ) */
5619 bool koenig_p;
5620 bool is_builtin_constant_p;
5621 bool saved_integral_constant_expression_p = false;
5622 bool saved_non_integral_constant_expression_p = false;
5623 VEC(tree,gc) *args;
5625 is_member_access = false;
5627 is_builtin_constant_p
5628 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5629 if (is_builtin_constant_p)
5631 /* The whole point of __builtin_constant_p is to allow
5632 non-constant expressions to appear as arguments. */
5633 saved_integral_constant_expression_p
5634 = parser->integral_constant_expression_p;
5635 saved_non_integral_constant_expression_p
5636 = parser->non_integral_constant_expression_p;
5637 parser->integral_constant_expression_p = false;
5639 args = (cp_parser_parenthesized_expression_list
5640 (parser, non_attr,
5641 /*cast_p=*/false, /*allow_expansion_p=*/true,
5642 /*non_constant_p=*/NULL));
5643 if (is_builtin_constant_p)
5645 parser->integral_constant_expression_p
5646 = saved_integral_constant_expression_p;
5647 parser->non_integral_constant_expression_p
5648 = saved_non_integral_constant_expression_p;
5651 if (args == NULL)
5653 postfix_expression = error_mark_node;
5654 break;
5657 /* Function calls are not permitted in
5658 constant-expressions. */
5659 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5660 && cp_parser_non_integral_constant_expression (parser,
5661 NIC_FUNC_CALL))
5663 postfix_expression = error_mark_node;
5664 release_tree_vector (args);
5665 break;
5668 koenig_p = false;
5669 if (idk == CP_ID_KIND_UNQUALIFIED
5670 || idk == CP_ID_KIND_TEMPLATE_ID)
5672 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5674 if (!VEC_empty (tree, args))
5676 koenig_p = true;
5677 if (!any_type_dependent_arguments_p (args))
5678 postfix_expression
5679 = perform_koenig_lookup (postfix_expression, args,
5680 /*include_std=*/false,
5681 tf_warning_or_error);
5683 else
5684 postfix_expression
5685 = unqualified_fn_lookup_error (postfix_expression);
5687 /* We do not perform argument-dependent lookup if
5688 normal lookup finds a non-function, in accordance
5689 with the expected resolution of DR 218. */
5690 else if (!VEC_empty (tree, args)
5691 && is_overloaded_fn (postfix_expression))
5693 tree fn = get_first_fn (postfix_expression);
5694 fn = STRIP_TEMPLATE (fn);
5696 /* Do not do argument dependent lookup if regular
5697 lookup finds a member function or a block-scope
5698 function declaration. [basic.lookup.argdep]/3 */
5699 if (!DECL_FUNCTION_MEMBER_P (fn)
5700 && !DECL_LOCAL_FUNCTION_P (fn))
5702 koenig_p = true;
5703 if (!any_type_dependent_arguments_p (args))
5704 postfix_expression
5705 = perform_koenig_lookup (postfix_expression, args,
5706 /*include_std=*/false,
5707 tf_warning_or_error);
5712 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5714 tree instance = TREE_OPERAND (postfix_expression, 0);
5715 tree fn = TREE_OPERAND (postfix_expression, 1);
5717 if (processing_template_decl
5718 && (type_dependent_expression_p (instance)
5719 || (!BASELINK_P (fn)
5720 && TREE_CODE (fn) != FIELD_DECL)
5721 || type_dependent_expression_p (fn)
5722 || any_type_dependent_arguments_p (args)))
5724 postfix_expression
5725 = build_nt_call_vec (postfix_expression, args);
5726 release_tree_vector (args);
5727 break;
5730 if (BASELINK_P (fn))
5732 postfix_expression
5733 = (build_new_method_call
5734 (instance, fn, &args, NULL_TREE,
5735 (idk == CP_ID_KIND_QUALIFIED
5736 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5737 : LOOKUP_NORMAL),
5738 /*fn_p=*/NULL,
5739 tf_warning_or_error));
5741 else
5742 postfix_expression
5743 = finish_call_expr (postfix_expression, &args,
5744 /*disallow_virtual=*/false,
5745 /*koenig_p=*/false,
5746 tf_warning_or_error);
5748 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5749 || TREE_CODE (postfix_expression) == MEMBER_REF
5750 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5751 postfix_expression = (build_offset_ref_call_from_tree
5752 (postfix_expression, &args,
5753 tf_warning_or_error));
5754 else if (idk == CP_ID_KIND_QUALIFIED)
5755 /* A call to a static class member, or a namespace-scope
5756 function. */
5757 postfix_expression
5758 = finish_call_expr (postfix_expression, &args,
5759 /*disallow_virtual=*/true,
5760 koenig_p,
5761 tf_warning_or_error);
5762 else
5763 /* All other function calls. */
5764 postfix_expression
5765 = finish_call_expr (postfix_expression, &args,
5766 /*disallow_virtual=*/false,
5767 koenig_p,
5768 tf_warning_or_error);
5770 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5771 idk = CP_ID_KIND_NONE;
5773 release_tree_vector (args);
5775 break;
5777 case CPP_DOT:
5778 case CPP_DEREF:
5779 /* postfix-expression . template [opt] id-expression
5780 postfix-expression . pseudo-destructor-name
5781 postfix-expression -> template [opt] id-expression
5782 postfix-expression -> pseudo-destructor-name */
5784 /* Consume the `.' or `->' operator. */
5785 cp_lexer_consume_token (parser->lexer);
5787 postfix_expression
5788 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5789 postfix_expression,
5790 false, &idk,
5791 token->location);
5793 is_member_access = true;
5794 break;
5796 case CPP_PLUS_PLUS:
5797 /* postfix-expression ++ */
5798 /* Consume the `++' token. */
5799 cp_lexer_consume_token (parser->lexer);
5800 /* Generate a representation for the complete expression. */
5801 postfix_expression
5802 = finish_increment_expr (postfix_expression,
5803 POSTINCREMENT_EXPR);
5804 /* Increments may not appear in constant-expressions. */
5805 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5806 postfix_expression = error_mark_node;
5807 idk = CP_ID_KIND_NONE;
5808 is_member_access = false;
5809 break;
5811 case CPP_MINUS_MINUS:
5812 /* postfix-expression -- */
5813 /* Consume the `--' token. */
5814 cp_lexer_consume_token (parser->lexer);
5815 /* Generate a representation for the complete expression. */
5816 postfix_expression
5817 = finish_increment_expr (postfix_expression,
5818 POSTDECREMENT_EXPR);
5819 /* Decrements may not appear in constant-expressions. */
5820 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5821 postfix_expression = error_mark_node;
5822 idk = CP_ID_KIND_NONE;
5823 is_member_access = false;
5824 break;
5826 default:
5827 if (pidk_return != NULL)
5828 * pidk_return = idk;
5829 if (member_access_only_p)
5830 return is_member_access? postfix_expression : error_mark_node;
5831 else
5832 return postfix_expression;
5836 /* We should never get here. */
5837 gcc_unreachable ();
5838 return error_mark_node;
5841 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5842 by cp_parser_builtin_offsetof. We're looking for
5844 postfix-expression [ expression ]
5845 postfix-expression [ braced-init-list ] (C++11)
5847 FOR_OFFSETOF is set if we're being called in that context, which
5848 changes how we deal with integer constant expressions. */
5850 static tree
5851 cp_parser_postfix_open_square_expression (cp_parser *parser,
5852 tree postfix_expression,
5853 bool for_offsetof)
5855 tree index;
5856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5858 /* Consume the `[' token. */
5859 cp_lexer_consume_token (parser->lexer);
5861 /* Parse the index expression. */
5862 /* ??? For offsetof, there is a question of what to allow here. If
5863 offsetof is not being used in an integral constant expression context,
5864 then we *could* get the right answer by computing the value at runtime.
5865 If we are in an integral constant expression context, then we might
5866 could accept any constant expression; hard to say without analysis.
5867 Rather than open the barn door too wide right away, allow only integer
5868 constant expressions here. */
5869 if (for_offsetof)
5870 index = cp_parser_constant_expression (parser, false, NULL);
5871 else
5873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5875 bool expr_nonconst_p;
5876 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5877 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5879 else
5880 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5883 /* Look for the closing `]'. */
5884 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5886 /* Build the ARRAY_REF. */
5887 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5889 /* When not doing offsetof, array references are not permitted in
5890 constant-expressions. */
5891 if (!for_offsetof
5892 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5893 postfix_expression = error_mark_node;
5895 return postfix_expression;
5898 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5899 by cp_parser_builtin_offsetof. We're looking for
5901 postfix-expression . template [opt] id-expression
5902 postfix-expression . pseudo-destructor-name
5903 postfix-expression -> template [opt] id-expression
5904 postfix-expression -> pseudo-destructor-name
5906 FOR_OFFSETOF is set if we're being called in that context. That sorta
5907 limits what of the above we'll actually accept, but nevermind.
5908 TOKEN_TYPE is the "." or "->" token, which will already have been
5909 removed from the stream. */
5911 static tree
5912 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5913 enum cpp_ttype token_type,
5914 tree postfix_expression,
5915 bool for_offsetof, cp_id_kind *idk,
5916 location_t location)
5918 tree name;
5919 bool dependent_p;
5920 bool pseudo_destructor_p;
5921 tree scope = NULL_TREE;
5923 /* If this is a `->' operator, dereference the pointer. */
5924 if (token_type == CPP_DEREF)
5925 postfix_expression = build_x_arrow (location, postfix_expression,
5926 tf_warning_or_error);
5927 /* Check to see whether or not the expression is type-dependent. */
5928 dependent_p = type_dependent_expression_p (postfix_expression);
5929 /* The identifier following the `->' or `.' is not qualified. */
5930 parser->scope = NULL_TREE;
5931 parser->qualifying_scope = NULL_TREE;
5932 parser->object_scope = NULL_TREE;
5933 *idk = CP_ID_KIND_NONE;
5935 /* Enter the scope corresponding to the type of the object
5936 given by the POSTFIX_EXPRESSION. */
5937 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5939 scope = TREE_TYPE (postfix_expression);
5940 /* According to the standard, no expression should ever have
5941 reference type. Unfortunately, we do not currently match
5942 the standard in this respect in that our internal representation
5943 of an expression may have reference type even when the standard
5944 says it does not. Therefore, we have to manually obtain the
5945 underlying type here. */
5946 scope = non_reference (scope);
5947 /* The type of the POSTFIX_EXPRESSION must be complete. */
5948 if (scope == unknown_type_node)
5950 error_at (location, "%qE does not have class type",
5951 postfix_expression);
5952 scope = NULL_TREE;
5954 /* Unlike the object expression in other contexts, *this is not
5955 required to be of complete type for purposes of class member
5956 access (5.2.5) outside the member function body. */
5957 else if (scope != current_class_ref
5958 && !(processing_template_decl && scope == current_class_type))
5959 scope = complete_type_or_else (scope, NULL_TREE);
5960 /* Let the name lookup machinery know that we are processing a
5961 class member access expression. */
5962 parser->context->object_type = scope;
5963 /* If something went wrong, we want to be able to discern that case,
5964 as opposed to the case where there was no SCOPE due to the type
5965 of expression being dependent. */
5966 if (!scope)
5967 scope = error_mark_node;
5968 /* If the SCOPE was erroneous, make the various semantic analysis
5969 functions exit quickly -- and without issuing additional error
5970 messages. */
5971 if (scope == error_mark_node)
5972 postfix_expression = error_mark_node;
5975 /* Assume this expression is not a pseudo-destructor access. */
5976 pseudo_destructor_p = false;
5978 /* If the SCOPE is a scalar type, then, if this is a valid program,
5979 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5980 is type dependent, it can be pseudo-destructor-name or something else.
5981 Try to parse it as pseudo-destructor-name first. */
5982 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5984 tree s;
5985 tree type;
5987 cp_parser_parse_tentatively (parser);
5988 /* Parse the pseudo-destructor-name. */
5989 s = NULL_TREE;
5990 cp_parser_pseudo_destructor_name (parser, &s, &type);
5991 if (dependent_p
5992 && (cp_parser_error_occurred (parser)
5993 || TREE_CODE (type) != TYPE_DECL
5994 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5995 cp_parser_abort_tentative_parse (parser);
5996 else if (cp_parser_parse_definitely (parser))
5998 pseudo_destructor_p = true;
5999 postfix_expression
6000 = finish_pseudo_destructor_expr (postfix_expression,
6001 s, TREE_TYPE (type));
6005 if (!pseudo_destructor_p)
6007 /* If the SCOPE is not a scalar type, we are looking at an
6008 ordinary class member access expression, rather than a
6009 pseudo-destructor-name. */
6010 bool template_p;
6011 cp_token *token = cp_lexer_peek_token (parser->lexer);
6012 /* Parse the id-expression. */
6013 name = (cp_parser_id_expression
6014 (parser,
6015 cp_parser_optional_template_keyword (parser),
6016 /*check_dependency_p=*/true,
6017 &template_p,
6018 /*declarator_p=*/false,
6019 /*optional_p=*/false));
6020 /* In general, build a SCOPE_REF if the member name is qualified.
6021 However, if the name was not dependent and has already been
6022 resolved; there is no need to build the SCOPE_REF. For example;
6024 struct X { void f(); };
6025 template <typename T> void f(T* t) { t->X::f(); }
6027 Even though "t" is dependent, "X::f" is not and has been resolved
6028 to a BASELINK; there is no need to include scope information. */
6030 /* But we do need to remember that there was an explicit scope for
6031 virtual function calls. */
6032 if (parser->scope)
6033 *idk = CP_ID_KIND_QUALIFIED;
6035 /* If the name is a template-id that names a type, we will get a
6036 TYPE_DECL here. That is invalid code. */
6037 if (TREE_CODE (name) == TYPE_DECL)
6039 error_at (token->location, "invalid use of %qD", name);
6040 postfix_expression = error_mark_node;
6042 else
6044 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6046 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6048 error_at (token->location, "%<%D::%D%> is not a class member",
6049 parser->scope, name);
6050 postfix_expression = error_mark_node;
6052 else
6053 name = build_qualified_name (/*type=*/NULL_TREE,
6054 parser->scope,
6055 name,
6056 template_p);
6057 parser->scope = NULL_TREE;
6058 parser->qualifying_scope = NULL_TREE;
6059 parser->object_scope = NULL_TREE;
6061 if (parser->scope && name && BASELINK_P (name))
6062 adjust_result_of_qualified_name_lookup
6063 (name, parser->scope, scope);
6064 postfix_expression
6065 = finish_class_member_access_expr (postfix_expression, name,
6066 template_p,
6067 tf_warning_or_error);
6071 /* We no longer need to look up names in the scope of the object on
6072 the left-hand side of the `.' or `->' operator. */
6073 parser->context->object_type = NULL_TREE;
6075 /* Outside of offsetof, these operators may not appear in
6076 constant-expressions. */
6077 if (!for_offsetof
6078 && (cp_parser_non_integral_constant_expression
6079 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6080 postfix_expression = error_mark_node;
6082 return postfix_expression;
6085 /* Parse a parenthesized expression-list.
6087 expression-list:
6088 assignment-expression
6089 expression-list, assignment-expression
6091 attribute-list:
6092 expression-list
6093 identifier
6094 identifier, expression-list
6096 CAST_P is true if this expression is the target of a cast.
6098 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6099 argument pack.
6101 Returns a vector of trees. Each element is a representation of an
6102 assignment-expression. NULL is returned if the ( and or ) are
6103 missing. An empty, but allocated, vector is returned on no
6104 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6105 if we are parsing an attribute list for an attribute that wants a
6106 plain identifier argument, normal_attr for an attribute that wants
6107 an expression, or non_attr if we aren't parsing an attribute list. If
6108 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6109 not all of the expressions in the list were constant. */
6111 static VEC(tree,gc) *
6112 cp_parser_parenthesized_expression_list (cp_parser* parser,
6113 int is_attribute_list,
6114 bool cast_p,
6115 bool allow_expansion_p,
6116 bool *non_constant_p)
6118 VEC(tree,gc) *expression_list;
6119 bool fold_expr_p = is_attribute_list != non_attr;
6120 tree identifier = NULL_TREE;
6121 bool saved_greater_than_is_operator_p;
6123 /* Assume all the expressions will be constant. */
6124 if (non_constant_p)
6125 *non_constant_p = false;
6127 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6128 return NULL;
6130 expression_list = make_tree_vector ();
6132 /* Within a parenthesized expression, a `>' token is always
6133 the greater-than operator. */
6134 saved_greater_than_is_operator_p
6135 = parser->greater_than_is_operator_p;
6136 parser->greater_than_is_operator_p = true;
6138 /* Consume expressions until there are no more. */
6139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6140 while (true)
6142 tree expr;
6144 /* At the beginning of attribute lists, check to see if the
6145 next token is an identifier. */
6146 if (is_attribute_list == id_attr
6147 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6149 cp_token *token;
6151 /* Consume the identifier. */
6152 token = cp_lexer_consume_token (parser->lexer);
6153 /* Save the identifier. */
6154 identifier = token->u.value;
6156 else
6158 bool expr_non_constant_p;
6160 /* Parse the next assignment-expression. */
6161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6163 /* A braced-init-list. */
6164 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6165 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6166 if (non_constant_p && expr_non_constant_p)
6167 *non_constant_p = true;
6169 else if (non_constant_p)
6171 expr = (cp_parser_constant_expression
6172 (parser, /*allow_non_constant_p=*/true,
6173 &expr_non_constant_p));
6174 if (expr_non_constant_p)
6175 *non_constant_p = true;
6177 else
6178 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6180 if (fold_expr_p)
6181 expr = fold_non_dependent_expr (expr);
6183 /* If we have an ellipsis, then this is an expression
6184 expansion. */
6185 if (allow_expansion_p
6186 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6188 /* Consume the `...'. */
6189 cp_lexer_consume_token (parser->lexer);
6191 /* Build the argument pack. */
6192 expr = make_pack_expansion (expr);
6195 /* Add it to the list. We add error_mark_node
6196 expressions to the list, so that we can still tell if
6197 the correct form for a parenthesized expression-list
6198 is found. That gives better errors. */
6199 VEC_safe_push (tree, gc, expression_list, expr);
6201 if (expr == error_mark_node)
6202 goto skip_comma;
6205 /* After the first item, attribute lists look the same as
6206 expression lists. */
6207 is_attribute_list = non_attr;
6209 get_comma:;
6210 /* If the next token isn't a `,', then we are done. */
6211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6212 break;
6214 /* Otherwise, consume the `,' and keep going. */
6215 cp_lexer_consume_token (parser->lexer);
6218 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6220 int ending;
6222 skip_comma:;
6223 /* We try and resync to an unnested comma, as that will give the
6224 user better diagnostics. */
6225 ending = cp_parser_skip_to_closing_parenthesis (parser,
6226 /*recovering=*/true,
6227 /*or_comma=*/true,
6228 /*consume_paren=*/true);
6229 if (ending < 0)
6230 goto get_comma;
6231 if (!ending)
6233 parser->greater_than_is_operator_p
6234 = saved_greater_than_is_operator_p;
6235 return NULL;
6239 parser->greater_than_is_operator_p
6240 = saved_greater_than_is_operator_p;
6242 if (identifier)
6243 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6245 return expression_list;
6248 /* Parse a pseudo-destructor-name.
6250 pseudo-destructor-name:
6251 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6252 :: [opt] nested-name-specifier template template-id :: ~ type-name
6253 :: [opt] nested-name-specifier [opt] ~ type-name
6255 If either of the first two productions is used, sets *SCOPE to the
6256 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6257 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6258 or ERROR_MARK_NODE if the parse fails. */
6260 static void
6261 cp_parser_pseudo_destructor_name (cp_parser* parser,
6262 tree* scope,
6263 tree* type)
6265 bool nested_name_specifier_p;
6267 /* Assume that things will not work out. */
6268 *type = error_mark_node;
6270 /* Look for the optional `::' operator. */
6271 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6272 /* Look for the optional nested-name-specifier. */
6273 nested_name_specifier_p
6274 = (cp_parser_nested_name_specifier_opt (parser,
6275 /*typename_keyword_p=*/false,
6276 /*check_dependency_p=*/true,
6277 /*type_p=*/false,
6278 /*is_declaration=*/false)
6279 != NULL_TREE);
6280 /* Now, if we saw a nested-name-specifier, we might be doing the
6281 second production. */
6282 if (nested_name_specifier_p
6283 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6285 /* Consume the `template' keyword. */
6286 cp_lexer_consume_token (parser->lexer);
6287 /* Parse the template-id. */
6288 cp_parser_template_id (parser,
6289 /*template_keyword_p=*/true,
6290 /*check_dependency_p=*/false,
6291 class_type,
6292 /*is_declaration=*/true);
6293 /* Look for the `::' token. */
6294 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6296 /* If the next token is not a `~', then there might be some
6297 additional qualification. */
6298 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6300 /* At this point, we're looking for "type-name :: ~". The type-name
6301 must not be a class-name, since this is a pseudo-destructor. So,
6302 it must be either an enum-name, or a typedef-name -- both of which
6303 are just identifiers. So, we peek ahead to check that the "::"
6304 and "~" tokens are present; if they are not, then we can avoid
6305 calling type_name. */
6306 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6307 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6308 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6310 cp_parser_error (parser, "non-scalar type");
6311 return;
6314 /* Look for the type-name. */
6315 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6316 if (*scope == error_mark_node)
6317 return;
6319 /* Look for the `::' token. */
6320 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6322 else
6323 *scope = NULL_TREE;
6325 /* Look for the `~'. */
6326 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6328 /* Once we see the ~, this has to be a pseudo-destructor. */
6329 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6330 cp_parser_commit_to_tentative_parse (parser);
6332 /* Look for the type-name again. We are not responsible for
6333 checking that it matches the first type-name. */
6334 *type = cp_parser_nonclass_name (parser);
6337 /* Parse a unary-expression.
6339 unary-expression:
6340 postfix-expression
6341 ++ cast-expression
6342 -- cast-expression
6343 unary-operator cast-expression
6344 sizeof unary-expression
6345 sizeof ( type-id )
6346 alignof ( type-id ) [C++0x]
6347 new-expression
6348 delete-expression
6350 GNU Extensions:
6352 unary-expression:
6353 __extension__ cast-expression
6354 __alignof__ unary-expression
6355 __alignof__ ( type-id )
6356 alignof unary-expression [C++0x]
6357 __real__ cast-expression
6358 __imag__ cast-expression
6359 && identifier
6361 ADDRESS_P is true iff the unary-expression is appearing as the
6362 operand of the `&' operator. CAST_P is true if this expression is
6363 the target of a cast.
6365 Returns a representation of the expression. */
6367 static tree
6368 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6369 cp_id_kind * pidk)
6371 cp_token *token;
6372 enum tree_code unary_operator;
6374 /* Peek at the next token. */
6375 token = cp_lexer_peek_token (parser->lexer);
6376 /* Some keywords give away the kind of expression. */
6377 if (token->type == CPP_KEYWORD)
6379 enum rid keyword = token->keyword;
6381 switch (keyword)
6383 case RID_ALIGNOF:
6384 case RID_SIZEOF:
6386 tree operand;
6387 enum tree_code op;
6389 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6390 /* Consume the token. */
6391 cp_lexer_consume_token (parser->lexer);
6392 /* Parse the operand. */
6393 operand = cp_parser_sizeof_operand (parser, keyword);
6395 if (TYPE_P (operand))
6396 return cxx_sizeof_or_alignof_type (operand, op, true);
6397 else
6399 /* ISO C++ defines alignof only with types, not with
6400 expressions. So pedwarn if alignof is used with a non-
6401 type expression. However, __alignof__ is ok. */
6402 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6403 pedwarn (token->location, OPT_Wpedantic,
6404 "ISO C++ does not allow %<alignof%> "
6405 "with a non-type");
6407 return cxx_sizeof_or_alignof_expr (operand, op, true);
6411 case RID_NEW:
6412 return cp_parser_new_expression (parser);
6414 case RID_DELETE:
6415 return cp_parser_delete_expression (parser);
6417 case RID_EXTENSION:
6419 /* The saved value of the PEDANTIC flag. */
6420 int saved_pedantic;
6421 tree expr;
6423 /* Save away the PEDANTIC flag. */
6424 cp_parser_extension_opt (parser, &saved_pedantic);
6425 /* Parse the cast-expression. */
6426 expr = cp_parser_simple_cast_expression (parser);
6427 /* Restore the PEDANTIC flag. */
6428 pedantic = saved_pedantic;
6430 return expr;
6433 case RID_REALPART:
6434 case RID_IMAGPART:
6436 tree expression;
6438 /* Consume the `__real__' or `__imag__' token. */
6439 cp_lexer_consume_token (parser->lexer);
6440 /* Parse the cast-expression. */
6441 expression = cp_parser_simple_cast_expression (parser);
6442 /* Create the complete representation. */
6443 return build_x_unary_op (token->location,
6444 (keyword == RID_REALPART
6445 ? REALPART_EXPR : IMAGPART_EXPR),
6446 expression,
6447 tf_warning_or_error);
6449 break;
6451 case RID_TRANSACTION_ATOMIC:
6452 case RID_TRANSACTION_RELAXED:
6453 return cp_parser_transaction_expression (parser, keyword);
6455 case RID_NOEXCEPT:
6457 tree expr;
6458 const char *saved_message;
6459 bool saved_integral_constant_expression_p;
6460 bool saved_non_integral_constant_expression_p;
6461 bool saved_greater_than_is_operator_p;
6463 cp_lexer_consume_token (parser->lexer);
6464 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6466 saved_message = parser->type_definition_forbidden_message;
6467 parser->type_definition_forbidden_message
6468 = G_("types may not be defined in %<noexcept%> expressions");
6470 saved_integral_constant_expression_p
6471 = parser->integral_constant_expression_p;
6472 saved_non_integral_constant_expression_p
6473 = parser->non_integral_constant_expression_p;
6474 parser->integral_constant_expression_p = false;
6476 saved_greater_than_is_operator_p
6477 = parser->greater_than_is_operator_p;
6478 parser->greater_than_is_operator_p = true;
6480 ++cp_unevaluated_operand;
6481 ++c_inhibit_evaluation_warnings;
6482 expr = cp_parser_expression (parser, false, NULL);
6483 --c_inhibit_evaluation_warnings;
6484 --cp_unevaluated_operand;
6486 parser->greater_than_is_operator_p
6487 = saved_greater_than_is_operator_p;
6489 parser->integral_constant_expression_p
6490 = saved_integral_constant_expression_p;
6491 parser->non_integral_constant_expression_p
6492 = saved_non_integral_constant_expression_p;
6494 parser->type_definition_forbidden_message = saved_message;
6496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6497 return finish_noexcept_expr (expr, tf_warning_or_error);
6500 default:
6501 break;
6505 /* Look for the `:: new' and `:: delete', which also signal the
6506 beginning of a new-expression, or delete-expression,
6507 respectively. If the next token is `::', then it might be one of
6508 these. */
6509 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6511 enum rid keyword;
6513 /* See if the token after the `::' is one of the keywords in
6514 which we're interested. */
6515 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6516 /* If it's `new', we have a new-expression. */
6517 if (keyword == RID_NEW)
6518 return cp_parser_new_expression (parser);
6519 /* Similarly, for `delete'. */
6520 else if (keyword == RID_DELETE)
6521 return cp_parser_delete_expression (parser);
6524 /* Look for a unary operator. */
6525 unary_operator = cp_parser_unary_operator (token);
6526 /* The `++' and `--' operators can be handled similarly, even though
6527 they are not technically unary-operators in the grammar. */
6528 if (unary_operator == ERROR_MARK)
6530 if (token->type == CPP_PLUS_PLUS)
6531 unary_operator = PREINCREMENT_EXPR;
6532 else if (token->type == CPP_MINUS_MINUS)
6533 unary_operator = PREDECREMENT_EXPR;
6534 /* Handle the GNU address-of-label extension. */
6535 else if (cp_parser_allow_gnu_extensions_p (parser)
6536 && token->type == CPP_AND_AND)
6538 tree identifier;
6539 tree expression;
6540 location_t loc = token->location;
6542 /* Consume the '&&' token. */
6543 cp_lexer_consume_token (parser->lexer);
6544 /* Look for the identifier. */
6545 identifier = cp_parser_identifier (parser);
6546 /* Create an expression representing the address. */
6547 expression = finish_label_address_expr (identifier, loc);
6548 if (cp_parser_non_integral_constant_expression (parser,
6549 NIC_ADDR_LABEL))
6550 expression = error_mark_node;
6551 return expression;
6554 if (unary_operator != ERROR_MARK)
6556 tree cast_expression;
6557 tree expression = error_mark_node;
6558 non_integral_constant non_constant_p = NIC_NONE;
6559 location_t loc = token->location;
6561 /* Consume the operator token. */
6562 token = cp_lexer_consume_token (parser->lexer);
6563 /* Parse the cast-expression. */
6564 cast_expression
6565 = cp_parser_cast_expression (parser,
6566 unary_operator == ADDR_EXPR,
6567 /*cast_p=*/false, pidk);
6568 /* Now, build an appropriate representation. */
6569 switch (unary_operator)
6571 case INDIRECT_REF:
6572 non_constant_p = NIC_STAR;
6573 expression = build_x_indirect_ref (loc, cast_expression,
6574 RO_UNARY_STAR,
6575 tf_warning_or_error);
6576 break;
6578 case ADDR_EXPR:
6579 non_constant_p = NIC_ADDR;
6580 /* Fall through. */
6581 case BIT_NOT_EXPR:
6582 expression = build_x_unary_op (loc, unary_operator,
6583 cast_expression,
6584 tf_warning_or_error);
6585 break;
6587 case PREINCREMENT_EXPR:
6588 case PREDECREMENT_EXPR:
6589 non_constant_p = unary_operator == PREINCREMENT_EXPR
6590 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6591 /* Fall through. */
6592 case UNARY_PLUS_EXPR:
6593 case NEGATE_EXPR:
6594 case TRUTH_NOT_EXPR:
6595 expression = finish_unary_op_expr (loc, unary_operator,
6596 cast_expression);
6597 break;
6599 default:
6600 gcc_unreachable ();
6603 if (non_constant_p != NIC_NONE
6604 && cp_parser_non_integral_constant_expression (parser,
6605 non_constant_p))
6606 expression = error_mark_node;
6608 return expression;
6611 return cp_parser_postfix_expression (parser, address_p, cast_p,
6612 /*member_access_only_p=*/false,
6613 pidk);
6616 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6617 unary-operator, the corresponding tree code is returned. */
6619 static enum tree_code
6620 cp_parser_unary_operator (cp_token* token)
6622 switch (token->type)
6624 case CPP_MULT:
6625 return INDIRECT_REF;
6627 case CPP_AND:
6628 return ADDR_EXPR;
6630 case CPP_PLUS:
6631 return UNARY_PLUS_EXPR;
6633 case CPP_MINUS:
6634 return NEGATE_EXPR;
6636 case CPP_NOT:
6637 return TRUTH_NOT_EXPR;
6639 case CPP_COMPL:
6640 return BIT_NOT_EXPR;
6642 default:
6643 return ERROR_MARK;
6647 /* Parse a new-expression.
6649 new-expression:
6650 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6651 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6653 Returns a representation of the expression. */
6655 static tree
6656 cp_parser_new_expression (cp_parser* parser)
6658 bool global_scope_p;
6659 VEC(tree,gc) *placement;
6660 tree type;
6661 VEC(tree,gc) *initializer;
6662 tree nelts = NULL_TREE;
6663 tree ret;
6665 /* Look for the optional `::' operator. */
6666 global_scope_p
6667 = (cp_parser_global_scope_opt (parser,
6668 /*current_scope_valid_p=*/false)
6669 != NULL_TREE);
6670 /* Look for the `new' operator. */
6671 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6672 /* There's no easy way to tell a new-placement from the
6673 `( type-id )' construct. */
6674 cp_parser_parse_tentatively (parser);
6675 /* Look for a new-placement. */
6676 placement = cp_parser_new_placement (parser);
6677 /* If that didn't work out, there's no new-placement. */
6678 if (!cp_parser_parse_definitely (parser))
6680 if (placement != NULL)
6681 release_tree_vector (placement);
6682 placement = NULL;
6685 /* If the next token is a `(', then we have a parenthesized
6686 type-id. */
6687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6689 cp_token *token;
6690 const char *saved_message = parser->type_definition_forbidden_message;
6692 /* Consume the `('. */
6693 cp_lexer_consume_token (parser->lexer);
6695 /* Parse the type-id. */
6696 parser->type_definition_forbidden_message
6697 = G_("types may not be defined in a new-expression");
6698 type = cp_parser_type_id (parser);
6699 parser->type_definition_forbidden_message = saved_message;
6701 /* Look for the closing `)'. */
6702 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6703 token = cp_lexer_peek_token (parser->lexer);
6704 /* There should not be a direct-new-declarator in this production,
6705 but GCC used to allowed this, so we check and emit a sensible error
6706 message for this case. */
6707 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6709 error_at (token->location,
6710 "array bound forbidden after parenthesized type-id");
6711 inform (token->location,
6712 "try removing the parentheses around the type-id");
6713 cp_parser_direct_new_declarator (parser);
6716 /* Otherwise, there must be a new-type-id. */
6717 else
6718 type = cp_parser_new_type_id (parser, &nelts);
6720 /* If the next token is a `(' or '{', then we have a new-initializer. */
6721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6722 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6723 initializer = cp_parser_new_initializer (parser);
6724 else
6725 initializer = NULL;
6727 /* A new-expression may not appear in an integral constant
6728 expression. */
6729 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6730 ret = error_mark_node;
6731 else
6733 /* Create a representation of the new-expression. */
6734 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6735 tf_warning_or_error);
6738 if (placement != NULL)
6739 release_tree_vector (placement);
6740 if (initializer != NULL)
6741 release_tree_vector (initializer);
6743 return ret;
6746 /* Parse a new-placement.
6748 new-placement:
6749 ( expression-list )
6751 Returns the same representation as for an expression-list. */
6753 static VEC(tree,gc) *
6754 cp_parser_new_placement (cp_parser* parser)
6756 VEC(tree,gc) *expression_list;
6758 /* Parse the expression-list. */
6759 expression_list = (cp_parser_parenthesized_expression_list
6760 (parser, non_attr, /*cast_p=*/false,
6761 /*allow_expansion_p=*/true,
6762 /*non_constant_p=*/NULL));
6764 return expression_list;
6767 /* Parse a new-type-id.
6769 new-type-id:
6770 type-specifier-seq new-declarator [opt]
6772 Returns the TYPE allocated. If the new-type-id indicates an array
6773 type, *NELTS is set to the number of elements in the last array
6774 bound; the TYPE will not include the last array bound. */
6776 static tree
6777 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6779 cp_decl_specifier_seq type_specifier_seq;
6780 cp_declarator *new_declarator;
6781 cp_declarator *declarator;
6782 cp_declarator *outer_declarator;
6783 const char *saved_message;
6785 /* The type-specifier sequence must not contain type definitions.
6786 (It cannot contain declarations of new types either, but if they
6787 are not definitions we will catch that because they are not
6788 complete.) */
6789 saved_message = parser->type_definition_forbidden_message;
6790 parser->type_definition_forbidden_message
6791 = G_("types may not be defined in a new-type-id");
6792 /* Parse the type-specifier-seq. */
6793 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6794 /*is_trailing_return=*/false,
6795 &type_specifier_seq);
6796 /* Restore the old message. */
6797 parser->type_definition_forbidden_message = saved_message;
6799 if (type_specifier_seq.type == error_mark_node)
6800 return error_mark_node;
6802 /* Parse the new-declarator. */
6803 new_declarator = cp_parser_new_declarator_opt (parser);
6805 /* Determine the number of elements in the last array dimension, if
6806 any. */
6807 *nelts = NULL_TREE;
6808 /* Skip down to the last array dimension. */
6809 declarator = new_declarator;
6810 outer_declarator = NULL;
6811 while (declarator && (declarator->kind == cdk_pointer
6812 || declarator->kind == cdk_ptrmem))
6814 outer_declarator = declarator;
6815 declarator = declarator->declarator;
6817 while (declarator
6818 && declarator->kind == cdk_array
6819 && declarator->declarator
6820 && declarator->declarator->kind == cdk_array)
6822 outer_declarator = declarator;
6823 declarator = declarator->declarator;
6826 if (declarator && declarator->kind == cdk_array)
6828 *nelts = declarator->u.array.bounds;
6829 if (*nelts == error_mark_node)
6830 *nelts = integer_one_node;
6832 if (outer_declarator)
6833 outer_declarator->declarator = declarator->declarator;
6834 else
6835 new_declarator = NULL;
6838 return groktypename (&type_specifier_seq, new_declarator, false);
6841 /* Parse an (optional) new-declarator.
6843 new-declarator:
6844 ptr-operator new-declarator [opt]
6845 direct-new-declarator
6847 Returns the declarator. */
6849 static cp_declarator *
6850 cp_parser_new_declarator_opt (cp_parser* parser)
6852 enum tree_code code;
6853 tree type;
6854 cp_cv_quals cv_quals;
6856 /* We don't know if there's a ptr-operator next, or not. */
6857 cp_parser_parse_tentatively (parser);
6858 /* Look for a ptr-operator. */
6859 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6860 /* If that worked, look for more new-declarators. */
6861 if (cp_parser_parse_definitely (parser))
6863 cp_declarator *declarator;
6865 /* Parse another optional declarator. */
6866 declarator = cp_parser_new_declarator_opt (parser);
6868 return cp_parser_make_indirect_declarator
6869 (code, type, cv_quals, declarator);
6872 /* If the next token is a `[', there is a direct-new-declarator. */
6873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6874 return cp_parser_direct_new_declarator (parser);
6876 return NULL;
6879 /* Parse a direct-new-declarator.
6881 direct-new-declarator:
6882 [ expression ]
6883 direct-new-declarator [constant-expression]
6887 static cp_declarator *
6888 cp_parser_direct_new_declarator (cp_parser* parser)
6890 cp_declarator *declarator = NULL;
6892 while (true)
6894 tree expression;
6895 cp_token *token;
6897 /* Look for the opening `['. */
6898 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6900 token = cp_lexer_peek_token (parser->lexer);
6901 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6902 /* The standard requires that the expression have integral
6903 type. DR 74 adds enumeration types. We believe that the
6904 real intent is that these expressions be handled like the
6905 expression in a `switch' condition, which also allows
6906 classes with a single conversion to integral or
6907 enumeration type. */
6908 if (!processing_template_decl)
6910 expression
6911 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6912 expression,
6913 /*complain=*/true);
6914 if (!expression)
6916 error_at (token->location,
6917 "expression in new-declarator must have integral "
6918 "or enumeration type");
6919 expression = error_mark_node;
6923 /* Look for the closing `]'. */
6924 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6926 /* Add this bound to the declarator. */
6927 declarator = make_array_declarator (declarator, expression);
6929 /* If the next token is not a `[', then there are no more
6930 bounds. */
6931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6932 break;
6935 return declarator;
6938 /* Parse a new-initializer.
6940 new-initializer:
6941 ( expression-list [opt] )
6942 braced-init-list
6944 Returns a representation of the expression-list. */
6946 static VEC(tree,gc) *
6947 cp_parser_new_initializer (cp_parser* parser)
6949 VEC(tree,gc) *expression_list;
6951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6953 tree t;
6954 bool expr_non_constant_p;
6955 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6956 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6957 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6958 expression_list = make_tree_vector_single (t);
6960 else
6961 expression_list = (cp_parser_parenthesized_expression_list
6962 (parser, non_attr, /*cast_p=*/false,
6963 /*allow_expansion_p=*/true,
6964 /*non_constant_p=*/NULL));
6966 return expression_list;
6969 /* Parse a delete-expression.
6971 delete-expression:
6972 :: [opt] delete cast-expression
6973 :: [opt] delete [ ] cast-expression
6975 Returns a representation of the expression. */
6977 static tree
6978 cp_parser_delete_expression (cp_parser* parser)
6980 bool global_scope_p;
6981 bool array_p;
6982 tree expression;
6984 /* Look for the optional `::' operator. */
6985 global_scope_p
6986 = (cp_parser_global_scope_opt (parser,
6987 /*current_scope_valid_p=*/false)
6988 != NULL_TREE);
6989 /* Look for the `delete' keyword. */
6990 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6991 /* See if the array syntax is in use. */
6992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6994 /* Consume the `[' token. */
6995 cp_lexer_consume_token (parser->lexer);
6996 /* Look for the `]' token. */
6997 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6998 /* Remember that this is the `[]' construct. */
6999 array_p = true;
7001 else
7002 array_p = false;
7004 /* Parse the cast-expression. */
7005 expression = cp_parser_simple_cast_expression (parser);
7007 /* A delete-expression may not appear in an integral constant
7008 expression. */
7009 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7010 return error_mark_node;
7012 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7013 tf_warning_or_error);
7016 /* Returns true if TOKEN may start a cast-expression and false
7017 otherwise. */
7019 static bool
7020 cp_parser_token_starts_cast_expression (cp_token *token)
7022 switch (token->type)
7024 case CPP_COMMA:
7025 case CPP_SEMICOLON:
7026 case CPP_QUERY:
7027 case CPP_COLON:
7028 case CPP_CLOSE_SQUARE:
7029 case CPP_CLOSE_PAREN:
7030 case CPP_CLOSE_BRACE:
7031 case CPP_DOT:
7032 case CPP_DOT_STAR:
7033 case CPP_DEREF:
7034 case CPP_DEREF_STAR:
7035 case CPP_DIV:
7036 case CPP_MOD:
7037 case CPP_LSHIFT:
7038 case CPP_RSHIFT:
7039 case CPP_LESS:
7040 case CPP_GREATER:
7041 case CPP_LESS_EQ:
7042 case CPP_GREATER_EQ:
7043 case CPP_EQ_EQ:
7044 case CPP_NOT_EQ:
7045 case CPP_EQ:
7046 case CPP_MULT_EQ:
7047 case CPP_DIV_EQ:
7048 case CPP_MOD_EQ:
7049 case CPP_PLUS_EQ:
7050 case CPP_MINUS_EQ:
7051 case CPP_RSHIFT_EQ:
7052 case CPP_LSHIFT_EQ:
7053 case CPP_AND_EQ:
7054 case CPP_XOR_EQ:
7055 case CPP_OR_EQ:
7056 case CPP_XOR:
7057 case CPP_OR:
7058 case CPP_OR_OR:
7059 case CPP_EOF:
7060 return false;
7062 /* '[' may start a primary-expression in obj-c++. */
7063 case CPP_OPEN_SQUARE:
7064 return c_dialect_objc ();
7066 default:
7067 return true;
7071 /* Parse a cast-expression.
7073 cast-expression:
7074 unary-expression
7075 ( type-id ) cast-expression
7077 ADDRESS_P is true iff the unary-expression is appearing as the
7078 operand of the `&' operator. CAST_P is true if this expression is
7079 the target of a cast.
7081 Returns a representation of the expression. */
7083 static tree
7084 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7085 cp_id_kind * pidk)
7087 /* If it's a `(', then we might be looking at a cast. */
7088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7090 tree type = NULL_TREE;
7091 tree expr = NULL_TREE;
7092 bool compound_literal_p;
7093 const char *saved_message;
7095 /* There's no way to know yet whether or not this is a cast.
7096 For example, `(int (3))' is a unary-expression, while `(int)
7097 3' is a cast. So, we resort to parsing tentatively. */
7098 cp_parser_parse_tentatively (parser);
7099 /* Types may not be defined in a cast. */
7100 saved_message = parser->type_definition_forbidden_message;
7101 parser->type_definition_forbidden_message
7102 = G_("types may not be defined in casts");
7103 /* Consume the `('. */
7104 cp_lexer_consume_token (parser->lexer);
7105 /* A very tricky bit is that `(struct S) { 3 }' is a
7106 compound-literal (which we permit in C++ as an extension).
7107 But, that construct is not a cast-expression -- it is a
7108 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7109 is legal; if the compound-literal were a cast-expression,
7110 you'd need an extra set of parentheses.) But, if we parse
7111 the type-id, and it happens to be a class-specifier, then we
7112 will commit to the parse at that point, because we cannot
7113 undo the action that is done when creating a new class. So,
7114 then we cannot back up and do a postfix-expression.
7116 Therefore, we scan ahead to the closing `)', and check to see
7117 if the token after the `)' is a `{'. If so, we are not
7118 looking at a cast-expression.
7120 Save tokens so that we can put them back. */
7121 cp_lexer_save_tokens (parser->lexer);
7122 /* Skip tokens until the next token is a closing parenthesis.
7123 If we find the closing `)', and the next token is a `{', then
7124 we are looking at a compound-literal. */
7125 compound_literal_p
7126 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7127 /*consume_paren=*/true)
7128 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7129 /* Roll back the tokens we skipped. */
7130 cp_lexer_rollback_tokens (parser->lexer);
7131 /* If we were looking at a compound-literal, simulate an error
7132 so that the call to cp_parser_parse_definitely below will
7133 fail. */
7134 if (compound_literal_p)
7135 cp_parser_simulate_error (parser);
7136 else
7138 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7139 parser->in_type_id_in_expr_p = true;
7140 /* Look for the type-id. */
7141 type = cp_parser_type_id (parser);
7142 /* Look for the closing `)'. */
7143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7144 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7147 /* Restore the saved message. */
7148 parser->type_definition_forbidden_message = saved_message;
7150 /* At this point this can only be either a cast or a
7151 parenthesized ctor such as `(T ())' that looks like a cast to
7152 function returning T. */
7153 if (!cp_parser_error_occurred (parser)
7154 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7155 (parser->lexer)))
7157 cp_parser_parse_definitely (parser);
7158 expr = cp_parser_cast_expression (parser,
7159 /*address_p=*/false,
7160 /*cast_p=*/true, pidk);
7162 /* Warn about old-style casts, if so requested. */
7163 if (warn_old_style_cast
7164 && !in_system_header
7165 && !VOID_TYPE_P (type)
7166 && current_lang_name != lang_name_c)
7167 warning (OPT_Wold_style_cast, "use of old-style cast");
7169 /* Only type conversions to integral or enumeration types
7170 can be used in constant-expressions. */
7171 if (!cast_valid_in_integral_constant_expression_p (type)
7172 && cp_parser_non_integral_constant_expression (parser,
7173 NIC_CAST))
7174 return error_mark_node;
7176 /* Perform the cast. */
7177 expr = build_c_cast (input_location, type, expr);
7178 return expr;
7180 else
7181 cp_parser_abort_tentative_parse (parser);
7184 /* If we get here, then it's not a cast, so it must be a
7185 unary-expression. */
7186 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7189 /* Parse a binary expression of the general form:
7191 pm-expression:
7192 cast-expression
7193 pm-expression .* cast-expression
7194 pm-expression ->* cast-expression
7196 multiplicative-expression:
7197 pm-expression
7198 multiplicative-expression * pm-expression
7199 multiplicative-expression / pm-expression
7200 multiplicative-expression % pm-expression
7202 additive-expression:
7203 multiplicative-expression
7204 additive-expression + multiplicative-expression
7205 additive-expression - multiplicative-expression
7207 shift-expression:
7208 additive-expression
7209 shift-expression << additive-expression
7210 shift-expression >> additive-expression
7212 relational-expression:
7213 shift-expression
7214 relational-expression < shift-expression
7215 relational-expression > shift-expression
7216 relational-expression <= shift-expression
7217 relational-expression >= shift-expression
7219 GNU Extension:
7221 relational-expression:
7222 relational-expression <? shift-expression
7223 relational-expression >? shift-expression
7225 equality-expression:
7226 relational-expression
7227 equality-expression == relational-expression
7228 equality-expression != relational-expression
7230 and-expression:
7231 equality-expression
7232 and-expression & equality-expression
7234 exclusive-or-expression:
7235 and-expression
7236 exclusive-or-expression ^ and-expression
7238 inclusive-or-expression:
7239 exclusive-or-expression
7240 inclusive-or-expression | exclusive-or-expression
7242 logical-and-expression:
7243 inclusive-or-expression
7244 logical-and-expression && inclusive-or-expression
7246 logical-or-expression:
7247 logical-and-expression
7248 logical-or-expression || logical-and-expression
7250 All these are implemented with a single function like:
7252 binary-expression:
7253 simple-cast-expression
7254 binary-expression <token> binary-expression
7256 CAST_P is true if this expression is the target of a cast.
7258 The binops_by_token map is used to get the tree codes for each <token> type.
7259 binary-expressions are associated according to a precedence table. */
7261 #define TOKEN_PRECEDENCE(token) \
7262 (((token->type == CPP_GREATER \
7263 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7264 && !parser->greater_than_is_operator_p) \
7265 ? PREC_NOT_OPERATOR \
7266 : binops_by_token[token->type].prec)
7268 static tree
7269 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7270 bool no_toplevel_fold_p,
7271 enum cp_parser_prec prec,
7272 cp_id_kind * pidk)
7274 cp_parser_expression_stack stack;
7275 cp_parser_expression_stack_entry *sp = &stack[0];
7276 cp_parser_expression_stack_entry current;
7277 tree rhs;
7278 cp_token *token;
7279 enum tree_code rhs_type;
7280 enum cp_parser_prec new_prec, lookahead_prec;
7281 tree overload;
7283 /* Parse the first expression. */
7284 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7285 cast_p, pidk);
7286 current.lhs_type = ERROR_MARK;
7287 current.prec = prec;
7289 if (cp_parser_error_occurred (parser))
7290 return error_mark_node;
7292 for (;;)
7294 /* Get an operator token. */
7295 token = cp_lexer_peek_token (parser->lexer);
7297 if (warn_cxx0x_compat
7298 && token->type == CPP_RSHIFT
7299 && !parser->greater_than_is_operator_p)
7301 if (warning_at (token->location, OPT_Wc__0x_compat,
7302 "%<>>%> operator is treated"
7303 " as two right angle brackets in C++11"))
7304 inform (token->location,
7305 "suggest parentheses around %<>>%> expression");
7308 new_prec = TOKEN_PRECEDENCE (token);
7310 /* Popping an entry off the stack means we completed a subexpression:
7311 - either we found a token which is not an operator (`>' where it is not
7312 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7313 will happen repeatedly;
7314 - or, we found an operator which has lower priority. This is the case
7315 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7316 parsing `3 * 4'. */
7317 if (new_prec <= current.prec)
7319 if (sp == stack)
7320 break;
7321 else
7322 goto pop;
7325 get_rhs:
7326 current.tree_type = binops_by_token[token->type].tree_type;
7327 current.loc = token->location;
7329 /* We used the operator token. */
7330 cp_lexer_consume_token (parser->lexer);
7332 /* For "false && x" or "true || x", x will never be executed;
7333 disable warnings while evaluating it. */
7334 if (current.tree_type == TRUTH_ANDIF_EXPR)
7335 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7336 else if (current.tree_type == TRUTH_ORIF_EXPR)
7337 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7339 /* Extract another operand. It may be the RHS of this expression
7340 or the LHS of a new, higher priority expression. */
7341 rhs = cp_parser_simple_cast_expression (parser);
7342 rhs_type = ERROR_MARK;
7344 /* Get another operator token. Look up its precedence to avoid
7345 building a useless (immediately popped) stack entry for common
7346 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7347 token = cp_lexer_peek_token (parser->lexer);
7348 lookahead_prec = TOKEN_PRECEDENCE (token);
7349 if (lookahead_prec > new_prec)
7351 /* ... and prepare to parse the RHS of the new, higher priority
7352 expression. Since precedence levels on the stack are
7353 monotonically increasing, we do not have to care about
7354 stack overflows. */
7355 *sp = current;
7356 ++sp;
7357 current.lhs = rhs;
7358 current.lhs_type = rhs_type;
7359 current.prec = new_prec;
7360 new_prec = lookahead_prec;
7361 goto get_rhs;
7363 pop:
7364 lookahead_prec = new_prec;
7365 /* If the stack is not empty, we have parsed into LHS the right side
7366 (`4' in the example above) of an expression we had suspended.
7367 We can use the information on the stack to recover the LHS (`3')
7368 from the stack together with the tree code (`MULT_EXPR'), and
7369 the precedence of the higher level subexpression
7370 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7371 which will be used to actually build the additive expression. */
7372 rhs = current.lhs;
7373 rhs_type = current.lhs_type;
7374 --sp;
7375 current = *sp;
7378 /* Undo the disabling of warnings done above. */
7379 if (current.tree_type == TRUTH_ANDIF_EXPR)
7380 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7381 else if (current.tree_type == TRUTH_ORIF_EXPR)
7382 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7384 overload = NULL;
7385 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7386 ERROR_MARK for everything that is not a binary expression.
7387 This makes warn_about_parentheses miss some warnings that
7388 involve unary operators. For unary expressions we should
7389 pass the correct tree_code unless the unary expression was
7390 surrounded by parentheses.
7392 if (no_toplevel_fold_p
7393 && lookahead_prec <= current.prec
7394 && sp == stack
7395 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7396 current.lhs = build2 (current.tree_type, boolean_type_node,
7397 current.lhs, rhs);
7398 else
7399 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7400 current.lhs, current.lhs_type,
7401 rhs, rhs_type, &overload,
7402 tf_warning_or_error);
7403 current.lhs_type = current.tree_type;
7405 /* If the binary operator required the use of an overloaded operator,
7406 then this expression cannot be an integral constant-expression.
7407 An overloaded operator can be used even if both operands are
7408 otherwise permissible in an integral constant-expression if at
7409 least one of the operands is of enumeration type. */
7411 if (overload
7412 && cp_parser_non_integral_constant_expression (parser,
7413 NIC_OVERLOADED))
7414 return error_mark_node;
7417 return current.lhs;
7421 /* Parse the `? expression : assignment-expression' part of a
7422 conditional-expression. The LOGICAL_OR_EXPR is the
7423 logical-or-expression that started the conditional-expression.
7424 Returns a representation of the entire conditional-expression.
7426 This routine is used by cp_parser_assignment_expression.
7428 ? expression : assignment-expression
7430 GNU Extensions:
7432 ? : assignment-expression */
7434 static tree
7435 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7437 tree expr;
7438 tree assignment_expr;
7439 struct cp_token *token;
7440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7442 /* Consume the `?' token. */
7443 cp_lexer_consume_token (parser->lexer);
7444 token = cp_lexer_peek_token (parser->lexer);
7445 if (cp_parser_allow_gnu_extensions_p (parser)
7446 && token->type == CPP_COLON)
7448 pedwarn (token->location, OPT_Wpedantic,
7449 "ISO C++ does not allow ?: with omitted middle operand");
7450 /* Implicit true clause. */
7451 expr = NULL_TREE;
7452 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7453 warn_for_omitted_condop (token->location, logical_or_expr);
7455 else
7457 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7458 parser->colon_corrects_to_scope_p = false;
7459 /* Parse the expression. */
7460 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7461 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7462 c_inhibit_evaluation_warnings +=
7463 ((logical_or_expr == truthvalue_true_node)
7464 - (logical_or_expr == truthvalue_false_node));
7465 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7468 /* The next token should be a `:'. */
7469 cp_parser_require (parser, CPP_COLON, RT_COLON);
7470 /* Parse the assignment-expression. */
7471 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7472 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7474 /* Build the conditional-expression. */
7475 return build_x_conditional_expr (loc, logical_or_expr,
7476 expr,
7477 assignment_expr,
7478 tf_warning_or_error);
7481 /* Parse an assignment-expression.
7483 assignment-expression:
7484 conditional-expression
7485 logical-or-expression assignment-operator assignment_expression
7486 throw-expression
7488 CAST_P is true if this expression is the target of a cast.
7490 Returns a representation for the expression. */
7492 static tree
7493 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7494 cp_id_kind * pidk)
7496 tree expr;
7498 /* If the next token is the `throw' keyword, then we're looking at
7499 a throw-expression. */
7500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7501 expr = cp_parser_throw_expression (parser);
7502 /* Otherwise, it must be that we are looking at a
7503 logical-or-expression. */
7504 else
7506 /* Parse the binary expressions (logical-or-expression). */
7507 expr = cp_parser_binary_expression (parser, cast_p, false,
7508 PREC_NOT_OPERATOR, pidk);
7509 /* If the next token is a `?' then we're actually looking at a
7510 conditional-expression. */
7511 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7512 return cp_parser_question_colon_clause (parser, expr);
7513 else
7515 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7517 /* If it's an assignment-operator, we're using the second
7518 production. */
7519 enum tree_code assignment_operator
7520 = cp_parser_assignment_operator_opt (parser);
7521 if (assignment_operator != ERROR_MARK)
7523 bool non_constant_p;
7524 location_t saved_input_location;
7526 /* Parse the right-hand side of the assignment. */
7527 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7529 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7532 /* An assignment may not appear in a
7533 constant-expression. */
7534 if (cp_parser_non_integral_constant_expression (parser,
7535 NIC_ASSIGNMENT))
7536 return error_mark_node;
7537 /* Build the assignment expression. Its default
7538 location is the location of the '=' token. */
7539 saved_input_location = input_location;
7540 input_location = loc;
7541 expr = build_x_modify_expr (loc, expr,
7542 assignment_operator,
7543 rhs,
7544 tf_warning_or_error);
7545 input_location = saved_input_location;
7550 return expr;
7553 /* Parse an (optional) assignment-operator.
7555 assignment-operator: one of
7556 = *= /= %= += -= >>= <<= &= ^= |=
7558 GNU Extension:
7560 assignment-operator: one of
7561 <?= >?=
7563 If the next token is an assignment operator, the corresponding tree
7564 code is returned, and the token is consumed. For example, for
7565 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7566 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7567 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7568 operator, ERROR_MARK is returned. */
7570 static enum tree_code
7571 cp_parser_assignment_operator_opt (cp_parser* parser)
7573 enum tree_code op;
7574 cp_token *token;
7576 /* Peek at the next token. */
7577 token = cp_lexer_peek_token (parser->lexer);
7579 switch (token->type)
7581 case CPP_EQ:
7582 op = NOP_EXPR;
7583 break;
7585 case CPP_MULT_EQ:
7586 op = MULT_EXPR;
7587 break;
7589 case CPP_DIV_EQ:
7590 op = TRUNC_DIV_EXPR;
7591 break;
7593 case CPP_MOD_EQ:
7594 op = TRUNC_MOD_EXPR;
7595 break;
7597 case CPP_PLUS_EQ:
7598 op = PLUS_EXPR;
7599 break;
7601 case CPP_MINUS_EQ:
7602 op = MINUS_EXPR;
7603 break;
7605 case CPP_RSHIFT_EQ:
7606 op = RSHIFT_EXPR;
7607 break;
7609 case CPP_LSHIFT_EQ:
7610 op = LSHIFT_EXPR;
7611 break;
7613 case CPP_AND_EQ:
7614 op = BIT_AND_EXPR;
7615 break;
7617 case CPP_XOR_EQ:
7618 op = BIT_XOR_EXPR;
7619 break;
7621 case CPP_OR_EQ:
7622 op = BIT_IOR_EXPR;
7623 break;
7625 default:
7626 /* Nothing else is an assignment operator. */
7627 op = ERROR_MARK;
7630 /* If it was an assignment operator, consume it. */
7631 if (op != ERROR_MARK)
7632 cp_lexer_consume_token (parser->lexer);
7634 return op;
7637 /* Parse an expression.
7639 expression:
7640 assignment-expression
7641 expression , assignment-expression
7643 CAST_P is true if this expression is the target of a cast.
7645 Returns a representation of the expression. */
7647 static tree
7648 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7650 tree expression = NULL_TREE;
7651 location_t loc = UNKNOWN_LOCATION;
7653 while (true)
7655 tree assignment_expression;
7657 /* Parse the next assignment-expression. */
7658 assignment_expression
7659 = cp_parser_assignment_expression (parser, cast_p, pidk);
7660 /* If this is the first assignment-expression, we can just
7661 save it away. */
7662 if (!expression)
7663 expression = assignment_expression;
7664 else
7665 expression = build_x_compound_expr (loc, expression,
7666 assignment_expression,
7667 tf_warning_or_error);
7668 /* If the next token is not a comma, then we are done with the
7669 expression. */
7670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7671 break;
7672 /* Consume the `,'. */
7673 loc = cp_lexer_peek_token (parser->lexer)->location;
7674 cp_lexer_consume_token (parser->lexer);
7675 /* A comma operator cannot appear in a constant-expression. */
7676 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7677 expression = error_mark_node;
7680 return expression;
7683 /* Parse a constant-expression.
7685 constant-expression:
7686 conditional-expression
7688 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7689 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7690 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7691 is false, NON_CONSTANT_P should be NULL. */
7693 static tree
7694 cp_parser_constant_expression (cp_parser* parser,
7695 bool allow_non_constant_p,
7696 bool *non_constant_p)
7698 bool saved_integral_constant_expression_p;
7699 bool saved_allow_non_integral_constant_expression_p;
7700 bool saved_non_integral_constant_expression_p;
7701 tree expression;
7703 /* It might seem that we could simply parse the
7704 conditional-expression, and then check to see if it were
7705 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7706 one that the compiler can figure out is constant, possibly after
7707 doing some simplifications or optimizations. The standard has a
7708 precise definition of constant-expression, and we must honor
7709 that, even though it is somewhat more restrictive.
7711 For example:
7713 int i[(2, 3)];
7715 is not a legal declaration, because `(2, 3)' is not a
7716 constant-expression. The `,' operator is forbidden in a
7717 constant-expression. However, GCC's constant-folding machinery
7718 will fold this operation to an INTEGER_CST for `3'. */
7720 /* Save the old settings. */
7721 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7722 saved_allow_non_integral_constant_expression_p
7723 = parser->allow_non_integral_constant_expression_p;
7724 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7725 /* We are now parsing a constant-expression. */
7726 parser->integral_constant_expression_p = true;
7727 parser->allow_non_integral_constant_expression_p
7728 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7729 parser->non_integral_constant_expression_p = false;
7730 /* Although the grammar says "conditional-expression", we parse an
7731 "assignment-expression", which also permits "throw-expression"
7732 and the use of assignment operators. In the case that
7733 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7734 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7735 actually essential that we look for an assignment-expression.
7736 For example, cp_parser_initializer_clauses uses this function to
7737 determine whether a particular assignment-expression is in fact
7738 constant. */
7739 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7740 /* Restore the old settings. */
7741 parser->integral_constant_expression_p
7742 = saved_integral_constant_expression_p;
7743 parser->allow_non_integral_constant_expression_p
7744 = saved_allow_non_integral_constant_expression_p;
7745 if (cxx_dialect >= cxx0x)
7747 /* Require an rvalue constant expression here; that's what our
7748 callers expect. Reference constant expressions are handled
7749 separately in e.g. cp_parser_template_argument. */
7750 bool is_const = potential_rvalue_constant_expression (expression);
7751 parser->non_integral_constant_expression_p = !is_const;
7752 if (!is_const && !allow_non_constant_p)
7753 require_potential_rvalue_constant_expression (expression);
7755 if (allow_non_constant_p)
7756 *non_constant_p = parser->non_integral_constant_expression_p;
7757 parser->non_integral_constant_expression_p
7758 = saved_non_integral_constant_expression_p;
7760 return expression;
7763 /* Parse __builtin_offsetof.
7765 offsetof-expression:
7766 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7768 offsetof-member-designator:
7769 id-expression
7770 | offsetof-member-designator "." id-expression
7771 | offsetof-member-designator "[" expression "]"
7772 | offsetof-member-designator "->" id-expression */
7774 static tree
7775 cp_parser_builtin_offsetof (cp_parser *parser)
7777 int save_ice_p, save_non_ice_p;
7778 tree type, expr;
7779 cp_id_kind dummy;
7780 cp_token *token;
7782 /* We're about to accept non-integral-constant things, but will
7783 definitely yield an integral constant expression. Save and
7784 restore these values around our local parsing. */
7785 save_ice_p = parser->integral_constant_expression_p;
7786 save_non_ice_p = parser->non_integral_constant_expression_p;
7788 /* Consume the "__builtin_offsetof" token. */
7789 cp_lexer_consume_token (parser->lexer);
7790 /* Consume the opening `('. */
7791 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7792 /* Parse the type-id. */
7793 type = cp_parser_type_id (parser);
7794 /* Look for the `,'. */
7795 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7796 token = cp_lexer_peek_token (parser->lexer);
7798 /* Build the (type *)null that begins the traditional offsetof macro. */
7799 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7800 tf_warning_or_error);
7802 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7803 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7804 true, &dummy, token->location);
7805 while (true)
7807 token = cp_lexer_peek_token (parser->lexer);
7808 switch (token->type)
7810 case CPP_OPEN_SQUARE:
7811 /* offsetof-member-designator "[" expression "]" */
7812 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7813 break;
7815 case CPP_DEREF:
7816 /* offsetof-member-designator "->" identifier */
7817 expr = grok_array_decl (token->location, expr, integer_zero_node);
7818 /* FALLTHRU */
7820 case CPP_DOT:
7821 /* offsetof-member-designator "." identifier */
7822 cp_lexer_consume_token (parser->lexer);
7823 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7824 expr, true, &dummy,
7825 token->location);
7826 break;
7828 case CPP_CLOSE_PAREN:
7829 /* Consume the ")" token. */
7830 cp_lexer_consume_token (parser->lexer);
7831 goto success;
7833 default:
7834 /* Error. We know the following require will fail, but
7835 that gives the proper error message. */
7836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7837 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7838 expr = error_mark_node;
7839 goto failure;
7843 success:
7844 /* If we're processing a template, we can't finish the semantics yet.
7845 Otherwise we can fold the entire expression now. */
7846 if (processing_template_decl)
7847 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7848 else
7849 expr = finish_offsetof (expr);
7851 failure:
7852 parser->integral_constant_expression_p = save_ice_p;
7853 parser->non_integral_constant_expression_p = save_non_ice_p;
7855 return expr;
7858 /* Parse a trait expression.
7860 Returns a representation of the expression, the underlying type
7861 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7863 static tree
7864 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7866 cp_trait_kind kind;
7867 tree type1, type2 = NULL_TREE;
7868 bool binary = false;
7869 cp_decl_specifier_seq decl_specs;
7871 switch (keyword)
7873 case RID_HAS_NOTHROW_ASSIGN:
7874 kind = CPTK_HAS_NOTHROW_ASSIGN;
7875 break;
7876 case RID_HAS_NOTHROW_CONSTRUCTOR:
7877 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7878 break;
7879 case RID_HAS_NOTHROW_COPY:
7880 kind = CPTK_HAS_NOTHROW_COPY;
7881 break;
7882 case RID_HAS_TRIVIAL_ASSIGN:
7883 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7884 break;
7885 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7886 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7887 break;
7888 case RID_HAS_TRIVIAL_COPY:
7889 kind = CPTK_HAS_TRIVIAL_COPY;
7890 break;
7891 case RID_HAS_TRIVIAL_DESTRUCTOR:
7892 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7893 break;
7894 case RID_HAS_VIRTUAL_DESTRUCTOR:
7895 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7896 break;
7897 case RID_IS_ABSTRACT:
7898 kind = CPTK_IS_ABSTRACT;
7899 break;
7900 case RID_IS_BASE_OF:
7901 kind = CPTK_IS_BASE_OF;
7902 binary = true;
7903 break;
7904 case RID_IS_CLASS:
7905 kind = CPTK_IS_CLASS;
7906 break;
7907 case RID_IS_CONVERTIBLE_TO:
7908 kind = CPTK_IS_CONVERTIBLE_TO;
7909 binary = true;
7910 break;
7911 case RID_IS_EMPTY:
7912 kind = CPTK_IS_EMPTY;
7913 break;
7914 case RID_IS_ENUM:
7915 kind = CPTK_IS_ENUM;
7916 break;
7917 case RID_IS_FINAL:
7918 kind = CPTK_IS_FINAL;
7919 break;
7920 case RID_IS_LITERAL_TYPE:
7921 kind = CPTK_IS_LITERAL_TYPE;
7922 break;
7923 case RID_IS_POD:
7924 kind = CPTK_IS_POD;
7925 break;
7926 case RID_IS_POLYMORPHIC:
7927 kind = CPTK_IS_POLYMORPHIC;
7928 break;
7929 case RID_IS_STD_LAYOUT:
7930 kind = CPTK_IS_STD_LAYOUT;
7931 break;
7932 case RID_IS_TRIVIAL:
7933 kind = CPTK_IS_TRIVIAL;
7934 break;
7935 case RID_IS_UNION:
7936 kind = CPTK_IS_UNION;
7937 break;
7938 case RID_UNDERLYING_TYPE:
7939 kind = CPTK_UNDERLYING_TYPE;
7940 break;
7941 case RID_BASES:
7942 kind = CPTK_BASES;
7943 break;
7944 case RID_DIRECT_BASES:
7945 kind = CPTK_DIRECT_BASES;
7946 break;
7947 default:
7948 gcc_unreachable ();
7951 /* Consume the token. */
7952 cp_lexer_consume_token (parser->lexer);
7954 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7956 type1 = cp_parser_type_id (parser);
7958 if (type1 == error_mark_node)
7959 return error_mark_node;
7961 /* Build a trivial decl-specifier-seq. */
7962 clear_decl_specs (&decl_specs);
7963 decl_specs.type = type1;
7965 /* Call grokdeclarator to figure out what type this is. */
7966 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7967 /*initialized=*/0, /*attrlist=*/NULL);
7969 if (binary)
7971 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7973 type2 = cp_parser_type_id (parser);
7975 if (type2 == error_mark_node)
7976 return error_mark_node;
7978 /* Build a trivial decl-specifier-seq. */
7979 clear_decl_specs (&decl_specs);
7980 decl_specs.type = type2;
7982 /* Call grokdeclarator to figure out what type this is. */
7983 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7984 /*initialized=*/0, /*attrlist=*/NULL);
7987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7989 /* Complete the trait expression, which may mean either processing
7990 the trait expr now or saving it for template instantiation. */
7991 switch(kind)
7993 case CPTK_UNDERLYING_TYPE:
7994 return finish_underlying_type (type1);
7995 case CPTK_BASES:
7996 return finish_bases (type1, false);
7997 case CPTK_DIRECT_BASES:
7998 return finish_bases (type1, true);
7999 default:
8000 return finish_trait_expr (kind, type1, type2);
8004 /* Lambdas that appear in variable initializer or default argument scope
8005 get that in their mangling, so we need to record it. We might as well
8006 use the count for function and namespace scopes as well. */
8007 static GTY(()) tree lambda_scope;
8008 static GTY(()) int lambda_count;
8009 typedef struct GTY(()) tree_int
8011 tree t;
8012 int i;
8013 } tree_int;
8014 DEF_VEC_O(tree_int);
8015 DEF_VEC_ALLOC_O(tree_int,gc);
8016 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8018 static void
8019 start_lambda_scope (tree decl)
8021 tree_int ti;
8022 gcc_assert (decl);
8023 /* Once we're inside a function, we ignore other scopes and just push
8024 the function again so that popping works properly. */
8025 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8026 decl = current_function_decl;
8027 ti.t = lambda_scope;
8028 ti.i = lambda_count;
8029 VEC_safe_push (tree_int, gc, lambda_scope_stack, ti);
8030 if (lambda_scope != decl)
8032 /* Don't reset the count if we're still in the same function. */
8033 lambda_scope = decl;
8034 lambda_count = 0;
8038 static void
8039 record_lambda_scope (tree lambda)
8041 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8042 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8045 static void
8046 finish_lambda_scope (void)
8048 tree_int *p = &VEC_last (tree_int, lambda_scope_stack);
8049 if (lambda_scope != p->t)
8051 lambda_scope = p->t;
8052 lambda_count = p->i;
8054 VEC_pop (tree_int, lambda_scope_stack);
8057 /* Parse a lambda expression.
8059 lambda-expression:
8060 lambda-introducer lambda-declarator [opt] compound-statement
8062 Returns a representation of the expression. */
8064 static tree
8065 cp_parser_lambda_expression (cp_parser* parser)
8067 tree lambda_expr = build_lambda_expr ();
8068 tree type;
8069 bool ok;
8071 LAMBDA_EXPR_LOCATION (lambda_expr)
8072 = cp_lexer_peek_token (parser->lexer)->location;
8074 if (cp_unevaluated_operand)
8075 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8076 "lambda-expression in unevaluated context");
8078 /* We may be in the middle of deferred access check. Disable
8079 it now. */
8080 push_deferring_access_checks (dk_no_deferred);
8082 cp_parser_lambda_introducer (parser, lambda_expr);
8084 type = begin_lambda_type (lambda_expr);
8085 if (type == error_mark_node)
8086 return error_mark_node;
8088 record_lambda_scope (lambda_expr);
8090 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8091 determine_visibility (TYPE_NAME (type));
8093 /* Now that we've started the type, add the capture fields for any
8094 explicit captures. */
8095 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8098 /* Inside the class, surrounding template-parameter-lists do not apply. */
8099 unsigned int saved_num_template_parameter_lists
8100 = parser->num_template_parameter_lists;
8101 unsigned char in_statement = parser->in_statement;
8102 bool in_switch_statement_p = parser->in_switch_statement_p;
8104 parser->num_template_parameter_lists = 0;
8105 parser->in_statement = 0;
8106 parser->in_switch_statement_p = false;
8108 /* By virtue of defining a local class, a lambda expression has access to
8109 the private variables of enclosing classes. */
8111 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8113 if (ok)
8114 cp_parser_lambda_body (parser, lambda_expr);
8115 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8116 cp_parser_skip_to_end_of_block_or_statement (parser);
8118 /* The capture list was built up in reverse order; fix that now. */
8120 tree newlist = NULL_TREE;
8121 tree elt, next;
8123 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8124 elt; elt = next)
8126 next = TREE_CHAIN (elt);
8127 TREE_CHAIN (elt) = newlist;
8128 newlist = elt;
8130 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8133 if (ok)
8134 maybe_add_lambda_conv_op (type);
8136 type = finish_struct (type, /*attributes=*/NULL_TREE);
8138 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8139 parser->in_statement = in_statement;
8140 parser->in_switch_statement_p = in_switch_statement_p;
8143 pop_deferring_access_checks ();
8145 /* This field is only used during parsing of the lambda. */
8146 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8148 /* This lambda shouldn't have any proxies left at this point. */
8149 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8150 /* And now that we're done, push proxies for an enclosing lambda. */
8151 insert_pending_capture_proxies ();
8153 if (ok)
8154 return build_lambda_object (lambda_expr);
8155 else
8156 return error_mark_node;
8159 /* Parse the beginning of a lambda expression.
8161 lambda-introducer:
8162 [ lambda-capture [opt] ]
8164 LAMBDA_EXPR is the current representation of the lambda expression. */
8166 static void
8167 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8169 /* Need commas after the first capture. */
8170 bool first = true;
8172 /* Eat the leading `['. */
8173 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8175 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8176 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8177 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8178 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8179 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8180 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8182 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8184 cp_lexer_consume_token (parser->lexer);
8185 first = false;
8188 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8190 cp_token* capture_token;
8191 tree capture_id;
8192 tree capture_init_expr;
8193 cp_id_kind idk = CP_ID_KIND_NONE;
8194 bool explicit_init_p = false;
8196 enum capture_kind_type
8198 BY_COPY,
8199 BY_REFERENCE
8201 enum capture_kind_type capture_kind = BY_COPY;
8203 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8205 error ("expected end of capture-list");
8206 return;
8209 if (first)
8210 first = false;
8211 else
8212 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8214 /* Possibly capture `this'. */
8215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8217 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8218 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8219 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8220 "with by-copy capture default");
8221 cp_lexer_consume_token (parser->lexer);
8222 add_capture (lambda_expr,
8223 /*id=*/this_identifier,
8224 /*initializer=*/finish_this_expr(),
8225 /*by_reference_p=*/false,
8226 explicit_init_p);
8227 continue;
8230 /* Remember whether we want to capture as a reference or not. */
8231 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8233 capture_kind = BY_REFERENCE;
8234 cp_lexer_consume_token (parser->lexer);
8237 /* Get the identifier. */
8238 capture_token = cp_lexer_peek_token (parser->lexer);
8239 capture_id = cp_parser_identifier (parser);
8241 if (capture_id == error_mark_node)
8242 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8243 delimiters, but I modified this to stop on unnested ']' as well. It
8244 was already changed to stop on unnested '}', so the
8245 "closing_parenthesis" name is no more misleading with my change. */
8247 cp_parser_skip_to_closing_parenthesis (parser,
8248 /*recovering=*/true,
8249 /*or_comma=*/true,
8250 /*consume_paren=*/true);
8251 break;
8254 /* Find the initializer for this capture. */
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8257 /* An explicit expression exists. */
8258 cp_lexer_consume_token (parser->lexer);
8259 pedwarn (input_location, OPT_Wpedantic,
8260 "ISO C++ does not allow initializers "
8261 "in lambda expression capture lists");
8262 capture_init_expr = cp_parser_assignment_expression (parser,
8263 /*cast_p=*/true,
8264 &idk);
8265 explicit_init_p = true;
8267 else
8269 const char* error_msg;
8271 /* Turn the identifier into an id-expression. */
8272 capture_init_expr
8273 = cp_parser_lookup_name
8274 (parser,
8275 capture_id,
8276 none_type,
8277 /*is_template=*/false,
8278 /*is_namespace=*/false,
8279 /*check_dependency=*/true,
8280 /*ambiguous_decls=*/NULL,
8281 capture_token->location);
8283 if (capture_init_expr == error_mark_node)
8285 unqualified_name_lookup_error (capture_id);
8286 continue;
8288 else if (DECL_P (capture_init_expr)
8289 && (TREE_CODE (capture_init_expr) != VAR_DECL
8290 && TREE_CODE (capture_init_expr) != PARM_DECL))
8292 error_at (capture_token->location,
8293 "capture of non-variable %qD ",
8294 capture_init_expr);
8295 inform (0, "%q+#D declared here", capture_init_expr);
8296 continue;
8298 if (TREE_CODE (capture_init_expr) == VAR_DECL
8299 && decl_storage_duration (capture_init_expr) != dk_auto)
8301 pedwarn (capture_token->location, 0, "capture of variable "
8302 "%qD with non-automatic storage duration",
8303 capture_init_expr);
8304 inform (0, "%q+#D declared here", capture_init_expr);
8305 continue;
8308 capture_init_expr
8309 = finish_id_expression
8310 (capture_id,
8311 capture_init_expr,
8312 parser->scope,
8313 &idk,
8314 /*integral_constant_expression_p=*/false,
8315 /*allow_non_integral_constant_expression_p=*/false,
8316 /*non_integral_constant_expression_p=*/NULL,
8317 /*template_p=*/false,
8318 /*done=*/true,
8319 /*address_p=*/false,
8320 /*template_arg_p=*/false,
8321 &error_msg,
8322 capture_token->location);
8325 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8326 && !explicit_init_p)
8328 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8329 && capture_kind == BY_COPY)
8330 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8331 "of %qD redundant with by-copy capture default",
8332 capture_id);
8333 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8334 && capture_kind == BY_REFERENCE)
8335 pedwarn (capture_token->location, 0, "explicit by-reference "
8336 "capture of %qD redundant with by-reference capture "
8337 "default", capture_id);
8340 add_capture (lambda_expr,
8341 capture_id,
8342 capture_init_expr,
8343 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8344 explicit_init_p);
8347 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8350 /* Parse the (optional) middle of a lambda expression.
8352 lambda-declarator:
8353 ( parameter-declaration-clause [opt] )
8354 attribute-specifier [opt]
8355 mutable [opt]
8356 exception-specification [opt]
8357 lambda-return-type-clause [opt]
8359 LAMBDA_EXPR is the current representation of the lambda expression. */
8361 static bool
8362 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8364 /* 5.1.1.4 of the standard says:
8365 If a lambda-expression does not include a lambda-declarator, it is as if
8366 the lambda-declarator were ().
8367 This means an empty parameter list, no attributes, and no exception
8368 specification. */
8369 tree param_list = void_list_node;
8370 tree attributes = NULL_TREE;
8371 tree exception_spec = NULL_TREE;
8372 tree t;
8374 /* The lambda-declarator is optional, but must begin with an opening
8375 parenthesis if present. */
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8378 cp_lexer_consume_token (parser->lexer);
8380 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8382 /* Parse parameters. */
8383 param_list = cp_parser_parameter_declaration_clause (parser);
8385 /* Default arguments shall not be specified in the
8386 parameter-declaration-clause of a lambda-declarator. */
8387 for (t = param_list; t; t = TREE_CHAIN (t))
8388 if (TREE_PURPOSE (t))
8389 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8390 "default argument specified for lambda parameter");
8392 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8394 attributes = cp_parser_attributes_opt (parser);
8396 /* Parse optional `mutable' keyword. */
8397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8399 cp_lexer_consume_token (parser->lexer);
8400 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8403 /* Parse optional exception specification. */
8404 exception_spec = cp_parser_exception_specification_opt (parser);
8406 /* Parse optional trailing return type. */
8407 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8409 cp_lexer_consume_token (parser->lexer);
8410 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8413 /* The function parameters must be in scope all the way until after the
8414 trailing-return-type in case of decltype. */
8415 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8416 pop_binding (DECL_NAME (t), t);
8418 leave_scope ();
8421 /* Create the function call operator.
8423 Messing with declarators like this is no uglier than building up the
8424 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8425 other code. */
8427 cp_decl_specifier_seq return_type_specs;
8428 cp_declarator* declarator;
8429 tree fco;
8430 int quals;
8431 void *p;
8433 clear_decl_specs (&return_type_specs);
8434 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8435 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8436 else
8437 /* Maybe we will deduce the return type later. */
8438 return_type_specs.type = make_auto ();
8440 p = obstack_alloc (&declarator_obstack, 0);
8442 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8443 sfk_none);
8445 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8446 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8447 declarator = make_call_declarator (declarator, param_list, quals,
8448 VIRT_SPEC_UNSPECIFIED,
8449 exception_spec,
8450 /*late_return_type=*/NULL_TREE);
8451 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8453 fco = grokmethod (&return_type_specs,
8454 declarator,
8455 attributes);
8456 if (fco != error_mark_node)
8458 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8459 DECL_ARTIFICIAL (fco) = 1;
8460 /* Give the object parameter a different name. */
8461 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8464 finish_member_declaration (fco);
8466 obstack_free (&declarator_obstack, p);
8468 return (fco != error_mark_node);
8472 /* Parse the body of a lambda expression, which is simply
8474 compound-statement
8476 but which requires special handling.
8477 LAMBDA_EXPR is the current representation of the lambda expression. */
8479 static void
8480 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8482 bool nested = (current_function_decl != NULL_TREE);
8483 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8484 if (nested)
8485 push_function_context ();
8486 else
8487 /* Still increment function_depth so that we don't GC in the
8488 middle of an expression. */
8489 ++function_depth;
8490 /* Clear this in case we're in the middle of a default argument. */
8491 parser->local_variables_forbidden_p = false;
8493 /* Finish the function call operator
8494 - class_specifier
8495 + late_parsing_for_member
8496 + function_definition_after_declarator
8497 + ctor_initializer_opt_and_function_body */
8499 tree fco = lambda_function (lambda_expr);
8500 tree body;
8501 bool done = false;
8502 tree compound_stmt;
8503 tree cap;
8505 /* Let the front end know that we are going to be defining this
8506 function. */
8507 start_preparsed_function (fco,
8508 NULL_TREE,
8509 SF_PRE_PARSED | SF_INCLASS_INLINE);
8511 start_lambda_scope (fco);
8512 body = begin_function_body ();
8514 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8515 goto out;
8517 /* Push the proxies for any explicit captures. */
8518 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8519 cap = TREE_CHAIN (cap))
8520 build_capture_proxy (TREE_PURPOSE (cap));
8522 compound_stmt = begin_compound_stmt (0);
8524 /* 5.1.1.4 of the standard says:
8525 If a lambda-expression does not include a trailing-return-type, it
8526 is as if the trailing-return-type denotes the following type:
8527 * if the compound-statement is of the form
8528 { return attribute-specifier [opt] expression ; }
8529 the type of the returned expression after lvalue-to-rvalue
8530 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8531 (_conv.array_ 4.2), and function-to-pointer conversion
8532 (_conv.func_ 4.3);
8533 * otherwise, void. */
8535 /* In a lambda that has neither a lambda-return-type-clause
8536 nor a deducible form, errors should be reported for return statements
8537 in the body. Since we used void as the placeholder return type, parsing
8538 the body as usual will give such desired behavior. */
8539 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8540 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8541 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8543 tree expr = NULL_TREE;
8544 cp_id_kind idk = CP_ID_KIND_NONE;
8546 /* Parse tentatively in case there's more after the initial return
8547 statement. */
8548 cp_parser_parse_tentatively (parser);
8550 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8552 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8555 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8557 if (cp_parser_parse_definitely (parser))
8559 if (!processing_template_decl)
8560 apply_deduced_return_type (fco, lambda_return_type (expr));
8562 /* Will get error here if type not deduced yet. */
8563 finish_return_stmt (expr);
8565 done = true;
8569 if (!done)
8571 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8572 cp_parser_label_declaration (parser);
8573 cp_parser_statement_seq_opt (parser, NULL_TREE);
8574 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8577 finish_compound_stmt (compound_stmt);
8579 out:
8580 finish_function_body (body);
8581 finish_lambda_scope ();
8583 /* Finish the function and generate code for it if necessary. */
8584 expand_or_defer_fn (finish_function (/*inline*/2));
8587 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8588 if (nested)
8589 pop_function_context();
8590 else
8591 --function_depth;
8594 /* Statements [gram.stmt.stmt] */
8596 /* Parse a statement.
8598 statement:
8599 labeled-statement
8600 expression-statement
8601 compound-statement
8602 selection-statement
8603 iteration-statement
8604 jump-statement
8605 declaration-statement
8606 try-block
8608 TM Extension:
8610 statement:
8611 atomic-statement
8613 IN_COMPOUND is true when the statement is nested inside a
8614 cp_parser_compound_statement; this matters for certain pragmas.
8616 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8617 is a (possibly labeled) if statement which is not enclosed in braces
8618 and has an else clause. This is used to implement -Wparentheses. */
8620 static void
8621 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8622 bool in_compound, bool *if_p)
8624 tree statement;
8625 cp_token *token;
8626 location_t statement_location;
8628 restart:
8629 if (if_p != NULL)
8630 *if_p = false;
8631 /* There is no statement yet. */
8632 statement = NULL_TREE;
8633 /* Peek at the next token. */
8634 token = cp_lexer_peek_token (parser->lexer);
8635 /* Remember the location of the first token in the statement. */
8636 statement_location = token->location;
8637 /* If this is a keyword, then that will often determine what kind of
8638 statement we have. */
8639 if (token->type == CPP_KEYWORD)
8641 enum rid keyword = token->keyword;
8643 switch (keyword)
8645 case RID_CASE:
8646 case RID_DEFAULT:
8647 /* Looks like a labeled-statement with a case label.
8648 Parse the label, and then use tail recursion to parse
8649 the statement. */
8650 cp_parser_label_for_labeled_statement (parser);
8651 goto restart;
8653 case RID_IF:
8654 case RID_SWITCH:
8655 statement = cp_parser_selection_statement (parser, if_p);
8656 break;
8658 case RID_WHILE:
8659 case RID_DO:
8660 case RID_FOR:
8661 statement = cp_parser_iteration_statement (parser);
8662 break;
8664 case RID_BREAK:
8665 case RID_CONTINUE:
8666 case RID_RETURN:
8667 case RID_GOTO:
8668 statement = cp_parser_jump_statement (parser);
8669 break;
8671 /* Objective-C++ exception-handling constructs. */
8672 case RID_AT_TRY:
8673 case RID_AT_CATCH:
8674 case RID_AT_FINALLY:
8675 case RID_AT_SYNCHRONIZED:
8676 case RID_AT_THROW:
8677 statement = cp_parser_objc_statement (parser);
8678 break;
8680 case RID_TRY:
8681 statement = cp_parser_try_block (parser);
8682 break;
8684 case RID_NAMESPACE:
8685 /* This must be a namespace alias definition. */
8686 cp_parser_declaration_statement (parser);
8687 return;
8689 case RID_TRANSACTION_ATOMIC:
8690 case RID_TRANSACTION_RELAXED:
8691 statement = cp_parser_transaction (parser, keyword);
8692 break;
8693 case RID_TRANSACTION_CANCEL:
8694 statement = cp_parser_transaction_cancel (parser);
8695 break;
8697 default:
8698 /* It might be a keyword like `int' that can start a
8699 declaration-statement. */
8700 break;
8703 else if (token->type == CPP_NAME)
8705 /* If the next token is a `:', then we are looking at a
8706 labeled-statement. */
8707 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8708 if (token->type == CPP_COLON)
8710 /* Looks like a labeled-statement with an ordinary label.
8711 Parse the label, and then use tail recursion to parse
8712 the statement. */
8713 cp_parser_label_for_labeled_statement (parser);
8714 goto restart;
8717 /* Anything that starts with a `{' must be a compound-statement. */
8718 else if (token->type == CPP_OPEN_BRACE)
8719 statement = cp_parser_compound_statement (parser, NULL, false, false);
8720 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8721 a statement all its own. */
8722 else if (token->type == CPP_PRAGMA)
8724 /* Only certain OpenMP pragmas are attached to statements, and thus
8725 are considered statements themselves. All others are not. In
8726 the context of a compound, accept the pragma as a "statement" and
8727 return so that we can check for a close brace. Otherwise we
8728 require a real statement and must go back and read one. */
8729 if (in_compound)
8730 cp_parser_pragma (parser, pragma_compound);
8731 else if (!cp_parser_pragma (parser, pragma_stmt))
8732 goto restart;
8733 return;
8735 else if (token->type == CPP_EOF)
8737 cp_parser_error (parser, "expected statement");
8738 return;
8741 /* Everything else must be a declaration-statement or an
8742 expression-statement. Try for the declaration-statement
8743 first, unless we are looking at a `;', in which case we know that
8744 we have an expression-statement. */
8745 if (!statement)
8747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8749 cp_parser_parse_tentatively (parser);
8750 /* Try to parse the declaration-statement. */
8751 cp_parser_declaration_statement (parser);
8752 /* If that worked, we're done. */
8753 if (cp_parser_parse_definitely (parser))
8754 return;
8756 /* Look for an expression-statement instead. */
8757 statement = cp_parser_expression_statement (parser, in_statement_expr);
8760 /* Set the line number for the statement. */
8761 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8762 SET_EXPR_LOCATION (statement, statement_location);
8765 /* Parse the label for a labeled-statement, i.e.
8767 identifier :
8768 case constant-expression :
8769 default :
8771 GNU Extension:
8772 case constant-expression ... constant-expression : statement
8774 When a label is parsed without errors, the label is added to the
8775 parse tree by the finish_* functions, so this function doesn't
8776 have to return the label. */
8778 static void
8779 cp_parser_label_for_labeled_statement (cp_parser* parser)
8781 cp_token *token;
8782 tree label = NULL_TREE;
8783 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8785 /* The next token should be an identifier. */
8786 token = cp_lexer_peek_token (parser->lexer);
8787 if (token->type != CPP_NAME
8788 && token->type != CPP_KEYWORD)
8790 cp_parser_error (parser, "expected labeled-statement");
8791 return;
8794 parser->colon_corrects_to_scope_p = false;
8795 switch (token->keyword)
8797 case RID_CASE:
8799 tree expr, expr_hi;
8800 cp_token *ellipsis;
8802 /* Consume the `case' token. */
8803 cp_lexer_consume_token (parser->lexer);
8804 /* Parse the constant-expression. */
8805 expr = cp_parser_constant_expression (parser,
8806 /*allow_non_constant_p=*/false,
8807 NULL);
8809 ellipsis = cp_lexer_peek_token (parser->lexer);
8810 if (ellipsis->type == CPP_ELLIPSIS)
8812 /* Consume the `...' token. */
8813 cp_lexer_consume_token (parser->lexer);
8814 expr_hi =
8815 cp_parser_constant_expression (parser,
8816 /*allow_non_constant_p=*/false,
8817 NULL);
8818 /* We don't need to emit warnings here, as the common code
8819 will do this for us. */
8821 else
8822 expr_hi = NULL_TREE;
8824 if (parser->in_switch_statement_p)
8825 finish_case_label (token->location, expr, expr_hi);
8826 else
8827 error_at (token->location,
8828 "case label %qE not within a switch statement",
8829 expr);
8831 break;
8833 case RID_DEFAULT:
8834 /* Consume the `default' token. */
8835 cp_lexer_consume_token (parser->lexer);
8837 if (parser->in_switch_statement_p)
8838 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8839 else
8840 error_at (token->location, "case label not within a switch statement");
8841 break;
8843 default:
8844 /* Anything else must be an ordinary label. */
8845 label = finish_label_stmt (cp_parser_identifier (parser));
8846 break;
8849 /* Require the `:' token. */
8850 cp_parser_require (parser, CPP_COLON, RT_COLON);
8852 /* An ordinary label may optionally be followed by attributes.
8853 However, this is only permitted if the attributes are then
8854 followed by a semicolon. This is because, for backward
8855 compatibility, when parsing
8856 lab: __attribute__ ((unused)) int i;
8857 we want the attribute to attach to "i", not "lab". */
8858 if (label != NULL_TREE
8859 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8861 tree attrs;
8863 cp_parser_parse_tentatively (parser);
8864 attrs = cp_parser_attributes_opt (parser);
8865 if (attrs == NULL_TREE
8866 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8867 cp_parser_abort_tentative_parse (parser);
8868 else if (!cp_parser_parse_definitely (parser))
8870 else
8871 cplus_decl_attributes (&label, attrs, 0);
8874 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8877 /* Parse an expression-statement.
8879 expression-statement:
8880 expression [opt] ;
8882 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8883 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8884 indicates whether this expression-statement is part of an
8885 expression statement. */
8887 static tree
8888 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8890 tree statement = NULL_TREE;
8891 cp_token *token = cp_lexer_peek_token (parser->lexer);
8893 /* If the next token is a ';', then there is no expression
8894 statement. */
8895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8896 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8898 /* Give a helpful message for "A<T>::type t;" and the like. */
8899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8900 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8902 if (TREE_CODE (statement) == SCOPE_REF)
8903 error_at (token->location, "need %<typename%> before %qE because "
8904 "%qT is a dependent scope",
8905 statement, TREE_OPERAND (statement, 0));
8906 else if (is_overloaded_fn (statement)
8907 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8909 /* A::A a; */
8910 tree fn = get_first_fn (statement);
8911 error_at (token->location,
8912 "%<%T::%D%> names the constructor, not the type",
8913 DECL_CONTEXT (fn), DECL_NAME (fn));
8917 /* Consume the final `;'. */
8918 cp_parser_consume_semicolon_at_end_of_statement (parser);
8920 if (in_statement_expr
8921 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8922 /* This is the final expression statement of a statement
8923 expression. */
8924 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8925 else if (statement)
8926 statement = finish_expr_stmt (statement);
8927 else
8928 finish_stmt ();
8930 return statement;
8933 /* Parse a compound-statement.
8935 compound-statement:
8936 { statement-seq [opt] }
8938 GNU extension:
8940 compound-statement:
8941 { label-declaration-seq [opt] statement-seq [opt] }
8943 label-declaration-seq:
8944 label-declaration
8945 label-declaration-seq label-declaration
8947 Returns a tree representing the statement. */
8949 static tree
8950 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8951 bool in_try, bool function_body)
8953 tree compound_stmt;
8955 /* Consume the `{'. */
8956 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8957 return error_mark_node;
8958 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8959 && !function_body)
8960 pedwarn (input_location, OPT_Wpedantic,
8961 "compound-statement in constexpr function");
8962 /* Begin the compound-statement. */
8963 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8964 /* If the next keyword is `__label__' we have a label declaration. */
8965 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8966 cp_parser_label_declaration (parser);
8967 /* Parse an (optional) statement-seq. */
8968 cp_parser_statement_seq_opt (parser, in_statement_expr);
8969 /* Finish the compound-statement. */
8970 finish_compound_stmt (compound_stmt);
8971 /* Consume the `}'. */
8972 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8974 return compound_stmt;
8977 /* Parse an (optional) statement-seq.
8979 statement-seq:
8980 statement
8981 statement-seq [opt] statement */
8983 static void
8984 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8986 /* Scan statements until there aren't any more. */
8987 while (true)
8989 cp_token *token = cp_lexer_peek_token (parser->lexer);
8991 /* If we are looking at a `}', then we have run out of
8992 statements; the same is true if we have reached the end
8993 of file, or have stumbled upon a stray '@end'. */
8994 if (token->type == CPP_CLOSE_BRACE
8995 || token->type == CPP_EOF
8996 || token->type == CPP_PRAGMA_EOL
8997 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8998 break;
9000 /* If we are in a compound statement and find 'else' then
9001 something went wrong. */
9002 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9004 if (parser->in_statement & IN_IF_STMT)
9005 break;
9006 else
9008 token = cp_lexer_consume_token (parser->lexer);
9009 error_at (token->location, "%<else%> without a previous %<if%>");
9013 /* Parse the statement. */
9014 cp_parser_statement (parser, in_statement_expr, true, NULL);
9018 /* Parse a selection-statement.
9020 selection-statement:
9021 if ( condition ) statement
9022 if ( condition ) statement else statement
9023 switch ( condition ) statement
9025 Returns the new IF_STMT or SWITCH_STMT.
9027 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9028 is a (possibly labeled) if statement which is not enclosed in
9029 braces and has an else clause. This is used to implement
9030 -Wparentheses. */
9032 static tree
9033 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9035 cp_token *token;
9036 enum rid keyword;
9038 if (if_p != NULL)
9039 *if_p = false;
9041 /* Peek at the next token. */
9042 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9044 /* See what kind of keyword it is. */
9045 keyword = token->keyword;
9046 switch (keyword)
9048 case RID_IF:
9049 case RID_SWITCH:
9051 tree statement;
9052 tree condition;
9054 /* Look for the `('. */
9055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9057 cp_parser_skip_to_end_of_statement (parser);
9058 return error_mark_node;
9061 /* Begin the selection-statement. */
9062 if (keyword == RID_IF)
9063 statement = begin_if_stmt ();
9064 else
9065 statement = begin_switch_stmt ();
9067 /* Parse the condition. */
9068 condition = cp_parser_condition (parser);
9069 /* Look for the `)'. */
9070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9071 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9072 /*consume_paren=*/true);
9074 if (keyword == RID_IF)
9076 bool nested_if;
9077 unsigned char in_statement;
9079 /* Add the condition. */
9080 finish_if_stmt_cond (condition, statement);
9082 /* Parse the then-clause. */
9083 in_statement = parser->in_statement;
9084 parser->in_statement |= IN_IF_STMT;
9085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9087 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9088 add_stmt (build_empty_stmt (loc));
9089 cp_lexer_consume_token (parser->lexer);
9090 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9091 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9092 "empty body in an %<if%> statement");
9093 nested_if = false;
9095 else
9096 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9097 parser->in_statement = in_statement;
9099 finish_then_clause (statement);
9101 /* If the next token is `else', parse the else-clause. */
9102 if (cp_lexer_next_token_is_keyword (parser->lexer,
9103 RID_ELSE))
9105 /* Consume the `else' keyword. */
9106 cp_lexer_consume_token (parser->lexer);
9107 begin_else_clause (statement);
9108 /* Parse the else-clause. */
9109 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9111 location_t loc;
9112 loc = cp_lexer_peek_token (parser->lexer)->location;
9113 warning_at (loc,
9114 OPT_Wempty_body, "suggest braces around "
9115 "empty body in an %<else%> statement");
9116 add_stmt (build_empty_stmt (loc));
9117 cp_lexer_consume_token (parser->lexer);
9119 else
9120 cp_parser_implicitly_scoped_statement (parser, NULL);
9122 finish_else_clause (statement);
9124 /* If we are currently parsing a then-clause, then
9125 IF_P will not be NULL. We set it to true to
9126 indicate that this if statement has an else clause.
9127 This may trigger the Wparentheses warning below
9128 when we get back up to the parent if statement. */
9129 if (if_p != NULL)
9130 *if_p = true;
9132 else
9134 /* This if statement does not have an else clause. If
9135 NESTED_IF is true, then the then-clause is an if
9136 statement which does have an else clause. We warn
9137 about the potential ambiguity. */
9138 if (nested_if)
9139 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9140 "suggest explicit braces to avoid ambiguous"
9141 " %<else%>");
9144 /* Now we're all done with the if-statement. */
9145 finish_if_stmt (statement);
9147 else
9149 bool in_switch_statement_p;
9150 unsigned char in_statement;
9152 /* Add the condition. */
9153 finish_switch_cond (condition, statement);
9155 /* Parse the body of the switch-statement. */
9156 in_switch_statement_p = parser->in_switch_statement_p;
9157 in_statement = parser->in_statement;
9158 parser->in_switch_statement_p = true;
9159 parser->in_statement |= IN_SWITCH_STMT;
9160 cp_parser_implicitly_scoped_statement (parser, NULL);
9161 parser->in_switch_statement_p = in_switch_statement_p;
9162 parser->in_statement = in_statement;
9164 /* Now we're all done with the switch-statement. */
9165 finish_switch_stmt (statement);
9168 return statement;
9170 break;
9172 default:
9173 cp_parser_error (parser, "expected selection-statement");
9174 return error_mark_node;
9178 /* Parse a condition.
9180 condition:
9181 expression
9182 type-specifier-seq declarator = initializer-clause
9183 type-specifier-seq declarator braced-init-list
9185 GNU Extension:
9187 condition:
9188 type-specifier-seq declarator asm-specification [opt]
9189 attributes [opt] = assignment-expression
9191 Returns the expression that should be tested. */
9193 static tree
9194 cp_parser_condition (cp_parser* parser)
9196 cp_decl_specifier_seq type_specifiers;
9197 const char *saved_message;
9198 int declares_class_or_enum;
9200 /* Try the declaration first. */
9201 cp_parser_parse_tentatively (parser);
9202 /* New types are not allowed in the type-specifier-seq for a
9203 condition. */
9204 saved_message = parser->type_definition_forbidden_message;
9205 parser->type_definition_forbidden_message
9206 = G_("types may not be defined in conditions");
9207 /* Parse the type-specifier-seq. */
9208 cp_parser_decl_specifier_seq (parser,
9209 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9210 &type_specifiers,
9211 &declares_class_or_enum);
9212 /* Restore the saved message. */
9213 parser->type_definition_forbidden_message = saved_message;
9214 /* If all is well, we might be looking at a declaration. */
9215 if (!cp_parser_error_occurred (parser))
9217 tree decl;
9218 tree asm_specification;
9219 tree attributes;
9220 cp_declarator *declarator;
9221 tree initializer = NULL_TREE;
9223 /* Parse the declarator. */
9224 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9225 /*ctor_dtor_or_conv_p=*/NULL,
9226 /*parenthesized_p=*/NULL,
9227 /*member_p=*/false);
9228 /* Parse the attributes. */
9229 attributes = cp_parser_attributes_opt (parser);
9230 /* Parse the asm-specification. */
9231 asm_specification = cp_parser_asm_specification_opt (parser);
9232 /* If the next token is not an `=' or '{', then we might still be
9233 looking at an expression. For example:
9235 if (A(a).x)
9237 looks like a decl-specifier-seq and a declarator -- but then
9238 there is no `=', so this is an expression. */
9239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9240 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9241 cp_parser_simulate_error (parser);
9243 /* If we did see an `=' or '{', then we are looking at a declaration
9244 for sure. */
9245 if (cp_parser_parse_definitely (parser))
9247 tree pushed_scope;
9248 bool non_constant_p;
9249 bool flags = LOOKUP_ONLYCONVERTING;
9251 /* Create the declaration. */
9252 decl = start_decl (declarator, &type_specifiers,
9253 /*initialized_p=*/true,
9254 attributes, /*prefix_attributes=*/NULL_TREE,
9255 &pushed_scope);
9257 /* Parse the initializer. */
9258 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9260 initializer = cp_parser_braced_list (parser, &non_constant_p);
9261 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9262 flags = 0;
9264 else
9266 /* Consume the `='. */
9267 cp_parser_require (parser, CPP_EQ, RT_EQ);
9268 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9270 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9271 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9273 /* Process the initializer. */
9274 cp_finish_decl (decl,
9275 initializer, !non_constant_p,
9276 asm_specification,
9277 flags);
9279 if (pushed_scope)
9280 pop_scope (pushed_scope);
9282 return convert_from_reference (decl);
9285 /* If we didn't even get past the declarator successfully, we are
9286 definitely not looking at a declaration. */
9287 else
9288 cp_parser_abort_tentative_parse (parser);
9290 /* Otherwise, we are looking at an expression. */
9291 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9294 /* Parses a for-statement or range-for-statement until the closing ')',
9295 not included. */
9297 static tree
9298 cp_parser_for (cp_parser *parser)
9300 tree init, scope, decl;
9301 bool is_range_for;
9303 /* Begin the for-statement. */
9304 scope = begin_for_scope (&init);
9306 /* Parse the initialization. */
9307 is_range_for = cp_parser_for_init_statement (parser, &decl);
9309 if (is_range_for)
9310 return cp_parser_range_for (parser, scope, init, decl);
9311 else
9312 return cp_parser_c_for (parser, scope, init);
9315 static tree
9316 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9318 /* Normal for loop */
9319 tree condition = NULL_TREE;
9320 tree expression = NULL_TREE;
9321 tree stmt;
9323 stmt = begin_for_stmt (scope, init);
9324 /* The for-init-statement has already been parsed in
9325 cp_parser_for_init_statement, so no work is needed here. */
9326 finish_for_init_stmt (stmt);
9328 /* If there's a condition, process it. */
9329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9330 condition = cp_parser_condition (parser);
9331 finish_for_cond (condition, stmt);
9332 /* Look for the `;'. */
9333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9335 /* If there's an expression, process it. */
9336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9337 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9338 finish_for_expr (expression, stmt);
9340 return stmt;
9343 /* Tries to parse a range-based for-statement:
9345 range-based-for:
9346 decl-specifier-seq declarator : expression
9348 The decl-specifier-seq declarator and the `:' are already parsed by
9349 cp_parser_for_init_statement. If processing_template_decl it returns a
9350 newly created RANGE_FOR_STMT; if not, it is converted to a
9351 regular FOR_STMT. */
9353 static tree
9354 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9356 tree stmt, range_expr;
9358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9360 bool expr_non_constant_p;
9361 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9363 else
9364 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9366 /* If in template, STMT is converted to a normal for-statement
9367 at instantiation. If not, it is done just ahead. */
9368 if (processing_template_decl)
9370 if (check_for_bare_parameter_packs (range_expr))
9371 range_expr = error_mark_node;
9372 stmt = begin_range_for_stmt (scope, init);
9373 finish_range_for_decl (stmt, range_decl, range_expr);
9374 if (!type_dependent_expression_p (range_expr)
9375 /* do_auto_deduction doesn't mess with template init-lists. */
9376 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9377 do_range_for_auto_deduction (range_decl, range_expr);
9379 else
9381 stmt = begin_for_stmt (scope, init);
9382 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9384 return stmt;
9387 /* Subroutine of cp_convert_range_for: given the initializer expression,
9388 builds up the range temporary. */
9390 static tree
9391 build_range_temp (tree range_expr)
9393 tree range_type, range_temp;
9395 /* Find out the type deduced by the declaration
9396 `auto &&__range = range_expr'. */
9397 range_type = cp_build_reference_type (make_auto (), true);
9398 range_type = do_auto_deduction (range_type, range_expr,
9399 type_uses_auto (range_type));
9401 /* Create the __range variable. */
9402 range_temp = build_decl (input_location, VAR_DECL,
9403 get_identifier ("__for_range"), range_type);
9404 TREE_USED (range_temp) = 1;
9405 DECL_ARTIFICIAL (range_temp) = 1;
9407 return range_temp;
9410 /* Used by cp_parser_range_for in template context: we aren't going to
9411 do a full conversion yet, but we still need to resolve auto in the
9412 type of the for-range-declaration if present. This is basically
9413 a shortcut version of cp_convert_range_for. */
9415 static void
9416 do_range_for_auto_deduction (tree decl, tree range_expr)
9418 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9419 if (auto_node)
9421 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9422 range_temp = convert_from_reference (build_range_temp (range_expr));
9423 iter_type = (cp_parser_perform_range_for_lookup
9424 (range_temp, &begin_dummy, &end_dummy));
9425 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9426 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9427 tf_warning_or_error);
9428 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9429 iter_decl, auto_node);
9433 /* Converts a range-based for-statement into a normal
9434 for-statement, as per the definition.
9436 for (RANGE_DECL : RANGE_EXPR)
9437 BLOCK
9439 should be equivalent to:
9442 auto &&__range = RANGE_EXPR;
9443 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9444 __begin != __end;
9445 ++__begin)
9447 RANGE_DECL = *__begin;
9448 BLOCK
9452 If RANGE_EXPR is an array:
9453 BEGIN_EXPR = __range
9454 END_EXPR = __range + ARRAY_SIZE(__range)
9455 Else if RANGE_EXPR has a member 'begin' or 'end':
9456 BEGIN_EXPR = __range.begin()
9457 END_EXPR = __range.end()
9458 Else:
9459 BEGIN_EXPR = begin(__range)
9460 END_EXPR = end(__range);
9462 If __range has a member 'begin' but not 'end', or vice versa, we must
9463 still use the second alternative (it will surely fail, however).
9464 When calling begin()/end() in the third alternative we must use
9465 argument dependent lookup, but always considering 'std' as an associated
9466 namespace. */
9468 tree
9469 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9471 tree begin, end;
9472 tree iter_type, begin_expr, end_expr;
9473 tree condition, expression;
9475 if (range_decl == error_mark_node || range_expr == error_mark_node)
9476 /* If an error happened previously do nothing or else a lot of
9477 unhelpful errors would be issued. */
9478 begin_expr = end_expr = iter_type = error_mark_node;
9479 else
9481 tree range_temp = build_range_temp (range_expr);
9482 pushdecl (range_temp);
9483 cp_finish_decl (range_temp, range_expr,
9484 /*is_constant_init*/false, NULL_TREE,
9485 LOOKUP_ONLYCONVERTING);
9487 range_temp = convert_from_reference (range_temp);
9488 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9489 &begin_expr, &end_expr);
9492 /* The new for initialization statement. */
9493 begin = build_decl (input_location, VAR_DECL,
9494 get_identifier ("__for_begin"), iter_type);
9495 TREE_USED (begin) = 1;
9496 DECL_ARTIFICIAL (begin) = 1;
9497 pushdecl (begin);
9498 cp_finish_decl (begin, begin_expr,
9499 /*is_constant_init*/false, NULL_TREE,
9500 LOOKUP_ONLYCONVERTING);
9502 end = build_decl (input_location, VAR_DECL,
9503 get_identifier ("__for_end"), iter_type);
9504 TREE_USED (end) = 1;
9505 DECL_ARTIFICIAL (end) = 1;
9506 pushdecl (end);
9507 cp_finish_decl (end, end_expr,
9508 /*is_constant_init*/false, NULL_TREE,
9509 LOOKUP_ONLYCONVERTING);
9511 finish_for_init_stmt (statement);
9513 /* The new for condition. */
9514 condition = build_x_binary_op (input_location, NE_EXPR,
9515 begin, ERROR_MARK,
9516 end, ERROR_MARK,
9517 NULL, tf_warning_or_error);
9518 finish_for_cond (condition, statement);
9520 /* The new increment expression. */
9521 expression = finish_unary_op_expr (input_location,
9522 PREINCREMENT_EXPR, begin);
9523 finish_for_expr (expression, statement);
9525 /* The declaration is initialized with *__begin inside the loop body. */
9526 cp_finish_decl (range_decl,
9527 build_x_indirect_ref (input_location, begin, RO_NULL,
9528 tf_warning_or_error),
9529 /*is_constant_init*/false, NULL_TREE,
9530 LOOKUP_ONLYCONVERTING);
9532 return statement;
9535 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9536 We need to solve both at the same time because the method used
9537 depends on the existence of members begin or end.
9538 Returns the type deduced for the iterator expression. */
9540 static tree
9541 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9543 if (error_operand_p (range))
9545 *begin = *end = error_mark_node;
9546 return error_mark_node;
9549 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9551 error ("range-based %<for%> expression of type %qT "
9552 "has incomplete type", TREE_TYPE (range));
9553 *begin = *end = error_mark_node;
9554 return error_mark_node;
9556 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9558 /* If RANGE is an array, we will use pointer arithmetic. */
9559 *begin = range;
9560 *end = build_binary_op (input_location, PLUS_EXPR,
9561 range,
9562 array_type_nelts_top (TREE_TYPE (range)),
9564 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9566 else
9568 /* If it is not an array, we must do a bit of magic. */
9569 tree id_begin, id_end;
9570 tree member_begin, member_end;
9572 *begin = *end = error_mark_node;
9574 id_begin = get_identifier ("begin");
9575 id_end = get_identifier ("end");
9576 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9577 /*protect=*/2, /*want_type=*/false,
9578 tf_warning_or_error);
9579 member_end = lookup_member (TREE_TYPE (range), id_end,
9580 /*protect=*/2, /*want_type=*/false,
9581 tf_warning_or_error);
9583 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9585 /* Use the member functions. */
9586 if (member_begin != NULL_TREE)
9587 *begin = cp_parser_range_for_member_function (range, id_begin);
9588 else
9589 error ("range-based %<for%> expression of type %qT has an "
9590 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9592 if (member_end != NULL_TREE)
9593 *end = cp_parser_range_for_member_function (range, id_end);
9594 else
9595 error ("range-based %<for%> expression of type %qT has a "
9596 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9598 else
9600 /* Use global functions with ADL. */
9601 VEC(tree,gc) *vec;
9602 vec = make_tree_vector ();
9604 VEC_safe_push (tree, gc, vec, range);
9606 member_begin = perform_koenig_lookup (id_begin, vec,
9607 /*include_std=*/true,
9608 tf_warning_or_error);
9609 *begin = finish_call_expr (member_begin, &vec, false, true,
9610 tf_warning_or_error);
9611 member_end = perform_koenig_lookup (id_end, vec,
9612 /*include_std=*/true,
9613 tf_warning_or_error);
9614 *end = finish_call_expr (member_end, &vec, false, true,
9615 tf_warning_or_error);
9617 release_tree_vector (vec);
9620 /* Last common checks. */
9621 if (*begin == error_mark_node || *end == error_mark_node)
9623 /* If one of the expressions is an error do no more checks. */
9624 *begin = *end = error_mark_node;
9625 return error_mark_node;
9627 else
9629 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9630 /* The unqualified type of the __begin and __end temporaries should
9631 be the same, as required by the multiple auto declaration. */
9632 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9633 error ("inconsistent begin/end types in range-based %<for%> "
9634 "statement: %qT and %qT",
9635 TREE_TYPE (*begin), TREE_TYPE (*end));
9636 return iter_type;
9641 /* Helper function for cp_parser_perform_range_for_lookup.
9642 Builds a tree for RANGE.IDENTIFIER(). */
9644 static tree
9645 cp_parser_range_for_member_function (tree range, tree identifier)
9647 tree member, res;
9648 VEC(tree,gc) *vec;
9650 member = finish_class_member_access_expr (range, identifier,
9651 false, tf_warning_or_error);
9652 if (member == error_mark_node)
9653 return error_mark_node;
9655 vec = make_tree_vector ();
9656 res = finish_call_expr (member, &vec,
9657 /*disallow_virtual=*/false,
9658 /*koenig_p=*/false,
9659 tf_warning_or_error);
9660 release_tree_vector (vec);
9661 return res;
9664 /* Parse an iteration-statement.
9666 iteration-statement:
9667 while ( condition ) statement
9668 do statement while ( expression ) ;
9669 for ( for-init-statement condition [opt] ; expression [opt] )
9670 statement
9672 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9674 static tree
9675 cp_parser_iteration_statement (cp_parser* parser)
9677 cp_token *token;
9678 enum rid keyword;
9679 tree statement;
9680 unsigned char in_statement;
9682 /* Peek at the next token. */
9683 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9684 if (!token)
9685 return error_mark_node;
9687 /* Remember whether or not we are already within an iteration
9688 statement. */
9689 in_statement = parser->in_statement;
9691 /* See what kind of keyword it is. */
9692 keyword = token->keyword;
9693 switch (keyword)
9695 case RID_WHILE:
9697 tree condition;
9699 /* Begin the while-statement. */
9700 statement = begin_while_stmt ();
9701 /* Look for the `('. */
9702 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9703 /* Parse the condition. */
9704 condition = cp_parser_condition (parser);
9705 finish_while_stmt_cond (condition, statement);
9706 /* Look for the `)'. */
9707 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9708 /* Parse the dependent statement. */
9709 parser->in_statement = IN_ITERATION_STMT;
9710 cp_parser_already_scoped_statement (parser);
9711 parser->in_statement = in_statement;
9712 /* We're done with the while-statement. */
9713 finish_while_stmt (statement);
9715 break;
9717 case RID_DO:
9719 tree expression;
9721 /* Begin the do-statement. */
9722 statement = begin_do_stmt ();
9723 /* Parse the body of the do-statement. */
9724 parser->in_statement = IN_ITERATION_STMT;
9725 cp_parser_implicitly_scoped_statement (parser, NULL);
9726 parser->in_statement = in_statement;
9727 finish_do_body (statement);
9728 /* Look for the `while' keyword. */
9729 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9730 /* Look for the `('. */
9731 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9732 /* Parse the expression. */
9733 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9734 /* We're done with the do-statement. */
9735 finish_do_stmt (expression, statement);
9736 /* Look for the `)'. */
9737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9738 /* Look for the `;'. */
9739 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9741 break;
9743 case RID_FOR:
9745 /* Look for the `('. */
9746 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9748 statement = cp_parser_for (parser);
9750 /* Look for the `)'. */
9751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9753 /* Parse the body of the for-statement. */
9754 parser->in_statement = IN_ITERATION_STMT;
9755 cp_parser_already_scoped_statement (parser);
9756 parser->in_statement = in_statement;
9758 /* We're done with the for-statement. */
9759 finish_for_stmt (statement);
9761 break;
9763 default:
9764 cp_parser_error (parser, "expected iteration-statement");
9765 statement = error_mark_node;
9766 break;
9769 return statement;
9772 /* Parse a for-init-statement or the declarator of a range-based-for.
9773 Returns true if a range-based-for declaration is seen.
9775 for-init-statement:
9776 expression-statement
9777 simple-declaration */
9779 static bool
9780 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9782 /* If the next token is a `;', then we have an empty
9783 expression-statement. Grammatically, this is also a
9784 simple-declaration, but an invalid one, because it does not
9785 declare anything. Therefore, if we did not handle this case
9786 specially, we would issue an error message about an invalid
9787 declaration. */
9788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9790 bool is_range_for = false;
9791 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9793 parser->colon_corrects_to_scope_p = false;
9795 /* We're going to speculatively look for a declaration, falling back
9796 to an expression, if necessary. */
9797 cp_parser_parse_tentatively (parser);
9798 /* Parse the declaration. */
9799 cp_parser_simple_declaration (parser,
9800 /*function_definition_allowed_p=*/false,
9801 decl);
9802 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9803 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9805 /* It is a range-for, consume the ':' */
9806 cp_lexer_consume_token (parser->lexer);
9807 is_range_for = true;
9808 if (cxx_dialect < cxx0x)
9810 error_at (cp_lexer_peek_token (parser->lexer)->location,
9811 "range-based %<for%> loops are not allowed "
9812 "in C++98 mode");
9813 *decl = error_mark_node;
9816 else
9817 /* The ';' is not consumed yet because we told
9818 cp_parser_simple_declaration not to. */
9819 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9821 if (cp_parser_parse_definitely (parser))
9822 return is_range_for;
9823 /* If the tentative parse failed, then we shall need to look for an
9824 expression-statement. */
9826 /* If we are here, it is an expression-statement. */
9827 cp_parser_expression_statement (parser, NULL_TREE);
9828 return false;
9831 /* Parse a jump-statement.
9833 jump-statement:
9834 break ;
9835 continue ;
9836 return expression [opt] ;
9837 return braced-init-list ;
9838 goto identifier ;
9840 GNU extension:
9842 jump-statement:
9843 goto * expression ;
9845 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9847 static tree
9848 cp_parser_jump_statement (cp_parser* parser)
9850 tree statement = error_mark_node;
9851 cp_token *token;
9852 enum rid keyword;
9853 unsigned char in_statement;
9855 /* Peek at the next token. */
9856 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9857 if (!token)
9858 return error_mark_node;
9860 /* See what kind of keyword it is. */
9861 keyword = token->keyword;
9862 switch (keyword)
9864 case RID_BREAK:
9865 in_statement = parser->in_statement & ~IN_IF_STMT;
9866 switch (in_statement)
9868 case 0:
9869 error_at (token->location, "break statement not within loop or switch");
9870 break;
9871 default:
9872 gcc_assert ((in_statement & IN_SWITCH_STMT)
9873 || in_statement == IN_ITERATION_STMT);
9874 statement = finish_break_stmt ();
9875 break;
9876 case IN_OMP_BLOCK:
9877 error_at (token->location, "invalid exit from OpenMP structured block");
9878 break;
9879 case IN_OMP_FOR:
9880 error_at (token->location, "break statement used with OpenMP for loop");
9881 break;
9883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9884 break;
9886 case RID_CONTINUE:
9887 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9889 case 0:
9890 error_at (token->location, "continue statement not within a loop");
9891 break;
9892 case IN_ITERATION_STMT:
9893 case IN_OMP_FOR:
9894 statement = finish_continue_stmt ();
9895 break;
9896 case IN_OMP_BLOCK:
9897 error_at (token->location, "invalid exit from OpenMP structured block");
9898 break;
9899 default:
9900 gcc_unreachable ();
9902 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9903 break;
9905 case RID_RETURN:
9907 tree expr;
9908 bool expr_non_constant_p;
9910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9912 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9913 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9915 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9916 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9917 else
9918 /* If the next token is a `;', then there is no
9919 expression. */
9920 expr = NULL_TREE;
9921 /* Build the return-statement. */
9922 statement = finish_return_stmt (expr);
9923 /* Look for the final `;'. */
9924 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9926 break;
9928 case RID_GOTO:
9929 /* Create the goto-statement. */
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9932 /* Issue a warning about this use of a GNU extension. */
9933 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9934 /* Consume the '*' token. */
9935 cp_lexer_consume_token (parser->lexer);
9936 /* Parse the dependent expression. */
9937 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9939 else
9940 finish_goto_stmt (cp_parser_identifier (parser));
9941 /* Look for the final `;'. */
9942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9943 break;
9945 default:
9946 cp_parser_error (parser, "expected jump-statement");
9947 break;
9950 return statement;
9953 /* Parse a declaration-statement.
9955 declaration-statement:
9956 block-declaration */
9958 static void
9959 cp_parser_declaration_statement (cp_parser* parser)
9961 void *p;
9963 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9964 p = obstack_alloc (&declarator_obstack, 0);
9966 /* Parse the block-declaration. */
9967 cp_parser_block_declaration (parser, /*statement_p=*/true);
9969 /* Free any declarators allocated. */
9970 obstack_free (&declarator_obstack, p);
9972 /* Finish off the statement. */
9973 finish_stmt ();
9976 /* Some dependent statements (like `if (cond) statement'), are
9977 implicitly in their own scope. In other words, if the statement is
9978 a single statement (as opposed to a compound-statement), it is
9979 none-the-less treated as if it were enclosed in braces. Any
9980 declarations appearing in the dependent statement are out of scope
9981 after control passes that point. This function parses a statement,
9982 but ensures that is in its own scope, even if it is not a
9983 compound-statement.
9985 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9986 is a (possibly labeled) if statement which is not enclosed in
9987 braces and has an else clause. This is used to implement
9988 -Wparentheses.
9990 Returns the new statement. */
9992 static tree
9993 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9995 tree statement;
9997 if (if_p != NULL)
9998 *if_p = false;
10000 /* Mark if () ; with a special NOP_EXPR. */
10001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10003 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10004 cp_lexer_consume_token (parser->lexer);
10005 statement = add_stmt (build_empty_stmt (loc));
10007 /* if a compound is opened, we simply parse the statement directly. */
10008 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10009 statement = cp_parser_compound_statement (parser, NULL, false, false);
10010 /* If the token is not a `{', then we must take special action. */
10011 else
10013 /* Create a compound-statement. */
10014 statement = begin_compound_stmt (0);
10015 /* Parse the dependent-statement. */
10016 cp_parser_statement (parser, NULL_TREE, false, if_p);
10017 /* Finish the dummy compound-statement. */
10018 finish_compound_stmt (statement);
10021 /* Return the statement. */
10022 return statement;
10025 /* For some dependent statements (like `while (cond) statement'), we
10026 have already created a scope. Therefore, even if the dependent
10027 statement is a compound-statement, we do not want to create another
10028 scope. */
10030 static void
10031 cp_parser_already_scoped_statement (cp_parser* parser)
10033 /* If the token is a `{', then we must take special action. */
10034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10035 cp_parser_statement (parser, NULL_TREE, false, NULL);
10036 else
10038 /* Avoid calling cp_parser_compound_statement, so that we
10039 don't create a new scope. Do everything else by hand. */
10040 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10041 /* If the next keyword is `__label__' we have a label declaration. */
10042 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10043 cp_parser_label_declaration (parser);
10044 /* Parse an (optional) statement-seq. */
10045 cp_parser_statement_seq_opt (parser, NULL_TREE);
10046 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10050 /* Declarations [gram.dcl.dcl] */
10052 /* Parse an optional declaration-sequence.
10054 declaration-seq:
10055 declaration
10056 declaration-seq declaration */
10058 static void
10059 cp_parser_declaration_seq_opt (cp_parser* parser)
10061 while (true)
10063 cp_token *token;
10065 token = cp_lexer_peek_token (parser->lexer);
10067 if (token->type == CPP_CLOSE_BRACE
10068 || token->type == CPP_EOF
10069 || token->type == CPP_PRAGMA_EOL)
10070 break;
10072 if (token->type == CPP_SEMICOLON)
10074 /* A declaration consisting of a single semicolon is
10075 invalid. Allow it unless we're being pedantic. */
10076 cp_lexer_consume_token (parser->lexer);
10077 if (!in_system_header)
10078 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10079 continue;
10082 /* If we're entering or exiting a region that's implicitly
10083 extern "C", modify the lang context appropriately. */
10084 if (!parser->implicit_extern_c && token->implicit_extern_c)
10086 push_lang_context (lang_name_c);
10087 parser->implicit_extern_c = true;
10089 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10091 pop_lang_context ();
10092 parser->implicit_extern_c = false;
10095 if (token->type == CPP_PRAGMA)
10097 /* A top-level declaration can consist solely of a #pragma.
10098 A nested declaration cannot, so this is done here and not
10099 in cp_parser_declaration. (A #pragma at block scope is
10100 handled in cp_parser_statement.) */
10101 cp_parser_pragma (parser, pragma_external);
10102 continue;
10105 /* Parse the declaration itself. */
10106 cp_parser_declaration (parser);
10110 /* Parse a declaration.
10112 declaration:
10113 block-declaration
10114 function-definition
10115 template-declaration
10116 explicit-instantiation
10117 explicit-specialization
10118 linkage-specification
10119 namespace-definition
10121 GNU extension:
10123 declaration:
10124 __extension__ declaration */
10126 static void
10127 cp_parser_declaration (cp_parser* parser)
10129 cp_token token1;
10130 cp_token token2;
10131 int saved_pedantic;
10132 void *p;
10133 tree attributes = NULL_TREE;
10135 /* Check for the `__extension__' keyword. */
10136 if (cp_parser_extension_opt (parser, &saved_pedantic))
10138 /* Parse the qualified declaration. */
10139 cp_parser_declaration (parser);
10140 /* Restore the PEDANTIC flag. */
10141 pedantic = saved_pedantic;
10143 return;
10146 /* Try to figure out what kind of declaration is present. */
10147 token1 = *cp_lexer_peek_token (parser->lexer);
10149 if (token1.type != CPP_EOF)
10150 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10151 else
10153 token2.type = CPP_EOF;
10154 token2.keyword = RID_MAX;
10157 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10158 p = obstack_alloc (&declarator_obstack, 0);
10160 /* If the next token is `extern' and the following token is a string
10161 literal, then we have a linkage specification. */
10162 if (token1.keyword == RID_EXTERN
10163 && cp_parser_is_pure_string_literal (&token2))
10164 cp_parser_linkage_specification (parser);
10165 /* If the next token is `template', then we have either a template
10166 declaration, an explicit instantiation, or an explicit
10167 specialization. */
10168 else if (token1.keyword == RID_TEMPLATE)
10170 /* `template <>' indicates a template specialization. */
10171 if (token2.type == CPP_LESS
10172 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10173 cp_parser_explicit_specialization (parser);
10174 /* `template <' indicates a template declaration. */
10175 else if (token2.type == CPP_LESS)
10176 cp_parser_template_declaration (parser, /*member_p=*/false);
10177 /* Anything else must be an explicit instantiation. */
10178 else
10179 cp_parser_explicit_instantiation (parser);
10181 /* If the next token is `export', then we have a template
10182 declaration. */
10183 else if (token1.keyword == RID_EXPORT)
10184 cp_parser_template_declaration (parser, /*member_p=*/false);
10185 /* If the next token is `extern', 'static' or 'inline' and the one
10186 after that is `template', we have a GNU extended explicit
10187 instantiation directive. */
10188 else if (cp_parser_allow_gnu_extensions_p (parser)
10189 && (token1.keyword == RID_EXTERN
10190 || token1.keyword == RID_STATIC
10191 || token1.keyword == RID_INLINE)
10192 && token2.keyword == RID_TEMPLATE)
10193 cp_parser_explicit_instantiation (parser);
10194 /* If the next token is `namespace', check for a named or unnamed
10195 namespace definition. */
10196 else if (token1.keyword == RID_NAMESPACE
10197 && (/* A named namespace definition. */
10198 (token2.type == CPP_NAME
10199 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10200 != CPP_EQ))
10201 /* An unnamed namespace definition. */
10202 || token2.type == CPP_OPEN_BRACE
10203 || token2.keyword == RID_ATTRIBUTE))
10204 cp_parser_namespace_definition (parser);
10205 /* An inline (associated) namespace definition. */
10206 else if (token1.keyword == RID_INLINE
10207 && token2.keyword == RID_NAMESPACE)
10208 cp_parser_namespace_definition (parser);
10209 /* Objective-C++ declaration/definition. */
10210 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10211 cp_parser_objc_declaration (parser, NULL_TREE);
10212 else if (c_dialect_objc ()
10213 && token1.keyword == RID_ATTRIBUTE
10214 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10215 cp_parser_objc_declaration (parser, attributes);
10216 /* We must have either a block declaration or a function
10217 definition. */
10218 else
10219 /* Try to parse a block-declaration, or a function-definition. */
10220 cp_parser_block_declaration (parser, /*statement_p=*/false);
10222 /* Free any declarators allocated. */
10223 obstack_free (&declarator_obstack, p);
10226 /* Parse a block-declaration.
10228 block-declaration:
10229 simple-declaration
10230 asm-definition
10231 namespace-alias-definition
10232 using-declaration
10233 using-directive
10235 GNU Extension:
10237 block-declaration:
10238 __extension__ block-declaration
10240 C++0x Extension:
10242 block-declaration:
10243 static_assert-declaration
10245 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10246 part of a declaration-statement. */
10248 static void
10249 cp_parser_block_declaration (cp_parser *parser,
10250 bool statement_p)
10252 cp_token *token1;
10253 int saved_pedantic;
10255 /* Check for the `__extension__' keyword. */
10256 if (cp_parser_extension_opt (parser, &saved_pedantic))
10258 /* Parse the qualified declaration. */
10259 cp_parser_block_declaration (parser, statement_p);
10260 /* Restore the PEDANTIC flag. */
10261 pedantic = saved_pedantic;
10263 return;
10266 /* Peek at the next token to figure out which kind of declaration is
10267 present. */
10268 token1 = cp_lexer_peek_token (parser->lexer);
10270 /* If the next keyword is `asm', we have an asm-definition. */
10271 if (token1->keyword == RID_ASM)
10273 if (statement_p)
10274 cp_parser_commit_to_tentative_parse (parser);
10275 cp_parser_asm_definition (parser);
10277 /* If the next keyword is `namespace', we have a
10278 namespace-alias-definition. */
10279 else if (token1->keyword == RID_NAMESPACE)
10280 cp_parser_namespace_alias_definition (parser);
10281 /* If the next keyword is `using', we have a
10282 using-declaration, a using-directive, or an alias-declaration. */
10283 else if (token1->keyword == RID_USING)
10285 cp_token *token2;
10287 if (statement_p)
10288 cp_parser_commit_to_tentative_parse (parser);
10289 /* If the token after `using' is `namespace', then we have a
10290 using-directive. */
10291 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10292 if (token2->keyword == RID_NAMESPACE)
10293 cp_parser_using_directive (parser);
10294 /* If the second token after 'using' is '=', then we have an
10295 alias-declaration. */
10296 else if (cxx_dialect >= cxx0x
10297 && token2->type == CPP_NAME
10298 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10299 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10300 == RID_ATTRIBUTE)))
10301 cp_parser_alias_declaration (parser);
10302 /* Otherwise, it's a using-declaration. */
10303 else
10304 cp_parser_using_declaration (parser,
10305 /*access_declaration_p=*/false);
10307 /* If the next keyword is `__label__' we have a misplaced label
10308 declaration. */
10309 else if (token1->keyword == RID_LABEL)
10311 cp_lexer_consume_token (parser->lexer);
10312 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10313 cp_parser_skip_to_end_of_statement (parser);
10314 /* If the next token is now a `;', consume it. */
10315 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10316 cp_lexer_consume_token (parser->lexer);
10318 /* If the next token is `static_assert' we have a static assertion. */
10319 else if (token1->keyword == RID_STATIC_ASSERT)
10320 cp_parser_static_assert (parser, /*member_p=*/false);
10321 /* Anything else must be a simple-declaration. */
10322 else
10323 cp_parser_simple_declaration (parser, !statement_p,
10324 /*maybe_range_for_decl*/NULL);
10327 /* Parse a simple-declaration.
10329 simple-declaration:
10330 decl-specifier-seq [opt] init-declarator-list [opt] ;
10332 init-declarator-list:
10333 init-declarator
10334 init-declarator-list , init-declarator
10336 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10337 function-definition as a simple-declaration.
10339 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10340 parsed declaration if it is an uninitialized single declarator not followed
10341 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10342 if present, will not be consumed. */
10344 static void
10345 cp_parser_simple_declaration (cp_parser* parser,
10346 bool function_definition_allowed_p,
10347 tree *maybe_range_for_decl)
10349 cp_decl_specifier_seq decl_specifiers;
10350 int declares_class_or_enum;
10351 bool saw_declarator;
10353 if (maybe_range_for_decl)
10354 *maybe_range_for_decl = NULL_TREE;
10356 /* Defer access checks until we know what is being declared; the
10357 checks for names appearing in the decl-specifier-seq should be
10358 done as if we were in the scope of the thing being declared. */
10359 push_deferring_access_checks (dk_deferred);
10361 /* Parse the decl-specifier-seq. We have to keep track of whether
10362 or not the decl-specifier-seq declares a named class or
10363 enumeration type, since that is the only case in which the
10364 init-declarator-list is allowed to be empty.
10366 [dcl.dcl]
10368 In a simple-declaration, the optional init-declarator-list can be
10369 omitted only when declaring a class or enumeration, that is when
10370 the decl-specifier-seq contains either a class-specifier, an
10371 elaborated-type-specifier, or an enum-specifier. */
10372 cp_parser_decl_specifier_seq (parser,
10373 CP_PARSER_FLAGS_OPTIONAL,
10374 &decl_specifiers,
10375 &declares_class_or_enum);
10376 /* We no longer need to defer access checks. */
10377 stop_deferring_access_checks ();
10379 /* In a block scope, a valid declaration must always have a
10380 decl-specifier-seq. By not trying to parse declarators, we can
10381 resolve the declaration/expression ambiguity more quickly. */
10382 if (!function_definition_allowed_p
10383 && !decl_specifiers.any_specifiers_p)
10385 cp_parser_error (parser, "expected declaration");
10386 goto done;
10389 /* If the next two tokens are both identifiers, the code is
10390 erroneous. The usual cause of this situation is code like:
10392 T t;
10394 where "T" should name a type -- but does not. */
10395 if (!decl_specifiers.any_type_specifiers_p
10396 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10398 /* If parsing tentatively, we should commit; we really are
10399 looking at a declaration. */
10400 cp_parser_commit_to_tentative_parse (parser);
10401 /* Give up. */
10402 goto done;
10405 /* If we have seen at least one decl-specifier, and the next token
10406 is not a parenthesis, then we must be looking at a declaration.
10407 (After "int (" we might be looking at a functional cast.) */
10408 if (decl_specifiers.any_specifiers_p
10409 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10410 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10411 && !cp_parser_error_occurred (parser))
10412 cp_parser_commit_to_tentative_parse (parser);
10414 /* Keep going until we hit the `;' at the end of the simple
10415 declaration. */
10416 saw_declarator = false;
10417 while (cp_lexer_next_token_is_not (parser->lexer,
10418 CPP_SEMICOLON))
10420 cp_token *token;
10421 bool function_definition_p;
10422 tree decl;
10424 if (saw_declarator)
10426 /* If we are processing next declarator, coma is expected */
10427 token = cp_lexer_peek_token (parser->lexer);
10428 gcc_assert (token->type == CPP_COMMA);
10429 cp_lexer_consume_token (parser->lexer);
10430 if (maybe_range_for_decl)
10431 *maybe_range_for_decl = error_mark_node;
10433 else
10434 saw_declarator = true;
10436 /* Parse the init-declarator. */
10437 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10438 /*checks=*/NULL,
10439 function_definition_allowed_p,
10440 /*member_p=*/false,
10441 declares_class_or_enum,
10442 &function_definition_p,
10443 maybe_range_for_decl);
10444 /* If an error occurred while parsing tentatively, exit quickly.
10445 (That usually happens when in the body of a function; each
10446 statement is treated as a declaration-statement until proven
10447 otherwise.) */
10448 if (cp_parser_error_occurred (parser))
10449 goto done;
10450 /* Handle function definitions specially. */
10451 if (function_definition_p)
10453 /* If the next token is a `,', then we are probably
10454 processing something like:
10456 void f() {}, *p;
10458 which is erroneous. */
10459 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10461 cp_token *token = cp_lexer_peek_token (parser->lexer);
10462 error_at (token->location,
10463 "mixing"
10464 " declarations and function-definitions is forbidden");
10466 /* Otherwise, we're done with the list of declarators. */
10467 else
10469 pop_deferring_access_checks ();
10470 return;
10473 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10474 *maybe_range_for_decl = decl;
10475 /* The next token should be either a `,' or a `;'. */
10476 token = cp_lexer_peek_token (parser->lexer);
10477 /* If it's a `,', there are more declarators to come. */
10478 if (token->type == CPP_COMMA)
10479 /* will be consumed next time around */;
10480 /* If it's a `;', we are done. */
10481 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10482 break;
10483 /* Anything else is an error. */
10484 else
10486 /* If we have already issued an error message we don't need
10487 to issue another one. */
10488 if (decl != error_mark_node
10489 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10490 cp_parser_error (parser, "expected %<,%> or %<;%>");
10491 /* Skip tokens until we reach the end of the statement. */
10492 cp_parser_skip_to_end_of_statement (parser);
10493 /* If the next token is now a `;', consume it. */
10494 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10495 cp_lexer_consume_token (parser->lexer);
10496 goto done;
10498 /* After the first time around, a function-definition is not
10499 allowed -- even if it was OK at first. For example:
10501 int i, f() {}
10503 is not valid. */
10504 function_definition_allowed_p = false;
10507 /* Issue an error message if no declarators are present, and the
10508 decl-specifier-seq does not itself declare a class or
10509 enumeration. */
10510 if (!saw_declarator)
10512 if (cp_parser_declares_only_class_p (parser))
10513 shadow_tag (&decl_specifiers);
10514 /* Perform any deferred access checks. */
10515 perform_deferred_access_checks (tf_warning_or_error);
10518 /* Consume the `;'. */
10519 if (!maybe_range_for_decl)
10520 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10522 done:
10523 pop_deferring_access_checks ();
10526 /* Parse a decl-specifier-seq.
10528 decl-specifier-seq:
10529 decl-specifier-seq [opt] decl-specifier
10531 decl-specifier:
10532 storage-class-specifier
10533 type-specifier
10534 function-specifier
10535 friend
10536 typedef
10538 GNU Extension:
10540 decl-specifier:
10541 attributes
10543 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10545 The parser flags FLAGS is used to control type-specifier parsing.
10547 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10548 flags:
10550 1: one of the decl-specifiers is an elaborated-type-specifier
10551 (i.e., a type declaration)
10552 2: one of the decl-specifiers is an enum-specifier or a
10553 class-specifier (i.e., a type definition)
10557 static void
10558 cp_parser_decl_specifier_seq (cp_parser* parser,
10559 cp_parser_flags flags,
10560 cp_decl_specifier_seq *decl_specs,
10561 int* declares_class_or_enum)
10563 bool constructor_possible_p = !parser->in_declarator_p;
10564 cp_token *start_token = NULL;
10565 cp_decl_spec ds;
10567 /* Clear DECL_SPECS. */
10568 clear_decl_specs (decl_specs);
10570 /* Assume no class or enumeration type is declared. */
10571 *declares_class_or_enum = 0;
10573 /* Keep reading specifiers until there are no more to read. */
10574 while (true)
10576 bool constructor_p;
10577 bool found_decl_spec;
10578 cp_token *token;
10579 ds = ds_last;
10581 /* Peek at the next token. */
10582 token = cp_lexer_peek_token (parser->lexer);
10584 /* Save the first token of the decl spec list for error
10585 reporting. */
10586 if (!start_token)
10587 start_token = token;
10588 /* Handle attributes. */
10589 if (token->keyword == RID_ATTRIBUTE)
10591 /* Parse the attributes. */
10592 decl_specs->attributes
10593 = chainon (decl_specs->attributes,
10594 cp_parser_attributes_opt (parser));
10595 if (decl_specs->locations[ds_attribute] == 0)
10596 decl_specs->locations[ds_attribute] = token->location;
10597 continue;
10599 /* Assume we will find a decl-specifier keyword. */
10600 found_decl_spec = true;
10601 /* If the next token is an appropriate keyword, we can simply
10602 add it to the list. */
10603 switch (token->keyword)
10605 /* decl-specifier:
10606 friend
10607 constexpr */
10608 case RID_FRIEND:
10609 if (!at_class_scope_p ())
10611 error_at (token->location, "%<friend%> used outside of class");
10612 cp_lexer_purge_token (parser->lexer);
10614 else
10616 ds = ds_friend;
10617 /* Consume the token. */
10618 cp_lexer_consume_token (parser->lexer);
10620 break;
10622 case RID_CONSTEXPR:
10623 ds = ds_constexpr;
10624 cp_lexer_consume_token (parser->lexer);
10625 break;
10627 /* function-specifier:
10628 inline
10629 virtual
10630 explicit */
10631 case RID_INLINE:
10632 case RID_VIRTUAL:
10633 case RID_EXPLICIT:
10634 cp_parser_function_specifier_opt (parser, decl_specs);
10635 break;
10637 /* decl-specifier:
10638 typedef */
10639 case RID_TYPEDEF:
10640 ds = ds_typedef;
10641 /* Consume the token. */
10642 cp_lexer_consume_token (parser->lexer);
10643 /* A constructor declarator cannot appear in a typedef. */
10644 constructor_possible_p = false;
10645 /* The "typedef" keyword can only occur in a declaration; we
10646 may as well commit at this point. */
10647 cp_parser_commit_to_tentative_parse (parser);
10649 if (decl_specs->storage_class != sc_none)
10650 decl_specs->conflicting_specifiers_p = true;
10651 break;
10653 /* storage-class-specifier:
10654 auto
10655 register
10656 static
10657 extern
10658 mutable
10660 GNU Extension:
10661 thread */
10662 case RID_AUTO:
10663 if (cxx_dialect == cxx98)
10665 /* Consume the token. */
10666 cp_lexer_consume_token (parser->lexer);
10668 /* Complain about `auto' as a storage specifier, if
10669 we're complaining about C++0x compatibility. */
10670 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10671 " changes meaning in C++11; please remove it");
10673 /* Set the storage class anyway. */
10674 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10675 token->location);
10677 else
10678 /* C++0x auto type-specifier. */
10679 found_decl_spec = false;
10680 break;
10682 case RID_REGISTER:
10683 case RID_STATIC:
10684 case RID_EXTERN:
10685 case RID_MUTABLE:
10686 /* Consume the token. */
10687 cp_lexer_consume_token (parser->lexer);
10688 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10689 token->location);
10690 break;
10691 case RID_THREAD:
10692 /* Consume the token. */
10693 ds = ds_thread;
10694 cp_lexer_consume_token (parser->lexer);
10695 break;
10697 default:
10698 /* We did not yet find a decl-specifier yet. */
10699 found_decl_spec = false;
10700 break;
10703 if (found_decl_spec
10704 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10705 && token->keyword != RID_CONSTEXPR)
10706 error ("decl-specifier invalid in condition");
10708 if (ds != ds_last)
10709 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10711 /* Constructors are a special case. The `S' in `S()' is not a
10712 decl-specifier; it is the beginning of the declarator. */
10713 constructor_p
10714 = (!found_decl_spec
10715 && constructor_possible_p
10716 && (cp_parser_constructor_declarator_p
10717 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10719 /* If we don't have a DECL_SPEC yet, then we must be looking at
10720 a type-specifier. */
10721 if (!found_decl_spec && !constructor_p)
10723 int decl_spec_declares_class_or_enum;
10724 bool is_cv_qualifier;
10725 tree type_spec;
10727 type_spec
10728 = cp_parser_type_specifier (parser, flags,
10729 decl_specs,
10730 /*is_declaration=*/true,
10731 &decl_spec_declares_class_or_enum,
10732 &is_cv_qualifier);
10733 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10735 /* If this type-specifier referenced a user-defined type
10736 (a typedef, class-name, etc.), then we can't allow any
10737 more such type-specifiers henceforth.
10739 [dcl.spec]
10741 The longest sequence of decl-specifiers that could
10742 possibly be a type name is taken as the
10743 decl-specifier-seq of a declaration. The sequence shall
10744 be self-consistent as described below.
10746 [dcl.type]
10748 As a general rule, at most one type-specifier is allowed
10749 in the complete decl-specifier-seq of a declaration. The
10750 only exceptions are the following:
10752 -- const or volatile can be combined with any other
10753 type-specifier.
10755 -- signed or unsigned can be combined with char, long,
10756 short, or int.
10758 -- ..
10760 Example:
10762 typedef char* Pc;
10763 void g (const int Pc);
10765 Here, Pc is *not* part of the decl-specifier seq; it's
10766 the declarator. Therefore, once we see a type-specifier
10767 (other than a cv-qualifier), we forbid any additional
10768 user-defined types. We *do* still allow things like `int
10769 int' to be considered a decl-specifier-seq, and issue the
10770 error message later. */
10771 if (type_spec && !is_cv_qualifier)
10772 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10773 /* A constructor declarator cannot follow a type-specifier. */
10774 if (type_spec)
10776 constructor_possible_p = false;
10777 found_decl_spec = true;
10778 if (!is_cv_qualifier)
10779 decl_specs->any_type_specifiers_p = true;
10783 /* If we still do not have a DECL_SPEC, then there are no more
10784 decl-specifiers. */
10785 if (!found_decl_spec)
10786 break;
10788 decl_specs->any_specifiers_p = true;
10789 /* After we see one decl-specifier, further decl-specifiers are
10790 always optional. */
10791 flags |= CP_PARSER_FLAGS_OPTIONAL;
10794 /* Don't allow a friend specifier with a class definition. */
10795 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10796 && (*declares_class_or_enum & 2))
10797 error_at (decl_specs->locations[ds_friend],
10798 "class definition may not be declared a friend");
10801 /* Parse an (optional) storage-class-specifier.
10803 storage-class-specifier:
10804 auto
10805 register
10806 static
10807 extern
10808 mutable
10810 GNU Extension:
10812 storage-class-specifier:
10813 thread
10815 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10817 static tree
10818 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10820 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10822 case RID_AUTO:
10823 if (cxx_dialect != cxx98)
10824 return NULL_TREE;
10825 /* Fall through for C++98. */
10827 case RID_REGISTER:
10828 case RID_STATIC:
10829 case RID_EXTERN:
10830 case RID_MUTABLE:
10831 case RID_THREAD:
10832 /* Consume the token. */
10833 return cp_lexer_consume_token (parser->lexer)->u.value;
10835 default:
10836 return NULL_TREE;
10840 /* Parse an (optional) function-specifier.
10842 function-specifier:
10843 inline
10844 virtual
10845 explicit
10847 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10848 Updates DECL_SPECS, if it is non-NULL. */
10850 static tree
10851 cp_parser_function_specifier_opt (cp_parser* parser,
10852 cp_decl_specifier_seq *decl_specs)
10854 cp_token *token = cp_lexer_peek_token (parser->lexer);
10855 switch (token->keyword)
10857 case RID_INLINE:
10858 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10859 break;
10861 case RID_VIRTUAL:
10862 /* 14.5.2.3 [temp.mem]
10864 A member function template shall not be virtual. */
10865 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10866 error_at (token->location, "templates may not be %<virtual%>");
10867 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10868 break;
10870 case RID_EXPLICIT:
10871 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10872 break;
10874 default:
10875 return NULL_TREE;
10878 /* Consume the token. */
10879 return cp_lexer_consume_token (parser->lexer)->u.value;
10882 /* Parse a linkage-specification.
10884 linkage-specification:
10885 extern string-literal { declaration-seq [opt] }
10886 extern string-literal declaration */
10888 static void
10889 cp_parser_linkage_specification (cp_parser* parser)
10891 tree linkage;
10893 /* Look for the `extern' keyword. */
10894 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10896 /* Look for the string-literal. */
10897 linkage = cp_parser_string_literal (parser, false, false);
10899 /* Transform the literal into an identifier. If the literal is a
10900 wide-character string, or contains embedded NULs, then we can't
10901 handle it as the user wants. */
10902 if (strlen (TREE_STRING_POINTER (linkage))
10903 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10905 cp_parser_error (parser, "invalid linkage-specification");
10906 /* Assume C++ linkage. */
10907 linkage = lang_name_cplusplus;
10909 else
10910 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10912 /* We're now using the new linkage. */
10913 push_lang_context (linkage);
10915 /* If the next token is a `{', then we're using the first
10916 production. */
10917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10919 /* Consume the `{' token. */
10920 cp_lexer_consume_token (parser->lexer);
10921 /* Parse the declarations. */
10922 cp_parser_declaration_seq_opt (parser);
10923 /* Look for the closing `}'. */
10924 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10926 /* Otherwise, there's just one declaration. */
10927 else
10929 bool saved_in_unbraced_linkage_specification_p;
10931 saved_in_unbraced_linkage_specification_p
10932 = parser->in_unbraced_linkage_specification_p;
10933 parser->in_unbraced_linkage_specification_p = true;
10934 cp_parser_declaration (parser);
10935 parser->in_unbraced_linkage_specification_p
10936 = saved_in_unbraced_linkage_specification_p;
10939 /* We're done with the linkage-specification. */
10940 pop_lang_context ();
10943 /* Parse a static_assert-declaration.
10945 static_assert-declaration:
10946 static_assert ( constant-expression , string-literal ) ;
10948 If MEMBER_P, this static_assert is a class member. */
10950 static void
10951 cp_parser_static_assert(cp_parser *parser, bool member_p)
10953 tree condition;
10954 tree message;
10955 cp_token *token;
10956 location_t saved_loc;
10957 bool dummy;
10959 /* Peek at the `static_assert' token so we can keep track of exactly
10960 where the static assertion started. */
10961 token = cp_lexer_peek_token (parser->lexer);
10962 saved_loc = token->location;
10964 /* Look for the `static_assert' keyword. */
10965 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10966 RT_STATIC_ASSERT))
10967 return;
10969 /* We know we are in a static assertion; commit to any tentative
10970 parse. */
10971 if (cp_parser_parsing_tentatively (parser))
10972 cp_parser_commit_to_tentative_parse (parser);
10974 /* Parse the `(' starting the static assertion condition. */
10975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10977 /* Parse the constant-expression. Allow a non-constant expression
10978 here in order to give better diagnostics in finish_static_assert. */
10979 condition =
10980 cp_parser_constant_expression (parser,
10981 /*allow_non_constant_p=*/true,
10982 /*non_constant_p=*/&dummy);
10984 /* Parse the separating `,'. */
10985 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10987 /* Parse the string-literal message. */
10988 message = cp_parser_string_literal (parser,
10989 /*translate=*/false,
10990 /*wide_ok=*/true);
10992 /* A `)' completes the static assertion. */
10993 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10994 cp_parser_skip_to_closing_parenthesis (parser,
10995 /*recovering=*/true,
10996 /*or_comma=*/false,
10997 /*consume_paren=*/true);
10999 /* A semicolon terminates the declaration. */
11000 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11002 /* Complete the static assertion, which may mean either processing
11003 the static assert now or saving it for template instantiation. */
11004 finish_static_assert (condition, message, saved_loc, member_p);
11007 /* Parse a `decltype' type. Returns the type.
11009 simple-type-specifier:
11010 decltype ( expression ) */
11012 static tree
11013 cp_parser_decltype (cp_parser *parser)
11015 tree expr;
11016 bool id_expression_or_member_access_p = false;
11017 const char *saved_message;
11018 bool saved_integral_constant_expression_p;
11019 bool saved_non_integral_constant_expression_p;
11020 cp_token *id_expr_start_token;
11021 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11023 if (start_token->type == CPP_DECLTYPE)
11025 /* Already parsed. */
11026 cp_lexer_consume_token (parser->lexer);
11027 return start_token->u.value;
11030 /* Look for the `decltype' token. */
11031 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11032 return error_mark_node;
11034 /* Types cannot be defined in a `decltype' expression. Save away the
11035 old message. */
11036 saved_message = parser->type_definition_forbidden_message;
11038 /* And create the new one. */
11039 parser->type_definition_forbidden_message
11040 = G_("types may not be defined in %<decltype%> expressions");
11042 /* The restrictions on constant-expressions do not apply inside
11043 decltype expressions. */
11044 saved_integral_constant_expression_p
11045 = parser->integral_constant_expression_p;
11046 saved_non_integral_constant_expression_p
11047 = parser->non_integral_constant_expression_p;
11048 parser->integral_constant_expression_p = false;
11050 /* Do not actually evaluate the expression. */
11051 ++cp_unevaluated_operand;
11053 /* Do not warn about problems with the expression. */
11054 ++c_inhibit_evaluation_warnings;
11056 /* Parse the opening `('. */
11057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11058 return error_mark_node;
11060 /* First, try parsing an id-expression. */
11061 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11062 cp_parser_parse_tentatively (parser);
11063 expr = cp_parser_id_expression (parser,
11064 /*template_keyword_p=*/false,
11065 /*check_dependency_p=*/true,
11066 /*template_p=*/NULL,
11067 /*declarator_p=*/false,
11068 /*optional_p=*/false);
11070 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11072 bool non_integral_constant_expression_p = false;
11073 tree id_expression = expr;
11074 cp_id_kind idk;
11075 const char *error_msg;
11077 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11078 /* Lookup the name we got back from the id-expression. */
11079 expr = cp_parser_lookup_name (parser, expr,
11080 none_type,
11081 /*is_template=*/false,
11082 /*is_namespace=*/false,
11083 /*check_dependency=*/true,
11084 /*ambiguous_decls=*/NULL,
11085 id_expr_start_token->location);
11087 if (expr
11088 && expr != error_mark_node
11089 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11090 && TREE_CODE (expr) != TYPE_DECL
11091 && (TREE_CODE (expr) != BIT_NOT_EXPR
11092 || !TYPE_P (TREE_OPERAND (expr, 0)))
11093 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11095 /* Complete lookup of the id-expression. */
11096 expr = (finish_id_expression
11097 (id_expression, expr, parser->scope, &idk,
11098 /*integral_constant_expression_p=*/false,
11099 /*allow_non_integral_constant_expression_p=*/true,
11100 &non_integral_constant_expression_p,
11101 /*template_p=*/false,
11102 /*done=*/true,
11103 /*address_p=*/false,
11104 /*template_arg_p=*/false,
11105 &error_msg,
11106 id_expr_start_token->location));
11108 if (expr == error_mark_node)
11109 /* We found an id-expression, but it was something that we
11110 should not have found. This is an error, not something
11111 we can recover from, so note that we found an
11112 id-expression and we'll recover as gracefully as
11113 possible. */
11114 id_expression_or_member_access_p = true;
11117 if (expr
11118 && expr != error_mark_node
11119 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11120 /* We have an id-expression. */
11121 id_expression_or_member_access_p = true;
11124 if (!id_expression_or_member_access_p)
11126 /* Abort the id-expression parse. */
11127 cp_parser_abort_tentative_parse (parser);
11129 /* Parsing tentatively, again. */
11130 cp_parser_parse_tentatively (parser);
11132 /* Parse a class member access. */
11133 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11134 /*cast_p=*/false,
11135 /*member_access_only_p=*/true, NULL);
11137 if (expr
11138 && expr != error_mark_node
11139 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11140 /* We have an id-expression. */
11141 id_expression_or_member_access_p = true;
11144 if (id_expression_or_member_access_p)
11145 /* We have parsed the complete id-expression or member access. */
11146 cp_parser_parse_definitely (parser);
11147 else
11149 bool saved_greater_than_is_operator_p;
11151 /* Abort our attempt to parse an id-expression or member access
11152 expression. */
11153 cp_parser_abort_tentative_parse (parser);
11155 /* Within a parenthesized expression, a `>' token is always
11156 the greater-than operator. */
11157 saved_greater_than_is_operator_p
11158 = parser->greater_than_is_operator_p;
11159 parser->greater_than_is_operator_p = true;
11161 /* Parse a full expression. */
11162 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11164 /* The `>' token might be the end of a template-id or
11165 template-parameter-list now. */
11166 parser->greater_than_is_operator_p
11167 = saved_greater_than_is_operator_p;
11170 /* Go back to evaluating expressions. */
11171 --cp_unevaluated_operand;
11172 --c_inhibit_evaluation_warnings;
11174 /* Restore the old message and the integral constant expression
11175 flags. */
11176 parser->type_definition_forbidden_message = saved_message;
11177 parser->integral_constant_expression_p
11178 = saved_integral_constant_expression_p;
11179 parser->non_integral_constant_expression_p
11180 = saved_non_integral_constant_expression_p;
11182 /* Parse to the closing `)'. */
11183 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11185 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11186 /*consume_paren=*/true);
11187 return error_mark_node;
11190 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11191 tf_warning_or_error);
11193 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11194 it again. */
11195 start_token->type = CPP_DECLTYPE;
11196 start_token->u.value = expr;
11197 start_token->keyword = RID_MAX;
11198 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11200 return expr;
11203 /* Special member functions [gram.special] */
11205 /* Parse a conversion-function-id.
11207 conversion-function-id:
11208 operator conversion-type-id
11210 Returns an IDENTIFIER_NODE representing the operator. */
11212 static tree
11213 cp_parser_conversion_function_id (cp_parser* parser)
11215 tree type;
11216 tree saved_scope;
11217 tree saved_qualifying_scope;
11218 tree saved_object_scope;
11219 tree pushed_scope = NULL_TREE;
11221 /* Look for the `operator' token. */
11222 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11223 return error_mark_node;
11224 /* When we parse the conversion-type-id, the current scope will be
11225 reset. However, we need that information in able to look up the
11226 conversion function later, so we save it here. */
11227 saved_scope = parser->scope;
11228 saved_qualifying_scope = parser->qualifying_scope;
11229 saved_object_scope = parser->object_scope;
11230 /* We must enter the scope of the class so that the names of
11231 entities declared within the class are available in the
11232 conversion-type-id. For example, consider:
11234 struct S {
11235 typedef int I;
11236 operator I();
11239 S::operator I() { ... }
11241 In order to see that `I' is a type-name in the definition, we
11242 must be in the scope of `S'. */
11243 if (saved_scope)
11244 pushed_scope = push_scope (saved_scope);
11245 /* Parse the conversion-type-id. */
11246 type = cp_parser_conversion_type_id (parser);
11247 /* Leave the scope of the class, if any. */
11248 if (pushed_scope)
11249 pop_scope (pushed_scope);
11250 /* Restore the saved scope. */
11251 parser->scope = saved_scope;
11252 parser->qualifying_scope = saved_qualifying_scope;
11253 parser->object_scope = saved_object_scope;
11254 /* If the TYPE is invalid, indicate failure. */
11255 if (type == error_mark_node)
11256 return error_mark_node;
11257 return mangle_conv_op_name_for_type (type);
11260 /* Parse a conversion-type-id:
11262 conversion-type-id:
11263 type-specifier-seq conversion-declarator [opt]
11265 Returns the TYPE specified. */
11267 static tree
11268 cp_parser_conversion_type_id (cp_parser* parser)
11270 tree attributes;
11271 cp_decl_specifier_seq type_specifiers;
11272 cp_declarator *declarator;
11273 tree type_specified;
11275 /* Parse the attributes. */
11276 attributes = cp_parser_attributes_opt (parser);
11277 /* Parse the type-specifiers. */
11278 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11279 /*is_trailing_return=*/false,
11280 &type_specifiers);
11281 /* If that didn't work, stop. */
11282 if (type_specifiers.type == error_mark_node)
11283 return error_mark_node;
11284 /* Parse the conversion-declarator. */
11285 declarator = cp_parser_conversion_declarator_opt (parser);
11287 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11288 /*initialized=*/0, &attributes);
11289 if (attributes)
11290 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11292 /* Don't give this error when parsing tentatively. This happens to
11293 work because we always parse this definitively once. */
11294 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11295 && type_uses_auto (type_specified))
11297 if (cxx_dialect < cxx1y)
11299 error ("invalid use of %<auto%> in conversion operator");
11300 return error_mark_node;
11302 else if (template_parm_scope_p ())
11303 warning (0, "use of %<auto%> in member template "
11304 "conversion operator can never be deduced");
11307 return type_specified;
11310 /* Parse an (optional) conversion-declarator.
11312 conversion-declarator:
11313 ptr-operator conversion-declarator [opt]
11317 static cp_declarator *
11318 cp_parser_conversion_declarator_opt (cp_parser* parser)
11320 enum tree_code code;
11321 tree class_type;
11322 cp_cv_quals cv_quals;
11324 /* We don't know if there's a ptr-operator next, or not. */
11325 cp_parser_parse_tentatively (parser);
11326 /* Try the ptr-operator. */
11327 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11328 /* If it worked, look for more conversion-declarators. */
11329 if (cp_parser_parse_definitely (parser))
11331 cp_declarator *declarator;
11333 /* Parse another optional declarator. */
11334 declarator = cp_parser_conversion_declarator_opt (parser);
11336 return cp_parser_make_indirect_declarator
11337 (code, class_type, cv_quals, declarator);
11340 return NULL;
11343 /* Parse an (optional) ctor-initializer.
11345 ctor-initializer:
11346 : mem-initializer-list
11348 Returns TRUE iff the ctor-initializer was actually present. */
11350 static bool
11351 cp_parser_ctor_initializer_opt (cp_parser* parser)
11353 /* If the next token is not a `:', then there is no
11354 ctor-initializer. */
11355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11357 /* Do default initialization of any bases and members. */
11358 if (DECL_CONSTRUCTOR_P (current_function_decl))
11359 finish_mem_initializers (NULL_TREE);
11361 return false;
11364 /* Consume the `:' token. */
11365 cp_lexer_consume_token (parser->lexer);
11366 /* And the mem-initializer-list. */
11367 cp_parser_mem_initializer_list (parser);
11369 return true;
11372 /* Parse a mem-initializer-list.
11374 mem-initializer-list:
11375 mem-initializer ... [opt]
11376 mem-initializer ... [opt] , mem-initializer-list */
11378 static void
11379 cp_parser_mem_initializer_list (cp_parser* parser)
11381 tree mem_initializer_list = NULL_TREE;
11382 tree target_ctor = error_mark_node;
11383 cp_token *token = cp_lexer_peek_token (parser->lexer);
11385 /* Let the semantic analysis code know that we are starting the
11386 mem-initializer-list. */
11387 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11388 error_at (token->location,
11389 "only constructors take member initializers");
11391 /* Loop through the list. */
11392 while (true)
11394 tree mem_initializer;
11396 token = cp_lexer_peek_token (parser->lexer);
11397 /* Parse the mem-initializer. */
11398 mem_initializer = cp_parser_mem_initializer (parser);
11399 /* If the next token is a `...', we're expanding member initializers. */
11400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11402 /* Consume the `...'. */
11403 cp_lexer_consume_token (parser->lexer);
11405 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11406 can be expanded but members cannot. */
11407 if (mem_initializer != error_mark_node
11408 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11410 error_at (token->location,
11411 "cannot expand initializer for member %<%D%>",
11412 TREE_PURPOSE (mem_initializer));
11413 mem_initializer = error_mark_node;
11416 /* Construct the pack expansion type. */
11417 if (mem_initializer != error_mark_node)
11418 mem_initializer = make_pack_expansion (mem_initializer);
11420 if (target_ctor != error_mark_node
11421 && mem_initializer != error_mark_node)
11423 error ("mem-initializer for %qD follows constructor delegation",
11424 TREE_PURPOSE (mem_initializer));
11425 mem_initializer = error_mark_node;
11427 /* Look for a target constructor. */
11428 if (mem_initializer != error_mark_node
11429 && TYPE_P (TREE_PURPOSE (mem_initializer))
11430 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11432 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11433 if (mem_initializer_list)
11435 error ("constructor delegation follows mem-initializer for %qD",
11436 TREE_PURPOSE (mem_initializer_list));
11437 mem_initializer = error_mark_node;
11439 target_ctor = mem_initializer;
11441 /* Add it to the list, unless it was erroneous. */
11442 if (mem_initializer != error_mark_node)
11444 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11445 mem_initializer_list = mem_initializer;
11447 /* If the next token is not a `,', we're done. */
11448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11449 break;
11450 /* Consume the `,' token. */
11451 cp_lexer_consume_token (parser->lexer);
11454 /* Perform semantic analysis. */
11455 if (DECL_CONSTRUCTOR_P (current_function_decl))
11456 finish_mem_initializers (mem_initializer_list);
11459 /* Parse a mem-initializer.
11461 mem-initializer:
11462 mem-initializer-id ( expression-list [opt] )
11463 mem-initializer-id braced-init-list
11465 GNU extension:
11467 mem-initializer:
11468 ( expression-list [opt] )
11470 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11471 class) or FIELD_DECL (for a non-static data member) to initialize;
11472 the TREE_VALUE is the expression-list. An empty initialization
11473 list is represented by void_list_node. */
11475 static tree
11476 cp_parser_mem_initializer (cp_parser* parser)
11478 tree mem_initializer_id;
11479 tree expression_list;
11480 tree member;
11481 cp_token *token = cp_lexer_peek_token (parser->lexer);
11483 /* Find out what is being initialized. */
11484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11486 permerror (token->location,
11487 "anachronistic old-style base class initializer");
11488 mem_initializer_id = NULL_TREE;
11490 else
11492 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11493 if (mem_initializer_id == error_mark_node)
11494 return mem_initializer_id;
11496 member = expand_member_init (mem_initializer_id);
11497 if (member && !DECL_P (member))
11498 in_base_initializer = 1;
11500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11502 bool expr_non_constant_p;
11503 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11504 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11505 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11506 expression_list = build_tree_list (NULL_TREE, expression_list);
11508 else
11510 VEC(tree,gc)* vec;
11511 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11512 /*cast_p=*/false,
11513 /*allow_expansion_p=*/true,
11514 /*non_constant_p=*/NULL);
11515 if (vec == NULL)
11516 return error_mark_node;
11517 expression_list = build_tree_list_vec (vec);
11518 release_tree_vector (vec);
11521 if (expression_list == error_mark_node)
11522 return error_mark_node;
11523 if (!expression_list)
11524 expression_list = void_type_node;
11526 in_base_initializer = 0;
11528 return member ? build_tree_list (member, expression_list) : error_mark_node;
11531 /* Parse a mem-initializer-id.
11533 mem-initializer-id:
11534 :: [opt] nested-name-specifier [opt] class-name
11535 identifier
11537 Returns a TYPE indicating the class to be initializer for the first
11538 production. Returns an IDENTIFIER_NODE indicating the data member
11539 to be initialized for the second production. */
11541 static tree
11542 cp_parser_mem_initializer_id (cp_parser* parser)
11544 bool global_scope_p;
11545 bool nested_name_specifier_p;
11546 bool template_p = false;
11547 tree id;
11549 cp_token *token = cp_lexer_peek_token (parser->lexer);
11551 /* `typename' is not allowed in this context ([temp.res]). */
11552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11554 error_at (token->location,
11555 "keyword %<typename%> not allowed in this context (a qualified "
11556 "member initializer is implicitly a type)");
11557 cp_lexer_consume_token (parser->lexer);
11559 /* Look for the optional `::' operator. */
11560 global_scope_p
11561 = (cp_parser_global_scope_opt (parser,
11562 /*current_scope_valid_p=*/false)
11563 != NULL_TREE);
11564 /* Look for the optional nested-name-specifier. The simplest way to
11565 implement:
11567 [temp.res]
11569 The keyword `typename' is not permitted in a base-specifier or
11570 mem-initializer; in these contexts a qualified name that
11571 depends on a template-parameter is implicitly assumed to be a
11572 type name.
11574 is to assume that we have seen the `typename' keyword at this
11575 point. */
11576 nested_name_specifier_p
11577 = (cp_parser_nested_name_specifier_opt (parser,
11578 /*typename_keyword_p=*/true,
11579 /*check_dependency_p=*/true,
11580 /*type_p=*/true,
11581 /*is_declaration=*/true)
11582 != NULL_TREE);
11583 if (nested_name_specifier_p)
11584 template_p = cp_parser_optional_template_keyword (parser);
11585 /* If there is a `::' operator or a nested-name-specifier, then we
11586 are definitely looking for a class-name. */
11587 if (global_scope_p || nested_name_specifier_p)
11588 return cp_parser_class_name (parser,
11589 /*typename_keyword_p=*/true,
11590 /*template_keyword_p=*/template_p,
11591 typename_type,
11592 /*check_dependency_p=*/true,
11593 /*class_head_p=*/false,
11594 /*is_declaration=*/true);
11595 /* Otherwise, we could also be looking for an ordinary identifier. */
11596 cp_parser_parse_tentatively (parser);
11597 /* Try a class-name. */
11598 id = cp_parser_class_name (parser,
11599 /*typename_keyword_p=*/true,
11600 /*template_keyword_p=*/false,
11601 none_type,
11602 /*check_dependency_p=*/true,
11603 /*class_head_p=*/false,
11604 /*is_declaration=*/true);
11605 /* If we found one, we're done. */
11606 if (cp_parser_parse_definitely (parser))
11607 return id;
11608 /* Otherwise, look for an ordinary identifier. */
11609 return cp_parser_identifier (parser);
11612 /* Overloading [gram.over] */
11614 /* Parse an operator-function-id.
11616 operator-function-id:
11617 operator operator
11619 Returns an IDENTIFIER_NODE for the operator which is a
11620 human-readable spelling of the identifier, e.g., `operator +'. */
11622 static tree
11623 cp_parser_operator_function_id (cp_parser* parser)
11625 /* Look for the `operator' keyword. */
11626 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11627 return error_mark_node;
11628 /* And then the name of the operator itself. */
11629 return cp_parser_operator (parser);
11632 /* Return an identifier node for a user-defined literal operator.
11633 The suffix identifier is chained to the operator name identifier. */
11635 static tree
11636 cp_literal_operator_id (const char* name)
11638 tree identifier;
11639 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11640 + strlen (name) + 10);
11641 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11642 identifier = get_identifier (buffer);
11643 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11645 return identifier;
11648 /* Parse an operator.
11650 operator:
11651 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11652 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11653 || ++ -- , ->* -> () []
11655 GNU Extensions:
11657 operator:
11658 <? >? <?= >?=
11660 Returns an IDENTIFIER_NODE for the operator which is a
11661 human-readable spelling of the identifier, e.g., `operator +'. */
11663 static tree
11664 cp_parser_operator (cp_parser* parser)
11666 tree id = NULL_TREE;
11667 cp_token *token;
11669 /* Peek at the next token. */
11670 token = cp_lexer_peek_token (parser->lexer);
11671 /* Figure out which operator we have. */
11672 switch (token->type)
11674 case CPP_KEYWORD:
11676 enum tree_code op;
11678 /* The keyword should be either `new' or `delete'. */
11679 if (token->keyword == RID_NEW)
11680 op = NEW_EXPR;
11681 else if (token->keyword == RID_DELETE)
11682 op = DELETE_EXPR;
11683 else
11684 break;
11686 /* Consume the `new' or `delete' token. */
11687 cp_lexer_consume_token (parser->lexer);
11689 /* Peek at the next token. */
11690 token = cp_lexer_peek_token (parser->lexer);
11691 /* If it's a `[' token then this is the array variant of the
11692 operator. */
11693 if (token->type == CPP_OPEN_SQUARE)
11695 /* Consume the `[' token. */
11696 cp_lexer_consume_token (parser->lexer);
11697 /* Look for the `]' token. */
11698 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11699 id = ansi_opname (op == NEW_EXPR
11700 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11702 /* Otherwise, we have the non-array variant. */
11703 else
11704 id = ansi_opname (op);
11706 return id;
11709 case CPP_PLUS:
11710 id = ansi_opname (PLUS_EXPR);
11711 break;
11713 case CPP_MINUS:
11714 id = ansi_opname (MINUS_EXPR);
11715 break;
11717 case CPP_MULT:
11718 id = ansi_opname (MULT_EXPR);
11719 break;
11721 case CPP_DIV:
11722 id = ansi_opname (TRUNC_DIV_EXPR);
11723 break;
11725 case CPP_MOD:
11726 id = ansi_opname (TRUNC_MOD_EXPR);
11727 break;
11729 case CPP_XOR:
11730 id = ansi_opname (BIT_XOR_EXPR);
11731 break;
11733 case CPP_AND:
11734 id = ansi_opname (BIT_AND_EXPR);
11735 break;
11737 case CPP_OR:
11738 id = ansi_opname (BIT_IOR_EXPR);
11739 break;
11741 case CPP_COMPL:
11742 id = ansi_opname (BIT_NOT_EXPR);
11743 break;
11745 case CPP_NOT:
11746 id = ansi_opname (TRUTH_NOT_EXPR);
11747 break;
11749 case CPP_EQ:
11750 id = ansi_assopname (NOP_EXPR);
11751 break;
11753 case CPP_LESS:
11754 id = ansi_opname (LT_EXPR);
11755 break;
11757 case CPP_GREATER:
11758 id = ansi_opname (GT_EXPR);
11759 break;
11761 case CPP_PLUS_EQ:
11762 id = ansi_assopname (PLUS_EXPR);
11763 break;
11765 case CPP_MINUS_EQ:
11766 id = ansi_assopname (MINUS_EXPR);
11767 break;
11769 case CPP_MULT_EQ:
11770 id = ansi_assopname (MULT_EXPR);
11771 break;
11773 case CPP_DIV_EQ:
11774 id = ansi_assopname (TRUNC_DIV_EXPR);
11775 break;
11777 case CPP_MOD_EQ:
11778 id = ansi_assopname (TRUNC_MOD_EXPR);
11779 break;
11781 case CPP_XOR_EQ:
11782 id = ansi_assopname (BIT_XOR_EXPR);
11783 break;
11785 case CPP_AND_EQ:
11786 id = ansi_assopname (BIT_AND_EXPR);
11787 break;
11789 case CPP_OR_EQ:
11790 id = ansi_assopname (BIT_IOR_EXPR);
11791 break;
11793 case CPP_LSHIFT:
11794 id = ansi_opname (LSHIFT_EXPR);
11795 break;
11797 case CPP_RSHIFT:
11798 id = ansi_opname (RSHIFT_EXPR);
11799 break;
11801 case CPP_LSHIFT_EQ:
11802 id = ansi_assopname (LSHIFT_EXPR);
11803 break;
11805 case CPP_RSHIFT_EQ:
11806 id = ansi_assopname (RSHIFT_EXPR);
11807 break;
11809 case CPP_EQ_EQ:
11810 id = ansi_opname (EQ_EXPR);
11811 break;
11813 case CPP_NOT_EQ:
11814 id = ansi_opname (NE_EXPR);
11815 break;
11817 case CPP_LESS_EQ:
11818 id = ansi_opname (LE_EXPR);
11819 break;
11821 case CPP_GREATER_EQ:
11822 id = ansi_opname (GE_EXPR);
11823 break;
11825 case CPP_AND_AND:
11826 id = ansi_opname (TRUTH_ANDIF_EXPR);
11827 break;
11829 case CPP_OR_OR:
11830 id = ansi_opname (TRUTH_ORIF_EXPR);
11831 break;
11833 case CPP_PLUS_PLUS:
11834 id = ansi_opname (POSTINCREMENT_EXPR);
11835 break;
11837 case CPP_MINUS_MINUS:
11838 id = ansi_opname (PREDECREMENT_EXPR);
11839 break;
11841 case CPP_COMMA:
11842 id = ansi_opname (COMPOUND_EXPR);
11843 break;
11845 case CPP_DEREF_STAR:
11846 id = ansi_opname (MEMBER_REF);
11847 break;
11849 case CPP_DEREF:
11850 id = ansi_opname (COMPONENT_REF);
11851 break;
11853 case CPP_OPEN_PAREN:
11854 /* Consume the `('. */
11855 cp_lexer_consume_token (parser->lexer);
11856 /* Look for the matching `)'. */
11857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11858 return ansi_opname (CALL_EXPR);
11860 case CPP_OPEN_SQUARE:
11861 /* Consume the `['. */
11862 cp_lexer_consume_token (parser->lexer);
11863 /* Look for the matching `]'. */
11864 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11865 return ansi_opname (ARRAY_REF);
11867 case CPP_STRING:
11868 if (cxx_dialect == cxx98)
11869 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11870 if (TREE_STRING_LENGTH (token->u.value) > 2)
11872 error ("expected empty string after %<operator%> keyword");
11873 return error_mark_node;
11875 /* Consume the string. */
11876 cp_lexer_consume_token (parser->lexer);
11877 /* Look for the suffix identifier. */
11878 token = cp_lexer_peek_token (parser->lexer);
11879 if (token->type == CPP_NAME)
11881 id = cp_parser_identifier (parser);
11882 if (id != error_mark_node)
11884 const char *name = IDENTIFIER_POINTER (id);
11885 return cp_literal_operator_id (name);
11888 else
11890 error ("expected suffix identifier");
11891 return error_mark_node;
11894 case CPP_STRING_USERDEF:
11895 error ("missing space between %<\"\"%> and suffix identifier");
11896 return error_mark_node;
11898 default:
11899 /* Anything else is an error. */
11900 break;
11903 /* If we have selected an identifier, we need to consume the
11904 operator token. */
11905 if (id)
11906 cp_lexer_consume_token (parser->lexer);
11907 /* Otherwise, no valid operator name was present. */
11908 else
11910 cp_parser_error (parser, "expected operator");
11911 id = error_mark_node;
11914 return id;
11917 /* Parse a template-declaration.
11919 template-declaration:
11920 export [opt] template < template-parameter-list > declaration
11922 If MEMBER_P is TRUE, this template-declaration occurs within a
11923 class-specifier.
11925 The grammar rule given by the standard isn't correct. What
11926 is really meant is:
11928 template-declaration:
11929 export [opt] template-parameter-list-seq
11930 decl-specifier-seq [opt] init-declarator [opt] ;
11931 export [opt] template-parameter-list-seq
11932 function-definition
11934 template-parameter-list-seq:
11935 template-parameter-list-seq [opt]
11936 template < template-parameter-list > */
11938 static void
11939 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11941 /* Check for `export'. */
11942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11944 /* Consume the `export' token. */
11945 cp_lexer_consume_token (parser->lexer);
11946 /* Warn that we do not support `export'. */
11947 warning (0, "keyword %<export%> not implemented, and will be ignored");
11950 cp_parser_template_declaration_after_export (parser, member_p);
11953 /* Parse a template-parameter-list.
11955 template-parameter-list:
11956 template-parameter
11957 template-parameter-list , template-parameter
11959 Returns a TREE_LIST. Each node represents a template parameter.
11960 The nodes are connected via their TREE_CHAINs. */
11962 static tree
11963 cp_parser_template_parameter_list (cp_parser* parser)
11965 tree parameter_list = NULL_TREE;
11967 begin_template_parm_list ();
11969 /* The loop below parses the template parms. We first need to know
11970 the total number of template parms to be able to compute proper
11971 canonical types of each dependent type. So after the loop, when
11972 we know the total number of template parms,
11973 end_template_parm_list computes the proper canonical types and
11974 fixes up the dependent types accordingly. */
11975 while (true)
11977 tree parameter;
11978 bool is_non_type;
11979 bool is_parameter_pack;
11980 location_t parm_loc;
11982 /* Parse the template-parameter. */
11983 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11984 parameter = cp_parser_template_parameter (parser,
11985 &is_non_type,
11986 &is_parameter_pack);
11987 /* Add it to the list. */
11988 if (parameter != error_mark_node)
11989 parameter_list = process_template_parm (parameter_list,
11990 parm_loc,
11991 parameter,
11992 is_non_type,
11993 is_parameter_pack);
11994 else
11996 tree err_parm = build_tree_list (parameter, parameter);
11997 parameter_list = chainon (parameter_list, err_parm);
12000 /* If the next token is not a `,', we're done. */
12001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12002 break;
12003 /* Otherwise, consume the `,' token. */
12004 cp_lexer_consume_token (parser->lexer);
12007 return end_template_parm_list (parameter_list);
12010 /* Parse a template-parameter.
12012 template-parameter:
12013 type-parameter
12014 parameter-declaration
12016 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12017 the parameter. The TREE_PURPOSE is the default value, if any.
12018 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12019 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12020 set to true iff this parameter is a parameter pack. */
12022 static tree
12023 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12024 bool *is_parameter_pack)
12026 cp_token *token;
12027 cp_parameter_declarator *parameter_declarator;
12028 cp_declarator *id_declarator;
12029 tree parm;
12031 /* Assume it is a type parameter or a template parameter. */
12032 *is_non_type = false;
12033 /* Assume it not a parameter pack. */
12034 *is_parameter_pack = false;
12035 /* Peek at the next token. */
12036 token = cp_lexer_peek_token (parser->lexer);
12037 /* If it is `class' or `template', we have a type-parameter. */
12038 if (token->keyword == RID_TEMPLATE)
12039 return cp_parser_type_parameter (parser, is_parameter_pack);
12040 /* If it is `class' or `typename' we do not know yet whether it is a
12041 type parameter or a non-type parameter. Consider:
12043 template <typename T, typename T::X X> ...
12047 template <class C, class D*> ...
12049 Here, the first parameter is a type parameter, and the second is
12050 a non-type parameter. We can tell by looking at the token after
12051 the identifier -- if it is a `,', `=', or `>' then we have a type
12052 parameter. */
12053 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12055 /* Peek at the token after `class' or `typename'. */
12056 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12057 /* If it's an ellipsis, we have a template type parameter
12058 pack. */
12059 if (token->type == CPP_ELLIPSIS)
12060 return cp_parser_type_parameter (parser, is_parameter_pack);
12061 /* If it's an identifier, skip it. */
12062 if (token->type == CPP_NAME)
12063 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12064 /* Now, see if the token looks like the end of a template
12065 parameter. */
12066 if (token->type == CPP_COMMA
12067 || token->type == CPP_EQ
12068 || token->type == CPP_GREATER)
12069 return cp_parser_type_parameter (parser, is_parameter_pack);
12072 /* Otherwise, it is a non-type parameter.
12074 [temp.param]
12076 When parsing a default template-argument for a non-type
12077 template-parameter, the first non-nested `>' is taken as the end
12078 of the template parameter-list rather than a greater-than
12079 operator. */
12080 *is_non_type = true;
12081 parameter_declarator
12082 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12083 /*parenthesized_p=*/NULL);
12085 /* If the parameter declaration is marked as a parameter pack, set
12086 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12087 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12088 grokdeclarator. */
12089 if (parameter_declarator
12090 && parameter_declarator->declarator
12091 && parameter_declarator->declarator->parameter_pack_p)
12093 *is_parameter_pack = true;
12094 parameter_declarator->declarator->parameter_pack_p = false;
12097 /* If the next token is an ellipsis, and we don't already have it
12098 marked as a parameter pack, then we have a parameter pack (that
12099 has no declarator). */
12100 if (!*is_parameter_pack
12101 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12102 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12104 /* Consume the `...'. */
12105 cp_lexer_consume_token (parser->lexer);
12106 maybe_warn_variadic_templates ();
12108 *is_parameter_pack = true;
12110 /* We might end up with a pack expansion as the type of the non-type
12111 template parameter, in which case this is a non-type template
12112 parameter pack. */
12113 else if (parameter_declarator
12114 && parameter_declarator->decl_specifiers.type
12115 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12117 *is_parameter_pack = true;
12118 parameter_declarator->decl_specifiers.type =
12119 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12122 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12124 /* Parameter packs cannot have default arguments. However, a
12125 user may try to do so, so we'll parse them and give an
12126 appropriate diagnostic here. */
12128 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12130 /* Find the name of the parameter pack. */
12131 id_declarator = parameter_declarator->declarator;
12132 while (id_declarator && id_declarator->kind != cdk_id)
12133 id_declarator = id_declarator->declarator;
12135 if (id_declarator && id_declarator->kind == cdk_id)
12136 error_at (start_token->location,
12137 "template parameter pack %qD cannot have a default argument",
12138 id_declarator->u.id.unqualified_name);
12139 else
12140 error_at (start_token->location,
12141 "template parameter pack cannot have a default argument");
12143 /* Parse the default argument, but throw away the result. */
12144 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12147 parm = grokdeclarator (parameter_declarator->declarator,
12148 &parameter_declarator->decl_specifiers,
12149 TPARM, /*initialized=*/0,
12150 /*attrlist=*/NULL);
12151 if (parm == error_mark_node)
12152 return error_mark_node;
12154 return build_tree_list (parameter_declarator->default_argument, parm);
12157 /* Parse a type-parameter.
12159 type-parameter:
12160 class identifier [opt]
12161 class identifier [opt] = type-id
12162 typename identifier [opt]
12163 typename identifier [opt] = type-id
12164 template < template-parameter-list > class identifier [opt]
12165 template < template-parameter-list > class identifier [opt]
12166 = id-expression
12168 GNU Extension (variadic templates):
12170 type-parameter:
12171 class ... identifier [opt]
12172 typename ... identifier [opt]
12174 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12175 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12176 the declaration of the parameter.
12178 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12180 static tree
12181 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12183 cp_token *token;
12184 tree parameter;
12186 /* Look for a keyword to tell us what kind of parameter this is. */
12187 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12188 if (!token)
12189 return error_mark_node;
12191 switch (token->keyword)
12193 case RID_CLASS:
12194 case RID_TYPENAME:
12196 tree identifier;
12197 tree default_argument;
12199 /* If the next token is an ellipsis, we have a template
12200 argument pack. */
12201 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12203 /* Consume the `...' token. */
12204 cp_lexer_consume_token (parser->lexer);
12205 maybe_warn_variadic_templates ();
12207 *is_parameter_pack = true;
12210 /* If the next token is an identifier, then it names the
12211 parameter. */
12212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12213 identifier = cp_parser_identifier (parser);
12214 else
12215 identifier = NULL_TREE;
12217 /* Create the parameter. */
12218 parameter = finish_template_type_parm (class_type_node, identifier);
12220 /* If the next token is an `=', we have a default argument. */
12221 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12223 /* Consume the `=' token. */
12224 cp_lexer_consume_token (parser->lexer);
12225 /* Parse the default-argument. */
12226 push_deferring_access_checks (dk_no_deferred);
12227 default_argument = cp_parser_type_id (parser);
12229 /* Template parameter packs cannot have default
12230 arguments. */
12231 if (*is_parameter_pack)
12233 if (identifier)
12234 error_at (token->location,
12235 "template parameter pack %qD cannot have a "
12236 "default argument", identifier);
12237 else
12238 error_at (token->location,
12239 "template parameter packs cannot have "
12240 "default arguments");
12241 default_argument = NULL_TREE;
12243 pop_deferring_access_checks ();
12245 else
12246 default_argument = NULL_TREE;
12248 /* Create the combined representation of the parameter and the
12249 default argument. */
12250 parameter = build_tree_list (default_argument, parameter);
12252 break;
12254 case RID_TEMPLATE:
12256 tree identifier;
12257 tree default_argument;
12259 /* Look for the `<'. */
12260 cp_parser_require (parser, CPP_LESS, RT_LESS);
12261 /* Parse the template-parameter-list. */
12262 cp_parser_template_parameter_list (parser);
12263 /* Look for the `>'. */
12264 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12265 /* Look for the `class' keyword. */
12266 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12267 /* If the next token is an ellipsis, we have a template
12268 argument pack. */
12269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12271 /* Consume the `...' token. */
12272 cp_lexer_consume_token (parser->lexer);
12273 maybe_warn_variadic_templates ();
12275 *is_parameter_pack = true;
12277 /* If the next token is an `=', then there is a
12278 default-argument. If the next token is a `>', we are at
12279 the end of the parameter-list. If the next token is a `,',
12280 then we are at the end of this parameter. */
12281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12282 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12283 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12285 identifier = cp_parser_identifier (parser);
12286 /* Treat invalid names as if the parameter were nameless. */
12287 if (identifier == error_mark_node)
12288 identifier = NULL_TREE;
12290 else
12291 identifier = NULL_TREE;
12293 /* Create the template parameter. */
12294 parameter = finish_template_template_parm (class_type_node,
12295 identifier);
12297 /* If the next token is an `=', then there is a
12298 default-argument. */
12299 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12301 bool is_template;
12303 /* Consume the `='. */
12304 cp_lexer_consume_token (parser->lexer);
12305 /* Parse the id-expression. */
12306 push_deferring_access_checks (dk_no_deferred);
12307 /* save token before parsing the id-expression, for error
12308 reporting */
12309 token = cp_lexer_peek_token (parser->lexer);
12310 default_argument
12311 = cp_parser_id_expression (parser,
12312 /*template_keyword_p=*/false,
12313 /*check_dependency_p=*/true,
12314 /*template_p=*/&is_template,
12315 /*declarator_p=*/false,
12316 /*optional_p=*/false);
12317 if (TREE_CODE (default_argument) == TYPE_DECL)
12318 /* If the id-expression was a template-id that refers to
12319 a template-class, we already have the declaration here,
12320 so no further lookup is needed. */
12322 else
12323 /* Look up the name. */
12324 default_argument
12325 = cp_parser_lookup_name (parser, default_argument,
12326 none_type,
12327 /*is_template=*/is_template,
12328 /*is_namespace=*/false,
12329 /*check_dependency=*/true,
12330 /*ambiguous_decls=*/NULL,
12331 token->location);
12332 /* See if the default argument is valid. */
12333 default_argument
12334 = check_template_template_default_arg (default_argument);
12336 /* Template parameter packs cannot have default
12337 arguments. */
12338 if (*is_parameter_pack)
12340 if (identifier)
12341 error_at (token->location,
12342 "template parameter pack %qD cannot "
12343 "have a default argument",
12344 identifier);
12345 else
12346 error_at (token->location, "template parameter packs cannot "
12347 "have default arguments");
12348 default_argument = NULL_TREE;
12350 pop_deferring_access_checks ();
12352 else
12353 default_argument = NULL_TREE;
12355 /* Create the combined representation of the parameter and the
12356 default argument. */
12357 parameter = build_tree_list (default_argument, parameter);
12359 break;
12361 default:
12362 gcc_unreachable ();
12363 break;
12366 return parameter;
12369 /* Parse a template-id.
12371 template-id:
12372 template-name < template-argument-list [opt] >
12374 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12375 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12376 returned. Otherwise, if the template-name names a function, or set
12377 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12378 names a class, returns a TYPE_DECL for the specialization.
12380 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12381 uninstantiated templates. */
12383 static tree
12384 cp_parser_template_id (cp_parser *parser,
12385 bool template_keyword_p,
12386 bool check_dependency_p,
12387 enum tag_types tag_type,
12388 bool is_declaration)
12390 int i;
12391 tree templ;
12392 tree arguments;
12393 tree template_id;
12394 cp_token_position start_of_id = 0;
12395 deferred_access_check *chk;
12396 VEC (deferred_access_check,gc) *access_check;
12397 cp_token *next_token = NULL, *next_token_2 = NULL;
12398 bool is_identifier;
12400 /* If the next token corresponds to a template-id, there is no need
12401 to reparse it. */
12402 next_token = cp_lexer_peek_token (parser->lexer);
12403 if (next_token->type == CPP_TEMPLATE_ID)
12405 struct tree_check *check_value;
12407 /* Get the stored value. */
12408 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12409 /* Perform any access checks that were deferred. */
12410 access_check = check_value->checks;
12411 if (access_check)
12413 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12414 perform_or_defer_access_check (chk->binfo,
12415 chk->decl,
12416 chk->diag_decl,
12417 tf_warning_or_error);
12419 /* Return the stored value. */
12420 return check_value->value;
12423 /* Avoid performing name lookup if there is no possibility of
12424 finding a template-id. */
12425 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12426 || (next_token->type == CPP_NAME
12427 && !cp_parser_nth_token_starts_template_argument_list_p
12428 (parser, 2)))
12430 cp_parser_error (parser, "expected template-id");
12431 return error_mark_node;
12434 /* Remember where the template-id starts. */
12435 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12436 start_of_id = cp_lexer_token_position (parser->lexer, false);
12438 push_deferring_access_checks (dk_deferred);
12440 /* Parse the template-name. */
12441 is_identifier = false;
12442 templ = cp_parser_template_name (parser, template_keyword_p,
12443 check_dependency_p,
12444 is_declaration,
12445 tag_type,
12446 &is_identifier);
12447 if (templ == error_mark_node || is_identifier)
12449 pop_deferring_access_checks ();
12450 return templ;
12453 /* If we find the sequence `[:' after a template-name, it's probably
12454 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12455 parse correctly the argument list. */
12456 next_token = cp_lexer_peek_token (parser->lexer);
12457 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12458 if (next_token->type == CPP_OPEN_SQUARE
12459 && next_token->flags & DIGRAPH
12460 && next_token_2->type == CPP_COLON
12461 && !(next_token_2->flags & PREV_WHITE))
12463 cp_parser_parse_tentatively (parser);
12464 /* Change `:' into `::'. */
12465 next_token_2->type = CPP_SCOPE;
12466 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12467 CPP_LESS. */
12468 cp_lexer_consume_token (parser->lexer);
12470 /* Parse the arguments. */
12471 arguments = cp_parser_enclosed_template_argument_list (parser);
12472 if (!cp_parser_parse_definitely (parser))
12474 /* If we couldn't parse an argument list, then we revert our changes
12475 and return simply an error. Maybe this is not a template-id
12476 after all. */
12477 next_token_2->type = CPP_COLON;
12478 cp_parser_error (parser, "expected %<<%>");
12479 pop_deferring_access_checks ();
12480 return error_mark_node;
12482 /* Otherwise, emit an error about the invalid digraph, but continue
12483 parsing because we got our argument list. In C++11 do not emit
12484 any error, per 2.5/3. */
12485 if (cxx_dialect < cxx0x
12486 && permerror (next_token->location,
12487 "%<<::%> cannot begin a template-argument list"))
12489 static bool hint = false;
12490 inform (next_token->location,
12491 "%<<:%> is an alternate spelling for %<[%>."
12492 " Insert whitespace between %<<%> and %<::%>");
12493 if (!hint && !flag_permissive)
12495 inform (next_token->location, "(if you use %<-fpermissive%> "
12496 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12497 "accept your code)");
12498 hint = true;
12502 else
12504 /* Look for the `<' that starts the template-argument-list. */
12505 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12507 pop_deferring_access_checks ();
12508 return error_mark_node;
12510 /* Parse the arguments. */
12511 arguments = cp_parser_enclosed_template_argument_list (parser);
12514 /* Build a representation of the specialization. */
12515 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12516 template_id = build_min_nt_loc (next_token->location,
12517 TEMPLATE_ID_EXPR,
12518 templ, arguments);
12519 else if (DECL_TYPE_TEMPLATE_P (templ)
12520 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12522 bool entering_scope;
12523 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12524 template (rather than some instantiation thereof) only if
12525 is not nested within some other construct. For example, in
12526 "template <typename T> void f(T) { A<T>::", A<T> is just an
12527 instantiation of A. */
12528 entering_scope = (template_parm_scope_p ()
12529 && cp_lexer_next_token_is (parser->lexer,
12530 CPP_SCOPE));
12531 template_id
12532 = finish_template_type (templ, arguments, entering_scope);
12534 else
12536 /* If it's not a class-template or a template-template, it should be
12537 a function-template. */
12538 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12539 || TREE_CODE (templ) == OVERLOAD
12540 || BASELINK_P (templ)));
12542 template_id = lookup_template_function (templ, arguments);
12545 /* If parsing tentatively, replace the sequence of tokens that makes
12546 up the template-id with a CPP_TEMPLATE_ID token. That way,
12547 should we re-parse the token stream, we will not have to repeat
12548 the effort required to do the parse, nor will we issue duplicate
12549 error messages about problems during instantiation of the
12550 template. */
12551 if (start_of_id)
12553 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12555 /* Reset the contents of the START_OF_ID token. */
12556 token->type = CPP_TEMPLATE_ID;
12557 /* Retrieve any deferred checks. Do not pop this access checks yet
12558 so the memory will not be reclaimed during token replacing below. */
12559 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12560 token->u.tree_check_value->value = template_id;
12561 token->u.tree_check_value->checks = get_deferred_access_checks ();
12562 token->keyword = RID_MAX;
12564 /* Purge all subsequent tokens. */
12565 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12567 /* ??? Can we actually assume that, if template_id ==
12568 error_mark_node, we will have issued a diagnostic to the
12569 user, as opposed to simply marking the tentative parse as
12570 failed? */
12571 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12572 error_at (token->location, "parse error in template argument list");
12575 pop_deferring_access_checks ();
12576 return template_id;
12579 /* Parse a template-name.
12581 template-name:
12582 identifier
12584 The standard should actually say:
12586 template-name:
12587 identifier
12588 operator-function-id
12590 A defect report has been filed about this issue.
12592 A conversion-function-id cannot be a template name because they cannot
12593 be part of a template-id. In fact, looking at this code:
12595 a.operator K<int>()
12597 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12598 It is impossible to call a templated conversion-function-id with an
12599 explicit argument list, since the only allowed template parameter is
12600 the type to which it is converting.
12602 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12603 `template' keyword, in a construction like:
12605 T::template f<3>()
12607 In that case `f' is taken to be a template-name, even though there
12608 is no way of knowing for sure.
12610 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12611 name refers to a set of overloaded functions, at least one of which
12612 is a template, or an IDENTIFIER_NODE with the name of the template,
12613 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12614 names are looked up inside uninstantiated templates. */
12616 static tree
12617 cp_parser_template_name (cp_parser* parser,
12618 bool template_keyword_p,
12619 bool check_dependency_p,
12620 bool is_declaration,
12621 enum tag_types tag_type,
12622 bool *is_identifier)
12624 tree identifier;
12625 tree decl;
12626 tree fns;
12627 cp_token *token = cp_lexer_peek_token (parser->lexer);
12629 /* If the next token is `operator', then we have either an
12630 operator-function-id or a conversion-function-id. */
12631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12633 /* We don't know whether we're looking at an
12634 operator-function-id or a conversion-function-id. */
12635 cp_parser_parse_tentatively (parser);
12636 /* Try an operator-function-id. */
12637 identifier = cp_parser_operator_function_id (parser);
12638 /* If that didn't work, try a conversion-function-id. */
12639 if (!cp_parser_parse_definitely (parser))
12641 cp_parser_error (parser, "expected template-name");
12642 return error_mark_node;
12645 /* Look for the identifier. */
12646 else
12647 identifier = cp_parser_identifier (parser);
12649 /* If we didn't find an identifier, we don't have a template-id. */
12650 if (identifier == error_mark_node)
12651 return error_mark_node;
12653 /* If the name immediately followed the `template' keyword, then it
12654 is a template-name. However, if the next token is not `<', then
12655 we do not treat it as a template-name, since it is not being used
12656 as part of a template-id. This enables us to handle constructs
12657 like:
12659 template <typename T> struct S { S(); };
12660 template <typename T> S<T>::S();
12662 correctly. We would treat `S' as a template -- if it were `S<T>'
12663 -- but we do not if there is no `<'. */
12665 if (processing_template_decl
12666 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12668 /* In a declaration, in a dependent context, we pretend that the
12669 "template" keyword was present in order to improve error
12670 recovery. For example, given:
12672 template <typename T> void f(T::X<int>);
12674 we want to treat "X<int>" as a template-id. */
12675 if (is_declaration
12676 && !template_keyword_p
12677 && parser->scope && TYPE_P (parser->scope)
12678 && check_dependency_p
12679 && dependent_scope_p (parser->scope)
12680 /* Do not do this for dtors (or ctors), since they never
12681 need the template keyword before their name. */
12682 && !constructor_name_p (identifier, parser->scope))
12684 cp_token_position start = 0;
12686 /* Explain what went wrong. */
12687 error_at (token->location, "non-template %qD used as template",
12688 identifier);
12689 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12690 parser->scope, identifier);
12691 /* If parsing tentatively, find the location of the "<" token. */
12692 if (cp_parser_simulate_error (parser))
12693 start = cp_lexer_token_position (parser->lexer, true);
12694 /* Parse the template arguments so that we can issue error
12695 messages about them. */
12696 cp_lexer_consume_token (parser->lexer);
12697 cp_parser_enclosed_template_argument_list (parser);
12698 /* Skip tokens until we find a good place from which to
12699 continue parsing. */
12700 cp_parser_skip_to_closing_parenthesis (parser,
12701 /*recovering=*/true,
12702 /*or_comma=*/true,
12703 /*consume_paren=*/false);
12704 /* If parsing tentatively, permanently remove the
12705 template argument list. That will prevent duplicate
12706 error messages from being issued about the missing
12707 "template" keyword. */
12708 if (start)
12709 cp_lexer_purge_tokens_after (parser->lexer, start);
12710 if (is_identifier)
12711 *is_identifier = true;
12712 return identifier;
12715 /* If the "template" keyword is present, then there is generally
12716 no point in doing name-lookup, so we just return IDENTIFIER.
12717 But, if the qualifying scope is non-dependent then we can
12718 (and must) do name-lookup normally. */
12719 if (template_keyword_p
12720 && (!parser->scope
12721 || (TYPE_P (parser->scope)
12722 && dependent_type_p (parser->scope))))
12723 return identifier;
12726 /* Look up the name. */
12727 decl = cp_parser_lookup_name (parser, identifier,
12728 tag_type,
12729 /*is_template=*/true,
12730 /*is_namespace=*/false,
12731 check_dependency_p,
12732 /*ambiguous_decls=*/NULL,
12733 token->location);
12735 /* If DECL is a template, then the name was a template-name. */
12736 if (TREE_CODE (decl) == TEMPLATE_DECL)
12738 else
12740 tree fn = NULL_TREE;
12742 /* The standard does not explicitly indicate whether a name that
12743 names a set of overloaded declarations, some of which are
12744 templates, is a template-name. However, such a name should
12745 be a template-name; otherwise, there is no way to form a
12746 template-id for the overloaded templates. */
12747 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12748 if (TREE_CODE (fns) == OVERLOAD)
12749 for (fn = fns; fn; fn = OVL_NEXT (fn))
12750 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12751 break;
12753 if (!fn)
12755 /* The name does not name a template. */
12756 cp_parser_error (parser, "expected template-name");
12757 return error_mark_node;
12761 /* If DECL is dependent, and refers to a function, then just return
12762 its name; we will look it up again during template instantiation. */
12763 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12765 tree scope = ovl_scope (decl);
12766 if (TYPE_P (scope) && dependent_type_p (scope))
12767 return identifier;
12770 return decl;
12773 /* Parse a template-argument-list.
12775 template-argument-list:
12776 template-argument ... [opt]
12777 template-argument-list , template-argument ... [opt]
12779 Returns a TREE_VEC containing the arguments. */
12781 static tree
12782 cp_parser_template_argument_list (cp_parser* parser)
12784 tree fixed_args[10];
12785 unsigned n_args = 0;
12786 unsigned alloced = 10;
12787 tree *arg_ary = fixed_args;
12788 tree vec;
12789 bool saved_in_template_argument_list_p;
12790 bool saved_ice_p;
12791 bool saved_non_ice_p;
12793 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12794 parser->in_template_argument_list_p = true;
12795 /* Even if the template-id appears in an integral
12796 constant-expression, the contents of the argument list do
12797 not. */
12798 saved_ice_p = parser->integral_constant_expression_p;
12799 parser->integral_constant_expression_p = false;
12800 saved_non_ice_p = parser->non_integral_constant_expression_p;
12801 parser->non_integral_constant_expression_p = false;
12803 /* Parse the arguments. */
12806 tree argument;
12808 if (n_args)
12809 /* Consume the comma. */
12810 cp_lexer_consume_token (parser->lexer);
12812 /* Parse the template-argument. */
12813 argument = cp_parser_template_argument (parser);
12815 /* If the next token is an ellipsis, we're expanding a template
12816 argument pack. */
12817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12819 if (argument == error_mark_node)
12821 cp_token *token = cp_lexer_peek_token (parser->lexer);
12822 error_at (token->location,
12823 "expected parameter pack before %<...%>");
12825 /* Consume the `...' token. */
12826 cp_lexer_consume_token (parser->lexer);
12828 /* Make the argument into a TYPE_PACK_EXPANSION or
12829 EXPR_PACK_EXPANSION. */
12830 argument = make_pack_expansion (argument);
12833 if (n_args == alloced)
12835 alloced *= 2;
12837 if (arg_ary == fixed_args)
12839 arg_ary = XNEWVEC (tree, alloced);
12840 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12842 else
12843 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12845 arg_ary[n_args++] = argument;
12847 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12849 vec = make_tree_vec (n_args);
12851 while (n_args--)
12852 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12854 if (arg_ary != fixed_args)
12855 free (arg_ary);
12856 parser->non_integral_constant_expression_p = saved_non_ice_p;
12857 parser->integral_constant_expression_p = saved_ice_p;
12858 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12859 #ifdef ENABLE_CHECKING
12860 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12861 #endif
12862 return vec;
12865 /* Parse a template-argument.
12867 template-argument:
12868 assignment-expression
12869 type-id
12870 id-expression
12872 The representation is that of an assignment-expression, type-id, or
12873 id-expression -- except that the qualified id-expression is
12874 evaluated, so that the value returned is either a DECL or an
12875 OVERLOAD.
12877 Although the standard says "assignment-expression", it forbids
12878 throw-expressions or assignments in the template argument.
12879 Therefore, we use "conditional-expression" instead. */
12881 static tree
12882 cp_parser_template_argument (cp_parser* parser)
12884 tree argument;
12885 bool template_p;
12886 bool address_p;
12887 bool maybe_type_id = false;
12888 cp_token *token = NULL, *argument_start_token = NULL;
12889 location_t loc = 0;
12890 cp_id_kind idk;
12892 /* There's really no way to know what we're looking at, so we just
12893 try each alternative in order.
12895 [temp.arg]
12897 In a template-argument, an ambiguity between a type-id and an
12898 expression is resolved to a type-id, regardless of the form of
12899 the corresponding template-parameter.
12901 Therefore, we try a type-id first. */
12902 cp_parser_parse_tentatively (parser);
12903 argument = cp_parser_template_type_arg (parser);
12904 /* If there was no error parsing the type-id but the next token is a
12905 '>>', our behavior depends on which dialect of C++ we're
12906 parsing. In C++98, we probably found a typo for '> >'. But there
12907 are type-id which are also valid expressions. For instance:
12909 struct X { int operator >> (int); };
12910 template <int V> struct Foo {};
12911 Foo<X () >> 5> r;
12913 Here 'X()' is a valid type-id of a function type, but the user just
12914 wanted to write the expression "X() >> 5". Thus, we remember that we
12915 found a valid type-id, but we still try to parse the argument as an
12916 expression to see what happens.
12918 In C++0x, the '>>' will be considered two separate '>'
12919 tokens. */
12920 if (!cp_parser_error_occurred (parser)
12921 && cxx_dialect == cxx98
12922 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12924 maybe_type_id = true;
12925 cp_parser_abort_tentative_parse (parser);
12927 else
12929 /* If the next token isn't a `,' or a `>', then this argument wasn't
12930 really finished. This means that the argument is not a valid
12931 type-id. */
12932 if (!cp_parser_next_token_ends_template_argument_p (parser))
12933 cp_parser_error (parser, "expected template-argument");
12934 /* If that worked, we're done. */
12935 if (cp_parser_parse_definitely (parser))
12936 return argument;
12938 /* We're still not sure what the argument will be. */
12939 cp_parser_parse_tentatively (parser);
12940 /* Try a template. */
12941 argument_start_token = cp_lexer_peek_token (parser->lexer);
12942 argument = cp_parser_id_expression (parser,
12943 /*template_keyword_p=*/false,
12944 /*check_dependency_p=*/true,
12945 &template_p,
12946 /*declarator_p=*/false,
12947 /*optional_p=*/false);
12948 /* If the next token isn't a `,' or a `>', then this argument wasn't
12949 really finished. */
12950 if (!cp_parser_next_token_ends_template_argument_p (parser))
12951 cp_parser_error (parser, "expected template-argument");
12952 if (!cp_parser_error_occurred (parser))
12954 /* Figure out what is being referred to. If the id-expression
12955 was for a class template specialization, then we will have a
12956 TYPE_DECL at this point. There is no need to do name lookup
12957 at this point in that case. */
12958 if (TREE_CODE (argument) != TYPE_DECL)
12959 argument = cp_parser_lookup_name (parser, argument,
12960 none_type,
12961 /*is_template=*/template_p,
12962 /*is_namespace=*/false,
12963 /*check_dependency=*/true,
12964 /*ambiguous_decls=*/NULL,
12965 argument_start_token->location);
12966 if (TREE_CODE (argument) != TEMPLATE_DECL
12967 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12968 cp_parser_error (parser, "expected template-name");
12970 if (cp_parser_parse_definitely (parser))
12971 return argument;
12972 /* It must be a non-type argument. There permitted cases are given
12973 in [temp.arg.nontype]:
12975 -- an integral constant-expression of integral or enumeration
12976 type; or
12978 -- the name of a non-type template-parameter; or
12980 -- the name of an object or function with external linkage...
12982 -- the address of an object or function with external linkage...
12984 -- a pointer to member... */
12985 /* Look for a non-type template parameter. */
12986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12988 cp_parser_parse_tentatively (parser);
12989 argument = cp_parser_primary_expression (parser,
12990 /*address_p=*/false,
12991 /*cast_p=*/false,
12992 /*template_arg_p=*/true,
12993 &idk);
12994 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12995 || !cp_parser_next_token_ends_template_argument_p (parser))
12996 cp_parser_simulate_error (parser);
12997 if (cp_parser_parse_definitely (parser))
12998 return argument;
13001 /* If the next token is "&", the argument must be the address of an
13002 object or function with external linkage. */
13003 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13004 if (address_p)
13006 loc = cp_lexer_peek_token (parser->lexer)->location;
13007 cp_lexer_consume_token (parser->lexer);
13009 /* See if we might have an id-expression. */
13010 token = cp_lexer_peek_token (parser->lexer);
13011 if (token->type == CPP_NAME
13012 || token->keyword == RID_OPERATOR
13013 || token->type == CPP_SCOPE
13014 || token->type == CPP_TEMPLATE_ID
13015 || token->type == CPP_NESTED_NAME_SPECIFIER)
13017 cp_parser_parse_tentatively (parser);
13018 argument = cp_parser_primary_expression (parser,
13019 address_p,
13020 /*cast_p=*/false,
13021 /*template_arg_p=*/true,
13022 &idk);
13023 if (cp_parser_error_occurred (parser)
13024 || !cp_parser_next_token_ends_template_argument_p (parser))
13025 cp_parser_abort_tentative_parse (parser);
13026 else
13028 tree probe;
13030 if (TREE_CODE (argument) == INDIRECT_REF)
13032 gcc_assert (REFERENCE_REF_P (argument));
13033 argument = TREE_OPERAND (argument, 0);
13036 /* If we're in a template, we represent a qualified-id referring
13037 to a static data member as a SCOPE_REF even if the scope isn't
13038 dependent so that we can check access control later. */
13039 probe = argument;
13040 if (TREE_CODE (probe) == SCOPE_REF)
13041 probe = TREE_OPERAND (probe, 1);
13042 if (TREE_CODE (probe) == VAR_DECL)
13044 /* A variable without external linkage might still be a
13045 valid constant-expression, so no error is issued here
13046 if the external-linkage check fails. */
13047 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13048 cp_parser_simulate_error (parser);
13050 else if (is_overloaded_fn (argument))
13051 /* All overloaded functions are allowed; if the external
13052 linkage test does not pass, an error will be issued
13053 later. */
13055 else if (address_p
13056 && (TREE_CODE (argument) == OFFSET_REF
13057 || TREE_CODE (argument) == SCOPE_REF))
13058 /* A pointer-to-member. */
13060 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13062 else
13063 cp_parser_simulate_error (parser);
13065 if (cp_parser_parse_definitely (parser))
13067 if (address_p)
13068 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13069 tf_warning_or_error);
13070 return argument;
13074 /* If the argument started with "&", there are no other valid
13075 alternatives at this point. */
13076 if (address_p)
13078 cp_parser_error (parser, "invalid non-type template argument");
13079 return error_mark_node;
13082 /* If the argument wasn't successfully parsed as a type-id followed
13083 by '>>', the argument can only be a constant expression now.
13084 Otherwise, we try parsing the constant-expression tentatively,
13085 because the argument could really be a type-id. */
13086 if (maybe_type_id)
13087 cp_parser_parse_tentatively (parser);
13088 argument = cp_parser_constant_expression (parser,
13089 /*allow_non_constant_p=*/false,
13090 /*non_constant_p=*/NULL);
13091 argument = fold_non_dependent_expr (argument);
13092 if (!maybe_type_id)
13093 return argument;
13094 if (!cp_parser_next_token_ends_template_argument_p (parser))
13095 cp_parser_error (parser, "expected template-argument");
13096 if (cp_parser_parse_definitely (parser))
13097 return argument;
13098 /* We did our best to parse the argument as a non type-id, but that
13099 was the only alternative that matched (albeit with a '>' after
13100 it). We can assume it's just a typo from the user, and a
13101 diagnostic will then be issued. */
13102 return cp_parser_template_type_arg (parser);
13105 /* Parse an explicit-instantiation.
13107 explicit-instantiation:
13108 template declaration
13110 Although the standard says `declaration', what it really means is:
13112 explicit-instantiation:
13113 template decl-specifier-seq [opt] declarator [opt] ;
13115 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13116 supposed to be allowed. A defect report has been filed about this
13117 issue.
13119 GNU Extension:
13121 explicit-instantiation:
13122 storage-class-specifier template
13123 decl-specifier-seq [opt] declarator [opt] ;
13124 function-specifier template
13125 decl-specifier-seq [opt] declarator [opt] ; */
13127 static void
13128 cp_parser_explicit_instantiation (cp_parser* parser)
13130 int declares_class_or_enum;
13131 cp_decl_specifier_seq decl_specifiers;
13132 tree extension_specifier = NULL_TREE;
13134 timevar_push (TV_TEMPLATE_INST);
13136 /* Look for an (optional) storage-class-specifier or
13137 function-specifier. */
13138 if (cp_parser_allow_gnu_extensions_p (parser))
13140 extension_specifier
13141 = cp_parser_storage_class_specifier_opt (parser);
13142 if (!extension_specifier)
13143 extension_specifier
13144 = cp_parser_function_specifier_opt (parser,
13145 /*decl_specs=*/NULL);
13148 /* Look for the `template' keyword. */
13149 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13150 /* Let the front end know that we are processing an explicit
13151 instantiation. */
13152 begin_explicit_instantiation ();
13153 /* [temp.explicit] says that we are supposed to ignore access
13154 control while processing explicit instantiation directives. */
13155 push_deferring_access_checks (dk_no_check);
13156 /* Parse a decl-specifier-seq. */
13157 cp_parser_decl_specifier_seq (parser,
13158 CP_PARSER_FLAGS_OPTIONAL,
13159 &decl_specifiers,
13160 &declares_class_or_enum);
13161 /* If there was exactly one decl-specifier, and it declared a class,
13162 and there's no declarator, then we have an explicit type
13163 instantiation. */
13164 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13166 tree type;
13168 type = check_tag_decl (&decl_specifiers);
13169 /* Turn access control back on for names used during
13170 template instantiation. */
13171 pop_deferring_access_checks ();
13172 if (type)
13173 do_type_instantiation (type, extension_specifier,
13174 /*complain=*/tf_error);
13176 else
13178 cp_declarator *declarator;
13179 tree decl;
13181 /* Parse the declarator. */
13182 declarator
13183 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13184 /*ctor_dtor_or_conv_p=*/NULL,
13185 /*parenthesized_p=*/NULL,
13186 /*member_p=*/false);
13187 if (declares_class_or_enum & 2)
13188 cp_parser_check_for_definition_in_return_type (declarator,
13189 decl_specifiers.type,
13190 decl_specifiers.locations[ds_type_spec]);
13191 if (declarator != cp_error_declarator)
13193 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13194 permerror (decl_specifiers.locations[ds_inline],
13195 "explicit instantiation shall not use"
13196 " %<inline%> specifier");
13197 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13198 permerror (decl_specifiers.locations[ds_constexpr],
13199 "explicit instantiation shall not use"
13200 " %<constexpr%> specifier");
13202 decl = grokdeclarator (declarator, &decl_specifiers,
13203 NORMAL, 0, &decl_specifiers.attributes);
13204 /* Turn access control back on for names used during
13205 template instantiation. */
13206 pop_deferring_access_checks ();
13207 /* Do the explicit instantiation. */
13208 do_decl_instantiation (decl, extension_specifier);
13210 else
13212 pop_deferring_access_checks ();
13213 /* Skip the body of the explicit instantiation. */
13214 cp_parser_skip_to_end_of_statement (parser);
13217 /* We're done with the instantiation. */
13218 end_explicit_instantiation ();
13220 cp_parser_consume_semicolon_at_end_of_statement (parser);
13222 timevar_pop (TV_TEMPLATE_INST);
13225 /* Parse an explicit-specialization.
13227 explicit-specialization:
13228 template < > declaration
13230 Although the standard says `declaration', what it really means is:
13232 explicit-specialization:
13233 template <> decl-specifier [opt] init-declarator [opt] ;
13234 template <> function-definition
13235 template <> explicit-specialization
13236 template <> template-declaration */
13238 static void
13239 cp_parser_explicit_specialization (cp_parser* parser)
13241 bool need_lang_pop;
13242 cp_token *token = cp_lexer_peek_token (parser->lexer);
13244 /* Look for the `template' keyword. */
13245 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13246 /* Look for the `<'. */
13247 cp_parser_require (parser, CPP_LESS, RT_LESS);
13248 /* Look for the `>'. */
13249 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13250 /* We have processed another parameter list. */
13251 ++parser->num_template_parameter_lists;
13252 /* [temp]
13254 A template ... explicit specialization ... shall not have C
13255 linkage. */
13256 if (current_lang_name == lang_name_c)
13258 error_at (token->location, "template specialization with C linkage");
13259 /* Give it C++ linkage to avoid confusing other parts of the
13260 front end. */
13261 push_lang_context (lang_name_cplusplus);
13262 need_lang_pop = true;
13264 else
13265 need_lang_pop = false;
13266 /* Let the front end know that we are beginning a specialization. */
13267 if (!begin_specialization ())
13269 end_specialization ();
13270 return;
13273 /* If the next keyword is `template', we need to figure out whether
13274 or not we're looking a template-declaration. */
13275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13277 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13278 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13279 cp_parser_template_declaration_after_export (parser,
13280 /*member_p=*/false);
13281 else
13282 cp_parser_explicit_specialization (parser);
13284 else
13285 /* Parse the dependent declaration. */
13286 cp_parser_single_declaration (parser,
13287 /*checks=*/NULL,
13288 /*member_p=*/false,
13289 /*explicit_specialization_p=*/true,
13290 /*friend_p=*/NULL);
13291 /* We're done with the specialization. */
13292 end_specialization ();
13293 /* For the erroneous case of a template with C linkage, we pushed an
13294 implicit C++ linkage scope; exit that scope now. */
13295 if (need_lang_pop)
13296 pop_lang_context ();
13297 /* We're done with this parameter list. */
13298 --parser->num_template_parameter_lists;
13301 /* Parse a type-specifier.
13303 type-specifier:
13304 simple-type-specifier
13305 class-specifier
13306 enum-specifier
13307 elaborated-type-specifier
13308 cv-qualifier
13310 GNU Extension:
13312 type-specifier:
13313 __complex__
13315 Returns a representation of the type-specifier. For a
13316 class-specifier, enum-specifier, or elaborated-type-specifier, a
13317 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13319 The parser flags FLAGS is used to control type-specifier parsing.
13321 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13322 in a decl-specifier-seq.
13324 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13325 class-specifier, enum-specifier, or elaborated-type-specifier, then
13326 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13327 if a type is declared; 2 if it is defined. Otherwise, it is set to
13328 zero.
13330 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13331 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13332 is set to FALSE. */
13334 static tree
13335 cp_parser_type_specifier (cp_parser* parser,
13336 cp_parser_flags flags,
13337 cp_decl_specifier_seq *decl_specs,
13338 bool is_declaration,
13339 int* declares_class_or_enum,
13340 bool* is_cv_qualifier)
13342 tree type_spec = NULL_TREE;
13343 cp_token *token;
13344 enum rid keyword;
13345 cp_decl_spec ds = ds_last;
13347 /* Assume this type-specifier does not declare a new type. */
13348 if (declares_class_or_enum)
13349 *declares_class_or_enum = 0;
13350 /* And that it does not specify a cv-qualifier. */
13351 if (is_cv_qualifier)
13352 *is_cv_qualifier = false;
13353 /* Peek at the next token. */
13354 token = cp_lexer_peek_token (parser->lexer);
13356 /* If we're looking at a keyword, we can use that to guide the
13357 production we choose. */
13358 keyword = token->keyword;
13359 switch (keyword)
13361 case RID_ENUM:
13362 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13363 goto elaborated_type_specifier;
13365 /* Look for the enum-specifier. */
13366 type_spec = cp_parser_enum_specifier (parser);
13367 /* If that worked, we're done. */
13368 if (type_spec)
13370 if (declares_class_or_enum)
13371 *declares_class_or_enum = 2;
13372 if (decl_specs)
13373 cp_parser_set_decl_spec_type (decl_specs,
13374 type_spec,
13375 token->location,
13376 /*type_definition_p=*/true);
13377 return type_spec;
13379 else
13380 goto elaborated_type_specifier;
13382 /* Any of these indicate either a class-specifier, or an
13383 elaborated-type-specifier. */
13384 case RID_CLASS:
13385 case RID_STRUCT:
13386 case RID_UNION:
13387 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13388 goto elaborated_type_specifier;
13390 /* Parse tentatively so that we can back up if we don't find a
13391 class-specifier. */
13392 cp_parser_parse_tentatively (parser);
13393 /* Look for the class-specifier. */
13394 type_spec = cp_parser_class_specifier (parser);
13395 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13396 /* If that worked, we're done. */
13397 if (cp_parser_parse_definitely (parser))
13399 if (declares_class_or_enum)
13400 *declares_class_or_enum = 2;
13401 if (decl_specs)
13402 cp_parser_set_decl_spec_type (decl_specs,
13403 type_spec,
13404 token->location,
13405 /*type_definition_p=*/true);
13406 return type_spec;
13409 /* Fall through. */
13410 elaborated_type_specifier:
13411 /* We're declaring (not defining) a class or enum. */
13412 if (declares_class_or_enum)
13413 *declares_class_or_enum = 1;
13415 /* Fall through. */
13416 case RID_TYPENAME:
13417 /* Look for an elaborated-type-specifier. */
13418 type_spec
13419 = (cp_parser_elaborated_type_specifier
13420 (parser,
13421 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13422 is_declaration));
13423 if (decl_specs)
13424 cp_parser_set_decl_spec_type (decl_specs,
13425 type_spec,
13426 token->location,
13427 /*type_definition_p=*/false);
13428 return type_spec;
13430 case RID_CONST:
13431 ds = ds_const;
13432 if (is_cv_qualifier)
13433 *is_cv_qualifier = true;
13434 break;
13436 case RID_VOLATILE:
13437 ds = ds_volatile;
13438 if (is_cv_qualifier)
13439 *is_cv_qualifier = true;
13440 break;
13442 case RID_RESTRICT:
13443 ds = ds_restrict;
13444 if (is_cv_qualifier)
13445 *is_cv_qualifier = true;
13446 break;
13448 case RID_COMPLEX:
13449 /* The `__complex__' keyword is a GNU extension. */
13450 ds = ds_complex;
13451 break;
13453 default:
13454 break;
13457 /* Handle simple keywords. */
13458 if (ds != ds_last)
13460 if (decl_specs)
13462 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13463 decl_specs->any_specifiers_p = true;
13465 return cp_lexer_consume_token (parser->lexer)->u.value;
13468 /* If we do not already have a type-specifier, assume we are looking
13469 at a simple-type-specifier. */
13470 type_spec = cp_parser_simple_type_specifier (parser,
13471 decl_specs,
13472 flags);
13474 /* If we didn't find a type-specifier, and a type-specifier was not
13475 optional in this context, issue an error message. */
13476 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13478 cp_parser_error (parser, "expected type specifier");
13479 return error_mark_node;
13482 return type_spec;
13485 /* Parse a simple-type-specifier.
13487 simple-type-specifier:
13488 :: [opt] nested-name-specifier [opt] type-name
13489 :: [opt] nested-name-specifier template template-id
13490 char
13491 wchar_t
13492 bool
13493 short
13495 long
13496 signed
13497 unsigned
13498 float
13499 double
13500 void
13502 C++0x Extension:
13504 simple-type-specifier:
13505 auto
13506 decltype ( expression )
13507 char16_t
13508 char32_t
13509 __underlying_type ( type-id )
13511 GNU Extension:
13513 simple-type-specifier:
13514 __int128
13515 __typeof__ unary-expression
13516 __typeof__ ( type-id )
13518 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13519 appropriately updated. */
13521 static tree
13522 cp_parser_simple_type_specifier (cp_parser* parser,
13523 cp_decl_specifier_seq *decl_specs,
13524 cp_parser_flags flags)
13526 tree type = NULL_TREE;
13527 cp_token *token;
13529 /* Peek at the next token. */
13530 token = cp_lexer_peek_token (parser->lexer);
13532 /* If we're looking at a keyword, things are easy. */
13533 switch (token->keyword)
13535 case RID_CHAR:
13536 if (decl_specs)
13537 decl_specs->explicit_char_p = true;
13538 type = char_type_node;
13539 break;
13540 case RID_CHAR16:
13541 type = char16_type_node;
13542 break;
13543 case RID_CHAR32:
13544 type = char32_type_node;
13545 break;
13546 case RID_WCHAR:
13547 type = wchar_type_node;
13548 break;
13549 case RID_BOOL:
13550 type = boolean_type_node;
13551 break;
13552 case RID_SHORT:
13553 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13554 type = short_integer_type_node;
13555 break;
13556 case RID_INT:
13557 if (decl_specs)
13558 decl_specs->explicit_int_p = true;
13559 type = integer_type_node;
13560 break;
13561 case RID_INT128:
13562 if (!int128_integer_type_node)
13563 break;
13564 if (decl_specs)
13565 decl_specs->explicit_int128_p = true;
13566 type = int128_integer_type_node;
13567 break;
13568 case RID_LONG:
13569 if (decl_specs)
13570 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13571 type = long_integer_type_node;
13572 break;
13573 case RID_SIGNED:
13574 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13575 type = integer_type_node;
13576 break;
13577 case RID_UNSIGNED:
13578 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13579 type = unsigned_type_node;
13580 break;
13581 case RID_FLOAT:
13582 type = float_type_node;
13583 break;
13584 case RID_DOUBLE:
13585 type = double_type_node;
13586 break;
13587 case RID_VOID:
13588 type = void_type_node;
13589 break;
13591 case RID_AUTO:
13592 maybe_warn_cpp0x (CPP0X_AUTO);
13593 type = make_auto ();
13594 break;
13596 case RID_DECLTYPE:
13597 /* Since DR 743, decltype can either be a simple-type-specifier by
13598 itself or begin a nested-name-specifier. Parsing it will replace
13599 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13600 handling below decide what to do. */
13601 cp_parser_decltype (parser);
13602 cp_lexer_set_token_position (parser->lexer, token);
13603 break;
13605 case RID_TYPEOF:
13606 /* Consume the `typeof' token. */
13607 cp_lexer_consume_token (parser->lexer);
13608 /* Parse the operand to `typeof'. */
13609 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13610 /* If it is not already a TYPE, take its type. */
13611 if (!TYPE_P (type))
13612 type = finish_typeof (type);
13614 if (decl_specs)
13615 cp_parser_set_decl_spec_type (decl_specs, type,
13616 token->location,
13617 /*type_definition_p=*/false);
13619 return type;
13621 case RID_UNDERLYING_TYPE:
13622 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13623 if (decl_specs)
13624 cp_parser_set_decl_spec_type (decl_specs, type,
13625 token->location,
13626 /*type_definition_p=*/false);
13628 return type;
13630 case RID_BASES:
13631 case RID_DIRECT_BASES:
13632 type = cp_parser_trait_expr (parser, token->keyword);
13633 if (decl_specs)
13634 cp_parser_set_decl_spec_type (decl_specs, type,
13635 token->location,
13636 /*type_definition_p=*/false);
13637 return type;
13638 default:
13639 break;
13642 /* If token is an already-parsed decltype not followed by ::,
13643 it's a simple-type-specifier. */
13644 if (token->type == CPP_DECLTYPE
13645 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13647 type = token->u.value;
13648 if (decl_specs)
13649 cp_parser_set_decl_spec_type (decl_specs, type,
13650 token->location,
13651 /*type_definition_p=*/false);
13652 cp_lexer_consume_token (parser->lexer);
13653 return type;
13656 /* If the type-specifier was for a built-in type, we're done. */
13657 if (type)
13659 /* Record the type. */
13660 if (decl_specs
13661 && (token->keyword != RID_SIGNED
13662 && token->keyword != RID_UNSIGNED
13663 && token->keyword != RID_SHORT
13664 && token->keyword != RID_LONG))
13665 cp_parser_set_decl_spec_type (decl_specs,
13666 type,
13667 token->location,
13668 /*type_definition_p=*/false);
13669 if (decl_specs)
13670 decl_specs->any_specifiers_p = true;
13672 /* Consume the token. */
13673 cp_lexer_consume_token (parser->lexer);
13675 /* There is no valid C++ program where a non-template type is
13676 followed by a "<". That usually indicates that the user thought
13677 that the type was a template. */
13678 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13679 token->location);
13681 return TYPE_NAME (type);
13684 /* The type-specifier must be a user-defined type. */
13685 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13687 bool qualified_p;
13688 bool global_p;
13690 /* Don't gobble tokens or issue error messages if this is an
13691 optional type-specifier. */
13692 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13693 cp_parser_parse_tentatively (parser);
13695 /* Look for the optional `::' operator. */
13696 global_p
13697 = (cp_parser_global_scope_opt (parser,
13698 /*current_scope_valid_p=*/false)
13699 != NULL_TREE);
13700 /* Look for the nested-name specifier. */
13701 qualified_p
13702 = (cp_parser_nested_name_specifier_opt (parser,
13703 /*typename_keyword_p=*/false,
13704 /*check_dependency_p=*/true,
13705 /*type_p=*/false,
13706 /*is_declaration=*/false)
13707 != NULL_TREE);
13708 token = cp_lexer_peek_token (parser->lexer);
13709 /* If we have seen a nested-name-specifier, and the next token
13710 is `template', then we are using the template-id production. */
13711 if (parser->scope
13712 && cp_parser_optional_template_keyword (parser))
13714 /* Look for the template-id. */
13715 type = cp_parser_template_id (parser,
13716 /*template_keyword_p=*/true,
13717 /*check_dependency_p=*/true,
13718 none_type,
13719 /*is_declaration=*/false);
13720 /* If the template-id did not name a type, we are out of
13721 luck. */
13722 if (TREE_CODE (type) != TYPE_DECL)
13724 cp_parser_error (parser, "expected template-id for type");
13725 type = NULL_TREE;
13728 /* Otherwise, look for a type-name. */
13729 else
13730 type = cp_parser_type_name (parser);
13731 /* Keep track of all name-lookups performed in class scopes. */
13732 if (type
13733 && !global_p
13734 && !qualified_p
13735 && TREE_CODE (type) == TYPE_DECL
13736 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13737 maybe_note_name_used_in_class (DECL_NAME (type), type);
13738 /* If it didn't work out, we don't have a TYPE. */
13739 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13740 && !cp_parser_parse_definitely (parser))
13741 type = NULL_TREE;
13742 if (type && decl_specs)
13743 cp_parser_set_decl_spec_type (decl_specs, type,
13744 token->location,
13745 /*type_definition_p=*/false);
13748 /* If we didn't get a type-name, issue an error message. */
13749 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13751 cp_parser_error (parser, "expected type-name");
13752 return error_mark_node;
13755 if (type && type != error_mark_node)
13757 /* See if TYPE is an Objective-C type, and if so, parse and
13758 accept any protocol references following it. Do this before
13759 the cp_parser_check_for_invalid_template_id() call, because
13760 Objective-C types can be followed by '<...>' which would
13761 enclose protocol names rather than template arguments, and so
13762 everything is fine. */
13763 if (c_dialect_objc () && !parser->scope
13764 && (objc_is_id (type) || objc_is_class_name (type)))
13766 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13767 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13769 /* Clobber the "unqualified" type previously entered into
13770 DECL_SPECS with the new, improved protocol-qualified version. */
13771 if (decl_specs)
13772 decl_specs->type = qual_type;
13774 return qual_type;
13777 /* There is no valid C++ program where a non-template type is
13778 followed by a "<". That usually indicates that the user
13779 thought that the type was a template. */
13780 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13781 none_type,
13782 token->location);
13785 return type;
13788 /* Parse a type-name.
13790 type-name:
13791 class-name
13792 enum-name
13793 typedef-name
13794 simple-template-id [in c++0x]
13796 enum-name:
13797 identifier
13799 typedef-name:
13800 identifier
13802 Returns a TYPE_DECL for the type. */
13804 static tree
13805 cp_parser_type_name (cp_parser* parser)
13807 tree type_decl;
13809 /* We can't know yet whether it is a class-name or not. */
13810 cp_parser_parse_tentatively (parser);
13811 /* Try a class-name. */
13812 type_decl = cp_parser_class_name (parser,
13813 /*typename_keyword_p=*/false,
13814 /*template_keyword_p=*/false,
13815 none_type,
13816 /*check_dependency_p=*/true,
13817 /*class_head_p=*/false,
13818 /*is_declaration=*/false);
13819 /* If it's not a class-name, keep looking. */
13820 if (!cp_parser_parse_definitely (parser))
13822 if (cxx_dialect < cxx0x)
13823 /* It must be a typedef-name or an enum-name. */
13824 return cp_parser_nonclass_name (parser);
13826 cp_parser_parse_tentatively (parser);
13827 /* It is either a simple-template-id representing an
13828 instantiation of an alias template... */
13829 type_decl = cp_parser_template_id (parser,
13830 /*template_keyword_p=*/false,
13831 /*check_dependency_p=*/false,
13832 none_type,
13833 /*is_declaration=*/false);
13834 /* Note that this must be an instantiation of an alias template
13835 because [temp.names]/6 says:
13837 A template-id that names an alias template specialization
13838 is a type-name.
13840 Whereas [temp.names]/7 says:
13842 A simple-template-id that names a class template
13843 specialization is a class-name. */
13844 if (type_decl != NULL_TREE
13845 && TREE_CODE (type_decl) == TYPE_DECL
13846 && TYPE_DECL_ALIAS_P (type_decl))
13847 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13848 else
13849 cp_parser_simulate_error (parser);
13851 if (!cp_parser_parse_definitely (parser))
13852 /* ... Or a typedef-name or an enum-name. */
13853 return cp_parser_nonclass_name (parser);
13856 return type_decl;
13859 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13861 enum-name:
13862 identifier
13864 typedef-name:
13865 identifier
13867 Returns a TYPE_DECL for the type. */
13869 static tree
13870 cp_parser_nonclass_name (cp_parser* parser)
13872 tree type_decl;
13873 tree identifier;
13875 cp_token *token = cp_lexer_peek_token (parser->lexer);
13876 identifier = cp_parser_identifier (parser);
13877 if (identifier == error_mark_node)
13878 return error_mark_node;
13880 /* Look up the type-name. */
13881 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13883 if (TREE_CODE (type_decl) == USING_DECL)
13885 if (!DECL_DEPENDENT_P (type_decl))
13886 type_decl = strip_using_decl (type_decl);
13887 else if (USING_DECL_TYPENAME_P (type_decl))
13889 /* We have found a type introduced by a using
13890 declaration at class scope that refers to a dependent
13891 type.
13893 using typename :: [opt] nested-name-specifier unqualified-id ;
13895 type_decl = make_typename_type (TREE_TYPE (type_decl),
13896 DECL_NAME (type_decl),
13897 typename_type, tf_error);
13898 if (type_decl != error_mark_node)
13899 type_decl = TYPE_NAME (type_decl);
13903 if (TREE_CODE (type_decl) != TYPE_DECL
13904 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13906 /* See if this is an Objective-C type. */
13907 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13908 tree type = objc_get_protocol_qualified_type (identifier, protos);
13909 if (type)
13910 type_decl = TYPE_NAME (type);
13913 /* Issue an error if we did not find a type-name. */
13914 if (TREE_CODE (type_decl) != TYPE_DECL
13915 /* In Objective-C, we have the complication that class names are
13916 normally type names and start declarations (eg, the
13917 "NSObject" in "NSObject *object;"), but can be used in an
13918 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13919 is an expression. So, a classname followed by a dot is not a
13920 valid type-name. */
13921 || (objc_is_class_name (TREE_TYPE (type_decl))
13922 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13924 if (!cp_parser_simulate_error (parser))
13925 cp_parser_name_lookup_error (parser, identifier, type_decl,
13926 NLE_TYPE, token->location);
13927 return error_mark_node;
13929 /* Remember that the name was used in the definition of the
13930 current class so that we can check later to see if the
13931 meaning would have been different after the class was
13932 entirely defined. */
13933 else if (type_decl != error_mark_node
13934 && !parser->scope)
13935 maybe_note_name_used_in_class (identifier, type_decl);
13937 return type_decl;
13940 /* Parse an elaborated-type-specifier. Note that the grammar given
13941 here incorporates the resolution to DR68.
13943 elaborated-type-specifier:
13944 class-key :: [opt] nested-name-specifier [opt] identifier
13945 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13946 enum-key :: [opt] nested-name-specifier [opt] identifier
13947 typename :: [opt] nested-name-specifier identifier
13948 typename :: [opt] nested-name-specifier template [opt]
13949 template-id
13951 GNU extension:
13953 elaborated-type-specifier:
13954 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13955 class-key attributes :: [opt] nested-name-specifier [opt]
13956 template [opt] template-id
13957 enum attributes :: [opt] nested-name-specifier [opt] identifier
13959 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13960 declared `friend'. If IS_DECLARATION is TRUE, then this
13961 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13962 something is being declared.
13964 Returns the TYPE specified. */
13966 static tree
13967 cp_parser_elaborated_type_specifier (cp_parser* parser,
13968 bool is_friend,
13969 bool is_declaration)
13971 enum tag_types tag_type;
13972 tree identifier;
13973 tree type = NULL_TREE;
13974 tree attributes = NULL_TREE;
13975 tree globalscope;
13976 cp_token *token = NULL;
13978 /* See if we're looking at the `enum' keyword. */
13979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13981 /* Consume the `enum' token. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* Remember that it's an enumeration type. */
13984 tag_type = enum_type;
13985 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13986 enums) is used here. */
13987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13988 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13990 pedwarn (input_location, 0, "elaborated-type-specifier "
13991 "for a scoped enum must not use the %<%D%> keyword",
13992 cp_lexer_peek_token (parser->lexer)->u.value);
13993 /* Consume the `struct' or `class' and parse it anyway. */
13994 cp_lexer_consume_token (parser->lexer);
13996 /* Parse the attributes. */
13997 attributes = cp_parser_attributes_opt (parser);
13999 /* Or, it might be `typename'. */
14000 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14001 RID_TYPENAME))
14003 /* Consume the `typename' token. */
14004 cp_lexer_consume_token (parser->lexer);
14005 /* Remember that it's a `typename' type. */
14006 tag_type = typename_type;
14008 /* Otherwise it must be a class-key. */
14009 else
14011 tag_type = cp_parser_class_key (parser);
14012 if (tag_type == none_type)
14013 return error_mark_node;
14014 /* Parse the attributes. */
14015 attributes = cp_parser_attributes_opt (parser);
14018 /* Look for the `::' operator. */
14019 globalscope = cp_parser_global_scope_opt (parser,
14020 /*current_scope_valid_p=*/false);
14021 /* Look for the nested-name-specifier. */
14022 if (tag_type == typename_type && !globalscope)
14024 if (!cp_parser_nested_name_specifier (parser,
14025 /*typename_keyword_p=*/true,
14026 /*check_dependency_p=*/true,
14027 /*type_p=*/true,
14028 is_declaration))
14029 return error_mark_node;
14031 else
14032 /* Even though `typename' is not present, the proposed resolution
14033 to Core Issue 180 says that in `class A<T>::B', `B' should be
14034 considered a type-name, even if `A<T>' is dependent. */
14035 cp_parser_nested_name_specifier_opt (parser,
14036 /*typename_keyword_p=*/true,
14037 /*check_dependency_p=*/true,
14038 /*type_p=*/true,
14039 is_declaration);
14040 /* For everything but enumeration types, consider a template-id.
14041 For an enumeration type, consider only a plain identifier. */
14042 if (tag_type != enum_type)
14044 bool template_p = false;
14045 tree decl;
14047 /* Allow the `template' keyword. */
14048 template_p = cp_parser_optional_template_keyword (parser);
14049 /* If we didn't see `template', we don't know if there's a
14050 template-id or not. */
14051 if (!template_p)
14052 cp_parser_parse_tentatively (parser);
14053 /* Parse the template-id. */
14054 token = cp_lexer_peek_token (parser->lexer);
14055 decl = cp_parser_template_id (parser, template_p,
14056 /*check_dependency_p=*/true,
14057 tag_type,
14058 is_declaration);
14059 /* If we didn't find a template-id, look for an ordinary
14060 identifier. */
14061 if (!template_p && !cp_parser_parse_definitely (parser))
14063 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14064 in effect, then we must assume that, upon instantiation, the
14065 template will correspond to a class. */
14066 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14067 && tag_type == typename_type)
14068 type = make_typename_type (parser->scope, decl,
14069 typename_type,
14070 /*complain=*/tf_error);
14071 /* If the `typename' keyword is in effect and DECL is not a type
14072 decl. Then type is non existant. */
14073 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14074 type = NULL_TREE;
14075 else
14076 type = check_elaborated_type_specifier (tag_type, decl,
14077 /*allow_template_p=*/true);
14080 if (!type)
14082 token = cp_lexer_peek_token (parser->lexer);
14083 identifier = cp_parser_identifier (parser);
14085 if (identifier == error_mark_node)
14087 parser->scope = NULL_TREE;
14088 return error_mark_node;
14091 /* For a `typename', we needn't call xref_tag. */
14092 if (tag_type == typename_type
14093 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14094 return cp_parser_make_typename_type (parser, parser->scope,
14095 identifier,
14096 token->location);
14097 /* Look up a qualified name in the usual way. */
14098 if (parser->scope)
14100 tree decl;
14101 tree ambiguous_decls;
14103 decl = cp_parser_lookup_name (parser, identifier,
14104 tag_type,
14105 /*is_template=*/false,
14106 /*is_namespace=*/false,
14107 /*check_dependency=*/true,
14108 &ambiguous_decls,
14109 token->location);
14111 /* If the lookup was ambiguous, an error will already have been
14112 issued. */
14113 if (ambiguous_decls)
14114 return error_mark_node;
14116 /* If we are parsing friend declaration, DECL may be a
14117 TEMPLATE_DECL tree node here. However, we need to check
14118 whether this TEMPLATE_DECL results in valid code. Consider
14119 the following example:
14121 namespace N {
14122 template <class T> class C {};
14124 class X {
14125 template <class T> friend class N::C; // #1, valid code
14127 template <class T> class Y {
14128 friend class N::C; // #2, invalid code
14131 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14132 name lookup of `N::C'. We see that friend declaration must
14133 be template for the code to be valid. Note that
14134 processing_template_decl does not work here since it is
14135 always 1 for the above two cases. */
14137 decl = (cp_parser_maybe_treat_template_as_class
14138 (decl, /*tag_name_p=*/is_friend
14139 && parser->num_template_parameter_lists));
14141 if (TREE_CODE (decl) != TYPE_DECL)
14143 cp_parser_diagnose_invalid_type_name (parser,
14144 parser->scope,
14145 identifier,
14146 token->location);
14147 return error_mark_node;
14150 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14152 bool allow_template = (parser->num_template_parameter_lists
14153 || DECL_SELF_REFERENCE_P (decl));
14154 type = check_elaborated_type_specifier (tag_type, decl,
14155 allow_template);
14157 if (type == error_mark_node)
14158 return error_mark_node;
14161 /* Forward declarations of nested types, such as
14163 class C1::C2;
14164 class C1::C2::C3;
14166 are invalid unless all components preceding the final '::'
14167 are complete. If all enclosing types are complete, these
14168 declarations become merely pointless.
14170 Invalid forward declarations of nested types are errors
14171 caught elsewhere in parsing. Those that are pointless arrive
14172 here. */
14174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14175 && !is_friend && !processing_explicit_instantiation)
14176 warning (0, "declaration %qD does not declare anything", decl);
14178 type = TREE_TYPE (decl);
14180 else
14182 /* An elaborated-type-specifier sometimes introduces a new type and
14183 sometimes names an existing type. Normally, the rule is that it
14184 introduces a new type only if there is not an existing type of
14185 the same name already in scope. For example, given:
14187 struct S {};
14188 void f() { struct S s; }
14190 the `struct S' in the body of `f' is the same `struct S' as in
14191 the global scope; the existing definition is used. However, if
14192 there were no global declaration, this would introduce a new
14193 local class named `S'.
14195 An exception to this rule applies to the following code:
14197 namespace N { struct S; }
14199 Here, the elaborated-type-specifier names a new type
14200 unconditionally; even if there is already an `S' in the
14201 containing scope this declaration names a new type.
14202 This exception only applies if the elaborated-type-specifier
14203 forms the complete declaration:
14205 [class.name]
14207 A declaration consisting solely of `class-key identifier ;' is
14208 either a redeclaration of the name in the current scope or a
14209 forward declaration of the identifier as a class name. It
14210 introduces the name into the current scope.
14212 We are in this situation precisely when the next token is a `;'.
14214 An exception to the exception is that a `friend' declaration does
14215 *not* name a new type; i.e., given:
14217 struct S { friend struct T; };
14219 `T' is not a new type in the scope of `S'.
14221 Also, `new struct S' or `sizeof (struct S)' never results in the
14222 definition of a new type; a new type can only be declared in a
14223 declaration context. */
14225 tag_scope ts;
14226 bool template_p;
14228 if (is_friend)
14229 /* Friends have special name lookup rules. */
14230 ts = ts_within_enclosing_non_class;
14231 else if (is_declaration
14232 && cp_lexer_next_token_is (parser->lexer,
14233 CPP_SEMICOLON))
14234 /* This is a `class-key identifier ;' */
14235 ts = ts_current;
14236 else
14237 ts = ts_global;
14239 template_p =
14240 (parser->num_template_parameter_lists
14241 && (cp_parser_next_token_starts_class_definition_p (parser)
14242 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14243 /* An unqualified name was used to reference this type, so
14244 there were no qualifying templates. */
14245 if (!cp_parser_check_template_parameters (parser,
14246 /*num_templates=*/0,
14247 token->location,
14248 /*declarator=*/NULL))
14249 return error_mark_node;
14250 type = xref_tag (tag_type, identifier, ts, template_p);
14254 if (type == error_mark_node)
14255 return error_mark_node;
14257 /* Allow attributes on forward declarations of classes. */
14258 if (attributes)
14260 if (TREE_CODE (type) == TYPENAME_TYPE)
14261 warning (OPT_Wattributes,
14262 "attributes ignored on uninstantiated type");
14263 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14264 && ! processing_explicit_instantiation)
14265 warning (OPT_Wattributes,
14266 "attributes ignored on template instantiation");
14267 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14268 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14269 else
14270 warning (OPT_Wattributes,
14271 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14274 if (tag_type != enum_type)
14276 /* Indicate whether this class was declared as a `class' or as a
14277 `struct'. */
14278 if (TREE_CODE (type) == RECORD_TYPE)
14279 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14280 cp_parser_check_class_key (tag_type, type);
14283 /* A "<" cannot follow an elaborated type specifier. If that
14284 happens, the user was probably trying to form a template-id. */
14285 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14286 token->location);
14288 return type;
14291 /* Parse an enum-specifier.
14293 enum-specifier:
14294 enum-head { enumerator-list [opt] }
14295 enum-head { enumerator-list , } [C++0x]
14297 enum-head:
14298 enum-key identifier [opt] enum-base [opt]
14299 enum-key nested-name-specifier identifier enum-base [opt]
14301 enum-key:
14302 enum
14303 enum class [C++0x]
14304 enum struct [C++0x]
14306 enum-base: [C++0x]
14307 : type-specifier-seq
14309 opaque-enum-specifier:
14310 enum-key identifier enum-base [opt] ;
14312 GNU Extensions:
14313 enum-key attributes[opt] identifier [opt] enum-base [opt]
14314 { enumerator-list [opt] }attributes[opt]
14315 enum-key attributes[opt] identifier [opt] enum-base [opt]
14316 { enumerator-list, }attributes[opt] [C++0x]
14318 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14319 if the token stream isn't an enum-specifier after all. */
14321 static tree
14322 cp_parser_enum_specifier (cp_parser* parser)
14324 tree identifier;
14325 tree type = NULL_TREE;
14326 tree prev_scope;
14327 tree nested_name_specifier = NULL_TREE;
14328 tree attributes;
14329 bool scoped_enum_p = false;
14330 bool has_underlying_type = false;
14331 bool nested_being_defined = false;
14332 bool new_value_list = false;
14333 bool is_new_type = false;
14334 bool is_anonymous = false;
14335 tree underlying_type = NULL_TREE;
14336 cp_token *type_start_token = NULL;
14337 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14339 parser->colon_corrects_to_scope_p = false;
14341 /* Parse tentatively so that we can back up if we don't find a
14342 enum-specifier. */
14343 cp_parser_parse_tentatively (parser);
14345 /* Caller guarantees that the current token is 'enum', an identifier
14346 possibly follows, and the token after that is an opening brace.
14347 If we don't have an identifier, fabricate an anonymous name for
14348 the enumeration being defined. */
14349 cp_lexer_consume_token (parser->lexer);
14351 /* Parse the "class" or "struct", which indicates a scoped
14352 enumeration type in C++0x. */
14353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14354 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14356 if (cxx_dialect < cxx0x)
14357 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14359 /* Consume the `struct' or `class' token. */
14360 cp_lexer_consume_token (parser->lexer);
14362 scoped_enum_p = true;
14365 attributes = cp_parser_attributes_opt (parser);
14367 /* Clear the qualification. */
14368 parser->scope = NULL_TREE;
14369 parser->qualifying_scope = NULL_TREE;
14370 parser->object_scope = NULL_TREE;
14372 /* Figure out in what scope the declaration is being placed. */
14373 prev_scope = current_scope ();
14375 type_start_token = cp_lexer_peek_token (parser->lexer);
14377 push_deferring_access_checks (dk_no_check);
14378 nested_name_specifier
14379 = cp_parser_nested_name_specifier_opt (parser,
14380 /*typename_keyword_p=*/true,
14381 /*check_dependency_p=*/false,
14382 /*type_p=*/false,
14383 /*is_declaration=*/false);
14385 if (nested_name_specifier)
14387 tree name;
14389 identifier = cp_parser_identifier (parser);
14390 name = cp_parser_lookup_name (parser, identifier,
14391 enum_type,
14392 /*is_template=*/false,
14393 /*is_namespace=*/false,
14394 /*check_dependency=*/true,
14395 /*ambiguous_decls=*/NULL,
14396 input_location);
14397 if (name)
14399 type = TREE_TYPE (name);
14400 if (TREE_CODE (type) == TYPENAME_TYPE)
14402 /* Are template enums allowed in ISO? */
14403 if (template_parm_scope_p ())
14404 pedwarn (type_start_token->location, OPT_Wpedantic,
14405 "%qD is an enumeration template", name);
14406 /* ignore a typename reference, for it will be solved by name
14407 in start_enum. */
14408 type = NULL_TREE;
14411 else
14412 error_at (type_start_token->location,
14413 "%qD is not an enumerator-name", identifier);
14415 else
14417 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14418 identifier = cp_parser_identifier (parser);
14419 else
14421 identifier = make_anon_name ();
14422 is_anonymous = true;
14425 pop_deferring_access_checks ();
14427 /* Check for the `:' that denotes a specified underlying type in C++0x.
14428 Note that a ':' could also indicate a bitfield width, however. */
14429 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14431 cp_decl_specifier_seq type_specifiers;
14433 /* Consume the `:'. */
14434 cp_lexer_consume_token (parser->lexer);
14436 /* Parse the type-specifier-seq. */
14437 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14438 /*is_trailing_return=*/false,
14439 &type_specifiers);
14441 /* At this point this is surely not elaborated type specifier. */
14442 if (!cp_parser_parse_definitely (parser))
14443 return NULL_TREE;
14445 if (cxx_dialect < cxx0x)
14446 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14448 has_underlying_type = true;
14450 /* If that didn't work, stop. */
14451 if (type_specifiers.type != error_mark_node)
14453 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14454 /*initialized=*/0, NULL);
14455 if (underlying_type == error_mark_node)
14456 underlying_type = NULL_TREE;
14460 /* Look for the `{' but don't consume it yet. */
14461 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14463 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14465 cp_parser_error (parser, "expected %<{%>");
14466 if (has_underlying_type)
14468 type = NULL_TREE;
14469 goto out;
14472 /* An opaque-enum-specifier must have a ';' here. */
14473 if ((scoped_enum_p || underlying_type)
14474 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14476 cp_parser_error (parser, "expected %<;%> or %<{%>");
14477 if (has_underlying_type)
14479 type = NULL_TREE;
14480 goto out;
14485 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14486 return NULL_TREE;
14488 if (nested_name_specifier)
14490 if (CLASS_TYPE_P (nested_name_specifier))
14492 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14493 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14494 push_scope (nested_name_specifier);
14496 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14498 push_nested_namespace (nested_name_specifier);
14502 /* Issue an error message if type-definitions are forbidden here. */
14503 if (!cp_parser_check_type_definition (parser))
14504 type = error_mark_node;
14505 else
14506 /* Create the new type. We do this before consuming the opening
14507 brace so the enum will be recorded as being on the line of its
14508 tag (or the 'enum' keyword, if there is no tag). */
14509 type = start_enum (identifier, type, underlying_type,
14510 scoped_enum_p, &is_new_type);
14512 /* If the next token is not '{' it is an opaque-enum-specifier or an
14513 elaborated-type-specifier. */
14514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14516 timevar_push (TV_PARSE_ENUM);
14517 if (nested_name_specifier)
14519 /* The following catches invalid code such as:
14520 enum class S<int>::E { A, B, C }; */
14521 if (!processing_specialization
14522 && CLASS_TYPE_P (nested_name_specifier)
14523 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14524 error_at (type_start_token->location, "cannot add an enumerator "
14525 "list to a template instantiation");
14527 /* If that scope does not contain the scope in which the
14528 class was originally declared, the program is invalid. */
14529 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14531 if (at_namespace_scope_p ())
14532 error_at (type_start_token->location,
14533 "declaration of %qD in namespace %qD which does not "
14534 "enclose %qD",
14535 type, prev_scope, nested_name_specifier);
14536 else
14537 error_at (type_start_token->location,
14538 "declaration of %qD in %qD which does not enclose %qD",
14539 type, prev_scope, nested_name_specifier);
14540 type = error_mark_node;
14544 if (scoped_enum_p)
14545 begin_scope (sk_scoped_enum, type);
14547 /* Consume the opening brace. */
14548 cp_lexer_consume_token (parser->lexer);
14550 if (type == error_mark_node)
14551 ; /* Nothing to add */
14552 else if (OPAQUE_ENUM_P (type)
14553 || (cxx_dialect > cxx98 && processing_specialization))
14555 new_value_list = true;
14556 SET_OPAQUE_ENUM_P (type, false);
14557 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14559 else
14561 error_at (type_start_token->location, "multiple definition of %q#T", type);
14562 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14563 "previous definition here");
14564 type = error_mark_node;
14567 if (type == error_mark_node)
14568 cp_parser_skip_to_end_of_block_or_statement (parser);
14569 /* If the next token is not '}', then there are some enumerators. */
14570 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14571 cp_parser_enumerator_list (parser, type);
14573 /* Consume the final '}'. */
14574 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14576 if (scoped_enum_p)
14577 finish_scope ();
14578 timevar_pop (TV_PARSE_ENUM);
14580 else
14582 /* If a ';' follows, then it is an opaque-enum-specifier
14583 and additional restrictions apply. */
14584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14586 if (is_anonymous)
14587 error_at (type_start_token->location,
14588 "opaque-enum-specifier without name");
14589 else if (nested_name_specifier)
14590 error_at (type_start_token->location,
14591 "opaque-enum-specifier must use a simple identifier");
14595 /* Look for trailing attributes to apply to this enumeration, and
14596 apply them if appropriate. */
14597 if (cp_parser_allow_gnu_extensions_p (parser))
14599 tree trailing_attr = cp_parser_attributes_opt (parser);
14600 trailing_attr = chainon (trailing_attr, attributes);
14601 cplus_decl_attributes (&type,
14602 trailing_attr,
14603 (int) ATTR_FLAG_TYPE_IN_PLACE);
14606 /* Finish up the enumeration. */
14607 if (type != error_mark_node)
14609 if (new_value_list)
14610 finish_enum_value_list (type);
14611 if (is_new_type)
14612 finish_enum (type);
14615 if (nested_name_specifier)
14617 if (CLASS_TYPE_P (nested_name_specifier))
14619 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14620 pop_scope (nested_name_specifier);
14622 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14624 pop_nested_namespace (nested_name_specifier);
14627 out:
14628 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14629 return type;
14632 /* Parse an enumerator-list. The enumerators all have the indicated
14633 TYPE.
14635 enumerator-list:
14636 enumerator-definition
14637 enumerator-list , enumerator-definition */
14639 static void
14640 cp_parser_enumerator_list (cp_parser* parser, tree type)
14642 while (true)
14644 /* Parse an enumerator-definition. */
14645 cp_parser_enumerator_definition (parser, type);
14647 /* If the next token is not a ',', we've reached the end of
14648 the list. */
14649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14650 break;
14651 /* Otherwise, consume the `,' and keep going. */
14652 cp_lexer_consume_token (parser->lexer);
14653 /* If the next token is a `}', there is a trailing comma. */
14654 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14656 if (cxx_dialect < cxx0x && !in_system_header)
14657 pedwarn (input_location, OPT_Wpedantic,
14658 "comma at end of enumerator list");
14659 break;
14664 /* Parse an enumerator-definition. The enumerator has the indicated
14665 TYPE.
14667 enumerator-definition:
14668 enumerator
14669 enumerator = constant-expression
14671 enumerator:
14672 identifier */
14674 static void
14675 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14677 tree identifier;
14678 tree value;
14679 location_t loc;
14681 /* Save the input location because we are interested in the location
14682 of the identifier and not the location of the explicit value. */
14683 loc = cp_lexer_peek_token (parser->lexer)->location;
14685 /* Look for the identifier. */
14686 identifier = cp_parser_identifier (parser);
14687 if (identifier == error_mark_node)
14688 return;
14690 /* If the next token is an '=', then there is an explicit value. */
14691 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14693 /* Consume the `=' token. */
14694 cp_lexer_consume_token (parser->lexer);
14695 /* Parse the value. */
14696 value = cp_parser_constant_expression (parser,
14697 /*allow_non_constant_p=*/false,
14698 NULL);
14700 else
14701 value = NULL_TREE;
14703 /* If we are processing a template, make sure the initializer of the
14704 enumerator doesn't contain any bare template parameter pack. */
14705 if (check_for_bare_parameter_packs (value))
14706 value = error_mark_node;
14708 /* integral_constant_value will pull out this expression, so make sure
14709 it's folded as appropriate. */
14710 value = fold_non_dependent_expr (value);
14712 /* Create the enumerator. */
14713 build_enumerator (identifier, value, type, loc);
14716 /* Parse a namespace-name.
14718 namespace-name:
14719 original-namespace-name
14720 namespace-alias
14722 Returns the NAMESPACE_DECL for the namespace. */
14724 static tree
14725 cp_parser_namespace_name (cp_parser* parser)
14727 tree identifier;
14728 tree namespace_decl;
14730 cp_token *token = cp_lexer_peek_token (parser->lexer);
14732 /* Get the name of the namespace. */
14733 identifier = cp_parser_identifier (parser);
14734 if (identifier == error_mark_node)
14735 return error_mark_node;
14737 /* Look up the identifier in the currently active scope. Look only
14738 for namespaces, due to:
14740 [basic.lookup.udir]
14742 When looking up a namespace-name in a using-directive or alias
14743 definition, only namespace names are considered.
14745 And:
14747 [basic.lookup.qual]
14749 During the lookup of a name preceding the :: scope resolution
14750 operator, object, function, and enumerator names are ignored.
14752 (Note that cp_parser_qualifying_entity only calls this
14753 function if the token after the name is the scope resolution
14754 operator.) */
14755 namespace_decl = cp_parser_lookup_name (parser, identifier,
14756 none_type,
14757 /*is_template=*/false,
14758 /*is_namespace=*/true,
14759 /*check_dependency=*/true,
14760 /*ambiguous_decls=*/NULL,
14761 token->location);
14762 /* If it's not a namespace, issue an error. */
14763 if (namespace_decl == error_mark_node
14764 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14767 error_at (token->location, "%qD is not a namespace-name", identifier);
14768 cp_parser_error (parser, "expected namespace-name");
14769 namespace_decl = error_mark_node;
14772 return namespace_decl;
14775 /* Parse a namespace-definition.
14777 namespace-definition:
14778 named-namespace-definition
14779 unnamed-namespace-definition
14781 named-namespace-definition:
14782 original-namespace-definition
14783 extension-namespace-definition
14785 original-namespace-definition:
14786 namespace identifier { namespace-body }
14788 extension-namespace-definition:
14789 namespace original-namespace-name { namespace-body }
14791 unnamed-namespace-definition:
14792 namespace { namespace-body } */
14794 static void
14795 cp_parser_namespace_definition (cp_parser* parser)
14797 tree identifier, attribs;
14798 bool has_visibility;
14799 bool is_inline;
14801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14803 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14804 is_inline = true;
14805 cp_lexer_consume_token (parser->lexer);
14807 else
14808 is_inline = false;
14810 /* Look for the `namespace' keyword. */
14811 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14813 /* Get the name of the namespace. We do not attempt to distinguish
14814 between an original-namespace-definition and an
14815 extension-namespace-definition at this point. The semantic
14816 analysis routines are responsible for that. */
14817 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14818 identifier = cp_parser_identifier (parser);
14819 else
14820 identifier = NULL_TREE;
14822 /* Parse any specified attributes. */
14823 attribs = cp_parser_attributes_opt (parser);
14825 /* Look for the `{' to start the namespace. */
14826 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14827 /* Start the namespace. */
14828 push_namespace (identifier);
14830 /* "inline namespace" is equivalent to a stub namespace definition
14831 followed by a strong using directive. */
14832 if (is_inline)
14834 tree name_space = current_namespace;
14835 /* Set up namespace association. */
14836 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14837 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14838 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14839 /* Import the contents of the inline namespace. */
14840 pop_namespace ();
14841 do_using_directive (name_space);
14842 push_namespace (identifier);
14845 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14847 /* Parse the body of the namespace. */
14848 cp_parser_namespace_body (parser);
14850 if (has_visibility)
14851 pop_visibility (1);
14853 /* Finish the namespace. */
14854 pop_namespace ();
14855 /* Look for the final `}'. */
14856 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14859 /* Parse a namespace-body.
14861 namespace-body:
14862 declaration-seq [opt] */
14864 static void
14865 cp_parser_namespace_body (cp_parser* parser)
14867 cp_parser_declaration_seq_opt (parser);
14870 /* Parse a namespace-alias-definition.
14872 namespace-alias-definition:
14873 namespace identifier = qualified-namespace-specifier ; */
14875 static void
14876 cp_parser_namespace_alias_definition (cp_parser* parser)
14878 tree identifier;
14879 tree namespace_specifier;
14881 cp_token *token = cp_lexer_peek_token (parser->lexer);
14883 /* Look for the `namespace' keyword. */
14884 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14885 /* Look for the identifier. */
14886 identifier = cp_parser_identifier (parser);
14887 if (identifier == error_mark_node)
14888 return;
14889 /* Look for the `=' token. */
14890 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14891 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14893 error_at (token->location, "%<namespace%> definition is not allowed here");
14894 /* Skip the definition. */
14895 cp_lexer_consume_token (parser->lexer);
14896 if (cp_parser_skip_to_closing_brace (parser))
14897 cp_lexer_consume_token (parser->lexer);
14898 return;
14900 cp_parser_require (parser, CPP_EQ, RT_EQ);
14901 /* Look for the qualified-namespace-specifier. */
14902 namespace_specifier
14903 = cp_parser_qualified_namespace_specifier (parser);
14904 /* Look for the `;' token. */
14905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14907 /* Register the alias in the symbol table. */
14908 do_namespace_alias (identifier, namespace_specifier);
14911 /* Parse a qualified-namespace-specifier.
14913 qualified-namespace-specifier:
14914 :: [opt] nested-name-specifier [opt] namespace-name
14916 Returns a NAMESPACE_DECL corresponding to the specified
14917 namespace. */
14919 static tree
14920 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14922 /* Look for the optional `::'. */
14923 cp_parser_global_scope_opt (parser,
14924 /*current_scope_valid_p=*/false);
14926 /* Look for the optional nested-name-specifier. */
14927 cp_parser_nested_name_specifier_opt (parser,
14928 /*typename_keyword_p=*/false,
14929 /*check_dependency_p=*/true,
14930 /*type_p=*/false,
14931 /*is_declaration=*/true);
14933 return cp_parser_namespace_name (parser);
14936 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14937 access declaration.
14939 using-declaration:
14940 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14941 using :: unqualified-id ;
14943 access-declaration:
14944 qualified-id ;
14948 static bool
14949 cp_parser_using_declaration (cp_parser* parser,
14950 bool access_declaration_p)
14952 cp_token *token;
14953 bool typename_p = false;
14954 bool global_scope_p;
14955 tree decl;
14956 tree identifier;
14957 tree qscope;
14958 int oldcount = errorcount;
14959 cp_token *diag_token = NULL;
14961 if (access_declaration_p)
14963 diag_token = cp_lexer_peek_token (parser->lexer);
14964 cp_parser_parse_tentatively (parser);
14966 else
14968 /* Look for the `using' keyword. */
14969 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14971 /* Peek at the next token. */
14972 token = cp_lexer_peek_token (parser->lexer);
14973 /* See if it's `typename'. */
14974 if (token->keyword == RID_TYPENAME)
14976 /* Remember that we've seen it. */
14977 typename_p = true;
14978 /* Consume the `typename' token. */
14979 cp_lexer_consume_token (parser->lexer);
14983 /* Look for the optional global scope qualification. */
14984 global_scope_p
14985 = (cp_parser_global_scope_opt (parser,
14986 /*current_scope_valid_p=*/false)
14987 != NULL_TREE);
14989 /* If we saw `typename', or didn't see `::', then there must be a
14990 nested-name-specifier present. */
14991 if (typename_p || !global_scope_p)
14992 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14993 /*check_dependency_p=*/true,
14994 /*type_p=*/false,
14995 /*is_declaration=*/true);
14996 /* Otherwise, we could be in either of the two productions. In that
14997 case, treat the nested-name-specifier as optional. */
14998 else
14999 qscope = cp_parser_nested_name_specifier_opt (parser,
15000 /*typename_keyword_p=*/false,
15001 /*check_dependency_p=*/true,
15002 /*type_p=*/false,
15003 /*is_declaration=*/true);
15004 if (!qscope)
15005 qscope = global_namespace;
15007 if (access_declaration_p && cp_parser_error_occurred (parser))
15008 /* Something has already gone wrong; there's no need to parse
15009 further. Since an error has occurred, the return value of
15010 cp_parser_parse_definitely will be false, as required. */
15011 return cp_parser_parse_definitely (parser);
15013 token = cp_lexer_peek_token (parser->lexer);
15014 /* Parse the unqualified-id. */
15015 identifier = cp_parser_unqualified_id (parser,
15016 /*template_keyword_p=*/false,
15017 /*check_dependency_p=*/true,
15018 /*declarator_p=*/true,
15019 /*optional_p=*/false);
15021 if (access_declaration_p)
15023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15024 cp_parser_simulate_error (parser);
15025 if (!cp_parser_parse_definitely (parser))
15026 return false;
15029 /* The function we call to handle a using-declaration is different
15030 depending on what scope we are in. */
15031 if (qscope == error_mark_node || identifier == error_mark_node)
15033 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15034 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15035 /* [namespace.udecl]
15037 A using declaration shall not name a template-id. */
15038 error_at (token->location,
15039 "a template-id may not appear in a using-declaration");
15040 else
15042 if (at_class_scope_p ())
15044 /* Create the USING_DECL. */
15045 decl = do_class_using_decl (parser->scope, identifier);
15047 if (decl && typename_p)
15048 USING_DECL_TYPENAME_P (decl) = 1;
15050 if (check_for_bare_parameter_packs (decl))
15051 return false;
15052 else
15053 /* Add it to the list of members in this class. */
15054 finish_member_declaration (decl);
15056 else
15058 decl = cp_parser_lookup_name_simple (parser,
15059 identifier,
15060 token->location);
15061 if (decl == error_mark_node)
15062 cp_parser_name_lookup_error (parser, identifier,
15063 decl, NLE_NULL,
15064 token->location);
15065 else if (check_for_bare_parameter_packs (decl))
15066 return false;
15067 else if (!at_namespace_scope_p ())
15068 do_local_using_decl (decl, qscope, identifier);
15069 else
15070 do_toplevel_using_decl (decl, qscope, identifier);
15074 /* Look for the final `;'. */
15075 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15077 if (access_declaration_p && errorcount == oldcount)
15078 warning_at (diag_token->location, OPT_Wdeprecated,
15079 "access declarations are deprecated "
15080 "in favour of using-declarations; "
15081 "suggestion: add the %<using%> keyword");
15083 return true;
15086 /* Parse an alias-declaration.
15088 alias-declaration:
15089 using identifier attribute-specifier-seq [opt] = type-id */
15091 static tree
15092 cp_parser_alias_declaration (cp_parser* parser)
15094 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15095 location_t id_location, using_location, attrs_location = 0;
15096 cp_declarator *declarator;
15097 cp_decl_specifier_seq decl_specs;
15098 bool member_p;
15099 const char *saved_message = NULL;
15101 /* Look for the `using' keyword. */
15102 using_location = cp_lexer_peek_token (parser->lexer)->location;
15103 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15104 id_location = cp_lexer_peek_token (parser->lexer)->location;
15105 id = cp_parser_identifier (parser);
15106 if (id == error_mark_node)
15107 return error_mark_node;
15109 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15110 attributes = cp_parser_attributes_opt (parser);
15111 if (attributes == error_mark_node)
15112 return error_mark_node;
15114 cp_parser_require (parser, CPP_EQ, RT_EQ);
15116 if (cp_parser_error_occurred (parser))
15117 return error_mark_node;
15119 /* Now we are going to parse the type-id of the declaration. */
15122 [dcl.type]/3 says:
15124 "A type-specifier-seq shall not define a class or enumeration
15125 unless it appears in the type-id of an alias-declaration (7.1.3) that
15126 is not the declaration of a template-declaration."
15128 In other words, if we currently are in an alias template, the
15129 type-id should not define a type.
15131 So let's set parser->type_definition_forbidden_message in that
15132 case; cp_parser_check_type_definition (called by
15133 cp_parser_class_specifier) will then emit an error if a type is
15134 defined in the type-id. */
15135 if (parser->num_template_parameter_lists)
15137 saved_message = parser->type_definition_forbidden_message;
15138 parser->type_definition_forbidden_message =
15139 G_("types may not be defined in alias template declarations");
15142 type = cp_parser_type_id (parser);
15144 /* Restore the error message if need be. */
15145 if (parser->num_template_parameter_lists)
15146 parser->type_definition_forbidden_message = saved_message;
15148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15150 if (cp_parser_error_occurred (parser))
15151 return error_mark_node;
15153 /* A typedef-name can also be introduced by an alias-declaration. The
15154 identifier following the using keyword becomes a typedef-name. It has
15155 the same semantics as if it were introduced by the typedef
15156 specifier. In particular, it does not define a new type and it shall
15157 not appear in the type-id. */
15159 clear_decl_specs (&decl_specs);
15160 decl_specs.type = type;
15161 if (attributes != NULL_TREE)
15163 decl_specs.attributes = attributes;
15164 set_and_check_decl_spec_loc (&decl_specs,
15165 ds_attribute,
15166 attrs_location);
15168 set_and_check_decl_spec_loc (&decl_specs,
15169 ds_typedef,
15170 using_location);
15171 set_and_check_decl_spec_loc (&decl_specs,
15172 ds_alias,
15173 using_location);
15175 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15176 declarator->id_loc = id_location;
15178 member_p = at_class_scope_p ();
15179 if (member_p)
15180 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15181 NULL_TREE, attributes);
15182 else
15183 decl = start_decl (declarator, &decl_specs, 0,
15184 attributes, NULL_TREE, &pushed_scope);
15185 if (decl == error_mark_node)
15186 return decl;
15188 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15190 if (pushed_scope)
15191 pop_scope (pushed_scope);
15193 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15194 added into the symbol table; otherwise, return the TYPE_DECL. */
15195 if (DECL_LANG_SPECIFIC (decl)
15196 && DECL_TEMPLATE_INFO (decl)
15197 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15199 decl = DECL_TI_TEMPLATE (decl);
15200 if (member_p)
15201 check_member_template (decl);
15204 return decl;
15207 /* Parse a using-directive.
15209 using-directive:
15210 using namespace :: [opt] nested-name-specifier [opt]
15211 namespace-name ; */
15213 static void
15214 cp_parser_using_directive (cp_parser* parser)
15216 tree namespace_decl;
15217 tree attribs;
15219 /* Look for the `using' keyword. */
15220 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15221 /* And the `namespace' keyword. */
15222 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15223 /* Look for the optional `::' operator. */
15224 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15225 /* And the optional nested-name-specifier. */
15226 cp_parser_nested_name_specifier_opt (parser,
15227 /*typename_keyword_p=*/false,
15228 /*check_dependency_p=*/true,
15229 /*type_p=*/false,
15230 /*is_declaration=*/true);
15231 /* Get the namespace being used. */
15232 namespace_decl = cp_parser_namespace_name (parser);
15233 /* And any specified attributes. */
15234 attribs = cp_parser_attributes_opt (parser);
15235 /* Update the symbol table. */
15236 parse_using_directive (namespace_decl, attribs);
15237 /* Look for the final `;'. */
15238 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15241 /* Parse an asm-definition.
15243 asm-definition:
15244 asm ( string-literal ) ;
15246 GNU Extension:
15248 asm-definition:
15249 asm volatile [opt] ( string-literal ) ;
15250 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15251 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15252 : asm-operand-list [opt] ) ;
15253 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15254 : asm-operand-list [opt]
15255 : asm-clobber-list [opt] ) ;
15256 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15257 : asm-clobber-list [opt]
15258 : asm-goto-list ) ; */
15260 static void
15261 cp_parser_asm_definition (cp_parser* parser)
15263 tree string;
15264 tree outputs = NULL_TREE;
15265 tree inputs = NULL_TREE;
15266 tree clobbers = NULL_TREE;
15267 tree labels = NULL_TREE;
15268 tree asm_stmt;
15269 bool volatile_p = false;
15270 bool extended_p = false;
15271 bool invalid_inputs_p = false;
15272 bool invalid_outputs_p = false;
15273 bool goto_p = false;
15274 required_token missing = RT_NONE;
15276 /* Look for the `asm' keyword. */
15277 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15278 /* See if the next token is `volatile'. */
15279 if (cp_parser_allow_gnu_extensions_p (parser)
15280 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15282 /* Remember that we saw the `volatile' keyword. */
15283 volatile_p = true;
15284 /* Consume the token. */
15285 cp_lexer_consume_token (parser->lexer);
15287 if (cp_parser_allow_gnu_extensions_p (parser)
15288 && parser->in_function_body
15289 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15291 /* Remember that we saw the `goto' keyword. */
15292 goto_p = true;
15293 /* Consume the token. */
15294 cp_lexer_consume_token (parser->lexer);
15296 /* Look for the opening `('. */
15297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15298 return;
15299 /* Look for the string. */
15300 string = cp_parser_string_literal (parser, false, false);
15301 if (string == error_mark_node)
15303 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15304 /*consume_paren=*/true);
15305 return;
15308 /* If we're allowing GNU extensions, check for the extended assembly
15309 syntax. Unfortunately, the `:' tokens need not be separated by
15310 a space in C, and so, for compatibility, we tolerate that here
15311 too. Doing that means that we have to treat the `::' operator as
15312 two `:' tokens. */
15313 if (cp_parser_allow_gnu_extensions_p (parser)
15314 && parser->in_function_body
15315 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15316 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15318 bool inputs_p = false;
15319 bool clobbers_p = false;
15320 bool labels_p = false;
15322 /* The extended syntax was used. */
15323 extended_p = true;
15325 /* Look for outputs. */
15326 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15328 /* Consume the `:'. */
15329 cp_lexer_consume_token (parser->lexer);
15330 /* Parse the output-operands. */
15331 if (cp_lexer_next_token_is_not (parser->lexer,
15332 CPP_COLON)
15333 && cp_lexer_next_token_is_not (parser->lexer,
15334 CPP_SCOPE)
15335 && cp_lexer_next_token_is_not (parser->lexer,
15336 CPP_CLOSE_PAREN)
15337 && !goto_p)
15338 outputs = cp_parser_asm_operand_list (parser);
15340 if (outputs == error_mark_node)
15341 invalid_outputs_p = true;
15343 /* If the next token is `::', there are no outputs, and the
15344 next token is the beginning of the inputs. */
15345 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15346 /* The inputs are coming next. */
15347 inputs_p = true;
15349 /* Look for inputs. */
15350 if (inputs_p
15351 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15353 /* Consume the `:' or `::'. */
15354 cp_lexer_consume_token (parser->lexer);
15355 /* Parse the output-operands. */
15356 if (cp_lexer_next_token_is_not (parser->lexer,
15357 CPP_COLON)
15358 && cp_lexer_next_token_is_not (parser->lexer,
15359 CPP_SCOPE)
15360 && cp_lexer_next_token_is_not (parser->lexer,
15361 CPP_CLOSE_PAREN))
15362 inputs = cp_parser_asm_operand_list (parser);
15364 if (inputs == error_mark_node)
15365 invalid_inputs_p = true;
15367 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15368 /* The clobbers are coming next. */
15369 clobbers_p = true;
15371 /* Look for clobbers. */
15372 if (clobbers_p
15373 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15375 clobbers_p = true;
15376 /* Consume the `:' or `::'. */
15377 cp_lexer_consume_token (parser->lexer);
15378 /* Parse the clobbers. */
15379 if (cp_lexer_next_token_is_not (parser->lexer,
15380 CPP_COLON)
15381 && cp_lexer_next_token_is_not (parser->lexer,
15382 CPP_CLOSE_PAREN))
15383 clobbers = cp_parser_asm_clobber_list (parser);
15385 else if (goto_p
15386 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15387 /* The labels are coming next. */
15388 labels_p = true;
15390 /* Look for labels. */
15391 if (labels_p
15392 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15394 labels_p = true;
15395 /* Consume the `:' or `::'. */
15396 cp_lexer_consume_token (parser->lexer);
15397 /* Parse the labels. */
15398 labels = cp_parser_asm_label_list (parser);
15401 if (goto_p && !labels_p)
15402 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15404 else if (goto_p)
15405 missing = RT_COLON_SCOPE;
15407 /* Look for the closing `)'. */
15408 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15409 missing ? missing : RT_CLOSE_PAREN))
15410 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15411 /*consume_paren=*/true);
15412 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15414 if (!invalid_inputs_p && !invalid_outputs_p)
15416 /* Create the ASM_EXPR. */
15417 if (parser->in_function_body)
15419 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15420 inputs, clobbers, labels);
15421 /* If the extended syntax was not used, mark the ASM_EXPR. */
15422 if (!extended_p)
15424 tree temp = asm_stmt;
15425 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15426 temp = TREE_OPERAND (temp, 0);
15428 ASM_INPUT_P (temp) = 1;
15431 else
15432 add_asm_node (string);
15436 /* Declarators [gram.dcl.decl] */
15438 /* Parse an init-declarator.
15440 init-declarator:
15441 declarator initializer [opt]
15443 GNU Extension:
15445 init-declarator:
15446 declarator asm-specification [opt] attributes [opt] initializer [opt]
15448 function-definition:
15449 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15450 function-body
15451 decl-specifier-seq [opt] declarator function-try-block
15453 GNU Extension:
15455 function-definition:
15456 __extension__ function-definition
15458 TM Extension:
15460 function-definition:
15461 decl-specifier-seq [opt] declarator function-transaction-block
15463 The DECL_SPECIFIERS apply to this declarator. Returns a
15464 representation of the entity declared. If MEMBER_P is TRUE, then
15465 this declarator appears in a class scope. The new DECL created by
15466 this declarator is returned.
15468 The CHECKS are access checks that should be performed once we know
15469 what entity is being declared (and, therefore, what classes have
15470 befriended it).
15472 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15473 for a function-definition here as well. If the declarator is a
15474 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15475 be TRUE upon return. By that point, the function-definition will
15476 have been completely parsed.
15478 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15479 is FALSE.
15481 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15482 parsed declaration if it is an uninitialized single declarator not followed
15483 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15484 if present, will not be consumed. If returned, this declarator will be
15485 created with SD_INITIALIZED but will not call cp_finish_decl. */
15487 static tree
15488 cp_parser_init_declarator (cp_parser* parser,
15489 cp_decl_specifier_seq *decl_specifiers,
15490 VEC (deferred_access_check,gc)* checks,
15491 bool function_definition_allowed_p,
15492 bool member_p,
15493 int declares_class_or_enum,
15494 bool* function_definition_p,
15495 tree* maybe_range_for_decl)
15497 cp_token *token = NULL, *asm_spec_start_token = NULL,
15498 *attributes_start_token = NULL;
15499 cp_declarator *declarator;
15500 tree prefix_attributes;
15501 tree attributes;
15502 tree asm_specification;
15503 tree initializer;
15504 tree decl = NULL_TREE;
15505 tree scope;
15506 int is_initialized;
15507 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15508 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15509 "(...)". */
15510 enum cpp_ttype initialization_kind;
15511 bool is_direct_init = false;
15512 bool is_non_constant_init;
15513 int ctor_dtor_or_conv_p;
15514 bool friend_p;
15515 tree pushed_scope = NULL_TREE;
15516 bool range_for_decl_p = false;
15518 /* Gather the attributes that were provided with the
15519 decl-specifiers. */
15520 prefix_attributes = decl_specifiers->attributes;
15522 /* Assume that this is not the declarator for a function
15523 definition. */
15524 if (function_definition_p)
15525 *function_definition_p = false;
15527 /* Defer access checks while parsing the declarator; we cannot know
15528 what names are accessible until we know what is being
15529 declared. */
15530 resume_deferring_access_checks ();
15532 /* Parse the declarator. */
15533 token = cp_lexer_peek_token (parser->lexer);
15534 declarator
15535 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15536 &ctor_dtor_or_conv_p,
15537 /*parenthesized_p=*/NULL,
15538 member_p);
15539 /* Gather up the deferred checks. */
15540 stop_deferring_access_checks ();
15542 /* If the DECLARATOR was erroneous, there's no need to go
15543 further. */
15544 if (declarator == cp_error_declarator)
15545 return error_mark_node;
15547 /* Check that the number of template-parameter-lists is OK. */
15548 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15549 token->location))
15550 return error_mark_node;
15552 if (declares_class_or_enum & 2)
15553 cp_parser_check_for_definition_in_return_type (declarator,
15554 decl_specifiers->type,
15555 decl_specifiers->locations[ds_type_spec]);
15557 /* Figure out what scope the entity declared by the DECLARATOR is
15558 located in. `grokdeclarator' sometimes changes the scope, so
15559 we compute it now. */
15560 scope = get_scope_of_declarator (declarator);
15562 /* Perform any lookups in the declared type which were thought to be
15563 dependent, but are not in the scope of the declarator. */
15564 decl_specifiers->type
15565 = maybe_update_decl_type (decl_specifiers->type, scope);
15567 /* If we're allowing GNU extensions, look for an asm-specification
15568 and attributes. */
15569 if (cp_parser_allow_gnu_extensions_p (parser))
15571 /* Look for an asm-specification. */
15572 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15573 asm_specification = cp_parser_asm_specification_opt (parser);
15574 /* And attributes. */
15575 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15576 attributes = cp_parser_attributes_opt (parser);
15578 else
15580 asm_specification = NULL_TREE;
15581 attributes = NULL_TREE;
15584 /* Peek at the next token. */
15585 token = cp_lexer_peek_token (parser->lexer);
15586 /* Check to see if the token indicates the start of a
15587 function-definition. */
15588 if (function_declarator_p (declarator)
15589 && cp_parser_token_starts_function_definition_p (token))
15591 if (!function_definition_allowed_p)
15593 /* If a function-definition should not appear here, issue an
15594 error message. */
15595 cp_parser_error (parser,
15596 "a function-definition is not allowed here");
15597 return error_mark_node;
15599 else
15601 location_t func_brace_location
15602 = cp_lexer_peek_token (parser->lexer)->location;
15604 /* Neither attributes nor an asm-specification are allowed
15605 on a function-definition. */
15606 if (asm_specification)
15607 error_at (asm_spec_start_token->location,
15608 "an asm-specification is not allowed "
15609 "on a function-definition");
15610 if (attributes)
15611 error_at (attributes_start_token->location,
15612 "attributes are not allowed on a function-definition");
15613 /* This is a function-definition. */
15614 *function_definition_p = true;
15616 /* Parse the function definition. */
15617 if (member_p)
15618 decl = cp_parser_save_member_function_body (parser,
15619 decl_specifiers,
15620 declarator,
15621 prefix_attributes);
15622 else
15623 decl
15624 = (cp_parser_function_definition_from_specifiers_and_declarator
15625 (parser, decl_specifiers, prefix_attributes, declarator));
15627 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15629 /* This is where the prologue starts... */
15630 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15631 = func_brace_location;
15634 return decl;
15638 /* [dcl.dcl]
15640 Only in function declarations for constructors, destructors, and
15641 type conversions can the decl-specifier-seq be omitted.
15643 We explicitly postpone this check past the point where we handle
15644 function-definitions because we tolerate function-definitions
15645 that are missing their return types in some modes. */
15646 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15648 cp_parser_error (parser,
15649 "expected constructor, destructor, or type conversion");
15650 return error_mark_node;
15653 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15654 if (token->type == CPP_EQ
15655 || token->type == CPP_OPEN_PAREN
15656 || token->type == CPP_OPEN_BRACE)
15658 is_initialized = SD_INITIALIZED;
15659 initialization_kind = token->type;
15660 if (maybe_range_for_decl)
15661 *maybe_range_for_decl = error_mark_node;
15663 if (token->type == CPP_EQ
15664 && function_declarator_p (declarator))
15666 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15667 if (t2->keyword == RID_DEFAULT)
15668 is_initialized = SD_DEFAULTED;
15669 else if (t2->keyword == RID_DELETE)
15670 is_initialized = SD_DELETED;
15673 else
15675 /* If the init-declarator isn't initialized and isn't followed by a
15676 `,' or `;', it's not a valid init-declarator. */
15677 if (token->type != CPP_COMMA
15678 && token->type != CPP_SEMICOLON)
15680 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15681 range_for_decl_p = true;
15682 else
15684 cp_parser_error (parser, "expected initializer");
15685 return error_mark_node;
15688 is_initialized = SD_UNINITIALIZED;
15689 initialization_kind = CPP_EOF;
15692 /* Because start_decl has side-effects, we should only call it if we
15693 know we're going ahead. By this point, we know that we cannot
15694 possibly be looking at any other construct. */
15695 cp_parser_commit_to_tentative_parse (parser);
15697 /* If the decl specifiers were bad, issue an error now that we're
15698 sure this was intended to be a declarator. Then continue
15699 declaring the variable(s), as int, to try to cut down on further
15700 errors. */
15701 if (decl_specifiers->any_specifiers_p
15702 && decl_specifiers->type == error_mark_node)
15704 cp_parser_error (parser, "invalid type in declaration");
15705 decl_specifiers->type = integer_type_node;
15708 /* Check to see whether or not this declaration is a friend. */
15709 friend_p = cp_parser_friend_p (decl_specifiers);
15711 /* Enter the newly declared entry in the symbol table. If we're
15712 processing a declaration in a class-specifier, we wait until
15713 after processing the initializer. */
15714 if (!member_p)
15716 if (parser->in_unbraced_linkage_specification_p)
15717 decl_specifiers->storage_class = sc_extern;
15718 decl = start_decl (declarator, decl_specifiers,
15719 range_for_decl_p? SD_INITIALIZED : is_initialized,
15720 attributes, prefix_attributes,
15721 &pushed_scope);
15722 /* Adjust location of decl if declarator->id_loc is more appropriate:
15723 set, and decl wasn't merged with another decl, in which case its
15724 location would be different from input_location, and more accurate. */
15725 if (DECL_P (decl)
15726 && declarator->id_loc != UNKNOWN_LOCATION
15727 && DECL_SOURCE_LOCATION (decl) == input_location)
15728 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15730 else if (scope)
15731 /* Enter the SCOPE. That way unqualified names appearing in the
15732 initializer will be looked up in SCOPE. */
15733 pushed_scope = push_scope (scope);
15735 /* Perform deferred access control checks, now that we know in which
15736 SCOPE the declared entity resides. */
15737 if (!member_p && decl)
15739 tree saved_current_function_decl = NULL_TREE;
15741 /* If the entity being declared is a function, pretend that we
15742 are in its scope. If it is a `friend', it may have access to
15743 things that would not otherwise be accessible. */
15744 if (TREE_CODE (decl) == FUNCTION_DECL)
15746 saved_current_function_decl = current_function_decl;
15747 current_function_decl = decl;
15750 /* Perform access checks for template parameters. */
15751 cp_parser_perform_template_parameter_access_checks (checks);
15753 /* Perform the access control checks for the declarator and the
15754 decl-specifiers. */
15755 perform_deferred_access_checks (tf_warning_or_error);
15757 /* Restore the saved value. */
15758 if (TREE_CODE (decl) == FUNCTION_DECL)
15759 current_function_decl = saved_current_function_decl;
15762 /* Parse the initializer. */
15763 initializer = NULL_TREE;
15764 is_direct_init = false;
15765 is_non_constant_init = true;
15766 if (is_initialized)
15768 if (function_declarator_p (declarator))
15770 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15771 if (initialization_kind == CPP_EQ)
15772 initializer = cp_parser_pure_specifier (parser);
15773 else
15775 /* If the declaration was erroneous, we don't really
15776 know what the user intended, so just silently
15777 consume the initializer. */
15778 if (decl != error_mark_node)
15779 error_at (initializer_start_token->location,
15780 "initializer provided for function");
15781 cp_parser_skip_to_closing_parenthesis (parser,
15782 /*recovering=*/true,
15783 /*or_comma=*/false,
15784 /*consume_paren=*/true);
15787 else
15789 /* We want to record the extra mangling scope for in-class
15790 initializers of class members and initializers of static data
15791 member templates. The former involves deferring
15792 parsing of the initializer until end of class as with default
15793 arguments. So right here we only handle the latter. */
15794 if (!member_p && processing_template_decl)
15795 start_lambda_scope (decl);
15796 initializer = cp_parser_initializer (parser,
15797 &is_direct_init,
15798 &is_non_constant_init);
15799 if (!member_p && processing_template_decl)
15800 finish_lambda_scope ();
15801 if (initializer == error_mark_node)
15802 cp_parser_skip_to_end_of_statement (parser);
15806 /* The old parser allows attributes to appear after a parenthesized
15807 initializer. Mark Mitchell proposed removing this functionality
15808 on the GCC mailing lists on 2002-08-13. This parser accepts the
15809 attributes -- but ignores them. */
15810 if (cp_parser_allow_gnu_extensions_p (parser)
15811 && initialization_kind == CPP_OPEN_PAREN)
15812 if (cp_parser_attributes_opt (parser))
15813 warning (OPT_Wattributes,
15814 "attributes after parenthesized initializer ignored");
15816 /* For an in-class declaration, use `grokfield' to create the
15817 declaration. */
15818 if (member_p)
15820 if (pushed_scope)
15822 pop_scope (pushed_scope);
15823 pushed_scope = NULL_TREE;
15825 decl = grokfield (declarator, decl_specifiers,
15826 initializer, !is_non_constant_init,
15827 /*asmspec=*/NULL_TREE,
15828 prefix_attributes);
15829 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15830 cp_parser_save_default_args (parser, decl);
15833 /* Finish processing the declaration. But, skip member
15834 declarations. */
15835 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15837 cp_finish_decl (decl,
15838 initializer, !is_non_constant_init,
15839 asm_specification,
15840 /* If the initializer is in parentheses, then this is
15841 a direct-initialization, which means that an
15842 `explicit' constructor is OK. Otherwise, an
15843 `explicit' constructor cannot be used. */
15844 ((is_direct_init || !is_initialized)
15845 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15847 else if ((cxx_dialect != cxx98) && friend_p
15848 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15849 /* Core issue #226 (C++0x only): A default template-argument
15850 shall not be specified in a friend class template
15851 declaration. */
15852 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
15853 /*is_partial=*/false, /*is_friend_decl=*/1);
15855 if (!friend_p && pushed_scope)
15856 pop_scope (pushed_scope);
15858 return decl;
15861 /* Parse a declarator.
15863 declarator:
15864 direct-declarator
15865 ptr-operator declarator
15867 abstract-declarator:
15868 ptr-operator abstract-declarator [opt]
15869 direct-abstract-declarator
15871 GNU Extensions:
15873 declarator:
15874 attributes [opt] direct-declarator
15875 attributes [opt] ptr-operator declarator
15877 abstract-declarator:
15878 attributes [opt] ptr-operator abstract-declarator [opt]
15879 attributes [opt] direct-abstract-declarator
15881 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15882 detect constructor, destructor or conversion operators. It is set
15883 to -1 if the declarator is a name, and +1 if it is a
15884 function. Otherwise it is set to zero. Usually you just want to
15885 test for >0, but internally the negative value is used.
15887 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15888 a decl-specifier-seq unless it declares a constructor, destructor,
15889 or conversion. It might seem that we could check this condition in
15890 semantic analysis, rather than parsing, but that makes it difficult
15891 to handle something like `f()'. We want to notice that there are
15892 no decl-specifiers, and therefore realize that this is an
15893 expression, not a declaration.)
15895 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15896 the declarator is a direct-declarator of the form "(...)".
15898 MEMBER_P is true iff this declarator is a member-declarator. */
15900 static cp_declarator *
15901 cp_parser_declarator (cp_parser* parser,
15902 cp_parser_declarator_kind dcl_kind,
15903 int* ctor_dtor_or_conv_p,
15904 bool* parenthesized_p,
15905 bool member_p)
15907 cp_declarator *declarator;
15908 enum tree_code code;
15909 cp_cv_quals cv_quals;
15910 tree class_type;
15911 tree attributes = NULL_TREE;
15913 /* Assume this is not a constructor, destructor, or type-conversion
15914 operator. */
15915 if (ctor_dtor_or_conv_p)
15916 *ctor_dtor_or_conv_p = 0;
15918 if (cp_parser_allow_gnu_extensions_p (parser))
15919 attributes = cp_parser_attributes_opt (parser);
15921 /* Check for the ptr-operator production. */
15922 cp_parser_parse_tentatively (parser);
15923 /* Parse the ptr-operator. */
15924 code = cp_parser_ptr_operator (parser,
15925 &class_type,
15926 &cv_quals);
15927 /* If that worked, then we have a ptr-operator. */
15928 if (cp_parser_parse_definitely (parser))
15930 /* If a ptr-operator was found, then this declarator was not
15931 parenthesized. */
15932 if (parenthesized_p)
15933 *parenthesized_p = true;
15934 /* The dependent declarator is optional if we are parsing an
15935 abstract-declarator. */
15936 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15937 cp_parser_parse_tentatively (parser);
15939 /* Parse the dependent declarator. */
15940 declarator = cp_parser_declarator (parser, dcl_kind,
15941 /*ctor_dtor_or_conv_p=*/NULL,
15942 /*parenthesized_p=*/NULL,
15943 /*member_p=*/false);
15945 /* If we are parsing an abstract-declarator, we must handle the
15946 case where the dependent declarator is absent. */
15947 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15948 && !cp_parser_parse_definitely (parser))
15949 declarator = NULL;
15951 declarator = cp_parser_make_indirect_declarator
15952 (code, class_type, cv_quals, declarator);
15954 /* Everything else is a direct-declarator. */
15955 else
15957 if (parenthesized_p)
15958 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15959 CPP_OPEN_PAREN);
15960 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15961 ctor_dtor_or_conv_p,
15962 member_p);
15965 if (attributes && declarator && declarator != cp_error_declarator)
15966 declarator->attributes = attributes;
15968 return declarator;
15971 /* Parse a direct-declarator or direct-abstract-declarator.
15973 direct-declarator:
15974 declarator-id
15975 direct-declarator ( parameter-declaration-clause )
15976 cv-qualifier-seq [opt]
15977 exception-specification [opt]
15978 direct-declarator [ constant-expression [opt] ]
15979 ( declarator )
15981 direct-abstract-declarator:
15982 direct-abstract-declarator [opt]
15983 ( parameter-declaration-clause )
15984 cv-qualifier-seq [opt]
15985 exception-specification [opt]
15986 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15987 ( abstract-declarator )
15989 Returns a representation of the declarator. DCL_KIND is
15990 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15991 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15992 we are parsing a direct-declarator. It is
15993 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15994 of ambiguity we prefer an abstract declarator, as per
15995 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15996 cp_parser_declarator. */
15998 static cp_declarator *
15999 cp_parser_direct_declarator (cp_parser* parser,
16000 cp_parser_declarator_kind dcl_kind,
16001 int* ctor_dtor_or_conv_p,
16002 bool member_p)
16004 cp_token *token;
16005 cp_declarator *declarator = NULL;
16006 tree scope = NULL_TREE;
16007 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16008 bool saved_in_declarator_p = parser->in_declarator_p;
16009 bool first = true;
16010 tree pushed_scope = NULL_TREE;
16012 while (true)
16014 /* Peek at the next token. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016 if (token->type == CPP_OPEN_PAREN)
16018 /* This is either a parameter-declaration-clause, or a
16019 parenthesized declarator. When we know we are parsing a
16020 named declarator, it must be a parenthesized declarator
16021 if FIRST is true. For instance, `(int)' is a
16022 parameter-declaration-clause, with an omitted
16023 direct-abstract-declarator. But `((*))', is a
16024 parenthesized abstract declarator. Finally, when T is a
16025 template parameter `(T)' is a
16026 parameter-declaration-clause, and not a parenthesized
16027 named declarator.
16029 We first try and parse a parameter-declaration-clause,
16030 and then try a nested declarator (if FIRST is true).
16032 It is not an error for it not to be a
16033 parameter-declaration-clause, even when FIRST is
16034 false. Consider,
16036 int i (int);
16037 int i (3);
16039 The first is the declaration of a function while the
16040 second is the definition of a variable, including its
16041 initializer.
16043 Having seen only the parenthesis, we cannot know which of
16044 these two alternatives should be selected. Even more
16045 complex are examples like:
16047 int i (int (a));
16048 int i (int (3));
16050 The former is a function-declaration; the latter is a
16051 variable initialization.
16053 Thus again, we try a parameter-declaration-clause, and if
16054 that fails, we back out and return. */
16056 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16058 tree params;
16059 unsigned saved_num_template_parameter_lists;
16060 bool is_declarator = false;
16061 tree t;
16063 /* In a member-declarator, the only valid interpretation
16064 of a parenthesis is the start of a
16065 parameter-declaration-clause. (It is invalid to
16066 initialize a static data member with a parenthesized
16067 initializer; only the "=" form of initialization is
16068 permitted.) */
16069 if (!member_p)
16070 cp_parser_parse_tentatively (parser);
16072 /* Consume the `('. */
16073 cp_lexer_consume_token (parser->lexer);
16074 if (first)
16076 /* If this is going to be an abstract declarator, we're
16077 in a declarator and we can't have default args. */
16078 parser->default_arg_ok_p = false;
16079 parser->in_declarator_p = true;
16082 /* Inside the function parameter list, surrounding
16083 template-parameter-lists do not apply. */
16084 saved_num_template_parameter_lists
16085 = parser->num_template_parameter_lists;
16086 parser->num_template_parameter_lists = 0;
16088 begin_scope (sk_function_parms, NULL_TREE);
16090 /* Parse the parameter-declaration-clause. */
16091 params = cp_parser_parameter_declaration_clause (parser);
16093 parser->num_template_parameter_lists
16094 = saved_num_template_parameter_lists;
16096 /* Consume the `)'. */
16097 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16099 /* If all went well, parse the cv-qualifier-seq and the
16100 exception-specification. */
16101 if (member_p || cp_parser_parse_definitely (parser))
16103 cp_cv_quals cv_quals;
16104 cp_virt_specifiers virt_specifiers;
16105 tree exception_specification;
16106 tree late_return;
16108 is_declarator = true;
16110 if (ctor_dtor_or_conv_p)
16111 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16112 first = false;
16114 /* Parse the cv-qualifier-seq. */
16115 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16116 /* And the exception-specification. */
16117 exception_specification
16118 = cp_parser_exception_specification_opt (parser);
16120 late_return = (cp_parser_late_return_type_opt
16121 (parser, member_p ? cv_quals : -1));
16123 /* Parse the virt-specifier-seq. */
16124 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16126 /* Create the function-declarator. */
16127 declarator = make_call_declarator (declarator,
16128 params,
16129 cv_quals,
16130 virt_specifiers,
16131 exception_specification,
16132 late_return);
16133 /* Any subsequent parameter lists are to do with
16134 return type, so are not those of the declared
16135 function. */
16136 parser->default_arg_ok_p = false;
16139 /* Remove the function parms from scope. */
16140 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16141 pop_binding (DECL_NAME (t), t);
16142 leave_scope();
16144 if (is_declarator)
16145 /* Repeat the main loop. */
16146 continue;
16149 /* If this is the first, we can try a parenthesized
16150 declarator. */
16151 if (first)
16153 bool saved_in_type_id_in_expr_p;
16155 parser->default_arg_ok_p = saved_default_arg_ok_p;
16156 parser->in_declarator_p = saved_in_declarator_p;
16158 /* Consume the `('. */
16159 cp_lexer_consume_token (parser->lexer);
16160 /* Parse the nested declarator. */
16161 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16162 parser->in_type_id_in_expr_p = true;
16163 declarator
16164 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16165 /*parenthesized_p=*/NULL,
16166 member_p);
16167 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16168 first = false;
16169 /* Expect a `)'. */
16170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16171 declarator = cp_error_declarator;
16172 if (declarator == cp_error_declarator)
16173 break;
16175 goto handle_declarator;
16177 /* Otherwise, we must be done. */
16178 else
16179 break;
16181 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16182 && token->type == CPP_OPEN_SQUARE)
16184 /* Parse an array-declarator. */
16185 tree bounds;
16187 if (ctor_dtor_or_conv_p)
16188 *ctor_dtor_or_conv_p = 0;
16190 first = false;
16191 parser->default_arg_ok_p = false;
16192 parser->in_declarator_p = true;
16193 /* Consume the `['. */
16194 cp_lexer_consume_token (parser->lexer);
16195 /* Peek at the next token. */
16196 token = cp_lexer_peek_token (parser->lexer);
16197 /* If the next token is `]', then there is no
16198 constant-expression. */
16199 if (token->type != CPP_CLOSE_SQUARE)
16201 bool non_constant_p;
16203 bounds
16204 = cp_parser_constant_expression (parser,
16205 /*allow_non_constant=*/true,
16206 &non_constant_p);
16207 if (!non_constant_p)
16208 /* OK */;
16209 else if (error_operand_p (bounds))
16210 /* Already gave an error. */;
16211 else if (!parser->in_function_body
16212 || current_binding_level->kind == sk_function_parms)
16214 /* Normally, the array bound must be an integral constant
16215 expression. However, as an extension, we allow VLAs
16216 in function scopes as long as they aren't part of a
16217 parameter declaration. */
16218 cp_parser_error (parser,
16219 "array bound is not an integer constant");
16220 bounds = error_mark_node;
16222 else if (processing_template_decl)
16224 /* Remember this wasn't a constant-expression. */
16225 bounds = build_nop (TREE_TYPE (bounds), bounds);
16226 TREE_SIDE_EFFECTS (bounds) = 1;
16229 else
16230 bounds = NULL_TREE;
16231 /* Look for the closing `]'. */
16232 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16234 declarator = cp_error_declarator;
16235 break;
16238 declarator = make_array_declarator (declarator, bounds);
16240 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16243 tree qualifying_scope;
16244 tree unqualified_name;
16245 special_function_kind sfk;
16246 bool abstract_ok;
16247 bool pack_expansion_p = false;
16248 cp_token *declarator_id_start_token;
16250 /* Parse a declarator-id */
16251 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16252 if (abstract_ok)
16254 cp_parser_parse_tentatively (parser);
16256 /* If we see an ellipsis, we should be looking at a
16257 parameter pack. */
16258 if (token->type == CPP_ELLIPSIS)
16260 /* Consume the `...' */
16261 cp_lexer_consume_token (parser->lexer);
16263 pack_expansion_p = true;
16267 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16268 unqualified_name
16269 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16270 qualifying_scope = parser->scope;
16271 if (abstract_ok)
16273 bool okay = false;
16275 if (!unqualified_name && pack_expansion_p)
16277 /* Check whether an error occurred. */
16278 okay = !cp_parser_error_occurred (parser);
16280 /* We already consumed the ellipsis to mark a
16281 parameter pack, but we have no way to report it,
16282 so abort the tentative parse. We will be exiting
16283 immediately anyway. */
16284 cp_parser_abort_tentative_parse (parser);
16286 else
16287 okay = cp_parser_parse_definitely (parser);
16289 if (!okay)
16290 unqualified_name = error_mark_node;
16291 else if (unqualified_name
16292 && (qualifying_scope
16293 || (TREE_CODE (unqualified_name)
16294 != IDENTIFIER_NODE)))
16296 cp_parser_error (parser, "expected unqualified-id");
16297 unqualified_name = error_mark_node;
16301 if (!unqualified_name)
16302 return NULL;
16303 if (unqualified_name == error_mark_node)
16305 declarator = cp_error_declarator;
16306 pack_expansion_p = false;
16307 declarator->parameter_pack_p = false;
16308 break;
16311 if (qualifying_scope && at_namespace_scope_p ()
16312 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16314 /* In the declaration of a member of a template class
16315 outside of the class itself, the SCOPE will sometimes
16316 be a TYPENAME_TYPE. For example, given:
16318 template <typename T>
16319 int S<T>::R::i = 3;
16321 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16322 this context, we must resolve S<T>::R to an ordinary
16323 type, rather than a typename type.
16325 The reason we normally avoid resolving TYPENAME_TYPEs
16326 is that a specialization of `S' might render
16327 `S<T>::R' not a type. However, if `S' is
16328 specialized, then this `i' will not be used, so there
16329 is no harm in resolving the types here. */
16330 tree type;
16332 /* Resolve the TYPENAME_TYPE. */
16333 type = resolve_typename_type (qualifying_scope,
16334 /*only_current_p=*/false);
16335 /* If that failed, the declarator is invalid. */
16336 if (TREE_CODE (type) == TYPENAME_TYPE)
16338 if (typedef_variant_p (type))
16339 error_at (declarator_id_start_token->location,
16340 "cannot define member of dependent typedef "
16341 "%qT", type);
16342 else
16343 error_at (declarator_id_start_token->location,
16344 "%<%T::%E%> is not a type",
16345 TYPE_CONTEXT (qualifying_scope),
16346 TYPE_IDENTIFIER (qualifying_scope));
16348 qualifying_scope = type;
16351 sfk = sfk_none;
16353 if (unqualified_name)
16355 tree class_type;
16357 if (qualifying_scope
16358 && CLASS_TYPE_P (qualifying_scope))
16359 class_type = qualifying_scope;
16360 else
16361 class_type = current_class_type;
16363 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16365 tree name_type = TREE_TYPE (unqualified_name);
16366 if (class_type && same_type_p (name_type, class_type))
16368 if (qualifying_scope
16369 && CLASSTYPE_USE_TEMPLATE (name_type))
16371 error_at (declarator_id_start_token->location,
16372 "invalid use of constructor as a template");
16373 inform (declarator_id_start_token->location,
16374 "use %<%T::%D%> instead of %<%T::%D%> to "
16375 "name the constructor in a qualified name",
16376 class_type,
16377 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16378 class_type, name_type);
16379 declarator = cp_error_declarator;
16380 break;
16382 else
16383 unqualified_name = constructor_name (class_type);
16385 else
16387 /* We do not attempt to print the declarator
16388 here because we do not have enough
16389 information about its original syntactic
16390 form. */
16391 cp_parser_error (parser, "invalid declarator");
16392 declarator = cp_error_declarator;
16393 break;
16397 if (class_type)
16399 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16400 sfk = sfk_destructor;
16401 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16402 sfk = sfk_conversion;
16403 else if (/* There's no way to declare a constructor
16404 for an anonymous type, even if the type
16405 got a name for linkage purposes. */
16406 !TYPE_WAS_ANONYMOUS (class_type)
16407 && constructor_name_p (unqualified_name,
16408 class_type))
16410 unqualified_name = constructor_name (class_type);
16411 sfk = sfk_constructor;
16413 else if (is_overloaded_fn (unqualified_name)
16414 && DECL_CONSTRUCTOR_P (get_first_fn
16415 (unqualified_name)))
16416 sfk = sfk_constructor;
16418 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16419 *ctor_dtor_or_conv_p = -1;
16422 declarator = make_id_declarator (qualifying_scope,
16423 unqualified_name,
16424 sfk);
16425 declarator->id_loc = token->location;
16426 declarator->parameter_pack_p = pack_expansion_p;
16428 if (pack_expansion_p)
16429 maybe_warn_variadic_templates ();
16432 handle_declarator:;
16433 scope = get_scope_of_declarator (declarator);
16434 if (scope)
16435 /* Any names that appear after the declarator-id for a
16436 member are looked up in the containing scope. */
16437 pushed_scope = push_scope (scope);
16438 parser->in_declarator_p = true;
16439 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16440 || (declarator && declarator->kind == cdk_id))
16441 /* Default args are only allowed on function
16442 declarations. */
16443 parser->default_arg_ok_p = saved_default_arg_ok_p;
16444 else
16445 parser->default_arg_ok_p = false;
16447 first = false;
16449 /* We're done. */
16450 else
16451 break;
16454 /* For an abstract declarator, we might wind up with nothing at this
16455 point. That's an error; the declarator is not optional. */
16456 if (!declarator)
16457 cp_parser_error (parser, "expected declarator");
16459 /* If we entered a scope, we must exit it now. */
16460 if (pushed_scope)
16461 pop_scope (pushed_scope);
16463 parser->default_arg_ok_p = saved_default_arg_ok_p;
16464 parser->in_declarator_p = saved_in_declarator_p;
16466 return declarator;
16469 /* Parse a ptr-operator.
16471 ptr-operator:
16472 * cv-qualifier-seq [opt]
16474 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16476 GNU Extension:
16478 ptr-operator:
16479 & cv-qualifier-seq [opt]
16481 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16482 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16483 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16484 filled in with the TYPE containing the member. *CV_QUALS is
16485 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16486 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16487 Note that the tree codes returned by this function have nothing
16488 to do with the types of trees that will be eventually be created
16489 to represent the pointer or reference type being parsed. They are
16490 just constants with suggestive names. */
16491 static enum tree_code
16492 cp_parser_ptr_operator (cp_parser* parser,
16493 tree* type,
16494 cp_cv_quals *cv_quals)
16496 enum tree_code code = ERROR_MARK;
16497 cp_token *token;
16499 /* Assume that it's not a pointer-to-member. */
16500 *type = NULL_TREE;
16501 /* And that there are no cv-qualifiers. */
16502 *cv_quals = TYPE_UNQUALIFIED;
16504 /* Peek at the next token. */
16505 token = cp_lexer_peek_token (parser->lexer);
16507 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16508 if (token->type == CPP_MULT)
16509 code = INDIRECT_REF;
16510 else if (token->type == CPP_AND)
16511 code = ADDR_EXPR;
16512 else if ((cxx_dialect != cxx98) &&
16513 token->type == CPP_AND_AND) /* C++0x only */
16514 code = NON_LVALUE_EXPR;
16516 if (code != ERROR_MARK)
16518 /* Consume the `*', `&' or `&&'. */
16519 cp_lexer_consume_token (parser->lexer);
16521 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16522 `&', if we are allowing GNU extensions. (The only qualifier
16523 that can legally appear after `&' is `restrict', but that is
16524 enforced during semantic analysis. */
16525 if (code == INDIRECT_REF
16526 || cp_parser_allow_gnu_extensions_p (parser))
16527 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16529 else
16531 /* Try the pointer-to-member case. */
16532 cp_parser_parse_tentatively (parser);
16533 /* Look for the optional `::' operator. */
16534 cp_parser_global_scope_opt (parser,
16535 /*current_scope_valid_p=*/false);
16536 /* Look for the nested-name specifier. */
16537 token = cp_lexer_peek_token (parser->lexer);
16538 cp_parser_nested_name_specifier (parser,
16539 /*typename_keyword_p=*/false,
16540 /*check_dependency_p=*/true,
16541 /*type_p=*/false,
16542 /*is_declaration=*/false);
16543 /* If we found it, and the next token is a `*', then we are
16544 indeed looking at a pointer-to-member operator. */
16545 if (!cp_parser_error_occurred (parser)
16546 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16548 /* Indicate that the `*' operator was used. */
16549 code = INDIRECT_REF;
16551 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16552 error_at (token->location, "%qD is a namespace", parser->scope);
16553 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16554 error_at (token->location, "cannot form pointer to member of "
16555 "non-class %q#T", parser->scope);
16556 else
16558 /* The type of which the member is a member is given by the
16559 current SCOPE. */
16560 *type = parser->scope;
16561 /* The next name will not be qualified. */
16562 parser->scope = NULL_TREE;
16563 parser->qualifying_scope = NULL_TREE;
16564 parser->object_scope = NULL_TREE;
16565 /* Look for the optional cv-qualifier-seq. */
16566 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16569 /* If that didn't work we don't have a ptr-operator. */
16570 if (!cp_parser_parse_definitely (parser))
16571 cp_parser_error (parser, "expected ptr-operator");
16574 return code;
16577 /* Parse an (optional) cv-qualifier-seq.
16579 cv-qualifier-seq:
16580 cv-qualifier cv-qualifier-seq [opt]
16582 cv-qualifier:
16583 const
16584 volatile
16586 GNU Extension:
16588 cv-qualifier:
16589 __restrict__
16591 Returns a bitmask representing the cv-qualifiers. */
16593 static cp_cv_quals
16594 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16596 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16598 while (true)
16600 cp_token *token;
16601 cp_cv_quals cv_qualifier;
16603 /* Peek at the next token. */
16604 token = cp_lexer_peek_token (parser->lexer);
16605 /* See if it's a cv-qualifier. */
16606 switch (token->keyword)
16608 case RID_CONST:
16609 cv_qualifier = TYPE_QUAL_CONST;
16610 break;
16612 case RID_VOLATILE:
16613 cv_qualifier = TYPE_QUAL_VOLATILE;
16614 break;
16616 case RID_RESTRICT:
16617 cv_qualifier = TYPE_QUAL_RESTRICT;
16618 break;
16620 default:
16621 cv_qualifier = TYPE_UNQUALIFIED;
16622 break;
16625 if (!cv_qualifier)
16626 break;
16628 if (cv_quals & cv_qualifier)
16630 error_at (token->location, "duplicate cv-qualifier");
16631 cp_lexer_purge_token (parser->lexer);
16633 else
16635 cp_lexer_consume_token (parser->lexer);
16636 cv_quals |= cv_qualifier;
16640 return cv_quals;
16643 /* Parse an (optional) virt-specifier-seq.
16645 virt-specifier-seq:
16646 virt-specifier virt-specifier-seq [opt]
16648 virt-specifier:
16649 override
16650 final
16652 Returns a bitmask representing the virt-specifiers. */
16654 static cp_virt_specifiers
16655 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16657 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16659 while (true)
16661 cp_token *token;
16662 cp_virt_specifiers virt_specifier;
16664 /* Peek at the next token. */
16665 token = cp_lexer_peek_token (parser->lexer);
16666 /* See if it's a virt-specifier-qualifier. */
16667 if (token->type != CPP_NAME)
16668 break;
16669 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16671 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16672 virt_specifier = VIRT_SPEC_OVERRIDE;
16674 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16676 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16677 virt_specifier = VIRT_SPEC_FINAL;
16679 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16681 virt_specifier = VIRT_SPEC_FINAL;
16683 else
16684 break;
16686 if (virt_specifiers & virt_specifier)
16688 error_at (token->location, "duplicate virt-specifier");
16689 cp_lexer_purge_token (parser->lexer);
16691 else
16693 cp_lexer_consume_token (parser->lexer);
16694 virt_specifiers |= virt_specifier;
16697 return virt_specifiers;
16700 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16701 is in scope even though it isn't real. */
16703 static void
16704 inject_this_parameter (tree ctype, cp_cv_quals quals)
16706 tree this_parm;
16708 if (current_class_ptr)
16710 /* We don't clear this between NSDMIs. Is it already what we want? */
16711 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16712 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16713 && cp_type_quals (type) == quals)
16714 return;
16717 this_parm = build_this_parm (ctype, quals);
16718 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16719 current_class_ptr = NULL_TREE;
16720 current_class_ref
16721 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16722 current_class_ptr = this_parm;
16725 /* Parse a late-specified return type, if any. This is not a separate
16726 non-terminal, but part of a function declarator, which looks like
16728 -> trailing-type-specifier-seq abstract-declarator(opt)
16730 Returns the type indicated by the type-id.
16732 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16733 function. */
16735 static tree
16736 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16738 cp_token *token;
16739 tree type;
16741 /* Peek at the next token. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 /* A late-specified return type is indicated by an initial '->'. */
16744 if (token->type != CPP_DEREF)
16745 return NULL_TREE;
16747 /* Consume the ->. */
16748 cp_lexer_consume_token (parser->lexer);
16750 if (quals >= 0)
16752 /* DR 1207: 'this' is in scope in the trailing return type. */
16753 gcc_assert (current_class_ptr == NULL_TREE);
16754 inject_this_parameter (current_class_type, quals);
16757 type = cp_parser_trailing_type_id (parser);
16759 if (quals >= 0)
16760 current_class_ptr = current_class_ref = NULL_TREE;
16762 return type;
16765 /* Parse a declarator-id.
16767 declarator-id:
16768 id-expression
16769 :: [opt] nested-name-specifier [opt] type-name
16771 In the `id-expression' case, the value returned is as for
16772 cp_parser_id_expression if the id-expression was an unqualified-id.
16773 If the id-expression was a qualified-id, then a SCOPE_REF is
16774 returned. The first operand is the scope (either a NAMESPACE_DECL
16775 or TREE_TYPE), but the second is still just a representation of an
16776 unqualified-id. */
16778 static tree
16779 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16781 tree id;
16782 /* The expression must be an id-expression. Assume that qualified
16783 names are the names of types so that:
16785 template <class T>
16786 int S<T>::R::i = 3;
16788 will work; we must treat `S<T>::R' as the name of a type.
16789 Similarly, assume that qualified names are templates, where
16790 required, so that:
16792 template <class T>
16793 int S<T>::R<T>::i = 3;
16795 will work, too. */
16796 id = cp_parser_id_expression (parser,
16797 /*template_keyword_p=*/false,
16798 /*check_dependency_p=*/false,
16799 /*template_p=*/NULL,
16800 /*declarator_p=*/true,
16801 optional_p);
16802 if (id && BASELINK_P (id))
16803 id = BASELINK_FUNCTIONS (id);
16804 return id;
16807 /* Parse a type-id.
16809 type-id:
16810 type-specifier-seq abstract-declarator [opt]
16812 Returns the TYPE specified. */
16814 static tree
16815 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16816 bool is_trailing_return)
16818 cp_decl_specifier_seq type_specifier_seq;
16819 cp_declarator *abstract_declarator;
16821 /* Parse the type-specifier-seq. */
16822 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16823 is_trailing_return,
16824 &type_specifier_seq);
16825 if (type_specifier_seq.type == error_mark_node)
16826 return error_mark_node;
16828 /* There might or might not be an abstract declarator. */
16829 cp_parser_parse_tentatively (parser);
16830 /* Look for the declarator. */
16831 abstract_declarator
16832 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16833 /*parenthesized_p=*/NULL,
16834 /*member_p=*/false);
16835 /* Check to see if there really was a declarator. */
16836 if (!cp_parser_parse_definitely (parser))
16837 abstract_declarator = NULL;
16839 if (type_specifier_seq.type
16840 && type_uses_auto (type_specifier_seq.type))
16842 /* A type-id with type 'auto' is only ok if the abstract declarator
16843 is a function declarator with a late-specified return type. */
16844 if (abstract_declarator
16845 && abstract_declarator->kind == cdk_function
16846 && abstract_declarator->u.function.late_return_type)
16847 /* OK */;
16848 else
16850 error ("invalid use of %<auto%>");
16851 return error_mark_node;
16855 return groktypename (&type_specifier_seq, abstract_declarator,
16856 is_template_arg);
16859 static tree cp_parser_type_id (cp_parser *parser)
16861 return cp_parser_type_id_1 (parser, false, false);
16864 static tree cp_parser_template_type_arg (cp_parser *parser)
16866 tree r;
16867 const char *saved_message = parser->type_definition_forbidden_message;
16868 parser->type_definition_forbidden_message
16869 = G_("types may not be defined in template arguments");
16870 r = cp_parser_type_id_1 (parser, true, false);
16871 parser->type_definition_forbidden_message = saved_message;
16872 return r;
16875 static tree cp_parser_trailing_type_id (cp_parser *parser)
16877 return cp_parser_type_id_1 (parser, false, true);
16880 /* Parse a type-specifier-seq.
16882 type-specifier-seq:
16883 type-specifier type-specifier-seq [opt]
16885 GNU extension:
16887 type-specifier-seq:
16888 attributes type-specifier-seq [opt]
16890 If IS_DECLARATION is true, we are at the start of a "condition" or
16891 exception-declaration, so we might be followed by a declarator-id.
16893 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16894 i.e. we've just seen "->".
16896 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16898 static void
16899 cp_parser_type_specifier_seq (cp_parser* parser,
16900 bool is_declaration,
16901 bool is_trailing_return,
16902 cp_decl_specifier_seq *type_specifier_seq)
16904 bool seen_type_specifier = false;
16905 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16906 cp_token *start_token = NULL;
16908 /* Clear the TYPE_SPECIFIER_SEQ. */
16909 clear_decl_specs (type_specifier_seq);
16911 /* In the context of a trailing return type, enum E { } is an
16912 elaborated-type-specifier followed by a function-body, not an
16913 enum-specifier. */
16914 if (is_trailing_return)
16915 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16917 /* Parse the type-specifiers and attributes. */
16918 while (true)
16920 tree type_specifier;
16921 bool is_cv_qualifier;
16923 /* Check for attributes first. */
16924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16926 type_specifier_seq->attributes =
16927 chainon (type_specifier_seq->attributes,
16928 cp_parser_attributes_opt (parser));
16929 continue;
16932 /* record the token of the beginning of the type specifier seq,
16933 for error reporting purposes*/
16934 if (!start_token)
16935 start_token = cp_lexer_peek_token (parser->lexer);
16937 /* Look for the type-specifier. */
16938 type_specifier = cp_parser_type_specifier (parser,
16939 flags,
16940 type_specifier_seq,
16941 /*is_declaration=*/false,
16942 NULL,
16943 &is_cv_qualifier);
16944 if (!type_specifier)
16946 /* If the first type-specifier could not be found, this is not a
16947 type-specifier-seq at all. */
16948 if (!seen_type_specifier)
16950 cp_parser_error (parser, "expected type-specifier");
16951 type_specifier_seq->type = error_mark_node;
16952 return;
16954 /* If subsequent type-specifiers could not be found, the
16955 type-specifier-seq is complete. */
16956 break;
16959 seen_type_specifier = true;
16960 /* The standard says that a condition can be:
16962 type-specifier-seq declarator = assignment-expression
16964 However, given:
16966 struct S {};
16967 if (int S = ...)
16969 we should treat the "S" as a declarator, not as a
16970 type-specifier. The standard doesn't say that explicitly for
16971 type-specifier-seq, but it does say that for
16972 decl-specifier-seq in an ordinary declaration. Perhaps it
16973 would be clearer just to allow a decl-specifier-seq here, and
16974 then add a semantic restriction that if any decl-specifiers
16975 that are not type-specifiers appear, the program is invalid. */
16976 if (is_declaration && !is_cv_qualifier)
16977 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16981 /* Parse a parameter-declaration-clause.
16983 parameter-declaration-clause:
16984 parameter-declaration-list [opt] ... [opt]
16985 parameter-declaration-list , ...
16987 Returns a representation for the parameter declarations. A return
16988 value of NULL indicates a parameter-declaration-clause consisting
16989 only of an ellipsis. */
16991 static tree
16992 cp_parser_parameter_declaration_clause (cp_parser* parser)
16994 tree parameters;
16995 cp_token *token;
16996 bool ellipsis_p;
16997 bool is_error;
16999 /* Peek at the next token. */
17000 token = cp_lexer_peek_token (parser->lexer);
17001 /* Check for trivial parameter-declaration-clauses. */
17002 if (token->type == CPP_ELLIPSIS)
17004 /* Consume the `...' token. */
17005 cp_lexer_consume_token (parser->lexer);
17006 return NULL_TREE;
17008 else if (token->type == CPP_CLOSE_PAREN)
17009 /* There are no parameters. */
17011 #ifndef NO_IMPLICIT_EXTERN_C
17012 if (in_system_header && current_class_type == NULL
17013 && current_lang_name == lang_name_c)
17014 return NULL_TREE;
17015 else
17016 #endif
17017 return void_list_node;
17019 /* Check for `(void)', too, which is a special case. */
17020 else if (token->keyword == RID_VOID
17021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17022 == CPP_CLOSE_PAREN))
17024 /* Consume the `void' token. */
17025 cp_lexer_consume_token (parser->lexer);
17026 /* There are no parameters. */
17027 return void_list_node;
17030 /* Parse the parameter-declaration-list. */
17031 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17032 /* If a parse error occurred while parsing the
17033 parameter-declaration-list, then the entire
17034 parameter-declaration-clause is erroneous. */
17035 if (is_error)
17036 return NULL;
17038 /* Peek at the next token. */
17039 token = cp_lexer_peek_token (parser->lexer);
17040 /* If it's a `,', the clause should terminate with an ellipsis. */
17041 if (token->type == CPP_COMMA)
17043 /* Consume the `,'. */
17044 cp_lexer_consume_token (parser->lexer);
17045 /* Expect an ellipsis. */
17046 ellipsis_p
17047 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17049 /* It might also be `...' if the optional trailing `,' was
17050 omitted. */
17051 else if (token->type == CPP_ELLIPSIS)
17053 /* Consume the `...' token. */
17054 cp_lexer_consume_token (parser->lexer);
17055 /* And remember that we saw it. */
17056 ellipsis_p = true;
17058 else
17059 ellipsis_p = false;
17061 /* Finish the parameter list. */
17062 if (!ellipsis_p)
17063 parameters = chainon (parameters, void_list_node);
17065 return parameters;
17068 /* Parse a parameter-declaration-list.
17070 parameter-declaration-list:
17071 parameter-declaration
17072 parameter-declaration-list , parameter-declaration
17074 Returns a representation of the parameter-declaration-list, as for
17075 cp_parser_parameter_declaration_clause. However, the
17076 `void_list_node' is never appended to the list. Upon return,
17077 *IS_ERROR will be true iff an error occurred. */
17079 static tree
17080 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17082 tree parameters = NULL_TREE;
17083 tree *tail = &parameters;
17084 bool saved_in_unbraced_linkage_specification_p;
17085 int index = 0;
17087 /* Assume all will go well. */
17088 *is_error = false;
17089 /* The special considerations that apply to a function within an
17090 unbraced linkage specifications do not apply to the parameters
17091 to the function. */
17092 saved_in_unbraced_linkage_specification_p
17093 = parser->in_unbraced_linkage_specification_p;
17094 parser->in_unbraced_linkage_specification_p = false;
17096 /* Look for more parameters. */
17097 while (true)
17099 cp_parameter_declarator *parameter;
17100 tree decl = error_mark_node;
17101 bool parenthesized_p = false;
17102 /* Parse the parameter. */
17103 parameter
17104 = cp_parser_parameter_declaration (parser,
17105 /*template_parm_p=*/false,
17106 &parenthesized_p);
17108 /* We don't know yet if the enclosing context is deprecated, so wait
17109 and warn in grokparms if appropriate. */
17110 deprecated_state = DEPRECATED_SUPPRESS;
17112 if (parameter)
17113 decl = grokdeclarator (parameter->declarator,
17114 &parameter->decl_specifiers,
17115 PARM,
17116 parameter->default_argument != NULL_TREE,
17117 &parameter->decl_specifiers.attributes);
17119 deprecated_state = DEPRECATED_NORMAL;
17121 /* If a parse error occurred parsing the parameter declaration,
17122 then the entire parameter-declaration-list is erroneous. */
17123 if (decl == error_mark_node)
17125 *is_error = true;
17126 parameters = error_mark_node;
17127 break;
17130 if (parameter->decl_specifiers.attributes)
17131 cplus_decl_attributes (&decl,
17132 parameter->decl_specifiers.attributes,
17134 if (DECL_NAME (decl))
17135 decl = pushdecl (decl);
17137 if (decl != error_mark_node)
17139 retrofit_lang_decl (decl);
17140 DECL_PARM_INDEX (decl) = ++index;
17141 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17144 /* Add the new parameter to the list. */
17145 *tail = build_tree_list (parameter->default_argument, decl);
17146 tail = &TREE_CHAIN (*tail);
17148 /* Peek at the next token. */
17149 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17150 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17151 /* These are for Objective-C++ */
17152 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17153 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17154 /* The parameter-declaration-list is complete. */
17155 break;
17156 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17158 cp_token *token;
17160 /* Peek at the next token. */
17161 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17162 /* If it's an ellipsis, then the list is complete. */
17163 if (token->type == CPP_ELLIPSIS)
17164 break;
17165 /* Otherwise, there must be more parameters. Consume the
17166 `,'. */
17167 cp_lexer_consume_token (parser->lexer);
17168 /* When parsing something like:
17170 int i(float f, double d)
17172 we can tell after seeing the declaration for "f" that we
17173 are not looking at an initialization of a variable "i",
17174 but rather at the declaration of a function "i".
17176 Due to the fact that the parsing of template arguments
17177 (as specified to a template-id) requires backtracking we
17178 cannot use this technique when inside a template argument
17179 list. */
17180 if (!parser->in_template_argument_list_p
17181 && !parser->in_type_id_in_expr_p
17182 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17183 /* However, a parameter-declaration of the form
17184 "foat(f)" (which is a valid declaration of a
17185 parameter "f") can also be interpreted as an
17186 expression (the conversion of "f" to "float"). */
17187 && !parenthesized_p)
17188 cp_parser_commit_to_tentative_parse (parser);
17190 else
17192 cp_parser_error (parser, "expected %<,%> or %<...%>");
17193 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17194 cp_parser_skip_to_closing_parenthesis (parser,
17195 /*recovering=*/true,
17196 /*or_comma=*/false,
17197 /*consume_paren=*/false);
17198 break;
17202 parser->in_unbraced_linkage_specification_p
17203 = saved_in_unbraced_linkage_specification_p;
17205 return parameters;
17208 /* Parse a parameter declaration.
17210 parameter-declaration:
17211 decl-specifier-seq ... [opt] declarator
17212 decl-specifier-seq declarator = assignment-expression
17213 decl-specifier-seq ... [opt] abstract-declarator [opt]
17214 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17216 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17217 declares a template parameter. (In that case, a non-nested `>'
17218 token encountered during the parsing of the assignment-expression
17219 is not interpreted as a greater-than operator.)
17221 Returns a representation of the parameter, or NULL if an error
17222 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17223 true iff the declarator is of the form "(p)". */
17225 static cp_parameter_declarator *
17226 cp_parser_parameter_declaration (cp_parser *parser,
17227 bool template_parm_p,
17228 bool *parenthesized_p)
17230 int declares_class_or_enum;
17231 cp_decl_specifier_seq decl_specifiers;
17232 cp_declarator *declarator;
17233 tree default_argument;
17234 cp_token *token = NULL, *declarator_token_start = NULL;
17235 const char *saved_message;
17237 /* In a template parameter, `>' is not an operator.
17239 [temp.param]
17241 When parsing a default template-argument for a non-type
17242 template-parameter, the first non-nested `>' is taken as the end
17243 of the template parameter-list rather than a greater-than
17244 operator. */
17246 /* Type definitions may not appear in parameter types. */
17247 saved_message = parser->type_definition_forbidden_message;
17248 parser->type_definition_forbidden_message
17249 = G_("types may not be defined in parameter types");
17251 /* Parse the declaration-specifiers. */
17252 cp_parser_decl_specifier_seq (parser,
17253 CP_PARSER_FLAGS_NONE,
17254 &decl_specifiers,
17255 &declares_class_or_enum);
17257 /* Complain about missing 'typename' or other invalid type names. */
17258 if (!decl_specifiers.any_type_specifiers_p)
17259 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17261 /* If an error occurred, there's no reason to attempt to parse the
17262 rest of the declaration. */
17263 if (cp_parser_error_occurred (parser))
17265 parser->type_definition_forbidden_message = saved_message;
17266 return NULL;
17269 /* Peek at the next token. */
17270 token = cp_lexer_peek_token (parser->lexer);
17272 /* If the next token is a `)', `,', `=', `>', or `...', then there
17273 is no declarator. However, when variadic templates are enabled,
17274 there may be a declarator following `...'. */
17275 if (token->type == CPP_CLOSE_PAREN
17276 || token->type == CPP_COMMA
17277 || token->type == CPP_EQ
17278 || token->type == CPP_GREATER)
17280 declarator = NULL;
17281 if (parenthesized_p)
17282 *parenthesized_p = false;
17284 /* Otherwise, there should be a declarator. */
17285 else
17287 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17288 parser->default_arg_ok_p = false;
17290 /* After seeing a decl-specifier-seq, if the next token is not a
17291 "(", there is no possibility that the code is a valid
17292 expression. Therefore, if parsing tentatively, we commit at
17293 this point. */
17294 if (!parser->in_template_argument_list_p
17295 /* In an expression context, having seen:
17297 (int((char ...
17299 we cannot be sure whether we are looking at a
17300 function-type (taking a "char" as a parameter) or a cast
17301 of some object of type "char" to "int". */
17302 && !parser->in_type_id_in_expr_p
17303 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17305 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17306 cp_parser_commit_to_tentative_parse (parser);
17307 /* Parse the declarator. */
17308 declarator_token_start = token;
17309 declarator = cp_parser_declarator (parser,
17310 CP_PARSER_DECLARATOR_EITHER,
17311 /*ctor_dtor_or_conv_p=*/NULL,
17312 parenthesized_p,
17313 /*member_p=*/false);
17314 parser->default_arg_ok_p = saved_default_arg_ok_p;
17315 /* After the declarator, allow more attributes. */
17316 decl_specifiers.attributes
17317 = chainon (decl_specifiers.attributes,
17318 cp_parser_attributes_opt (parser));
17321 /* If the next token is an ellipsis, and we have not seen a
17322 declarator name, and the type of the declarator contains parameter
17323 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17324 a parameter pack expansion expression. Otherwise, leave the
17325 ellipsis for a C-style variadic function. */
17326 token = cp_lexer_peek_token (parser->lexer);
17327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17329 tree type = decl_specifiers.type;
17331 if (type && DECL_P (type))
17332 type = TREE_TYPE (type);
17334 if (type
17335 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17336 && declarator_can_be_parameter_pack (declarator)
17337 && (!declarator || !declarator->parameter_pack_p)
17338 && uses_parameter_packs (type))
17340 /* Consume the `...'. */
17341 cp_lexer_consume_token (parser->lexer);
17342 maybe_warn_variadic_templates ();
17344 /* Build a pack expansion type */
17345 if (declarator)
17346 declarator->parameter_pack_p = true;
17347 else
17348 decl_specifiers.type = make_pack_expansion (type);
17352 /* The restriction on defining new types applies only to the type
17353 of the parameter, not to the default argument. */
17354 parser->type_definition_forbidden_message = saved_message;
17356 /* If the next token is `=', then process a default argument. */
17357 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17359 token = cp_lexer_peek_token (parser->lexer);
17360 /* If we are defining a class, then the tokens that make up the
17361 default argument must be saved and processed later. */
17362 if (!template_parm_p && at_class_scope_p ()
17363 && TYPE_BEING_DEFINED (current_class_type)
17364 && !LAMBDA_TYPE_P (current_class_type))
17365 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17366 /* Outside of a class definition, we can just parse the
17367 assignment-expression. */
17368 else
17369 default_argument
17370 = cp_parser_default_argument (parser, template_parm_p);
17372 if (!parser->default_arg_ok_p)
17374 if (flag_permissive)
17375 warning (0, "deprecated use of default argument for parameter of non-function");
17376 else
17378 error_at (token->location,
17379 "default arguments are only "
17380 "permitted for function parameters");
17381 default_argument = NULL_TREE;
17384 else if ((declarator && declarator->parameter_pack_p)
17385 || (decl_specifiers.type
17386 && PACK_EXPANSION_P (decl_specifiers.type)))
17388 /* Find the name of the parameter pack. */
17389 cp_declarator *id_declarator = declarator;
17390 while (id_declarator && id_declarator->kind != cdk_id)
17391 id_declarator = id_declarator->declarator;
17393 if (id_declarator && id_declarator->kind == cdk_id)
17394 error_at (declarator_token_start->location,
17395 template_parm_p
17396 ? G_("template parameter pack %qD "
17397 "cannot have a default argument")
17398 : G_("parameter pack %qD cannot have "
17399 "a default argument"),
17400 id_declarator->u.id.unqualified_name);
17401 else
17402 error_at (declarator_token_start->location,
17403 template_parm_p
17404 ? G_("template parameter pack cannot have "
17405 "a default argument")
17406 : G_("parameter pack cannot have a "
17407 "default argument"));
17409 default_argument = NULL_TREE;
17412 else
17413 default_argument = NULL_TREE;
17415 return make_parameter_declarator (&decl_specifiers,
17416 declarator,
17417 default_argument);
17420 /* Parse a default argument and return it.
17422 TEMPLATE_PARM_P is true if this is a default argument for a
17423 non-type template parameter. */
17424 static tree
17425 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17427 tree default_argument = NULL_TREE;
17428 bool saved_greater_than_is_operator_p;
17429 bool saved_local_variables_forbidden_p;
17430 bool non_constant_p, is_direct_init;
17432 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17433 set correctly. */
17434 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17435 parser->greater_than_is_operator_p = !template_parm_p;
17436 /* Local variable names (and the `this' keyword) may not
17437 appear in a default argument. */
17438 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17439 parser->local_variables_forbidden_p = true;
17440 /* Parse the assignment-expression. */
17441 if (template_parm_p)
17442 push_deferring_access_checks (dk_no_deferred);
17443 default_argument
17444 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17445 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17447 if (template_parm_p)
17448 pop_deferring_access_checks ();
17449 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17450 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17452 return default_argument;
17455 /* Parse a function-body.
17457 function-body:
17458 compound_statement */
17460 static void
17461 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17463 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17466 /* Parse a ctor-initializer-opt followed by a function-body. Return
17467 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17468 is true we are parsing a function-try-block. */
17470 static bool
17471 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17472 bool in_function_try_block)
17474 tree body, list;
17475 bool ctor_initializer_p;
17476 const bool check_body_p =
17477 DECL_CONSTRUCTOR_P (current_function_decl)
17478 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17479 tree last = NULL;
17481 /* Begin the function body. */
17482 body = begin_function_body ();
17483 /* Parse the optional ctor-initializer. */
17484 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17486 /* If we're parsing a constexpr constructor definition, we need
17487 to check that the constructor body is indeed empty. However,
17488 before we get to cp_parser_function_body lot of junk has been
17489 generated, so we can't just check that we have an empty block.
17490 Rather we take a snapshot of the outermost block, and check whether
17491 cp_parser_function_body changed its state. */
17492 if (check_body_p)
17494 list = cur_stmt_list;
17495 if (STATEMENT_LIST_TAIL (list))
17496 last = STATEMENT_LIST_TAIL (list)->stmt;
17498 /* Parse the function-body. */
17499 cp_parser_function_body (parser, in_function_try_block);
17500 if (check_body_p)
17501 check_constexpr_ctor_body (last, list);
17502 /* Finish the function body. */
17503 finish_function_body (body);
17505 return ctor_initializer_p;
17508 /* Parse an initializer.
17510 initializer:
17511 = initializer-clause
17512 ( expression-list )
17514 Returns an expression representing the initializer. If no
17515 initializer is present, NULL_TREE is returned.
17517 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17518 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17519 set to TRUE if there is no initializer present. If there is an
17520 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17521 is set to true; otherwise it is set to false. */
17523 static tree
17524 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17525 bool* non_constant_p)
17527 cp_token *token;
17528 tree init;
17530 /* Peek at the next token. */
17531 token = cp_lexer_peek_token (parser->lexer);
17533 /* Let our caller know whether or not this initializer was
17534 parenthesized. */
17535 *is_direct_init = (token->type != CPP_EQ);
17536 /* Assume that the initializer is constant. */
17537 *non_constant_p = false;
17539 if (token->type == CPP_EQ)
17541 /* Consume the `='. */
17542 cp_lexer_consume_token (parser->lexer);
17543 /* Parse the initializer-clause. */
17544 init = cp_parser_initializer_clause (parser, non_constant_p);
17546 else if (token->type == CPP_OPEN_PAREN)
17548 VEC(tree,gc) *vec;
17549 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17550 /*cast_p=*/false,
17551 /*allow_expansion_p=*/true,
17552 non_constant_p);
17553 if (vec == NULL)
17554 return error_mark_node;
17555 init = build_tree_list_vec (vec);
17556 release_tree_vector (vec);
17558 else if (token->type == CPP_OPEN_BRACE)
17560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17561 init = cp_parser_braced_list (parser, non_constant_p);
17562 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17564 else
17566 /* Anything else is an error. */
17567 cp_parser_error (parser, "expected initializer");
17568 init = error_mark_node;
17571 return init;
17574 /* Parse an initializer-clause.
17576 initializer-clause:
17577 assignment-expression
17578 braced-init-list
17580 Returns an expression representing the initializer.
17582 If the `assignment-expression' production is used the value
17583 returned is simply a representation for the expression.
17585 Otherwise, calls cp_parser_braced_list. */
17587 static tree
17588 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17590 tree initializer;
17592 /* Assume the expression is constant. */
17593 *non_constant_p = false;
17595 /* If it is not a `{', then we are looking at an
17596 assignment-expression. */
17597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17599 initializer
17600 = cp_parser_constant_expression (parser,
17601 /*allow_non_constant_p=*/true,
17602 non_constant_p);
17604 else
17605 initializer = cp_parser_braced_list (parser, non_constant_p);
17607 return initializer;
17610 /* Parse a brace-enclosed initializer list.
17612 braced-init-list:
17613 { initializer-list , [opt] }
17616 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17617 the elements of the initializer-list (or NULL, if the last
17618 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17619 NULL_TREE. There is no way to detect whether or not the optional
17620 trailing `,' was provided. NON_CONSTANT_P is as for
17621 cp_parser_initializer. */
17623 static tree
17624 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17626 tree initializer;
17628 /* Consume the `{' token. */
17629 cp_lexer_consume_token (parser->lexer);
17630 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17631 initializer = make_node (CONSTRUCTOR);
17632 /* If it's not a `}', then there is a non-trivial initializer. */
17633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17635 /* Parse the initializer list. */
17636 CONSTRUCTOR_ELTS (initializer)
17637 = cp_parser_initializer_list (parser, non_constant_p);
17638 /* A trailing `,' token is allowed. */
17639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17640 cp_lexer_consume_token (parser->lexer);
17642 /* Now, there should be a trailing `}'. */
17643 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17644 TREE_TYPE (initializer) = init_list_type_node;
17645 return initializer;
17648 /* Parse an initializer-list.
17650 initializer-list:
17651 initializer-clause ... [opt]
17652 initializer-list , initializer-clause ... [opt]
17654 GNU Extension:
17656 initializer-list:
17657 designation initializer-clause ...[opt]
17658 initializer-list , designation initializer-clause ...[opt]
17660 designation:
17661 . identifier =
17662 identifier :
17663 [ constant-expression ] =
17665 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17666 for the initializer. If the INDEX of the elt is non-NULL, it is the
17667 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17668 as for cp_parser_initializer. */
17670 static VEC(constructor_elt,gc) *
17671 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17673 VEC(constructor_elt,gc) *v = NULL;
17675 /* Assume all of the expressions are constant. */
17676 *non_constant_p = false;
17678 /* Parse the rest of the list. */
17679 while (true)
17681 cp_token *token;
17682 tree designator;
17683 tree initializer;
17684 bool clause_non_constant_p;
17686 /* If the next token is an identifier and the following one is a
17687 colon, we are looking at the GNU designated-initializer
17688 syntax. */
17689 if (cp_parser_allow_gnu_extensions_p (parser)
17690 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17691 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17693 /* Warn the user that they are using an extension. */
17694 pedwarn (input_location, OPT_Wpedantic,
17695 "ISO C++ does not allow designated initializers");
17696 /* Consume the identifier. */
17697 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17698 /* Consume the `:'. */
17699 cp_lexer_consume_token (parser->lexer);
17701 /* Also handle the C99 syntax, '. id ='. */
17702 else if (cp_parser_allow_gnu_extensions_p (parser)
17703 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17704 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17705 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17707 /* Warn the user that they are using an extension. */
17708 pedwarn (input_location, OPT_Wpedantic,
17709 "ISO C++ does not allow C99 designated initializers");
17710 /* Consume the `.'. */
17711 cp_lexer_consume_token (parser->lexer);
17712 /* Consume the identifier. */
17713 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17714 /* Consume the `='. */
17715 cp_lexer_consume_token (parser->lexer);
17717 /* Also handle C99 array designators, '[ const ] ='. */
17718 else if (cp_parser_allow_gnu_extensions_p (parser)
17719 && !c_dialect_objc ()
17720 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17722 /* In C++11, [ could start a lambda-introducer. */
17723 cp_parser_parse_tentatively (parser);
17724 cp_lexer_consume_token (parser->lexer);
17725 designator = cp_parser_constant_expression (parser, false, NULL);
17726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17727 cp_parser_require (parser, CPP_EQ, RT_EQ);
17728 if (!cp_parser_parse_definitely (parser))
17729 designator = NULL_TREE;
17731 else
17732 designator = NULL_TREE;
17734 /* Parse the initializer. */
17735 initializer = cp_parser_initializer_clause (parser,
17736 &clause_non_constant_p);
17737 /* If any clause is non-constant, so is the entire initializer. */
17738 if (clause_non_constant_p)
17739 *non_constant_p = true;
17741 /* If we have an ellipsis, this is an initializer pack
17742 expansion. */
17743 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17745 /* Consume the `...'. */
17746 cp_lexer_consume_token (parser->lexer);
17748 /* Turn the initializer into an initializer expansion. */
17749 initializer = make_pack_expansion (initializer);
17752 /* Add it to the vector. */
17753 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17755 /* If the next token is not a comma, we have reached the end of
17756 the list. */
17757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17758 break;
17760 /* Peek at the next token. */
17761 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17762 /* If the next token is a `}', then we're still done. An
17763 initializer-clause can have a trailing `,' after the
17764 initializer-list and before the closing `}'. */
17765 if (token->type == CPP_CLOSE_BRACE)
17766 break;
17768 /* Consume the `,' token. */
17769 cp_lexer_consume_token (parser->lexer);
17772 return v;
17775 /* Classes [gram.class] */
17777 /* Parse a class-name.
17779 class-name:
17780 identifier
17781 template-id
17783 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17784 to indicate that names looked up in dependent types should be
17785 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17786 keyword has been used to indicate that the name that appears next
17787 is a template. TAG_TYPE indicates the explicit tag given before
17788 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17789 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17790 is the class being defined in a class-head.
17792 Returns the TYPE_DECL representing the class. */
17794 static tree
17795 cp_parser_class_name (cp_parser *parser,
17796 bool typename_keyword_p,
17797 bool template_keyword_p,
17798 enum tag_types tag_type,
17799 bool check_dependency_p,
17800 bool class_head_p,
17801 bool is_declaration)
17803 tree decl;
17804 tree scope;
17805 bool typename_p;
17806 cp_token *token;
17807 tree identifier = NULL_TREE;
17809 /* All class-names start with an identifier. */
17810 token = cp_lexer_peek_token (parser->lexer);
17811 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17813 cp_parser_error (parser, "expected class-name");
17814 return error_mark_node;
17817 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17818 to a template-id, so we save it here. */
17819 scope = parser->scope;
17820 if (scope == error_mark_node)
17821 return error_mark_node;
17823 /* Any name names a type if we're following the `typename' keyword
17824 in a qualified name where the enclosing scope is type-dependent. */
17825 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17826 && dependent_type_p (scope));
17827 /* Handle the common case (an identifier, but not a template-id)
17828 efficiently. */
17829 if (token->type == CPP_NAME
17830 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17832 cp_token *identifier_token;
17833 bool ambiguous_p;
17835 /* Look for the identifier. */
17836 identifier_token = cp_lexer_peek_token (parser->lexer);
17837 ambiguous_p = identifier_token->ambiguous_p;
17838 identifier = cp_parser_identifier (parser);
17839 /* If the next token isn't an identifier, we are certainly not
17840 looking at a class-name. */
17841 if (identifier == error_mark_node)
17842 decl = error_mark_node;
17843 /* If we know this is a type-name, there's no need to look it
17844 up. */
17845 else if (typename_p)
17846 decl = identifier;
17847 else
17849 tree ambiguous_decls;
17850 /* If we already know that this lookup is ambiguous, then
17851 we've already issued an error message; there's no reason
17852 to check again. */
17853 if (ambiguous_p)
17855 cp_parser_simulate_error (parser);
17856 return error_mark_node;
17858 /* If the next token is a `::', then the name must be a type
17859 name.
17861 [basic.lookup.qual]
17863 During the lookup for a name preceding the :: scope
17864 resolution operator, object, function, and enumerator
17865 names are ignored. */
17866 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17867 tag_type = typename_type;
17868 /* Look up the name. */
17869 decl = cp_parser_lookup_name (parser, identifier,
17870 tag_type,
17871 /*is_template=*/false,
17872 /*is_namespace=*/false,
17873 check_dependency_p,
17874 &ambiguous_decls,
17875 identifier_token->location);
17876 if (ambiguous_decls)
17878 if (cp_parser_parsing_tentatively (parser))
17879 cp_parser_simulate_error (parser);
17880 return error_mark_node;
17884 else
17886 /* Try a template-id. */
17887 decl = cp_parser_template_id (parser, template_keyword_p,
17888 check_dependency_p,
17889 tag_type,
17890 is_declaration);
17891 if (decl == error_mark_node)
17892 return error_mark_node;
17895 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17897 /* If this is a typename, create a TYPENAME_TYPE. */
17898 if (typename_p && decl != error_mark_node)
17900 decl = make_typename_type (scope, decl, typename_type,
17901 /*complain=*/tf_error);
17902 if (decl != error_mark_node)
17903 decl = TYPE_NAME (decl);
17906 decl = strip_using_decl (decl);
17908 /* Check to see that it is really the name of a class. */
17909 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17910 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17911 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17912 /* Situations like this:
17914 template <typename T> struct A {
17915 typename T::template X<int>::I i;
17918 are problematic. Is `T::template X<int>' a class-name? The
17919 standard does not seem to be definitive, but there is no other
17920 valid interpretation of the following `::'. Therefore, those
17921 names are considered class-names. */
17923 decl = make_typename_type (scope, decl, tag_type, tf_error);
17924 if (decl != error_mark_node)
17925 decl = TYPE_NAME (decl);
17927 else if (TREE_CODE (decl) != TYPE_DECL
17928 || TREE_TYPE (decl) == error_mark_node
17929 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17930 /* In Objective-C 2.0, a classname followed by '.' starts a
17931 dot-syntax expression, and it's not a type-name. */
17932 || (c_dialect_objc ()
17933 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17934 && objc_is_class_name (decl)))
17935 decl = error_mark_node;
17937 if (decl == error_mark_node)
17938 cp_parser_error (parser, "expected class-name");
17939 else if (identifier && !parser->scope)
17940 maybe_note_name_used_in_class (identifier, decl);
17942 return decl;
17945 /* Parse a class-specifier.
17947 class-specifier:
17948 class-head { member-specification [opt] }
17950 Returns the TREE_TYPE representing the class. */
17952 static tree
17953 cp_parser_class_specifier_1 (cp_parser* parser)
17955 tree type;
17956 tree attributes = NULL_TREE;
17957 bool nested_name_specifier_p;
17958 unsigned saved_num_template_parameter_lists;
17959 bool saved_in_function_body;
17960 unsigned char in_statement;
17961 bool in_switch_statement_p;
17962 bool saved_in_unbraced_linkage_specification_p;
17963 tree old_scope = NULL_TREE;
17964 tree scope = NULL_TREE;
17965 cp_token *closing_brace;
17967 push_deferring_access_checks (dk_no_deferred);
17969 /* Parse the class-head. */
17970 type = cp_parser_class_head (parser,
17971 &nested_name_specifier_p);
17972 /* If the class-head was a semantic disaster, skip the entire body
17973 of the class. */
17974 if (!type)
17976 cp_parser_skip_to_end_of_block_or_statement (parser);
17977 pop_deferring_access_checks ();
17978 return error_mark_node;
17981 /* Look for the `{'. */
17982 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17984 pop_deferring_access_checks ();
17985 return error_mark_node;
17988 /* Issue an error message if type-definitions are forbidden here. */
17989 cp_parser_check_type_definition (parser);
17990 /* Remember that we are defining one more class. */
17991 ++parser->num_classes_being_defined;
17992 /* Inside the class, surrounding template-parameter-lists do not
17993 apply. */
17994 saved_num_template_parameter_lists
17995 = parser->num_template_parameter_lists;
17996 parser->num_template_parameter_lists = 0;
17997 /* We are not in a function body. */
17998 saved_in_function_body = parser->in_function_body;
17999 parser->in_function_body = false;
18000 /* Or in a loop. */
18001 in_statement = parser->in_statement;
18002 parser->in_statement = 0;
18003 /* Or in a switch. */
18004 in_switch_statement_p = parser->in_switch_statement_p;
18005 parser->in_switch_statement_p = false;
18006 /* We are not immediately inside an extern "lang" block. */
18007 saved_in_unbraced_linkage_specification_p
18008 = parser->in_unbraced_linkage_specification_p;
18009 parser->in_unbraced_linkage_specification_p = false;
18011 /* Start the class. */
18012 if (nested_name_specifier_p)
18014 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18015 old_scope = push_inner_scope (scope);
18017 type = begin_class_definition (type);
18019 if (type == error_mark_node)
18020 /* If the type is erroneous, skip the entire body of the class. */
18021 cp_parser_skip_to_closing_brace (parser);
18022 else
18023 /* Parse the member-specification. */
18024 cp_parser_member_specification_opt (parser);
18026 /* Look for the trailing `}'. */
18027 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18028 /* Look for trailing attributes to apply to this class. */
18029 if (cp_parser_allow_gnu_extensions_p (parser))
18030 attributes = cp_parser_attributes_opt (parser);
18031 if (type != error_mark_node)
18032 type = finish_struct (type, attributes);
18033 if (nested_name_specifier_p)
18034 pop_inner_scope (old_scope, scope);
18036 /* We've finished a type definition. Check for the common syntax
18037 error of forgetting a semicolon after the definition. We need to
18038 be careful, as we can't just check for not-a-semicolon and be done
18039 with it; the user might have typed:
18041 class X { } c = ...;
18042 class X { } *p = ...;
18044 and so forth. Instead, enumerate all the possible tokens that
18045 might follow this production; if we don't see one of them, then
18046 complain and silently insert the semicolon. */
18048 cp_token *token = cp_lexer_peek_token (parser->lexer);
18049 bool want_semicolon = true;
18051 switch (token->type)
18053 case CPP_NAME:
18054 case CPP_SEMICOLON:
18055 case CPP_MULT:
18056 case CPP_AND:
18057 case CPP_OPEN_PAREN:
18058 case CPP_CLOSE_PAREN:
18059 case CPP_COMMA:
18060 want_semicolon = false;
18061 break;
18063 /* While it's legal for type qualifiers and storage class
18064 specifiers to follow type definitions in the grammar, only
18065 compiler testsuites contain code like that. Assume that if
18066 we see such code, then what we're really seeing is a case
18067 like:
18069 class X { }
18070 const <type> var = ...;
18074 class Y { }
18075 static <type> func (...) ...
18077 i.e. the qualifier or specifier applies to the next
18078 declaration. To do so, however, we need to look ahead one
18079 more token to see if *that* token is a type specifier.
18081 This code could be improved to handle:
18083 class Z { }
18084 static const <type> var = ...; */
18085 case CPP_KEYWORD:
18086 if (keyword_is_decl_specifier (token->keyword))
18088 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18090 /* Handling user-defined types here would be nice, but very
18091 tricky. */
18092 want_semicolon
18093 = (lookahead->type == CPP_KEYWORD
18094 && keyword_begins_type_specifier (lookahead->keyword));
18096 break;
18097 default:
18098 break;
18101 /* If we don't have a type, then something is very wrong and we
18102 shouldn't try to do anything clever. Likewise for not seeing the
18103 closing brace. */
18104 if (closing_brace && TYPE_P (type) && want_semicolon)
18106 cp_token_position prev
18107 = cp_lexer_previous_token_position (parser->lexer);
18108 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18109 location_t loc = prev_token->location;
18111 if (CLASSTYPE_DECLARED_CLASS (type))
18112 error_at (loc, "expected %<;%> after class definition");
18113 else if (TREE_CODE (type) == RECORD_TYPE)
18114 error_at (loc, "expected %<;%> after struct definition");
18115 else if (TREE_CODE (type) == UNION_TYPE)
18116 error_at (loc, "expected %<;%> after union definition");
18117 else
18118 gcc_unreachable ();
18120 /* Unget one token and smash it to look as though we encountered
18121 a semicolon in the input stream. */
18122 cp_lexer_set_token_position (parser->lexer, prev);
18123 token = cp_lexer_peek_token (parser->lexer);
18124 token->type = CPP_SEMICOLON;
18125 token->keyword = RID_MAX;
18129 /* If this class is not itself within the scope of another class,
18130 then we need to parse the bodies of all of the queued function
18131 definitions. Note that the queued functions defined in a class
18132 are not always processed immediately following the
18133 class-specifier for that class. Consider:
18135 struct A {
18136 struct B { void f() { sizeof (A); } };
18139 If `f' were processed before the processing of `A' were
18140 completed, there would be no way to compute the size of `A'.
18141 Note that the nesting we are interested in here is lexical --
18142 not the semantic nesting given by TYPE_CONTEXT. In particular,
18143 for:
18145 struct A { struct B; };
18146 struct A::B { void f() { } };
18148 there is no need to delay the parsing of `A::B::f'. */
18149 if (--parser->num_classes_being_defined == 0)
18151 tree decl;
18152 tree class_type = NULL_TREE;
18153 tree pushed_scope = NULL_TREE;
18154 unsigned ix;
18155 cp_default_arg_entry *e;
18156 tree save_ccp, save_ccr;
18158 /* In a first pass, parse default arguments to the functions.
18159 Then, in a second pass, parse the bodies of the functions.
18160 This two-phased approach handles cases like:
18162 struct S {
18163 void f() { g(); }
18164 void g(int i = 3);
18168 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18169 ix, e)
18171 decl = e->decl;
18172 /* If there are default arguments that have not yet been processed,
18173 take care of them now. */
18174 if (class_type != e->class_type)
18176 if (pushed_scope)
18177 pop_scope (pushed_scope);
18178 class_type = e->class_type;
18179 pushed_scope = push_scope (class_type);
18181 /* Make sure that any template parameters are in scope. */
18182 maybe_begin_member_template_processing (decl);
18183 /* Parse the default argument expressions. */
18184 cp_parser_late_parsing_default_args (parser, decl);
18185 /* Remove any template parameters from the symbol table. */
18186 maybe_end_member_template_processing ();
18188 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18189 /* Now parse any NSDMIs. */
18190 save_ccp = current_class_ptr;
18191 save_ccr = current_class_ref;
18192 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18194 if (class_type != DECL_CONTEXT (decl))
18196 if (pushed_scope)
18197 pop_scope (pushed_scope);
18198 class_type = DECL_CONTEXT (decl);
18199 pushed_scope = push_scope (class_type);
18201 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18202 cp_parser_late_parsing_nsdmi (parser, decl);
18204 VEC_truncate (tree, unparsed_nsdmis, 0);
18205 current_class_ptr = save_ccp;
18206 current_class_ref = save_ccr;
18207 if (pushed_scope)
18208 pop_scope (pushed_scope);
18209 /* Now parse the body of the functions. */
18210 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18211 cp_parser_late_parsing_for_member (parser, decl);
18212 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18215 /* Put back any saved access checks. */
18216 pop_deferring_access_checks ();
18218 /* Restore saved state. */
18219 parser->in_switch_statement_p = in_switch_statement_p;
18220 parser->in_statement = in_statement;
18221 parser->in_function_body = saved_in_function_body;
18222 parser->num_template_parameter_lists
18223 = saved_num_template_parameter_lists;
18224 parser->in_unbraced_linkage_specification_p
18225 = saved_in_unbraced_linkage_specification_p;
18227 return type;
18230 static tree
18231 cp_parser_class_specifier (cp_parser* parser)
18233 tree ret;
18234 timevar_push (TV_PARSE_STRUCT);
18235 ret = cp_parser_class_specifier_1 (parser);
18236 timevar_pop (TV_PARSE_STRUCT);
18237 return ret;
18240 /* Parse a class-head.
18242 class-head:
18243 class-key identifier [opt] base-clause [opt]
18244 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18245 class-key nested-name-specifier [opt] template-id
18246 base-clause [opt]
18248 class-virt-specifier:
18249 final
18251 GNU Extensions:
18252 class-key attributes identifier [opt] base-clause [opt]
18253 class-key attributes nested-name-specifier identifier base-clause [opt]
18254 class-key attributes nested-name-specifier [opt] template-id
18255 base-clause [opt]
18257 Upon return BASES is initialized to the list of base classes (or
18258 NULL, if there are none) in the same form returned by
18259 cp_parser_base_clause.
18261 Returns the TYPE of the indicated class. Sets
18262 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18263 involving a nested-name-specifier was used, and FALSE otherwise.
18265 Returns error_mark_node if this is not a class-head.
18267 Returns NULL_TREE if the class-head is syntactically valid, but
18268 semantically invalid in a way that means we should skip the entire
18269 body of the class. */
18271 static tree
18272 cp_parser_class_head (cp_parser* parser,
18273 bool* nested_name_specifier_p)
18275 tree nested_name_specifier;
18276 enum tag_types class_key;
18277 tree id = NULL_TREE;
18278 tree type = NULL_TREE;
18279 tree attributes;
18280 tree bases;
18281 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18282 bool template_id_p = false;
18283 bool qualified_p = false;
18284 bool invalid_nested_name_p = false;
18285 bool invalid_explicit_specialization_p = false;
18286 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18287 tree pushed_scope = NULL_TREE;
18288 unsigned num_templates;
18289 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18290 /* Assume no nested-name-specifier will be present. */
18291 *nested_name_specifier_p = false;
18292 /* Assume no template parameter lists will be used in defining the
18293 type. */
18294 num_templates = 0;
18295 parser->colon_corrects_to_scope_p = false;
18297 /* Look for the class-key. */
18298 class_key = cp_parser_class_key (parser);
18299 if (class_key == none_type)
18300 return error_mark_node;
18302 /* Parse the attributes. */
18303 attributes = cp_parser_attributes_opt (parser);
18305 /* If the next token is `::', that is invalid -- but sometimes
18306 people do try to write:
18308 struct ::S {};
18310 Handle this gracefully by accepting the extra qualifier, and then
18311 issuing an error about it later if this really is a
18312 class-head. If it turns out just to be an elaborated type
18313 specifier, remain silent. */
18314 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18315 qualified_p = true;
18317 push_deferring_access_checks (dk_no_check);
18319 /* Determine the name of the class. Begin by looking for an
18320 optional nested-name-specifier. */
18321 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18322 nested_name_specifier
18323 = cp_parser_nested_name_specifier_opt (parser,
18324 /*typename_keyword_p=*/false,
18325 /*check_dependency_p=*/false,
18326 /*type_p=*/true,
18327 /*is_declaration=*/false);
18328 /* If there was a nested-name-specifier, then there *must* be an
18329 identifier. */
18330 if (nested_name_specifier)
18332 type_start_token = cp_lexer_peek_token (parser->lexer);
18333 /* Although the grammar says `identifier', it really means
18334 `class-name' or `template-name'. You are only allowed to
18335 define a class that has already been declared with this
18336 syntax.
18338 The proposed resolution for Core Issue 180 says that wherever
18339 you see `class T::X' you should treat `X' as a type-name.
18341 It is OK to define an inaccessible class; for example:
18343 class A { class B; };
18344 class A::B {};
18346 We do not know if we will see a class-name, or a
18347 template-name. We look for a class-name first, in case the
18348 class-name is a template-id; if we looked for the
18349 template-name first we would stop after the template-name. */
18350 cp_parser_parse_tentatively (parser);
18351 type = cp_parser_class_name (parser,
18352 /*typename_keyword_p=*/false,
18353 /*template_keyword_p=*/false,
18354 class_type,
18355 /*check_dependency_p=*/false,
18356 /*class_head_p=*/true,
18357 /*is_declaration=*/false);
18358 /* If that didn't work, ignore the nested-name-specifier. */
18359 if (!cp_parser_parse_definitely (parser))
18361 invalid_nested_name_p = true;
18362 type_start_token = cp_lexer_peek_token (parser->lexer);
18363 id = cp_parser_identifier (parser);
18364 if (id == error_mark_node)
18365 id = NULL_TREE;
18367 /* If we could not find a corresponding TYPE, treat this
18368 declaration like an unqualified declaration. */
18369 if (type == error_mark_node)
18370 nested_name_specifier = NULL_TREE;
18371 /* Otherwise, count the number of templates used in TYPE and its
18372 containing scopes. */
18373 else
18375 tree scope;
18377 for (scope = TREE_TYPE (type);
18378 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18379 scope = (TYPE_P (scope)
18380 ? TYPE_CONTEXT (scope)
18381 : DECL_CONTEXT (scope)))
18382 if (TYPE_P (scope)
18383 && CLASS_TYPE_P (scope)
18384 && CLASSTYPE_TEMPLATE_INFO (scope)
18385 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18386 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18387 ++num_templates;
18390 /* Otherwise, the identifier is optional. */
18391 else
18393 /* We don't know whether what comes next is a template-id,
18394 an identifier, or nothing at all. */
18395 cp_parser_parse_tentatively (parser);
18396 /* Check for a template-id. */
18397 type_start_token = cp_lexer_peek_token (parser->lexer);
18398 id = cp_parser_template_id (parser,
18399 /*template_keyword_p=*/false,
18400 /*check_dependency_p=*/true,
18401 class_key,
18402 /*is_declaration=*/true);
18403 /* If that didn't work, it could still be an identifier. */
18404 if (!cp_parser_parse_definitely (parser))
18406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18408 type_start_token = cp_lexer_peek_token (parser->lexer);
18409 id = cp_parser_identifier (parser);
18411 else
18412 id = NULL_TREE;
18414 else
18416 template_id_p = true;
18417 ++num_templates;
18421 pop_deferring_access_checks ();
18423 if (id)
18425 cp_parser_check_for_invalid_template_id (parser, id,
18426 class_key,
18427 type_start_token->location);
18429 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18431 /* If it's not a `:' or a `{' then we can't really be looking at a
18432 class-head, since a class-head only appears as part of a
18433 class-specifier. We have to detect this situation before calling
18434 xref_tag, since that has irreversible side-effects. */
18435 if (!cp_parser_next_token_starts_class_definition_p (parser))
18437 cp_parser_error (parser, "expected %<{%> or %<:%>");
18438 type = error_mark_node;
18439 goto out;
18442 /* At this point, we're going ahead with the class-specifier, even
18443 if some other problem occurs. */
18444 cp_parser_commit_to_tentative_parse (parser);
18445 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18447 cp_parser_error (parser,
18448 "cannot specify %<override%> for a class");
18449 type = error_mark_node;
18450 goto out;
18452 /* Issue the error about the overly-qualified name now. */
18453 if (qualified_p)
18455 cp_parser_error (parser,
18456 "global qualification of class name is invalid");
18457 type = error_mark_node;
18458 goto out;
18460 else if (invalid_nested_name_p)
18462 cp_parser_error (parser,
18463 "qualified name does not name a class");
18464 type = error_mark_node;
18465 goto out;
18467 else if (nested_name_specifier)
18469 tree scope;
18471 /* Reject typedef-names in class heads. */
18472 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18474 error_at (type_start_token->location,
18475 "invalid class name in declaration of %qD",
18476 type);
18477 type = NULL_TREE;
18478 goto done;
18481 /* Figure out in what scope the declaration is being placed. */
18482 scope = current_scope ();
18483 /* If that scope does not contain the scope in which the
18484 class was originally declared, the program is invalid. */
18485 if (scope && !is_ancestor (scope, nested_name_specifier))
18487 if (at_namespace_scope_p ())
18488 error_at (type_start_token->location,
18489 "declaration of %qD in namespace %qD which does not "
18490 "enclose %qD",
18491 type, scope, nested_name_specifier);
18492 else
18493 error_at (type_start_token->location,
18494 "declaration of %qD in %qD which does not enclose %qD",
18495 type, scope, nested_name_specifier);
18496 type = NULL_TREE;
18497 goto done;
18499 /* [dcl.meaning]
18501 A declarator-id shall not be qualified except for the
18502 definition of a ... nested class outside of its class
18503 ... [or] the definition or explicit instantiation of a
18504 class member of a namespace outside of its namespace. */
18505 if (scope == nested_name_specifier)
18507 permerror (nested_name_specifier_token_start->location,
18508 "extra qualification not allowed");
18509 nested_name_specifier = NULL_TREE;
18510 num_templates = 0;
18513 /* An explicit-specialization must be preceded by "template <>". If
18514 it is not, try to recover gracefully. */
18515 if (at_namespace_scope_p ()
18516 && parser->num_template_parameter_lists == 0
18517 && template_id_p)
18519 error_at (type_start_token->location,
18520 "an explicit specialization must be preceded by %<template <>%>");
18521 invalid_explicit_specialization_p = true;
18522 /* Take the same action that would have been taken by
18523 cp_parser_explicit_specialization. */
18524 ++parser->num_template_parameter_lists;
18525 begin_specialization ();
18527 /* There must be no "return" statements between this point and the
18528 end of this function; set "type "to the correct return value and
18529 use "goto done;" to return. */
18530 /* Make sure that the right number of template parameters were
18531 present. */
18532 if (!cp_parser_check_template_parameters (parser, num_templates,
18533 type_start_token->location,
18534 /*declarator=*/NULL))
18536 /* If something went wrong, there is no point in even trying to
18537 process the class-definition. */
18538 type = NULL_TREE;
18539 goto done;
18542 /* Look up the type. */
18543 if (template_id_p)
18545 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18546 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18547 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18549 error_at (type_start_token->location,
18550 "function template %qD redeclared as a class template", id);
18551 type = error_mark_node;
18553 else
18555 type = TREE_TYPE (id);
18556 type = maybe_process_partial_specialization (type);
18558 if (nested_name_specifier)
18559 pushed_scope = push_scope (nested_name_specifier);
18561 else if (nested_name_specifier)
18563 tree class_type;
18565 /* Given:
18567 template <typename T> struct S { struct T };
18568 template <typename T> struct S<T>::T { };
18570 we will get a TYPENAME_TYPE when processing the definition of
18571 `S::T'. We need to resolve it to the actual type before we
18572 try to define it. */
18573 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18575 class_type = resolve_typename_type (TREE_TYPE (type),
18576 /*only_current_p=*/false);
18577 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18578 type = TYPE_NAME (class_type);
18579 else
18581 cp_parser_error (parser, "could not resolve typename type");
18582 type = error_mark_node;
18586 if (maybe_process_partial_specialization (TREE_TYPE (type))
18587 == error_mark_node)
18589 type = NULL_TREE;
18590 goto done;
18593 class_type = current_class_type;
18594 /* Enter the scope indicated by the nested-name-specifier. */
18595 pushed_scope = push_scope (nested_name_specifier);
18596 /* Get the canonical version of this type. */
18597 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18598 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18599 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18601 type = push_template_decl (type);
18602 if (type == error_mark_node)
18604 type = NULL_TREE;
18605 goto done;
18609 type = TREE_TYPE (type);
18610 *nested_name_specifier_p = true;
18612 else /* The name is not a nested name. */
18614 /* If the class was unnamed, create a dummy name. */
18615 if (!id)
18616 id = make_anon_name ();
18617 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18618 parser->num_template_parameter_lists);
18621 /* Indicate whether this class was declared as a `class' or as a
18622 `struct'. */
18623 if (TREE_CODE (type) == RECORD_TYPE)
18624 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18625 cp_parser_check_class_key (class_key, type);
18627 /* If this type was already complete, and we see another definition,
18628 that's an error. */
18629 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18631 error_at (type_start_token->location, "redefinition of %q#T",
18632 type);
18633 error_at (type_start_token->location, "previous definition of %q+#T",
18634 type);
18635 type = NULL_TREE;
18636 goto done;
18638 else if (type == error_mark_node)
18639 type = NULL_TREE;
18641 if (type)
18643 /* Apply attributes now, before any use of the class as a template
18644 argument in its base list. */
18645 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18646 fixup_attribute_variants (type);
18649 /* We will have entered the scope containing the class; the names of
18650 base classes should be looked up in that context. For example:
18652 struct A { struct B {}; struct C; };
18653 struct A::C : B {};
18655 is valid. */
18657 /* Get the list of base-classes, if there is one. */
18658 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18659 bases = cp_parser_base_clause (parser);
18660 else
18661 bases = NULL_TREE;
18663 /* If we're really defining a class, process the base classes.
18664 If they're invalid, fail. */
18665 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18666 && !xref_basetypes (type, bases))
18667 type = NULL_TREE;
18669 done:
18670 /* Leave the scope given by the nested-name-specifier. We will
18671 enter the class scope itself while processing the members. */
18672 if (pushed_scope)
18673 pop_scope (pushed_scope);
18675 if (invalid_explicit_specialization_p)
18677 end_specialization ();
18678 --parser->num_template_parameter_lists;
18681 if (type)
18682 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18683 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18684 CLASSTYPE_FINAL (type) = 1;
18685 out:
18686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18687 return type;
18690 /* Parse a class-key.
18692 class-key:
18693 class
18694 struct
18695 union
18697 Returns the kind of class-key specified, or none_type to indicate
18698 error. */
18700 static enum tag_types
18701 cp_parser_class_key (cp_parser* parser)
18703 cp_token *token;
18704 enum tag_types tag_type;
18706 /* Look for the class-key. */
18707 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18708 if (!token)
18709 return none_type;
18711 /* Check to see if the TOKEN is a class-key. */
18712 tag_type = cp_parser_token_is_class_key (token);
18713 if (!tag_type)
18714 cp_parser_error (parser, "expected class-key");
18715 return tag_type;
18718 /* Parse an (optional) member-specification.
18720 member-specification:
18721 member-declaration member-specification [opt]
18722 access-specifier : member-specification [opt] */
18724 static void
18725 cp_parser_member_specification_opt (cp_parser* parser)
18727 while (true)
18729 cp_token *token;
18730 enum rid keyword;
18732 /* Peek at the next token. */
18733 token = cp_lexer_peek_token (parser->lexer);
18734 /* If it's a `}', or EOF then we've seen all the members. */
18735 if (token->type == CPP_CLOSE_BRACE
18736 || token->type == CPP_EOF
18737 || token->type == CPP_PRAGMA_EOL)
18738 break;
18740 /* See if this token is a keyword. */
18741 keyword = token->keyword;
18742 switch (keyword)
18744 case RID_PUBLIC:
18745 case RID_PROTECTED:
18746 case RID_PRIVATE:
18747 /* Consume the access-specifier. */
18748 cp_lexer_consume_token (parser->lexer);
18749 /* Remember which access-specifier is active. */
18750 current_access_specifier = token->u.value;
18751 /* Look for the `:'. */
18752 cp_parser_require (parser, CPP_COLON, RT_COLON);
18753 break;
18755 default:
18756 /* Accept #pragmas at class scope. */
18757 if (token->type == CPP_PRAGMA)
18759 cp_parser_pragma (parser, pragma_external);
18760 break;
18763 /* Otherwise, the next construction must be a
18764 member-declaration. */
18765 cp_parser_member_declaration (parser);
18770 /* Parse a member-declaration.
18772 member-declaration:
18773 decl-specifier-seq [opt] member-declarator-list [opt] ;
18774 function-definition ; [opt]
18775 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18776 using-declaration
18777 template-declaration
18778 alias-declaration
18780 member-declarator-list:
18781 member-declarator
18782 member-declarator-list , member-declarator
18784 member-declarator:
18785 declarator pure-specifier [opt]
18786 declarator constant-initializer [opt]
18787 identifier [opt] : constant-expression
18789 GNU Extensions:
18791 member-declaration:
18792 __extension__ member-declaration
18794 member-declarator:
18795 declarator attributes [opt] pure-specifier [opt]
18796 declarator attributes [opt] constant-initializer [opt]
18797 identifier [opt] attributes [opt] : constant-expression
18799 C++0x Extensions:
18801 member-declaration:
18802 static_assert-declaration */
18804 static void
18805 cp_parser_member_declaration (cp_parser* parser)
18807 cp_decl_specifier_seq decl_specifiers;
18808 tree prefix_attributes;
18809 tree decl;
18810 int declares_class_or_enum;
18811 bool friend_p;
18812 cp_token *token = NULL;
18813 cp_token *decl_spec_token_start = NULL;
18814 cp_token *initializer_token_start = NULL;
18815 int saved_pedantic;
18816 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18818 /* Check for the `__extension__' keyword. */
18819 if (cp_parser_extension_opt (parser, &saved_pedantic))
18821 /* Recurse. */
18822 cp_parser_member_declaration (parser);
18823 /* Restore the old value of the PEDANTIC flag. */
18824 pedantic = saved_pedantic;
18826 return;
18829 /* Check for a template-declaration. */
18830 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18832 /* An explicit specialization here is an error condition, and we
18833 expect the specialization handler to detect and report this. */
18834 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18835 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18836 cp_parser_explicit_specialization (parser);
18837 else
18838 cp_parser_template_declaration (parser, /*member_p=*/true);
18840 return;
18843 /* Check for a using-declaration. */
18844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18846 if (cxx_dialect < cxx0x)
18848 /* Parse the using-declaration. */
18849 cp_parser_using_declaration (parser,
18850 /*access_declaration_p=*/false);
18851 return;
18853 else
18855 tree decl;
18856 cp_parser_parse_tentatively (parser);
18857 decl = cp_parser_alias_declaration (parser);
18858 if (cp_parser_parse_definitely (parser))
18859 finish_member_declaration (decl);
18860 else
18861 cp_parser_using_declaration (parser,
18862 /*access_declaration_p=*/false);
18863 return;
18867 /* Check for @defs. */
18868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18870 tree ivar, member;
18871 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18872 ivar = ivar_chains;
18873 while (ivar)
18875 member = ivar;
18876 ivar = TREE_CHAIN (member);
18877 TREE_CHAIN (member) = NULL_TREE;
18878 finish_member_declaration (member);
18880 return;
18883 /* If the next token is `static_assert' we have a static assertion. */
18884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18886 cp_parser_static_assert (parser, /*member_p=*/true);
18887 return;
18890 parser->colon_corrects_to_scope_p = false;
18892 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18893 goto out;
18895 /* Parse the decl-specifier-seq. */
18896 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18897 cp_parser_decl_specifier_seq (parser,
18898 CP_PARSER_FLAGS_OPTIONAL,
18899 &decl_specifiers,
18900 &declares_class_or_enum);
18901 prefix_attributes = decl_specifiers.attributes;
18902 decl_specifiers.attributes = NULL_TREE;
18903 /* Check for an invalid type-name. */
18904 if (!decl_specifiers.any_type_specifiers_p
18905 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18906 goto out;
18907 /* If there is no declarator, then the decl-specifier-seq should
18908 specify a type. */
18909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18911 /* If there was no decl-specifier-seq, and the next token is a
18912 `;', then we have something like:
18914 struct S { ; };
18916 [class.mem]
18918 Each member-declaration shall declare at least one member
18919 name of the class. */
18920 if (!decl_specifiers.any_specifiers_p)
18922 cp_token *token = cp_lexer_peek_token (parser->lexer);
18923 if (!in_system_header_at (token->location))
18924 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18926 else
18928 tree type;
18930 /* See if this declaration is a friend. */
18931 friend_p = cp_parser_friend_p (&decl_specifiers);
18932 /* If there were decl-specifiers, check to see if there was
18933 a class-declaration. */
18934 type = check_tag_decl (&decl_specifiers);
18935 /* Nested classes have already been added to the class, but
18936 a `friend' needs to be explicitly registered. */
18937 if (friend_p)
18939 /* If the `friend' keyword was present, the friend must
18940 be introduced with a class-key. */
18941 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18942 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18943 "in C++03 a class-key must be used "
18944 "when declaring a friend");
18945 /* In this case:
18947 template <typename T> struct A {
18948 friend struct A<T>::B;
18951 A<T>::B will be represented by a TYPENAME_TYPE, and
18952 therefore not recognized by check_tag_decl. */
18953 if (!type)
18955 type = decl_specifiers.type;
18956 if (type && TREE_CODE (type) == TYPE_DECL)
18957 type = TREE_TYPE (type);
18959 if (!type || !TYPE_P (type))
18960 error_at (decl_spec_token_start->location,
18961 "friend declaration does not name a class or "
18962 "function");
18963 else
18964 make_friend_class (current_class_type, type,
18965 /*complain=*/true);
18967 /* If there is no TYPE, an error message will already have
18968 been issued. */
18969 else if (!type || type == error_mark_node)
18971 /* An anonymous aggregate has to be handled specially; such
18972 a declaration really declares a data member (with a
18973 particular type), as opposed to a nested class. */
18974 else if (ANON_AGGR_TYPE_P (type))
18976 /* C++11 9.5/6. */
18977 if (decl_specifiers.storage_class != sc_none)
18978 error_at (decl_spec_token_start->location,
18979 "a storage class on an anonymous aggregate "
18980 "in class scope is not allowed");
18982 /* Remove constructors and such from TYPE, now that we
18983 know it is an anonymous aggregate. */
18984 fixup_anonymous_aggr (type);
18985 /* And make the corresponding data member. */
18986 decl = build_decl (decl_spec_token_start->location,
18987 FIELD_DECL, NULL_TREE, type);
18988 /* Add it to the class. */
18989 finish_member_declaration (decl);
18991 else
18992 cp_parser_check_access_in_redeclaration
18993 (TYPE_NAME (type),
18994 decl_spec_token_start->location);
18997 else
18999 bool assume_semicolon = false;
19001 /* See if these declarations will be friends. */
19002 friend_p = cp_parser_friend_p (&decl_specifiers);
19004 /* Keep going until we hit the `;' at the end of the
19005 declaration. */
19006 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19008 tree attributes = NULL_TREE;
19009 tree first_attribute;
19011 /* Peek at the next token. */
19012 token = cp_lexer_peek_token (parser->lexer);
19014 /* Check for a bitfield declaration. */
19015 if (token->type == CPP_COLON
19016 || (token->type == CPP_NAME
19017 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19018 == CPP_COLON))
19020 tree identifier;
19021 tree width;
19023 /* Get the name of the bitfield. Note that we cannot just
19024 check TOKEN here because it may have been invalidated by
19025 the call to cp_lexer_peek_nth_token above. */
19026 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19027 identifier = cp_parser_identifier (parser);
19028 else
19029 identifier = NULL_TREE;
19031 /* Consume the `:' token. */
19032 cp_lexer_consume_token (parser->lexer);
19033 /* Get the width of the bitfield. */
19034 width
19035 = cp_parser_constant_expression (parser,
19036 /*allow_non_constant=*/false,
19037 NULL);
19039 /* Look for attributes that apply to the bitfield. */
19040 attributes = cp_parser_attributes_opt (parser);
19041 /* Remember which attributes are prefix attributes and
19042 which are not. */
19043 first_attribute = attributes;
19044 /* Combine the attributes. */
19045 attributes = chainon (prefix_attributes, attributes);
19047 /* Create the bitfield declaration. */
19048 decl = grokbitfield (identifier
19049 ? make_id_declarator (NULL_TREE,
19050 identifier,
19051 sfk_none)
19052 : NULL,
19053 &decl_specifiers,
19054 width,
19055 attributes);
19057 else
19059 cp_declarator *declarator;
19060 tree initializer;
19061 tree asm_specification;
19062 int ctor_dtor_or_conv_p;
19064 /* Parse the declarator. */
19065 declarator
19066 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19067 &ctor_dtor_or_conv_p,
19068 /*parenthesized_p=*/NULL,
19069 /*member_p=*/true);
19071 /* If something went wrong parsing the declarator, make sure
19072 that we at least consume some tokens. */
19073 if (declarator == cp_error_declarator)
19075 /* Skip to the end of the statement. */
19076 cp_parser_skip_to_end_of_statement (parser);
19077 /* If the next token is not a semicolon, that is
19078 probably because we just skipped over the body of
19079 a function. So, we consume a semicolon if
19080 present, but do not issue an error message if it
19081 is not present. */
19082 if (cp_lexer_next_token_is (parser->lexer,
19083 CPP_SEMICOLON))
19084 cp_lexer_consume_token (parser->lexer);
19085 goto out;
19088 if (declares_class_or_enum & 2)
19089 cp_parser_check_for_definition_in_return_type
19090 (declarator, decl_specifiers.type,
19091 decl_specifiers.locations[ds_type_spec]);
19093 /* Look for an asm-specification. */
19094 asm_specification = cp_parser_asm_specification_opt (parser);
19095 /* Look for attributes that apply to the declaration. */
19096 attributes = cp_parser_attributes_opt (parser);
19097 /* Remember which attributes are prefix attributes and
19098 which are not. */
19099 first_attribute = attributes;
19100 /* Combine the attributes. */
19101 attributes = chainon (prefix_attributes, attributes);
19103 /* If it's an `=', then we have a constant-initializer or a
19104 pure-specifier. It is not correct to parse the
19105 initializer before registering the member declaration
19106 since the member declaration should be in scope while
19107 its initializer is processed. However, the rest of the
19108 front end does not yet provide an interface that allows
19109 us to handle this correctly. */
19110 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19112 /* In [class.mem]:
19114 A pure-specifier shall be used only in the declaration of
19115 a virtual function.
19117 A member-declarator can contain a constant-initializer
19118 only if it declares a static member of integral or
19119 enumeration type.
19121 Therefore, if the DECLARATOR is for a function, we look
19122 for a pure-specifier; otherwise, we look for a
19123 constant-initializer. When we call `grokfield', it will
19124 perform more stringent semantics checks. */
19125 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19126 if (function_declarator_p (declarator)
19127 || (decl_specifiers.type
19128 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19129 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19130 == FUNCTION_TYPE)))
19131 initializer = cp_parser_pure_specifier (parser);
19132 else if (decl_specifiers.storage_class != sc_static)
19133 initializer = cp_parser_save_nsdmi (parser);
19134 else if (cxx_dialect >= cxx0x)
19136 bool nonconst;
19137 /* Don't require a constant rvalue in C++11, since we
19138 might want a reference constant. We'll enforce
19139 constancy later. */
19140 cp_lexer_consume_token (parser->lexer);
19141 /* Parse the initializer. */
19142 initializer = cp_parser_initializer_clause (parser,
19143 &nonconst);
19145 else
19146 /* Parse the initializer. */
19147 initializer = cp_parser_constant_initializer (parser);
19149 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19150 && !function_declarator_p (declarator))
19152 bool x;
19153 if (decl_specifiers.storage_class != sc_static)
19154 initializer = cp_parser_save_nsdmi (parser);
19155 else
19156 initializer = cp_parser_initializer (parser, &x, &x);
19158 /* Otherwise, there is no initializer. */
19159 else
19160 initializer = NULL_TREE;
19162 /* See if we are probably looking at a function
19163 definition. We are certainly not looking at a
19164 member-declarator. Calling `grokfield' has
19165 side-effects, so we must not do it unless we are sure
19166 that we are looking at a member-declarator. */
19167 if (cp_parser_token_starts_function_definition_p
19168 (cp_lexer_peek_token (parser->lexer)))
19170 /* The grammar does not allow a pure-specifier to be
19171 used when a member function is defined. (It is
19172 possible that this fact is an oversight in the
19173 standard, since a pure function may be defined
19174 outside of the class-specifier. */
19175 if (initializer && initializer_token_start)
19176 error_at (initializer_token_start->location,
19177 "pure-specifier on function-definition");
19178 decl = cp_parser_save_member_function_body (parser,
19179 &decl_specifiers,
19180 declarator,
19181 attributes);
19182 /* If the member was not a friend, declare it here. */
19183 if (!friend_p)
19184 finish_member_declaration (decl);
19185 /* Peek at the next token. */
19186 token = cp_lexer_peek_token (parser->lexer);
19187 /* If the next token is a semicolon, consume it. */
19188 if (token->type == CPP_SEMICOLON)
19189 cp_lexer_consume_token (parser->lexer);
19190 goto out;
19192 else
19193 if (declarator->kind == cdk_function)
19194 declarator->id_loc = token->location;
19195 /* Create the declaration. */
19196 decl = grokfield (declarator, &decl_specifiers,
19197 initializer, /*init_const_expr_p=*/true,
19198 asm_specification,
19199 attributes);
19202 /* Reset PREFIX_ATTRIBUTES. */
19203 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19204 attributes = TREE_CHAIN (attributes);
19205 if (attributes)
19206 TREE_CHAIN (attributes) = NULL_TREE;
19208 /* If there is any qualification still in effect, clear it
19209 now; we will be starting fresh with the next declarator. */
19210 parser->scope = NULL_TREE;
19211 parser->qualifying_scope = NULL_TREE;
19212 parser->object_scope = NULL_TREE;
19213 /* If it's a `,', then there are more declarators. */
19214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19215 cp_lexer_consume_token (parser->lexer);
19216 /* If the next token isn't a `;', then we have a parse error. */
19217 else if (cp_lexer_next_token_is_not (parser->lexer,
19218 CPP_SEMICOLON))
19220 /* The next token might be a ways away from where the
19221 actual semicolon is missing. Find the previous token
19222 and use that for our error position. */
19223 cp_token *token = cp_lexer_previous_token (parser->lexer);
19224 error_at (token->location,
19225 "expected %<;%> at end of member declaration");
19227 /* Assume that the user meant to provide a semicolon. If
19228 we were to cp_parser_skip_to_end_of_statement, we might
19229 skip to a semicolon inside a member function definition
19230 and issue nonsensical error messages. */
19231 assume_semicolon = true;
19234 if (decl)
19236 /* Add DECL to the list of members. */
19237 if (!friend_p)
19238 finish_member_declaration (decl);
19240 if (TREE_CODE (decl) == FUNCTION_DECL)
19241 cp_parser_save_default_args (parser, decl);
19242 else if (TREE_CODE (decl) == FIELD_DECL
19243 && !DECL_C_BIT_FIELD (decl)
19244 && DECL_INITIAL (decl))
19245 /* Add DECL to the queue of NSDMI to be parsed later. */
19246 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19249 if (assume_semicolon)
19250 goto out;
19254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19255 out:
19256 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19259 /* Parse a pure-specifier.
19261 pure-specifier:
19264 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19265 Otherwise, ERROR_MARK_NODE is returned. */
19267 static tree
19268 cp_parser_pure_specifier (cp_parser* parser)
19270 cp_token *token;
19272 /* Look for the `=' token. */
19273 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19274 return error_mark_node;
19275 /* Look for the `0' token. */
19276 token = cp_lexer_peek_token (parser->lexer);
19278 if (token->type == CPP_EOF
19279 || token->type == CPP_PRAGMA_EOL)
19280 return error_mark_node;
19282 cp_lexer_consume_token (parser->lexer);
19284 /* Accept = default or = delete in c++0x mode. */
19285 if (token->keyword == RID_DEFAULT
19286 || token->keyword == RID_DELETE)
19288 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19289 return token->u.value;
19292 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19293 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19295 cp_parser_error (parser,
19296 "invalid pure specifier (only %<= 0%> is allowed)");
19297 cp_parser_skip_to_end_of_statement (parser);
19298 return error_mark_node;
19300 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19302 error_at (token->location, "templates may not be %<virtual%>");
19303 return error_mark_node;
19306 return integer_zero_node;
19309 /* Parse a constant-initializer.
19311 constant-initializer:
19312 = constant-expression
19314 Returns a representation of the constant-expression. */
19316 static tree
19317 cp_parser_constant_initializer (cp_parser* parser)
19319 /* Look for the `=' token. */
19320 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19321 return error_mark_node;
19323 /* It is invalid to write:
19325 struct S { static const int i = { 7 }; };
19328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19330 cp_parser_error (parser,
19331 "a brace-enclosed initializer is not allowed here");
19332 /* Consume the opening brace. */
19333 cp_lexer_consume_token (parser->lexer);
19334 /* Skip the initializer. */
19335 cp_parser_skip_to_closing_brace (parser);
19336 /* Look for the trailing `}'. */
19337 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19339 return error_mark_node;
19342 return cp_parser_constant_expression (parser,
19343 /*allow_non_constant=*/false,
19344 NULL);
19347 /* Derived classes [gram.class.derived] */
19349 /* Parse a base-clause.
19351 base-clause:
19352 : base-specifier-list
19354 base-specifier-list:
19355 base-specifier ... [opt]
19356 base-specifier-list , base-specifier ... [opt]
19358 Returns a TREE_LIST representing the base-classes, in the order in
19359 which they were declared. The representation of each node is as
19360 described by cp_parser_base_specifier.
19362 In the case that no bases are specified, this function will return
19363 NULL_TREE, not ERROR_MARK_NODE. */
19365 static tree
19366 cp_parser_base_clause (cp_parser* parser)
19368 tree bases = NULL_TREE;
19370 /* Look for the `:' that begins the list. */
19371 cp_parser_require (parser, CPP_COLON, RT_COLON);
19373 /* Scan the base-specifier-list. */
19374 while (true)
19376 cp_token *token;
19377 tree base;
19378 bool pack_expansion_p = false;
19380 /* Look for the base-specifier. */
19381 base = cp_parser_base_specifier (parser);
19382 /* Look for the (optional) ellipsis. */
19383 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19385 /* Consume the `...'. */
19386 cp_lexer_consume_token (parser->lexer);
19388 pack_expansion_p = true;
19391 /* Add BASE to the front of the list. */
19392 if (base && base != error_mark_node)
19394 if (pack_expansion_p)
19395 /* Make this a pack expansion type. */
19396 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19398 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19400 TREE_CHAIN (base) = bases;
19401 bases = base;
19404 /* Peek at the next token. */
19405 token = cp_lexer_peek_token (parser->lexer);
19406 /* If it's not a comma, then the list is complete. */
19407 if (token->type != CPP_COMMA)
19408 break;
19409 /* Consume the `,'. */
19410 cp_lexer_consume_token (parser->lexer);
19413 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19414 base class had a qualified name. However, the next name that
19415 appears is certainly not qualified. */
19416 parser->scope = NULL_TREE;
19417 parser->qualifying_scope = NULL_TREE;
19418 parser->object_scope = NULL_TREE;
19420 return nreverse (bases);
19423 /* Parse a base-specifier.
19425 base-specifier:
19426 :: [opt] nested-name-specifier [opt] class-name
19427 virtual access-specifier [opt] :: [opt] nested-name-specifier
19428 [opt] class-name
19429 access-specifier virtual [opt] :: [opt] nested-name-specifier
19430 [opt] class-name
19432 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19433 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19434 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19435 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19437 static tree
19438 cp_parser_base_specifier (cp_parser* parser)
19440 cp_token *token;
19441 bool done = false;
19442 bool virtual_p = false;
19443 bool duplicate_virtual_error_issued_p = false;
19444 bool duplicate_access_error_issued_p = false;
19445 bool class_scope_p, template_p;
19446 tree access = access_default_node;
19447 tree type;
19449 /* Process the optional `virtual' and `access-specifier'. */
19450 while (!done)
19452 /* Peek at the next token. */
19453 token = cp_lexer_peek_token (parser->lexer);
19454 /* Process `virtual'. */
19455 switch (token->keyword)
19457 case RID_VIRTUAL:
19458 /* If `virtual' appears more than once, issue an error. */
19459 if (virtual_p && !duplicate_virtual_error_issued_p)
19461 cp_parser_error (parser,
19462 "%<virtual%> specified more than once in base-specified");
19463 duplicate_virtual_error_issued_p = true;
19466 virtual_p = true;
19468 /* Consume the `virtual' token. */
19469 cp_lexer_consume_token (parser->lexer);
19471 break;
19473 case RID_PUBLIC:
19474 case RID_PROTECTED:
19475 case RID_PRIVATE:
19476 /* If more than one access specifier appears, issue an
19477 error. */
19478 if (access != access_default_node
19479 && !duplicate_access_error_issued_p)
19481 cp_parser_error (parser,
19482 "more than one access specifier in base-specified");
19483 duplicate_access_error_issued_p = true;
19486 access = ridpointers[(int) token->keyword];
19488 /* Consume the access-specifier. */
19489 cp_lexer_consume_token (parser->lexer);
19491 break;
19493 default:
19494 done = true;
19495 break;
19498 /* It is not uncommon to see programs mechanically, erroneously, use
19499 the 'typename' keyword to denote (dependent) qualified types
19500 as base classes. */
19501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19503 token = cp_lexer_peek_token (parser->lexer);
19504 if (!processing_template_decl)
19505 error_at (token->location,
19506 "keyword %<typename%> not allowed outside of templates");
19507 else
19508 error_at (token->location,
19509 "keyword %<typename%> not allowed in this context "
19510 "(the base class is implicitly a type)");
19511 cp_lexer_consume_token (parser->lexer);
19514 /* Look for the optional `::' operator. */
19515 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19516 /* Look for the nested-name-specifier. The simplest way to
19517 implement:
19519 [temp.res]
19521 The keyword `typename' is not permitted in a base-specifier or
19522 mem-initializer; in these contexts a qualified name that
19523 depends on a template-parameter is implicitly assumed to be a
19524 type name.
19526 is to pretend that we have seen the `typename' keyword at this
19527 point. */
19528 cp_parser_nested_name_specifier_opt (parser,
19529 /*typename_keyword_p=*/true,
19530 /*check_dependency_p=*/true,
19531 typename_type,
19532 /*is_declaration=*/true);
19533 /* If the base class is given by a qualified name, assume that names
19534 we see are type names or templates, as appropriate. */
19535 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19536 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19538 if (!parser->scope
19539 && cp_lexer_next_token_is_decltype (parser->lexer))
19540 /* DR 950 allows decltype as a base-specifier. */
19541 type = cp_parser_decltype (parser);
19542 else
19544 /* Otherwise, look for the class-name. */
19545 type = cp_parser_class_name (parser,
19546 class_scope_p,
19547 template_p,
19548 typename_type,
19549 /*check_dependency_p=*/true,
19550 /*class_head_p=*/false,
19551 /*is_declaration=*/true);
19552 type = TREE_TYPE (type);
19555 if (type == error_mark_node)
19556 return error_mark_node;
19558 return finish_base_specifier (type, access, virtual_p);
19561 /* Exception handling [gram.exception] */
19563 /* Parse an (optional) noexcept-specification.
19565 noexcept-specification:
19566 noexcept ( constant-expression ) [opt]
19568 If no noexcept-specification is present, returns NULL_TREE.
19569 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19570 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19571 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19572 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19573 in which case a boolean condition is returned instead. */
19575 static tree
19576 cp_parser_noexcept_specification_opt (cp_parser* parser,
19577 bool require_constexpr,
19578 bool* consumed_expr,
19579 bool return_cond)
19581 cp_token *token;
19582 const char *saved_message;
19584 /* Peek at the next token. */
19585 token = cp_lexer_peek_token (parser->lexer);
19587 /* Is it a noexcept-specification? */
19588 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19590 tree expr;
19591 cp_lexer_consume_token (parser->lexer);
19593 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19595 cp_lexer_consume_token (parser->lexer);
19597 if (require_constexpr)
19599 /* Types may not be defined in an exception-specification. */
19600 saved_message = parser->type_definition_forbidden_message;
19601 parser->type_definition_forbidden_message
19602 = G_("types may not be defined in an exception-specification");
19604 expr = cp_parser_constant_expression (parser, false, NULL);
19606 /* Restore the saved message. */
19607 parser->type_definition_forbidden_message = saved_message;
19609 else
19611 expr = cp_parser_expression (parser, false, NULL);
19612 *consumed_expr = true;
19615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19617 else
19619 expr = boolean_true_node;
19620 if (!require_constexpr)
19621 *consumed_expr = false;
19624 /* We cannot build a noexcept-spec right away because this will check
19625 that expr is a constexpr. */
19626 if (!return_cond)
19627 return build_noexcept_spec (expr, tf_warning_or_error);
19628 else
19629 return expr;
19631 else
19632 return NULL_TREE;
19635 /* Parse an (optional) exception-specification.
19637 exception-specification:
19638 throw ( type-id-list [opt] )
19640 Returns a TREE_LIST representing the exception-specification. The
19641 TREE_VALUE of each node is a type. */
19643 static tree
19644 cp_parser_exception_specification_opt (cp_parser* parser)
19646 cp_token *token;
19647 tree type_id_list;
19648 const char *saved_message;
19650 /* Peek at the next token. */
19651 token = cp_lexer_peek_token (parser->lexer);
19653 /* Is it a noexcept-specification? */
19654 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19655 false);
19656 if (type_id_list != NULL_TREE)
19657 return type_id_list;
19659 /* If it's not `throw', then there's no exception-specification. */
19660 if (!cp_parser_is_keyword (token, RID_THROW))
19661 return NULL_TREE;
19663 #if 0
19664 /* Enable this once a lot of code has transitioned to noexcept? */
19665 if (cxx_dialect >= cxx0x && !in_system_header)
19666 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19667 "deprecated in C++0x; use %<noexcept%> instead");
19668 #endif
19670 /* Consume the `throw'. */
19671 cp_lexer_consume_token (parser->lexer);
19673 /* Look for the `('. */
19674 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19676 /* Peek at the next token. */
19677 token = cp_lexer_peek_token (parser->lexer);
19678 /* If it's not a `)', then there is a type-id-list. */
19679 if (token->type != CPP_CLOSE_PAREN)
19681 /* Types may not be defined in an exception-specification. */
19682 saved_message = parser->type_definition_forbidden_message;
19683 parser->type_definition_forbidden_message
19684 = G_("types may not be defined in an exception-specification");
19685 /* Parse the type-id-list. */
19686 type_id_list = cp_parser_type_id_list (parser);
19687 /* Restore the saved message. */
19688 parser->type_definition_forbidden_message = saved_message;
19690 else
19691 type_id_list = empty_except_spec;
19693 /* Look for the `)'. */
19694 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19696 return type_id_list;
19699 /* Parse an (optional) type-id-list.
19701 type-id-list:
19702 type-id ... [opt]
19703 type-id-list , type-id ... [opt]
19705 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19706 in the order that the types were presented. */
19708 static tree
19709 cp_parser_type_id_list (cp_parser* parser)
19711 tree types = NULL_TREE;
19713 while (true)
19715 cp_token *token;
19716 tree type;
19718 /* Get the next type-id. */
19719 type = cp_parser_type_id (parser);
19720 /* Parse the optional ellipsis. */
19721 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19723 /* Consume the `...'. */
19724 cp_lexer_consume_token (parser->lexer);
19726 /* Turn the type into a pack expansion expression. */
19727 type = make_pack_expansion (type);
19729 /* Add it to the list. */
19730 types = add_exception_specifier (types, type, /*complain=*/1);
19731 /* Peek at the next token. */
19732 token = cp_lexer_peek_token (parser->lexer);
19733 /* If it is not a `,', we are done. */
19734 if (token->type != CPP_COMMA)
19735 break;
19736 /* Consume the `,'. */
19737 cp_lexer_consume_token (parser->lexer);
19740 return nreverse (types);
19743 /* Parse a try-block.
19745 try-block:
19746 try compound-statement handler-seq */
19748 static tree
19749 cp_parser_try_block (cp_parser* parser)
19751 tree try_block;
19753 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19754 try_block = begin_try_block ();
19755 cp_parser_compound_statement (parser, NULL, true, false);
19756 finish_try_block (try_block);
19757 cp_parser_handler_seq (parser);
19758 finish_handler_sequence (try_block);
19760 return try_block;
19763 /* Parse a function-try-block.
19765 function-try-block:
19766 try ctor-initializer [opt] function-body handler-seq */
19768 static bool
19769 cp_parser_function_try_block (cp_parser* parser)
19771 tree compound_stmt;
19772 tree try_block;
19773 bool ctor_initializer_p;
19775 /* Look for the `try' keyword. */
19776 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19777 return false;
19778 /* Let the rest of the front end know where we are. */
19779 try_block = begin_function_try_block (&compound_stmt);
19780 /* Parse the function-body. */
19781 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19782 (parser, /*in_function_try_block=*/true);
19783 /* We're done with the `try' part. */
19784 finish_function_try_block (try_block);
19785 /* Parse the handlers. */
19786 cp_parser_handler_seq (parser);
19787 /* We're done with the handlers. */
19788 finish_function_handler_sequence (try_block, compound_stmt);
19790 return ctor_initializer_p;
19793 /* Parse a handler-seq.
19795 handler-seq:
19796 handler handler-seq [opt] */
19798 static void
19799 cp_parser_handler_seq (cp_parser* parser)
19801 while (true)
19803 cp_token *token;
19805 /* Parse the handler. */
19806 cp_parser_handler (parser);
19807 /* Peek at the next token. */
19808 token = cp_lexer_peek_token (parser->lexer);
19809 /* If it's not `catch' then there are no more handlers. */
19810 if (!cp_parser_is_keyword (token, RID_CATCH))
19811 break;
19815 /* Parse a handler.
19817 handler:
19818 catch ( exception-declaration ) compound-statement */
19820 static void
19821 cp_parser_handler (cp_parser* parser)
19823 tree handler;
19824 tree declaration;
19826 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19827 handler = begin_handler ();
19828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19829 declaration = cp_parser_exception_declaration (parser);
19830 finish_handler_parms (declaration, handler);
19831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19832 cp_parser_compound_statement (parser, NULL, false, false);
19833 finish_handler (handler);
19836 /* Parse an exception-declaration.
19838 exception-declaration:
19839 type-specifier-seq declarator
19840 type-specifier-seq abstract-declarator
19841 type-specifier-seq
19844 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19845 ellipsis variant is used. */
19847 static tree
19848 cp_parser_exception_declaration (cp_parser* parser)
19850 cp_decl_specifier_seq type_specifiers;
19851 cp_declarator *declarator;
19852 const char *saved_message;
19854 /* If it's an ellipsis, it's easy to handle. */
19855 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19857 /* Consume the `...' token. */
19858 cp_lexer_consume_token (parser->lexer);
19859 return NULL_TREE;
19862 /* Types may not be defined in exception-declarations. */
19863 saved_message = parser->type_definition_forbidden_message;
19864 parser->type_definition_forbidden_message
19865 = G_("types may not be defined in exception-declarations");
19867 /* Parse the type-specifier-seq. */
19868 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19869 /*is_trailing_return=*/false,
19870 &type_specifiers);
19871 /* If it's a `)', then there is no declarator. */
19872 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19873 declarator = NULL;
19874 else
19875 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19876 /*ctor_dtor_or_conv_p=*/NULL,
19877 /*parenthesized_p=*/NULL,
19878 /*member_p=*/false);
19880 /* Restore the saved message. */
19881 parser->type_definition_forbidden_message = saved_message;
19883 if (!type_specifiers.any_specifiers_p)
19884 return error_mark_node;
19886 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19889 /* Parse a throw-expression.
19891 throw-expression:
19892 throw assignment-expression [opt]
19894 Returns a THROW_EXPR representing the throw-expression. */
19896 static tree
19897 cp_parser_throw_expression (cp_parser* parser)
19899 tree expression;
19900 cp_token* token;
19902 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19903 token = cp_lexer_peek_token (parser->lexer);
19904 /* Figure out whether or not there is an assignment-expression
19905 following the "throw" keyword. */
19906 if (token->type == CPP_COMMA
19907 || token->type == CPP_SEMICOLON
19908 || token->type == CPP_CLOSE_PAREN
19909 || token->type == CPP_CLOSE_SQUARE
19910 || token->type == CPP_CLOSE_BRACE
19911 || token->type == CPP_COLON)
19912 expression = NULL_TREE;
19913 else
19914 expression = cp_parser_assignment_expression (parser,
19915 /*cast_p=*/false, NULL);
19917 return build_throw (expression);
19920 /* GNU Extensions */
19922 /* Parse an (optional) asm-specification.
19924 asm-specification:
19925 asm ( string-literal )
19927 If the asm-specification is present, returns a STRING_CST
19928 corresponding to the string-literal. Otherwise, returns
19929 NULL_TREE. */
19931 static tree
19932 cp_parser_asm_specification_opt (cp_parser* parser)
19934 cp_token *token;
19935 tree asm_specification;
19937 /* Peek at the next token. */
19938 token = cp_lexer_peek_token (parser->lexer);
19939 /* If the next token isn't the `asm' keyword, then there's no
19940 asm-specification. */
19941 if (!cp_parser_is_keyword (token, RID_ASM))
19942 return NULL_TREE;
19944 /* Consume the `asm' token. */
19945 cp_lexer_consume_token (parser->lexer);
19946 /* Look for the `('. */
19947 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19949 /* Look for the string-literal. */
19950 asm_specification = cp_parser_string_literal (parser, false, false);
19952 /* Look for the `)'. */
19953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19955 return asm_specification;
19958 /* Parse an asm-operand-list.
19960 asm-operand-list:
19961 asm-operand
19962 asm-operand-list , asm-operand
19964 asm-operand:
19965 string-literal ( expression )
19966 [ string-literal ] string-literal ( expression )
19968 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19969 each node is the expression. The TREE_PURPOSE is itself a
19970 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19971 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19972 is a STRING_CST for the string literal before the parenthesis. Returns
19973 ERROR_MARK_NODE if any of the operands are invalid. */
19975 static tree
19976 cp_parser_asm_operand_list (cp_parser* parser)
19978 tree asm_operands = NULL_TREE;
19979 bool invalid_operands = false;
19981 while (true)
19983 tree string_literal;
19984 tree expression;
19985 tree name;
19987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19989 /* Consume the `[' token. */
19990 cp_lexer_consume_token (parser->lexer);
19991 /* Read the operand name. */
19992 name = cp_parser_identifier (parser);
19993 if (name != error_mark_node)
19994 name = build_string (IDENTIFIER_LENGTH (name),
19995 IDENTIFIER_POINTER (name));
19996 /* Look for the closing `]'. */
19997 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19999 else
20000 name = NULL_TREE;
20001 /* Look for the string-literal. */
20002 string_literal = cp_parser_string_literal (parser, false, false);
20004 /* Look for the `('. */
20005 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20006 /* Parse the expression. */
20007 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20008 /* Look for the `)'. */
20009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20011 if (name == error_mark_node
20012 || string_literal == error_mark_node
20013 || expression == error_mark_node)
20014 invalid_operands = true;
20016 /* Add this operand to the list. */
20017 asm_operands = tree_cons (build_tree_list (name, string_literal),
20018 expression,
20019 asm_operands);
20020 /* If the next token is not a `,', there are no more
20021 operands. */
20022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20023 break;
20024 /* Consume the `,'. */
20025 cp_lexer_consume_token (parser->lexer);
20028 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20031 /* Parse an asm-clobber-list.
20033 asm-clobber-list:
20034 string-literal
20035 asm-clobber-list , string-literal
20037 Returns a TREE_LIST, indicating the clobbers in the order that they
20038 appeared. The TREE_VALUE of each node is a STRING_CST. */
20040 static tree
20041 cp_parser_asm_clobber_list (cp_parser* parser)
20043 tree clobbers = NULL_TREE;
20045 while (true)
20047 tree string_literal;
20049 /* Look for the string literal. */
20050 string_literal = cp_parser_string_literal (parser, false, false);
20051 /* Add it to the list. */
20052 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20053 /* If the next token is not a `,', then the list is
20054 complete. */
20055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20056 break;
20057 /* Consume the `,' token. */
20058 cp_lexer_consume_token (parser->lexer);
20061 return clobbers;
20064 /* Parse an asm-label-list.
20066 asm-label-list:
20067 identifier
20068 asm-label-list , identifier
20070 Returns a TREE_LIST, indicating the labels in the order that they
20071 appeared. The TREE_VALUE of each node is a label. */
20073 static tree
20074 cp_parser_asm_label_list (cp_parser* parser)
20076 tree labels = NULL_TREE;
20078 while (true)
20080 tree identifier, label, name;
20082 /* Look for the identifier. */
20083 identifier = cp_parser_identifier (parser);
20084 if (!error_operand_p (identifier))
20086 label = lookup_label (identifier);
20087 if (TREE_CODE (label) == LABEL_DECL)
20089 TREE_USED (label) = 1;
20090 check_goto (label);
20091 name = build_string (IDENTIFIER_LENGTH (identifier),
20092 IDENTIFIER_POINTER (identifier));
20093 labels = tree_cons (name, label, labels);
20096 /* If the next token is not a `,', then the list is
20097 complete. */
20098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20099 break;
20100 /* Consume the `,' token. */
20101 cp_lexer_consume_token (parser->lexer);
20104 return nreverse (labels);
20107 /* Parse an (optional) series of attributes.
20109 attributes:
20110 attributes attribute
20112 attribute:
20113 __attribute__ (( attribute-list [opt] ))
20115 The return value is as for cp_parser_attribute_list. */
20117 static tree
20118 cp_parser_attributes_opt (cp_parser* parser)
20120 tree attributes = NULL_TREE;
20122 while (true)
20124 cp_token *token;
20125 tree attribute_list;
20126 bool ok = true;
20128 /* Peek at the next token. */
20129 token = cp_lexer_peek_token (parser->lexer);
20130 /* If it's not `__attribute__', then we're done. */
20131 if (token->keyword != RID_ATTRIBUTE)
20132 break;
20134 /* Consume the `__attribute__' keyword. */
20135 cp_lexer_consume_token (parser->lexer);
20136 /* Look for the two `(' tokens. */
20137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20138 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20140 /* Peek at the next token. */
20141 token = cp_lexer_peek_token (parser->lexer);
20142 if (token->type != CPP_CLOSE_PAREN)
20143 /* Parse the attribute-list. */
20144 attribute_list = cp_parser_attribute_list (parser);
20145 else
20146 /* If the next token is a `)', then there is no attribute
20147 list. */
20148 attribute_list = NULL;
20150 /* Look for the two `)' tokens. */
20151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20152 ok = false;
20153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20154 ok = false;
20155 if (!ok)
20156 cp_parser_skip_to_end_of_statement (parser);
20158 /* Add these new attributes to the list. */
20159 attributes = chainon (attributes, attribute_list);
20162 return attributes;
20165 /* Parse an attribute-list.
20167 attribute-list:
20168 attribute
20169 attribute-list , attribute
20171 attribute:
20172 identifier
20173 identifier ( identifier )
20174 identifier ( identifier , expression-list )
20175 identifier ( expression-list )
20177 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20178 to an attribute. The TREE_PURPOSE of each node is the identifier
20179 indicating which attribute is in use. The TREE_VALUE represents
20180 the arguments, if any. */
20182 static tree
20183 cp_parser_attribute_list (cp_parser* parser)
20185 tree attribute_list = NULL_TREE;
20186 bool save_translate_strings_p = parser->translate_strings_p;
20188 parser->translate_strings_p = false;
20189 while (true)
20191 cp_token *token;
20192 tree identifier;
20193 tree attribute;
20195 /* Look for the identifier. We also allow keywords here; for
20196 example `__attribute__ ((const))' is legal. */
20197 token = cp_lexer_peek_token (parser->lexer);
20198 if (token->type == CPP_NAME
20199 || token->type == CPP_KEYWORD)
20201 tree arguments = NULL_TREE;
20203 /* Consume the token. */
20204 token = cp_lexer_consume_token (parser->lexer);
20206 /* Save away the identifier that indicates which attribute
20207 this is. */
20208 identifier = (token->type == CPP_KEYWORD)
20209 /* For keywords, use the canonical spelling, not the
20210 parsed identifier. */
20211 ? ridpointers[(int) token->keyword]
20212 : token->u.value;
20214 attribute = build_tree_list (identifier, NULL_TREE);
20216 /* Peek at the next token. */
20217 token = cp_lexer_peek_token (parser->lexer);
20218 /* If it's an `(', then parse the attribute arguments. */
20219 if (token->type == CPP_OPEN_PAREN)
20221 VEC(tree,gc) *vec;
20222 int attr_flag = (attribute_takes_identifier_p (identifier)
20223 ? id_attr : normal_attr);
20224 vec = cp_parser_parenthesized_expression_list
20225 (parser, attr_flag, /*cast_p=*/false,
20226 /*allow_expansion_p=*/false,
20227 /*non_constant_p=*/NULL);
20228 if (vec == NULL)
20229 arguments = error_mark_node;
20230 else
20232 arguments = build_tree_list_vec (vec);
20233 release_tree_vector (vec);
20235 /* Save the arguments away. */
20236 TREE_VALUE (attribute) = arguments;
20239 if (arguments != error_mark_node)
20241 /* Add this attribute to the list. */
20242 TREE_CHAIN (attribute) = attribute_list;
20243 attribute_list = attribute;
20246 token = cp_lexer_peek_token (parser->lexer);
20248 /* Now, look for more attributes. If the next token isn't a
20249 `,', we're done. */
20250 if (token->type != CPP_COMMA)
20251 break;
20253 /* Consume the comma and keep going. */
20254 cp_lexer_consume_token (parser->lexer);
20256 parser->translate_strings_p = save_translate_strings_p;
20258 /* We built up the list in reverse order. */
20259 return nreverse (attribute_list);
20262 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20263 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20264 current value of the PEDANTIC flag, regardless of whether or not
20265 the `__extension__' keyword is present. The caller is responsible
20266 for restoring the value of the PEDANTIC flag. */
20268 static bool
20269 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20271 /* Save the old value of the PEDANTIC flag. */
20272 *saved_pedantic = pedantic;
20274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20276 /* Consume the `__extension__' token. */
20277 cp_lexer_consume_token (parser->lexer);
20278 /* We're not being pedantic while the `__extension__' keyword is
20279 in effect. */
20280 pedantic = 0;
20282 return true;
20285 return false;
20288 /* Parse a label declaration.
20290 label-declaration:
20291 __label__ label-declarator-seq ;
20293 label-declarator-seq:
20294 identifier , label-declarator-seq
20295 identifier */
20297 static void
20298 cp_parser_label_declaration (cp_parser* parser)
20300 /* Look for the `__label__' keyword. */
20301 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20303 while (true)
20305 tree identifier;
20307 /* Look for an identifier. */
20308 identifier = cp_parser_identifier (parser);
20309 /* If we failed, stop. */
20310 if (identifier == error_mark_node)
20311 break;
20312 /* Declare it as a label. */
20313 finish_label_decl (identifier);
20314 /* If the next token is a `;', stop. */
20315 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20316 break;
20317 /* Look for the `,' separating the label declarations. */
20318 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20321 /* Look for the final `;'. */
20322 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20325 /* Support Functions */
20327 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20328 NAME should have one of the representations used for an
20329 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20330 is returned. If PARSER->SCOPE is a dependent type, then a
20331 SCOPE_REF is returned.
20333 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20334 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20335 was formed. Abstractly, such entities should not be passed to this
20336 function, because they do not need to be looked up, but it is
20337 simpler to check for this special case here, rather than at the
20338 call-sites.
20340 In cases not explicitly covered above, this function returns a
20341 DECL, OVERLOAD, or baselink representing the result of the lookup.
20342 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20343 is returned.
20345 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20346 (e.g., "struct") that was used. In that case bindings that do not
20347 refer to types are ignored.
20349 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20350 ignored.
20352 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20353 are ignored.
20355 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20356 types.
20358 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20359 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20360 NULL_TREE otherwise. */
20362 static tree
20363 cp_parser_lookup_name (cp_parser *parser, tree name,
20364 enum tag_types tag_type,
20365 bool is_template,
20366 bool is_namespace,
20367 bool check_dependency,
20368 tree *ambiguous_decls,
20369 location_t name_location)
20371 tree decl;
20372 tree object_type = parser->context->object_type;
20374 /* Assume that the lookup will be unambiguous. */
20375 if (ambiguous_decls)
20376 *ambiguous_decls = NULL_TREE;
20378 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20379 no longer valid. Note that if we are parsing tentatively, and
20380 the parse fails, OBJECT_TYPE will be automatically restored. */
20381 parser->context->object_type = NULL_TREE;
20383 if (name == error_mark_node)
20384 return error_mark_node;
20386 /* A template-id has already been resolved; there is no lookup to
20387 do. */
20388 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20389 return name;
20390 if (BASELINK_P (name))
20392 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20393 == TEMPLATE_ID_EXPR);
20394 return name;
20397 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20398 it should already have been checked to make sure that the name
20399 used matches the type being destroyed. */
20400 if (TREE_CODE (name) == BIT_NOT_EXPR)
20402 tree type;
20404 /* Figure out to which type this destructor applies. */
20405 if (parser->scope)
20406 type = parser->scope;
20407 else if (object_type)
20408 type = object_type;
20409 else
20410 type = current_class_type;
20411 /* If that's not a class type, there is no destructor. */
20412 if (!type || !CLASS_TYPE_P (type))
20413 return error_mark_node;
20414 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20415 lazily_declare_fn (sfk_destructor, type);
20416 if (!CLASSTYPE_DESTRUCTORS (type))
20417 return error_mark_node;
20418 /* If it was a class type, return the destructor. */
20419 return CLASSTYPE_DESTRUCTORS (type);
20422 /* By this point, the NAME should be an ordinary identifier. If
20423 the id-expression was a qualified name, the qualifying scope is
20424 stored in PARSER->SCOPE at this point. */
20425 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20427 /* Perform the lookup. */
20428 if (parser->scope)
20430 bool dependent_p;
20432 if (parser->scope == error_mark_node)
20433 return error_mark_node;
20435 /* If the SCOPE is dependent, the lookup must be deferred until
20436 the template is instantiated -- unless we are explicitly
20437 looking up names in uninstantiated templates. Even then, we
20438 cannot look up the name if the scope is not a class type; it
20439 might, for example, be a template type parameter. */
20440 dependent_p = (TYPE_P (parser->scope)
20441 && dependent_scope_p (parser->scope));
20442 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20443 && dependent_p)
20444 /* Defer lookup. */
20445 decl = error_mark_node;
20446 else
20448 tree pushed_scope = NULL_TREE;
20450 /* If PARSER->SCOPE is a dependent type, then it must be a
20451 class type, and we must not be checking dependencies;
20452 otherwise, we would have processed this lookup above. So
20453 that PARSER->SCOPE is not considered a dependent base by
20454 lookup_member, we must enter the scope here. */
20455 if (dependent_p)
20456 pushed_scope = push_scope (parser->scope);
20458 /* If the PARSER->SCOPE is a template specialization, it
20459 may be instantiated during name lookup. In that case,
20460 errors may be issued. Even if we rollback the current
20461 tentative parse, those errors are valid. */
20462 decl = lookup_qualified_name (parser->scope, name,
20463 tag_type != none_type,
20464 /*complain=*/true);
20466 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20467 lookup result and the nested-name-specifier nominates a class C:
20468 * if the name specified after the nested-name-specifier, when
20469 looked up in C, is the injected-class-name of C (Clause 9), or
20470 * if the name specified after the nested-name-specifier is the
20471 same as the identifier or the simple-template-id's template-
20472 name in the last component of the nested-name-specifier,
20473 the name is instead considered to name the constructor of
20474 class C. [ Note: for example, the constructor is not an
20475 acceptable lookup result in an elaborated-type-specifier so
20476 the constructor would not be used in place of the
20477 injected-class-name. --end note ] Such a constructor name
20478 shall be used only in the declarator-id of a declaration that
20479 names a constructor or in a using-declaration. */
20480 if (tag_type == none_type
20481 && DECL_SELF_REFERENCE_P (decl)
20482 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20483 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20484 tag_type != none_type,
20485 /*complain=*/true);
20487 /* If we have a single function from a using decl, pull it out. */
20488 if (TREE_CODE (decl) == OVERLOAD
20489 && !really_overloaded_fn (decl))
20490 decl = OVL_FUNCTION (decl);
20492 if (pushed_scope)
20493 pop_scope (pushed_scope);
20496 /* If the scope is a dependent type and either we deferred lookup or
20497 we did lookup but didn't find the name, rememeber the name. */
20498 if (decl == error_mark_node && TYPE_P (parser->scope)
20499 && dependent_type_p (parser->scope))
20501 if (tag_type)
20503 tree type;
20505 /* The resolution to Core Issue 180 says that `struct
20506 A::B' should be considered a type-name, even if `A'
20507 is dependent. */
20508 type = make_typename_type (parser->scope, name, tag_type,
20509 /*complain=*/tf_error);
20510 decl = TYPE_NAME (type);
20512 else if (is_template
20513 && (cp_parser_next_token_ends_template_argument_p (parser)
20514 || cp_lexer_next_token_is (parser->lexer,
20515 CPP_CLOSE_PAREN)))
20516 decl = make_unbound_class_template (parser->scope,
20517 name, NULL_TREE,
20518 /*complain=*/tf_error);
20519 else
20520 decl = build_qualified_name (/*type=*/NULL_TREE,
20521 parser->scope, name,
20522 is_template);
20524 parser->qualifying_scope = parser->scope;
20525 parser->object_scope = NULL_TREE;
20527 else if (object_type)
20529 tree object_decl = NULL_TREE;
20530 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20531 OBJECT_TYPE is not a class. */
20532 if (CLASS_TYPE_P (object_type))
20533 /* If the OBJECT_TYPE is a template specialization, it may
20534 be instantiated during name lookup. In that case, errors
20535 may be issued. Even if we rollback the current tentative
20536 parse, those errors are valid. */
20537 object_decl = lookup_member (object_type,
20538 name,
20539 /*protect=*/0,
20540 tag_type != none_type,
20541 tf_warning_or_error);
20542 /* Look it up in the enclosing context, too. */
20543 decl = lookup_name_real (name, tag_type != none_type,
20544 /*nonclass=*/0,
20545 /*block_p=*/true, is_namespace, 0);
20546 parser->object_scope = object_type;
20547 parser->qualifying_scope = NULL_TREE;
20548 if (object_decl)
20549 decl = object_decl;
20551 else
20553 decl = lookup_name_real (name, tag_type != none_type,
20554 /*nonclass=*/0,
20555 /*block_p=*/true, is_namespace, 0);
20556 parser->qualifying_scope = NULL_TREE;
20557 parser->object_scope = NULL_TREE;
20560 /* If the lookup failed, let our caller know. */
20561 if (!decl || decl == error_mark_node)
20562 return error_mark_node;
20564 /* Pull out the template from an injected-class-name (or multiple). */
20565 if (is_template)
20566 decl = maybe_get_template_decl_from_type_decl (decl);
20568 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20569 if (TREE_CODE (decl) == TREE_LIST)
20571 if (ambiguous_decls)
20572 *ambiguous_decls = decl;
20573 /* The error message we have to print is too complicated for
20574 cp_parser_error, so we incorporate its actions directly. */
20575 if (!cp_parser_simulate_error (parser))
20577 error_at (name_location, "reference to %qD is ambiguous",
20578 name);
20579 print_candidates (decl);
20581 return error_mark_node;
20584 gcc_assert (DECL_P (decl)
20585 || TREE_CODE (decl) == OVERLOAD
20586 || TREE_CODE (decl) == SCOPE_REF
20587 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20588 || BASELINK_P (decl));
20590 /* If we have resolved the name of a member declaration, check to
20591 see if the declaration is accessible. When the name resolves to
20592 set of overloaded functions, accessibility is checked when
20593 overload resolution is done.
20595 During an explicit instantiation, access is not checked at all,
20596 as per [temp.explicit]. */
20597 if (DECL_P (decl))
20598 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20600 maybe_record_typedef_use (decl);
20602 return decl;
20605 /* Like cp_parser_lookup_name, but for use in the typical case where
20606 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20607 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20609 static tree
20610 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20612 return cp_parser_lookup_name (parser, name,
20613 none_type,
20614 /*is_template=*/false,
20615 /*is_namespace=*/false,
20616 /*check_dependency=*/true,
20617 /*ambiguous_decls=*/NULL,
20618 location);
20621 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20622 the current context, return the TYPE_DECL. If TAG_NAME_P is
20623 true, the DECL indicates the class being defined in a class-head,
20624 or declared in an elaborated-type-specifier.
20626 Otherwise, return DECL. */
20628 static tree
20629 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20631 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20632 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20634 struct A {
20635 template <typename T> struct B;
20638 template <typename T> struct A::B {};
20640 Similarly, in an elaborated-type-specifier:
20642 namespace N { struct X{}; }
20644 struct A {
20645 template <typename T> friend struct N::X;
20648 However, if the DECL refers to a class type, and we are in
20649 the scope of the class, then the name lookup automatically
20650 finds the TYPE_DECL created by build_self_reference rather
20651 than a TEMPLATE_DECL. For example, in:
20653 template <class T> struct S {
20654 S s;
20657 there is no need to handle such case. */
20659 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20660 return DECL_TEMPLATE_RESULT (decl);
20662 return decl;
20665 /* If too many, or too few, template-parameter lists apply to the
20666 declarator, issue an error message. Returns TRUE if all went well,
20667 and FALSE otherwise. */
20669 static bool
20670 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20671 cp_declarator *declarator,
20672 location_t declarator_location)
20674 switch (declarator->kind)
20676 case cdk_id:
20678 unsigned num_templates = 0;
20679 tree scope = declarator->u.id.qualifying_scope;
20681 if (scope)
20682 num_templates = num_template_headers_for_class (scope);
20683 else if (TREE_CODE (declarator->u.id.unqualified_name)
20684 == TEMPLATE_ID_EXPR)
20685 /* If the DECLARATOR has the form `X<y>' then it uses one
20686 additional level of template parameters. */
20687 ++num_templates;
20689 return cp_parser_check_template_parameters
20690 (parser, num_templates, declarator_location, declarator);
20693 case cdk_function:
20694 case cdk_array:
20695 case cdk_pointer:
20696 case cdk_reference:
20697 case cdk_ptrmem:
20698 return (cp_parser_check_declarator_template_parameters
20699 (parser, declarator->declarator, declarator_location));
20701 case cdk_error:
20702 return true;
20704 default:
20705 gcc_unreachable ();
20707 return false;
20710 /* NUM_TEMPLATES were used in the current declaration. If that is
20711 invalid, return FALSE and issue an error messages. Otherwise,
20712 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20713 declarator and we can print more accurate diagnostics. */
20715 static bool
20716 cp_parser_check_template_parameters (cp_parser* parser,
20717 unsigned num_templates,
20718 location_t location,
20719 cp_declarator *declarator)
20721 /* If there are the same number of template classes and parameter
20722 lists, that's OK. */
20723 if (parser->num_template_parameter_lists == num_templates)
20724 return true;
20725 /* If there are more, but only one more, then we are referring to a
20726 member template. That's OK too. */
20727 if (parser->num_template_parameter_lists == num_templates + 1)
20728 return true;
20729 /* If there are more template classes than parameter lists, we have
20730 something like:
20732 template <class T> void S<T>::R<T>::f (); */
20733 if (parser->num_template_parameter_lists < num_templates)
20735 if (declarator && !current_function_decl)
20736 error_at (location, "specializing member %<%T::%E%> "
20737 "requires %<template<>%> syntax",
20738 declarator->u.id.qualifying_scope,
20739 declarator->u.id.unqualified_name);
20740 else if (declarator)
20741 error_at (location, "invalid declaration of %<%T::%E%>",
20742 declarator->u.id.qualifying_scope,
20743 declarator->u.id.unqualified_name);
20744 else
20745 error_at (location, "too few template-parameter-lists");
20746 return false;
20748 /* Otherwise, there are too many template parameter lists. We have
20749 something like:
20751 template <class T> template <class U> void S::f(); */
20752 error_at (location, "too many template-parameter-lists");
20753 return false;
20756 /* Parse an optional `::' token indicating that the following name is
20757 from the global namespace. If so, PARSER->SCOPE is set to the
20758 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20759 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20760 Returns the new value of PARSER->SCOPE, if the `::' token is
20761 present, and NULL_TREE otherwise. */
20763 static tree
20764 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20766 cp_token *token;
20768 /* Peek at the next token. */
20769 token = cp_lexer_peek_token (parser->lexer);
20770 /* If we're looking at a `::' token then we're starting from the
20771 global namespace, not our current location. */
20772 if (token->type == CPP_SCOPE)
20774 /* Consume the `::' token. */
20775 cp_lexer_consume_token (parser->lexer);
20776 /* Set the SCOPE so that we know where to start the lookup. */
20777 parser->scope = global_namespace;
20778 parser->qualifying_scope = global_namespace;
20779 parser->object_scope = NULL_TREE;
20781 return parser->scope;
20783 else if (!current_scope_valid_p)
20785 parser->scope = NULL_TREE;
20786 parser->qualifying_scope = NULL_TREE;
20787 parser->object_scope = NULL_TREE;
20790 return NULL_TREE;
20793 /* Returns TRUE if the upcoming token sequence is the start of a
20794 constructor declarator. If FRIEND_P is true, the declarator is
20795 preceded by the `friend' specifier. */
20797 static bool
20798 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20800 bool constructor_p;
20801 tree nested_name_specifier;
20802 cp_token *next_token;
20804 /* The common case is that this is not a constructor declarator, so
20805 try to avoid doing lots of work if at all possible. It's not
20806 valid declare a constructor at function scope. */
20807 if (parser->in_function_body)
20808 return false;
20809 /* And only certain tokens can begin a constructor declarator. */
20810 next_token = cp_lexer_peek_token (parser->lexer);
20811 if (next_token->type != CPP_NAME
20812 && next_token->type != CPP_SCOPE
20813 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20814 && next_token->type != CPP_TEMPLATE_ID)
20815 return false;
20817 /* Parse tentatively; we are going to roll back all of the tokens
20818 consumed here. */
20819 cp_parser_parse_tentatively (parser);
20820 /* Assume that we are looking at a constructor declarator. */
20821 constructor_p = true;
20823 /* Look for the optional `::' operator. */
20824 cp_parser_global_scope_opt (parser,
20825 /*current_scope_valid_p=*/false);
20826 /* Look for the nested-name-specifier. */
20827 nested_name_specifier
20828 = (cp_parser_nested_name_specifier_opt (parser,
20829 /*typename_keyword_p=*/false,
20830 /*check_dependency_p=*/false,
20831 /*type_p=*/false,
20832 /*is_declaration=*/false));
20833 /* Outside of a class-specifier, there must be a
20834 nested-name-specifier. */
20835 if (!nested_name_specifier &&
20836 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20837 || friend_p))
20838 constructor_p = false;
20839 else if (nested_name_specifier == error_mark_node)
20840 constructor_p = false;
20842 /* If we have a class scope, this is easy; DR 147 says that S::S always
20843 names the constructor, and no other qualified name could. */
20844 if (constructor_p && nested_name_specifier
20845 && CLASS_TYPE_P (nested_name_specifier))
20847 tree id = cp_parser_unqualified_id (parser,
20848 /*template_keyword_p=*/false,
20849 /*check_dependency_p=*/false,
20850 /*declarator_p=*/true,
20851 /*optional_p=*/false);
20852 if (is_overloaded_fn (id))
20853 id = DECL_NAME (get_first_fn (id));
20854 if (!constructor_name_p (id, nested_name_specifier))
20855 constructor_p = false;
20857 /* If we still think that this might be a constructor-declarator,
20858 look for a class-name. */
20859 else if (constructor_p)
20861 /* If we have:
20863 template <typename T> struct S {
20864 S();
20867 we must recognize that the nested `S' names a class. */
20868 tree type_decl;
20869 type_decl = cp_parser_class_name (parser,
20870 /*typename_keyword_p=*/false,
20871 /*template_keyword_p=*/false,
20872 none_type,
20873 /*check_dependency_p=*/false,
20874 /*class_head_p=*/false,
20875 /*is_declaration=*/false);
20876 /* If there was no class-name, then this is not a constructor. */
20877 constructor_p = !cp_parser_error_occurred (parser);
20879 /* If we're still considering a constructor, we have to see a `(',
20880 to begin the parameter-declaration-clause, followed by either a
20881 `)', an `...', or a decl-specifier. We need to check for a
20882 type-specifier to avoid being fooled into thinking that:
20884 S (f) (int);
20886 is a constructor. (It is actually a function named `f' that
20887 takes one parameter (of type `int') and returns a value of type
20888 `S'. */
20889 if (constructor_p
20890 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20891 constructor_p = false;
20893 if (constructor_p
20894 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20895 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20896 /* A parameter declaration begins with a decl-specifier,
20897 which is either the "attribute" keyword, a storage class
20898 specifier, or (usually) a type-specifier. */
20899 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20901 tree type;
20902 tree pushed_scope = NULL_TREE;
20903 unsigned saved_num_template_parameter_lists;
20905 /* Names appearing in the type-specifier should be looked up
20906 in the scope of the class. */
20907 if (current_class_type)
20908 type = NULL_TREE;
20909 else
20911 type = TREE_TYPE (type_decl);
20912 if (TREE_CODE (type) == TYPENAME_TYPE)
20914 type = resolve_typename_type (type,
20915 /*only_current_p=*/false);
20916 if (TREE_CODE (type) == TYPENAME_TYPE)
20918 cp_parser_abort_tentative_parse (parser);
20919 return false;
20922 pushed_scope = push_scope (type);
20925 /* Inside the constructor parameter list, surrounding
20926 template-parameter-lists do not apply. */
20927 saved_num_template_parameter_lists
20928 = parser->num_template_parameter_lists;
20929 parser->num_template_parameter_lists = 0;
20931 /* Look for the type-specifier. */
20932 cp_parser_type_specifier (parser,
20933 CP_PARSER_FLAGS_NONE,
20934 /*decl_specs=*/NULL,
20935 /*is_declarator=*/true,
20936 /*declares_class_or_enum=*/NULL,
20937 /*is_cv_qualifier=*/NULL);
20939 parser->num_template_parameter_lists
20940 = saved_num_template_parameter_lists;
20942 /* Leave the scope of the class. */
20943 if (pushed_scope)
20944 pop_scope (pushed_scope);
20946 constructor_p = !cp_parser_error_occurred (parser);
20950 /* We did not really want to consume any tokens. */
20951 cp_parser_abort_tentative_parse (parser);
20953 return constructor_p;
20956 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20957 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20958 they must be performed once we are in the scope of the function.
20960 Returns the function defined. */
20962 static tree
20963 cp_parser_function_definition_from_specifiers_and_declarator
20964 (cp_parser* parser,
20965 cp_decl_specifier_seq *decl_specifiers,
20966 tree attributes,
20967 const cp_declarator *declarator)
20969 tree fn;
20970 bool success_p;
20972 /* Begin the function-definition. */
20973 success_p = start_function (decl_specifiers, declarator, attributes);
20975 /* The things we're about to see are not directly qualified by any
20976 template headers we've seen thus far. */
20977 reset_specialization ();
20979 /* If there were names looked up in the decl-specifier-seq that we
20980 did not check, check them now. We must wait until we are in the
20981 scope of the function to perform the checks, since the function
20982 might be a friend. */
20983 perform_deferred_access_checks (tf_warning_or_error);
20985 if (!success_p)
20987 /* Skip the entire function. */
20988 cp_parser_skip_to_end_of_block_or_statement (parser);
20989 fn = error_mark_node;
20991 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20993 /* Seen already, skip it. An error message has already been output. */
20994 cp_parser_skip_to_end_of_block_or_statement (parser);
20995 fn = current_function_decl;
20996 current_function_decl = NULL_TREE;
20997 /* If this is a function from a class, pop the nested class. */
20998 if (current_class_name)
20999 pop_nested_class ();
21001 else
21003 timevar_id_t tv;
21004 if (DECL_DECLARED_INLINE_P (current_function_decl))
21005 tv = TV_PARSE_INLINE;
21006 else
21007 tv = TV_PARSE_FUNC;
21008 timevar_push (tv);
21009 fn = cp_parser_function_definition_after_declarator (parser,
21010 /*inline_p=*/false);
21011 timevar_pop (tv);
21014 return fn;
21017 /* Parse the part of a function-definition that follows the
21018 declarator. INLINE_P is TRUE iff this function is an inline
21019 function defined within a class-specifier.
21021 Returns the function defined. */
21023 static tree
21024 cp_parser_function_definition_after_declarator (cp_parser* parser,
21025 bool inline_p)
21027 tree fn;
21028 bool ctor_initializer_p = false;
21029 bool saved_in_unbraced_linkage_specification_p;
21030 bool saved_in_function_body;
21031 unsigned saved_num_template_parameter_lists;
21032 cp_token *token;
21034 saved_in_function_body = parser->in_function_body;
21035 parser->in_function_body = true;
21036 /* If the next token is `return', then the code may be trying to
21037 make use of the "named return value" extension that G++ used to
21038 support. */
21039 token = cp_lexer_peek_token (parser->lexer);
21040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21042 /* Consume the `return' keyword. */
21043 cp_lexer_consume_token (parser->lexer);
21044 /* Look for the identifier that indicates what value is to be
21045 returned. */
21046 cp_parser_identifier (parser);
21047 /* Issue an error message. */
21048 error_at (token->location,
21049 "named return values are no longer supported");
21050 /* Skip tokens until we reach the start of the function body. */
21051 while (true)
21053 cp_token *token = cp_lexer_peek_token (parser->lexer);
21054 if (token->type == CPP_OPEN_BRACE
21055 || token->type == CPP_EOF
21056 || token->type == CPP_PRAGMA_EOL)
21057 break;
21058 cp_lexer_consume_token (parser->lexer);
21061 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21062 anything declared inside `f'. */
21063 saved_in_unbraced_linkage_specification_p
21064 = parser->in_unbraced_linkage_specification_p;
21065 parser->in_unbraced_linkage_specification_p = false;
21066 /* Inside the function, surrounding template-parameter-lists do not
21067 apply. */
21068 saved_num_template_parameter_lists
21069 = parser->num_template_parameter_lists;
21070 parser->num_template_parameter_lists = 0;
21072 start_lambda_scope (current_function_decl);
21074 /* If the next token is `try', `__transaction_atomic', or
21075 `__transaction_relaxed`, then we are looking at either function-try-block
21076 or function-transaction-block. Note that all of these include the
21077 function-body. */
21078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21079 ctor_initializer_p = cp_parser_function_transaction (parser,
21080 RID_TRANSACTION_ATOMIC);
21081 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21082 RID_TRANSACTION_RELAXED))
21083 ctor_initializer_p = cp_parser_function_transaction (parser,
21084 RID_TRANSACTION_RELAXED);
21085 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21086 ctor_initializer_p = cp_parser_function_try_block (parser);
21087 else
21088 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21089 (parser, /*in_function_try_block=*/false);
21091 finish_lambda_scope ();
21093 /* Finish the function. */
21094 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21095 (inline_p ? 2 : 0));
21096 /* Generate code for it, if necessary. */
21097 expand_or_defer_fn (fn);
21098 /* Restore the saved values. */
21099 parser->in_unbraced_linkage_specification_p
21100 = saved_in_unbraced_linkage_specification_p;
21101 parser->num_template_parameter_lists
21102 = saved_num_template_parameter_lists;
21103 parser->in_function_body = saved_in_function_body;
21105 return fn;
21108 /* Parse a template-declaration, assuming that the `export' (and
21109 `extern') keywords, if present, has already been scanned. MEMBER_P
21110 is as for cp_parser_template_declaration. */
21112 static void
21113 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21115 tree decl = NULL_TREE;
21116 VEC (deferred_access_check,gc) *checks;
21117 tree parameter_list;
21118 bool friend_p = false;
21119 bool need_lang_pop;
21120 cp_token *token;
21122 /* Look for the `template' keyword. */
21123 token = cp_lexer_peek_token (parser->lexer);
21124 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21125 return;
21127 /* And the `<'. */
21128 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21129 return;
21130 if (at_class_scope_p () && current_function_decl)
21132 /* 14.5.2.2 [temp.mem]
21134 A local class shall not have member templates. */
21135 error_at (token->location,
21136 "invalid declaration of member template in local class");
21137 cp_parser_skip_to_end_of_block_or_statement (parser);
21138 return;
21140 /* [temp]
21142 A template ... shall not have C linkage. */
21143 if (current_lang_name == lang_name_c)
21145 error_at (token->location, "template with C linkage");
21146 /* Give it C++ linkage to avoid confusing other parts of the
21147 front end. */
21148 push_lang_context (lang_name_cplusplus);
21149 need_lang_pop = true;
21151 else
21152 need_lang_pop = false;
21154 /* We cannot perform access checks on the template parameter
21155 declarations until we know what is being declared, just as we
21156 cannot check the decl-specifier list. */
21157 push_deferring_access_checks (dk_deferred);
21159 /* If the next token is `>', then we have an invalid
21160 specialization. Rather than complain about an invalid template
21161 parameter, issue an error message here. */
21162 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21164 cp_parser_error (parser, "invalid explicit specialization");
21165 begin_specialization ();
21166 parameter_list = NULL_TREE;
21168 else
21170 /* Parse the template parameters. */
21171 parameter_list = cp_parser_template_parameter_list (parser);
21174 /* Get the deferred access checks from the parameter list. These
21175 will be checked once we know what is being declared, as for a
21176 member template the checks must be performed in the scope of the
21177 class containing the member. */
21178 checks = get_deferred_access_checks ();
21180 /* Look for the `>'. */
21181 cp_parser_skip_to_end_of_template_parameter_list (parser);
21182 /* We just processed one more parameter list. */
21183 ++parser->num_template_parameter_lists;
21184 /* If the next token is `template', there are more template
21185 parameters. */
21186 if (cp_lexer_next_token_is_keyword (parser->lexer,
21187 RID_TEMPLATE))
21188 cp_parser_template_declaration_after_export (parser, member_p);
21189 else if (cxx_dialect >= cxx0x
21190 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21191 decl = cp_parser_alias_declaration (parser);
21192 else
21194 /* There are no access checks when parsing a template, as we do not
21195 know if a specialization will be a friend. */
21196 push_deferring_access_checks (dk_no_check);
21197 token = cp_lexer_peek_token (parser->lexer);
21198 decl = cp_parser_single_declaration (parser,
21199 checks,
21200 member_p,
21201 /*explicit_specialization_p=*/false,
21202 &friend_p);
21203 pop_deferring_access_checks ();
21205 /* If this is a member template declaration, let the front
21206 end know. */
21207 if (member_p && !friend_p && decl)
21209 if (TREE_CODE (decl) == TYPE_DECL)
21210 cp_parser_check_access_in_redeclaration (decl, token->location);
21212 decl = finish_member_template_decl (decl);
21214 else if (friend_p && decl
21215 && (TREE_CODE (decl) == TYPE_DECL
21216 || DECL_TYPE_TEMPLATE_P (decl)))
21217 make_friend_class (current_class_type, TREE_TYPE (decl),
21218 /*complain=*/true);
21220 /* We are done with the current parameter list. */
21221 --parser->num_template_parameter_lists;
21223 pop_deferring_access_checks ();
21225 /* Finish up. */
21226 finish_template_decl (parameter_list);
21228 /* Check the template arguments for a literal operator template. */
21229 if (decl
21230 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21231 && UDLIT_OPER_P (DECL_NAME (decl)))
21233 bool ok = true;
21234 if (parameter_list == NULL_TREE)
21235 ok = false;
21236 else
21238 int num_parms = TREE_VEC_LENGTH (parameter_list);
21239 if (num_parms != 1)
21240 ok = false;
21241 else
21243 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21244 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21245 if (TREE_TYPE (parm) != char_type_node
21246 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21247 ok = false;
21250 if (!ok)
21251 error ("literal operator template %qD has invalid parameter list."
21252 " Expected non-type template argument pack <char...>",
21253 decl);
21255 /* Register member declarations. */
21256 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21257 finish_member_declaration (decl);
21258 /* For the erroneous case of a template with C linkage, we pushed an
21259 implicit C++ linkage scope; exit that scope now. */
21260 if (need_lang_pop)
21261 pop_lang_context ();
21262 /* If DECL is a function template, we must return to parse it later.
21263 (Even though there is no definition, there might be default
21264 arguments that need handling.) */
21265 if (member_p && decl
21266 && (TREE_CODE (decl) == FUNCTION_DECL
21267 || DECL_FUNCTION_TEMPLATE_P (decl)))
21268 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21271 /* Perform the deferred access checks from a template-parameter-list.
21272 CHECKS is a TREE_LIST of access checks, as returned by
21273 get_deferred_access_checks. */
21275 static void
21276 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21278 ++processing_template_parmlist;
21279 perform_access_checks (checks, tf_warning_or_error);
21280 --processing_template_parmlist;
21283 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21284 `function-definition' sequence that follows a template header.
21285 If MEMBER_P is true, this declaration appears in a class scope.
21287 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21288 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21290 static tree
21291 cp_parser_single_declaration (cp_parser* parser,
21292 VEC (deferred_access_check,gc)* checks,
21293 bool member_p,
21294 bool explicit_specialization_p,
21295 bool* friend_p)
21297 int declares_class_or_enum;
21298 tree decl = NULL_TREE;
21299 cp_decl_specifier_seq decl_specifiers;
21300 bool function_definition_p = false;
21301 cp_token *decl_spec_token_start;
21303 /* This function is only used when processing a template
21304 declaration. */
21305 gcc_assert (innermost_scope_kind () == sk_template_parms
21306 || innermost_scope_kind () == sk_template_spec);
21308 /* Defer access checks until we know what is being declared. */
21309 push_deferring_access_checks (dk_deferred);
21311 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21312 alternative. */
21313 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21314 cp_parser_decl_specifier_seq (parser,
21315 CP_PARSER_FLAGS_OPTIONAL,
21316 &decl_specifiers,
21317 &declares_class_or_enum);
21318 if (friend_p)
21319 *friend_p = cp_parser_friend_p (&decl_specifiers);
21321 /* There are no template typedefs. */
21322 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21324 error_at (decl_spec_token_start->location,
21325 "template declaration of %<typedef%>");
21326 decl = error_mark_node;
21329 /* Gather up the access checks that occurred the
21330 decl-specifier-seq. */
21331 stop_deferring_access_checks ();
21333 /* Check for the declaration of a template class. */
21334 if (declares_class_or_enum)
21336 if (cp_parser_declares_only_class_p (parser))
21338 decl = shadow_tag (&decl_specifiers);
21340 /* In this case:
21342 struct C {
21343 friend template <typename T> struct A<T>::B;
21346 A<T>::B will be represented by a TYPENAME_TYPE, and
21347 therefore not recognized by shadow_tag. */
21348 if (friend_p && *friend_p
21349 && !decl
21350 && decl_specifiers.type
21351 && TYPE_P (decl_specifiers.type))
21352 decl = decl_specifiers.type;
21354 if (decl && decl != error_mark_node)
21355 decl = TYPE_NAME (decl);
21356 else
21357 decl = error_mark_node;
21359 /* Perform access checks for template parameters. */
21360 cp_parser_perform_template_parameter_access_checks (checks);
21364 /* Complain about missing 'typename' or other invalid type names. */
21365 if (!decl_specifiers.any_type_specifiers_p
21366 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21368 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21369 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21370 the rest of this declaration. */
21371 decl = error_mark_node;
21372 goto out;
21375 /* If it's not a template class, try for a template function. If
21376 the next token is a `;', then this declaration does not declare
21377 anything. But, if there were errors in the decl-specifiers, then
21378 the error might well have come from an attempted class-specifier.
21379 In that case, there's no need to warn about a missing declarator. */
21380 if (!decl
21381 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21382 || decl_specifiers.type != error_mark_node))
21384 decl = cp_parser_init_declarator (parser,
21385 &decl_specifiers,
21386 checks,
21387 /*function_definition_allowed_p=*/true,
21388 member_p,
21389 declares_class_or_enum,
21390 &function_definition_p,
21391 NULL);
21393 /* 7.1.1-1 [dcl.stc]
21395 A storage-class-specifier shall not be specified in an explicit
21396 specialization... */
21397 if (decl
21398 && explicit_specialization_p
21399 && decl_specifiers.storage_class != sc_none)
21401 error_at (decl_spec_token_start->location,
21402 "explicit template specialization cannot have a storage class");
21403 decl = error_mark_node;
21406 if (decl && TREE_CODE (decl) == VAR_DECL)
21407 check_template_variable (decl);
21410 /* Look for a trailing `;' after the declaration. */
21411 if (!function_definition_p
21412 && (decl == error_mark_node
21413 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21414 cp_parser_skip_to_end_of_block_or_statement (parser);
21416 out:
21417 pop_deferring_access_checks ();
21419 /* Clear any current qualification; whatever comes next is the start
21420 of something new. */
21421 parser->scope = NULL_TREE;
21422 parser->qualifying_scope = NULL_TREE;
21423 parser->object_scope = NULL_TREE;
21425 return decl;
21428 /* Parse a cast-expression that is not the operand of a unary "&". */
21430 static tree
21431 cp_parser_simple_cast_expression (cp_parser *parser)
21433 return cp_parser_cast_expression (parser, /*address_p=*/false,
21434 /*cast_p=*/false, NULL);
21437 /* Parse a functional cast to TYPE. Returns an expression
21438 representing the cast. */
21440 static tree
21441 cp_parser_functional_cast (cp_parser* parser, tree type)
21443 VEC(tree,gc) *vec;
21444 tree expression_list;
21445 tree cast;
21446 bool nonconst_p;
21448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21451 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21452 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21453 if (TREE_CODE (type) == TYPE_DECL)
21454 type = TREE_TYPE (type);
21455 return finish_compound_literal (type, expression_list,
21456 tf_warning_or_error);
21460 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21461 /*cast_p=*/true,
21462 /*allow_expansion_p=*/true,
21463 /*non_constant_p=*/NULL);
21464 if (vec == NULL)
21465 expression_list = error_mark_node;
21466 else
21468 expression_list = build_tree_list_vec (vec);
21469 release_tree_vector (vec);
21472 cast = build_functional_cast (type, expression_list,
21473 tf_warning_or_error);
21474 /* [expr.const]/1: In an integral constant expression "only type
21475 conversions to integral or enumeration type can be used". */
21476 if (TREE_CODE (type) == TYPE_DECL)
21477 type = TREE_TYPE (type);
21478 if (cast != error_mark_node
21479 && !cast_valid_in_integral_constant_expression_p (type)
21480 && cp_parser_non_integral_constant_expression (parser,
21481 NIC_CONSTRUCTOR))
21482 return error_mark_node;
21483 return cast;
21486 /* Save the tokens that make up the body of a member function defined
21487 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21488 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21489 specifiers applied to the declaration. Returns the FUNCTION_DECL
21490 for the member function. */
21492 static tree
21493 cp_parser_save_member_function_body (cp_parser* parser,
21494 cp_decl_specifier_seq *decl_specifiers,
21495 cp_declarator *declarator,
21496 tree attributes)
21498 cp_token *first;
21499 cp_token *last;
21500 tree fn;
21502 /* Create the FUNCTION_DECL. */
21503 fn = grokmethod (decl_specifiers, declarator, attributes);
21504 /* If something went badly wrong, bail out now. */
21505 if (fn == error_mark_node)
21507 /* If there's a function-body, skip it. */
21508 if (cp_parser_token_starts_function_definition_p
21509 (cp_lexer_peek_token (parser->lexer)))
21510 cp_parser_skip_to_end_of_block_or_statement (parser);
21511 return error_mark_node;
21514 /* Remember it, if there default args to post process. */
21515 cp_parser_save_default_args (parser, fn);
21517 /* Save away the tokens that make up the body of the
21518 function. */
21519 first = parser->lexer->next_token;
21520 /* We can have braced-init-list mem-initializers before the fn body. */
21521 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21523 cp_lexer_consume_token (parser->lexer);
21524 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21525 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21527 /* cache_group will stop after an un-nested { } pair, too. */
21528 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21529 break;
21531 /* variadic mem-inits have ... after the ')'. */
21532 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21533 cp_lexer_consume_token (parser->lexer);
21536 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21537 /* Handle function try blocks. */
21538 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21539 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21540 last = parser->lexer->next_token;
21542 /* Save away the inline definition; we will process it when the
21543 class is complete. */
21544 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21545 DECL_PENDING_INLINE_P (fn) = 1;
21547 /* We need to know that this was defined in the class, so that
21548 friend templates are handled correctly. */
21549 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21551 /* Add FN to the queue of functions to be parsed later. */
21552 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21554 return fn;
21557 /* Save the tokens that make up the in-class initializer for a non-static
21558 data member. Returns a DEFAULT_ARG. */
21560 static tree
21561 cp_parser_save_nsdmi (cp_parser* parser)
21563 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21566 /* Parse a template-argument-list, as well as the trailing ">" (but
21567 not the opening "<"). See cp_parser_template_argument_list for the
21568 return value. */
21570 static tree
21571 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21573 tree arguments;
21574 tree saved_scope;
21575 tree saved_qualifying_scope;
21576 tree saved_object_scope;
21577 bool saved_greater_than_is_operator_p;
21578 int saved_unevaluated_operand;
21579 int saved_inhibit_evaluation_warnings;
21581 /* [temp.names]
21583 When parsing a template-id, the first non-nested `>' is taken as
21584 the end of the template-argument-list rather than a greater-than
21585 operator. */
21586 saved_greater_than_is_operator_p
21587 = parser->greater_than_is_operator_p;
21588 parser->greater_than_is_operator_p = false;
21589 /* Parsing the argument list may modify SCOPE, so we save it
21590 here. */
21591 saved_scope = parser->scope;
21592 saved_qualifying_scope = parser->qualifying_scope;
21593 saved_object_scope = parser->object_scope;
21594 /* We need to evaluate the template arguments, even though this
21595 template-id may be nested within a "sizeof". */
21596 saved_unevaluated_operand = cp_unevaluated_operand;
21597 cp_unevaluated_operand = 0;
21598 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21599 c_inhibit_evaluation_warnings = 0;
21600 /* Parse the template-argument-list itself. */
21601 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21602 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21603 arguments = NULL_TREE;
21604 else
21605 arguments = cp_parser_template_argument_list (parser);
21606 /* Look for the `>' that ends the template-argument-list. If we find
21607 a '>>' instead, it's probably just a typo. */
21608 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21610 if (cxx_dialect != cxx98)
21612 /* In C++0x, a `>>' in a template argument list or cast
21613 expression is considered to be two separate `>'
21614 tokens. So, change the current token to a `>', but don't
21615 consume it: it will be consumed later when the outer
21616 template argument list (or cast expression) is parsed.
21617 Note that this replacement of `>' for `>>' is necessary
21618 even if we are parsing tentatively: in the tentative
21619 case, after calling
21620 cp_parser_enclosed_template_argument_list we will always
21621 throw away all of the template arguments and the first
21622 closing `>', either because the template argument list
21623 was erroneous or because we are replacing those tokens
21624 with a CPP_TEMPLATE_ID token. The second `>' (which will
21625 not have been thrown away) is needed either to close an
21626 outer template argument list or to complete a new-style
21627 cast. */
21628 cp_token *token = cp_lexer_peek_token (parser->lexer);
21629 token->type = CPP_GREATER;
21631 else if (!saved_greater_than_is_operator_p)
21633 /* If we're in a nested template argument list, the '>>' has
21634 to be a typo for '> >'. We emit the error message, but we
21635 continue parsing and we push a '>' as next token, so that
21636 the argument list will be parsed correctly. Note that the
21637 global source location is still on the token before the
21638 '>>', so we need to say explicitly where we want it. */
21639 cp_token *token = cp_lexer_peek_token (parser->lexer);
21640 error_at (token->location, "%<>>%> should be %<> >%> "
21641 "within a nested template argument list");
21643 token->type = CPP_GREATER;
21645 else
21647 /* If this is not a nested template argument list, the '>>'
21648 is a typo for '>'. Emit an error message and continue.
21649 Same deal about the token location, but here we can get it
21650 right by consuming the '>>' before issuing the diagnostic. */
21651 cp_token *token = cp_lexer_consume_token (parser->lexer);
21652 error_at (token->location,
21653 "spurious %<>>%>, use %<>%> to terminate "
21654 "a template argument list");
21657 else
21658 cp_parser_skip_to_end_of_template_parameter_list (parser);
21659 /* The `>' token might be a greater-than operator again now. */
21660 parser->greater_than_is_operator_p
21661 = saved_greater_than_is_operator_p;
21662 /* Restore the SAVED_SCOPE. */
21663 parser->scope = saved_scope;
21664 parser->qualifying_scope = saved_qualifying_scope;
21665 parser->object_scope = saved_object_scope;
21666 cp_unevaluated_operand = saved_unevaluated_operand;
21667 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21669 return arguments;
21672 /* MEMBER_FUNCTION is a member function, or a friend. If default
21673 arguments, or the body of the function have not yet been parsed,
21674 parse them now. */
21676 static void
21677 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21679 timevar_push (TV_PARSE_INMETH);
21680 /* If this member is a template, get the underlying
21681 FUNCTION_DECL. */
21682 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21683 member_function = DECL_TEMPLATE_RESULT (member_function);
21685 /* There should not be any class definitions in progress at this
21686 point; the bodies of members are only parsed outside of all class
21687 definitions. */
21688 gcc_assert (parser->num_classes_being_defined == 0);
21689 /* While we're parsing the member functions we might encounter more
21690 classes. We want to handle them right away, but we don't want
21691 them getting mixed up with functions that are currently in the
21692 queue. */
21693 push_unparsed_function_queues (parser);
21695 /* Make sure that any template parameters are in scope. */
21696 maybe_begin_member_template_processing (member_function);
21698 /* If the body of the function has not yet been parsed, parse it
21699 now. */
21700 if (DECL_PENDING_INLINE_P (member_function))
21702 tree function_scope;
21703 cp_token_cache *tokens;
21705 /* The function is no longer pending; we are processing it. */
21706 tokens = DECL_PENDING_INLINE_INFO (member_function);
21707 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21708 DECL_PENDING_INLINE_P (member_function) = 0;
21710 /* If this is a local class, enter the scope of the containing
21711 function. */
21712 function_scope = current_function_decl;
21713 if (function_scope)
21714 push_function_context ();
21716 /* Push the body of the function onto the lexer stack. */
21717 cp_parser_push_lexer_for_tokens (parser, tokens);
21719 /* Let the front end know that we going to be defining this
21720 function. */
21721 start_preparsed_function (member_function, NULL_TREE,
21722 SF_PRE_PARSED | SF_INCLASS_INLINE);
21724 /* Don't do access checking if it is a templated function. */
21725 if (processing_template_decl)
21726 push_deferring_access_checks (dk_no_check);
21728 /* Now, parse the body of the function. */
21729 cp_parser_function_definition_after_declarator (parser,
21730 /*inline_p=*/true);
21732 if (processing_template_decl)
21733 pop_deferring_access_checks ();
21735 /* Leave the scope of the containing function. */
21736 if (function_scope)
21737 pop_function_context ();
21738 cp_parser_pop_lexer (parser);
21741 /* Remove any template parameters from the symbol table. */
21742 maybe_end_member_template_processing ();
21744 /* Restore the queue. */
21745 pop_unparsed_function_queues (parser);
21746 timevar_pop (TV_PARSE_INMETH);
21749 /* If DECL contains any default args, remember it on the unparsed
21750 functions queue. */
21752 static void
21753 cp_parser_save_default_args (cp_parser* parser, tree decl)
21755 tree probe;
21757 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21758 probe;
21759 probe = TREE_CHAIN (probe))
21760 if (TREE_PURPOSE (probe))
21762 cp_default_arg_entry entry = {current_class_type, decl};
21763 VEC_safe_push (cp_default_arg_entry, gc,
21764 unparsed_funs_with_default_args, entry);
21765 break;
21769 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21770 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21771 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21772 from the parameter-type-list. */
21774 static tree
21775 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21776 tree default_arg, tree parmtype)
21778 cp_token_cache *tokens;
21779 tree parsed_arg;
21780 bool dummy;
21782 if (default_arg == error_mark_node)
21783 return error_mark_node;
21785 /* Push the saved tokens for the default argument onto the parser's
21786 lexer stack. */
21787 tokens = DEFARG_TOKENS (default_arg);
21788 cp_parser_push_lexer_for_tokens (parser, tokens);
21790 start_lambda_scope (decl);
21792 /* Parse the default argument. */
21793 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21794 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21795 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21797 finish_lambda_scope ();
21799 if (parsed_arg == error_mark_node)
21800 cp_parser_skip_to_end_of_statement (parser);
21802 if (!processing_template_decl)
21804 /* In a non-template class, check conversions now. In a template,
21805 we'll wait and instantiate these as needed. */
21806 if (TREE_CODE (decl) == PARM_DECL)
21807 parsed_arg = check_default_argument (parmtype, parsed_arg);
21808 else
21810 int flags = LOOKUP_IMPLICIT;
21811 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21812 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21813 flags = LOOKUP_NORMAL;
21814 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21818 /* If the token stream has not been completely used up, then
21819 there was extra junk after the end of the default
21820 argument. */
21821 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21823 if (TREE_CODE (decl) == PARM_DECL)
21824 cp_parser_error (parser, "expected %<,%>");
21825 else
21826 cp_parser_error (parser, "expected %<;%>");
21829 /* Revert to the main lexer. */
21830 cp_parser_pop_lexer (parser);
21832 return parsed_arg;
21835 /* FIELD is a non-static data member with an initializer which we saved for
21836 later; parse it now. */
21838 static void
21839 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21841 tree def;
21843 push_unparsed_function_queues (parser);
21844 def = cp_parser_late_parse_one_default_arg (parser, field,
21845 DECL_INITIAL (field),
21846 NULL_TREE);
21847 pop_unparsed_function_queues (parser);
21849 DECL_INITIAL (field) = def;
21852 /* FN is a FUNCTION_DECL which may contains a parameter with an
21853 unparsed DEFAULT_ARG. Parse the default args now. This function
21854 assumes that the current scope is the scope in which the default
21855 argument should be processed. */
21857 static void
21858 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21860 bool saved_local_variables_forbidden_p;
21861 tree parm, parmdecl;
21863 /* While we're parsing the default args, we might (due to the
21864 statement expression extension) encounter more classes. We want
21865 to handle them right away, but we don't want them getting mixed
21866 up with default args that are currently in the queue. */
21867 push_unparsed_function_queues (parser);
21869 /* Local variable names (and the `this' keyword) may not appear
21870 in a default argument. */
21871 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21872 parser->local_variables_forbidden_p = true;
21874 push_defarg_context (fn);
21876 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21877 parmdecl = DECL_ARGUMENTS (fn);
21878 parm && parm != void_list_node;
21879 parm = TREE_CHAIN (parm),
21880 parmdecl = DECL_CHAIN (parmdecl))
21882 tree default_arg = TREE_PURPOSE (parm);
21883 tree parsed_arg;
21884 VEC(tree,gc) *insts;
21885 tree copy;
21886 unsigned ix;
21888 if (!default_arg)
21889 continue;
21891 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21892 /* This can happen for a friend declaration for a function
21893 already declared with default arguments. */
21894 continue;
21896 parsed_arg
21897 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21898 default_arg,
21899 TREE_VALUE (parm));
21900 if (parsed_arg == error_mark_node)
21902 continue;
21905 TREE_PURPOSE (parm) = parsed_arg;
21907 /* Update any instantiations we've already created. */
21908 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21909 VEC_iterate (tree, insts, ix, copy); ix++)
21910 TREE_PURPOSE (copy) = parsed_arg;
21913 pop_defarg_context ();
21915 /* Make sure no default arg is missing. */
21916 check_default_args (fn);
21918 /* Restore the state of local_variables_forbidden_p. */
21919 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21921 /* Restore the queue. */
21922 pop_unparsed_function_queues (parser);
21925 /* Parse the operand of `sizeof' (or a similar operator). Returns
21926 either a TYPE or an expression, depending on the form of the
21927 input. The KEYWORD indicates which kind of expression we have
21928 encountered. */
21930 static tree
21931 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21933 tree expr = NULL_TREE;
21934 const char *saved_message;
21935 char *tmp;
21936 bool saved_integral_constant_expression_p;
21937 bool saved_non_integral_constant_expression_p;
21938 bool pack_expansion_p = false;
21940 /* Types cannot be defined in a `sizeof' expression. Save away the
21941 old message. */
21942 saved_message = parser->type_definition_forbidden_message;
21943 /* And create the new one. */
21944 tmp = concat ("types may not be defined in %<",
21945 IDENTIFIER_POINTER (ridpointers[keyword]),
21946 "%> expressions", NULL);
21947 parser->type_definition_forbidden_message = tmp;
21949 /* The restrictions on constant-expressions do not apply inside
21950 sizeof expressions. */
21951 saved_integral_constant_expression_p
21952 = parser->integral_constant_expression_p;
21953 saved_non_integral_constant_expression_p
21954 = parser->non_integral_constant_expression_p;
21955 parser->integral_constant_expression_p = false;
21957 /* If it's a `...', then we are computing the length of a parameter
21958 pack. */
21959 if (keyword == RID_SIZEOF
21960 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21962 /* Consume the `...'. */
21963 cp_lexer_consume_token (parser->lexer);
21964 maybe_warn_variadic_templates ();
21966 /* Note that this is an expansion. */
21967 pack_expansion_p = true;
21970 /* Do not actually evaluate the expression. */
21971 ++cp_unevaluated_operand;
21972 ++c_inhibit_evaluation_warnings;
21973 /* If it's a `(', then we might be looking at the type-id
21974 construction. */
21975 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21977 tree type;
21978 bool saved_in_type_id_in_expr_p;
21980 /* We can't be sure yet whether we're looking at a type-id or an
21981 expression. */
21982 cp_parser_parse_tentatively (parser);
21983 /* Consume the `('. */
21984 cp_lexer_consume_token (parser->lexer);
21985 /* Parse the type-id. */
21986 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21987 parser->in_type_id_in_expr_p = true;
21988 type = cp_parser_type_id (parser);
21989 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21990 /* Now, look for the trailing `)'. */
21991 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21992 /* If all went well, then we're done. */
21993 if (cp_parser_parse_definitely (parser))
21995 cp_decl_specifier_seq decl_specs;
21997 /* Build a trivial decl-specifier-seq. */
21998 clear_decl_specs (&decl_specs);
21999 decl_specs.type = type;
22001 /* Call grokdeclarator to figure out what type this is. */
22002 expr = grokdeclarator (NULL,
22003 &decl_specs,
22004 TYPENAME,
22005 /*initialized=*/0,
22006 /*attrlist=*/NULL);
22009 else if (pack_expansion_p)
22010 permerror (cp_lexer_peek_token (parser->lexer)->location,
22011 "%<sizeof...%> argument must be surrounded by parentheses");
22013 /* If the type-id production did not work out, then we must be
22014 looking at the unary-expression production. */
22015 if (!expr)
22016 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22017 /*cast_p=*/false, NULL);
22019 if (pack_expansion_p)
22020 /* Build a pack expansion. */
22021 expr = make_pack_expansion (expr);
22023 /* Go back to evaluating expressions. */
22024 --cp_unevaluated_operand;
22025 --c_inhibit_evaluation_warnings;
22027 /* Free the message we created. */
22028 free (tmp);
22029 /* And restore the old one. */
22030 parser->type_definition_forbidden_message = saved_message;
22031 parser->integral_constant_expression_p
22032 = saved_integral_constant_expression_p;
22033 parser->non_integral_constant_expression_p
22034 = saved_non_integral_constant_expression_p;
22036 return expr;
22039 /* If the current declaration has no declarator, return true. */
22041 static bool
22042 cp_parser_declares_only_class_p (cp_parser *parser)
22044 /* If the next token is a `;' or a `,' then there is no
22045 declarator. */
22046 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22047 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22050 /* Update the DECL_SPECS to reflect the storage class indicated by
22051 KEYWORD. */
22053 static void
22054 cp_parser_set_storage_class (cp_parser *parser,
22055 cp_decl_specifier_seq *decl_specs,
22056 enum rid keyword,
22057 location_t location)
22059 cp_storage_class storage_class;
22061 if (parser->in_unbraced_linkage_specification_p)
22063 error_at (location, "invalid use of %qD in linkage specification",
22064 ridpointers[keyword]);
22065 return;
22067 else if (decl_specs->storage_class != sc_none)
22069 decl_specs->conflicting_specifiers_p = true;
22070 return;
22073 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22074 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22076 error_at (decl_specs->locations[ds_thread],
22077 "%<__thread%> before %qD", ridpointers[keyword]);
22078 decl_specs->locations[ds_thread] = 0;
22081 switch (keyword)
22083 case RID_AUTO:
22084 storage_class = sc_auto;
22085 break;
22086 case RID_REGISTER:
22087 storage_class = sc_register;
22088 break;
22089 case RID_STATIC:
22090 storage_class = sc_static;
22091 break;
22092 case RID_EXTERN:
22093 storage_class = sc_extern;
22094 break;
22095 case RID_MUTABLE:
22096 storage_class = sc_mutable;
22097 break;
22098 default:
22099 gcc_unreachable ();
22101 decl_specs->storage_class = storage_class;
22102 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22104 /* A storage class specifier cannot be applied alongside a typedef
22105 specifier. If there is a typedef specifier present then set
22106 conflicting_specifiers_p which will trigger an error later
22107 on in grokdeclarator. */
22108 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22109 decl_specs->conflicting_specifiers_p = true;
22112 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22113 is true, the type is a class or enum definition. */
22115 static void
22116 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22117 tree type_spec,
22118 location_t location,
22119 bool type_definition_p)
22121 decl_specs->any_specifiers_p = true;
22123 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22124 (with, for example, in "typedef int wchar_t;") we remember that
22125 this is what happened. In system headers, we ignore these
22126 declarations so that G++ can work with system headers that are not
22127 C++-safe. */
22128 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22129 && !type_definition_p
22130 && (type_spec == boolean_type_node
22131 || type_spec == char16_type_node
22132 || type_spec == char32_type_node
22133 || type_spec == wchar_type_node)
22134 && (decl_specs->type
22135 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22136 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22137 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22138 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22140 decl_specs->redefined_builtin_type = type_spec;
22141 set_and_check_decl_spec_loc (decl_specs,
22142 ds_redefined_builtin_type_spec,
22143 location);
22144 if (!decl_specs->type)
22146 decl_specs->type = type_spec;
22147 decl_specs->type_definition_p = false;
22148 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22151 else if (decl_specs->type)
22152 decl_specs->multiple_types_p = true;
22153 else
22155 decl_specs->type = type_spec;
22156 decl_specs->type_definition_p = type_definition_p;
22157 decl_specs->redefined_builtin_type = NULL_TREE;
22158 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22162 /* Set the location for a declarator specifier and check if it is
22163 duplicated.
22165 DECL_SPECS is the sequence of declarator specifiers onto which to
22166 set the location.
22168 DS is the single declarator specifier to set which location is to
22169 be set onto the existing sequence of declarators.
22171 LOCATION is the location for the declarator specifier to
22172 consider. */
22174 static void
22175 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22176 cp_decl_spec ds, source_location location)
22178 gcc_assert (ds < ds_last);
22180 if (decl_specs == NULL)
22181 return;
22183 if (decl_specs->locations[ds] == 0)
22184 decl_specs->locations[ds] = location;
22185 else
22187 if (ds == ds_long)
22189 if (decl_specs->locations[ds_long_long] != 0)
22190 error_at (location,
22191 "%<long long long%> is too long for GCC");
22192 else
22194 decl_specs->locations[ds_long_long] = location;
22195 pedwarn_cxx98 (location,
22196 OPT_Wlong_long,
22197 "ISO C++ 1998 does not support %<long long%>");
22200 else
22202 static const char *const decl_spec_names[] = {
22203 "signed",
22204 "unsigned",
22205 "short",
22206 "long",
22207 "const",
22208 "volatile",
22209 "restrict",
22210 "inline",
22211 "virtual",
22212 "explicit",
22213 "friend",
22214 "typedef",
22215 "using",
22216 "constexpr",
22217 "__complex",
22218 "__thread"
22220 error_at (location,
22221 "duplicate %qs", decl_spec_names[ds]);
22226 /* Return true iff the declarator specifier DS is present in the
22227 sequence of declarator specifiers DECL_SPECS. */
22229 bool
22230 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22231 cp_decl_spec ds)
22233 gcc_assert (ds < ds_last);
22235 if (decl_specs == NULL)
22236 return false;
22238 return decl_specs->locations[ds] != 0;
22241 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22242 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22244 static bool
22245 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22247 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22250 /* Issue an error message indicating that TOKEN_DESC was expected.
22251 If KEYWORD is true, it indicated this function is called by
22252 cp_parser_require_keword and the required token can only be
22253 a indicated keyword. */
22255 static void
22256 cp_parser_required_error (cp_parser *parser,
22257 required_token token_desc,
22258 bool keyword)
22260 switch (token_desc)
22262 case RT_NEW:
22263 cp_parser_error (parser, "expected %<new%>");
22264 return;
22265 case RT_DELETE:
22266 cp_parser_error (parser, "expected %<delete%>");
22267 return;
22268 case RT_RETURN:
22269 cp_parser_error (parser, "expected %<return%>");
22270 return;
22271 case RT_WHILE:
22272 cp_parser_error (parser, "expected %<while%>");
22273 return;
22274 case RT_EXTERN:
22275 cp_parser_error (parser, "expected %<extern%>");
22276 return;
22277 case RT_STATIC_ASSERT:
22278 cp_parser_error (parser, "expected %<static_assert%>");
22279 return;
22280 case RT_DECLTYPE:
22281 cp_parser_error (parser, "expected %<decltype%>");
22282 return;
22283 case RT_OPERATOR:
22284 cp_parser_error (parser, "expected %<operator%>");
22285 return;
22286 case RT_CLASS:
22287 cp_parser_error (parser, "expected %<class%>");
22288 return;
22289 case RT_TEMPLATE:
22290 cp_parser_error (parser, "expected %<template%>");
22291 return;
22292 case RT_NAMESPACE:
22293 cp_parser_error (parser, "expected %<namespace%>");
22294 return;
22295 case RT_USING:
22296 cp_parser_error (parser, "expected %<using%>");
22297 return;
22298 case RT_ASM:
22299 cp_parser_error (parser, "expected %<asm%>");
22300 return;
22301 case RT_TRY:
22302 cp_parser_error (parser, "expected %<try%>");
22303 return;
22304 case RT_CATCH:
22305 cp_parser_error (parser, "expected %<catch%>");
22306 return;
22307 case RT_THROW:
22308 cp_parser_error (parser, "expected %<throw%>");
22309 return;
22310 case RT_LABEL:
22311 cp_parser_error (parser, "expected %<__label__%>");
22312 return;
22313 case RT_AT_TRY:
22314 cp_parser_error (parser, "expected %<@try%>");
22315 return;
22316 case RT_AT_SYNCHRONIZED:
22317 cp_parser_error (parser, "expected %<@synchronized%>");
22318 return;
22319 case RT_AT_THROW:
22320 cp_parser_error (parser, "expected %<@throw%>");
22321 return;
22322 case RT_TRANSACTION_ATOMIC:
22323 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22324 return;
22325 case RT_TRANSACTION_RELAXED:
22326 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22327 return;
22328 default:
22329 break;
22331 if (!keyword)
22333 switch (token_desc)
22335 case RT_SEMICOLON:
22336 cp_parser_error (parser, "expected %<;%>");
22337 return;
22338 case RT_OPEN_PAREN:
22339 cp_parser_error (parser, "expected %<(%>");
22340 return;
22341 case RT_CLOSE_BRACE:
22342 cp_parser_error (parser, "expected %<}%>");
22343 return;
22344 case RT_OPEN_BRACE:
22345 cp_parser_error (parser, "expected %<{%>");
22346 return;
22347 case RT_CLOSE_SQUARE:
22348 cp_parser_error (parser, "expected %<]%>");
22349 return;
22350 case RT_OPEN_SQUARE:
22351 cp_parser_error (parser, "expected %<[%>");
22352 return;
22353 case RT_COMMA:
22354 cp_parser_error (parser, "expected %<,%>");
22355 return;
22356 case RT_SCOPE:
22357 cp_parser_error (parser, "expected %<::%>");
22358 return;
22359 case RT_LESS:
22360 cp_parser_error (parser, "expected %<<%>");
22361 return;
22362 case RT_GREATER:
22363 cp_parser_error (parser, "expected %<>%>");
22364 return;
22365 case RT_EQ:
22366 cp_parser_error (parser, "expected %<=%>");
22367 return;
22368 case RT_ELLIPSIS:
22369 cp_parser_error (parser, "expected %<...%>");
22370 return;
22371 case RT_MULT:
22372 cp_parser_error (parser, "expected %<*%>");
22373 return;
22374 case RT_COMPL:
22375 cp_parser_error (parser, "expected %<~%>");
22376 return;
22377 case RT_COLON:
22378 cp_parser_error (parser, "expected %<:%>");
22379 return;
22380 case RT_COLON_SCOPE:
22381 cp_parser_error (parser, "expected %<:%> or %<::%>");
22382 return;
22383 case RT_CLOSE_PAREN:
22384 cp_parser_error (parser, "expected %<)%>");
22385 return;
22386 case RT_COMMA_CLOSE_PAREN:
22387 cp_parser_error (parser, "expected %<,%> or %<)%>");
22388 return;
22389 case RT_PRAGMA_EOL:
22390 cp_parser_error (parser, "expected end of line");
22391 return;
22392 case RT_NAME:
22393 cp_parser_error (parser, "expected identifier");
22394 return;
22395 case RT_SELECT:
22396 cp_parser_error (parser, "expected selection-statement");
22397 return;
22398 case RT_INTERATION:
22399 cp_parser_error (parser, "expected iteration-statement");
22400 return;
22401 case RT_JUMP:
22402 cp_parser_error (parser, "expected jump-statement");
22403 return;
22404 case RT_CLASS_KEY:
22405 cp_parser_error (parser, "expected class-key");
22406 return;
22407 case RT_CLASS_TYPENAME_TEMPLATE:
22408 cp_parser_error (parser,
22409 "expected %<class%>, %<typename%>, or %<template%>");
22410 return;
22411 default:
22412 gcc_unreachable ();
22415 else
22416 gcc_unreachable ();
22421 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22422 issue an error message indicating that TOKEN_DESC was expected.
22424 Returns the token consumed, if the token had the appropriate type.
22425 Otherwise, returns NULL. */
22427 static cp_token *
22428 cp_parser_require (cp_parser* parser,
22429 enum cpp_ttype type,
22430 required_token token_desc)
22432 if (cp_lexer_next_token_is (parser->lexer, type))
22433 return cp_lexer_consume_token (parser->lexer);
22434 else
22436 /* Output the MESSAGE -- unless we're parsing tentatively. */
22437 if (!cp_parser_simulate_error (parser))
22438 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22439 return NULL;
22443 /* An error message is produced if the next token is not '>'.
22444 All further tokens are skipped until the desired token is
22445 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22447 static void
22448 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22450 /* Current level of '< ... >'. */
22451 unsigned level = 0;
22452 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22453 unsigned nesting_depth = 0;
22455 /* Are we ready, yet? If not, issue error message. */
22456 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22457 return;
22459 /* Skip tokens until the desired token is found. */
22460 while (true)
22462 /* Peek at the next token. */
22463 switch (cp_lexer_peek_token (parser->lexer)->type)
22465 case CPP_LESS:
22466 if (!nesting_depth)
22467 ++level;
22468 break;
22470 case CPP_RSHIFT:
22471 if (cxx_dialect == cxx98)
22472 /* C++0x views the `>>' operator as two `>' tokens, but
22473 C++98 does not. */
22474 break;
22475 else if (!nesting_depth && level-- == 0)
22477 /* We've hit a `>>' where the first `>' closes the
22478 template argument list, and the second `>' is
22479 spurious. Just consume the `>>' and stop; we've
22480 already produced at least one error. */
22481 cp_lexer_consume_token (parser->lexer);
22482 return;
22484 /* Fall through for C++0x, so we handle the second `>' in
22485 the `>>'. */
22487 case CPP_GREATER:
22488 if (!nesting_depth && level-- == 0)
22490 /* We've reached the token we want, consume it and stop. */
22491 cp_lexer_consume_token (parser->lexer);
22492 return;
22494 break;
22496 case CPP_OPEN_PAREN:
22497 case CPP_OPEN_SQUARE:
22498 ++nesting_depth;
22499 break;
22501 case CPP_CLOSE_PAREN:
22502 case CPP_CLOSE_SQUARE:
22503 if (nesting_depth-- == 0)
22504 return;
22505 break;
22507 case CPP_EOF:
22508 case CPP_PRAGMA_EOL:
22509 case CPP_SEMICOLON:
22510 case CPP_OPEN_BRACE:
22511 case CPP_CLOSE_BRACE:
22512 /* The '>' was probably forgotten, don't look further. */
22513 return;
22515 default:
22516 break;
22519 /* Consume this token. */
22520 cp_lexer_consume_token (parser->lexer);
22524 /* If the next token is the indicated keyword, consume it. Otherwise,
22525 issue an error message indicating that TOKEN_DESC was expected.
22527 Returns the token consumed, if the token had the appropriate type.
22528 Otherwise, returns NULL. */
22530 static cp_token *
22531 cp_parser_require_keyword (cp_parser* parser,
22532 enum rid keyword,
22533 required_token token_desc)
22535 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22537 if (token && token->keyword != keyword)
22539 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22540 return NULL;
22543 return token;
22546 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22547 function-definition. */
22549 static bool
22550 cp_parser_token_starts_function_definition_p (cp_token* token)
22552 return (/* An ordinary function-body begins with an `{'. */
22553 token->type == CPP_OPEN_BRACE
22554 /* A ctor-initializer begins with a `:'. */
22555 || token->type == CPP_COLON
22556 /* A function-try-block begins with `try'. */
22557 || token->keyword == RID_TRY
22558 /* A function-transaction-block begins with `__transaction_atomic'
22559 or `__transaction_relaxed'. */
22560 || token->keyword == RID_TRANSACTION_ATOMIC
22561 || token->keyword == RID_TRANSACTION_RELAXED
22562 /* The named return value extension begins with `return'. */
22563 || token->keyword == RID_RETURN);
22566 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22567 definition. */
22569 static bool
22570 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22572 cp_token *token;
22574 token = cp_lexer_peek_token (parser->lexer);
22575 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22578 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22579 C++0x) ending a template-argument. */
22581 static bool
22582 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22584 cp_token *token;
22586 token = cp_lexer_peek_token (parser->lexer);
22587 return (token->type == CPP_COMMA
22588 || token->type == CPP_GREATER
22589 || token->type == CPP_ELLIPSIS
22590 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22593 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22594 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22596 static bool
22597 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22598 size_t n)
22600 cp_token *token;
22602 token = cp_lexer_peek_nth_token (parser->lexer, n);
22603 if (token->type == CPP_LESS)
22604 return true;
22605 /* Check for the sequence `<::' in the original code. It would be lexed as
22606 `[:', where `[' is a digraph, and there is no whitespace before
22607 `:'. */
22608 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22610 cp_token *token2;
22611 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22612 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22613 return true;
22615 return false;
22618 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22619 or none_type otherwise. */
22621 static enum tag_types
22622 cp_parser_token_is_class_key (cp_token* token)
22624 switch (token->keyword)
22626 case RID_CLASS:
22627 return class_type;
22628 case RID_STRUCT:
22629 return record_type;
22630 case RID_UNION:
22631 return union_type;
22633 default:
22634 return none_type;
22638 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22640 static void
22641 cp_parser_check_class_key (enum tag_types class_key, tree type)
22643 if (type == error_mark_node)
22644 return;
22645 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22647 permerror (input_location, "%qs tag used in naming %q#T",
22648 class_key == union_type ? "union"
22649 : class_key == record_type ? "struct" : "class",
22650 type);
22651 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22652 "%q#T was previously declared here", type);
22656 /* Issue an error message if DECL is redeclared with different
22657 access than its original declaration [class.access.spec/3].
22658 This applies to nested classes and nested class templates.
22659 [class.mem/1]. */
22661 static void
22662 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22664 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22665 return;
22667 if ((TREE_PRIVATE (decl)
22668 != (current_access_specifier == access_private_node))
22669 || (TREE_PROTECTED (decl)
22670 != (current_access_specifier == access_protected_node)))
22671 error_at (location, "%qD redeclared with different access", decl);
22674 /* Look for the `template' keyword, as a syntactic disambiguator.
22675 Return TRUE iff it is present, in which case it will be
22676 consumed. */
22678 static bool
22679 cp_parser_optional_template_keyword (cp_parser *parser)
22681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22683 /* The `template' keyword can only be used within templates;
22684 outside templates the parser can always figure out what is a
22685 template and what is not. */
22686 if (!processing_template_decl)
22688 cp_token *token = cp_lexer_peek_token (parser->lexer);
22689 error_at (token->location,
22690 "%<template%> (as a disambiguator) is only allowed "
22691 "within templates");
22692 /* If this part of the token stream is rescanned, the same
22693 error message would be generated. So, we purge the token
22694 from the stream. */
22695 cp_lexer_purge_token (parser->lexer);
22696 return false;
22698 else
22700 /* Consume the `template' keyword. */
22701 cp_lexer_consume_token (parser->lexer);
22702 return true;
22706 return false;
22709 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22710 set PARSER->SCOPE, and perform other related actions. */
22712 static void
22713 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22715 int i;
22716 struct tree_check *check_value;
22717 deferred_access_check *chk;
22718 VEC (deferred_access_check,gc) *checks;
22720 /* Get the stored value. */
22721 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22722 /* Perform any access checks that were deferred. */
22723 checks = check_value->checks;
22724 if (checks)
22726 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22727 perform_or_defer_access_check (chk->binfo,
22728 chk->decl,
22729 chk->diag_decl, tf_warning_or_error);
22731 /* Set the scope from the stored value. */
22732 parser->scope = check_value->value;
22733 parser->qualifying_scope = check_value->qualifying_scope;
22734 parser->object_scope = NULL_TREE;
22737 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22738 encounter the end of a block before what we were looking for. */
22740 static bool
22741 cp_parser_cache_group (cp_parser *parser,
22742 enum cpp_ttype end,
22743 unsigned depth)
22745 while (true)
22747 cp_token *token = cp_lexer_peek_token (parser->lexer);
22749 /* Abort a parenthesized expression if we encounter a semicolon. */
22750 if ((end == CPP_CLOSE_PAREN || depth == 0)
22751 && token->type == CPP_SEMICOLON)
22752 return true;
22753 /* If we've reached the end of the file, stop. */
22754 if (token->type == CPP_EOF
22755 || (end != CPP_PRAGMA_EOL
22756 && token->type == CPP_PRAGMA_EOL))
22757 return true;
22758 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22759 /* We've hit the end of an enclosing block, so there's been some
22760 kind of syntax error. */
22761 return true;
22763 /* Consume the token. */
22764 cp_lexer_consume_token (parser->lexer);
22765 /* See if it starts a new group. */
22766 if (token->type == CPP_OPEN_BRACE)
22768 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22769 /* In theory this should probably check end == '}', but
22770 cp_parser_save_member_function_body needs it to exit
22771 after either '}' or ')' when called with ')'. */
22772 if (depth == 0)
22773 return false;
22775 else if (token->type == CPP_OPEN_PAREN)
22777 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22778 if (depth == 0 && end == CPP_CLOSE_PAREN)
22779 return false;
22781 else if (token->type == CPP_PRAGMA)
22782 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22783 else if (token->type == end)
22784 return false;
22788 /* Like above, for caching a default argument or NSDMI. Both of these are
22789 terminated by a non-nested comma, but it can be unclear whether or not a
22790 comma is nested in a template argument list unless we do more parsing.
22791 In order to handle this ambiguity, when we encounter a ',' after a '<'
22792 we try to parse what follows as a parameter-declaration-list (in the
22793 case of a default argument) or a member-declarator (in the case of an
22794 NSDMI). If that succeeds, then we stop caching. */
22796 static tree
22797 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22799 unsigned depth = 0;
22800 int maybe_template_id = 0;
22801 cp_token *first_token;
22802 cp_token *token;
22803 tree default_argument;
22805 /* Add tokens until we have processed the entire default
22806 argument. We add the range [first_token, token). */
22807 first_token = cp_lexer_peek_token (parser->lexer);
22808 if (first_token->type == CPP_OPEN_BRACE)
22810 /* For list-initialization, this is straightforward. */
22811 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22812 token = cp_lexer_peek_token (parser->lexer);
22814 else while (true)
22816 bool done = false;
22818 /* Peek at the next token. */
22819 token = cp_lexer_peek_token (parser->lexer);
22820 /* What we do depends on what token we have. */
22821 switch (token->type)
22823 /* In valid code, a default argument must be
22824 immediately followed by a `,' `)', or `...'. */
22825 case CPP_COMMA:
22826 if (depth == 0 && maybe_template_id)
22828 /* If we've seen a '<', we might be in a
22829 template-argument-list. Until Core issue 325 is
22830 resolved, we don't know how this situation ought
22831 to be handled, so try to DTRT. We check whether
22832 what comes after the comma is a valid parameter
22833 declaration list. If it is, then the comma ends
22834 the default argument; otherwise the default
22835 argument continues. */
22836 bool error = false;
22837 tree t;
22839 /* Set ITALP so cp_parser_parameter_declaration_list
22840 doesn't decide to commit to this parse. */
22841 bool saved_italp = parser->in_template_argument_list_p;
22842 parser->in_template_argument_list_p = true;
22844 cp_parser_parse_tentatively (parser);
22845 cp_lexer_consume_token (parser->lexer);
22847 if (nsdmi)
22849 int ctor_dtor_or_conv_p;
22850 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22851 &ctor_dtor_or_conv_p,
22852 /*parenthesized_p=*/NULL,
22853 /*member_p=*/true);
22855 else
22857 begin_scope (sk_function_parms, NULL_TREE);
22858 cp_parser_parameter_declaration_list (parser, &error);
22859 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22860 pop_binding (DECL_NAME (t), t);
22861 leave_scope ();
22863 if (!cp_parser_error_occurred (parser) && !error)
22864 done = true;
22865 cp_parser_abort_tentative_parse (parser);
22867 parser->in_template_argument_list_p = saved_italp;
22868 break;
22870 case CPP_CLOSE_PAREN:
22871 case CPP_ELLIPSIS:
22872 /* If we run into a non-nested `;', `}', or `]',
22873 then the code is invalid -- but the default
22874 argument is certainly over. */
22875 case CPP_SEMICOLON:
22876 case CPP_CLOSE_BRACE:
22877 case CPP_CLOSE_SQUARE:
22878 if (depth == 0)
22879 done = true;
22880 /* Update DEPTH, if necessary. */
22881 else if (token->type == CPP_CLOSE_PAREN
22882 || token->type == CPP_CLOSE_BRACE
22883 || token->type == CPP_CLOSE_SQUARE)
22884 --depth;
22885 break;
22887 case CPP_OPEN_PAREN:
22888 case CPP_OPEN_SQUARE:
22889 case CPP_OPEN_BRACE:
22890 ++depth;
22891 break;
22893 case CPP_LESS:
22894 if (depth == 0)
22895 /* This might be the comparison operator, or it might
22896 start a template argument list. */
22897 ++maybe_template_id;
22898 break;
22900 case CPP_RSHIFT:
22901 if (cxx_dialect == cxx98)
22902 break;
22903 /* Fall through for C++0x, which treats the `>>'
22904 operator like two `>' tokens in certain
22905 cases. */
22907 case CPP_GREATER:
22908 if (depth == 0)
22910 /* This might be an operator, or it might close a
22911 template argument list. But if a previous '<'
22912 started a template argument list, this will have
22913 closed it, so we can't be in one anymore. */
22914 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22915 if (maybe_template_id < 0)
22916 maybe_template_id = 0;
22918 break;
22920 /* If we run out of tokens, issue an error message. */
22921 case CPP_EOF:
22922 case CPP_PRAGMA_EOL:
22923 error_at (token->location, "file ends in default argument");
22924 done = true;
22925 break;
22927 case CPP_NAME:
22928 case CPP_SCOPE:
22929 /* In these cases, we should look for template-ids.
22930 For example, if the default argument is
22931 `X<int, double>()', we need to do name lookup to
22932 figure out whether or not `X' is a template; if
22933 so, the `,' does not end the default argument.
22935 That is not yet done. */
22936 break;
22938 default:
22939 break;
22942 /* If we've reached the end, stop. */
22943 if (done)
22944 break;
22946 /* Add the token to the token block. */
22947 token = cp_lexer_consume_token (parser->lexer);
22950 /* Create a DEFAULT_ARG to represent the unparsed default
22951 argument. */
22952 default_argument = make_node (DEFAULT_ARG);
22953 DEFARG_TOKENS (default_argument)
22954 = cp_token_cache_new (first_token, token);
22955 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22957 return default_argument;
22960 /* Begin parsing tentatively. We always save tokens while parsing
22961 tentatively so that if the tentative parsing fails we can restore the
22962 tokens. */
22964 static void
22965 cp_parser_parse_tentatively (cp_parser* parser)
22967 /* Enter a new parsing context. */
22968 parser->context = cp_parser_context_new (parser->context);
22969 /* Begin saving tokens. */
22970 cp_lexer_save_tokens (parser->lexer);
22971 /* In order to avoid repetitive access control error messages,
22972 access checks are queued up until we are no longer parsing
22973 tentatively. */
22974 push_deferring_access_checks (dk_deferred);
22977 /* Commit to the currently active tentative parse. */
22979 static void
22980 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22982 cp_parser_context *context;
22983 cp_lexer *lexer;
22985 /* Mark all of the levels as committed. */
22986 lexer = parser->lexer;
22987 for (context = parser->context; context->next; context = context->next)
22989 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22990 break;
22991 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22992 while (!cp_lexer_saving_tokens (lexer))
22993 lexer = lexer->next;
22994 cp_lexer_commit_tokens (lexer);
22998 /* Abort the currently active tentative parse. All consumed tokens
22999 will be rolled back, and no diagnostics will be issued. */
23001 static void
23002 cp_parser_abort_tentative_parse (cp_parser* parser)
23004 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23005 || errorcount > 0);
23006 cp_parser_simulate_error (parser);
23007 /* Now, pretend that we want to see if the construct was
23008 successfully parsed. */
23009 cp_parser_parse_definitely (parser);
23012 /* Stop parsing tentatively. If a parse error has occurred, restore the
23013 token stream. Otherwise, commit to the tokens we have consumed.
23014 Returns true if no error occurred; false otherwise. */
23016 static bool
23017 cp_parser_parse_definitely (cp_parser* parser)
23019 bool error_occurred;
23020 cp_parser_context *context;
23022 /* Remember whether or not an error occurred, since we are about to
23023 destroy that information. */
23024 error_occurred = cp_parser_error_occurred (parser);
23025 /* Remove the topmost context from the stack. */
23026 context = parser->context;
23027 parser->context = context->next;
23028 /* If no parse errors occurred, commit to the tentative parse. */
23029 if (!error_occurred)
23031 /* Commit to the tokens read tentatively, unless that was
23032 already done. */
23033 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23034 cp_lexer_commit_tokens (parser->lexer);
23036 pop_to_parent_deferring_access_checks ();
23038 /* Otherwise, if errors occurred, roll back our state so that things
23039 are just as they were before we began the tentative parse. */
23040 else
23042 cp_lexer_rollback_tokens (parser->lexer);
23043 pop_deferring_access_checks ();
23045 /* Add the context to the front of the free list. */
23046 context->next = cp_parser_context_free_list;
23047 cp_parser_context_free_list = context;
23049 return !error_occurred;
23052 /* Returns true if we are parsing tentatively and are not committed to
23053 this tentative parse. */
23055 static bool
23056 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23058 return (cp_parser_parsing_tentatively (parser)
23059 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23062 /* Returns nonzero iff an error has occurred during the most recent
23063 tentative parse. */
23065 static bool
23066 cp_parser_error_occurred (cp_parser* parser)
23068 return (cp_parser_parsing_tentatively (parser)
23069 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23072 /* Returns nonzero if GNU extensions are allowed. */
23074 static bool
23075 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23077 return parser->allow_gnu_extensions_p;
23080 /* Objective-C++ Productions */
23083 /* Parse an Objective-C expression, which feeds into a primary-expression
23084 above.
23086 objc-expression:
23087 objc-message-expression
23088 objc-string-literal
23089 objc-encode-expression
23090 objc-protocol-expression
23091 objc-selector-expression
23093 Returns a tree representation of the expression. */
23095 static tree
23096 cp_parser_objc_expression (cp_parser* parser)
23098 /* Try to figure out what kind of declaration is present. */
23099 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23101 switch (kwd->type)
23103 case CPP_OPEN_SQUARE:
23104 return cp_parser_objc_message_expression (parser);
23106 case CPP_OBJC_STRING:
23107 kwd = cp_lexer_consume_token (parser->lexer);
23108 return objc_build_string_object (kwd->u.value);
23110 case CPP_KEYWORD:
23111 switch (kwd->keyword)
23113 case RID_AT_ENCODE:
23114 return cp_parser_objc_encode_expression (parser);
23116 case RID_AT_PROTOCOL:
23117 return cp_parser_objc_protocol_expression (parser);
23119 case RID_AT_SELECTOR:
23120 return cp_parser_objc_selector_expression (parser);
23122 default:
23123 break;
23125 default:
23126 error_at (kwd->location,
23127 "misplaced %<@%D%> Objective-C++ construct",
23128 kwd->u.value);
23129 cp_parser_skip_to_end_of_block_or_statement (parser);
23132 return error_mark_node;
23135 /* Parse an Objective-C message expression.
23137 objc-message-expression:
23138 [ objc-message-receiver objc-message-args ]
23140 Returns a representation of an Objective-C message. */
23142 static tree
23143 cp_parser_objc_message_expression (cp_parser* parser)
23145 tree receiver, messageargs;
23147 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23148 receiver = cp_parser_objc_message_receiver (parser);
23149 messageargs = cp_parser_objc_message_args (parser);
23150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23152 return objc_build_message_expr (receiver, messageargs);
23155 /* Parse an objc-message-receiver.
23157 objc-message-receiver:
23158 expression
23159 simple-type-specifier
23161 Returns a representation of the type or expression. */
23163 static tree
23164 cp_parser_objc_message_receiver (cp_parser* parser)
23166 tree rcv;
23168 /* An Objective-C message receiver may be either (1) a type
23169 or (2) an expression. */
23170 cp_parser_parse_tentatively (parser);
23171 rcv = cp_parser_expression (parser, false, NULL);
23173 if (cp_parser_parse_definitely (parser))
23174 return rcv;
23176 rcv = cp_parser_simple_type_specifier (parser,
23177 /*decl_specs=*/NULL,
23178 CP_PARSER_FLAGS_NONE);
23180 return objc_get_class_reference (rcv);
23183 /* Parse the arguments and selectors comprising an Objective-C message.
23185 objc-message-args:
23186 objc-selector
23187 objc-selector-args
23188 objc-selector-args , objc-comma-args
23190 objc-selector-args:
23191 objc-selector [opt] : assignment-expression
23192 objc-selector-args objc-selector [opt] : assignment-expression
23194 objc-comma-args:
23195 assignment-expression
23196 objc-comma-args , assignment-expression
23198 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23199 selector arguments and TREE_VALUE containing a list of comma
23200 arguments. */
23202 static tree
23203 cp_parser_objc_message_args (cp_parser* parser)
23205 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23206 bool maybe_unary_selector_p = true;
23207 cp_token *token = cp_lexer_peek_token (parser->lexer);
23209 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23211 tree selector = NULL_TREE, arg;
23213 if (token->type != CPP_COLON)
23214 selector = cp_parser_objc_selector (parser);
23216 /* Detect if we have a unary selector. */
23217 if (maybe_unary_selector_p
23218 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23219 return build_tree_list (selector, NULL_TREE);
23221 maybe_unary_selector_p = false;
23222 cp_parser_require (parser, CPP_COLON, RT_COLON);
23223 arg = cp_parser_assignment_expression (parser, false, NULL);
23225 sel_args
23226 = chainon (sel_args,
23227 build_tree_list (selector, arg));
23229 token = cp_lexer_peek_token (parser->lexer);
23232 /* Handle non-selector arguments, if any. */
23233 while (token->type == CPP_COMMA)
23235 tree arg;
23237 cp_lexer_consume_token (parser->lexer);
23238 arg = cp_parser_assignment_expression (parser, false, NULL);
23240 addl_args
23241 = chainon (addl_args,
23242 build_tree_list (NULL_TREE, arg));
23244 token = cp_lexer_peek_token (parser->lexer);
23247 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23249 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23250 return build_tree_list (error_mark_node, error_mark_node);
23253 return build_tree_list (sel_args, addl_args);
23256 /* Parse an Objective-C encode expression.
23258 objc-encode-expression:
23259 @encode objc-typename
23261 Returns an encoded representation of the type argument. */
23263 static tree
23264 cp_parser_objc_encode_expression (cp_parser* parser)
23266 tree type;
23267 cp_token *token;
23269 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23270 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23271 token = cp_lexer_peek_token (parser->lexer);
23272 type = complete_type (cp_parser_type_id (parser));
23273 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23275 if (!type)
23277 error_at (token->location,
23278 "%<@encode%> must specify a type as an argument");
23279 return error_mark_node;
23282 /* This happens if we find @encode(T) (where T is a template
23283 typename or something dependent on a template typename) when
23284 parsing a template. In that case, we can't compile it
23285 immediately, but we rather create an AT_ENCODE_EXPR which will
23286 need to be instantiated when the template is used.
23288 if (dependent_type_p (type))
23290 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23291 TREE_READONLY (value) = 1;
23292 return value;
23295 return objc_build_encode_expr (type);
23298 /* Parse an Objective-C @defs expression. */
23300 static tree
23301 cp_parser_objc_defs_expression (cp_parser *parser)
23303 tree name;
23305 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23306 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23307 name = cp_parser_identifier (parser);
23308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23310 return objc_get_class_ivars (name);
23313 /* Parse an Objective-C protocol expression.
23315 objc-protocol-expression:
23316 @protocol ( identifier )
23318 Returns a representation of the protocol expression. */
23320 static tree
23321 cp_parser_objc_protocol_expression (cp_parser* parser)
23323 tree proto;
23325 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23326 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23327 proto = cp_parser_identifier (parser);
23328 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23330 return objc_build_protocol_expr (proto);
23333 /* Parse an Objective-C selector expression.
23335 objc-selector-expression:
23336 @selector ( objc-method-signature )
23338 objc-method-signature:
23339 objc-selector
23340 objc-selector-seq
23342 objc-selector-seq:
23343 objc-selector :
23344 objc-selector-seq objc-selector :
23346 Returns a representation of the method selector. */
23348 static tree
23349 cp_parser_objc_selector_expression (cp_parser* parser)
23351 tree sel_seq = NULL_TREE;
23352 bool maybe_unary_selector_p = true;
23353 cp_token *token;
23354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23356 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23357 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23358 token = cp_lexer_peek_token (parser->lexer);
23360 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23361 || token->type == CPP_SCOPE)
23363 tree selector = NULL_TREE;
23365 if (token->type != CPP_COLON
23366 || token->type == CPP_SCOPE)
23367 selector = cp_parser_objc_selector (parser);
23369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23370 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23372 /* Detect if we have a unary selector. */
23373 if (maybe_unary_selector_p)
23375 sel_seq = selector;
23376 goto finish_selector;
23378 else
23380 cp_parser_error (parser, "expected %<:%>");
23383 maybe_unary_selector_p = false;
23384 token = cp_lexer_consume_token (parser->lexer);
23386 if (token->type == CPP_SCOPE)
23388 sel_seq
23389 = chainon (sel_seq,
23390 build_tree_list (selector, NULL_TREE));
23391 sel_seq
23392 = chainon (sel_seq,
23393 build_tree_list (NULL_TREE, NULL_TREE));
23395 else
23396 sel_seq
23397 = chainon (sel_seq,
23398 build_tree_list (selector, NULL_TREE));
23400 token = cp_lexer_peek_token (parser->lexer);
23403 finish_selector:
23404 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23406 return objc_build_selector_expr (loc, sel_seq);
23409 /* Parse a list of identifiers.
23411 objc-identifier-list:
23412 identifier
23413 objc-identifier-list , identifier
23415 Returns a TREE_LIST of identifier nodes. */
23417 static tree
23418 cp_parser_objc_identifier_list (cp_parser* parser)
23420 tree identifier;
23421 tree list;
23422 cp_token *sep;
23424 identifier = cp_parser_identifier (parser);
23425 if (identifier == error_mark_node)
23426 return error_mark_node;
23428 list = build_tree_list (NULL_TREE, identifier);
23429 sep = cp_lexer_peek_token (parser->lexer);
23431 while (sep->type == CPP_COMMA)
23433 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23434 identifier = cp_parser_identifier (parser);
23435 if (identifier == error_mark_node)
23436 return list;
23438 list = chainon (list, build_tree_list (NULL_TREE,
23439 identifier));
23440 sep = cp_lexer_peek_token (parser->lexer);
23443 return list;
23446 /* Parse an Objective-C alias declaration.
23448 objc-alias-declaration:
23449 @compatibility_alias identifier identifier ;
23451 This function registers the alias mapping with the Objective-C front end.
23452 It returns nothing. */
23454 static void
23455 cp_parser_objc_alias_declaration (cp_parser* parser)
23457 tree alias, orig;
23459 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23460 alias = cp_parser_identifier (parser);
23461 orig = cp_parser_identifier (parser);
23462 objc_declare_alias (alias, orig);
23463 cp_parser_consume_semicolon_at_end_of_statement (parser);
23466 /* Parse an Objective-C class forward-declaration.
23468 objc-class-declaration:
23469 @class objc-identifier-list ;
23471 The function registers the forward declarations with the Objective-C
23472 front end. It returns nothing. */
23474 static void
23475 cp_parser_objc_class_declaration (cp_parser* parser)
23477 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23478 while (true)
23480 tree id;
23482 id = cp_parser_identifier (parser);
23483 if (id == error_mark_node)
23484 break;
23486 objc_declare_class (id);
23488 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23489 cp_lexer_consume_token (parser->lexer);
23490 else
23491 break;
23493 cp_parser_consume_semicolon_at_end_of_statement (parser);
23496 /* Parse a list of Objective-C protocol references.
23498 objc-protocol-refs-opt:
23499 objc-protocol-refs [opt]
23501 objc-protocol-refs:
23502 < objc-identifier-list >
23504 Returns a TREE_LIST of identifiers, if any. */
23506 static tree
23507 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23509 tree protorefs = NULL_TREE;
23511 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23513 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23514 protorefs = cp_parser_objc_identifier_list (parser);
23515 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23518 return protorefs;
23521 /* Parse a Objective-C visibility specification. */
23523 static void
23524 cp_parser_objc_visibility_spec (cp_parser* parser)
23526 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23528 switch (vis->keyword)
23530 case RID_AT_PRIVATE:
23531 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23532 break;
23533 case RID_AT_PROTECTED:
23534 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23535 break;
23536 case RID_AT_PUBLIC:
23537 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23538 break;
23539 case RID_AT_PACKAGE:
23540 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23541 break;
23542 default:
23543 return;
23546 /* Eat '@private'/'@protected'/'@public'. */
23547 cp_lexer_consume_token (parser->lexer);
23550 /* Parse an Objective-C method type. Return 'true' if it is a class
23551 (+) method, and 'false' if it is an instance (-) method. */
23553 static inline bool
23554 cp_parser_objc_method_type (cp_parser* parser)
23556 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23557 return true;
23558 else
23559 return false;
23562 /* Parse an Objective-C protocol qualifier. */
23564 static tree
23565 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23567 tree quals = NULL_TREE, node;
23568 cp_token *token = cp_lexer_peek_token (parser->lexer);
23570 node = token->u.value;
23572 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23573 && (node == ridpointers [(int) RID_IN]
23574 || node == ridpointers [(int) RID_OUT]
23575 || node == ridpointers [(int) RID_INOUT]
23576 || node == ridpointers [(int) RID_BYCOPY]
23577 || node == ridpointers [(int) RID_BYREF]
23578 || node == ridpointers [(int) RID_ONEWAY]))
23580 quals = tree_cons (NULL_TREE, node, quals);
23581 cp_lexer_consume_token (parser->lexer);
23582 token = cp_lexer_peek_token (parser->lexer);
23583 node = token->u.value;
23586 return quals;
23589 /* Parse an Objective-C typename. */
23591 static tree
23592 cp_parser_objc_typename (cp_parser* parser)
23594 tree type_name = NULL_TREE;
23596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23598 tree proto_quals, cp_type = NULL_TREE;
23600 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23601 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23603 /* An ObjC type name may consist of just protocol qualifiers, in which
23604 case the type shall default to 'id'. */
23605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23607 cp_type = cp_parser_type_id (parser);
23609 /* If the type could not be parsed, an error has already
23610 been produced. For error recovery, behave as if it had
23611 not been specified, which will use the default type
23612 'id'. */
23613 if (cp_type == error_mark_node)
23615 cp_type = NULL_TREE;
23616 /* We need to skip to the closing parenthesis as
23617 cp_parser_type_id() does not seem to do it for
23618 us. */
23619 cp_parser_skip_to_closing_parenthesis (parser,
23620 /*recovering=*/true,
23621 /*or_comma=*/false,
23622 /*consume_paren=*/false);
23626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23627 type_name = build_tree_list (proto_quals, cp_type);
23630 return type_name;
23633 /* Check to see if TYPE refers to an Objective-C selector name. */
23635 static bool
23636 cp_parser_objc_selector_p (enum cpp_ttype type)
23638 return (type == CPP_NAME || type == CPP_KEYWORD
23639 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23640 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23641 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23642 || type == CPP_XOR || type == CPP_XOR_EQ);
23645 /* Parse an Objective-C selector. */
23647 static tree
23648 cp_parser_objc_selector (cp_parser* parser)
23650 cp_token *token = cp_lexer_consume_token (parser->lexer);
23652 if (!cp_parser_objc_selector_p (token->type))
23654 error_at (token->location, "invalid Objective-C++ selector name");
23655 return error_mark_node;
23658 /* C++ operator names are allowed to appear in ObjC selectors. */
23659 switch (token->type)
23661 case CPP_AND_AND: return get_identifier ("and");
23662 case CPP_AND_EQ: return get_identifier ("and_eq");
23663 case CPP_AND: return get_identifier ("bitand");
23664 case CPP_OR: return get_identifier ("bitor");
23665 case CPP_COMPL: return get_identifier ("compl");
23666 case CPP_NOT: return get_identifier ("not");
23667 case CPP_NOT_EQ: return get_identifier ("not_eq");
23668 case CPP_OR_OR: return get_identifier ("or");
23669 case CPP_OR_EQ: return get_identifier ("or_eq");
23670 case CPP_XOR: return get_identifier ("xor");
23671 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23672 default: return token->u.value;
23676 /* Parse an Objective-C params list. */
23678 static tree
23679 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23681 tree params = NULL_TREE;
23682 bool maybe_unary_selector_p = true;
23683 cp_token *token = cp_lexer_peek_token (parser->lexer);
23685 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23687 tree selector = NULL_TREE, type_name, identifier;
23688 tree parm_attr = NULL_TREE;
23690 if (token->keyword == RID_ATTRIBUTE)
23691 break;
23693 if (token->type != CPP_COLON)
23694 selector = cp_parser_objc_selector (parser);
23696 /* Detect if we have a unary selector. */
23697 if (maybe_unary_selector_p
23698 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23700 params = selector; /* Might be followed by attributes. */
23701 break;
23704 maybe_unary_selector_p = false;
23705 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23707 /* Something went quite wrong. There should be a colon
23708 here, but there is not. Stop parsing parameters. */
23709 break;
23711 type_name = cp_parser_objc_typename (parser);
23712 /* New ObjC allows attributes on parameters too. */
23713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23714 parm_attr = cp_parser_attributes_opt (parser);
23715 identifier = cp_parser_identifier (parser);
23717 params
23718 = chainon (params,
23719 objc_build_keyword_decl (selector,
23720 type_name,
23721 identifier,
23722 parm_attr));
23724 token = cp_lexer_peek_token (parser->lexer);
23727 if (params == NULL_TREE)
23729 cp_parser_error (parser, "objective-c++ method declaration is expected");
23730 return error_mark_node;
23733 /* We allow tail attributes for the method. */
23734 if (token->keyword == RID_ATTRIBUTE)
23736 *attributes = cp_parser_attributes_opt (parser);
23737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23738 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23739 return params;
23740 cp_parser_error (parser,
23741 "method attributes must be specified at the end");
23742 return error_mark_node;
23745 if (params == NULL_TREE)
23747 cp_parser_error (parser, "objective-c++ method declaration is expected");
23748 return error_mark_node;
23750 return params;
23753 /* Parse the non-keyword Objective-C params. */
23755 static tree
23756 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23757 tree* attributes)
23759 tree params = make_node (TREE_LIST);
23760 cp_token *token = cp_lexer_peek_token (parser->lexer);
23761 *ellipsisp = false; /* Initially, assume no ellipsis. */
23763 while (token->type == CPP_COMMA)
23765 cp_parameter_declarator *parmdecl;
23766 tree parm;
23768 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23769 token = cp_lexer_peek_token (parser->lexer);
23771 if (token->type == CPP_ELLIPSIS)
23773 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23774 *ellipsisp = true;
23775 token = cp_lexer_peek_token (parser->lexer);
23776 break;
23779 /* TODO: parse attributes for tail parameters. */
23780 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23781 parm = grokdeclarator (parmdecl->declarator,
23782 &parmdecl->decl_specifiers,
23783 PARM, /*initialized=*/0,
23784 /*attrlist=*/NULL);
23786 chainon (params, build_tree_list (NULL_TREE, parm));
23787 token = cp_lexer_peek_token (parser->lexer);
23790 /* We allow tail attributes for the method. */
23791 if (token->keyword == RID_ATTRIBUTE)
23793 if (*attributes == NULL_TREE)
23795 *attributes = cp_parser_attributes_opt (parser);
23796 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23797 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23798 return params;
23800 else
23801 /* We have an error, but parse the attributes, so that we can
23802 carry on. */
23803 *attributes = cp_parser_attributes_opt (parser);
23805 cp_parser_error (parser,
23806 "method attributes must be specified at the end");
23807 return error_mark_node;
23810 return params;
23813 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23815 static void
23816 cp_parser_objc_interstitial_code (cp_parser* parser)
23818 cp_token *token = cp_lexer_peek_token (parser->lexer);
23820 /* If the next token is `extern' and the following token is a string
23821 literal, then we have a linkage specification. */
23822 if (token->keyword == RID_EXTERN
23823 && cp_parser_is_pure_string_literal
23824 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23825 cp_parser_linkage_specification (parser);
23826 /* Handle #pragma, if any. */
23827 else if (token->type == CPP_PRAGMA)
23828 cp_parser_pragma (parser, pragma_external);
23829 /* Allow stray semicolons. */
23830 else if (token->type == CPP_SEMICOLON)
23831 cp_lexer_consume_token (parser->lexer);
23832 /* Mark methods as optional or required, when building protocols. */
23833 else if (token->keyword == RID_AT_OPTIONAL)
23835 cp_lexer_consume_token (parser->lexer);
23836 objc_set_method_opt (true);
23838 else if (token->keyword == RID_AT_REQUIRED)
23840 cp_lexer_consume_token (parser->lexer);
23841 objc_set_method_opt (false);
23843 else if (token->keyword == RID_NAMESPACE)
23844 cp_parser_namespace_definition (parser);
23845 /* Other stray characters must generate errors. */
23846 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23848 cp_lexer_consume_token (parser->lexer);
23849 error ("stray %qs between Objective-C++ methods",
23850 token->type == CPP_OPEN_BRACE ? "{" : "}");
23852 /* Finally, try to parse a block-declaration, or a function-definition. */
23853 else
23854 cp_parser_block_declaration (parser, /*statement_p=*/false);
23857 /* Parse a method signature. */
23859 static tree
23860 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23862 tree rettype, kwdparms, optparms;
23863 bool ellipsis = false;
23864 bool is_class_method;
23866 is_class_method = cp_parser_objc_method_type (parser);
23867 rettype = cp_parser_objc_typename (parser);
23868 *attributes = NULL_TREE;
23869 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23870 if (kwdparms == error_mark_node)
23871 return error_mark_node;
23872 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23873 if (optparms == error_mark_node)
23874 return error_mark_node;
23876 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23879 static bool
23880 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23882 tree tattr;
23883 cp_lexer_save_tokens (parser->lexer);
23884 tattr = cp_parser_attributes_opt (parser);
23885 gcc_assert (tattr) ;
23887 /* If the attributes are followed by a method introducer, this is not allowed.
23888 Dump the attributes and flag the situation. */
23889 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23890 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23891 return true;
23893 /* Otherwise, the attributes introduce some interstitial code, possibly so
23894 rewind to allow that check. */
23895 cp_lexer_rollback_tokens (parser->lexer);
23896 return false;
23899 /* Parse an Objective-C method prototype list. */
23901 static void
23902 cp_parser_objc_method_prototype_list (cp_parser* parser)
23904 cp_token *token = cp_lexer_peek_token (parser->lexer);
23906 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23908 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23910 tree attributes, sig;
23911 bool is_class_method;
23912 if (token->type == CPP_PLUS)
23913 is_class_method = true;
23914 else
23915 is_class_method = false;
23916 sig = cp_parser_objc_method_signature (parser, &attributes);
23917 if (sig == error_mark_node)
23919 cp_parser_skip_to_end_of_block_or_statement (parser);
23920 token = cp_lexer_peek_token (parser->lexer);
23921 continue;
23923 objc_add_method_declaration (is_class_method, sig, attributes);
23924 cp_parser_consume_semicolon_at_end_of_statement (parser);
23926 else if (token->keyword == RID_AT_PROPERTY)
23927 cp_parser_objc_at_property_declaration (parser);
23928 else if (token->keyword == RID_ATTRIBUTE
23929 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23930 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23931 OPT_Wattributes,
23932 "prefix attributes are ignored for methods");
23933 else
23934 /* Allow for interspersed non-ObjC++ code. */
23935 cp_parser_objc_interstitial_code (parser);
23937 token = cp_lexer_peek_token (parser->lexer);
23940 if (token->type != CPP_EOF)
23941 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23942 else
23943 cp_parser_error (parser, "expected %<@end%>");
23945 objc_finish_interface ();
23948 /* Parse an Objective-C method definition list. */
23950 static void
23951 cp_parser_objc_method_definition_list (cp_parser* parser)
23953 cp_token *token = cp_lexer_peek_token (parser->lexer);
23955 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23957 tree meth;
23959 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23961 cp_token *ptk;
23962 tree sig, attribute;
23963 bool is_class_method;
23964 if (token->type == CPP_PLUS)
23965 is_class_method = true;
23966 else
23967 is_class_method = false;
23968 push_deferring_access_checks (dk_deferred);
23969 sig = cp_parser_objc_method_signature (parser, &attribute);
23970 if (sig == error_mark_node)
23972 cp_parser_skip_to_end_of_block_or_statement (parser);
23973 token = cp_lexer_peek_token (parser->lexer);
23974 continue;
23976 objc_start_method_definition (is_class_method, sig, attribute,
23977 NULL_TREE);
23979 /* For historical reasons, we accept an optional semicolon. */
23980 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23981 cp_lexer_consume_token (parser->lexer);
23983 ptk = cp_lexer_peek_token (parser->lexer);
23984 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23985 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23987 perform_deferred_access_checks (tf_warning_or_error);
23988 stop_deferring_access_checks ();
23989 meth = cp_parser_function_definition_after_declarator (parser,
23990 false);
23991 pop_deferring_access_checks ();
23992 objc_finish_method_definition (meth);
23995 /* The following case will be removed once @synthesize is
23996 completely implemented. */
23997 else if (token->keyword == RID_AT_PROPERTY)
23998 cp_parser_objc_at_property_declaration (parser);
23999 else if (token->keyword == RID_AT_SYNTHESIZE)
24000 cp_parser_objc_at_synthesize_declaration (parser);
24001 else if (token->keyword == RID_AT_DYNAMIC)
24002 cp_parser_objc_at_dynamic_declaration (parser);
24003 else if (token->keyword == RID_ATTRIBUTE
24004 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24005 warning_at (token->location, OPT_Wattributes,
24006 "prefix attributes are ignored for methods");
24007 else
24008 /* Allow for interspersed non-ObjC++ code. */
24009 cp_parser_objc_interstitial_code (parser);
24011 token = cp_lexer_peek_token (parser->lexer);
24014 if (token->type != CPP_EOF)
24015 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24016 else
24017 cp_parser_error (parser, "expected %<@end%>");
24019 objc_finish_implementation ();
24022 /* Parse Objective-C ivars. */
24024 static void
24025 cp_parser_objc_class_ivars (cp_parser* parser)
24027 cp_token *token = cp_lexer_peek_token (parser->lexer);
24029 if (token->type != CPP_OPEN_BRACE)
24030 return; /* No ivars specified. */
24032 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24033 token = cp_lexer_peek_token (parser->lexer);
24035 while (token->type != CPP_CLOSE_BRACE
24036 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24038 cp_decl_specifier_seq declspecs;
24039 int decl_class_or_enum_p;
24040 tree prefix_attributes;
24042 cp_parser_objc_visibility_spec (parser);
24044 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24045 break;
24047 cp_parser_decl_specifier_seq (parser,
24048 CP_PARSER_FLAGS_OPTIONAL,
24049 &declspecs,
24050 &decl_class_or_enum_p);
24052 /* auto, register, static, extern, mutable. */
24053 if (declspecs.storage_class != sc_none)
24055 cp_parser_error (parser, "invalid type for instance variable");
24056 declspecs.storage_class = sc_none;
24059 /* __thread. */
24060 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24062 cp_parser_error (parser, "invalid type for instance variable");
24063 declspecs.locations[ds_thread] = 0;
24066 /* typedef. */
24067 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24069 cp_parser_error (parser, "invalid type for instance variable");
24070 declspecs.locations[ds_thread] = 0;
24073 prefix_attributes = declspecs.attributes;
24074 declspecs.attributes = NULL_TREE;
24076 /* Keep going until we hit the `;' at the end of the
24077 declaration. */
24078 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24080 tree width = NULL_TREE, attributes, first_attribute, decl;
24081 cp_declarator *declarator = NULL;
24082 int ctor_dtor_or_conv_p;
24084 /* Check for a (possibly unnamed) bitfield declaration. */
24085 token = cp_lexer_peek_token (parser->lexer);
24086 if (token->type == CPP_COLON)
24087 goto eat_colon;
24089 if (token->type == CPP_NAME
24090 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24091 == CPP_COLON))
24093 /* Get the name of the bitfield. */
24094 declarator = make_id_declarator (NULL_TREE,
24095 cp_parser_identifier (parser),
24096 sfk_none);
24098 eat_colon:
24099 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24100 /* Get the width of the bitfield. */
24101 width
24102 = cp_parser_constant_expression (parser,
24103 /*allow_non_constant=*/false,
24104 NULL);
24106 else
24108 /* Parse the declarator. */
24109 declarator
24110 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24111 &ctor_dtor_or_conv_p,
24112 /*parenthesized_p=*/NULL,
24113 /*member_p=*/false);
24116 /* Look for attributes that apply to the ivar. */
24117 attributes = cp_parser_attributes_opt (parser);
24118 /* Remember which attributes are prefix attributes and
24119 which are not. */
24120 first_attribute = attributes;
24121 /* Combine the attributes. */
24122 attributes = chainon (prefix_attributes, attributes);
24124 if (width)
24125 /* Create the bitfield declaration. */
24126 decl = grokbitfield (declarator, &declspecs,
24127 width,
24128 attributes);
24129 else
24130 decl = grokfield (declarator, &declspecs,
24131 NULL_TREE, /*init_const_expr_p=*/false,
24132 NULL_TREE, attributes);
24134 /* Add the instance variable. */
24135 if (decl != error_mark_node && decl != NULL_TREE)
24136 objc_add_instance_variable (decl);
24138 /* Reset PREFIX_ATTRIBUTES. */
24139 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24140 attributes = TREE_CHAIN (attributes);
24141 if (attributes)
24142 TREE_CHAIN (attributes) = NULL_TREE;
24144 token = cp_lexer_peek_token (parser->lexer);
24146 if (token->type == CPP_COMMA)
24148 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24149 continue;
24151 break;
24154 cp_parser_consume_semicolon_at_end_of_statement (parser);
24155 token = cp_lexer_peek_token (parser->lexer);
24158 if (token->keyword == RID_AT_END)
24159 cp_parser_error (parser, "expected %<}%>");
24161 /* Do not consume the RID_AT_END, so it will be read again as terminating
24162 the @interface of @implementation. */
24163 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24164 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24166 /* For historical reasons, we accept an optional semicolon. */
24167 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24168 cp_lexer_consume_token (parser->lexer);
24171 /* Parse an Objective-C protocol declaration. */
24173 static void
24174 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24176 tree proto, protorefs;
24177 cp_token *tok;
24179 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24182 tok = cp_lexer_peek_token (parser->lexer);
24183 error_at (tok->location, "identifier expected after %<@protocol%>");
24184 cp_parser_consume_semicolon_at_end_of_statement (parser);
24185 return;
24188 /* See if we have a forward declaration or a definition. */
24189 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24191 /* Try a forward declaration first. */
24192 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24194 while (true)
24196 tree id;
24198 id = cp_parser_identifier (parser);
24199 if (id == error_mark_node)
24200 break;
24202 objc_declare_protocol (id, attributes);
24204 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24205 cp_lexer_consume_token (parser->lexer);
24206 else
24207 break;
24209 cp_parser_consume_semicolon_at_end_of_statement (parser);
24212 /* Ok, we got a full-fledged definition (or at least should). */
24213 else
24215 proto = cp_parser_identifier (parser);
24216 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24217 objc_start_protocol (proto, protorefs, attributes);
24218 cp_parser_objc_method_prototype_list (parser);
24222 /* Parse an Objective-C superclass or category. */
24224 static void
24225 cp_parser_objc_superclass_or_category (cp_parser *parser,
24226 bool iface_p,
24227 tree *super,
24228 tree *categ, bool *is_class_extension)
24230 cp_token *next = cp_lexer_peek_token (parser->lexer);
24232 *super = *categ = NULL_TREE;
24233 *is_class_extension = false;
24234 if (next->type == CPP_COLON)
24236 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24237 *super = cp_parser_identifier (parser);
24239 else if (next->type == CPP_OPEN_PAREN)
24241 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24243 /* If there is no category name, and this is an @interface, we
24244 have a class extension. */
24245 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24247 *categ = NULL_TREE;
24248 *is_class_extension = true;
24250 else
24251 *categ = cp_parser_identifier (parser);
24253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24257 /* Parse an Objective-C class interface. */
24259 static void
24260 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24262 tree name, super, categ, protos;
24263 bool is_class_extension;
24265 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24266 name = cp_parser_identifier (parser);
24267 if (name == error_mark_node)
24269 /* It's hard to recover because even if valid @interface stuff
24270 is to follow, we can't compile it (or validate it) if we
24271 don't even know which class it refers to. Let's assume this
24272 was a stray '@interface' token in the stream and skip it.
24274 return;
24276 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24277 &is_class_extension);
24278 protos = cp_parser_objc_protocol_refs_opt (parser);
24280 /* We have either a class or a category on our hands. */
24281 if (categ || is_class_extension)
24282 objc_start_category_interface (name, categ, protos, attributes);
24283 else
24285 objc_start_class_interface (name, super, protos, attributes);
24286 /* Handle instance variable declarations, if any. */
24287 cp_parser_objc_class_ivars (parser);
24288 objc_continue_interface ();
24291 cp_parser_objc_method_prototype_list (parser);
24294 /* Parse an Objective-C class implementation. */
24296 static void
24297 cp_parser_objc_class_implementation (cp_parser* parser)
24299 tree name, super, categ;
24300 bool is_class_extension;
24302 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24303 name = cp_parser_identifier (parser);
24304 if (name == error_mark_node)
24306 /* It's hard to recover because even if valid @implementation
24307 stuff is to follow, we can't compile it (or validate it) if
24308 we don't even know which class it refers to. Let's assume
24309 this was a stray '@implementation' token in the stream and
24310 skip it.
24312 return;
24314 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24315 &is_class_extension);
24317 /* We have either a class or a category on our hands. */
24318 if (categ)
24319 objc_start_category_implementation (name, categ);
24320 else
24322 objc_start_class_implementation (name, super);
24323 /* Handle instance variable declarations, if any. */
24324 cp_parser_objc_class_ivars (parser);
24325 objc_continue_implementation ();
24328 cp_parser_objc_method_definition_list (parser);
24331 /* Consume the @end token and finish off the implementation. */
24333 static void
24334 cp_parser_objc_end_implementation (cp_parser* parser)
24336 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24337 objc_finish_implementation ();
24340 /* Parse an Objective-C declaration. */
24342 static void
24343 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24345 /* Try to figure out what kind of declaration is present. */
24346 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24348 if (attributes)
24349 switch (kwd->keyword)
24351 case RID_AT_ALIAS:
24352 case RID_AT_CLASS:
24353 case RID_AT_END:
24354 error_at (kwd->location, "attributes may not be specified before"
24355 " the %<@%D%> Objective-C++ keyword",
24356 kwd->u.value);
24357 attributes = NULL;
24358 break;
24359 case RID_AT_IMPLEMENTATION:
24360 warning_at (kwd->location, OPT_Wattributes,
24361 "prefix attributes are ignored before %<@%D%>",
24362 kwd->u.value);
24363 attributes = NULL;
24364 default:
24365 break;
24368 switch (kwd->keyword)
24370 case RID_AT_ALIAS:
24371 cp_parser_objc_alias_declaration (parser);
24372 break;
24373 case RID_AT_CLASS:
24374 cp_parser_objc_class_declaration (parser);
24375 break;
24376 case RID_AT_PROTOCOL:
24377 cp_parser_objc_protocol_declaration (parser, attributes);
24378 break;
24379 case RID_AT_INTERFACE:
24380 cp_parser_objc_class_interface (parser, attributes);
24381 break;
24382 case RID_AT_IMPLEMENTATION:
24383 cp_parser_objc_class_implementation (parser);
24384 break;
24385 case RID_AT_END:
24386 cp_parser_objc_end_implementation (parser);
24387 break;
24388 default:
24389 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24390 kwd->u.value);
24391 cp_parser_skip_to_end_of_block_or_statement (parser);
24395 /* Parse an Objective-C try-catch-finally statement.
24397 objc-try-catch-finally-stmt:
24398 @try compound-statement objc-catch-clause-seq [opt]
24399 objc-finally-clause [opt]
24401 objc-catch-clause-seq:
24402 objc-catch-clause objc-catch-clause-seq [opt]
24404 objc-catch-clause:
24405 @catch ( objc-exception-declaration ) compound-statement
24407 objc-finally-clause:
24408 @finally compound-statement
24410 objc-exception-declaration:
24411 parameter-declaration
24412 '...'
24414 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24416 Returns NULL_TREE.
24418 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24419 for C. Keep them in sync. */
24421 static tree
24422 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24424 location_t location;
24425 tree stmt;
24427 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24428 location = cp_lexer_peek_token (parser->lexer)->location;
24429 objc_maybe_warn_exceptions (location);
24430 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24431 node, lest it get absorbed into the surrounding block. */
24432 stmt = push_stmt_list ();
24433 cp_parser_compound_statement (parser, NULL, false, false);
24434 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24436 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24438 cp_parameter_declarator *parm;
24439 tree parameter_declaration = error_mark_node;
24440 bool seen_open_paren = false;
24442 cp_lexer_consume_token (parser->lexer);
24443 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24444 seen_open_paren = true;
24445 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24447 /* We have "@catch (...)" (where the '...' are literally
24448 what is in the code). Skip the '...'.
24449 parameter_declaration is set to NULL_TREE, and
24450 objc_being_catch_clauses() knows that that means
24451 '...'. */
24452 cp_lexer_consume_token (parser->lexer);
24453 parameter_declaration = NULL_TREE;
24455 else
24457 /* We have "@catch (NSException *exception)" or something
24458 like that. Parse the parameter declaration. */
24459 parm = cp_parser_parameter_declaration (parser, false, NULL);
24460 if (parm == NULL)
24461 parameter_declaration = error_mark_node;
24462 else
24463 parameter_declaration = grokdeclarator (parm->declarator,
24464 &parm->decl_specifiers,
24465 PARM, /*initialized=*/0,
24466 /*attrlist=*/NULL);
24468 if (seen_open_paren)
24469 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24470 else
24472 /* If there was no open parenthesis, we are recovering from
24473 an error, and we are trying to figure out what mistake
24474 the user has made. */
24476 /* If there is an immediate closing parenthesis, the user
24477 probably forgot the opening one (ie, they typed "@catch
24478 NSException *e)". Parse the closing parenthesis and keep
24479 going. */
24480 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24481 cp_lexer_consume_token (parser->lexer);
24483 /* If these is no immediate closing parenthesis, the user
24484 probably doesn't know that parenthesis are required at
24485 all (ie, they typed "@catch NSException *e"). So, just
24486 forget about the closing parenthesis and keep going. */
24488 objc_begin_catch_clause (parameter_declaration);
24489 cp_parser_compound_statement (parser, NULL, false, false);
24490 objc_finish_catch_clause ();
24492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24494 cp_lexer_consume_token (parser->lexer);
24495 location = cp_lexer_peek_token (parser->lexer)->location;
24496 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24497 node, lest it get absorbed into the surrounding block. */
24498 stmt = push_stmt_list ();
24499 cp_parser_compound_statement (parser, NULL, false, false);
24500 objc_build_finally_clause (location, pop_stmt_list (stmt));
24503 return objc_finish_try_stmt ();
24506 /* Parse an Objective-C synchronized statement.
24508 objc-synchronized-stmt:
24509 @synchronized ( expression ) compound-statement
24511 Returns NULL_TREE. */
24513 static tree
24514 cp_parser_objc_synchronized_statement (cp_parser *parser)
24516 location_t location;
24517 tree lock, stmt;
24519 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24521 location = cp_lexer_peek_token (parser->lexer)->location;
24522 objc_maybe_warn_exceptions (location);
24523 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24524 lock = cp_parser_expression (parser, false, NULL);
24525 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24527 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24528 node, lest it get absorbed into the surrounding block. */
24529 stmt = push_stmt_list ();
24530 cp_parser_compound_statement (parser, NULL, false, false);
24532 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24535 /* Parse an Objective-C throw statement.
24537 objc-throw-stmt:
24538 @throw assignment-expression [opt] ;
24540 Returns a constructed '@throw' statement. */
24542 static tree
24543 cp_parser_objc_throw_statement (cp_parser *parser)
24545 tree expr = NULL_TREE;
24546 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24548 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24551 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24553 cp_parser_consume_semicolon_at_end_of_statement (parser);
24555 return objc_build_throw_stmt (loc, expr);
24558 /* Parse an Objective-C statement. */
24560 static tree
24561 cp_parser_objc_statement (cp_parser * parser)
24563 /* Try to figure out what kind of declaration is present. */
24564 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24566 switch (kwd->keyword)
24568 case RID_AT_TRY:
24569 return cp_parser_objc_try_catch_finally_statement (parser);
24570 case RID_AT_SYNCHRONIZED:
24571 return cp_parser_objc_synchronized_statement (parser);
24572 case RID_AT_THROW:
24573 return cp_parser_objc_throw_statement (parser);
24574 default:
24575 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24576 kwd->u.value);
24577 cp_parser_skip_to_end_of_block_or_statement (parser);
24580 return error_mark_node;
24583 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24584 look ahead to see if an objc keyword follows the attributes. This
24585 is to detect the use of prefix attributes on ObjC @interface and
24586 @protocol. */
24588 static bool
24589 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24591 cp_lexer_save_tokens (parser->lexer);
24592 *attrib = cp_parser_attributes_opt (parser);
24593 gcc_assert (*attrib);
24594 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24596 cp_lexer_commit_tokens (parser->lexer);
24597 return true;
24599 cp_lexer_rollback_tokens (parser->lexer);
24600 return false;
24603 /* This routine is a minimal replacement for
24604 c_parser_struct_declaration () used when parsing the list of
24605 types/names or ObjC++ properties. For example, when parsing the
24606 code
24608 @property (readonly) int a, b, c;
24610 this function is responsible for parsing "int a, int b, int c" and
24611 returning the declarations as CHAIN of DECLs.
24613 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24614 similar parsing. */
24615 static tree
24616 cp_parser_objc_struct_declaration (cp_parser *parser)
24618 tree decls = NULL_TREE;
24619 cp_decl_specifier_seq declspecs;
24620 int decl_class_or_enum_p;
24621 tree prefix_attributes;
24623 cp_parser_decl_specifier_seq (parser,
24624 CP_PARSER_FLAGS_NONE,
24625 &declspecs,
24626 &decl_class_or_enum_p);
24628 if (declspecs.type == error_mark_node)
24629 return error_mark_node;
24631 /* auto, register, static, extern, mutable. */
24632 if (declspecs.storage_class != sc_none)
24634 cp_parser_error (parser, "invalid type for property");
24635 declspecs.storage_class = sc_none;
24638 /* __thread. */
24639 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24641 cp_parser_error (parser, "invalid type for property");
24642 declspecs.locations[ds_thread] = 0;
24645 /* typedef. */
24646 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24648 cp_parser_error (parser, "invalid type for property");
24649 declspecs.locations[ds_typedef] = 0;
24652 prefix_attributes = declspecs.attributes;
24653 declspecs.attributes = NULL_TREE;
24655 /* Keep going until we hit the `;' at the end of the declaration. */
24656 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24658 tree attributes, first_attribute, decl;
24659 cp_declarator *declarator;
24660 cp_token *token;
24662 /* Parse the declarator. */
24663 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24664 NULL, NULL, false);
24666 /* Look for attributes that apply to the ivar. */
24667 attributes = cp_parser_attributes_opt (parser);
24668 /* Remember which attributes are prefix attributes and
24669 which are not. */
24670 first_attribute = attributes;
24671 /* Combine the attributes. */
24672 attributes = chainon (prefix_attributes, attributes);
24674 decl = grokfield (declarator, &declspecs,
24675 NULL_TREE, /*init_const_expr_p=*/false,
24676 NULL_TREE, attributes);
24678 if (decl == error_mark_node || decl == NULL_TREE)
24679 return error_mark_node;
24681 /* Reset PREFIX_ATTRIBUTES. */
24682 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24683 attributes = TREE_CHAIN (attributes);
24684 if (attributes)
24685 TREE_CHAIN (attributes) = NULL_TREE;
24687 DECL_CHAIN (decl) = decls;
24688 decls = decl;
24690 token = cp_lexer_peek_token (parser->lexer);
24691 if (token->type == CPP_COMMA)
24693 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24694 continue;
24696 else
24697 break;
24699 return decls;
24702 /* Parse an Objective-C @property declaration. The syntax is:
24704 objc-property-declaration:
24705 '@property' objc-property-attributes[opt] struct-declaration ;
24707 objc-property-attributes:
24708 '(' objc-property-attribute-list ')'
24710 objc-property-attribute-list:
24711 objc-property-attribute
24712 objc-property-attribute-list, objc-property-attribute
24714 objc-property-attribute
24715 'getter' = identifier
24716 'setter' = identifier
24717 'readonly'
24718 'readwrite'
24719 'assign'
24720 'retain'
24721 'copy'
24722 'nonatomic'
24724 For example:
24725 @property NSString *name;
24726 @property (readonly) id object;
24727 @property (retain, nonatomic, getter=getTheName) id name;
24728 @property int a, b, c;
24730 PS: This function is identical to
24731 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24732 static void
24733 cp_parser_objc_at_property_declaration (cp_parser *parser)
24735 /* The following variables hold the attributes of the properties as
24736 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24737 seen. When we see an attribute, we set them to 'true' (if they
24738 are boolean properties) or to the identifier (if they have an
24739 argument, ie, for getter and setter). Note that here we only
24740 parse the list of attributes, check the syntax and accumulate the
24741 attributes that we find. objc_add_property_declaration() will
24742 then process the information. */
24743 bool property_assign = false;
24744 bool property_copy = false;
24745 tree property_getter_ident = NULL_TREE;
24746 bool property_nonatomic = false;
24747 bool property_readonly = false;
24748 bool property_readwrite = false;
24749 bool property_retain = false;
24750 tree property_setter_ident = NULL_TREE;
24752 /* 'properties' is the list of properties that we read. Usually a
24753 single one, but maybe more (eg, in "@property int a, b, c;" there
24754 are three). */
24755 tree properties;
24756 location_t loc;
24758 loc = cp_lexer_peek_token (parser->lexer)->location;
24760 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24762 /* Parse the optional attribute list... */
24763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24765 /* Eat the '('. */
24766 cp_lexer_consume_token (parser->lexer);
24768 while (true)
24770 bool syntax_error = false;
24771 cp_token *token = cp_lexer_peek_token (parser->lexer);
24772 enum rid keyword;
24774 if (token->type != CPP_NAME)
24776 cp_parser_error (parser, "expected identifier");
24777 break;
24779 keyword = C_RID_CODE (token->u.value);
24780 cp_lexer_consume_token (parser->lexer);
24781 switch (keyword)
24783 case RID_ASSIGN: property_assign = true; break;
24784 case RID_COPY: property_copy = true; break;
24785 case RID_NONATOMIC: property_nonatomic = true; break;
24786 case RID_READONLY: property_readonly = true; break;
24787 case RID_READWRITE: property_readwrite = true; break;
24788 case RID_RETAIN: property_retain = true; break;
24790 case RID_GETTER:
24791 case RID_SETTER:
24792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24794 if (keyword == RID_GETTER)
24795 cp_parser_error (parser,
24796 "missing %<=%> (after %<getter%> attribute)");
24797 else
24798 cp_parser_error (parser,
24799 "missing %<=%> (after %<setter%> attribute)");
24800 syntax_error = true;
24801 break;
24803 cp_lexer_consume_token (parser->lexer); /* eat the = */
24804 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24806 cp_parser_error (parser, "expected identifier");
24807 syntax_error = true;
24808 break;
24810 if (keyword == RID_SETTER)
24812 if (property_setter_ident != NULL_TREE)
24814 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24815 cp_lexer_consume_token (parser->lexer);
24817 else
24818 property_setter_ident = cp_parser_objc_selector (parser);
24819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24820 cp_parser_error (parser, "setter name must terminate with %<:%>");
24821 else
24822 cp_lexer_consume_token (parser->lexer);
24824 else
24826 if (property_getter_ident != NULL_TREE)
24828 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24829 cp_lexer_consume_token (parser->lexer);
24831 else
24832 property_getter_ident = cp_parser_objc_selector (parser);
24834 break;
24835 default:
24836 cp_parser_error (parser, "unknown property attribute");
24837 syntax_error = true;
24838 break;
24841 if (syntax_error)
24842 break;
24844 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24845 cp_lexer_consume_token (parser->lexer);
24846 else
24847 break;
24850 /* FIXME: "@property (setter, assign);" will generate a spurious
24851 "error: expected ‘)’ before ‘,’ token". This is because
24852 cp_parser_require, unlike the C counterpart, will produce an
24853 error even if we are in error recovery. */
24854 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24856 cp_parser_skip_to_closing_parenthesis (parser,
24857 /*recovering=*/true,
24858 /*or_comma=*/false,
24859 /*consume_paren=*/true);
24863 /* ... and the property declaration(s). */
24864 properties = cp_parser_objc_struct_declaration (parser);
24866 if (properties == error_mark_node)
24868 cp_parser_skip_to_end_of_statement (parser);
24869 /* If the next token is now a `;', consume it. */
24870 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24871 cp_lexer_consume_token (parser->lexer);
24872 return;
24875 if (properties == NULL_TREE)
24876 cp_parser_error (parser, "expected identifier");
24877 else
24879 /* Comma-separated properties are chained together in
24880 reverse order; add them one by one. */
24881 properties = nreverse (properties);
24883 for (; properties; properties = TREE_CHAIN (properties))
24884 objc_add_property_declaration (loc, copy_node (properties),
24885 property_readonly, property_readwrite,
24886 property_assign, property_retain,
24887 property_copy, property_nonatomic,
24888 property_getter_ident, property_setter_ident);
24891 cp_parser_consume_semicolon_at_end_of_statement (parser);
24894 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24896 objc-synthesize-declaration:
24897 @synthesize objc-synthesize-identifier-list ;
24899 objc-synthesize-identifier-list:
24900 objc-synthesize-identifier
24901 objc-synthesize-identifier-list, objc-synthesize-identifier
24903 objc-synthesize-identifier
24904 identifier
24905 identifier = identifier
24907 For example:
24908 @synthesize MyProperty;
24909 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24911 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24912 for C. Keep them in sync.
24914 static void
24915 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24917 tree list = NULL_TREE;
24918 location_t loc;
24919 loc = cp_lexer_peek_token (parser->lexer)->location;
24921 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24922 while (true)
24924 tree property, ivar;
24925 property = cp_parser_identifier (parser);
24926 if (property == error_mark_node)
24928 cp_parser_consume_semicolon_at_end_of_statement (parser);
24929 return;
24931 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24933 cp_lexer_consume_token (parser->lexer);
24934 ivar = cp_parser_identifier (parser);
24935 if (ivar == error_mark_node)
24937 cp_parser_consume_semicolon_at_end_of_statement (parser);
24938 return;
24941 else
24942 ivar = NULL_TREE;
24943 list = chainon (list, build_tree_list (ivar, property));
24944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24945 cp_lexer_consume_token (parser->lexer);
24946 else
24947 break;
24949 cp_parser_consume_semicolon_at_end_of_statement (parser);
24950 objc_add_synthesize_declaration (loc, list);
24953 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24955 objc-dynamic-declaration:
24956 @dynamic identifier-list ;
24958 For example:
24959 @dynamic MyProperty;
24960 @dynamic MyProperty, AnotherProperty;
24962 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24963 for C. Keep them in sync.
24965 static void
24966 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24968 tree list = NULL_TREE;
24969 location_t loc;
24970 loc = cp_lexer_peek_token (parser->lexer)->location;
24972 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24973 while (true)
24975 tree property;
24976 property = cp_parser_identifier (parser);
24977 if (property == error_mark_node)
24979 cp_parser_consume_semicolon_at_end_of_statement (parser);
24980 return;
24982 list = chainon (list, build_tree_list (NULL, property));
24983 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24984 cp_lexer_consume_token (parser->lexer);
24985 else
24986 break;
24988 cp_parser_consume_semicolon_at_end_of_statement (parser);
24989 objc_add_dynamic_declaration (loc, list);
24993 /* OpenMP 2.5 parsing routines. */
24995 /* Returns name of the next clause.
24996 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24997 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24998 returned and the token is consumed. */
25000 static pragma_omp_clause
25001 cp_parser_omp_clause_name (cp_parser *parser)
25003 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25006 result = PRAGMA_OMP_CLAUSE_IF;
25007 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25008 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25009 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25010 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25011 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25013 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25014 const char *p = IDENTIFIER_POINTER (id);
25016 switch (p[0])
25018 case 'c':
25019 if (!strcmp ("collapse", p))
25020 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25021 else if (!strcmp ("copyin", p))
25022 result = PRAGMA_OMP_CLAUSE_COPYIN;
25023 else if (!strcmp ("copyprivate", p))
25024 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25025 break;
25026 case 'f':
25027 if (!strcmp ("final", p))
25028 result = PRAGMA_OMP_CLAUSE_FINAL;
25029 else if (!strcmp ("firstprivate", p))
25030 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25031 break;
25032 case 'l':
25033 if (!strcmp ("lastprivate", p))
25034 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25035 break;
25036 case 'm':
25037 if (!strcmp ("mergeable", p))
25038 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25039 break;
25040 case 'n':
25041 if (!strcmp ("nowait", p))
25042 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25043 else if (!strcmp ("num_threads", p))
25044 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25045 break;
25046 case 'o':
25047 if (!strcmp ("ordered", p))
25048 result = PRAGMA_OMP_CLAUSE_ORDERED;
25049 break;
25050 case 'r':
25051 if (!strcmp ("reduction", p))
25052 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25053 break;
25054 case 's':
25055 if (!strcmp ("schedule", p))
25056 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25057 else if (!strcmp ("shared", p))
25058 result = PRAGMA_OMP_CLAUSE_SHARED;
25059 break;
25060 case 'u':
25061 if (!strcmp ("untied", p))
25062 result = PRAGMA_OMP_CLAUSE_UNTIED;
25063 break;
25067 if (result != PRAGMA_OMP_CLAUSE_NONE)
25068 cp_lexer_consume_token (parser->lexer);
25070 return result;
25073 /* Validate that a clause of the given type does not already exist. */
25075 static void
25076 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25077 const char *name, location_t location)
25079 tree c;
25081 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25082 if (OMP_CLAUSE_CODE (c) == code)
25084 error_at (location, "too many %qs clauses", name);
25085 break;
25089 /* OpenMP 2.5:
25090 variable-list:
25091 identifier
25092 variable-list , identifier
25094 In addition, we match a closing parenthesis. An opening parenthesis
25095 will have been consumed by the caller.
25097 If KIND is nonzero, create the appropriate node and install the decl
25098 in OMP_CLAUSE_DECL and add the node to the head of the list.
25100 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25101 return the list created. */
25103 static tree
25104 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25105 tree list)
25107 cp_token *token;
25108 while (1)
25110 tree name, decl;
25112 token = cp_lexer_peek_token (parser->lexer);
25113 name = cp_parser_id_expression (parser, /*template_p=*/false,
25114 /*check_dependency_p=*/true,
25115 /*template_p=*/NULL,
25116 /*declarator_p=*/false,
25117 /*optional_p=*/false);
25118 if (name == error_mark_node)
25119 goto skip_comma;
25121 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25122 if (decl == error_mark_node)
25123 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25124 token->location);
25125 else if (kind != 0)
25127 tree u = build_omp_clause (token->location, kind);
25128 OMP_CLAUSE_DECL (u) = decl;
25129 OMP_CLAUSE_CHAIN (u) = list;
25130 list = u;
25132 else
25133 list = tree_cons (decl, NULL_TREE, list);
25135 get_comma:
25136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25137 break;
25138 cp_lexer_consume_token (parser->lexer);
25141 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25143 int ending;
25145 /* Try to resync to an unnested comma. Copied from
25146 cp_parser_parenthesized_expression_list. */
25147 skip_comma:
25148 ending = cp_parser_skip_to_closing_parenthesis (parser,
25149 /*recovering=*/true,
25150 /*or_comma=*/true,
25151 /*consume_paren=*/true);
25152 if (ending < 0)
25153 goto get_comma;
25156 return list;
25159 /* Similarly, but expect leading and trailing parenthesis. This is a very
25160 common case for omp clauses. */
25162 static tree
25163 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25165 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25166 return cp_parser_omp_var_list_no_open (parser, kind, list);
25167 return list;
25170 /* OpenMP 3.0:
25171 collapse ( constant-expression ) */
25173 static tree
25174 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25176 tree c, num;
25177 location_t loc;
25178 HOST_WIDE_INT n;
25180 loc = cp_lexer_peek_token (parser->lexer)->location;
25181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25182 return list;
25184 num = cp_parser_constant_expression (parser, false, NULL);
25186 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25187 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25188 /*or_comma=*/false,
25189 /*consume_paren=*/true);
25191 if (num == error_mark_node)
25192 return list;
25193 num = fold_non_dependent_expr (num);
25194 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25195 || !host_integerp (num, 0)
25196 || (n = tree_low_cst (num, 0)) <= 0
25197 || (int) n != n)
25199 error_at (loc, "collapse argument needs positive constant integer expression");
25200 return list;
25203 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25204 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25205 OMP_CLAUSE_CHAIN (c) = list;
25206 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25208 return c;
25211 /* OpenMP 2.5:
25212 default ( shared | none ) */
25214 static tree
25215 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25217 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25218 tree c;
25220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25221 return list;
25222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25225 const char *p = IDENTIFIER_POINTER (id);
25227 switch (p[0])
25229 case 'n':
25230 if (strcmp ("none", p) != 0)
25231 goto invalid_kind;
25232 kind = OMP_CLAUSE_DEFAULT_NONE;
25233 break;
25235 case 's':
25236 if (strcmp ("shared", p) != 0)
25237 goto invalid_kind;
25238 kind = OMP_CLAUSE_DEFAULT_SHARED;
25239 break;
25241 default:
25242 goto invalid_kind;
25245 cp_lexer_consume_token (parser->lexer);
25247 else
25249 invalid_kind:
25250 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25255 /*or_comma=*/false,
25256 /*consume_paren=*/true);
25258 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25259 return list;
25261 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25262 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25263 OMP_CLAUSE_CHAIN (c) = list;
25264 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25266 return c;
25269 /* OpenMP 3.1:
25270 final ( expression ) */
25272 static tree
25273 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25275 tree t, c;
25277 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25278 return list;
25280 t = cp_parser_condition (parser);
25282 if (t == error_mark_node
25283 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25285 /*or_comma=*/false,
25286 /*consume_paren=*/true);
25288 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25290 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25291 OMP_CLAUSE_FINAL_EXPR (c) = t;
25292 OMP_CLAUSE_CHAIN (c) = list;
25294 return c;
25297 /* OpenMP 2.5:
25298 if ( expression ) */
25300 static tree
25301 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25303 tree t, c;
25305 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25306 return list;
25308 t = cp_parser_condition (parser);
25310 if (t == error_mark_node
25311 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25312 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25313 /*or_comma=*/false,
25314 /*consume_paren=*/true);
25316 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25318 c = build_omp_clause (location, OMP_CLAUSE_IF);
25319 OMP_CLAUSE_IF_EXPR (c) = t;
25320 OMP_CLAUSE_CHAIN (c) = list;
25322 return c;
25325 /* OpenMP 3.1:
25326 mergeable */
25328 static tree
25329 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25330 tree list, location_t location)
25332 tree c;
25334 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25335 location);
25337 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25338 OMP_CLAUSE_CHAIN (c) = list;
25339 return c;
25342 /* OpenMP 2.5:
25343 nowait */
25345 static tree
25346 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25347 tree list, location_t location)
25349 tree c;
25351 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25353 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25354 OMP_CLAUSE_CHAIN (c) = list;
25355 return c;
25358 /* OpenMP 2.5:
25359 num_threads ( expression ) */
25361 static tree
25362 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25363 location_t location)
25365 tree t, c;
25367 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25368 return list;
25370 t = cp_parser_expression (parser, false, NULL);
25372 if (t == error_mark_node
25373 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25375 /*or_comma=*/false,
25376 /*consume_paren=*/true);
25378 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25379 "num_threads", location);
25381 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25382 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25383 OMP_CLAUSE_CHAIN (c) = list;
25385 return c;
25388 /* OpenMP 2.5:
25389 ordered */
25391 static tree
25392 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
25393 tree list, location_t location)
25395 tree c;
25397 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25398 "ordered", location);
25400 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25401 OMP_CLAUSE_CHAIN (c) = list;
25402 return c;
25405 /* OpenMP 2.5:
25406 reduction ( reduction-operator : variable-list )
25408 reduction-operator:
25409 One of: + * - & ^ | && ||
25411 OpenMP 3.1:
25413 reduction-operator:
25414 One of: + * - & ^ | && || min max */
25416 static tree
25417 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25419 enum tree_code code;
25420 tree nlist, c;
25422 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25423 return list;
25425 switch (cp_lexer_peek_token (parser->lexer)->type)
25427 case CPP_PLUS:
25428 code = PLUS_EXPR;
25429 break;
25430 case CPP_MULT:
25431 code = MULT_EXPR;
25432 break;
25433 case CPP_MINUS:
25434 code = MINUS_EXPR;
25435 break;
25436 case CPP_AND:
25437 code = BIT_AND_EXPR;
25438 break;
25439 case CPP_XOR:
25440 code = BIT_XOR_EXPR;
25441 break;
25442 case CPP_OR:
25443 code = BIT_IOR_EXPR;
25444 break;
25445 case CPP_AND_AND:
25446 code = TRUTH_ANDIF_EXPR;
25447 break;
25448 case CPP_OR_OR:
25449 code = TRUTH_ORIF_EXPR;
25450 break;
25451 case CPP_NAME:
25453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25454 const char *p = IDENTIFIER_POINTER (id);
25456 if (strcmp (p, "min") == 0)
25458 code = MIN_EXPR;
25459 break;
25461 if (strcmp (p, "max") == 0)
25463 code = MAX_EXPR;
25464 break;
25467 /* FALLTHROUGH */
25468 default:
25469 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25470 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25471 resync_fail:
25472 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25473 /*or_comma=*/false,
25474 /*consume_paren=*/true);
25475 return list;
25477 cp_lexer_consume_token (parser->lexer);
25479 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25480 goto resync_fail;
25482 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25483 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25484 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25486 return nlist;
25489 /* OpenMP 2.5:
25490 schedule ( schedule-kind )
25491 schedule ( schedule-kind , expression )
25493 schedule-kind:
25494 static | dynamic | guided | runtime | auto */
25496 static tree
25497 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25499 tree c, t;
25501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25502 return list;
25504 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25508 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25509 const char *p = IDENTIFIER_POINTER (id);
25511 switch (p[0])
25513 case 'd':
25514 if (strcmp ("dynamic", p) != 0)
25515 goto invalid_kind;
25516 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25517 break;
25519 case 'g':
25520 if (strcmp ("guided", p) != 0)
25521 goto invalid_kind;
25522 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25523 break;
25525 case 'r':
25526 if (strcmp ("runtime", p) != 0)
25527 goto invalid_kind;
25528 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25529 break;
25531 default:
25532 goto invalid_kind;
25535 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25536 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25537 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25538 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25539 else
25540 goto invalid_kind;
25541 cp_lexer_consume_token (parser->lexer);
25543 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25545 cp_token *token;
25546 cp_lexer_consume_token (parser->lexer);
25548 token = cp_lexer_peek_token (parser->lexer);
25549 t = cp_parser_assignment_expression (parser, false, NULL);
25551 if (t == error_mark_node)
25552 goto resync_fail;
25553 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25554 error_at (token->location, "schedule %<runtime%> does not take "
25555 "a %<chunk_size%> parameter");
25556 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25557 error_at (token->location, "schedule %<auto%> does not take "
25558 "a %<chunk_size%> parameter");
25559 else
25560 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25563 goto resync_fail;
25565 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25566 goto resync_fail;
25568 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25569 OMP_CLAUSE_CHAIN (c) = list;
25570 return c;
25572 invalid_kind:
25573 cp_parser_error (parser, "invalid schedule kind");
25574 resync_fail:
25575 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25576 /*or_comma=*/false,
25577 /*consume_paren=*/true);
25578 return list;
25581 /* OpenMP 3.0:
25582 untied */
25584 static tree
25585 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
25586 tree list, location_t location)
25588 tree c;
25590 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25592 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25593 OMP_CLAUSE_CHAIN (c) = list;
25594 return c;
25597 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25598 is a bitmask in MASK. Return the list of clauses found; the result
25599 of clause default goes in *pdefault. */
25601 static tree
25602 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25603 const char *where, cp_token *pragma_tok)
25605 tree clauses = NULL;
25606 bool first = true;
25607 cp_token *token = NULL;
25609 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25611 pragma_omp_clause c_kind;
25612 const char *c_name;
25613 tree prev = clauses;
25615 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25616 cp_lexer_consume_token (parser->lexer);
25618 token = cp_lexer_peek_token (parser->lexer);
25619 c_kind = cp_parser_omp_clause_name (parser);
25620 first = false;
25622 switch (c_kind)
25624 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25625 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25626 token->location);
25627 c_name = "collapse";
25628 break;
25629 case PRAGMA_OMP_CLAUSE_COPYIN:
25630 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25631 c_name = "copyin";
25632 break;
25633 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25635 clauses);
25636 c_name = "copyprivate";
25637 break;
25638 case PRAGMA_OMP_CLAUSE_DEFAULT:
25639 clauses = cp_parser_omp_clause_default (parser, clauses,
25640 token->location);
25641 c_name = "default";
25642 break;
25643 case PRAGMA_OMP_CLAUSE_FINAL:
25644 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25645 c_name = "final";
25646 break;
25647 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25648 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25649 clauses);
25650 c_name = "firstprivate";
25651 break;
25652 case PRAGMA_OMP_CLAUSE_IF:
25653 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25654 c_name = "if";
25655 break;
25656 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25657 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25658 clauses);
25659 c_name = "lastprivate";
25660 break;
25661 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25662 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25663 token->location);
25664 c_name = "mergeable";
25665 break;
25666 case PRAGMA_OMP_CLAUSE_NOWAIT:
25667 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25668 c_name = "nowait";
25669 break;
25670 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25671 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25672 token->location);
25673 c_name = "num_threads";
25674 break;
25675 case PRAGMA_OMP_CLAUSE_ORDERED:
25676 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25677 token->location);
25678 c_name = "ordered";
25679 break;
25680 case PRAGMA_OMP_CLAUSE_PRIVATE:
25681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25682 clauses);
25683 c_name = "private";
25684 break;
25685 case PRAGMA_OMP_CLAUSE_REDUCTION:
25686 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25687 c_name = "reduction";
25688 break;
25689 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25690 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25691 token->location);
25692 c_name = "schedule";
25693 break;
25694 case PRAGMA_OMP_CLAUSE_SHARED:
25695 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25696 clauses);
25697 c_name = "shared";
25698 break;
25699 case PRAGMA_OMP_CLAUSE_UNTIED:
25700 clauses = cp_parser_omp_clause_untied (parser, clauses,
25701 token->location);
25702 c_name = "nowait";
25703 break;
25704 default:
25705 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25706 goto saw_error;
25709 if (((mask >> c_kind) & 1) == 0)
25711 /* Remove the invalid clause(s) from the list to avoid
25712 confusing the rest of the compiler. */
25713 clauses = prev;
25714 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25717 saw_error:
25718 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25719 return finish_omp_clauses (clauses);
25722 /* OpenMP 2.5:
25723 structured-block:
25724 statement
25726 In practice, we're also interested in adding the statement to an
25727 outer node. So it is convenient if we work around the fact that
25728 cp_parser_statement calls add_stmt. */
25730 static unsigned
25731 cp_parser_begin_omp_structured_block (cp_parser *parser)
25733 unsigned save = parser->in_statement;
25735 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25736 This preserves the "not within loop or switch" style error messages
25737 for nonsense cases like
25738 void foo() {
25739 #pragma omp single
25740 break;
25743 if (parser->in_statement)
25744 parser->in_statement = IN_OMP_BLOCK;
25746 return save;
25749 static void
25750 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25752 parser->in_statement = save;
25755 static tree
25756 cp_parser_omp_structured_block (cp_parser *parser)
25758 tree stmt = begin_omp_structured_block ();
25759 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25761 cp_parser_statement (parser, NULL_TREE, false, NULL);
25763 cp_parser_end_omp_structured_block (parser, save);
25764 return finish_omp_structured_block (stmt);
25767 /* OpenMP 2.5:
25768 # pragma omp atomic new-line
25769 expression-stmt
25771 expression-stmt:
25772 x binop= expr | x++ | ++x | x-- | --x
25773 binop:
25774 +, *, -, /, &, ^, |, <<, >>
25776 where x is an lvalue expression with scalar type.
25778 OpenMP 3.1:
25779 # pragma omp atomic new-line
25780 update-stmt
25782 # pragma omp atomic read new-line
25783 read-stmt
25785 # pragma omp atomic write new-line
25786 write-stmt
25788 # pragma omp atomic update new-line
25789 update-stmt
25791 # pragma omp atomic capture new-line
25792 capture-stmt
25794 # pragma omp atomic capture new-line
25795 capture-block
25797 read-stmt:
25798 v = x
25799 write-stmt:
25800 x = expr
25801 update-stmt:
25802 expression-stmt | x = x binop expr
25803 capture-stmt:
25804 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25805 capture-block:
25806 { v = x; update-stmt; } | { update-stmt; v = x; }
25808 where x and v are lvalue expressions with scalar type. */
25810 static void
25811 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25813 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25814 tree rhs1 = NULL_TREE, orig_lhs;
25815 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25816 bool structured_block = false;
25818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25820 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25821 const char *p = IDENTIFIER_POINTER (id);
25823 if (!strcmp (p, "read"))
25824 code = OMP_ATOMIC_READ;
25825 else if (!strcmp (p, "write"))
25826 code = NOP_EXPR;
25827 else if (!strcmp (p, "update"))
25828 code = OMP_ATOMIC;
25829 else if (!strcmp (p, "capture"))
25830 code = OMP_ATOMIC_CAPTURE_NEW;
25831 else
25832 p = NULL;
25833 if (p)
25834 cp_lexer_consume_token (parser->lexer);
25836 cp_parser_require_pragma_eol (parser, pragma_tok);
25838 switch (code)
25840 case OMP_ATOMIC_READ:
25841 case NOP_EXPR: /* atomic write */
25842 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25843 /*cast_p=*/false, NULL);
25844 if (v == error_mark_node)
25845 goto saw_error;
25846 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25847 goto saw_error;
25848 if (code == NOP_EXPR)
25849 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25850 else
25851 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25852 /*cast_p=*/false, NULL);
25853 if (lhs == error_mark_node)
25854 goto saw_error;
25855 if (code == NOP_EXPR)
25857 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25858 opcode. */
25859 code = OMP_ATOMIC;
25860 rhs = lhs;
25861 lhs = v;
25862 v = NULL_TREE;
25864 goto done;
25865 case OMP_ATOMIC_CAPTURE_NEW:
25866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25868 cp_lexer_consume_token (parser->lexer);
25869 structured_block = true;
25871 else
25873 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25874 /*cast_p=*/false, NULL);
25875 if (v == error_mark_node)
25876 goto saw_error;
25877 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25878 goto saw_error;
25880 default:
25881 break;
25884 restart:
25885 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25886 /*cast_p=*/false, NULL);
25887 orig_lhs = lhs;
25888 switch (TREE_CODE (lhs))
25890 case ERROR_MARK:
25891 goto saw_error;
25893 case POSTINCREMENT_EXPR:
25894 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25895 code = OMP_ATOMIC_CAPTURE_OLD;
25896 /* FALLTHROUGH */
25897 case PREINCREMENT_EXPR:
25898 lhs = TREE_OPERAND (lhs, 0);
25899 opcode = PLUS_EXPR;
25900 rhs = integer_one_node;
25901 break;
25903 case POSTDECREMENT_EXPR:
25904 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25905 code = OMP_ATOMIC_CAPTURE_OLD;
25906 /* FALLTHROUGH */
25907 case PREDECREMENT_EXPR:
25908 lhs = TREE_OPERAND (lhs, 0);
25909 opcode = MINUS_EXPR;
25910 rhs = integer_one_node;
25911 break;
25913 case COMPOUND_EXPR:
25914 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25915 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25916 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25917 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25918 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25919 (TREE_OPERAND (lhs, 1), 0), 0)))
25920 == BOOLEAN_TYPE)
25921 /* Undo effects of boolean_increment for post {in,de}crement. */
25922 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25923 /* FALLTHRU */
25924 case MODIFY_EXPR:
25925 if (TREE_CODE (lhs) == MODIFY_EXPR
25926 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25928 /* Undo effects of boolean_increment. */
25929 if (integer_onep (TREE_OPERAND (lhs, 1)))
25931 /* This is pre or post increment. */
25932 rhs = TREE_OPERAND (lhs, 1);
25933 lhs = TREE_OPERAND (lhs, 0);
25934 opcode = NOP_EXPR;
25935 if (code == OMP_ATOMIC_CAPTURE_NEW
25936 && !structured_block
25937 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25938 code = OMP_ATOMIC_CAPTURE_OLD;
25939 break;
25942 /* FALLTHRU */
25943 default:
25944 switch (cp_lexer_peek_token (parser->lexer)->type)
25946 case CPP_MULT_EQ:
25947 opcode = MULT_EXPR;
25948 break;
25949 case CPP_DIV_EQ:
25950 opcode = TRUNC_DIV_EXPR;
25951 break;
25952 case CPP_PLUS_EQ:
25953 opcode = PLUS_EXPR;
25954 break;
25955 case CPP_MINUS_EQ:
25956 opcode = MINUS_EXPR;
25957 break;
25958 case CPP_LSHIFT_EQ:
25959 opcode = LSHIFT_EXPR;
25960 break;
25961 case CPP_RSHIFT_EQ:
25962 opcode = RSHIFT_EXPR;
25963 break;
25964 case CPP_AND_EQ:
25965 opcode = BIT_AND_EXPR;
25966 break;
25967 case CPP_OR_EQ:
25968 opcode = BIT_IOR_EXPR;
25969 break;
25970 case CPP_XOR_EQ:
25971 opcode = BIT_XOR_EXPR;
25972 break;
25973 case CPP_EQ:
25974 if (structured_block || code == OMP_ATOMIC)
25976 enum cp_parser_prec oprec;
25977 cp_token *token;
25978 cp_lexer_consume_token (parser->lexer);
25979 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25980 /*cast_p=*/false, NULL);
25981 if (rhs1 == error_mark_node)
25982 goto saw_error;
25983 token = cp_lexer_peek_token (parser->lexer);
25984 switch (token->type)
25986 case CPP_SEMICOLON:
25987 if (code == OMP_ATOMIC_CAPTURE_NEW)
25989 code = OMP_ATOMIC_CAPTURE_OLD;
25990 v = lhs;
25991 lhs = NULL_TREE;
25992 lhs1 = rhs1;
25993 rhs1 = NULL_TREE;
25994 cp_lexer_consume_token (parser->lexer);
25995 goto restart;
25997 cp_parser_error (parser,
25998 "invalid form of %<#pragma omp atomic%>");
25999 goto saw_error;
26000 case CPP_MULT:
26001 opcode = MULT_EXPR;
26002 break;
26003 case CPP_DIV:
26004 opcode = TRUNC_DIV_EXPR;
26005 break;
26006 case CPP_PLUS:
26007 opcode = PLUS_EXPR;
26008 break;
26009 case CPP_MINUS:
26010 opcode = MINUS_EXPR;
26011 break;
26012 case CPP_LSHIFT:
26013 opcode = LSHIFT_EXPR;
26014 break;
26015 case CPP_RSHIFT:
26016 opcode = RSHIFT_EXPR;
26017 break;
26018 case CPP_AND:
26019 opcode = BIT_AND_EXPR;
26020 break;
26021 case CPP_OR:
26022 opcode = BIT_IOR_EXPR;
26023 break;
26024 case CPP_XOR:
26025 opcode = BIT_XOR_EXPR;
26026 break;
26027 default:
26028 cp_parser_error (parser,
26029 "invalid operator for %<#pragma omp atomic%>");
26030 goto saw_error;
26032 oprec = TOKEN_PRECEDENCE (token);
26033 gcc_assert (oprec != PREC_NOT_OPERATOR);
26034 if (commutative_tree_code (opcode))
26035 oprec = (enum cp_parser_prec) (oprec - 1);
26036 cp_lexer_consume_token (parser->lexer);
26037 rhs = cp_parser_binary_expression (parser, false, false,
26038 oprec, NULL);
26039 if (rhs == error_mark_node)
26040 goto saw_error;
26041 goto stmt_done;
26043 /* FALLTHROUGH */
26044 default:
26045 cp_parser_error (parser,
26046 "invalid operator for %<#pragma omp atomic%>");
26047 goto saw_error;
26049 cp_lexer_consume_token (parser->lexer);
26051 rhs = cp_parser_expression (parser, false, NULL);
26052 if (rhs == error_mark_node)
26053 goto saw_error;
26054 break;
26056 stmt_done:
26057 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26059 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26060 goto saw_error;
26061 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26062 /*cast_p=*/false, NULL);
26063 if (v == error_mark_node)
26064 goto saw_error;
26065 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26066 goto saw_error;
26067 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26068 /*cast_p=*/false, NULL);
26069 if (lhs1 == error_mark_node)
26070 goto saw_error;
26072 if (structured_block)
26074 cp_parser_consume_semicolon_at_end_of_statement (parser);
26075 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26077 done:
26078 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26079 if (!structured_block)
26080 cp_parser_consume_semicolon_at_end_of_statement (parser);
26081 return;
26083 saw_error:
26084 cp_parser_skip_to_end_of_block_or_statement (parser);
26085 if (structured_block)
26087 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26088 cp_lexer_consume_token (parser->lexer);
26089 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26091 cp_parser_skip_to_end_of_block_or_statement (parser);
26092 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26093 cp_lexer_consume_token (parser->lexer);
26099 /* OpenMP 2.5:
26100 # pragma omp barrier new-line */
26102 static void
26103 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26105 cp_parser_require_pragma_eol (parser, pragma_tok);
26106 finish_omp_barrier ();
26109 /* OpenMP 2.5:
26110 # pragma omp critical [(name)] new-line
26111 structured-block */
26113 static tree
26114 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26116 tree stmt, name = NULL;
26118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26120 cp_lexer_consume_token (parser->lexer);
26122 name = cp_parser_identifier (parser);
26124 if (name == error_mark_node
26125 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26126 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26127 /*or_comma=*/false,
26128 /*consume_paren=*/true);
26129 if (name == error_mark_node)
26130 name = NULL;
26132 cp_parser_require_pragma_eol (parser, pragma_tok);
26134 stmt = cp_parser_omp_structured_block (parser);
26135 return c_finish_omp_critical (input_location, stmt, name);
26138 /* OpenMP 2.5:
26139 # pragma omp flush flush-vars[opt] new-line
26141 flush-vars:
26142 ( variable-list ) */
26144 static void
26145 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26148 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26149 cp_parser_require_pragma_eol (parser, pragma_tok);
26151 finish_omp_flush ();
26154 /* Helper function, to parse omp for increment expression. */
26156 static tree
26157 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26159 tree cond = cp_parser_binary_expression (parser, false, true,
26160 PREC_NOT_OPERATOR, NULL);
26161 if (cond == error_mark_node
26162 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26164 cp_parser_skip_to_end_of_statement (parser);
26165 return error_mark_node;
26168 switch (TREE_CODE (cond))
26170 case GT_EXPR:
26171 case GE_EXPR:
26172 case LT_EXPR:
26173 case LE_EXPR:
26174 break;
26175 default:
26176 return error_mark_node;
26179 /* If decl is an iterator, preserve LHS and RHS of the relational
26180 expr until finish_omp_for. */
26181 if (decl
26182 && (type_dependent_expression_p (decl)
26183 || CLASS_TYPE_P (TREE_TYPE (decl))))
26184 return cond;
26186 return build_x_binary_op (input_location, TREE_CODE (cond),
26187 TREE_OPERAND (cond, 0), ERROR_MARK,
26188 TREE_OPERAND (cond, 1), ERROR_MARK,
26189 /*overload=*/NULL, tf_warning_or_error);
26192 /* Helper function, to parse omp for increment expression. */
26194 static tree
26195 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26197 cp_token *token = cp_lexer_peek_token (parser->lexer);
26198 enum tree_code op;
26199 tree lhs, rhs;
26200 cp_id_kind idk;
26201 bool decl_first;
26203 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26205 op = (token->type == CPP_PLUS_PLUS
26206 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26207 cp_lexer_consume_token (parser->lexer);
26208 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26209 if (lhs != decl)
26210 return error_mark_node;
26211 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26214 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26215 if (lhs != decl)
26216 return error_mark_node;
26218 token = cp_lexer_peek_token (parser->lexer);
26219 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26221 op = (token->type == CPP_PLUS_PLUS
26222 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26223 cp_lexer_consume_token (parser->lexer);
26224 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26227 op = cp_parser_assignment_operator_opt (parser);
26228 if (op == ERROR_MARK)
26229 return error_mark_node;
26231 if (op != NOP_EXPR)
26233 rhs = cp_parser_assignment_expression (parser, false, NULL);
26234 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26235 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26238 lhs = cp_parser_binary_expression (parser, false, false,
26239 PREC_ADDITIVE_EXPRESSION, NULL);
26240 token = cp_lexer_peek_token (parser->lexer);
26241 decl_first = lhs == decl;
26242 if (decl_first)
26243 lhs = NULL_TREE;
26244 if (token->type != CPP_PLUS
26245 && token->type != CPP_MINUS)
26246 return error_mark_node;
26250 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26251 cp_lexer_consume_token (parser->lexer);
26252 rhs = cp_parser_binary_expression (parser, false, false,
26253 PREC_ADDITIVE_EXPRESSION, NULL);
26254 token = cp_lexer_peek_token (parser->lexer);
26255 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26257 if (lhs == NULL_TREE)
26259 if (op == PLUS_EXPR)
26260 lhs = rhs;
26261 else
26262 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26263 tf_warning_or_error);
26265 else
26266 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26267 ERROR_MARK, NULL, tf_warning_or_error);
26270 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26272 if (!decl_first)
26274 if (rhs != decl || op == MINUS_EXPR)
26275 return error_mark_node;
26276 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26278 else
26279 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26281 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26284 /* Parse the restricted form of the for statement allowed by OpenMP. */
26286 static tree
26287 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26289 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26290 tree real_decl, initv, condv, incrv, declv;
26291 tree this_pre_body, cl;
26292 location_t loc_first;
26293 bool collapse_err = false;
26294 int i, collapse = 1, nbraces = 0;
26295 VEC(tree,gc) *for_block = make_tree_vector ();
26297 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26298 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26299 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26301 gcc_assert (collapse >= 1);
26303 declv = make_tree_vec (collapse);
26304 initv = make_tree_vec (collapse);
26305 condv = make_tree_vec (collapse);
26306 incrv = make_tree_vec (collapse);
26308 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26310 for (i = 0; i < collapse; i++)
26312 int bracecount = 0;
26313 bool add_private_clause = false;
26314 location_t loc;
26316 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26318 cp_parser_error (parser, "for statement expected");
26319 return NULL;
26321 loc = cp_lexer_consume_token (parser->lexer)->location;
26323 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26324 return NULL;
26326 init = decl = real_decl = NULL;
26327 this_pre_body = push_stmt_list ();
26328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26330 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26332 init-expr:
26333 var = lb
26334 integer-type var = lb
26335 random-access-iterator-type var = lb
26336 pointer-type var = lb
26338 cp_decl_specifier_seq type_specifiers;
26340 /* First, try to parse as an initialized declaration. See
26341 cp_parser_condition, from whence the bulk of this is copied. */
26343 cp_parser_parse_tentatively (parser);
26344 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26345 /*is_trailing_return=*/false,
26346 &type_specifiers);
26347 if (cp_parser_parse_definitely (parser))
26349 /* If parsing a type specifier seq succeeded, then this
26350 MUST be a initialized declaration. */
26351 tree asm_specification, attributes;
26352 cp_declarator *declarator;
26354 declarator = cp_parser_declarator (parser,
26355 CP_PARSER_DECLARATOR_NAMED,
26356 /*ctor_dtor_or_conv_p=*/NULL,
26357 /*parenthesized_p=*/NULL,
26358 /*member_p=*/false);
26359 attributes = cp_parser_attributes_opt (parser);
26360 asm_specification = cp_parser_asm_specification_opt (parser);
26362 if (declarator == cp_error_declarator)
26363 cp_parser_skip_to_end_of_statement (parser);
26365 else
26367 tree pushed_scope, auto_node;
26369 decl = start_decl (declarator, &type_specifiers,
26370 SD_INITIALIZED, attributes,
26371 /*prefix_attributes=*/NULL_TREE,
26372 &pushed_scope);
26374 auto_node = type_uses_auto (TREE_TYPE (decl));
26375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26377 if (cp_lexer_next_token_is (parser->lexer,
26378 CPP_OPEN_PAREN))
26379 error ("parenthesized initialization is not allowed in "
26380 "OpenMP %<for%> loop");
26381 else
26382 /* Trigger an error. */
26383 cp_parser_require (parser, CPP_EQ, RT_EQ);
26385 init = error_mark_node;
26386 cp_parser_skip_to_end_of_statement (parser);
26388 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26389 || type_dependent_expression_p (decl)
26390 || auto_node)
26392 bool is_direct_init, is_non_constant_init;
26394 init = cp_parser_initializer (parser,
26395 &is_direct_init,
26396 &is_non_constant_init);
26398 if (auto_node)
26400 TREE_TYPE (decl)
26401 = do_auto_deduction (TREE_TYPE (decl), init,
26402 auto_node);
26404 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26405 && !type_dependent_expression_p (decl))
26406 goto non_class;
26409 cp_finish_decl (decl, init, !is_non_constant_init,
26410 asm_specification,
26411 LOOKUP_ONLYCONVERTING);
26412 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26414 VEC_safe_push (tree, gc, for_block, this_pre_body);
26415 init = NULL_TREE;
26417 else
26418 init = pop_stmt_list (this_pre_body);
26419 this_pre_body = NULL_TREE;
26421 else
26423 /* Consume '='. */
26424 cp_lexer_consume_token (parser->lexer);
26425 init = cp_parser_assignment_expression (parser, false, NULL);
26427 non_class:
26428 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26429 init = error_mark_node;
26430 else
26431 cp_finish_decl (decl, NULL_TREE,
26432 /*init_const_expr_p=*/false,
26433 asm_specification,
26434 LOOKUP_ONLYCONVERTING);
26437 if (pushed_scope)
26438 pop_scope (pushed_scope);
26441 else
26443 cp_id_kind idk;
26444 /* If parsing a type specifier sequence failed, then
26445 this MUST be a simple expression. */
26446 cp_parser_parse_tentatively (parser);
26447 decl = cp_parser_primary_expression (parser, false, false,
26448 false, &idk);
26449 if (!cp_parser_error_occurred (parser)
26450 && decl
26451 && DECL_P (decl)
26452 && CLASS_TYPE_P (TREE_TYPE (decl)))
26454 tree rhs;
26456 cp_parser_parse_definitely (parser);
26457 cp_parser_require (parser, CPP_EQ, RT_EQ);
26458 rhs = cp_parser_assignment_expression (parser, false, NULL);
26459 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26460 decl, NOP_EXPR,
26461 rhs,
26462 tf_warning_or_error));
26463 add_private_clause = true;
26465 else
26467 decl = NULL;
26468 cp_parser_abort_tentative_parse (parser);
26469 init = cp_parser_expression (parser, false, NULL);
26470 if (init)
26472 if (TREE_CODE (init) == MODIFY_EXPR
26473 || TREE_CODE (init) == MODOP_EXPR)
26474 real_decl = TREE_OPERAND (init, 0);
26479 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26480 if (this_pre_body)
26482 this_pre_body = pop_stmt_list (this_pre_body);
26483 if (pre_body)
26485 tree t = pre_body;
26486 pre_body = push_stmt_list ();
26487 add_stmt (t);
26488 add_stmt (this_pre_body);
26489 pre_body = pop_stmt_list (pre_body);
26491 else
26492 pre_body = this_pre_body;
26495 if (decl)
26496 real_decl = decl;
26497 if (par_clauses != NULL && real_decl != NULL_TREE)
26499 tree *c;
26500 for (c = par_clauses; *c ; )
26501 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26502 && OMP_CLAUSE_DECL (*c) == real_decl)
26504 error_at (loc, "iteration variable %qD"
26505 " should not be firstprivate", real_decl);
26506 *c = OMP_CLAUSE_CHAIN (*c);
26508 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26509 && OMP_CLAUSE_DECL (*c) == real_decl)
26511 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26512 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26513 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26514 OMP_CLAUSE_DECL (l) = real_decl;
26515 OMP_CLAUSE_CHAIN (l) = clauses;
26516 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26517 clauses = l;
26518 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26519 CP_OMP_CLAUSE_INFO (*c) = NULL;
26520 add_private_clause = false;
26522 else
26524 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26525 && OMP_CLAUSE_DECL (*c) == real_decl)
26526 add_private_clause = false;
26527 c = &OMP_CLAUSE_CHAIN (*c);
26531 if (add_private_clause)
26533 tree c;
26534 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26536 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26537 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26538 && OMP_CLAUSE_DECL (c) == decl)
26539 break;
26540 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26541 && OMP_CLAUSE_DECL (c) == decl)
26542 error_at (loc, "iteration variable %qD "
26543 "should not be firstprivate",
26544 decl);
26545 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26546 && OMP_CLAUSE_DECL (c) == decl)
26547 error_at (loc, "iteration variable %qD should not be reduction",
26548 decl);
26550 if (c == NULL)
26552 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26553 OMP_CLAUSE_DECL (c) = decl;
26554 c = finish_omp_clauses (c);
26555 if (c)
26557 OMP_CLAUSE_CHAIN (c) = clauses;
26558 clauses = c;
26563 cond = NULL;
26564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26565 cond = cp_parser_omp_for_cond (parser, decl);
26566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26568 incr = NULL;
26569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26571 /* If decl is an iterator, preserve the operator on decl
26572 until finish_omp_for. */
26573 if (real_decl
26574 && ((processing_template_decl
26575 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26576 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26577 incr = cp_parser_omp_for_incr (parser, real_decl);
26578 else
26579 incr = cp_parser_expression (parser, false, NULL);
26580 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
26581 SET_EXPR_LOCATION (incr, input_location);
26584 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26585 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26586 /*or_comma=*/false,
26587 /*consume_paren=*/true);
26589 TREE_VEC_ELT (declv, i) = decl;
26590 TREE_VEC_ELT (initv, i) = init;
26591 TREE_VEC_ELT (condv, i) = cond;
26592 TREE_VEC_ELT (incrv, i) = incr;
26594 if (i == collapse - 1)
26595 break;
26597 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26598 in between the collapsed for loops to be still considered perfectly
26599 nested. Hopefully the final version clarifies this.
26600 For now handle (multiple) {'s and empty statements. */
26601 cp_parser_parse_tentatively (parser);
26604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26605 break;
26606 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26608 cp_lexer_consume_token (parser->lexer);
26609 bracecount++;
26611 else if (bracecount
26612 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26613 cp_lexer_consume_token (parser->lexer);
26614 else
26616 loc = cp_lexer_peek_token (parser->lexer)->location;
26617 error_at (loc, "not enough collapsed for loops");
26618 collapse_err = true;
26619 cp_parser_abort_tentative_parse (parser);
26620 declv = NULL_TREE;
26621 break;
26624 while (1);
26626 if (declv)
26628 cp_parser_parse_definitely (parser);
26629 nbraces += bracecount;
26633 /* Note that we saved the original contents of this flag when we entered
26634 the structured block, and so we don't need to re-save it here. */
26635 parser->in_statement = IN_OMP_FOR;
26637 /* Note that the grammar doesn't call for a structured block here,
26638 though the loop as a whole is a structured block. */
26639 body = push_stmt_list ();
26640 cp_parser_statement (parser, NULL_TREE, false, NULL);
26641 body = pop_stmt_list (body);
26643 if (declv == NULL_TREE)
26644 ret = NULL_TREE;
26645 else
26646 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26647 pre_body, clauses);
26649 while (nbraces)
26651 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26653 cp_lexer_consume_token (parser->lexer);
26654 nbraces--;
26656 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26657 cp_lexer_consume_token (parser->lexer);
26658 else
26660 if (!collapse_err)
26662 error_at (cp_lexer_peek_token (parser->lexer)->location,
26663 "collapsed loops not perfectly nested");
26665 collapse_err = true;
26666 cp_parser_statement_seq_opt (parser, NULL);
26667 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26668 break;
26672 while (!VEC_empty (tree, for_block))
26673 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26674 release_tree_vector (for_block);
26676 return ret;
26679 /* OpenMP 2.5:
26680 #pragma omp for for-clause[optseq] new-line
26681 for-loop */
26683 #define OMP_FOR_CLAUSE_MASK \
26684 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26685 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26686 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26687 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26688 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26689 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26690 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26691 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26693 static tree
26694 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26696 tree clauses, sb, ret;
26697 unsigned int save;
26699 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26700 "#pragma omp for", pragma_tok);
26702 sb = begin_omp_structured_block ();
26703 save = cp_parser_begin_omp_structured_block (parser);
26705 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26707 cp_parser_end_omp_structured_block (parser, save);
26708 add_stmt (finish_omp_structured_block (sb));
26710 return ret;
26713 /* OpenMP 2.5:
26714 # pragma omp master new-line
26715 structured-block */
26717 static tree
26718 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26720 cp_parser_require_pragma_eol (parser, pragma_tok);
26721 return c_finish_omp_master (input_location,
26722 cp_parser_omp_structured_block (parser));
26725 /* OpenMP 2.5:
26726 # pragma omp ordered new-line
26727 structured-block */
26729 static tree
26730 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26732 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26733 cp_parser_require_pragma_eol (parser, pragma_tok);
26734 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26737 /* OpenMP 2.5:
26739 section-scope:
26740 { section-sequence }
26742 section-sequence:
26743 section-directive[opt] structured-block
26744 section-sequence section-directive structured-block */
26746 static tree
26747 cp_parser_omp_sections_scope (cp_parser *parser)
26749 tree stmt, substmt;
26750 bool error_suppress = false;
26751 cp_token *tok;
26753 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26754 return NULL_TREE;
26756 stmt = push_stmt_list ();
26758 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26760 unsigned save;
26762 substmt = begin_omp_structured_block ();
26763 save = cp_parser_begin_omp_structured_block (parser);
26765 while (1)
26767 cp_parser_statement (parser, NULL_TREE, false, NULL);
26769 tok = cp_lexer_peek_token (parser->lexer);
26770 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26771 break;
26772 if (tok->type == CPP_CLOSE_BRACE)
26773 break;
26774 if (tok->type == CPP_EOF)
26775 break;
26778 cp_parser_end_omp_structured_block (parser, save);
26779 substmt = finish_omp_structured_block (substmt);
26780 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26781 add_stmt (substmt);
26784 while (1)
26786 tok = cp_lexer_peek_token (parser->lexer);
26787 if (tok->type == CPP_CLOSE_BRACE)
26788 break;
26789 if (tok->type == CPP_EOF)
26790 break;
26792 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26794 cp_lexer_consume_token (parser->lexer);
26795 cp_parser_require_pragma_eol (parser, tok);
26796 error_suppress = false;
26798 else if (!error_suppress)
26800 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26801 error_suppress = true;
26804 substmt = cp_parser_omp_structured_block (parser);
26805 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26806 add_stmt (substmt);
26808 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26810 substmt = pop_stmt_list (stmt);
26812 stmt = make_node (OMP_SECTIONS);
26813 TREE_TYPE (stmt) = void_type_node;
26814 OMP_SECTIONS_BODY (stmt) = substmt;
26816 add_stmt (stmt);
26817 return stmt;
26820 /* OpenMP 2.5:
26821 # pragma omp sections sections-clause[optseq] newline
26822 sections-scope */
26824 #define OMP_SECTIONS_CLAUSE_MASK \
26825 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26826 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26827 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26828 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26829 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26831 static tree
26832 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26834 tree clauses, ret;
26836 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26837 "#pragma omp sections", pragma_tok);
26839 ret = cp_parser_omp_sections_scope (parser);
26840 if (ret)
26841 OMP_SECTIONS_CLAUSES (ret) = clauses;
26843 return ret;
26846 /* OpenMP 2.5:
26847 # pragma parallel parallel-clause new-line
26848 # pragma parallel for parallel-for-clause new-line
26849 # pragma parallel sections parallel-sections-clause new-line */
26851 #define OMP_PARALLEL_CLAUSE_MASK \
26852 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26853 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26854 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26855 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26856 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26857 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26858 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26859 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26861 static tree
26862 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26864 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26865 const char *p_name = "#pragma omp parallel";
26866 tree stmt, clauses, par_clause, ws_clause, block;
26867 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26868 unsigned int save;
26869 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26873 cp_lexer_consume_token (parser->lexer);
26874 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26875 p_name = "#pragma omp parallel for";
26876 mask |= OMP_FOR_CLAUSE_MASK;
26877 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26879 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26881 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26882 const char *p = IDENTIFIER_POINTER (id);
26883 if (strcmp (p, "sections") == 0)
26885 cp_lexer_consume_token (parser->lexer);
26886 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26887 p_name = "#pragma omp parallel sections";
26888 mask |= OMP_SECTIONS_CLAUSE_MASK;
26889 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26893 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26894 block = begin_omp_parallel ();
26895 save = cp_parser_begin_omp_structured_block (parser);
26897 switch (p_kind)
26899 case PRAGMA_OMP_PARALLEL:
26900 cp_parser_statement (parser, NULL_TREE, false, NULL);
26901 par_clause = clauses;
26902 break;
26904 case PRAGMA_OMP_PARALLEL_FOR:
26905 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26906 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26907 break;
26909 case PRAGMA_OMP_PARALLEL_SECTIONS:
26910 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26911 stmt = cp_parser_omp_sections_scope (parser);
26912 if (stmt)
26913 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26914 break;
26916 default:
26917 gcc_unreachable ();
26920 cp_parser_end_omp_structured_block (parser, save);
26921 stmt = finish_omp_parallel (par_clause, block);
26922 if (p_kind != PRAGMA_OMP_PARALLEL)
26923 OMP_PARALLEL_COMBINED (stmt) = 1;
26924 return stmt;
26927 /* OpenMP 2.5:
26928 # pragma omp single single-clause[optseq] new-line
26929 structured-block */
26931 #define OMP_SINGLE_CLAUSE_MASK \
26932 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26933 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26934 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26935 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26937 static tree
26938 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26940 tree stmt = make_node (OMP_SINGLE);
26941 TREE_TYPE (stmt) = void_type_node;
26943 OMP_SINGLE_CLAUSES (stmt)
26944 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26945 "#pragma omp single", pragma_tok);
26946 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26948 return add_stmt (stmt);
26951 /* OpenMP 3.0:
26952 # pragma omp task task-clause[optseq] new-line
26953 structured-block */
26955 #define OMP_TASK_CLAUSE_MASK \
26956 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26957 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26958 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26959 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26960 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26961 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26962 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26963 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26965 static tree
26966 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26968 tree clauses, block;
26969 unsigned int save;
26971 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26972 "#pragma omp task", pragma_tok);
26973 block = begin_omp_task ();
26974 save = cp_parser_begin_omp_structured_block (parser);
26975 cp_parser_statement (parser, NULL_TREE, false, NULL);
26976 cp_parser_end_omp_structured_block (parser, save);
26977 return finish_omp_task (clauses, block);
26980 /* OpenMP 3.0:
26981 # pragma omp taskwait new-line */
26983 static void
26984 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26986 cp_parser_require_pragma_eol (parser, pragma_tok);
26987 finish_omp_taskwait ();
26990 /* OpenMP 3.1:
26991 # pragma omp taskyield new-line */
26993 static void
26994 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26996 cp_parser_require_pragma_eol (parser, pragma_tok);
26997 finish_omp_taskyield ();
27000 /* OpenMP 2.5:
27001 # pragma omp threadprivate (variable-list) */
27003 static void
27004 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27006 tree vars;
27008 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27009 cp_parser_require_pragma_eol (parser, pragma_tok);
27011 finish_omp_threadprivate (vars);
27014 /* Main entry point to OpenMP statement pragmas. */
27016 static void
27017 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27019 tree stmt;
27021 switch (pragma_tok->pragma_kind)
27023 case PRAGMA_OMP_ATOMIC:
27024 cp_parser_omp_atomic (parser, pragma_tok);
27025 return;
27026 case PRAGMA_OMP_CRITICAL:
27027 stmt = cp_parser_omp_critical (parser, pragma_tok);
27028 break;
27029 case PRAGMA_OMP_FOR:
27030 stmt = cp_parser_omp_for (parser, pragma_tok);
27031 break;
27032 case PRAGMA_OMP_MASTER:
27033 stmt = cp_parser_omp_master (parser, pragma_tok);
27034 break;
27035 case PRAGMA_OMP_ORDERED:
27036 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27037 break;
27038 case PRAGMA_OMP_PARALLEL:
27039 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27040 break;
27041 case PRAGMA_OMP_SECTIONS:
27042 stmt = cp_parser_omp_sections (parser, pragma_tok);
27043 break;
27044 case PRAGMA_OMP_SINGLE:
27045 stmt = cp_parser_omp_single (parser, pragma_tok);
27046 break;
27047 case PRAGMA_OMP_TASK:
27048 stmt = cp_parser_omp_task (parser, pragma_tok);
27049 break;
27050 default:
27051 gcc_unreachable ();
27054 if (stmt)
27055 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27058 /* Transactional Memory parsing routines. */
27060 /* Parse a transaction attribute.
27062 txn-attribute:
27063 attribute
27064 [ [ identifier ] ]
27066 ??? Simplify this when C++0x bracket attributes are
27067 implemented properly. */
27069 static tree
27070 cp_parser_txn_attribute_opt (cp_parser *parser)
27072 cp_token *token;
27073 tree attr_name, attr = NULL;
27075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27076 return cp_parser_attributes_opt (parser);
27078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27079 return NULL_TREE;
27080 cp_lexer_consume_token (parser->lexer);
27081 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27082 goto error1;
27084 token = cp_lexer_peek_token (parser->lexer);
27085 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27087 token = cp_lexer_consume_token (parser->lexer);
27089 attr_name = (token->type == CPP_KEYWORD
27090 /* For keywords, use the canonical spelling,
27091 not the parsed identifier. */
27092 ? ridpointers[(int) token->keyword]
27093 : token->u.value);
27094 attr = build_tree_list (attr_name, NULL_TREE);
27096 else
27097 cp_parser_error (parser, "expected identifier");
27099 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27100 error1:
27101 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27102 return attr;
27105 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27107 transaction-statement:
27108 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27109 compound-statement
27110 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27113 static tree
27114 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27116 unsigned char old_in = parser->in_transaction;
27117 unsigned char this_in = 1, new_in;
27118 cp_token *token;
27119 tree stmt, attrs, noex;
27121 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27122 || keyword == RID_TRANSACTION_RELAXED);
27123 token = cp_parser_require_keyword (parser, keyword,
27124 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27125 : RT_TRANSACTION_RELAXED));
27126 gcc_assert (token != NULL);
27128 if (keyword == RID_TRANSACTION_RELAXED)
27129 this_in |= TM_STMT_ATTR_RELAXED;
27130 else
27132 attrs = cp_parser_txn_attribute_opt (parser);
27133 if (attrs)
27134 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27137 /* Parse a noexcept specification. */
27138 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27140 /* Keep track if we're in the lexical scope of an outer transaction. */
27141 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27143 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27145 parser->in_transaction = new_in;
27146 cp_parser_compound_statement (parser, NULL, false, false);
27147 parser->in_transaction = old_in;
27149 finish_transaction_stmt (stmt, NULL, this_in, noex);
27151 return stmt;
27154 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27156 transaction-expression:
27157 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27158 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27161 static tree
27162 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27164 unsigned char old_in = parser->in_transaction;
27165 unsigned char this_in = 1;
27166 cp_token *token;
27167 tree expr, noex;
27168 bool noex_expr;
27170 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27171 || keyword == RID_TRANSACTION_RELAXED);
27173 if (!flag_tm)
27174 error (keyword == RID_TRANSACTION_RELAXED
27175 ? G_("%<__transaction_relaxed%> without transactional memory "
27176 "support enabled")
27177 : G_("%<__transaction_atomic%> without transactional memory "
27178 "support enabled"));
27180 token = cp_parser_require_keyword (parser, keyword,
27181 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27182 : RT_TRANSACTION_RELAXED));
27183 gcc_assert (token != NULL);
27185 if (keyword == RID_TRANSACTION_RELAXED)
27186 this_in |= TM_STMT_ATTR_RELAXED;
27188 /* Set this early. This might mean that we allow transaction_cancel in
27189 an expression that we find out later actually has to be a constexpr.
27190 However, we expect that cxx_constant_value will be able to deal with
27191 this; also, if the noexcept has no constexpr, then what we parse next
27192 really is a transaction's body. */
27193 parser->in_transaction = this_in;
27195 /* Parse a noexcept specification. */
27196 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27197 true);
27199 if (!noex || !noex_expr
27200 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27202 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27204 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27205 finish_parenthesized_expr (expr);
27207 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27209 else
27211 /* The only expression that is available got parsed for the noexcept
27212 already. noexcept is true then. */
27213 expr = noex;
27214 noex = boolean_true_node;
27217 expr = build_transaction_expr (token->location, expr, this_in, noex);
27218 parser->in_transaction = old_in;
27220 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27221 return error_mark_node;
27223 return (flag_tm ? expr : error_mark_node);
27226 /* Parse a function-transaction-block.
27228 function-transaction-block:
27229 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27230 function-body
27231 __transaction_atomic txn-attribute[opt] function-try-block
27232 __transaction_relaxed ctor-initializer[opt] function-body
27233 __transaction_relaxed function-try-block
27236 static bool
27237 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27239 unsigned char old_in = parser->in_transaction;
27240 unsigned char new_in = 1;
27241 tree compound_stmt, stmt, attrs;
27242 bool ctor_initializer_p;
27243 cp_token *token;
27245 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27246 || keyword == RID_TRANSACTION_RELAXED);
27247 token = cp_parser_require_keyword (parser, keyword,
27248 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27249 : RT_TRANSACTION_RELAXED));
27250 gcc_assert (token != NULL);
27252 if (keyword == RID_TRANSACTION_RELAXED)
27253 new_in |= TM_STMT_ATTR_RELAXED;
27254 else
27256 attrs = cp_parser_txn_attribute_opt (parser);
27257 if (attrs)
27258 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27261 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27263 parser->in_transaction = new_in;
27265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27266 ctor_initializer_p = cp_parser_function_try_block (parser);
27267 else
27268 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27269 (parser, /*in_function_try_block=*/false);
27271 parser->in_transaction = old_in;
27273 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27275 return ctor_initializer_p;
27278 /* Parse a __transaction_cancel statement.
27280 cancel-statement:
27281 __transaction_cancel txn-attribute[opt] ;
27282 __transaction_cancel txn-attribute[opt] throw-expression ;
27284 ??? Cancel and throw is not yet implemented. */
27286 static tree
27287 cp_parser_transaction_cancel (cp_parser *parser)
27289 cp_token *token;
27290 bool is_outer = false;
27291 tree stmt, attrs;
27293 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27294 RT_TRANSACTION_CANCEL);
27295 gcc_assert (token != NULL);
27297 attrs = cp_parser_txn_attribute_opt (parser);
27298 if (attrs)
27299 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27301 /* ??? Parse cancel-and-throw here. */
27303 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27305 if (!flag_tm)
27307 error_at (token->location, "%<__transaction_cancel%> without "
27308 "transactional memory support enabled");
27309 return error_mark_node;
27311 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27313 error_at (token->location, "%<__transaction_cancel%> within a "
27314 "%<__transaction_relaxed%>");
27315 return error_mark_node;
27317 else if (is_outer)
27319 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27320 && !is_tm_may_cancel_outer (current_function_decl))
27322 error_at (token->location, "outer %<__transaction_cancel%> not "
27323 "within outer %<__transaction_atomic%>");
27324 error_at (token->location,
27325 " or a %<transaction_may_cancel_outer%> function");
27326 return error_mark_node;
27329 else if (parser->in_transaction == 0)
27331 error_at (token->location, "%<__transaction_cancel%> not within "
27332 "%<__transaction_atomic%>");
27333 return error_mark_node;
27336 stmt = build_tm_abort_call (token->location, is_outer);
27337 add_stmt (stmt);
27338 finish_stmt ();
27340 return stmt;
27343 /* The parser. */
27345 static GTY (()) cp_parser *the_parser;
27348 /* Special handling for the first token or line in the file. The first
27349 thing in the file might be #pragma GCC pch_preprocess, which loads a
27350 PCH file, which is a GC collection point. So we need to handle this
27351 first pragma without benefit of an existing lexer structure.
27353 Always returns one token to the caller in *FIRST_TOKEN. This is
27354 either the true first token of the file, or the first token after
27355 the initial pragma. */
27357 static void
27358 cp_parser_initial_pragma (cp_token *first_token)
27360 tree name = NULL;
27362 cp_lexer_get_preprocessor_token (NULL, first_token);
27363 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27364 return;
27366 cp_lexer_get_preprocessor_token (NULL, first_token);
27367 if (first_token->type == CPP_STRING)
27369 name = first_token->u.value;
27371 cp_lexer_get_preprocessor_token (NULL, first_token);
27372 if (first_token->type != CPP_PRAGMA_EOL)
27373 error_at (first_token->location,
27374 "junk at end of %<#pragma GCC pch_preprocess%>");
27376 else
27377 error_at (first_token->location, "expected string literal");
27379 /* Skip to the end of the pragma. */
27380 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27381 cp_lexer_get_preprocessor_token (NULL, first_token);
27383 /* Now actually load the PCH file. */
27384 if (name)
27385 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27387 /* Read one more token to return to our caller. We have to do this
27388 after reading the PCH file in, since its pointers have to be
27389 live. */
27390 cp_lexer_get_preprocessor_token (NULL, first_token);
27393 /* Normal parsing of a pragma token. Here we can (and must) use the
27394 regular lexer. */
27396 static bool
27397 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27399 cp_token *pragma_tok;
27400 unsigned int id;
27402 pragma_tok = cp_lexer_consume_token (parser->lexer);
27403 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27404 parser->lexer->in_pragma = true;
27406 id = pragma_tok->pragma_kind;
27407 switch (id)
27409 case PRAGMA_GCC_PCH_PREPROCESS:
27410 error_at (pragma_tok->location,
27411 "%<#pragma GCC pch_preprocess%> must be first");
27412 break;
27414 case PRAGMA_OMP_BARRIER:
27415 switch (context)
27417 case pragma_compound:
27418 cp_parser_omp_barrier (parser, pragma_tok);
27419 return false;
27420 case pragma_stmt:
27421 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27422 "used in compound statements");
27423 break;
27424 default:
27425 goto bad_stmt;
27427 break;
27429 case PRAGMA_OMP_FLUSH:
27430 switch (context)
27432 case pragma_compound:
27433 cp_parser_omp_flush (parser, pragma_tok);
27434 return false;
27435 case pragma_stmt:
27436 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27437 "used in compound statements");
27438 break;
27439 default:
27440 goto bad_stmt;
27442 break;
27444 case PRAGMA_OMP_TASKWAIT:
27445 switch (context)
27447 case pragma_compound:
27448 cp_parser_omp_taskwait (parser, pragma_tok);
27449 return false;
27450 case pragma_stmt:
27451 error_at (pragma_tok->location,
27452 "%<#pragma omp taskwait%> may only be "
27453 "used in compound statements");
27454 break;
27455 default:
27456 goto bad_stmt;
27458 break;
27460 case PRAGMA_OMP_TASKYIELD:
27461 switch (context)
27463 case pragma_compound:
27464 cp_parser_omp_taskyield (parser, pragma_tok);
27465 return false;
27466 case pragma_stmt:
27467 error_at (pragma_tok->location,
27468 "%<#pragma omp taskyield%> may only be "
27469 "used in compound statements");
27470 break;
27471 default:
27472 goto bad_stmt;
27474 break;
27476 case PRAGMA_OMP_THREADPRIVATE:
27477 cp_parser_omp_threadprivate (parser, pragma_tok);
27478 return false;
27480 case PRAGMA_OMP_ATOMIC:
27481 case PRAGMA_OMP_CRITICAL:
27482 case PRAGMA_OMP_FOR:
27483 case PRAGMA_OMP_MASTER:
27484 case PRAGMA_OMP_ORDERED:
27485 case PRAGMA_OMP_PARALLEL:
27486 case PRAGMA_OMP_SECTIONS:
27487 case PRAGMA_OMP_SINGLE:
27488 case PRAGMA_OMP_TASK:
27489 if (context == pragma_external)
27490 goto bad_stmt;
27491 cp_parser_omp_construct (parser, pragma_tok);
27492 return true;
27494 case PRAGMA_OMP_SECTION:
27495 error_at (pragma_tok->location,
27496 "%<#pragma omp section%> may only be used in "
27497 "%<#pragma omp sections%> construct");
27498 break;
27500 default:
27501 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27502 c_invoke_pragma_handler (id);
27503 break;
27505 bad_stmt:
27506 cp_parser_error (parser, "expected declaration specifiers");
27507 break;
27510 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27511 return false;
27514 /* The interface the pragma parsers have to the lexer. */
27516 enum cpp_ttype
27517 pragma_lex (tree *value)
27519 cp_token *tok;
27520 enum cpp_ttype ret;
27522 tok = cp_lexer_peek_token (the_parser->lexer);
27524 ret = tok->type;
27525 *value = tok->u.value;
27527 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27528 ret = CPP_EOF;
27529 else if (ret == CPP_STRING)
27530 *value = cp_parser_string_literal (the_parser, false, false);
27531 else
27533 cp_lexer_consume_token (the_parser->lexer);
27534 if (ret == CPP_KEYWORD)
27535 ret = CPP_NAME;
27538 return ret;
27542 /* External interface. */
27544 /* Parse one entire translation unit. */
27546 void
27547 c_parse_file (void)
27549 static bool already_called = false;
27551 if (already_called)
27553 sorry ("inter-module optimizations not implemented for C++");
27554 return;
27556 already_called = true;
27558 the_parser = cp_parser_new ();
27559 push_deferring_access_checks (flag_access_control
27560 ? dk_no_deferred : dk_no_check);
27561 cp_parser_translation_unit (the_parser);
27562 the_parser = NULL;
27565 #include "gt-cp-parser.h"