gcc/c-family/
[official-gcc.git] / gcc / cp / parser.c
blob360e1973dbe16c7bcb669f6109739e676e6a5cae
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 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 "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "plugin.h"
42 /* The lexer. */
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
52 tree value;
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
60 /* A C++ token. */
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
68 /* Token flags. */
69 unsigned char flags;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
79 location_t location;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
94 static cp_token eof_token =
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
102 it is created. */
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
109 buffer. */
110 size_t buffer_length;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
123 tokens. */
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
129 /* True if we should output debugging information. */
130 bool debugging_p;
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
134 bool in_pragma;
135 } cp_lexer;
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
141 a lexer. */
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
149 } cp_token_cache;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153 NIC_NONE,
154 /* floating-point literal */
155 NIC_FLOAT,
156 /* %<this%> */
157 NIC_THIS,
158 /* %<__FUNCTION__%> */
159 NIC_FUNC_NAME,
160 /* %<__PRETTY_FUNCTION__%> */
161 NIC_PRETTY_FUNC,
162 /* %<__func__%> */
163 NIC_C99_FUNC,
164 /* "%<va_arg%> */
165 NIC_VA_ARG,
166 /* a cast */
167 NIC_CAST,
168 /* %<typeid%> operator */
169 NIC_TYPEID,
170 /* non-constant compound literals */
171 NIC_NCC,
172 /* a function call */
173 NIC_FUNC_CALL,
174 /* an increment */
175 NIC_INC,
176 /* an decrement */
177 NIC_DEC,
178 /* an array reference */
179 NIC_ARRAY_REF,
180 /* %<->%> */
181 NIC_ARROW,
182 /* %<.%> */
183 NIC_POINT,
184 /* the address of a label */
185 NIC_ADDR_LABEL,
186 /* %<*%> */
187 NIC_STAR,
188 /* %<&%> */
189 NIC_ADDR,
190 /* %<++%> */
191 NIC_PREINCREMENT,
192 /* %<--%> */
193 NIC_PREDECREMENT,
194 /* %<new%> */
195 NIC_NEW,
196 /* %<delete%> */
197 NIC_DEL,
198 /* calls to overloaded operators */
199 NIC_OVERLOADED,
200 /* an assignment */
201 NIC_ASSIGNMENT,
202 /* a comma operator */
203 NIC_COMMA,
204 /* a call to a constructor */
205 NIC_CONSTRUCTOR
206 } non_integral_constant;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210 /* NULL */
211 NLE_NULL,
212 /* is not a type */
213 NLE_TYPE,
214 /* is not a class or namespace */
215 NLE_CXX98,
216 /* is not a class, namespace, or enumeration */
217 NLE_NOT_CXX98
218 } name_lookup_error;
220 /* The various kinds of required token */
221 typedef enum required_token {
222 RT_NONE,
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
229 RT_COMMA, /* ',' */
230 RT_SCOPE, /* '::' */
231 RT_LESS, /* '<' */
232 RT_GREATER, /* '>' */
233 RT_EQ, /* '=' */
234 RT_ELLIPSIS, /* '...' */
235 RT_MULT, /* '*' */
236 RT_COMPL, /* '~' */
237 RT_COLON, /* ':' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
244 /* The type is CPP_KEYWORD */
245 RT_NEW, /* new */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
257 RT_ASM, /* asm */
258 RT_TRY, /* try */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
273 /* Prototypes. */
275 static cp_lexer *cp_lexer_new_main
276 (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280 (cp_lexer *);
281 static int cp_lexer_saving_tokens
282 (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284 (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290 (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300 (cp_lexer *);
301 static void cp_lexer_purge_token
302 (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306 (cp_lexer *);
307 static void cp_lexer_commit_tokens
308 (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310 (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315 (cp_lexer *);
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
333 static void cp_parser_initial_pragma
334 (cp_token *);
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
364 /* Variables. */
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376 preprocessor. */
378 static cp_lexer *
379 cp_lexer_new_main (void)
381 cp_token first_token;
382 cp_lexer *lexer;
383 cp_token *pos;
384 size_t alloc;
385 size_t space;
386 cp_token *buffer;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
409 /* Put the first token in the buffer. */
410 space = alloc;
411 pos = buffer;
412 *pos = first_token;
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
417 pos++;
418 if (!--space)
420 space = alloc;
421 alloc *= 2;
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
425 cp_lexer_get_preprocessor_token (lexer, pos);
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
434 done_lexing = true;
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
437 return lexer;
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
462 #endif
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
465 return lexer;
468 /* Frees all resources associated with LEXER. */
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
473 if (lexer->buffer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
476 ggc_free (lexer);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
502 return pos;
505 /* nonzero if we are presently saving tokens. */
507 static inline int
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
510 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
518 static void
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
521 static int is_extern_c = 0;
523 /* Get a new token from the preprocessor. */
524 token->type
525 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527 token->keyword = RID_MAX;
528 token->pragma_kind = PRAGMA_NONE;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c += pending_lang_change;
534 pending_lang_change = 0;
535 token->implicit_extern_c = is_extern_c > 0;
537 /* Check to see if this token is a keyword. */
538 if (token->type == CPP_NAME)
540 if (C_IS_RESERVED_WORD (token->u.value))
542 /* Mark this token as a keyword. */
543 token->type = CPP_KEYWORD;
544 /* Record which keyword. */
545 token->keyword = C_RID_CODE (token->u.value);
547 else
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
553 /* Warn about the C++0x keyword (but still treat it as
554 an identifier). */
555 warning (OPT_Wc__0x_compat,
556 "identifier %qE will become a keyword in C++0x",
557 token->u.value);
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token->u.value, RID_MAX);
564 token->ambiguous_p = false;
565 token->keyword = RID_MAX;
568 else if (token->type == CPP_AT_NAME)
570 /* This only happens in Objective-C++; it must be a keyword. */
571 token->type = CPP_KEYWORD;
572 switch (C_RID_CODE (token->u.value))
574 /* Replace 'class' with '@class', 'private' with '@private',
575 etc. This prevents confusion with the C++ keyword
576 'class', and makes the tokens consistent with other
577 Objective-C 'AT' keywords. For example '@class' is
578 reported as RID_AT_CLASS which is consistent with
579 '@synchronized', which is reported as
580 RID_AT_SYNCHRONIZED.
582 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
583 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
584 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
585 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
586 case RID_THROW: token->keyword = RID_AT_THROW; break;
587 case RID_TRY: token->keyword = RID_AT_TRY; break;
588 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
589 default: token->keyword = C_RID_CODE (token->u.value);
592 else if (token->type == CPP_PRAGMA)
594 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
595 token->pragma_kind = ((enum pragma_kind)
596 TREE_INT_CST_LOW (token->u.value));
597 token->u.value = NULL_TREE;
601 /* Update the globals input_location and the input file stack from TOKEN. */
602 static inline void
603 cp_lexer_set_source_position_from_token (cp_token *token)
605 if (token->type != CPP_EOF)
607 input_location = token->location;
611 /* Return a pointer to the next token in the token stream, but do not
612 consume it. */
614 static inline cp_token *
615 cp_lexer_peek_token (cp_lexer *lexer)
617 if (cp_lexer_debugging_p (lexer))
619 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
620 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
621 putc ('\n', cp_lexer_debug_stream);
623 return lexer->next_token;
626 /* Return true if the next token has the indicated TYPE. */
628 static inline bool
629 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
631 return cp_lexer_peek_token (lexer)->type == type;
634 /* Return true if the next token does not have the indicated TYPE. */
636 static inline bool
637 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
639 return !cp_lexer_next_token_is (lexer, type);
642 /* Return true if the next token is the indicated KEYWORD. */
644 static inline bool
645 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
647 return cp_lexer_peek_token (lexer)->keyword == keyword;
650 /* Return true if the next token is not the indicated KEYWORD. */
652 static inline bool
653 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
655 return cp_lexer_peek_token (lexer)->keyword != keyword;
658 /* Return true if the next token is a keyword for a decl-specifier. */
660 static bool
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
663 cp_token *token;
665 token = cp_lexer_peek_token (lexer);
666 switch (token->keyword)
668 /* auto specifier: storage-class-specifier in C++,
669 simple-type-specifier in C++0x. */
670 case RID_AUTO:
671 /* Storage classes. */
672 case RID_REGISTER:
673 case RID_STATIC:
674 case RID_EXTERN:
675 case RID_MUTABLE:
676 case RID_THREAD:
677 /* Elaborated type specifiers. */
678 case RID_ENUM:
679 case RID_CLASS:
680 case RID_STRUCT:
681 case RID_UNION:
682 case RID_TYPENAME:
683 /* Simple type specifiers. */
684 case RID_CHAR:
685 case RID_CHAR16:
686 case RID_CHAR32:
687 case RID_WCHAR:
688 case RID_BOOL:
689 case RID_SHORT:
690 case RID_INT:
691 case RID_LONG:
692 case RID_INT128:
693 case RID_SIGNED:
694 case RID_UNSIGNED:
695 case RID_FLOAT:
696 case RID_DOUBLE:
697 case RID_VOID:
698 /* GNU extensions. */
699 case RID_ATTRIBUTE:
700 case RID_TYPEOF:
701 /* C++0x extensions. */
702 case RID_DECLTYPE:
703 return true;
705 default:
706 return false;
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token (except
712 that it is not inline). One would like to disallow that case, but
713 there is one case (cp_parser_nth_token_starts_template_id) where
714 the caller passes a variable for N and it might be 1. */
716 static cp_token *
717 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
719 cp_token *token;
721 /* N is 1-based, not zero-based. */
722 gcc_assert (n > 0);
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream,
726 "cp_lexer: peeking ahead %ld at token: ", (long)n);
728 --n;
729 token = lexer->next_token;
730 gcc_assert (!n || token != &eof_token);
731 while (n != 0)
733 ++token;
734 if (token == lexer->last_token)
736 token = &eof_token;
737 break;
740 if (token->type != CPP_PURGED)
741 --n;
744 if (cp_lexer_debugging_p (lexer))
746 cp_lexer_print_token (cp_lexer_debug_stream, token);
747 putc ('\n', cp_lexer_debug_stream);
750 return token;
753 /* Return the next token, and advance the lexer's next_token pointer
754 to point to the next non-purged token. */
756 static cp_token *
757 cp_lexer_consume_token (cp_lexer* lexer)
759 cp_token *token = lexer->next_token;
761 gcc_assert (token != &eof_token);
762 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
766 lexer->next_token++;
767 if (lexer->next_token == lexer->last_token)
769 lexer->next_token = &eof_token;
770 break;
774 while (lexer->next_token->type == CPP_PURGED);
776 cp_lexer_set_source_position_from_token (token);
778 /* Provide debugging output. */
779 if (cp_lexer_debugging_p (lexer))
781 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
782 cp_lexer_print_token (cp_lexer_debug_stream, token);
783 putc ('\n', cp_lexer_debug_stream);
786 return token;
789 /* Permanently remove the next token from the token stream, and
790 advance the next_token pointer to refer to the next non-purged
791 token. */
793 static void
794 cp_lexer_purge_token (cp_lexer *lexer)
796 cp_token *tok = lexer->next_token;
798 gcc_assert (tok != &eof_token);
799 tok->type = CPP_PURGED;
800 tok->location = UNKNOWN_LOCATION;
801 tok->u.value = NULL_TREE;
802 tok->keyword = RID_MAX;
806 tok++;
807 if (tok == lexer->last_token)
809 tok = &eof_token;
810 break;
813 while (tok->type == CPP_PURGED);
814 lexer->next_token = tok;
817 /* Permanently remove all tokens after TOK, up to, but not
818 including, the token that will be returned next by
819 cp_lexer_peek_token. */
821 static void
822 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
824 cp_token *peek = lexer->next_token;
826 if (peek == &eof_token)
827 peek = lexer->last_token;
829 gcc_assert (tok < peek);
831 for ( tok += 1; tok != peek; tok += 1)
833 tok->type = CPP_PURGED;
834 tok->location = UNKNOWN_LOCATION;
835 tok->u.value = NULL_TREE;
836 tok->keyword = RID_MAX;
840 /* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 VEC_safe_push (cp_token_position, heap,
851 lexer->saved_tokens, lexer->next_token);
854 /* Commit to the portion of the token stream most recently saved. */
856 static void
857 cp_lexer_commit_tokens (cp_lexer* lexer)
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer))
861 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
863 VEC_pop (cp_token_position, lexer->saved_tokens);
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867 to the token stream. Stop saving tokens. */
869 static void
870 cp_lexer_rollback_tokens (cp_lexer* lexer)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
876 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
879 /* Print a representation of the TOKEN on the STREAM. */
881 #ifdef ENABLE_CHECKING
883 static void
884 cp_lexer_print_token (FILE * stream, cp_token *token)
886 /* We don't use cpp_type2name here because the parser defines
887 a few tokens of its own. */
888 static const char *const token_names[] = {
889 /* cpplib-defined token types */
890 #define OP(e, s) #e,
891 #define TK(e, s) #e,
892 TTYPE_TABLE
893 #undef OP
894 #undef TK
895 /* C++ parser token types - see "Manifest constants", above. */
896 "KEYWORD",
897 "TEMPLATE_ID",
898 "NESTED_NAME_SPECIFIER",
899 "PURGED"
902 /* If we have a name for the token, print it out. Otherwise, we
903 simply give the numeric code. */
904 gcc_assert (token->type < ARRAY_SIZE(token_names));
905 fputs (token_names[token->type], stream);
907 /* For some tokens, print the associated data. */
908 switch (token->type)
910 case CPP_KEYWORD:
911 /* Some keywords have a value that is not an IDENTIFIER_NODE.
912 For example, `struct' is mapped to an INTEGER_CST. */
913 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
914 break;
915 /* else fall through */
916 case CPP_NAME:
917 fputs (IDENTIFIER_POINTER (token->u.value), stream);
918 break;
920 case CPP_STRING:
921 case CPP_STRING16:
922 case CPP_STRING32:
923 case CPP_WSTRING:
924 case CPP_UTF8STRING:
925 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
926 break;
928 default:
929 break;
933 /* Start emitting debugging information. */
935 static void
936 cp_lexer_start_debugging (cp_lexer* lexer)
938 lexer->debugging_p = true;
941 /* Stop emitting debugging information. */
943 static void
944 cp_lexer_stop_debugging (cp_lexer* lexer)
946 lexer->debugging_p = false;
949 #endif /* ENABLE_CHECKING */
951 /* Create a new cp_token_cache, representing a range of tokens. */
953 static cp_token_cache *
954 cp_token_cache_new (cp_token *first, cp_token *last)
956 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
957 cache->first = first;
958 cache->last = last;
959 return cache;
963 /* Decl-specifiers. */
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
967 static void
968 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
970 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
973 /* Declarators. */
975 /* Nothing other than the parser should be creating declarators;
976 declarators are a semi-syntactic representation of C++ entities.
977 Other parts of the front end that need to create entities (like
978 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
980 static cp_declarator *make_call_declarator
981 (cp_declarator *, tree, cp_cv_quals, tree, tree);
982 static cp_declarator *make_array_declarator
983 (cp_declarator *, tree);
984 static cp_declarator *make_pointer_declarator
985 (cp_cv_quals, cp_declarator *);
986 static cp_declarator *make_reference_declarator
987 (cp_cv_quals, cp_declarator *, bool);
988 static cp_parameter_declarator *make_parameter_declarator
989 (cp_decl_specifier_seq *, cp_declarator *, tree);
990 static cp_declarator *make_ptrmem_declarator
991 (cp_cv_quals, tree, cp_declarator *);
993 /* An erroneous declarator. */
994 static cp_declarator *cp_error_declarator;
996 /* The obstack on which declarators and related data structures are
997 allocated. */
998 static struct obstack declarator_obstack;
1000 /* Alloc BYTES from the declarator memory pool. */
1002 static inline void *
1003 alloc_declarator (size_t bytes)
1005 return obstack_alloc (&declarator_obstack, bytes);
1008 /* Allocate a declarator of the indicated KIND. Clear fields that are
1009 common to all declarators. */
1011 static cp_declarator *
1012 make_declarator (cp_declarator_kind kind)
1014 cp_declarator *declarator;
1016 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1017 declarator->kind = kind;
1018 declarator->attributes = NULL_TREE;
1019 declarator->declarator = NULL;
1020 declarator->parameter_pack_p = false;
1021 declarator->id_loc = UNKNOWN_LOCATION;
1023 return declarator;
1026 /* Make a declarator for a generalized identifier. If
1027 QUALIFYING_SCOPE is non-NULL, the identifier is
1028 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1030 is, if any. */
1032 static cp_declarator *
1033 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1034 special_function_kind sfk)
1036 cp_declarator *declarator;
1038 /* It is valid to write:
1040 class C { void f(); };
1041 typedef C D;
1042 void D::f();
1044 The standard is not clear about whether `typedef const C D' is
1045 legal; as of 2002-09-15 the committee is considering that
1046 question. EDG 3.0 allows that syntax. Therefore, we do as
1047 well. */
1048 if (qualifying_scope && TYPE_P (qualifying_scope))
1049 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1051 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1052 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1053 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1055 declarator = make_declarator (cdk_id);
1056 declarator->u.id.qualifying_scope = qualifying_scope;
1057 declarator->u.id.unqualified_name = unqualified_name;
1058 declarator->u.id.sfk = sfk;
1060 return declarator;
1063 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1064 of modifiers such as const or volatile to apply to the pointer
1065 type, represented as identifiers. */
1067 cp_declarator *
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1070 cp_declarator *declarator;
1072 declarator = make_declarator (cdk_pointer);
1073 declarator->declarator = target;
1074 declarator->u.pointer.qualifiers = cv_qualifiers;
1075 declarator->u.pointer.class_type = NULL_TREE;
1076 if (target)
1078 declarator->id_loc = target->id_loc;
1079 declarator->parameter_pack_p = target->parameter_pack_p;
1080 target->parameter_pack_p = false;
1082 else
1083 declarator->parameter_pack_p = false;
1085 return declarator;
1088 /* Like make_pointer_declarator -- but for references. */
1090 cp_declarator *
1091 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1092 bool rvalue_ref)
1094 cp_declarator *declarator;
1096 declarator = make_declarator (cdk_reference);
1097 declarator->declarator = target;
1098 declarator->u.reference.qualifiers = cv_qualifiers;
1099 declarator->u.reference.rvalue_ref = rvalue_ref;
1100 if (target)
1102 declarator->id_loc = target->id_loc;
1103 declarator->parameter_pack_p = target->parameter_pack_p;
1104 target->parameter_pack_p = false;
1106 else
1107 declarator->parameter_pack_p = false;
1109 return declarator;
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113 member of CLASS_TYPE. */
1115 cp_declarator *
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1117 cp_declarator *pointee)
1119 cp_declarator *declarator;
1121 declarator = make_declarator (cdk_ptrmem);
1122 declarator->declarator = pointee;
1123 declarator->u.pointer.qualifiers = cv_qualifiers;
1124 declarator->u.pointer.class_type = class_type;
1126 if (pointee)
1128 declarator->parameter_pack_p = pointee->parameter_pack_p;
1129 pointee->parameter_pack_p = false;
1131 else
1132 declarator->parameter_pack_p = false;
1134 return declarator;
1137 /* Make a declarator for the function given by TARGET, with the
1138 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1139 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1140 indicates what exceptions can be thrown. */
1142 cp_declarator *
1143 make_call_declarator (cp_declarator *target,
1144 tree parms,
1145 cp_cv_quals cv_qualifiers,
1146 tree exception_specification,
1147 tree late_return_type)
1149 cp_declarator *declarator;
1151 declarator = make_declarator (cdk_function);
1152 declarator->declarator = target;
1153 declarator->u.function.parameters = parms;
1154 declarator->u.function.qualifiers = cv_qualifiers;
1155 declarator->u.function.exception_specification = exception_specification;
1156 declarator->u.function.late_return_type = late_return_type;
1157 if (target)
1159 declarator->id_loc = target->id_loc;
1160 declarator->parameter_pack_p = target->parameter_pack_p;
1161 target->parameter_pack_p = false;
1163 else
1164 declarator->parameter_pack_p = false;
1166 return declarator;
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170 defined by ELEMENT. */
1172 cp_declarator *
1173 make_array_declarator (cp_declarator *element, tree bounds)
1175 cp_declarator *declarator;
1177 declarator = make_declarator (cdk_array);
1178 declarator->declarator = element;
1179 declarator->u.array.bounds = bounds;
1180 if (element)
1182 declarator->id_loc = element->id_loc;
1183 declarator->parameter_pack_p = element->parameter_pack_p;
1184 element->parameter_pack_p = false;
1186 else
1187 declarator->parameter_pack_p = false;
1189 return declarator;
1192 /* Determine whether the declarator we've seen so far can be a
1193 parameter pack, when followed by an ellipsis. */
1194 static bool
1195 declarator_can_be_parameter_pack (cp_declarator *declarator)
1197 /* Search for a declarator name, or any other declarator that goes
1198 after the point where the ellipsis could appear in a parameter
1199 pack. If we find any of these, then this declarator can not be
1200 made into a parameter pack. */
1201 bool found = false;
1202 while (declarator && !found)
1204 switch ((int)declarator->kind)
1206 case cdk_id:
1207 case cdk_array:
1208 found = true;
1209 break;
1211 case cdk_error:
1212 return true;
1214 default:
1215 declarator = declarator->declarator;
1216 break;
1220 return !found;
1223 cp_parameter_declarator *no_parameters;
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226 DECLARATOR and DEFAULT_ARGUMENT. */
1228 cp_parameter_declarator *
1229 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1230 cp_declarator *declarator,
1231 tree default_argument)
1233 cp_parameter_declarator *parameter;
1235 parameter = ((cp_parameter_declarator *)
1236 alloc_declarator (sizeof (cp_parameter_declarator)));
1237 parameter->next = NULL;
1238 if (decl_specifiers)
1239 parameter->decl_specifiers = *decl_specifiers;
1240 else
1241 clear_decl_specs (&parameter->decl_specifiers);
1242 parameter->declarator = declarator;
1243 parameter->default_argument = default_argument;
1244 parameter->ellipsis_p = false;
1246 return parameter;
1249 /* Returns true iff DECLARATOR is a declaration for a function. */
1251 static bool
1252 function_declarator_p (const cp_declarator *declarator)
1254 while (declarator)
1256 if (declarator->kind == cdk_function
1257 && declarator->declarator->kind == cdk_id)
1258 return true;
1259 if (declarator->kind == cdk_id
1260 || declarator->kind == cdk_error)
1261 return false;
1262 declarator = declarator->declarator;
1264 return false;
1267 /* The parser. */
1269 /* Overview
1270 --------
1272 A cp_parser parses the token stream as specified by the C++
1273 grammar. Its job is purely parsing, not semantic analysis. For
1274 example, the parser breaks the token stream into declarators,
1275 expressions, statements, and other similar syntactic constructs.
1276 It does not check that the types of the expressions on either side
1277 of an assignment-statement are compatible, or that a function is
1278 not declared with a parameter of type `void'.
1280 The parser invokes routines elsewhere in the compiler to perform
1281 semantic analysis and to build up the abstract syntax tree for the
1282 code processed.
1284 The parser (and the template instantiation code, which is, in a
1285 way, a close relative of parsing) are the only parts of the
1286 compiler that should be calling push_scope and pop_scope, or
1287 related functions. The parser (and template instantiation code)
1288 keeps track of what scope is presently active; everything else
1289 should simply honor that. (The code that generates static
1290 initializers may also need to set the scope, in order to check
1291 access control correctly when emitting the initializers.)
1293 Methodology
1294 -----------
1296 The parser is of the standard recursive-descent variety. Upcoming
1297 tokens in the token stream are examined in order to determine which
1298 production to use when parsing a non-terminal. Some C++ constructs
1299 require arbitrary look ahead to disambiguate. For example, it is
1300 impossible, in the general case, to tell whether a statement is an
1301 expression or declaration without scanning the entire statement.
1302 Therefore, the parser is capable of "parsing tentatively." When the
1303 parser is not sure what construct comes next, it enters this mode.
1304 Then, while we attempt to parse the construct, the parser queues up
1305 error messages, rather than issuing them immediately, and saves the
1306 tokens it consumes. If the construct is parsed successfully, the
1307 parser "commits", i.e., it issues any queued error messages and
1308 the tokens that were being preserved are permanently discarded.
1309 If, however, the construct is not parsed successfully, the parser
1310 rolls back its state completely so that it can resume parsing using
1311 a different alternative.
1313 Future Improvements
1314 -------------------
1316 The performance of the parser could probably be improved substantially.
1317 We could often eliminate the need to parse tentatively by looking ahead
1318 a little bit. In some places, this approach might not entirely eliminate
1319 the need to parse tentatively, but it might still speed up the average
1320 case. */
1322 /* Flags that are passed to some parsing functions. These values can
1323 be bitwise-ored together. */
1325 enum
1327 /* No flags. */
1328 CP_PARSER_FLAGS_NONE = 0x0,
1329 /* The construct is optional. If it is not present, then no error
1330 should be issued. */
1331 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1332 /* When parsing a type-specifier, treat user-defined type-names
1333 as non-type identifiers. */
1334 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1335 /* When parsing a type-specifier, do not try to parse a class-specifier
1336 or enum-specifier. */
1337 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1338 /* When parsing a decl-specifier-seq, only allow type-specifier or
1339 constexpr. */
1340 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1343 /* This type is used for parameters and variables which hold
1344 combinations of the above flags. */
1345 typedef int cp_parser_flags;
1347 /* The different kinds of declarators we want to parse. */
1349 typedef enum cp_parser_declarator_kind
1351 /* We want an abstract declarator. */
1352 CP_PARSER_DECLARATOR_ABSTRACT,
1353 /* We want a named declarator. */
1354 CP_PARSER_DECLARATOR_NAMED,
1355 /* We don't mind, but the name must be an unqualified-id. */
1356 CP_PARSER_DECLARATOR_EITHER
1357 } cp_parser_declarator_kind;
1359 /* The precedence values used to parse binary expressions. The minimum value
1360 of PREC must be 1, because zero is reserved to quickly discriminate
1361 binary operators from other tokens. */
1363 enum cp_parser_prec
1365 PREC_NOT_OPERATOR,
1366 PREC_LOGICAL_OR_EXPRESSION,
1367 PREC_LOGICAL_AND_EXPRESSION,
1368 PREC_INCLUSIVE_OR_EXPRESSION,
1369 PREC_EXCLUSIVE_OR_EXPRESSION,
1370 PREC_AND_EXPRESSION,
1371 PREC_EQUALITY_EXPRESSION,
1372 PREC_RELATIONAL_EXPRESSION,
1373 PREC_SHIFT_EXPRESSION,
1374 PREC_ADDITIVE_EXPRESSION,
1375 PREC_MULTIPLICATIVE_EXPRESSION,
1376 PREC_PM_EXPRESSION,
1377 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1380 /* A mapping from a token type to a corresponding tree node type, with a
1381 precedence value. */
1383 typedef struct cp_parser_binary_operations_map_node
1385 /* The token type. */
1386 enum cpp_ttype token_type;
1387 /* The corresponding tree code. */
1388 enum tree_code tree_type;
1389 /* The precedence of this operator. */
1390 enum cp_parser_prec prec;
1391 } cp_parser_binary_operations_map_node;
1393 /* The status of a tentative parse. */
1395 typedef enum cp_parser_status_kind
1397 /* No errors have occurred. */
1398 CP_PARSER_STATUS_KIND_NO_ERROR,
1399 /* An error has occurred. */
1400 CP_PARSER_STATUS_KIND_ERROR,
1401 /* We are committed to this tentative parse, whether or not an error
1402 has occurred. */
1403 CP_PARSER_STATUS_KIND_COMMITTED
1404 } cp_parser_status_kind;
1406 typedef struct cp_parser_expression_stack_entry
1408 /* Left hand side of the binary operation we are currently
1409 parsing. */
1410 tree lhs;
1411 /* Original tree code for left hand side, if it was a binary
1412 expression itself (used for -Wparentheses). */
1413 enum tree_code lhs_type;
1414 /* Tree code for the binary operation we are parsing. */
1415 enum tree_code tree_type;
1416 /* Precedence of the binary operation we are parsing. */
1417 enum cp_parser_prec prec;
1418 } cp_parser_expression_stack_entry;
1420 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1421 entries because precedence levels on the stack are monotonically
1422 increasing. */
1423 typedef struct cp_parser_expression_stack_entry
1424 cp_parser_expression_stack[NUM_PREC_VALUES];
1426 /* Context that is saved and restored when parsing tentatively. */
1427 typedef struct GTY (()) cp_parser_context {
1428 /* If this is a tentative parsing context, the status of the
1429 tentative parse. */
1430 enum cp_parser_status_kind status;
1431 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1432 that are looked up in this context must be looked up both in the
1433 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1434 the context of the containing expression. */
1435 tree object_type;
1437 /* The next parsing context in the stack. */
1438 struct cp_parser_context *next;
1439 } cp_parser_context;
1441 /* Prototypes. */
1443 /* Constructors and destructors. */
1445 static cp_parser_context *cp_parser_context_new
1446 (cp_parser_context *);
1448 /* Class variables. */
1450 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1452 /* The operator-precedence table used by cp_parser_binary_expression.
1453 Transformed into an associative array (binops_by_token) by
1454 cp_parser_new. */
1456 static const cp_parser_binary_operations_map_node binops[] = {
1457 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1458 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1460 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1461 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1462 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1464 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1465 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1467 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1468 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1470 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1471 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1472 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1473 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1475 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1476 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1478 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1480 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1482 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1484 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1486 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1489 /* The same as binops, but initialized by cp_parser_new so that
1490 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1491 for speed. */
1492 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1494 /* Constructors and destructors. */
1496 /* Construct a new context. The context below this one on the stack
1497 is given by NEXT. */
1499 static cp_parser_context *
1500 cp_parser_context_new (cp_parser_context* next)
1502 cp_parser_context *context;
1504 /* Allocate the storage. */
1505 if (cp_parser_context_free_list != NULL)
1507 /* Pull the first entry from the free list. */
1508 context = cp_parser_context_free_list;
1509 cp_parser_context_free_list = context->next;
1510 memset (context, 0, sizeof (*context));
1512 else
1513 context = ggc_alloc_cleared_cp_parser_context ();
1515 /* No errors have occurred yet in this context. */
1516 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1517 /* If this is not the bottommost context, copy information that we
1518 need from the previous context. */
1519 if (next)
1521 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1522 expression, then we are parsing one in this context, too. */
1523 context->object_type = next->object_type;
1524 /* Thread the stack. */
1525 context->next = next;
1528 return context;
1531 /* An entry in a queue of function arguments that require post-processing. */
1533 typedef struct GTY(()) cp_default_arg_entry_d {
1534 /* The current_class_type when we parsed this arg. */
1535 tree class_type;
1537 /* The function decl itself. */
1538 tree decl;
1539 } cp_default_arg_entry;
1541 DEF_VEC_O(cp_default_arg_entry);
1542 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1544 /* An entry in a stack for member functions of local classes. */
1546 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1547 /* Functions with default arguments that require post-processing.
1548 Functions appear in this list in declaration order. */
1549 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1551 /* Functions with defintions that require post-processing. Functions
1552 appear in this list in declaration order. */
1553 VEC(tree,gc) *funs_with_definitions;
1554 } cp_unparsed_functions_entry;
1556 DEF_VEC_O(cp_unparsed_functions_entry);
1557 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1559 /* The cp_parser structure represents the C++ parser. */
1561 typedef struct GTY(()) cp_parser {
1562 /* The lexer from which we are obtaining tokens. */
1563 cp_lexer *lexer;
1565 /* The scope in which names should be looked up. If NULL_TREE, then
1566 we look up names in the scope that is currently open in the
1567 source program. If non-NULL, this is either a TYPE or
1568 NAMESPACE_DECL for the scope in which we should look. It can
1569 also be ERROR_MARK, when we've parsed a bogus scope.
1571 This value is not cleared automatically after a name is looked
1572 up, so we must be careful to clear it before starting a new look
1573 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1574 will look up `Z' in the scope of `X', rather than the current
1575 scope.) Unfortunately, it is difficult to tell when name lookup
1576 is complete, because we sometimes peek at a token, look it up,
1577 and then decide not to consume it. */
1578 tree scope;
1580 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1581 last lookup took place. OBJECT_SCOPE is used if an expression
1582 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1583 respectively. QUALIFYING_SCOPE is used for an expression of the
1584 form "X::Y"; it refers to X. */
1585 tree object_scope;
1586 tree qualifying_scope;
1588 /* A stack of parsing contexts. All but the bottom entry on the
1589 stack will be tentative contexts.
1591 We parse tentatively in order to determine which construct is in
1592 use in some situations. For example, in order to determine
1593 whether a statement is an expression-statement or a
1594 declaration-statement we parse it tentatively as a
1595 declaration-statement. If that fails, we then reparse the same
1596 token stream as an expression-statement. */
1597 cp_parser_context *context;
1599 /* True if we are parsing GNU C++. If this flag is not set, then
1600 GNU extensions are not recognized. */
1601 bool allow_gnu_extensions_p;
1603 /* TRUE if the `>' token should be interpreted as the greater-than
1604 operator. FALSE if it is the end of a template-id or
1605 template-parameter-list. In C++0x mode, this flag also applies to
1606 `>>' tokens, which are viewed as two consecutive `>' tokens when
1607 this flag is FALSE. */
1608 bool greater_than_is_operator_p;
1610 /* TRUE if default arguments are allowed within a parameter list
1611 that starts at this point. FALSE if only a gnu extension makes
1612 them permissible. */
1613 bool default_arg_ok_p;
1615 /* TRUE if we are parsing an integral constant-expression. See
1616 [expr.const] for a precise definition. */
1617 bool integral_constant_expression_p;
1619 /* TRUE if we are parsing an integral constant-expression -- but a
1620 non-constant expression should be permitted as well. This flag
1621 is used when parsing an array bound so that GNU variable-length
1622 arrays are tolerated. */
1623 bool allow_non_integral_constant_expression_p;
1625 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1626 been seen that makes the expression non-constant. */
1627 bool non_integral_constant_expression_p;
1629 /* TRUE if local variable names and `this' are forbidden in the
1630 current context. */
1631 bool local_variables_forbidden_p;
1633 /* TRUE if the declaration we are parsing is part of a
1634 linkage-specification of the form `extern string-literal
1635 declaration'. */
1636 bool in_unbraced_linkage_specification_p;
1638 /* TRUE if we are presently parsing a declarator, after the
1639 direct-declarator. */
1640 bool in_declarator_p;
1642 /* TRUE if we are presently parsing a template-argument-list. */
1643 bool in_template_argument_list_p;
1645 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1646 to IN_OMP_BLOCK if parsing OpenMP structured block and
1647 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1648 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1649 iteration-statement, OpenMP block or loop within that switch. */
1650 #define IN_SWITCH_STMT 1
1651 #define IN_ITERATION_STMT 2
1652 #define IN_OMP_BLOCK 4
1653 #define IN_OMP_FOR 8
1654 #define IN_IF_STMT 16
1655 unsigned char in_statement;
1657 /* TRUE if we are presently parsing the body of a switch statement.
1658 Note that this doesn't quite overlap with in_statement above.
1659 The difference relates to giving the right sets of error messages:
1660 "case not in switch" vs "break statement used with OpenMP...". */
1661 bool in_switch_statement_p;
1663 /* TRUE if we are parsing a type-id in an expression context. In
1664 such a situation, both "type (expr)" and "type (type)" are valid
1665 alternatives. */
1666 bool in_type_id_in_expr_p;
1668 /* TRUE if we are currently in a header file where declarations are
1669 implicitly extern "C". */
1670 bool implicit_extern_c;
1672 /* TRUE if strings in expressions should be translated to the execution
1673 character set. */
1674 bool translate_strings_p;
1676 /* TRUE if we are presently parsing the body of a function, but not
1677 a local class. */
1678 bool in_function_body;
1680 /* If non-NULL, then we are parsing a construct where new type
1681 definitions are not permitted. The string stored here will be
1682 issued as an error message if a type is defined. */
1683 const char *type_definition_forbidden_message;
1685 /* A stack used for member functions of local classes. The lists
1686 contained in an individual entry can only be processed once the
1687 outermost class being defined is complete. */
1688 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1690 /* The number of classes whose definitions are currently in
1691 progress. */
1692 unsigned num_classes_being_defined;
1694 /* The number of template parameter lists that apply directly to the
1695 current declaration. */
1696 unsigned num_template_parameter_lists;
1697 } cp_parser;
1699 /* Managing the unparsed function queues. */
1701 #define unparsed_funs_with_default_args \
1702 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1703 #define unparsed_funs_with_definitions \
1704 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1706 static void
1707 push_unparsed_function_queues (cp_parser *parser)
1709 VEC_safe_push (cp_unparsed_functions_entry, gc,
1710 parser->unparsed_queues, NULL);
1711 unparsed_funs_with_default_args = NULL;
1712 unparsed_funs_with_definitions = make_tree_vector ();
1715 static void
1716 pop_unparsed_function_queues (cp_parser *parser)
1718 release_tree_vector (unparsed_funs_with_definitions);
1719 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1722 /* Prototypes. */
1724 /* Constructors and destructors. */
1726 static cp_parser *cp_parser_new
1727 (void);
1729 /* Routines to parse various constructs.
1731 Those that return `tree' will return the error_mark_node (rather
1732 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1733 Sometimes, they will return an ordinary node if error-recovery was
1734 attempted, even though a parse error occurred. So, to check
1735 whether or not a parse error occurred, you should always use
1736 cp_parser_error_occurred. If the construct is optional (indicated
1737 either by an `_opt' in the name of the function that does the
1738 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1739 the construct is not present. */
1741 /* Lexical conventions [gram.lex] */
1743 static tree cp_parser_identifier
1744 (cp_parser *);
1745 static tree cp_parser_string_literal
1746 (cp_parser *, bool, bool);
1748 /* Basic concepts [gram.basic] */
1750 static bool cp_parser_translation_unit
1751 (cp_parser *);
1753 /* Expressions [gram.expr] */
1755 static tree cp_parser_primary_expression
1756 (cp_parser *, bool, bool, bool, cp_id_kind *);
1757 static tree cp_parser_id_expression
1758 (cp_parser *, bool, bool, bool *, bool, bool);
1759 static tree cp_parser_unqualified_id
1760 (cp_parser *, bool, bool, bool, bool);
1761 static tree cp_parser_nested_name_specifier_opt
1762 (cp_parser *, bool, bool, bool, bool);
1763 static tree cp_parser_nested_name_specifier
1764 (cp_parser *, bool, bool, bool, bool);
1765 static tree cp_parser_qualifying_entity
1766 (cp_parser *, bool, bool, bool, bool, bool);
1767 static tree cp_parser_postfix_expression
1768 (cp_parser *, bool, bool, bool, cp_id_kind *);
1769 static tree cp_parser_postfix_open_square_expression
1770 (cp_parser *, tree, bool);
1771 static tree cp_parser_postfix_dot_deref_expression
1772 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1773 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1774 (cp_parser *, int, bool, bool, bool *);
1775 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1776 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1777 static void cp_parser_pseudo_destructor_name
1778 (cp_parser *, tree *, tree *);
1779 static tree cp_parser_unary_expression
1780 (cp_parser *, bool, bool, cp_id_kind *);
1781 static enum tree_code cp_parser_unary_operator
1782 (cp_token *);
1783 static tree cp_parser_new_expression
1784 (cp_parser *);
1785 static VEC(tree,gc) *cp_parser_new_placement
1786 (cp_parser *);
1787 static tree cp_parser_new_type_id
1788 (cp_parser *, tree *);
1789 static cp_declarator *cp_parser_new_declarator_opt
1790 (cp_parser *);
1791 static cp_declarator *cp_parser_direct_new_declarator
1792 (cp_parser *);
1793 static VEC(tree,gc) *cp_parser_new_initializer
1794 (cp_parser *);
1795 static tree cp_parser_delete_expression
1796 (cp_parser *);
1797 static tree cp_parser_cast_expression
1798 (cp_parser *, bool, bool, cp_id_kind *);
1799 static tree cp_parser_binary_expression
1800 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1801 static tree cp_parser_question_colon_clause
1802 (cp_parser *, tree);
1803 static tree cp_parser_assignment_expression
1804 (cp_parser *, bool, cp_id_kind *);
1805 static enum tree_code cp_parser_assignment_operator_opt
1806 (cp_parser *);
1807 static tree cp_parser_expression
1808 (cp_parser *, bool, cp_id_kind *);
1809 static tree cp_parser_constant_expression
1810 (cp_parser *, bool, bool *);
1811 static tree cp_parser_builtin_offsetof
1812 (cp_parser *);
1813 static tree cp_parser_lambda_expression
1814 (cp_parser *);
1815 static void cp_parser_lambda_introducer
1816 (cp_parser *, tree);
1817 static void cp_parser_lambda_declarator_opt
1818 (cp_parser *, tree);
1819 static void cp_parser_lambda_body
1820 (cp_parser *, tree);
1822 /* Statements [gram.stmt.stmt] */
1824 static void cp_parser_statement
1825 (cp_parser *, tree, bool, bool *);
1826 static void cp_parser_label_for_labeled_statement
1827 (cp_parser *);
1828 static tree cp_parser_expression_statement
1829 (cp_parser *, tree);
1830 static tree cp_parser_compound_statement
1831 (cp_parser *, tree, bool);
1832 static void cp_parser_statement_seq_opt
1833 (cp_parser *, tree);
1834 static tree cp_parser_selection_statement
1835 (cp_parser *, bool *);
1836 static tree cp_parser_condition
1837 (cp_parser *);
1838 static tree cp_parser_iteration_statement
1839 (cp_parser *);
1840 static void cp_parser_for_init_statement
1841 (cp_parser *);
1842 static tree cp_parser_c_for
1843 (cp_parser *);
1844 static tree cp_parser_range_for
1845 (cp_parser *);
1846 static tree cp_parser_jump_statement
1847 (cp_parser *);
1848 static void cp_parser_declaration_statement
1849 (cp_parser *);
1851 static tree cp_parser_implicitly_scoped_statement
1852 (cp_parser *, bool *);
1853 static void cp_parser_already_scoped_statement
1854 (cp_parser *);
1856 /* Declarations [gram.dcl.dcl] */
1858 static void cp_parser_declaration_seq_opt
1859 (cp_parser *);
1860 static void cp_parser_declaration
1861 (cp_parser *);
1862 static void cp_parser_block_declaration
1863 (cp_parser *, bool);
1864 static void cp_parser_simple_declaration
1865 (cp_parser *, bool);
1866 static void cp_parser_decl_specifier_seq
1867 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1868 static tree cp_parser_storage_class_specifier_opt
1869 (cp_parser *);
1870 static tree cp_parser_function_specifier_opt
1871 (cp_parser *, cp_decl_specifier_seq *);
1872 static tree cp_parser_type_specifier
1873 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1874 int *, bool *);
1875 static tree cp_parser_simple_type_specifier
1876 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1877 static tree cp_parser_type_name
1878 (cp_parser *);
1879 static tree cp_parser_nonclass_name
1880 (cp_parser* parser);
1881 static tree cp_parser_elaborated_type_specifier
1882 (cp_parser *, bool, bool);
1883 static tree cp_parser_enum_specifier
1884 (cp_parser *);
1885 static void cp_parser_enumerator_list
1886 (cp_parser *, tree);
1887 static void cp_parser_enumerator_definition
1888 (cp_parser *, tree);
1889 static tree cp_parser_namespace_name
1890 (cp_parser *);
1891 static void cp_parser_namespace_definition
1892 (cp_parser *);
1893 static void cp_parser_namespace_body
1894 (cp_parser *);
1895 static tree cp_parser_qualified_namespace_specifier
1896 (cp_parser *);
1897 static void cp_parser_namespace_alias_definition
1898 (cp_parser *);
1899 static bool cp_parser_using_declaration
1900 (cp_parser *, bool);
1901 static void cp_parser_using_directive
1902 (cp_parser *);
1903 static void cp_parser_asm_definition
1904 (cp_parser *);
1905 static void cp_parser_linkage_specification
1906 (cp_parser *);
1907 static void cp_parser_static_assert
1908 (cp_parser *, bool);
1909 static tree cp_parser_decltype
1910 (cp_parser *);
1912 /* Declarators [gram.dcl.decl] */
1914 static tree cp_parser_init_declarator
1915 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1916 static cp_declarator *cp_parser_declarator
1917 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1918 static cp_declarator *cp_parser_direct_declarator
1919 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1920 static enum tree_code cp_parser_ptr_operator
1921 (cp_parser *, tree *, cp_cv_quals *);
1922 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1923 (cp_parser *);
1924 static tree cp_parser_late_return_type_opt
1925 (cp_parser *);
1926 static tree cp_parser_declarator_id
1927 (cp_parser *, bool);
1928 static tree cp_parser_type_id
1929 (cp_parser *);
1930 static tree cp_parser_template_type_arg
1931 (cp_parser *);
1932 static tree cp_parser_trailing_type_id (cp_parser *);
1933 static tree cp_parser_type_id_1
1934 (cp_parser *, bool, bool);
1935 static void cp_parser_type_specifier_seq
1936 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1937 static tree cp_parser_parameter_declaration_clause
1938 (cp_parser *);
1939 static tree cp_parser_parameter_declaration_list
1940 (cp_parser *, bool *);
1941 static cp_parameter_declarator *cp_parser_parameter_declaration
1942 (cp_parser *, bool, bool *);
1943 static tree cp_parser_default_argument
1944 (cp_parser *, bool);
1945 static void cp_parser_function_body
1946 (cp_parser *);
1947 static tree cp_parser_initializer
1948 (cp_parser *, bool *, bool *);
1949 static tree cp_parser_initializer_clause
1950 (cp_parser *, bool *);
1951 static tree cp_parser_braced_list
1952 (cp_parser*, bool*);
1953 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1954 (cp_parser *, bool *);
1956 static bool cp_parser_ctor_initializer_opt_and_function_body
1957 (cp_parser *);
1959 /* Classes [gram.class] */
1961 static tree cp_parser_class_name
1962 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1963 static tree cp_parser_class_specifier
1964 (cp_parser *);
1965 static tree cp_parser_class_head
1966 (cp_parser *, bool *, tree *, tree *);
1967 static enum tag_types cp_parser_class_key
1968 (cp_parser *);
1969 static void cp_parser_member_specification_opt
1970 (cp_parser *);
1971 static void cp_parser_member_declaration
1972 (cp_parser *);
1973 static tree cp_parser_pure_specifier
1974 (cp_parser *);
1975 static tree cp_parser_constant_initializer
1976 (cp_parser *);
1978 /* Derived classes [gram.class.derived] */
1980 static tree cp_parser_base_clause
1981 (cp_parser *);
1982 static tree cp_parser_base_specifier
1983 (cp_parser *);
1985 /* Special member functions [gram.special] */
1987 static tree cp_parser_conversion_function_id
1988 (cp_parser *);
1989 static tree cp_parser_conversion_type_id
1990 (cp_parser *);
1991 static cp_declarator *cp_parser_conversion_declarator_opt
1992 (cp_parser *);
1993 static bool cp_parser_ctor_initializer_opt
1994 (cp_parser *);
1995 static void cp_parser_mem_initializer_list
1996 (cp_parser *);
1997 static tree cp_parser_mem_initializer
1998 (cp_parser *);
1999 static tree cp_parser_mem_initializer_id
2000 (cp_parser *);
2002 /* Overloading [gram.over] */
2004 static tree cp_parser_operator_function_id
2005 (cp_parser *);
2006 static tree cp_parser_operator
2007 (cp_parser *);
2009 /* Templates [gram.temp] */
2011 static void cp_parser_template_declaration
2012 (cp_parser *, bool);
2013 static tree cp_parser_template_parameter_list
2014 (cp_parser *);
2015 static tree cp_parser_template_parameter
2016 (cp_parser *, bool *, bool *);
2017 static tree cp_parser_type_parameter
2018 (cp_parser *, bool *);
2019 static tree cp_parser_template_id
2020 (cp_parser *, bool, bool, bool);
2021 static tree cp_parser_template_name
2022 (cp_parser *, bool, bool, bool, bool *);
2023 static tree cp_parser_template_argument_list
2024 (cp_parser *);
2025 static tree cp_parser_template_argument
2026 (cp_parser *);
2027 static void cp_parser_explicit_instantiation
2028 (cp_parser *);
2029 static void cp_parser_explicit_specialization
2030 (cp_parser *);
2032 /* Exception handling [gram.exception] */
2034 static tree cp_parser_try_block
2035 (cp_parser *);
2036 static bool cp_parser_function_try_block
2037 (cp_parser *);
2038 static void cp_parser_handler_seq
2039 (cp_parser *);
2040 static void cp_parser_handler
2041 (cp_parser *);
2042 static tree cp_parser_exception_declaration
2043 (cp_parser *);
2044 static tree cp_parser_throw_expression
2045 (cp_parser *);
2046 static tree cp_parser_exception_specification_opt
2047 (cp_parser *);
2048 static tree cp_parser_type_id_list
2049 (cp_parser *);
2051 /* GNU Extensions */
2053 static tree cp_parser_asm_specification_opt
2054 (cp_parser *);
2055 static tree cp_parser_asm_operand_list
2056 (cp_parser *);
2057 static tree cp_parser_asm_clobber_list
2058 (cp_parser *);
2059 static tree cp_parser_asm_label_list
2060 (cp_parser *);
2061 static tree cp_parser_attributes_opt
2062 (cp_parser *);
2063 static tree cp_parser_attribute_list
2064 (cp_parser *);
2065 static bool cp_parser_extension_opt
2066 (cp_parser *, int *);
2067 static void cp_parser_label_declaration
2068 (cp_parser *);
2070 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2071 static bool cp_parser_pragma
2072 (cp_parser *, enum pragma_context);
2074 /* Objective-C++ Productions */
2076 static tree cp_parser_objc_message_receiver
2077 (cp_parser *);
2078 static tree cp_parser_objc_message_args
2079 (cp_parser *);
2080 static tree cp_parser_objc_message_expression
2081 (cp_parser *);
2082 static tree cp_parser_objc_encode_expression
2083 (cp_parser *);
2084 static tree cp_parser_objc_defs_expression
2085 (cp_parser *);
2086 static tree cp_parser_objc_protocol_expression
2087 (cp_parser *);
2088 static tree cp_parser_objc_selector_expression
2089 (cp_parser *);
2090 static tree cp_parser_objc_expression
2091 (cp_parser *);
2092 static bool cp_parser_objc_selector_p
2093 (enum cpp_ttype);
2094 static tree cp_parser_objc_selector
2095 (cp_parser *);
2096 static tree cp_parser_objc_protocol_refs_opt
2097 (cp_parser *);
2098 static void cp_parser_objc_declaration
2099 (cp_parser *, tree);
2100 static tree cp_parser_objc_statement
2101 (cp_parser *);
2102 static bool cp_parser_objc_valid_prefix_attributes
2103 (cp_parser *, tree *);
2104 static void cp_parser_objc_at_property_declaration
2105 (cp_parser *) ;
2106 static void cp_parser_objc_at_synthesize_declaration
2107 (cp_parser *) ;
2108 static void cp_parser_objc_at_dynamic_declaration
2109 (cp_parser *) ;
2110 static tree cp_parser_objc_struct_declaration
2111 (cp_parser *) ;
2113 /* Utility Routines */
2115 static tree cp_parser_lookup_name
2116 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2117 static tree cp_parser_lookup_name_simple
2118 (cp_parser *, tree, location_t);
2119 static tree cp_parser_maybe_treat_template_as_class
2120 (tree, bool);
2121 static bool cp_parser_check_declarator_template_parameters
2122 (cp_parser *, cp_declarator *, location_t);
2123 static bool cp_parser_check_template_parameters
2124 (cp_parser *, unsigned, location_t, cp_declarator *);
2125 static tree cp_parser_simple_cast_expression
2126 (cp_parser *);
2127 static tree cp_parser_global_scope_opt
2128 (cp_parser *, bool);
2129 static bool cp_parser_constructor_declarator_p
2130 (cp_parser *, bool);
2131 static tree cp_parser_function_definition_from_specifiers_and_declarator
2132 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2133 static tree cp_parser_function_definition_after_declarator
2134 (cp_parser *, bool);
2135 static void cp_parser_template_declaration_after_export
2136 (cp_parser *, bool);
2137 static void cp_parser_perform_template_parameter_access_checks
2138 (VEC (deferred_access_check,gc)*);
2139 static tree cp_parser_single_declaration
2140 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2141 static tree cp_parser_functional_cast
2142 (cp_parser *, tree);
2143 static tree cp_parser_save_member_function_body
2144 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2145 static tree cp_parser_enclosed_template_argument_list
2146 (cp_parser *);
2147 static void cp_parser_save_default_args
2148 (cp_parser *, tree);
2149 static void cp_parser_late_parsing_for_member
2150 (cp_parser *, tree);
2151 static void cp_parser_late_parsing_default_args
2152 (cp_parser *, tree);
2153 static tree cp_parser_sizeof_operand
2154 (cp_parser *, enum rid);
2155 static tree cp_parser_trait_expr
2156 (cp_parser *, enum rid);
2157 static bool cp_parser_declares_only_class_p
2158 (cp_parser *);
2159 static void cp_parser_set_storage_class
2160 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2161 static void cp_parser_set_decl_spec_type
2162 (cp_decl_specifier_seq *, tree, location_t, bool);
2163 static bool cp_parser_friend_p
2164 (const cp_decl_specifier_seq *);
2165 static void cp_parser_required_error
2166 (cp_parser *, required_token, bool);
2167 static cp_token *cp_parser_require
2168 (cp_parser *, enum cpp_ttype, required_token);
2169 static cp_token *cp_parser_require_keyword
2170 (cp_parser *, enum rid, required_token);
2171 static bool cp_parser_token_starts_function_definition_p
2172 (cp_token *);
2173 static bool cp_parser_next_token_starts_class_definition_p
2174 (cp_parser *);
2175 static bool cp_parser_next_token_ends_template_argument_p
2176 (cp_parser *);
2177 static bool cp_parser_nth_token_starts_template_argument_list_p
2178 (cp_parser *, size_t);
2179 static enum tag_types cp_parser_token_is_class_key
2180 (cp_token *);
2181 static void cp_parser_check_class_key
2182 (enum tag_types, tree type);
2183 static void cp_parser_check_access_in_redeclaration
2184 (tree type, location_t location);
2185 static bool cp_parser_optional_template_keyword
2186 (cp_parser *);
2187 static void cp_parser_pre_parsed_nested_name_specifier
2188 (cp_parser *);
2189 static bool cp_parser_cache_group
2190 (cp_parser *, enum cpp_ttype, unsigned);
2191 static void cp_parser_parse_tentatively
2192 (cp_parser *);
2193 static void cp_parser_commit_to_tentative_parse
2194 (cp_parser *);
2195 static void cp_parser_abort_tentative_parse
2196 (cp_parser *);
2197 static bool cp_parser_parse_definitely
2198 (cp_parser *);
2199 static inline bool cp_parser_parsing_tentatively
2200 (cp_parser *);
2201 static bool cp_parser_uncommitted_to_tentative_parse_p
2202 (cp_parser *);
2203 static void cp_parser_error
2204 (cp_parser *, const char *);
2205 static void cp_parser_name_lookup_error
2206 (cp_parser *, tree, tree, name_lookup_error, location_t);
2207 static bool cp_parser_simulate_error
2208 (cp_parser *);
2209 static bool cp_parser_check_type_definition
2210 (cp_parser *);
2211 static void cp_parser_check_for_definition_in_return_type
2212 (cp_declarator *, tree, location_t type_location);
2213 static void cp_parser_check_for_invalid_template_id
2214 (cp_parser *, tree, location_t location);
2215 static bool cp_parser_non_integral_constant_expression
2216 (cp_parser *, non_integral_constant);
2217 static void cp_parser_diagnose_invalid_type_name
2218 (cp_parser *, tree, tree, location_t);
2219 static bool cp_parser_parse_and_diagnose_invalid_type_name
2220 (cp_parser *);
2221 static int cp_parser_skip_to_closing_parenthesis
2222 (cp_parser *, bool, bool, bool);
2223 static void cp_parser_skip_to_end_of_statement
2224 (cp_parser *);
2225 static void cp_parser_consume_semicolon_at_end_of_statement
2226 (cp_parser *);
2227 static void cp_parser_skip_to_end_of_block_or_statement
2228 (cp_parser *);
2229 static bool cp_parser_skip_to_closing_brace
2230 (cp_parser *);
2231 static void cp_parser_skip_to_end_of_template_parameter_list
2232 (cp_parser *);
2233 static void cp_parser_skip_to_pragma_eol
2234 (cp_parser*, cp_token *);
2235 static bool cp_parser_error_occurred
2236 (cp_parser *);
2237 static bool cp_parser_allow_gnu_extensions_p
2238 (cp_parser *);
2239 static bool cp_parser_is_string_literal
2240 (cp_token *);
2241 static bool cp_parser_is_keyword
2242 (cp_token *, enum rid);
2243 static tree cp_parser_make_typename_type
2244 (cp_parser *, tree, tree, location_t location);
2245 static cp_declarator * cp_parser_make_indirect_declarator
2246 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2248 /* Returns nonzero if we are parsing tentatively. */
2250 static inline bool
2251 cp_parser_parsing_tentatively (cp_parser* parser)
2253 return parser->context->next != NULL;
2256 /* Returns nonzero if TOKEN is a string literal. */
2258 static bool
2259 cp_parser_is_string_literal (cp_token* token)
2261 return (token->type == CPP_STRING ||
2262 token->type == CPP_STRING16 ||
2263 token->type == CPP_STRING32 ||
2264 token->type == CPP_WSTRING ||
2265 token->type == CPP_UTF8STRING);
2268 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2270 static bool
2271 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2273 return token->keyword == keyword;
2276 /* If not parsing tentatively, issue a diagnostic of the form
2277 FILE:LINE: MESSAGE before TOKEN
2278 where TOKEN is the next token in the input stream. MESSAGE
2279 (specified by the caller) is usually of the form "expected
2280 OTHER-TOKEN". */
2282 static void
2283 cp_parser_error (cp_parser* parser, const char* gmsgid)
2285 if (!cp_parser_simulate_error (parser))
2287 cp_token *token = cp_lexer_peek_token (parser->lexer);
2288 /* This diagnostic makes more sense if it is tagged to the line
2289 of the token we just peeked at. */
2290 cp_lexer_set_source_position_from_token (token);
2292 if (token->type == CPP_PRAGMA)
2294 error_at (token->location,
2295 "%<#pragma%> is not allowed here");
2296 cp_parser_skip_to_pragma_eol (parser, token);
2297 return;
2300 c_parse_error (gmsgid,
2301 /* Because c_parser_error does not understand
2302 CPP_KEYWORD, keywords are treated like
2303 identifiers. */
2304 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2305 token->u.value, token->flags);
2309 /* Issue an error about name-lookup failing. NAME is the
2310 IDENTIFIER_NODE DECL is the result of
2311 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2312 the thing that we hoped to find. */
2314 static void
2315 cp_parser_name_lookup_error (cp_parser* parser,
2316 tree name,
2317 tree decl,
2318 name_lookup_error desired,
2319 location_t location)
2321 /* If name lookup completely failed, tell the user that NAME was not
2322 declared. */
2323 if (decl == error_mark_node)
2325 if (parser->scope && parser->scope != global_namespace)
2326 error_at (location, "%<%E::%E%> has not been declared",
2327 parser->scope, name);
2328 else if (parser->scope == global_namespace)
2329 error_at (location, "%<::%E%> has not been declared", name);
2330 else if (parser->object_scope
2331 && !CLASS_TYPE_P (parser->object_scope))
2332 error_at (location, "request for member %qE in non-class type %qT",
2333 name, parser->object_scope);
2334 else if (parser->object_scope)
2335 error_at (location, "%<%T::%E%> has not been declared",
2336 parser->object_scope, name);
2337 else
2338 error_at (location, "%qE has not been declared", name);
2340 else if (parser->scope && parser->scope != global_namespace)
2342 switch (desired)
2344 case NLE_TYPE:
2345 error_at (location, "%<%E::%E%> is not a type",
2346 parser->scope, name);
2347 break;
2348 case NLE_CXX98:
2349 error_at (location, "%<%E::%E%> is not a class or namespace",
2350 parser->scope, name);
2351 break;
2352 case NLE_NOT_CXX98:
2353 error_at (location,
2354 "%<%E::%E%> is not a class, namespace, or enumeration",
2355 parser->scope, name);
2356 break;
2357 default:
2358 gcc_unreachable ();
2362 else if (parser->scope == global_namespace)
2364 switch (desired)
2366 case NLE_TYPE:
2367 error_at (location, "%<::%E%> is not a type", name);
2368 break;
2369 case NLE_CXX98:
2370 error_at (location, "%<::%E%> is not a class or namespace", name);
2371 break;
2372 case NLE_NOT_CXX98:
2373 error_at (location,
2374 "%<::%E%> is not a class, namespace, or enumeration",
2375 name);
2376 break;
2377 default:
2378 gcc_unreachable ();
2381 else
2383 switch (desired)
2385 case NLE_TYPE:
2386 error_at (location, "%qE is not a type", name);
2387 break;
2388 case NLE_CXX98:
2389 error_at (location, "%qE is not a class or namespace", name);
2390 break;
2391 case NLE_NOT_CXX98:
2392 error_at (location,
2393 "%qE is not a class, namespace, or enumeration", name);
2394 break;
2395 default:
2396 gcc_unreachable ();
2401 /* If we are parsing tentatively, remember that an error has occurred
2402 during this tentative parse. Returns true if the error was
2403 simulated; false if a message should be issued by the caller. */
2405 static bool
2406 cp_parser_simulate_error (cp_parser* parser)
2408 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2410 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2411 return true;
2413 return false;
2416 /* Check for repeated decl-specifiers. */
2418 static void
2419 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2420 location_t location)
2422 int ds;
2424 for (ds = ds_first; ds != ds_last; ++ds)
2426 unsigned count = decl_specs->specs[ds];
2427 if (count < 2)
2428 continue;
2429 /* The "long" specifier is a special case because of "long long". */
2430 if (ds == ds_long)
2432 if (count > 2)
2433 error_at (location, "%<long long long%> is too long for GCC");
2434 else
2435 pedwarn_cxx98 (location, OPT_Wlong_long,
2436 "ISO C++ 1998 does not support %<long long%>");
2438 else if (count > 1)
2440 static const char *const decl_spec_names[] = {
2441 "signed",
2442 "unsigned",
2443 "short",
2444 "long",
2445 "const",
2446 "volatile",
2447 "restrict",
2448 "inline",
2449 "virtual",
2450 "explicit",
2451 "friend",
2452 "typedef",
2453 "constexpr",
2454 "__complex",
2455 "__thread"
2457 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2462 /* This function is called when a type is defined. If type
2463 definitions are forbidden at this point, an error message is
2464 issued. */
2466 static bool
2467 cp_parser_check_type_definition (cp_parser* parser)
2469 /* If types are forbidden here, issue a message. */
2470 if (parser->type_definition_forbidden_message)
2472 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2473 in the message need to be interpreted. */
2474 error (parser->type_definition_forbidden_message);
2475 return false;
2477 return true;
2480 /* This function is called when the DECLARATOR is processed. The TYPE
2481 was a type defined in the decl-specifiers. If it is invalid to
2482 define a type in the decl-specifiers for DECLARATOR, an error is
2483 issued. TYPE_LOCATION is the location of TYPE and is used
2484 for error reporting. */
2486 static void
2487 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2488 tree type, location_t type_location)
2490 /* [dcl.fct] forbids type definitions in return types.
2491 Unfortunately, it's not easy to know whether or not we are
2492 processing a return type until after the fact. */
2493 while (declarator
2494 && (declarator->kind == cdk_pointer
2495 || declarator->kind == cdk_reference
2496 || declarator->kind == cdk_ptrmem))
2497 declarator = declarator->declarator;
2498 if (declarator
2499 && declarator->kind == cdk_function)
2501 error_at (type_location,
2502 "new types may not be defined in a return type");
2503 inform (type_location,
2504 "(perhaps a semicolon is missing after the definition of %qT)",
2505 type);
2509 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2510 "<" in any valid C++ program. If the next token is indeed "<",
2511 issue a message warning the user about what appears to be an
2512 invalid attempt to form a template-id. LOCATION is the location
2513 of the type-specifier (TYPE) */
2515 static void
2516 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2517 tree type, location_t location)
2519 cp_token_position start = 0;
2521 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2523 if (TYPE_P (type))
2524 error_at (location, "%qT is not a template", type);
2525 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2526 error_at (location, "%qE is not a template", type);
2527 else
2528 error_at (location, "invalid template-id");
2529 /* Remember the location of the invalid "<". */
2530 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2531 start = cp_lexer_token_position (parser->lexer, true);
2532 /* Consume the "<". */
2533 cp_lexer_consume_token (parser->lexer);
2534 /* Parse the template arguments. */
2535 cp_parser_enclosed_template_argument_list (parser);
2536 /* Permanently remove the invalid template arguments so that
2537 this error message is not issued again. */
2538 if (start)
2539 cp_lexer_purge_tokens_after (parser->lexer, start);
2543 /* If parsing an integral constant-expression, issue an error message
2544 about the fact that THING appeared and return true. Otherwise,
2545 return false. In either case, set
2546 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2548 static bool
2549 cp_parser_non_integral_constant_expression (cp_parser *parser,
2550 non_integral_constant thing)
2552 parser->non_integral_constant_expression_p = true;
2553 if (parser->integral_constant_expression_p)
2555 if (!parser->allow_non_integral_constant_expression_p)
2557 const char *msg = NULL;
2558 switch (thing)
2560 case NIC_FLOAT:
2561 error ("floating-point literal "
2562 "cannot appear in a constant-expression");
2563 return true;
2564 case NIC_CAST:
2565 error ("a cast to a type other than an integral or "
2566 "enumeration type cannot appear in a "
2567 "constant-expression");
2568 return true;
2569 case NIC_TYPEID:
2570 error ("%<typeid%> operator "
2571 "cannot appear in a constant-expression");
2572 return true;
2573 case NIC_NCC:
2574 error ("non-constant compound literals "
2575 "cannot appear in a constant-expression");
2576 return true;
2577 case NIC_FUNC_CALL:
2578 error ("a function call "
2579 "cannot appear in a constant-expression");
2580 return true;
2581 case NIC_INC:
2582 error ("an increment "
2583 "cannot appear in a constant-expression");
2584 return true;
2585 case NIC_DEC:
2586 error ("an decrement "
2587 "cannot appear in a constant-expression");
2588 return true;
2589 case NIC_ARRAY_REF:
2590 error ("an array reference "
2591 "cannot appear in a constant-expression");
2592 return true;
2593 case NIC_ADDR_LABEL:
2594 error ("the address of a label "
2595 "cannot appear in a constant-expression");
2596 return true;
2597 case NIC_OVERLOADED:
2598 error ("calls to overloaded operators "
2599 "cannot appear in a constant-expression");
2600 return true;
2601 case NIC_ASSIGNMENT:
2602 error ("an assignment cannot appear in a constant-expression");
2603 return true;
2604 case NIC_COMMA:
2605 error ("a comma operator "
2606 "cannot appear in a constant-expression");
2607 return true;
2608 case NIC_CONSTRUCTOR:
2609 error ("a call to a constructor "
2610 "cannot appear in a constant-expression");
2611 return true;
2612 case NIC_THIS:
2613 msg = "this";
2614 break;
2615 case NIC_FUNC_NAME:
2616 msg = "__FUNCTION__";
2617 break;
2618 case NIC_PRETTY_FUNC:
2619 msg = "__PRETTY_FUNCTION__";
2620 break;
2621 case NIC_C99_FUNC:
2622 msg = "__func__";
2623 break;
2624 case NIC_VA_ARG:
2625 msg = "va_arg";
2626 break;
2627 case NIC_ARROW:
2628 msg = "->";
2629 break;
2630 case NIC_POINT:
2631 msg = ".";
2632 break;
2633 case NIC_STAR:
2634 msg = "*";
2635 break;
2636 case NIC_ADDR:
2637 msg = "&";
2638 break;
2639 case NIC_PREINCREMENT:
2640 msg = "++";
2641 break;
2642 case NIC_PREDECREMENT:
2643 msg = "--";
2644 break;
2645 case NIC_NEW:
2646 msg = "new";
2647 break;
2648 case NIC_DEL:
2649 msg = "delete";
2650 break;
2651 default:
2652 gcc_unreachable ();
2654 if (msg)
2655 error ("%qs cannot appear in a constant-expression", msg);
2656 return true;
2659 return false;
2662 /* Emit a diagnostic for an invalid type name. SCOPE is the
2663 qualifying scope (or NULL, if none) for ID. This function commits
2664 to the current active tentative parse, if any. (Otherwise, the
2665 problematic construct might be encountered again later, resulting
2666 in duplicate error messages.) LOCATION is the location of ID. */
2668 static void
2669 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2670 tree scope, tree id,
2671 location_t location)
2673 tree decl, old_scope;
2674 /* Try to lookup the identifier. */
2675 old_scope = parser->scope;
2676 parser->scope = scope;
2677 decl = cp_parser_lookup_name_simple (parser, id, location);
2678 parser->scope = old_scope;
2679 /* If the lookup found a template-name, it means that the user forgot
2680 to specify an argument list. Emit a useful error message. */
2681 if (TREE_CODE (decl) == TEMPLATE_DECL)
2682 error_at (location,
2683 "invalid use of template-name %qE without an argument list",
2684 decl);
2685 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2686 error_at (location, "invalid use of destructor %qD as a type", id);
2687 else if (TREE_CODE (decl) == TYPE_DECL)
2688 /* Something like 'unsigned A a;' */
2689 error_at (location, "invalid combination of multiple type-specifiers");
2690 else if (!parser->scope)
2692 /* Issue an error message. */
2693 error_at (location, "%qE does not name a type", id);
2694 /* If we're in a template class, it's possible that the user was
2695 referring to a type from a base class. For example:
2697 template <typename T> struct A { typedef T X; };
2698 template <typename T> struct B : public A<T> { X x; };
2700 The user should have said "typename A<T>::X". */
2701 if (processing_template_decl && current_class_type
2702 && TYPE_BINFO (current_class_type))
2704 tree b;
2706 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2708 b = TREE_CHAIN (b))
2710 tree base_type = BINFO_TYPE (b);
2711 if (CLASS_TYPE_P (base_type)
2712 && dependent_type_p (base_type))
2714 tree field;
2715 /* Go from a particular instantiation of the
2716 template (which will have an empty TYPE_FIELDs),
2717 to the main version. */
2718 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2719 for (field = TYPE_FIELDS (base_type);
2720 field;
2721 field = DECL_CHAIN (field))
2722 if (TREE_CODE (field) == TYPE_DECL
2723 && DECL_NAME (field) == id)
2725 inform (location,
2726 "(perhaps %<typename %T::%E%> was intended)",
2727 BINFO_TYPE (b), id);
2728 break;
2730 if (field)
2731 break;
2736 /* Here we diagnose qualified-ids where the scope is actually correct,
2737 but the identifier does not resolve to a valid type name. */
2738 else if (parser->scope != error_mark_node)
2740 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2741 error_at (location, "%qE in namespace %qE does not name a type",
2742 id, parser->scope);
2743 else if (CLASS_TYPE_P (parser->scope)
2744 && constructor_name_p (id, parser->scope))
2746 /* A<T>::A<T>() */
2747 error_at (location, "%<%T::%E%> names the constructor, not"
2748 " the type", parser->scope, id);
2749 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2750 error_at (location, "and %qT has no template constructors",
2751 parser->scope);
2753 else if (TYPE_P (parser->scope)
2754 && dependent_scope_p (parser->scope))
2755 error_at (location, "need %<typename%> before %<%T::%E%> because "
2756 "%qT is a dependent scope",
2757 parser->scope, id, parser->scope);
2758 else if (TYPE_P (parser->scope))
2759 error_at (location, "%qE in class %qT does not name a type",
2760 id, parser->scope);
2761 else
2762 gcc_unreachable ();
2764 cp_parser_commit_to_tentative_parse (parser);
2767 /* Check for a common situation where a type-name should be present,
2768 but is not, and issue a sensible error message. Returns true if an
2769 invalid type-name was detected.
2771 The situation handled by this function are variable declarations of the
2772 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2773 Usually, `ID' should name a type, but if we got here it means that it
2774 does not. We try to emit the best possible error message depending on
2775 how exactly the id-expression looks like. */
2777 static bool
2778 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2780 tree id;
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2783 /* Avoid duplicate error about ambiguous lookup. */
2784 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2786 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2787 if (next->type == CPP_NAME && next->ambiguous_p)
2788 goto out;
2791 cp_parser_parse_tentatively (parser);
2792 id = cp_parser_id_expression (parser,
2793 /*template_keyword_p=*/false,
2794 /*check_dependency_p=*/true,
2795 /*template_p=*/NULL,
2796 /*declarator_p=*/true,
2797 /*optional_p=*/false);
2798 /* If the next token is a (, this is a function with no explicit return
2799 type, i.e. constructor, destructor or conversion op. */
2800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2801 || TREE_CODE (id) == TYPE_DECL)
2803 cp_parser_abort_tentative_parse (parser);
2804 return false;
2806 if (!cp_parser_parse_definitely (parser))
2807 return false;
2809 /* Emit a diagnostic for the invalid type. */
2810 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2811 id, token->location);
2812 out:
2813 /* If we aren't in the middle of a declarator (i.e. in a
2814 parameter-declaration-clause), skip to the end of the declaration;
2815 there's no point in trying to process it. */
2816 if (!parser->in_declarator_p)
2817 cp_parser_skip_to_end_of_block_or_statement (parser);
2818 return true;
2821 /* Consume tokens up to, and including, the next non-nested closing `)'.
2822 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2823 are doing error recovery. Returns -1 if OR_COMMA is true and we
2824 found an unnested comma. */
2826 static int
2827 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2828 bool recovering,
2829 bool or_comma,
2830 bool consume_paren)
2832 unsigned paren_depth = 0;
2833 unsigned brace_depth = 0;
2834 unsigned square_depth = 0;
2836 if (recovering && !or_comma
2837 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2838 return 0;
2840 while (true)
2842 cp_token * token = cp_lexer_peek_token (parser->lexer);
2844 switch (token->type)
2846 case CPP_EOF:
2847 case CPP_PRAGMA_EOL:
2848 /* If we've run out of tokens, then there is no closing `)'. */
2849 return 0;
2851 /* This is good for lambda expression capture-lists. */
2852 case CPP_OPEN_SQUARE:
2853 ++square_depth;
2854 break;
2855 case CPP_CLOSE_SQUARE:
2856 if (!square_depth--)
2857 return 0;
2858 break;
2860 case CPP_SEMICOLON:
2861 /* This matches the processing in skip_to_end_of_statement. */
2862 if (!brace_depth)
2863 return 0;
2864 break;
2866 case CPP_OPEN_BRACE:
2867 ++brace_depth;
2868 break;
2869 case CPP_CLOSE_BRACE:
2870 if (!brace_depth--)
2871 return 0;
2872 break;
2874 case CPP_COMMA:
2875 if (recovering && or_comma && !brace_depth && !paren_depth
2876 && !square_depth)
2877 return -1;
2878 break;
2880 case CPP_OPEN_PAREN:
2881 if (!brace_depth)
2882 ++paren_depth;
2883 break;
2885 case CPP_CLOSE_PAREN:
2886 if (!brace_depth && !paren_depth--)
2888 if (consume_paren)
2889 cp_lexer_consume_token (parser->lexer);
2890 return 1;
2892 break;
2894 default:
2895 break;
2898 /* Consume the token. */
2899 cp_lexer_consume_token (parser->lexer);
2903 /* Consume tokens until we reach the end of the current statement.
2904 Normally, that will be just before consuming a `;'. However, if a
2905 non-nested `}' comes first, then we stop before consuming that. */
2907 static void
2908 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2910 unsigned nesting_depth = 0;
2912 while (true)
2914 cp_token *token = cp_lexer_peek_token (parser->lexer);
2916 switch (token->type)
2918 case CPP_EOF:
2919 case CPP_PRAGMA_EOL:
2920 /* If we've run out of tokens, stop. */
2921 return;
2923 case CPP_SEMICOLON:
2924 /* If the next token is a `;', we have reached the end of the
2925 statement. */
2926 if (!nesting_depth)
2927 return;
2928 break;
2930 case CPP_CLOSE_BRACE:
2931 /* If this is a non-nested '}', stop before consuming it.
2932 That way, when confronted with something like:
2934 { 3 + }
2936 we stop before consuming the closing '}', even though we
2937 have not yet reached a `;'. */
2938 if (nesting_depth == 0)
2939 return;
2941 /* If it is the closing '}' for a block that we have
2942 scanned, stop -- but only after consuming the token.
2943 That way given:
2945 void f g () { ... }
2946 typedef int I;
2948 we will stop after the body of the erroneously declared
2949 function, but before consuming the following `typedef'
2950 declaration. */
2951 if (--nesting_depth == 0)
2953 cp_lexer_consume_token (parser->lexer);
2954 return;
2957 case CPP_OPEN_BRACE:
2958 ++nesting_depth;
2959 break;
2961 default:
2962 break;
2965 /* Consume the token. */
2966 cp_lexer_consume_token (parser->lexer);
2970 /* This function is called at the end of a statement or declaration.
2971 If the next token is a semicolon, it is consumed; otherwise, error
2972 recovery is attempted. */
2974 static void
2975 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2977 /* Look for the trailing `;'. */
2978 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2980 /* If there is additional (erroneous) input, skip to the end of
2981 the statement. */
2982 cp_parser_skip_to_end_of_statement (parser);
2983 /* If the next token is now a `;', consume it. */
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2985 cp_lexer_consume_token (parser->lexer);
2989 /* Skip tokens until we have consumed an entire block, or until we
2990 have consumed a non-nested `;'. */
2992 static void
2993 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2995 int nesting_depth = 0;
2997 while (nesting_depth >= 0)
2999 cp_token *token = cp_lexer_peek_token (parser->lexer);
3001 switch (token->type)
3003 case CPP_EOF:
3004 case CPP_PRAGMA_EOL:
3005 /* If we've run out of tokens, stop. */
3006 return;
3008 case CPP_SEMICOLON:
3009 /* Stop if this is an unnested ';'. */
3010 if (!nesting_depth)
3011 nesting_depth = -1;
3012 break;
3014 case CPP_CLOSE_BRACE:
3015 /* Stop if this is an unnested '}', or closes the outermost
3016 nesting level. */
3017 nesting_depth--;
3018 if (nesting_depth < 0)
3019 return;
3020 if (!nesting_depth)
3021 nesting_depth = -1;
3022 break;
3024 case CPP_OPEN_BRACE:
3025 /* Nest. */
3026 nesting_depth++;
3027 break;
3029 default:
3030 break;
3033 /* Consume the token. */
3034 cp_lexer_consume_token (parser->lexer);
3038 /* Skip tokens until a non-nested closing curly brace is the next
3039 token, or there are no more tokens. Return true in the first case,
3040 false otherwise. */
3042 static bool
3043 cp_parser_skip_to_closing_brace (cp_parser *parser)
3045 unsigned nesting_depth = 0;
3047 while (true)
3049 cp_token *token = cp_lexer_peek_token (parser->lexer);
3051 switch (token->type)
3053 case CPP_EOF:
3054 case CPP_PRAGMA_EOL:
3055 /* If we've run out of tokens, stop. */
3056 return false;
3058 case CPP_CLOSE_BRACE:
3059 /* If the next token is a non-nested `}', then we have reached
3060 the end of the current block. */
3061 if (nesting_depth-- == 0)
3062 return true;
3063 break;
3065 case CPP_OPEN_BRACE:
3066 /* If it the next token is a `{', then we are entering a new
3067 block. Consume the entire block. */
3068 ++nesting_depth;
3069 break;
3071 default:
3072 break;
3075 /* Consume the token. */
3076 cp_lexer_consume_token (parser->lexer);
3080 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3081 parameter is the PRAGMA token, allowing us to purge the entire pragma
3082 sequence. */
3084 static void
3085 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3087 cp_token *token;
3089 parser->lexer->in_pragma = false;
3092 token = cp_lexer_consume_token (parser->lexer);
3093 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3095 /* Ensure that the pragma is not parsed again. */
3096 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3099 /* Require pragma end of line, resyncing with it as necessary. The
3100 arguments are as for cp_parser_skip_to_pragma_eol. */
3102 static void
3103 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3105 parser->lexer->in_pragma = false;
3106 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3107 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3110 /* This is a simple wrapper around make_typename_type. When the id is
3111 an unresolved identifier node, we can provide a superior diagnostic
3112 using cp_parser_diagnose_invalid_type_name. */
3114 static tree
3115 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3116 tree id, location_t id_location)
3118 tree result;
3119 if (TREE_CODE (id) == IDENTIFIER_NODE)
3121 result = make_typename_type (scope, id, typename_type,
3122 /*complain=*/tf_none);
3123 if (result == error_mark_node)
3124 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3125 return result;
3127 return make_typename_type (scope, id, typename_type, tf_error);
3130 /* This is a wrapper around the
3131 make_{pointer,ptrmem,reference}_declarator functions that decides
3132 which one to call based on the CODE and CLASS_TYPE arguments. The
3133 CODE argument should be one of the values returned by
3134 cp_parser_ptr_operator. */
3135 static cp_declarator *
3136 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3137 cp_cv_quals cv_qualifiers,
3138 cp_declarator *target)
3140 if (code == ERROR_MARK)
3141 return cp_error_declarator;
3143 if (code == INDIRECT_REF)
3144 if (class_type == NULL_TREE)
3145 return make_pointer_declarator (cv_qualifiers, target);
3146 else
3147 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3148 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3149 return make_reference_declarator (cv_qualifiers, target, false);
3150 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3151 return make_reference_declarator (cv_qualifiers, target, true);
3152 gcc_unreachable ();
3155 /* Create a new C++ parser. */
3157 static cp_parser *
3158 cp_parser_new (void)
3160 cp_parser *parser;
3161 cp_lexer *lexer;
3162 unsigned i;
3164 /* cp_lexer_new_main is called before doing GC allocation because
3165 cp_lexer_new_main might load a PCH file. */
3166 lexer = cp_lexer_new_main ();
3168 /* Initialize the binops_by_token so that we can get the tree
3169 directly from the token. */
3170 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3171 binops_by_token[binops[i].token_type] = binops[i];
3173 parser = ggc_alloc_cleared_cp_parser ();
3174 parser->lexer = lexer;
3175 parser->context = cp_parser_context_new (NULL);
3177 /* For now, we always accept GNU extensions. */
3178 parser->allow_gnu_extensions_p = 1;
3180 /* The `>' token is a greater-than operator, not the end of a
3181 template-id. */
3182 parser->greater_than_is_operator_p = true;
3184 parser->default_arg_ok_p = true;
3186 /* We are not parsing a constant-expression. */
3187 parser->integral_constant_expression_p = false;
3188 parser->allow_non_integral_constant_expression_p = false;
3189 parser->non_integral_constant_expression_p = false;
3191 /* Local variable names are not forbidden. */
3192 parser->local_variables_forbidden_p = false;
3194 /* We are not processing an `extern "C"' declaration. */
3195 parser->in_unbraced_linkage_specification_p = false;
3197 /* We are not processing a declarator. */
3198 parser->in_declarator_p = false;
3200 /* We are not processing a template-argument-list. */
3201 parser->in_template_argument_list_p = false;
3203 /* We are not in an iteration statement. */
3204 parser->in_statement = 0;
3206 /* We are not in a switch statement. */
3207 parser->in_switch_statement_p = false;
3209 /* We are not parsing a type-id inside an expression. */
3210 parser->in_type_id_in_expr_p = false;
3212 /* Declarations aren't implicitly extern "C". */
3213 parser->implicit_extern_c = false;
3215 /* String literals should be translated to the execution character set. */
3216 parser->translate_strings_p = true;
3218 /* We are not parsing a function body. */
3219 parser->in_function_body = false;
3221 /* The unparsed function queue is empty. */
3222 push_unparsed_function_queues (parser);
3224 /* There are no classes being defined. */
3225 parser->num_classes_being_defined = 0;
3227 /* No template parameters apply. */
3228 parser->num_template_parameter_lists = 0;
3230 return parser;
3233 /* Create a cp_lexer structure which will emit the tokens in CACHE
3234 and push it onto the parser's lexer stack. This is used for delayed
3235 parsing of in-class method bodies and default arguments, and should
3236 not be confused with tentative parsing. */
3237 static void
3238 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3240 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3241 lexer->next = parser->lexer;
3242 parser->lexer = lexer;
3244 /* Move the current source position to that of the first token in the
3245 new lexer. */
3246 cp_lexer_set_source_position_from_token (lexer->next_token);
3249 /* Pop the top lexer off the parser stack. This is never used for the
3250 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3251 static void
3252 cp_parser_pop_lexer (cp_parser *parser)
3254 cp_lexer *lexer = parser->lexer;
3255 parser->lexer = lexer->next;
3256 cp_lexer_destroy (lexer);
3258 /* Put the current source position back where it was before this
3259 lexer was pushed. */
3260 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3263 /* Lexical conventions [gram.lex] */
3265 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3266 identifier. */
3268 static tree
3269 cp_parser_identifier (cp_parser* parser)
3271 cp_token *token;
3273 /* Look for the identifier. */
3274 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3275 /* Return the value. */
3276 return token ? token->u.value : error_mark_node;
3279 /* Parse a sequence of adjacent string constants. Returns a
3280 TREE_STRING representing the combined, nul-terminated string
3281 constant. If TRANSLATE is true, translate the string to the
3282 execution character set. If WIDE_OK is true, a wide string is
3283 invalid here.
3285 C++98 [lex.string] says that if a narrow string literal token is
3286 adjacent to a wide string literal token, the behavior is undefined.
3287 However, C99 6.4.5p4 says that this results in a wide string literal.
3288 We follow C99 here, for consistency with the C front end.
3290 This code is largely lifted from lex_string() in c-lex.c.
3292 FUTURE: ObjC++ will need to handle @-strings here. */
3293 static tree
3294 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3296 tree value;
3297 size_t count;
3298 struct obstack str_ob;
3299 cpp_string str, istr, *strs;
3300 cp_token *tok;
3301 enum cpp_ttype type;
3303 tok = cp_lexer_peek_token (parser->lexer);
3304 if (!cp_parser_is_string_literal (tok))
3306 cp_parser_error (parser, "expected string-literal");
3307 return error_mark_node;
3310 type = tok->type;
3312 /* Try to avoid the overhead of creating and destroying an obstack
3313 for the common case of just one string. */
3314 if (!cp_parser_is_string_literal
3315 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3317 cp_lexer_consume_token (parser->lexer);
3319 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3320 str.len = TREE_STRING_LENGTH (tok->u.value);
3321 count = 1;
3323 strs = &str;
3325 else
3327 gcc_obstack_init (&str_ob);
3328 count = 0;
3332 cp_lexer_consume_token (parser->lexer);
3333 count++;
3334 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3335 str.len = TREE_STRING_LENGTH (tok->u.value);
3337 if (type != tok->type)
3339 if (type == CPP_STRING)
3340 type = tok->type;
3341 else if (tok->type != CPP_STRING)
3342 error_at (tok->location,
3343 "unsupported non-standard concatenation "
3344 "of string literals");
3347 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3349 tok = cp_lexer_peek_token (parser->lexer);
3351 while (cp_parser_is_string_literal (tok));
3353 strs = (cpp_string *) obstack_finish (&str_ob);
3356 if (type != CPP_STRING && !wide_ok)
3358 cp_parser_error (parser, "a wide string is invalid in this context");
3359 type = CPP_STRING;
3362 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3363 (parse_in, strs, count, &istr, type))
3365 value = build_string (istr.len, (const char *)istr.text);
3366 free (CONST_CAST (unsigned char *, istr.text));
3368 switch (type)
3370 default:
3371 case CPP_STRING:
3372 case CPP_UTF8STRING:
3373 TREE_TYPE (value) = char_array_type_node;
3374 break;
3375 case CPP_STRING16:
3376 TREE_TYPE (value) = char16_array_type_node;
3377 break;
3378 case CPP_STRING32:
3379 TREE_TYPE (value) = char32_array_type_node;
3380 break;
3381 case CPP_WSTRING:
3382 TREE_TYPE (value) = wchar_array_type_node;
3383 break;
3386 value = fix_string_type (value);
3388 else
3389 /* cpp_interpret_string has issued an error. */
3390 value = error_mark_node;
3392 if (count > 1)
3393 obstack_free (&str_ob, 0);
3395 return value;
3399 /* Basic concepts [gram.basic] */
3401 /* Parse a translation-unit.
3403 translation-unit:
3404 declaration-seq [opt]
3406 Returns TRUE if all went well. */
3408 static bool
3409 cp_parser_translation_unit (cp_parser* parser)
3411 /* The address of the first non-permanent object on the declarator
3412 obstack. */
3413 static void *declarator_obstack_base;
3415 bool success;
3417 /* Create the declarator obstack, if necessary. */
3418 if (!cp_error_declarator)
3420 gcc_obstack_init (&declarator_obstack);
3421 /* Create the error declarator. */
3422 cp_error_declarator = make_declarator (cdk_error);
3423 /* Create the empty parameter list. */
3424 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3425 /* Remember where the base of the declarator obstack lies. */
3426 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3429 cp_parser_declaration_seq_opt (parser);
3431 /* If there are no tokens left then all went well. */
3432 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3434 /* Get rid of the token array; we don't need it any more. */
3435 cp_lexer_destroy (parser->lexer);
3436 parser->lexer = NULL;
3438 /* This file might have been a context that's implicitly extern
3439 "C". If so, pop the lang context. (Only relevant for PCH.) */
3440 if (parser->implicit_extern_c)
3442 pop_lang_context ();
3443 parser->implicit_extern_c = false;
3446 /* Finish up. */
3447 finish_translation_unit ();
3449 success = true;
3451 else
3453 cp_parser_error (parser, "expected declaration");
3454 success = false;
3457 /* Make sure the declarator obstack was fully cleaned up. */
3458 gcc_assert (obstack_next_free (&declarator_obstack)
3459 == declarator_obstack_base);
3461 /* All went well. */
3462 return success;
3465 /* Expressions [gram.expr] */
3467 /* Parse a primary-expression.
3469 primary-expression:
3470 literal
3471 this
3472 ( expression )
3473 id-expression
3475 GNU Extensions:
3477 primary-expression:
3478 ( compound-statement )
3479 __builtin_va_arg ( assignment-expression , type-id )
3480 __builtin_offsetof ( type-id , offsetof-expression )
3482 C++ Extensions:
3483 __has_nothrow_assign ( type-id )
3484 __has_nothrow_constructor ( type-id )
3485 __has_nothrow_copy ( type-id )
3486 __has_trivial_assign ( type-id )
3487 __has_trivial_constructor ( type-id )
3488 __has_trivial_copy ( type-id )
3489 __has_trivial_destructor ( type-id )
3490 __has_virtual_destructor ( type-id )
3491 __is_abstract ( type-id )
3492 __is_base_of ( type-id , type-id )
3493 __is_class ( type-id )
3494 __is_convertible_to ( type-id , type-id )
3495 __is_empty ( type-id )
3496 __is_enum ( type-id )
3497 __is_pod ( type-id )
3498 __is_polymorphic ( type-id )
3499 __is_union ( type-id )
3501 Objective-C++ Extension:
3503 primary-expression:
3504 objc-expression
3506 literal:
3507 __null
3509 ADDRESS_P is true iff this expression was immediately preceded by
3510 "&" and therefore might denote a pointer-to-member. CAST_P is true
3511 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3512 true iff this expression is a template argument.
3514 Returns a representation of the expression. Upon return, *IDK
3515 indicates what kind of id-expression (if any) was present. */
3517 static tree
3518 cp_parser_primary_expression (cp_parser *parser,
3519 bool address_p,
3520 bool cast_p,
3521 bool template_arg_p,
3522 cp_id_kind *idk)
3524 cp_token *token = NULL;
3526 /* Assume the primary expression is not an id-expression. */
3527 *idk = CP_ID_KIND_NONE;
3529 /* Peek at the next token. */
3530 token = cp_lexer_peek_token (parser->lexer);
3531 switch (token->type)
3533 /* literal:
3534 integer-literal
3535 character-literal
3536 floating-literal
3537 string-literal
3538 boolean-literal */
3539 case CPP_CHAR:
3540 case CPP_CHAR16:
3541 case CPP_CHAR32:
3542 case CPP_WCHAR:
3543 case CPP_NUMBER:
3544 token = cp_lexer_consume_token (parser->lexer);
3545 if (TREE_CODE (token->u.value) == FIXED_CST)
3547 error_at (token->location,
3548 "fixed-point types not supported in C++");
3549 return error_mark_node;
3551 /* Floating-point literals are only allowed in an integral
3552 constant expression if they are cast to an integral or
3553 enumeration type. */
3554 if (TREE_CODE (token->u.value) == REAL_CST
3555 && parser->integral_constant_expression_p
3556 && pedantic)
3558 /* CAST_P will be set even in invalid code like "int(2.7 +
3559 ...)". Therefore, we have to check that the next token
3560 is sure to end the cast. */
3561 if (cast_p)
3563 cp_token *next_token;
3565 next_token = cp_lexer_peek_token (parser->lexer);
3566 if (/* The comma at the end of an
3567 enumerator-definition. */
3568 next_token->type != CPP_COMMA
3569 /* The curly brace at the end of an enum-specifier. */
3570 && next_token->type != CPP_CLOSE_BRACE
3571 /* The end of a statement. */
3572 && next_token->type != CPP_SEMICOLON
3573 /* The end of the cast-expression. */
3574 && next_token->type != CPP_CLOSE_PAREN
3575 /* The end of an array bound. */
3576 && next_token->type != CPP_CLOSE_SQUARE
3577 /* The closing ">" in a template-argument-list. */
3578 && (next_token->type != CPP_GREATER
3579 || parser->greater_than_is_operator_p)
3580 /* C++0x only: A ">>" treated like two ">" tokens,
3581 in a template-argument-list. */
3582 && (next_token->type != CPP_RSHIFT
3583 || (cxx_dialect == cxx98)
3584 || parser->greater_than_is_operator_p))
3585 cast_p = false;
3588 /* If we are within a cast, then the constraint that the
3589 cast is to an integral or enumeration type will be
3590 checked at that point. If we are not within a cast, then
3591 this code is invalid. */
3592 if (!cast_p)
3593 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3595 return token->u.value;
3597 case CPP_STRING:
3598 case CPP_STRING16:
3599 case CPP_STRING32:
3600 case CPP_WSTRING:
3601 case CPP_UTF8STRING:
3602 /* ??? Should wide strings be allowed when parser->translate_strings_p
3603 is false (i.e. in attributes)? If not, we can kill the third
3604 argument to cp_parser_string_literal. */
3605 return cp_parser_string_literal (parser,
3606 parser->translate_strings_p,
3607 true);
3609 case CPP_OPEN_PAREN:
3611 tree expr;
3612 bool saved_greater_than_is_operator_p;
3614 /* Consume the `('. */
3615 cp_lexer_consume_token (parser->lexer);
3616 /* Within a parenthesized expression, a `>' token is always
3617 the greater-than operator. */
3618 saved_greater_than_is_operator_p
3619 = parser->greater_than_is_operator_p;
3620 parser->greater_than_is_operator_p = true;
3621 /* If we see `( { ' then we are looking at the beginning of
3622 a GNU statement-expression. */
3623 if (cp_parser_allow_gnu_extensions_p (parser)
3624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3626 /* Statement-expressions are not allowed by the standard. */
3627 pedwarn (token->location, OPT_pedantic,
3628 "ISO C++ forbids braced-groups within expressions");
3630 /* And they're not allowed outside of a function-body; you
3631 cannot, for example, write:
3633 int i = ({ int j = 3; j + 1; });
3635 at class or namespace scope. */
3636 if (!parser->in_function_body
3637 || parser->in_template_argument_list_p)
3639 error_at (token->location,
3640 "statement-expressions are not allowed outside "
3641 "functions nor in template-argument lists");
3642 cp_parser_skip_to_end_of_block_or_statement (parser);
3643 expr = error_mark_node;
3645 else
3647 /* Start the statement-expression. */
3648 expr = begin_stmt_expr ();
3649 /* Parse the compound-statement. */
3650 cp_parser_compound_statement (parser, expr, false);
3651 /* Finish up. */
3652 expr = finish_stmt_expr (expr, false);
3655 else
3657 /* Parse the parenthesized expression. */
3658 expr = cp_parser_expression (parser, cast_p, idk);
3659 /* Let the front end know that this expression was
3660 enclosed in parentheses. This matters in case, for
3661 example, the expression is of the form `A::B', since
3662 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3663 not. */
3664 finish_parenthesized_expr (expr);
3666 /* The `>' token might be the end of a template-id or
3667 template-parameter-list now. */
3668 parser->greater_than_is_operator_p
3669 = saved_greater_than_is_operator_p;
3670 /* Consume the `)'. */
3671 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3672 cp_parser_skip_to_end_of_statement (parser);
3674 return expr;
3677 case CPP_OPEN_SQUARE:
3678 if (c_dialect_objc ())
3679 /* We have an Objective-C++ message. */
3680 return cp_parser_objc_expression (parser);
3681 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3682 return cp_parser_lambda_expression (parser);
3684 case CPP_OBJC_STRING:
3685 if (c_dialect_objc ())
3686 /* We have an Objective-C++ string literal. */
3687 return cp_parser_objc_expression (parser);
3688 cp_parser_error (parser, "expected primary-expression");
3689 return error_mark_node;
3691 case CPP_KEYWORD:
3692 switch (token->keyword)
3694 /* These two are the boolean literals. */
3695 case RID_TRUE:
3696 cp_lexer_consume_token (parser->lexer);
3697 return boolean_true_node;
3698 case RID_FALSE:
3699 cp_lexer_consume_token (parser->lexer);
3700 return boolean_false_node;
3702 /* The `__null' literal. */
3703 case RID_NULL:
3704 cp_lexer_consume_token (parser->lexer);
3705 return null_node;
3707 /* The `nullptr' literal. */
3708 case RID_NULLPTR:
3709 cp_lexer_consume_token (parser->lexer);
3710 return nullptr_node;
3712 /* Recognize the `this' keyword. */
3713 case RID_THIS:
3714 cp_lexer_consume_token (parser->lexer);
3715 if (parser->local_variables_forbidden_p)
3717 error_at (token->location,
3718 "%<this%> may not be used in this context");
3719 return error_mark_node;
3721 /* Pointers cannot appear in constant-expressions. */
3722 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3723 return error_mark_node;
3724 return finish_this_expr ();
3726 /* The `operator' keyword can be the beginning of an
3727 id-expression. */
3728 case RID_OPERATOR:
3729 goto id_expression;
3731 case RID_FUNCTION_NAME:
3732 case RID_PRETTY_FUNCTION_NAME:
3733 case RID_C99_FUNCTION_NAME:
3735 non_integral_constant name;
3737 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3738 __func__ are the names of variables -- but they are
3739 treated specially. Therefore, they are handled here,
3740 rather than relying on the generic id-expression logic
3741 below. Grammatically, these names are id-expressions.
3743 Consume the token. */
3744 token = cp_lexer_consume_token (parser->lexer);
3746 switch (token->keyword)
3748 case RID_FUNCTION_NAME:
3749 name = NIC_FUNC_NAME;
3750 break;
3751 case RID_PRETTY_FUNCTION_NAME:
3752 name = NIC_PRETTY_FUNC;
3753 break;
3754 case RID_C99_FUNCTION_NAME:
3755 name = NIC_C99_FUNC;
3756 break;
3757 default:
3758 gcc_unreachable ();
3761 if (cp_parser_non_integral_constant_expression (parser, name))
3762 return error_mark_node;
3764 /* Look up the name. */
3765 return finish_fname (token->u.value);
3768 case RID_VA_ARG:
3770 tree expression;
3771 tree type;
3773 /* The `__builtin_va_arg' construct is used to handle
3774 `va_arg'. Consume the `__builtin_va_arg' token. */
3775 cp_lexer_consume_token (parser->lexer);
3776 /* Look for the opening `('. */
3777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3778 /* Now, parse the assignment-expression. */
3779 expression = cp_parser_assignment_expression (parser,
3780 /*cast_p=*/false, NULL);
3781 /* Look for the `,'. */
3782 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3783 /* Parse the type-id. */
3784 type = cp_parser_type_id (parser);
3785 /* Look for the closing `)'. */
3786 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3787 /* Using `va_arg' in a constant-expression is not
3788 allowed. */
3789 if (cp_parser_non_integral_constant_expression (parser,
3790 NIC_VA_ARG))
3791 return error_mark_node;
3792 return build_x_va_arg (expression, type);
3795 case RID_OFFSETOF:
3796 return cp_parser_builtin_offsetof (parser);
3798 case RID_HAS_NOTHROW_ASSIGN:
3799 case RID_HAS_NOTHROW_CONSTRUCTOR:
3800 case RID_HAS_NOTHROW_COPY:
3801 case RID_HAS_TRIVIAL_ASSIGN:
3802 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3803 case RID_HAS_TRIVIAL_COPY:
3804 case RID_HAS_TRIVIAL_DESTRUCTOR:
3805 case RID_HAS_VIRTUAL_DESTRUCTOR:
3806 case RID_IS_ABSTRACT:
3807 case RID_IS_BASE_OF:
3808 case RID_IS_CLASS:
3809 case RID_IS_CONVERTIBLE_TO:
3810 case RID_IS_EMPTY:
3811 case RID_IS_ENUM:
3812 case RID_IS_POD:
3813 case RID_IS_POLYMORPHIC:
3814 case RID_IS_STD_LAYOUT:
3815 case RID_IS_TRIVIAL:
3816 case RID_IS_UNION:
3817 case RID_IS_LITERAL_TYPE:
3818 return cp_parser_trait_expr (parser, token->keyword);
3820 /* Objective-C++ expressions. */
3821 case RID_AT_ENCODE:
3822 case RID_AT_PROTOCOL:
3823 case RID_AT_SELECTOR:
3824 return cp_parser_objc_expression (parser);
3826 case RID_TEMPLATE:
3827 if (parser->in_function_body
3828 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3829 == CPP_LESS))
3831 error_at (token->location,
3832 "a template declaration cannot appear at block scope");
3833 cp_parser_skip_to_end_of_block_or_statement (parser);
3834 return error_mark_node;
3836 default:
3837 cp_parser_error (parser, "expected primary-expression");
3838 return error_mark_node;
3841 /* An id-expression can start with either an identifier, a
3842 `::' as the beginning of a qualified-id, or the "operator"
3843 keyword. */
3844 case CPP_NAME:
3845 case CPP_SCOPE:
3846 case CPP_TEMPLATE_ID:
3847 case CPP_NESTED_NAME_SPECIFIER:
3849 tree id_expression;
3850 tree decl;
3851 const char *error_msg;
3852 bool template_p;
3853 bool done;
3854 cp_token *id_expr_token;
3856 id_expression:
3857 /* Parse the id-expression. */
3858 id_expression
3859 = cp_parser_id_expression (parser,
3860 /*template_keyword_p=*/false,
3861 /*check_dependency_p=*/true,
3862 &template_p,
3863 /*declarator_p=*/false,
3864 /*optional_p=*/false);
3865 if (id_expression == error_mark_node)
3866 return error_mark_node;
3867 id_expr_token = token;
3868 token = cp_lexer_peek_token (parser->lexer);
3869 done = (token->type != CPP_OPEN_SQUARE
3870 && token->type != CPP_OPEN_PAREN
3871 && token->type != CPP_DOT
3872 && token->type != CPP_DEREF
3873 && token->type != CPP_PLUS_PLUS
3874 && token->type != CPP_MINUS_MINUS);
3875 /* If we have a template-id, then no further lookup is
3876 required. If the template-id was for a template-class, we
3877 will sometimes have a TYPE_DECL at this point. */
3878 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3879 || TREE_CODE (id_expression) == TYPE_DECL)
3880 decl = id_expression;
3881 /* Look up the name. */
3882 else
3884 tree ambiguous_decls;
3886 /* If we already know that this lookup is ambiguous, then
3887 we've already issued an error message; there's no reason
3888 to check again. */
3889 if (id_expr_token->type == CPP_NAME
3890 && id_expr_token->ambiguous_p)
3892 cp_parser_simulate_error (parser);
3893 return error_mark_node;
3896 decl = cp_parser_lookup_name (parser, id_expression,
3897 none_type,
3898 template_p,
3899 /*is_namespace=*/false,
3900 /*check_dependency=*/true,
3901 &ambiguous_decls,
3902 id_expr_token->location);
3903 /* If the lookup was ambiguous, an error will already have
3904 been issued. */
3905 if (ambiguous_decls)
3906 return error_mark_node;
3908 /* In Objective-C++, an instance variable (ivar) may be preferred
3909 to whatever cp_parser_lookup_name() found. */
3910 decl = objc_lookup_ivar (decl, id_expression);
3912 /* If name lookup gives us a SCOPE_REF, then the
3913 qualifying scope was dependent. */
3914 if (TREE_CODE (decl) == SCOPE_REF)
3916 /* At this point, we do not know if DECL is a valid
3917 integral constant expression. We assume that it is
3918 in fact such an expression, so that code like:
3920 template <int N> struct A {
3921 int a[B<N>::i];
3924 is accepted. At template-instantiation time, we
3925 will check that B<N>::i is actually a constant. */
3926 return decl;
3928 /* Check to see if DECL is a local variable in a context
3929 where that is forbidden. */
3930 if (parser->local_variables_forbidden_p
3931 && local_variable_p (decl))
3933 /* It might be that we only found DECL because we are
3934 trying to be generous with pre-ISO scoping rules.
3935 For example, consider:
3937 int i;
3938 void g() {
3939 for (int i = 0; i < 10; ++i) {}
3940 extern void f(int j = i);
3943 Here, name look up will originally find the out
3944 of scope `i'. We need to issue a warning message,
3945 but then use the global `i'. */
3946 decl = check_for_out_of_scope_variable (decl);
3947 if (local_variable_p (decl))
3949 error_at (id_expr_token->location,
3950 "local variable %qD may not appear in this context",
3951 decl);
3952 return error_mark_node;
3957 decl = (finish_id_expression
3958 (id_expression, decl, parser->scope,
3959 idk,
3960 parser->integral_constant_expression_p,
3961 parser->allow_non_integral_constant_expression_p,
3962 &parser->non_integral_constant_expression_p,
3963 template_p, done, address_p,
3964 template_arg_p,
3965 &error_msg,
3966 id_expr_token->location));
3967 if (error_msg)
3968 cp_parser_error (parser, error_msg);
3969 return decl;
3972 /* Anything else is an error. */
3973 default:
3974 cp_parser_error (parser, "expected primary-expression");
3975 return error_mark_node;
3979 /* Parse an id-expression.
3981 id-expression:
3982 unqualified-id
3983 qualified-id
3985 qualified-id:
3986 :: [opt] nested-name-specifier template [opt] unqualified-id
3987 :: identifier
3988 :: operator-function-id
3989 :: template-id
3991 Return a representation of the unqualified portion of the
3992 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3993 a `::' or nested-name-specifier.
3995 Often, if the id-expression was a qualified-id, the caller will
3996 want to make a SCOPE_REF to represent the qualified-id. This
3997 function does not do this in order to avoid wastefully creating
3998 SCOPE_REFs when they are not required.
4000 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4001 `template' keyword.
4003 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4004 uninstantiated templates.
4006 If *TEMPLATE_P is non-NULL, it is set to true iff the
4007 `template' keyword is used to explicitly indicate that the entity
4008 named is a template.
4010 If DECLARATOR_P is true, the id-expression is appearing as part of
4011 a declarator, rather than as part of an expression. */
4013 static tree
4014 cp_parser_id_expression (cp_parser *parser,
4015 bool template_keyword_p,
4016 bool check_dependency_p,
4017 bool *template_p,
4018 bool declarator_p,
4019 bool optional_p)
4021 bool global_scope_p;
4022 bool nested_name_specifier_p;
4024 /* Assume the `template' keyword was not used. */
4025 if (template_p)
4026 *template_p = template_keyword_p;
4028 /* Look for the optional `::' operator. */
4029 global_scope_p
4030 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4031 != NULL_TREE);
4032 /* Look for the optional nested-name-specifier. */
4033 nested_name_specifier_p
4034 = (cp_parser_nested_name_specifier_opt (parser,
4035 /*typename_keyword_p=*/false,
4036 check_dependency_p,
4037 /*type_p=*/false,
4038 declarator_p)
4039 != NULL_TREE);
4040 /* If there is a nested-name-specifier, then we are looking at
4041 the first qualified-id production. */
4042 if (nested_name_specifier_p)
4044 tree saved_scope;
4045 tree saved_object_scope;
4046 tree saved_qualifying_scope;
4047 tree unqualified_id;
4048 bool is_template;
4050 /* See if the next token is the `template' keyword. */
4051 if (!template_p)
4052 template_p = &is_template;
4053 *template_p = cp_parser_optional_template_keyword (parser);
4054 /* Name lookup we do during the processing of the
4055 unqualified-id might obliterate SCOPE. */
4056 saved_scope = parser->scope;
4057 saved_object_scope = parser->object_scope;
4058 saved_qualifying_scope = parser->qualifying_scope;
4059 /* Process the final unqualified-id. */
4060 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4061 check_dependency_p,
4062 declarator_p,
4063 /*optional_p=*/false);
4064 /* Restore the SAVED_SCOPE for our caller. */
4065 parser->scope = saved_scope;
4066 parser->object_scope = saved_object_scope;
4067 parser->qualifying_scope = saved_qualifying_scope;
4069 return unqualified_id;
4071 /* Otherwise, if we are in global scope, then we are looking at one
4072 of the other qualified-id productions. */
4073 else if (global_scope_p)
4075 cp_token *token;
4076 tree id;
4078 /* Peek at the next token. */
4079 token = cp_lexer_peek_token (parser->lexer);
4081 /* If it's an identifier, and the next token is not a "<", then
4082 we can avoid the template-id case. This is an optimization
4083 for this common case. */
4084 if (token->type == CPP_NAME
4085 && !cp_parser_nth_token_starts_template_argument_list_p
4086 (parser, 2))
4087 return cp_parser_identifier (parser);
4089 cp_parser_parse_tentatively (parser);
4090 /* Try a template-id. */
4091 id = cp_parser_template_id (parser,
4092 /*template_keyword_p=*/false,
4093 /*check_dependency_p=*/true,
4094 declarator_p);
4095 /* If that worked, we're done. */
4096 if (cp_parser_parse_definitely (parser))
4097 return id;
4099 /* Peek at the next token. (Changes in the token buffer may
4100 have invalidated the pointer obtained above.) */
4101 token = cp_lexer_peek_token (parser->lexer);
4103 switch (token->type)
4105 case CPP_NAME:
4106 return cp_parser_identifier (parser);
4108 case CPP_KEYWORD:
4109 if (token->keyword == RID_OPERATOR)
4110 return cp_parser_operator_function_id (parser);
4111 /* Fall through. */
4113 default:
4114 cp_parser_error (parser, "expected id-expression");
4115 return error_mark_node;
4118 else
4119 return cp_parser_unqualified_id (parser, template_keyword_p,
4120 /*check_dependency_p=*/true,
4121 declarator_p,
4122 optional_p);
4125 /* Parse an unqualified-id.
4127 unqualified-id:
4128 identifier
4129 operator-function-id
4130 conversion-function-id
4131 ~ class-name
4132 template-id
4134 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4135 keyword, in a construct like `A::template ...'.
4137 Returns a representation of unqualified-id. For the `identifier'
4138 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4139 production a BIT_NOT_EXPR is returned; the operand of the
4140 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4141 other productions, see the documentation accompanying the
4142 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4143 names are looked up in uninstantiated templates. If DECLARATOR_P
4144 is true, the unqualified-id is appearing as part of a declarator,
4145 rather than as part of an expression. */
4147 static tree
4148 cp_parser_unqualified_id (cp_parser* parser,
4149 bool template_keyword_p,
4150 bool check_dependency_p,
4151 bool declarator_p,
4152 bool optional_p)
4154 cp_token *token;
4156 /* Peek at the next token. */
4157 token = cp_lexer_peek_token (parser->lexer);
4159 switch (token->type)
4161 case CPP_NAME:
4163 tree id;
4165 /* We don't know yet whether or not this will be a
4166 template-id. */
4167 cp_parser_parse_tentatively (parser);
4168 /* Try a template-id. */
4169 id = cp_parser_template_id (parser, template_keyword_p,
4170 check_dependency_p,
4171 declarator_p);
4172 /* If it worked, we're done. */
4173 if (cp_parser_parse_definitely (parser))
4174 return id;
4175 /* Otherwise, it's an ordinary identifier. */
4176 return cp_parser_identifier (parser);
4179 case CPP_TEMPLATE_ID:
4180 return cp_parser_template_id (parser, template_keyword_p,
4181 check_dependency_p,
4182 declarator_p);
4184 case CPP_COMPL:
4186 tree type_decl;
4187 tree qualifying_scope;
4188 tree object_scope;
4189 tree scope;
4190 bool done;
4192 /* Consume the `~' token. */
4193 cp_lexer_consume_token (parser->lexer);
4194 /* Parse the class-name. The standard, as written, seems to
4195 say that:
4197 template <typename T> struct S { ~S (); };
4198 template <typename T> S<T>::~S() {}
4200 is invalid, since `~' must be followed by a class-name, but
4201 `S<T>' is dependent, and so not known to be a class.
4202 That's not right; we need to look in uninstantiated
4203 templates. A further complication arises from:
4205 template <typename T> void f(T t) {
4206 t.T::~T();
4209 Here, it is not possible to look up `T' in the scope of `T'
4210 itself. We must look in both the current scope, and the
4211 scope of the containing complete expression.
4213 Yet another issue is:
4215 struct S {
4216 int S;
4217 ~S();
4220 S::~S() {}
4222 The standard does not seem to say that the `S' in `~S'
4223 should refer to the type `S' and not the data member
4224 `S::S'. */
4226 /* DR 244 says that we look up the name after the "~" in the
4227 same scope as we looked up the qualifying name. That idea
4228 isn't fully worked out; it's more complicated than that. */
4229 scope = parser->scope;
4230 object_scope = parser->object_scope;
4231 qualifying_scope = parser->qualifying_scope;
4233 /* Check for invalid scopes. */
4234 if (scope == error_mark_node)
4236 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4237 cp_lexer_consume_token (parser->lexer);
4238 return error_mark_node;
4240 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4242 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4243 error_at (token->location,
4244 "scope %qT before %<~%> is not a class-name",
4245 scope);
4246 cp_parser_simulate_error (parser);
4247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4248 cp_lexer_consume_token (parser->lexer);
4249 return error_mark_node;
4251 gcc_assert (!scope || TYPE_P (scope));
4253 /* If the name is of the form "X::~X" it's OK even if X is a
4254 typedef. */
4255 token = cp_lexer_peek_token (parser->lexer);
4256 if (scope
4257 && token->type == CPP_NAME
4258 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4259 != CPP_LESS)
4260 && (token->u.value == TYPE_IDENTIFIER (scope)
4261 || constructor_name_p (token->u.value, scope)))
4263 cp_lexer_consume_token (parser->lexer);
4264 return build_nt (BIT_NOT_EXPR, scope);
4267 /* If there was an explicit qualification (S::~T), first look
4268 in the scope given by the qualification (i.e., S).
4270 Note: in the calls to cp_parser_class_name below we pass
4271 typename_type so that lookup finds the injected-class-name
4272 rather than the constructor. */
4273 done = false;
4274 type_decl = NULL_TREE;
4275 if (scope)
4277 cp_parser_parse_tentatively (parser);
4278 type_decl = cp_parser_class_name (parser,
4279 /*typename_keyword_p=*/false,
4280 /*template_keyword_p=*/false,
4281 typename_type,
4282 /*check_dependency=*/false,
4283 /*class_head_p=*/false,
4284 declarator_p);
4285 if (cp_parser_parse_definitely (parser))
4286 done = true;
4288 /* In "N::S::~S", look in "N" as well. */
4289 if (!done && scope && qualifying_scope)
4291 cp_parser_parse_tentatively (parser);
4292 parser->scope = qualifying_scope;
4293 parser->object_scope = NULL_TREE;
4294 parser->qualifying_scope = NULL_TREE;
4295 type_decl
4296 = cp_parser_class_name (parser,
4297 /*typename_keyword_p=*/false,
4298 /*template_keyword_p=*/false,
4299 typename_type,
4300 /*check_dependency=*/false,
4301 /*class_head_p=*/false,
4302 declarator_p);
4303 if (cp_parser_parse_definitely (parser))
4304 done = true;
4306 /* In "p->S::~T", look in the scope given by "*p" as well. */
4307 else if (!done && object_scope)
4309 cp_parser_parse_tentatively (parser);
4310 parser->scope = object_scope;
4311 parser->object_scope = NULL_TREE;
4312 parser->qualifying_scope = NULL_TREE;
4313 type_decl
4314 = cp_parser_class_name (parser,
4315 /*typename_keyword_p=*/false,
4316 /*template_keyword_p=*/false,
4317 typename_type,
4318 /*check_dependency=*/false,
4319 /*class_head_p=*/false,
4320 declarator_p);
4321 if (cp_parser_parse_definitely (parser))
4322 done = true;
4324 /* Look in the surrounding context. */
4325 if (!done)
4327 parser->scope = NULL_TREE;
4328 parser->object_scope = NULL_TREE;
4329 parser->qualifying_scope = NULL_TREE;
4330 if (processing_template_decl)
4331 cp_parser_parse_tentatively (parser);
4332 type_decl
4333 = cp_parser_class_name (parser,
4334 /*typename_keyword_p=*/false,
4335 /*template_keyword_p=*/false,
4336 typename_type,
4337 /*check_dependency=*/false,
4338 /*class_head_p=*/false,
4339 declarator_p);
4340 if (processing_template_decl
4341 && ! cp_parser_parse_definitely (parser))
4343 /* We couldn't find a type with this name, so just accept
4344 it and check for a match at instantiation time. */
4345 type_decl = cp_parser_identifier (parser);
4346 if (type_decl != error_mark_node)
4347 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4348 return type_decl;
4351 /* If an error occurred, assume that the name of the
4352 destructor is the same as the name of the qualifying
4353 class. That allows us to keep parsing after running
4354 into ill-formed destructor names. */
4355 if (type_decl == error_mark_node && scope)
4356 return build_nt (BIT_NOT_EXPR, scope);
4357 else if (type_decl == error_mark_node)
4358 return error_mark_node;
4360 /* Check that destructor name and scope match. */
4361 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4363 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4364 error_at (token->location,
4365 "declaration of %<~%T%> as member of %qT",
4366 type_decl, scope);
4367 cp_parser_simulate_error (parser);
4368 return error_mark_node;
4371 /* [class.dtor]
4373 A typedef-name that names a class shall not be used as the
4374 identifier in the declarator for a destructor declaration. */
4375 if (declarator_p
4376 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4377 && !DECL_SELF_REFERENCE_P (type_decl)
4378 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4379 error_at (token->location,
4380 "typedef-name %qD used as destructor declarator",
4381 type_decl);
4383 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4386 case CPP_KEYWORD:
4387 if (token->keyword == RID_OPERATOR)
4389 tree id;
4391 /* This could be a template-id, so we try that first. */
4392 cp_parser_parse_tentatively (parser);
4393 /* Try a template-id. */
4394 id = cp_parser_template_id (parser, template_keyword_p,
4395 /*check_dependency_p=*/true,
4396 declarator_p);
4397 /* If that worked, we're done. */
4398 if (cp_parser_parse_definitely (parser))
4399 return id;
4400 /* We still don't know whether we're looking at an
4401 operator-function-id or a conversion-function-id. */
4402 cp_parser_parse_tentatively (parser);
4403 /* Try an operator-function-id. */
4404 id = cp_parser_operator_function_id (parser);
4405 /* If that didn't work, try a conversion-function-id. */
4406 if (!cp_parser_parse_definitely (parser))
4407 id = cp_parser_conversion_function_id (parser);
4409 return id;
4411 /* Fall through. */
4413 default:
4414 if (optional_p)
4415 return NULL_TREE;
4416 cp_parser_error (parser, "expected unqualified-id");
4417 return error_mark_node;
4421 /* Parse an (optional) nested-name-specifier.
4423 nested-name-specifier: [C++98]
4424 class-or-namespace-name :: nested-name-specifier [opt]
4425 class-or-namespace-name :: template nested-name-specifier [opt]
4427 nested-name-specifier: [C++0x]
4428 type-name ::
4429 namespace-name ::
4430 nested-name-specifier identifier ::
4431 nested-name-specifier template [opt] simple-template-id ::
4433 PARSER->SCOPE should be set appropriately before this function is
4434 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4435 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4436 in name lookups.
4438 Sets PARSER->SCOPE to the class (TYPE) or namespace
4439 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4440 it unchanged if there is no nested-name-specifier. Returns the new
4441 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4443 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4444 part of a declaration and/or decl-specifier. */
4446 static tree
4447 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4448 bool typename_keyword_p,
4449 bool check_dependency_p,
4450 bool type_p,
4451 bool is_declaration)
4453 bool success = false;
4454 cp_token_position start = 0;
4455 cp_token *token;
4457 /* Remember where the nested-name-specifier starts. */
4458 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4460 start = cp_lexer_token_position (parser->lexer, false);
4461 push_deferring_access_checks (dk_deferred);
4464 while (true)
4466 tree new_scope;
4467 tree old_scope;
4468 tree saved_qualifying_scope;
4469 bool template_keyword_p;
4471 /* Spot cases that cannot be the beginning of a
4472 nested-name-specifier. */
4473 token = cp_lexer_peek_token (parser->lexer);
4475 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4476 the already parsed nested-name-specifier. */
4477 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4479 /* Grab the nested-name-specifier and continue the loop. */
4480 cp_parser_pre_parsed_nested_name_specifier (parser);
4481 /* If we originally encountered this nested-name-specifier
4482 with IS_DECLARATION set to false, we will not have
4483 resolved TYPENAME_TYPEs, so we must do so here. */
4484 if (is_declaration
4485 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4487 new_scope = resolve_typename_type (parser->scope,
4488 /*only_current_p=*/false);
4489 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4490 parser->scope = new_scope;
4492 success = true;
4493 continue;
4496 /* Spot cases that cannot be the beginning of a
4497 nested-name-specifier. On the second and subsequent times
4498 through the loop, we look for the `template' keyword. */
4499 if (success && token->keyword == RID_TEMPLATE)
4501 /* A template-id can start a nested-name-specifier. */
4502 else if (token->type == CPP_TEMPLATE_ID)
4504 else
4506 /* If the next token is not an identifier, then it is
4507 definitely not a type-name or namespace-name. */
4508 if (token->type != CPP_NAME)
4509 break;
4510 /* If the following token is neither a `<' (to begin a
4511 template-id), nor a `::', then we are not looking at a
4512 nested-name-specifier. */
4513 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4514 if (token->type != CPP_SCOPE
4515 && !cp_parser_nth_token_starts_template_argument_list_p
4516 (parser, 2))
4517 break;
4520 /* The nested-name-specifier is optional, so we parse
4521 tentatively. */
4522 cp_parser_parse_tentatively (parser);
4524 /* Look for the optional `template' keyword, if this isn't the
4525 first time through the loop. */
4526 if (success)
4527 template_keyword_p = cp_parser_optional_template_keyword (parser);
4528 else
4529 template_keyword_p = false;
4531 /* Save the old scope since the name lookup we are about to do
4532 might destroy it. */
4533 old_scope = parser->scope;
4534 saved_qualifying_scope = parser->qualifying_scope;
4535 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4536 look up names in "X<T>::I" in order to determine that "Y" is
4537 a template. So, if we have a typename at this point, we make
4538 an effort to look through it. */
4539 if (is_declaration
4540 && !typename_keyword_p
4541 && parser->scope
4542 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4543 parser->scope = resolve_typename_type (parser->scope,
4544 /*only_current_p=*/false);
4545 /* Parse the qualifying entity. */
4546 new_scope
4547 = cp_parser_qualifying_entity (parser,
4548 typename_keyword_p,
4549 template_keyword_p,
4550 check_dependency_p,
4551 type_p,
4552 is_declaration);
4553 /* Look for the `::' token. */
4554 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4556 /* If we found what we wanted, we keep going; otherwise, we're
4557 done. */
4558 if (!cp_parser_parse_definitely (parser))
4560 bool error_p = false;
4562 /* Restore the OLD_SCOPE since it was valid before the
4563 failed attempt at finding the last
4564 class-or-namespace-name. */
4565 parser->scope = old_scope;
4566 parser->qualifying_scope = saved_qualifying_scope;
4567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4568 break;
4569 /* If the next token is an identifier, and the one after
4570 that is a `::', then any valid interpretation would have
4571 found a class-or-namespace-name. */
4572 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4573 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4574 == CPP_SCOPE)
4575 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4576 != CPP_COMPL))
4578 token = cp_lexer_consume_token (parser->lexer);
4579 if (!error_p)
4581 if (!token->ambiguous_p)
4583 tree decl;
4584 tree ambiguous_decls;
4586 decl = cp_parser_lookup_name (parser, token->u.value,
4587 none_type,
4588 /*is_template=*/false,
4589 /*is_namespace=*/false,
4590 /*check_dependency=*/true,
4591 &ambiguous_decls,
4592 token->location);
4593 if (TREE_CODE (decl) == TEMPLATE_DECL)
4594 error_at (token->location,
4595 "%qD used without template parameters",
4596 decl);
4597 else if (ambiguous_decls)
4599 error_at (token->location,
4600 "reference to %qD is ambiguous",
4601 token->u.value);
4602 print_candidates (ambiguous_decls);
4603 decl = error_mark_node;
4605 else
4607 if (cxx_dialect != cxx98)
4608 cp_parser_name_lookup_error
4609 (parser, token->u.value, decl, NLE_NOT_CXX98,
4610 token->location);
4611 else
4612 cp_parser_name_lookup_error
4613 (parser, token->u.value, decl, NLE_CXX98,
4614 token->location);
4617 parser->scope = error_mark_node;
4618 error_p = true;
4619 /* Treat this as a successful nested-name-specifier
4620 due to:
4622 [basic.lookup.qual]
4624 If the name found is not a class-name (clause
4625 _class_) or namespace-name (_namespace.def_), the
4626 program is ill-formed. */
4627 success = true;
4629 cp_lexer_consume_token (parser->lexer);
4631 break;
4633 /* We've found one valid nested-name-specifier. */
4634 success = true;
4635 /* Name lookup always gives us a DECL. */
4636 if (TREE_CODE (new_scope) == TYPE_DECL)
4637 new_scope = TREE_TYPE (new_scope);
4638 /* Uses of "template" must be followed by actual templates. */
4639 if (template_keyword_p
4640 && !(CLASS_TYPE_P (new_scope)
4641 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4642 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4643 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4644 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4645 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4646 == TEMPLATE_ID_EXPR)))
4647 permerror (input_location, TYPE_P (new_scope)
4648 ? "%qT is not a template"
4649 : "%qD is not a template",
4650 new_scope);
4651 /* If it is a class scope, try to complete it; we are about to
4652 be looking up names inside the class. */
4653 if (TYPE_P (new_scope)
4654 /* Since checking types for dependency can be expensive,
4655 avoid doing it if the type is already complete. */
4656 && !COMPLETE_TYPE_P (new_scope)
4657 /* Do not try to complete dependent types. */
4658 && !dependent_type_p (new_scope))
4660 new_scope = complete_type (new_scope);
4661 /* If it is a typedef to current class, use the current
4662 class instead, as the typedef won't have any names inside
4663 it yet. */
4664 if (!COMPLETE_TYPE_P (new_scope)
4665 && currently_open_class (new_scope))
4666 new_scope = TYPE_MAIN_VARIANT (new_scope);
4668 /* Make sure we look in the right scope the next time through
4669 the loop. */
4670 parser->scope = new_scope;
4673 /* If parsing tentatively, replace the sequence of tokens that makes
4674 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4675 token. That way, should we re-parse the token stream, we will
4676 not have to repeat the effort required to do the parse, nor will
4677 we issue duplicate error messages. */
4678 if (success && start)
4680 cp_token *token;
4682 token = cp_lexer_token_at (parser->lexer, start);
4683 /* Reset the contents of the START token. */
4684 token->type = CPP_NESTED_NAME_SPECIFIER;
4685 /* Retrieve any deferred checks. Do not pop this access checks yet
4686 so the memory will not be reclaimed during token replacing below. */
4687 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4688 token->u.tree_check_value->value = parser->scope;
4689 token->u.tree_check_value->checks = get_deferred_access_checks ();
4690 token->u.tree_check_value->qualifying_scope =
4691 parser->qualifying_scope;
4692 token->keyword = RID_MAX;
4694 /* Purge all subsequent tokens. */
4695 cp_lexer_purge_tokens_after (parser->lexer, start);
4698 if (start)
4699 pop_to_parent_deferring_access_checks ();
4701 return success ? parser->scope : NULL_TREE;
4704 /* Parse a nested-name-specifier. See
4705 cp_parser_nested_name_specifier_opt for details. This function
4706 behaves identically, except that it will an issue an error if no
4707 nested-name-specifier is present. */
4709 static tree
4710 cp_parser_nested_name_specifier (cp_parser *parser,
4711 bool typename_keyword_p,
4712 bool check_dependency_p,
4713 bool type_p,
4714 bool is_declaration)
4716 tree scope;
4718 /* Look for the nested-name-specifier. */
4719 scope = cp_parser_nested_name_specifier_opt (parser,
4720 typename_keyword_p,
4721 check_dependency_p,
4722 type_p,
4723 is_declaration);
4724 /* If it was not present, issue an error message. */
4725 if (!scope)
4727 cp_parser_error (parser, "expected nested-name-specifier");
4728 parser->scope = NULL_TREE;
4731 return scope;
4734 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4735 this is either a class-name or a namespace-name (which corresponds
4736 to the class-or-namespace-name production in the grammar). For
4737 C++0x, it can also be a type-name that refers to an enumeration
4738 type.
4740 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4741 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4742 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4743 TYPE_P is TRUE iff the next name should be taken as a class-name,
4744 even the same name is declared to be another entity in the same
4745 scope.
4747 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4748 specified by the class-or-namespace-name. If neither is found the
4749 ERROR_MARK_NODE is returned. */
4751 static tree
4752 cp_parser_qualifying_entity (cp_parser *parser,
4753 bool typename_keyword_p,
4754 bool template_keyword_p,
4755 bool check_dependency_p,
4756 bool type_p,
4757 bool is_declaration)
4759 tree saved_scope;
4760 tree saved_qualifying_scope;
4761 tree saved_object_scope;
4762 tree scope;
4763 bool only_class_p;
4764 bool successful_parse_p;
4766 /* Before we try to parse the class-name, we must save away the
4767 current PARSER->SCOPE since cp_parser_class_name will destroy
4768 it. */
4769 saved_scope = parser->scope;
4770 saved_qualifying_scope = parser->qualifying_scope;
4771 saved_object_scope = parser->object_scope;
4772 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4773 there is no need to look for a namespace-name. */
4774 only_class_p = template_keyword_p
4775 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4776 if (!only_class_p)
4777 cp_parser_parse_tentatively (parser);
4778 scope = cp_parser_class_name (parser,
4779 typename_keyword_p,
4780 template_keyword_p,
4781 type_p ? class_type : none_type,
4782 check_dependency_p,
4783 /*class_head_p=*/false,
4784 is_declaration);
4785 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4786 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4787 if (!only_class_p
4788 && cxx_dialect != cxx98
4789 && !successful_parse_p)
4791 /* Restore the saved scope. */
4792 parser->scope = saved_scope;
4793 parser->qualifying_scope = saved_qualifying_scope;
4794 parser->object_scope = saved_object_scope;
4796 /* Parse tentatively. */
4797 cp_parser_parse_tentatively (parser);
4799 /* Parse a typedef-name or enum-name. */
4800 scope = cp_parser_nonclass_name (parser);
4802 /* "If the name found does not designate a namespace or a class,
4803 enumeration, or dependent type, the program is ill-formed."
4805 We cover classes and dependent types above and namespaces below,
4806 so this code is only looking for enums. */
4807 if (!scope || TREE_CODE (scope) != TYPE_DECL
4808 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4809 cp_parser_simulate_error (parser);
4811 successful_parse_p = cp_parser_parse_definitely (parser);
4813 /* If that didn't work, try for a namespace-name. */
4814 if (!only_class_p && !successful_parse_p)
4816 /* Restore the saved scope. */
4817 parser->scope = saved_scope;
4818 parser->qualifying_scope = saved_qualifying_scope;
4819 parser->object_scope = saved_object_scope;
4820 /* If we are not looking at an identifier followed by the scope
4821 resolution operator, then this is not part of a
4822 nested-name-specifier. (Note that this function is only used
4823 to parse the components of a nested-name-specifier.) */
4824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4825 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4826 return error_mark_node;
4827 scope = cp_parser_namespace_name (parser);
4830 return scope;
4833 /* Parse a postfix-expression.
4835 postfix-expression:
4836 primary-expression
4837 postfix-expression [ expression ]
4838 postfix-expression ( expression-list [opt] )
4839 simple-type-specifier ( expression-list [opt] )
4840 typename :: [opt] nested-name-specifier identifier
4841 ( expression-list [opt] )
4842 typename :: [opt] nested-name-specifier template [opt] template-id
4843 ( expression-list [opt] )
4844 postfix-expression . template [opt] id-expression
4845 postfix-expression -> template [opt] id-expression
4846 postfix-expression . pseudo-destructor-name
4847 postfix-expression -> pseudo-destructor-name
4848 postfix-expression ++
4849 postfix-expression --
4850 dynamic_cast < type-id > ( expression )
4851 static_cast < type-id > ( expression )
4852 reinterpret_cast < type-id > ( expression )
4853 const_cast < type-id > ( expression )
4854 typeid ( expression )
4855 typeid ( type-id )
4857 GNU Extension:
4859 postfix-expression:
4860 ( type-id ) { initializer-list , [opt] }
4862 This extension is a GNU version of the C99 compound-literal
4863 construct. (The C99 grammar uses `type-name' instead of `type-id',
4864 but they are essentially the same concept.)
4866 If ADDRESS_P is true, the postfix expression is the operand of the
4867 `&' operator. CAST_P is true if this expression is the target of a
4868 cast.
4870 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4871 class member access expressions [expr.ref].
4873 Returns a representation of the expression. */
4875 static tree
4876 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4877 bool member_access_only_p,
4878 cp_id_kind * pidk_return)
4880 cp_token *token;
4881 enum rid keyword;
4882 cp_id_kind idk = CP_ID_KIND_NONE;
4883 tree postfix_expression = NULL_TREE;
4884 bool is_member_access = false;
4886 /* Peek at the next token. */
4887 token = cp_lexer_peek_token (parser->lexer);
4888 /* Some of the productions are determined by keywords. */
4889 keyword = token->keyword;
4890 switch (keyword)
4892 case RID_DYNCAST:
4893 case RID_STATCAST:
4894 case RID_REINTCAST:
4895 case RID_CONSTCAST:
4897 tree type;
4898 tree expression;
4899 const char *saved_message;
4901 /* All of these can be handled in the same way from the point
4902 of view of parsing. Begin by consuming the token
4903 identifying the cast. */
4904 cp_lexer_consume_token (parser->lexer);
4906 /* New types cannot be defined in the cast. */
4907 saved_message = parser->type_definition_forbidden_message;
4908 parser->type_definition_forbidden_message
4909 = G_("types may not be defined in casts");
4911 /* Look for the opening `<'. */
4912 cp_parser_require (parser, CPP_LESS, RT_LESS);
4913 /* Parse the type to which we are casting. */
4914 type = cp_parser_type_id (parser);
4915 /* Look for the closing `>'. */
4916 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4917 /* Restore the old message. */
4918 parser->type_definition_forbidden_message = saved_message;
4920 /* And the expression which is being cast. */
4921 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4922 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4923 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4925 /* Only type conversions to integral or enumeration types
4926 can be used in constant-expressions. */
4927 if (!cast_valid_in_integral_constant_expression_p (type)
4928 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4929 return error_mark_node;
4931 switch (keyword)
4933 case RID_DYNCAST:
4934 postfix_expression
4935 = build_dynamic_cast (type, expression, tf_warning_or_error);
4936 break;
4937 case RID_STATCAST:
4938 postfix_expression
4939 = build_static_cast (type, expression, tf_warning_or_error);
4940 break;
4941 case RID_REINTCAST:
4942 postfix_expression
4943 = build_reinterpret_cast (type, expression,
4944 tf_warning_or_error);
4945 break;
4946 case RID_CONSTCAST:
4947 postfix_expression
4948 = build_const_cast (type, expression, tf_warning_or_error);
4949 break;
4950 default:
4951 gcc_unreachable ();
4954 break;
4956 case RID_TYPEID:
4958 tree type;
4959 const char *saved_message;
4960 bool saved_in_type_id_in_expr_p;
4962 /* Consume the `typeid' token. */
4963 cp_lexer_consume_token (parser->lexer);
4964 /* Look for the `(' token. */
4965 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4966 /* Types cannot be defined in a `typeid' expression. */
4967 saved_message = parser->type_definition_forbidden_message;
4968 parser->type_definition_forbidden_message
4969 = G_("types may not be defined in a %<typeid%> expression");
4970 /* We can't be sure yet whether we're looking at a type-id or an
4971 expression. */
4972 cp_parser_parse_tentatively (parser);
4973 /* Try a type-id first. */
4974 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4975 parser->in_type_id_in_expr_p = true;
4976 type = cp_parser_type_id (parser);
4977 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4978 /* Look for the `)' token. Otherwise, we can't be sure that
4979 we're not looking at an expression: consider `typeid (int
4980 (3))', for example. */
4981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4982 /* If all went well, simply lookup the type-id. */
4983 if (cp_parser_parse_definitely (parser))
4984 postfix_expression = get_typeid (type);
4985 /* Otherwise, fall back to the expression variant. */
4986 else
4988 tree expression;
4990 /* Look for an expression. */
4991 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4992 /* Compute its typeid. */
4993 postfix_expression = build_typeid (expression);
4994 /* Look for the `)' token. */
4995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4997 /* Restore the saved message. */
4998 parser->type_definition_forbidden_message = saved_message;
4999 /* `typeid' may not appear in an integral constant expression. */
5000 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5001 return error_mark_node;
5003 break;
5005 case RID_TYPENAME:
5007 tree type;
5008 /* The syntax permitted here is the same permitted for an
5009 elaborated-type-specifier. */
5010 type = cp_parser_elaborated_type_specifier (parser,
5011 /*is_friend=*/false,
5012 /*is_declaration=*/false);
5013 postfix_expression = cp_parser_functional_cast (parser, type);
5015 break;
5017 default:
5019 tree type;
5021 /* If the next thing is a simple-type-specifier, we may be
5022 looking at a functional cast. We could also be looking at
5023 an id-expression. So, we try the functional cast, and if
5024 that doesn't work we fall back to the primary-expression. */
5025 cp_parser_parse_tentatively (parser);
5026 /* Look for the simple-type-specifier. */
5027 type = cp_parser_simple_type_specifier (parser,
5028 /*decl_specs=*/NULL,
5029 CP_PARSER_FLAGS_NONE);
5030 /* Parse the cast itself. */
5031 if (!cp_parser_error_occurred (parser))
5032 postfix_expression
5033 = cp_parser_functional_cast (parser, type);
5034 /* If that worked, we're done. */
5035 if (cp_parser_parse_definitely (parser))
5036 break;
5038 /* If the functional-cast didn't work out, try a
5039 compound-literal. */
5040 if (cp_parser_allow_gnu_extensions_p (parser)
5041 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5043 VEC(constructor_elt,gc) *initializer_list = NULL;
5044 bool saved_in_type_id_in_expr_p;
5046 cp_parser_parse_tentatively (parser);
5047 /* Consume the `('. */
5048 cp_lexer_consume_token (parser->lexer);
5049 /* Parse the type. */
5050 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5051 parser->in_type_id_in_expr_p = true;
5052 type = cp_parser_type_id (parser);
5053 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5054 /* Look for the `)'. */
5055 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5056 /* Look for the `{'. */
5057 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5058 /* If things aren't going well, there's no need to
5059 keep going. */
5060 if (!cp_parser_error_occurred (parser))
5062 bool non_constant_p;
5063 /* Parse the initializer-list. */
5064 initializer_list
5065 = cp_parser_initializer_list (parser, &non_constant_p);
5066 /* Allow a trailing `,'. */
5067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5068 cp_lexer_consume_token (parser->lexer);
5069 /* Look for the final `}'. */
5070 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5072 /* If that worked, we're definitely looking at a
5073 compound-literal expression. */
5074 if (cp_parser_parse_definitely (parser))
5076 /* Warn the user that a compound literal is not
5077 allowed in standard C++. */
5078 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5079 /* For simplicity, we disallow compound literals in
5080 constant-expressions. We could
5081 allow compound literals of integer type, whose
5082 initializer was a constant, in constant
5083 expressions. Permitting that usage, as a further
5084 extension, would not change the meaning of any
5085 currently accepted programs. (Of course, as
5086 compound literals are not part of ISO C++, the
5087 standard has nothing to say.) */
5088 if (cp_parser_non_integral_constant_expression (parser,
5089 NIC_NCC))
5091 postfix_expression = error_mark_node;
5092 break;
5094 /* Form the representation of the compound-literal. */
5095 postfix_expression
5096 = (finish_compound_literal
5097 (type, build_constructor (init_list_type_node,
5098 initializer_list)));
5099 break;
5103 /* It must be a primary-expression. */
5104 postfix_expression
5105 = cp_parser_primary_expression (parser, address_p, cast_p,
5106 /*template_arg_p=*/false,
5107 &idk);
5109 break;
5112 /* Keep looping until the postfix-expression is complete. */
5113 while (true)
5115 if (idk == CP_ID_KIND_UNQUALIFIED
5116 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5117 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5118 /* It is not a Koenig lookup function call. */
5119 postfix_expression
5120 = unqualified_name_lookup_error (postfix_expression);
5122 /* Peek at the next token. */
5123 token = cp_lexer_peek_token (parser->lexer);
5125 switch (token->type)
5127 case CPP_OPEN_SQUARE:
5128 postfix_expression
5129 = cp_parser_postfix_open_square_expression (parser,
5130 postfix_expression,
5131 false);
5132 idk = CP_ID_KIND_NONE;
5133 is_member_access = false;
5134 break;
5136 case CPP_OPEN_PAREN:
5137 /* postfix-expression ( expression-list [opt] ) */
5139 bool koenig_p;
5140 bool is_builtin_constant_p;
5141 bool saved_integral_constant_expression_p = false;
5142 bool saved_non_integral_constant_expression_p = false;
5143 VEC(tree,gc) *args;
5145 is_member_access = false;
5147 is_builtin_constant_p
5148 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5149 if (is_builtin_constant_p)
5151 /* The whole point of __builtin_constant_p is to allow
5152 non-constant expressions to appear as arguments. */
5153 saved_integral_constant_expression_p
5154 = parser->integral_constant_expression_p;
5155 saved_non_integral_constant_expression_p
5156 = parser->non_integral_constant_expression_p;
5157 parser->integral_constant_expression_p = false;
5159 args = (cp_parser_parenthesized_expression_list
5160 (parser, non_attr,
5161 /*cast_p=*/false, /*allow_expansion_p=*/true,
5162 /*non_constant_p=*/NULL));
5163 if (is_builtin_constant_p)
5165 parser->integral_constant_expression_p
5166 = saved_integral_constant_expression_p;
5167 parser->non_integral_constant_expression_p
5168 = saved_non_integral_constant_expression_p;
5171 if (args == NULL)
5173 postfix_expression = error_mark_node;
5174 break;
5177 /* Function calls are not permitted in
5178 constant-expressions. */
5179 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5180 && cp_parser_non_integral_constant_expression (parser,
5181 NIC_FUNC_CALL))
5183 postfix_expression = error_mark_node;
5184 release_tree_vector (args);
5185 break;
5188 koenig_p = false;
5189 if (idk == CP_ID_KIND_UNQUALIFIED
5190 || idk == CP_ID_KIND_TEMPLATE_ID)
5192 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5194 if (!VEC_empty (tree, args))
5196 koenig_p = true;
5197 if (!any_type_dependent_arguments_p (args))
5198 postfix_expression
5199 = perform_koenig_lookup (postfix_expression, args,
5200 /*include_std=*/false);
5202 else
5203 postfix_expression
5204 = unqualified_fn_lookup_error (postfix_expression);
5206 /* We do not perform argument-dependent lookup if
5207 normal lookup finds a non-function, in accordance
5208 with the expected resolution of DR 218. */
5209 else if (!VEC_empty (tree, args)
5210 && is_overloaded_fn (postfix_expression))
5212 tree fn = get_first_fn (postfix_expression);
5213 fn = STRIP_TEMPLATE (fn);
5215 /* Do not do argument dependent lookup if regular
5216 lookup finds a member function or a block-scope
5217 function declaration. [basic.lookup.argdep]/3 */
5218 if (!DECL_FUNCTION_MEMBER_P (fn)
5219 && !DECL_LOCAL_FUNCTION_P (fn))
5221 koenig_p = true;
5222 if (!any_type_dependent_arguments_p (args))
5223 postfix_expression
5224 = perform_koenig_lookup (postfix_expression, args,
5225 /*include_std=*/false);
5230 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5232 tree instance = TREE_OPERAND (postfix_expression, 0);
5233 tree fn = TREE_OPERAND (postfix_expression, 1);
5235 if (processing_template_decl
5236 && (type_dependent_expression_p (instance)
5237 || (!BASELINK_P (fn)
5238 && TREE_CODE (fn) != FIELD_DECL)
5239 || type_dependent_expression_p (fn)
5240 || any_type_dependent_arguments_p (args)))
5242 postfix_expression
5243 = build_nt_call_vec (postfix_expression, args);
5244 release_tree_vector (args);
5245 break;
5248 if (BASELINK_P (fn))
5250 postfix_expression
5251 = (build_new_method_call
5252 (instance, fn, &args, NULL_TREE,
5253 (idk == CP_ID_KIND_QUALIFIED
5254 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5255 /*fn_p=*/NULL,
5256 tf_warning_or_error));
5258 else
5259 postfix_expression
5260 = finish_call_expr (postfix_expression, &args,
5261 /*disallow_virtual=*/false,
5262 /*koenig_p=*/false,
5263 tf_warning_or_error);
5265 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5266 || TREE_CODE (postfix_expression) == MEMBER_REF
5267 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5268 postfix_expression = (build_offset_ref_call_from_tree
5269 (postfix_expression, &args));
5270 else if (idk == CP_ID_KIND_QUALIFIED)
5271 /* A call to a static class member, or a namespace-scope
5272 function. */
5273 postfix_expression
5274 = finish_call_expr (postfix_expression, &args,
5275 /*disallow_virtual=*/true,
5276 koenig_p,
5277 tf_warning_or_error);
5278 else
5279 /* All other function calls. */
5280 postfix_expression
5281 = finish_call_expr (postfix_expression, &args,
5282 /*disallow_virtual=*/false,
5283 koenig_p,
5284 tf_warning_or_error);
5286 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5287 idk = CP_ID_KIND_NONE;
5289 release_tree_vector (args);
5291 break;
5293 case CPP_DOT:
5294 case CPP_DEREF:
5295 /* postfix-expression . template [opt] id-expression
5296 postfix-expression . pseudo-destructor-name
5297 postfix-expression -> template [opt] id-expression
5298 postfix-expression -> pseudo-destructor-name */
5300 /* Consume the `.' or `->' operator. */
5301 cp_lexer_consume_token (parser->lexer);
5303 postfix_expression
5304 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5305 postfix_expression,
5306 false, &idk,
5307 token->location);
5309 is_member_access = true;
5310 break;
5312 case CPP_PLUS_PLUS:
5313 /* postfix-expression ++ */
5314 /* Consume the `++' token. */
5315 cp_lexer_consume_token (parser->lexer);
5316 /* Generate a representation for the complete expression. */
5317 postfix_expression
5318 = finish_increment_expr (postfix_expression,
5319 POSTINCREMENT_EXPR);
5320 /* Increments may not appear in constant-expressions. */
5321 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5322 postfix_expression = error_mark_node;
5323 idk = CP_ID_KIND_NONE;
5324 is_member_access = false;
5325 break;
5327 case CPP_MINUS_MINUS:
5328 /* postfix-expression -- */
5329 /* Consume the `--' token. */
5330 cp_lexer_consume_token (parser->lexer);
5331 /* Generate a representation for the complete expression. */
5332 postfix_expression
5333 = finish_increment_expr (postfix_expression,
5334 POSTDECREMENT_EXPR);
5335 /* Decrements may not appear in constant-expressions. */
5336 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5337 postfix_expression = error_mark_node;
5338 idk = CP_ID_KIND_NONE;
5339 is_member_access = false;
5340 break;
5342 default:
5343 if (pidk_return != NULL)
5344 * pidk_return = idk;
5345 if (member_access_only_p)
5346 return is_member_access? postfix_expression : error_mark_node;
5347 else
5348 return postfix_expression;
5352 /* We should never get here. */
5353 gcc_unreachable ();
5354 return error_mark_node;
5357 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5358 by cp_parser_builtin_offsetof. We're looking for
5360 postfix-expression [ expression ]
5362 FOR_OFFSETOF is set if we're being called in that context, which
5363 changes how we deal with integer constant expressions. */
5365 static tree
5366 cp_parser_postfix_open_square_expression (cp_parser *parser,
5367 tree postfix_expression,
5368 bool for_offsetof)
5370 tree index;
5372 /* Consume the `[' token. */
5373 cp_lexer_consume_token (parser->lexer);
5375 /* Parse the index expression. */
5376 /* ??? For offsetof, there is a question of what to allow here. If
5377 offsetof is not being used in an integral constant expression context,
5378 then we *could* get the right answer by computing the value at runtime.
5379 If we are in an integral constant expression context, then we might
5380 could accept any constant expression; hard to say without analysis.
5381 Rather than open the barn door too wide right away, allow only integer
5382 constant expressions here. */
5383 if (for_offsetof)
5384 index = cp_parser_constant_expression (parser, false, NULL);
5385 else
5386 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5388 /* Look for the closing `]'. */
5389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5391 /* Build the ARRAY_REF. */
5392 postfix_expression = grok_array_decl (postfix_expression, index);
5394 /* When not doing offsetof, array references are not permitted in
5395 constant-expressions. */
5396 if (!for_offsetof
5397 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5398 postfix_expression = error_mark_node;
5400 return postfix_expression;
5403 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5404 by cp_parser_builtin_offsetof. We're looking for
5406 postfix-expression . template [opt] id-expression
5407 postfix-expression . pseudo-destructor-name
5408 postfix-expression -> template [opt] id-expression
5409 postfix-expression -> pseudo-destructor-name
5411 FOR_OFFSETOF is set if we're being called in that context. That sorta
5412 limits what of the above we'll actually accept, but nevermind.
5413 TOKEN_TYPE is the "." or "->" token, which will already have been
5414 removed from the stream. */
5416 static tree
5417 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5418 enum cpp_ttype token_type,
5419 tree postfix_expression,
5420 bool for_offsetof, cp_id_kind *idk,
5421 location_t location)
5423 tree name;
5424 bool dependent_p;
5425 bool pseudo_destructor_p;
5426 tree scope = NULL_TREE;
5428 /* If this is a `->' operator, dereference the pointer. */
5429 if (token_type == CPP_DEREF)
5430 postfix_expression = build_x_arrow (postfix_expression);
5431 /* Check to see whether or not the expression is type-dependent. */
5432 dependent_p = type_dependent_expression_p (postfix_expression);
5433 /* The identifier following the `->' or `.' is not qualified. */
5434 parser->scope = NULL_TREE;
5435 parser->qualifying_scope = NULL_TREE;
5436 parser->object_scope = NULL_TREE;
5437 *idk = CP_ID_KIND_NONE;
5439 /* Enter the scope corresponding to the type of the object
5440 given by the POSTFIX_EXPRESSION. */
5441 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5443 scope = TREE_TYPE (postfix_expression);
5444 /* According to the standard, no expression should ever have
5445 reference type. Unfortunately, we do not currently match
5446 the standard in this respect in that our internal representation
5447 of an expression may have reference type even when the standard
5448 says it does not. Therefore, we have to manually obtain the
5449 underlying type here. */
5450 scope = non_reference (scope);
5451 /* The type of the POSTFIX_EXPRESSION must be complete. */
5452 if (scope == unknown_type_node)
5454 error_at (location, "%qE does not have class type",
5455 postfix_expression);
5456 scope = NULL_TREE;
5458 else
5459 scope = complete_type_or_else (scope, NULL_TREE);
5460 /* Let the name lookup machinery know that we are processing a
5461 class member access expression. */
5462 parser->context->object_type = scope;
5463 /* If something went wrong, we want to be able to discern that case,
5464 as opposed to the case where there was no SCOPE due to the type
5465 of expression being dependent. */
5466 if (!scope)
5467 scope = error_mark_node;
5468 /* If the SCOPE was erroneous, make the various semantic analysis
5469 functions exit quickly -- and without issuing additional error
5470 messages. */
5471 if (scope == error_mark_node)
5472 postfix_expression = error_mark_node;
5475 /* Assume this expression is not a pseudo-destructor access. */
5476 pseudo_destructor_p = false;
5478 /* If the SCOPE is a scalar type, then, if this is a valid program,
5479 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5480 is type dependent, it can be pseudo-destructor-name or something else.
5481 Try to parse it as pseudo-destructor-name first. */
5482 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5484 tree s;
5485 tree type;
5487 cp_parser_parse_tentatively (parser);
5488 /* Parse the pseudo-destructor-name. */
5489 s = NULL_TREE;
5490 cp_parser_pseudo_destructor_name (parser, &s, &type);
5491 if (dependent_p
5492 && (cp_parser_error_occurred (parser)
5493 || TREE_CODE (type) != TYPE_DECL
5494 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5495 cp_parser_abort_tentative_parse (parser);
5496 else if (cp_parser_parse_definitely (parser))
5498 pseudo_destructor_p = true;
5499 postfix_expression
5500 = finish_pseudo_destructor_expr (postfix_expression,
5501 s, TREE_TYPE (type));
5505 if (!pseudo_destructor_p)
5507 /* If the SCOPE is not a scalar type, we are looking at an
5508 ordinary class member access expression, rather than a
5509 pseudo-destructor-name. */
5510 bool template_p;
5511 cp_token *token = cp_lexer_peek_token (parser->lexer);
5512 /* Parse the id-expression. */
5513 name = (cp_parser_id_expression
5514 (parser,
5515 cp_parser_optional_template_keyword (parser),
5516 /*check_dependency_p=*/true,
5517 &template_p,
5518 /*declarator_p=*/false,
5519 /*optional_p=*/false));
5520 /* In general, build a SCOPE_REF if the member name is qualified.
5521 However, if the name was not dependent and has already been
5522 resolved; there is no need to build the SCOPE_REF. For example;
5524 struct X { void f(); };
5525 template <typename T> void f(T* t) { t->X::f(); }
5527 Even though "t" is dependent, "X::f" is not and has been resolved
5528 to a BASELINK; there is no need to include scope information. */
5530 /* But we do need to remember that there was an explicit scope for
5531 virtual function calls. */
5532 if (parser->scope)
5533 *idk = CP_ID_KIND_QUALIFIED;
5535 /* If the name is a template-id that names a type, we will get a
5536 TYPE_DECL here. That is invalid code. */
5537 if (TREE_CODE (name) == TYPE_DECL)
5539 error_at (token->location, "invalid use of %qD", name);
5540 postfix_expression = error_mark_node;
5542 else
5544 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5546 name = build_qualified_name (/*type=*/NULL_TREE,
5547 parser->scope,
5548 name,
5549 template_p);
5550 parser->scope = NULL_TREE;
5551 parser->qualifying_scope = NULL_TREE;
5552 parser->object_scope = NULL_TREE;
5554 if (scope && name && BASELINK_P (name))
5555 adjust_result_of_qualified_name_lookup
5556 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5557 postfix_expression
5558 = finish_class_member_access_expr (postfix_expression, name,
5559 template_p,
5560 tf_warning_or_error);
5564 /* We no longer need to look up names in the scope of the object on
5565 the left-hand side of the `.' or `->' operator. */
5566 parser->context->object_type = NULL_TREE;
5568 /* Outside of offsetof, these operators may not appear in
5569 constant-expressions. */
5570 if (!for_offsetof
5571 && (cp_parser_non_integral_constant_expression
5572 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5573 postfix_expression = error_mark_node;
5575 return postfix_expression;
5578 /* Parse a parenthesized expression-list.
5580 expression-list:
5581 assignment-expression
5582 expression-list, assignment-expression
5584 attribute-list:
5585 expression-list
5586 identifier
5587 identifier, expression-list
5589 CAST_P is true if this expression is the target of a cast.
5591 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5592 argument pack.
5594 Returns a vector of trees. Each element is a representation of an
5595 assignment-expression. NULL is returned if the ( and or ) are
5596 missing. An empty, but allocated, vector is returned on no
5597 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5598 if we are parsing an attribute list for an attribute that wants a
5599 plain identifier argument, normal_attr for an attribute that wants
5600 an expression, or non_attr if we aren't parsing an attribute list. If
5601 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5602 not all of the expressions in the list were constant. */
5604 static VEC(tree,gc) *
5605 cp_parser_parenthesized_expression_list (cp_parser* parser,
5606 int is_attribute_list,
5607 bool cast_p,
5608 bool allow_expansion_p,
5609 bool *non_constant_p)
5611 VEC(tree,gc) *expression_list;
5612 bool fold_expr_p = is_attribute_list != non_attr;
5613 tree identifier = NULL_TREE;
5614 bool saved_greater_than_is_operator_p;
5616 /* Assume all the expressions will be constant. */
5617 if (non_constant_p)
5618 *non_constant_p = false;
5620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5621 return NULL;
5623 expression_list = make_tree_vector ();
5625 /* Within a parenthesized expression, a `>' token is always
5626 the greater-than operator. */
5627 saved_greater_than_is_operator_p
5628 = parser->greater_than_is_operator_p;
5629 parser->greater_than_is_operator_p = true;
5631 /* Consume expressions until there are no more. */
5632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5633 while (true)
5635 tree expr;
5637 /* At the beginning of attribute lists, check to see if the
5638 next token is an identifier. */
5639 if (is_attribute_list == id_attr
5640 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5642 cp_token *token;
5644 /* Consume the identifier. */
5645 token = cp_lexer_consume_token (parser->lexer);
5646 /* Save the identifier. */
5647 identifier = token->u.value;
5649 else
5651 bool expr_non_constant_p;
5653 /* Parse the next assignment-expression. */
5654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5656 /* A braced-init-list. */
5657 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5658 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5659 if (non_constant_p && expr_non_constant_p)
5660 *non_constant_p = true;
5662 else if (non_constant_p)
5664 expr = (cp_parser_constant_expression
5665 (parser, /*allow_non_constant_p=*/true,
5666 &expr_non_constant_p));
5667 if (expr_non_constant_p)
5668 *non_constant_p = true;
5670 else
5671 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5673 if (fold_expr_p)
5674 expr = fold_non_dependent_expr (expr);
5676 /* If we have an ellipsis, then this is an expression
5677 expansion. */
5678 if (allow_expansion_p
5679 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5681 /* Consume the `...'. */
5682 cp_lexer_consume_token (parser->lexer);
5684 /* Build the argument pack. */
5685 expr = make_pack_expansion (expr);
5688 /* Add it to the list. We add error_mark_node
5689 expressions to the list, so that we can still tell if
5690 the correct form for a parenthesized expression-list
5691 is found. That gives better errors. */
5692 VEC_safe_push (tree, gc, expression_list, expr);
5694 if (expr == error_mark_node)
5695 goto skip_comma;
5698 /* After the first item, attribute lists look the same as
5699 expression lists. */
5700 is_attribute_list = non_attr;
5702 get_comma:;
5703 /* If the next token isn't a `,', then we are done. */
5704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5705 break;
5707 /* Otherwise, consume the `,' and keep going. */
5708 cp_lexer_consume_token (parser->lexer);
5711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5713 int ending;
5715 skip_comma:;
5716 /* We try and resync to an unnested comma, as that will give the
5717 user better diagnostics. */
5718 ending = cp_parser_skip_to_closing_parenthesis (parser,
5719 /*recovering=*/true,
5720 /*or_comma=*/true,
5721 /*consume_paren=*/true);
5722 if (ending < 0)
5723 goto get_comma;
5724 if (!ending)
5726 parser->greater_than_is_operator_p
5727 = saved_greater_than_is_operator_p;
5728 return NULL;
5732 parser->greater_than_is_operator_p
5733 = saved_greater_than_is_operator_p;
5735 if (identifier)
5736 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5738 return expression_list;
5741 /* Parse a pseudo-destructor-name.
5743 pseudo-destructor-name:
5744 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5745 :: [opt] nested-name-specifier template template-id :: ~ type-name
5746 :: [opt] nested-name-specifier [opt] ~ type-name
5748 If either of the first two productions is used, sets *SCOPE to the
5749 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5750 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5751 or ERROR_MARK_NODE if the parse fails. */
5753 static void
5754 cp_parser_pseudo_destructor_name (cp_parser* parser,
5755 tree* scope,
5756 tree* type)
5758 bool nested_name_specifier_p;
5760 /* Assume that things will not work out. */
5761 *type = error_mark_node;
5763 /* Look for the optional `::' operator. */
5764 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5765 /* Look for the optional nested-name-specifier. */
5766 nested_name_specifier_p
5767 = (cp_parser_nested_name_specifier_opt (parser,
5768 /*typename_keyword_p=*/false,
5769 /*check_dependency_p=*/true,
5770 /*type_p=*/false,
5771 /*is_declaration=*/false)
5772 != NULL_TREE);
5773 /* Now, if we saw a nested-name-specifier, we might be doing the
5774 second production. */
5775 if (nested_name_specifier_p
5776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5778 /* Consume the `template' keyword. */
5779 cp_lexer_consume_token (parser->lexer);
5780 /* Parse the template-id. */
5781 cp_parser_template_id (parser,
5782 /*template_keyword_p=*/true,
5783 /*check_dependency_p=*/false,
5784 /*is_declaration=*/true);
5785 /* Look for the `::' token. */
5786 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5788 /* If the next token is not a `~', then there might be some
5789 additional qualification. */
5790 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5792 /* At this point, we're looking for "type-name :: ~". The type-name
5793 must not be a class-name, since this is a pseudo-destructor. So,
5794 it must be either an enum-name, or a typedef-name -- both of which
5795 are just identifiers. So, we peek ahead to check that the "::"
5796 and "~" tokens are present; if they are not, then we can avoid
5797 calling type_name. */
5798 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5799 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5800 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5802 cp_parser_error (parser, "non-scalar type");
5803 return;
5806 /* Look for the type-name. */
5807 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5808 if (*scope == error_mark_node)
5809 return;
5811 /* Look for the `::' token. */
5812 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5814 else
5815 *scope = NULL_TREE;
5817 /* Look for the `~'. */
5818 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5819 /* Look for the type-name again. We are not responsible for
5820 checking that it matches the first type-name. */
5821 *type = cp_parser_nonclass_name (parser);
5824 /* Parse a unary-expression.
5826 unary-expression:
5827 postfix-expression
5828 ++ cast-expression
5829 -- cast-expression
5830 unary-operator cast-expression
5831 sizeof unary-expression
5832 sizeof ( type-id )
5833 new-expression
5834 delete-expression
5836 GNU Extensions:
5838 unary-expression:
5839 __extension__ cast-expression
5840 __alignof__ unary-expression
5841 __alignof__ ( type-id )
5842 __real__ cast-expression
5843 __imag__ cast-expression
5844 && identifier
5846 ADDRESS_P is true iff the unary-expression is appearing as the
5847 operand of the `&' operator. CAST_P is true if this expression is
5848 the target of a cast.
5850 Returns a representation of the expression. */
5852 static tree
5853 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5854 cp_id_kind * pidk)
5856 cp_token *token;
5857 enum tree_code unary_operator;
5859 /* Peek at the next token. */
5860 token = cp_lexer_peek_token (parser->lexer);
5861 /* Some keywords give away the kind of expression. */
5862 if (token->type == CPP_KEYWORD)
5864 enum rid keyword = token->keyword;
5866 switch (keyword)
5868 case RID_ALIGNOF:
5869 case RID_SIZEOF:
5871 tree operand;
5872 enum tree_code op;
5874 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5875 /* Consume the token. */
5876 cp_lexer_consume_token (parser->lexer);
5877 /* Parse the operand. */
5878 operand = cp_parser_sizeof_operand (parser, keyword);
5880 if (TYPE_P (operand))
5881 return cxx_sizeof_or_alignof_type (operand, op, true);
5882 else
5883 return cxx_sizeof_or_alignof_expr (operand, op, true);
5886 case RID_NEW:
5887 return cp_parser_new_expression (parser);
5889 case RID_DELETE:
5890 return cp_parser_delete_expression (parser);
5892 case RID_EXTENSION:
5894 /* The saved value of the PEDANTIC flag. */
5895 int saved_pedantic;
5896 tree expr;
5898 /* Save away the PEDANTIC flag. */
5899 cp_parser_extension_opt (parser, &saved_pedantic);
5900 /* Parse the cast-expression. */
5901 expr = cp_parser_simple_cast_expression (parser);
5902 /* Restore the PEDANTIC flag. */
5903 pedantic = saved_pedantic;
5905 return expr;
5908 case RID_REALPART:
5909 case RID_IMAGPART:
5911 tree expression;
5913 /* Consume the `__real__' or `__imag__' token. */
5914 cp_lexer_consume_token (parser->lexer);
5915 /* Parse the cast-expression. */
5916 expression = cp_parser_simple_cast_expression (parser);
5917 /* Create the complete representation. */
5918 return build_x_unary_op ((keyword == RID_REALPART
5919 ? REALPART_EXPR : IMAGPART_EXPR),
5920 expression,
5921 tf_warning_or_error);
5923 break;
5925 case RID_NOEXCEPT:
5927 tree expr;
5928 const char *saved_message;
5929 bool saved_integral_constant_expression_p;
5930 bool saved_non_integral_constant_expression_p;
5931 bool saved_greater_than_is_operator_p;
5933 cp_lexer_consume_token (parser->lexer);
5934 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5936 saved_message = parser->type_definition_forbidden_message;
5937 parser->type_definition_forbidden_message
5938 = G_("types may not be defined in %<noexcept%> expressions");
5940 saved_integral_constant_expression_p
5941 = parser->integral_constant_expression_p;
5942 saved_non_integral_constant_expression_p
5943 = parser->non_integral_constant_expression_p;
5944 parser->integral_constant_expression_p = false;
5946 saved_greater_than_is_operator_p
5947 = parser->greater_than_is_operator_p;
5948 parser->greater_than_is_operator_p = true;
5950 ++cp_unevaluated_operand;
5951 ++c_inhibit_evaluation_warnings;
5952 expr = cp_parser_expression (parser, false, NULL);
5953 --c_inhibit_evaluation_warnings;
5954 --cp_unevaluated_operand;
5956 parser->greater_than_is_operator_p
5957 = saved_greater_than_is_operator_p;
5959 parser->integral_constant_expression_p
5960 = saved_integral_constant_expression_p;
5961 parser->non_integral_constant_expression_p
5962 = saved_non_integral_constant_expression_p;
5964 parser->type_definition_forbidden_message = saved_message;
5966 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5967 return finish_noexcept_expr (expr, tf_warning_or_error);
5970 default:
5971 break;
5975 /* Look for the `:: new' and `:: delete', which also signal the
5976 beginning of a new-expression, or delete-expression,
5977 respectively. If the next token is `::', then it might be one of
5978 these. */
5979 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5981 enum rid keyword;
5983 /* See if the token after the `::' is one of the keywords in
5984 which we're interested. */
5985 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5986 /* If it's `new', we have a new-expression. */
5987 if (keyword == RID_NEW)
5988 return cp_parser_new_expression (parser);
5989 /* Similarly, for `delete'. */
5990 else if (keyword == RID_DELETE)
5991 return cp_parser_delete_expression (parser);
5994 /* Look for a unary operator. */
5995 unary_operator = cp_parser_unary_operator (token);
5996 /* The `++' and `--' operators can be handled similarly, even though
5997 they are not technically unary-operators in the grammar. */
5998 if (unary_operator == ERROR_MARK)
6000 if (token->type == CPP_PLUS_PLUS)
6001 unary_operator = PREINCREMENT_EXPR;
6002 else if (token->type == CPP_MINUS_MINUS)
6003 unary_operator = PREDECREMENT_EXPR;
6004 /* Handle the GNU address-of-label extension. */
6005 else if (cp_parser_allow_gnu_extensions_p (parser)
6006 && token->type == CPP_AND_AND)
6008 tree identifier;
6009 tree expression;
6010 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6012 /* Consume the '&&' token. */
6013 cp_lexer_consume_token (parser->lexer);
6014 /* Look for the identifier. */
6015 identifier = cp_parser_identifier (parser);
6016 /* Create an expression representing the address. */
6017 expression = finish_label_address_expr (identifier, loc);
6018 if (cp_parser_non_integral_constant_expression (parser,
6019 NIC_ADDR_LABEL))
6020 expression = error_mark_node;
6021 return expression;
6024 if (unary_operator != ERROR_MARK)
6026 tree cast_expression;
6027 tree expression = error_mark_node;
6028 non_integral_constant non_constant_p = NIC_NONE;
6030 /* Consume the operator token. */
6031 token = cp_lexer_consume_token (parser->lexer);
6032 /* Parse the cast-expression. */
6033 cast_expression
6034 = cp_parser_cast_expression (parser,
6035 unary_operator == ADDR_EXPR,
6036 /*cast_p=*/false, pidk);
6037 /* Now, build an appropriate representation. */
6038 switch (unary_operator)
6040 case INDIRECT_REF:
6041 non_constant_p = NIC_STAR;
6042 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6043 tf_warning_or_error);
6044 break;
6046 case ADDR_EXPR:
6047 non_constant_p = NIC_ADDR;
6048 /* Fall through. */
6049 case BIT_NOT_EXPR:
6050 expression = build_x_unary_op (unary_operator, cast_expression,
6051 tf_warning_or_error);
6052 break;
6054 case PREINCREMENT_EXPR:
6055 case PREDECREMENT_EXPR:
6056 non_constant_p = unary_operator == PREINCREMENT_EXPR
6057 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6058 /* Fall through. */
6059 case UNARY_PLUS_EXPR:
6060 case NEGATE_EXPR:
6061 case TRUTH_NOT_EXPR:
6062 expression = finish_unary_op_expr (unary_operator, cast_expression);
6063 break;
6065 default:
6066 gcc_unreachable ();
6069 if (non_constant_p != NIC_NONE
6070 && cp_parser_non_integral_constant_expression (parser,
6071 non_constant_p))
6072 expression = error_mark_node;
6074 return expression;
6077 return cp_parser_postfix_expression (parser, address_p, cast_p,
6078 /*member_access_only_p=*/false,
6079 pidk);
6082 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6083 unary-operator, the corresponding tree code is returned. */
6085 static enum tree_code
6086 cp_parser_unary_operator (cp_token* token)
6088 switch (token->type)
6090 case CPP_MULT:
6091 return INDIRECT_REF;
6093 case CPP_AND:
6094 return ADDR_EXPR;
6096 case CPP_PLUS:
6097 return UNARY_PLUS_EXPR;
6099 case CPP_MINUS:
6100 return NEGATE_EXPR;
6102 case CPP_NOT:
6103 return TRUTH_NOT_EXPR;
6105 case CPP_COMPL:
6106 return BIT_NOT_EXPR;
6108 default:
6109 return ERROR_MARK;
6113 /* Parse a new-expression.
6115 new-expression:
6116 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6117 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6119 Returns a representation of the expression. */
6121 static tree
6122 cp_parser_new_expression (cp_parser* parser)
6124 bool global_scope_p;
6125 VEC(tree,gc) *placement;
6126 tree type;
6127 VEC(tree,gc) *initializer;
6128 tree nelts;
6129 tree ret;
6131 /* Look for the optional `::' operator. */
6132 global_scope_p
6133 = (cp_parser_global_scope_opt (parser,
6134 /*current_scope_valid_p=*/false)
6135 != NULL_TREE);
6136 /* Look for the `new' operator. */
6137 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6138 /* There's no easy way to tell a new-placement from the
6139 `( type-id )' construct. */
6140 cp_parser_parse_tentatively (parser);
6141 /* Look for a new-placement. */
6142 placement = cp_parser_new_placement (parser);
6143 /* If that didn't work out, there's no new-placement. */
6144 if (!cp_parser_parse_definitely (parser))
6146 if (placement != NULL)
6147 release_tree_vector (placement);
6148 placement = NULL;
6151 /* If the next token is a `(', then we have a parenthesized
6152 type-id. */
6153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6155 cp_token *token;
6156 /* Consume the `('. */
6157 cp_lexer_consume_token (parser->lexer);
6158 /* Parse the type-id. */
6159 type = cp_parser_type_id (parser);
6160 /* Look for the closing `)'. */
6161 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6162 token = cp_lexer_peek_token (parser->lexer);
6163 /* There should not be a direct-new-declarator in this production,
6164 but GCC used to allowed this, so we check and emit a sensible error
6165 message for this case. */
6166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6168 error_at (token->location,
6169 "array bound forbidden after parenthesized type-id");
6170 inform (token->location,
6171 "try removing the parentheses around the type-id");
6172 cp_parser_direct_new_declarator (parser);
6174 nelts = NULL_TREE;
6176 /* Otherwise, there must be a new-type-id. */
6177 else
6178 type = cp_parser_new_type_id (parser, &nelts);
6180 /* If the next token is a `(' or '{', then we have a new-initializer. */
6181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6182 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6183 initializer = cp_parser_new_initializer (parser);
6184 else
6185 initializer = NULL;
6187 /* A new-expression may not appear in an integral constant
6188 expression. */
6189 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6190 ret = error_mark_node;
6191 else
6193 /* Create a representation of the new-expression. */
6194 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6195 tf_warning_or_error);
6198 if (placement != NULL)
6199 release_tree_vector (placement);
6200 if (initializer != NULL)
6201 release_tree_vector (initializer);
6203 return ret;
6206 /* Parse a new-placement.
6208 new-placement:
6209 ( expression-list )
6211 Returns the same representation as for an expression-list. */
6213 static VEC(tree,gc) *
6214 cp_parser_new_placement (cp_parser* parser)
6216 VEC(tree,gc) *expression_list;
6218 /* Parse the expression-list. */
6219 expression_list = (cp_parser_parenthesized_expression_list
6220 (parser, non_attr, /*cast_p=*/false,
6221 /*allow_expansion_p=*/true,
6222 /*non_constant_p=*/NULL));
6224 return expression_list;
6227 /* Parse a new-type-id.
6229 new-type-id:
6230 type-specifier-seq new-declarator [opt]
6232 Returns the TYPE allocated. If the new-type-id indicates an array
6233 type, *NELTS is set to the number of elements in the last array
6234 bound; the TYPE will not include the last array bound. */
6236 static tree
6237 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6239 cp_decl_specifier_seq type_specifier_seq;
6240 cp_declarator *new_declarator;
6241 cp_declarator *declarator;
6242 cp_declarator *outer_declarator;
6243 const char *saved_message;
6244 tree type;
6246 /* The type-specifier sequence must not contain type definitions.
6247 (It cannot contain declarations of new types either, but if they
6248 are not definitions we will catch that because they are not
6249 complete.) */
6250 saved_message = parser->type_definition_forbidden_message;
6251 parser->type_definition_forbidden_message
6252 = G_("types may not be defined in a new-type-id");
6253 /* Parse the type-specifier-seq. */
6254 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6255 /*is_trailing_return=*/false,
6256 &type_specifier_seq);
6257 /* Restore the old message. */
6258 parser->type_definition_forbidden_message = saved_message;
6259 /* Parse the new-declarator. */
6260 new_declarator = cp_parser_new_declarator_opt (parser);
6262 /* Determine the number of elements in the last array dimension, if
6263 any. */
6264 *nelts = NULL_TREE;
6265 /* Skip down to the last array dimension. */
6266 declarator = new_declarator;
6267 outer_declarator = NULL;
6268 while (declarator && (declarator->kind == cdk_pointer
6269 || declarator->kind == cdk_ptrmem))
6271 outer_declarator = declarator;
6272 declarator = declarator->declarator;
6274 while (declarator
6275 && declarator->kind == cdk_array
6276 && declarator->declarator
6277 && declarator->declarator->kind == cdk_array)
6279 outer_declarator = declarator;
6280 declarator = declarator->declarator;
6283 if (declarator && declarator->kind == cdk_array)
6285 *nelts = declarator->u.array.bounds;
6286 if (*nelts == error_mark_node)
6287 *nelts = integer_one_node;
6289 if (outer_declarator)
6290 outer_declarator->declarator = declarator->declarator;
6291 else
6292 new_declarator = NULL;
6295 type = groktypename (&type_specifier_seq, new_declarator, false);
6296 return type;
6299 /* Parse an (optional) new-declarator.
6301 new-declarator:
6302 ptr-operator new-declarator [opt]
6303 direct-new-declarator
6305 Returns the declarator. */
6307 static cp_declarator *
6308 cp_parser_new_declarator_opt (cp_parser* parser)
6310 enum tree_code code;
6311 tree type;
6312 cp_cv_quals cv_quals;
6314 /* We don't know if there's a ptr-operator next, or not. */
6315 cp_parser_parse_tentatively (parser);
6316 /* Look for a ptr-operator. */
6317 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6318 /* If that worked, look for more new-declarators. */
6319 if (cp_parser_parse_definitely (parser))
6321 cp_declarator *declarator;
6323 /* Parse another optional declarator. */
6324 declarator = cp_parser_new_declarator_opt (parser);
6326 return cp_parser_make_indirect_declarator
6327 (code, type, cv_quals, declarator);
6330 /* If the next token is a `[', there is a direct-new-declarator. */
6331 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6332 return cp_parser_direct_new_declarator (parser);
6334 return NULL;
6337 /* Parse a direct-new-declarator.
6339 direct-new-declarator:
6340 [ expression ]
6341 direct-new-declarator [constant-expression]
6345 static cp_declarator *
6346 cp_parser_direct_new_declarator (cp_parser* parser)
6348 cp_declarator *declarator = NULL;
6350 while (true)
6352 tree expression;
6354 /* Look for the opening `['. */
6355 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6356 /* The first expression is not required to be constant. */
6357 if (!declarator)
6359 cp_token *token = cp_lexer_peek_token (parser->lexer);
6360 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6361 /* The standard requires that the expression have integral
6362 type. DR 74 adds enumeration types. We believe that the
6363 real intent is that these expressions be handled like the
6364 expression in a `switch' condition, which also allows
6365 classes with a single conversion to integral or
6366 enumeration type. */
6367 if (!processing_template_decl)
6369 expression
6370 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6371 expression,
6372 /*complain=*/true);
6373 if (!expression)
6375 error_at (token->location,
6376 "expression in new-declarator must have integral "
6377 "or enumeration type");
6378 expression = error_mark_node;
6382 /* But all the other expressions must be. */
6383 else
6384 expression
6385 = cp_parser_constant_expression (parser,
6386 /*allow_non_constant=*/false,
6387 NULL);
6388 /* Look for the closing `]'. */
6389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6391 /* Add this bound to the declarator. */
6392 declarator = make_array_declarator (declarator, expression);
6394 /* If the next token is not a `[', then there are no more
6395 bounds. */
6396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6397 break;
6400 return declarator;
6403 /* Parse a new-initializer.
6405 new-initializer:
6406 ( expression-list [opt] )
6407 braced-init-list
6409 Returns a representation of the expression-list. */
6411 static VEC(tree,gc) *
6412 cp_parser_new_initializer (cp_parser* parser)
6414 VEC(tree,gc) *expression_list;
6416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6418 tree t;
6419 bool expr_non_constant_p;
6420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6421 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6422 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6423 expression_list = make_tree_vector_single (t);
6425 else
6426 expression_list = (cp_parser_parenthesized_expression_list
6427 (parser, non_attr, /*cast_p=*/false,
6428 /*allow_expansion_p=*/true,
6429 /*non_constant_p=*/NULL));
6431 return expression_list;
6434 /* Parse a delete-expression.
6436 delete-expression:
6437 :: [opt] delete cast-expression
6438 :: [opt] delete [ ] cast-expression
6440 Returns a representation of the expression. */
6442 static tree
6443 cp_parser_delete_expression (cp_parser* parser)
6445 bool global_scope_p;
6446 bool array_p;
6447 tree expression;
6449 /* Look for the optional `::' operator. */
6450 global_scope_p
6451 = (cp_parser_global_scope_opt (parser,
6452 /*current_scope_valid_p=*/false)
6453 != NULL_TREE);
6454 /* Look for the `delete' keyword. */
6455 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6456 /* See if the array syntax is in use. */
6457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6459 /* Consume the `[' token. */
6460 cp_lexer_consume_token (parser->lexer);
6461 /* Look for the `]' token. */
6462 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6463 /* Remember that this is the `[]' construct. */
6464 array_p = true;
6466 else
6467 array_p = false;
6469 /* Parse the cast-expression. */
6470 expression = cp_parser_simple_cast_expression (parser);
6472 /* A delete-expression may not appear in an integral constant
6473 expression. */
6474 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6475 return error_mark_node;
6477 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6480 /* Returns true if TOKEN may start a cast-expression and false
6481 otherwise. */
6483 static bool
6484 cp_parser_token_starts_cast_expression (cp_token *token)
6486 switch (token->type)
6488 case CPP_COMMA:
6489 case CPP_SEMICOLON:
6490 case CPP_QUERY:
6491 case CPP_COLON:
6492 case CPP_CLOSE_SQUARE:
6493 case CPP_CLOSE_PAREN:
6494 case CPP_CLOSE_BRACE:
6495 case CPP_DOT:
6496 case CPP_DOT_STAR:
6497 case CPP_DEREF:
6498 case CPP_DEREF_STAR:
6499 case CPP_DIV:
6500 case CPP_MOD:
6501 case CPP_LSHIFT:
6502 case CPP_RSHIFT:
6503 case CPP_LESS:
6504 case CPP_GREATER:
6505 case CPP_LESS_EQ:
6506 case CPP_GREATER_EQ:
6507 case CPP_EQ_EQ:
6508 case CPP_NOT_EQ:
6509 case CPP_EQ:
6510 case CPP_MULT_EQ:
6511 case CPP_DIV_EQ:
6512 case CPP_MOD_EQ:
6513 case CPP_PLUS_EQ:
6514 case CPP_MINUS_EQ:
6515 case CPP_RSHIFT_EQ:
6516 case CPP_LSHIFT_EQ:
6517 case CPP_AND_EQ:
6518 case CPP_XOR_EQ:
6519 case CPP_OR_EQ:
6520 case CPP_XOR:
6521 case CPP_OR:
6522 case CPP_OR_OR:
6523 case CPP_EOF:
6524 return false;
6526 /* '[' may start a primary-expression in obj-c++. */
6527 case CPP_OPEN_SQUARE:
6528 return c_dialect_objc ();
6530 default:
6531 return true;
6535 /* Parse a cast-expression.
6537 cast-expression:
6538 unary-expression
6539 ( type-id ) cast-expression
6541 ADDRESS_P is true iff the unary-expression is appearing as the
6542 operand of the `&' operator. CAST_P is true if this expression is
6543 the target of a cast.
6545 Returns a representation of the expression. */
6547 static tree
6548 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6549 cp_id_kind * pidk)
6551 /* If it's a `(', then we might be looking at a cast. */
6552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6554 tree type = NULL_TREE;
6555 tree expr = NULL_TREE;
6556 bool compound_literal_p;
6557 const char *saved_message;
6559 /* There's no way to know yet whether or not this is a cast.
6560 For example, `(int (3))' is a unary-expression, while `(int)
6561 3' is a cast. So, we resort to parsing tentatively. */
6562 cp_parser_parse_tentatively (parser);
6563 /* Types may not be defined in a cast. */
6564 saved_message = parser->type_definition_forbidden_message;
6565 parser->type_definition_forbidden_message
6566 = G_("types may not be defined in casts");
6567 /* Consume the `('. */
6568 cp_lexer_consume_token (parser->lexer);
6569 /* A very tricky bit is that `(struct S) { 3 }' is a
6570 compound-literal (which we permit in C++ as an extension).
6571 But, that construct is not a cast-expression -- it is a
6572 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6573 is legal; if the compound-literal were a cast-expression,
6574 you'd need an extra set of parentheses.) But, if we parse
6575 the type-id, and it happens to be a class-specifier, then we
6576 will commit to the parse at that point, because we cannot
6577 undo the action that is done when creating a new class. So,
6578 then we cannot back up and do a postfix-expression.
6580 Therefore, we scan ahead to the closing `)', and check to see
6581 if the token after the `)' is a `{'. If so, we are not
6582 looking at a cast-expression.
6584 Save tokens so that we can put them back. */
6585 cp_lexer_save_tokens (parser->lexer);
6586 /* Skip tokens until the next token is a closing parenthesis.
6587 If we find the closing `)', and the next token is a `{', then
6588 we are looking at a compound-literal. */
6589 compound_literal_p
6590 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6591 /*consume_paren=*/true)
6592 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6593 /* Roll back the tokens we skipped. */
6594 cp_lexer_rollback_tokens (parser->lexer);
6595 /* If we were looking at a compound-literal, simulate an error
6596 so that the call to cp_parser_parse_definitely below will
6597 fail. */
6598 if (compound_literal_p)
6599 cp_parser_simulate_error (parser);
6600 else
6602 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6603 parser->in_type_id_in_expr_p = true;
6604 /* Look for the type-id. */
6605 type = cp_parser_type_id (parser);
6606 /* Look for the closing `)'. */
6607 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6608 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6611 /* Restore the saved message. */
6612 parser->type_definition_forbidden_message = saved_message;
6614 /* At this point this can only be either a cast or a
6615 parenthesized ctor such as `(T ())' that looks like a cast to
6616 function returning T. */
6617 if (!cp_parser_error_occurred (parser)
6618 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6619 (parser->lexer)))
6621 cp_parser_parse_definitely (parser);
6622 expr = cp_parser_cast_expression (parser,
6623 /*address_p=*/false,
6624 /*cast_p=*/true, pidk);
6626 /* Warn about old-style casts, if so requested. */
6627 if (warn_old_style_cast
6628 && !in_system_header
6629 && !VOID_TYPE_P (type)
6630 && current_lang_name != lang_name_c)
6631 warning (OPT_Wold_style_cast, "use of old-style cast");
6633 /* Only type conversions to integral or enumeration types
6634 can be used in constant-expressions. */
6635 if (!cast_valid_in_integral_constant_expression_p (type)
6636 && cp_parser_non_integral_constant_expression (parser,
6637 NIC_CAST))
6638 return error_mark_node;
6640 /* Perform the cast. */
6641 expr = build_c_cast (input_location, type, expr);
6642 return expr;
6644 else
6645 cp_parser_abort_tentative_parse (parser);
6648 /* If we get here, then it's not a cast, so it must be a
6649 unary-expression. */
6650 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6653 /* Parse a binary expression of the general form:
6655 pm-expression:
6656 cast-expression
6657 pm-expression .* cast-expression
6658 pm-expression ->* cast-expression
6660 multiplicative-expression:
6661 pm-expression
6662 multiplicative-expression * pm-expression
6663 multiplicative-expression / pm-expression
6664 multiplicative-expression % pm-expression
6666 additive-expression:
6667 multiplicative-expression
6668 additive-expression + multiplicative-expression
6669 additive-expression - multiplicative-expression
6671 shift-expression:
6672 additive-expression
6673 shift-expression << additive-expression
6674 shift-expression >> additive-expression
6676 relational-expression:
6677 shift-expression
6678 relational-expression < shift-expression
6679 relational-expression > shift-expression
6680 relational-expression <= shift-expression
6681 relational-expression >= shift-expression
6683 GNU Extension:
6685 relational-expression:
6686 relational-expression <? shift-expression
6687 relational-expression >? shift-expression
6689 equality-expression:
6690 relational-expression
6691 equality-expression == relational-expression
6692 equality-expression != relational-expression
6694 and-expression:
6695 equality-expression
6696 and-expression & equality-expression
6698 exclusive-or-expression:
6699 and-expression
6700 exclusive-or-expression ^ and-expression
6702 inclusive-or-expression:
6703 exclusive-or-expression
6704 inclusive-or-expression | exclusive-or-expression
6706 logical-and-expression:
6707 inclusive-or-expression
6708 logical-and-expression && inclusive-or-expression
6710 logical-or-expression:
6711 logical-and-expression
6712 logical-or-expression || logical-and-expression
6714 All these are implemented with a single function like:
6716 binary-expression:
6717 simple-cast-expression
6718 binary-expression <token> binary-expression
6720 CAST_P is true if this expression is the target of a cast.
6722 The binops_by_token map is used to get the tree codes for each <token> type.
6723 binary-expressions are associated according to a precedence table. */
6725 #define TOKEN_PRECEDENCE(token) \
6726 (((token->type == CPP_GREATER \
6727 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6728 && !parser->greater_than_is_operator_p) \
6729 ? PREC_NOT_OPERATOR \
6730 : binops_by_token[token->type].prec)
6732 static tree
6733 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6734 bool no_toplevel_fold_p,
6735 enum cp_parser_prec prec,
6736 cp_id_kind * pidk)
6738 cp_parser_expression_stack stack;
6739 cp_parser_expression_stack_entry *sp = &stack[0];
6740 tree lhs, rhs;
6741 cp_token *token;
6742 enum tree_code tree_type, lhs_type, rhs_type;
6743 enum cp_parser_prec new_prec, lookahead_prec;
6744 bool overloaded_p;
6746 /* Parse the first expression. */
6747 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6748 lhs_type = ERROR_MARK;
6750 for (;;)
6752 /* Get an operator token. */
6753 token = cp_lexer_peek_token (parser->lexer);
6755 if (warn_cxx0x_compat
6756 && token->type == CPP_RSHIFT
6757 && !parser->greater_than_is_operator_p)
6759 if (warning_at (token->location, OPT_Wc__0x_compat,
6760 "%<>>%> operator will be treated as"
6761 " two right angle brackets in C++0x"))
6762 inform (token->location,
6763 "suggest parentheses around %<>>%> expression");
6766 new_prec = TOKEN_PRECEDENCE (token);
6768 /* Popping an entry off the stack means we completed a subexpression:
6769 - either we found a token which is not an operator (`>' where it is not
6770 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6771 will happen repeatedly;
6772 - or, we found an operator which has lower priority. This is the case
6773 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6774 parsing `3 * 4'. */
6775 if (new_prec <= prec)
6777 if (sp == stack)
6778 break;
6779 else
6780 goto pop;
6783 get_rhs:
6784 tree_type = binops_by_token[token->type].tree_type;
6786 /* We used the operator token. */
6787 cp_lexer_consume_token (parser->lexer);
6789 /* For "false && x" or "true || x", x will never be executed;
6790 disable warnings while evaluating it. */
6791 if (tree_type == TRUTH_ANDIF_EXPR)
6792 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6793 else if (tree_type == TRUTH_ORIF_EXPR)
6794 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6796 /* Extract another operand. It may be the RHS of this expression
6797 or the LHS of a new, higher priority expression. */
6798 rhs = cp_parser_simple_cast_expression (parser);
6799 rhs_type = ERROR_MARK;
6801 /* Get another operator token. Look up its precedence to avoid
6802 building a useless (immediately popped) stack entry for common
6803 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6804 token = cp_lexer_peek_token (parser->lexer);
6805 lookahead_prec = TOKEN_PRECEDENCE (token);
6806 if (lookahead_prec > new_prec)
6808 /* ... and prepare to parse the RHS of the new, higher priority
6809 expression. Since precedence levels on the stack are
6810 monotonically increasing, we do not have to care about
6811 stack overflows. */
6812 sp->prec = prec;
6813 sp->tree_type = tree_type;
6814 sp->lhs = lhs;
6815 sp->lhs_type = lhs_type;
6816 sp++;
6817 lhs = rhs;
6818 lhs_type = rhs_type;
6819 prec = new_prec;
6820 new_prec = lookahead_prec;
6821 goto get_rhs;
6823 pop:
6824 lookahead_prec = new_prec;
6825 /* If the stack is not empty, we have parsed into LHS the right side
6826 (`4' in the example above) of an expression we had suspended.
6827 We can use the information on the stack to recover the LHS (`3')
6828 from the stack together with the tree code (`MULT_EXPR'), and
6829 the precedence of the higher level subexpression
6830 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6831 which will be used to actually build the additive expression. */
6832 --sp;
6833 prec = sp->prec;
6834 tree_type = sp->tree_type;
6835 rhs = lhs;
6836 rhs_type = lhs_type;
6837 lhs = sp->lhs;
6838 lhs_type = sp->lhs_type;
6841 /* Undo the disabling of warnings done above. */
6842 if (tree_type == TRUTH_ANDIF_EXPR)
6843 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6844 else if (tree_type == TRUTH_ORIF_EXPR)
6845 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6847 overloaded_p = false;
6848 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6849 ERROR_MARK for everything that is not a binary expression.
6850 This makes warn_about_parentheses miss some warnings that
6851 involve unary operators. For unary expressions we should
6852 pass the correct tree_code unless the unary expression was
6853 surrounded by parentheses.
6855 if (no_toplevel_fold_p
6856 && lookahead_prec <= prec
6857 && sp == stack
6858 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6859 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6860 else
6861 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6862 &overloaded_p, tf_warning_or_error);
6863 lhs_type = tree_type;
6865 /* If the binary operator required the use of an overloaded operator,
6866 then this expression cannot be an integral constant-expression.
6867 An overloaded operator can be used even if both operands are
6868 otherwise permissible in an integral constant-expression if at
6869 least one of the operands is of enumeration type. */
6871 if (overloaded_p
6872 && cp_parser_non_integral_constant_expression (parser,
6873 NIC_OVERLOADED))
6874 return error_mark_node;
6877 return lhs;
6881 /* Parse the `? expression : assignment-expression' part of a
6882 conditional-expression. The LOGICAL_OR_EXPR is the
6883 logical-or-expression that started the conditional-expression.
6884 Returns a representation of the entire conditional-expression.
6886 This routine is used by cp_parser_assignment_expression.
6888 ? expression : assignment-expression
6890 GNU Extensions:
6892 ? : assignment-expression */
6894 static tree
6895 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6897 tree expr;
6898 tree assignment_expr;
6899 struct cp_token *token;
6901 /* Consume the `?' token. */
6902 cp_lexer_consume_token (parser->lexer);
6903 token = cp_lexer_peek_token (parser->lexer);
6904 if (cp_parser_allow_gnu_extensions_p (parser)
6905 && token->type == CPP_COLON)
6907 pedwarn (token->location, OPT_pedantic,
6908 "ISO C++ does not allow ?: with omitted middle operand");
6909 /* Implicit true clause. */
6910 expr = NULL_TREE;
6911 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6912 warn_for_omitted_condop (token->location, logical_or_expr);
6914 else
6916 /* Parse the expression. */
6917 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6918 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6919 c_inhibit_evaluation_warnings +=
6920 ((logical_or_expr == truthvalue_true_node)
6921 - (logical_or_expr == truthvalue_false_node));
6924 /* The next token should be a `:'. */
6925 cp_parser_require (parser, CPP_COLON, RT_COLON);
6926 /* Parse the assignment-expression. */
6927 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6928 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6930 /* Build the conditional-expression. */
6931 return build_x_conditional_expr (logical_or_expr,
6932 expr,
6933 assignment_expr,
6934 tf_warning_or_error);
6937 /* Parse an assignment-expression.
6939 assignment-expression:
6940 conditional-expression
6941 logical-or-expression assignment-operator assignment_expression
6942 throw-expression
6944 CAST_P is true if this expression is the target of a cast.
6946 Returns a representation for the expression. */
6948 static tree
6949 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6950 cp_id_kind * pidk)
6952 tree expr;
6954 /* If the next token is the `throw' keyword, then we're looking at
6955 a throw-expression. */
6956 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6957 expr = cp_parser_throw_expression (parser);
6958 /* Otherwise, it must be that we are looking at a
6959 logical-or-expression. */
6960 else
6962 /* Parse the binary expressions (logical-or-expression). */
6963 expr = cp_parser_binary_expression (parser, cast_p, false,
6964 PREC_NOT_OPERATOR, pidk);
6965 /* If the next token is a `?' then we're actually looking at a
6966 conditional-expression. */
6967 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6968 return cp_parser_question_colon_clause (parser, expr);
6969 else
6971 enum tree_code assignment_operator;
6973 /* If it's an assignment-operator, we're using the second
6974 production. */
6975 assignment_operator
6976 = cp_parser_assignment_operator_opt (parser);
6977 if (assignment_operator != ERROR_MARK)
6979 bool non_constant_p;
6981 /* Parse the right-hand side of the assignment. */
6982 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6984 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6985 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6987 /* An assignment may not appear in a
6988 constant-expression. */
6989 if (cp_parser_non_integral_constant_expression (parser,
6990 NIC_ASSIGNMENT))
6991 return error_mark_node;
6992 /* Build the assignment expression. */
6993 expr = build_x_modify_expr (expr,
6994 assignment_operator,
6995 rhs,
6996 tf_warning_or_error);
7001 return expr;
7004 /* Parse an (optional) assignment-operator.
7006 assignment-operator: one of
7007 = *= /= %= += -= >>= <<= &= ^= |=
7009 GNU Extension:
7011 assignment-operator: one of
7012 <?= >?=
7014 If the next token is an assignment operator, the corresponding tree
7015 code is returned, and the token is consumed. For example, for
7016 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7017 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7018 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7019 operator, ERROR_MARK is returned. */
7021 static enum tree_code
7022 cp_parser_assignment_operator_opt (cp_parser* parser)
7024 enum tree_code op;
7025 cp_token *token;
7027 /* Peek at the next token. */
7028 token = cp_lexer_peek_token (parser->lexer);
7030 switch (token->type)
7032 case CPP_EQ:
7033 op = NOP_EXPR;
7034 break;
7036 case CPP_MULT_EQ:
7037 op = MULT_EXPR;
7038 break;
7040 case CPP_DIV_EQ:
7041 op = TRUNC_DIV_EXPR;
7042 break;
7044 case CPP_MOD_EQ:
7045 op = TRUNC_MOD_EXPR;
7046 break;
7048 case CPP_PLUS_EQ:
7049 op = PLUS_EXPR;
7050 break;
7052 case CPP_MINUS_EQ:
7053 op = MINUS_EXPR;
7054 break;
7056 case CPP_RSHIFT_EQ:
7057 op = RSHIFT_EXPR;
7058 break;
7060 case CPP_LSHIFT_EQ:
7061 op = LSHIFT_EXPR;
7062 break;
7064 case CPP_AND_EQ:
7065 op = BIT_AND_EXPR;
7066 break;
7068 case CPP_XOR_EQ:
7069 op = BIT_XOR_EXPR;
7070 break;
7072 case CPP_OR_EQ:
7073 op = BIT_IOR_EXPR;
7074 break;
7076 default:
7077 /* Nothing else is an assignment operator. */
7078 op = ERROR_MARK;
7081 /* If it was an assignment operator, consume it. */
7082 if (op != ERROR_MARK)
7083 cp_lexer_consume_token (parser->lexer);
7085 return op;
7088 /* Parse an expression.
7090 expression:
7091 assignment-expression
7092 expression , assignment-expression
7094 CAST_P is true if this expression is the target of a cast.
7096 Returns a representation of the expression. */
7098 static tree
7099 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7101 tree expression = NULL_TREE;
7103 while (true)
7105 tree assignment_expression;
7107 /* Parse the next assignment-expression. */
7108 assignment_expression
7109 = cp_parser_assignment_expression (parser, cast_p, pidk);
7110 /* If this is the first assignment-expression, we can just
7111 save it away. */
7112 if (!expression)
7113 expression = assignment_expression;
7114 else
7115 expression = build_x_compound_expr (expression,
7116 assignment_expression,
7117 tf_warning_or_error);
7118 /* If the next token is not a comma, then we are done with the
7119 expression. */
7120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7121 break;
7122 /* Consume the `,'. */
7123 cp_lexer_consume_token (parser->lexer);
7124 /* A comma operator cannot appear in a constant-expression. */
7125 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7126 expression = error_mark_node;
7129 return expression;
7132 /* Parse a constant-expression.
7134 constant-expression:
7135 conditional-expression
7137 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7138 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7139 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7140 is false, NON_CONSTANT_P should be NULL. */
7142 static tree
7143 cp_parser_constant_expression (cp_parser* parser,
7144 bool allow_non_constant_p,
7145 bool *non_constant_p)
7147 bool saved_integral_constant_expression_p;
7148 bool saved_allow_non_integral_constant_expression_p;
7149 bool saved_non_integral_constant_expression_p;
7150 tree expression;
7152 /* It might seem that we could simply parse the
7153 conditional-expression, and then check to see if it were
7154 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7155 one that the compiler can figure out is constant, possibly after
7156 doing some simplifications or optimizations. The standard has a
7157 precise definition of constant-expression, and we must honor
7158 that, even though it is somewhat more restrictive.
7160 For example:
7162 int i[(2, 3)];
7164 is not a legal declaration, because `(2, 3)' is not a
7165 constant-expression. The `,' operator is forbidden in a
7166 constant-expression. However, GCC's constant-folding machinery
7167 will fold this operation to an INTEGER_CST for `3'. */
7169 /* Save the old settings. */
7170 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7171 saved_allow_non_integral_constant_expression_p
7172 = parser->allow_non_integral_constant_expression_p;
7173 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7174 /* We are now parsing a constant-expression. */
7175 parser->integral_constant_expression_p = true;
7176 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7177 parser->non_integral_constant_expression_p = false;
7178 /* Although the grammar says "conditional-expression", we parse an
7179 "assignment-expression", which also permits "throw-expression"
7180 and the use of assignment operators. In the case that
7181 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7182 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7183 actually essential that we look for an assignment-expression.
7184 For example, cp_parser_initializer_clauses uses this function to
7185 determine whether a particular assignment-expression is in fact
7186 constant. */
7187 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7188 /* Restore the old settings. */
7189 parser->integral_constant_expression_p
7190 = saved_integral_constant_expression_p;
7191 parser->allow_non_integral_constant_expression_p
7192 = saved_allow_non_integral_constant_expression_p;
7193 if (allow_non_constant_p)
7194 *non_constant_p = parser->non_integral_constant_expression_p;
7195 else if (parser->non_integral_constant_expression_p)
7196 expression = error_mark_node;
7197 parser->non_integral_constant_expression_p
7198 = saved_non_integral_constant_expression_p;
7200 return expression;
7203 /* Parse __builtin_offsetof.
7205 offsetof-expression:
7206 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7208 offsetof-member-designator:
7209 id-expression
7210 | offsetof-member-designator "." id-expression
7211 | offsetof-member-designator "[" expression "]"
7212 | offsetof-member-designator "->" id-expression */
7214 static tree
7215 cp_parser_builtin_offsetof (cp_parser *parser)
7217 int save_ice_p, save_non_ice_p;
7218 tree type, expr;
7219 cp_id_kind dummy;
7220 cp_token *token;
7222 /* We're about to accept non-integral-constant things, but will
7223 definitely yield an integral constant expression. Save and
7224 restore these values around our local parsing. */
7225 save_ice_p = parser->integral_constant_expression_p;
7226 save_non_ice_p = parser->non_integral_constant_expression_p;
7228 /* Consume the "__builtin_offsetof" token. */
7229 cp_lexer_consume_token (parser->lexer);
7230 /* Consume the opening `('. */
7231 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7232 /* Parse the type-id. */
7233 type = cp_parser_type_id (parser);
7234 /* Look for the `,'. */
7235 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7236 token = cp_lexer_peek_token (parser->lexer);
7238 /* Build the (type *)null that begins the traditional offsetof macro. */
7239 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7240 tf_warning_or_error);
7242 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7243 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7244 true, &dummy, token->location);
7245 while (true)
7247 token = cp_lexer_peek_token (parser->lexer);
7248 switch (token->type)
7250 case CPP_OPEN_SQUARE:
7251 /* offsetof-member-designator "[" expression "]" */
7252 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7253 break;
7255 case CPP_DEREF:
7256 /* offsetof-member-designator "->" identifier */
7257 expr = grok_array_decl (expr, integer_zero_node);
7258 /* FALLTHRU */
7260 case CPP_DOT:
7261 /* offsetof-member-designator "." identifier */
7262 cp_lexer_consume_token (parser->lexer);
7263 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7264 expr, true, &dummy,
7265 token->location);
7266 break;
7268 case CPP_CLOSE_PAREN:
7269 /* Consume the ")" token. */
7270 cp_lexer_consume_token (parser->lexer);
7271 goto success;
7273 default:
7274 /* Error. We know the following require will fail, but
7275 that gives the proper error message. */
7276 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7277 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7278 expr = error_mark_node;
7279 goto failure;
7283 success:
7284 /* If we're processing a template, we can't finish the semantics yet.
7285 Otherwise we can fold the entire expression now. */
7286 if (processing_template_decl)
7287 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7288 else
7289 expr = finish_offsetof (expr);
7291 failure:
7292 parser->integral_constant_expression_p = save_ice_p;
7293 parser->non_integral_constant_expression_p = save_non_ice_p;
7295 return expr;
7298 /* Parse a trait expression. */
7300 static tree
7301 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7303 cp_trait_kind kind;
7304 tree type1, type2 = NULL_TREE;
7305 bool binary = false;
7306 cp_decl_specifier_seq decl_specs;
7308 switch (keyword)
7310 case RID_HAS_NOTHROW_ASSIGN:
7311 kind = CPTK_HAS_NOTHROW_ASSIGN;
7312 break;
7313 case RID_HAS_NOTHROW_CONSTRUCTOR:
7314 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7315 break;
7316 case RID_HAS_NOTHROW_COPY:
7317 kind = CPTK_HAS_NOTHROW_COPY;
7318 break;
7319 case RID_HAS_TRIVIAL_ASSIGN:
7320 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7321 break;
7322 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7323 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7324 break;
7325 case RID_HAS_TRIVIAL_COPY:
7326 kind = CPTK_HAS_TRIVIAL_COPY;
7327 break;
7328 case RID_HAS_TRIVIAL_DESTRUCTOR:
7329 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7330 break;
7331 case RID_HAS_VIRTUAL_DESTRUCTOR:
7332 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7333 break;
7334 case RID_IS_ABSTRACT:
7335 kind = CPTK_IS_ABSTRACT;
7336 break;
7337 case RID_IS_BASE_OF:
7338 kind = CPTK_IS_BASE_OF;
7339 binary = true;
7340 break;
7341 case RID_IS_CLASS:
7342 kind = CPTK_IS_CLASS;
7343 break;
7344 case RID_IS_CONVERTIBLE_TO:
7345 kind = CPTK_IS_CONVERTIBLE_TO;
7346 binary = true;
7347 break;
7348 case RID_IS_EMPTY:
7349 kind = CPTK_IS_EMPTY;
7350 break;
7351 case RID_IS_ENUM:
7352 kind = CPTK_IS_ENUM;
7353 break;
7354 case RID_IS_POD:
7355 kind = CPTK_IS_POD;
7356 break;
7357 case RID_IS_POLYMORPHIC:
7358 kind = CPTK_IS_POLYMORPHIC;
7359 break;
7360 case RID_IS_STD_LAYOUT:
7361 kind = CPTK_IS_STD_LAYOUT;
7362 break;
7363 case RID_IS_TRIVIAL:
7364 kind = CPTK_IS_TRIVIAL;
7365 break;
7366 case RID_IS_UNION:
7367 kind = CPTK_IS_UNION;
7368 break;
7369 case RID_IS_LITERAL_TYPE:
7370 kind = CPTK_IS_LITERAL_TYPE;
7371 break;
7372 default:
7373 gcc_unreachable ();
7376 /* Consume the token. */
7377 cp_lexer_consume_token (parser->lexer);
7379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7381 type1 = cp_parser_type_id (parser);
7383 if (type1 == error_mark_node)
7384 return error_mark_node;
7386 /* Build a trivial decl-specifier-seq. */
7387 clear_decl_specs (&decl_specs);
7388 decl_specs.type = type1;
7390 /* Call grokdeclarator to figure out what type this is. */
7391 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7392 /*initialized=*/0, /*attrlist=*/NULL);
7394 if (binary)
7396 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7398 type2 = cp_parser_type_id (parser);
7400 if (type2 == error_mark_node)
7401 return error_mark_node;
7403 /* Build a trivial decl-specifier-seq. */
7404 clear_decl_specs (&decl_specs);
7405 decl_specs.type = type2;
7407 /* Call grokdeclarator to figure out what type this is. */
7408 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7409 /*initialized=*/0, /*attrlist=*/NULL);
7412 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7414 /* Complete the trait expression, which may mean either processing
7415 the trait expr now or saving it for template instantiation. */
7416 return finish_trait_expr (kind, type1, type2);
7419 /* Lambdas that appear in variable initializer or default argument scope
7420 get that in their mangling, so we need to record it. We might as well
7421 use the count for function and namespace scopes as well. */
7422 static GTY(()) tree lambda_scope;
7423 static GTY(()) int lambda_count;
7424 typedef struct GTY(()) tree_int
7426 tree t;
7427 int i;
7428 } tree_int;
7429 DEF_VEC_O(tree_int);
7430 DEF_VEC_ALLOC_O(tree_int,gc);
7431 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7433 static void
7434 start_lambda_scope (tree decl)
7436 tree_int ti;
7437 gcc_assert (decl);
7438 /* Once we're inside a function, we ignore other scopes and just push
7439 the function again so that popping works properly. */
7440 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7441 decl = current_function_decl;
7442 ti.t = lambda_scope;
7443 ti.i = lambda_count;
7444 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7445 if (lambda_scope != decl)
7447 /* Don't reset the count if we're still in the same function. */
7448 lambda_scope = decl;
7449 lambda_count = 0;
7453 static void
7454 record_lambda_scope (tree lambda)
7456 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7457 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7460 static void
7461 finish_lambda_scope (void)
7463 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7464 if (lambda_scope != p->t)
7466 lambda_scope = p->t;
7467 lambda_count = p->i;
7469 VEC_pop (tree_int, lambda_scope_stack);
7472 /* Parse a lambda expression.
7474 lambda-expression:
7475 lambda-introducer lambda-declarator [opt] compound-statement
7477 Returns a representation of the expression. */
7479 static tree
7480 cp_parser_lambda_expression (cp_parser* parser)
7482 tree lambda_expr = build_lambda_expr ();
7483 tree type;
7485 LAMBDA_EXPR_LOCATION (lambda_expr)
7486 = cp_lexer_peek_token (parser->lexer)->location;
7488 if (cp_unevaluated_operand)
7489 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7490 "lambda-expression in unevaluated context");
7492 /* We may be in the middle of deferred access check. Disable
7493 it now. */
7494 push_deferring_access_checks (dk_no_deferred);
7496 cp_parser_lambda_introducer (parser, lambda_expr);
7498 type = begin_lambda_type (lambda_expr);
7500 record_lambda_scope (lambda_expr);
7502 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7503 determine_visibility (TYPE_NAME (type));
7505 /* Now that we've started the type, add the capture fields for any
7506 explicit captures. */
7507 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7510 /* Inside the class, surrounding template-parameter-lists do not apply. */
7511 unsigned int saved_num_template_parameter_lists
7512 = parser->num_template_parameter_lists;
7514 parser->num_template_parameter_lists = 0;
7516 /* By virtue of defining a local class, a lambda expression has access to
7517 the private variables of enclosing classes. */
7519 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7521 cp_parser_lambda_body (parser, lambda_expr);
7523 /* The capture list was built up in reverse order; fix that now. */
7525 tree newlist = NULL_TREE;
7526 tree elt, next;
7528 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7529 elt; elt = next)
7531 tree field = TREE_PURPOSE (elt);
7532 char *buf;
7534 next = TREE_CHAIN (elt);
7535 TREE_CHAIN (elt) = newlist;
7536 newlist = elt;
7538 /* Also add __ to the beginning of the field name so that code
7539 outside the lambda body can't see the captured name. We could
7540 just remove the name entirely, but this is more useful for
7541 debugging. */
7542 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7543 /* The 'this' capture already starts with __. */
7544 continue;
7546 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7547 buf[1] = buf[0] = '_';
7548 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7549 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7550 DECL_NAME (field) = get_identifier (buf);
7552 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7555 maybe_add_lambda_conv_op (type);
7557 type = finish_struct (type, /*attributes=*/NULL_TREE);
7559 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7562 pop_deferring_access_checks ();
7564 return build_lambda_object (lambda_expr);
7567 /* Parse the beginning of a lambda expression.
7569 lambda-introducer:
7570 [ lambda-capture [opt] ]
7572 LAMBDA_EXPR is the current representation of the lambda expression. */
7574 static void
7575 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7577 /* Need commas after the first capture. */
7578 bool first = true;
7580 /* Eat the leading `['. */
7581 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7583 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7584 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7585 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7586 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7587 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7588 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7590 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7592 cp_lexer_consume_token (parser->lexer);
7593 first = false;
7596 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7598 cp_token* capture_token;
7599 tree capture_id;
7600 tree capture_init_expr;
7601 cp_id_kind idk = CP_ID_KIND_NONE;
7602 bool explicit_init_p = false;
7604 enum capture_kind_type
7606 BY_COPY,
7607 BY_REFERENCE
7609 enum capture_kind_type capture_kind = BY_COPY;
7611 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7613 error ("expected end of capture-list");
7614 return;
7617 if (first)
7618 first = false;
7619 else
7620 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7622 /* Possibly capture `this'. */
7623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7625 cp_lexer_consume_token (parser->lexer);
7626 add_capture (lambda_expr,
7627 /*id=*/get_identifier ("__this"),
7628 /*initializer=*/finish_this_expr(),
7629 /*by_reference_p=*/false,
7630 explicit_init_p);
7631 continue;
7634 /* Remember whether we want to capture as a reference or not. */
7635 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7637 capture_kind = BY_REFERENCE;
7638 cp_lexer_consume_token (parser->lexer);
7641 /* Get the identifier. */
7642 capture_token = cp_lexer_peek_token (parser->lexer);
7643 capture_id = cp_parser_identifier (parser);
7645 if (capture_id == error_mark_node)
7646 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7647 delimiters, but I modified this to stop on unnested ']' as well. It
7648 was already changed to stop on unnested '}', so the
7649 "closing_parenthesis" name is no more misleading with my change. */
7651 cp_parser_skip_to_closing_parenthesis (parser,
7652 /*recovering=*/true,
7653 /*or_comma=*/true,
7654 /*consume_paren=*/true);
7655 break;
7658 /* Find the initializer for this capture. */
7659 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7661 /* An explicit expression exists. */
7662 cp_lexer_consume_token (parser->lexer);
7663 pedwarn (input_location, OPT_pedantic,
7664 "ISO C++ does not allow initializers "
7665 "in lambda expression capture lists");
7666 capture_init_expr = cp_parser_assignment_expression (parser,
7667 /*cast_p=*/true,
7668 &idk);
7669 explicit_init_p = true;
7671 else
7673 const char* error_msg;
7675 /* Turn the identifier into an id-expression. */
7676 capture_init_expr
7677 = cp_parser_lookup_name
7678 (parser,
7679 capture_id,
7680 none_type,
7681 /*is_template=*/false,
7682 /*is_namespace=*/false,
7683 /*check_dependency=*/true,
7684 /*ambiguous_decls=*/NULL,
7685 capture_token->location);
7687 capture_init_expr
7688 = finish_id_expression
7689 (capture_id,
7690 capture_init_expr,
7691 parser->scope,
7692 &idk,
7693 /*integral_constant_expression_p=*/false,
7694 /*allow_non_integral_constant_expression_p=*/false,
7695 /*non_integral_constant_expression_p=*/NULL,
7696 /*template_p=*/false,
7697 /*done=*/true,
7698 /*address_p=*/false,
7699 /*template_arg_p=*/false,
7700 &error_msg,
7701 capture_token->location);
7704 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7705 capture_init_expr
7706 = unqualified_name_lookup_error (capture_init_expr);
7708 add_capture (lambda_expr,
7709 capture_id,
7710 capture_init_expr,
7711 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7712 explicit_init_p);
7715 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7718 /* Parse the (optional) middle of a lambda expression.
7720 lambda-declarator:
7721 ( parameter-declaration-clause [opt] )
7722 attribute-specifier [opt]
7723 mutable [opt]
7724 exception-specification [opt]
7725 lambda-return-type-clause [opt]
7727 LAMBDA_EXPR is the current representation of the lambda expression. */
7729 static void
7730 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7732 /* 5.1.1.4 of the standard says:
7733 If a lambda-expression does not include a lambda-declarator, it is as if
7734 the lambda-declarator were ().
7735 This means an empty parameter list, no attributes, and no exception
7736 specification. */
7737 tree param_list = void_list_node;
7738 tree attributes = NULL_TREE;
7739 tree exception_spec = NULL_TREE;
7740 tree t;
7742 /* The lambda-declarator is optional, but must begin with an opening
7743 parenthesis if present. */
7744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7746 cp_lexer_consume_token (parser->lexer);
7748 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7750 /* Parse parameters. */
7751 param_list = cp_parser_parameter_declaration_clause (parser);
7753 /* Default arguments shall not be specified in the
7754 parameter-declaration-clause of a lambda-declarator. */
7755 for (t = param_list; t; t = TREE_CHAIN (t))
7756 if (TREE_PURPOSE (t))
7757 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7758 "default argument specified for lambda parameter");
7760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7762 attributes = cp_parser_attributes_opt (parser);
7764 /* Parse optional `mutable' keyword. */
7765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7767 cp_lexer_consume_token (parser->lexer);
7768 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7771 /* Parse optional exception specification. */
7772 exception_spec = cp_parser_exception_specification_opt (parser);
7774 /* Parse optional trailing return type. */
7775 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7777 cp_lexer_consume_token (parser->lexer);
7778 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7781 /* The function parameters must be in scope all the way until after the
7782 trailing-return-type in case of decltype. */
7783 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7784 pop_binding (DECL_NAME (t), t);
7786 leave_scope ();
7789 /* Create the function call operator.
7791 Messing with declarators like this is no uglier than building up the
7792 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7793 other code. */
7795 cp_decl_specifier_seq return_type_specs;
7796 cp_declarator* declarator;
7797 tree fco;
7798 int quals;
7799 void *p;
7801 clear_decl_specs (&return_type_specs);
7802 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7803 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7804 else
7805 /* Maybe we will deduce the return type later, but we can use void
7806 as a placeholder return type anyways. */
7807 return_type_specs.type = void_type_node;
7809 p = obstack_alloc (&declarator_obstack, 0);
7811 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7812 sfk_none);
7814 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7815 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7816 declarator = make_call_declarator (declarator, param_list, quals,
7817 exception_spec,
7818 /*late_return_type=*/NULL_TREE);
7819 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7821 fco = grokmethod (&return_type_specs,
7822 declarator,
7823 attributes);
7824 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7825 DECL_ARTIFICIAL (fco) = 1;
7827 finish_member_declaration (fco);
7829 obstack_free (&declarator_obstack, p);
7833 /* Parse the body of a lambda expression, which is simply
7835 compound-statement
7837 but which requires special handling.
7838 LAMBDA_EXPR is the current representation of the lambda expression. */
7840 static void
7841 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7843 bool nested = (current_function_decl != NULL_TREE);
7844 if (nested)
7845 push_function_context ();
7847 /* Finish the function call operator
7848 - class_specifier
7849 + late_parsing_for_member
7850 + function_definition_after_declarator
7851 + ctor_initializer_opt_and_function_body */
7853 tree fco = lambda_function (lambda_expr);
7854 tree body;
7855 bool done = false;
7857 /* Let the front end know that we are going to be defining this
7858 function. */
7859 start_preparsed_function (fco,
7860 NULL_TREE,
7861 SF_PRE_PARSED | SF_INCLASS_INLINE);
7863 start_lambda_scope (fco);
7864 body = begin_function_body ();
7866 /* 5.1.1.4 of the standard says:
7867 If a lambda-expression does not include a trailing-return-type, it
7868 is as if the trailing-return-type denotes the following type:
7869 * if the compound-statement is of the form
7870 { return attribute-specifier [opt] expression ; }
7871 the type of the returned expression after lvalue-to-rvalue
7872 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7873 (_conv.array_ 4.2), and function-to-pointer conversion
7874 (_conv.func_ 4.3);
7875 * otherwise, void. */
7877 /* In a lambda that has neither a lambda-return-type-clause
7878 nor a deducible form, errors should be reported for return statements
7879 in the body. Since we used void as the placeholder return type, parsing
7880 the body as usual will give such desired behavior. */
7881 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7882 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7883 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7884 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7886 tree compound_stmt;
7887 tree expr = NULL_TREE;
7888 cp_id_kind idk = CP_ID_KIND_NONE;
7890 /* Parse tentatively in case there's more after the initial return
7891 statement. */
7892 cp_parser_parse_tentatively (parser);
7894 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7895 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7897 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7899 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7900 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7902 if (cp_parser_parse_definitely (parser))
7904 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7906 compound_stmt = begin_compound_stmt (0);
7907 /* Will get error here if type not deduced yet. */
7908 finish_return_stmt (expr);
7909 finish_compound_stmt (compound_stmt);
7911 done = true;
7915 if (!done)
7917 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7918 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7919 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7920 cp_parser_compound_stmt does not pass it. */
7921 cp_parser_function_body (parser);
7922 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7925 finish_function_body (body);
7926 finish_lambda_scope ();
7928 /* Finish the function and generate code for it if necessary. */
7929 expand_or_defer_fn (finish_function (/*inline*/2));
7932 if (nested)
7933 pop_function_context();
7936 /* Statements [gram.stmt.stmt] */
7938 /* Parse a statement.
7940 statement:
7941 labeled-statement
7942 expression-statement
7943 compound-statement
7944 selection-statement
7945 iteration-statement
7946 jump-statement
7947 declaration-statement
7948 try-block
7950 IN_COMPOUND is true when the statement is nested inside a
7951 cp_parser_compound_statement; this matters for certain pragmas.
7953 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7954 is a (possibly labeled) if statement which is not enclosed in braces
7955 and has an else clause. This is used to implement -Wparentheses. */
7957 static void
7958 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7959 bool in_compound, bool *if_p)
7961 tree statement;
7962 cp_token *token;
7963 location_t statement_location;
7965 restart:
7966 if (if_p != NULL)
7967 *if_p = false;
7968 /* There is no statement yet. */
7969 statement = NULL_TREE;
7970 /* Peek at the next token. */
7971 token = cp_lexer_peek_token (parser->lexer);
7972 /* Remember the location of the first token in the statement. */
7973 statement_location = token->location;
7974 /* If this is a keyword, then that will often determine what kind of
7975 statement we have. */
7976 if (token->type == CPP_KEYWORD)
7978 enum rid keyword = token->keyword;
7980 switch (keyword)
7982 case RID_CASE:
7983 case RID_DEFAULT:
7984 /* Looks like a labeled-statement with a case label.
7985 Parse the label, and then use tail recursion to parse
7986 the statement. */
7987 cp_parser_label_for_labeled_statement (parser);
7988 goto restart;
7990 case RID_IF:
7991 case RID_SWITCH:
7992 statement = cp_parser_selection_statement (parser, if_p);
7993 break;
7995 case RID_WHILE:
7996 case RID_DO:
7997 case RID_FOR:
7998 statement = cp_parser_iteration_statement (parser);
7999 break;
8001 case RID_BREAK:
8002 case RID_CONTINUE:
8003 case RID_RETURN:
8004 case RID_GOTO:
8005 statement = cp_parser_jump_statement (parser);
8006 break;
8008 /* Objective-C++ exception-handling constructs. */
8009 case RID_AT_TRY:
8010 case RID_AT_CATCH:
8011 case RID_AT_FINALLY:
8012 case RID_AT_SYNCHRONIZED:
8013 case RID_AT_THROW:
8014 statement = cp_parser_objc_statement (parser);
8015 break;
8017 case RID_TRY:
8018 statement = cp_parser_try_block (parser);
8019 break;
8021 case RID_NAMESPACE:
8022 /* This must be a namespace alias definition. */
8023 cp_parser_declaration_statement (parser);
8024 return;
8026 default:
8027 /* It might be a keyword like `int' that can start a
8028 declaration-statement. */
8029 break;
8032 else if (token->type == CPP_NAME)
8034 /* If the next token is a `:', then we are looking at a
8035 labeled-statement. */
8036 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8037 if (token->type == CPP_COLON)
8039 /* Looks like a labeled-statement with an ordinary label.
8040 Parse the label, and then use tail recursion to parse
8041 the statement. */
8042 cp_parser_label_for_labeled_statement (parser);
8043 goto restart;
8046 /* Anything that starts with a `{' must be a compound-statement. */
8047 else if (token->type == CPP_OPEN_BRACE)
8048 statement = cp_parser_compound_statement (parser, NULL, false);
8049 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8050 a statement all its own. */
8051 else if (token->type == CPP_PRAGMA)
8053 /* Only certain OpenMP pragmas are attached to statements, and thus
8054 are considered statements themselves. All others are not. In
8055 the context of a compound, accept the pragma as a "statement" and
8056 return so that we can check for a close brace. Otherwise we
8057 require a real statement and must go back and read one. */
8058 if (in_compound)
8059 cp_parser_pragma (parser, pragma_compound);
8060 else if (!cp_parser_pragma (parser, pragma_stmt))
8061 goto restart;
8062 return;
8064 else if (token->type == CPP_EOF)
8066 cp_parser_error (parser, "expected statement");
8067 return;
8070 /* Everything else must be a declaration-statement or an
8071 expression-statement. Try for the declaration-statement
8072 first, unless we are looking at a `;', in which case we know that
8073 we have an expression-statement. */
8074 if (!statement)
8076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8078 cp_parser_parse_tentatively (parser);
8079 /* Try to parse the declaration-statement. */
8080 cp_parser_declaration_statement (parser);
8081 /* If that worked, we're done. */
8082 if (cp_parser_parse_definitely (parser))
8083 return;
8085 /* Look for an expression-statement instead. */
8086 statement = cp_parser_expression_statement (parser, in_statement_expr);
8089 /* Set the line number for the statement. */
8090 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8091 SET_EXPR_LOCATION (statement, statement_location);
8094 /* Parse the label for a labeled-statement, i.e.
8096 identifier :
8097 case constant-expression :
8098 default :
8100 GNU Extension:
8101 case constant-expression ... constant-expression : statement
8103 When a label is parsed without errors, the label is added to the
8104 parse tree by the finish_* functions, so this function doesn't
8105 have to return the label. */
8107 static void
8108 cp_parser_label_for_labeled_statement (cp_parser* parser)
8110 cp_token *token;
8111 tree label = NULL_TREE;
8113 /* The next token should be an identifier. */
8114 token = cp_lexer_peek_token (parser->lexer);
8115 if (token->type != CPP_NAME
8116 && token->type != CPP_KEYWORD)
8118 cp_parser_error (parser, "expected labeled-statement");
8119 return;
8122 switch (token->keyword)
8124 case RID_CASE:
8126 tree expr, expr_hi;
8127 cp_token *ellipsis;
8129 /* Consume the `case' token. */
8130 cp_lexer_consume_token (parser->lexer);
8131 /* Parse the constant-expression. */
8132 expr = cp_parser_constant_expression (parser,
8133 /*allow_non_constant_p=*/false,
8134 NULL);
8136 ellipsis = cp_lexer_peek_token (parser->lexer);
8137 if (ellipsis->type == CPP_ELLIPSIS)
8139 /* Consume the `...' token. */
8140 cp_lexer_consume_token (parser->lexer);
8141 expr_hi =
8142 cp_parser_constant_expression (parser,
8143 /*allow_non_constant_p=*/false,
8144 NULL);
8145 /* We don't need to emit warnings here, as the common code
8146 will do this for us. */
8148 else
8149 expr_hi = NULL_TREE;
8151 if (parser->in_switch_statement_p)
8152 finish_case_label (token->location, expr, expr_hi);
8153 else
8154 error_at (token->location,
8155 "case label %qE not within a switch statement",
8156 expr);
8158 break;
8160 case RID_DEFAULT:
8161 /* Consume the `default' token. */
8162 cp_lexer_consume_token (parser->lexer);
8164 if (parser->in_switch_statement_p)
8165 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8166 else
8167 error_at (token->location, "case label not within a switch statement");
8168 break;
8170 default:
8171 /* Anything else must be an ordinary label. */
8172 label = finish_label_stmt (cp_parser_identifier (parser));
8173 break;
8176 /* Require the `:' token. */
8177 cp_parser_require (parser, CPP_COLON, RT_COLON);
8179 /* An ordinary label may optionally be followed by attributes.
8180 However, this is only permitted if the attributes are then
8181 followed by a semicolon. This is because, for backward
8182 compatibility, when parsing
8183 lab: __attribute__ ((unused)) int i;
8184 we want the attribute to attach to "i", not "lab". */
8185 if (label != NULL_TREE
8186 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8188 tree attrs;
8190 cp_parser_parse_tentatively (parser);
8191 attrs = cp_parser_attributes_opt (parser);
8192 if (attrs == NULL_TREE
8193 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8194 cp_parser_abort_tentative_parse (parser);
8195 else if (!cp_parser_parse_definitely (parser))
8197 else
8198 cplus_decl_attributes (&label, attrs, 0);
8202 /* Parse an expression-statement.
8204 expression-statement:
8205 expression [opt] ;
8207 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8208 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8209 indicates whether this expression-statement is part of an
8210 expression statement. */
8212 static tree
8213 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8215 tree statement = NULL_TREE;
8216 cp_token *token = cp_lexer_peek_token (parser->lexer);
8218 /* If the next token is a ';', then there is no expression
8219 statement. */
8220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8221 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8223 /* Give a helpful message for "A<T>::type t;" and the like. */
8224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8225 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8227 if (TREE_CODE (statement) == SCOPE_REF)
8228 error_at (token->location, "need %<typename%> before %qE because "
8229 "%qT is a dependent scope",
8230 statement, TREE_OPERAND (statement, 0));
8231 else if (is_overloaded_fn (statement)
8232 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8234 /* A::A a; */
8235 tree fn = get_first_fn (statement);
8236 error_at (token->location,
8237 "%<%T::%D%> names the constructor, not the type",
8238 DECL_CONTEXT (fn), DECL_NAME (fn));
8242 /* Consume the final `;'. */
8243 cp_parser_consume_semicolon_at_end_of_statement (parser);
8245 if (in_statement_expr
8246 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8247 /* This is the final expression statement of a statement
8248 expression. */
8249 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8250 else if (statement)
8251 statement = finish_expr_stmt (statement);
8252 else
8253 finish_stmt ();
8255 return statement;
8258 /* Parse a compound-statement.
8260 compound-statement:
8261 { statement-seq [opt] }
8263 GNU extension:
8265 compound-statement:
8266 { label-declaration-seq [opt] statement-seq [opt] }
8268 label-declaration-seq:
8269 label-declaration
8270 label-declaration-seq label-declaration
8272 Returns a tree representing the statement. */
8274 static tree
8275 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8276 bool in_try)
8278 tree compound_stmt;
8280 /* Consume the `{'. */
8281 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8282 return error_mark_node;
8283 /* Begin the compound-statement. */
8284 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8285 /* If the next keyword is `__label__' we have a label declaration. */
8286 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8287 cp_parser_label_declaration (parser);
8288 /* Parse an (optional) statement-seq. */
8289 cp_parser_statement_seq_opt (parser, in_statement_expr);
8290 /* Finish the compound-statement. */
8291 finish_compound_stmt (compound_stmt);
8292 /* Consume the `}'. */
8293 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8295 return compound_stmt;
8298 /* Parse an (optional) statement-seq.
8300 statement-seq:
8301 statement
8302 statement-seq [opt] statement */
8304 static void
8305 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8307 /* Scan statements until there aren't any more. */
8308 while (true)
8310 cp_token *token = cp_lexer_peek_token (parser->lexer);
8312 /* If we are looking at a `}', then we have run out of
8313 statements; the same is true if we have reached the end
8314 of file, or have stumbled upon a stray '@end'. */
8315 if (token->type == CPP_CLOSE_BRACE
8316 || token->type == CPP_EOF
8317 || token->type == CPP_PRAGMA_EOL
8318 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8319 break;
8321 /* If we are in a compound statement and find 'else' then
8322 something went wrong. */
8323 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8325 if (parser->in_statement & IN_IF_STMT)
8326 break;
8327 else
8329 token = cp_lexer_consume_token (parser->lexer);
8330 error_at (token->location, "%<else%> without a previous %<if%>");
8334 /* Parse the statement. */
8335 cp_parser_statement (parser, in_statement_expr, true, NULL);
8339 /* Parse a selection-statement.
8341 selection-statement:
8342 if ( condition ) statement
8343 if ( condition ) statement else statement
8344 switch ( condition ) statement
8346 Returns the new IF_STMT or SWITCH_STMT.
8348 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8349 is a (possibly labeled) if statement which is not enclosed in
8350 braces and has an else clause. This is used to implement
8351 -Wparentheses. */
8353 static tree
8354 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8356 cp_token *token;
8357 enum rid keyword;
8359 if (if_p != NULL)
8360 *if_p = false;
8362 /* Peek at the next token. */
8363 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8365 /* See what kind of keyword it is. */
8366 keyword = token->keyword;
8367 switch (keyword)
8369 case RID_IF:
8370 case RID_SWITCH:
8372 tree statement;
8373 tree condition;
8375 /* Look for the `('. */
8376 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8378 cp_parser_skip_to_end_of_statement (parser);
8379 return error_mark_node;
8382 /* Begin the selection-statement. */
8383 if (keyword == RID_IF)
8384 statement = begin_if_stmt ();
8385 else
8386 statement = begin_switch_stmt ();
8388 /* Parse the condition. */
8389 condition = cp_parser_condition (parser);
8390 /* Look for the `)'. */
8391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8392 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8393 /*consume_paren=*/true);
8395 if (keyword == RID_IF)
8397 bool nested_if;
8398 unsigned char in_statement;
8400 /* Add the condition. */
8401 finish_if_stmt_cond (condition, statement);
8403 /* Parse the then-clause. */
8404 in_statement = parser->in_statement;
8405 parser->in_statement |= IN_IF_STMT;
8406 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8408 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8409 add_stmt (build_empty_stmt (loc));
8410 cp_lexer_consume_token (parser->lexer);
8411 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8412 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8413 "empty body in an %<if%> statement");
8414 nested_if = false;
8416 else
8417 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8418 parser->in_statement = in_statement;
8420 finish_then_clause (statement);
8422 /* If the next token is `else', parse the else-clause. */
8423 if (cp_lexer_next_token_is_keyword (parser->lexer,
8424 RID_ELSE))
8426 /* Consume the `else' keyword. */
8427 cp_lexer_consume_token (parser->lexer);
8428 begin_else_clause (statement);
8429 /* Parse the else-clause. */
8430 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8432 location_t loc;
8433 loc = cp_lexer_peek_token (parser->lexer)->location;
8434 warning_at (loc,
8435 OPT_Wempty_body, "suggest braces around "
8436 "empty body in an %<else%> statement");
8437 add_stmt (build_empty_stmt (loc));
8438 cp_lexer_consume_token (parser->lexer);
8440 else
8441 cp_parser_implicitly_scoped_statement (parser, NULL);
8443 finish_else_clause (statement);
8445 /* If we are currently parsing a then-clause, then
8446 IF_P will not be NULL. We set it to true to
8447 indicate that this if statement has an else clause.
8448 This may trigger the Wparentheses warning below
8449 when we get back up to the parent if statement. */
8450 if (if_p != NULL)
8451 *if_p = true;
8453 else
8455 /* This if statement does not have an else clause. If
8456 NESTED_IF is true, then the then-clause is an if
8457 statement which does have an else clause. We warn
8458 about the potential ambiguity. */
8459 if (nested_if)
8460 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8461 "suggest explicit braces to avoid ambiguous"
8462 " %<else%>");
8465 /* Now we're all done with the if-statement. */
8466 finish_if_stmt (statement);
8468 else
8470 bool in_switch_statement_p;
8471 unsigned char in_statement;
8473 /* Add the condition. */
8474 finish_switch_cond (condition, statement);
8476 /* Parse the body of the switch-statement. */
8477 in_switch_statement_p = parser->in_switch_statement_p;
8478 in_statement = parser->in_statement;
8479 parser->in_switch_statement_p = true;
8480 parser->in_statement |= IN_SWITCH_STMT;
8481 cp_parser_implicitly_scoped_statement (parser, NULL);
8482 parser->in_switch_statement_p = in_switch_statement_p;
8483 parser->in_statement = in_statement;
8485 /* Now we're all done with the switch-statement. */
8486 finish_switch_stmt (statement);
8489 return statement;
8491 break;
8493 default:
8494 cp_parser_error (parser, "expected selection-statement");
8495 return error_mark_node;
8499 /* Parse a condition.
8501 condition:
8502 expression
8503 type-specifier-seq declarator = initializer-clause
8504 type-specifier-seq declarator braced-init-list
8506 GNU Extension:
8508 condition:
8509 type-specifier-seq declarator asm-specification [opt]
8510 attributes [opt] = assignment-expression
8512 Returns the expression that should be tested. */
8514 static tree
8515 cp_parser_condition (cp_parser* parser)
8517 cp_decl_specifier_seq type_specifiers;
8518 const char *saved_message;
8519 int declares_class_or_enum;
8521 /* Try the declaration first. */
8522 cp_parser_parse_tentatively (parser);
8523 /* New types are not allowed in the type-specifier-seq for a
8524 condition. */
8525 saved_message = parser->type_definition_forbidden_message;
8526 parser->type_definition_forbidden_message
8527 = G_("types may not be defined in conditions");
8528 /* Parse the type-specifier-seq. */
8529 cp_parser_decl_specifier_seq (parser,
8530 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8531 &type_specifiers,
8532 &declares_class_or_enum);
8533 /* Restore the saved message. */
8534 parser->type_definition_forbidden_message = saved_message;
8535 /* If all is well, we might be looking at a declaration. */
8536 if (!cp_parser_error_occurred (parser))
8538 tree decl;
8539 tree asm_specification;
8540 tree attributes;
8541 cp_declarator *declarator;
8542 tree initializer = NULL_TREE;
8544 /* Parse the declarator. */
8545 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8546 /*ctor_dtor_or_conv_p=*/NULL,
8547 /*parenthesized_p=*/NULL,
8548 /*member_p=*/false);
8549 /* Parse the attributes. */
8550 attributes = cp_parser_attributes_opt (parser);
8551 /* Parse the asm-specification. */
8552 asm_specification = cp_parser_asm_specification_opt (parser);
8553 /* If the next token is not an `=' or '{', then we might still be
8554 looking at an expression. For example:
8556 if (A(a).x)
8558 looks like a decl-specifier-seq and a declarator -- but then
8559 there is no `=', so this is an expression. */
8560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8561 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8562 cp_parser_simulate_error (parser);
8564 /* If we did see an `=' or '{', then we are looking at a declaration
8565 for sure. */
8566 if (cp_parser_parse_definitely (parser))
8568 tree pushed_scope;
8569 bool non_constant_p;
8570 bool flags = LOOKUP_ONLYCONVERTING;
8572 /* Create the declaration. */
8573 decl = start_decl (declarator, &type_specifiers,
8574 /*initialized_p=*/true,
8575 attributes, /*prefix_attributes=*/NULL_TREE,
8576 &pushed_scope);
8578 /* Parse the initializer. */
8579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8581 initializer = cp_parser_braced_list (parser, &non_constant_p);
8582 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8583 flags = 0;
8585 else
8587 /* Consume the `='. */
8588 cp_parser_require (parser, CPP_EQ, RT_EQ);
8589 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8591 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8592 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8594 if (!non_constant_p)
8595 initializer = fold_non_dependent_expr (initializer);
8597 /* Process the initializer. */
8598 cp_finish_decl (decl,
8599 initializer, !non_constant_p,
8600 asm_specification,
8601 flags);
8603 if (pushed_scope)
8604 pop_scope (pushed_scope);
8606 return convert_from_reference (decl);
8609 /* If we didn't even get past the declarator successfully, we are
8610 definitely not looking at a declaration. */
8611 else
8612 cp_parser_abort_tentative_parse (parser);
8614 /* Otherwise, we are looking at an expression. */
8615 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8618 /* Parses a traditional for-statement until the closing ')', not included. */
8620 static tree
8621 cp_parser_c_for (cp_parser *parser)
8623 /* Normal for loop */
8624 tree stmt;
8625 tree condition = NULL_TREE;
8626 tree expression = NULL_TREE;
8628 /* Begin the for-statement. */
8629 stmt = begin_for_stmt ();
8631 /* Parse the initialization. */
8632 cp_parser_for_init_statement (parser);
8633 finish_for_init_stmt (stmt);
8635 /* If there's a condition, process it. */
8636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8637 condition = cp_parser_condition (parser);
8638 finish_for_cond (condition, stmt);
8639 /* Look for the `;'. */
8640 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8642 /* If there's an expression, process it. */
8643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8644 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8645 finish_for_expr (expression, stmt);
8647 return stmt;
8650 /* Tries to parse a range-based for-statement:
8652 range-based-for:
8653 type-specifier-seq declarator : expression
8655 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8656 expression. Note that the *DECL is returned unfinished, so
8657 later you should call cp_finish_decl().
8659 Returns TRUE iff a range-based for is parsed. */
8661 static tree
8662 cp_parser_range_for (cp_parser *parser)
8664 tree stmt, range_decl, range_expr;
8665 cp_decl_specifier_seq type_specifiers;
8666 cp_declarator *declarator;
8667 const char *saved_message;
8668 tree attributes, pushed_scope;
8670 cp_parser_parse_tentatively (parser);
8671 /* New types are not allowed in the type-specifier-seq for a
8672 range-based for loop. */
8673 saved_message = parser->type_definition_forbidden_message;
8674 parser->type_definition_forbidden_message
8675 = G_("types may not be defined in range-based for loops");
8676 /* Parse the type-specifier-seq. */
8677 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8678 /*is_trailing_return=*/false,
8679 &type_specifiers);
8680 /* Restore the saved message. */
8681 parser->type_definition_forbidden_message = saved_message;
8682 /* If all is well, we might be looking at a declaration. */
8683 if (cp_parser_error_occurred (parser))
8685 cp_parser_abort_tentative_parse (parser);
8686 return NULL_TREE;
8688 /* Parse the declarator. */
8689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8690 /*ctor_dtor_or_conv_p=*/NULL,
8691 /*parenthesized_p=*/NULL,
8692 /*member_p=*/false);
8693 /* Parse the attributes. */
8694 attributes = cp_parser_attributes_opt (parser);
8695 /* The next token should be `:'. */
8696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8697 cp_parser_simulate_error (parser);
8699 /* Check if it is a range-based for */
8700 if (!cp_parser_parse_definitely (parser))
8701 return NULL_TREE;
8703 cp_parser_require (parser, CPP_COLON, RT_COLON);
8704 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8706 bool expr_non_constant_p;
8707 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8709 else
8710 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8712 /* If in template, STMT is converted to a normal for-statements
8713 at instantiation. If not, it is done just ahead. */
8714 if (processing_template_decl)
8715 stmt = begin_range_for_stmt ();
8716 else
8717 stmt = begin_for_stmt ();
8719 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8720 range_decl = start_decl (declarator, &type_specifiers,
8721 /*initialized_p=*/SD_INITIALIZED,
8722 attributes, /*prefix_attributes=*/NULL_TREE,
8723 &pushed_scope);
8724 /* No scope allowed here */
8725 pop_scope (pushed_scope);
8727 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8728 finish_range_for_decl (stmt, range_decl, range_expr);
8729 else
8730 /* Convert the range-based for loop into a normal for-statement. */
8731 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8733 return stmt;
8736 /* Converts a range-based for-statement into a normal
8737 for-statement, as per the definition.
8739 for (RANGE_DECL : RANGE_EXPR)
8740 BLOCK
8742 should be equivalent to:
8745 auto &&__range = RANGE_EXPR;
8746 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8747 __begin != __end;
8748 ++__begin)
8750 RANGE_DECL = *__begin;
8751 BLOCK
8755 If RANGE_EXPR is an array:
8756 BEGIN_EXPR = __range
8757 END_EXPR = __range + ARRAY_SIZE(__range)
8758 Else:
8759 BEGIN_EXPR = begin(__range)
8760 END_EXPR = end(__range);
8762 When calling begin()/end() we must use argument dependent
8763 lookup, but always considering 'std' as an associated namespace. */
8765 tree
8766 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8768 tree range_type, range_temp;
8769 tree begin, end;
8770 tree iter_type, begin_expr, end_expr;
8771 tree condition, expression;
8773 /* Find out the type deduced by the declaration
8774 * `auto &&__range = range_expr' */
8775 range_type = cp_build_reference_type (make_auto (), true);
8776 range_type = do_auto_deduction (range_type, range_expr,
8777 type_uses_auto (range_type));
8779 /* Create the __range variable */
8780 range_temp = build_decl (input_location, VAR_DECL,
8781 get_identifier ("__for_range"), range_type);
8782 TREE_USED (range_temp) = 1;
8783 DECL_ARTIFICIAL (range_temp) = 1;
8784 pushdecl (range_temp);
8785 cp_finish_decl (range_temp, range_expr,
8786 /*is_constant_init*/false, NULL_TREE,
8787 LOOKUP_ONLYCONVERTING);
8789 range_temp = convert_from_reference (range_temp);
8791 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8793 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8794 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8795 begin_expr = range_temp;
8796 end_expr
8797 = build_binary_op (input_location, PLUS_EXPR,
8798 range_temp,
8799 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8801 else
8803 /* If it is not an array, we must call begin(__range)/end__range() */
8804 VEC(tree,gc) *vec;
8806 begin_expr = get_identifier ("begin");
8807 vec = make_tree_vector ();
8808 VEC_safe_push (tree, gc, vec, range_temp);
8809 begin_expr = perform_koenig_lookup (begin_expr, vec,
8810 /*include_std=*/true);
8811 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8812 tf_warning_or_error);
8813 release_tree_vector (vec);
8815 end_expr = get_identifier ("end");
8816 vec = make_tree_vector ();
8817 VEC_safe_push (tree, gc, vec, range_temp);
8818 end_expr = perform_koenig_lookup (end_expr, vec,
8819 /*include_std=*/true);
8820 end_expr = finish_call_expr (end_expr, &vec, false, true,
8821 tf_warning_or_error);
8822 release_tree_vector (vec);
8824 /* The unqualified type of the __begin and __end temporaries should
8825 * be the same as required by the multiple auto declaration */
8826 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8827 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8828 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8829 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8832 /* The new for initialization statement */
8833 begin = build_decl (input_location, VAR_DECL,
8834 get_identifier ("__for_begin"), iter_type);
8835 TREE_USED (begin) = 1;
8836 DECL_ARTIFICIAL (begin) = 1;
8837 pushdecl (begin);
8838 cp_finish_decl (begin, begin_expr,
8839 /*is_constant_init*/false, NULL_TREE,
8840 LOOKUP_ONLYCONVERTING);
8842 end = build_decl (input_location, VAR_DECL,
8843 get_identifier ("__for_end"), iter_type);
8844 TREE_USED (end) = 1;
8845 DECL_ARTIFICIAL (end) = 1;
8846 pushdecl (end);
8847 cp_finish_decl (end, end_expr,
8848 /*is_constant_init*/false, NULL_TREE,
8849 LOOKUP_ONLYCONVERTING);
8851 finish_for_init_stmt (statement);
8853 /* The new for condition */
8854 condition = build_x_binary_op (NE_EXPR,
8855 begin, ERROR_MARK,
8856 end, ERROR_MARK,
8857 NULL, tf_warning_or_error);
8858 finish_for_cond (condition, statement);
8860 /* The new increment expression */
8861 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8862 finish_for_expr (expression, statement);
8864 /* The declaration is initialized with *__begin inside the loop body */
8865 cp_finish_decl (range_decl,
8866 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8867 /*is_constant_init*/false, NULL_TREE,
8868 LOOKUP_ONLYCONVERTING);
8870 return statement;
8874 /* Parse an iteration-statement.
8876 iteration-statement:
8877 while ( condition ) statement
8878 do statement while ( expression ) ;
8879 for ( for-init-statement condition [opt] ; expression [opt] )
8880 statement
8882 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8884 static tree
8885 cp_parser_iteration_statement (cp_parser* parser)
8887 cp_token *token;
8888 enum rid keyword;
8889 tree statement;
8890 unsigned char in_statement;
8892 /* Peek at the next token. */
8893 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8894 if (!token)
8895 return error_mark_node;
8897 /* Remember whether or not we are already within an iteration
8898 statement. */
8899 in_statement = parser->in_statement;
8901 /* See what kind of keyword it is. */
8902 keyword = token->keyword;
8903 switch (keyword)
8905 case RID_WHILE:
8907 tree condition;
8909 /* Begin the while-statement. */
8910 statement = begin_while_stmt ();
8911 /* Look for the `('. */
8912 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8913 /* Parse the condition. */
8914 condition = cp_parser_condition (parser);
8915 finish_while_stmt_cond (condition, statement);
8916 /* Look for the `)'. */
8917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8918 /* Parse the dependent statement. */
8919 parser->in_statement = IN_ITERATION_STMT;
8920 cp_parser_already_scoped_statement (parser);
8921 parser->in_statement = in_statement;
8922 /* We're done with the while-statement. */
8923 finish_while_stmt (statement);
8925 break;
8927 case RID_DO:
8929 tree expression;
8931 /* Begin the do-statement. */
8932 statement = begin_do_stmt ();
8933 /* Parse the body of the do-statement. */
8934 parser->in_statement = IN_ITERATION_STMT;
8935 cp_parser_implicitly_scoped_statement (parser, NULL);
8936 parser->in_statement = in_statement;
8937 finish_do_body (statement);
8938 /* Look for the `while' keyword. */
8939 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8940 /* Look for the `('. */
8941 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8942 /* Parse the expression. */
8943 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8944 /* We're done with the do-statement. */
8945 finish_do_stmt (expression, statement);
8946 /* Look for the `)'. */
8947 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8948 /* Look for the `;'. */
8949 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8951 break;
8953 case RID_FOR:
8955 /* Look for the `('. */
8956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8958 if (cxx_dialect == cxx0x)
8959 statement = cp_parser_range_for (parser);
8960 else
8961 statement = NULL_TREE;
8962 if (statement == NULL_TREE)
8963 statement = cp_parser_c_for (parser);
8965 /* Look for the `)'. */
8966 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8968 /* Parse the body of the for-statement. */
8969 parser->in_statement = IN_ITERATION_STMT;
8970 cp_parser_already_scoped_statement (parser);
8971 parser->in_statement = in_statement;
8973 /* We're done with the for-statement. */
8974 finish_for_stmt (statement);
8976 break;
8978 default:
8979 cp_parser_error (parser, "expected iteration-statement");
8980 statement = error_mark_node;
8981 break;
8984 return statement;
8987 /* Parse a for-init-statement.
8989 for-init-statement:
8990 expression-statement
8991 simple-declaration */
8993 static void
8994 cp_parser_for_init_statement (cp_parser* parser)
8996 /* If the next token is a `;', then we have an empty
8997 expression-statement. Grammatically, this is also a
8998 simple-declaration, but an invalid one, because it does not
8999 declare anything. Therefore, if we did not handle this case
9000 specially, we would issue an error message about an invalid
9001 declaration. */
9002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9004 /* We're going to speculatively look for a declaration, falling back
9005 to an expression, if necessary. */
9006 cp_parser_parse_tentatively (parser);
9007 /* Parse the declaration. */
9008 cp_parser_simple_declaration (parser,
9009 /*function_definition_allowed_p=*/false);
9010 /* If the tentative parse failed, then we shall need to look for an
9011 expression-statement. */
9012 if (cp_parser_parse_definitely (parser))
9013 return;
9016 cp_parser_expression_statement (parser, NULL_TREE);
9019 /* Parse a jump-statement.
9021 jump-statement:
9022 break ;
9023 continue ;
9024 return expression [opt] ;
9025 return braced-init-list ;
9026 goto identifier ;
9028 GNU extension:
9030 jump-statement:
9031 goto * expression ;
9033 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9035 static tree
9036 cp_parser_jump_statement (cp_parser* parser)
9038 tree statement = error_mark_node;
9039 cp_token *token;
9040 enum rid keyword;
9041 unsigned char in_statement;
9043 /* Peek at the next token. */
9044 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9045 if (!token)
9046 return error_mark_node;
9048 /* See what kind of keyword it is. */
9049 keyword = token->keyword;
9050 switch (keyword)
9052 case RID_BREAK:
9053 in_statement = parser->in_statement & ~IN_IF_STMT;
9054 switch (in_statement)
9056 case 0:
9057 error_at (token->location, "break statement not within loop or switch");
9058 break;
9059 default:
9060 gcc_assert ((in_statement & IN_SWITCH_STMT)
9061 || in_statement == IN_ITERATION_STMT);
9062 statement = finish_break_stmt ();
9063 break;
9064 case IN_OMP_BLOCK:
9065 error_at (token->location, "invalid exit from OpenMP structured block");
9066 break;
9067 case IN_OMP_FOR:
9068 error_at (token->location, "break statement used with OpenMP for loop");
9069 break;
9071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9072 break;
9074 case RID_CONTINUE:
9075 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9077 case 0:
9078 error_at (token->location, "continue statement not within a loop");
9079 break;
9080 case IN_ITERATION_STMT:
9081 case IN_OMP_FOR:
9082 statement = finish_continue_stmt ();
9083 break;
9084 case IN_OMP_BLOCK:
9085 error_at (token->location, "invalid exit from OpenMP structured block");
9086 break;
9087 default:
9088 gcc_unreachable ();
9090 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9091 break;
9093 case RID_RETURN:
9095 tree expr;
9096 bool expr_non_constant_p;
9098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9100 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9101 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9103 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9104 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9105 else
9106 /* If the next token is a `;', then there is no
9107 expression. */
9108 expr = NULL_TREE;
9109 /* Build the return-statement. */
9110 statement = finish_return_stmt (expr);
9111 /* Look for the final `;'. */
9112 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9114 break;
9116 case RID_GOTO:
9117 /* Create the goto-statement. */
9118 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9120 /* Issue a warning about this use of a GNU extension. */
9121 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9122 /* Consume the '*' token. */
9123 cp_lexer_consume_token (parser->lexer);
9124 /* Parse the dependent expression. */
9125 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9127 else
9128 finish_goto_stmt (cp_parser_identifier (parser));
9129 /* Look for the final `;'. */
9130 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9131 break;
9133 default:
9134 cp_parser_error (parser, "expected jump-statement");
9135 break;
9138 return statement;
9141 /* Parse a declaration-statement.
9143 declaration-statement:
9144 block-declaration */
9146 static void
9147 cp_parser_declaration_statement (cp_parser* parser)
9149 void *p;
9151 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9152 p = obstack_alloc (&declarator_obstack, 0);
9154 /* Parse the block-declaration. */
9155 cp_parser_block_declaration (parser, /*statement_p=*/true);
9157 /* Free any declarators allocated. */
9158 obstack_free (&declarator_obstack, p);
9160 /* Finish off the statement. */
9161 finish_stmt ();
9164 /* Some dependent statements (like `if (cond) statement'), are
9165 implicitly in their own scope. In other words, if the statement is
9166 a single statement (as opposed to a compound-statement), it is
9167 none-the-less treated as if it were enclosed in braces. Any
9168 declarations appearing in the dependent statement are out of scope
9169 after control passes that point. This function parses a statement,
9170 but ensures that is in its own scope, even if it is not a
9171 compound-statement.
9173 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9174 is a (possibly labeled) if statement which is not enclosed in
9175 braces and has an else clause. This is used to implement
9176 -Wparentheses.
9178 Returns the new statement. */
9180 static tree
9181 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9183 tree statement;
9185 if (if_p != NULL)
9186 *if_p = false;
9188 /* Mark if () ; with a special NOP_EXPR. */
9189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9192 cp_lexer_consume_token (parser->lexer);
9193 statement = add_stmt (build_empty_stmt (loc));
9195 /* if a compound is opened, we simply parse the statement directly. */
9196 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9197 statement = cp_parser_compound_statement (parser, NULL, false);
9198 /* If the token is not a `{', then we must take special action. */
9199 else
9201 /* Create a compound-statement. */
9202 statement = begin_compound_stmt (0);
9203 /* Parse the dependent-statement. */
9204 cp_parser_statement (parser, NULL_TREE, false, if_p);
9205 /* Finish the dummy compound-statement. */
9206 finish_compound_stmt (statement);
9209 /* Return the statement. */
9210 return statement;
9213 /* For some dependent statements (like `while (cond) statement'), we
9214 have already created a scope. Therefore, even if the dependent
9215 statement is a compound-statement, we do not want to create another
9216 scope. */
9218 static void
9219 cp_parser_already_scoped_statement (cp_parser* parser)
9221 /* If the token is a `{', then we must take special action. */
9222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9223 cp_parser_statement (parser, NULL_TREE, false, NULL);
9224 else
9226 /* Avoid calling cp_parser_compound_statement, so that we
9227 don't create a new scope. Do everything else by hand. */
9228 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9229 /* If the next keyword is `__label__' we have a label declaration. */
9230 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9231 cp_parser_label_declaration (parser);
9232 /* Parse an (optional) statement-seq. */
9233 cp_parser_statement_seq_opt (parser, NULL_TREE);
9234 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9238 /* Declarations [gram.dcl.dcl] */
9240 /* Parse an optional declaration-sequence.
9242 declaration-seq:
9243 declaration
9244 declaration-seq declaration */
9246 static void
9247 cp_parser_declaration_seq_opt (cp_parser* parser)
9249 while (true)
9251 cp_token *token;
9253 token = cp_lexer_peek_token (parser->lexer);
9255 if (token->type == CPP_CLOSE_BRACE
9256 || token->type == CPP_EOF
9257 || token->type == CPP_PRAGMA_EOL)
9258 break;
9260 if (token->type == CPP_SEMICOLON)
9262 /* A declaration consisting of a single semicolon is
9263 invalid. Allow it unless we're being pedantic. */
9264 cp_lexer_consume_token (parser->lexer);
9265 if (!in_system_header)
9266 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9267 continue;
9270 /* If we're entering or exiting a region that's implicitly
9271 extern "C", modify the lang context appropriately. */
9272 if (!parser->implicit_extern_c && token->implicit_extern_c)
9274 push_lang_context (lang_name_c);
9275 parser->implicit_extern_c = true;
9277 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9279 pop_lang_context ();
9280 parser->implicit_extern_c = false;
9283 if (token->type == CPP_PRAGMA)
9285 /* A top-level declaration can consist solely of a #pragma.
9286 A nested declaration cannot, so this is done here and not
9287 in cp_parser_declaration. (A #pragma at block scope is
9288 handled in cp_parser_statement.) */
9289 cp_parser_pragma (parser, pragma_external);
9290 continue;
9293 /* Parse the declaration itself. */
9294 cp_parser_declaration (parser);
9298 /* Parse a declaration.
9300 declaration:
9301 block-declaration
9302 function-definition
9303 template-declaration
9304 explicit-instantiation
9305 explicit-specialization
9306 linkage-specification
9307 namespace-definition
9309 GNU extension:
9311 declaration:
9312 __extension__ declaration */
9314 static void
9315 cp_parser_declaration (cp_parser* parser)
9317 cp_token token1;
9318 cp_token token2;
9319 int saved_pedantic;
9320 void *p;
9321 tree attributes = NULL_TREE;
9323 /* Check for the `__extension__' keyword. */
9324 if (cp_parser_extension_opt (parser, &saved_pedantic))
9326 /* Parse the qualified declaration. */
9327 cp_parser_declaration (parser);
9328 /* Restore the PEDANTIC flag. */
9329 pedantic = saved_pedantic;
9331 return;
9334 /* Try to figure out what kind of declaration is present. */
9335 token1 = *cp_lexer_peek_token (parser->lexer);
9337 if (token1.type != CPP_EOF)
9338 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9339 else
9341 token2.type = CPP_EOF;
9342 token2.keyword = RID_MAX;
9345 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9346 p = obstack_alloc (&declarator_obstack, 0);
9348 /* If the next token is `extern' and the following token is a string
9349 literal, then we have a linkage specification. */
9350 if (token1.keyword == RID_EXTERN
9351 && cp_parser_is_string_literal (&token2))
9352 cp_parser_linkage_specification (parser);
9353 /* If the next token is `template', then we have either a template
9354 declaration, an explicit instantiation, or an explicit
9355 specialization. */
9356 else if (token1.keyword == RID_TEMPLATE)
9358 /* `template <>' indicates a template specialization. */
9359 if (token2.type == CPP_LESS
9360 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9361 cp_parser_explicit_specialization (parser);
9362 /* `template <' indicates a template declaration. */
9363 else if (token2.type == CPP_LESS)
9364 cp_parser_template_declaration (parser, /*member_p=*/false);
9365 /* Anything else must be an explicit instantiation. */
9366 else
9367 cp_parser_explicit_instantiation (parser);
9369 /* If the next token is `export', then we have a template
9370 declaration. */
9371 else if (token1.keyword == RID_EXPORT)
9372 cp_parser_template_declaration (parser, /*member_p=*/false);
9373 /* If the next token is `extern', 'static' or 'inline' and the one
9374 after that is `template', we have a GNU extended explicit
9375 instantiation directive. */
9376 else if (cp_parser_allow_gnu_extensions_p (parser)
9377 && (token1.keyword == RID_EXTERN
9378 || token1.keyword == RID_STATIC
9379 || token1.keyword == RID_INLINE)
9380 && token2.keyword == RID_TEMPLATE)
9381 cp_parser_explicit_instantiation (parser);
9382 /* If the next token is `namespace', check for a named or unnamed
9383 namespace definition. */
9384 else if (token1.keyword == RID_NAMESPACE
9385 && (/* A named namespace definition. */
9386 (token2.type == CPP_NAME
9387 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9388 != CPP_EQ))
9389 /* An unnamed namespace definition. */
9390 || token2.type == CPP_OPEN_BRACE
9391 || token2.keyword == RID_ATTRIBUTE))
9392 cp_parser_namespace_definition (parser);
9393 /* An inline (associated) namespace definition. */
9394 else if (token1.keyword == RID_INLINE
9395 && token2.keyword == RID_NAMESPACE)
9396 cp_parser_namespace_definition (parser);
9397 /* Objective-C++ declaration/definition. */
9398 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9399 cp_parser_objc_declaration (parser, NULL_TREE);
9400 else if (c_dialect_objc ()
9401 && token1.keyword == RID_ATTRIBUTE
9402 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9403 cp_parser_objc_declaration (parser, attributes);
9404 /* We must have either a block declaration or a function
9405 definition. */
9406 else
9407 /* Try to parse a block-declaration, or a function-definition. */
9408 cp_parser_block_declaration (parser, /*statement_p=*/false);
9410 /* Free any declarators allocated. */
9411 obstack_free (&declarator_obstack, p);
9414 /* Parse a block-declaration.
9416 block-declaration:
9417 simple-declaration
9418 asm-definition
9419 namespace-alias-definition
9420 using-declaration
9421 using-directive
9423 GNU Extension:
9425 block-declaration:
9426 __extension__ block-declaration
9428 C++0x Extension:
9430 block-declaration:
9431 static_assert-declaration
9433 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9434 part of a declaration-statement. */
9436 static void
9437 cp_parser_block_declaration (cp_parser *parser,
9438 bool statement_p)
9440 cp_token *token1;
9441 int saved_pedantic;
9443 /* Check for the `__extension__' keyword. */
9444 if (cp_parser_extension_opt (parser, &saved_pedantic))
9446 /* Parse the qualified declaration. */
9447 cp_parser_block_declaration (parser, statement_p);
9448 /* Restore the PEDANTIC flag. */
9449 pedantic = saved_pedantic;
9451 return;
9454 /* Peek at the next token to figure out which kind of declaration is
9455 present. */
9456 token1 = cp_lexer_peek_token (parser->lexer);
9458 /* If the next keyword is `asm', we have an asm-definition. */
9459 if (token1->keyword == RID_ASM)
9461 if (statement_p)
9462 cp_parser_commit_to_tentative_parse (parser);
9463 cp_parser_asm_definition (parser);
9465 /* If the next keyword is `namespace', we have a
9466 namespace-alias-definition. */
9467 else if (token1->keyword == RID_NAMESPACE)
9468 cp_parser_namespace_alias_definition (parser);
9469 /* If the next keyword is `using', we have either a
9470 using-declaration or a using-directive. */
9471 else if (token1->keyword == RID_USING)
9473 cp_token *token2;
9475 if (statement_p)
9476 cp_parser_commit_to_tentative_parse (parser);
9477 /* If the token after `using' is `namespace', then we have a
9478 using-directive. */
9479 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9480 if (token2->keyword == RID_NAMESPACE)
9481 cp_parser_using_directive (parser);
9482 /* Otherwise, it's a using-declaration. */
9483 else
9484 cp_parser_using_declaration (parser,
9485 /*access_declaration_p=*/false);
9487 /* If the next keyword is `__label__' we have a misplaced label
9488 declaration. */
9489 else if (token1->keyword == RID_LABEL)
9491 cp_lexer_consume_token (parser->lexer);
9492 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9493 cp_parser_skip_to_end_of_statement (parser);
9494 /* If the next token is now a `;', consume it. */
9495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9496 cp_lexer_consume_token (parser->lexer);
9498 /* If the next token is `static_assert' we have a static assertion. */
9499 else if (token1->keyword == RID_STATIC_ASSERT)
9500 cp_parser_static_assert (parser, /*member_p=*/false);
9501 /* Anything else must be a simple-declaration. */
9502 else
9503 cp_parser_simple_declaration (parser, !statement_p);
9506 /* Parse a simple-declaration.
9508 simple-declaration:
9509 decl-specifier-seq [opt] init-declarator-list [opt] ;
9511 init-declarator-list:
9512 init-declarator
9513 init-declarator-list , init-declarator
9515 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9516 function-definition as a simple-declaration. */
9518 static void
9519 cp_parser_simple_declaration (cp_parser* parser,
9520 bool function_definition_allowed_p)
9522 cp_decl_specifier_seq decl_specifiers;
9523 int declares_class_or_enum;
9524 bool saw_declarator;
9526 /* Defer access checks until we know what is being declared; the
9527 checks for names appearing in the decl-specifier-seq should be
9528 done as if we were in the scope of the thing being declared. */
9529 push_deferring_access_checks (dk_deferred);
9531 /* Parse the decl-specifier-seq. We have to keep track of whether
9532 or not the decl-specifier-seq declares a named class or
9533 enumeration type, since that is the only case in which the
9534 init-declarator-list is allowed to be empty.
9536 [dcl.dcl]
9538 In a simple-declaration, the optional init-declarator-list can be
9539 omitted only when declaring a class or enumeration, that is when
9540 the decl-specifier-seq contains either a class-specifier, an
9541 elaborated-type-specifier, or an enum-specifier. */
9542 cp_parser_decl_specifier_seq (parser,
9543 CP_PARSER_FLAGS_OPTIONAL,
9544 &decl_specifiers,
9545 &declares_class_or_enum);
9546 /* We no longer need to defer access checks. */
9547 stop_deferring_access_checks ();
9549 /* In a block scope, a valid declaration must always have a
9550 decl-specifier-seq. By not trying to parse declarators, we can
9551 resolve the declaration/expression ambiguity more quickly. */
9552 if (!function_definition_allowed_p
9553 && !decl_specifiers.any_specifiers_p)
9555 cp_parser_error (parser, "expected declaration");
9556 goto done;
9559 /* If the next two tokens are both identifiers, the code is
9560 erroneous. The usual cause of this situation is code like:
9562 T t;
9564 where "T" should name a type -- but does not. */
9565 if (!decl_specifiers.any_type_specifiers_p
9566 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9568 /* If parsing tentatively, we should commit; we really are
9569 looking at a declaration. */
9570 cp_parser_commit_to_tentative_parse (parser);
9571 /* Give up. */
9572 goto done;
9575 /* If we have seen at least one decl-specifier, and the next token
9576 is not a parenthesis, then we must be looking at a declaration.
9577 (After "int (" we might be looking at a functional cast.) */
9578 if (decl_specifiers.any_specifiers_p
9579 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9580 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9581 && !cp_parser_error_occurred (parser))
9582 cp_parser_commit_to_tentative_parse (parser);
9584 /* Keep going until we hit the `;' at the end of the simple
9585 declaration. */
9586 saw_declarator = false;
9587 while (cp_lexer_next_token_is_not (parser->lexer,
9588 CPP_SEMICOLON))
9590 cp_token *token;
9591 bool function_definition_p;
9592 tree decl;
9594 if (saw_declarator)
9596 /* If we are processing next declarator, coma is expected */
9597 token = cp_lexer_peek_token (parser->lexer);
9598 gcc_assert (token->type == CPP_COMMA);
9599 cp_lexer_consume_token (parser->lexer);
9601 else
9602 saw_declarator = true;
9604 /* Parse the init-declarator. */
9605 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9606 /*checks=*/NULL,
9607 function_definition_allowed_p,
9608 /*member_p=*/false,
9609 declares_class_or_enum,
9610 &function_definition_p);
9611 /* If an error occurred while parsing tentatively, exit quickly.
9612 (That usually happens when in the body of a function; each
9613 statement is treated as a declaration-statement until proven
9614 otherwise.) */
9615 if (cp_parser_error_occurred (parser))
9616 goto done;
9617 /* Handle function definitions specially. */
9618 if (function_definition_p)
9620 /* If the next token is a `,', then we are probably
9621 processing something like:
9623 void f() {}, *p;
9625 which is erroneous. */
9626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9628 cp_token *token = cp_lexer_peek_token (parser->lexer);
9629 error_at (token->location,
9630 "mixing"
9631 " declarations and function-definitions is forbidden");
9633 /* Otherwise, we're done with the list of declarators. */
9634 else
9636 pop_deferring_access_checks ();
9637 return;
9640 /* The next token should be either a `,' or a `;'. */
9641 token = cp_lexer_peek_token (parser->lexer);
9642 /* If it's a `,', there are more declarators to come. */
9643 if (token->type == CPP_COMMA)
9644 /* will be consumed next time around */;
9645 /* If it's a `;', we are done. */
9646 else if (token->type == CPP_SEMICOLON)
9647 break;
9648 /* Anything else is an error. */
9649 else
9651 /* If we have already issued an error message we don't need
9652 to issue another one. */
9653 if (decl != error_mark_node
9654 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9655 cp_parser_error (parser, "expected %<,%> or %<;%>");
9656 /* Skip tokens until we reach the end of the statement. */
9657 cp_parser_skip_to_end_of_statement (parser);
9658 /* If the next token is now a `;', consume it. */
9659 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9660 cp_lexer_consume_token (parser->lexer);
9661 goto done;
9663 /* After the first time around, a function-definition is not
9664 allowed -- even if it was OK at first. For example:
9666 int i, f() {}
9668 is not valid. */
9669 function_definition_allowed_p = false;
9672 /* Issue an error message if no declarators are present, and the
9673 decl-specifier-seq does not itself declare a class or
9674 enumeration. */
9675 if (!saw_declarator)
9677 if (cp_parser_declares_only_class_p (parser))
9678 shadow_tag (&decl_specifiers);
9679 /* Perform any deferred access checks. */
9680 perform_deferred_access_checks ();
9683 /* Consume the `;'. */
9684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9686 done:
9687 pop_deferring_access_checks ();
9690 /* Parse a decl-specifier-seq.
9692 decl-specifier-seq:
9693 decl-specifier-seq [opt] decl-specifier
9695 decl-specifier:
9696 storage-class-specifier
9697 type-specifier
9698 function-specifier
9699 friend
9700 typedef
9702 GNU Extension:
9704 decl-specifier:
9705 attributes
9707 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9709 The parser flags FLAGS is used to control type-specifier parsing.
9711 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9712 flags:
9714 1: one of the decl-specifiers is an elaborated-type-specifier
9715 (i.e., a type declaration)
9716 2: one of the decl-specifiers is an enum-specifier or a
9717 class-specifier (i.e., a type definition)
9721 static void
9722 cp_parser_decl_specifier_seq (cp_parser* parser,
9723 cp_parser_flags flags,
9724 cp_decl_specifier_seq *decl_specs,
9725 int* declares_class_or_enum)
9727 bool constructor_possible_p = !parser->in_declarator_p;
9728 cp_token *start_token = NULL;
9730 /* Clear DECL_SPECS. */
9731 clear_decl_specs (decl_specs);
9733 /* Assume no class or enumeration type is declared. */
9734 *declares_class_or_enum = 0;
9736 /* Keep reading specifiers until there are no more to read. */
9737 while (true)
9739 bool constructor_p;
9740 bool found_decl_spec;
9741 cp_token *token;
9743 /* Peek at the next token. */
9744 token = cp_lexer_peek_token (parser->lexer);
9746 /* Save the first token of the decl spec list for error
9747 reporting. */
9748 if (!start_token)
9749 start_token = token;
9750 /* Handle attributes. */
9751 if (token->keyword == RID_ATTRIBUTE)
9753 /* Parse the attributes. */
9754 decl_specs->attributes
9755 = chainon (decl_specs->attributes,
9756 cp_parser_attributes_opt (parser));
9757 continue;
9759 /* Assume we will find a decl-specifier keyword. */
9760 found_decl_spec = true;
9761 /* If the next token is an appropriate keyword, we can simply
9762 add it to the list. */
9763 switch (token->keyword)
9765 /* decl-specifier:
9766 friend
9767 constexpr */
9768 case RID_FRIEND:
9769 if (!at_class_scope_p ())
9771 error_at (token->location, "%<friend%> used outside of class");
9772 cp_lexer_purge_token (parser->lexer);
9774 else
9776 ++decl_specs->specs[(int) ds_friend];
9777 /* Consume the token. */
9778 cp_lexer_consume_token (parser->lexer);
9780 break;
9782 case RID_CONSTEXPR:
9783 ++decl_specs->specs[(int) ds_constexpr];
9784 cp_lexer_consume_token (parser->lexer);
9785 break;
9787 /* function-specifier:
9788 inline
9789 virtual
9790 explicit */
9791 case RID_INLINE:
9792 case RID_VIRTUAL:
9793 case RID_EXPLICIT:
9794 cp_parser_function_specifier_opt (parser, decl_specs);
9795 break;
9797 /* decl-specifier:
9798 typedef */
9799 case RID_TYPEDEF:
9800 ++decl_specs->specs[(int) ds_typedef];
9801 /* Consume the token. */
9802 cp_lexer_consume_token (parser->lexer);
9803 /* A constructor declarator cannot appear in a typedef. */
9804 constructor_possible_p = false;
9805 /* The "typedef" keyword can only occur in a declaration; we
9806 may as well commit at this point. */
9807 cp_parser_commit_to_tentative_parse (parser);
9809 if (decl_specs->storage_class != sc_none)
9810 decl_specs->conflicting_specifiers_p = true;
9811 break;
9813 /* storage-class-specifier:
9814 auto
9815 register
9816 static
9817 extern
9818 mutable
9820 GNU Extension:
9821 thread */
9822 case RID_AUTO:
9823 if (cxx_dialect == cxx98)
9825 /* Consume the token. */
9826 cp_lexer_consume_token (parser->lexer);
9828 /* Complain about `auto' as a storage specifier, if
9829 we're complaining about C++0x compatibility. */
9830 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9831 " will change meaning in C++0x; please remove it");
9833 /* Set the storage class anyway. */
9834 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9835 token->location);
9837 else
9838 /* C++0x auto type-specifier. */
9839 found_decl_spec = false;
9840 break;
9842 case RID_REGISTER:
9843 case RID_STATIC:
9844 case RID_EXTERN:
9845 case RID_MUTABLE:
9846 /* Consume the token. */
9847 cp_lexer_consume_token (parser->lexer);
9848 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9849 token->location);
9850 break;
9851 case RID_THREAD:
9852 /* Consume the token. */
9853 cp_lexer_consume_token (parser->lexer);
9854 ++decl_specs->specs[(int) ds_thread];
9855 break;
9857 default:
9858 /* We did not yet find a decl-specifier yet. */
9859 found_decl_spec = false;
9860 break;
9863 if (found_decl_spec
9864 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9865 && token->keyword != RID_CONSTEXPR)
9866 error ("decl-specifier invalid in condition");
9868 /* Constructors are a special case. The `S' in `S()' is not a
9869 decl-specifier; it is the beginning of the declarator. */
9870 constructor_p
9871 = (!found_decl_spec
9872 && constructor_possible_p
9873 && (cp_parser_constructor_declarator_p
9874 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9876 /* If we don't have a DECL_SPEC yet, then we must be looking at
9877 a type-specifier. */
9878 if (!found_decl_spec && !constructor_p)
9880 int decl_spec_declares_class_or_enum;
9881 bool is_cv_qualifier;
9882 tree type_spec;
9884 type_spec
9885 = cp_parser_type_specifier (parser, flags,
9886 decl_specs,
9887 /*is_declaration=*/true,
9888 &decl_spec_declares_class_or_enum,
9889 &is_cv_qualifier);
9890 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9892 /* If this type-specifier referenced a user-defined type
9893 (a typedef, class-name, etc.), then we can't allow any
9894 more such type-specifiers henceforth.
9896 [dcl.spec]
9898 The longest sequence of decl-specifiers that could
9899 possibly be a type name is taken as the
9900 decl-specifier-seq of a declaration. The sequence shall
9901 be self-consistent as described below.
9903 [dcl.type]
9905 As a general rule, at most one type-specifier is allowed
9906 in the complete decl-specifier-seq of a declaration. The
9907 only exceptions are the following:
9909 -- const or volatile can be combined with any other
9910 type-specifier.
9912 -- signed or unsigned can be combined with char, long,
9913 short, or int.
9915 -- ..
9917 Example:
9919 typedef char* Pc;
9920 void g (const int Pc);
9922 Here, Pc is *not* part of the decl-specifier seq; it's
9923 the declarator. Therefore, once we see a type-specifier
9924 (other than a cv-qualifier), we forbid any additional
9925 user-defined types. We *do* still allow things like `int
9926 int' to be considered a decl-specifier-seq, and issue the
9927 error message later. */
9928 if (type_spec && !is_cv_qualifier)
9929 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9930 /* A constructor declarator cannot follow a type-specifier. */
9931 if (type_spec)
9933 constructor_possible_p = false;
9934 found_decl_spec = true;
9935 if (!is_cv_qualifier)
9936 decl_specs->any_type_specifiers_p = true;
9940 /* If we still do not have a DECL_SPEC, then there are no more
9941 decl-specifiers. */
9942 if (!found_decl_spec)
9943 break;
9945 decl_specs->any_specifiers_p = true;
9946 /* After we see one decl-specifier, further decl-specifiers are
9947 always optional. */
9948 flags |= CP_PARSER_FLAGS_OPTIONAL;
9951 cp_parser_check_decl_spec (decl_specs, start_token->location);
9953 /* Don't allow a friend specifier with a class definition. */
9954 if (decl_specs->specs[(int) ds_friend] != 0
9955 && (*declares_class_or_enum & 2))
9956 error_at (start_token->location,
9957 "class definition may not be declared a friend");
9960 /* Parse an (optional) storage-class-specifier.
9962 storage-class-specifier:
9963 auto
9964 register
9965 static
9966 extern
9967 mutable
9969 GNU Extension:
9971 storage-class-specifier:
9972 thread
9974 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9976 static tree
9977 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9979 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9981 case RID_AUTO:
9982 if (cxx_dialect != cxx98)
9983 return NULL_TREE;
9984 /* Fall through for C++98. */
9986 case RID_REGISTER:
9987 case RID_STATIC:
9988 case RID_EXTERN:
9989 case RID_MUTABLE:
9990 case RID_THREAD:
9991 /* Consume the token. */
9992 return cp_lexer_consume_token (parser->lexer)->u.value;
9994 default:
9995 return NULL_TREE;
9999 /* Parse an (optional) function-specifier.
10001 function-specifier:
10002 inline
10003 virtual
10004 explicit
10006 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10007 Updates DECL_SPECS, if it is non-NULL. */
10009 static tree
10010 cp_parser_function_specifier_opt (cp_parser* parser,
10011 cp_decl_specifier_seq *decl_specs)
10013 cp_token *token = cp_lexer_peek_token (parser->lexer);
10014 switch (token->keyword)
10016 case RID_INLINE:
10017 if (decl_specs)
10018 ++decl_specs->specs[(int) ds_inline];
10019 break;
10021 case RID_VIRTUAL:
10022 /* 14.5.2.3 [temp.mem]
10024 A member function template shall not be virtual. */
10025 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10026 error_at (token->location, "templates may not be %<virtual%>");
10027 else if (decl_specs)
10028 ++decl_specs->specs[(int) ds_virtual];
10029 break;
10031 case RID_EXPLICIT:
10032 if (decl_specs)
10033 ++decl_specs->specs[(int) ds_explicit];
10034 break;
10036 default:
10037 return NULL_TREE;
10040 /* Consume the token. */
10041 return cp_lexer_consume_token (parser->lexer)->u.value;
10044 /* Parse a linkage-specification.
10046 linkage-specification:
10047 extern string-literal { declaration-seq [opt] }
10048 extern string-literal declaration */
10050 static void
10051 cp_parser_linkage_specification (cp_parser* parser)
10053 tree linkage;
10055 /* Look for the `extern' keyword. */
10056 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10058 /* Look for the string-literal. */
10059 linkage = cp_parser_string_literal (parser, false, false);
10061 /* Transform the literal into an identifier. If the literal is a
10062 wide-character string, or contains embedded NULs, then we can't
10063 handle it as the user wants. */
10064 if (strlen (TREE_STRING_POINTER (linkage))
10065 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10067 cp_parser_error (parser, "invalid linkage-specification");
10068 /* Assume C++ linkage. */
10069 linkage = lang_name_cplusplus;
10071 else
10072 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10074 /* We're now using the new linkage. */
10075 push_lang_context (linkage);
10077 /* If the next token is a `{', then we're using the first
10078 production. */
10079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10081 /* Consume the `{' token. */
10082 cp_lexer_consume_token (parser->lexer);
10083 /* Parse the declarations. */
10084 cp_parser_declaration_seq_opt (parser);
10085 /* Look for the closing `}'. */
10086 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10088 /* Otherwise, there's just one declaration. */
10089 else
10091 bool saved_in_unbraced_linkage_specification_p;
10093 saved_in_unbraced_linkage_specification_p
10094 = parser->in_unbraced_linkage_specification_p;
10095 parser->in_unbraced_linkage_specification_p = true;
10096 cp_parser_declaration (parser);
10097 parser->in_unbraced_linkage_specification_p
10098 = saved_in_unbraced_linkage_specification_p;
10101 /* We're done with the linkage-specification. */
10102 pop_lang_context ();
10105 /* Parse a static_assert-declaration.
10107 static_assert-declaration:
10108 static_assert ( constant-expression , string-literal ) ;
10110 If MEMBER_P, this static_assert is a class member. */
10112 static void
10113 cp_parser_static_assert(cp_parser *parser, bool member_p)
10115 tree condition;
10116 tree message;
10117 cp_token *token;
10118 location_t saved_loc;
10120 /* Peek at the `static_assert' token so we can keep track of exactly
10121 where the static assertion started. */
10122 token = cp_lexer_peek_token (parser->lexer);
10123 saved_loc = token->location;
10125 /* Look for the `static_assert' keyword. */
10126 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10127 RT_STATIC_ASSERT))
10128 return;
10130 /* We know we are in a static assertion; commit to any tentative
10131 parse. */
10132 if (cp_parser_parsing_tentatively (parser))
10133 cp_parser_commit_to_tentative_parse (parser);
10135 /* Parse the `(' starting the static assertion condition. */
10136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10138 /* Parse the constant-expression. */
10139 condition =
10140 cp_parser_constant_expression (parser,
10141 /*allow_non_constant_p=*/false,
10142 /*non_constant_p=*/NULL);
10144 /* Parse the separating `,'. */
10145 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10147 /* Parse the string-literal message. */
10148 message = cp_parser_string_literal (parser,
10149 /*translate=*/false,
10150 /*wide_ok=*/true);
10152 /* A `)' completes the static assertion. */
10153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10154 cp_parser_skip_to_closing_parenthesis (parser,
10155 /*recovering=*/true,
10156 /*or_comma=*/false,
10157 /*consume_paren=*/true);
10159 /* A semicolon terminates the declaration. */
10160 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10162 /* Complete the static assertion, which may mean either processing
10163 the static assert now or saving it for template instantiation. */
10164 finish_static_assert (condition, message, saved_loc, member_p);
10167 /* Parse a `decltype' type. Returns the type.
10169 simple-type-specifier:
10170 decltype ( expression ) */
10172 static tree
10173 cp_parser_decltype (cp_parser *parser)
10175 tree expr;
10176 bool id_expression_or_member_access_p = false;
10177 const char *saved_message;
10178 bool saved_integral_constant_expression_p;
10179 bool saved_non_integral_constant_expression_p;
10180 cp_token *id_expr_start_token;
10182 /* Look for the `decltype' token. */
10183 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10184 return error_mark_node;
10186 /* Types cannot be defined in a `decltype' expression. Save away the
10187 old message. */
10188 saved_message = parser->type_definition_forbidden_message;
10190 /* And create the new one. */
10191 parser->type_definition_forbidden_message
10192 = G_("types may not be defined in %<decltype%> expressions");
10194 /* The restrictions on constant-expressions do not apply inside
10195 decltype expressions. */
10196 saved_integral_constant_expression_p
10197 = parser->integral_constant_expression_p;
10198 saved_non_integral_constant_expression_p
10199 = parser->non_integral_constant_expression_p;
10200 parser->integral_constant_expression_p = false;
10202 /* Do not actually evaluate the expression. */
10203 ++cp_unevaluated_operand;
10205 /* Do not warn about problems with the expression. */
10206 ++c_inhibit_evaluation_warnings;
10208 /* Parse the opening `('. */
10209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10210 return error_mark_node;
10212 /* First, try parsing an id-expression. */
10213 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10214 cp_parser_parse_tentatively (parser);
10215 expr = cp_parser_id_expression (parser,
10216 /*template_keyword_p=*/false,
10217 /*check_dependency_p=*/true,
10218 /*template_p=*/NULL,
10219 /*declarator_p=*/false,
10220 /*optional_p=*/false);
10222 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10224 bool non_integral_constant_expression_p = false;
10225 tree id_expression = expr;
10226 cp_id_kind idk;
10227 const char *error_msg;
10229 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10230 /* Lookup the name we got back from the id-expression. */
10231 expr = cp_parser_lookup_name (parser, expr,
10232 none_type,
10233 /*is_template=*/false,
10234 /*is_namespace=*/false,
10235 /*check_dependency=*/true,
10236 /*ambiguous_decls=*/NULL,
10237 id_expr_start_token->location);
10239 if (expr
10240 && expr != error_mark_node
10241 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10242 && TREE_CODE (expr) != TYPE_DECL
10243 && (TREE_CODE (expr) != BIT_NOT_EXPR
10244 || !TYPE_P (TREE_OPERAND (expr, 0)))
10245 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10247 /* Complete lookup of the id-expression. */
10248 expr = (finish_id_expression
10249 (id_expression, expr, parser->scope, &idk,
10250 /*integral_constant_expression_p=*/false,
10251 /*allow_non_integral_constant_expression_p=*/true,
10252 &non_integral_constant_expression_p,
10253 /*template_p=*/false,
10254 /*done=*/true,
10255 /*address_p=*/false,
10256 /*template_arg_p=*/false,
10257 &error_msg,
10258 id_expr_start_token->location));
10260 if (expr == error_mark_node)
10261 /* We found an id-expression, but it was something that we
10262 should not have found. This is an error, not something
10263 we can recover from, so note that we found an
10264 id-expression and we'll recover as gracefully as
10265 possible. */
10266 id_expression_or_member_access_p = true;
10269 if (expr
10270 && expr != error_mark_node
10271 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10272 /* We have an id-expression. */
10273 id_expression_or_member_access_p = true;
10276 if (!id_expression_or_member_access_p)
10278 /* Abort the id-expression parse. */
10279 cp_parser_abort_tentative_parse (parser);
10281 /* Parsing tentatively, again. */
10282 cp_parser_parse_tentatively (parser);
10284 /* Parse a class member access. */
10285 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10286 /*cast_p=*/false,
10287 /*member_access_only_p=*/true, NULL);
10289 if (expr
10290 && expr != error_mark_node
10291 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10292 /* We have an id-expression. */
10293 id_expression_or_member_access_p = true;
10296 if (id_expression_or_member_access_p)
10297 /* We have parsed the complete id-expression or member access. */
10298 cp_parser_parse_definitely (parser);
10299 else
10301 bool saved_greater_than_is_operator_p;
10303 /* Abort our attempt to parse an id-expression or member access
10304 expression. */
10305 cp_parser_abort_tentative_parse (parser);
10307 /* Within a parenthesized expression, a `>' token is always
10308 the greater-than operator. */
10309 saved_greater_than_is_operator_p
10310 = parser->greater_than_is_operator_p;
10311 parser->greater_than_is_operator_p = true;
10313 /* Parse a full expression. */
10314 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10316 /* The `>' token might be the end of a template-id or
10317 template-parameter-list now. */
10318 parser->greater_than_is_operator_p
10319 = saved_greater_than_is_operator_p;
10322 /* Go back to evaluating expressions. */
10323 --cp_unevaluated_operand;
10324 --c_inhibit_evaluation_warnings;
10326 /* Restore the old message and the integral constant expression
10327 flags. */
10328 parser->type_definition_forbidden_message = saved_message;
10329 parser->integral_constant_expression_p
10330 = saved_integral_constant_expression_p;
10331 parser->non_integral_constant_expression_p
10332 = saved_non_integral_constant_expression_p;
10334 if (expr == error_mark_node)
10336 /* Skip everything up to the closing `)'. */
10337 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10338 /*consume_paren=*/true);
10339 return error_mark_node;
10342 /* Parse to the closing `)'. */
10343 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10345 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10346 /*consume_paren=*/true);
10347 return error_mark_node;
10350 return finish_decltype_type (expr, id_expression_or_member_access_p);
10353 /* Special member functions [gram.special] */
10355 /* Parse a conversion-function-id.
10357 conversion-function-id:
10358 operator conversion-type-id
10360 Returns an IDENTIFIER_NODE representing the operator. */
10362 static tree
10363 cp_parser_conversion_function_id (cp_parser* parser)
10365 tree type;
10366 tree saved_scope;
10367 tree saved_qualifying_scope;
10368 tree saved_object_scope;
10369 tree pushed_scope = NULL_TREE;
10371 /* Look for the `operator' token. */
10372 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10373 return error_mark_node;
10374 /* When we parse the conversion-type-id, the current scope will be
10375 reset. However, we need that information in able to look up the
10376 conversion function later, so we save it here. */
10377 saved_scope = parser->scope;
10378 saved_qualifying_scope = parser->qualifying_scope;
10379 saved_object_scope = parser->object_scope;
10380 /* We must enter the scope of the class so that the names of
10381 entities declared within the class are available in the
10382 conversion-type-id. For example, consider:
10384 struct S {
10385 typedef int I;
10386 operator I();
10389 S::operator I() { ... }
10391 In order to see that `I' is a type-name in the definition, we
10392 must be in the scope of `S'. */
10393 if (saved_scope)
10394 pushed_scope = push_scope (saved_scope);
10395 /* Parse the conversion-type-id. */
10396 type = cp_parser_conversion_type_id (parser);
10397 /* Leave the scope of the class, if any. */
10398 if (pushed_scope)
10399 pop_scope (pushed_scope);
10400 /* Restore the saved scope. */
10401 parser->scope = saved_scope;
10402 parser->qualifying_scope = saved_qualifying_scope;
10403 parser->object_scope = saved_object_scope;
10404 /* If the TYPE is invalid, indicate failure. */
10405 if (type == error_mark_node)
10406 return error_mark_node;
10407 return mangle_conv_op_name_for_type (type);
10410 /* Parse a conversion-type-id:
10412 conversion-type-id:
10413 type-specifier-seq conversion-declarator [opt]
10415 Returns the TYPE specified. */
10417 static tree
10418 cp_parser_conversion_type_id (cp_parser* parser)
10420 tree attributes;
10421 cp_decl_specifier_seq type_specifiers;
10422 cp_declarator *declarator;
10423 tree type_specified;
10425 /* Parse the attributes. */
10426 attributes = cp_parser_attributes_opt (parser);
10427 /* Parse the type-specifiers. */
10428 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10429 /*is_trailing_return=*/false,
10430 &type_specifiers);
10431 /* If that didn't work, stop. */
10432 if (type_specifiers.type == error_mark_node)
10433 return error_mark_node;
10434 /* Parse the conversion-declarator. */
10435 declarator = cp_parser_conversion_declarator_opt (parser);
10437 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10438 /*initialized=*/0, &attributes);
10439 if (attributes)
10440 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10442 /* Don't give this error when parsing tentatively. This happens to
10443 work because we always parse this definitively once. */
10444 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10445 && type_uses_auto (type_specified))
10447 error ("invalid use of %<auto%> in conversion operator");
10448 return error_mark_node;
10451 return type_specified;
10454 /* Parse an (optional) conversion-declarator.
10456 conversion-declarator:
10457 ptr-operator conversion-declarator [opt]
10461 static cp_declarator *
10462 cp_parser_conversion_declarator_opt (cp_parser* parser)
10464 enum tree_code code;
10465 tree class_type;
10466 cp_cv_quals cv_quals;
10468 /* We don't know if there's a ptr-operator next, or not. */
10469 cp_parser_parse_tentatively (parser);
10470 /* Try the ptr-operator. */
10471 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10472 /* If it worked, look for more conversion-declarators. */
10473 if (cp_parser_parse_definitely (parser))
10475 cp_declarator *declarator;
10477 /* Parse another optional declarator. */
10478 declarator = cp_parser_conversion_declarator_opt (parser);
10480 return cp_parser_make_indirect_declarator
10481 (code, class_type, cv_quals, declarator);
10484 return NULL;
10487 /* Parse an (optional) ctor-initializer.
10489 ctor-initializer:
10490 : mem-initializer-list
10492 Returns TRUE iff the ctor-initializer was actually present. */
10494 static bool
10495 cp_parser_ctor_initializer_opt (cp_parser* parser)
10497 /* If the next token is not a `:', then there is no
10498 ctor-initializer. */
10499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10501 /* Do default initialization of any bases and members. */
10502 if (DECL_CONSTRUCTOR_P (current_function_decl))
10503 finish_mem_initializers (NULL_TREE);
10505 return false;
10508 /* Consume the `:' token. */
10509 cp_lexer_consume_token (parser->lexer);
10510 /* And the mem-initializer-list. */
10511 cp_parser_mem_initializer_list (parser);
10513 return true;
10516 /* Parse a mem-initializer-list.
10518 mem-initializer-list:
10519 mem-initializer ... [opt]
10520 mem-initializer ... [opt] , mem-initializer-list */
10522 static void
10523 cp_parser_mem_initializer_list (cp_parser* parser)
10525 tree mem_initializer_list = NULL_TREE;
10526 cp_token *token = cp_lexer_peek_token (parser->lexer);
10528 /* Let the semantic analysis code know that we are starting the
10529 mem-initializer-list. */
10530 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10531 error_at (token->location,
10532 "only constructors take member initializers");
10534 /* Loop through the list. */
10535 while (true)
10537 tree mem_initializer;
10539 token = cp_lexer_peek_token (parser->lexer);
10540 /* Parse the mem-initializer. */
10541 mem_initializer = cp_parser_mem_initializer (parser);
10542 /* If the next token is a `...', we're expanding member initializers. */
10543 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10545 /* Consume the `...'. */
10546 cp_lexer_consume_token (parser->lexer);
10548 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10549 can be expanded but members cannot. */
10550 if (mem_initializer != error_mark_node
10551 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10553 error_at (token->location,
10554 "cannot expand initializer for member %<%D%>",
10555 TREE_PURPOSE (mem_initializer));
10556 mem_initializer = error_mark_node;
10559 /* Construct the pack expansion type. */
10560 if (mem_initializer != error_mark_node)
10561 mem_initializer = make_pack_expansion (mem_initializer);
10563 /* Add it to the list, unless it was erroneous. */
10564 if (mem_initializer != error_mark_node)
10566 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10567 mem_initializer_list = mem_initializer;
10569 /* If the next token is not a `,', we're done. */
10570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10571 break;
10572 /* Consume the `,' token. */
10573 cp_lexer_consume_token (parser->lexer);
10576 /* Perform semantic analysis. */
10577 if (DECL_CONSTRUCTOR_P (current_function_decl))
10578 finish_mem_initializers (mem_initializer_list);
10581 /* Parse a mem-initializer.
10583 mem-initializer:
10584 mem-initializer-id ( expression-list [opt] )
10585 mem-initializer-id braced-init-list
10587 GNU extension:
10589 mem-initializer:
10590 ( expression-list [opt] )
10592 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10593 class) or FIELD_DECL (for a non-static data member) to initialize;
10594 the TREE_VALUE is the expression-list. An empty initialization
10595 list is represented by void_list_node. */
10597 static tree
10598 cp_parser_mem_initializer (cp_parser* parser)
10600 tree mem_initializer_id;
10601 tree expression_list;
10602 tree member;
10603 cp_token *token = cp_lexer_peek_token (parser->lexer);
10605 /* Find out what is being initialized. */
10606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10608 permerror (token->location,
10609 "anachronistic old-style base class initializer");
10610 mem_initializer_id = NULL_TREE;
10612 else
10614 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10615 if (mem_initializer_id == error_mark_node)
10616 return mem_initializer_id;
10618 member = expand_member_init (mem_initializer_id);
10619 if (member && !DECL_P (member))
10620 in_base_initializer = 1;
10622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10624 bool expr_non_constant_p;
10625 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10626 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10627 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10628 expression_list = build_tree_list (NULL_TREE, expression_list);
10630 else
10632 VEC(tree,gc)* vec;
10633 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10634 /*cast_p=*/false,
10635 /*allow_expansion_p=*/true,
10636 /*non_constant_p=*/NULL);
10637 if (vec == NULL)
10638 return error_mark_node;
10639 expression_list = build_tree_list_vec (vec);
10640 release_tree_vector (vec);
10643 if (expression_list == error_mark_node)
10644 return error_mark_node;
10645 if (!expression_list)
10646 expression_list = void_type_node;
10648 in_base_initializer = 0;
10650 return member ? build_tree_list (member, expression_list) : error_mark_node;
10653 /* Parse a mem-initializer-id.
10655 mem-initializer-id:
10656 :: [opt] nested-name-specifier [opt] class-name
10657 identifier
10659 Returns a TYPE indicating the class to be initializer for the first
10660 production. Returns an IDENTIFIER_NODE indicating the data member
10661 to be initialized for the second production. */
10663 static tree
10664 cp_parser_mem_initializer_id (cp_parser* parser)
10666 bool global_scope_p;
10667 bool nested_name_specifier_p;
10668 bool template_p = false;
10669 tree id;
10671 cp_token *token = cp_lexer_peek_token (parser->lexer);
10673 /* `typename' is not allowed in this context ([temp.res]). */
10674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10676 error_at (token->location,
10677 "keyword %<typename%> not allowed in this context (a qualified "
10678 "member initializer is implicitly a type)");
10679 cp_lexer_consume_token (parser->lexer);
10681 /* Look for the optional `::' operator. */
10682 global_scope_p
10683 = (cp_parser_global_scope_opt (parser,
10684 /*current_scope_valid_p=*/false)
10685 != NULL_TREE);
10686 /* Look for the optional nested-name-specifier. The simplest way to
10687 implement:
10689 [temp.res]
10691 The keyword `typename' is not permitted in a base-specifier or
10692 mem-initializer; in these contexts a qualified name that
10693 depends on a template-parameter is implicitly assumed to be a
10694 type name.
10696 is to assume that we have seen the `typename' keyword at this
10697 point. */
10698 nested_name_specifier_p
10699 = (cp_parser_nested_name_specifier_opt (parser,
10700 /*typename_keyword_p=*/true,
10701 /*check_dependency_p=*/true,
10702 /*type_p=*/true,
10703 /*is_declaration=*/true)
10704 != NULL_TREE);
10705 if (nested_name_specifier_p)
10706 template_p = cp_parser_optional_template_keyword (parser);
10707 /* If there is a `::' operator or a nested-name-specifier, then we
10708 are definitely looking for a class-name. */
10709 if (global_scope_p || nested_name_specifier_p)
10710 return cp_parser_class_name (parser,
10711 /*typename_keyword_p=*/true,
10712 /*template_keyword_p=*/template_p,
10713 typename_type,
10714 /*check_dependency_p=*/true,
10715 /*class_head_p=*/false,
10716 /*is_declaration=*/true);
10717 /* Otherwise, we could also be looking for an ordinary identifier. */
10718 cp_parser_parse_tentatively (parser);
10719 /* Try a class-name. */
10720 id = cp_parser_class_name (parser,
10721 /*typename_keyword_p=*/true,
10722 /*template_keyword_p=*/false,
10723 none_type,
10724 /*check_dependency_p=*/true,
10725 /*class_head_p=*/false,
10726 /*is_declaration=*/true);
10727 /* If we found one, we're done. */
10728 if (cp_parser_parse_definitely (parser))
10729 return id;
10730 /* Otherwise, look for an ordinary identifier. */
10731 return cp_parser_identifier (parser);
10734 /* Overloading [gram.over] */
10736 /* Parse an operator-function-id.
10738 operator-function-id:
10739 operator operator
10741 Returns an IDENTIFIER_NODE for the operator which is a
10742 human-readable spelling of the identifier, e.g., `operator +'. */
10744 static tree
10745 cp_parser_operator_function_id (cp_parser* parser)
10747 /* Look for the `operator' keyword. */
10748 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10749 return error_mark_node;
10750 /* And then the name of the operator itself. */
10751 return cp_parser_operator (parser);
10754 /* Parse an operator.
10756 operator:
10757 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10758 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10759 || ++ -- , ->* -> () []
10761 GNU Extensions:
10763 operator:
10764 <? >? <?= >?=
10766 Returns an IDENTIFIER_NODE for the operator which is a
10767 human-readable spelling of the identifier, e.g., `operator +'. */
10769 static tree
10770 cp_parser_operator (cp_parser* parser)
10772 tree id = NULL_TREE;
10773 cp_token *token;
10775 /* Peek at the next token. */
10776 token = cp_lexer_peek_token (parser->lexer);
10777 /* Figure out which operator we have. */
10778 switch (token->type)
10780 case CPP_KEYWORD:
10782 enum tree_code op;
10784 /* The keyword should be either `new' or `delete'. */
10785 if (token->keyword == RID_NEW)
10786 op = NEW_EXPR;
10787 else if (token->keyword == RID_DELETE)
10788 op = DELETE_EXPR;
10789 else
10790 break;
10792 /* Consume the `new' or `delete' token. */
10793 cp_lexer_consume_token (parser->lexer);
10795 /* Peek at the next token. */
10796 token = cp_lexer_peek_token (parser->lexer);
10797 /* If it's a `[' token then this is the array variant of the
10798 operator. */
10799 if (token->type == CPP_OPEN_SQUARE)
10801 /* Consume the `[' token. */
10802 cp_lexer_consume_token (parser->lexer);
10803 /* Look for the `]' token. */
10804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10805 id = ansi_opname (op == NEW_EXPR
10806 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10808 /* Otherwise, we have the non-array variant. */
10809 else
10810 id = ansi_opname (op);
10812 return id;
10815 case CPP_PLUS:
10816 id = ansi_opname (PLUS_EXPR);
10817 break;
10819 case CPP_MINUS:
10820 id = ansi_opname (MINUS_EXPR);
10821 break;
10823 case CPP_MULT:
10824 id = ansi_opname (MULT_EXPR);
10825 break;
10827 case CPP_DIV:
10828 id = ansi_opname (TRUNC_DIV_EXPR);
10829 break;
10831 case CPP_MOD:
10832 id = ansi_opname (TRUNC_MOD_EXPR);
10833 break;
10835 case CPP_XOR:
10836 id = ansi_opname (BIT_XOR_EXPR);
10837 break;
10839 case CPP_AND:
10840 id = ansi_opname (BIT_AND_EXPR);
10841 break;
10843 case CPP_OR:
10844 id = ansi_opname (BIT_IOR_EXPR);
10845 break;
10847 case CPP_COMPL:
10848 id = ansi_opname (BIT_NOT_EXPR);
10849 break;
10851 case CPP_NOT:
10852 id = ansi_opname (TRUTH_NOT_EXPR);
10853 break;
10855 case CPP_EQ:
10856 id = ansi_assopname (NOP_EXPR);
10857 break;
10859 case CPP_LESS:
10860 id = ansi_opname (LT_EXPR);
10861 break;
10863 case CPP_GREATER:
10864 id = ansi_opname (GT_EXPR);
10865 break;
10867 case CPP_PLUS_EQ:
10868 id = ansi_assopname (PLUS_EXPR);
10869 break;
10871 case CPP_MINUS_EQ:
10872 id = ansi_assopname (MINUS_EXPR);
10873 break;
10875 case CPP_MULT_EQ:
10876 id = ansi_assopname (MULT_EXPR);
10877 break;
10879 case CPP_DIV_EQ:
10880 id = ansi_assopname (TRUNC_DIV_EXPR);
10881 break;
10883 case CPP_MOD_EQ:
10884 id = ansi_assopname (TRUNC_MOD_EXPR);
10885 break;
10887 case CPP_XOR_EQ:
10888 id = ansi_assopname (BIT_XOR_EXPR);
10889 break;
10891 case CPP_AND_EQ:
10892 id = ansi_assopname (BIT_AND_EXPR);
10893 break;
10895 case CPP_OR_EQ:
10896 id = ansi_assopname (BIT_IOR_EXPR);
10897 break;
10899 case CPP_LSHIFT:
10900 id = ansi_opname (LSHIFT_EXPR);
10901 break;
10903 case CPP_RSHIFT:
10904 id = ansi_opname (RSHIFT_EXPR);
10905 break;
10907 case CPP_LSHIFT_EQ:
10908 id = ansi_assopname (LSHIFT_EXPR);
10909 break;
10911 case CPP_RSHIFT_EQ:
10912 id = ansi_assopname (RSHIFT_EXPR);
10913 break;
10915 case CPP_EQ_EQ:
10916 id = ansi_opname (EQ_EXPR);
10917 break;
10919 case CPP_NOT_EQ:
10920 id = ansi_opname (NE_EXPR);
10921 break;
10923 case CPP_LESS_EQ:
10924 id = ansi_opname (LE_EXPR);
10925 break;
10927 case CPP_GREATER_EQ:
10928 id = ansi_opname (GE_EXPR);
10929 break;
10931 case CPP_AND_AND:
10932 id = ansi_opname (TRUTH_ANDIF_EXPR);
10933 break;
10935 case CPP_OR_OR:
10936 id = ansi_opname (TRUTH_ORIF_EXPR);
10937 break;
10939 case CPP_PLUS_PLUS:
10940 id = ansi_opname (POSTINCREMENT_EXPR);
10941 break;
10943 case CPP_MINUS_MINUS:
10944 id = ansi_opname (PREDECREMENT_EXPR);
10945 break;
10947 case CPP_COMMA:
10948 id = ansi_opname (COMPOUND_EXPR);
10949 break;
10951 case CPP_DEREF_STAR:
10952 id = ansi_opname (MEMBER_REF);
10953 break;
10955 case CPP_DEREF:
10956 id = ansi_opname (COMPONENT_REF);
10957 break;
10959 case CPP_OPEN_PAREN:
10960 /* Consume the `('. */
10961 cp_lexer_consume_token (parser->lexer);
10962 /* Look for the matching `)'. */
10963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10964 return ansi_opname (CALL_EXPR);
10966 case CPP_OPEN_SQUARE:
10967 /* Consume the `['. */
10968 cp_lexer_consume_token (parser->lexer);
10969 /* Look for the matching `]'. */
10970 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10971 return ansi_opname (ARRAY_REF);
10973 default:
10974 /* Anything else is an error. */
10975 break;
10978 /* If we have selected an identifier, we need to consume the
10979 operator token. */
10980 if (id)
10981 cp_lexer_consume_token (parser->lexer);
10982 /* Otherwise, no valid operator name was present. */
10983 else
10985 cp_parser_error (parser, "expected operator");
10986 id = error_mark_node;
10989 return id;
10992 /* Parse a template-declaration.
10994 template-declaration:
10995 export [opt] template < template-parameter-list > declaration
10997 If MEMBER_P is TRUE, this template-declaration occurs within a
10998 class-specifier.
11000 The grammar rule given by the standard isn't correct. What
11001 is really meant is:
11003 template-declaration:
11004 export [opt] template-parameter-list-seq
11005 decl-specifier-seq [opt] init-declarator [opt] ;
11006 export [opt] template-parameter-list-seq
11007 function-definition
11009 template-parameter-list-seq:
11010 template-parameter-list-seq [opt]
11011 template < template-parameter-list > */
11013 static void
11014 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11016 /* Check for `export'. */
11017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11019 /* Consume the `export' token. */
11020 cp_lexer_consume_token (parser->lexer);
11021 /* Warn that we do not support `export'. */
11022 warning (0, "keyword %<export%> not implemented, and will be ignored");
11025 cp_parser_template_declaration_after_export (parser, member_p);
11028 /* Parse a template-parameter-list.
11030 template-parameter-list:
11031 template-parameter
11032 template-parameter-list , template-parameter
11034 Returns a TREE_LIST. Each node represents a template parameter.
11035 The nodes are connected via their TREE_CHAINs. */
11037 static tree
11038 cp_parser_template_parameter_list (cp_parser* parser)
11040 tree parameter_list = NULL_TREE;
11042 begin_template_parm_list ();
11043 while (true)
11045 tree parameter;
11046 bool is_non_type;
11047 bool is_parameter_pack;
11048 location_t parm_loc;
11050 /* Parse the template-parameter. */
11051 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11052 parameter = cp_parser_template_parameter (parser,
11053 &is_non_type,
11054 &is_parameter_pack);
11055 /* Add it to the list. */
11056 if (parameter != error_mark_node)
11057 parameter_list = process_template_parm (parameter_list,
11058 parm_loc,
11059 parameter,
11060 is_non_type,
11061 is_parameter_pack);
11062 else
11064 tree err_parm = build_tree_list (parameter, parameter);
11065 TREE_VALUE (err_parm) = error_mark_node;
11066 parameter_list = chainon (parameter_list, err_parm);
11069 /* If the next token is not a `,', we're done. */
11070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11071 break;
11072 /* Otherwise, consume the `,' token. */
11073 cp_lexer_consume_token (parser->lexer);
11076 return end_template_parm_list (parameter_list);
11079 /* Parse a template-parameter.
11081 template-parameter:
11082 type-parameter
11083 parameter-declaration
11085 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11086 the parameter. The TREE_PURPOSE is the default value, if any.
11087 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11088 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11089 set to true iff this parameter is a parameter pack. */
11091 static tree
11092 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11093 bool *is_parameter_pack)
11095 cp_token *token;
11096 cp_parameter_declarator *parameter_declarator;
11097 cp_declarator *id_declarator;
11098 tree parm;
11100 /* Assume it is a type parameter or a template parameter. */
11101 *is_non_type = false;
11102 /* Assume it not a parameter pack. */
11103 *is_parameter_pack = false;
11104 /* Peek at the next token. */
11105 token = cp_lexer_peek_token (parser->lexer);
11106 /* If it is `class' or `template', we have a type-parameter. */
11107 if (token->keyword == RID_TEMPLATE)
11108 return cp_parser_type_parameter (parser, is_parameter_pack);
11109 /* If it is `class' or `typename' we do not know yet whether it is a
11110 type parameter or a non-type parameter. Consider:
11112 template <typename T, typename T::X X> ...
11116 template <class C, class D*> ...
11118 Here, the first parameter is a type parameter, and the second is
11119 a non-type parameter. We can tell by looking at the token after
11120 the identifier -- if it is a `,', `=', or `>' then we have a type
11121 parameter. */
11122 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11124 /* Peek at the token after `class' or `typename'. */
11125 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11126 /* If it's an ellipsis, we have a template type parameter
11127 pack. */
11128 if (token->type == CPP_ELLIPSIS)
11129 return cp_parser_type_parameter (parser, is_parameter_pack);
11130 /* If it's an identifier, skip it. */
11131 if (token->type == CPP_NAME)
11132 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11133 /* Now, see if the token looks like the end of a template
11134 parameter. */
11135 if (token->type == CPP_COMMA
11136 || token->type == CPP_EQ
11137 || token->type == CPP_GREATER)
11138 return cp_parser_type_parameter (parser, is_parameter_pack);
11141 /* Otherwise, it is a non-type parameter.
11143 [temp.param]
11145 When parsing a default template-argument for a non-type
11146 template-parameter, the first non-nested `>' is taken as the end
11147 of the template parameter-list rather than a greater-than
11148 operator. */
11149 *is_non_type = true;
11150 parameter_declarator
11151 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11152 /*parenthesized_p=*/NULL);
11154 /* If the parameter declaration is marked as a parameter pack, set
11155 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11156 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11157 grokdeclarator. */
11158 if (parameter_declarator
11159 && parameter_declarator->declarator
11160 && parameter_declarator->declarator->parameter_pack_p)
11162 *is_parameter_pack = true;
11163 parameter_declarator->declarator->parameter_pack_p = false;
11166 /* If the next token is an ellipsis, and we don't already have it
11167 marked as a parameter pack, then we have a parameter pack (that
11168 has no declarator). */
11169 if (!*is_parameter_pack
11170 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11171 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11173 /* Consume the `...'. */
11174 cp_lexer_consume_token (parser->lexer);
11175 maybe_warn_variadic_templates ();
11177 *is_parameter_pack = true;
11179 /* We might end up with a pack expansion as the type of the non-type
11180 template parameter, in which case this is a non-type template
11181 parameter pack. */
11182 else if (parameter_declarator
11183 && parameter_declarator->decl_specifiers.type
11184 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11186 *is_parameter_pack = true;
11187 parameter_declarator->decl_specifiers.type =
11188 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11191 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11193 /* Parameter packs cannot have default arguments. However, a
11194 user may try to do so, so we'll parse them and give an
11195 appropriate diagnostic here. */
11197 /* Consume the `='. */
11198 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11199 cp_lexer_consume_token (parser->lexer);
11201 /* Find the name of the parameter pack. */
11202 id_declarator = parameter_declarator->declarator;
11203 while (id_declarator && id_declarator->kind != cdk_id)
11204 id_declarator = id_declarator->declarator;
11206 if (id_declarator && id_declarator->kind == cdk_id)
11207 error_at (start_token->location,
11208 "template parameter pack %qD cannot have a default argument",
11209 id_declarator->u.id.unqualified_name);
11210 else
11211 error_at (start_token->location,
11212 "template parameter pack cannot have a default argument");
11214 /* Parse the default argument, but throw away the result. */
11215 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11218 parm = grokdeclarator (parameter_declarator->declarator,
11219 &parameter_declarator->decl_specifiers,
11220 TPARM, /*initialized=*/0,
11221 /*attrlist=*/NULL);
11222 if (parm == error_mark_node)
11223 return error_mark_node;
11225 return build_tree_list (parameter_declarator->default_argument, parm);
11228 /* Parse a type-parameter.
11230 type-parameter:
11231 class identifier [opt]
11232 class identifier [opt] = type-id
11233 typename identifier [opt]
11234 typename identifier [opt] = type-id
11235 template < template-parameter-list > class identifier [opt]
11236 template < template-parameter-list > class identifier [opt]
11237 = id-expression
11239 GNU Extension (variadic templates):
11241 type-parameter:
11242 class ... identifier [opt]
11243 typename ... identifier [opt]
11245 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11246 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11247 the declaration of the parameter.
11249 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11251 static tree
11252 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11254 cp_token *token;
11255 tree parameter;
11257 /* Look for a keyword to tell us what kind of parameter this is. */
11258 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11259 if (!token)
11260 return error_mark_node;
11262 switch (token->keyword)
11264 case RID_CLASS:
11265 case RID_TYPENAME:
11267 tree identifier;
11268 tree default_argument;
11270 /* If the next token is an ellipsis, we have a template
11271 argument pack. */
11272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11274 /* Consume the `...' token. */
11275 cp_lexer_consume_token (parser->lexer);
11276 maybe_warn_variadic_templates ();
11278 *is_parameter_pack = true;
11281 /* If the next token is an identifier, then it names the
11282 parameter. */
11283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11284 identifier = cp_parser_identifier (parser);
11285 else
11286 identifier = NULL_TREE;
11288 /* Create the parameter. */
11289 parameter = finish_template_type_parm (class_type_node, identifier);
11291 /* If the next token is an `=', we have a default argument. */
11292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11294 /* Consume the `=' token. */
11295 cp_lexer_consume_token (parser->lexer);
11296 /* Parse the default-argument. */
11297 push_deferring_access_checks (dk_no_deferred);
11298 default_argument = cp_parser_type_id (parser);
11300 /* Template parameter packs cannot have default
11301 arguments. */
11302 if (*is_parameter_pack)
11304 if (identifier)
11305 error_at (token->location,
11306 "template parameter pack %qD cannot have a "
11307 "default argument", identifier);
11308 else
11309 error_at (token->location,
11310 "template parameter packs cannot have "
11311 "default arguments");
11312 default_argument = NULL_TREE;
11314 pop_deferring_access_checks ();
11316 else
11317 default_argument = NULL_TREE;
11319 /* Create the combined representation of the parameter and the
11320 default argument. */
11321 parameter = build_tree_list (default_argument, parameter);
11323 break;
11325 case RID_TEMPLATE:
11327 tree identifier;
11328 tree default_argument;
11330 /* Look for the `<'. */
11331 cp_parser_require (parser, CPP_LESS, RT_LESS);
11332 /* Parse the template-parameter-list. */
11333 cp_parser_template_parameter_list (parser);
11334 /* Look for the `>'. */
11335 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11336 /* Look for the `class' keyword. */
11337 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11338 /* If the next token is an ellipsis, we have a template
11339 argument pack. */
11340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11342 /* Consume the `...' token. */
11343 cp_lexer_consume_token (parser->lexer);
11344 maybe_warn_variadic_templates ();
11346 *is_parameter_pack = true;
11348 /* If the next token is an `=', then there is a
11349 default-argument. If the next token is a `>', we are at
11350 the end of the parameter-list. If the next token is a `,',
11351 then we are at the end of this parameter. */
11352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11353 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11354 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11356 identifier = cp_parser_identifier (parser);
11357 /* Treat invalid names as if the parameter were nameless. */
11358 if (identifier == error_mark_node)
11359 identifier = NULL_TREE;
11361 else
11362 identifier = NULL_TREE;
11364 /* Create the template parameter. */
11365 parameter = finish_template_template_parm (class_type_node,
11366 identifier);
11368 /* If the next token is an `=', then there is a
11369 default-argument. */
11370 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11372 bool is_template;
11374 /* Consume the `='. */
11375 cp_lexer_consume_token (parser->lexer);
11376 /* Parse the id-expression. */
11377 push_deferring_access_checks (dk_no_deferred);
11378 /* save token before parsing the id-expression, for error
11379 reporting */
11380 token = cp_lexer_peek_token (parser->lexer);
11381 default_argument
11382 = cp_parser_id_expression (parser,
11383 /*template_keyword_p=*/false,
11384 /*check_dependency_p=*/true,
11385 /*template_p=*/&is_template,
11386 /*declarator_p=*/false,
11387 /*optional_p=*/false);
11388 if (TREE_CODE (default_argument) == TYPE_DECL)
11389 /* If the id-expression was a template-id that refers to
11390 a template-class, we already have the declaration here,
11391 so no further lookup is needed. */
11393 else
11394 /* Look up the name. */
11395 default_argument
11396 = cp_parser_lookup_name (parser, default_argument,
11397 none_type,
11398 /*is_template=*/is_template,
11399 /*is_namespace=*/false,
11400 /*check_dependency=*/true,
11401 /*ambiguous_decls=*/NULL,
11402 token->location);
11403 /* See if the default argument is valid. */
11404 default_argument
11405 = check_template_template_default_arg (default_argument);
11407 /* Template parameter packs cannot have default
11408 arguments. */
11409 if (*is_parameter_pack)
11411 if (identifier)
11412 error_at (token->location,
11413 "template parameter pack %qD cannot "
11414 "have a default argument",
11415 identifier);
11416 else
11417 error_at (token->location, "template parameter packs cannot "
11418 "have default arguments");
11419 default_argument = NULL_TREE;
11421 pop_deferring_access_checks ();
11423 else
11424 default_argument = NULL_TREE;
11426 /* Create the combined representation of the parameter and the
11427 default argument. */
11428 parameter = build_tree_list (default_argument, parameter);
11430 break;
11432 default:
11433 gcc_unreachable ();
11434 break;
11437 return parameter;
11440 /* Parse a template-id.
11442 template-id:
11443 template-name < template-argument-list [opt] >
11445 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11446 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11447 returned. Otherwise, if the template-name names a function, or set
11448 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11449 names a class, returns a TYPE_DECL for the specialization.
11451 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11452 uninstantiated templates. */
11454 static tree
11455 cp_parser_template_id (cp_parser *parser,
11456 bool template_keyword_p,
11457 bool check_dependency_p,
11458 bool is_declaration)
11460 int i;
11461 tree templ;
11462 tree arguments;
11463 tree template_id;
11464 cp_token_position start_of_id = 0;
11465 deferred_access_check *chk;
11466 VEC (deferred_access_check,gc) *access_check;
11467 cp_token *next_token = NULL, *next_token_2 = NULL;
11468 bool is_identifier;
11470 /* If the next token corresponds to a template-id, there is no need
11471 to reparse it. */
11472 next_token = cp_lexer_peek_token (parser->lexer);
11473 if (next_token->type == CPP_TEMPLATE_ID)
11475 struct tree_check *check_value;
11477 /* Get the stored value. */
11478 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11479 /* Perform any access checks that were deferred. */
11480 access_check = check_value->checks;
11481 if (access_check)
11483 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11484 perform_or_defer_access_check (chk->binfo,
11485 chk->decl,
11486 chk->diag_decl);
11488 /* Return the stored value. */
11489 return check_value->value;
11492 /* Avoid performing name lookup if there is no possibility of
11493 finding a template-id. */
11494 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11495 || (next_token->type == CPP_NAME
11496 && !cp_parser_nth_token_starts_template_argument_list_p
11497 (parser, 2)))
11499 cp_parser_error (parser, "expected template-id");
11500 return error_mark_node;
11503 /* Remember where the template-id starts. */
11504 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11505 start_of_id = cp_lexer_token_position (parser->lexer, false);
11507 push_deferring_access_checks (dk_deferred);
11509 /* Parse the template-name. */
11510 is_identifier = false;
11511 templ = cp_parser_template_name (parser, template_keyword_p,
11512 check_dependency_p,
11513 is_declaration,
11514 &is_identifier);
11515 if (templ == error_mark_node || is_identifier)
11517 pop_deferring_access_checks ();
11518 return templ;
11521 /* If we find the sequence `[:' after a template-name, it's probably
11522 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11523 parse correctly the argument list. */
11524 next_token = cp_lexer_peek_token (parser->lexer);
11525 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11526 if (next_token->type == CPP_OPEN_SQUARE
11527 && next_token->flags & DIGRAPH
11528 && next_token_2->type == CPP_COLON
11529 && !(next_token_2->flags & PREV_WHITE))
11531 cp_parser_parse_tentatively (parser);
11532 /* Change `:' into `::'. */
11533 next_token_2->type = CPP_SCOPE;
11534 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11535 CPP_LESS. */
11536 cp_lexer_consume_token (parser->lexer);
11538 /* Parse the arguments. */
11539 arguments = cp_parser_enclosed_template_argument_list (parser);
11540 if (!cp_parser_parse_definitely (parser))
11542 /* If we couldn't parse an argument list, then we revert our changes
11543 and return simply an error. Maybe this is not a template-id
11544 after all. */
11545 next_token_2->type = CPP_COLON;
11546 cp_parser_error (parser, "expected %<<%>");
11547 pop_deferring_access_checks ();
11548 return error_mark_node;
11550 /* Otherwise, emit an error about the invalid digraph, but continue
11551 parsing because we got our argument list. */
11552 if (permerror (next_token->location,
11553 "%<<::%> cannot begin a template-argument list"))
11555 static bool hint = false;
11556 inform (next_token->location,
11557 "%<<:%> is an alternate spelling for %<[%>."
11558 " Insert whitespace between %<<%> and %<::%>");
11559 if (!hint && !flag_permissive)
11561 inform (next_token->location, "(if you use %<-fpermissive%>"
11562 " G++ will accept your code)");
11563 hint = true;
11567 else
11569 /* Look for the `<' that starts the template-argument-list. */
11570 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11572 pop_deferring_access_checks ();
11573 return error_mark_node;
11575 /* Parse the arguments. */
11576 arguments = cp_parser_enclosed_template_argument_list (parser);
11579 /* Build a representation of the specialization. */
11580 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11581 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11582 else if (DECL_CLASS_TEMPLATE_P (templ)
11583 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11585 bool entering_scope;
11586 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11587 template (rather than some instantiation thereof) only if
11588 is not nested within some other construct. For example, in
11589 "template <typename T> void f(T) { A<T>::", A<T> is just an
11590 instantiation of A. */
11591 entering_scope = (template_parm_scope_p ()
11592 && cp_lexer_next_token_is (parser->lexer,
11593 CPP_SCOPE));
11594 template_id
11595 = finish_template_type (templ, arguments, entering_scope);
11597 else
11599 /* If it's not a class-template or a template-template, it should be
11600 a function-template. */
11601 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11602 || TREE_CODE (templ) == OVERLOAD
11603 || BASELINK_P (templ)));
11605 template_id = lookup_template_function (templ, arguments);
11608 /* If parsing tentatively, replace the sequence of tokens that makes
11609 up the template-id with a CPP_TEMPLATE_ID token. That way,
11610 should we re-parse the token stream, we will not have to repeat
11611 the effort required to do the parse, nor will we issue duplicate
11612 error messages about problems during instantiation of the
11613 template. */
11614 if (start_of_id)
11616 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11618 /* Reset the contents of the START_OF_ID token. */
11619 token->type = CPP_TEMPLATE_ID;
11620 /* Retrieve any deferred checks. Do not pop this access checks yet
11621 so the memory will not be reclaimed during token replacing below. */
11622 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11623 token->u.tree_check_value->value = template_id;
11624 token->u.tree_check_value->checks = get_deferred_access_checks ();
11625 token->keyword = RID_MAX;
11627 /* Purge all subsequent tokens. */
11628 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11630 /* ??? Can we actually assume that, if template_id ==
11631 error_mark_node, we will have issued a diagnostic to the
11632 user, as opposed to simply marking the tentative parse as
11633 failed? */
11634 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11635 error_at (token->location, "parse error in template argument list");
11638 pop_deferring_access_checks ();
11639 return template_id;
11642 /* Parse a template-name.
11644 template-name:
11645 identifier
11647 The standard should actually say:
11649 template-name:
11650 identifier
11651 operator-function-id
11653 A defect report has been filed about this issue.
11655 A conversion-function-id cannot be a template name because they cannot
11656 be part of a template-id. In fact, looking at this code:
11658 a.operator K<int>()
11660 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11661 It is impossible to call a templated conversion-function-id with an
11662 explicit argument list, since the only allowed template parameter is
11663 the type to which it is converting.
11665 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11666 `template' keyword, in a construction like:
11668 T::template f<3>()
11670 In that case `f' is taken to be a template-name, even though there
11671 is no way of knowing for sure.
11673 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11674 name refers to a set of overloaded functions, at least one of which
11675 is a template, or an IDENTIFIER_NODE with the name of the template,
11676 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11677 names are looked up inside uninstantiated templates. */
11679 static tree
11680 cp_parser_template_name (cp_parser* parser,
11681 bool template_keyword_p,
11682 bool check_dependency_p,
11683 bool is_declaration,
11684 bool *is_identifier)
11686 tree identifier;
11687 tree decl;
11688 tree fns;
11689 cp_token *token = cp_lexer_peek_token (parser->lexer);
11691 /* If the next token is `operator', then we have either an
11692 operator-function-id or a conversion-function-id. */
11693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11695 /* We don't know whether we're looking at an
11696 operator-function-id or a conversion-function-id. */
11697 cp_parser_parse_tentatively (parser);
11698 /* Try an operator-function-id. */
11699 identifier = cp_parser_operator_function_id (parser);
11700 /* If that didn't work, try a conversion-function-id. */
11701 if (!cp_parser_parse_definitely (parser))
11703 cp_parser_error (parser, "expected template-name");
11704 return error_mark_node;
11707 /* Look for the identifier. */
11708 else
11709 identifier = cp_parser_identifier (parser);
11711 /* If we didn't find an identifier, we don't have a template-id. */
11712 if (identifier == error_mark_node)
11713 return error_mark_node;
11715 /* If the name immediately followed the `template' keyword, then it
11716 is a template-name. However, if the next token is not `<', then
11717 we do not treat it as a template-name, since it is not being used
11718 as part of a template-id. This enables us to handle constructs
11719 like:
11721 template <typename T> struct S { S(); };
11722 template <typename T> S<T>::S();
11724 correctly. We would treat `S' as a template -- if it were `S<T>'
11725 -- but we do not if there is no `<'. */
11727 if (processing_template_decl
11728 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11730 /* In a declaration, in a dependent context, we pretend that the
11731 "template" keyword was present in order to improve error
11732 recovery. For example, given:
11734 template <typename T> void f(T::X<int>);
11736 we want to treat "X<int>" as a template-id. */
11737 if (is_declaration
11738 && !template_keyword_p
11739 && parser->scope && TYPE_P (parser->scope)
11740 && check_dependency_p
11741 && dependent_scope_p (parser->scope)
11742 /* Do not do this for dtors (or ctors), since they never
11743 need the template keyword before their name. */
11744 && !constructor_name_p (identifier, parser->scope))
11746 cp_token_position start = 0;
11748 /* Explain what went wrong. */
11749 error_at (token->location, "non-template %qD used as template",
11750 identifier);
11751 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11752 parser->scope, identifier);
11753 /* If parsing tentatively, find the location of the "<" token. */
11754 if (cp_parser_simulate_error (parser))
11755 start = cp_lexer_token_position (parser->lexer, true);
11756 /* Parse the template arguments so that we can issue error
11757 messages about them. */
11758 cp_lexer_consume_token (parser->lexer);
11759 cp_parser_enclosed_template_argument_list (parser);
11760 /* Skip tokens until we find a good place from which to
11761 continue parsing. */
11762 cp_parser_skip_to_closing_parenthesis (parser,
11763 /*recovering=*/true,
11764 /*or_comma=*/true,
11765 /*consume_paren=*/false);
11766 /* If parsing tentatively, permanently remove the
11767 template argument list. That will prevent duplicate
11768 error messages from being issued about the missing
11769 "template" keyword. */
11770 if (start)
11771 cp_lexer_purge_tokens_after (parser->lexer, start);
11772 if (is_identifier)
11773 *is_identifier = true;
11774 return identifier;
11777 /* If the "template" keyword is present, then there is generally
11778 no point in doing name-lookup, so we just return IDENTIFIER.
11779 But, if the qualifying scope is non-dependent then we can
11780 (and must) do name-lookup normally. */
11781 if (template_keyword_p
11782 && (!parser->scope
11783 || (TYPE_P (parser->scope)
11784 && dependent_type_p (parser->scope))))
11785 return identifier;
11788 /* Look up the name. */
11789 decl = cp_parser_lookup_name (parser, identifier,
11790 none_type,
11791 /*is_template=*/true,
11792 /*is_namespace=*/false,
11793 check_dependency_p,
11794 /*ambiguous_decls=*/NULL,
11795 token->location);
11797 /* If DECL is a template, then the name was a template-name. */
11798 if (TREE_CODE (decl) == TEMPLATE_DECL)
11800 else
11802 tree fn = NULL_TREE;
11804 /* The standard does not explicitly indicate whether a name that
11805 names a set of overloaded declarations, some of which are
11806 templates, is a template-name. However, such a name should
11807 be a template-name; otherwise, there is no way to form a
11808 template-id for the overloaded templates. */
11809 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11810 if (TREE_CODE (fns) == OVERLOAD)
11811 for (fn = fns; fn; fn = OVL_NEXT (fn))
11812 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11813 break;
11815 if (!fn)
11817 /* The name does not name a template. */
11818 cp_parser_error (parser, "expected template-name");
11819 return error_mark_node;
11823 /* If DECL is dependent, and refers to a function, then just return
11824 its name; we will look it up again during template instantiation. */
11825 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11827 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11828 if (TYPE_P (scope) && dependent_type_p (scope))
11829 return identifier;
11832 return decl;
11835 /* Parse a template-argument-list.
11837 template-argument-list:
11838 template-argument ... [opt]
11839 template-argument-list , template-argument ... [opt]
11841 Returns a TREE_VEC containing the arguments. */
11843 static tree
11844 cp_parser_template_argument_list (cp_parser* parser)
11846 tree fixed_args[10];
11847 unsigned n_args = 0;
11848 unsigned alloced = 10;
11849 tree *arg_ary = fixed_args;
11850 tree vec;
11851 bool saved_in_template_argument_list_p;
11852 bool saved_ice_p;
11853 bool saved_non_ice_p;
11855 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11856 parser->in_template_argument_list_p = true;
11857 /* Even if the template-id appears in an integral
11858 constant-expression, the contents of the argument list do
11859 not. */
11860 saved_ice_p = parser->integral_constant_expression_p;
11861 parser->integral_constant_expression_p = false;
11862 saved_non_ice_p = parser->non_integral_constant_expression_p;
11863 parser->non_integral_constant_expression_p = false;
11864 /* Parse the arguments. */
11867 tree argument;
11869 if (n_args)
11870 /* Consume the comma. */
11871 cp_lexer_consume_token (parser->lexer);
11873 /* Parse the template-argument. */
11874 argument = cp_parser_template_argument (parser);
11876 /* If the next token is an ellipsis, we're expanding a template
11877 argument pack. */
11878 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11880 if (argument == error_mark_node)
11882 cp_token *token = cp_lexer_peek_token (parser->lexer);
11883 error_at (token->location,
11884 "expected parameter pack before %<...%>");
11886 /* Consume the `...' token. */
11887 cp_lexer_consume_token (parser->lexer);
11889 /* Make the argument into a TYPE_PACK_EXPANSION or
11890 EXPR_PACK_EXPANSION. */
11891 argument = make_pack_expansion (argument);
11894 if (n_args == alloced)
11896 alloced *= 2;
11898 if (arg_ary == fixed_args)
11900 arg_ary = XNEWVEC (tree, alloced);
11901 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11903 else
11904 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11906 arg_ary[n_args++] = argument;
11908 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11910 vec = make_tree_vec (n_args);
11912 while (n_args--)
11913 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11915 if (arg_ary != fixed_args)
11916 free (arg_ary);
11917 parser->non_integral_constant_expression_p = saved_non_ice_p;
11918 parser->integral_constant_expression_p = saved_ice_p;
11919 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11920 #ifdef ENABLE_CHECKING
11921 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11922 #endif
11923 return vec;
11926 /* Parse a template-argument.
11928 template-argument:
11929 assignment-expression
11930 type-id
11931 id-expression
11933 The representation is that of an assignment-expression, type-id, or
11934 id-expression -- except that the qualified id-expression is
11935 evaluated, so that the value returned is either a DECL or an
11936 OVERLOAD.
11938 Although the standard says "assignment-expression", it forbids
11939 throw-expressions or assignments in the template argument.
11940 Therefore, we use "conditional-expression" instead. */
11942 static tree
11943 cp_parser_template_argument (cp_parser* parser)
11945 tree argument;
11946 bool template_p;
11947 bool address_p;
11948 bool maybe_type_id = false;
11949 cp_token *token = NULL, *argument_start_token = NULL;
11950 cp_id_kind idk;
11952 /* There's really no way to know what we're looking at, so we just
11953 try each alternative in order.
11955 [temp.arg]
11957 In a template-argument, an ambiguity between a type-id and an
11958 expression is resolved to a type-id, regardless of the form of
11959 the corresponding template-parameter.
11961 Therefore, we try a type-id first. */
11962 cp_parser_parse_tentatively (parser);
11963 argument = cp_parser_template_type_arg (parser);
11964 /* If there was no error parsing the type-id but the next token is a
11965 '>>', our behavior depends on which dialect of C++ we're
11966 parsing. In C++98, we probably found a typo for '> >'. But there
11967 are type-id which are also valid expressions. For instance:
11969 struct X { int operator >> (int); };
11970 template <int V> struct Foo {};
11971 Foo<X () >> 5> r;
11973 Here 'X()' is a valid type-id of a function type, but the user just
11974 wanted to write the expression "X() >> 5". Thus, we remember that we
11975 found a valid type-id, but we still try to parse the argument as an
11976 expression to see what happens.
11978 In C++0x, the '>>' will be considered two separate '>'
11979 tokens. */
11980 if (!cp_parser_error_occurred (parser)
11981 && cxx_dialect == cxx98
11982 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11984 maybe_type_id = true;
11985 cp_parser_abort_tentative_parse (parser);
11987 else
11989 /* If the next token isn't a `,' or a `>', then this argument wasn't
11990 really finished. This means that the argument is not a valid
11991 type-id. */
11992 if (!cp_parser_next_token_ends_template_argument_p (parser))
11993 cp_parser_error (parser, "expected template-argument");
11994 /* If that worked, we're done. */
11995 if (cp_parser_parse_definitely (parser))
11996 return argument;
11998 /* We're still not sure what the argument will be. */
11999 cp_parser_parse_tentatively (parser);
12000 /* Try a template. */
12001 argument_start_token = cp_lexer_peek_token (parser->lexer);
12002 argument = cp_parser_id_expression (parser,
12003 /*template_keyword_p=*/false,
12004 /*check_dependency_p=*/true,
12005 &template_p,
12006 /*declarator_p=*/false,
12007 /*optional_p=*/false);
12008 /* If the next token isn't a `,' or a `>', then this argument wasn't
12009 really finished. */
12010 if (!cp_parser_next_token_ends_template_argument_p (parser))
12011 cp_parser_error (parser, "expected template-argument");
12012 if (!cp_parser_error_occurred (parser))
12014 /* Figure out what is being referred to. If the id-expression
12015 was for a class template specialization, then we will have a
12016 TYPE_DECL at this point. There is no need to do name lookup
12017 at this point in that case. */
12018 if (TREE_CODE (argument) != TYPE_DECL)
12019 argument = cp_parser_lookup_name (parser, argument,
12020 none_type,
12021 /*is_template=*/template_p,
12022 /*is_namespace=*/false,
12023 /*check_dependency=*/true,
12024 /*ambiguous_decls=*/NULL,
12025 argument_start_token->location);
12026 if (TREE_CODE (argument) != TEMPLATE_DECL
12027 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12028 cp_parser_error (parser, "expected template-name");
12030 if (cp_parser_parse_definitely (parser))
12031 return argument;
12032 /* It must be a non-type argument. There permitted cases are given
12033 in [temp.arg.nontype]:
12035 -- an integral constant-expression of integral or enumeration
12036 type; or
12038 -- the name of a non-type template-parameter; or
12040 -- the name of an object or function with external linkage...
12042 -- the address of an object or function with external linkage...
12044 -- a pointer to member... */
12045 /* Look for a non-type template parameter. */
12046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12048 cp_parser_parse_tentatively (parser);
12049 argument = cp_parser_primary_expression (parser,
12050 /*address_p=*/false,
12051 /*cast_p=*/false,
12052 /*template_arg_p=*/true,
12053 &idk);
12054 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12055 || !cp_parser_next_token_ends_template_argument_p (parser))
12056 cp_parser_simulate_error (parser);
12057 if (cp_parser_parse_definitely (parser))
12058 return argument;
12061 /* If the next token is "&", the argument must be the address of an
12062 object or function with external linkage. */
12063 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12064 if (address_p)
12065 cp_lexer_consume_token (parser->lexer);
12066 /* See if we might have an id-expression. */
12067 token = cp_lexer_peek_token (parser->lexer);
12068 if (token->type == CPP_NAME
12069 || token->keyword == RID_OPERATOR
12070 || token->type == CPP_SCOPE
12071 || token->type == CPP_TEMPLATE_ID
12072 || token->type == CPP_NESTED_NAME_SPECIFIER)
12074 cp_parser_parse_tentatively (parser);
12075 argument = cp_parser_primary_expression (parser,
12076 address_p,
12077 /*cast_p=*/false,
12078 /*template_arg_p=*/true,
12079 &idk);
12080 if (cp_parser_error_occurred (parser)
12081 || !cp_parser_next_token_ends_template_argument_p (parser))
12082 cp_parser_abort_tentative_parse (parser);
12083 else
12085 tree probe;
12087 if (TREE_CODE (argument) == INDIRECT_REF)
12089 gcc_assert (REFERENCE_REF_P (argument));
12090 argument = TREE_OPERAND (argument, 0);
12093 /* If we're in a template, we represent a qualified-id referring
12094 to a static data member as a SCOPE_REF even if the scope isn't
12095 dependent so that we can check access control later. */
12096 probe = argument;
12097 if (TREE_CODE (probe) == SCOPE_REF)
12098 probe = TREE_OPERAND (probe, 1);
12099 if (TREE_CODE (probe) == VAR_DECL)
12101 /* A variable without external linkage might still be a
12102 valid constant-expression, so no error is issued here
12103 if the external-linkage check fails. */
12104 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12105 cp_parser_simulate_error (parser);
12107 else if (is_overloaded_fn (argument))
12108 /* All overloaded functions are allowed; if the external
12109 linkage test does not pass, an error will be issued
12110 later. */
12112 else if (address_p
12113 && (TREE_CODE (argument) == OFFSET_REF
12114 || TREE_CODE (argument) == SCOPE_REF))
12115 /* A pointer-to-member. */
12117 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12119 else
12120 cp_parser_simulate_error (parser);
12122 if (cp_parser_parse_definitely (parser))
12124 if (address_p)
12125 argument = build_x_unary_op (ADDR_EXPR, argument,
12126 tf_warning_or_error);
12127 return argument;
12131 /* If the argument started with "&", there are no other valid
12132 alternatives at this point. */
12133 if (address_p)
12135 cp_parser_error (parser, "invalid non-type template argument");
12136 return error_mark_node;
12139 /* If the argument wasn't successfully parsed as a type-id followed
12140 by '>>', the argument can only be a constant expression now.
12141 Otherwise, we try parsing the constant-expression tentatively,
12142 because the argument could really be a type-id. */
12143 if (maybe_type_id)
12144 cp_parser_parse_tentatively (parser);
12145 argument = cp_parser_constant_expression (parser,
12146 /*allow_non_constant_p=*/false,
12147 /*non_constant_p=*/NULL);
12148 argument = fold_non_dependent_expr (argument);
12149 if (!maybe_type_id)
12150 return argument;
12151 if (!cp_parser_next_token_ends_template_argument_p (parser))
12152 cp_parser_error (parser, "expected template-argument");
12153 if (cp_parser_parse_definitely (parser))
12154 return argument;
12155 /* We did our best to parse the argument as a non type-id, but that
12156 was the only alternative that matched (albeit with a '>' after
12157 it). We can assume it's just a typo from the user, and a
12158 diagnostic will then be issued. */
12159 return cp_parser_template_type_arg (parser);
12162 /* Parse an explicit-instantiation.
12164 explicit-instantiation:
12165 template declaration
12167 Although the standard says `declaration', what it really means is:
12169 explicit-instantiation:
12170 template decl-specifier-seq [opt] declarator [opt] ;
12172 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12173 supposed to be allowed. A defect report has been filed about this
12174 issue.
12176 GNU Extension:
12178 explicit-instantiation:
12179 storage-class-specifier template
12180 decl-specifier-seq [opt] declarator [opt] ;
12181 function-specifier template
12182 decl-specifier-seq [opt] declarator [opt] ; */
12184 static void
12185 cp_parser_explicit_instantiation (cp_parser* parser)
12187 int declares_class_or_enum;
12188 cp_decl_specifier_seq decl_specifiers;
12189 tree extension_specifier = NULL_TREE;
12191 /* Look for an (optional) storage-class-specifier or
12192 function-specifier. */
12193 if (cp_parser_allow_gnu_extensions_p (parser))
12195 extension_specifier
12196 = cp_parser_storage_class_specifier_opt (parser);
12197 if (!extension_specifier)
12198 extension_specifier
12199 = cp_parser_function_specifier_opt (parser,
12200 /*decl_specs=*/NULL);
12203 /* Look for the `template' keyword. */
12204 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12205 /* Let the front end know that we are processing an explicit
12206 instantiation. */
12207 begin_explicit_instantiation ();
12208 /* [temp.explicit] says that we are supposed to ignore access
12209 control while processing explicit instantiation directives. */
12210 push_deferring_access_checks (dk_no_check);
12211 /* Parse a decl-specifier-seq. */
12212 cp_parser_decl_specifier_seq (parser,
12213 CP_PARSER_FLAGS_OPTIONAL,
12214 &decl_specifiers,
12215 &declares_class_or_enum);
12216 /* If there was exactly one decl-specifier, and it declared a class,
12217 and there's no declarator, then we have an explicit type
12218 instantiation. */
12219 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12221 tree type;
12223 type = check_tag_decl (&decl_specifiers);
12224 /* Turn access control back on for names used during
12225 template instantiation. */
12226 pop_deferring_access_checks ();
12227 if (type)
12228 do_type_instantiation (type, extension_specifier,
12229 /*complain=*/tf_error);
12231 else
12233 cp_declarator *declarator;
12234 tree decl;
12236 /* Parse the declarator. */
12237 declarator
12238 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12239 /*ctor_dtor_or_conv_p=*/NULL,
12240 /*parenthesized_p=*/NULL,
12241 /*member_p=*/false);
12242 if (declares_class_or_enum & 2)
12243 cp_parser_check_for_definition_in_return_type (declarator,
12244 decl_specifiers.type,
12245 decl_specifiers.type_location);
12246 if (declarator != cp_error_declarator)
12248 if (decl_specifiers.specs[(int)ds_inline])
12249 permerror (input_location, "explicit instantiation shall not use"
12250 " %<inline%> specifier");
12251 if (decl_specifiers.specs[(int)ds_constexpr])
12252 permerror (input_location, "explicit instantiation shall not use"
12253 " %<constexpr%> specifier");
12255 decl = grokdeclarator (declarator, &decl_specifiers,
12256 NORMAL, 0, &decl_specifiers.attributes);
12257 /* Turn access control back on for names used during
12258 template instantiation. */
12259 pop_deferring_access_checks ();
12260 /* Do the explicit instantiation. */
12261 do_decl_instantiation (decl, extension_specifier);
12263 else
12265 pop_deferring_access_checks ();
12266 /* Skip the body of the explicit instantiation. */
12267 cp_parser_skip_to_end_of_statement (parser);
12270 /* We're done with the instantiation. */
12271 end_explicit_instantiation ();
12273 cp_parser_consume_semicolon_at_end_of_statement (parser);
12276 /* Parse an explicit-specialization.
12278 explicit-specialization:
12279 template < > declaration
12281 Although the standard says `declaration', what it really means is:
12283 explicit-specialization:
12284 template <> decl-specifier [opt] init-declarator [opt] ;
12285 template <> function-definition
12286 template <> explicit-specialization
12287 template <> template-declaration */
12289 static void
12290 cp_parser_explicit_specialization (cp_parser* parser)
12292 bool need_lang_pop;
12293 cp_token *token = cp_lexer_peek_token (parser->lexer);
12295 /* Look for the `template' keyword. */
12296 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12297 /* Look for the `<'. */
12298 cp_parser_require (parser, CPP_LESS, RT_LESS);
12299 /* Look for the `>'. */
12300 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12301 /* We have processed another parameter list. */
12302 ++parser->num_template_parameter_lists;
12303 /* [temp]
12305 A template ... explicit specialization ... shall not have C
12306 linkage. */
12307 if (current_lang_name == lang_name_c)
12309 error_at (token->location, "template specialization with C linkage");
12310 /* Give it C++ linkage to avoid confusing other parts of the
12311 front end. */
12312 push_lang_context (lang_name_cplusplus);
12313 need_lang_pop = true;
12315 else
12316 need_lang_pop = false;
12317 /* Let the front end know that we are beginning a specialization. */
12318 if (!begin_specialization ())
12320 end_specialization ();
12321 return;
12324 /* If the next keyword is `template', we need to figure out whether
12325 or not we're looking a template-declaration. */
12326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12328 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12329 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12330 cp_parser_template_declaration_after_export (parser,
12331 /*member_p=*/false);
12332 else
12333 cp_parser_explicit_specialization (parser);
12335 else
12336 /* Parse the dependent declaration. */
12337 cp_parser_single_declaration (parser,
12338 /*checks=*/NULL,
12339 /*member_p=*/false,
12340 /*explicit_specialization_p=*/true,
12341 /*friend_p=*/NULL);
12342 /* We're done with the specialization. */
12343 end_specialization ();
12344 /* For the erroneous case of a template with C linkage, we pushed an
12345 implicit C++ linkage scope; exit that scope now. */
12346 if (need_lang_pop)
12347 pop_lang_context ();
12348 /* We're done with this parameter list. */
12349 --parser->num_template_parameter_lists;
12352 /* Parse a type-specifier.
12354 type-specifier:
12355 simple-type-specifier
12356 class-specifier
12357 enum-specifier
12358 elaborated-type-specifier
12359 cv-qualifier
12361 GNU Extension:
12363 type-specifier:
12364 __complex__
12366 Returns a representation of the type-specifier. For a
12367 class-specifier, enum-specifier, or elaborated-type-specifier, a
12368 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12370 The parser flags FLAGS is used to control type-specifier parsing.
12372 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12373 in a decl-specifier-seq.
12375 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12376 class-specifier, enum-specifier, or elaborated-type-specifier, then
12377 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12378 if a type is declared; 2 if it is defined. Otherwise, it is set to
12379 zero.
12381 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12382 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12383 is set to FALSE. */
12385 static tree
12386 cp_parser_type_specifier (cp_parser* parser,
12387 cp_parser_flags flags,
12388 cp_decl_specifier_seq *decl_specs,
12389 bool is_declaration,
12390 int* declares_class_or_enum,
12391 bool* is_cv_qualifier)
12393 tree type_spec = NULL_TREE;
12394 cp_token *token;
12395 enum rid keyword;
12396 cp_decl_spec ds = ds_last;
12398 /* Assume this type-specifier does not declare a new type. */
12399 if (declares_class_or_enum)
12400 *declares_class_or_enum = 0;
12401 /* And that it does not specify a cv-qualifier. */
12402 if (is_cv_qualifier)
12403 *is_cv_qualifier = false;
12404 /* Peek at the next token. */
12405 token = cp_lexer_peek_token (parser->lexer);
12407 /* If we're looking at a keyword, we can use that to guide the
12408 production we choose. */
12409 keyword = token->keyword;
12410 switch (keyword)
12412 case RID_ENUM:
12413 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12414 goto elaborated_type_specifier;
12416 /* Look for the enum-specifier. */
12417 type_spec = cp_parser_enum_specifier (parser);
12418 /* If that worked, we're done. */
12419 if (type_spec)
12421 if (declares_class_or_enum)
12422 *declares_class_or_enum = 2;
12423 if (decl_specs)
12424 cp_parser_set_decl_spec_type (decl_specs,
12425 type_spec,
12426 token->location,
12427 /*user_defined_p=*/true);
12428 return type_spec;
12430 else
12431 goto elaborated_type_specifier;
12433 /* Any of these indicate either a class-specifier, or an
12434 elaborated-type-specifier. */
12435 case RID_CLASS:
12436 case RID_STRUCT:
12437 case RID_UNION:
12438 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12439 goto elaborated_type_specifier;
12441 /* Parse tentatively so that we can back up if we don't find a
12442 class-specifier. */
12443 cp_parser_parse_tentatively (parser);
12444 /* Look for the class-specifier. */
12445 type_spec = cp_parser_class_specifier (parser);
12446 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12447 /* If that worked, we're done. */
12448 if (cp_parser_parse_definitely (parser))
12450 if (declares_class_or_enum)
12451 *declares_class_or_enum = 2;
12452 if (decl_specs)
12453 cp_parser_set_decl_spec_type (decl_specs,
12454 type_spec,
12455 token->location,
12456 /*user_defined_p=*/true);
12457 return type_spec;
12460 /* Fall through. */
12461 elaborated_type_specifier:
12462 /* We're declaring (not defining) a class or enum. */
12463 if (declares_class_or_enum)
12464 *declares_class_or_enum = 1;
12466 /* Fall through. */
12467 case RID_TYPENAME:
12468 /* Look for an elaborated-type-specifier. */
12469 type_spec
12470 = (cp_parser_elaborated_type_specifier
12471 (parser,
12472 decl_specs && decl_specs->specs[(int) ds_friend],
12473 is_declaration));
12474 if (decl_specs)
12475 cp_parser_set_decl_spec_type (decl_specs,
12476 type_spec,
12477 token->location,
12478 /*user_defined_p=*/true);
12479 return type_spec;
12481 case RID_CONST:
12482 ds = ds_const;
12483 if (is_cv_qualifier)
12484 *is_cv_qualifier = true;
12485 break;
12487 case RID_VOLATILE:
12488 ds = ds_volatile;
12489 if (is_cv_qualifier)
12490 *is_cv_qualifier = true;
12491 break;
12493 case RID_RESTRICT:
12494 ds = ds_restrict;
12495 if (is_cv_qualifier)
12496 *is_cv_qualifier = true;
12497 break;
12499 case RID_COMPLEX:
12500 /* The `__complex__' keyword is a GNU extension. */
12501 ds = ds_complex;
12502 break;
12504 default:
12505 break;
12508 /* Handle simple keywords. */
12509 if (ds != ds_last)
12511 if (decl_specs)
12513 ++decl_specs->specs[(int)ds];
12514 decl_specs->any_specifiers_p = true;
12516 return cp_lexer_consume_token (parser->lexer)->u.value;
12519 /* If we do not already have a type-specifier, assume we are looking
12520 at a simple-type-specifier. */
12521 type_spec = cp_parser_simple_type_specifier (parser,
12522 decl_specs,
12523 flags);
12525 /* If we didn't find a type-specifier, and a type-specifier was not
12526 optional in this context, issue an error message. */
12527 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12529 cp_parser_error (parser, "expected type specifier");
12530 return error_mark_node;
12533 return type_spec;
12536 /* Parse a simple-type-specifier.
12538 simple-type-specifier:
12539 :: [opt] nested-name-specifier [opt] type-name
12540 :: [opt] nested-name-specifier template template-id
12541 char
12542 wchar_t
12543 bool
12544 short
12546 long
12547 signed
12548 unsigned
12549 float
12550 double
12551 void
12553 C++0x Extension:
12555 simple-type-specifier:
12556 auto
12557 decltype ( expression )
12558 char16_t
12559 char32_t
12561 GNU Extension:
12563 simple-type-specifier:
12564 __int128
12565 __typeof__ unary-expression
12566 __typeof__ ( type-id )
12568 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12569 appropriately updated. */
12571 static tree
12572 cp_parser_simple_type_specifier (cp_parser* parser,
12573 cp_decl_specifier_seq *decl_specs,
12574 cp_parser_flags flags)
12576 tree type = NULL_TREE;
12577 cp_token *token;
12579 /* Peek at the next token. */
12580 token = cp_lexer_peek_token (parser->lexer);
12582 /* If we're looking at a keyword, things are easy. */
12583 switch (token->keyword)
12585 case RID_CHAR:
12586 if (decl_specs)
12587 decl_specs->explicit_char_p = true;
12588 type = char_type_node;
12589 break;
12590 case RID_CHAR16:
12591 type = char16_type_node;
12592 break;
12593 case RID_CHAR32:
12594 type = char32_type_node;
12595 break;
12596 case RID_WCHAR:
12597 type = wchar_type_node;
12598 break;
12599 case RID_BOOL:
12600 type = boolean_type_node;
12601 break;
12602 case RID_SHORT:
12603 if (decl_specs)
12604 ++decl_specs->specs[(int) ds_short];
12605 type = short_integer_type_node;
12606 break;
12607 case RID_INT:
12608 if (decl_specs)
12609 decl_specs->explicit_int_p = true;
12610 type = integer_type_node;
12611 break;
12612 case RID_INT128:
12613 if (!int128_integer_type_node)
12614 break;
12615 if (decl_specs)
12616 decl_specs->explicit_int128_p = true;
12617 type = int128_integer_type_node;
12618 break;
12619 case RID_LONG:
12620 if (decl_specs)
12621 ++decl_specs->specs[(int) ds_long];
12622 type = long_integer_type_node;
12623 break;
12624 case RID_SIGNED:
12625 if (decl_specs)
12626 ++decl_specs->specs[(int) ds_signed];
12627 type = integer_type_node;
12628 break;
12629 case RID_UNSIGNED:
12630 if (decl_specs)
12631 ++decl_specs->specs[(int) ds_unsigned];
12632 type = unsigned_type_node;
12633 break;
12634 case RID_FLOAT:
12635 type = float_type_node;
12636 break;
12637 case RID_DOUBLE:
12638 type = double_type_node;
12639 break;
12640 case RID_VOID:
12641 type = void_type_node;
12642 break;
12644 case RID_AUTO:
12645 maybe_warn_cpp0x (CPP0X_AUTO);
12646 type = make_auto ();
12647 break;
12649 case RID_DECLTYPE:
12650 /* Parse the `decltype' type. */
12651 type = cp_parser_decltype (parser);
12653 if (decl_specs)
12654 cp_parser_set_decl_spec_type (decl_specs, type,
12655 token->location,
12656 /*user_defined_p=*/true);
12658 return type;
12660 case RID_TYPEOF:
12661 /* Consume the `typeof' token. */
12662 cp_lexer_consume_token (parser->lexer);
12663 /* Parse the operand to `typeof'. */
12664 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12665 /* If it is not already a TYPE, take its type. */
12666 if (!TYPE_P (type))
12667 type = finish_typeof (type);
12669 if (decl_specs)
12670 cp_parser_set_decl_spec_type (decl_specs, type,
12671 token->location,
12672 /*user_defined_p=*/true);
12674 return type;
12676 default:
12677 break;
12680 /* If the type-specifier was for a built-in type, we're done. */
12681 if (type)
12683 /* Record the type. */
12684 if (decl_specs
12685 && (token->keyword != RID_SIGNED
12686 && token->keyword != RID_UNSIGNED
12687 && token->keyword != RID_SHORT
12688 && token->keyword != RID_LONG))
12689 cp_parser_set_decl_spec_type (decl_specs,
12690 type,
12691 token->location,
12692 /*user_defined=*/false);
12693 if (decl_specs)
12694 decl_specs->any_specifiers_p = true;
12696 /* Consume the token. */
12697 cp_lexer_consume_token (parser->lexer);
12699 /* There is no valid C++ program where a non-template type is
12700 followed by a "<". That usually indicates that the user thought
12701 that the type was a template. */
12702 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12704 return TYPE_NAME (type);
12707 /* The type-specifier must be a user-defined type. */
12708 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12710 bool qualified_p;
12711 bool global_p;
12713 /* Don't gobble tokens or issue error messages if this is an
12714 optional type-specifier. */
12715 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12716 cp_parser_parse_tentatively (parser);
12718 /* Look for the optional `::' operator. */
12719 global_p
12720 = (cp_parser_global_scope_opt (parser,
12721 /*current_scope_valid_p=*/false)
12722 != NULL_TREE);
12723 /* Look for the nested-name specifier. */
12724 qualified_p
12725 = (cp_parser_nested_name_specifier_opt (parser,
12726 /*typename_keyword_p=*/false,
12727 /*check_dependency_p=*/true,
12728 /*type_p=*/false,
12729 /*is_declaration=*/false)
12730 != NULL_TREE);
12731 token = cp_lexer_peek_token (parser->lexer);
12732 /* If we have seen a nested-name-specifier, and the next token
12733 is `template', then we are using the template-id production. */
12734 if (parser->scope
12735 && cp_parser_optional_template_keyword (parser))
12737 /* Look for the template-id. */
12738 type = cp_parser_template_id (parser,
12739 /*template_keyword_p=*/true,
12740 /*check_dependency_p=*/true,
12741 /*is_declaration=*/false);
12742 /* If the template-id did not name a type, we are out of
12743 luck. */
12744 if (TREE_CODE (type) != TYPE_DECL)
12746 cp_parser_error (parser, "expected template-id for type");
12747 type = NULL_TREE;
12750 /* Otherwise, look for a type-name. */
12751 else
12752 type = cp_parser_type_name (parser);
12753 /* Keep track of all name-lookups performed in class scopes. */
12754 if (type
12755 && !global_p
12756 && !qualified_p
12757 && TREE_CODE (type) == TYPE_DECL
12758 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12759 maybe_note_name_used_in_class (DECL_NAME (type), type);
12760 /* If it didn't work out, we don't have a TYPE. */
12761 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12762 && !cp_parser_parse_definitely (parser))
12763 type = NULL_TREE;
12764 if (type && decl_specs)
12765 cp_parser_set_decl_spec_type (decl_specs, type,
12766 token->location,
12767 /*user_defined=*/true);
12770 /* If we didn't get a type-name, issue an error message. */
12771 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12773 cp_parser_error (parser, "expected type-name");
12774 return error_mark_node;
12777 /* There is no valid C++ program where a non-template type is
12778 followed by a "<". That usually indicates that the user thought
12779 that the type was a template. */
12780 if (type && type != error_mark_node)
12782 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12783 If it is, then the '<'...'>' enclose protocol names rather than
12784 template arguments, and so everything is fine. */
12785 if (c_dialect_objc () && !parser->scope
12786 && (objc_is_id (type) || objc_is_class_name (type)))
12788 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12789 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12791 /* Clobber the "unqualified" type previously entered into
12792 DECL_SPECS with the new, improved protocol-qualified version. */
12793 if (decl_specs)
12794 decl_specs->type = qual_type;
12796 return qual_type;
12799 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12800 token->location);
12803 return type;
12806 /* Parse a type-name.
12808 type-name:
12809 class-name
12810 enum-name
12811 typedef-name
12813 enum-name:
12814 identifier
12816 typedef-name:
12817 identifier
12819 Returns a TYPE_DECL for the type. */
12821 static tree
12822 cp_parser_type_name (cp_parser* parser)
12824 tree type_decl;
12826 /* We can't know yet whether it is a class-name or not. */
12827 cp_parser_parse_tentatively (parser);
12828 /* Try a class-name. */
12829 type_decl = cp_parser_class_name (parser,
12830 /*typename_keyword_p=*/false,
12831 /*template_keyword_p=*/false,
12832 none_type,
12833 /*check_dependency_p=*/true,
12834 /*class_head_p=*/false,
12835 /*is_declaration=*/false);
12836 /* If it's not a class-name, keep looking. */
12837 if (!cp_parser_parse_definitely (parser))
12839 /* It must be a typedef-name or an enum-name. */
12840 return cp_parser_nonclass_name (parser);
12843 return type_decl;
12846 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12848 enum-name:
12849 identifier
12851 typedef-name:
12852 identifier
12854 Returns a TYPE_DECL for the type. */
12856 static tree
12857 cp_parser_nonclass_name (cp_parser* parser)
12859 tree type_decl;
12860 tree identifier;
12862 cp_token *token = cp_lexer_peek_token (parser->lexer);
12863 identifier = cp_parser_identifier (parser);
12864 if (identifier == error_mark_node)
12865 return error_mark_node;
12867 /* Look up the type-name. */
12868 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12870 if (TREE_CODE (type_decl) != TYPE_DECL
12871 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12873 /* See if this is an Objective-C type. */
12874 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12875 tree type = objc_get_protocol_qualified_type (identifier, protos);
12876 if (type)
12877 type_decl = TYPE_NAME (type);
12880 /* Issue an error if we did not find a type-name. */
12881 if (TREE_CODE (type_decl) != TYPE_DECL)
12883 if (!cp_parser_simulate_error (parser))
12884 cp_parser_name_lookup_error (parser, identifier, type_decl,
12885 NLE_TYPE, token->location);
12886 return error_mark_node;
12888 /* Remember that the name was used in the definition of the
12889 current class so that we can check later to see if the
12890 meaning would have been different after the class was
12891 entirely defined. */
12892 else if (type_decl != error_mark_node
12893 && !parser->scope)
12894 maybe_note_name_used_in_class (identifier, type_decl);
12896 return type_decl;
12899 /* Parse an elaborated-type-specifier. Note that the grammar given
12900 here incorporates the resolution to DR68.
12902 elaborated-type-specifier:
12903 class-key :: [opt] nested-name-specifier [opt] identifier
12904 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12905 enum-key :: [opt] nested-name-specifier [opt] identifier
12906 typename :: [opt] nested-name-specifier identifier
12907 typename :: [opt] nested-name-specifier template [opt]
12908 template-id
12910 GNU extension:
12912 elaborated-type-specifier:
12913 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12914 class-key attributes :: [opt] nested-name-specifier [opt]
12915 template [opt] template-id
12916 enum attributes :: [opt] nested-name-specifier [opt] identifier
12918 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12919 declared `friend'. If IS_DECLARATION is TRUE, then this
12920 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12921 something is being declared.
12923 Returns the TYPE specified. */
12925 static tree
12926 cp_parser_elaborated_type_specifier (cp_parser* parser,
12927 bool is_friend,
12928 bool is_declaration)
12930 enum tag_types tag_type;
12931 tree identifier;
12932 tree type = NULL_TREE;
12933 tree attributes = NULL_TREE;
12934 tree globalscope;
12935 cp_token *token = NULL;
12937 /* See if we're looking at the `enum' keyword. */
12938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12940 /* Consume the `enum' token. */
12941 cp_lexer_consume_token (parser->lexer);
12942 /* Remember that it's an enumeration type. */
12943 tag_type = enum_type;
12944 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12945 enums) is used here. */
12946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12947 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12949 pedwarn (input_location, 0, "elaborated-type-specifier "
12950 "for a scoped enum must not use the %<%D%> keyword",
12951 cp_lexer_peek_token (parser->lexer)->u.value);
12952 /* Consume the `struct' or `class' and parse it anyway. */
12953 cp_lexer_consume_token (parser->lexer);
12955 /* Parse the attributes. */
12956 attributes = cp_parser_attributes_opt (parser);
12958 /* Or, it might be `typename'. */
12959 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12960 RID_TYPENAME))
12962 /* Consume the `typename' token. */
12963 cp_lexer_consume_token (parser->lexer);
12964 /* Remember that it's a `typename' type. */
12965 tag_type = typename_type;
12967 /* Otherwise it must be a class-key. */
12968 else
12970 tag_type = cp_parser_class_key (parser);
12971 if (tag_type == none_type)
12972 return error_mark_node;
12973 /* Parse the attributes. */
12974 attributes = cp_parser_attributes_opt (parser);
12977 /* Look for the `::' operator. */
12978 globalscope = cp_parser_global_scope_opt (parser,
12979 /*current_scope_valid_p=*/false);
12980 /* Look for the nested-name-specifier. */
12981 if (tag_type == typename_type && !globalscope)
12983 if (!cp_parser_nested_name_specifier (parser,
12984 /*typename_keyword_p=*/true,
12985 /*check_dependency_p=*/true,
12986 /*type_p=*/true,
12987 is_declaration))
12988 return error_mark_node;
12990 else
12991 /* Even though `typename' is not present, the proposed resolution
12992 to Core Issue 180 says that in `class A<T>::B', `B' should be
12993 considered a type-name, even if `A<T>' is dependent. */
12994 cp_parser_nested_name_specifier_opt (parser,
12995 /*typename_keyword_p=*/true,
12996 /*check_dependency_p=*/true,
12997 /*type_p=*/true,
12998 is_declaration);
12999 /* For everything but enumeration types, consider a template-id.
13000 For an enumeration type, consider only a plain identifier. */
13001 if (tag_type != enum_type)
13003 bool template_p = false;
13004 tree decl;
13006 /* Allow the `template' keyword. */
13007 template_p = cp_parser_optional_template_keyword (parser);
13008 /* If we didn't see `template', we don't know if there's a
13009 template-id or not. */
13010 if (!template_p)
13011 cp_parser_parse_tentatively (parser);
13012 /* Parse the template-id. */
13013 token = cp_lexer_peek_token (parser->lexer);
13014 decl = cp_parser_template_id (parser, template_p,
13015 /*check_dependency_p=*/true,
13016 is_declaration);
13017 /* If we didn't find a template-id, look for an ordinary
13018 identifier. */
13019 if (!template_p && !cp_parser_parse_definitely (parser))
13021 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13022 in effect, then we must assume that, upon instantiation, the
13023 template will correspond to a class. */
13024 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13025 && tag_type == typename_type)
13026 type = make_typename_type (parser->scope, decl,
13027 typename_type,
13028 /*complain=*/tf_error);
13029 /* If the `typename' keyword is in effect and DECL is not a type
13030 decl. Then type is non existant. */
13031 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13032 type = NULL_TREE;
13033 else
13034 type = TREE_TYPE (decl);
13037 if (!type)
13039 token = cp_lexer_peek_token (parser->lexer);
13040 identifier = cp_parser_identifier (parser);
13042 if (identifier == error_mark_node)
13044 parser->scope = NULL_TREE;
13045 return error_mark_node;
13048 /* For a `typename', we needn't call xref_tag. */
13049 if (tag_type == typename_type
13050 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13051 return cp_parser_make_typename_type (parser, parser->scope,
13052 identifier,
13053 token->location);
13054 /* Look up a qualified name in the usual way. */
13055 if (parser->scope)
13057 tree decl;
13058 tree ambiguous_decls;
13060 decl = cp_parser_lookup_name (parser, identifier,
13061 tag_type,
13062 /*is_template=*/false,
13063 /*is_namespace=*/false,
13064 /*check_dependency=*/true,
13065 &ambiguous_decls,
13066 token->location);
13068 /* If the lookup was ambiguous, an error will already have been
13069 issued. */
13070 if (ambiguous_decls)
13071 return error_mark_node;
13073 /* If we are parsing friend declaration, DECL may be a
13074 TEMPLATE_DECL tree node here. However, we need to check
13075 whether this TEMPLATE_DECL results in valid code. Consider
13076 the following example:
13078 namespace N {
13079 template <class T> class C {};
13081 class X {
13082 template <class T> friend class N::C; // #1, valid code
13084 template <class T> class Y {
13085 friend class N::C; // #2, invalid code
13088 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13089 name lookup of `N::C'. We see that friend declaration must
13090 be template for the code to be valid. Note that
13091 processing_template_decl does not work here since it is
13092 always 1 for the above two cases. */
13094 decl = (cp_parser_maybe_treat_template_as_class
13095 (decl, /*tag_name_p=*/is_friend
13096 && parser->num_template_parameter_lists));
13098 if (TREE_CODE (decl) != TYPE_DECL)
13100 cp_parser_diagnose_invalid_type_name (parser,
13101 parser->scope,
13102 identifier,
13103 token->location);
13104 return error_mark_node;
13107 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13109 bool allow_template = (parser->num_template_parameter_lists
13110 || DECL_SELF_REFERENCE_P (decl));
13111 type = check_elaborated_type_specifier (tag_type, decl,
13112 allow_template);
13114 if (type == error_mark_node)
13115 return error_mark_node;
13118 /* Forward declarations of nested types, such as
13120 class C1::C2;
13121 class C1::C2::C3;
13123 are invalid unless all components preceding the final '::'
13124 are complete. If all enclosing types are complete, these
13125 declarations become merely pointless.
13127 Invalid forward declarations of nested types are errors
13128 caught elsewhere in parsing. Those that are pointless arrive
13129 here. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13132 && !is_friend && !processing_explicit_instantiation)
13133 warning (0, "declaration %qD does not declare anything", decl);
13135 type = TREE_TYPE (decl);
13137 else
13139 /* An elaborated-type-specifier sometimes introduces a new type and
13140 sometimes names an existing type. Normally, the rule is that it
13141 introduces a new type only if there is not an existing type of
13142 the same name already in scope. For example, given:
13144 struct S {};
13145 void f() { struct S s; }
13147 the `struct S' in the body of `f' is the same `struct S' as in
13148 the global scope; the existing definition is used. However, if
13149 there were no global declaration, this would introduce a new
13150 local class named `S'.
13152 An exception to this rule applies to the following code:
13154 namespace N { struct S; }
13156 Here, the elaborated-type-specifier names a new type
13157 unconditionally; even if there is already an `S' in the
13158 containing scope this declaration names a new type.
13159 This exception only applies if the elaborated-type-specifier
13160 forms the complete declaration:
13162 [class.name]
13164 A declaration consisting solely of `class-key identifier ;' is
13165 either a redeclaration of the name in the current scope or a
13166 forward declaration of the identifier as a class name. It
13167 introduces the name into the current scope.
13169 We are in this situation precisely when the next token is a `;'.
13171 An exception to the exception is that a `friend' declaration does
13172 *not* name a new type; i.e., given:
13174 struct S { friend struct T; };
13176 `T' is not a new type in the scope of `S'.
13178 Also, `new struct S' or `sizeof (struct S)' never results in the
13179 definition of a new type; a new type can only be declared in a
13180 declaration context. */
13182 tag_scope ts;
13183 bool template_p;
13185 if (is_friend)
13186 /* Friends have special name lookup rules. */
13187 ts = ts_within_enclosing_non_class;
13188 else if (is_declaration
13189 && cp_lexer_next_token_is (parser->lexer,
13190 CPP_SEMICOLON))
13191 /* This is a `class-key identifier ;' */
13192 ts = ts_current;
13193 else
13194 ts = ts_global;
13196 template_p =
13197 (parser->num_template_parameter_lists
13198 && (cp_parser_next_token_starts_class_definition_p (parser)
13199 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13200 /* An unqualified name was used to reference this type, so
13201 there were no qualifying templates. */
13202 if (!cp_parser_check_template_parameters (parser,
13203 /*num_templates=*/0,
13204 token->location,
13205 /*declarator=*/NULL))
13206 return error_mark_node;
13207 type = xref_tag (tag_type, identifier, ts, template_p);
13211 if (type == error_mark_node)
13212 return error_mark_node;
13214 /* Allow attributes on forward declarations of classes. */
13215 if (attributes)
13217 if (TREE_CODE (type) == TYPENAME_TYPE)
13218 warning (OPT_Wattributes,
13219 "attributes ignored on uninstantiated type");
13220 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13221 && ! processing_explicit_instantiation)
13222 warning (OPT_Wattributes,
13223 "attributes ignored on template instantiation");
13224 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13225 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13226 else
13227 warning (OPT_Wattributes,
13228 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13231 if (tag_type != enum_type)
13232 cp_parser_check_class_key (tag_type, type);
13234 /* A "<" cannot follow an elaborated type specifier. If that
13235 happens, the user was probably trying to form a template-id. */
13236 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13238 return type;
13241 /* Parse an enum-specifier.
13243 enum-specifier:
13244 enum-head { enumerator-list [opt] }
13246 enum-head:
13247 enum-key identifier [opt] enum-base [opt]
13248 enum-key nested-name-specifier identifier enum-base [opt]
13250 enum-key:
13251 enum
13252 enum class [C++0x]
13253 enum struct [C++0x]
13255 enum-base: [C++0x]
13256 : type-specifier-seq
13258 opaque-enum-specifier:
13259 enum-key identifier enum-base [opt] ;
13261 GNU Extensions:
13262 enum-key attributes[opt] identifier [opt] enum-base [opt]
13263 { enumerator-list [opt] }attributes[opt]
13265 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13266 if the token stream isn't an enum-specifier after all. */
13268 static tree
13269 cp_parser_enum_specifier (cp_parser* parser)
13271 tree identifier;
13272 tree type = NULL_TREE;
13273 tree prev_scope;
13274 tree nested_name_specifier = NULL_TREE;
13275 tree attributes;
13276 bool scoped_enum_p = false;
13277 bool has_underlying_type = false;
13278 bool nested_being_defined = false;
13279 bool new_value_list = false;
13280 bool is_new_type = false;
13281 bool is_anonymous = false;
13282 tree underlying_type = NULL_TREE;
13283 cp_token *type_start_token = NULL;
13285 /* Parse tentatively so that we can back up if we don't find a
13286 enum-specifier. */
13287 cp_parser_parse_tentatively (parser);
13289 /* Caller guarantees that the current token is 'enum', an identifier
13290 possibly follows, and the token after that is an opening brace.
13291 If we don't have an identifier, fabricate an anonymous name for
13292 the enumeration being defined. */
13293 cp_lexer_consume_token (parser->lexer);
13295 /* Parse the "class" or "struct", which indicates a scoped
13296 enumeration type in C++0x. */
13297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13298 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13300 if (cxx_dialect < cxx0x)
13301 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13303 /* Consume the `struct' or `class' token. */
13304 cp_lexer_consume_token (parser->lexer);
13306 scoped_enum_p = true;
13309 attributes = cp_parser_attributes_opt (parser);
13311 /* Clear the qualification. */
13312 parser->scope = NULL_TREE;
13313 parser->qualifying_scope = NULL_TREE;
13314 parser->object_scope = NULL_TREE;
13316 /* Figure out in what scope the declaration is being placed. */
13317 prev_scope = current_scope ();
13319 type_start_token = cp_lexer_peek_token (parser->lexer);
13321 push_deferring_access_checks (dk_no_check);
13322 nested_name_specifier
13323 = cp_parser_nested_name_specifier_opt (parser,
13324 /*typename_keyword_p=*/true,
13325 /*check_dependency_p=*/false,
13326 /*type_p=*/false,
13327 /*is_declaration=*/false);
13329 if (nested_name_specifier)
13331 tree name;
13333 identifier = cp_parser_identifier (parser);
13334 name = cp_parser_lookup_name (parser, identifier,
13335 enum_type,
13336 /*is_template=*/false,
13337 /*is_namespace=*/false,
13338 /*check_dependency=*/true,
13339 /*ambiguous_decls=*/NULL,
13340 input_location);
13341 if (name)
13343 type = TREE_TYPE (name);
13344 if (TREE_CODE (type) == TYPENAME_TYPE)
13346 /* Are template enums allowed in ISO? */
13347 if (template_parm_scope_p ())
13348 pedwarn (type_start_token->location, OPT_pedantic,
13349 "%qD is an enumeration template", name);
13350 /* ignore a typename reference, for it will be solved by name
13351 in start_enum. */
13352 type = NULL_TREE;
13355 else
13356 error_at (type_start_token->location,
13357 "%qD is not an enumerator-name", identifier);
13359 else
13361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13362 identifier = cp_parser_identifier (parser);
13363 else
13365 identifier = make_anon_name ();
13366 is_anonymous = true;
13369 pop_deferring_access_checks ();
13371 /* Check for the `:' that denotes a specified underlying type in C++0x.
13372 Note that a ':' could also indicate a bitfield width, however. */
13373 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13375 cp_decl_specifier_seq type_specifiers;
13377 /* Consume the `:'. */
13378 cp_lexer_consume_token (parser->lexer);
13380 /* Parse the type-specifier-seq. */
13381 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13382 /*is_trailing_return=*/false,
13383 &type_specifiers);
13385 /* At this point this is surely not elaborated type specifier. */
13386 if (!cp_parser_parse_definitely (parser))
13387 return NULL_TREE;
13389 if (cxx_dialect < cxx0x)
13390 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13392 has_underlying_type = true;
13394 /* If that didn't work, stop. */
13395 if (type_specifiers.type != error_mark_node)
13397 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13398 /*initialized=*/0, NULL);
13399 if (underlying_type == error_mark_node)
13400 underlying_type = NULL_TREE;
13404 /* Look for the `{' but don't consume it yet. */
13405 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13407 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13409 cp_parser_error (parser, "expected %<{%>");
13410 if (has_underlying_type)
13411 return NULL_TREE;
13413 /* An opaque-enum-specifier must have a ';' here. */
13414 if ((scoped_enum_p || underlying_type)
13415 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13417 cp_parser_error (parser, "expected %<;%> or %<{%>");
13418 if (has_underlying_type)
13419 return NULL_TREE;
13423 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13424 return NULL_TREE;
13426 if (nested_name_specifier)
13428 if (CLASS_TYPE_P (nested_name_specifier))
13430 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13431 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13432 push_scope (nested_name_specifier);
13434 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13436 push_nested_namespace (nested_name_specifier);
13440 /* Issue an error message if type-definitions are forbidden here. */
13441 if (!cp_parser_check_type_definition (parser))
13442 type = error_mark_node;
13443 else
13444 /* Create the new type. We do this before consuming the opening
13445 brace so the enum will be recorded as being on the line of its
13446 tag (or the 'enum' keyword, if there is no tag). */
13447 type = start_enum (identifier, type, underlying_type,
13448 scoped_enum_p, &is_new_type);
13450 /* If the next token is not '{' it is an opaque-enum-specifier or an
13451 elaborated-type-specifier. */
13452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13454 if (nested_name_specifier)
13456 /* The following catches invalid code such as:
13457 enum class S<int>::E { A, B, C }; */
13458 if (!processing_specialization
13459 && CLASS_TYPE_P (nested_name_specifier)
13460 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13461 error_at (type_start_token->location, "cannot add an enumerator "
13462 "list to a template instantiation");
13464 /* If that scope does not contain the scope in which the
13465 class was originally declared, the program is invalid. */
13466 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13468 if (at_namespace_scope_p ())
13469 error_at (type_start_token->location,
13470 "declaration of %qD in namespace %qD which does not "
13471 "enclose %qD",
13472 type, prev_scope, nested_name_specifier);
13473 else
13474 error_at (type_start_token->location,
13475 "declaration of %qD in %qD which does not enclose %qD",
13476 type, prev_scope, nested_name_specifier);
13477 type = error_mark_node;
13481 if (scoped_enum_p)
13482 begin_scope (sk_scoped_enum, type);
13484 /* Consume the opening brace. */
13485 cp_lexer_consume_token (parser->lexer);
13487 if (type == error_mark_node)
13488 ; /* Nothing to add */
13489 else if (OPAQUE_ENUM_P (type)
13490 || (cxx_dialect > cxx98 && processing_specialization))
13492 new_value_list = true;
13493 SET_OPAQUE_ENUM_P (type, false);
13494 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13496 else
13498 error_at (type_start_token->location, "multiple definition of %q#T", type);
13499 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13500 "previous definition here");
13501 type = error_mark_node;
13504 if (type == error_mark_node)
13505 cp_parser_skip_to_end_of_block_or_statement (parser);
13506 /* If the next token is not '}', then there are some enumerators. */
13507 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13508 cp_parser_enumerator_list (parser, type);
13510 /* Consume the final '}'. */
13511 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13513 if (scoped_enum_p)
13514 finish_scope ();
13516 else
13518 /* If a ';' follows, then it is an opaque-enum-specifier
13519 and additional restrictions apply. */
13520 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13522 if (is_anonymous)
13523 error_at (type_start_token->location,
13524 "opaque-enum-specifier without name");
13525 else if (nested_name_specifier)
13526 error_at (type_start_token->location,
13527 "opaque-enum-specifier must use a simple identifier");
13531 /* Look for trailing attributes to apply to this enumeration, and
13532 apply them if appropriate. */
13533 if (cp_parser_allow_gnu_extensions_p (parser))
13535 tree trailing_attr = cp_parser_attributes_opt (parser);
13536 trailing_attr = chainon (trailing_attr, attributes);
13537 cplus_decl_attributes (&type,
13538 trailing_attr,
13539 (int) ATTR_FLAG_TYPE_IN_PLACE);
13542 /* Finish up the enumeration. */
13543 if (type != error_mark_node)
13545 if (new_value_list)
13546 finish_enum_value_list (type);
13547 if (is_new_type)
13548 finish_enum (type);
13551 if (nested_name_specifier)
13553 if (CLASS_TYPE_P (nested_name_specifier))
13555 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13556 pop_scope (nested_name_specifier);
13558 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13560 pop_nested_namespace (nested_name_specifier);
13563 return type;
13566 /* Parse an enumerator-list. The enumerators all have the indicated
13567 TYPE.
13569 enumerator-list:
13570 enumerator-definition
13571 enumerator-list , enumerator-definition */
13573 static void
13574 cp_parser_enumerator_list (cp_parser* parser, tree type)
13576 while (true)
13578 /* Parse an enumerator-definition. */
13579 cp_parser_enumerator_definition (parser, type);
13581 /* If the next token is not a ',', we've reached the end of
13582 the list. */
13583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13584 break;
13585 /* Otherwise, consume the `,' and keep going. */
13586 cp_lexer_consume_token (parser->lexer);
13587 /* If the next token is a `}', there is a trailing comma. */
13588 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13590 if (!in_system_header)
13591 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13592 break;
13597 /* Parse an enumerator-definition. The enumerator has the indicated
13598 TYPE.
13600 enumerator-definition:
13601 enumerator
13602 enumerator = constant-expression
13604 enumerator:
13605 identifier */
13607 static void
13608 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13610 tree identifier;
13611 tree value;
13612 location_t loc;
13614 /* Save the input location because we are interested in the location
13615 of the identifier and not the location of the explicit value. */
13616 loc = cp_lexer_peek_token (parser->lexer)->location;
13618 /* Look for the identifier. */
13619 identifier = cp_parser_identifier (parser);
13620 if (identifier == error_mark_node)
13621 return;
13623 /* If the next token is an '=', then there is an explicit value. */
13624 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13626 /* Consume the `=' token. */
13627 cp_lexer_consume_token (parser->lexer);
13628 /* Parse the value. */
13629 value = cp_parser_constant_expression (parser,
13630 /*allow_non_constant_p=*/false,
13631 NULL);
13633 else
13634 value = NULL_TREE;
13636 /* If we are processing a template, make sure the initializer of the
13637 enumerator doesn't contain any bare template parameter pack. */
13638 if (check_for_bare_parameter_packs (value))
13639 value = error_mark_node;
13641 /* Create the enumerator. */
13642 build_enumerator (identifier, value, type, loc);
13645 /* Parse a namespace-name.
13647 namespace-name:
13648 original-namespace-name
13649 namespace-alias
13651 Returns the NAMESPACE_DECL for the namespace. */
13653 static tree
13654 cp_parser_namespace_name (cp_parser* parser)
13656 tree identifier;
13657 tree namespace_decl;
13659 cp_token *token = cp_lexer_peek_token (parser->lexer);
13661 /* Get the name of the namespace. */
13662 identifier = cp_parser_identifier (parser);
13663 if (identifier == error_mark_node)
13664 return error_mark_node;
13666 /* Look up the identifier in the currently active scope. Look only
13667 for namespaces, due to:
13669 [basic.lookup.udir]
13671 When looking up a namespace-name in a using-directive or alias
13672 definition, only namespace names are considered.
13674 And:
13676 [basic.lookup.qual]
13678 During the lookup of a name preceding the :: scope resolution
13679 operator, object, function, and enumerator names are ignored.
13681 (Note that cp_parser_qualifying_entity only calls this
13682 function if the token after the name is the scope resolution
13683 operator.) */
13684 namespace_decl = cp_parser_lookup_name (parser, identifier,
13685 none_type,
13686 /*is_template=*/false,
13687 /*is_namespace=*/true,
13688 /*check_dependency=*/true,
13689 /*ambiguous_decls=*/NULL,
13690 token->location);
13691 /* If it's not a namespace, issue an error. */
13692 if (namespace_decl == error_mark_node
13693 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13695 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13696 error_at (token->location, "%qD is not a namespace-name", identifier);
13697 cp_parser_error (parser, "expected namespace-name");
13698 namespace_decl = error_mark_node;
13701 return namespace_decl;
13704 /* Parse a namespace-definition.
13706 namespace-definition:
13707 named-namespace-definition
13708 unnamed-namespace-definition
13710 named-namespace-definition:
13711 original-namespace-definition
13712 extension-namespace-definition
13714 original-namespace-definition:
13715 namespace identifier { namespace-body }
13717 extension-namespace-definition:
13718 namespace original-namespace-name { namespace-body }
13720 unnamed-namespace-definition:
13721 namespace { namespace-body } */
13723 static void
13724 cp_parser_namespace_definition (cp_parser* parser)
13726 tree identifier, attribs;
13727 bool has_visibility;
13728 bool is_inline;
13730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13732 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13733 is_inline = true;
13734 cp_lexer_consume_token (parser->lexer);
13736 else
13737 is_inline = false;
13739 /* Look for the `namespace' keyword. */
13740 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13742 /* Get the name of the namespace. We do not attempt to distinguish
13743 between an original-namespace-definition and an
13744 extension-namespace-definition at this point. The semantic
13745 analysis routines are responsible for that. */
13746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13747 identifier = cp_parser_identifier (parser);
13748 else
13749 identifier = NULL_TREE;
13751 /* Parse any specified attributes. */
13752 attribs = cp_parser_attributes_opt (parser);
13754 /* Look for the `{' to start the namespace. */
13755 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13756 /* Start the namespace. */
13757 push_namespace (identifier);
13759 /* "inline namespace" is equivalent to a stub namespace definition
13760 followed by a strong using directive. */
13761 if (is_inline)
13763 tree name_space = current_namespace;
13764 /* Set up namespace association. */
13765 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13766 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13767 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13768 /* Import the contents of the inline namespace. */
13769 pop_namespace ();
13770 do_using_directive (name_space);
13771 push_namespace (identifier);
13774 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13776 /* Parse the body of the namespace. */
13777 cp_parser_namespace_body (parser);
13779 #ifdef HANDLE_PRAGMA_VISIBILITY
13780 if (has_visibility)
13781 pop_visibility (1);
13782 #endif
13784 /* Finish the namespace. */
13785 pop_namespace ();
13786 /* Look for the final `}'. */
13787 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13790 /* Parse a namespace-body.
13792 namespace-body:
13793 declaration-seq [opt] */
13795 static void
13796 cp_parser_namespace_body (cp_parser* parser)
13798 cp_parser_declaration_seq_opt (parser);
13801 /* Parse a namespace-alias-definition.
13803 namespace-alias-definition:
13804 namespace identifier = qualified-namespace-specifier ; */
13806 static void
13807 cp_parser_namespace_alias_definition (cp_parser* parser)
13809 tree identifier;
13810 tree namespace_specifier;
13812 cp_token *token = cp_lexer_peek_token (parser->lexer);
13814 /* Look for the `namespace' keyword. */
13815 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13816 /* Look for the identifier. */
13817 identifier = cp_parser_identifier (parser);
13818 if (identifier == error_mark_node)
13819 return;
13820 /* Look for the `=' token. */
13821 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13822 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13824 error_at (token->location, "%<namespace%> definition is not allowed here");
13825 /* Skip the definition. */
13826 cp_lexer_consume_token (parser->lexer);
13827 if (cp_parser_skip_to_closing_brace (parser))
13828 cp_lexer_consume_token (parser->lexer);
13829 return;
13831 cp_parser_require (parser, CPP_EQ, RT_EQ);
13832 /* Look for the qualified-namespace-specifier. */
13833 namespace_specifier
13834 = cp_parser_qualified_namespace_specifier (parser);
13835 /* Look for the `;' token. */
13836 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13838 /* Register the alias in the symbol table. */
13839 do_namespace_alias (identifier, namespace_specifier);
13842 /* Parse a qualified-namespace-specifier.
13844 qualified-namespace-specifier:
13845 :: [opt] nested-name-specifier [opt] namespace-name
13847 Returns a NAMESPACE_DECL corresponding to the specified
13848 namespace. */
13850 static tree
13851 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13853 /* Look for the optional `::'. */
13854 cp_parser_global_scope_opt (parser,
13855 /*current_scope_valid_p=*/false);
13857 /* Look for the optional nested-name-specifier. */
13858 cp_parser_nested_name_specifier_opt (parser,
13859 /*typename_keyword_p=*/false,
13860 /*check_dependency_p=*/true,
13861 /*type_p=*/false,
13862 /*is_declaration=*/true);
13864 return cp_parser_namespace_name (parser);
13867 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13868 access declaration.
13870 using-declaration:
13871 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13872 using :: unqualified-id ;
13874 access-declaration:
13875 qualified-id ;
13879 static bool
13880 cp_parser_using_declaration (cp_parser* parser,
13881 bool access_declaration_p)
13883 cp_token *token;
13884 bool typename_p = false;
13885 bool global_scope_p;
13886 tree decl;
13887 tree identifier;
13888 tree qscope;
13890 if (access_declaration_p)
13891 cp_parser_parse_tentatively (parser);
13892 else
13894 /* Look for the `using' keyword. */
13895 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13897 /* Peek at the next token. */
13898 token = cp_lexer_peek_token (parser->lexer);
13899 /* See if it's `typename'. */
13900 if (token->keyword == RID_TYPENAME)
13902 /* Remember that we've seen it. */
13903 typename_p = true;
13904 /* Consume the `typename' token. */
13905 cp_lexer_consume_token (parser->lexer);
13909 /* Look for the optional global scope qualification. */
13910 global_scope_p
13911 = (cp_parser_global_scope_opt (parser,
13912 /*current_scope_valid_p=*/false)
13913 != NULL_TREE);
13915 /* If we saw `typename', or didn't see `::', then there must be a
13916 nested-name-specifier present. */
13917 if (typename_p || !global_scope_p)
13918 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13919 /*check_dependency_p=*/true,
13920 /*type_p=*/false,
13921 /*is_declaration=*/true);
13922 /* Otherwise, we could be in either of the two productions. In that
13923 case, treat the nested-name-specifier as optional. */
13924 else
13925 qscope = cp_parser_nested_name_specifier_opt (parser,
13926 /*typename_keyword_p=*/false,
13927 /*check_dependency_p=*/true,
13928 /*type_p=*/false,
13929 /*is_declaration=*/true);
13930 if (!qscope)
13931 qscope = global_namespace;
13933 if (access_declaration_p && cp_parser_error_occurred (parser))
13934 /* Something has already gone wrong; there's no need to parse
13935 further. Since an error has occurred, the return value of
13936 cp_parser_parse_definitely will be false, as required. */
13937 return cp_parser_parse_definitely (parser);
13939 token = cp_lexer_peek_token (parser->lexer);
13940 /* Parse the unqualified-id. */
13941 identifier = cp_parser_unqualified_id (parser,
13942 /*template_keyword_p=*/false,
13943 /*check_dependency_p=*/true,
13944 /*declarator_p=*/true,
13945 /*optional_p=*/false);
13947 if (access_declaration_p)
13949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13950 cp_parser_simulate_error (parser);
13951 if (!cp_parser_parse_definitely (parser))
13952 return false;
13955 /* The function we call to handle a using-declaration is different
13956 depending on what scope we are in. */
13957 if (qscope == error_mark_node || identifier == error_mark_node)
13959 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13960 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13961 /* [namespace.udecl]
13963 A using declaration shall not name a template-id. */
13964 error_at (token->location,
13965 "a template-id may not appear in a using-declaration");
13966 else
13968 if (at_class_scope_p ())
13970 /* Create the USING_DECL. */
13971 decl = do_class_using_decl (parser->scope, identifier);
13973 if (check_for_bare_parameter_packs (decl))
13974 return false;
13975 else
13976 /* Add it to the list of members in this class. */
13977 finish_member_declaration (decl);
13979 else
13981 decl = cp_parser_lookup_name_simple (parser,
13982 identifier,
13983 token->location);
13984 if (decl == error_mark_node)
13985 cp_parser_name_lookup_error (parser, identifier,
13986 decl, NLE_NULL,
13987 token->location);
13988 else if (check_for_bare_parameter_packs (decl))
13989 return false;
13990 else if (!at_namespace_scope_p ())
13991 do_local_using_decl (decl, qscope, identifier);
13992 else
13993 do_toplevel_using_decl (decl, qscope, identifier);
13997 /* Look for the final `;'. */
13998 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14000 return true;
14003 /* Parse a using-directive.
14005 using-directive:
14006 using namespace :: [opt] nested-name-specifier [opt]
14007 namespace-name ; */
14009 static void
14010 cp_parser_using_directive (cp_parser* parser)
14012 tree namespace_decl;
14013 tree attribs;
14015 /* Look for the `using' keyword. */
14016 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14017 /* And the `namespace' keyword. */
14018 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14019 /* Look for the optional `::' operator. */
14020 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14021 /* And the optional nested-name-specifier. */
14022 cp_parser_nested_name_specifier_opt (parser,
14023 /*typename_keyword_p=*/false,
14024 /*check_dependency_p=*/true,
14025 /*type_p=*/false,
14026 /*is_declaration=*/true);
14027 /* Get the namespace being used. */
14028 namespace_decl = cp_parser_namespace_name (parser);
14029 /* And any specified attributes. */
14030 attribs = cp_parser_attributes_opt (parser);
14031 /* Update the symbol table. */
14032 parse_using_directive (namespace_decl, attribs);
14033 /* Look for the final `;'. */
14034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14037 /* Parse an asm-definition.
14039 asm-definition:
14040 asm ( string-literal ) ;
14042 GNU Extension:
14044 asm-definition:
14045 asm volatile [opt] ( string-literal ) ;
14046 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14047 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14048 : asm-operand-list [opt] ) ;
14049 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14050 : asm-operand-list [opt]
14051 : asm-clobber-list [opt] ) ;
14052 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14053 : asm-clobber-list [opt]
14054 : asm-goto-list ) ; */
14056 static void
14057 cp_parser_asm_definition (cp_parser* parser)
14059 tree string;
14060 tree outputs = NULL_TREE;
14061 tree inputs = NULL_TREE;
14062 tree clobbers = NULL_TREE;
14063 tree labels = NULL_TREE;
14064 tree asm_stmt;
14065 bool volatile_p = false;
14066 bool extended_p = false;
14067 bool invalid_inputs_p = false;
14068 bool invalid_outputs_p = false;
14069 bool goto_p = false;
14070 required_token missing = RT_NONE;
14072 /* Look for the `asm' keyword. */
14073 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14074 /* See if the next token is `volatile'. */
14075 if (cp_parser_allow_gnu_extensions_p (parser)
14076 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14078 /* Remember that we saw the `volatile' keyword. */
14079 volatile_p = true;
14080 /* Consume the token. */
14081 cp_lexer_consume_token (parser->lexer);
14083 if (cp_parser_allow_gnu_extensions_p (parser)
14084 && parser->in_function_body
14085 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14087 /* Remember that we saw the `goto' keyword. */
14088 goto_p = true;
14089 /* Consume the token. */
14090 cp_lexer_consume_token (parser->lexer);
14092 /* Look for the opening `('. */
14093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14094 return;
14095 /* Look for the string. */
14096 string = cp_parser_string_literal (parser, false, false);
14097 if (string == error_mark_node)
14099 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14100 /*consume_paren=*/true);
14101 return;
14104 /* If we're allowing GNU extensions, check for the extended assembly
14105 syntax. Unfortunately, the `:' tokens need not be separated by
14106 a space in C, and so, for compatibility, we tolerate that here
14107 too. Doing that means that we have to treat the `::' operator as
14108 two `:' tokens. */
14109 if (cp_parser_allow_gnu_extensions_p (parser)
14110 && parser->in_function_body
14111 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14112 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14114 bool inputs_p = false;
14115 bool clobbers_p = false;
14116 bool labels_p = false;
14118 /* The extended syntax was used. */
14119 extended_p = true;
14121 /* Look for outputs. */
14122 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14124 /* Consume the `:'. */
14125 cp_lexer_consume_token (parser->lexer);
14126 /* Parse the output-operands. */
14127 if (cp_lexer_next_token_is_not (parser->lexer,
14128 CPP_COLON)
14129 && cp_lexer_next_token_is_not (parser->lexer,
14130 CPP_SCOPE)
14131 && cp_lexer_next_token_is_not (parser->lexer,
14132 CPP_CLOSE_PAREN)
14133 && !goto_p)
14134 outputs = cp_parser_asm_operand_list (parser);
14136 if (outputs == error_mark_node)
14137 invalid_outputs_p = true;
14139 /* If the next token is `::', there are no outputs, and the
14140 next token is the beginning of the inputs. */
14141 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14142 /* The inputs are coming next. */
14143 inputs_p = true;
14145 /* Look for inputs. */
14146 if (inputs_p
14147 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14149 /* Consume the `:' or `::'. */
14150 cp_lexer_consume_token (parser->lexer);
14151 /* Parse the output-operands. */
14152 if (cp_lexer_next_token_is_not (parser->lexer,
14153 CPP_COLON)
14154 && cp_lexer_next_token_is_not (parser->lexer,
14155 CPP_SCOPE)
14156 && cp_lexer_next_token_is_not (parser->lexer,
14157 CPP_CLOSE_PAREN))
14158 inputs = cp_parser_asm_operand_list (parser);
14160 if (inputs == error_mark_node)
14161 invalid_inputs_p = true;
14163 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14164 /* The clobbers are coming next. */
14165 clobbers_p = true;
14167 /* Look for clobbers. */
14168 if (clobbers_p
14169 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14171 clobbers_p = true;
14172 /* Consume the `:' or `::'. */
14173 cp_lexer_consume_token (parser->lexer);
14174 /* Parse the clobbers. */
14175 if (cp_lexer_next_token_is_not (parser->lexer,
14176 CPP_COLON)
14177 && cp_lexer_next_token_is_not (parser->lexer,
14178 CPP_CLOSE_PAREN))
14179 clobbers = cp_parser_asm_clobber_list (parser);
14181 else if (goto_p
14182 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14183 /* The labels are coming next. */
14184 labels_p = true;
14186 /* Look for labels. */
14187 if (labels_p
14188 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14190 labels_p = true;
14191 /* Consume the `:' or `::'. */
14192 cp_lexer_consume_token (parser->lexer);
14193 /* Parse the labels. */
14194 labels = cp_parser_asm_label_list (parser);
14197 if (goto_p && !labels_p)
14198 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14200 else if (goto_p)
14201 missing = RT_COLON_SCOPE;
14203 /* Look for the closing `)'. */
14204 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14205 missing ? missing : RT_CLOSE_PAREN))
14206 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14207 /*consume_paren=*/true);
14208 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14210 if (!invalid_inputs_p && !invalid_outputs_p)
14212 /* Create the ASM_EXPR. */
14213 if (parser->in_function_body)
14215 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14216 inputs, clobbers, labels);
14217 /* If the extended syntax was not used, mark the ASM_EXPR. */
14218 if (!extended_p)
14220 tree temp = asm_stmt;
14221 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14222 temp = TREE_OPERAND (temp, 0);
14224 ASM_INPUT_P (temp) = 1;
14227 else
14228 cgraph_add_asm_node (string);
14232 /* Declarators [gram.dcl.decl] */
14234 /* Parse an init-declarator.
14236 init-declarator:
14237 declarator initializer [opt]
14239 GNU Extension:
14241 init-declarator:
14242 declarator asm-specification [opt] attributes [opt] initializer [opt]
14244 function-definition:
14245 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14246 function-body
14247 decl-specifier-seq [opt] declarator function-try-block
14249 GNU Extension:
14251 function-definition:
14252 __extension__ function-definition
14254 The DECL_SPECIFIERS apply to this declarator. Returns a
14255 representation of the entity declared. If MEMBER_P is TRUE, then
14256 this declarator appears in a class scope. The new DECL created by
14257 this declarator is returned.
14259 The CHECKS are access checks that should be performed once we know
14260 what entity is being declared (and, therefore, what classes have
14261 befriended it).
14263 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14264 for a function-definition here as well. If the declarator is a
14265 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14266 be TRUE upon return. By that point, the function-definition will
14267 have been completely parsed.
14269 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14270 is FALSE. */
14272 static tree
14273 cp_parser_init_declarator (cp_parser* parser,
14274 cp_decl_specifier_seq *decl_specifiers,
14275 VEC (deferred_access_check,gc)* checks,
14276 bool function_definition_allowed_p,
14277 bool member_p,
14278 int declares_class_or_enum,
14279 bool* function_definition_p)
14281 cp_token *token = NULL, *asm_spec_start_token = NULL,
14282 *attributes_start_token = NULL;
14283 cp_declarator *declarator;
14284 tree prefix_attributes;
14285 tree attributes;
14286 tree asm_specification;
14287 tree initializer;
14288 tree decl = NULL_TREE;
14289 tree scope;
14290 int is_initialized;
14291 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14292 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14293 "(...)". */
14294 enum cpp_ttype initialization_kind;
14295 bool is_direct_init = false;
14296 bool is_non_constant_init;
14297 int ctor_dtor_or_conv_p;
14298 bool friend_p;
14299 tree pushed_scope = NULL;
14301 /* Gather the attributes that were provided with the
14302 decl-specifiers. */
14303 prefix_attributes = decl_specifiers->attributes;
14305 /* Assume that this is not the declarator for a function
14306 definition. */
14307 if (function_definition_p)
14308 *function_definition_p = false;
14310 /* Defer access checks while parsing the declarator; we cannot know
14311 what names are accessible until we know what is being
14312 declared. */
14313 resume_deferring_access_checks ();
14315 /* Parse the declarator. */
14316 token = cp_lexer_peek_token (parser->lexer);
14317 declarator
14318 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14319 &ctor_dtor_or_conv_p,
14320 /*parenthesized_p=*/NULL,
14321 /*member_p=*/false);
14322 /* Gather up the deferred checks. */
14323 stop_deferring_access_checks ();
14325 /* If the DECLARATOR was erroneous, there's no need to go
14326 further. */
14327 if (declarator == cp_error_declarator)
14328 return error_mark_node;
14330 /* Check that the number of template-parameter-lists is OK. */
14331 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14332 token->location))
14333 return error_mark_node;
14335 if (declares_class_or_enum & 2)
14336 cp_parser_check_for_definition_in_return_type (declarator,
14337 decl_specifiers->type,
14338 decl_specifiers->type_location);
14340 /* Figure out what scope the entity declared by the DECLARATOR is
14341 located in. `grokdeclarator' sometimes changes the scope, so
14342 we compute it now. */
14343 scope = get_scope_of_declarator (declarator);
14345 /* Perform any lookups in the declared type which were thought to be
14346 dependent, but are not in the scope of the declarator. */
14347 decl_specifiers->type
14348 = maybe_update_decl_type (decl_specifiers->type, scope);
14350 /* If we're allowing GNU extensions, look for an asm-specification
14351 and attributes. */
14352 if (cp_parser_allow_gnu_extensions_p (parser))
14354 /* Look for an asm-specification. */
14355 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14356 asm_specification = cp_parser_asm_specification_opt (parser);
14357 /* And attributes. */
14358 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14359 attributes = cp_parser_attributes_opt (parser);
14361 else
14363 asm_specification = NULL_TREE;
14364 attributes = NULL_TREE;
14367 /* Peek at the next token. */
14368 token = cp_lexer_peek_token (parser->lexer);
14369 /* Check to see if the token indicates the start of a
14370 function-definition. */
14371 if (function_declarator_p (declarator)
14372 && cp_parser_token_starts_function_definition_p (token))
14374 if (!function_definition_allowed_p)
14376 /* If a function-definition should not appear here, issue an
14377 error message. */
14378 cp_parser_error (parser,
14379 "a function-definition is not allowed here");
14380 return error_mark_node;
14382 else
14384 location_t func_brace_location
14385 = cp_lexer_peek_token (parser->lexer)->location;
14387 /* Neither attributes nor an asm-specification are allowed
14388 on a function-definition. */
14389 if (asm_specification)
14390 error_at (asm_spec_start_token->location,
14391 "an asm-specification is not allowed "
14392 "on a function-definition");
14393 if (attributes)
14394 error_at (attributes_start_token->location,
14395 "attributes are not allowed on a function-definition");
14396 /* This is a function-definition. */
14397 *function_definition_p = true;
14399 /* Parse the function definition. */
14400 if (member_p)
14401 decl = cp_parser_save_member_function_body (parser,
14402 decl_specifiers,
14403 declarator,
14404 prefix_attributes);
14405 else
14406 decl
14407 = (cp_parser_function_definition_from_specifiers_and_declarator
14408 (parser, decl_specifiers, prefix_attributes, declarator));
14410 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14412 /* This is where the prologue starts... */
14413 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14414 = func_brace_location;
14417 return decl;
14421 /* [dcl.dcl]
14423 Only in function declarations for constructors, destructors, and
14424 type conversions can the decl-specifier-seq be omitted.
14426 We explicitly postpone this check past the point where we handle
14427 function-definitions because we tolerate function-definitions
14428 that are missing their return types in some modes. */
14429 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14431 cp_parser_error (parser,
14432 "expected constructor, destructor, or type conversion");
14433 return error_mark_node;
14436 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14437 if (token->type == CPP_EQ
14438 || token->type == CPP_OPEN_PAREN
14439 || token->type == CPP_OPEN_BRACE)
14441 is_initialized = SD_INITIALIZED;
14442 initialization_kind = token->type;
14444 if (token->type == CPP_EQ
14445 && function_declarator_p (declarator))
14447 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14448 if (t2->keyword == RID_DEFAULT)
14449 is_initialized = SD_DEFAULTED;
14450 else if (t2->keyword == RID_DELETE)
14451 is_initialized = SD_DELETED;
14454 else
14456 /* If the init-declarator isn't initialized and isn't followed by a
14457 `,' or `;', it's not a valid init-declarator. */
14458 if (token->type != CPP_COMMA
14459 && token->type != CPP_SEMICOLON)
14461 cp_parser_error (parser, "expected initializer");
14462 return error_mark_node;
14464 is_initialized = SD_UNINITIALIZED;
14465 initialization_kind = CPP_EOF;
14468 /* Because start_decl has side-effects, we should only call it if we
14469 know we're going ahead. By this point, we know that we cannot
14470 possibly be looking at any other construct. */
14471 cp_parser_commit_to_tentative_parse (parser);
14473 /* If the decl specifiers were bad, issue an error now that we're
14474 sure this was intended to be a declarator. Then continue
14475 declaring the variable(s), as int, to try to cut down on further
14476 errors. */
14477 if (decl_specifiers->any_specifiers_p
14478 && decl_specifiers->type == error_mark_node)
14480 cp_parser_error (parser, "invalid type in declaration");
14481 decl_specifiers->type = integer_type_node;
14484 /* Check to see whether or not this declaration is a friend. */
14485 friend_p = cp_parser_friend_p (decl_specifiers);
14487 /* Enter the newly declared entry in the symbol table. If we're
14488 processing a declaration in a class-specifier, we wait until
14489 after processing the initializer. */
14490 if (!member_p)
14492 if (parser->in_unbraced_linkage_specification_p)
14493 decl_specifiers->storage_class = sc_extern;
14494 decl = start_decl (declarator, decl_specifiers,
14495 is_initialized, attributes, prefix_attributes,
14496 &pushed_scope);
14497 /* Adjust location of decl if declarator->id_loc is more appropriate:
14498 set, and decl wasn't merged with another decl, in which case its
14499 location would be different from input_location, and more accurate. */
14500 if (DECL_P (decl)
14501 && declarator->id_loc != UNKNOWN_LOCATION
14502 && DECL_SOURCE_LOCATION (decl) == input_location)
14503 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14505 else if (scope)
14506 /* Enter the SCOPE. That way unqualified names appearing in the
14507 initializer will be looked up in SCOPE. */
14508 pushed_scope = push_scope (scope);
14510 /* Perform deferred access control checks, now that we know in which
14511 SCOPE the declared entity resides. */
14512 if (!member_p && decl)
14514 tree saved_current_function_decl = NULL_TREE;
14516 /* If the entity being declared is a function, pretend that we
14517 are in its scope. If it is a `friend', it may have access to
14518 things that would not otherwise be accessible. */
14519 if (TREE_CODE (decl) == FUNCTION_DECL)
14521 saved_current_function_decl = current_function_decl;
14522 current_function_decl = decl;
14525 /* Perform access checks for template parameters. */
14526 cp_parser_perform_template_parameter_access_checks (checks);
14528 /* Perform the access control checks for the declarator and the
14529 decl-specifiers. */
14530 perform_deferred_access_checks ();
14532 /* Restore the saved value. */
14533 if (TREE_CODE (decl) == FUNCTION_DECL)
14534 current_function_decl = saved_current_function_decl;
14537 /* Parse the initializer. */
14538 initializer = NULL_TREE;
14539 is_direct_init = false;
14540 is_non_constant_init = true;
14541 if (is_initialized)
14543 if (function_declarator_p (declarator))
14545 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14546 if (initialization_kind == CPP_EQ)
14547 initializer = cp_parser_pure_specifier (parser);
14548 else
14550 /* If the declaration was erroneous, we don't really
14551 know what the user intended, so just silently
14552 consume the initializer. */
14553 if (decl != error_mark_node)
14554 error_at (initializer_start_token->location,
14555 "initializer provided for function");
14556 cp_parser_skip_to_closing_parenthesis (parser,
14557 /*recovering=*/true,
14558 /*or_comma=*/false,
14559 /*consume_paren=*/true);
14562 else
14564 /* We want to record the extra mangling scope for in-class
14565 initializers of class members and initializers of static data
14566 member templates. The former is a C++0x feature which isn't
14567 implemented yet, and I expect it will involve deferring
14568 parsing of the initializer until end of class as with default
14569 arguments. So right here we only handle the latter. */
14570 if (!member_p && processing_template_decl)
14571 start_lambda_scope (decl);
14572 initializer = cp_parser_initializer (parser,
14573 &is_direct_init,
14574 &is_non_constant_init);
14575 if (!member_p && processing_template_decl)
14576 finish_lambda_scope ();
14580 /* The old parser allows attributes to appear after a parenthesized
14581 initializer. Mark Mitchell proposed removing this functionality
14582 on the GCC mailing lists on 2002-08-13. This parser accepts the
14583 attributes -- but ignores them. */
14584 if (cp_parser_allow_gnu_extensions_p (parser)
14585 && initialization_kind == CPP_OPEN_PAREN)
14586 if (cp_parser_attributes_opt (parser))
14587 warning (OPT_Wattributes,
14588 "attributes after parenthesized initializer ignored");
14590 /* For an in-class declaration, use `grokfield' to create the
14591 declaration. */
14592 if (member_p)
14594 if (pushed_scope)
14596 pop_scope (pushed_scope);
14597 pushed_scope = false;
14599 decl = grokfield (declarator, decl_specifiers,
14600 initializer, !is_non_constant_init,
14601 /*asmspec=*/NULL_TREE,
14602 prefix_attributes);
14603 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14604 cp_parser_save_default_args (parser, decl);
14607 /* Finish processing the declaration. But, skip friend
14608 declarations. */
14609 if (!friend_p && decl && decl != error_mark_node)
14611 cp_finish_decl (decl,
14612 initializer, !is_non_constant_init,
14613 asm_specification,
14614 /* If the initializer is in parentheses, then this is
14615 a direct-initialization, which means that an
14616 `explicit' constructor is OK. Otherwise, an
14617 `explicit' constructor cannot be used. */
14618 ((is_direct_init || !is_initialized)
14619 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14621 else if ((cxx_dialect != cxx98) && friend_p
14622 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14623 /* Core issue #226 (C++0x only): A default template-argument
14624 shall not be specified in a friend class template
14625 declaration. */
14626 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14627 /*is_partial=*/0, /*is_friend_decl=*/1);
14629 if (!friend_p && pushed_scope)
14630 pop_scope (pushed_scope);
14632 return decl;
14635 /* Parse a declarator.
14637 declarator:
14638 direct-declarator
14639 ptr-operator declarator
14641 abstract-declarator:
14642 ptr-operator abstract-declarator [opt]
14643 direct-abstract-declarator
14645 GNU Extensions:
14647 declarator:
14648 attributes [opt] direct-declarator
14649 attributes [opt] ptr-operator declarator
14651 abstract-declarator:
14652 attributes [opt] ptr-operator abstract-declarator [opt]
14653 attributes [opt] direct-abstract-declarator
14655 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14656 detect constructor, destructor or conversion operators. It is set
14657 to -1 if the declarator is a name, and +1 if it is a
14658 function. Otherwise it is set to zero. Usually you just want to
14659 test for >0, but internally the negative value is used.
14661 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14662 a decl-specifier-seq unless it declares a constructor, destructor,
14663 or conversion. It might seem that we could check this condition in
14664 semantic analysis, rather than parsing, but that makes it difficult
14665 to handle something like `f()'. We want to notice that there are
14666 no decl-specifiers, and therefore realize that this is an
14667 expression, not a declaration.)
14669 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14670 the declarator is a direct-declarator of the form "(...)".
14672 MEMBER_P is true iff this declarator is a member-declarator. */
14674 static cp_declarator *
14675 cp_parser_declarator (cp_parser* parser,
14676 cp_parser_declarator_kind dcl_kind,
14677 int* ctor_dtor_or_conv_p,
14678 bool* parenthesized_p,
14679 bool member_p)
14681 cp_declarator *declarator;
14682 enum tree_code code;
14683 cp_cv_quals cv_quals;
14684 tree class_type;
14685 tree attributes = NULL_TREE;
14687 /* Assume this is not a constructor, destructor, or type-conversion
14688 operator. */
14689 if (ctor_dtor_or_conv_p)
14690 *ctor_dtor_or_conv_p = 0;
14692 if (cp_parser_allow_gnu_extensions_p (parser))
14693 attributes = cp_parser_attributes_opt (parser);
14695 /* Check for the ptr-operator production. */
14696 cp_parser_parse_tentatively (parser);
14697 /* Parse the ptr-operator. */
14698 code = cp_parser_ptr_operator (parser,
14699 &class_type,
14700 &cv_quals);
14701 /* If that worked, then we have a ptr-operator. */
14702 if (cp_parser_parse_definitely (parser))
14704 /* If a ptr-operator was found, then this declarator was not
14705 parenthesized. */
14706 if (parenthesized_p)
14707 *parenthesized_p = true;
14708 /* The dependent declarator is optional if we are parsing an
14709 abstract-declarator. */
14710 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14711 cp_parser_parse_tentatively (parser);
14713 /* Parse the dependent declarator. */
14714 declarator = cp_parser_declarator (parser, dcl_kind,
14715 /*ctor_dtor_or_conv_p=*/NULL,
14716 /*parenthesized_p=*/NULL,
14717 /*member_p=*/false);
14719 /* If we are parsing an abstract-declarator, we must handle the
14720 case where the dependent declarator is absent. */
14721 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14722 && !cp_parser_parse_definitely (parser))
14723 declarator = NULL;
14725 declarator = cp_parser_make_indirect_declarator
14726 (code, class_type, cv_quals, declarator);
14728 /* Everything else is a direct-declarator. */
14729 else
14731 if (parenthesized_p)
14732 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14733 CPP_OPEN_PAREN);
14734 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14735 ctor_dtor_or_conv_p,
14736 member_p);
14739 if (attributes && declarator && declarator != cp_error_declarator)
14740 declarator->attributes = attributes;
14742 return declarator;
14745 /* Parse a direct-declarator or direct-abstract-declarator.
14747 direct-declarator:
14748 declarator-id
14749 direct-declarator ( parameter-declaration-clause )
14750 cv-qualifier-seq [opt]
14751 exception-specification [opt]
14752 direct-declarator [ constant-expression [opt] ]
14753 ( declarator )
14755 direct-abstract-declarator:
14756 direct-abstract-declarator [opt]
14757 ( parameter-declaration-clause )
14758 cv-qualifier-seq [opt]
14759 exception-specification [opt]
14760 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14761 ( abstract-declarator )
14763 Returns a representation of the declarator. DCL_KIND is
14764 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14765 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14766 we are parsing a direct-declarator. It is
14767 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14768 of ambiguity we prefer an abstract declarator, as per
14769 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14770 cp_parser_declarator. */
14772 static cp_declarator *
14773 cp_parser_direct_declarator (cp_parser* parser,
14774 cp_parser_declarator_kind dcl_kind,
14775 int* ctor_dtor_or_conv_p,
14776 bool member_p)
14778 cp_token *token;
14779 cp_declarator *declarator = NULL;
14780 tree scope = NULL_TREE;
14781 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14782 bool saved_in_declarator_p = parser->in_declarator_p;
14783 bool first = true;
14784 tree pushed_scope = NULL_TREE;
14786 while (true)
14788 /* Peek at the next token. */
14789 token = cp_lexer_peek_token (parser->lexer);
14790 if (token->type == CPP_OPEN_PAREN)
14792 /* This is either a parameter-declaration-clause, or a
14793 parenthesized declarator. When we know we are parsing a
14794 named declarator, it must be a parenthesized declarator
14795 if FIRST is true. For instance, `(int)' is a
14796 parameter-declaration-clause, with an omitted
14797 direct-abstract-declarator. But `((*))', is a
14798 parenthesized abstract declarator. Finally, when T is a
14799 template parameter `(T)' is a
14800 parameter-declaration-clause, and not a parenthesized
14801 named declarator.
14803 We first try and parse a parameter-declaration-clause,
14804 and then try a nested declarator (if FIRST is true).
14806 It is not an error for it not to be a
14807 parameter-declaration-clause, even when FIRST is
14808 false. Consider,
14810 int i (int);
14811 int i (3);
14813 The first is the declaration of a function while the
14814 second is the definition of a variable, including its
14815 initializer.
14817 Having seen only the parenthesis, we cannot know which of
14818 these two alternatives should be selected. Even more
14819 complex are examples like:
14821 int i (int (a));
14822 int i (int (3));
14824 The former is a function-declaration; the latter is a
14825 variable initialization.
14827 Thus again, we try a parameter-declaration-clause, and if
14828 that fails, we back out and return. */
14830 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14832 tree params;
14833 unsigned saved_num_template_parameter_lists;
14834 bool is_declarator = false;
14835 tree t;
14837 /* In a member-declarator, the only valid interpretation
14838 of a parenthesis is the start of a
14839 parameter-declaration-clause. (It is invalid to
14840 initialize a static data member with a parenthesized
14841 initializer; only the "=" form of initialization is
14842 permitted.) */
14843 if (!member_p)
14844 cp_parser_parse_tentatively (parser);
14846 /* Consume the `('. */
14847 cp_lexer_consume_token (parser->lexer);
14848 if (first)
14850 /* If this is going to be an abstract declarator, we're
14851 in a declarator and we can't have default args. */
14852 parser->default_arg_ok_p = false;
14853 parser->in_declarator_p = true;
14856 /* Inside the function parameter list, surrounding
14857 template-parameter-lists do not apply. */
14858 saved_num_template_parameter_lists
14859 = parser->num_template_parameter_lists;
14860 parser->num_template_parameter_lists = 0;
14862 begin_scope (sk_function_parms, NULL_TREE);
14864 /* Parse the parameter-declaration-clause. */
14865 params = cp_parser_parameter_declaration_clause (parser);
14867 parser->num_template_parameter_lists
14868 = saved_num_template_parameter_lists;
14870 /* If all went well, parse the cv-qualifier-seq and the
14871 exception-specification. */
14872 if (member_p || cp_parser_parse_definitely (parser))
14874 cp_cv_quals cv_quals;
14875 tree exception_specification;
14876 tree late_return;
14878 is_declarator = true;
14880 if (ctor_dtor_or_conv_p)
14881 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14882 first = false;
14883 /* Consume the `)'. */
14884 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14886 /* Parse the cv-qualifier-seq. */
14887 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14888 /* And the exception-specification. */
14889 exception_specification
14890 = cp_parser_exception_specification_opt (parser);
14892 late_return
14893 = cp_parser_late_return_type_opt (parser);
14895 /* Create the function-declarator. */
14896 declarator = make_call_declarator (declarator,
14897 params,
14898 cv_quals,
14899 exception_specification,
14900 late_return);
14901 /* Any subsequent parameter lists are to do with
14902 return type, so are not those of the declared
14903 function. */
14904 parser->default_arg_ok_p = false;
14907 /* Remove the function parms from scope. */
14908 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14909 pop_binding (DECL_NAME (t), t);
14910 leave_scope();
14912 if (is_declarator)
14913 /* Repeat the main loop. */
14914 continue;
14917 /* If this is the first, we can try a parenthesized
14918 declarator. */
14919 if (first)
14921 bool saved_in_type_id_in_expr_p;
14923 parser->default_arg_ok_p = saved_default_arg_ok_p;
14924 parser->in_declarator_p = saved_in_declarator_p;
14926 /* Consume the `('. */
14927 cp_lexer_consume_token (parser->lexer);
14928 /* Parse the nested declarator. */
14929 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14930 parser->in_type_id_in_expr_p = true;
14931 declarator
14932 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14933 /*parenthesized_p=*/NULL,
14934 member_p);
14935 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14936 first = false;
14937 /* Expect a `)'. */
14938 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14939 declarator = cp_error_declarator;
14940 if (declarator == cp_error_declarator)
14941 break;
14943 goto handle_declarator;
14945 /* Otherwise, we must be done. */
14946 else
14947 break;
14949 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14950 && token->type == CPP_OPEN_SQUARE)
14952 /* Parse an array-declarator. */
14953 tree bounds;
14955 if (ctor_dtor_or_conv_p)
14956 *ctor_dtor_or_conv_p = 0;
14958 first = false;
14959 parser->default_arg_ok_p = false;
14960 parser->in_declarator_p = true;
14961 /* Consume the `['. */
14962 cp_lexer_consume_token (parser->lexer);
14963 /* Peek at the next token. */
14964 token = cp_lexer_peek_token (parser->lexer);
14965 /* If the next token is `]', then there is no
14966 constant-expression. */
14967 if (token->type != CPP_CLOSE_SQUARE)
14969 bool non_constant_p;
14971 bounds
14972 = cp_parser_constant_expression (parser,
14973 /*allow_non_constant=*/true,
14974 &non_constant_p);
14975 if (!non_constant_p)
14976 bounds = fold_non_dependent_expr (bounds);
14977 /* Normally, the array bound must be an integral constant
14978 expression. However, as an extension, we allow VLAs
14979 in function scopes as long as they aren't part of a
14980 parameter declaration. */
14981 else if (!parser->in_function_body
14982 || current_binding_level->kind == sk_function_parms)
14984 cp_parser_error (parser,
14985 "array bound is not an integer constant");
14986 bounds = error_mark_node;
14988 else if (processing_template_decl && !error_operand_p (bounds))
14990 /* Remember this wasn't a constant-expression. */
14991 bounds = build_nop (TREE_TYPE (bounds), bounds);
14992 TREE_SIDE_EFFECTS (bounds) = 1;
14995 else
14996 bounds = NULL_TREE;
14997 /* Look for the closing `]'. */
14998 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15000 declarator = cp_error_declarator;
15001 break;
15004 declarator = make_array_declarator (declarator, bounds);
15006 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15009 tree qualifying_scope;
15010 tree unqualified_name;
15011 special_function_kind sfk;
15012 bool abstract_ok;
15013 bool pack_expansion_p = false;
15014 cp_token *declarator_id_start_token;
15016 /* Parse a declarator-id */
15017 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15018 if (abstract_ok)
15020 cp_parser_parse_tentatively (parser);
15022 /* If we see an ellipsis, we should be looking at a
15023 parameter pack. */
15024 if (token->type == CPP_ELLIPSIS)
15026 /* Consume the `...' */
15027 cp_lexer_consume_token (parser->lexer);
15029 pack_expansion_p = true;
15033 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15034 unqualified_name
15035 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15036 qualifying_scope = parser->scope;
15037 if (abstract_ok)
15039 bool okay = false;
15041 if (!unqualified_name && pack_expansion_p)
15043 /* Check whether an error occurred. */
15044 okay = !cp_parser_error_occurred (parser);
15046 /* We already consumed the ellipsis to mark a
15047 parameter pack, but we have no way to report it,
15048 so abort the tentative parse. We will be exiting
15049 immediately anyway. */
15050 cp_parser_abort_tentative_parse (parser);
15052 else
15053 okay = cp_parser_parse_definitely (parser);
15055 if (!okay)
15056 unqualified_name = error_mark_node;
15057 else if (unqualified_name
15058 && (qualifying_scope
15059 || (TREE_CODE (unqualified_name)
15060 != IDENTIFIER_NODE)))
15062 cp_parser_error (parser, "expected unqualified-id");
15063 unqualified_name = error_mark_node;
15067 if (!unqualified_name)
15068 return NULL;
15069 if (unqualified_name == error_mark_node)
15071 declarator = cp_error_declarator;
15072 pack_expansion_p = false;
15073 declarator->parameter_pack_p = false;
15074 break;
15077 if (qualifying_scope && at_namespace_scope_p ()
15078 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15080 /* In the declaration of a member of a template class
15081 outside of the class itself, the SCOPE will sometimes
15082 be a TYPENAME_TYPE. For example, given:
15084 template <typename T>
15085 int S<T>::R::i = 3;
15087 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15088 this context, we must resolve S<T>::R to an ordinary
15089 type, rather than a typename type.
15091 The reason we normally avoid resolving TYPENAME_TYPEs
15092 is that a specialization of `S' might render
15093 `S<T>::R' not a type. However, if `S' is
15094 specialized, then this `i' will not be used, so there
15095 is no harm in resolving the types here. */
15096 tree type;
15098 /* Resolve the TYPENAME_TYPE. */
15099 type = resolve_typename_type (qualifying_scope,
15100 /*only_current_p=*/false);
15101 /* If that failed, the declarator is invalid. */
15102 if (TREE_CODE (type) == TYPENAME_TYPE)
15104 if (typedef_variant_p (type))
15105 error_at (declarator_id_start_token->location,
15106 "cannot define member of dependent typedef "
15107 "%qT", type);
15108 else
15109 error_at (declarator_id_start_token->location,
15110 "%<%T::%E%> is not a type",
15111 TYPE_CONTEXT (qualifying_scope),
15112 TYPE_IDENTIFIER (qualifying_scope));
15114 qualifying_scope = type;
15117 sfk = sfk_none;
15119 if (unqualified_name)
15121 tree class_type;
15123 if (qualifying_scope
15124 && CLASS_TYPE_P (qualifying_scope))
15125 class_type = qualifying_scope;
15126 else
15127 class_type = current_class_type;
15129 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15131 tree name_type = TREE_TYPE (unqualified_name);
15132 if (class_type && same_type_p (name_type, class_type))
15134 if (qualifying_scope
15135 && CLASSTYPE_USE_TEMPLATE (name_type))
15137 error_at (declarator_id_start_token->location,
15138 "invalid use of constructor as a template");
15139 inform (declarator_id_start_token->location,
15140 "use %<%T::%D%> instead of %<%T::%D%> to "
15141 "name the constructor in a qualified name",
15142 class_type,
15143 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15144 class_type, name_type);
15145 declarator = cp_error_declarator;
15146 break;
15148 else
15149 unqualified_name = constructor_name (class_type);
15151 else
15153 /* We do not attempt to print the declarator
15154 here because we do not have enough
15155 information about its original syntactic
15156 form. */
15157 cp_parser_error (parser, "invalid declarator");
15158 declarator = cp_error_declarator;
15159 break;
15163 if (class_type)
15165 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15166 sfk = sfk_destructor;
15167 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15168 sfk = sfk_conversion;
15169 else if (/* There's no way to declare a constructor
15170 for an anonymous type, even if the type
15171 got a name for linkage purposes. */
15172 !TYPE_WAS_ANONYMOUS (class_type)
15173 && constructor_name_p (unqualified_name,
15174 class_type))
15176 unqualified_name = constructor_name (class_type);
15177 sfk = sfk_constructor;
15179 else if (is_overloaded_fn (unqualified_name)
15180 && DECL_CONSTRUCTOR_P (get_first_fn
15181 (unqualified_name)))
15182 sfk = sfk_constructor;
15184 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15185 *ctor_dtor_or_conv_p = -1;
15188 declarator = make_id_declarator (qualifying_scope,
15189 unqualified_name,
15190 sfk);
15191 declarator->id_loc = token->location;
15192 declarator->parameter_pack_p = pack_expansion_p;
15194 if (pack_expansion_p)
15195 maybe_warn_variadic_templates ();
15198 handle_declarator:;
15199 scope = get_scope_of_declarator (declarator);
15200 if (scope)
15201 /* Any names that appear after the declarator-id for a
15202 member are looked up in the containing scope. */
15203 pushed_scope = push_scope (scope);
15204 parser->in_declarator_p = true;
15205 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15206 || (declarator && declarator->kind == cdk_id))
15207 /* Default args are only allowed on function
15208 declarations. */
15209 parser->default_arg_ok_p = saved_default_arg_ok_p;
15210 else
15211 parser->default_arg_ok_p = false;
15213 first = false;
15215 /* We're done. */
15216 else
15217 break;
15220 /* For an abstract declarator, we might wind up with nothing at this
15221 point. That's an error; the declarator is not optional. */
15222 if (!declarator)
15223 cp_parser_error (parser, "expected declarator");
15225 /* If we entered a scope, we must exit it now. */
15226 if (pushed_scope)
15227 pop_scope (pushed_scope);
15229 parser->default_arg_ok_p = saved_default_arg_ok_p;
15230 parser->in_declarator_p = saved_in_declarator_p;
15232 return declarator;
15235 /* Parse a ptr-operator.
15237 ptr-operator:
15238 * cv-qualifier-seq [opt]
15240 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15242 GNU Extension:
15244 ptr-operator:
15245 & cv-qualifier-seq [opt]
15247 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15248 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15249 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15250 filled in with the TYPE containing the member. *CV_QUALS is
15251 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15252 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15253 Note that the tree codes returned by this function have nothing
15254 to do with the types of trees that will be eventually be created
15255 to represent the pointer or reference type being parsed. They are
15256 just constants with suggestive names. */
15257 static enum tree_code
15258 cp_parser_ptr_operator (cp_parser* parser,
15259 tree* type,
15260 cp_cv_quals *cv_quals)
15262 enum tree_code code = ERROR_MARK;
15263 cp_token *token;
15265 /* Assume that it's not a pointer-to-member. */
15266 *type = NULL_TREE;
15267 /* And that there are no cv-qualifiers. */
15268 *cv_quals = TYPE_UNQUALIFIED;
15270 /* Peek at the next token. */
15271 token = cp_lexer_peek_token (parser->lexer);
15273 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15274 if (token->type == CPP_MULT)
15275 code = INDIRECT_REF;
15276 else if (token->type == CPP_AND)
15277 code = ADDR_EXPR;
15278 else if ((cxx_dialect != cxx98) &&
15279 token->type == CPP_AND_AND) /* C++0x only */
15280 code = NON_LVALUE_EXPR;
15282 if (code != ERROR_MARK)
15284 /* Consume the `*', `&' or `&&'. */
15285 cp_lexer_consume_token (parser->lexer);
15287 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15288 `&', if we are allowing GNU extensions. (The only qualifier
15289 that can legally appear after `&' is `restrict', but that is
15290 enforced during semantic analysis. */
15291 if (code == INDIRECT_REF
15292 || cp_parser_allow_gnu_extensions_p (parser))
15293 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15295 else
15297 /* Try the pointer-to-member case. */
15298 cp_parser_parse_tentatively (parser);
15299 /* Look for the optional `::' operator. */
15300 cp_parser_global_scope_opt (parser,
15301 /*current_scope_valid_p=*/false);
15302 /* Look for the nested-name specifier. */
15303 token = cp_lexer_peek_token (parser->lexer);
15304 cp_parser_nested_name_specifier (parser,
15305 /*typename_keyword_p=*/false,
15306 /*check_dependency_p=*/true,
15307 /*type_p=*/false,
15308 /*is_declaration=*/false);
15309 /* If we found it, and the next token is a `*', then we are
15310 indeed looking at a pointer-to-member operator. */
15311 if (!cp_parser_error_occurred (parser)
15312 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15314 /* Indicate that the `*' operator was used. */
15315 code = INDIRECT_REF;
15317 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15318 error_at (token->location, "%qD is a namespace", parser->scope);
15319 else
15321 /* The type of which the member is a member is given by the
15322 current SCOPE. */
15323 *type = parser->scope;
15324 /* The next name will not be qualified. */
15325 parser->scope = NULL_TREE;
15326 parser->qualifying_scope = NULL_TREE;
15327 parser->object_scope = NULL_TREE;
15328 /* Look for the optional cv-qualifier-seq. */
15329 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15332 /* If that didn't work we don't have a ptr-operator. */
15333 if (!cp_parser_parse_definitely (parser))
15334 cp_parser_error (parser, "expected ptr-operator");
15337 return code;
15340 /* Parse an (optional) cv-qualifier-seq.
15342 cv-qualifier-seq:
15343 cv-qualifier cv-qualifier-seq [opt]
15345 cv-qualifier:
15346 const
15347 volatile
15349 GNU Extension:
15351 cv-qualifier:
15352 __restrict__
15354 Returns a bitmask representing the cv-qualifiers. */
15356 static cp_cv_quals
15357 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15359 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15361 while (true)
15363 cp_token *token;
15364 cp_cv_quals cv_qualifier;
15366 /* Peek at the next token. */
15367 token = cp_lexer_peek_token (parser->lexer);
15368 /* See if it's a cv-qualifier. */
15369 switch (token->keyword)
15371 case RID_CONST:
15372 cv_qualifier = TYPE_QUAL_CONST;
15373 break;
15375 case RID_VOLATILE:
15376 cv_qualifier = TYPE_QUAL_VOLATILE;
15377 break;
15379 case RID_RESTRICT:
15380 cv_qualifier = TYPE_QUAL_RESTRICT;
15381 break;
15383 default:
15384 cv_qualifier = TYPE_UNQUALIFIED;
15385 break;
15388 if (!cv_qualifier)
15389 break;
15391 if (cv_quals & cv_qualifier)
15393 error_at (token->location, "duplicate cv-qualifier");
15394 cp_lexer_purge_token (parser->lexer);
15396 else
15398 cp_lexer_consume_token (parser->lexer);
15399 cv_quals |= cv_qualifier;
15403 return cv_quals;
15406 /* Parse a late-specified return type, if any. This is not a separate
15407 non-terminal, but part of a function declarator, which looks like
15409 -> trailing-type-specifier-seq abstract-declarator(opt)
15411 Returns the type indicated by the type-id. */
15413 static tree
15414 cp_parser_late_return_type_opt (cp_parser* parser)
15416 cp_token *token;
15418 /* Peek at the next token. */
15419 token = cp_lexer_peek_token (parser->lexer);
15420 /* A late-specified return type is indicated by an initial '->'. */
15421 if (token->type != CPP_DEREF)
15422 return NULL_TREE;
15424 /* Consume the ->. */
15425 cp_lexer_consume_token (parser->lexer);
15427 return cp_parser_trailing_type_id (parser);
15430 /* Parse a declarator-id.
15432 declarator-id:
15433 id-expression
15434 :: [opt] nested-name-specifier [opt] type-name
15436 In the `id-expression' case, the value returned is as for
15437 cp_parser_id_expression if the id-expression was an unqualified-id.
15438 If the id-expression was a qualified-id, then a SCOPE_REF is
15439 returned. The first operand is the scope (either a NAMESPACE_DECL
15440 or TREE_TYPE), but the second is still just a representation of an
15441 unqualified-id. */
15443 static tree
15444 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15446 tree id;
15447 /* The expression must be an id-expression. Assume that qualified
15448 names are the names of types so that:
15450 template <class T>
15451 int S<T>::R::i = 3;
15453 will work; we must treat `S<T>::R' as the name of a type.
15454 Similarly, assume that qualified names are templates, where
15455 required, so that:
15457 template <class T>
15458 int S<T>::R<T>::i = 3;
15460 will work, too. */
15461 id = cp_parser_id_expression (parser,
15462 /*template_keyword_p=*/false,
15463 /*check_dependency_p=*/false,
15464 /*template_p=*/NULL,
15465 /*declarator_p=*/true,
15466 optional_p);
15467 if (id && BASELINK_P (id))
15468 id = BASELINK_FUNCTIONS (id);
15469 return id;
15472 /* Parse a type-id.
15474 type-id:
15475 type-specifier-seq abstract-declarator [opt]
15477 Returns the TYPE specified. */
15479 static tree
15480 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15481 bool is_trailing_return)
15483 cp_decl_specifier_seq type_specifier_seq;
15484 cp_declarator *abstract_declarator;
15486 /* Parse the type-specifier-seq. */
15487 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15488 is_trailing_return,
15489 &type_specifier_seq);
15490 if (type_specifier_seq.type == error_mark_node)
15491 return error_mark_node;
15493 /* There might or might not be an abstract declarator. */
15494 cp_parser_parse_tentatively (parser);
15495 /* Look for the declarator. */
15496 abstract_declarator
15497 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15498 /*parenthesized_p=*/NULL,
15499 /*member_p=*/false);
15500 /* Check to see if there really was a declarator. */
15501 if (!cp_parser_parse_definitely (parser))
15502 abstract_declarator = NULL;
15504 if (type_specifier_seq.type
15505 && type_uses_auto (type_specifier_seq.type))
15507 /* A type-id with type 'auto' is only ok if the abstract declarator
15508 is a function declarator with a late-specified return type. */
15509 if (abstract_declarator
15510 && abstract_declarator->kind == cdk_function
15511 && abstract_declarator->u.function.late_return_type)
15512 /* OK */;
15513 else
15515 error ("invalid use of %<auto%>");
15516 return error_mark_node;
15520 return groktypename (&type_specifier_seq, abstract_declarator,
15521 is_template_arg);
15524 static tree cp_parser_type_id (cp_parser *parser)
15526 return cp_parser_type_id_1 (parser, false, false);
15529 static tree cp_parser_template_type_arg (cp_parser *parser)
15531 return cp_parser_type_id_1 (parser, true, false);
15534 static tree cp_parser_trailing_type_id (cp_parser *parser)
15536 return cp_parser_type_id_1 (parser, false, true);
15539 /* Parse a type-specifier-seq.
15541 type-specifier-seq:
15542 type-specifier type-specifier-seq [opt]
15544 GNU extension:
15546 type-specifier-seq:
15547 attributes type-specifier-seq [opt]
15549 If IS_DECLARATION is true, we are at the start of a "condition" or
15550 exception-declaration, so we might be followed by a declarator-id.
15552 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15553 i.e. we've just seen "->".
15555 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15557 static void
15558 cp_parser_type_specifier_seq (cp_parser* parser,
15559 bool is_declaration,
15560 bool is_trailing_return,
15561 cp_decl_specifier_seq *type_specifier_seq)
15563 bool seen_type_specifier = false;
15564 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15565 cp_token *start_token = NULL;
15567 /* Clear the TYPE_SPECIFIER_SEQ. */
15568 clear_decl_specs (type_specifier_seq);
15570 /* In the context of a trailing return type, enum E { } is an
15571 elaborated-type-specifier followed by a function-body, not an
15572 enum-specifier. */
15573 if (is_trailing_return)
15574 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15576 /* Parse the type-specifiers and attributes. */
15577 while (true)
15579 tree type_specifier;
15580 bool is_cv_qualifier;
15582 /* Check for attributes first. */
15583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15585 type_specifier_seq->attributes =
15586 chainon (type_specifier_seq->attributes,
15587 cp_parser_attributes_opt (parser));
15588 continue;
15591 /* record the token of the beginning of the type specifier seq,
15592 for error reporting purposes*/
15593 if (!start_token)
15594 start_token = cp_lexer_peek_token (parser->lexer);
15596 /* Look for the type-specifier. */
15597 type_specifier = cp_parser_type_specifier (parser,
15598 flags,
15599 type_specifier_seq,
15600 /*is_declaration=*/false,
15601 NULL,
15602 &is_cv_qualifier);
15603 if (!type_specifier)
15605 /* If the first type-specifier could not be found, this is not a
15606 type-specifier-seq at all. */
15607 if (!seen_type_specifier)
15609 cp_parser_error (parser, "expected type-specifier");
15610 type_specifier_seq->type = error_mark_node;
15611 return;
15613 /* If subsequent type-specifiers could not be found, the
15614 type-specifier-seq is complete. */
15615 break;
15618 seen_type_specifier = true;
15619 /* The standard says that a condition can be:
15621 type-specifier-seq declarator = assignment-expression
15623 However, given:
15625 struct S {};
15626 if (int S = ...)
15628 we should treat the "S" as a declarator, not as a
15629 type-specifier. The standard doesn't say that explicitly for
15630 type-specifier-seq, but it does say that for
15631 decl-specifier-seq in an ordinary declaration. Perhaps it
15632 would be clearer just to allow a decl-specifier-seq here, and
15633 then add a semantic restriction that if any decl-specifiers
15634 that are not type-specifiers appear, the program is invalid. */
15635 if (is_declaration && !is_cv_qualifier)
15636 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15639 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15642 /* Parse a parameter-declaration-clause.
15644 parameter-declaration-clause:
15645 parameter-declaration-list [opt] ... [opt]
15646 parameter-declaration-list , ...
15648 Returns a representation for the parameter declarations. A return
15649 value of NULL indicates a parameter-declaration-clause consisting
15650 only of an ellipsis. */
15652 static tree
15653 cp_parser_parameter_declaration_clause (cp_parser* parser)
15655 tree parameters;
15656 cp_token *token;
15657 bool ellipsis_p;
15658 bool is_error;
15660 /* Peek at the next token. */
15661 token = cp_lexer_peek_token (parser->lexer);
15662 /* Check for trivial parameter-declaration-clauses. */
15663 if (token->type == CPP_ELLIPSIS)
15665 /* Consume the `...' token. */
15666 cp_lexer_consume_token (parser->lexer);
15667 return NULL_TREE;
15669 else if (token->type == CPP_CLOSE_PAREN)
15670 /* There are no parameters. */
15672 #ifndef NO_IMPLICIT_EXTERN_C
15673 if (in_system_header && current_class_type == NULL
15674 && current_lang_name == lang_name_c)
15675 return NULL_TREE;
15676 else
15677 #endif
15678 return void_list_node;
15680 /* Check for `(void)', too, which is a special case. */
15681 else if (token->keyword == RID_VOID
15682 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15683 == CPP_CLOSE_PAREN))
15685 /* Consume the `void' token. */
15686 cp_lexer_consume_token (parser->lexer);
15687 /* There are no parameters. */
15688 return void_list_node;
15691 /* Parse the parameter-declaration-list. */
15692 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15693 /* If a parse error occurred while parsing the
15694 parameter-declaration-list, then the entire
15695 parameter-declaration-clause is erroneous. */
15696 if (is_error)
15697 return NULL;
15699 /* Peek at the next token. */
15700 token = cp_lexer_peek_token (parser->lexer);
15701 /* If it's a `,', the clause should terminate with an ellipsis. */
15702 if (token->type == CPP_COMMA)
15704 /* Consume the `,'. */
15705 cp_lexer_consume_token (parser->lexer);
15706 /* Expect an ellipsis. */
15707 ellipsis_p
15708 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15710 /* It might also be `...' if the optional trailing `,' was
15711 omitted. */
15712 else if (token->type == CPP_ELLIPSIS)
15714 /* Consume the `...' token. */
15715 cp_lexer_consume_token (parser->lexer);
15716 /* And remember that we saw it. */
15717 ellipsis_p = true;
15719 else
15720 ellipsis_p = false;
15722 /* Finish the parameter list. */
15723 if (!ellipsis_p)
15724 parameters = chainon (parameters, void_list_node);
15726 return parameters;
15729 /* Parse a parameter-declaration-list.
15731 parameter-declaration-list:
15732 parameter-declaration
15733 parameter-declaration-list , parameter-declaration
15735 Returns a representation of the parameter-declaration-list, as for
15736 cp_parser_parameter_declaration_clause. However, the
15737 `void_list_node' is never appended to the list. Upon return,
15738 *IS_ERROR will be true iff an error occurred. */
15740 static tree
15741 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15743 tree parameters = NULL_TREE;
15744 tree *tail = &parameters;
15745 bool saved_in_unbraced_linkage_specification_p;
15746 int index = 0;
15748 /* Assume all will go well. */
15749 *is_error = false;
15750 /* The special considerations that apply to a function within an
15751 unbraced linkage specifications do not apply to the parameters
15752 to the function. */
15753 saved_in_unbraced_linkage_specification_p
15754 = parser->in_unbraced_linkage_specification_p;
15755 parser->in_unbraced_linkage_specification_p = false;
15757 /* Look for more parameters. */
15758 while (true)
15760 cp_parameter_declarator *parameter;
15761 tree decl = error_mark_node;
15762 bool parenthesized_p;
15763 /* Parse the parameter. */
15764 parameter
15765 = cp_parser_parameter_declaration (parser,
15766 /*template_parm_p=*/false,
15767 &parenthesized_p);
15769 /* We don't know yet if the enclosing context is deprecated, so wait
15770 and warn in grokparms if appropriate. */
15771 deprecated_state = DEPRECATED_SUPPRESS;
15773 if (parameter)
15774 decl = grokdeclarator (parameter->declarator,
15775 &parameter->decl_specifiers,
15776 PARM,
15777 parameter->default_argument != NULL_TREE,
15778 &parameter->decl_specifiers.attributes);
15780 deprecated_state = DEPRECATED_NORMAL;
15782 /* If a parse error occurred parsing the parameter declaration,
15783 then the entire parameter-declaration-list is erroneous. */
15784 if (decl == error_mark_node)
15786 *is_error = true;
15787 parameters = error_mark_node;
15788 break;
15791 if (parameter->decl_specifiers.attributes)
15792 cplus_decl_attributes (&decl,
15793 parameter->decl_specifiers.attributes,
15795 if (DECL_NAME (decl))
15796 decl = pushdecl (decl);
15798 if (decl != error_mark_node)
15800 retrofit_lang_decl (decl);
15801 DECL_PARM_INDEX (decl) = ++index;
15804 /* Add the new parameter to the list. */
15805 *tail = build_tree_list (parameter->default_argument, decl);
15806 tail = &TREE_CHAIN (*tail);
15808 /* Peek at the next token. */
15809 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15810 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15811 /* These are for Objective-C++ */
15812 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15813 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15814 /* The parameter-declaration-list is complete. */
15815 break;
15816 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15818 cp_token *token;
15820 /* Peek at the next token. */
15821 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15822 /* If it's an ellipsis, then the list is complete. */
15823 if (token->type == CPP_ELLIPSIS)
15824 break;
15825 /* Otherwise, there must be more parameters. Consume the
15826 `,'. */
15827 cp_lexer_consume_token (parser->lexer);
15828 /* When parsing something like:
15830 int i(float f, double d)
15832 we can tell after seeing the declaration for "f" that we
15833 are not looking at an initialization of a variable "i",
15834 but rather at the declaration of a function "i".
15836 Due to the fact that the parsing of template arguments
15837 (as specified to a template-id) requires backtracking we
15838 cannot use this technique when inside a template argument
15839 list. */
15840 if (!parser->in_template_argument_list_p
15841 && !parser->in_type_id_in_expr_p
15842 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15843 /* However, a parameter-declaration of the form
15844 "foat(f)" (which is a valid declaration of a
15845 parameter "f") can also be interpreted as an
15846 expression (the conversion of "f" to "float"). */
15847 && !parenthesized_p)
15848 cp_parser_commit_to_tentative_parse (parser);
15850 else
15852 cp_parser_error (parser, "expected %<,%> or %<...%>");
15853 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15854 cp_parser_skip_to_closing_parenthesis (parser,
15855 /*recovering=*/true,
15856 /*or_comma=*/false,
15857 /*consume_paren=*/false);
15858 break;
15862 parser->in_unbraced_linkage_specification_p
15863 = saved_in_unbraced_linkage_specification_p;
15865 return parameters;
15868 /* Parse a parameter declaration.
15870 parameter-declaration:
15871 decl-specifier-seq ... [opt] declarator
15872 decl-specifier-seq declarator = assignment-expression
15873 decl-specifier-seq ... [opt] abstract-declarator [opt]
15874 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15876 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15877 declares a template parameter. (In that case, a non-nested `>'
15878 token encountered during the parsing of the assignment-expression
15879 is not interpreted as a greater-than operator.)
15881 Returns a representation of the parameter, or NULL if an error
15882 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15883 true iff the declarator is of the form "(p)". */
15885 static cp_parameter_declarator *
15886 cp_parser_parameter_declaration (cp_parser *parser,
15887 bool template_parm_p,
15888 bool *parenthesized_p)
15890 int declares_class_or_enum;
15891 cp_decl_specifier_seq decl_specifiers;
15892 cp_declarator *declarator;
15893 tree default_argument;
15894 cp_token *token = NULL, *declarator_token_start = NULL;
15895 const char *saved_message;
15897 /* In a template parameter, `>' is not an operator.
15899 [temp.param]
15901 When parsing a default template-argument for a non-type
15902 template-parameter, the first non-nested `>' is taken as the end
15903 of the template parameter-list rather than a greater-than
15904 operator. */
15906 /* Type definitions may not appear in parameter types. */
15907 saved_message = parser->type_definition_forbidden_message;
15908 parser->type_definition_forbidden_message
15909 = G_("types may not be defined in parameter types");
15911 /* Parse the declaration-specifiers. */
15912 cp_parser_decl_specifier_seq (parser,
15913 CP_PARSER_FLAGS_NONE,
15914 &decl_specifiers,
15915 &declares_class_or_enum);
15917 /* Complain about missing 'typename' or other invalid type names. */
15918 if (!decl_specifiers.any_type_specifiers_p)
15919 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15921 /* If an error occurred, there's no reason to attempt to parse the
15922 rest of the declaration. */
15923 if (cp_parser_error_occurred (parser))
15925 parser->type_definition_forbidden_message = saved_message;
15926 return NULL;
15929 /* Peek at the next token. */
15930 token = cp_lexer_peek_token (parser->lexer);
15932 /* If the next token is a `)', `,', `=', `>', or `...', then there
15933 is no declarator. However, when variadic templates are enabled,
15934 there may be a declarator following `...'. */
15935 if (token->type == CPP_CLOSE_PAREN
15936 || token->type == CPP_COMMA
15937 || token->type == CPP_EQ
15938 || token->type == CPP_GREATER)
15940 declarator = NULL;
15941 if (parenthesized_p)
15942 *parenthesized_p = false;
15944 /* Otherwise, there should be a declarator. */
15945 else
15947 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15948 parser->default_arg_ok_p = false;
15950 /* After seeing a decl-specifier-seq, if the next token is not a
15951 "(", there is no possibility that the code is a valid
15952 expression. Therefore, if parsing tentatively, we commit at
15953 this point. */
15954 if (!parser->in_template_argument_list_p
15955 /* In an expression context, having seen:
15957 (int((char ...
15959 we cannot be sure whether we are looking at a
15960 function-type (taking a "char" as a parameter) or a cast
15961 of some object of type "char" to "int". */
15962 && !parser->in_type_id_in_expr_p
15963 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15964 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15965 cp_parser_commit_to_tentative_parse (parser);
15966 /* Parse the declarator. */
15967 declarator_token_start = token;
15968 declarator = cp_parser_declarator (parser,
15969 CP_PARSER_DECLARATOR_EITHER,
15970 /*ctor_dtor_or_conv_p=*/NULL,
15971 parenthesized_p,
15972 /*member_p=*/false);
15973 parser->default_arg_ok_p = saved_default_arg_ok_p;
15974 /* After the declarator, allow more attributes. */
15975 decl_specifiers.attributes
15976 = chainon (decl_specifiers.attributes,
15977 cp_parser_attributes_opt (parser));
15980 /* If the next token is an ellipsis, and we have not seen a
15981 declarator name, and the type of the declarator contains parameter
15982 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15983 a parameter pack expansion expression. Otherwise, leave the
15984 ellipsis for a C-style variadic function. */
15985 token = cp_lexer_peek_token (parser->lexer);
15986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15988 tree type = decl_specifiers.type;
15990 if (type && DECL_P (type))
15991 type = TREE_TYPE (type);
15993 if (type
15994 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15995 && declarator_can_be_parameter_pack (declarator)
15996 && (!declarator || !declarator->parameter_pack_p)
15997 && uses_parameter_packs (type))
15999 /* Consume the `...'. */
16000 cp_lexer_consume_token (parser->lexer);
16001 maybe_warn_variadic_templates ();
16003 /* Build a pack expansion type */
16004 if (declarator)
16005 declarator->parameter_pack_p = true;
16006 else
16007 decl_specifiers.type = make_pack_expansion (type);
16011 /* The restriction on defining new types applies only to the type
16012 of the parameter, not to the default argument. */
16013 parser->type_definition_forbidden_message = saved_message;
16015 /* If the next token is `=', then process a default argument. */
16016 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16018 /* Consume the `='. */
16019 cp_lexer_consume_token (parser->lexer);
16021 /* If we are defining a class, then the tokens that make up the
16022 default argument must be saved and processed later. */
16023 if (!template_parm_p && at_class_scope_p ()
16024 && TYPE_BEING_DEFINED (current_class_type)
16025 && !LAMBDA_TYPE_P (current_class_type))
16027 unsigned depth = 0;
16028 int maybe_template_id = 0;
16029 cp_token *first_token;
16030 cp_token *token;
16032 /* Add tokens until we have processed the entire default
16033 argument. We add the range [first_token, token). */
16034 first_token = cp_lexer_peek_token (parser->lexer);
16035 while (true)
16037 bool done = false;
16039 /* Peek at the next token. */
16040 token = cp_lexer_peek_token (parser->lexer);
16041 /* What we do depends on what token we have. */
16042 switch (token->type)
16044 /* In valid code, a default argument must be
16045 immediately followed by a `,' `)', or `...'. */
16046 case CPP_COMMA:
16047 if (depth == 0 && maybe_template_id)
16049 /* If we've seen a '<', we might be in a
16050 template-argument-list. Until Core issue 325 is
16051 resolved, we don't know how this situation ought
16052 to be handled, so try to DTRT. We check whether
16053 what comes after the comma is a valid parameter
16054 declaration list. If it is, then the comma ends
16055 the default argument; otherwise the default
16056 argument continues. */
16057 bool error = false;
16058 tree t;
16060 /* Set ITALP so cp_parser_parameter_declaration_list
16061 doesn't decide to commit to this parse. */
16062 bool saved_italp = parser->in_template_argument_list_p;
16063 parser->in_template_argument_list_p = true;
16065 cp_parser_parse_tentatively (parser);
16066 cp_lexer_consume_token (parser->lexer);
16067 begin_scope (sk_function_parms, NULL_TREE);
16068 cp_parser_parameter_declaration_list (parser, &error);
16069 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16070 pop_binding (DECL_NAME (t), t);
16071 leave_scope ();
16072 if (!cp_parser_error_occurred (parser) && !error)
16073 done = true;
16074 cp_parser_abort_tentative_parse (parser);
16076 parser->in_template_argument_list_p = saved_italp;
16077 break;
16079 case CPP_CLOSE_PAREN:
16080 case CPP_ELLIPSIS:
16081 /* If we run into a non-nested `;', `}', or `]',
16082 then the code is invalid -- but the default
16083 argument is certainly over. */
16084 case CPP_SEMICOLON:
16085 case CPP_CLOSE_BRACE:
16086 case CPP_CLOSE_SQUARE:
16087 if (depth == 0)
16088 done = true;
16089 /* Update DEPTH, if necessary. */
16090 else if (token->type == CPP_CLOSE_PAREN
16091 || token->type == CPP_CLOSE_BRACE
16092 || token->type == CPP_CLOSE_SQUARE)
16093 --depth;
16094 break;
16096 case CPP_OPEN_PAREN:
16097 case CPP_OPEN_SQUARE:
16098 case CPP_OPEN_BRACE:
16099 ++depth;
16100 break;
16102 case CPP_LESS:
16103 if (depth == 0)
16104 /* This might be the comparison operator, or it might
16105 start a template argument list. */
16106 ++maybe_template_id;
16107 break;
16109 case CPP_RSHIFT:
16110 if (cxx_dialect == cxx98)
16111 break;
16112 /* Fall through for C++0x, which treats the `>>'
16113 operator like two `>' tokens in certain
16114 cases. */
16116 case CPP_GREATER:
16117 if (depth == 0)
16119 /* This might be an operator, or it might close a
16120 template argument list. But if a previous '<'
16121 started a template argument list, this will have
16122 closed it, so we can't be in one anymore. */
16123 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16124 if (maybe_template_id < 0)
16125 maybe_template_id = 0;
16127 break;
16129 /* If we run out of tokens, issue an error message. */
16130 case CPP_EOF:
16131 case CPP_PRAGMA_EOL:
16132 error_at (token->location, "file ends in default argument");
16133 done = true;
16134 break;
16136 case CPP_NAME:
16137 case CPP_SCOPE:
16138 /* In these cases, we should look for template-ids.
16139 For example, if the default argument is
16140 `X<int, double>()', we need to do name lookup to
16141 figure out whether or not `X' is a template; if
16142 so, the `,' does not end the default argument.
16144 That is not yet done. */
16145 break;
16147 default:
16148 break;
16151 /* If we've reached the end, stop. */
16152 if (done)
16153 break;
16155 /* Add the token to the token block. */
16156 token = cp_lexer_consume_token (parser->lexer);
16159 /* Create a DEFAULT_ARG to represent the unparsed default
16160 argument. */
16161 default_argument = make_node (DEFAULT_ARG);
16162 DEFARG_TOKENS (default_argument)
16163 = cp_token_cache_new (first_token, token);
16164 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16166 /* Outside of a class definition, we can just parse the
16167 assignment-expression. */
16168 else
16170 token = cp_lexer_peek_token (parser->lexer);
16171 default_argument
16172 = cp_parser_default_argument (parser, template_parm_p);
16175 if (!parser->default_arg_ok_p)
16177 if (flag_permissive)
16178 warning (0, "deprecated use of default argument for parameter of non-function");
16179 else
16181 error_at (token->location,
16182 "default arguments are only "
16183 "permitted for function parameters");
16184 default_argument = NULL_TREE;
16187 else if ((declarator && declarator->parameter_pack_p)
16188 || (decl_specifiers.type
16189 && PACK_EXPANSION_P (decl_specifiers.type)))
16191 /* Find the name of the parameter pack. */
16192 cp_declarator *id_declarator = declarator;
16193 while (id_declarator && id_declarator->kind != cdk_id)
16194 id_declarator = id_declarator->declarator;
16196 if (id_declarator && id_declarator->kind == cdk_id)
16197 error_at (declarator_token_start->location,
16198 template_parm_p
16199 ? "template parameter pack %qD"
16200 " cannot have a default argument"
16201 : "parameter pack %qD cannot have a default argument",
16202 id_declarator->u.id.unqualified_name);
16203 else
16204 error_at (declarator_token_start->location,
16205 template_parm_p
16206 ? "template parameter pack cannot have a default argument"
16207 : "parameter pack cannot have a default argument");
16209 default_argument = NULL_TREE;
16212 else
16213 default_argument = NULL_TREE;
16215 return make_parameter_declarator (&decl_specifiers,
16216 declarator,
16217 default_argument);
16220 /* Parse a default argument and return it.
16222 TEMPLATE_PARM_P is true if this is a default argument for a
16223 non-type template parameter. */
16224 static tree
16225 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16227 tree default_argument = NULL_TREE;
16228 bool saved_greater_than_is_operator_p;
16229 bool saved_local_variables_forbidden_p;
16231 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16232 set correctly. */
16233 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16234 parser->greater_than_is_operator_p = !template_parm_p;
16235 /* Local variable names (and the `this' keyword) may not
16236 appear in a default argument. */
16237 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16238 parser->local_variables_forbidden_p = true;
16239 /* Parse the assignment-expression. */
16240 if (template_parm_p)
16241 push_deferring_access_checks (dk_no_deferred);
16242 default_argument
16243 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16244 if (template_parm_p)
16245 pop_deferring_access_checks ();
16246 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16247 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16249 return default_argument;
16252 /* Parse a function-body.
16254 function-body:
16255 compound_statement */
16257 static void
16258 cp_parser_function_body (cp_parser *parser)
16260 cp_parser_compound_statement (parser, NULL, false);
16263 /* Parse a ctor-initializer-opt followed by a function-body. Return
16264 true if a ctor-initializer was present. */
16266 static bool
16267 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16269 tree body, list;
16270 bool ctor_initializer_p;
16271 const bool check_body_p =
16272 DECL_CONSTRUCTOR_P (current_function_decl)
16273 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16274 tree last = NULL;
16276 /* Begin the function body. */
16277 body = begin_function_body ();
16278 /* Parse the optional ctor-initializer. */
16279 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16281 /* If we're parsing a constexpr constructor definition, we need
16282 to check that the constructor body is indeed empty. However,
16283 before we get to cp_parser_function_body lot of junk has been
16284 generated, so we can't just check that we have an empty block.
16285 Rather we take a snapshot of the outermost block, and check whether
16286 cp_parser_function_body changed its state. */
16287 if (check_body_p)
16289 list = body;
16290 if (TREE_CODE (list) == BIND_EXPR)
16291 list = BIND_EXPR_BODY (list);
16292 if (TREE_CODE (list) == STATEMENT_LIST
16293 && STATEMENT_LIST_TAIL (list) != NULL)
16294 last = STATEMENT_LIST_TAIL (list)->stmt;
16296 /* Parse the function-body. */
16297 cp_parser_function_body (parser);
16298 if (check_body_p
16299 && (TREE_CODE (list) != STATEMENT_LIST
16300 || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL)
16301 || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt)))
16303 error ("constexpr constructor does not have empty body");
16304 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
16306 /* Finish the function body. */
16307 finish_function_body (body);
16309 return ctor_initializer_p;
16312 /* Parse an initializer.
16314 initializer:
16315 = initializer-clause
16316 ( expression-list )
16318 Returns an expression representing the initializer. If no
16319 initializer is present, NULL_TREE is returned.
16321 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16322 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16323 set to TRUE if there is no initializer present. If there is an
16324 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16325 is set to true; otherwise it is set to false. */
16327 static tree
16328 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16329 bool* non_constant_p)
16331 cp_token *token;
16332 tree init;
16334 /* Peek at the next token. */
16335 token = cp_lexer_peek_token (parser->lexer);
16337 /* Let our caller know whether or not this initializer was
16338 parenthesized. */
16339 *is_direct_init = (token->type != CPP_EQ);
16340 /* Assume that the initializer is constant. */
16341 *non_constant_p = false;
16343 if (token->type == CPP_EQ)
16345 /* Consume the `='. */
16346 cp_lexer_consume_token (parser->lexer);
16347 /* Parse the initializer-clause. */
16348 init = cp_parser_initializer_clause (parser, non_constant_p);
16350 else if (token->type == CPP_OPEN_PAREN)
16352 VEC(tree,gc) *vec;
16353 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16354 /*cast_p=*/false,
16355 /*allow_expansion_p=*/true,
16356 non_constant_p);
16357 if (vec == NULL)
16358 return error_mark_node;
16359 init = build_tree_list_vec (vec);
16360 release_tree_vector (vec);
16362 else if (token->type == CPP_OPEN_BRACE)
16364 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16365 init = cp_parser_braced_list (parser, non_constant_p);
16366 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16368 else
16370 /* Anything else is an error. */
16371 cp_parser_error (parser, "expected initializer");
16372 init = error_mark_node;
16375 return init;
16378 /* Parse an initializer-clause.
16380 initializer-clause:
16381 assignment-expression
16382 braced-init-list
16384 Returns an expression representing the initializer.
16386 If the `assignment-expression' production is used the value
16387 returned is simply a representation for the expression.
16389 Otherwise, calls cp_parser_braced_list. */
16391 static tree
16392 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16394 tree initializer;
16396 /* Assume the expression is constant. */
16397 *non_constant_p = false;
16399 /* If it is not a `{', then we are looking at an
16400 assignment-expression. */
16401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16403 initializer
16404 = cp_parser_constant_expression (parser,
16405 /*allow_non_constant_p=*/true,
16406 non_constant_p);
16407 if (!*non_constant_p)
16408 initializer = fold_non_dependent_expr (initializer);
16410 else
16411 initializer = cp_parser_braced_list (parser, non_constant_p);
16413 return initializer;
16416 /* Parse a brace-enclosed initializer list.
16418 braced-init-list:
16419 { initializer-list , [opt] }
16422 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16423 the elements of the initializer-list (or NULL, if the last
16424 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16425 NULL_TREE. There is no way to detect whether or not the optional
16426 trailing `,' was provided. NON_CONSTANT_P is as for
16427 cp_parser_initializer. */
16429 static tree
16430 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16432 tree initializer;
16434 /* Consume the `{' token. */
16435 cp_lexer_consume_token (parser->lexer);
16436 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16437 initializer = make_node (CONSTRUCTOR);
16438 /* If it's not a `}', then there is a non-trivial initializer. */
16439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16441 /* Parse the initializer list. */
16442 CONSTRUCTOR_ELTS (initializer)
16443 = cp_parser_initializer_list (parser, non_constant_p);
16444 /* A trailing `,' token is allowed. */
16445 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16446 cp_lexer_consume_token (parser->lexer);
16448 /* Now, there should be a trailing `}'. */
16449 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16450 TREE_TYPE (initializer) = init_list_type_node;
16451 return initializer;
16454 /* Parse an initializer-list.
16456 initializer-list:
16457 initializer-clause ... [opt]
16458 initializer-list , initializer-clause ... [opt]
16460 GNU Extension:
16462 initializer-list:
16463 identifier : initializer-clause
16464 initializer-list, identifier : initializer-clause
16466 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16467 for the initializer. If the INDEX of the elt is non-NULL, it is the
16468 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16469 as for cp_parser_initializer. */
16471 static VEC(constructor_elt,gc) *
16472 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16474 VEC(constructor_elt,gc) *v = NULL;
16476 /* Assume all of the expressions are constant. */
16477 *non_constant_p = false;
16479 /* Parse the rest of the list. */
16480 while (true)
16482 cp_token *token;
16483 tree identifier;
16484 tree initializer;
16485 bool clause_non_constant_p;
16487 /* If the next token is an identifier and the following one is a
16488 colon, we are looking at the GNU designated-initializer
16489 syntax. */
16490 if (cp_parser_allow_gnu_extensions_p (parser)
16491 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16492 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16494 /* Warn the user that they are using an extension. */
16495 pedwarn (input_location, OPT_pedantic,
16496 "ISO C++ does not allow designated initializers");
16497 /* Consume the identifier. */
16498 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16499 /* Consume the `:'. */
16500 cp_lexer_consume_token (parser->lexer);
16502 else
16503 identifier = NULL_TREE;
16505 /* Parse the initializer. */
16506 initializer = cp_parser_initializer_clause (parser,
16507 &clause_non_constant_p);
16508 /* If any clause is non-constant, so is the entire initializer. */
16509 if (clause_non_constant_p)
16510 *non_constant_p = true;
16512 /* If we have an ellipsis, this is an initializer pack
16513 expansion. */
16514 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16516 /* Consume the `...'. */
16517 cp_lexer_consume_token (parser->lexer);
16519 /* Turn the initializer into an initializer expansion. */
16520 initializer = make_pack_expansion (initializer);
16523 /* Add it to the vector. */
16524 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16526 /* If the next token is not a comma, we have reached the end of
16527 the list. */
16528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16529 break;
16531 /* Peek at the next token. */
16532 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16533 /* If the next token is a `}', then we're still done. An
16534 initializer-clause can have a trailing `,' after the
16535 initializer-list and before the closing `}'. */
16536 if (token->type == CPP_CLOSE_BRACE)
16537 break;
16539 /* Consume the `,' token. */
16540 cp_lexer_consume_token (parser->lexer);
16543 return v;
16546 /* Classes [gram.class] */
16548 /* Parse a class-name.
16550 class-name:
16551 identifier
16552 template-id
16554 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16555 to indicate that names looked up in dependent types should be
16556 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16557 keyword has been used to indicate that the name that appears next
16558 is a template. TAG_TYPE indicates the explicit tag given before
16559 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16560 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16561 is the class being defined in a class-head.
16563 Returns the TYPE_DECL representing the class. */
16565 static tree
16566 cp_parser_class_name (cp_parser *parser,
16567 bool typename_keyword_p,
16568 bool template_keyword_p,
16569 enum tag_types tag_type,
16570 bool check_dependency_p,
16571 bool class_head_p,
16572 bool is_declaration)
16574 tree decl;
16575 tree scope;
16576 bool typename_p;
16577 cp_token *token;
16578 tree identifier = NULL_TREE;
16580 /* All class-names start with an identifier. */
16581 token = cp_lexer_peek_token (parser->lexer);
16582 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16584 cp_parser_error (parser, "expected class-name");
16585 return error_mark_node;
16588 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16589 to a template-id, so we save it here. */
16590 scope = parser->scope;
16591 if (scope == error_mark_node)
16592 return error_mark_node;
16594 /* Any name names a type if we're following the `typename' keyword
16595 in a qualified name where the enclosing scope is type-dependent. */
16596 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16597 && dependent_type_p (scope));
16598 /* Handle the common case (an identifier, but not a template-id)
16599 efficiently. */
16600 if (token->type == CPP_NAME
16601 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16603 cp_token *identifier_token;
16604 bool ambiguous_p;
16606 /* Look for the identifier. */
16607 identifier_token = cp_lexer_peek_token (parser->lexer);
16608 ambiguous_p = identifier_token->ambiguous_p;
16609 identifier = cp_parser_identifier (parser);
16610 /* If the next token isn't an identifier, we are certainly not
16611 looking at a class-name. */
16612 if (identifier == error_mark_node)
16613 decl = error_mark_node;
16614 /* If we know this is a type-name, there's no need to look it
16615 up. */
16616 else if (typename_p)
16617 decl = identifier;
16618 else
16620 tree ambiguous_decls;
16621 /* If we already know that this lookup is ambiguous, then
16622 we've already issued an error message; there's no reason
16623 to check again. */
16624 if (ambiguous_p)
16626 cp_parser_simulate_error (parser);
16627 return error_mark_node;
16629 /* If the next token is a `::', then the name must be a type
16630 name.
16632 [basic.lookup.qual]
16634 During the lookup for a name preceding the :: scope
16635 resolution operator, object, function, and enumerator
16636 names are ignored. */
16637 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16638 tag_type = typename_type;
16639 /* Look up the name. */
16640 decl = cp_parser_lookup_name (parser, identifier,
16641 tag_type,
16642 /*is_template=*/false,
16643 /*is_namespace=*/false,
16644 check_dependency_p,
16645 &ambiguous_decls,
16646 identifier_token->location);
16647 if (ambiguous_decls)
16649 if (cp_parser_parsing_tentatively (parser))
16650 cp_parser_simulate_error (parser);
16651 return error_mark_node;
16655 else
16657 /* Try a template-id. */
16658 decl = cp_parser_template_id (parser, template_keyword_p,
16659 check_dependency_p,
16660 is_declaration);
16661 if (decl == error_mark_node)
16662 return error_mark_node;
16665 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16667 /* If this is a typename, create a TYPENAME_TYPE. */
16668 if (typename_p && decl != error_mark_node)
16670 decl = make_typename_type (scope, decl, typename_type,
16671 /*complain=*/tf_error);
16672 if (decl != error_mark_node)
16673 decl = TYPE_NAME (decl);
16676 /* Check to see that it is really the name of a class. */
16677 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16678 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16679 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16680 /* Situations like this:
16682 template <typename T> struct A {
16683 typename T::template X<int>::I i;
16686 are problematic. Is `T::template X<int>' a class-name? The
16687 standard does not seem to be definitive, but there is no other
16688 valid interpretation of the following `::'. Therefore, those
16689 names are considered class-names. */
16691 decl = make_typename_type (scope, decl, tag_type, tf_error);
16692 if (decl != error_mark_node)
16693 decl = TYPE_NAME (decl);
16695 else if (TREE_CODE (decl) != TYPE_DECL
16696 || TREE_TYPE (decl) == error_mark_node
16697 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16698 decl = error_mark_node;
16700 if (decl == error_mark_node)
16701 cp_parser_error (parser, "expected class-name");
16702 else if (identifier && !parser->scope)
16703 maybe_note_name_used_in_class (identifier, decl);
16705 return decl;
16708 /* Parse a class-specifier.
16710 class-specifier:
16711 class-head { member-specification [opt] }
16713 Returns the TREE_TYPE representing the class. */
16715 static tree
16716 cp_parser_class_specifier (cp_parser* parser)
16718 tree type;
16719 tree attributes = NULL_TREE;
16720 bool nested_name_specifier_p;
16721 unsigned saved_num_template_parameter_lists;
16722 bool saved_in_function_body;
16723 bool saved_in_unbraced_linkage_specification_p;
16724 tree old_scope = NULL_TREE;
16725 tree scope = NULL_TREE;
16726 tree bases;
16728 push_deferring_access_checks (dk_no_deferred);
16730 /* Parse the class-head. */
16731 type = cp_parser_class_head (parser,
16732 &nested_name_specifier_p,
16733 &attributes,
16734 &bases);
16735 /* If the class-head was a semantic disaster, skip the entire body
16736 of the class. */
16737 if (!type)
16739 cp_parser_skip_to_end_of_block_or_statement (parser);
16740 pop_deferring_access_checks ();
16741 return error_mark_node;
16744 /* Look for the `{'. */
16745 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16747 pop_deferring_access_checks ();
16748 return error_mark_node;
16751 /* Process the base classes. If they're invalid, skip the
16752 entire class body. */
16753 if (!xref_basetypes (type, bases))
16755 /* Consuming the closing brace yields better error messages
16756 later on. */
16757 if (cp_parser_skip_to_closing_brace (parser))
16758 cp_lexer_consume_token (parser->lexer);
16759 pop_deferring_access_checks ();
16760 return error_mark_node;
16763 /* Issue an error message if type-definitions are forbidden here. */
16764 cp_parser_check_type_definition (parser);
16765 /* Remember that we are defining one more class. */
16766 ++parser->num_classes_being_defined;
16767 /* Inside the class, surrounding template-parameter-lists do not
16768 apply. */
16769 saved_num_template_parameter_lists
16770 = parser->num_template_parameter_lists;
16771 parser->num_template_parameter_lists = 0;
16772 /* We are not in a function body. */
16773 saved_in_function_body = parser->in_function_body;
16774 parser->in_function_body = false;
16775 /* We are not immediately inside an extern "lang" block. */
16776 saved_in_unbraced_linkage_specification_p
16777 = parser->in_unbraced_linkage_specification_p;
16778 parser->in_unbraced_linkage_specification_p = false;
16780 /* Start the class. */
16781 if (nested_name_specifier_p)
16783 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16784 old_scope = push_inner_scope (scope);
16786 type = begin_class_definition (type, attributes);
16788 if (type == error_mark_node)
16789 /* If the type is erroneous, skip the entire body of the class. */
16790 cp_parser_skip_to_closing_brace (parser);
16791 else
16792 /* Parse the member-specification. */
16793 cp_parser_member_specification_opt (parser);
16795 /* Look for the trailing `}'. */
16796 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16797 /* Look for trailing attributes to apply to this class. */
16798 if (cp_parser_allow_gnu_extensions_p (parser))
16799 attributes = cp_parser_attributes_opt (parser);
16800 if (type != error_mark_node)
16801 type = finish_struct (type, attributes);
16802 if (nested_name_specifier_p)
16803 pop_inner_scope (old_scope, scope);
16804 /* If this class is not itself within the scope of another class,
16805 then we need to parse the bodies of all of the queued function
16806 definitions. Note that the queued functions defined in a class
16807 are not always processed immediately following the
16808 class-specifier for that class. Consider:
16810 struct A {
16811 struct B { void f() { sizeof (A); } };
16814 If `f' were processed before the processing of `A' were
16815 completed, there would be no way to compute the size of `A'.
16816 Note that the nesting we are interested in here is lexical --
16817 not the semantic nesting given by TYPE_CONTEXT. In particular,
16818 for:
16820 struct A { struct B; };
16821 struct A::B { void f() { } };
16823 there is no need to delay the parsing of `A::B::f'. */
16824 if (--parser->num_classes_being_defined == 0)
16826 tree fn;
16827 tree class_type = NULL_TREE;
16828 tree pushed_scope = NULL_TREE;
16829 unsigned ix;
16830 cp_default_arg_entry *e;
16832 /* In a first pass, parse default arguments to the functions.
16833 Then, in a second pass, parse the bodies of the functions.
16834 This two-phased approach handles cases like:
16836 struct S {
16837 void f() { g(); }
16838 void g(int i = 3);
16842 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16843 ix, e)
16845 fn = e->decl;
16846 /* If there are default arguments that have not yet been processed,
16847 take care of them now. */
16848 if (class_type != e->class_type)
16850 if (pushed_scope)
16851 pop_scope (pushed_scope);
16852 class_type = e->class_type;
16853 pushed_scope = push_scope (class_type);
16855 /* Make sure that any template parameters are in scope. */
16856 maybe_begin_member_template_processing (fn);
16857 /* Parse the default argument expressions. */
16858 cp_parser_late_parsing_default_args (parser, fn);
16859 /* Remove any template parameters from the symbol table. */
16860 maybe_end_member_template_processing ();
16862 if (pushed_scope)
16863 pop_scope (pushed_scope);
16864 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16865 /* Now parse the body of the functions. */
16866 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16867 cp_parser_late_parsing_for_member (parser, fn);
16868 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16871 /* Put back any saved access checks. */
16872 pop_deferring_access_checks ();
16874 /* Restore saved state. */
16875 parser->in_function_body = saved_in_function_body;
16876 parser->num_template_parameter_lists
16877 = saved_num_template_parameter_lists;
16878 parser->in_unbraced_linkage_specification_p
16879 = saved_in_unbraced_linkage_specification_p;
16881 return type;
16884 /* Parse a class-head.
16886 class-head:
16887 class-key identifier [opt] base-clause [opt]
16888 class-key nested-name-specifier identifier base-clause [opt]
16889 class-key nested-name-specifier [opt] template-id
16890 base-clause [opt]
16892 GNU Extensions:
16893 class-key attributes identifier [opt] base-clause [opt]
16894 class-key attributes nested-name-specifier identifier base-clause [opt]
16895 class-key attributes nested-name-specifier [opt] template-id
16896 base-clause [opt]
16898 Upon return BASES is initialized to the list of base classes (or
16899 NULL, if there are none) in the same form returned by
16900 cp_parser_base_clause.
16902 Returns the TYPE of the indicated class. Sets
16903 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16904 involving a nested-name-specifier was used, and FALSE otherwise.
16906 Returns error_mark_node if this is not a class-head.
16908 Returns NULL_TREE if the class-head is syntactically valid, but
16909 semantically invalid in a way that means we should skip the entire
16910 body of the class. */
16912 static tree
16913 cp_parser_class_head (cp_parser* parser,
16914 bool* nested_name_specifier_p,
16915 tree *attributes_p,
16916 tree *bases)
16918 tree nested_name_specifier;
16919 enum tag_types class_key;
16920 tree id = NULL_TREE;
16921 tree type = NULL_TREE;
16922 tree attributes;
16923 bool template_id_p = false;
16924 bool qualified_p = false;
16925 bool invalid_nested_name_p = false;
16926 bool invalid_explicit_specialization_p = false;
16927 tree pushed_scope = NULL_TREE;
16928 unsigned num_templates;
16929 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16930 /* Assume no nested-name-specifier will be present. */
16931 *nested_name_specifier_p = false;
16932 /* Assume no template parameter lists will be used in defining the
16933 type. */
16934 num_templates = 0;
16936 *bases = NULL_TREE;
16938 /* Look for the class-key. */
16939 class_key = cp_parser_class_key (parser);
16940 if (class_key == none_type)
16941 return error_mark_node;
16943 /* Parse the attributes. */
16944 attributes = cp_parser_attributes_opt (parser);
16946 /* If the next token is `::', that is invalid -- but sometimes
16947 people do try to write:
16949 struct ::S {};
16951 Handle this gracefully by accepting the extra qualifier, and then
16952 issuing an error about it later if this really is a
16953 class-head. If it turns out just to be an elaborated type
16954 specifier, remain silent. */
16955 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16956 qualified_p = true;
16958 push_deferring_access_checks (dk_no_check);
16960 /* Determine the name of the class. Begin by looking for an
16961 optional nested-name-specifier. */
16962 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16963 nested_name_specifier
16964 = cp_parser_nested_name_specifier_opt (parser,
16965 /*typename_keyword_p=*/false,
16966 /*check_dependency_p=*/false,
16967 /*type_p=*/false,
16968 /*is_declaration=*/false);
16969 /* If there was a nested-name-specifier, then there *must* be an
16970 identifier. */
16971 if (nested_name_specifier)
16973 type_start_token = cp_lexer_peek_token (parser->lexer);
16974 /* Although the grammar says `identifier', it really means
16975 `class-name' or `template-name'. You are only allowed to
16976 define a class that has already been declared with this
16977 syntax.
16979 The proposed resolution for Core Issue 180 says that wherever
16980 you see `class T::X' you should treat `X' as a type-name.
16982 It is OK to define an inaccessible class; for example:
16984 class A { class B; };
16985 class A::B {};
16987 We do not know if we will see a class-name, or a
16988 template-name. We look for a class-name first, in case the
16989 class-name is a template-id; if we looked for the
16990 template-name first we would stop after the template-name. */
16991 cp_parser_parse_tentatively (parser);
16992 type = cp_parser_class_name (parser,
16993 /*typename_keyword_p=*/false,
16994 /*template_keyword_p=*/false,
16995 class_type,
16996 /*check_dependency_p=*/false,
16997 /*class_head_p=*/true,
16998 /*is_declaration=*/false);
16999 /* If that didn't work, ignore the nested-name-specifier. */
17000 if (!cp_parser_parse_definitely (parser))
17002 invalid_nested_name_p = true;
17003 type_start_token = cp_lexer_peek_token (parser->lexer);
17004 id = cp_parser_identifier (parser);
17005 if (id == error_mark_node)
17006 id = NULL_TREE;
17008 /* If we could not find a corresponding TYPE, treat this
17009 declaration like an unqualified declaration. */
17010 if (type == error_mark_node)
17011 nested_name_specifier = NULL_TREE;
17012 /* Otherwise, count the number of templates used in TYPE and its
17013 containing scopes. */
17014 else
17016 tree scope;
17018 for (scope = TREE_TYPE (type);
17019 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17020 scope = (TYPE_P (scope)
17021 ? TYPE_CONTEXT (scope)
17022 : DECL_CONTEXT (scope)))
17023 if (TYPE_P (scope)
17024 && CLASS_TYPE_P (scope)
17025 && CLASSTYPE_TEMPLATE_INFO (scope)
17026 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17027 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17028 ++num_templates;
17031 /* Otherwise, the identifier is optional. */
17032 else
17034 /* We don't know whether what comes next is a template-id,
17035 an identifier, or nothing at all. */
17036 cp_parser_parse_tentatively (parser);
17037 /* Check for a template-id. */
17038 type_start_token = cp_lexer_peek_token (parser->lexer);
17039 id = cp_parser_template_id (parser,
17040 /*template_keyword_p=*/false,
17041 /*check_dependency_p=*/true,
17042 /*is_declaration=*/true);
17043 /* If that didn't work, it could still be an identifier. */
17044 if (!cp_parser_parse_definitely (parser))
17046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17048 type_start_token = cp_lexer_peek_token (parser->lexer);
17049 id = cp_parser_identifier (parser);
17051 else
17052 id = NULL_TREE;
17054 else
17056 template_id_p = true;
17057 ++num_templates;
17061 pop_deferring_access_checks ();
17063 if (id)
17064 cp_parser_check_for_invalid_template_id (parser, id,
17065 type_start_token->location);
17067 /* If it's not a `:' or a `{' then we can't really be looking at a
17068 class-head, since a class-head only appears as part of a
17069 class-specifier. We have to detect this situation before calling
17070 xref_tag, since that has irreversible side-effects. */
17071 if (!cp_parser_next_token_starts_class_definition_p (parser))
17073 cp_parser_error (parser, "expected %<{%> or %<:%>");
17074 return error_mark_node;
17077 /* At this point, we're going ahead with the class-specifier, even
17078 if some other problem occurs. */
17079 cp_parser_commit_to_tentative_parse (parser);
17080 /* Issue the error about the overly-qualified name now. */
17081 if (qualified_p)
17083 cp_parser_error (parser,
17084 "global qualification of class name is invalid");
17085 return error_mark_node;
17087 else if (invalid_nested_name_p)
17089 cp_parser_error (parser,
17090 "qualified name does not name a class");
17091 return error_mark_node;
17093 else if (nested_name_specifier)
17095 tree scope;
17097 /* Reject typedef-names in class heads. */
17098 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17100 error_at (type_start_token->location,
17101 "invalid class name in declaration of %qD",
17102 type);
17103 type = NULL_TREE;
17104 goto done;
17107 /* Figure out in what scope the declaration is being placed. */
17108 scope = current_scope ();
17109 /* If that scope does not contain the scope in which the
17110 class was originally declared, the program is invalid. */
17111 if (scope && !is_ancestor (scope, nested_name_specifier))
17113 if (at_namespace_scope_p ())
17114 error_at (type_start_token->location,
17115 "declaration of %qD in namespace %qD which does not "
17116 "enclose %qD",
17117 type, scope, nested_name_specifier);
17118 else
17119 error_at (type_start_token->location,
17120 "declaration of %qD in %qD which does not enclose %qD",
17121 type, scope, nested_name_specifier);
17122 type = NULL_TREE;
17123 goto done;
17125 /* [dcl.meaning]
17127 A declarator-id shall not be qualified except for the
17128 definition of a ... nested class outside of its class
17129 ... [or] the definition or explicit instantiation of a
17130 class member of a namespace outside of its namespace. */
17131 if (scope == nested_name_specifier)
17133 permerror (nested_name_specifier_token_start->location,
17134 "extra qualification not allowed");
17135 nested_name_specifier = NULL_TREE;
17136 num_templates = 0;
17139 /* An explicit-specialization must be preceded by "template <>". If
17140 it is not, try to recover gracefully. */
17141 if (at_namespace_scope_p ()
17142 && parser->num_template_parameter_lists == 0
17143 && template_id_p)
17145 error_at (type_start_token->location,
17146 "an explicit specialization must be preceded by %<template <>%>");
17147 invalid_explicit_specialization_p = true;
17148 /* Take the same action that would have been taken by
17149 cp_parser_explicit_specialization. */
17150 ++parser->num_template_parameter_lists;
17151 begin_specialization ();
17153 /* There must be no "return" statements between this point and the
17154 end of this function; set "type "to the correct return value and
17155 use "goto done;" to return. */
17156 /* Make sure that the right number of template parameters were
17157 present. */
17158 if (!cp_parser_check_template_parameters (parser, num_templates,
17159 type_start_token->location,
17160 /*declarator=*/NULL))
17162 /* If something went wrong, there is no point in even trying to
17163 process the class-definition. */
17164 type = NULL_TREE;
17165 goto done;
17168 /* Look up the type. */
17169 if (template_id_p)
17171 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17172 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17173 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17175 error_at (type_start_token->location,
17176 "function template %qD redeclared as a class template", id);
17177 type = error_mark_node;
17179 else
17181 type = TREE_TYPE (id);
17182 type = maybe_process_partial_specialization (type);
17184 if (nested_name_specifier)
17185 pushed_scope = push_scope (nested_name_specifier);
17187 else if (nested_name_specifier)
17189 tree class_type;
17191 /* Given:
17193 template <typename T> struct S { struct T };
17194 template <typename T> struct S<T>::T { };
17196 we will get a TYPENAME_TYPE when processing the definition of
17197 `S::T'. We need to resolve it to the actual type before we
17198 try to define it. */
17199 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17201 class_type = resolve_typename_type (TREE_TYPE (type),
17202 /*only_current_p=*/false);
17203 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17204 type = TYPE_NAME (class_type);
17205 else
17207 cp_parser_error (parser, "could not resolve typename type");
17208 type = error_mark_node;
17212 if (maybe_process_partial_specialization (TREE_TYPE (type))
17213 == error_mark_node)
17215 type = NULL_TREE;
17216 goto done;
17219 class_type = current_class_type;
17220 /* Enter the scope indicated by the nested-name-specifier. */
17221 pushed_scope = push_scope (nested_name_specifier);
17222 /* Get the canonical version of this type. */
17223 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17224 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17225 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17227 type = push_template_decl (type);
17228 if (type == error_mark_node)
17230 type = NULL_TREE;
17231 goto done;
17235 type = TREE_TYPE (type);
17236 *nested_name_specifier_p = true;
17238 else /* The name is not a nested name. */
17240 /* If the class was unnamed, create a dummy name. */
17241 if (!id)
17242 id = make_anon_name ();
17243 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17244 parser->num_template_parameter_lists);
17247 /* Indicate whether this class was declared as a `class' or as a
17248 `struct'. */
17249 if (TREE_CODE (type) == RECORD_TYPE)
17250 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17251 cp_parser_check_class_key (class_key, type);
17253 /* If this type was already complete, and we see another definition,
17254 that's an error. */
17255 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17257 error_at (type_start_token->location, "redefinition of %q#T",
17258 type);
17259 error_at (type_start_token->location, "previous definition of %q+#T",
17260 type);
17261 type = NULL_TREE;
17262 goto done;
17264 else if (type == error_mark_node)
17265 type = NULL_TREE;
17267 /* We will have entered the scope containing the class; the names of
17268 base classes should be looked up in that context. For example:
17270 struct A { struct B {}; struct C; };
17271 struct A::C : B {};
17273 is valid. */
17275 /* Get the list of base-classes, if there is one. */
17276 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17277 *bases = cp_parser_base_clause (parser);
17279 done:
17280 /* Leave the scope given by the nested-name-specifier. We will
17281 enter the class scope itself while processing the members. */
17282 if (pushed_scope)
17283 pop_scope (pushed_scope);
17285 if (invalid_explicit_specialization_p)
17287 end_specialization ();
17288 --parser->num_template_parameter_lists;
17291 if (type)
17292 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17293 *attributes_p = attributes;
17294 return type;
17297 /* Parse a class-key.
17299 class-key:
17300 class
17301 struct
17302 union
17304 Returns the kind of class-key specified, or none_type to indicate
17305 error. */
17307 static enum tag_types
17308 cp_parser_class_key (cp_parser* parser)
17310 cp_token *token;
17311 enum tag_types tag_type;
17313 /* Look for the class-key. */
17314 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17315 if (!token)
17316 return none_type;
17318 /* Check to see if the TOKEN is a class-key. */
17319 tag_type = cp_parser_token_is_class_key (token);
17320 if (!tag_type)
17321 cp_parser_error (parser, "expected class-key");
17322 return tag_type;
17325 /* Parse an (optional) member-specification.
17327 member-specification:
17328 member-declaration member-specification [opt]
17329 access-specifier : member-specification [opt] */
17331 static void
17332 cp_parser_member_specification_opt (cp_parser* parser)
17334 while (true)
17336 cp_token *token;
17337 enum rid keyword;
17339 /* Peek at the next token. */
17340 token = cp_lexer_peek_token (parser->lexer);
17341 /* If it's a `}', or EOF then we've seen all the members. */
17342 if (token->type == CPP_CLOSE_BRACE
17343 || token->type == CPP_EOF
17344 || token->type == CPP_PRAGMA_EOL)
17345 break;
17347 /* See if this token is a keyword. */
17348 keyword = token->keyword;
17349 switch (keyword)
17351 case RID_PUBLIC:
17352 case RID_PROTECTED:
17353 case RID_PRIVATE:
17354 /* Consume the access-specifier. */
17355 cp_lexer_consume_token (parser->lexer);
17356 /* Remember which access-specifier is active. */
17357 current_access_specifier = token->u.value;
17358 /* Look for the `:'. */
17359 cp_parser_require (parser, CPP_COLON, RT_COLON);
17360 break;
17362 default:
17363 /* Accept #pragmas at class scope. */
17364 if (token->type == CPP_PRAGMA)
17366 cp_parser_pragma (parser, pragma_external);
17367 break;
17370 /* Otherwise, the next construction must be a
17371 member-declaration. */
17372 cp_parser_member_declaration (parser);
17377 /* Parse a member-declaration.
17379 member-declaration:
17380 decl-specifier-seq [opt] member-declarator-list [opt] ;
17381 function-definition ; [opt]
17382 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17383 using-declaration
17384 template-declaration
17386 member-declarator-list:
17387 member-declarator
17388 member-declarator-list , member-declarator
17390 member-declarator:
17391 declarator pure-specifier [opt]
17392 declarator constant-initializer [opt]
17393 identifier [opt] : constant-expression
17395 GNU Extensions:
17397 member-declaration:
17398 __extension__ member-declaration
17400 member-declarator:
17401 declarator attributes [opt] pure-specifier [opt]
17402 declarator attributes [opt] constant-initializer [opt]
17403 identifier [opt] attributes [opt] : constant-expression
17405 C++0x Extensions:
17407 member-declaration:
17408 static_assert-declaration */
17410 static void
17411 cp_parser_member_declaration (cp_parser* parser)
17413 cp_decl_specifier_seq decl_specifiers;
17414 tree prefix_attributes;
17415 tree decl;
17416 int declares_class_or_enum;
17417 bool friend_p;
17418 cp_token *token = NULL;
17419 cp_token *decl_spec_token_start = NULL;
17420 cp_token *initializer_token_start = NULL;
17421 int saved_pedantic;
17423 /* Check for the `__extension__' keyword. */
17424 if (cp_parser_extension_opt (parser, &saved_pedantic))
17426 /* Recurse. */
17427 cp_parser_member_declaration (parser);
17428 /* Restore the old value of the PEDANTIC flag. */
17429 pedantic = saved_pedantic;
17431 return;
17434 /* Check for a template-declaration. */
17435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17437 /* An explicit specialization here is an error condition, and we
17438 expect the specialization handler to detect and report this. */
17439 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17440 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17441 cp_parser_explicit_specialization (parser);
17442 else
17443 cp_parser_template_declaration (parser, /*member_p=*/true);
17445 return;
17448 /* Check for a using-declaration. */
17449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17451 /* Parse the using-declaration. */
17452 cp_parser_using_declaration (parser,
17453 /*access_declaration_p=*/false);
17454 return;
17457 /* Check for @defs. */
17458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17460 tree ivar, member;
17461 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17462 ivar = ivar_chains;
17463 while (ivar)
17465 member = ivar;
17466 ivar = TREE_CHAIN (member);
17467 TREE_CHAIN (member) = NULL_TREE;
17468 finish_member_declaration (member);
17470 return;
17473 /* If the next token is `static_assert' we have a static assertion. */
17474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17476 cp_parser_static_assert (parser, /*member_p=*/true);
17477 return;
17480 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17481 return;
17483 /* Parse the decl-specifier-seq. */
17484 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17485 cp_parser_decl_specifier_seq (parser,
17486 CP_PARSER_FLAGS_OPTIONAL,
17487 &decl_specifiers,
17488 &declares_class_or_enum);
17489 prefix_attributes = decl_specifiers.attributes;
17490 decl_specifiers.attributes = NULL_TREE;
17491 /* Check for an invalid type-name. */
17492 if (!decl_specifiers.any_type_specifiers_p
17493 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17494 return;
17495 /* If there is no declarator, then the decl-specifier-seq should
17496 specify a type. */
17497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17499 /* If there was no decl-specifier-seq, and the next token is a
17500 `;', then we have something like:
17502 struct S { ; };
17504 [class.mem]
17506 Each member-declaration shall declare at least one member
17507 name of the class. */
17508 if (!decl_specifiers.any_specifiers_p)
17510 cp_token *token = cp_lexer_peek_token (parser->lexer);
17511 if (!in_system_header_at (token->location))
17512 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17514 else
17516 tree type;
17518 /* See if this declaration is a friend. */
17519 friend_p = cp_parser_friend_p (&decl_specifiers);
17520 /* If there were decl-specifiers, check to see if there was
17521 a class-declaration. */
17522 type = check_tag_decl (&decl_specifiers);
17523 /* Nested classes have already been added to the class, but
17524 a `friend' needs to be explicitly registered. */
17525 if (friend_p)
17527 /* If the `friend' keyword was present, the friend must
17528 be introduced with a class-key. */
17529 if (!declares_class_or_enum)
17530 error_at (decl_spec_token_start->location,
17531 "a class-key must be used when declaring a friend");
17532 /* In this case:
17534 template <typename T> struct A {
17535 friend struct A<T>::B;
17538 A<T>::B will be represented by a TYPENAME_TYPE, and
17539 therefore not recognized by check_tag_decl. */
17540 if (!type
17541 && decl_specifiers.type
17542 && TYPE_P (decl_specifiers.type))
17543 type = decl_specifiers.type;
17544 if (!type || !TYPE_P (type))
17545 error_at (decl_spec_token_start->location,
17546 "friend declaration does not name a class or "
17547 "function");
17548 else
17549 make_friend_class (current_class_type, type,
17550 /*complain=*/true);
17552 /* If there is no TYPE, an error message will already have
17553 been issued. */
17554 else if (!type || type == error_mark_node)
17556 /* An anonymous aggregate has to be handled specially; such
17557 a declaration really declares a data member (with a
17558 particular type), as opposed to a nested class. */
17559 else if (ANON_AGGR_TYPE_P (type))
17561 /* Remove constructors and such from TYPE, now that we
17562 know it is an anonymous aggregate. */
17563 fixup_anonymous_aggr (type);
17564 /* And make the corresponding data member. */
17565 decl = build_decl (decl_spec_token_start->location,
17566 FIELD_DECL, NULL_TREE, type);
17567 /* Add it to the class. */
17568 finish_member_declaration (decl);
17570 else
17571 cp_parser_check_access_in_redeclaration
17572 (TYPE_NAME (type),
17573 decl_spec_token_start->location);
17576 else
17578 /* See if these declarations will be friends. */
17579 friend_p = cp_parser_friend_p (&decl_specifiers);
17581 /* Keep going until we hit the `;' at the end of the
17582 declaration. */
17583 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17585 tree attributes = NULL_TREE;
17586 tree first_attribute;
17588 /* Peek at the next token. */
17589 token = cp_lexer_peek_token (parser->lexer);
17591 /* Check for a bitfield declaration. */
17592 if (token->type == CPP_COLON
17593 || (token->type == CPP_NAME
17594 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17595 == CPP_COLON))
17597 tree identifier;
17598 tree width;
17600 /* Get the name of the bitfield. Note that we cannot just
17601 check TOKEN here because it may have been invalidated by
17602 the call to cp_lexer_peek_nth_token above. */
17603 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17604 identifier = cp_parser_identifier (parser);
17605 else
17606 identifier = NULL_TREE;
17608 /* Consume the `:' token. */
17609 cp_lexer_consume_token (parser->lexer);
17610 /* Get the width of the bitfield. */
17611 width
17612 = cp_parser_constant_expression (parser,
17613 /*allow_non_constant=*/false,
17614 NULL);
17616 /* Look for attributes that apply to the bitfield. */
17617 attributes = cp_parser_attributes_opt (parser);
17618 /* Remember which attributes are prefix attributes and
17619 which are not. */
17620 first_attribute = attributes;
17621 /* Combine the attributes. */
17622 attributes = chainon (prefix_attributes, attributes);
17624 /* Create the bitfield declaration. */
17625 decl = grokbitfield (identifier
17626 ? make_id_declarator (NULL_TREE,
17627 identifier,
17628 sfk_none)
17629 : NULL,
17630 &decl_specifiers,
17631 width,
17632 attributes);
17634 else
17636 cp_declarator *declarator;
17637 tree initializer;
17638 tree asm_specification;
17639 int ctor_dtor_or_conv_p;
17641 /* Parse the declarator. */
17642 declarator
17643 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17644 &ctor_dtor_or_conv_p,
17645 /*parenthesized_p=*/NULL,
17646 /*member_p=*/true);
17648 /* If something went wrong parsing the declarator, make sure
17649 that we at least consume some tokens. */
17650 if (declarator == cp_error_declarator)
17652 /* Skip to the end of the statement. */
17653 cp_parser_skip_to_end_of_statement (parser);
17654 /* If the next token is not a semicolon, that is
17655 probably because we just skipped over the body of
17656 a function. So, we consume a semicolon if
17657 present, but do not issue an error message if it
17658 is not present. */
17659 if (cp_lexer_next_token_is (parser->lexer,
17660 CPP_SEMICOLON))
17661 cp_lexer_consume_token (parser->lexer);
17662 return;
17665 if (declares_class_or_enum & 2)
17666 cp_parser_check_for_definition_in_return_type
17667 (declarator, decl_specifiers.type,
17668 decl_specifiers.type_location);
17670 /* Look for an asm-specification. */
17671 asm_specification = cp_parser_asm_specification_opt (parser);
17672 /* Look for attributes that apply to the declaration. */
17673 attributes = cp_parser_attributes_opt (parser);
17674 /* Remember which attributes are prefix attributes and
17675 which are not. */
17676 first_attribute = attributes;
17677 /* Combine the attributes. */
17678 attributes = chainon (prefix_attributes, attributes);
17680 /* If it's an `=', then we have a constant-initializer or a
17681 pure-specifier. It is not correct to parse the
17682 initializer before registering the member declaration
17683 since the member declaration should be in scope while
17684 its initializer is processed. However, the rest of the
17685 front end does not yet provide an interface that allows
17686 us to handle this correctly. */
17687 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17689 /* In [class.mem]:
17691 A pure-specifier shall be used only in the declaration of
17692 a virtual function.
17694 A member-declarator can contain a constant-initializer
17695 only if it declares a static member of integral or
17696 enumeration type.
17698 Therefore, if the DECLARATOR is for a function, we look
17699 for a pure-specifier; otherwise, we look for a
17700 constant-initializer. When we call `grokfield', it will
17701 perform more stringent semantics checks. */
17702 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17703 if (function_declarator_p (declarator))
17704 initializer = cp_parser_pure_specifier (parser);
17705 else
17706 /* Parse the initializer. */
17707 initializer = cp_parser_constant_initializer (parser);
17709 /* Otherwise, there is no initializer. */
17710 else
17711 initializer = NULL_TREE;
17713 /* See if we are probably looking at a function
17714 definition. We are certainly not looking at a
17715 member-declarator. Calling `grokfield' has
17716 side-effects, so we must not do it unless we are sure
17717 that we are looking at a member-declarator. */
17718 if (cp_parser_token_starts_function_definition_p
17719 (cp_lexer_peek_token (parser->lexer)))
17721 /* The grammar does not allow a pure-specifier to be
17722 used when a member function is defined. (It is
17723 possible that this fact is an oversight in the
17724 standard, since a pure function may be defined
17725 outside of the class-specifier. */
17726 if (initializer)
17727 error_at (initializer_token_start->location,
17728 "pure-specifier on function-definition");
17729 decl = cp_parser_save_member_function_body (parser,
17730 &decl_specifiers,
17731 declarator,
17732 attributes);
17733 /* If the member was not a friend, declare it here. */
17734 if (!friend_p)
17735 finish_member_declaration (decl);
17736 /* Peek at the next token. */
17737 token = cp_lexer_peek_token (parser->lexer);
17738 /* If the next token is a semicolon, consume it. */
17739 if (token->type == CPP_SEMICOLON)
17740 cp_lexer_consume_token (parser->lexer);
17741 return;
17743 else
17744 if (declarator->kind == cdk_function)
17745 declarator->id_loc = token->location;
17746 /* Create the declaration. */
17747 decl = grokfield (declarator, &decl_specifiers,
17748 initializer, /*init_const_expr_p=*/true,
17749 asm_specification,
17750 attributes);
17753 /* Reset PREFIX_ATTRIBUTES. */
17754 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17755 attributes = TREE_CHAIN (attributes);
17756 if (attributes)
17757 TREE_CHAIN (attributes) = NULL_TREE;
17759 /* If there is any qualification still in effect, clear it
17760 now; we will be starting fresh with the next declarator. */
17761 parser->scope = NULL_TREE;
17762 parser->qualifying_scope = NULL_TREE;
17763 parser->object_scope = NULL_TREE;
17764 /* If it's a `,', then there are more declarators. */
17765 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17766 cp_lexer_consume_token (parser->lexer);
17767 /* If the next token isn't a `;', then we have a parse error. */
17768 else if (cp_lexer_next_token_is_not (parser->lexer,
17769 CPP_SEMICOLON))
17771 cp_parser_error (parser, "expected %<;%>");
17772 /* Skip tokens until we find a `;'. */
17773 cp_parser_skip_to_end_of_statement (parser);
17775 break;
17778 if (decl)
17780 /* Add DECL to the list of members. */
17781 if (!friend_p)
17782 finish_member_declaration (decl);
17784 if (TREE_CODE (decl) == FUNCTION_DECL)
17785 cp_parser_save_default_args (parser, decl);
17790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17793 /* Parse a pure-specifier.
17795 pure-specifier:
17798 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17799 Otherwise, ERROR_MARK_NODE is returned. */
17801 static tree
17802 cp_parser_pure_specifier (cp_parser* parser)
17804 cp_token *token;
17806 /* Look for the `=' token. */
17807 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17808 return error_mark_node;
17809 /* Look for the `0' token. */
17810 token = cp_lexer_peek_token (parser->lexer);
17812 if (token->type == CPP_EOF
17813 || token->type == CPP_PRAGMA_EOL)
17814 return error_mark_node;
17816 cp_lexer_consume_token (parser->lexer);
17818 /* Accept = default or = delete in c++0x mode. */
17819 if (token->keyword == RID_DEFAULT
17820 || token->keyword == RID_DELETE)
17822 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17823 return token->u.value;
17826 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17827 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17829 cp_parser_error (parser,
17830 "invalid pure specifier (only %<= 0%> is allowed)");
17831 cp_parser_skip_to_end_of_statement (parser);
17832 return error_mark_node;
17834 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17836 error_at (token->location, "templates may not be %<virtual%>");
17837 return error_mark_node;
17840 return integer_zero_node;
17843 /* Parse a constant-initializer.
17845 constant-initializer:
17846 = constant-expression
17848 Returns a representation of the constant-expression. */
17850 static tree
17851 cp_parser_constant_initializer (cp_parser* parser)
17853 /* Look for the `=' token. */
17854 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17855 return error_mark_node;
17857 /* It is invalid to write:
17859 struct S { static const int i = { 7 }; };
17862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17864 cp_parser_error (parser,
17865 "a brace-enclosed initializer is not allowed here");
17866 /* Consume the opening brace. */
17867 cp_lexer_consume_token (parser->lexer);
17868 /* Skip the initializer. */
17869 cp_parser_skip_to_closing_brace (parser);
17870 /* Look for the trailing `}'. */
17871 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17873 return error_mark_node;
17876 return cp_parser_constant_expression (parser,
17877 /*allow_non_constant=*/false,
17878 NULL);
17881 /* Derived classes [gram.class.derived] */
17883 /* Parse a base-clause.
17885 base-clause:
17886 : base-specifier-list
17888 base-specifier-list:
17889 base-specifier ... [opt]
17890 base-specifier-list , base-specifier ... [opt]
17892 Returns a TREE_LIST representing the base-classes, in the order in
17893 which they were declared. The representation of each node is as
17894 described by cp_parser_base_specifier.
17896 In the case that no bases are specified, this function will return
17897 NULL_TREE, not ERROR_MARK_NODE. */
17899 static tree
17900 cp_parser_base_clause (cp_parser* parser)
17902 tree bases = NULL_TREE;
17904 /* Look for the `:' that begins the list. */
17905 cp_parser_require (parser, CPP_COLON, RT_COLON);
17907 /* Scan the base-specifier-list. */
17908 while (true)
17910 cp_token *token;
17911 tree base;
17912 bool pack_expansion_p = false;
17914 /* Look for the base-specifier. */
17915 base = cp_parser_base_specifier (parser);
17916 /* Look for the (optional) ellipsis. */
17917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17919 /* Consume the `...'. */
17920 cp_lexer_consume_token (parser->lexer);
17922 pack_expansion_p = true;
17925 /* Add BASE to the front of the list. */
17926 if (base != error_mark_node)
17928 if (pack_expansion_p)
17929 /* Make this a pack expansion type. */
17930 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17933 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17935 TREE_CHAIN (base) = bases;
17936 bases = base;
17939 /* Peek at the next token. */
17940 token = cp_lexer_peek_token (parser->lexer);
17941 /* If it's not a comma, then the list is complete. */
17942 if (token->type != CPP_COMMA)
17943 break;
17944 /* Consume the `,'. */
17945 cp_lexer_consume_token (parser->lexer);
17948 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17949 base class had a qualified name. However, the next name that
17950 appears is certainly not qualified. */
17951 parser->scope = NULL_TREE;
17952 parser->qualifying_scope = NULL_TREE;
17953 parser->object_scope = NULL_TREE;
17955 return nreverse (bases);
17958 /* Parse a base-specifier.
17960 base-specifier:
17961 :: [opt] nested-name-specifier [opt] class-name
17962 virtual access-specifier [opt] :: [opt] nested-name-specifier
17963 [opt] class-name
17964 access-specifier virtual [opt] :: [opt] nested-name-specifier
17965 [opt] class-name
17967 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17968 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17969 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17970 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17972 static tree
17973 cp_parser_base_specifier (cp_parser* parser)
17975 cp_token *token;
17976 bool done = false;
17977 bool virtual_p = false;
17978 bool duplicate_virtual_error_issued_p = false;
17979 bool duplicate_access_error_issued_p = false;
17980 bool class_scope_p, template_p;
17981 tree access = access_default_node;
17982 tree type;
17984 /* Process the optional `virtual' and `access-specifier'. */
17985 while (!done)
17987 /* Peek at the next token. */
17988 token = cp_lexer_peek_token (parser->lexer);
17989 /* Process `virtual'. */
17990 switch (token->keyword)
17992 case RID_VIRTUAL:
17993 /* If `virtual' appears more than once, issue an error. */
17994 if (virtual_p && !duplicate_virtual_error_issued_p)
17996 cp_parser_error (parser,
17997 "%<virtual%> specified more than once in base-specified");
17998 duplicate_virtual_error_issued_p = true;
18001 virtual_p = true;
18003 /* Consume the `virtual' token. */
18004 cp_lexer_consume_token (parser->lexer);
18006 break;
18008 case RID_PUBLIC:
18009 case RID_PROTECTED:
18010 case RID_PRIVATE:
18011 /* If more than one access specifier appears, issue an
18012 error. */
18013 if (access != access_default_node
18014 && !duplicate_access_error_issued_p)
18016 cp_parser_error (parser,
18017 "more than one access specifier in base-specified");
18018 duplicate_access_error_issued_p = true;
18021 access = ridpointers[(int) token->keyword];
18023 /* Consume the access-specifier. */
18024 cp_lexer_consume_token (parser->lexer);
18026 break;
18028 default:
18029 done = true;
18030 break;
18033 /* It is not uncommon to see programs mechanically, erroneously, use
18034 the 'typename' keyword to denote (dependent) qualified types
18035 as base classes. */
18036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18038 token = cp_lexer_peek_token (parser->lexer);
18039 if (!processing_template_decl)
18040 error_at (token->location,
18041 "keyword %<typename%> not allowed outside of templates");
18042 else
18043 error_at (token->location,
18044 "keyword %<typename%> not allowed in this context "
18045 "(the base class is implicitly a type)");
18046 cp_lexer_consume_token (parser->lexer);
18049 /* Look for the optional `::' operator. */
18050 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18051 /* Look for the nested-name-specifier. The simplest way to
18052 implement:
18054 [temp.res]
18056 The keyword `typename' is not permitted in a base-specifier or
18057 mem-initializer; in these contexts a qualified name that
18058 depends on a template-parameter is implicitly assumed to be a
18059 type name.
18061 is to pretend that we have seen the `typename' keyword at this
18062 point. */
18063 cp_parser_nested_name_specifier_opt (parser,
18064 /*typename_keyword_p=*/true,
18065 /*check_dependency_p=*/true,
18066 typename_type,
18067 /*is_declaration=*/true);
18068 /* If the base class is given by a qualified name, assume that names
18069 we see are type names or templates, as appropriate. */
18070 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18071 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18073 /* Finally, look for the class-name. */
18074 type = cp_parser_class_name (parser,
18075 class_scope_p,
18076 template_p,
18077 typename_type,
18078 /*check_dependency_p=*/true,
18079 /*class_head_p=*/false,
18080 /*is_declaration=*/true);
18082 if (type == error_mark_node)
18083 return error_mark_node;
18085 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18088 /* Exception handling [gram.exception] */
18090 /* Parse an (optional) exception-specification.
18092 exception-specification:
18093 throw ( type-id-list [opt] )
18095 Returns a TREE_LIST representing the exception-specification. The
18096 TREE_VALUE of each node is a type. */
18098 static tree
18099 cp_parser_exception_specification_opt (cp_parser* parser)
18101 cp_token *token;
18102 tree type_id_list;
18103 const char *saved_message;
18105 /* Peek at the next token. */
18106 token = cp_lexer_peek_token (parser->lexer);
18108 /* Is it a noexcept-specification? */
18109 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18111 tree expr;
18112 cp_lexer_consume_token (parser->lexer);
18114 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18116 cp_lexer_consume_token (parser->lexer);
18118 /* Types may not be defined in an exception-specification. */
18119 saved_message = parser->type_definition_forbidden_message;
18120 parser->type_definition_forbidden_message
18121 = G_("types may not be defined in an exception-specification");
18123 expr = cp_parser_constant_expression (parser, false, NULL);
18125 /* Restore the saved message. */
18126 parser->type_definition_forbidden_message = saved_message;
18128 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18130 else
18131 expr = boolean_true_node;
18133 return build_noexcept_spec (expr, tf_warning_or_error);
18136 /* If it's not `throw', then there's no exception-specification. */
18137 if (!cp_parser_is_keyword (token, RID_THROW))
18138 return NULL_TREE;
18140 #if 0
18141 /* Enable this once a lot of code has transitioned to noexcept? */
18142 if (cxx_dialect == cxx0x && !in_system_header)
18143 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18144 "deprecated in C++0x; use %<noexcept%> instead.");
18145 #endif
18147 /* Consume the `throw'. */
18148 cp_lexer_consume_token (parser->lexer);
18150 /* Look for the `('. */
18151 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18153 /* Peek at the next token. */
18154 token = cp_lexer_peek_token (parser->lexer);
18155 /* If it's not a `)', then there is a type-id-list. */
18156 if (token->type != CPP_CLOSE_PAREN)
18158 /* Types may not be defined in an exception-specification. */
18159 saved_message = parser->type_definition_forbidden_message;
18160 parser->type_definition_forbidden_message
18161 = G_("types may not be defined in an exception-specification");
18162 /* Parse the type-id-list. */
18163 type_id_list = cp_parser_type_id_list (parser);
18164 /* Restore the saved message. */
18165 parser->type_definition_forbidden_message = saved_message;
18167 else
18168 type_id_list = empty_except_spec;
18170 /* Look for the `)'. */
18171 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18173 return type_id_list;
18176 /* Parse an (optional) type-id-list.
18178 type-id-list:
18179 type-id ... [opt]
18180 type-id-list , type-id ... [opt]
18182 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18183 in the order that the types were presented. */
18185 static tree
18186 cp_parser_type_id_list (cp_parser* parser)
18188 tree types = NULL_TREE;
18190 while (true)
18192 cp_token *token;
18193 tree type;
18195 /* Get the next type-id. */
18196 type = cp_parser_type_id (parser);
18197 /* Parse the optional ellipsis. */
18198 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18200 /* Consume the `...'. */
18201 cp_lexer_consume_token (parser->lexer);
18203 /* Turn the type into a pack expansion expression. */
18204 type = make_pack_expansion (type);
18206 /* Add it to the list. */
18207 types = add_exception_specifier (types, type, /*complain=*/1);
18208 /* Peek at the next token. */
18209 token = cp_lexer_peek_token (parser->lexer);
18210 /* If it is not a `,', we are done. */
18211 if (token->type != CPP_COMMA)
18212 break;
18213 /* Consume the `,'. */
18214 cp_lexer_consume_token (parser->lexer);
18217 return nreverse (types);
18220 /* Parse a try-block.
18222 try-block:
18223 try compound-statement handler-seq */
18225 static tree
18226 cp_parser_try_block (cp_parser* parser)
18228 tree try_block;
18230 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18231 try_block = begin_try_block ();
18232 cp_parser_compound_statement (parser, NULL, true);
18233 finish_try_block (try_block);
18234 cp_parser_handler_seq (parser);
18235 finish_handler_sequence (try_block);
18237 return try_block;
18240 /* Parse a function-try-block.
18242 function-try-block:
18243 try ctor-initializer [opt] function-body handler-seq */
18245 static bool
18246 cp_parser_function_try_block (cp_parser* parser)
18248 tree compound_stmt;
18249 tree try_block;
18250 bool ctor_initializer_p;
18252 /* Look for the `try' keyword. */
18253 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18254 return false;
18255 /* Let the rest of the front end know where we are. */
18256 try_block = begin_function_try_block (&compound_stmt);
18257 /* Parse the function-body. */
18258 ctor_initializer_p
18259 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18260 /* We're done with the `try' part. */
18261 finish_function_try_block (try_block);
18262 /* Parse the handlers. */
18263 cp_parser_handler_seq (parser);
18264 /* We're done with the handlers. */
18265 finish_function_handler_sequence (try_block, compound_stmt);
18267 return ctor_initializer_p;
18270 /* Parse a handler-seq.
18272 handler-seq:
18273 handler handler-seq [opt] */
18275 static void
18276 cp_parser_handler_seq (cp_parser* parser)
18278 while (true)
18280 cp_token *token;
18282 /* Parse the handler. */
18283 cp_parser_handler (parser);
18284 /* Peek at the next token. */
18285 token = cp_lexer_peek_token (parser->lexer);
18286 /* If it's not `catch' then there are no more handlers. */
18287 if (!cp_parser_is_keyword (token, RID_CATCH))
18288 break;
18292 /* Parse a handler.
18294 handler:
18295 catch ( exception-declaration ) compound-statement */
18297 static void
18298 cp_parser_handler (cp_parser* parser)
18300 tree handler;
18301 tree declaration;
18303 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18304 handler = begin_handler ();
18305 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18306 declaration = cp_parser_exception_declaration (parser);
18307 finish_handler_parms (declaration, handler);
18308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18309 cp_parser_compound_statement (parser, NULL, false);
18310 finish_handler (handler);
18313 /* Parse an exception-declaration.
18315 exception-declaration:
18316 type-specifier-seq declarator
18317 type-specifier-seq abstract-declarator
18318 type-specifier-seq
18321 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18322 ellipsis variant is used. */
18324 static tree
18325 cp_parser_exception_declaration (cp_parser* parser)
18327 cp_decl_specifier_seq type_specifiers;
18328 cp_declarator *declarator;
18329 const char *saved_message;
18331 /* If it's an ellipsis, it's easy to handle. */
18332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18334 /* Consume the `...' token. */
18335 cp_lexer_consume_token (parser->lexer);
18336 return NULL_TREE;
18339 /* Types may not be defined in exception-declarations. */
18340 saved_message = parser->type_definition_forbidden_message;
18341 parser->type_definition_forbidden_message
18342 = G_("types may not be defined in exception-declarations");
18344 /* Parse the type-specifier-seq. */
18345 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18346 /*is_trailing_return=*/false,
18347 &type_specifiers);
18348 /* If it's a `)', then there is no declarator. */
18349 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18350 declarator = NULL;
18351 else
18352 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18353 /*ctor_dtor_or_conv_p=*/NULL,
18354 /*parenthesized_p=*/NULL,
18355 /*member_p=*/false);
18357 /* Restore the saved message. */
18358 parser->type_definition_forbidden_message = saved_message;
18360 if (!type_specifiers.any_specifiers_p)
18361 return error_mark_node;
18363 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18366 /* Parse a throw-expression.
18368 throw-expression:
18369 throw assignment-expression [opt]
18371 Returns a THROW_EXPR representing the throw-expression. */
18373 static tree
18374 cp_parser_throw_expression (cp_parser* parser)
18376 tree expression;
18377 cp_token* token;
18379 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18380 token = cp_lexer_peek_token (parser->lexer);
18381 /* Figure out whether or not there is an assignment-expression
18382 following the "throw" keyword. */
18383 if (token->type == CPP_COMMA
18384 || token->type == CPP_SEMICOLON
18385 || token->type == CPP_CLOSE_PAREN
18386 || token->type == CPP_CLOSE_SQUARE
18387 || token->type == CPP_CLOSE_BRACE
18388 || token->type == CPP_COLON)
18389 expression = NULL_TREE;
18390 else
18391 expression = cp_parser_assignment_expression (parser,
18392 /*cast_p=*/false, NULL);
18394 return build_throw (expression);
18397 /* GNU Extensions */
18399 /* Parse an (optional) asm-specification.
18401 asm-specification:
18402 asm ( string-literal )
18404 If the asm-specification is present, returns a STRING_CST
18405 corresponding to the string-literal. Otherwise, returns
18406 NULL_TREE. */
18408 static tree
18409 cp_parser_asm_specification_opt (cp_parser* parser)
18411 cp_token *token;
18412 tree asm_specification;
18414 /* Peek at the next token. */
18415 token = cp_lexer_peek_token (parser->lexer);
18416 /* If the next token isn't the `asm' keyword, then there's no
18417 asm-specification. */
18418 if (!cp_parser_is_keyword (token, RID_ASM))
18419 return NULL_TREE;
18421 /* Consume the `asm' token. */
18422 cp_lexer_consume_token (parser->lexer);
18423 /* Look for the `('. */
18424 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18426 /* Look for the string-literal. */
18427 asm_specification = cp_parser_string_literal (parser, false, false);
18429 /* Look for the `)'. */
18430 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18432 return asm_specification;
18435 /* Parse an asm-operand-list.
18437 asm-operand-list:
18438 asm-operand
18439 asm-operand-list , asm-operand
18441 asm-operand:
18442 string-literal ( expression )
18443 [ string-literal ] string-literal ( expression )
18445 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18446 each node is the expression. The TREE_PURPOSE is itself a
18447 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18448 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18449 is a STRING_CST for the string literal before the parenthesis. Returns
18450 ERROR_MARK_NODE if any of the operands are invalid. */
18452 static tree
18453 cp_parser_asm_operand_list (cp_parser* parser)
18455 tree asm_operands = NULL_TREE;
18456 bool invalid_operands = false;
18458 while (true)
18460 tree string_literal;
18461 tree expression;
18462 tree name;
18464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18466 /* Consume the `[' token. */
18467 cp_lexer_consume_token (parser->lexer);
18468 /* Read the operand name. */
18469 name = cp_parser_identifier (parser);
18470 if (name != error_mark_node)
18471 name = build_string (IDENTIFIER_LENGTH (name),
18472 IDENTIFIER_POINTER (name));
18473 /* Look for the closing `]'. */
18474 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18476 else
18477 name = NULL_TREE;
18478 /* Look for the string-literal. */
18479 string_literal = cp_parser_string_literal (parser, false, false);
18481 /* Look for the `('. */
18482 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18483 /* Parse the expression. */
18484 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18485 /* Look for the `)'. */
18486 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18488 if (name == error_mark_node
18489 || string_literal == error_mark_node
18490 || expression == error_mark_node)
18491 invalid_operands = true;
18493 /* Add this operand to the list. */
18494 asm_operands = tree_cons (build_tree_list (name, string_literal),
18495 expression,
18496 asm_operands);
18497 /* If the next token is not a `,', there are no more
18498 operands. */
18499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18500 break;
18501 /* Consume the `,'. */
18502 cp_lexer_consume_token (parser->lexer);
18505 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18508 /* Parse an asm-clobber-list.
18510 asm-clobber-list:
18511 string-literal
18512 asm-clobber-list , string-literal
18514 Returns a TREE_LIST, indicating the clobbers in the order that they
18515 appeared. The TREE_VALUE of each node is a STRING_CST. */
18517 static tree
18518 cp_parser_asm_clobber_list (cp_parser* parser)
18520 tree clobbers = NULL_TREE;
18522 while (true)
18524 tree string_literal;
18526 /* Look for the string literal. */
18527 string_literal = cp_parser_string_literal (parser, false, false);
18528 /* Add it to the list. */
18529 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18530 /* If the next token is not a `,', then the list is
18531 complete. */
18532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18533 break;
18534 /* Consume the `,' token. */
18535 cp_lexer_consume_token (parser->lexer);
18538 return clobbers;
18541 /* Parse an asm-label-list.
18543 asm-label-list:
18544 identifier
18545 asm-label-list , identifier
18547 Returns a TREE_LIST, indicating the labels in the order that they
18548 appeared. The TREE_VALUE of each node is a label. */
18550 static tree
18551 cp_parser_asm_label_list (cp_parser* parser)
18553 tree labels = NULL_TREE;
18555 while (true)
18557 tree identifier, label, name;
18559 /* Look for the identifier. */
18560 identifier = cp_parser_identifier (parser);
18561 if (!error_operand_p (identifier))
18563 label = lookup_label (identifier);
18564 if (TREE_CODE (label) == LABEL_DECL)
18566 TREE_USED (label) = 1;
18567 check_goto (label);
18568 name = build_string (IDENTIFIER_LENGTH (identifier),
18569 IDENTIFIER_POINTER (identifier));
18570 labels = tree_cons (name, label, labels);
18573 /* If the next token is not a `,', then the list is
18574 complete. */
18575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18576 break;
18577 /* Consume the `,' token. */
18578 cp_lexer_consume_token (parser->lexer);
18581 return nreverse (labels);
18584 /* Parse an (optional) series of attributes.
18586 attributes:
18587 attributes attribute
18589 attribute:
18590 __attribute__ (( attribute-list [opt] ))
18592 The return value is as for cp_parser_attribute_list. */
18594 static tree
18595 cp_parser_attributes_opt (cp_parser* parser)
18597 tree attributes = NULL_TREE;
18599 while (true)
18601 cp_token *token;
18602 tree attribute_list;
18604 /* Peek at the next token. */
18605 token = cp_lexer_peek_token (parser->lexer);
18606 /* If it's not `__attribute__', then we're done. */
18607 if (token->keyword != RID_ATTRIBUTE)
18608 break;
18610 /* Consume the `__attribute__' keyword. */
18611 cp_lexer_consume_token (parser->lexer);
18612 /* Look for the two `(' tokens. */
18613 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18614 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18616 /* Peek at the next token. */
18617 token = cp_lexer_peek_token (parser->lexer);
18618 if (token->type != CPP_CLOSE_PAREN)
18619 /* Parse the attribute-list. */
18620 attribute_list = cp_parser_attribute_list (parser);
18621 else
18622 /* If the next token is a `)', then there is no attribute
18623 list. */
18624 attribute_list = NULL;
18626 /* Look for the two `)' tokens. */
18627 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18628 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18630 /* Add these new attributes to the list. */
18631 attributes = chainon (attributes, attribute_list);
18634 return attributes;
18637 /* Parse an attribute-list.
18639 attribute-list:
18640 attribute
18641 attribute-list , attribute
18643 attribute:
18644 identifier
18645 identifier ( identifier )
18646 identifier ( identifier , expression-list )
18647 identifier ( expression-list )
18649 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18650 to an attribute. The TREE_PURPOSE of each node is the identifier
18651 indicating which attribute is in use. The TREE_VALUE represents
18652 the arguments, if any. */
18654 static tree
18655 cp_parser_attribute_list (cp_parser* parser)
18657 tree attribute_list = NULL_TREE;
18658 bool save_translate_strings_p = parser->translate_strings_p;
18660 parser->translate_strings_p = false;
18661 while (true)
18663 cp_token *token;
18664 tree identifier;
18665 tree attribute;
18667 /* Look for the identifier. We also allow keywords here; for
18668 example `__attribute__ ((const))' is legal. */
18669 token = cp_lexer_peek_token (parser->lexer);
18670 if (token->type == CPP_NAME
18671 || token->type == CPP_KEYWORD)
18673 tree arguments = NULL_TREE;
18675 /* Consume the token. */
18676 token = cp_lexer_consume_token (parser->lexer);
18678 /* Save away the identifier that indicates which attribute
18679 this is. */
18680 identifier = (token->type == CPP_KEYWORD)
18681 /* For keywords, use the canonical spelling, not the
18682 parsed identifier. */
18683 ? ridpointers[(int) token->keyword]
18684 : token->u.value;
18686 attribute = build_tree_list (identifier, NULL_TREE);
18688 /* Peek at the next token. */
18689 token = cp_lexer_peek_token (parser->lexer);
18690 /* If it's an `(', then parse the attribute arguments. */
18691 if (token->type == CPP_OPEN_PAREN)
18693 VEC(tree,gc) *vec;
18694 int attr_flag = (attribute_takes_identifier_p (identifier)
18695 ? id_attr : normal_attr);
18696 vec = cp_parser_parenthesized_expression_list
18697 (parser, attr_flag, /*cast_p=*/false,
18698 /*allow_expansion_p=*/false,
18699 /*non_constant_p=*/NULL);
18700 if (vec == NULL)
18701 arguments = error_mark_node;
18702 else
18704 arguments = build_tree_list_vec (vec);
18705 release_tree_vector (vec);
18707 /* Save the arguments away. */
18708 TREE_VALUE (attribute) = arguments;
18711 if (arguments != error_mark_node)
18713 /* Add this attribute to the list. */
18714 TREE_CHAIN (attribute) = attribute_list;
18715 attribute_list = attribute;
18718 token = cp_lexer_peek_token (parser->lexer);
18720 /* Now, look for more attributes. If the next token isn't a
18721 `,', we're done. */
18722 if (token->type != CPP_COMMA)
18723 break;
18725 /* Consume the comma and keep going. */
18726 cp_lexer_consume_token (parser->lexer);
18728 parser->translate_strings_p = save_translate_strings_p;
18730 /* We built up the list in reverse order. */
18731 return nreverse (attribute_list);
18734 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18735 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18736 current value of the PEDANTIC flag, regardless of whether or not
18737 the `__extension__' keyword is present. The caller is responsible
18738 for restoring the value of the PEDANTIC flag. */
18740 static bool
18741 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18743 /* Save the old value of the PEDANTIC flag. */
18744 *saved_pedantic = pedantic;
18746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18748 /* Consume the `__extension__' token. */
18749 cp_lexer_consume_token (parser->lexer);
18750 /* We're not being pedantic while the `__extension__' keyword is
18751 in effect. */
18752 pedantic = 0;
18754 return true;
18757 return false;
18760 /* Parse a label declaration.
18762 label-declaration:
18763 __label__ label-declarator-seq ;
18765 label-declarator-seq:
18766 identifier , label-declarator-seq
18767 identifier */
18769 static void
18770 cp_parser_label_declaration (cp_parser* parser)
18772 /* Look for the `__label__' keyword. */
18773 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18775 while (true)
18777 tree identifier;
18779 /* Look for an identifier. */
18780 identifier = cp_parser_identifier (parser);
18781 /* If we failed, stop. */
18782 if (identifier == error_mark_node)
18783 break;
18784 /* Declare it as a label. */
18785 finish_label_decl (identifier);
18786 /* If the next token is a `;', stop. */
18787 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18788 break;
18789 /* Look for the `,' separating the label declarations. */
18790 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18793 /* Look for the final `;'. */
18794 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18797 /* Support Functions */
18799 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18800 NAME should have one of the representations used for an
18801 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18802 is returned. If PARSER->SCOPE is a dependent type, then a
18803 SCOPE_REF is returned.
18805 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18806 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18807 was formed. Abstractly, such entities should not be passed to this
18808 function, because they do not need to be looked up, but it is
18809 simpler to check for this special case here, rather than at the
18810 call-sites.
18812 In cases not explicitly covered above, this function returns a
18813 DECL, OVERLOAD, or baselink representing the result of the lookup.
18814 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18815 is returned.
18817 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18818 (e.g., "struct") that was used. In that case bindings that do not
18819 refer to types are ignored.
18821 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18822 ignored.
18824 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18825 are ignored.
18827 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18828 types.
18830 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18831 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18832 NULL_TREE otherwise. */
18834 static tree
18835 cp_parser_lookup_name (cp_parser *parser, tree name,
18836 enum tag_types tag_type,
18837 bool is_template,
18838 bool is_namespace,
18839 bool check_dependency,
18840 tree *ambiguous_decls,
18841 location_t name_location)
18843 int flags = 0;
18844 tree decl;
18845 tree object_type = parser->context->object_type;
18847 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18848 flags |= LOOKUP_COMPLAIN;
18850 /* Assume that the lookup will be unambiguous. */
18851 if (ambiguous_decls)
18852 *ambiguous_decls = NULL_TREE;
18854 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18855 no longer valid. Note that if we are parsing tentatively, and
18856 the parse fails, OBJECT_TYPE will be automatically restored. */
18857 parser->context->object_type = NULL_TREE;
18859 if (name == error_mark_node)
18860 return error_mark_node;
18862 /* A template-id has already been resolved; there is no lookup to
18863 do. */
18864 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18865 return name;
18866 if (BASELINK_P (name))
18868 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18869 == TEMPLATE_ID_EXPR);
18870 return name;
18873 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18874 it should already have been checked to make sure that the name
18875 used matches the type being destroyed. */
18876 if (TREE_CODE (name) == BIT_NOT_EXPR)
18878 tree type;
18880 /* Figure out to which type this destructor applies. */
18881 if (parser->scope)
18882 type = parser->scope;
18883 else if (object_type)
18884 type = object_type;
18885 else
18886 type = current_class_type;
18887 /* If that's not a class type, there is no destructor. */
18888 if (!type || !CLASS_TYPE_P (type))
18889 return error_mark_node;
18890 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18891 lazily_declare_fn (sfk_destructor, type);
18892 if (!CLASSTYPE_DESTRUCTORS (type))
18893 return error_mark_node;
18894 /* If it was a class type, return the destructor. */
18895 return CLASSTYPE_DESTRUCTORS (type);
18898 /* By this point, the NAME should be an ordinary identifier. If
18899 the id-expression was a qualified name, the qualifying scope is
18900 stored in PARSER->SCOPE at this point. */
18901 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18903 /* Perform the lookup. */
18904 if (parser->scope)
18906 bool dependent_p;
18908 if (parser->scope == error_mark_node)
18909 return error_mark_node;
18911 /* If the SCOPE is dependent, the lookup must be deferred until
18912 the template is instantiated -- unless we are explicitly
18913 looking up names in uninstantiated templates. Even then, we
18914 cannot look up the name if the scope is not a class type; it
18915 might, for example, be a template type parameter. */
18916 dependent_p = (TYPE_P (parser->scope)
18917 && dependent_scope_p (parser->scope));
18918 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18919 && dependent_p)
18920 /* Defer lookup. */
18921 decl = error_mark_node;
18922 else
18924 tree pushed_scope = NULL_TREE;
18926 /* If PARSER->SCOPE is a dependent type, then it must be a
18927 class type, and we must not be checking dependencies;
18928 otherwise, we would have processed this lookup above. So
18929 that PARSER->SCOPE is not considered a dependent base by
18930 lookup_member, we must enter the scope here. */
18931 if (dependent_p)
18932 pushed_scope = push_scope (parser->scope);
18934 /* If the PARSER->SCOPE is a template specialization, it
18935 may be instantiated during name lookup. In that case,
18936 errors may be issued. Even if we rollback the current
18937 tentative parse, those errors are valid. */
18938 decl = lookup_qualified_name (parser->scope, name,
18939 tag_type != none_type,
18940 /*complain=*/true);
18942 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18943 lookup result and the nested-name-specifier nominates a class C:
18944 * if the name specified after the nested-name-specifier, when
18945 looked up in C, is the injected-class-name of C (Clause 9), or
18946 * if the name specified after the nested-name-specifier is the
18947 same as the identifier or the simple-template-id's template-
18948 name in the last component of the nested-name-specifier,
18949 the name is instead considered to name the constructor of
18950 class C. [ Note: for example, the constructor is not an
18951 acceptable lookup result in an elaborated-type-specifier so
18952 the constructor would not be used in place of the
18953 injected-class-name. --end note ] Such a constructor name
18954 shall be used only in the declarator-id of a declaration that
18955 names a constructor or in a using-declaration. */
18956 if (tag_type == none_type
18957 && DECL_SELF_REFERENCE_P (decl)
18958 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18959 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18960 tag_type != none_type,
18961 /*complain=*/true);
18963 /* If we have a single function from a using decl, pull it out. */
18964 if (TREE_CODE (decl) == OVERLOAD
18965 && !really_overloaded_fn (decl))
18966 decl = OVL_FUNCTION (decl);
18968 if (pushed_scope)
18969 pop_scope (pushed_scope);
18972 /* If the scope is a dependent type and either we deferred lookup or
18973 we did lookup but didn't find the name, rememeber the name. */
18974 if (decl == error_mark_node && TYPE_P (parser->scope)
18975 && dependent_type_p (parser->scope))
18977 if (tag_type)
18979 tree type;
18981 /* The resolution to Core Issue 180 says that `struct
18982 A::B' should be considered a type-name, even if `A'
18983 is dependent. */
18984 type = make_typename_type (parser->scope, name, tag_type,
18985 /*complain=*/tf_error);
18986 decl = TYPE_NAME (type);
18988 else if (is_template
18989 && (cp_parser_next_token_ends_template_argument_p (parser)
18990 || cp_lexer_next_token_is (parser->lexer,
18991 CPP_CLOSE_PAREN)))
18992 decl = make_unbound_class_template (parser->scope,
18993 name, NULL_TREE,
18994 /*complain=*/tf_error);
18995 else
18996 decl = build_qualified_name (/*type=*/NULL_TREE,
18997 parser->scope, name,
18998 is_template);
19000 parser->qualifying_scope = parser->scope;
19001 parser->object_scope = NULL_TREE;
19003 else if (object_type)
19005 tree object_decl = NULL_TREE;
19006 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19007 OBJECT_TYPE is not a class. */
19008 if (CLASS_TYPE_P (object_type))
19009 /* If the OBJECT_TYPE is a template specialization, it may
19010 be instantiated during name lookup. In that case, errors
19011 may be issued. Even if we rollback the current tentative
19012 parse, those errors are valid. */
19013 object_decl = lookup_member (object_type,
19014 name,
19015 /*protect=*/0,
19016 tag_type != none_type);
19017 /* Look it up in the enclosing context, too. */
19018 decl = lookup_name_real (name, tag_type != none_type,
19019 /*nonclass=*/0,
19020 /*block_p=*/true, is_namespace, flags);
19021 parser->object_scope = object_type;
19022 parser->qualifying_scope = NULL_TREE;
19023 if (object_decl)
19024 decl = object_decl;
19026 else
19028 decl = lookup_name_real (name, tag_type != none_type,
19029 /*nonclass=*/0,
19030 /*block_p=*/true, is_namespace, flags);
19031 parser->qualifying_scope = NULL_TREE;
19032 parser->object_scope = NULL_TREE;
19035 /* If the lookup failed, let our caller know. */
19036 if (!decl || decl == error_mark_node)
19037 return error_mark_node;
19039 /* Pull out the template from an injected-class-name (or multiple). */
19040 if (is_template)
19041 decl = maybe_get_template_decl_from_type_decl (decl);
19043 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19044 if (TREE_CODE (decl) == TREE_LIST)
19046 if (ambiguous_decls)
19047 *ambiguous_decls = decl;
19048 /* The error message we have to print is too complicated for
19049 cp_parser_error, so we incorporate its actions directly. */
19050 if (!cp_parser_simulate_error (parser))
19052 error_at (name_location, "reference to %qD is ambiguous",
19053 name);
19054 print_candidates (decl);
19056 return error_mark_node;
19059 gcc_assert (DECL_P (decl)
19060 || TREE_CODE (decl) == OVERLOAD
19061 || TREE_CODE (decl) == SCOPE_REF
19062 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19063 || BASELINK_P (decl));
19065 /* If we have resolved the name of a member declaration, check to
19066 see if the declaration is accessible. When the name resolves to
19067 set of overloaded functions, accessibility is checked when
19068 overload resolution is done.
19070 During an explicit instantiation, access is not checked at all,
19071 as per [temp.explicit]. */
19072 if (DECL_P (decl))
19073 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19075 return decl;
19078 /* Like cp_parser_lookup_name, but for use in the typical case where
19079 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19080 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19082 static tree
19083 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19085 return cp_parser_lookup_name (parser, name,
19086 none_type,
19087 /*is_template=*/false,
19088 /*is_namespace=*/false,
19089 /*check_dependency=*/true,
19090 /*ambiguous_decls=*/NULL,
19091 location);
19094 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19095 the current context, return the TYPE_DECL. If TAG_NAME_P is
19096 true, the DECL indicates the class being defined in a class-head,
19097 or declared in an elaborated-type-specifier.
19099 Otherwise, return DECL. */
19101 static tree
19102 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19104 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19105 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19107 struct A {
19108 template <typename T> struct B;
19111 template <typename T> struct A::B {};
19113 Similarly, in an elaborated-type-specifier:
19115 namespace N { struct X{}; }
19117 struct A {
19118 template <typename T> friend struct N::X;
19121 However, if the DECL refers to a class type, and we are in
19122 the scope of the class, then the name lookup automatically
19123 finds the TYPE_DECL created by build_self_reference rather
19124 than a TEMPLATE_DECL. For example, in:
19126 template <class T> struct S {
19127 S s;
19130 there is no need to handle such case. */
19132 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19133 return DECL_TEMPLATE_RESULT (decl);
19135 return decl;
19138 /* If too many, or too few, template-parameter lists apply to the
19139 declarator, issue an error message. Returns TRUE if all went well,
19140 and FALSE otherwise. */
19142 static bool
19143 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19144 cp_declarator *declarator,
19145 location_t declarator_location)
19147 unsigned num_templates;
19149 /* We haven't seen any classes that involve template parameters yet. */
19150 num_templates = 0;
19152 switch (declarator->kind)
19154 case cdk_id:
19155 if (declarator->u.id.qualifying_scope)
19157 tree scope;
19159 scope = declarator->u.id.qualifying_scope;
19161 while (scope && CLASS_TYPE_P (scope))
19163 /* You're supposed to have one `template <...>'
19164 for every template class, but you don't need one
19165 for a full specialization. For example:
19167 template <class T> struct S{};
19168 template <> struct S<int> { void f(); };
19169 void S<int>::f () {}
19171 is correct; there shouldn't be a `template <>' for
19172 the definition of `S<int>::f'. */
19173 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19174 /* If SCOPE does not have template information of any
19175 kind, then it is not a template, nor is it nested
19176 within a template. */
19177 break;
19178 if (explicit_class_specialization_p (scope))
19179 break;
19180 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19181 ++num_templates;
19183 scope = TYPE_CONTEXT (scope);
19186 else if (TREE_CODE (declarator->u.id.unqualified_name)
19187 == TEMPLATE_ID_EXPR)
19188 /* If the DECLARATOR has the form `X<y>' then it uses one
19189 additional level of template parameters. */
19190 ++num_templates;
19192 return cp_parser_check_template_parameters
19193 (parser, num_templates, declarator_location, declarator);
19196 case cdk_function:
19197 case cdk_array:
19198 case cdk_pointer:
19199 case cdk_reference:
19200 case cdk_ptrmem:
19201 return (cp_parser_check_declarator_template_parameters
19202 (parser, declarator->declarator, declarator_location));
19204 case cdk_error:
19205 return true;
19207 default:
19208 gcc_unreachable ();
19210 return false;
19213 /* NUM_TEMPLATES were used in the current declaration. If that is
19214 invalid, return FALSE and issue an error messages. Otherwise,
19215 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19216 declarator and we can print more accurate diagnostics. */
19218 static bool
19219 cp_parser_check_template_parameters (cp_parser* parser,
19220 unsigned num_templates,
19221 location_t location,
19222 cp_declarator *declarator)
19224 /* If there are the same number of template classes and parameter
19225 lists, that's OK. */
19226 if (parser->num_template_parameter_lists == num_templates)
19227 return true;
19228 /* If there are more, but only one more, then we are referring to a
19229 member template. That's OK too. */
19230 if (parser->num_template_parameter_lists == num_templates + 1)
19231 return true;
19232 /* If there are more template classes than parameter lists, we have
19233 something like:
19235 template <class T> void S<T>::R<T>::f (); */
19236 if (parser->num_template_parameter_lists < num_templates)
19238 if (declarator && !current_function_decl)
19239 error_at (location, "specializing member %<%T::%E%> "
19240 "requires %<template<>%> syntax",
19241 declarator->u.id.qualifying_scope,
19242 declarator->u.id.unqualified_name);
19243 else if (declarator)
19244 error_at (location, "invalid declaration of %<%T::%E%>",
19245 declarator->u.id.qualifying_scope,
19246 declarator->u.id.unqualified_name);
19247 else
19248 error_at (location, "too few template-parameter-lists");
19249 return false;
19251 /* Otherwise, there are too many template parameter lists. We have
19252 something like:
19254 template <class T> template <class U> void S::f(); */
19255 error_at (location, "too many template-parameter-lists");
19256 return false;
19259 /* Parse an optional `::' token indicating that the following name is
19260 from the global namespace. If so, PARSER->SCOPE is set to the
19261 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19262 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19263 Returns the new value of PARSER->SCOPE, if the `::' token is
19264 present, and NULL_TREE otherwise. */
19266 static tree
19267 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19269 cp_token *token;
19271 /* Peek at the next token. */
19272 token = cp_lexer_peek_token (parser->lexer);
19273 /* If we're looking at a `::' token then we're starting from the
19274 global namespace, not our current location. */
19275 if (token->type == CPP_SCOPE)
19277 /* Consume the `::' token. */
19278 cp_lexer_consume_token (parser->lexer);
19279 /* Set the SCOPE so that we know where to start the lookup. */
19280 parser->scope = global_namespace;
19281 parser->qualifying_scope = global_namespace;
19282 parser->object_scope = NULL_TREE;
19284 return parser->scope;
19286 else if (!current_scope_valid_p)
19288 parser->scope = NULL_TREE;
19289 parser->qualifying_scope = NULL_TREE;
19290 parser->object_scope = NULL_TREE;
19293 return NULL_TREE;
19296 /* Returns TRUE if the upcoming token sequence is the start of a
19297 constructor declarator. If FRIEND_P is true, the declarator is
19298 preceded by the `friend' specifier. */
19300 static bool
19301 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19303 bool constructor_p;
19304 tree nested_name_specifier;
19305 cp_token *next_token;
19307 /* The common case is that this is not a constructor declarator, so
19308 try to avoid doing lots of work if at all possible. It's not
19309 valid declare a constructor at function scope. */
19310 if (parser->in_function_body)
19311 return false;
19312 /* And only certain tokens can begin a constructor declarator. */
19313 next_token = cp_lexer_peek_token (parser->lexer);
19314 if (next_token->type != CPP_NAME
19315 && next_token->type != CPP_SCOPE
19316 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19317 && next_token->type != CPP_TEMPLATE_ID)
19318 return false;
19320 /* Parse tentatively; we are going to roll back all of the tokens
19321 consumed here. */
19322 cp_parser_parse_tentatively (parser);
19323 /* Assume that we are looking at a constructor declarator. */
19324 constructor_p = true;
19326 /* Look for the optional `::' operator. */
19327 cp_parser_global_scope_opt (parser,
19328 /*current_scope_valid_p=*/false);
19329 /* Look for the nested-name-specifier. */
19330 nested_name_specifier
19331 = (cp_parser_nested_name_specifier_opt (parser,
19332 /*typename_keyword_p=*/false,
19333 /*check_dependency_p=*/false,
19334 /*type_p=*/false,
19335 /*is_declaration=*/false));
19336 /* Outside of a class-specifier, there must be a
19337 nested-name-specifier. */
19338 if (!nested_name_specifier &&
19339 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19340 || friend_p))
19341 constructor_p = false;
19342 else if (nested_name_specifier == error_mark_node)
19343 constructor_p = false;
19345 /* If we have a class scope, this is easy; DR 147 says that S::S always
19346 names the constructor, and no other qualified name could. */
19347 if (constructor_p && nested_name_specifier
19348 && TYPE_P (nested_name_specifier))
19350 tree id = cp_parser_unqualified_id (parser,
19351 /*template_keyword_p=*/false,
19352 /*check_dependency_p=*/false,
19353 /*declarator_p=*/true,
19354 /*optional_p=*/false);
19355 if (is_overloaded_fn (id))
19356 id = DECL_NAME (get_first_fn (id));
19357 if (!constructor_name_p (id, nested_name_specifier))
19358 constructor_p = false;
19360 /* If we still think that this might be a constructor-declarator,
19361 look for a class-name. */
19362 else if (constructor_p)
19364 /* If we have:
19366 template <typename T> struct S {
19367 S();
19370 we must recognize that the nested `S' names a class. */
19371 tree type_decl;
19372 type_decl = cp_parser_class_name (parser,
19373 /*typename_keyword_p=*/false,
19374 /*template_keyword_p=*/false,
19375 none_type,
19376 /*check_dependency_p=*/false,
19377 /*class_head_p=*/false,
19378 /*is_declaration=*/false);
19379 /* If there was no class-name, then this is not a constructor. */
19380 constructor_p = !cp_parser_error_occurred (parser);
19382 /* If we're still considering a constructor, we have to see a `(',
19383 to begin the parameter-declaration-clause, followed by either a
19384 `)', an `...', or a decl-specifier. We need to check for a
19385 type-specifier to avoid being fooled into thinking that:
19387 S (f) (int);
19389 is a constructor. (It is actually a function named `f' that
19390 takes one parameter (of type `int') and returns a value of type
19391 `S'. */
19392 if (constructor_p
19393 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19394 constructor_p = false;
19396 if (constructor_p
19397 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19398 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19399 /* A parameter declaration begins with a decl-specifier,
19400 which is either the "attribute" keyword, a storage class
19401 specifier, or (usually) a type-specifier. */
19402 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19404 tree type;
19405 tree pushed_scope = NULL_TREE;
19406 unsigned saved_num_template_parameter_lists;
19408 /* Names appearing in the type-specifier should be looked up
19409 in the scope of the class. */
19410 if (current_class_type)
19411 type = NULL_TREE;
19412 else
19414 type = TREE_TYPE (type_decl);
19415 if (TREE_CODE (type) == TYPENAME_TYPE)
19417 type = resolve_typename_type (type,
19418 /*only_current_p=*/false);
19419 if (TREE_CODE (type) == TYPENAME_TYPE)
19421 cp_parser_abort_tentative_parse (parser);
19422 return false;
19425 pushed_scope = push_scope (type);
19428 /* Inside the constructor parameter list, surrounding
19429 template-parameter-lists do not apply. */
19430 saved_num_template_parameter_lists
19431 = parser->num_template_parameter_lists;
19432 parser->num_template_parameter_lists = 0;
19434 /* Look for the type-specifier. */
19435 cp_parser_type_specifier (parser,
19436 CP_PARSER_FLAGS_NONE,
19437 /*decl_specs=*/NULL,
19438 /*is_declarator=*/true,
19439 /*declares_class_or_enum=*/NULL,
19440 /*is_cv_qualifier=*/NULL);
19442 parser->num_template_parameter_lists
19443 = saved_num_template_parameter_lists;
19445 /* Leave the scope of the class. */
19446 if (pushed_scope)
19447 pop_scope (pushed_scope);
19449 constructor_p = !cp_parser_error_occurred (parser);
19453 /* We did not really want to consume any tokens. */
19454 cp_parser_abort_tentative_parse (parser);
19456 return constructor_p;
19459 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19460 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19461 they must be performed once we are in the scope of the function.
19463 Returns the function defined. */
19465 static tree
19466 cp_parser_function_definition_from_specifiers_and_declarator
19467 (cp_parser* parser,
19468 cp_decl_specifier_seq *decl_specifiers,
19469 tree attributes,
19470 const cp_declarator *declarator)
19472 tree fn;
19473 bool success_p;
19475 /* Begin the function-definition. */
19476 success_p = start_function (decl_specifiers, declarator, attributes);
19478 /* The things we're about to see are not directly qualified by any
19479 template headers we've seen thus far. */
19480 reset_specialization ();
19482 /* If there were names looked up in the decl-specifier-seq that we
19483 did not check, check them now. We must wait until we are in the
19484 scope of the function to perform the checks, since the function
19485 might be a friend. */
19486 perform_deferred_access_checks ();
19488 if (!success_p)
19490 /* Skip the entire function. */
19491 cp_parser_skip_to_end_of_block_or_statement (parser);
19492 fn = error_mark_node;
19494 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19496 /* Seen already, skip it. An error message has already been output. */
19497 cp_parser_skip_to_end_of_block_or_statement (parser);
19498 fn = current_function_decl;
19499 current_function_decl = NULL_TREE;
19500 /* If this is a function from a class, pop the nested class. */
19501 if (current_class_name)
19502 pop_nested_class ();
19504 else
19505 fn = cp_parser_function_definition_after_declarator (parser,
19506 /*inline_p=*/false);
19508 return fn;
19511 /* Parse the part of a function-definition that follows the
19512 declarator. INLINE_P is TRUE iff this function is an inline
19513 function defined within a class-specifier.
19515 Returns the function defined. */
19517 static tree
19518 cp_parser_function_definition_after_declarator (cp_parser* parser,
19519 bool inline_p)
19521 tree fn;
19522 bool ctor_initializer_p = false;
19523 bool saved_in_unbraced_linkage_specification_p;
19524 bool saved_in_function_body;
19525 unsigned saved_num_template_parameter_lists;
19526 cp_token *token;
19528 saved_in_function_body = parser->in_function_body;
19529 parser->in_function_body = true;
19530 /* If the next token is `return', then the code may be trying to
19531 make use of the "named return value" extension that G++ used to
19532 support. */
19533 token = cp_lexer_peek_token (parser->lexer);
19534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19536 /* Consume the `return' keyword. */
19537 cp_lexer_consume_token (parser->lexer);
19538 /* Look for the identifier that indicates what value is to be
19539 returned. */
19540 cp_parser_identifier (parser);
19541 /* Issue an error message. */
19542 error_at (token->location,
19543 "named return values are no longer supported");
19544 /* Skip tokens until we reach the start of the function body. */
19545 while (true)
19547 cp_token *token = cp_lexer_peek_token (parser->lexer);
19548 if (token->type == CPP_OPEN_BRACE
19549 || token->type == CPP_EOF
19550 || token->type == CPP_PRAGMA_EOL)
19551 break;
19552 cp_lexer_consume_token (parser->lexer);
19555 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19556 anything declared inside `f'. */
19557 saved_in_unbraced_linkage_specification_p
19558 = parser->in_unbraced_linkage_specification_p;
19559 parser->in_unbraced_linkage_specification_p = false;
19560 /* Inside the function, surrounding template-parameter-lists do not
19561 apply. */
19562 saved_num_template_parameter_lists
19563 = parser->num_template_parameter_lists;
19564 parser->num_template_parameter_lists = 0;
19566 start_lambda_scope (current_function_decl);
19568 /* If the next token is `try', then we are looking at a
19569 function-try-block. */
19570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19571 ctor_initializer_p = cp_parser_function_try_block (parser);
19572 /* A function-try-block includes the function-body, so we only do
19573 this next part if we're not processing a function-try-block. */
19574 else
19575 ctor_initializer_p
19576 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19578 finish_lambda_scope ();
19580 /* Finish the function. */
19581 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19582 (inline_p ? 2 : 0));
19583 /* Generate code for it, if necessary. */
19584 expand_or_defer_fn (fn);
19585 /* Restore the saved values. */
19586 parser->in_unbraced_linkage_specification_p
19587 = saved_in_unbraced_linkage_specification_p;
19588 parser->num_template_parameter_lists
19589 = saved_num_template_parameter_lists;
19590 parser->in_function_body = saved_in_function_body;
19592 return fn;
19595 /* Parse a template-declaration, assuming that the `export' (and
19596 `extern') keywords, if present, has already been scanned. MEMBER_P
19597 is as for cp_parser_template_declaration. */
19599 static void
19600 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19602 tree decl = NULL_TREE;
19603 VEC (deferred_access_check,gc) *checks;
19604 tree parameter_list;
19605 bool friend_p = false;
19606 bool need_lang_pop;
19607 cp_token *token;
19609 /* Look for the `template' keyword. */
19610 token = cp_lexer_peek_token (parser->lexer);
19611 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19612 return;
19614 /* And the `<'. */
19615 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19616 return;
19617 if (at_class_scope_p () && current_function_decl)
19619 /* 14.5.2.2 [temp.mem]
19621 A local class shall not have member templates. */
19622 error_at (token->location,
19623 "invalid declaration of member template in local class");
19624 cp_parser_skip_to_end_of_block_or_statement (parser);
19625 return;
19627 /* [temp]
19629 A template ... shall not have C linkage. */
19630 if (current_lang_name == lang_name_c)
19632 error_at (token->location, "template with C linkage");
19633 /* Give it C++ linkage to avoid confusing other parts of the
19634 front end. */
19635 push_lang_context (lang_name_cplusplus);
19636 need_lang_pop = true;
19638 else
19639 need_lang_pop = false;
19641 /* We cannot perform access checks on the template parameter
19642 declarations until we know what is being declared, just as we
19643 cannot check the decl-specifier list. */
19644 push_deferring_access_checks (dk_deferred);
19646 /* If the next token is `>', then we have an invalid
19647 specialization. Rather than complain about an invalid template
19648 parameter, issue an error message here. */
19649 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19651 cp_parser_error (parser, "invalid explicit specialization");
19652 begin_specialization ();
19653 parameter_list = NULL_TREE;
19655 else
19656 /* Parse the template parameters. */
19657 parameter_list = cp_parser_template_parameter_list (parser);
19659 /* Get the deferred access checks from the parameter list. These
19660 will be checked once we know what is being declared, as for a
19661 member template the checks must be performed in the scope of the
19662 class containing the member. */
19663 checks = get_deferred_access_checks ();
19665 /* Look for the `>'. */
19666 cp_parser_skip_to_end_of_template_parameter_list (parser);
19667 /* We just processed one more parameter list. */
19668 ++parser->num_template_parameter_lists;
19669 /* If the next token is `template', there are more template
19670 parameters. */
19671 if (cp_lexer_next_token_is_keyword (parser->lexer,
19672 RID_TEMPLATE))
19673 cp_parser_template_declaration_after_export (parser, member_p);
19674 else
19676 /* There are no access checks when parsing a template, as we do not
19677 know if a specialization will be a friend. */
19678 push_deferring_access_checks (dk_no_check);
19679 token = cp_lexer_peek_token (parser->lexer);
19680 decl = cp_parser_single_declaration (parser,
19681 checks,
19682 member_p,
19683 /*explicit_specialization_p=*/false,
19684 &friend_p);
19685 pop_deferring_access_checks ();
19687 /* If this is a member template declaration, let the front
19688 end know. */
19689 if (member_p && !friend_p && decl)
19691 if (TREE_CODE (decl) == TYPE_DECL)
19692 cp_parser_check_access_in_redeclaration (decl, token->location);
19694 decl = finish_member_template_decl (decl);
19696 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19697 make_friend_class (current_class_type, TREE_TYPE (decl),
19698 /*complain=*/true);
19700 /* We are done with the current parameter list. */
19701 --parser->num_template_parameter_lists;
19703 pop_deferring_access_checks ();
19705 /* Finish up. */
19706 finish_template_decl (parameter_list);
19708 /* Register member declarations. */
19709 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19710 finish_member_declaration (decl);
19711 /* For the erroneous case of a template with C linkage, we pushed an
19712 implicit C++ linkage scope; exit that scope now. */
19713 if (need_lang_pop)
19714 pop_lang_context ();
19715 /* If DECL is a function template, we must return to parse it later.
19716 (Even though there is no definition, there might be default
19717 arguments that need handling.) */
19718 if (member_p && decl
19719 && (TREE_CODE (decl) == FUNCTION_DECL
19720 || DECL_FUNCTION_TEMPLATE_P (decl)))
19721 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19724 /* Perform the deferred access checks from a template-parameter-list.
19725 CHECKS is a TREE_LIST of access checks, as returned by
19726 get_deferred_access_checks. */
19728 static void
19729 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19731 ++processing_template_parmlist;
19732 perform_access_checks (checks);
19733 --processing_template_parmlist;
19736 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19737 `function-definition' sequence. MEMBER_P is true, this declaration
19738 appears in a class scope.
19740 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19741 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19743 static tree
19744 cp_parser_single_declaration (cp_parser* parser,
19745 VEC (deferred_access_check,gc)* checks,
19746 bool member_p,
19747 bool explicit_specialization_p,
19748 bool* friend_p)
19750 int declares_class_or_enum;
19751 tree decl = NULL_TREE;
19752 cp_decl_specifier_seq decl_specifiers;
19753 bool function_definition_p = false;
19754 cp_token *decl_spec_token_start;
19756 /* This function is only used when processing a template
19757 declaration. */
19758 gcc_assert (innermost_scope_kind () == sk_template_parms
19759 || innermost_scope_kind () == sk_template_spec);
19761 /* Defer access checks until we know what is being declared. */
19762 push_deferring_access_checks (dk_deferred);
19764 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19765 alternative. */
19766 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19767 cp_parser_decl_specifier_seq (parser,
19768 CP_PARSER_FLAGS_OPTIONAL,
19769 &decl_specifiers,
19770 &declares_class_or_enum);
19771 if (friend_p)
19772 *friend_p = cp_parser_friend_p (&decl_specifiers);
19774 /* There are no template typedefs. */
19775 if (decl_specifiers.specs[(int) ds_typedef])
19777 error_at (decl_spec_token_start->location,
19778 "template declaration of %<typedef%>");
19779 decl = error_mark_node;
19782 /* Gather up the access checks that occurred the
19783 decl-specifier-seq. */
19784 stop_deferring_access_checks ();
19786 /* Check for the declaration of a template class. */
19787 if (declares_class_or_enum)
19789 if (cp_parser_declares_only_class_p (parser))
19791 decl = shadow_tag (&decl_specifiers);
19793 /* In this case:
19795 struct C {
19796 friend template <typename T> struct A<T>::B;
19799 A<T>::B will be represented by a TYPENAME_TYPE, and
19800 therefore not recognized by shadow_tag. */
19801 if (friend_p && *friend_p
19802 && !decl
19803 && decl_specifiers.type
19804 && TYPE_P (decl_specifiers.type))
19805 decl = decl_specifiers.type;
19807 if (decl && decl != error_mark_node)
19808 decl = TYPE_NAME (decl);
19809 else
19810 decl = error_mark_node;
19812 /* Perform access checks for template parameters. */
19813 cp_parser_perform_template_parameter_access_checks (checks);
19817 /* Complain about missing 'typename' or other invalid type names. */
19818 if (!decl_specifiers.any_type_specifiers_p)
19819 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19821 /* If it's not a template class, try for a template function. If
19822 the next token is a `;', then this declaration does not declare
19823 anything. But, if there were errors in the decl-specifiers, then
19824 the error might well have come from an attempted class-specifier.
19825 In that case, there's no need to warn about a missing declarator. */
19826 if (!decl
19827 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19828 || decl_specifiers.type != error_mark_node))
19830 decl = cp_parser_init_declarator (parser,
19831 &decl_specifiers,
19832 checks,
19833 /*function_definition_allowed_p=*/true,
19834 member_p,
19835 declares_class_or_enum,
19836 &function_definition_p);
19838 /* 7.1.1-1 [dcl.stc]
19840 A storage-class-specifier shall not be specified in an explicit
19841 specialization... */
19842 if (decl
19843 && explicit_specialization_p
19844 && decl_specifiers.storage_class != sc_none)
19846 error_at (decl_spec_token_start->location,
19847 "explicit template specialization cannot have a storage class");
19848 decl = error_mark_node;
19852 pop_deferring_access_checks ();
19854 /* Clear any current qualification; whatever comes next is the start
19855 of something new. */
19856 parser->scope = NULL_TREE;
19857 parser->qualifying_scope = NULL_TREE;
19858 parser->object_scope = NULL_TREE;
19859 /* Look for a trailing `;' after the declaration. */
19860 if (!function_definition_p
19861 && (decl == error_mark_node
19862 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19863 cp_parser_skip_to_end_of_block_or_statement (parser);
19865 return decl;
19868 /* Parse a cast-expression that is not the operand of a unary "&". */
19870 static tree
19871 cp_parser_simple_cast_expression (cp_parser *parser)
19873 return cp_parser_cast_expression (parser, /*address_p=*/false,
19874 /*cast_p=*/false, NULL);
19877 /* Parse a functional cast to TYPE. Returns an expression
19878 representing the cast. */
19880 static tree
19881 cp_parser_functional_cast (cp_parser* parser, tree type)
19883 VEC(tree,gc) *vec;
19884 tree expression_list;
19885 tree cast;
19886 bool nonconst_p;
19888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19891 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19892 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19893 if (TREE_CODE (type) == TYPE_DECL)
19894 type = TREE_TYPE (type);
19895 return finish_compound_literal (type, expression_list);
19899 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19900 /*cast_p=*/true,
19901 /*allow_expansion_p=*/true,
19902 /*non_constant_p=*/NULL);
19903 if (vec == NULL)
19904 expression_list = error_mark_node;
19905 else
19907 expression_list = build_tree_list_vec (vec);
19908 release_tree_vector (vec);
19911 cast = build_functional_cast (type, expression_list,
19912 tf_warning_or_error);
19913 /* [expr.const]/1: In an integral constant expression "only type
19914 conversions to integral or enumeration type can be used". */
19915 if (TREE_CODE (type) == TYPE_DECL)
19916 type = TREE_TYPE (type);
19917 if (cast != error_mark_node
19918 && !cast_valid_in_integral_constant_expression_p (type)
19919 && cp_parser_non_integral_constant_expression (parser,
19920 NIC_CONSTRUCTOR))
19921 return error_mark_node;
19922 return cast;
19925 /* Save the tokens that make up the body of a member function defined
19926 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19927 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19928 specifiers applied to the declaration. Returns the FUNCTION_DECL
19929 for the member function. */
19931 static tree
19932 cp_parser_save_member_function_body (cp_parser* parser,
19933 cp_decl_specifier_seq *decl_specifiers,
19934 cp_declarator *declarator,
19935 tree attributes)
19937 cp_token *first;
19938 cp_token *last;
19939 tree fn;
19941 /* Create the FUNCTION_DECL. */
19942 fn = grokmethod (decl_specifiers, declarator, attributes);
19943 /* If something went badly wrong, bail out now. */
19944 if (fn == error_mark_node)
19946 /* If there's a function-body, skip it. */
19947 if (cp_parser_token_starts_function_definition_p
19948 (cp_lexer_peek_token (parser->lexer)))
19949 cp_parser_skip_to_end_of_block_or_statement (parser);
19950 return error_mark_node;
19953 /* Remember it, if there default args to post process. */
19954 cp_parser_save_default_args (parser, fn);
19956 /* Save away the tokens that make up the body of the
19957 function. */
19958 first = parser->lexer->next_token;
19959 /* We can have braced-init-list mem-initializers before the fn body. */
19960 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19962 cp_lexer_consume_token (parser->lexer);
19963 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19964 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19966 /* cache_group will stop after an un-nested { } pair, too. */
19967 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19968 break;
19970 /* variadic mem-inits have ... after the ')'. */
19971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19972 cp_lexer_consume_token (parser->lexer);
19975 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19976 /* Handle function try blocks. */
19977 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19978 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19979 last = parser->lexer->next_token;
19981 /* Save away the inline definition; we will process it when the
19982 class is complete. */
19983 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19984 DECL_PENDING_INLINE_P (fn) = 1;
19986 /* We need to know that this was defined in the class, so that
19987 friend templates are handled correctly. */
19988 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19990 /* Add FN to the queue of functions to be parsed later. */
19991 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19993 return fn;
19996 /* Parse a template-argument-list, as well as the trailing ">" (but
19997 not the opening ">"). See cp_parser_template_argument_list for the
19998 return value. */
20000 static tree
20001 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20003 tree arguments;
20004 tree saved_scope;
20005 tree saved_qualifying_scope;
20006 tree saved_object_scope;
20007 bool saved_greater_than_is_operator_p;
20008 int saved_unevaluated_operand;
20009 int saved_inhibit_evaluation_warnings;
20011 /* [temp.names]
20013 When parsing a template-id, the first non-nested `>' is taken as
20014 the end of the template-argument-list rather than a greater-than
20015 operator. */
20016 saved_greater_than_is_operator_p
20017 = parser->greater_than_is_operator_p;
20018 parser->greater_than_is_operator_p = false;
20019 /* Parsing the argument list may modify SCOPE, so we save it
20020 here. */
20021 saved_scope = parser->scope;
20022 saved_qualifying_scope = parser->qualifying_scope;
20023 saved_object_scope = parser->object_scope;
20024 /* We need to evaluate the template arguments, even though this
20025 template-id may be nested within a "sizeof". */
20026 saved_unevaluated_operand = cp_unevaluated_operand;
20027 cp_unevaluated_operand = 0;
20028 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20029 c_inhibit_evaluation_warnings = 0;
20030 /* Parse the template-argument-list itself. */
20031 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20032 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20033 arguments = NULL_TREE;
20034 else
20035 arguments = cp_parser_template_argument_list (parser);
20036 /* Look for the `>' that ends the template-argument-list. If we find
20037 a '>>' instead, it's probably just a typo. */
20038 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20040 if (cxx_dialect != cxx98)
20042 /* In C++0x, a `>>' in a template argument list or cast
20043 expression is considered to be two separate `>'
20044 tokens. So, change the current token to a `>', but don't
20045 consume it: it will be consumed later when the outer
20046 template argument list (or cast expression) is parsed.
20047 Note that this replacement of `>' for `>>' is necessary
20048 even if we are parsing tentatively: in the tentative
20049 case, after calling
20050 cp_parser_enclosed_template_argument_list we will always
20051 throw away all of the template arguments and the first
20052 closing `>', either because the template argument list
20053 was erroneous or because we are replacing those tokens
20054 with a CPP_TEMPLATE_ID token. The second `>' (which will
20055 not have been thrown away) is needed either to close an
20056 outer template argument list or to complete a new-style
20057 cast. */
20058 cp_token *token = cp_lexer_peek_token (parser->lexer);
20059 token->type = CPP_GREATER;
20061 else if (!saved_greater_than_is_operator_p)
20063 /* If we're in a nested template argument list, the '>>' has
20064 to be a typo for '> >'. We emit the error message, but we
20065 continue parsing and we push a '>' as next token, so that
20066 the argument list will be parsed correctly. Note that the
20067 global source location is still on the token before the
20068 '>>', so we need to say explicitly where we want it. */
20069 cp_token *token = cp_lexer_peek_token (parser->lexer);
20070 error_at (token->location, "%<>>%> should be %<> >%> "
20071 "within a nested template argument list");
20073 token->type = CPP_GREATER;
20075 else
20077 /* If this is not a nested template argument list, the '>>'
20078 is a typo for '>'. Emit an error message and continue.
20079 Same deal about the token location, but here we can get it
20080 right by consuming the '>>' before issuing the diagnostic. */
20081 cp_token *token = cp_lexer_consume_token (parser->lexer);
20082 error_at (token->location,
20083 "spurious %<>>%>, use %<>%> to terminate "
20084 "a template argument list");
20087 else
20088 cp_parser_skip_to_end_of_template_parameter_list (parser);
20089 /* The `>' token might be a greater-than operator again now. */
20090 parser->greater_than_is_operator_p
20091 = saved_greater_than_is_operator_p;
20092 /* Restore the SAVED_SCOPE. */
20093 parser->scope = saved_scope;
20094 parser->qualifying_scope = saved_qualifying_scope;
20095 parser->object_scope = saved_object_scope;
20096 cp_unevaluated_operand = saved_unevaluated_operand;
20097 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20099 return arguments;
20102 /* MEMBER_FUNCTION is a member function, or a friend. If default
20103 arguments, or the body of the function have not yet been parsed,
20104 parse them now. */
20106 static void
20107 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20109 /* If this member is a template, get the underlying
20110 FUNCTION_DECL. */
20111 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20112 member_function = DECL_TEMPLATE_RESULT (member_function);
20114 /* There should not be any class definitions in progress at this
20115 point; the bodies of members are only parsed outside of all class
20116 definitions. */
20117 gcc_assert (parser->num_classes_being_defined == 0);
20118 /* While we're parsing the member functions we might encounter more
20119 classes. We want to handle them right away, but we don't want
20120 them getting mixed up with functions that are currently in the
20121 queue. */
20122 push_unparsed_function_queues (parser);
20124 /* Make sure that any template parameters are in scope. */
20125 maybe_begin_member_template_processing (member_function);
20127 /* If the body of the function has not yet been parsed, parse it
20128 now. */
20129 if (DECL_PENDING_INLINE_P (member_function))
20131 tree function_scope;
20132 cp_token_cache *tokens;
20134 /* The function is no longer pending; we are processing it. */
20135 tokens = DECL_PENDING_INLINE_INFO (member_function);
20136 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20137 DECL_PENDING_INLINE_P (member_function) = 0;
20139 /* If this is a local class, enter the scope of the containing
20140 function. */
20141 function_scope = current_function_decl;
20142 if (function_scope)
20143 push_function_context ();
20145 /* Push the body of the function onto the lexer stack. */
20146 cp_parser_push_lexer_for_tokens (parser, tokens);
20148 /* Let the front end know that we going to be defining this
20149 function. */
20150 start_preparsed_function (member_function, NULL_TREE,
20151 SF_PRE_PARSED | SF_INCLASS_INLINE);
20153 /* Don't do access checking if it is a templated function. */
20154 if (processing_template_decl)
20155 push_deferring_access_checks (dk_no_check);
20157 /* Now, parse the body of the function. */
20158 cp_parser_function_definition_after_declarator (parser,
20159 /*inline_p=*/true);
20161 if (processing_template_decl)
20162 pop_deferring_access_checks ();
20164 /* Leave the scope of the containing function. */
20165 if (function_scope)
20166 pop_function_context ();
20167 cp_parser_pop_lexer (parser);
20170 /* Remove any template parameters from the symbol table. */
20171 maybe_end_member_template_processing ();
20173 /* Restore the queue. */
20174 pop_unparsed_function_queues (parser);
20177 /* If DECL contains any default args, remember it on the unparsed
20178 functions queue. */
20180 static void
20181 cp_parser_save_default_args (cp_parser* parser, tree decl)
20183 tree probe;
20185 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20186 probe;
20187 probe = TREE_CHAIN (probe))
20188 if (TREE_PURPOSE (probe))
20190 cp_default_arg_entry *entry
20191 = VEC_safe_push (cp_default_arg_entry, gc,
20192 unparsed_funs_with_default_args, NULL);
20193 entry->class_type = current_class_type;
20194 entry->decl = decl;
20195 break;
20199 /* FN is a FUNCTION_DECL which may contains a parameter with an
20200 unparsed DEFAULT_ARG. Parse the default args now. This function
20201 assumes that the current scope is the scope in which the default
20202 argument should be processed. */
20204 static void
20205 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20207 bool saved_local_variables_forbidden_p;
20208 tree parm, parmdecl;
20210 /* While we're parsing the default args, we might (due to the
20211 statement expression extension) encounter more classes. We want
20212 to handle them right away, but we don't want them getting mixed
20213 up with default args that are currently in the queue. */
20214 push_unparsed_function_queues (parser);
20216 /* Local variable names (and the `this' keyword) may not appear
20217 in a default argument. */
20218 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20219 parser->local_variables_forbidden_p = true;
20221 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20222 parmdecl = DECL_ARGUMENTS (fn);
20223 parm && parm != void_list_node;
20224 parm = TREE_CHAIN (parm),
20225 parmdecl = DECL_CHAIN (parmdecl))
20227 cp_token_cache *tokens;
20228 tree default_arg = TREE_PURPOSE (parm);
20229 tree parsed_arg;
20230 VEC(tree,gc) *insts;
20231 tree copy;
20232 unsigned ix;
20234 if (!default_arg)
20235 continue;
20237 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20238 /* This can happen for a friend declaration for a function
20239 already declared with default arguments. */
20240 continue;
20242 /* Push the saved tokens for the default argument onto the parser's
20243 lexer stack. */
20244 tokens = DEFARG_TOKENS (default_arg);
20245 cp_parser_push_lexer_for_tokens (parser, tokens);
20247 start_lambda_scope (parmdecl);
20249 /* Parse the assignment-expression. */
20250 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20251 if (parsed_arg == error_mark_node)
20253 cp_parser_pop_lexer (parser);
20254 continue;
20257 if (!processing_template_decl)
20258 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20260 TREE_PURPOSE (parm) = parsed_arg;
20262 /* Update any instantiations we've already created. */
20263 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20264 VEC_iterate (tree, insts, ix, copy); ix++)
20265 TREE_PURPOSE (copy) = parsed_arg;
20267 finish_lambda_scope ();
20269 /* If the token stream has not been completely used up, then
20270 there was extra junk after the end of the default
20271 argument. */
20272 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20273 cp_parser_error (parser, "expected %<,%>");
20275 /* Revert to the main lexer. */
20276 cp_parser_pop_lexer (parser);
20279 /* Make sure no default arg is missing. */
20280 check_default_args (fn);
20282 /* Restore the state of local_variables_forbidden_p. */
20283 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20285 /* Restore the queue. */
20286 pop_unparsed_function_queues (parser);
20289 /* Parse the operand of `sizeof' (or a similar operator). Returns
20290 either a TYPE or an expression, depending on the form of the
20291 input. The KEYWORD indicates which kind of expression we have
20292 encountered. */
20294 static tree
20295 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20297 tree expr = NULL_TREE;
20298 const char *saved_message;
20299 char *tmp;
20300 bool saved_integral_constant_expression_p;
20301 bool saved_non_integral_constant_expression_p;
20302 bool pack_expansion_p = false;
20304 /* Types cannot be defined in a `sizeof' expression. Save away the
20305 old message. */
20306 saved_message = parser->type_definition_forbidden_message;
20307 /* And create the new one. */
20308 tmp = concat ("types may not be defined in %<",
20309 IDENTIFIER_POINTER (ridpointers[keyword]),
20310 "%> expressions", NULL);
20311 parser->type_definition_forbidden_message = tmp;
20313 /* The restrictions on constant-expressions do not apply inside
20314 sizeof expressions. */
20315 saved_integral_constant_expression_p
20316 = parser->integral_constant_expression_p;
20317 saved_non_integral_constant_expression_p
20318 = parser->non_integral_constant_expression_p;
20319 parser->integral_constant_expression_p = false;
20321 /* If it's a `...', then we are computing the length of a parameter
20322 pack. */
20323 if (keyword == RID_SIZEOF
20324 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20326 /* Consume the `...'. */
20327 cp_lexer_consume_token (parser->lexer);
20328 maybe_warn_variadic_templates ();
20330 /* Note that this is an expansion. */
20331 pack_expansion_p = true;
20334 /* Do not actually evaluate the expression. */
20335 ++cp_unevaluated_operand;
20336 ++c_inhibit_evaluation_warnings;
20337 /* If it's a `(', then we might be looking at the type-id
20338 construction. */
20339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20341 tree type;
20342 bool saved_in_type_id_in_expr_p;
20344 /* We can't be sure yet whether we're looking at a type-id or an
20345 expression. */
20346 cp_parser_parse_tentatively (parser);
20347 /* Consume the `('. */
20348 cp_lexer_consume_token (parser->lexer);
20349 /* Parse the type-id. */
20350 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20351 parser->in_type_id_in_expr_p = true;
20352 type = cp_parser_type_id (parser);
20353 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20354 /* Now, look for the trailing `)'. */
20355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20356 /* If all went well, then we're done. */
20357 if (cp_parser_parse_definitely (parser))
20359 cp_decl_specifier_seq decl_specs;
20361 /* Build a trivial decl-specifier-seq. */
20362 clear_decl_specs (&decl_specs);
20363 decl_specs.type = type;
20365 /* Call grokdeclarator to figure out what type this is. */
20366 expr = grokdeclarator (NULL,
20367 &decl_specs,
20368 TYPENAME,
20369 /*initialized=*/0,
20370 /*attrlist=*/NULL);
20374 /* If the type-id production did not work out, then we must be
20375 looking at the unary-expression production. */
20376 if (!expr)
20377 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20378 /*cast_p=*/false, NULL);
20380 if (pack_expansion_p)
20381 /* Build a pack expansion. */
20382 expr = make_pack_expansion (expr);
20384 /* Go back to evaluating expressions. */
20385 --cp_unevaluated_operand;
20386 --c_inhibit_evaluation_warnings;
20388 /* Free the message we created. */
20389 free (tmp);
20390 /* And restore the old one. */
20391 parser->type_definition_forbidden_message = saved_message;
20392 parser->integral_constant_expression_p
20393 = saved_integral_constant_expression_p;
20394 parser->non_integral_constant_expression_p
20395 = saved_non_integral_constant_expression_p;
20397 return expr;
20400 /* If the current declaration has no declarator, return true. */
20402 static bool
20403 cp_parser_declares_only_class_p (cp_parser *parser)
20405 /* If the next token is a `;' or a `,' then there is no
20406 declarator. */
20407 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20408 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20411 /* Update the DECL_SPECS to reflect the storage class indicated by
20412 KEYWORD. */
20414 static void
20415 cp_parser_set_storage_class (cp_parser *parser,
20416 cp_decl_specifier_seq *decl_specs,
20417 enum rid keyword,
20418 location_t location)
20420 cp_storage_class storage_class;
20422 if (parser->in_unbraced_linkage_specification_p)
20424 error_at (location, "invalid use of %qD in linkage specification",
20425 ridpointers[keyword]);
20426 return;
20428 else if (decl_specs->storage_class != sc_none)
20430 decl_specs->conflicting_specifiers_p = true;
20431 return;
20434 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20435 && decl_specs->specs[(int) ds_thread])
20437 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20438 decl_specs->specs[(int) ds_thread] = 0;
20441 switch (keyword)
20443 case RID_AUTO:
20444 storage_class = sc_auto;
20445 break;
20446 case RID_REGISTER:
20447 storage_class = sc_register;
20448 break;
20449 case RID_STATIC:
20450 storage_class = sc_static;
20451 break;
20452 case RID_EXTERN:
20453 storage_class = sc_extern;
20454 break;
20455 case RID_MUTABLE:
20456 storage_class = sc_mutable;
20457 break;
20458 default:
20459 gcc_unreachable ();
20461 decl_specs->storage_class = storage_class;
20463 /* A storage class specifier cannot be applied alongside a typedef
20464 specifier. If there is a typedef specifier present then set
20465 conflicting_specifiers_p which will trigger an error later
20466 on in grokdeclarator. */
20467 if (decl_specs->specs[(int)ds_typedef])
20468 decl_specs->conflicting_specifiers_p = true;
20471 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20472 is true, the type is a user-defined type; otherwise it is a
20473 built-in type specified by a keyword. */
20475 static void
20476 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20477 tree type_spec,
20478 location_t location,
20479 bool user_defined_p)
20481 decl_specs->any_specifiers_p = true;
20483 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20484 (with, for example, in "typedef int wchar_t;") we remember that
20485 this is what happened. In system headers, we ignore these
20486 declarations so that G++ can work with system headers that are not
20487 C++-safe. */
20488 if (decl_specs->specs[(int) ds_typedef]
20489 && !user_defined_p
20490 && (type_spec == boolean_type_node
20491 || type_spec == char16_type_node
20492 || type_spec == char32_type_node
20493 || type_spec == wchar_type_node)
20494 && (decl_specs->type
20495 || decl_specs->specs[(int) ds_long]
20496 || decl_specs->specs[(int) ds_short]
20497 || decl_specs->specs[(int) ds_unsigned]
20498 || decl_specs->specs[(int) ds_signed]))
20500 decl_specs->redefined_builtin_type = type_spec;
20501 if (!decl_specs->type)
20503 decl_specs->type = type_spec;
20504 decl_specs->user_defined_type_p = false;
20505 decl_specs->type_location = location;
20508 else if (decl_specs->type)
20509 decl_specs->multiple_types_p = true;
20510 else
20512 decl_specs->type = type_spec;
20513 decl_specs->user_defined_type_p = user_defined_p;
20514 decl_specs->redefined_builtin_type = NULL_TREE;
20515 decl_specs->type_location = location;
20519 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20520 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20522 static bool
20523 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20525 return decl_specifiers->specs[(int) ds_friend] != 0;
20528 /* Issue an error message indicating that TOKEN_DESC was expected.
20529 If KEYWORD is true, it indicated this function is called by
20530 cp_parser_require_keword and the required token can only be
20531 a indicated keyword. */
20533 static void
20534 cp_parser_required_error (cp_parser *parser,
20535 required_token token_desc,
20536 bool keyword)
20538 switch (token_desc)
20540 case RT_NEW:
20541 cp_parser_error (parser, "expected %<new%>");
20542 return;
20543 case RT_DELETE:
20544 cp_parser_error (parser, "expected %<delete%>");
20545 return;
20546 case RT_RETURN:
20547 cp_parser_error (parser, "expected %<return%>");
20548 return;
20549 case RT_WHILE:
20550 cp_parser_error (parser, "expected %<while%>");
20551 return;
20552 case RT_EXTERN:
20553 cp_parser_error (parser, "expected %<extern%>");
20554 return;
20555 case RT_STATIC_ASSERT:
20556 cp_parser_error (parser, "expected %<static_assert%>");
20557 return;
20558 case RT_DECLTYPE:
20559 cp_parser_error (parser, "expected %<decltype%>");
20560 return;
20561 case RT_OPERATOR:
20562 cp_parser_error (parser, "expected %<operator%>");
20563 return;
20564 case RT_CLASS:
20565 cp_parser_error (parser, "expected %<class%>");
20566 return;
20567 case RT_TEMPLATE:
20568 cp_parser_error (parser, "expected %<template%>");
20569 return;
20570 case RT_NAMESPACE:
20571 cp_parser_error (parser, "expected %<namespace%>");
20572 return;
20573 case RT_USING:
20574 cp_parser_error (parser, "expected %<using%>");
20575 return;
20576 case RT_ASM:
20577 cp_parser_error (parser, "expected %<asm%>");
20578 return;
20579 case RT_TRY:
20580 cp_parser_error (parser, "expected %<try%>");
20581 return;
20582 case RT_CATCH:
20583 cp_parser_error (parser, "expected %<catch%>");
20584 return;
20585 case RT_THROW:
20586 cp_parser_error (parser, "expected %<throw%>");
20587 return;
20588 case RT_LABEL:
20589 cp_parser_error (parser, "expected %<__label__%>");
20590 return;
20591 case RT_AT_TRY:
20592 cp_parser_error (parser, "expected %<@try%>");
20593 return;
20594 case RT_AT_SYNCHRONIZED:
20595 cp_parser_error (parser, "expected %<@synchronized%>");
20596 return;
20597 case RT_AT_THROW:
20598 cp_parser_error (parser, "expected %<@throw%>");
20599 return;
20600 default:
20601 break;
20603 if (!keyword)
20605 switch (token_desc)
20607 case RT_SEMICOLON:
20608 cp_parser_error (parser, "expected %<;%>");
20609 return;
20610 case RT_OPEN_PAREN:
20611 cp_parser_error (parser, "expected %<(%>");
20612 return;
20613 case RT_CLOSE_BRACE:
20614 cp_parser_error (parser, "expected %<}%>");
20615 return;
20616 case RT_OPEN_BRACE:
20617 cp_parser_error (parser, "expected %<{%>");
20618 return;
20619 case RT_CLOSE_SQUARE:
20620 cp_parser_error (parser, "expected %<]%>");
20621 return;
20622 case RT_OPEN_SQUARE:
20623 cp_parser_error (parser, "expected %<[%>");
20624 return;
20625 case RT_COMMA:
20626 cp_parser_error (parser, "expected %<,%>");
20627 return;
20628 case RT_SCOPE:
20629 cp_parser_error (parser, "expected %<::%>");
20630 return;
20631 case RT_LESS:
20632 cp_parser_error (parser, "expected %<<%>");
20633 return;
20634 case RT_GREATER:
20635 cp_parser_error (parser, "expected %<>%>");
20636 return;
20637 case RT_EQ:
20638 cp_parser_error (parser, "expected %<=%>");
20639 return;
20640 case RT_ELLIPSIS:
20641 cp_parser_error (parser, "expected %<...%>");
20642 return;
20643 case RT_MULT:
20644 cp_parser_error (parser, "expected %<*%>");
20645 return;
20646 case RT_COMPL:
20647 cp_parser_error (parser, "expected %<~%>");
20648 return;
20649 case RT_COLON:
20650 cp_parser_error (parser, "expected %<:%>");
20651 return;
20652 case RT_COLON_SCOPE:
20653 cp_parser_error (parser, "expected %<:%> or %<::%>");
20654 return;
20655 case RT_CLOSE_PAREN:
20656 cp_parser_error (parser, "expected %<)%>");
20657 return;
20658 case RT_COMMA_CLOSE_PAREN:
20659 cp_parser_error (parser, "expected %<,%> or %<)%>");
20660 return;
20661 case RT_PRAGMA_EOL:
20662 cp_parser_error (parser, "expected end of line");
20663 return;
20664 case RT_NAME:
20665 cp_parser_error (parser, "expected identifier");
20666 return;
20667 case RT_SELECT:
20668 cp_parser_error (parser, "expected selection-statement");
20669 return;
20670 case RT_INTERATION:
20671 cp_parser_error (parser, "expected iteration-statement");
20672 return;
20673 case RT_JUMP:
20674 cp_parser_error (parser, "expected jump-statement");
20675 return;
20676 case RT_CLASS_KEY:
20677 cp_parser_error (parser, "expected class-key");
20678 return;
20679 case RT_CLASS_TYPENAME_TEMPLATE:
20680 cp_parser_error (parser,
20681 "expected %<class%>, %<typename%>, or %<template%>");
20682 return;
20683 default:
20684 gcc_unreachable ();
20687 else
20688 gcc_unreachable ();
20693 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20694 issue an error message indicating that TOKEN_DESC was expected.
20696 Returns the token consumed, if the token had the appropriate type.
20697 Otherwise, returns NULL. */
20699 static cp_token *
20700 cp_parser_require (cp_parser* parser,
20701 enum cpp_ttype type,
20702 required_token token_desc)
20704 if (cp_lexer_next_token_is (parser->lexer, type))
20705 return cp_lexer_consume_token (parser->lexer);
20706 else
20708 /* Output the MESSAGE -- unless we're parsing tentatively. */
20709 if (!cp_parser_simulate_error (parser))
20710 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20711 return NULL;
20715 /* An error message is produced if the next token is not '>'.
20716 All further tokens are skipped until the desired token is
20717 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20719 static void
20720 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20722 /* Current level of '< ... >'. */
20723 unsigned level = 0;
20724 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20725 unsigned nesting_depth = 0;
20727 /* Are we ready, yet? If not, issue error message. */
20728 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20729 return;
20731 /* Skip tokens until the desired token is found. */
20732 while (true)
20734 /* Peek at the next token. */
20735 switch (cp_lexer_peek_token (parser->lexer)->type)
20737 case CPP_LESS:
20738 if (!nesting_depth)
20739 ++level;
20740 break;
20742 case CPP_RSHIFT:
20743 if (cxx_dialect == cxx98)
20744 /* C++0x views the `>>' operator as two `>' tokens, but
20745 C++98 does not. */
20746 break;
20747 else if (!nesting_depth && level-- == 0)
20749 /* We've hit a `>>' where the first `>' closes the
20750 template argument list, and the second `>' is
20751 spurious. Just consume the `>>' and stop; we've
20752 already produced at least one error. */
20753 cp_lexer_consume_token (parser->lexer);
20754 return;
20756 /* Fall through for C++0x, so we handle the second `>' in
20757 the `>>'. */
20759 case CPP_GREATER:
20760 if (!nesting_depth && level-- == 0)
20762 /* We've reached the token we want, consume it and stop. */
20763 cp_lexer_consume_token (parser->lexer);
20764 return;
20766 break;
20768 case CPP_OPEN_PAREN:
20769 case CPP_OPEN_SQUARE:
20770 ++nesting_depth;
20771 break;
20773 case CPP_CLOSE_PAREN:
20774 case CPP_CLOSE_SQUARE:
20775 if (nesting_depth-- == 0)
20776 return;
20777 break;
20779 case CPP_EOF:
20780 case CPP_PRAGMA_EOL:
20781 case CPP_SEMICOLON:
20782 case CPP_OPEN_BRACE:
20783 case CPP_CLOSE_BRACE:
20784 /* The '>' was probably forgotten, don't look further. */
20785 return;
20787 default:
20788 break;
20791 /* Consume this token. */
20792 cp_lexer_consume_token (parser->lexer);
20796 /* If the next token is the indicated keyword, consume it. Otherwise,
20797 issue an error message indicating that TOKEN_DESC was expected.
20799 Returns the token consumed, if the token had the appropriate type.
20800 Otherwise, returns NULL. */
20802 static cp_token *
20803 cp_parser_require_keyword (cp_parser* parser,
20804 enum rid keyword,
20805 required_token token_desc)
20807 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20809 if (token && token->keyword != keyword)
20811 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20812 return NULL;
20815 return token;
20818 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20819 function-definition. */
20821 static bool
20822 cp_parser_token_starts_function_definition_p (cp_token* token)
20824 return (/* An ordinary function-body begins with an `{'. */
20825 token->type == CPP_OPEN_BRACE
20826 /* A ctor-initializer begins with a `:'. */
20827 || token->type == CPP_COLON
20828 /* A function-try-block begins with `try'. */
20829 || token->keyword == RID_TRY
20830 /* The named return value extension begins with `return'. */
20831 || token->keyword == RID_RETURN);
20834 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20835 definition. */
20837 static bool
20838 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20840 cp_token *token;
20842 token = cp_lexer_peek_token (parser->lexer);
20843 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20846 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20847 C++0x) ending a template-argument. */
20849 static bool
20850 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20852 cp_token *token;
20854 token = cp_lexer_peek_token (parser->lexer);
20855 return (token->type == CPP_COMMA
20856 || token->type == CPP_GREATER
20857 || token->type == CPP_ELLIPSIS
20858 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20861 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20862 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20864 static bool
20865 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20866 size_t n)
20868 cp_token *token;
20870 token = cp_lexer_peek_nth_token (parser->lexer, n);
20871 if (token->type == CPP_LESS)
20872 return true;
20873 /* Check for the sequence `<::' in the original code. It would be lexed as
20874 `[:', where `[' is a digraph, and there is no whitespace before
20875 `:'. */
20876 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20878 cp_token *token2;
20879 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20880 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20881 return true;
20883 return false;
20886 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20887 or none_type otherwise. */
20889 static enum tag_types
20890 cp_parser_token_is_class_key (cp_token* token)
20892 switch (token->keyword)
20894 case RID_CLASS:
20895 return class_type;
20896 case RID_STRUCT:
20897 return record_type;
20898 case RID_UNION:
20899 return union_type;
20901 default:
20902 return none_type;
20906 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20908 static void
20909 cp_parser_check_class_key (enum tag_types class_key, tree type)
20911 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20912 permerror (input_location, "%qs tag used in naming %q#T",
20913 class_key == union_type ? "union"
20914 : class_key == record_type ? "struct" : "class",
20915 type);
20918 /* Issue an error message if DECL is redeclared with different
20919 access than its original declaration [class.access.spec/3].
20920 This applies to nested classes and nested class templates.
20921 [class.mem/1]. */
20923 static void
20924 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20926 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20927 return;
20929 if ((TREE_PRIVATE (decl)
20930 != (current_access_specifier == access_private_node))
20931 || (TREE_PROTECTED (decl)
20932 != (current_access_specifier == access_protected_node)))
20933 error_at (location, "%qD redeclared with different access", decl);
20936 /* Look for the `template' keyword, as a syntactic disambiguator.
20937 Return TRUE iff it is present, in which case it will be
20938 consumed. */
20940 static bool
20941 cp_parser_optional_template_keyword (cp_parser *parser)
20943 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20945 /* The `template' keyword can only be used within templates;
20946 outside templates the parser can always figure out what is a
20947 template and what is not. */
20948 if (!processing_template_decl)
20950 cp_token *token = cp_lexer_peek_token (parser->lexer);
20951 error_at (token->location,
20952 "%<template%> (as a disambiguator) is only allowed "
20953 "within templates");
20954 /* If this part of the token stream is rescanned, the same
20955 error message would be generated. So, we purge the token
20956 from the stream. */
20957 cp_lexer_purge_token (parser->lexer);
20958 return false;
20960 else
20962 /* Consume the `template' keyword. */
20963 cp_lexer_consume_token (parser->lexer);
20964 return true;
20968 return false;
20971 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20972 set PARSER->SCOPE, and perform other related actions. */
20974 static void
20975 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20977 int i;
20978 struct tree_check *check_value;
20979 deferred_access_check *chk;
20980 VEC (deferred_access_check,gc) *checks;
20982 /* Get the stored value. */
20983 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20984 /* Perform any access checks that were deferred. */
20985 checks = check_value->checks;
20986 if (checks)
20988 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20989 perform_or_defer_access_check (chk->binfo,
20990 chk->decl,
20991 chk->diag_decl);
20993 /* Set the scope from the stored value. */
20994 parser->scope = check_value->value;
20995 parser->qualifying_scope = check_value->qualifying_scope;
20996 parser->object_scope = NULL_TREE;
20999 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21000 encounter the end of a block before what we were looking for. */
21002 static bool
21003 cp_parser_cache_group (cp_parser *parser,
21004 enum cpp_ttype end,
21005 unsigned depth)
21007 while (true)
21009 cp_token *token = cp_lexer_peek_token (parser->lexer);
21011 /* Abort a parenthesized expression if we encounter a semicolon. */
21012 if ((end == CPP_CLOSE_PAREN || depth == 0)
21013 && token->type == CPP_SEMICOLON)
21014 return true;
21015 /* If we've reached the end of the file, stop. */
21016 if (token->type == CPP_EOF
21017 || (end != CPP_PRAGMA_EOL
21018 && token->type == CPP_PRAGMA_EOL))
21019 return true;
21020 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21021 /* We've hit the end of an enclosing block, so there's been some
21022 kind of syntax error. */
21023 return true;
21025 /* Consume the token. */
21026 cp_lexer_consume_token (parser->lexer);
21027 /* See if it starts a new group. */
21028 if (token->type == CPP_OPEN_BRACE)
21030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21031 /* In theory this should probably check end == '}', but
21032 cp_parser_save_member_function_body needs it to exit
21033 after either '}' or ')' when called with ')'. */
21034 if (depth == 0)
21035 return false;
21037 else if (token->type == CPP_OPEN_PAREN)
21039 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21040 if (depth == 0 && end == CPP_CLOSE_PAREN)
21041 return false;
21043 else if (token->type == CPP_PRAGMA)
21044 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21045 else if (token->type == end)
21046 return false;
21050 /* Begin parsing tentatively. We always save tokens while parsing
21051 tentatively so that if the tentative parsing fails we can restore the
21052 tokens. */
21054 static void
21055 cp_parser_parse_tentatively (cp_parser* parser)
21057 /* Enter a new parsing context. */
21058 parser->context = cp_parser_context_new (parser->context);
21059 /* Begin saving tokens. */
21060 cp_lexer_save_tokens (parser->lexer);
21061 /* In order to avoid repetitive access control error messages,
21062 access checks are queued up until we are no longer parsing
21063 tentatively. */
21064 push_deferring_access_checks (dk_deferred);
21067 /* Commit to the currently active tentative parse. */
21069 static void
21070 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21072 cp_parser_context *context;
21073 cp_lexer *lexer;
21075 /* Mark all of the levels as committed. */
21076 lexer = parser->lexer;
21077 for (context = parser->context; context->next; context = context->next)
21079 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21080 break;
21081 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21082 while (!cp_lexer_saving_tokens (lexer))
21083 lexer = lexer->next;
21084 cp_lexer_commit_tokens (lexer);
21088 /* Abort the currently active tentative parse. All consumed tokens
21089 will be rolled back, and no diagnostics will be issued. */
21091 static void
21092 cp_parser_abort_tentative_parse (cp_parser* parser)
21094 cp_parser_simulate_error (parser);
21095 /* Now, pretend that we want to see if the construct was
21096 successfully parsed. */
21097 cp_parser_parse_definitely (parser);
21100 /* Stop parsing tentatively. If a parse error has occurred, restore the
21101 token stream. Otherwise, commit to the tokens we have consumed.
21102 Returns true if no error occurred; false otherwise. */
21104 static bool
21105 cp_parser_parse_definitely (cp_parser* parser)
21107 bool error_occurred;
21108 cp_parser_context *context;
21110 /* Remember whether or not an error occurred, since we are about to
21111 destroy that information. */
21112 error_occurred = cp_parser_error_occurred (parser);
21113 /* Remove the topmost context from the stack. */
21114 context = parser->context;
21115 parser->context = context->next;
21116 /* If no parse errors occurred, commit to the tentative parse. */
21117 if (!error_occurred)
21119 /* Commit to the tokens read tentatively, unless that was
21120 already done. */
21121 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21122 cp_lexer_commit_tokens (parser->lexer);
21124 pop_to_parent_deferring_access_checks ();
21126 /* Otherwise, if errors occurred, roll back our state so that things
21127 are just as they were before we began the tentative parse. */
21128 else
21130 cp_lexer_rollback_tokens (parser->lexer);
21131 pop_deferring_access_checks ();
21133 /* Add the context to the front of the free list. */
21134 context->next = cp_parser_context_free_list;
21135 cp_parser_context_free_list = context;
21137 return !error_occurred;
21140 /* Returns true if we are parsing tentatively and are not committed to
21141 this tentative parse. */
21143 static bool
21144 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21146 return (cp_parser_parsing_tentatively (parser)
21147 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21150 /* Returns nonzero iff an error has occurred during the most recent
21151 tentative parse. */
21153 static bool
21154 cp_parser_error_occurred (cp_parser* parser)
21156 return (cp_parser_parsing_tentatively (parser)
21157 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21160 /* Returns nonzero if GNU extensions are allowed. */
21162 static bool
21163 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21165 return parser->allow_gnu_extensions_p;
21168 /* Objective-C++ Productions */
21171 /* Parse an Objective-C expression, which feeds into a primary-expression
21172 above.
21174 objc-expression:
21175 objc-message-expression
21176 objc-string-literal
21177 objc-encode-expression
21178 objc-protocol-expression
21179 objc-selector-expression
21181 Returns a tree representation of the expression. */
21183 static tree
21184 cp_parser_objc_expression (cp_parser* parser)
21186 /* Try to figure out what kind of declaration is present. */
21187 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21189 switch (kwd->type)
21191 case CPP_OPEN_SQUARE:
21192 return cp_parser_objc_message_expression (parser);
21194 case CPP_OBJC_STRING:
21195 kwd = cp_lexer_consume_token (parser->lexer);
21196 return objc_build_string_object (kwd->u.value);
21198 case CPP_KEYWORD:
21199 switch (kwd->keyword)
21201 case RID_AT_ENCODE:
21202 return cp_parser_objc_encode_expression (parser);
21204 case RID_AT_PROTOCOL:
21205 return cp_parser_objc_protocol_expression (parser);
21207 case RID_AT_SELECTOR:
21208 return cp_parser_objc_selector_expression (parser);
21210 default:
21211 break;
21213 default:
21214 error_at (kwd->location,
21215 "misplaced %<@%D%> Objective-C++ construct",
21216 kwd->u.value);
21217 cp_parser_skip_to_end_of_block_or_statement (parser);
21220 return error_mark_node;
21223 /* Parse an Objective-C message expression.
21225 objc-message-expression:
21226 [ objc-message-receiver objc-message-args ]
21228 Returns a representation of an Objective-C message. */
21230 static tree
21231 cp_parser_objc_message_expression (cp_parser* parser)
21233 tree receiver, messageargs;
21235 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21236 receiver = cp_parser_objc_message_receiver (parser);
21237 messageargs = cp_parser_objc_message_args (parser);
21238 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21240 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21243 /* Parse an objc-message-receiver.
21245 objc-message-receiver:
21246 expression
21247 simple-type-specifier
21249 Returns a representation of the type or expression. */
21251 static tree
21252 cp_parser_objc_message_receiver (cp_parser* parser)
21254 tree rcv;
21256 /* An Objective-C message receiver may be either (1) a type
21257 or (2) an expression. */
21258 cp_parser_parse_tentatively (parser);
21259 rcv = cp_parser_expression (parser, false, NULL);
21261 if (cp_parser_parse_definitely (parser))
21262 return rcv;
21264 rcv = cp_parser_simple_type_specifier (parser,
21265 /*decl_specs=*/NULL,
21266 CP_PARSER_FLAGS_NONE);
21268 return objc_get_class_reference (rcv);
21271 /* Parse the arguments and selectors comprising an Objective-C message.
21273 objc-message-args:
21274 objc-selector
21275 objc-selector-args
21276 objc-selector-args , objc-comma-args
21278 objc-selector-args:
21279 objc-selector [opt] : assignment-expression
21280 objc-selector-args objc-selector [opt] : assignment-expression
21282 objc-comma-args:
21283 assignment-expression
21284 objc-comma-args , assignment-expression
21286 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21287 selector arguments and TREE_VALUE containing a list of comma
21288 arguments. */
21290 static tree
21291 cp_parser_objc_message_args (cp_parser* parser)
21293 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21294 bool maybe_unary_selector_p = true;
21295 cp_token *token = cp_lexer_peek_token (parser->lexer);
21297 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21299 tree selector = NULL_TREE, arg;
21301 if (token->type != CPP_COLON)
21302 selector = cp_parser_objc_selector (parser);
21304 /* Detect if we have a unary selector. */
21305 if (maybe_unary_selector_p
21306 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21307 return build_tree_list (selector, NULL_TREE);
21309 maybe_unary_selector_p = false;
21310 cp_parser_require (parser, CPP_COLON, RT_COLON);
21311 arg = cp_parser_assignment_expression (parser, false, NULL);
21313 sel_args
21314 = chainon (sel_args,
21315 build_tree_list (selector, arg));
21317 token = cp_lexer_peek_token (parser->lexer);
21320 /* Handle non-selector arguments, if any. */
21321 while (token->type == CPP_COMMA)
21323 tree arg;
21325 cp_lexer_consume_token (parser->lexer);
21326 arg = cp_parser_assignment_expression (parser, false, NULL);
21328 addl_args
21329 = chainon (addl_args,
21330 build_tree_list (NULL_TREE, arg));
21332 token = cp_lexer_peek_token (parser->lexer);
21335 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21337 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21338 return build_tree_list (error_mark_node, error_mark_node);
21341 return build_tree_list (sel_args, addl_args);
21344 /* Parse an Objective-C encode expression.
21346 objc-encode-expression:
21347 @encode objc-typename
21349 Returns an encoded representation of the type argument. */
21351 static tree
21352 cp_parser_objc_encode_expression (cp_parser* parser)
21354 tree type;
21355 cp_token *token;
21357 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21358 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21359 token = cp_lexer_peek_token (parser->lexer);
21360 type = complete_type (cp_parser_type_id (parser));
21361 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21363 if (!type)
21365 error_at (token->location,
21366 "%<@encode%> must specify a type as an argument");
21367 return error_mark_node;
21370 /* This happens if we find @encode(T) (where T is a template
21371 typename or something dependent on a template typename) when
21372 parsing a template. In that case, we can't compile it
21373 immediately, but we rather create an AT_ENCODE_EXPR which will
21374 need to be instantiated when the template is used.
21376 if (dependent_type_p (type))
21378 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21379 TREE_READONLY (value) = 1;
21380 return value;
21383 return objc_build_encode_expr (type);
21386 /* Parse an Objective-C @defs expression. */
21388 static tree
21389 cp_parser_objc_defs_expression (cp_parser *parser)
21391 tree name;
21393 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21394 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21395 name = cp_parser_identifier (parser);
21396 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21398 return objc_get_class_ivars (name);
21401 /* Parse an Objective-C protocol expression.
21403 objc-protocol-expression:
21404 @protocol ( identifier )
21406 Returns a representation of the protocol expression. */
21408 static tree
21409 cp_parser_objc_protocol_expression (cp_parser* parser)
21411 tree proto;
21413 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21414 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21415 proto = cp_parser_identifier (parser);
21416 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21418 return objc_build_protocol_expr (proto);
21421 /* Parse an Objective-C selector expression.
21423 objc-selector-expression:
21424 @selector ( objc-method-signature )
21426 objc-method-signature:
21427 objc-selector
21428 objc-selector-seq
21430 objc-selector-seq:
21431 objc-selector :
21432 objc-selector-seq objc-selector :
21434 Returns a representation of the method selector. */
21436 static tree
21437 cp_parser_objc_selector_expression (cp_parser* parser)
21439 tree sel_seq = NULL_TREE;
21440 bool maybe_unary_selector_p = true;
21441 cp_token *token;
21442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21444 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21445 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21446 token = cp_lexer_peek_token (parser->lexer);
21448 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21449 || token->type == CPP_SCOPE)
21451 tree selector = NULL_TREE;
21453 if (token->type != CPP_COLON
21454 || token->type == CPP_SCOPE)
21455 selector = cp_parser_objc_selector (parser);
21457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21458 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21460 /* Detect if we have a unary selector. */
21461 if (maybe_unary_selector_p)
21463 sel_seq = selector;
21464 goto finish_selector;
21466 else
21468 cp_parser_error (parser, "expected %<:%>");
21471 maybe_unary_selector_p = false;
21472 token = cp_lexer_consume_token (parser->lexer);
21474 if (token->type == CPP_SCOPE)
21476 sel_seq
21477 = chainon (sel_seq,
21478 build_tree_list (selector, NULL_TREE));
21479 sel_seq
21480 = chainon (sel_seq,
21481 build_tree_list (NULL_TREE, NULL_TREE));
21483 else
21484 sel_seq
21485 = chainon (sel_seq,
21486 build_tree_list (selector, NULL_TREE));
21488 token = cp_lexer_peek_token (parser->lexer);
21491 finish_selector:
21492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21494 return objc_build_selector_expr (loc, sel_seq);
21497 /* Parse a list of identifiers.
21499 objc-identifier-list:
21500 identifier
21501 objc-identifier-list , identifier
21503 Returns a TREE_LIST of identifier nodes. */
21505 static tree
21506 cp_parser_objc_identifier_list (cp_parser* parser)
21508 tree identifier;
21509 tree list;
21510 cp_token *sep;
21512 identifier = cp_parser_identifier (parser);
21513 if (identifier == error_mark_node)
21514 return error_mark_node;
21516 list = build_tree_list (NULL_TREE, identifier);
21517 sep = cp_lexer_peek_token (parser->lexer);
21519 while (sep->type == CPP_COMMA)
21521 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21522 identifier = cp_parser_identifier (parser);
21523 if (identifier == error_mark_node)
21524 return list;
21526 list = chainon (list, build_tree_list (NULL_TREE,
21527 identifier));
21528 sep = cp_lexer_peek_token (parser->lexer);
21531 return list;
21534 /* Parse an Objective-C alias declaration.
21536 objc-alias-declaration:
21537 @compatibility_alias identifier identifier ;
21539 This function registers the alias mapping with the Objective-C front end.
21540 It returns nothing. */
21542 static void
21543 cp_parser_objc_alias_declaration (cp_parser* parser)
21545 tree alias, orig;
21547 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21548 alias = cp_parser_identifier (parser);
21549 orig = cp_parser_identifier (parser);
21550 objc_declare_alias (alias, orig);
21551 cp_parser_consume_semicolon_at_end_of_statement (parser);
21554 /* Parse an Objective-C class forward-declaration.
21556 objc-class-declaration:
21557 @class objc-identifier-list ;
21559 The function registers the forward declarations with the Objective-C
21560 front end. It returns nothing. */
21562 static void
21563 cp_parser_objc_class_declaration (cp_parser* parser)
21565 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21566 objc_declare_class (cp_parser_objc_identifier_list (parser));
21567 cp_parser_consume_semicolon_at_end_of_statement (parser);
21570 /* Parse a list of Objective-C protocol references.
21572 objc-protocol-refs-opt:
21573 objc-protocol-refs [opt]
21575 objc-protocol-refs:
21576 < objc-identifier-list >
21578 Returns a TREE_LIST of identifiers, if any. */
21580 static tree
21581 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21583 tree protorefs = NULL_TREE;
21585 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21587 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21588 protorefs = cp_parser_objc_identifier_list (parser);
21589 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21592 return protorefs;
21595 /* Parse a Objective-C visibility specification. */
21597 static void
21598 cp_parser_objc_visibility_spec (cp_parser* parser)
21600 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21602 switch (vis->keyword)
21604 case RID_AT_PRIVATE:
21605 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21606 break;
21607 case RID_AT_PROTECTED:
21608 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21609 break;
21610 case RID_AT_PUBLIC:
21611 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21612 break;
21613 case RID_AT_PACKAGE:
21614 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21615 break;
21616 default:
21617 return;
21620 /* Eat '@private'/'@protected'/'@public'. */
21621 cp_lexer_consume_token (parser->lexer);
21624 /* Parse an Objective-C method type. Return 'true' if it is a class
21625 (+) method, and 'false' if it is an instance (-) method. */
21627 static inline bool
21628 cp_parser_objc_method_type (cp_parser* parser)
21630 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21631 return true;
21632 else
21633 return false;
21636 /* Parse an Objective-C protocol qualifier. */
21638 static tree
21639 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21641 tree quals = NULL_TREE, node;
21642 cp_token *token = cp_lexer_peek_token (parser->lexer);
21644 node = token->u.value;
21646 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21647 && (node == ridpointers [(int) RID_IN]
21648 || node == ridpointers [(int) RID_OUT]
21649 || node == ridpointers [(int) RID_INOUT]
21650 || node == ridpointers [(int) RID_BYCOPY]
21651 || node == ridpointers [(int) RID_BYREF]
21652 || node == ridpointers [(int) RID_ONEWAY]))
21654 quals = tree_cons (NULL_TREE, node, quals);
21655 cp_lexer_consume_token (parser->lexer);
21656 token = cp_lexer_peek_token (parser->lexer);
21657 node = token->u.value;
21660 return quals;
21663 /* Parse an Objective-C typename. */
21665 static tree
21666 cp_parser_objc_typename (cp_parser* parser)
21668 tree type_name = NULL_TREE;
21670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21672 tree proto_quals, cp_type = NULL_TREE;
21674 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21675 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21677 /* An ObjC type name may consist of just protocol qualifiers, in which
21678 case the type shall default to 'id'. */
21679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21680 cp_type = cp_parser_type_id (parser);
21682 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21683 type_name = build_tree_list (proto_quals, cp_type);
21686 return type_name;
21689 /* Check to see if TYPE refers to an Objective-C selector name. */
21691 static bool
21692 cp_parser_objc_selector_p (enum cpp_ttype type)
21694 return (type == CPP_NAME || type == CPP_KEYWORD
21695 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21696 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21697 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21698 || type == CPP_XOR || type == CPP_XOR_EQ);
21701 /* Parse an Objective-C selector. */
21703 static tree
21704 cp_parser_objc_selector (cp_parser* parser)
21706 cp_token *token = cp_lexer_consume_token (parser->lexer);
21708 if (!cp_parser_objc_selector_p (token->type))
21710 error_at (token->location, "invalid Objective-C++ selector name");
21711 return error_mark_node;
21714 /* C++ operator names are allowed to appear in ObjC selectors. */
21715 switch (token->type)
21717 case CPP_AND_AND: return get_identifier ("and");
21718 case CPP_AND_EQ: return get_identifier ("and_eq");
21719 case CPP_AND: return get_identifier ("bitand");
21720 case CPP_OR: return get_identifier ("bitor");
21721 case CPP_COMPL: return get_identifier ("compl");
21722 case CPP_NOT: return get_identifier ("not");
21723 case CPP_NOT_EQ: return get_identifier ("not_eq");
21724 case CPP_OR_OR: return get_identifier ("or");
21725 case CPP_OR_EQ: return get_identifier ("or_eq");
21726 case CPP_XOR: return get_identifier ("xor");
21727 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21728 default: return token->u.value;
21732 /* Parse an Objective-C params list. */
21734 static tree
21735 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21737 tree params = NULL_TREE;
21738 bool maybe_unary_selector_p = true;
21739 cp_token *token = cp_lexer_peek_token (parser->lexer);
21741 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21743 tree selector = NULL_TREE, type_name, identifier;
21744 tree parm_attr = NULL_TREE;
21746 if (token->keyword == RID_ATTRIBUTE)
21747 break;
21749 if (token->type != CPP_COLON)
21750 selector = cp_parser_objc_selector (parser);
21752 /* Detect if we have a unary selector. */
21753 if (maybe_unary_selector_p
21754 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21756 params = selector; /* Might be followed by attributes. */
21757 break;
21760 maybe_unary_selector_p = false;
21761 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21763 /* Something went quite wrong. There should be a colon
21764 here, but there is not. Stop parsing parameters. */
21765 break;
21767 type_name = cp_parser_objc_typename (parser);
21768 /* New ObjC allows attributes on parameters too. */
21769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21770 parm_attr = cp_parser_attributes_opt (parser);
21771 identifier = cp_parser_identifier (parser);
21773 params
21774 = chainon (params,
21775 objc_build_keyword_decl (selector,
21776 type_name,
21777 identifier,
21778 parm_attr));
21780 token = cp_lexer_peek_token (parser->lexer);
21783 if (params == NULL_TREE)
21785 cp_parser_error (parser, "objective-c++ method declaration is expected");
21786 return error_mark_node;
21789 /* We allow tail attributes for the method. */
21790 if (token->keyword == RID_ATTRIBUTE)
21792 *attributes = cp_parser_attributes_opt (parser);
21793 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21794 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21795 return params;
21796 cp_parser_error (parser,
21797 "method attributes must be specified at the end");
21798 return error_mark_node;
21801 if (params == NULL_TREE)
21803 cp_parser_error (parser, "objective-c++ method declaration is expected");
21804 return error_mark_node;
21806 return params;
21809 /* Parse the non-keyword Objective-C params. */
21811 static tree
21812 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21813 tree* attributes)
21815 tree params = make_node (TREE_LIST);
21816 cp_token *token = cp_lexer_peek_token (parser->lexer);
21817 *ellipsisp = false; /* Initially, assume no ellipsis. */
21819 while (token->type == CPP_COMMA)
21821 cp_parameter_declarator *parmdecl;
21822 tree parm;
21824 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21825 token = cp_lexer_peek_token (parser->lexer);
21827 if (token->type == CPP_ELLIPSIS)
21829 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21830 *ellipsisp = true;
21831 token = cp_lexer_peek_token (parser->lexer);
21832 break;
21835 /* TODO: parse attributes for tail parameters. */
21836 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21837 parm = grokdeclarator (parmdecl->declarator,
21838 &parmdecl->decl_specifiers,
21839 PARM, /*initialized=*/0,
21840 /*attrlist=*/NULL);
21842 chainon (params, build_tree_list (NULL_TREE, parm));
21843 token = cp_lexer_peek_token (parser->lexer);
21846 /* We allow tail attributes for the method. */
21847 if (token->keyword == RID_ATTRIBUTE)
21849 if (*attributes == NULL_TREE)
21851 *attributes = cp_parser_attributes_opt (parser);
21852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21853 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21854 return params;
21856 else
21857 /* We have an error, but parse the attributes, so that we can
21858 carry on. */
21859 *attributes = cp_parser_attributes_opt (parser);
21861 cp_parser_error (parser,
21862 "method attributes must be specified at the end");
21863 return error_mark_node;
21866 return params;
21869 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21871 static void
21872 cp_parser_objc_interstitial_code (cp_parser* parser)
21874 cp_token *token = cp_lexer_peek_token (parser->lexer);
21876 /* If the next token is `extern' and the following token is a string
21877 literal, then we have a linkage specification. */
21878 if (token->keyword == RID_EXTERN
21879 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21880 cp_parser_linkage_specification (parser);
21881 /* Handle #pragma, if any. */
21882 else if (token->type == CPP_PRAGMA)
21883 cp_parser_pragma (parser, pragma_external);
21884 /* Allow stray semicolons. */
21885 else if (token->type == CPP_SEMICOLON)
21886 cp_lexer_consume_token (parser->lexer);
21887 /* Mark methods as optional or required, when building protocols. */
21888 else if (token->keyword == RID_AT_OPTIONAL)
21890 cp_lexer_consume_token (parser->lexer);
21891 objc_set_method_opt (true);
21893 else if (token->keyword == RID_AT_REQUIRED)
21895 cp_lexer_consume_token (parser->lexer);
21896 objc_set_method_opt (false);
21898 else if (token->keyword == RID_NAMESPACE)
21899 cp_parser_namespace_definition (parser);
21900 /* Other stray characters must generate errors. */
21901 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21903 cp_lexer_consume_token (parser->lexer);
21904 error ("stray `%s' between Objective-C++ methods",
21905 token->type == CPP_OPEN_BRACE ? "{" : "}");
21907 /* Finally, try to parse a block-declaration, or a function-definition. */
21908 else
21909 cp_parser_block_declaration (parser, /*statement_p=*/false);
21912 /* Parse a method signature. */
21914 static tree
21915 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21917 tree rettype, kwdparms, optparms;
21918 bool ellipsis = false;
21919 bool is_class_method;
21921 is_class_method = cp_parser_objc_method_type (parser);
21922 rettype = cp_parser_objc_typename (parser);
21923 *attributes = NULL_TREE;
21924 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21925 if (kwdparms == error_mark_node)
21926 return error_mark_node;
21927 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21928 if (optparms == error_mark_node)
21929 return error_mark_node;
21931 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
21934 static bool
21935 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21937 tree tattr;
21938 cp_lexer_save_tokens (parser->lexer);
21939 tattr = cp_parser_attributes_opt (parser);
21940 gcc_assert (tattr) ;
21942 /* If the attributes are followed by a method introducer, this is not allowed.
21943 Dump the attributes and flag the situation. */
21944 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21945 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21946 return true;
21948 /* Otherwise, the attributes introduce some interstitial code, possibly so
21949 rewind to allow that check. */
21950 cp_lexer_rollback_tokens (parser->lexer);
21951 return false;
21954 /* Parse an Objective-C method prototype list. */
21956 static void
21957 cp_parser_objc_method_prototype_list (cp_parser* parser)
21959 cp_token *token = cp_lexer_peek_token (parser->lexer);
21961 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21963 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21965 tree attributes, sig;
21966 bool is_class_method;
21967 if (token->type == CPP_PLUS)
21968 is_class_method = true;
21969 else
21970 is_class_method = false;
21971 sig = cp_parser_objc_method_signature (parser, &attributes);
21972 if (sig == error_mark_node)
21974 cp_parser_skip_to_end_of_block_or_statement (parser);
21975 token = cp_lexer_peek_token (parser->lexer);
21976 continue;
21978 objc_add_method_declaration (is_class_method, sig, attributes);
21979 cp_parser_consume_semicolon_at_end_of_statement (parser);
21981 else if (token->keyword == RID_AT_PROPERTY)
21982 cp_parser_objc_at_property_declaration (parser);
21983 else if (token->keyword == RID_ATTRIBUTE
21984 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21985 warning_at (cp_lexer_peek_token (parser->lexer)->location,
21986 OPT_Wattributes,
21987 "prefix attributes are ignored for methods");
21988 else
21989 /* Allow for interspersed non-ObjC++ code. */
21990 cp_parser_objc_interstitial_code (parser);
21992 token = cp_lexer_peek_token (parser->lexer);
21995 if (token->type != CPP_EOF)
21996 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21997 else
21998 cp_parser_error (parser, "expected %<@end%>");
22000 objc_finish_interface ();
22003 /* Parse an Objective-C method definition list. */
22005 static void
22006 cp_parser_objc_method_definition_list (cp_parser* parser)
22008 cp_token *token = cp_lexer_peek_token (parser->lexer);
22010 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22012 tree meth;
22014 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22016 cp_token *ptk;
22017 tree sig, attribute;
22018 bool is_class_method;
22019 if (token->type == CPP_PLUS)
22020 is_class_method = true;
22021 else
22022 is_class_method = false;
22023 push_deferring_access_checks (dk_deferred);
22024 sig = cp_parser_objc_method_signature (parser, &attribute);
22025 if (sig == error_mark_node)
22027 cp_parser_skip_to_end_of_block_or_statement (parser);
22028 token = cp_lexer_peek_token (parser->lexer);
22029 continue;
22031 objc_start_method_definition (is_class_method, sig, attribute);
22033 /* For historical reasons, we accept an optional semicolon. */
22034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22035 cp_lexer_consume_token (parser->lexer);
22037 ptk = cp_lexer_peek_token (parser->lexer);
22038 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22039 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22041 perform_deferred_access_checks ();
22042 stop_deferring_access_checks ();
22043 meth = cp_parser_function_definition_after_declarator (parser,
22044 false);
22045 pop_deferring_access_checks ();
22046 objc_finish_method_definition (meth);
22049 /* The following case will be removed once @synthesize is
22050 completely implemented. */
22051 else if (token->keyword == RID_AT_PROPERTY)
22052 cp_parser_objc_at_property_declaration (parser);
22053 else if (token->keyword == RID_AT_SYNTHESIZE)
22054 cp_parser_objc_at_synthesize_declaration (parser);
22055 else if (token->keyword == RID_AT_DYNAMIC)
22056 cp_parser_objc_at_dynamic_declaration (parser);
22057 else if (token->keyword == RID_ATTRIBUTE
22058 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22059 warning_at (token->location, OPT_Wattributes,
22060 "prefix attributes are ignored for methods");
22061 else
22062 /* Allow for interspersed non-ObjC++ code. */
22063 cp_parser_objc_interstitial_code (parser);
22065 token = cp_lexer_peek_token (parser->lexer);
22068 if (token->type != CPP_EOF)
22069 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22070 else
22071 cp_parser_error (parser, "expected %<@end%>");
22073 objc_finish_implementation ();
22076 /* Parse Objective-C ivars. */
22078 static void
22079 cp_parser_objc_class_ivars (cp_parser* parser)
22081 cp_token *token = cp_lexer_peek_token (parser->lexer);
22083 if (token->type != CPP_OPEN_BRACE)
22084 return; /* No ivars specified. */
22086 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22087 token = cp_lexer_peek_token (parser->lexer);
22089 while (token->type != CPP_CLOSE_BRACE
22090 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22092 cp_decl_specifier_seq declspecs;
22093 int decl_class_or_enum_p;
22094 tree prefix_attributes;
22096 cp_parser_objc_visibility_spec (parser);
22098 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22099 break;
22101 cp_parser_decl_specifier_seq (parser,
22102 CP_PARSER_FLAGS_OPTIONAL,
22103 &declspecs,
22104 &decl_class_or_enum_p);
22106 /* auto, register, static, extern, mutable. */
22107 if (declspecs.storage_class != sc_none)
22109 cp_parser_error (parser, "invalid type for instance variable");
22110 declspecs.storage_class = sc_none;
22113 /* __thread. */
22114 if (declspecs.specs[(int) ds_thread])
22116 cp_parser_error (parser, "invalid type for instance variable");
22117 declspecs.specs[(int) ds_thread] = 0;
22120 /* typedef. */
22121 if (declspecs.specs[(int) ds_typedef])
22123 cp_parser_error (parser, "invalid type for instance variable");
22124 declspecs.specs[(int) ds_typedef] = 0;
22127 prefix_attributes = declspecs.attributes;
22128 declspecs.attributes = NULL_TREE;
22130 /* Keep going until we hit the `;' at the end of the
22131 declaration. */
22132 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22134 tree width = NULL_TREE, attributes, first_attribute, decl;
22135 cp_declarator *declarator = NULL;
22136 int ctor_dtor_or_conv_p;
22138 /* Check for a (possibly unnamed) bitfield declaration. */
22139 token = cp_lexer_peek_token (parser->lexer);
22140 if (token->type == CPP_COLON)
22141 goto eat_colon;
22143 if (token->type == CPP_NAME
22144 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22145 == CPP_COLON))
22147 /* Get the name of the bitfield. */
22148 declarator = make_id_declarator (NULL_TREE,
22149 cp_parser_identifier (parser),
22150 sfk_none);
22152 eat_colon:
22153 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22154 /* Get the width of the bitfield. */
22155 width
22156 = cp_parser_constant_expression (parser,
22157 /*allow_non_constant=*/false,
22158 NULL);
22160 else
22162 /* Parse the declarator. */
22163 declarator
22164 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22165 &ctor_dtor_or_conv_p,
22166 /*parenthesized_p=*/NULL,
22167 /*member_p=*/false);
22170 /* Look for attributes that apply to the ivar. */
22171 attributes = cp_parser_attributes_opt (parser);
22172 /* Remember which attributes are prefix attributes and
22173 which are not. */
22174 first_attribute = attributes;
22175 /* Combine the attributes. */
22176 attributes = chainon (prefix_attributes, attributes);
22178 if (width)
22179 /* Create the bitfield declaration. */
22180 decl = grokbitfield (declarator, &declspecs,
22181 width,
22182 attributes);
22183 else
22184 decl = grokfield (declarator, &declspecs,
22185 NULL_TREE, /*init_const_expr_p=*/false,
22186 NULL_TREE, attributes);
22188 /* Add the instance variable. */
22189 objc_add_instance_variable (decl);
22191 /* Reset PREFIX_ATTRIBUTES. */
22192 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22193 attributes = TREE_CHAIN (attributes);
22194 if (attributes)
22195 TREE_CHAIN (attributes) = NULL_TREE;
22197 token = cp_lexer_peek_token (parser->lexer);
22199 if (token->type == CPP_COMMA)
22201 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22202 continue;
22204 break;
22207 cp_parser_consume_semicolon_at_end_of_statement (parser);
22208 token = cp_lexer_peek_token (parser->lexer);
22211 if (token->keyword == RID_AT_END)
22212 cp_parser_error (parser, "expected %<}%>");
22214 /* Do not consume the RID_AT_END, so it will be read again as terminating
22215 the @interface of @implementation. */
22216 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22217 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22219 /* For historical reasons, we accept an optional semicolon. */
22220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22221 cp_lexer_consume_token (parser->lexer);
22224 /* Parse an Objective-C protocol declaration. */
22226 static void
22227 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22229 tree proto, protorefs;
22230 cp_token *tok;
22232 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22235 tok = cp_lexer_peek_token (parser->lexer);
22236 error_at (tok->location, "identifier expected after %<@protocol%>");
22237 goto finish;
22240 /* See if we have a forward declaration or a definition. */
22241 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22243 /* Try a forward declaration first. */
22244 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22246 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22247 finish:
22248 cp_parser_consume_semicolon_at_end_of_statement (parser);
22251 /* Ok, we got a full-fledged definition (or at least should). */
22252 else
22254 proto = cp_parser_identifier (parser);
22255 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22256 objc_start_protocol (proto, protorefs, attributes);
22257 cp_parser_objc_method_prototype_list (parser);
22261 /* Parse an Objective-C superclass or category. */
22263 static void
22264 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22265 tree *categ)
22267 cp_token *next = cp_lexer_peek_token (parser->lexer);
22269 *super = *categ = NULL_TREE;
22270 if (next->type == CPP_COLON)
22272 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22273 *super = cp_parser_identifier (parser);
22275 else if (next->type == CPP_OPEN_PAREN)
22277 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22278 *categ = cp_parser_identifier (parser);
22279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22283 /* Parse an Objective-C class interface. */
22285 static void
22286 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22288 tree name, super, categ, protos;
22290 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22291 name = cp_parser_identifier (parser);
22292 if (name == error_mark_node)
22294 /* It's hard to recover because even if valid @interface stuff
22295 is to follow, we can't compile it (or validate it) if we
22296 don't even know which class it refers to. Let's assume this
22297 was a stray '@interface' token in the stream and skip it.
22299 return;
22301 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22302 protos = cp_parser_objc_protocol_refs_opt (parser);
22304 /* We have either a class or a category on our hands. */
22305 if (categ)
22306 objc_start_category_interface (name, categ, protos, attributes);
22307 else
22309 objc_start_class_interface (name, super, protos, attributes);
22310 /* Handle instance variable declarations, if any. */
22311 cp_parser_objc_class_ivars (parser);
22312 objc_continue_interface ();
22315 cp_parser_objc_method_prototype_list (parser);
22318 /* Parse an Objective-C class implementation. */
22320 static void
22321 cp_parser_objc_class_implementation (cp_parser* parser)
22323 tree name, super, categ;
22325 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22326 name = cp_parser_identifier (parser);
22327 if (name == error_mark_node)
22329 /* It's hard to recover because even if valid @implementation
22330 stuff is to follow, we can't compile it (or validate it) if
22331 we don't even know which class it refers to. Let's assume
22332 this was a stray '@implementation' token in the stream and
22333 skip it.
22335 return;
22337 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22339 /* We have either a class or a category on our hands. */
22340 if (categ)
22341 objc_start_category_implementation (name, categ);
22342 else
22344 objc_start_class_implementation (name, super);
22345 /* Handle instance variable declarations, if any. */
22346 cp_parser_objc_class_ivars (parser);
22347 objc_continue_implementation ();
22350 cp_parser_objc_method_definition_list (parser);
22353 /* Consume the @end token and finish off the implementation. */
22355 static void
22356 cp_parser_objc_end_implementation (cp_parser* parser)
22358 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22359 objc_finish_implementation ();
22362 /* Parse an Objective-C declaration. */
22364 static void
22365 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22367 /* Try to figure out what kind of declaration is present. */
22368 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22370 if (attributes)
22371 switch (kwd->keyword)
22373 case RID_AT_ALIAS:
22374 case RID_AT_CLASS:
22375 case RID_AT_END:
22376 error_at (kwd->location, "attributes may not be specified before"
22377 " the %<@%D%> Objective-C++ keyword",
22378 kwd->u.value);
22379 attributes = NULL;
22380 break;
22381 case RID_AT_IMPLEMENTATION:
22382 warning_at (kwd->location, OPT_Wattributes,
22383 "prefix attributes are ignored before %<@%D%>",
22384 kwd->u.value);
22385 attributes = NULL;
22386 default:
22387 break;
22390 switch (kwd->keyword)
22392 case RID_AT_ALIAS:
22393 cp_parser_objc_alias_declaration (parser);
22394 break;
22395 case RID_AT_CLASS:
22396 cp_parser_objc_class_declaration (parser);
22397 break;
22398 case RID_AT_PROTOCOL:
22399 cp_parser_objc_protocol_declaration (parser, attributes);
22400 break;
22401 case RID_AT_INTERFACE:
22402 cp_parser_objc_class_interface (parser, attributes);
22403 break;
22404 case RID_AT_IMPLEMENTATION:
22405 cp_parser_objc_class_implementation (parser);
22406 break;
22407 case RID_AT_END:
22408 cp_parser_objc_end_implementation (parser);
22409 break;
22410 default:
22411 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22412 kwd->u.value);
22413 cp_parser_skip_to_end_of_block_or_statement (parser);
22417 /* Parse an Objective-C try-catch-finally statement.
22419 objc-try-catch-finally-stmt:
22420 @try compound-statement objc-catch-clause-seq [opt]
22421 objc-finally-clause [opt]
22423 objc-catch-clause-seq:
22424 objc-catch-clause objc-catch-clause-seq [opt]
22426 objc-catch-clause:
22427 @catch ( exception-declaration ) compound-statement
22429 objc-finally-clause
22430 @finally compound-statement
22432 Returns NULL_TREE. */
22434 static tree
22435 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22436 location_t location;
22437 tree stmt;
22439 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22440 location = cp_lexer_peek_token (parser->lexer)->location;
22441 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22442 node, lest it get absorbed into the surrounding block. */
22443 stmt = push_stmt_list ();
22444 cp_parser_compound_statement (parser, NULL, false);
22445 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22447 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22449 cp_parameter_declarator *parmdecl;
22450 tree parm;
22452 cp_lexer_consume_token (parser->lexer);
22453 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22454 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22455 parm = grokdeclarator (parmdecl->declarator,
22456 &parmdecl->decl_specifiers,
22457 PARM, /*initialized=*/0,
22458 /*attrlist=*/NULL);
22459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22460 objc_begin_catch_clause (parm);
22461 cp_parser_compound_statement (parser, NULL, false);
22462 objc_finish_catch_clause ();
22465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22467 cp_lexer_consume_token (parser->lexer);
22468 location = cp_lexer_peek_token (parser->lexer)->location;
22469 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22470 node, lest it get absorbed into the surrounding block. */
22471 stmt = push_stmt_list ();
22472 cp_parser_compound_statement (parser, NULL, false);
22473 objc_build_finally_clause (location, pop_stmt_list (stmt));
22476 return objc_finish_try_stmt ();
22479 /* Parse an Objective-C synchronized statement.
22481 objc-synchronized-stmt:
22482 @synchronized ( expression ) compound-statement
22484 Returns NULL_TREE. */
22486 static tree
22487 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22488 location_t location;
22489 tree lock, stmt;
22491 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22493 location = cp_lexer_peek_token (parser->lexer)->location;
22494 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22495 lock = cp_parser_expression (parser, false, NULL);
22496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22498 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22499 node, lest it get absorbed into the surrounding block. */
22500 stmt = push_stmt_list ();
22501 cp_parser_compound_statement (parser, NULL, false);
22503 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22506 /* Parse an Objective-C throw statement.
22508 objc-throw-stmt:
22509 @throw assignment-expression [opt] ;
22511 Returns a constructed '@throw' statement. */
22513 static tree
22514 cp_parser_objc_throw_statement (cp_parser *parser) {
22515 tree expr = NULL_TREE;
22516 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22518 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22521 expr = cp_parser_assignment_expression (parser, false, NULL);
22523 cp_parser_consume_semicolon_at_end_of_statement (parser);
22525 return objc_build_throw_stmt (loc, expr);
22528 /* Parse an Objective-C statement. */
22530 static tree
22531 cp_parser_objc_statement (cp_parser * parser) {
22532 /* Try to figure out what kind of declaration is present. */
22533 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22535 switch (kwd->keyword)
22537 case RID_AT_TRY:
22538 return cp_parser_objc_try_catch_finally_statement (parser);
22539 case RID_AT_SYNCHRONIZED:
22540 return cp_parser_objc_synchronized_statement (parser);
22541 case RID_AT_THROW:
22542 return cp_parser_objc_throw_statement (parser);
22543 default:
22544 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22545 kwd->u.value);
22546 cp_parser_skip_to_end_of_block_or_statement (parser);
22549 return error_mark_node;
22552 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22553 look ahead to see if an objc keyword follows the attributes. This
22554 is to detect the use of prefix attributes on ObjC @interface and
22555 @protocol. */
22557 static bool
22558 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22560 cp_lexer_save_tokens (parser->lexer);
22561 *attrib = cp_parser_attributes_opt (parser);
22562 gcc_assert (*attrib);
22563 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22565 cp_lexer_commit_tokens (parser->lexer);
22566 return true;
22568 cp_lexer_rollback_tokens (parser->lexer);
22569 return false;
22572 /* This routine is a minimal replacement for
22573 c_parser_struct_declaration () used when parsing the list of
22574 types/names or ObjC++ properties. For example, when parsing the
22575 code
22577 @property (readonly) int a, b, c;
22579 this function is responsible for parsing "int a, int b, int c" and
22580 returning the declarations as CHAIN of DECLs.
22582 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22583 similar parsing. */
22584 static tree
22585 cp_parser_objc_struct_declaration (cp_parser *parser)
22587 tree decls = NULL_TREE;
22588 cp_decl_specifier_seq declspecs;
22589 int decl_class_or_enum_p;
22590 tree prefix_attributes;
22592 cp_parser_decl_specifier_seq (parser,
22593 CP_PARSER_FLAGS_NONE,
22594 &declspecs,
22595 &decl_class_or_enum_p);
22597 if (declspecs.type == error_mark_node)
22598 return error_mark_node;
22600 /* auto, register, static, extern, mutable. */
22601 if (declspecs.storage_class != sc_none)
22603 cp_parser_error (parser, "invalid type for property");
22604 declspecs.storage_class = sc_none;
22607 /* __thread. */
22608 if (declspecs.specs[(int) ds_thread])
22610 cp_parser_error (parser, "invalid type for property");
22611 declspecs.specs[(int) ds_thread] = 0;
22614 /* typedef. */
22615 if (declspecs.specs[(int) ds_typedef])
22617 cp_parser_error (parser, "invalid type for property");
22618 declspecs.specs[(int) ds_typedef] = 0;
22621 prefix_attributes = declspecs.attributes;
22622 declspecs.attributes = NULL_TREE;
22624 /* Keep going until we hit the `;' at the end of the declaration. */
22625 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22627 tree attributes, first_attribute, decl;
22628 cp_declarator *declarator;
22629 cp_token *token;
22631 /* Parse the declarator. */
22632 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22633 NULL, NULL, false);
22635 /* Look for attributes that apply to the ivar. */
22636 attributes = cp_parser_attributes_opt (parser);
22637 /* Remember which attributes are prefix attributes and
22638 which are not. */
22639 first_attribute = attributes;
22640 /* Combine the attributes. */
22641 attributes = chainon (prefix_attributes, attributes);
22643 decl = grokfield (declarator, &declspecs,
22644 NULL_TREE, /*init_const_expr_p=*/false,
22645 NULL_TREE, attributes);
22647 if (decl == error_mark_node || decl == NULL_TREE)
22648 return error_mark_node;
22650 /* Reset PREFIX_ATTRIBUTES. */
22651 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22652 attributes = TREE_CHAIN (attributes);
22653 if (attributes)
22654 TREE_CHAIN (attributes) = NULL_TREE;
22656 DECL_CHAIN (decl) = decls;
22657 decls = decl;
22659 token = cp_lexer_peek_token (parser->lexer);
22660 if (token->type == CPP_COMMA)
22662 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22663 continue;
22665 else
22666 break;
22668 return decls;
22671 /* Parse an Objective-C @property declaration. The syntax is:
22673 objc-property-declaration:
22674 '@property' objc-property-attributes[opt] struct-declaration ;
22676 objc-property-attributes:
22677 '(' objc-property-attribute-list ')'
22679 objc-property-attribute-list:
22680 objc-property-attribute
22681 objc-property-attribute-list, objc-property-attribute
22683 objc-property-attribute
22684 'getter' = identifier
22685 'setter' = identifier
22686 'readonly'
22687 'readwrite'
22688 'assign'
22689 'retain'
22690 'copy'
22691 'nonatomic'
22693 For example:
22694 @property NSString *name;
22695 @property (readonly) id object;
22696 @property (retain, nonatomic, getter=getTheName) id name;
22697 @property int a, b, c;
22699 PS: This function is identical to
22700 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22701 static void
22702 cp_parser_objc_at_property_declaration (cp_parser *parser)
22704 /* The following variables hold the attributes of the properties as
22705 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22706 seen. When we see an attribute, we set them to 'true' (if they
22707 are boolean properties) or to the identifier (if they have an
22708 argument, ie, for getter and setter). Note that here we only
22709 parse the list of attributes, check the syntax and accumulate the
22710 attributes that we find. objc_add_property_declaration() will
22711 then process the information. */
22712 bool property_assign = false;
22713 bool property_copy = false;
22714 tree property_getter_ident = NULL_TREE;
22715 bool property_nonatomic = false;
22716 bool property_readonly = false;
22717 bool property_readwrite = false;
22718 bool property_retain = false;
22719 tree property_setter_ident = NULL_TREE;
22720 /* The following two will be removed once @synthesize is
22721 implemented. */
22722 bool property_copies = false;
22723 tree property_ivar_ident = NULL_TREE;
22725 /* 'properties' is the list of properties that we read. Usually a
22726 single one, but maybe more (eg, in "@property int a, b, c;" there
22727 are three). */
22728 tree properties;
22729 location_t loc;
22731 loc = cp_lexer_peek_token (parser->lexer)->location;
22733 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22735 /* Parse the optional attribute list... */
22736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22738 /* Eat the '('. */
22739 cp_lexer_consume_token (parser->lexer);
22741 while (true)
22743 bool syntax_error = false;
22744 cp_token *token = cp_lexer_peek_token (parser->lexer);
22745 enum rid keyword;
22747 if (token->type != CPP_NAME)
22749 cp_parser_error (parser, "expected identifier");
22750 break;
22752 keyword = C_RID_CODE (token->u.value);
22753 cp_lexer_consume_token (parser->lexer);
22754 switch (keyword)
22756 case RID_ASSIGN: property_assign = true; break;
22757 case RID_COPIES: property_copies = true; break;
22758 case RID_COPY: property_copy = true; break;
22759 case RID_NONATOMIC: property_nonatomic = true; break;
22760 case RID_READONLY: property_readonly = true; break;
22761 case RID_READWRITE: property_readwrite = true; break;
22762 case RID_RETAIN: property_retain = true; break;
22764 case RID_GETTER:
22765 case RID_SETTER:
22766 case RID_IVAR:
22767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22769 cp_parser_error (parser,
22770 "getter/setter/ivar attribute must be followed by %<=%>");
22771 syntax_error = true;
22772 break;
22774 cp_lexer_consume_token (parser->lexer); /* eat the = */
22775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22777 cp_parser_error (parser, "expected identifier");
22778 syntax_error = true;
22779 break;
22781 if (keyword == RID_SETTER)
22783 if (property_setter_ident != NULL_TREE)
22784 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22785 else
22786 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22787 cp_lexer_consume_token (parser->lexer);
22788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22789 cp_parser_error (parser, "setter name must terminate with %<:%>");
22790 else
22791 cp_lexer_consume_token (parser->lexer);
22793 else if (keyword == RID_GETTER)
22795 if (property_getter_ident != NULL_TREE)
22796 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22797 else
22798 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22799 cp_lexer_consume_token (parser->lexer);
22801 else /* RID_IVAR, this case will go away. */
22803 if (property_ivar_ident != NULL_TREE)
22804 cp_parser_error (parser, "the %<ivar%> attribute may only be specified once");
22805 else
22806 property_ivar_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22807 cp_lexer_consume_token (parser->lexer);
22809 break;
22810 default:
22811 cp_parser_error (parser, "unknown property attribute");
22812 syntax_error = true;
22813 break;
22816 if (syntax_error)
22817 break;
22819 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22820 cp_lexer_consume_token (parser->lexer);
22821 else
22822 break;
22825 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22827 cp_parser_skip_to_closing_parenthesis (parser,
22828 /*recovering=*/true,
22829 /*or_comma=*/false,
22830 /*consume_paren=*/true);
22834 /* ... and the property declaration(s). */
22835 properties = cp_parser_objc_struct_declaration (parser);
22837 if (properties == error_mark_node)
22839 cp_parser_skip_to_end_of_statement (parser);
22840 /* If the next token is now a `;', consume it. */
22841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22842 cp_lexer_consume_token (parser->lexer);
22843 return;
22846 if (properties == NULL_TREE)
22847 cp_parser_error (parser, "expected identifier");
22848 else
22850 /* Comma-separated properties are chained together in
22851 reverse order; add them one by one. */
22852 properties = nreverse (properties);
22854 for (; properties; properties = TREE_CHAIN (properties))
22855 objc_add_property_declaration (loc, copy_node (properties),
22856 property_readonly, property_readwrite,
22857 property_assign, property_retain,
22858 property_copy, property_nonatomic,
22859 property_getter_ident, property_setter_ident,
22860 /* The following two will be removed. */
22861 property_copies, property_ivar_ident);
22864 cp_parser_consume_semicolon_at_end_of_statement (parser);
22867 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22869 objc-synthesize-declaration:
22870 @synthesize objc-synthesize-identifier-list ;
22872 objc-synthesize-identifier-list:
22873 objc-synthesize-identifier
22874 objc-synthesize-identifier-list, objc-synthesize-identifier
22876 objc-synthesize-identifier
22877 identifier
22878 identifier = identifier
22880 For example:
22881 @synthesize MyProperty;
22882 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22884 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22885 for C. Keep them in sync.
22887 static void
22888 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22890 tree list = NULL_TREE;
22891 location_t loc;
22892 loc = cp_lexer_peek_token (parser->lexer)->location;
22894 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
22895 while (true)
22897 tree property, ivar;
22898 property = cp_parser_identifier (parser);
22899 if (property == error_mark_node)
22901 cp_parser_consume_semicolon_at_end_of_statement (parser);
22902 return;
22904 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22906 cp_lexer_consume_token (parser->lexer);
22907 ivar = cp_parser_identifier (parser);
22908 if (ivar == error_mark_node)
22910 cp_parser_consume_semicolon_at_end_of_statement (parser);
22911 return;
22914 else
22915 ivar = NULL_TREE;
22916 list = chainon (list, build_tree_list (ivar, property));
22917 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22918 cp_lexer_consume_token (parser->lexer);
22919 else
22920 break;
22922 cp_parser_consume_semicolon_at_end_of_statement (parser);
22923 objc_add_synthesize_declaration (loc, list);
22926 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22928 objc-dynamic-declaration:
22929 @dynamic identifier-list ;
22931 For example:
22932 @dynamic MyProperty;
22933 @dynamic MyProperty, AnotherProperty;
22935 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22936 for C. Keep them in sync.
22938 static void
22939 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22941 tree list = NULL_TREE;
22942 location_t loc;
22943 loc = cp_lexer_peek_token (parser->lexer)->location;
22945 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
22946 while (true)
22948 tree property;
22949 property = cp_parser_identifier (parser);
22950 if (property == error_mark_node)
22952 cp_parser_consume_semicolon_at_end_of_statement (parser);
22953 return;
22955 list = chainon (list, build_tree_list (NULL, property));
22956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22957 cp_lexer_consume_token (parser->lexer);
22958 else
22959 break;
22961 cp_parser_consume_semicolon_at_end_of_statement (parser);
22962 objc_add_dynamic_declaration (loc, list);
22966 /* OpenMP 2.5 parsing routines. */
22968 /* Returns name of the next clause.
22969 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22970 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22971 returned and the token is consumed. */
22973 static pragma_omp_clause
22974 cp_parser_omp_clause_name (cp_parser *parser)
22976 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22979 result = PRAGMA_OMP_CLAUSE_IF;
22980 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22981 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22982 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22983 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22984 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22986 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22987 const char *p = IDENTIFIER_POINTER (id);
22989 switch (p[0])
22991 case 'c':
22992 if (!strcmp ("collapse", p))
22993 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22994 else if (!strcmp ("copyin", p))
22995 result = PRAGMA_OMP_CLAUSE_COPYIN;
22996 else if (!strcmp ("copyprivate", p))
22997 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22998 break;
22999 case 'f':
23000 if (!strcmp ("firstprivate", p))
23001 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23002 break;
23003 case 'l':
23004 if (!strcmp ("lastprivate", p))
23005 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23006 break;
23007 case 'n':
23008 if (!strcmp ("nowait", p))
23009 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23010 else if (!strcmp ("num_threads", p))
23011 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23012 break;
23013 case 'o':
23014 if (!strcmp ("ordered", p))
23015 result = PRAGMA_OMP_CLAUSE_ORDERED;
23016 break;
23017 case 'r':
23018 if (!strcmp ("reduction", p))
23019 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23020 break;
23021 case 's':
23022 if (!strcmp ("schedule", p))
23023 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23024 else if (!strcmp ("shared", p))
23025 result = PRAGMA_OMP_CLAUSE_SHARED;
23026 break;
23027 case 'u':
23028 if (!strcmp ("untied", p))
23029 result = PRAGMA_OMP_CLAUSE_UNTIED;
23030 break;
23034 if (result != PRAGMA_OMP_CLAUSE_NONE)
23035 cp_lexer_consume_token (parser->lexer);
23037 return result;
23040 /* Validate that a clause of the given type does not already exist. */
23042 static void
23043 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23044 const char *name, location_t location)
23046 tree c;
23048 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23049 if (OMP_CLAUSE_CODE (c) == code)
23051 error_at (location, "too many %qs clauses", name);
23052 break;
23056 /* OpenMP 2.5:
23057 variable-list:
23058 identifier
23059 variable-list , identifier
23061 In addition, we match a closing parenthesis. An opening parenthesis
23062 will have been consumed by the caller.
23064 If KIND is nonzero, create the appropriate node and install the decl
23065 in OMP_CLAUSE_DECL and add the node to the head of the list.
23067 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23068 return the list created. */
23070 static tree
23071 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23072 tree list)
23074 cp_token *token;
23075 while (1)
23077 tree name, decl;
23079 token = cp_lexer_peek_token (parser->lexer);
23080 name = cp_parser_id_expression (parser, /*template_p=*/false,
23081 /*check_dependency_p=*/true,
23082 /*template_p=*/NULL,
23083 /*declarator_p=*/false,
23084 /*optional_p=*/false);
23085 if (name == error_mark_node)
23086 goto skip_comma;
23088 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23089 if (decl == error_mark_node)
23090 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23091 token->location);
23092 else if (kind != 0)
23094 tree u = build_omp_clause (token->location, kind);
23095 OMP_CLAUSE_DECL (u) = decl;
23096 OMP_CLAUSE_CHAIN (u) = list;
23097 list = u;
23099 else
23100 list = tree_cons (decl, NULL_TREE, list);
23102 get_comma:
23103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23104 break;
23105 cp_lexer_consume_token (parser->lexer);
23108 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23110 int ending;
23112 /* Try to resync to an unnested comma. Copied from
23113 cp_parser_parenthesized_expression_list. */
23114 skip_comma:
23115 ending = cp_parser_skip_to_closing_parenthesis (parser,
23116 /*recovering=*/true,
23117 /*or_comma=*/true,
23118 /*consume_paren=*/true);
23119 if (ending < 0)
23120 goto get_comma;
23123 return list;
23126 /* Similarly, but expect leading and trailing parenthesis. This is a very
23127 common case for omp clauses. */
23129 static tree
23130 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23132 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23133 return cp_parser_omp_var_list_no_open (parser, kind, list);
23134 return list;
23137 /* OpenMP 3.0:
23138 collapse ( constant-expression ) */
23140 static tree
23141 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23143 tree c, num;
23144 location_t loc;
23145 HOST_WIDE_INT n;
23147 loc = cp_lexer_peek_token (parser->lexer)->location;
23148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23149 return list;
23151 num = cp_parser_constant_expression (parser, false, NULL);
23153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23155 /*or_comma=*/false,
23156 /*consume_paren=*/true);
23158 if (num == error_mark_node)
23159 return list;
23160 num = fold_non_dependent_expr (num);
23161 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23162 || !host_integerp (num, 0)
23163 || (n = tree_low_cst (num, 0)) <= 0
23164 || (int) n != n)
23166 error_at (loc, "collapse argument needs positive constant integer expression");
23167 return list;
23170 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23171 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23172 OMP_CLAUSE_CHAIN (c) = list;
23173 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23175 return c;
23178 /* OpenMP 2.5:
23179 default ( shared | none ) */
23181 static tree
23182 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23184 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23185 tree c;
23187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23188 return list;
23189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23192 const char *p = IDENTIFIER_POINTER (id);
23194 switch (p[0])
23196 case 'n':
23197 if (strcmp ("none", p) != 0)
23198 goto invalid_kind;
23199 kind = OMP_CLAUSE_DEFAULT_NONE;
23200 break;
23202 case 's':
23203 if (strcmp ("shared", p) != 0)
23204 goto invalid_kind;
23205 kind = OMP_CLAUSE_DEFAULT_SHARED;
23206 break;
23208 default:
23209 goto invalid_kind;
23212 cp_lexer_consume_token (parser->lexer);
23214 else
23216 invalid_kind:
23217 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23222 /*or_comma=*/false,
23223 /*consume_paren=*/true);
23225 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23226 return list;
23228 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23229 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23230 OMP_CLAUSE_CHAIN (c) = list;
23231 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23233 return c;
23236 /* OpenMP 2.5:
23237 if ( expression ) */
23239 static tree
23240 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23242 tree t, c;
23244 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23245 return list;
23247 t = cp_parser_condition (parser);
23249 if (t == error_mark_node
23250 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23252 /*or_comma=*/false,
23253 /*consume_paren=*/true);
23255 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23257 c = build_omp_clause (location, OMP_CLAUSE_IF);
23258 OMP_CLAUSE_IF_EXPR (c) = t;
23259 OMP_CLAUSE_CHAIN (c) = list;
23261 return c;
23264 /* OpenMP 2.5:
23265 nowait */
23267 static tree
23268 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23269 tree list, location_t location)
23271 tree c;
23273 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23275 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23276 OMP_CLAUSE_CHAIN (c) = list;
23277 return c;
23280 /* OpenMP 2.5:
23281 num_threads ( expression ) */
23283 static tree
23284 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23285 location_t location)
23287 tree t, c;
23289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23290 return list;
23292 t = cp_parser_expression (parser, false, NULL);
23294 if (t == error_mark_node
23295 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23297 /*or_comma=*/false,
23298 /*consume_paren=*/true);
23300 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23301 "num_threads", location);
23303 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23304 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23305 OMP_CLAUSE_CHAIN (c) = list;
23307 return c;
23310 /* OpenMP 2.5:
23311 ordered */
23313 static tree
23314 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23315 tree list, location_t location)
23317 tree c;
23319 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23320 "ordered", location);
23322 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23323 OMP_CLAUSE_CHAIN (c) = list;
23324 return c;
23327 /* OpenMP 2.5:
23328 reduction ( reduction-operator : variable-list )
23330 reduction-operator:
23331 One of: + * - & ^ | && || */
23333 static tree
23334 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23336 enum tree_code code;
23337 tree nlist, c;
23339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23340 return list;
23342 switch (cp_lexer_peek_token (parser->lexer)->type)
23344 case CPP_PLUS:
23345 code = PLUS_EXPR;
23346 break;
23347 case CPP_MULT:
23348 code = MULT_EXPR;
23349 break;
23350 case CPP_MINUS:
23351 code = MINUS_EXPR;
23352 break;
23353 case CPP_AND:
23354 code = BIT_AND_EXPR;
23355 break;
23356 case CPP_XOR:
23357 code = BIT_XOR_EXPR;
23358 break;
23359 case CPP_OR:
23360 code = BIT_IOR_EXPR;
23361 break;
23362 case CPP_AND_AND:
23363 code = TRUTH_ANDIF_EXPR;
23364 break;
23365 case CPP_OR_OR:
23366 code = TRUTH_ORIF_EXPR;
23367 break;
23368 default:
23369 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23370 "%<|%>, %<&&%>, or %<||%>");
23371 resync_fail:
23372 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23373 /*or_comma=*/false,
23374 /*consume_paren=*/true);
23375 return list;
23377 cp_lexer_consume_token (parser->lexer);
23379 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23380 goto resync_fail;
23382 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23383 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23384 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23386 return nlist;
23389 /* OpenMP 2.5:
23390 schedule ( schedule-kind )
23391 schedule ( schedule-kind , expression )
23393 schedule-kind:
23394 static | dynamic | guided | runtime | auto */
23396 static tree
23397 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23399 tree c, t;
23401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23402 return list;
23404 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23408 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23409 const char *p = IDENTIFIER_POINTER (id);
23411 switch (p[0])
23413 case 'd':
23414 if (strcmp ("dynamic", p) != 0)
23415 goto invalid_kind;
23416 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23417 break;
23419 case 'g':
23420 if (strcmp ("guided", p) != 0)
23421 goto invalid_kind;
23422 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23423 break;
23425 case 'r':
23426 if (strcmp ("runtime", p) != 0)
23427 goto invalid_kind;
23428 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23429 break;
23431 default:
23432 goto invalid_kind;
23435 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23436 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23437 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23438 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23439 else
23440 goto invalid_kind;
23441 cp_lexer_consume_token (parser->lexer);
23443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23445 cp_token *token;
23446 cp_lexer_consume_token (parser->lexer);
23448 token = cp_lexer_peek_token (parser->lexer);
23449 t = cp_parser_assignment_expression (parser, false, NULL);
23451 if (t == error_mark_node)
23452 goto resync_fail;
23453 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23454 error_at (token->location, "schedule %<runtime%> does not take "
23455 "a %<chunk_size%> parameter");
23456 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23457 error_at (token->location, "schedule %<auto%> does not take "
23458 "a %<chunk_size%> parameter");
23459 else
23460 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23462 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23463 goto resync_fail;
23465 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23466 goto resync_fail;
23468 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23469 OMP_CLAUSE_CHAIN (c) = list;
23470 return c;
23472 invalid_kind:
23473 cp_parser_error (parser, "invalid schedule kind");
23474 resync_fail:
23475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23476 /*or_comma=*/false,
23477 /*consume_paren=*/true);
23478 return list;
23481 /* OpenMP 3.0:
23482 untied */
23484 static tree
23485 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23486 tree list, location_t location)
23488 tree c;
23490 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23492 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23493 OMP_CLAUSE_CHAIN (c) = list;
23494 return c;
23497 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23498 is a bitmask in MASK. Return the list of clauses found; the result
23499 of clause default goes in *pdefault. */
23501 static tree
23502 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23503 const char *where, cp_token *pragma_tok)
23505 tree clauses = NULL;
23506 bool first = true;
23507 cp_token *token = NULL;
23509 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23511 pragma_omp_clause c_kind;
23512 const char *c_name;
23513 tree prev = clauses;
23515 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23516 cp_lexer_consume_token (parser->lexer);
23518 token = cp_lexer_peek_token (parser->lexer);
23519 c_kind = cp_parser_omp_clause_name (parser);
23520 first = false;
23522 switch (c_kind)
23524 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23525 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23526 token->location);
23527 c_name = "collapse";
23528 break;
23529 case PRAGMA_OMP_CLAUSE_COPYIN:
23530 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23531 c_name = "copyin";
23532 break;
23533 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23534 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23535 clauses);
23536 c_name = "copyprivate";
23537 break;
23538 case PRAGMA_OMP_CLAUSE_DEFAULT:
23539 clauses = cp_parser_omp_clause_default (parser, clauses,
23540 token->location);
23541 c_name = "default";
23542 break;
23543 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23545 clauses);
23546 c_name = "firstprivate";
23547 break;
23548 case PRAGMA_OMP_CLAUSE_IF:
23549 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23550 c_name = "if";
23551 break;
23552 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23553 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23554 clauses);
23555 c_name = "lastprivate";
23556 break;
23557 case PRAGMA_OMP_CLAUSE_NOWAIT:
23558 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23559 c_name = "nowait";
23560 break;
23561 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23562 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23563 token->location);
23564 c_name = "num_threads";
23565 break;
23566 case PRAGMA_OMP_CLAUSE_ORDERED:
23567 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23568 token->location);
23569 c_name = "ordered";
23570 break;
23571 case PRAGMA_OMP_CLAUSE_PRIVATE:
23572 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23573 clauses);
23574 c_name = "private";
23575 break;
23576 case PRAGMA_OMP_CLAUSE_REDUCTION:
23577 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23578 c_name = "reduction";
23579 break;
23580 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23581 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23582 token->location);
23583 c_name = "schedule";
23584 break;
23585 case PRAGMA_OMP_CLAUSE_SHARED:
23586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23587 clauses);
23588 c_name = "shared";
23589 break;
23590 case PRAGMA_OMP_CLAUSE_UNTIED:
23591 clauses = cp_parser_omp_clause_untied (parser, clauses,
23592 token->location);
23593 c_name = "nowait";
23594 break;
23595 default:
23596 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23597 goto saw_error;
23600 if (((mask >> c_kind) & 1) == 0)
23602 /* Remove the invalid clause(s) from the list to avoid
23603 confusing the rest of the compiler. */
23604 clauses = prev;
23605 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23608 saw_error:
23609 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23610 return finish_omp_clauses (clauses);
23613 /* OpenMP 2.5:
23614 structured-block:
23615 statement
23617 In practice, we're also interested in adding the statement to an
23618 outer node. So it is convenient if we work around the fact that
23619 cp_parser_statement calls add_stmt. */
23621 static unsigned
23622 cp_parser_begin_omp_structured_block (cp_parser *parser)
23624 unsigned save = parser->in_statement;
23626 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23627 This preserves the "not within loop or switch" style error messages
23628 for nonsense cases like
23629 void foo() {
23630 #pragma omp single
23631 break;
23634 if (parser->in_statement)
23635 parser->in_statement = IN_OMP_BLOCK;
23637 return save;
23640 static void
23641 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23643 parser->in_statement = save;
23646 static tree
23647 cp_parser_omp_structured_block (cp_parser *parser)
23649 tree stmt = begin_omp_structured_block ();
23650 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23652 cp_parser_statement (parser, NULL_TREE, false, NULL);
23654 cp_parser_end_omp_structured_block (parser, save);
23655 return finish_omp_structured_block (stmt);
23658 /* OpenMP 2.5:
23659 # pragma omp atomic new-line
23660 expression-stmt
23662 expression-stmt:
23663 x binop= expr | x++ | ++x | x-- | --x
23664 binop:
23665 +, *, -, /, &, ^, |, <<, >>
23667 where x is an lvalue expression with scalar type. */
23669 static void
23670 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23672 tree lhs, rhs;
23673 enum tree_code code;
23675 cp_parser_require_pragma_eol (parser, pragma_tok);
23677 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23678 /*cast_p=*/false, NULL);
23679 switch (TREE_CODE (lhs))
23681 case ERROR_MARK:
23682 goto saw_error;
23684 case PREINCREMENT_EXPR:
23685 case POSTINCREMENT_EXPR:
23686 lhs = TREE_OPERAND (lhs, 0);
23687 code = PLUS_EXPR;
23688 rhs = integer_one_node;
23689 break;
23691 case PREDECREMENT_EXPR:
23692 case POSTDECREMENT_EXPR:
23693 lhs = TREE_OPERAND (lhs, 0);
23694 code = MINUS_EXPR;
23695 rhs = integer_one_node;
23696 break;
23698 case COMPOUND_EXPR:
23699 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23700 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23701 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23702 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23703 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23704 (TREE_OPERAND (lhs, 1), 0), 0)))
23705 == BOOLEAN_TYPE)
23706 /* Undo effects of boolean_increment for post {in,de}crement. */
23707 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23708 /* FALLTHRU */
23709 case MODIFY_EXPR:
23710 if (TREE_CODE (lhs) == MODIFY_EXPR
23711 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23713 /* Undo effects of boolean_increment. */
23714 if (integer_onep (TREE_OPERAND (lhs, 1)))
23716 /* This is pre or post increment. */
23717 rhs = TREE_OPERAND (lhs, 1);
23718 lhs = TREE_OPERAND (lhs, 0);
23719 code = NOP_EXPR;
23720 break;
23723 /* FALLTHRU */
23724 default:
23725 switch (cp_lexer_peek_token (parser->lexer)->type)
23727 case CPP_MULT_EQ:
23728 code = MULT_EXPR;
23729 break;
23730 case CPP_DIV_EQ:
23731 code = TRUNC_DIV_EXPR;
23732 break;
23733 case CPP_PLUS_EQ:
23734 code = PLUS_EXPR;
23735 break;
23736 case CPP_MINUS_EQ:
23737 code = MINUS_EXPR;
23738 break;
23739 case CPP_LSHIFT_EQ:
23740 code = LSHIFT_EXPR;
23741 break;
23742 case CPP_RSHIFT_EQ:
23743 code = RSHIFT_EXPR;
23744 break;
23745 case CPP_AND_EQ:
23746 code = BIT_AND_EXPR;
23747 break;
23748 case CPP_OR_EQ:
23749 code = BIT_IOR_EXPR;
23750 break;
23751 case CPP_XOR_EQ:
23752 code = BIT_XOR_EXPR;
23753 break;
23754 default:
23755 cp_parser_error (parser,
23756 "invalid operator for %<#pragma omp atomic%>");
23757 goto saw_error;
23759 cp_lexer_consume_token (parser->lexer);
23761 rhs = cp_parser_expression (parser, false, NULL);
23762 if (rhs == error_mark_node)
23763 goto saw_error;
23764 break;
23766 finish_omp_atomic (code, lhs, rhs);
23767 cp_parser_consume_semicolon_at_end_of_statement (parser);
23768 return;
23770 saw_error:
23771 cp_parser_skip_to_end_of_block_or_statement (parser);
23775 /* OpenMP 2.5:
23776 # pragma omp barrier new-line */
23778 static void
23779 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23781 cp_parser_require_pragma_eol (parser, pragma_tok);
23782 finish_omp_barrier ();
23785 /* OpenMP 2.5:
23786 # pragma omp critical [(name)] new-line
23787 structured-block */
23789 static tree
23790 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23792 tree stmt, name = NULL;
23794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23796 cp_lexer_consume_token (parser->lexer);
23798 name = cp_parser_identifier (parser);
23800 if (name == error_mark_node
23801 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23802 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23803 /*or_comma=*/false,
23804 /*consume_paren=*/true);
23805 if (name == error_mark_node)
23806 name = NULL;
23808 cp_parser_require_pragma_eol (parser, pragma_tok);
23810 stmt = cp_parser_omp_structured_block (parser);
23811 return c_finish_omp_critical (input_location, stmt, name);
23814 /* OpenMP 2.5:
23815 # pragma omp flush flush-vars[opt] new-line
23817 flush-vars:
23818 ( variable-list ) */
23820 static void
23821 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23824 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23825 cp_parser_require_pragma_eol (parser, pragma_tok);
23827 finish_omp_flush ();
23830 /* Helper function, to parse omp for increment expression. */
23832 static tree
23833 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23835 tree cond = cp_parser_binary_expression (parser, false, true,
23836 PREC_NOT_OPERATOR, NULL);
23837 bool overloaded_p;
23839 if (cond == error_mark_node
23840 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23842 cp_parser_skip_to_end_of_statement (parser);
23843 return error_mark_node;
23846 switch (TREE_CODE (cond))
23848 case GT_EXPR:
23849 case GE_EXPR:
23850 case LT_EXPR:
23851 case LE_EXPR:
23852 break;
23853 default:
23854 return error_mark_node;
23857 /* If decl is an iterator, preserve LHS and RHS of the relational
23858 expr until finish_omp_for. */
23859 if (decl
23860 && (type_dependent_expression_p (decl)
23861 || CLASS_TYPE_P (TREE_TYPE (decl))))
23862 return cond;
23864 return build_x_binary_op (TREE_CODE (cond),
23865 TREE_OPERAND (cond, 0), ERROR_MARK,
23866 TREE_OPERAND (cond, 1), ERROR_MARK,
23867 &overloaded_p, tf_warning_or_error);
23870 /* Helper function, to parse omp for increment expression. */
23872 static tree
23873 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23875 cp_token *token = cp_lexer_peek_token (parser->lexer);
23876 enum tree_code op;
23877 tree lhs, rhs;
23878 cp_id_kind idk;
23879 bool decl_first;
23881 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23883 op = (token->type == CPP_PLUS_PLUS
23884 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23885 cp_lexer_consume_token (parser->lexer);
23886 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23887 if (lhs != decl)
23888 return error_mark_node;
23889 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23892 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23893 if (lhs != decl)
23894 return error_mark_node;
23896 token = cp_lexer_peek_token (parser->lexer);
23897 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23899 op = (token->type == CPP_PLUS_PLUS
23900 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23901 cp_lexer_consume_token (parser->lexer);
23902 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23905 op = cp_parser_assignment_operator_opt (parser);
23906 if (op == ERROR_MARK)
23907 return error_mark_node;
23909 if (op != NOP_EXPR)
23911 rhs = cp_parser_assignment_expression (parser, false, NULL);
23912 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23913 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23916 lhs = cp_parser_binary_expression (parser, false, false,
23917 PREC_ADDITIVE_EXPRESSION, NULL);
23918 token = cp_lexer_peek_token (parser->lexer);
23919 decl_first = lhs == decl;
23920 if (decl_first)
23921 lhs = NULL_TREE;
23922 if (token->type != CPP_PLUS
23923 && token->type != CPP_MINUS)
23924 return error_mark_node;
23928 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23929 cp_lexer_consume_token (parser->lexer);
23930 rhs = cp_parser_binary_expression (parser, false, false,
23931 PREC_ADDITIVE_EXPRESSION, NULL);
23932 token = cp_lexer_peek_token (parser->lexer);
23933 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23935 if (lhs == NULL_TREE)
23937 if (op == PLUS_EXPR)
23938 lhs = rhs;
23939 else
23940 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23942 else
23943 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23944 NULL, tf_warning_or_error);
23947 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23949 if (!decl_first)
23951 if (rhs != decl || op == MINUS_EXPR)
23952 return error_mark_node;
23953 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23955 else
23956 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23958 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23961 /* Parse the restricted form of the for statement allowed by OpenMP. */
23963 static tree
23964 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23966 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23967 tree real_decl, initv, condv, incrv, declv;
23968 tree this_pre_body, cl;
23969 location_t loc_first;
23970 bool collapse_err = false;
23971 int i, collapse = 1, nbraces = 0;
23972 VEC(tree,gc) *for_block = make_tree_vector ();
23974 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23975 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23976 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23978 gcc_assert (collapse >= 1);
23980 declv = make_tree_vec (collapse);
23981 initv = make_tree_vec (collapse);
23982 condv = make_tree_vec (collapse);
23983 incrv = make_tree_vec (collapse);
23985 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23987 for (i = 0; i < collapse; i++)
23989 int bracecount = 0;
23990 bool add_private_clause = false;
23991 location_t loc;
23993 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23995 cp_parser_error (parser, "for statement expected");
23996 return NULL;
23998 loc = cp_lexer_consume_token (parser->lexer)->location;
24000 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24001 return NULL;
24003 init = decl = real_decl = NULL;
24004 this_pre_body = push_stmt_list ();
24005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24007 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24009 init-expr:
24010 var = lb
24011 integer-type var = lb
24012 random-access-iterator-type var = lb
24013 pointer-type var = lb
24015 cp_decl_specifier_seq type_specifiers;
24017 /* First, try to parse as an initialized declaration. See
24018 cp_parser_condition, from whence the bulk of this is copied. */
24020 cp_parser_parse_tentatively (parser);
24021 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24022 /*is_trailing_return=*/false,
24023 &type_specifiers);
24024 if (cp_parser_parse_definitely (parser))
24026 /* If parsing a type specifier seq succeeded, then this
24027 MUST be a initialized declaration. */
24028 tree asm_specification, attributes;
24029 cp_declarator *declarator;
24031 declarator = cp_parser_declarator (parser,
24032 CP_PARSER_DECLARATOR_NAMED,
24033 /*ctor_dtor_or_conv_p=*/NULL,
24034 /*parenthesized_p=*/NULL,
24035 /*member_p=*/false);
24036 attributes = cp_parser_attributes_opt (parser);
24037 asm_specification = cp_parser_asm_specification_opt (parser);
24039 if (declarator == cp_error_declarator)
24040 cp_parser_skip_to_end_of_statement (parser);
24042 else
24044 tree pushed_scope, auto_node;
24046 decl = start_decl (declarator, &type_specifiers,
24047 SD_INITIALIZED, attributes,
24048 /*prefix_attributes=*/NULL_TREE,
24049 &pushed_scope);
24051 auto_node = type_uses_auto (TREE_TYPE (decl));
24052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24054 if (cp_lexer_next_token_is (parser->lexer,
24055 CPP_OPEN_PAREN))
24056 error ("parenthesized initialization is not allowed in "
24057 "OpenMP %<for%> loop");
24058 else
24059 /* Trigger an error. */
24060 cp_parser_require (parser, CPP_EQ, RT_EQ);
24062 init = error_mark_node;
24063 cp_parser_skip_to_end_of_statement (parser);
24065 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24066 || type_dependent_expression_p (decl)
24067 || auto_node)
24069 bool is_direct_init, is_non_constant_init;
24071 init = cp_parser_initializer (parser,
24072 &is_direct_init,
24073 &is_non_constant_init);
24075 if (auto_node && describable_type (init))
24077 TREE_TYPE (decl)
24078 = do_auto_deduction (TREE_TYPE (decl), init,
24079 auto_node);
24081 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24082 && !type_dependent_expression_p (decl))
24083 goto non_class;
24086 cp_finish_decl (decl, init, !is_non_constant_init,
24087 asm_specification,
24088 LOOKUP_ONLYCONVERTING);
24089 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24091 VEC_safe_push (tree, gc, for_block, this_pre_body);
24092 init = NULL_TREE;
24094 else
24095 init = pop_stmt_list (this_pre_body);
24096 this_pre_body = NULL_TREE;
24098 else
24100 /* Consume '='. */
24101 cp_lexer_consume_token (parser->lexer);
24102 init = cp_parser_assignment_expression (parser, false, NULL);
24104 non_class:
24105 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24106 init = error_mark_node;
24107 else
24108 cp_finish_decl (decl, NULL_TREE,
24109 /*init_const_expr_p=*/false,
24110 asm_specification,
24111 LOOKUP_ONLYCONVERTING);
24114 if (pushed_scope)
24115 pop_scope (pushed_scope);
24118 else
24120 cp_id_kind idk;
24121 /* If parsing a type specifier sequence failed, then
24122 this MUST be a simple expression. */
24123 cp_parser_parse_tentatively (parser);
24124 decl = cp_parser_primary_expression (parser, false, false,
24125 false, &idk);
24126 if (!cp_parser_error_occurred (parser)
24127 && decl
24128 && DECL_P (decl)
24129 && CLASS_TYPE_P (TREE_TYPE (decl)))
24131 tree rhs;
24133 cp_parser_parse_definitely (parser);
24134 cp_parser_require (parser, CPP_EQ, RT_EQ);
24135 rhs = cp_parser_assignment_expression (parser, false, NULL);
24136 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24137 rhs,
24138 tf_warning_or_error));
24139 add_private_clause = true;
24141 else
24143 decl = NULL;
24144 cp_parser_abort_tentative_parse (parser);
24145 init = cp_parser_expression (parser, false, NULL);
24146 if (init)
24148 if (TREE_CODE (init) == MODIFY_EXPR
24149 || TREE_CODE (init) == MODOP_EXPR)
24150 real_decl = TREE_OPERAND (init, 0);
24155 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24156 if (this_pre_body)
24158 this_pre_body = pop_stmt_list (this_pre_body);
24159 if (pre_body)
24161 tree t = pre_body;
24162 pre_body = push_stmt_list ();
24163 add_stmt (t);
24164 add_stmt (this_pre_body);
24165 pre_body = pop_stmt_list (pre_body);
24167 else
24168 pre_body = this_pre_body;
24171 if (decl)
24172 real_decl = decl;
24173 if (par_clauses != NULL && real_decl != NULL_TREE)
24175 tree *c;
24176 for (c = par_clauses; *c ; )
24177 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24178 && OMP_CLAUSE_DECL (*c) == real_decl)
24180 error_at (loc, "iteration variable %qD"
24181 " should not be firstprivate", real_decl);
24182 *c = OMP_CLAUSE_CHAIN (*c);
24184 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24185 && OMP_CLAUSE_DECL (*c) == real_decl)
24187 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24188 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24189 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24190 OMP_CLAUSE_DECL (l) = real_decl;
24191 OMP_CLAUSE_CHAIN (l) = clauses;
24192 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24193 clauses = l;
24194 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24195 CP_OMP_CLAUSE_INFO (*c) = NULL;
24196 add_private_clause = false;
24198 else
24200 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24201 && OMP_CLAUSE_DECL (*c) == real_decl)
24202 add_private_clause = false;
24203 c = &OMP_CLAUSE_CHAIN (*c);
24207 if (add_private_clause)
24209 tree c;
24210 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24212 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24213 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24214 && OMP_CLAUSE_DECL (c) == decl)
24215 break;
24216 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24217 && OMP_CLAUSE_DECL (c) == decl)
24218 error_at (loc, "iteration variable %qD "
24219 "should not be firstprivate",
24220 decl);
24221 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24222 && OMP_CLAUSE_DECL (c) == decl)
24223 error_at (loc, "iteration variable %qD should not be reduction",
24224 decl);
24226 if (c == NULL)
24228 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24229 OMP_CLAUSE_DECL (c) = decl;
24230 c = finish_omp_clauses (c);
24231 if (c)
24233 OMP_CLAUSE_CHAIN (c) = clauses;
24234 clauses = c;
24239 cond = NULL;
24240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24241 cond = cp_parser_omp_for_cond (parser, decl);
24242 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24244 incr = NULL;
24245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24247 /* If decl is an iterator, preserve the operator on decl
24248 until finish_omp_for. */
24249 if (decl
24250 && (type_dependent_expression_p (decl)
24251 || CLASS_TYPE_P (TREE_TYPE (decl))))
24252 incr = cp_parser_omp_for_incr (parser, decl);
24253 else
24254 incr = cp_parser_expression (parser, false, NULL);
24257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24259 /*or_comma=*/false,
24260 /*consume_paren=*/true);
24262 TREE_VEC_ELT (declv, i) = decl;
24263 TREE_VEC_ELT (initv, i) = init;
24264 TREE_VEC_ELT (condv, i) = cond;
24265 TREE_VEC_ELT (incrv, i) = incr;
24267 if (i == collapse - 1)
24268 break;
24270 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24271 in between the collapsed for loops to be still considered perfectly
24272 nested. Hopefully the final version clarifies this.
24273 For now handle (multiple) {'s and empty statements. */
24274 cp_parser_parse_tentatively (parser);
24277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24278 break;
24279 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24281 cp_lexer_consume_token (parser->lexer);
24282 bracecount++;
24284 else if (bracecount
24285 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24286 cp_lexer_consume_token (parser->lexer);
24287 else
24289 loc = cp_lexer_peek_token (parser->lexer)->location;
24290 error_at (loc, "not enough collapsed for loops");
24291 collapse_err = true;
24292 cp_parser_abort_tentative_parse (parser);
24293 declv = NULL_TREE;
24294 break;
24297 while (1);
24299 if (declv)
24301 cp_parser_parse_definitely (parser);
24302 nbraces += bracecount;
24306 /* Note that we saved the original contents of this flag when we entered
24307 the structured block, and so we don't need to re-save it here. */
24308 parser->in_statement = IN_OMP_FOR;
24310 /* Note that the grammar doesn't call for a structured block here,
24311 though the loop as a whole is a structured block. */
24312 body = push_stmt_list ();
24313 cp_parser_statement (parser, NULL_TREE, false, NULL);
24314 body = pop_stmt_list (body);
24316 if (declv == NULL_TREE)
24317 ret = NULL_TREE;
24318 else
24319 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24320 pre_body, clauses);
24322 while (nbraces)
24324 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24326 cp_lexer_consume_token (parser->lexer);
24327 nbraces--;
24329 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24330 cp_lexer_consume_token (parser->lexer);
24331 else
24333 if (!collapse_err)
24335 error_at (cp_lexer_peek_token (parser->lexer)->location,
24336 "collapsed loops not perfectly nested");
24338 collapse_err = true;
24339 cp_parser_statement_seq_opt (parser, NULL);
24340 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24341 break;
24345 while (!VEC_empty (tree, for_block))
24346 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24347 release_tree_vector (for_block);
24349 return ret;
24352 /* OpenMP 2.5:
24353 #pragma omp for for-clause[optseq] new-line
24354 for-loop */
24356 #define OMP_FOR_CLAUSE_MASK \
24357 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24358 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24359 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24360 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24361 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24362 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24363 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24364 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24366 static tree
24367 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24369 tree clauses, sb, ret;
24370 unsigned int save;
24372 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24373 "#pragma omp for", pragma_tok);
24375 sb = begin_omp_structured_block ();
24376 save = cp_parser_begin_omp_structured_block (parser);
24378 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24380 cp_parser_end_omp_structured_block (parser, save);
24381 add_stmt (finish_omp_structured_block (sb));
24383 return ret;
24386 /* OpenMP 2.5:
24387 # pragma omp master new-line
24388 structured-block */
24390 static tree
24391 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24393 cp_parser_require_pragma_eol (parser, pragma_tok);
24394 return c_finish_omp_master (input_location,
24395 cp_parser_omp_structured_block (parser));
24398 /* OpenMP 2.5:
24399 # pragma omp ordered new-line
24400 structured-block */
24402 static tree
24403 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24405 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24406 cp_parser_require_pragma_eol (parser, pragma_tok);
24407 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24410 /* OpenMP 2.5:
24412 section-scope:
24413 { section-sequence }
24415 section-sequence:
24416 section-directive[opt] structured-block
24417 section-sequence section-directive structured-block */
24419 static tree
24420 cp_parser_omp_sections_scope (cp_parser *parser)
24422 tree stmt, substmt;
24423 bool error_suppress = false;
24424 cp_token *tok;
24426 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24427 return NULL_TREE;
24429 stmt = push_stmt_list ();
24431 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24433 unsigned save;
24435 substmt = begin_omp_structured_block ();
24436 save = cp_parser_begin_omp_structured_block (parser);
24438 while (1)
24440 cp_parser_statement (parser, NULL_TREE, false, NULL);
24442 tok = cp_lexer_peek_token (parser->lexer);
24443 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24444 break;
24445 if (tok->type == CPP_CLOSE_BRACE)
24446 break;
24447 if (tok->type == CPP_EOF)
24448 break;
24451 cp_parser_end_omp_structured_block (parser, save);
24452 substmt = finish_omp_structured_block (substmt);
24453 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24454 add_stmt (substmt);
24457 while (1)
24459 tok = cp_lexer_peek_token (parser->lexer);
24460 if (tok->type == CPP_CLOSE_BRACE)
24461 break;
24462 if (tok->type == CPP_EOF)
24463 break;
24465 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24467 cp_lexer_consume_token (parser->lexer);
24468 cp_parser_require_pragma_eol (parser, tok);
24469 error_suppress = false;
24471 else if (!error_suppress)
24473 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24474 error_suppress = true;
24477 substmt = cp_parser_omp_structured_block (parser);
24478 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24479 add_stmt (substmt);
24481 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24483 substmt = pop_stmt_list (stmt);
24485 stmt = make_node (OMP_SECTIONS);
24486 TREE_TYPE (stmt) = void_type_node;
24487 OMP_SECTIONS_BODY (stmt) = substmt;
24489 add_stmt (stmt);
24490 return stmt;
24493 /* OpenMP 2.5:
24494 # pragma omp sections sections-clause[optseq] newline
24495 sections-scope */
24497 #define OMP_SECTIONS_CLAUSE_MASK \
24498 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24499 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24500 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24501 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24502 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24504 static tree
24505 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24507 tree clauses, ret;
24509 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24510 "#pragma omp sections", pragma_tok);
24512 ret = cp_parser_omp_sections_scope (parser);
24513 if (ret)
24514 OMP_SECTIONS_CLAUSES (ret) = clauses;
24516 return ret;
24519 /* OpenMP 2.5:
24520 # pragma parallel parallel-clause new-line
24521 # pragma parallel for parallel-for-clause new-line
24522 # pragma parallel sections parallel-sections-clause new-line */
24524 #define OMP_PARALLEL_CLAUSE_MASK \
24525 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24526 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24527 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24528 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24529 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24530 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24531 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24532 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24534 static tree
24535 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24537 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24538 const char *p_name = "#pragma omp parallel";
24539 tree stmt, clauses, par_clause, ws_clause, block;
24540 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24541 unsigned int save;
24542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24546 cp_lexer_consume_token (parser->lexer);
24547 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24548 p_name = "#pragma omp parallel for";
24549 mask |= OMP_FOR_CLAUSE_MASK;
24550 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24552 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24555 const char *p = IDENTIFIER_POINTER (id);
24556 if (strcmp (p, "sections") == 0)
24558 cp_lexer_consume_token (parser->lexer);
24559 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24560 p_name = "#pragma omp parallel sections";
24561 mask |= OMP_SECTIONS_CLAUSE_MASK;
24562 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24566 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24567 block = begin_omp_parallel ();
24568 save = cp_parser_begin_omp_structured_block (parser);
24570 switch (p_kind)
24572 case PRAGMA_OMP_PARALLEL:
24573 cp_parser_statement (parser, NULL_TREE, false, NULL);
24574 par_clause = clauses;
24575 break;
24577 case PRAGMA_OMP_PARALLEL_FOR:
24578 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24579 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24580 break;
24582 case PRAGMA_OMP_PARALLEL_SECTIONS:
24583 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24584 stmt = cp_parser_omp_sections_scope (parser);
24585 if (stmt)
24586 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24587 break;
24589 default:
24590 gcc_unreachable ();
24593 cp_parser_end_omp_structured_block (parser, save);
24594 stmt = finish_omp_parallel (par_clause, block);
24595 if (p_kind != PRAGMA_OMP_PARALLEL)
24596 OMP_PARALLEL_COMBINED (stmt) = 1;
24597 return stmt;
24600 /* OpenMP 2.5:
24601 # pragma omp single single-clause[optseq] new-line
24602 structured-block */
24604 #define OMP_SINGLE_CLAUSE_MASK \
24605 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24606 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24607 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24608 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24610 static tree
24611 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24613 tree stmt = make_node (OMP_SINGLE);
24614 TREE_TYPE (stmt) = void_type_node;
24616 OMP_SINGLE_CLAUSES (stmt)
24617 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24618 "#pragma omp single", pragma_tok);
24619 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24621 return add_stmt (stmt);
24624 /* OpenMP 3.0:
24625 # pragma omp task task-clause[optseq] new-line
24626 structured-block */
24628 #define OMP_TASK_CLAUSE_MASK \
24629 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24630 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24631 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24632 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24633 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24634 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24636 static tree
24637 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24639 tree clauses, block;
24640 unsigned int save;
24642 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24643 "#pragma omp task", pragma_tok);
24644 block = begin_omp_task ();
24645 save = cp_parser_begin_omp_structured_block (parser);
24646 cp_parser_statement (parser, NULL_TREE, false, NULL);
24647 cp_parser_end_omp_structured_block (parser, save);
24648 return finish_omp_task (clauses, block);
24651 /* OpenMP 3.0:
24652 # pragma omp taskwait new-line */
24654 static void
24655 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24657 cp_parser_require_pragma_eol (parser, pragma_tok);
24658 finish_omp_taskwait ();
24661 /* OpenMP 2.5:
24662 # pragma omp threadprivate (variable-list) */
24664 static void
24665 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24667 tree vars;
24669 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24670 cp_parser_require_pragma_eol (parser, pragma_tok);
24672 finish_omp_threadprivate (vars);
24675 /* Main entry point to OpenMP statement pragmas. */
24677 static void
24678 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24680 tree stmt;
24682 switch (pragma_tok->pragma_kind)
24684 case PRAGMA_OMP_ATOMIC:
24685 cp_parser_omp_atomic (parser, pragma_tok);
24686 return;
24687 case PRAGMA_OMP_CRITICAL:
24688 stmt = cp_parser_omp_critical (parser, pragma_tok);
24689 break;
24690 case PRAGMA_OMP_FOR:
24691 stmt = cp_parser_omp_for (parser, pragma_tok);
24692 break;
24693 case PRAGMA_OMP_MASTER:
24694 stmt = cp_parser_omp_master (parser, pragma_tok);
24695 break;
24696 case PRAGMA_OMP_ORDERED:
24697 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24698 break;
24699 case PRAGMA_OMP_PARALLEL:
24700 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24701 break;
24702 case PRAGMA_OMP_SECTIONS:
24703 stmt = cp_parser_omp_sections (parser, pragma_tok);
24704 break;
24705 case PRAGMA_OMP_SINGLE:
24706 stmt = cp_parser_omp_single (parser, pragma_tok);
24707 break;
24708 case PRAGMA_OMP_TASK:
24709 stmt = cp_parser_omp_task (parser, pragma_tok);
24710 break;
24711 default:
24712 gcc_unreachable ();
24715 if (stmt)
24716 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24719 /* The parser. */
24721 static GTY (()) cp_parser *the_parser;
24724 /* Special handling for the first token or line in the file. The first
24725 thing in the file might be #pragma GCC pch_preprocess, which loads a
24726 PCH file, which is a GC collection point. So we need to handle this
24727 first pragma without benefit of an existing lexer structure.
24729 Always returns one token to the caller in *FIRST_TOKEN. This is
24730 either the true first token of the file, or the first token after
24731 the initial pragma. */
24733 static void
24734 cp_parser_initial_pragma (cp_token *first_token)
24736 tree name = NULL;
24738 cp_lexer_get_preprocessor_token (NULL, first_token);
24739 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24740 return;
24742 cp_lexer_get_preprocessor_token (NULL, first_token);
24743 if (first_token->type == CPP_STRING)
24745 name = first_token->u.value;
24747 cp_lexer_get_preprocessor_token (NULL, first_token);
24748 if (first_token->type != CPP_PRAGMA_EOL)
24749 error_at (first_token->location,
24750 "junk at end of %<#pragma GCC pch_preprocess%>");
24752 else
24753 error_at (first_token->location, "expected string literal");
24755 /* Skip to the end of the pragma. */
24756 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24757 cp_lexer_get_preprocessor_token (NULL, first_token);
24759 /* Now actually load the PCH file. */
24760 if (name)
24761 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24763 /* Read one more token to return to our caller. We have to do this
24764 after reading the PCH file in, since its pointers have to be
24765 live. */
24766 cp_lexer_get_preprocessor_token (NULL, first_token);
24769 /* Normal parsing of a pragma token. Here we can (and must) use the
24770 regular lexer. */
24772 static bool
24773 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24775 cp_token *pragma_tok;
24776 unsigned int id;
24778 pragma_tok = cp_lexer_consume_token (parser->lexer);
24779 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24780 parser->lexer->in_pragma = true;
24782 id = pragma_tok->pragma_kind;
24783 switch (id)
24785 case PRAGMA_GCC_PCH_PREPROCESS:
24786 error_at (pragma_tok->location,
24787 "%<#pragma GCC pch_preprocess%> must be first");
24788 break;
24790 case PRAGMA_OMP_BARRIER:
24791 switch (context)
24793 case pragma_compound:
24794 cp_parser_omp_barrier (parser, pragma_tok);
24795 return false;
24796 case pragma_stmt:
24797 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24798 "used in compound statements");
24799 break;
24800 default:
24801 goto bad_stmt;
24803 break;
24805 case PRAGMA_OMP_FLUSH:
24806 switch (context)
24808 case pragma_compound:
24809 cp_parser_omp_flush (parser, pragma_tok);
24810 return false;
24811 case pragma_stmt:
24812 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24813 "used in compound statements");
24814 break;
24815 default:
24816 goto bad_stmt;
24818 break;
24820 case PRAGMA_OMP_TASKWAIT:
24821 switch (context)
24823 case pragma_compound:
24824 cp_parser_omp_taskwait (parser, pragma_tok);
24825 return false;
24826 case pragma_stmt:
24827 error_at (pragma_tok->location,
24828 "%<#pragma omp taskwait%> may only be "
24829 "used in compound statements");
24830 break;
24831 default:
24832 goto bad_stmt;
24834 break;
24836 case PRAGMA_OMP_THREADPRIVATE:
24837 cp_parser_omp_threadprivate (parser, pragma_tok);
24838 return false;
24840 case PRAGMA_OMP_ATOMIC:
24841 case PRAGMA_OMP_CRITICAL:
24842 case PRAGMA_OMP_FOR:
24843 case PRAGMA_OMP_MASTER:
24844 case PRAGMA_OMP_ORDERED:
24845 case PRAGMA_OMP_PARALLEL:
24846 case PRAGMA_OMP_SECTIONS:
24847 case PRAGMA_OMP_SINGLE:
24848 case PRAGMA_OMP_TASK:
24849 if (context == pragma_external)
24850 goto bad_stmt;
24851 cp_parser_omp_construct (parser, pragma_tok);
24852 return true;
24854 case PRAGMA_OMP_SECTION:
24855 error_at (pragma_tok->location,
24856 "%<#pragma omp section%> may only be used in "
24857 "%<#pragma omp sections%> construct");
24858 break;
24860 default:
24861 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24862 c_invoke_pragma_handler (id);
24863 break;
24865 bad_stmt:
24866 cp_parser_error (parser, "expected declaration specifiers");
24867 break;
24870 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24871 return false;
24874 /* The interface the pragma parsers have to the lexer. */
24876 enum cpp_ttype
24877 pragma_lex (tree *value)
24879 cp_token *tok;
24880 enum cpp_ttype ret;
24882 tok = cp_lexer_peek_token (the_parser->lexer);
24884 ret = tok->type;
24885 *value = tok->u.value;
24887 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24888 ret = CPP_EOF;
24889 else if (ret == CPP_STRING)
24890 *value = cp_parser_string_literal (the_parser, false, false);
24891 else
24893 cp_lexer_consume_token (the_parser->lexer);
24894 if (ret == CPP_KEYWORD)
24895 ret = CPP_NAME;
24898 return ret;
24902 /* External interface. */
24904 /* Parse one entire translation unit. */
24906 void
24907 c_parse_file (void)
24909 static bool already_called = false;
24911 if (already_called)
24913 sorry ("inter-module optimizations not implemented for C++");
24914 return;
24916 already_called = true;
24918 the_parser = cp_parser_new ();
24919 push_deferring_access_checks (flag_access_control
24920 ? dk_no_deferred : dk_no_check);
24921 cp_parser_translation_unit (the_parser);
24922 the_parser = NULL;
24925 #include "gt-cp-parser.h"