2011-02-13 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / cp / parser.c
blob42a0a39a32d4fe44c3c68eb6cafa87f1f7f6e2a5
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 "output.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "plugin.h"
41 /* The lexer. */
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
46 /* A token's value and its associated deferred access checks and
47 qualifying scope. */
49 struct GTY(()) tree_check {
50 /* The value associated with the token. */
51 tree value;
52 /* The checks that have been associated with value. */
53 VEC (deferred_access_check, gc)* checks;
54 /* The token's qualifying scope (used when it is a
55 CPP_NESTED_NAME_SPECIFIER). */
56 tree qualifying_scope;
59 /* A C++ token. */
61 typedef struct GTY (()) cp_token {
62 /* The kind of token. */
63 ENUM_BITFIELD (cpp_ttype) type : 8;
64 /* If this token is a keyword, this value indicates which keyword.
65 Otherwise, this value is RID_MAX. */
66 ENUM_BITFIELD (rid) keyword : 8;
67 /* Token flags. */
68 unsigned char flags;
69 /* Identifier for the pragma. */
70 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
71 /* True if this token is from a context where it is implicitly extern "C" */
72 BOOL_BITFIELD implicit_extern_c : 1;
73 /* True for a CPP_NAME token that is not a keyword (i.e., for which
74 KEYWORD is RID_MAX) iff this name was looked up and found to be
75 ambiguous. An error has already been reported. */
76 BOOL_BITFIELD ambiguous_p : 1;
77 /* The location at which this token was found. */
78 location_t location;
79 /* The value associated with this token, if any. */
80 union cp_token_value {
81 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
82 struct tree_check* GTY((tag ("1"))) tree_check_value;
83 /* Use for all other tokens. */
84 tree GTY((tag ("0"))) value;
85 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
86 } cp_token;
88 /* We use a stack of token pointer for saving token sets. */
89 typedef struct cp_token *cp_token_position;
90 DEF_VEC_P (cp_token_position);
91 DEF_VEC_ALLOC_P (cp_token_position,heap);
93 static cp_token eof_token =
95 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 /* The cp_lexer structure represents the C++ lexer. It is responsible
99 for managing the token stream from the preprocessor and supplying
100 it to the parser. Tokens are never added to the cp_lexer after
101 it is created. */
103 typedef struct GTY (()) cp_lexer {
104 /* The memory allocated for the buffer. NULL if this lexer does not
105 own the token buffer. */
106 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
107 /* If the lexer owns the buffer, this is the number of tokens in the
108 buffer. */
109 size_t buffer_length;
111 /* A pointer just past the last available token. The tokens
112 in this lexer are [buffer, last_token). */
113 cp_token_position GTY ((skip)) last_token;
115 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
116 no more available tokens. */
117 cp_token_position GTY ((skip)) next_token;
119 /* A stack indicating positions at which cp_lexer_save_tokens was
120 called. The top entry is the most recent position at which we
121 began saving tokens. If the stack is non-empty, we are saving
122 tokens. */
123 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125 /* The next lexer in a linked list of lexers. */
126 struct cp_lexer *next;
128 /* True if we should output debugging information. */
129 bool debugging_p;
131 /* True if we're in the context of parsing a pragma, and should not
132 increment past the end-of-line marker. */
133 bool in_pragma;
134 } cp_lexer;
136 /* cp_token_cache is a range of tokens. There is no need to represent
137 allocate heap memory for it, since tokens are never removed from the
138 lexer's array. There is also no need for the GC to walk through
139 a cp_token_cache, since everything in here is referenced through
140 a lexer. */
142 typedef struct GTY(()) cp_token_cache {
143 /* The beginning of the token range. */
144 cp_token * GTY((skip)) first;
146 /* Points immediately after the last token in the range. */
147 cp_token * GTY ((skip)) last;
148 } cp_token_cache;
150 /* The various kinds of non integral constant we encounter. */
151 typedef enum non_integral_constant {
152 NIC_NONE,
153 /* floating-point literal */
154 NIC_FLOAT,
155 /* %<this%> */
156 NIC_THIS,
157 /* %<__FUNCTION__%> */
158 NIC_FUNC_NAME,
159 /* %<__PRETTY_FUNCTION__%> */
160 NIC_PRETTY_FUNC,
161 /* %<__func__%> */
162 NIC_C99_FUNC,
163 /* "%<va_arg%> */
164 NIC_VA_ARG,
165 /* a cast */
166 NIC_CAST,
167 /* %<typeid%> operator */
168 NIC_TYPEID,
169 /* non-constant compound literals */
170 NIC_NCC,
171 /* a function call */
172 NIC_FUNC_CALL,
173 /* an increment */
174 NIC_INC,
175 /* an decrement */
176 NIC_DEC,
177 /* an array reference */
178 NIC_ARRAY_REF,
179 /* %<->%> */
180 NIC_ARROW,
181 /* %<.%> */
182 NIC_POINT,
183 /* the address of a label */
184 NIC_ADDR_LABEL,
185 /* %<*%> */
186 NIC_STAR,
187 /* %<&%> */
188 NIC_ADDR,
189 /* %<++%> */
190 NIC_PREINCREMENT,
191 /* %<--%> */
192 NIC_PREDECREMENT,
193 /* %<new%> */
194 NIC_NEW,
195 /* %<delete%> */
196 NIC_DEL,
197 /* calls to overloaded operators */
198 NIC_OVERLOADED,
199 /* an assignment */
200 NIC_ASSIGNMENT,
201 /* a comma operator */
202 NIC_COMMA,
203 /* a call to a constructor */
204 NIC_CONSTRUCTOR
205 } non_integral_constant;
207 /* The various kinds of errors about name-lookup failing. */
208 typedef enum name_lookup_error {
209 /* NULL */
210 NLE_NULL,
211 /* is not a type */
212 NLE_TYPE,
213 /* is not a class or namespace */
214 NLE_CXX98,
215 /* is not a class, namespace, or enumeration */
216 NLE_NOT_CXX98
217 } name_lookup_error;
219 /* The various kinds of required token */
220 typedef enum required_token {
221 RT_NONE,
222 RT_SEMICOLON, /* ';' */
223 RT_OPEN_PAREN, /* '(' */
224 RT_CLOSE_BRACE, /* '}' */
225 RT_OPEN_BRACE, /* '{' */
226 RT_CLOSE_SQUARE, /* ']' */
227 RT_OPEN_SQUARE, /* '[' */
228 RT_COMMA, /* ',' */
229 RT_SCOPE, /* '::' */
230 RT_LESS, /* '<' */
231 RT_GREATER, /* '>' */
232 RT_EQ, /* '=' */
233 RT_ELLIPSIS, /* '...' */
234 RT_MULT, /* '*' */
235 RT_COMPL, /* '~' */
236 RT_COLON, /* ':' */
237 RT_COLON_SCOPE, /* ':' or '::' */
238 RT_CLOSE_PAREN, /* ')' */
239 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
240 RT_PRAGMA_EOL, /* end of line */
241 RT_NAME, /* identifier */
243 /* The type is CPP_KEYWORD */
244 RT_NEW, /* new */
245 RT_DELETE, /* delete */
246 RT_RETURN, /* return */
247 RT_WHILE, /* while */
248 RT_EXTERN, /* extern */
249 RT_STATIC_ASSERT, /* static_assert */
250 RT_DECLTYPE, /* decltype */
251 RT_OPERATOR, /* operator */
252 RT_CLASS, /* class */
253 RT_TEMPLATE, /* template */
254 RT_NAMESPACE, /* namespace */
255 RT_USING, /* using */
256 RT_ASM, /* asm */
257 RT_TRY, /* try */
258 RT_CATCH, /* catch */
259 RT_THROW, /* throw */
260 RT_LABEL, /* __label__ */
261 RT_AT_TRY, /* @try */
262 RT_AT_SYNCHRONIZED, /* @synchronized */
263 RT_AT_THROW, /* @throw */
265 RT_SELECT, /* selection-statement */
266 RT_INTERATION, /* iteration-statement */
267 RT_JUMP, /* jump-statement */
268 RT_CLASS_KEY, /* class-key */
269 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
270 } required_token;
272 /* Prototypes. */
274 static cp_lexer *cp_lexer_new_main
275 (void);
276 static cp_lexer *cp_lexer_new_from_tokens
277 (cp_token_cache *tokens);
278 static void cp_lexer_destroy
279 (cp_lexer *);
280 static int cp_lexer_saving_tokens
281 (const cp_lexer *);
282 static cp_token_position cp_lexer_token_position
283 (cp_lexer *, bool);
284 static cp_token *cp_lexer_token_at
285 (cp_lexer *, cp_token_position);
286 static void cp_lexer_get_preprocessor_token
287 (cp_lexer *, cp_token *);
288 static inline cp_token *cp_lexer_peek_token
289 (cp_lexer *);
290 static cp_token *cp_lexer_peek_nth_token
291 (cp_lexer *, size_t);
292 static inline bool cp_lexer_next_token_is
293 (cp_lexer *, enum cpp_ttype);
294 static bool cp_lexer_next_token_is_not
295 (cp_lexer *, enum cpp_ttype);
296 static bool cp_lexer_next_token_is_keyword
297 (cp_lexer *, enum rid);
298 static cp_token *cp_lexer_consume_token
299 (cp_lexer *);
300 static void cp_lexer_purge_token
301 (cp_lexer *);
302 static void cp_lexer_purge_tokens_after
303 (cp_lexer *, cp_token_position);
304 static void cp_lexer_save_tokens
305 (cp_lexer *);
306 static void cp_lexer_commit_tokens
307 (cp_lexer *);
308 static void cp_lexer_rollback_tokens
309 (cp_lexer *);
310 #ifdef ENABLE_CHECKING
311 static void cp_lexer_print_token
312 (FILE *, cp_token *);
313 static inline bool cp_lexer_debugging_p
314 (cp_lexer *);
315 static void cp_lexer_start_debugging
316 (cp_lexer *) ATTRIBUTE_UNUSED;
317 static void cp_lexer_stop_debugging
318 (cp_lexer *) ATTRIBUTE_UNUSED;
319 #else
320 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
321 about passing NULL to functions that require non-NULL arguments
322 (fputs, fprintf). It will never be used, so all we need is a value
323 of the right type that's guaranteed not to be NULL. */
324 #define cp_lexer_debug_stream stdout
325 #define cp_lexer_print_token(str, tok) (void) 0
326 #define cp_lexer_debugging_p(lexer) 0
327 #endif /* ENABLE_CHECKING */
329 static cp_token_cache *cp_token_cache_new
330 (cp_token *, cp_token *);
332 static void cp_parser_initial_pragma
333 (cp_token *);
335 /* Manifest constants. */
336 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
337 #define CP_SAVED_TOKEN_STACK 5
339 /* A token type for keywords, as opposed to ordinary identifiers. */
340 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342 /* A token type for template-ids. If a template-id is processed while
343 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
344 the value of the CPP_TEMPLATE_ID is whatever was returned by
345 cp_parser_template_id. */
346 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348 /* A token type for nested-name-specifiers. If a
349 nested-name-specifier is processed while parsing tentatively, it is
350 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
351 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
352 cp_parser_nested_name_specifier_opt. */
353 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355 /* A token type for tokens that are not tokens at all; these are used
356 to represent slots in the array where there used to be a token
357 that has now been deleted. */
358 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360 /* The number of token types, including C++-specific ones. */
361 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363 /* Variables. */
365 #ifdef ENABLE_CHECKING
366 /* The stream to which debugging output should be written. */
367 static FILE *cp_lexer_debug_stream;
368 #endif /* ENABLE_CHECKING */
370 /* Nonzero if we are parsing an unevaluated operand: an operand to
371 sizeof, typeof, or alignof. */
372 int cp_unevaluated_operand;
374 /* Create a new main C++ lexer, the lexer that gets tokens from the
375 preprocessor. */
377 static cp_lexer *
378 cp_lexer_new_main (void)
380 cp_token first_token;
381 cp_lexer *lexer;
382 cp_token *pos;
383 size_t alloc;
384 size_t space;
385 cp_token *buffer;
387 /* It's possible that parsing the first pragma will load a PCH file,
388 which is a GC collection point. So we have to do that before
389 allocating any memory. */
390 cp_parser_initial_pragma (&first_token);
392 c_common_no_more_pch ();
394 /* Allocate the memory. */
395 lexer = ggc_alloc_cleared_cp_lexer ();
397 #ifdef ENABLE_CHECKING
398 /* Initially we are not debugging. */
399 lexer->debugging_p = false;
400 #endif /* ENABLE_CHECKING */
401 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
402 CP_SAVED_TOKEN_STACK);
404 /* Create the buffer. */
405 alloc = CP_LEXER_BUFFER_SIZE;
406 buffer = ggc_alloc_vec_cp_token (alloc);
408 /* Put the first token in the buffer. */
409 space = alloc;
410 pos = buffer;
411 *pos = first_token;
413 /* Get the remaining tokens from the preprocessor. */
414 while (pos->type != CPP_EOF)
416 pos++;
417 if (!--space)
419 space = alloc;
420 alloc *= 2;
421 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
422 pos = buffer + space;
424 cp_lexer_get_preprocessor_token (lexer, pos);
426 lexer->buffer = buffer;
427 lexer->buffer_length = alloc - space;
428 lexer->last_token = pos;
429 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431 /* Subsequent preprocessor diagnostics should use compiler
432 diagnostic functions to get the compiler source location. */
433 done_lexing = true;
435 gcc_assert (lexer->next_token->type != CPP_PURGED);
436 return lexer;
439 /* Create a new lexer whose token stream is primed with the tokens in
440 CACHE. When these tokens are exhausted, no new tokens will be read. */
442 static cp_lexer *
443 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 cp_token *first = cache->first;
446 cp_token *last = cache->last;
447 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449 /* We do not own the buffer. */
450 lexer->buffer = NULL;
451 lexer->buffer_length = 0;
452 lexer->next_token = first == last ? &eof_token : first;
453 lexer->last_token = last;
455 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
456 CP_SAVED_TOKEN_STACK);
458 #ifdef ENABLE_CHECKING
459 /* Initially we are not debugging. */
460 lexer->debugging_p = false;
461 #endif
463 gcc_assert (lexer->next_token->type != CPP_PURGED);
464 return lexer;
467 /* Frees all resources associated with LEXER. */
469 static void
470 cp_lexer_destroy (cp_lexer *lexer)
472 if (lexer->buffer)
473 ggc_free (lexer->buffer);
474 VEC_free (cp_token_position, heap, lexer->saved_tokens);
475 ggc_free (lexer);
478 /* Returns nonzero if debugging information should be output. */
480 #ifdef ENABLE_CHECKING
482 static inline bool
483 cp_lexer_debugging_p (cp_lexer *lexer)
485 return lexer->debugging_p;
488 #endif /* ENABLE_CHECKING */
490 static inline cp_token_position
491 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 gcc_assert (!previous_p || lexer->next_token != &eof_token);
495 return lexer->next_token - previous_p;
498 static inline cp_token *
499 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 return pos;
504 static inline void
505 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
507 lexer->next_token = cp_lexer_token_at (lexer, pos);
510 static inline cp_token_position
511 cp_lexer_previous_token_position (cp_lexer *lexer)
513 if (lexer->next_token == &eof_token)
514 return lexer->last_token - 1;
515 else
516 return cp_lexer_token_position (lexer, true);
519 static inline cp_token *
520 cp_lexer_previous_token (cp_lexer *lexer)
522 cp_token_position tp = cp_lexer_previous_token_position (lexer);
524 return cp_lexer_token_at (lexer, tp);
527 /* nonzero if we are presently saving tokens. */
529 static inline int
530 cp_lexer_saving_tokens (const cp_lexer* lexer)
532 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
535 /* Store the next token from the preprocessor in *TOKEN. Return true
536 if we reach EOF. If LEXER is NULL, assume we are handling an
537 initial #pragma pch_preprocess, and thus want the lexer to return
538 processed strings. */
540 static void
541 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
543 static int is_extern_c = 0;
545 /* Get a new token from the preprocessor. */
546 token->type
547 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
548 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
549 token->keyword = RID_MAX;
550 token->pragma_kind = PRAGMA_NONE;
552 /* On some systems, some header files are surrounded by an
553 implicit extern "C" block. Set a flag in the token if it
554 comes from such a header. */
555 is_extern_c += pending_lang_change;
556 pending_lang_change = 0;
557 token->implicit_extern_c = is_extern_c > 0;
559 /* Check to see if this token is a keyword. */
560 if (token->type == CPP_NAME)
562 if (C_IS_RESERVED_WORD (token->u.value))
564 /* Mark this token as a keyword. */
565 token->type = CPP_KEYWORD;
566 /* Record which keyword. */
567 token->keyword = C_RID_CODE (token->u.value);
569 else
571 if (warn_cxx0x_compat
572 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
573 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
575 /* Warn about the C++0x keyword (but still treat it as
576 an identifier). */
577 warning (OPT_Wc__0x_compat,
578 "identifier %qE will become a keyword in C++0x",
579 token->u.value);
581 /* Clear out the C_RID_CODE so we don't warn about this
582 particular identifier-turned-keyword again. */
583 C_SET_RID_CODE (token->u.value, RID_MAX);
586 token->ambiguous_p = false;
587 token->keyword = RID_MAX;
590 else if (token->type == CPP_AT_NAME)
592 /* This only happens in Objective-C++; it must be a keyword. */
593 token->type = CPP_KEYWORD;
594 switch (C_RID_CODE (token->u.value))
596 /* Replace 'class' with '@class', 'private' with '@private',
597 etc. This prevents confusion with the C++ keyword
598 'class', and makes the tokens consistent with other
599 Objective-C 'AT' keywords. For example '@class' is
600 reported as RID_AT_CLASS which is consistent with
601 '@synchronized', which is reported as
602 RID_AT_SYNCHRONIZED.
604 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
605 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
606 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
607 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
608 case RID_THROW: token->keyword = RID_AT_THROW; break;
609 case RID_TRY: token->keyword = RID_AT_TRY; break;
610 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
611 default: token->keyword = C_RID_CODE (token->u.value);
614 else if (token->type == CPP_PRAGMA)
616 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
617 token->pragma_kind = ((enum pragma_kind)
618 TREE_INT_CST_LOW (token->u.value));
619 token->u.value = NULL_TREE;
623 /* Update the globals input_location and the input file stack from TOKEN. */
624 static inline void
625 cp_lexer_set_source_position_from_token (cp_token *token)
627 if (token->type != CPP_EOF)
629 input_location = token->location;
633 /* Return a pointer to the next token in the token stream, but do not
634 consume it. */
636 static inline cp_token *
637 cp_lexer_peek_token (cp_lexer *lexer)
639 if (cp_lexer_debugging_p (lexer))
641 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
642 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
643 putc ('\n', cp_lexer_debug_stream);
645 return lexer->next_token;
648 /* Return true if the next token has the indicated TYPE. */
650 static inline bool
651 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
653 return cp_lexer_peek_token (lexer)->type == type;
656 /* Return true if the next token does not have the indicated TYPE. */
658 static inline bool
659 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
661 return !cp_lexer_next_token_is (lexer, type);
664 /* Return true if the next token is the indicated KEYWORD. */
666 static inline bool
667 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
669 return cp_lexer_peek_token (lexer)->keyword == keyword;
672 /* Return true if the next token is not the indicated KEYWORD. */
674 static inline bool
675 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
677 return cp_lexer_peek_token (lexer)->keyword != keyword;
680 /* Return true if the next token is a keyword for a decl-specifier. */
682 static bool
683 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
685 cp_token *token;
687 token = cp_lexer_peek_token (lexer);
688 switch (token->keyword)
690 /* auto specifier: storage-class-specifier in C++,
691 simple-type-specifier in C++0x. */
692 case RID_AUTO:
693 /* Storage classes. */
694 case RID_REGISTER:
695 case RID_STATIC:
696 case RID_EXTERN:
697 case RID_MUTABLE:
698 case RID_THREAD:
699 /* Elaborated type specifiers. */
700 case RID_ENUM:
701 case RID_CLASS:
702 case RID_STRUCT:
703 case RID_UNION:
704 case RID_TYPENAME:
705 /* Simple type specifiers. */
706 case RID_CHAR:
707 case RID_CHAR16:
708 case RID_CHAR32:
709 case RID_WCHAR:
710 case RID_BOOL:
711 case RID_SHORT:
712 case RID_INT:
713 case RID_LONG:
714 case RID_INT128:
715 case RID_SIGNED:
716 case RID_UNSIGNED:
717 case RID_FLOAT:
718 case RID_DOUBLE:
719 case RID_VOID:
720 /* GNU extensions. */
721 case RID_ATTRIBUTE:
722 case RID_TYPEOF:
723 /* C++0x extensions. */
724 case RID_DECLTYPE:
725 return true;
727 default:
728 return false;
732 /* Return a pointer to the Nth token in the token stream. If N is 1,
733 then this is precisely equivalent to cp_lexer_peek_token (except
734 that it is not inline). One would like to disallow that case, but
735 there is one case (cp_parser_nth_token_starts_template_id) where
736 the caller passes a variable for N and it might be 1. */
738 static cp_token *
739 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
741 cp_token *token;
743 /* N is 1-based, not zero-based. */
744 gcc_assert (n > 0);
746 if (cp_lexer_debugging_p (lexer))
747 fprintf (cp_lexer_debug_stream,
748 "cp_lexer: peeking ahead %ld at token: ", (long)n);
750 --n;
751 token = lexer->next_token;
752 gcc_assert (!n || token != &eof_token);
753 while (n != 0)
755 ++token;
756 if (token == lexer->last_token)
758 token = &eof_token;
759 break;
762 if (token->type != CPP_PURGED)
763 --n;
766 if (cp_lexer_debugging_p (lexer))
768 cp_lexer_print_token (cp_lexer_debug_stream, token);
769 putc ('\n', cp_lexer_debug_stream);
772 return token;
775 /* Return the next token, and advance the lexer's next_token pointer
776 to point to the next non-purged token. */
778 static cp_token *
779 cp_lexer_consume_token (cp_lexer* lexer)
781 cp_token *token = lexer->next_token;
783 gcc_assert (token != &eof_token);
784 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
788 lexer->next_token++;
789 if (lexer->next_token == lexer->last_token)
791 lexer->next_token = &eof_token;
792 break;
796 while (lexer->next_token->type == CPP_PURGED);
798 cp_lexer_set_source_position_from_token (token);
800 /* Provide debugging output. */
801 if (cp_lexer_debugging_p (lexer))
803 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
804 cp_lexer_print_token (cp_lexer_debug_stream, token);
805 putc ('\n', cp_lexer_debug_stream);
808 return token;
811 /* Permanently remove the next token from the token stream, and
812 advance the next_token pointer to refer to the next non-purged
813 token. */
815 static void
816 cp_lexer_purge_token (cp_lexer *lexer)
818 cp_token *tok = lexer->next_token;
820 gcc_assert (tok != &eof_token);
821 tok->type = CPP_PURGED;
822 tok->location = UNKNOWN_LOCATION;
823 tok->u.value = NULL_TREE;
824 tok->keyword = RID_MAX;
828 tok++;
829 if (tok == lexer->last_token)
831 tok = &eof_token;
832 break;
835 while (tok->type == CPP_PURGED);
836 lexer->next_token = tok;
839 /* Permanently remove all tokens after TOK, up to, but not
840 including, the token that will be returned next by
841 cp_lexer_peek_token. */
843 static void
844 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
846 cp_token *peek = lexer->next_token;
848 if (peek == &eof_token)
849 peek = lexer->last_token;
851 gcc_assert (tok < peek);
853 for ( tok += 1; tok != peek; tok += 1)
855 tok->type = CPP_PURGED;
856 tok->location = UNKNOWN_LOCATION;
857 tok->u.value = NULL_TREE;
858 tok->keyword = RID_MAX;
862 /* Begin saving tokens. All tokens consumed after this point will be
863 preserved. */
865 static void
866 cp_lexer_save_tokens (cp_lexer* lexer)
868 /* Provide debugging output. */
869 if (cp_lexer_debugging_p (lexer))
870 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
872 VEC_safe_push (cp_token_position, heap,
873 lexer->saved_tokens, lexer->next_token);
876 /* Commit to the portion of the token stream most recently saved. */
878 static void
879 cp_lexer_commit_tokens (cp_lexer* lexer)
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
885 VEC_pop (cp_token_position, lexer->saved_tokens);
888 /* Return all tokens saved since the last call to cp_lexer_save_tokens
889 to the token stream. Stop saving tokens. */
891 static void
892 cp_lexer_rollback_tokens (cp_lexer* lexer)
894 /* Provide debugging output. */
895 if (cp_lexer_debugging_p (lexer))
896 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
898 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
901 /* Print a representation of the TOKEN on the STREAM. */
903 #ifdef ENABLE_CHECKING
905 static void
906 cp_lexer_print_token (FILE * stream, cp_token *token)
908 /* We don't use cpp_type2name here because the parser defines
909 a few tokens of its own. */
910 static const char *const token_names[] = {
911 /* cpplib-defined token types */
912 #define OP(e, s) #e,
913 #define TK(e, s) #e,
914 TTYPE_TABLE
915 #undef OP
916 #undef TK
917 /* C++ parser token types - see "Manifest constants", above. */
918 "KEYWORD",
919 "TEMPLATE_ID",
920 "NESTED_NAME_SPECIFIER",
921 "PURGED"
924 /* If we have a name for the token, print it out. Otherwise, we
925 simply give the numeric code. */
926 gcc_assert (token->type < ARRAY_SIZE(token_names));
927 fputs (token_names[token->type], stream);
929 /* For some tokens, print the associated data. */
930 switch (token->type)
932 case CPP_KEYWORD:
933 /* Some keywords have a value that is not an IDENTIFIER_NODE.
934 For example, `struct' is mapped to an INTEGER_CST. */
935 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
936 break;
937 /* else fall through */
938 case CPP_NAME:
939 fputs (IDENTIFIER_POINTER (token->u.value), stream);
940 break;
942 case CPP_STRING:
943 case CPP_STRING16:
944 case CPP_STRING32:
945 case CPP_WSTRING:
946 case CPP_UTF8STRING:
947 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
948 break;
950 default:
951 break;
955 /* Start emitting debugging information. */
957 static void
958 cp_lexer_start_debugging (cp_lexer* lexer)
960 lexer->debugging_p = true;
963 /* Stop emitting debugging information. */
965 static void
966 cp_lexer_stop_debugging (cp_lexer* lexer)
968 lexer->debugging_p = false;
971 #endif /* ENABLE_CHECKING */
973 /* Create a new cp_token_cache, representing a range of tokens. */
975 static cp_token_cache *
976 cp_token_cache_new (cp_token *first, cp_token *last)
978 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
979 cache->first = first;
980 cache->last = last;
981 return cache;
985 /* Decl-specifiers. */
987 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
989 static void
990 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
992 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
995 /* Declarators. */
997 /* Nothing other than the parser should be creating declarators;
998 declarators are a semi-syntactic representation of C++ entities.
999 Other parts of the front end that need to create entities (like
1000 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1002 static cp_declarator *make_call_declarator
1003 (cp_declarator *, tree, cp_cv_quals, tree, tree);
1004 static cp_declarator *make_array_declarator
1005 (cp_declarator *, tree);
1006 static cp_declarator *make_pointer_declarator
1007 (cp_cv_quals, cp_declarator *);
1008 static cp_declarator *make_reference_declarator
1009 (cp_cv_quals, cp_declarator *, bool);
1010 static cp_parameter_declarator *make_parameter_declarator
1011 (cp_decl_specifier_seq *, cp_declarator *, tree);
1012 static cp_declarator *make_ptrmem_declarator
1013 (cp_cv_quals, tree, cp_declarator *);
1015 /* An erroneous declarator. */
1016 static cp_declarator *cp_error_declarator;
1018 /* The obstack on which declarators and related data structures are
1019 allocated. */
1020 static struct obstack declarator_obstack;
1022 /* Alloc BYTES from the declarator memory pool. */
1024 static inline void *
1025 alloc_declarator (size_t bytes)
1027 return obstack_alloc (&declarator_obstack, bytes);
1030 /* Allocate a declarator of the indicated KIND. Clear fields that are
1031 common to all declarators. */
1033 static cp_declarator *
1034 make_declarator (cp_declarator_kind kind)
1036 cp_declarator *declarator;
1038 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1039 declarator->kind = kind;
1040 declarator->attributes = NULL_TREE;
1041 declarator->declarator = NULL;
1042 declarator->parameter_pack_p = false;
1043 declarator->id_loc = UNKNOWN_LOCATION;
1045 return declarator;
1048 /* Make a declarator for a generalized identifier. If
1049 QUALIFYING_SCOPE is non-NULL, the identifier is
1050 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1051 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1052 is, if any. */
1054 static cp_declarator *
1055 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1056 special_function_kind sfk)
1058 cp_declarator *declarator;
1060 /* It is valid to write:
1062 class C { void f(); };
1063 typedef C D;
1064 void D::f();
1066 The standard is not clear about whether `typedef const C D' is
1067 legal; as of 2002-09-15 the committee is considering that
1068 question. EDG 3.0 allows that syntax. Therefore, we do as
1069 well. */
1070 if (qualifying_scope && TYPE_P (qualifying_scope))
1071 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1073 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1074 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1075 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1077 declarator = make_declarator (cdk_id);
1078 declarator->u.id.qualifying_scope = qualifying_scope;
1079 declarator->u.id.unqualified_name = unqualified_name;
1080 declarator->u.id.sfk = sfk;
1082 return declarator;
1085 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1086 of modifiers such as const or volatile to apply to the pointer
1087 type, represented as identifiers. */
1089 cp_declarator *
1090 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1092 cp_declarator *declarator;
1094 declarator = make_declarator (cdk_pointer);
1095 declarator->declarator = target;
1096 declarator->u.pointer.qualifiers = cv_qualifiers;
1097 declarator->u.pointer.class_type = NULL_TREE;
1098 if (target)
1100 declarator->id_loc = target->id_loc;
1101 declarator->parameter_pack_p = target->parameter_pack_p;
1102 target->parameter_pack_p = false;
1104 else
1105 declarator->parameter_pack_p = false;
1107 return declarator;
1110 /* Like make_pointer_declarator -- but for references. */
1112 cp_declarator *
1113 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1114 bool rvalue_ref)
1116 cp_declarator *declarator;
1118 declarator = make_declarator (cdk_reference);
1119 declarator->declarator = target;
1120 declarator->u.reference.qualifiers = cv_qualifiers;
1121 declarator->u.reference.rvalue_ref = rvalue_ref;
1122 if (target)
1124 declarator->id_loc = target->id_loc;
1125 declarator->parameter_pack_p = target->parameter_pack_p;
1126 target->parameter_pack_p = false;
1128 else
1129 declarator->parameter_pack_p = false;
1131 return declarator;
1134 /* Like make_pointer_declarator -- but for a pointer to a non-static
1135 member of CLASS_TYPE. */
1137 cp_declarator *
1138 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1139 cp_declarator *pointee)
1141 cp_declarator *declarator;
1143 declarator = make_declarator (cdk_ptrmem);
1144 declarator->declarator = pointee;
1145 declarator->u.pointer.qualifiers = cv_qualifiers;
1146 declarator->u.pointer.class_type = class_type;
1148 if (pointee)
1150 declarator->parameter_pack_p = pointee->parameter_pack_p;
1151 pointee->parameter_pack_p = false;
1153 else
1154 declarator->parameter_pack_p = false;
1156 return declarator;
1159 /* Make a declarator for the function given by TARGET, with the
1160 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1161 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1162 indicates what exceptions can be thrown. */
1164 cp_declarator *
1165 make_call_declarator (cp_declarator *target,
1166 tree parms,
1167 cp_cv_quals cv_qualifiers,
1168 tree exception_specification,
1169 tree late_return_type)
1171 cp_declarator *declarator;
1173 declarator = make_declarator (cdk_function);
1174 declarator->declarator = target;
1175 declarator->u.function.parameters = parms;
1176 declarator->u.function.qualifiers = cv_qualifiers;
1177 declarator->u.function.exception_specification = exception_specification;
1178 declarator->u.function.late_return_type = late_return_type;
1179 if (target)
1181 declarator->id_loc = target->id_loc;
1182 declarator->parameter_pack_p = target->parameter_pack_p;
1183 target->parameter_pack_p = false;
1185 else
1186 declarator->parameter_pack_p = false;
1188 return declarator;
1191 /* Make a declarator for an array of BOUNDS elements, each of which is
1192 defined by ELEMENT. */
1194 cp_declarator *
1195 make_array_declarator (cp_declarator *element, tree bounds)
1197 cp_declarator *declarator;
1199 declarator = make_declarator (cdk_array);
1200 declarator->declarator = element;
1201 declarator->u.array.bounds = bounds;
1202 if (element)
1204 declarator->id_loc = element->id_loc;
1205 declarator->parameter_pack_p = element->parameter_pack_p;
1206 element->parameter_pack_p = false;
1208 else
1209 declarator->parameter_pack_p = false;
1211 return declarator;
1214 /* Determine whether the declarator we've seen so far can be a
1215 parameter pack, when followed by an ellipsis. */
1216 static bool
1217 declarator_can_be_parameter_pack (cp_declarator *declarator)
1219 /* Search for a declarator name, or any other declarator that goes
1220 after the point where the ellipsis could appear in a parameter
1221 pack. If we find any of these, then this declarator can not be
1222 made into a parameter pack. */
1223 bool found = false;
1224 while (declarator && !found)
1226 switch ((int)declarator->kind)
1228 case cdk_id:
1229 case cdk_array:
1230 found = true;
1231 break;
1233 case cdk_error:
1234 return true;
1236 default:
1237 declarator = declarator->declarator;
1238 break;
1242 return !found;
1245 cp_parameter_declarator *no_parameters;
1247 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1248 DECLARATOR and DEFAULT_ARGUMENT. */
1250 cp_parameter_declarator *
1251 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1252 cp_declarator *declarator,
1253 tree default_argument)
1255 cp_parameter_declarator *parameter;
1257 parameter = ((cp_parameter_declarator *)
1258 alloc_declarator (sizeof (cp_parameter_declarator)));
1259 parameter->next = NULL;
1260 if (decl_specifiers)
1261 parameter->decl_specifiers = *decl_specifiers;
1262 else
1263 clear_decl_specs (&parameter->decl_specifiers);
1264 parameter->declarator = declarator;
1265 parameter->default_argument = default_argument;
1266 parameter->ellipsis_p = false;
1268 return parameter;
1271 /* Returns true iff DECLARATOR is a declaration for a function. */
1273 static bool
1274 function_declarator_p (const cp_declarator *declarator)
1276 while (declarator)
1278 if (declarator->kind == cdk_function
1279 && declarator->declarator->kind == cdk_id)
1280 return true;
1281 if (declarator->kind == cdk_id
1282 || declarator->kind == cdk_error)
1283 return false;
1284 declarator = declarator->declarator;
1286 return false;
1289 /* The parser. */
1291 /* Overview
1292 --------
1294 A cp_parser parses the token stream as specified by the C++
1295 grammar. Its job is purely parsing, not semantic analysis. For
1296 example, the parser breaks the token stream into declarators,
1297 expressions, statements, and other similar syntactic constructs.
1298 It does not check that the types of the expressions on either side
1299 of an assignment-statement are compatible, or that a function is
1300 not declared with a parameter of type `void'.
1302 The parser invokes routines elsewhere in the compiler to perform
1303 semantic analysis and to build up the abstract syntax tree for the
1304 code processed.
1306 The parser (and the template instantiation code, which is, in a
1307 way, a close relative of parsing) are the only parts of the
1308 compiler that should be calling push_scope and pop_scope, or
1309 related functions. The parser (and template instantiation code)
1310 keeps track of what scope is presently active; everything else
1311 should simply honor that. (The code that generates static
1312 initializers may also need to set the scope, in order to check
1313 access control correctly when emitting the initializers.)
1315 Methodology
1316 -----------
1318 The parser is of the standard recursive-descent variety. Upcoming
1319 tokens in the token stream are examined in order to determine which
1320 production to use when parsing a non-terminal. Some C++ constructs
1321 require arbitrary look ahead to disambiguate. For example, it is
1322 impossible, in the general case, to tell whether a statement is an
1323 expression or declaration without scanning the entire statement.
1324 Therefore, the parser is capable of "parsing tentatively." When the
1325 parser is not sure what construct comes next, it enters this mode.
1326 Then, while we attempt to parse the construct, the parser queues up
1327 error messages, rather than issuing them immediately, and saves the
1328 tokens it consumes. If the construct is parsed successfully, the
1329 parser "commits", i.e., it issues any queued error messages and
1330 the tokens that were being preserved are permanently discarded.
1331 If, however, the construct is not parsed successfully, the parser
1332 rolls back its state completely so that it can resume parsing using
1333 a different alternative.
1335 Future Improvements
1336 -------------------
1338 The performance of the parser could probably be improved substantially.
1339 We could often eliminate the need to parse tentatively by looking ahead
1340 a little bit. In some places, this approach might not entirely eliminate
1341 the need to parse tentatively, but it might still speed up the average
1342 case. */
1344 /* Flags that are passed to some parsing functions. These values can
1345 be bitwise-ored together. */
1347 enum
1349 /* No flags. */
1350 CP_PARSER_FLAGS_NONE = 0x0,
1351 /* The construct is optional. If it is not present, then no error
1352 should be issued. */
1353 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1354 /* When parsing a type-specifier, treat user-defined type-names
1355 as non-type identifiers. */
1356 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1357 /* When parsing a type-specifier, do not try to parse a class-specifier
1358 or enum-specifier. */
1359 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1360 /* When parsing a decl-specifier-seq, only allow type-specifier or
1361 constexpr. */
1362 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1365 /* This type is used for parameters and variables which hold
1366 combinations of the above flags. */
1367 typedef int cp_parser_flags;
1369 /* The different kinds of declarators we want to parse. */
1371 typedef enum cp_parser_declarator_kind
1373 /* We want an abstract declarator. */
1374 CP_PARSER_DECLARATOR_ABSTRACT,
1375 /* We want a named declarator. */
1376 CP_PARSER_DECLARATOR_NAMED,
1377 /* We don't mind, but the name must be an unqualified-id. */
1378 CP_PARSER_DECLARATOR_EITHER
1379 } cp_parser_declarator_kind;
1381 /* The precedence values used to parse binary expressions. The minimum value
1382 of PREC must be 1, because zero is reserved to quickly discriminate
1383 binary operators from other tokens. */
1385 enum cp_parser_prec
1387 PREC_NOT_OPERATOR,
1388 PREC_LOGICAL_OR_EXPRESSION,
1389 PREC_LOGICAL_AND_EXPRESSION,
1390 PREC_INCLUSIVE_OR_EXPRESSION,
1391 PREC_EXCLUSIVE_OR_EXPRESSION,
1392 PREC_AND_EXPRESSION,
1393 PREC_EQUALITY_EXPRESSION,
1394 PREC_RELATIONAL_EXPRESSION,
1395 PREC_SHIFT_EXPRESSION,
1396 PREC_ADDITIVE_EXPRESSION,
1397 PREC_MULTIPLICATIVE_EXPRESSION,
1398 PREC_PM_EXPRESSION,
1399 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1402 /* A mapping from a token type to a corresponding tree node type, with a
1403 precedence value. */
1405 typedef struct cp_parser_binary_operations_map_node
1407 /* The token type. */
1408 enum cpp_ttype token_type;
1409 /* The corresponding tree code. */
1410 enum tree_code tree_type;
1411 /* The precedence of this operator. */
1412 enum cp_parser_prec prec;
1413 } cp_parser_binary_operations_map_node;
1415 /* The status of a tentative parse. */
1417 typedef enum cp_parser_status_kind
1419 /* No errors have occurred. */
1420 CP_PARSER_STATUS_KIND_NO_ERROR,
1421 /* An error has occurred. */
1422 CP_PARSER_STATUS_KIND_ERROR,
1423 /* We are committed to this tentative parse, whether or not an error
1424 has occurred. */
1425 CP_PARSER_STATUS_KIND_COMMITTED
1426 } cp_parser_status_kind;
1428 typedef struct cp_parser_expression_stack_entry
1430 /* Left hand side of the binary operation we are currently
1431 parsing. */
1432 tree lhs;
1433 /* Original tree code for left hand side, if it was a binary
1434 expression itself (used for -Wparentheses). */
1435 enum tree_code lhs_type;
1436 /* Tree code for the binary operation we are parsing. */
1437 enum tree_code tree_type;
1438 /* Precedence of the binary operation we are parsing. */
1439 enum cp_parser_prec prec;
1440 } cp_parser_expression_stack_entry;
1442 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1443 entries because precedence levels on the stack are monotonically
1444 increasing. */
1445 typedef struct cp_parser_expression_stack_entry
1446 cp_parser_expression_stack[NUM_PREC_VALUES];
1448 /* Context that is saved and restored when parsing tentatively. */
1449 typedef struct GTY (()) cp_parser_context {
1450 /* If this is a tentative parsing context, the status of the
1451 tentative parse. */
1452 enum cp_parser_status_kind status;
1453 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1454 that are looked up in this context must be looked up both in the
1455 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1456 the context of the containing expression. */
1457 tree object_type;
1459 /* The next parsing context in the stack. */
1460 struct cp_parser_context *next;
1461 } cp_parser_context;
1463 /* Prototypes. */
1465 /* Constructors and destructors. */
1467 static cp_parser_context *cp_parser_context_new
1468 (cp_parser_context *);
1470 /* Class variables. */
1472 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1474 /* The operator-precedence table used by cp_parser_binary_expression.
1475 Transformed into an associative array (binops_by_token) by
1476 cp_parser_new. */
1478 static const cp_parser_binary_operations_map_node binops[] = {
1479 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1480 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1482 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1483 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1486 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1487 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1489 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1490 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1492 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1493 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1497 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1498 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1500 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1502 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1504 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1506 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1508 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1511 /* The same as binops, but initialized by cp_parser_new so that
1512 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1513 for speed. */
1514 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1516 /* Constructors and destructors. */
1518 /* Construct a new context. The context below this one on the stack
1519 is given by NEXT. */
1521 static cp_parser_context *
1522 cp_parser_context_new (cp_parser_context* next)
1524 cp_parser_context *context;
1526 /* Allocate the storage. */
1527 if (cp_parser_context_free_list != NULL)
1529 /* Pull the first entry from the free list. */
1530 context = cp_parser_context_free_list;
1531 cp_parser_context_free_list = context->next;
1532 memset (context, 0, sizeof (*context));
1534 else
1535 context = ggc_alloc_cleared_cp_parser_context ();
1537 /* No errors have occurred yet in this context. */
1538 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1539 /* If this is not the bottommost context, copy information that we
1540 need from the previous context. */
1541 if (next)
1543 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1544 expression, then we are parsing one in this context, too. */
1545 context->object_type = next->object_type;
1546 /* Thread the stack. */
1547 context->next = next;
1550 return context;
1553 /* An entry in a queue of function arguments that require post-processing. */
1555 typedef struct GTY(()) cp_default_arg_entry_d {
1556 /* The current_class_type when we parsed this arg. */
1557 tree class_type;
1559 /* The function decl itself. */
1560 tree decl;
1561 } cp_default_arg_entry;
1563 DEF_VEC_O(cp_default_arg_entry);
1564 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1566 /* An entry in a stack for member functions of local classes. */
1568 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1569 /* Functions with default arguments that require post-processing.
1570 Functions appear in this list in declaration order. */
1571 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1573 /* Functions with defintions that require post-processing. Functions
1574 appear in this list in declaration order. */
1575 VEC(tree,gc) *funs_with_definitions;
1576 } cp_unparsed_functions_entry;
1578 DEF_VEC_O(cp_unparsed_functions_entry);
1579 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1581 /* The cp_parser structure represents the C++ parser. */
1583 typedef struct GTY(()) cp_parser {
1584 /* The lexer from which we are obtaining tokens. */
1585 cp_lexer *lexer;
1587 /* The scope in which names should be looked up. If NULL_TREE, then
1588 we look up names in the scope that is currently open in the
1589 source program. If non-NULL, this is either a TYPE or
1590 NAMESPACE_DECL for the scope in which we should look. It can
1591 also be ERROR_MARK, when we've parsed a bogus scope.
1593 This value is not cleared automatically after a name is looked
1594 up, so we must be careful to clear it before starting a new look
1595 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1596 will look up `Z' in the scope of `X', rather than the current
1597 scope.) Unfortunately, it is difficult to tell when name lookup
1598 is complete, because we sometimes peek at a token, look it up,
1599 and then decide not to consume it. */
1600 tree scope;
1602 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1603 last lookup took place. OBJECT_SCOPE is used if an expression
1604 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1605 respectively. QUALIFYING_SCOPE is used for an expression of the
1606 form "X::Y"; it refers to X. */
1607 tree object_scope;
1608 tree qualifying_scope;
1610 /* A stack of parsing contexts. All but the bottom entry on the
1611 stack will be tentative contexts.
1613 We parse tentatively in order to determine which construct is in
1614 use in some situations. For example, in order to determine
1615 whether a statement is an expression-statement or a
1616 declaration-statement we parse it tentatively as a
1617 declaration-statement. If that fails, we then reparse the same
1618 token stream as an expression-statement. */
1619 cp_parser_context *context;
1621 /* True if we are parsing GNU C++. If this flag is not set, then
1622 GNU extensions are not recognized. */
1623 bool allow_gnu_extensions_p;
1625 /* TRUE if the `>' token should be interpreted as the greater-than
1626 operator. FALSE if it is the end of a template-id or
1627 template-parameter-list. In C++0x mode, this flag also applies to
1628 `>>' tokens, which are viewed as two consecutive `>' tokens when
1629 this flag is FALSE. */
1630 bool greater_than_is_operator_p;
1632 /* TRUE if default arguments are allowed within a parameter list
1633 that starts at this point. FALSE if only a gnu extension makes
1634 them permissible. */
1635 bool default_arg_ok_p;
1637 /* TRUE if we are parsing an integral constant-expression. See
1638 [expr.const] for a precise definition. */
1639 bool integral_constant_expression_p;
1641 /* TRUE if we are parsing an integral constant-expression -- but a
1642 non-constant expression should be permitted as well. This flag
1643 is used when parsing an array bound so that GNU variable-length
1644 arrays are tolerated. */
1645 bool allow_non_integral_constant_expression_p;
1647 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1648 been seen that makes the expression non-constant. */
1649 bool non_integral_constant_expression_p;
1651 /* TRUE if local variable names and `this' are forbidden in the
1652 current context. */
1653 bool local_variables_forbidden_p;
1655 /* TRUE if the declaration we are parsing is part of a
1656 linkage-specification of the form `extern string-literal
1657 declaration'. */
1658 bool in_unbraced_linkage_specification_p;
1660 /* TRUE if we are presently parsing a declarator, after the
1661 direct-declarator. */
1662 bool in_declarator_p;
1664 /* TRUE if we are presently parsing a template-argument-list. */
1665 bool in_template_argument_list_p;
1667 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1668 to IN_OMP_BLOCK if parsing OpenMP structured block and
1669 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1670 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1671 iteration-statement, OpenMP block or loop within that switch. */
1672 #define IN_SWITCH_STMT 1
1673 #define IN_ITERATION_STMT 2
1674 #define IN_OMP_BLOCK 4
1675 #define IN_OMP_FOR 8
1676 #define IN_IF_STMT 16
1677 unsigned char in_statement;
1679 /* TRUE if we are presently parsing the body of a switch statement.
1680 Note that this doesn't quite overlap with in_statement above.
1681 The difference relates to giving the right sets of error messages:
1682 "case not in switch" vs "break statement used with OpenMP...". */
1683 bool in_switch_statement_p;
1685 /* TRUE if we are parsing a type-id in an expression context. In
1686 such a situation, both "type (expr)" and "type (type)" are valid
1687 alternatives. */
1688 bool in_type_id_in_expr_p;
1690 /* TRUE if we are currently in a header file where declarations are
1691 implicitly extern "C". */
1692 bool implicit_extern_c;
1694 /* TRUE if strings in expressions should be translated to the execution
1695 character set. */
1696 bool translate_strings_p;
1698 /* TRUE if we are presently parsing the body of a function, but not
1699 a local class. */
1700 bool in_function_body;
1702 /* TRUE if we can auto-correct a colon to a scope operator. */
1703 bool colon_corrects_to_scope_p;
1705 /* If non-NULL, then we are parsing a construct where new type
1706 definitions are not permitted. The string stored here will be
1707 issued as an error message if a type is defined. */
1708 const char *type_definition_forbidden_message;
1710 /* A stack used for member functions of local classes. The lists
1711 contained in an individual entry can only be processed once the
1712 outermost class being defined is complete. */
1713 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1715 /* The number of classes whose definitions are currently in
1716 progress. */
1717 unsigned num_classes_being_defined;
1719 /* The number of template parameter lists that apply directly to the
1720 current declaration. */
1721 unsigned num_template_parameter_lists;
1722 } cp_parser;
1724 /* Managing the unparsed function queues. */
1726 #define unparsed_funs_with_default_args \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1728 #define unparsed_funs_with_definitions \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1734 VEC_safe_push (cp_unparsed_functions_entry, gc,
1735 parser->unparsed_queues, NULL);
1736 unparsed_funs_with_default_args = NULL;
1737 unparsed_funs_with_definitions = make_tree_vector ();
1740 static void
1741 pop_unparsed_function_queues (cp_parser *parser)
1743 release_tree_vector (unparsed_funs_with_definitions);
1744 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1747 /* Prototypes. */
1749 /* Constructors and destructors. */
1751 static cp_parser *cp_parser_new
1752 (void);
1754 /* Routines to parse various constructs.
1756 Those that return `tree' will return the error_mark_node (rather
1757 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1758 Sometimes, they will return an ordinary node if error-recovery was
1759 attempted, even though a parse error occurred. So, to check
1760 whether or not a parse error occurred, you should always use
1761 cp_parser_error_occurred. If the construct is optional (indicated
1762 either by an `_opt' in the name of the function that does the
1763 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1764 the construct is not present. */
1766 /* Lexical conventions [gram.lex] */
1768 static tree cp_parser_identifier
1769 (cp_parser *);
1770 static tree cp_parser_string_literal
1771 (cp_parser *, bool, bool);
1773 /* Basic concepts [gram.basic] */
1775 static bool cp_parser_translation_unit
1776 (cp_parser *);
1778 /* Expressions [gram.expr] */
1780 static tree cp_parser_primary_expression
1781 (cp_parser *, bool, bool, bool, cp_id_kind *);
1782 static tree cp_parser_id_expression
1783 (cp_parser *, bool, bool, bool *, bool, bool);
1784 static tree cp_parser_unqualified_id
1785 (cp_parser *, bool, bool, bool, bool);
1786 static tree cp_parser_nested_name_specifier_opt
1787 (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_nested_name_specifier
1789 (cp_parser *, bool, bool, bool, bool);
1790 static tree cp_parser_qualifying_entity
1791 (cp_parser *, bool, bool, bool, bool, bool);
1792 static tree cp_parser_postfix_expression
1793 (cp_parser *, bool, bool, bool, cp_id_kind *);
1794 static tree cp_parser_postfix_open_square_expression
1795 (cp_parser *, tree, bool);
1796 static tree cp_parser_postfix_dot_deref_expression
1797 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1798 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1799 (cp_parser *, int, bool, bool, bool *);
1800 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1801 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1802 static void cp_parser_pseudo_destructor_name
1803 (cp_parser *, tree *, tree *);
1804 static tree cp_parser_unary_expression
1805 (cp_parser *, bool, bool, cp_id_kind *);
1806 static enum tree_code cp_parser_unary_operator
1807 (cp_token *);
1808 static tree cp_parser_new_expression
1809 (cp_parser *);
1810 static VEC(tree,gc) *cp_parser_new_placement
1811 (cp_parser *);
1812 static tree cp_parser_new_type_id
1813 (cp_parser *, tree *);
1814 static cp_declarator *cp_parser_new_declarator_opt
1815 (cp_parser *);
1816 static cp_declarator *cp_parser_direct_new_declarator
1817 (cp_parser *);
1818 static VEC(tree,gc) *cp_parser_new_initializer
1819 (cp_parser *);
1820 static tree cp_parser_delete_expression
1821 (cp_parser *);
1822 static tree cp_parser_cast_expression
1823 (cp_parser *, bool, bool, cp_id_kind *);
1824 static tree cp_parser_binary_expression
1825 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1826 static tree cp_parser_question_colon_clause
1827 (cp_parser *, tree);
1828 static tree cp_parser_assignment_expression
1829 (cp_parser *, bool, cp_id_kind *);
1830 static enum tree_code cp_parser_assignment_operator_opt
1831 (cp_parser *);
1832 static tree cp_parser_expression
1833 (cp_parser *, bool, cp_id_kind *);
1834 static tree cp_parser_constant_expression
1835 (cp_parser *, bool, bool *);
1836 static tree cp_parser_builtin_offsetof
1837 (cp_parser *);
1838 static tree cp_parser_lambda_expression
1839 (cp_parser *);
1840 static void cp_parser_lambda_introducer
1841 (cp_parser *, tree);
1842 static void cp_parser_lambda_declarator_opt
1843 (cp_parser *, tree);
1844 static void cp_parser_lambda_body
1845 (cp_parser *, tree);
1847 /* Statements [gram.stmt.stmt] */
1849 static void cp_parser_statement
1850 (cp_parser *, tree, bool, bool *);
1851 static void cp_parser_label_for_labeled_statement
1852 (cp_parser *);
1853 static tree cp_parser_expression_statement
1854 (cp_parser *, tree);
1855 static tree cp_parser_compound_statement
1856 (cp_parser *, tree, bool);
1857 static void cp_parser_statement_seq_opt
1858 (cp_parser *, tree);
1859 static tree cp_parser_selection_statement
1860 (cp_parser *, bool *);
1861 static tree cp_parser_condition
1862 (cp_parser *);
1863 static tree cp_parser_iteration_statement
1864 (cp_parser *);
1865 static bool cp_parser_for_init_statement
1866 (cp_parser *, tree *decl);
1867 static tree cp_parser_for
1868 (cp_parser *);
1869 static tree cp_parser_c_for
1870 (cp_parser *, tree, tree);
1871 static tree cp_parser_range_for
1872 (cp_parser *, tree, tree, tree);
1873 static tree cp_parser_jump_statement
1874 (cp_parser *);
1875 static void cp_parser_declaration_statement
1876 (cp_parser *);
1878 static tree cp_parser_implicitly_scoped_statement
1879 (cp_parser *, bool *);
1880 static void cp_parser_already_scoped_statement
1881 (cp_parser *);
1883 /* Declarations [gram.dcl.dcl] */
1885 static void cp_parser_declaration_seq_opt
1886 (cp_parser *);
1887 static void cp_parser_declaration
1888 (cp_parser *);
1889 static void cp_parser_block_declaration
1890 (cp_parser *, bool);
1891 static void cp_parser_simple_declaration
1892 (cp_parser *, bool, tree *);
1893 static void cp_parser_decl_specifier_seq
1894 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1895 static tree cp_parser_storage_class_specifier_opt
1896 (cp_parser *);
1897 static tree cp_parser_function_specifier_opt
1898 (cp_parser *, cp_decl_specifier_seq *);
1899 static tree cp_parser_type_specifier
1900 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1901 int *, bool *);
1902 static tree cp_parser_simple_type_specifier
1903 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1904 static tree cp_parser_type_name
1905 (cp_parser *);
1906 static tree cp_parser_nonclass_name
1907 (cp_parser* parser);
1908 static tree cp_parser_elaborated_type_specifier
1909 (cp_parser *, bool, bool);
1910 static tree cp_parser_enum_specifier
1911 (cp_parser *);
1912 static void cp_parser_enumerator_list
1913 (cp_parser *, tree);
1914 static void cp_parser_enumerator_definition
1915 (cp_parser *, tree);
1916 static tree cp_parser_namespace_name
1917 (cp_parser *);
1918 static void cp_parser_namespace_definition
1919 (cp_parser *);
1920 static void cp_parser_namespace_body
1921 (cp_parser *);
1922 static tree cp_parser_qualified_namespace_specifier
1923 (cp_parser *);
1924 static void cp_parser_namespace_alias_definition
1925 (cp_parser *);
1926 static bool cp_parser_using_declaration
1927 (cp_parser *, bool);
1928 static void cp_parser_using_directive
1929 (cp_parser *);
1930 static void cp_parser_asm_definition
1931 (cp_parser *);
1932 static void cp_parser_linkage_specification
1933 (cp_parser *);
1934 static void cp_parser_static_assert
1935 (cp_parser *, bool);
1936 static tree cp_parser_decltype
1937 (cp_parser *);
1939 /* Declarators [gram.dcl.decl] */
1941 static tree cp_parser_init_declarator
1942 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1943 static cp_declarator *cp_parser_declarator
1944 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1945 static cp_declarator *cp_parser_direct_declarator
1946 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1947 static enum tree_code cp_parser_ptr_operator
1948 (cp_parser *, tree *, cp_cv_quals *);
1949 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1950 (cp_parser *);
1951 static tree cp_parser_late_return_type_opt
1952 (cp_parser *);
1953 static tree cp_parser_declarator_id
1954 (cp_parser *, bool);
1955 static tree cp_parser_type_id
1956 (cp_parser *);
1957 static tree cp_parser_template_type_arg
1958 (cp_parser *);
1959 static tree cp_parser_trailing_type_id (cp_parser *);
1960 static tree cp_parser_type_id_1
1961 (cp_parser *, bool, bool);
1962 static void cp_parser_type_specifier_seq
1963 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1964 static tree cp_parser_parameter_declaration_clause
1965 (cp_parser *);
1966 static tree cp_parser_parameter_declaration_list
1967 (cp_parser *, bool *);
1968 static cp_parameter_declarator *cp_parser_parameter_declaration
1969 (cp_parser *, bool, bool *);
1970 static tree cp_parser_default_argument
1971 (cp_parser *, bool);
1972 static void cp_parser_function_body
1973 (cp_parser *);
1974 static tree cp_parser_initializer
1975 (cp_parser *, bool *, bool *);
1976 static tree cp_parser_initializer_clause
1977 (cp_parser *, bool *);
1978 static tree cp_parser_braced_list
1979 (cp_parser*, bool*);
1980 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1981 (cp_parser *, bool *);
1983 static bool cp_parser_ctor_initializer_opt_and_function_body
1984 (cp_parser *);
1986 /* Classes [gram.class] */
1988 static tree cp_parser_class_name
1989 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1990 static tree cp_parser_class_specifier
1991 (cp_parser *);
1992 static tree cp_parser_class_head
1993 (cp_parser *, bool *, tree *, tree *);
1994 static enum tag_types cp_parser_class_key
1995 (cp_parser *);
1996 static void cp_parser_member_specification_opt
1997 (cp_parser *);
1998 static void cp_parser_member_declaration
1999 (cp_parser *);
2000 static tree cp_parser_pure_specifier
2001 (cp_parser *);
2002 static tree cp_parser_constant_initializer
2003 (cp_parser *);
2005 /* Derived classes [gram.class.derived] */
2007 static tree cp_parser_base_clause
2008 (cp_parser *);
2009 static tree cp_parser_base_specifier
2010 (cp_parser *);
2012 /* Special member functions [gram.special] */
2014 static tree cp_parser_conversion_function_id
2015 (cp_parser *);
2016 static tree cp_parser_conversion_type_id
2017 (cp_parser *);
2018 static cp_declarator *cp_parser_conversion_declarator_opt
2019 (cp_parser *);
2020 static bool cp_parser_ctor_initializer_opt
2021 (cp_parser *);
2022 static void cp_parser_mem_initializer_list
2023 (cp_parser *);
2024 static tree cp_parser_mem_initializer
2025 (cp_parser *);
2026 static tree cp_parser_mem_initializer_id
2027 (cp_parser *);
2029 /* Overloading [gram.over] */
2031 static tree cp_parser_operator_function_id
2032 (cp_parser *);
2033 static tree cp_parser_operator
2034 (cp_parser *);
2036 /* Templates [gram.temp] */
2038 static void cp_parser_template_declaration
2039 (cp_parser *, bool);
2040 static tree cp_parser_template_parameter_list
2041 (cp_parser *);
2042 static tree cp_parser_template_parameter
2043 (cp_parser *, bool *, bool *);
2044 static tree cp_parser_type_parameter
2045 (cp_parser *, bool *);
2046 static tree cp_parser_template_id
2047 (cp_parser *, bool, bool, bool);
2048 static tree cp_parser_template_name
2049 (cp_parser *, bool, bool, bool, bool *);
2050 static tree cp_parser_template_argument_list
2051 (cp_parser *);
2052 static tree cp_parser_template_argument
2053 (cp_parser *);
2054 static void cp_parser_explicit_instantiation
2055 (cp_parser *);
2056 static void cp_parser_explicit_specialization
2057 (cp_parser *);
2059 /* Exception handling [gram.exception] */
2061 static tree cp_parser_try_block
2062 (cp_parser *);
2063 static bool cp_parser_function_try_block
2064 (cp_parser *);
2065 static void cp_parser_handler_seq
2066 (cp_parser *);
2067 static void cp_parser_handler
2068 (cp_parser *);
2069 static tree cp_parser_exception_declaration
2070 (cp_parser *);
2071 static tree cp_parser_throw_expression
2072 (cp_parser *);
2073 static tree cp_parser_exception_specification_opt
2074 (cp_parser *);
2075 static tree cp_parser_type_id_list
2076 (cp_parser *);
2078 /* GNU Extensions */
2080 static tree cp_parser_asm_specification_opt
2081 (cp_parser *);
2082 static tree cp_parser_asm_operand_list
2083 (cp_parser *);
2084 static tree cp_parser_asm_clobber_list
2085 (cp_parser *);
2086 static tree cp_parser_asm_label_list
2087 (cp_parser *);
2088 static tree cp_parser_attributes_opt
2089 (cp_parser *);
2090 static tree cp_parser_attribute_list
2091 (cp_parser *);
2092 static bool cp_parser_extension_opt
2093 (cp_parser *, int *);
2094 static void cp_parser_label_declaration
2095 (cp_parser *);
2097 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2098 static bool cp_parser_pragma
2099 (cp_parser *, enum pragma_context);
2101 #include "objcp/plugin/parser.h"
2103 /* Utility Routines */
2105 static tree cp_parser_lookup_name
2106 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2107 static tree cp_parser_lookup_name_simple
2108 (cp_parser *, tree, location_t);
2109 static tree cp_parser_maybe_treat_template_as_class
2110 (tree, bool);
2111 static bool cp_parser_check_declarator_template_parameters
2112 (cp_parser *, cp_declarator *, location_t);
2113 static bool cp_parser_check_template_parameters
2114 (cp_parser *, unsigned, location_t, cp_declarator *);
2115 static tree cp_parser_simple_cast_expression
2116 (cp_parser *);
2117 static tree cp_parser_global_scope_opt
2118 (cp_parser *, bool);
2119 static bool cp_parser_constructor_declarator_p
2120 (cp_parser *, bool);
2121 static tree cp_parser_function_definition_from_specifiers_and_declarator
2122 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2123 static tree cp_parser_function_definition_after_declarator
2124 (cp_parser *, bool);
2125 static void cp_parser_template_declaration_after_export
2126 (cp_parser *, bool);
2127 static void cp_parser_perform_template_parameter_access_checks
2128 (VEC (deferred_access_check,gc)*);
2129 static tree cp_parser_single_declaration
2130 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2131 static tree cp_parser_functional_cast
2132 (cp_parser *, tree);
2133 static tree cp_parser_save_member_function_body
2134 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2135 static tree cp_parser_enclosed_template_argument_list
2136 (cp_parser *);
2137 static void cp_parser_save_default_args
2138 (cp_parser *, tree);
2139 static void cp_parser_late_parsing_for_member
2140 (cp_parser *, tree);
2141 static void cp_parser_late_parsing_default_args
2142 (cp_parser *, tree);
2143 static tree cp_parser_sizeof_operand
2144 (cp_parser *, enum rid);
2145 static tree cp_parser_trait_expr
2146 (cp_parser *, enum rid);
2147 static bool cp_parser_declares_only_class_p
2148 (cp_parser *);
2149 static void cp_parser_set_storage_class
2150 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2151 static void cp_parser_set_decl_spec_type
2152 (cp_decl_specifier_seq *, tree, location_t, bool);
2153 static bool cp_parser_friend_p
2154 (const cp_decl_specifier_seq *);
2155 static void cp_parser_required_error
2156 (cp_parser *, required_token, bool);
2157 static cp_token *cp_parser_require
2158 (cp_parser *, enum cpp_ttype, required_token);
2159 static cp_token *cp_parser_require_keyword
2160 (cp_parser *, enum rid, required_token);
2161 static bool cp_parser_token_starts_function_definition_p
2162 (cp_token *);
2163 static bool cp_parser_next_token_starts_class_definition_p
2164 (cp_parser *);
2165 static bool cp_parser_next_token_ends_template_argument_p
2166 (cp_parser *);
2167 static bool cp_parser_nth_token_starts_template_argument_list_p
2168 (cp_parser *, size_t);
2169 static enum tag_types cp_parser_token_is_class_key
2170 (cp_token *);
2171 static void cp_parser_check_class_key
2172 (enum tag_types, tree type);
2173 static void cp_parser_check_access_in_redeclaration
2174 (tree type, location_t location);
2175 static bool cp_parser_optional_template_keyword
2176 (cp_parser *);
2177 static void cp_parser_pre_parsed_nested_name_specifier
2178 (cp_parser *);
2179 static bool cp_parser_cache_group
2180 (cp_parser *, enum cpp_ttype, unsigned);
2181 static void cp_parser_parse_tentatively
2182 (cp_parser *);
2183 static void cp_parser_commit_to_tentative_parse
2184 (cp_parser *);
2185 static void cp_parser_abort_tentative_parse
2186 (cp_parser *);
2187 static bool cp_parser_parse_definitely
2188 (cp_parser *);
2189 static inline bool cp_parser_parsing_tentatively
2190 (cp_parser *);
2191 static bool cp_parser_uncommitted_to_tentative_parse_p
2192 (cp_parser *);
2193 static void cp_parser_error
2194 (cp_parser *, const char *);
2195 static void cp_parser_name_lookup_error
2196 (cp_parser *, tree, tree, name_lookup_error, location_t);
2197 static bool cp_parser_simulate_error
2198 (cp_parser *);
2199 static bool cp_parser_check_type_definition
2200 (cp_parser *);
2201 static void cp_parser_check_for_definition_in_return_type
2202 (cp_declarator *, tree, location_t type_location);
2203 static void cp_parser_check_for_invalid_template_id
2204 (cp_parser *, tree, location_t location);
2205 static bool cp_parser_non_integral_constant_expression
2206 (cp_parser *, non_integral_constant);
2207 static void cp_parser_diagnose_invalid_type_name
2208 (cp_parser *, tree, tree, location_t);
2209 static bool cp_parser_parse_and_diagnose_invalid_type_name
2210 (cp_parser *);
2211 static int cp_parser_skip_to_closing_parenthesis
2212 (cp_parser *, bool, bool, bool);
2213 static void cp_parser_skip_to_end_of_statement
2214 (cp_parser *);
2215 static void cp_parser_consume_semicolon_at_end_of_statement
2216 (cp_parser *);
2217 static void cp_parser_skip_to_end_of_block_or_statement
2218 (cp_parser *);
2219 static bool cp_parser_skip_to_closing_brace
2220 (cp_parser *);
2221 static void cp_parser_skip_to_end_of_template_parameter_list
2222 (cp_parser *);
2223 static void cp_parser_skip_to_pragma_eol
2224 (cp_parser*, cp_token *);
2225 static bool cp_parser_error_occurred
2226 (cp_parser *);
2227 static bool cp_parser_allow_gnu_extensions_p
2228 (cp_parser *);
2229 static bool cp_parser_is_string_literal
2230 (cp_token *);
2231 static bool cp_parser_is_keyword
2232 (cp_token *, enum rid);
2233 static tree cp_parser_make_typename_type
2234 (cp_parser *, tree, tree, location_t location);
2235 static cp_declarator * cp_parser_make_indirect_declarator
2236 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2238 /* Returns nonzero if we are parsing tentatively. */
2240 static inline bool
2241 cp_parser_parsing_tentatively (cp_parser* parser)
2243 return parser->context->next != NULL;
2246 /* Returns nonzero if TOKEN is a string literal. */
2248 static bool
2249 cp_parser_is_string_literal (cp_token* token)
2251 return (token->type == CPP_STRING ||
2252 token->type == CPP_STRING16 ||
2253 token->type == CPP_STRING32 ||
2254 token->type == CPP_WSTRING ||
2255 token->type == CPP_UTF8STRING);
2258 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2260 static bool
2261 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2263 return token->keyword == keyword;
2266 /* If not parsing tentatively, issue a diagnostic of the form
2267 FILE:LINE: MESSAGE before TOKEN
2268 where TOKEN is the next token in the input stream. MESSAGE
2269 (specified by the caller) is usually of the form "expected
2270 OTHER-TOKEN". */
2272 static void
2273 cp_parser_error (cp_parser* parser, const char* gmsgid)
2275 if (!cp_parser_simulate_error (parser))
2277 cp_token *token = cp_lexer_peek_token (parser->lexer);
2278 /* This diagnostic makes more sense if it is tagged to the line
2279 of the token we just peeked at. */
2280 cp_lexer_set_source_position_from_token (token);
2282 if (token->type == CPP_PRAGMA)
2284 error_at (token->location,
2285 "%<#pragma%> is not allowed here");
2286 cp_parser_skip_to_pragma_eol (parser, token);
2287 return;
2290 c_parse_error (gmsgid,
2291 /* Because c_parser_error does not understand
2292 CPP_KEYWORD, keywords are treated like
2293 identifiers. */
2294 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2295 token->u.value, token->flags);
2299 /* Issue an error about name-lookup failing. NAME is the
2300 IDENTIFIER_NODE DECL is the result of
2301 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2302 the thing that we hoped to find. */
2304 static void
2305 cp_parser_name_lookup_error (cp_parser* parser,
2306 tree name,
2307 tree decl,
2308 name_lookup_error desired,
2309 location_t location)
2311 /* If name lookup completely failed, tell the user that NAME was not
2312 declared. */
2313 if (decl == error_mark_node)
2315 if (parser->scope && parser->scope != global_namespace)
2316 error_at (location, "%<%E::%E%> has not been declared",
2317 parser->scope, name);
2318 else if (parser->scope == global_namespace)
2319 error_at (location, "%<::%E%> has not been declared", name);
2320 else if (parser->object_scope
2321 && !CLASS_TYPE_P (parser->object_scope))
2322 error_at (location, "request for member %qE in non-class type %qT",
2323 name, parser->object_scope);
2324 else if (parser->object_scope)
2325 error_at (location, "%<%T::%E%> has not been declared",
2326 parser->object_scope, name);
2327 else
2328 error_at (location, "%qE has not been declared", name);
2330 else if (parser->scope && parser->scope != global_namespace)
2332 switch (desired)
2334 case NLE_TYPE:
2335 error_at (location, "%<%E::%E%> is not a type",
2336 parser->scope, name);
2337 break;
2338 case NLE_CXX98:
2339 error_at (location, "%<%E::%E%> is not a class or namespace",
2340 parser->scope, name);
2341 break;
2342 case NLE_NOT_CXX98:
2343 error_at (location,
2344 "%<%E::%E%> is not a class, namespace, or enumeration",
2345 parser->scope, name);
2346 break;
2347 default:
2348 gcc_unreachable ();
2352 else if (parser->scope == global_namespace)
2354 switch (desired)
2356 case NLE_TYPE:
2357 error_at (location, "%<::%E%> is not a type", name);
2358 break;
2359 case NLE_CXX98:
2360 error_at (location, "%<::%E%> is not a class or namespace", name);
2361 break;
2362 case NLE_NOT_CXX98:
2363 error_at (location,
2364 "%<::%E%> is not a class, namespace, or enumeration",
2365 name);
2366 break;
2367 default:
2368 gcc_unreachable ();
2371 else
2373 switch (desired)
2375 case NLE_TYPE:
2376 error_at (location, "%qE is not a type", name);
2377 break;
2378 case NLE_CXX98:
2379 error_at (location, "%qE is not a class or namespace", name);
2380 break;
2381 case NLE_NOT_CXX98:
2382 error_at (location,
2383 "%qE is not a class, namespace, or enumeration", name);
2384 break;
2385 default:
2386 gcc_unreachable ();
2391 /* If we are parsing tentatively, remember that an error has occurred
2392 during this tentative parse. Returns true if the error was
2393 simulated; false if a message should be issued by the caller. */
2395 static bool
2396 cp_parser_simulate_error (cp_parser* parser)
2398 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2400 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2401 return true;
2403 return false;
2406 /* Check for repeated decl-specifiers. */
2408 static void
2409 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2410 location_t location)
2412 int ds;
2414 for (ds = ds_first; ds != ds_last; ++ds)
2416 unsigned count = decl_specs->specs[ds];
2417 if (count < 2)
2418 continue;
2419 /* The "long" specifier is a special case because of "long long". */
2420 if (ds == ds_long)
2422 if (count > 2)
2423 error_at (location, "%<long long long%> is too long for GCC");
2424 else
2425 pedwarn_cxx98 (location, OPT_Wlong_long,
2426 "ISO C++ 1998 does not support %<long long%>");
2428 else if (count > 1)
2430 static const char *const decl_spec_names[] = {
2431 "signed",
2432 "unsigned",
2433 "short",
2434 "long",
2435 "const",
2436 "volatile",
2437 "restrict",
2438 "inline",
2439 "virtual",
2440 "explicit",
2441 "friend",
2442 "typedef",
2443 "constexpr",
2444 "__complex",
2445 "__thread"
2447 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2452 /* This function is called when a type is defined. If type
2453 definitions are forbidden at this point, an error message is
2454 issued. */
2456 static bool
2457 cp_parser_check_type_definition (cp_parser* parser)
2459 /* If types are forbidden here, issue a message. */
2460 if (parser->type_definition_forbidden_message)
2462 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2463 in the message need to be interpreted. */
2464 error (parser->type_definition_forbidden_message);
2465 return false;
2467 return true;
2470 /* This function is called when the DECLARATOR is processed. The TYPE
2471 was a type defined in the decl-specifiers. If it is invalid to
2472 define a type in the decl-specifiers for DECLARATOR, an error is
2473 issued. TYPE_LOCATION is the location of TYPE and is used
2474 for error reporting. */
2476 static void
2477 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2478 tree type, location_t type_location)
2480 /* [dcl.fct] forbids type definitions in return types.
2481 Unfortunately, it's not easy to know whether or not we are
2482 processing a return type until after the fact. */
2483 while (declarator
2484 && (declarator->kind == cdk_pointer
2485 || declarator->kind == cdk_reference
2486 || declarator->kind == cdk_ptrmem))
2487 declarator = declarator->declarator;
2488 if (declarator
2489 && declarator->kind == cdk_function)
2491 error_at (type_location,
2492 "new types may not be defined in a return type");
2493 inform (type_location,
2494 "(perhaps a semicolon is missing after the definition of %qT)",
2495 type);
2499 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2500 "<" in any valid C++ program. If the next token is indeed "<",
2501 issue a message warning the user about what appears to be an
2502 invalid attempt to form a template-id. LOCATION is the location
2503 of the type-specifier (TYPE) */
2505 static void
2506 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2507 tree type, location_t location)
2509 cp_token_position start = 0;
2511 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2513 if (TYPE_P (type))
2514 error_at (location, "%qT is not a template", type);
2515 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2516 error_at (location, "%qE is not a template", type);
2517 else
2518 error_at (location, "invalid template-id");
2519 /* Remember the location of the invalid "<". */
2520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2521 start = cp_lexer_token_position (parser->lexer, true);
2522 /* Consume the "<". */
2523 cp_lexer_consume_token (parser->lexer);
2524 /* Parse the template arguments. */
2525 cp_parser_enclosed_template_argument_list (parser);
2526 /* Permanently remove the invalid template arguments so that
2527 this error message is not issued again. */
2528 if (start)
2529 cp_lexer_purge_tokens_after (parser->lexer, start);
2533 /* If parsing an integral constant-expression, issue an error message
2534 about the fact that THING appeared and return true. Otherwise,
2535 return false. In either case, set
2536 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2538 static bool
2539 cp_parser_non_integral_constant_expression (cp_parser *parser,
2540 non_integral_constant thing)
2542 parser->non_integral_constant_expression_p = true;
2543 if (parser->integral_constant_expression_p)
2545 if (!parser->allow_non_integral_constant_expression_p)
2547 const char *msg = NULL;
2548 switch (thing)
2550 case NIC_FLOAT:
2551 error ("floating-point literal "
2552 "cannot appear in a constant-expression");
2553 return true;
2554 case NIC_CAST:
2555 error ("a cast to a type other than an integral or "
2556 "enumeration type cannot appear in a "
2557 "constant-expression");
2558 return true;
2559 case NIC_TYPEID:
2560 error ("%<typeid%> operator "
2561 "cannot appear in a constant-expression");
2562 return true;
2563 case NIC_NCC:
2564 error ("non-constant compound literals "
2565 "cannot appear in a constant-expression");
2566 return true;
2567 case NIC_FUNC_CALL:
2568 error ("a function call "
2569 "cannot appear in a constant-expression");
2570 return true;
2571 case NIC_INC:
2572 error ("an increment "
2573 "cannot appear in a constant-expression");
2574 return true;
2575 case NIC_DEC:
2576 error ("an decrement "
2577 "cannot appear in a constant-expression");
2578 return true;
2579 case NIC_ARRAY_REF:
2580 error ("an array reference "
2581 "cannot appear in a constant-expression");
2582 return true;
2583 case NIC_ADDR_LABEL:
2584 error ("the address of a label "
2585 "cannot appear in a constant-expression");
2586 return true;
2587 case NIC_OVERLOADED:
2588 error ("calls to overloaded operators "
2589 "cannot appear in a constant-expression");
2590 return true;
2591 case NIC_ASSIGNMENT:
2592 error ("an assignment cannot appear in a constant-expression");
2593 return true;
2594 case NIC_COMMA:
2595 error ("a comma operator "
2596 "cannot appear in a constant-expression");
2597 return true;
2598 case NIC_CONSTRUCTOR:
2599 error ("a call to a constructor "
2600 "cannot appear in a constant-expression");
2601 return true;
2602 case NIC_THIS:
2603 msg = "this";
2604 break;
2605 case NIC_FUNC_NAME:
2606 msg = "__FUNCTION__";
2607 break;
2608 case NIC_PRETTY_FUNC:
2609 msg = "__PRETTY_FUNCTION__";
2610 break;
2611 case NIC_C99_FUNC:
2612 msg = "__func__";
2613 break;
2614 case NIC_VA_ARG:
2615 msg = "va_arg";
2616 break;
2617 case NIC_ARROW:
2618 msg = "->";
2619 break;
2620 case NIC_POINT:
2621 msg = ".";
2622 break;
2623 case NIC_STAR:
2624 msg = "*";
2625 break;
2626 case NIC_ADDR:
2627 msg = "&";
2628 break;
2629 case NIC_PREINCREMENT:
2630 msg = "++";
2631 break;
2632 case NIC_PREDECREMENT:
2633 msg = "--";
2634 break;
2635 case NIC_NEW:
2636 msg = "new";
2637 break;
2638 case NIC_DEL:
2639 msg = "delete";
2640 break;
2641 default:
2642 gcc_unreachable ();
2644 if (msg)
2645 error ("%qs cannot appear in a constant-expression", msg);
2646 return true;
2649 return false;
2652 /* Emit a diagnostic for an invalid type name. SCOPE is the
2653 qualifying scope (or NULL, if none) for ID. This function commits
2654 to the current active tentative parse, if any. (Otherwise, the
2655 problematic construct might be encountered again later, resulting
2656 in duplicate error messages.) LOCATION is the location of ID. */
2658 static void
2659 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2660 tree scope, tree id,
2661 location_t location)
2663 tree decl, old_scope;
2664 /* Try to lookup the identifier. */
2665 old_scope = parser->scope;
2666 parser->scope = scope;
2667 decl = cp_parser_lookup_name_simple (parser, id, location);
2668 parser->scope = old_scope;
2669 /* If the lookup found a template-name, it means that the user forgot
2670 to specify an argument list. Emit a useful error message. */
2671 if (TREE_CODE (decl) == TEMPLATE_DECL)
2672 error_at (location,
2673 "invalid use of template-name %qE without an argument list",
2674 decl);
2675 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2676 error_at (location, "invalid use of destructor %qD as a type", id);
2677 else if (TREE_CODE (decl) == TYPE_DECL)
2678 /* Something like 'unsigned A a;' */
2679 error_at (location, "invalid combination of multiple type-specifiers");
2680 else if (!parser->scope)
2682 /* Issue an error message. */
2683 error_at (location, "%qE does not name a type", id);
2684 /* If we're in a template class, it's possible that the user was
2685 referring to a type from a base class. For example:
2687 template <typename T> struct A { typedef T X; };
2688 template <typename T> struct B : public A<T> { X x; };
2690 The user should have said "typename A<T>::X". */
2691 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2692 inform (location, "C++0x %<constexpr%> only available with "
2693 "-std=c++0x or -std=gnu++0x");
2694 else if (processing_template_decl && current_class_type
2695 && TYPE_BINFO (current_class_type))
2697 tree b;
2699 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2701 b = TREE_CHAIN (b))
2703 tree base_type = BINFO_TYPE (b);
2704 if (CLASS_TYPE_P (base_type)
2705 && dependent_type_p (base_type))
2707 tree field;
2708 /* Go from a particular instantiation of the
2709 template (which will have an empty TYPE_FIELDs),
2710 to the main version. */
2711 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2712 for (field = TYPE_FIELDS (base_type);
2713 field;
2714 field = DECL_CHAIN (field))
2715 if (TREE_CODE (field) == TYPE_DECL
2716 && DECL_NAME (field) == id)
2718 inform (location,
2719 "(perhaps %<typename %T::%E%> was intended)",
2720 BINFO_TYPE (b), id);
2721 break;
2723 if (field)
2724 break;
2729 /* Here we diagnose qualified-ids where the scope is actually correct,
2730 but the identifier does not resolve to a valid type name. */
2731 else if (parser->scope != error_mark_node)
2733 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2734 error_at (location, "%qE in namespace %qE does not name a type",
2735 id, parser->scope);
2736 else if (CLASS_TYPE_P (parser->scope)
2737 && constructor_name_p (id, parser->scope))
2739 /* A<T>::A<T>() */
2740 error_at (location, "%<%T::%E%> names the constructor, not"
2741 " the type", parser->scope, id);
2742 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2743 error_at (location, "and %qT has no template constructors",
2744 parser->scope);
2746 else if (TYPE_P (parser->scope)
2747 && dependent_scope_p (parser->scope))
2748 error_at (location, "need %<typename%> before %<%T::%E%> because "
2749 "%qT is a dependent scope",
2750 parser->scope, id, parser->scope);
2751 else if (TYPE_P (parser->scope))
2752 error_at (location, "%qE in class %qT does not name a type",
2753 id, parser->scope);
2754 else
2755 gcc_unreachable ();
2757 cp_parser_commit_to_tentative_parse (parser);
2760 /* Check for a common situation where a type-name should be present,
2761 but is not, and issue a sensible error message. Returns true if an
2762 invalid type-name was detected.
2764 The situation handled by this function are variable declarations of the
2765 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2766 Usually, `ID' should name a type, but if we got here it means that it
2767 does not. We try to emit the best possible error message depending on
2768 how exactly the id-expression looks like. */
2770 static bool
2771 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2773 tree id;
2774 cp_token *token = cp_lexer_peek_token (parser->lexer);
2776 /* Avoid duplicate error about ambiguous lookup. */
2777 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2779 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2780 if (next->type == CPP_NAME && next->ambiguous_p)
2781 goto out;
2784 cp_parser_parse_tentatively (parser);
2785 id = cp_parser_id_expression (parser,
2786 /*template_keyword_p=*/false,
2787 /*check_dependency_p=*/true,
2788 /*template_p=*/NULL,
2789 /*declarator_p=*/true,
2790 /*optional_p=*/false);
2791 /* If the next token is a (, this is a function with no explicit return
2792 type, i.e. constructor, destructor or conversion op. */
2793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2794 || TREE_CODE (id) == TYPE_DECL)
2796 cp_parser_abort_tentative_parse (parser);
2797 return false;
2799 if (!cp_parser_parse_definitely (parser))
2800 return false;
2802 /* Emit a diagnostic for the invalid type. */
2803 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2804 id, token->location);
2805 out:
2806 /* If we aren't in the middle of a declarator (i.e. in a
2807 parameter-declaration-clause), skip to the end of the declaration;
2808 there's no point in trying to process it. */
2809 if (!parser->in_declarator_p)
2810 cp_parser_skip_to_end_of_block_or_statement (parser);
2811 return true;
2814 /* Consume tokens up to, and including, the next non-nested closing `)'.
2815 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2816 are doing error recovery. Returns -1 if OR_COMMA is true and we
2817 found an unnested comma. */
2819 static int
2820 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2821 bool recovering,
2822 bool or_comma,
2823 bool consume_paren)
2825 unsigned paren_depth = 0;
2826 unsigned brace_depth = 0;
2827 unsigned square_depth = 0;
2829 if (recovering && !or_comma
2830 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2831 return 0;
2833 while (true)
2835 cp_token * token = cp_lexer_peek_token (parser->lexer);
2837 switch (token->type)
2839 case CPP_EOF:
2840 case CPP_PRAGMA_EOL:
2841 /* If we've run out of tokens, then there is no closing `)'. */
2842 return 0;
2844 /* This is good for lambda expression capture-lists. */
2845 case CPP_OPEN_SQUARE:
2846 ++square_depth;
2847 break;
2848 case CPP_CLOSE_SQUARE:
2849 if (!square_depth--)
2850 return 0;
2851 break;
2853 case CPP_SEMICOLON:
2854 /* This matches the processing in skip_to_end_of_statement. */
2855 if (!brace_depth)
2856 return 0;
2857 break;
2859 case CPP_OPEN_BRACE:
2860 ++brace_depth;
2861 break;
2862 case CPP_CLOSE_BRACE:
2863 if (!brace_depth--)
2864 return 0;
2865 break;
2867 case CPP_COMMA:
2868 if (recovering && or_comma && !brace_depth && !paren_depth
2869 && !square_depth)
2870 return -1;
2871 break;
2873 case CPP_OPEN_PAREN:
2874 if (!brace_depth)
2875 ++paren_depth;
2876 break;
2878 case CPP_CLOSE_PAREN:
2879 if (!brace_depth && !paren_depth--)
2881 if (consume_paren)
2882 cp_lexer_consume_token (parser->lexer);
2883 return 1;
2885 break;
2887 default:
2888 break;
2891 /* Consume the token. */
2892 cp_lexer_consume_token (parser->lexer);
2896 /* Consume tokens until we reach the end of the current statement.
2897 Normally, that will be just before consuming a `;'. However, if a
2898 non-nested `}' comes first, then we stop before consuming that. */
2900 static void
2901 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2903 unsigned nesting_depth = 0;
2905 while (true)
2907 cp_token *token = cp_lexer_peek_token (parser->lexer);
2909 switch (token->type)
2911 case CPP_EOF:
2912 case CPP_PRAGMA_EOL:
2913 /* If we've run out of tokens, stop. */
2914 return;
2916 case CPP_SEMICOLON:
2917 /* If the next token is a `;', we have reached the end of the
2918 statement. */
2919 if (!nesting_depth)
2920 return;
2921 break;
2923 case CPP_CLOSE_BRACE:
2924 /* If this is a non-nested '}', stop before consuming it.
2925 That way, when confronted with something like:
2927 { 3 + }
2929 we stop before consuming the closing '}', even though we
2930 have not yet reached a `;'. */
2931 if (nesting_depth == 0)
2932 return;
2934 /* If it is the closing '}' for a block that we have
2935 scanned, stop -- but only after consuming the token.
2936 That way given:
2938 void f g () { ... }
2939 typedef int I;
2941 we will stop after the body of the erroneously declared
2942 function, but before consuming the following `typedef'
2943 declaration. */
2944 if (--nesting_depth == 0)
2946 cp_lexer_consume_token (parser->lexer);
2947 return;
2950 case CPP_OPEN_BRACE:
2951 ++nesting_depth;
2952 break;
2954 default:
2955 break;
2958 /* Consume the token. */
2959 cp_lexer_consume_token (parser->lexer);
2963 /* This function is called at the end of a statement or declaration.
2964 If the next token is a semicolon, it is consumed; otherwise, error
2965 recovery is attempted. */
2967 static void
2968 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2970 /* Look for the trailing `;'. */
2971 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2973 /* If there is additional (erroneous) input, skip to the end of
2974 the statement. */
2975 cp_parser_skip_to_end_of_statement (parser);
2976 /* If the next token is now a `;', consume it. */
2977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2978 cp_lexer_consume_token (parser->lexer);
2982 /* Skip tokens until we have consumed an entire block, or until we
2983 have consumed a non-nested `;'. */
2985 static void
2986 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2988 int nesting_depth = 0;
2990 while (nesting_depth >= 0)
2992 cp_token *token = cp_lexer_peek_token (parser->lexer);
2994 switch (token->type)
2996 case CPP_EOF:
2997 case CPP_PRAGMA_EOL:
2998 /* If we've run out of tokens, stop. */
2999 return;
3001 case CPP_SEMICOLON:
3002 /* Stop if this is an unnested ';'. */
3003 if (!nesting_depth)
3004 nesting_depth = -1;
3005 break;
3007 case CPP_CLOSE_BRACE:
3008 /* Stop if this is an unnested '}', or closes the outermost
3009 nesting level. */
3010 nesting_depth--;
3011 if (nesting_depth < 0)
3012 return;
3013 if (!nesting_depth)
3014 nesting_depth = -1;
3015 break;
3017 case CPP_OPEN_BRACE:
3018 /* Nest. */
3019 nesting_depth++;
3020 break;
3022 default:
3023 break;
3026 /* Consume the token. */
3027 cp_lexer_consume_token (parser->lexer);
3031 /* Skip tokens until a non-nested closing curly brace is the next
3032 token, or there are no more tokens. Return true in the first case,
3033 false otherwise. */
3035 static bool
3036 cp_parser_skip_to_closing_brace (cp_parser *parser)
3038 unsigned nesting_depth = 0;
3040 while (true)
3042 cp_token *token = cp_lexer_peek_token (parser->lexer);
3044 switch (token->type)
3046 case CPP_EOF:
3047 case CPP_PRAGMA_EOL:
3048 /* If we've run out of tokens, stop. */
3049 return false;
3051 case CPP_CLOSE_BRACE:
3052 /* If the next token is a non-nested `}', then we have reached
3053 the end of the current block. */
3054 if (nesting_depth-- == 0)
3055 return true;
3056 break;
3058 case CPP_OPEN_BRACE:
3059 /* If it the next token is a `{', then we are entering a new
3060 block. Consume the entire block. */
3061 ++nesting_depth;
3062 break;
3064 default:
3065 break;
3068 /* Consume the token. */
3069 cp_lexer_consume_token (parser->lexer);
3073 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3074 parameter is the PRAGMA token, allowing us to purge the entire pragma
3075 sequence. */
3077 static void
3078 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3080 cp_token *token;
3082 parser->lexer->in_pragma = false;
3085 token = cp_lexer_consume_token (parser->lexer);
3086 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3088 /* Ensure that the pragma is not parsed again. */
3089 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3092 /* Require pragma end of line, resyncing with it as necessary. The
3093 arguments are as for cp_parser_skip_to_pragma_eol. */
3095 static void
3096 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3098 parser->lexer->in_pragma = false;
3099 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3100 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3103 /* This is a simple wrapper around make_typename_type. When the id is
3104 an unresolved identifier node, we can provide a superior diagnostic
3105 using cp_parser_diagnose_invalid_type_name. */
3107 static tree
3108 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3109 tree id, location_t id_location)
3111 tree result;
3112 if (TREE_CODE (id) == IDENTIFIER_NODE)
3114 result = make_typename_type (scope, id, typename_type,
3115 /*complain=*/tf_none);
3116 if (result == error_mark_node)
3117 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3118 return result;
3120 return make_typename_type (scope, id, typename_type, tf_error);
3123 /* This is a wrapper around the
3124 make_{pointer,ptrmem,reference}_declarator functions that decides
3125 which one to call based on the CODE and CLASS_TYPE arguments. The
3126 CODE argument should be one of the values returned by
3127 cp_parser_ptr_operator. */
3128 static cp_declarator *
3129 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3130 cp_cv_quals cv_qualifiers,
3131 cp_declarator *target)
3133 if (code == ERROR_MARK)
3134 return cp_error_declarator;
3136 if (code == INDIRECT_REF)
3137 if (class_type == NULL_TREE)
3138 return make_pointer_declarator (cv_qualifiers, target);
3139 else
3140 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3141 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3142 return make_reference_declarator (cv_qualifiers, target, false);
3143 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3144 return make_reference_declarator (cv_qualifiers, target, true);
3145 gcc_unreachable ();
3148 /* Create a new C++ parser. */
3150 static cp_parser *
3151 cp_parser_new (void)
3153 cp_parser *parser;
3154 cp_lexer *lexer;
3155 unsigned i;
3157 /* cp_lexer_new_main is called before doing GC allocation because
3158 cp_lexer_new_main might load a PCH file. */
3159 lexer = cp_lexer_new_main ();
3161 /* Initialize the binops_by_token so that we can get the tree
3162 directly from the token. */
3163 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3164 binops_by_token[binops[i].token_type] = binops[i];
3166 parser = ggc_alloc_cleared_cp_parser ();
3167 parser->lexer = lexer;
3168 parser->context = cp_parser_context_new (NULL);
3170 /* For now, we always accept GNU extensions. */
3171 parser->allow_gnu_extensions_p = 1;
3173 /* The `>' token is a greater-than operator, not the end of a
3174 template-id. */
3175 parser->greater_than_is_operator_p = true;
3177 parser->default_arg_ok_p = true;
3179 /* We are not parsing a constant-expression. */
3180 parser->integral_constant_expression_p = false;
3181 parser->allow_non_integral_constant_expression_p = false;
3182 parser->non_integral_constant_expression_p = false;
3184 /* Local variable names are not forbidden. */
3185 parser->local_variables_forbidden_p = false;
3187 /* We are not processing an `extern "C"' declaration. */
3188 parser->in_unbraced_linkage_specification_p = false;
3190 /* We are not processing a declarator. */
3191 parser->in_declarator_p = false;
3193 /* We are not processing a template-argument-list. */
3194 parser->in_template_argument_list_p = false;
3196 /* We are not in an iteration statement. */
3197 parser->in_statement = 0;
3199 /* We are not in a switch statement. */
3200 parser->in_switch_statement_p = false;
3202 /* We are not parsing a type-id inside an expression. */
3203 parser->in_type_id_in_expr_p = false;
3205 /* Declarations aren't implicitly extern "C". */
3206 parser->implicit_extern_c = false;
3208 /* String literals should be translated to the execution character set. */
3209 parser->translate_strings_p = true;
3211 /* We are not parsing a function body. */
3212 parser->in_function_body = false;
3214 /* We can correct until told otherwise. */
3215 parser->colon_corrects_to_scope_p = true;
3217 /* The unparsed function queue is empty. */
3218 push_unparsed_function_queues (parser);
3220 /* There are no classes being defined. */
3221 parser->num_classes_being_defined = 0;
3223 /* No template parameters apply. */
3224 parser->num_template_parameter_lists = 0;
3226 return parser;
3229 /* Create a cp_lexer structure which will emit the tokens in CACHE
3230 and push it onto the parser's lexer stack. This is used for delayed
3231 parsing of in-class method bodies and default arguments, and should
3232 not be confused with tentative parsing. */
3233 static void
3234 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3236 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3237 lexer->next = parser->lexer;
3238 parser->lexer = lexer;
3240 /* Move the current source position to that of the first token in the
3241 new lexer. */
3242 cp_lexer_set_source_position_from_token (lexer->next_token);
3245 /* Pop the top lexer off the parser stack. This is never used for the
3246 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3247 static void
3248 cp_parser_pop_lexer (cp_parser *parser)
3250 cp_lexer *lexer = parser->lexer;
3251 parser->lexer = lexer->next;
3252 cp_lexer_destroy (lexer);
3254 /* Put the current source position back where it was before this
3255 lexer was pushed. */
3256 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3259 /* Lexical conventions [gram.lex] */
3261 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3262 identifier. */
3264 static tree
3265 cp_parser_identifier (cp_parser* parser)
3267 cp_token *token;
3269 /* Look for the identifier. */
3270 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3271 /* Return the value. */
3272 return token ? token->u.value : error_mark_node;
3275 /* Parse a sequence of adjacent string constants. Returns a
3276 TREE_STRING representing the combined, nul-terminated string
3277 constant. If TRANSLATE is true, translate the string to the
3278 execution character set. If WIDE_OK is true, a wide string is
3279 invalid here.
3281 C++98 [lex.string] says that if a narrow string literal token is
3282 adjacent to a wide string literal token, the behavior is undefined.
3283 However, C99 6.4.5p4 says that this results in a wide string literal.
3284 We follow C99 here, for consistency with the C front end.
3286 This code is largely lifted from lex_string() in c-lex.c.
3288 FUTURE: ObjC++ will need to handle @-strings here. */
3289 static tree
3290 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3292 tree value;
3293 size_t count;
3294 struct obstack str_ob;
3295 cpp_string str, istr, *strs;
3296 cp_token *tok;
3297 enum cpp_ttype type;
3299 tok = cp_lexer_peek_token (parser->lexer);
3300 if (!cp_parser_is_string_literal (tok))
3302 cp_parser_error (parser, "expected string-literal");
3303 return error_mark_node;
3306 type = tok->type;
3308 /* Try to avoid the overhead of creating and destroying an obstack
3309 for the common case of just one string. */
3310 if (!cp_parser_is_string_literal
3311 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3313 cp_lexer_consume_token (parser->lexer);
3315 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3316 str.len = TREE_STRING_LENGTH (tok->u.value);
3317 count = 1;
3319 strs = &str;
3321 else
3323 gcc_obstack_init (&str_ob);
3324 count = 0;
3328 cp_lexer_consume_token (parser->lexer);
3329 count++;
3330 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3331 str.len = TREE_STRING_LENGTH (tok->u.value);
3333 if (type != tok->type)
3335 if (type == CPP_STRING)
3336 type = tok->type;
3337 else if (tok->type != CPP_STRING)
3338 error_at (tok->location,
3339 "unsupported non-standard concatenation "
3340 "of string literals");
3343 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3345 tok = cp_lexer_peek_token (parser->lexer);
3347 while (cp_parser_is_string_literal (tok));
3349 strs = (cpp_string *) obstack_finish (&str_ob);
3352 if (type != CPP_STRING && !wide_ok)
3354 cp_parser_error (parser, "a wide string is invalid in this context");
3355 type = CPP_STRING;
3358 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3359 (parse_in, strs, count, &istr, type))
3361 value = build_string (istr.len, (const char *)istr.text);
3362 free (CONST_CAST (unsigned char *, istr.text));
3364 switch (type)
3366 default:
3367 case CPP_STRING:
3368 case CPP_UTF8STRING:
3369 TREE_TYPE (value) = char_array_type_node;
3370 break;
3371 case CPP_STRING16:
3372 TREE_TYPE (value) = char16_array_type_node;
3373 break;
3374 case CPP_STRING32:
3375 TREE_TYPE (value) = char32_array_type_node;
3376 break;
3377 case CPP_WSTRING:
3378 TREE_TYPE (value) = wchar_array_type_node;
3379 break;
3382 value = fix_string_type (value);
3384 else
3385 /* cpp_interpret_string has issued an error. */
3386 value = error_mark_node;
3388 if (count > 1)
3389 obstack_free (&str_ob, 0);
3391 return value;
3395 /* Basic concepts [gram.basic] */
3397 /* Parse a translation-unit.
3399 translation-unit:
3400 declaration-seq [opt]
3402 Returns TRUE if all went well. */
3404 static bool
3405 cp_parser_translation_unit (cp_parser* parser)
3407 /* The address of the first non-permanent object on the declarator
3408 obstack. */
3409 static void *declarator_obstack_base;
3411 bool success;
3413 /* Create the declarator obstack, if necessary. */
3414 if (!cp_error_declarator)
3416 gcc_obstack_init (&declarator_obstack);
3417 /* Create the error declarator. */
3418 cp_error_declarator = make_declarator (cdk_error);
3419 /* Create the empty parameter list. */
3420 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3421 /* Remember where the base of the declarator obstack lies. */
3422 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3425 cp_parser_declaration_seq_opt (parser);
3427 /* If there are no tokens left then all went well. */
3428 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3430 /* Get rid of the token array; we don't need it any more. */
3431 cp_lexer_destroy (parser->lexer);
3432 parser->lexer = NULL;
3434 /* This file might have been a context that's implicitly extern
3435 "C". If so, pop the lang context. (Only relevant for PCH.) */
3436 if (parser->implicit_extern_c)
3438 pop_lang_context ();
3439 parser->implicit_extern_c = false;
3442 /* Finish up. */
3443 finish_translation_unit ();
3445 success = true;
3447 else
3449 cp_parser_error (parser, "expected declaration");
3450 success = false;
3453 /* Make sure the declarator obstack was fully cleaned up. */
3454 gcc_assert (obstack_next_free (&declarator_obstack)
3455 == declarator_obstack_base);
3457 /* All went well. */
3458 return success;
3461 /* Expressions [gram.expr] */
3463 /* Parse a primary-expression.
3465 primary-expression:
3466 literal
3467 this
3468 ( expression )
3469 id-expression
3471 GNU Extensions:
3473 primary-expression:
3474 ( compound-statement )
3475 __builtin_va_arg ( assignment-expression , type-id )
3476 __builtin_offsetof ( type-id , offsetof-expression )
3478 C++ Extensions:
3479 __has_nothrow_assign ( type-id )
3480 __has_nothrow_constructor ( type-id )
3481 __has_nothrow_copy ( type-id )
3482 __has_trivial_assign ( type-id )
3483 __has_trivial_constructor ( type-id )
3484 __has_trivial_copy ( type-id )
3485 __has_trivial_destructor ( type-id )
3486 __has_virtual_destructor ( type-id )
3487 __is_abstract ( type-id )
3488 __is_base_of ( type-id , type-id )
3489 __is_class ( type-id )
3490 __is_convertible_to ( type-id , type-id )
3491 __is_empty ( type-id )
3492 __is_enum ( type-id )
3493 __is_pod ( type-id )
3494 __is_polymorphic ( type-id )
3495 __is_union ( type-id )
3497 Objective-C++ Extension:
3499 primary-expression:
3500 objc-expression
3502 literal:
3503 __null
3505 ADDRESS_P is true iff this expression was immediately preceded by
3506 "&" and therefore might denote a pointer-to-member. CAST_P is true
3507 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3508 true iff this expression is a template argument.
3510 Returns a representation of the expression. Upon return, *IDK
3511 indicates what kind of id-expression (if any) was present. */
3513 static tree
3514 cp_parser_primary_expression (cp_parser *parser,
3515 bool address_p,
3516 bool cast_p,
3517 bool template_arg_p,
3518 cp_id_kind *idk)
3520 cp_token *token = NULL;
3522 /* Assume the primary expression is not an id-expression. */
3523 *idk = CP_ID_KIND_NONE;
3525 /* Peek at the next token. */
3526 token = cp_lexer_peek_token (parser->lexer);
3527 switch (token->type)
3529 /* literal:
3530 integer-literal
3531 character-literal
3532 floating-literal
3533 string-literal
3534 boolean-literal */
3535 case CPP_CHAR:
3536 case CPP_CHAR16:
3537 case CPP_CHAR32:
3538 case CPP_WCHAR:
3539 case CPP_NUMBER:
3540 token = cp_lexer_consume_token (parser->lexer);
3541 if (TREE_CODE (token->u.value) == FIXED_CST)
3543 error_at (token->location,
3544 "fixed-point types not supported in C++");
3545 return error_mark_node;
3547 /* Floating-point literals are only allowed in an integral
3548 constant expression if they are cast to an integral or
3549 enumeration type. */
3550 if (TREE_CODE (token->u.value) == REAL_CST
3551 && parser->integral_constant_expression_p
3552 && pedantic)
3554 /* CAST_P will be set even in invalid code like "int(2.7 +
3555 ...)". Therefore, we have to check that the next token
3556 is sure to end the cast. */
3557 if (cast_p)
3559 cp_token *next_token;
3561 next_token = cp_lexer_peek_token (parser->lexer);
3562 if (/* The comma at the end of an
3563 enumerator-definition. */
3564 next_token->type != CPP_COMMA
3565 /* The curly brace at the end of an enum-specifier. */
3566 && next_token->type != CPP_CLOSE_BRACE
3567 /* The end of a statement. */
3568 && next_token->type != CPP_SEMICOLON
3569 /* The end of the cast-expression. */
3570 && next_token->type != CPP_CLOSE_PAREN
3571 /* The end of an array bound. */
3572 && next_token->type != CPP_CLOSE_SQUARE
3573 /* The closing ">" in a template-argument-list. */
3574 && (next_token->type != CPP_GREATER
3575 || parser->greater_than_is_operator_p)
3576 /* C++0x only: A ">>" treated like two ">" tokens,
3577 in a template-argument-list. */
3578 && (next_token->type != CPP_RSHIFT
3579 || (cxx_dialect == cxx98)
3580 || parser->greater_than_is_operator_p))
3581 cast_p = false;
3584 /* If we are within a cast, then the constraint that the
3585 cast is to an integral or enumeration type will be
3586 checked at that point. If we are not within a cast, then
3587 this code is invalid. */
3588 if (!cast_p)
3589 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3591 return token->u.value;
3593 case CPP_STRING:
3594 case CPP_STRING16:
3595 case CPP_STRING32:
3596 case CPP_WSTRING:
3597 case CPP_UTF8STRING:
3598 /* ??? Should wide strings be allowed when parser->translate_strings_p
3599 is false (i.e. in attributes)? If not, we can kill the third
3600 argument to cp_parser_string_literal. */
3601 return cp_parser_string_literal (parser,
3602 parser->translate_strings_p,
3603 true);
3605 case CPP_OPEN_PAREN:
3607 tree expr;
3608 bool saved_greater_than_is_operator_p;
3610 /* Consume the `('. */
3611 cp_lexer_consume_token (parser->lexer);
3612 /* Within a parenthesized expression, a `>' token is always
3613 the greater-than operator. */
3614 saved_greater_than_is_operator_p
3615 = parser->greater_than_is_operator_p;
3616 parser->greater_than_is_operator_p = true;
3617 /* If we see `( { ' then we are looking at the beginning of
3618 a GNU statement-expression. */
3619 if (cp_parser_allow_gnu_extensions_p (parser)
3620 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3622 /* Statement-expressions are not allowed by the standard. */
3623 pedwarn (token->location, OPT_pedantic,
3624 "ISO C++ forbids braced-groups within expressions");
3626 /* And they're not allowed outside of a function-body; you
3627 cannot, for example, write:
3629 int i = ({ int j = 3; j + 1; });
3631 at class or namespace scope. */
3632 if (!parser->in_function_body
3633 || parser->in_template_argument_list_p)
3635 error_at (token->location,
3636 "statement-expressions are not allowed outside "
3637 "functions nor in template-argument lists");
3638 cp_parser_skip_to_end_of_block_or_statement (parser);
3639 expr = error_mark_node;
3641 else
3643 /* Start the statement-expression. */
3644 expr = begin_stmt_expr ();
3645 /* Parse the compound-statement. */
3646 cp_parser_compound_statement (parser, expr, false);
3647 /* Finish up. */
3648 expr = finish_stmt_expr (expr, false);
3651 else
3653 /* Parse the parenthesized expression. */
3654 expr = cp_parser_expression (parser, cast_p, idk);
3655 /* Let the front end know that this expression was
3656 enclosed in parentheses. This matters in case, for
3657 example, the expression is of the form `A::B', since
3658 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3659 not. */
3660 finish_parenthesized_expr (expr);
3662 /* The `>' token might be the end of a template-id or
3663 template-parameter-list now. */
3664 parser->greater_than_is_operator_p
3665 = saved_greater_than_is_operator_p;
3666 /* Consume the `)'. */
3667 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3668 cp_parser_skip_to_end_of_statement (parser);
3670 return expr;
3673 case CPP_OPEN_SQUARE:
3674 PLUGIN_PRIMARY_EXPRESSION_3(parser);
3675 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3676 return cp_parser_lambda_expression (parser);
3678 PLUGIN_PRIMARY_EXPRESSION_2(parser, cp_parser_error)
3680 case CPP_KEYWORD:
3681 switch (token->keyword)
3683 /* These two are the boolean literals. */
3684 case RID_TRUE:
3685 cp_lexer_consume_token (parser->lexer);
3686 return boolean_true_node;
3687 case RID_FALSE:
3688 cp_lexer_consume_token (parser->lexer);
3689 return boolean_false_node;
3691 /* The `__null' literal. */
3692 case RID_NULL:
3693 cp_lexer_consume_token (parser->lexer);
3694 return null_node;
3696 /* The `nullptr' literal. */
3697 case RID_NULLPTR:
3698 cp_lexer_consume_token (parser->lexer);
3699 return nullptr_node;
3701 /* Recognize the `this' keyword. */
3702 case RID_THIS:
3703 cp_lexer_consume_token (parser->lexer);
3704 if (parser->local_variables_forbidden_p)
3706 error_at (token->location,
3707 "%<this%> may not be used in this context");
3708 return error_mark_node;
3710 /* Pointers cannot appear in constant-expressions. */
3711 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3712 return error_mark_node;
3713 return finish_this_expr ();
3715 /* The `operator' keyword can be the beginning of an
3716 id-expression. */
3717 case RID_OPERATOR:
3718 goto id_expression;
3720 case RID_FUNCTION_NAME:
3721 case RID_PRETTY_FUNCTION_NAME:
3722 case RID_C99_FUNCTION_NAME:
3724 non_integral_constant name;
3726 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3727 __func__ are the names of variables -- but they are
3728 treated specially. Therefore, they are handled here,
3729 rather than relying on the generic id-expression logic
3730 below. Grammatically, these names are id-expressions.
3732 Consume the token. */
3733 token = cp_lexer_consume_token (parser->lexer);
3735 switch (token->keyword)
3737 case RID_FUNCTION_NAME:
3738 name = NIC_FUNC_NAME;
3739 break;
3740 case RID_PRETTY_FUNCTION_NAME:
3741 name = NIC_PRETTY_FUNC;
3742 break;
3743 case RID_C99_FUNCTION_NAME:
3744 name = NIC_C99_FUNC;
3745 break;
3746 default:
3747 gcc_unreachable ();
3750 if (cp_parser_non_integral_constant_expression (parser, name))
3751 return error_mark_node;
3753 /* Look up the name. */
3754 return finish_fname (token->u.value);
3757 case RID_VA_ARG:
3759 tree expression;
3760 tree type;
3762 /* The `__builtin_va_arg' construct is used to handle
3763 `va_arg'. Consume the `__builtin_va_arg' token. */
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Look for the opening `('. */
3766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3767 /* Now, parse the assignment-expression. */
3768 expression = cp_parser_assignment_expression (parser,
3769 /*cast_p=*/false, NULL);
3770 /* Look for the `,'. */
3771 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3772 /* Parse the type-id. */
3773 type = cp_parser_type_id (parser);
3774 /* Look for the closing `)'. */
3775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3776 /* Using `va_arg' in a constant-expression is not
3777 allowed. */
3778 if (cp_parser_non_integral_constant_expression (parser,
3779 NIC_VA_ARG))
3780 return error_mark_node;
3781 return build_x_va_arg (expression, type);
3784 case RID_OFFSETOF:
3785 return cp_parser_builtin_offsetof (parser);
3787 case RID_HAS_NOTHROW_ASSIGN:
3788 case RID_HAS_NOTHROW_CONSTRUCTOR:
3789 case RID_HAS_NOTHROW_COPY:
3790 case RID_HAS_TRIVIAL_ASSIGN:
3791 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3792 case RID_HAS_TRIVIAL_COPY:
3793 case RID_HAS_TRIVIAL_DESTRUCTOR:
3794 case RID_HAS_VIRTUAL_DESTRUCTOR:
3795 case RID_IS_ABSTRACT:
3796 case RID_IS_BASE_OF:
3797 case RID_IS_CLASS:
3798 case RID_IS_CONVERTIBLE_TO:
3799 case RID_IS_EMPTY:
3800 case RID_IS_ENUM:
3801 case RID_IS_POD:
3802 case RID_IS_POLYMORPHIC:
3803 case RID_IS_STD_LAYOUT:
3804 case RID_IS_TRIVIAL:
3805 case RID_IS_UNION:
3806 case RID_IS_LITERAL_TYPE:
3807 return cp_parser_trait_expr (parser, token->keyword);
3809 PLUGIN_PRIMARY_EXPRESSION_1(parser)
3811 case RID_TEMPLATE:
3812 if (parser->in_function_body
3813 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3814 == CPP_LESS))
3816 error_at (token->location,
3817 "a template declaration cannot appear at block scope");
3818 cp_parser_skip_to_end_of_block_or_statement (parser);
3819 return error_mark_node;
3821 default:
3822 cp_parser_error (parser, "expected primary-expression");
3823 return error_mark_node;
3826 /* An id-expression can start with either an identifier, a
3827 `::' as the beginning of a qualified-id, or the "operator"
3828 keyword. */
3829 case CPP_NAME:
3830 case CPP_SCOPE:
3831 case CPP_TEMPLATE_ID:
3832 case CPP_NESTED_NAME_SPECIFIER:
3834 tree id_expression;
3835 tree decl;
3836 const char *error_msg;
3837 bool template_p;
3838 bool done;
3839 cp_token *id_expr_token;
3841 id_expression:
3842 /* Parse the id-expression. */
3843 id_expression
3844 = cp_parser_id_expression (parser,
3845 /*template_keyword_p=*/false,
3846 /*check_dependency_p=*/true,
3847 &template_p,
3848 /*declarator_p=*/false,
3849 /*optional_p=*/false);
3850 if (id_expression == error_mark_node)
3851 return error_mark_node;
3852 id_expr_token = token;
3853 token = cp_lexer_peek_token (parser->lexer);
3854 done = (token->type != CPP_OPEN_SQUARE
3855 && token->type != CPP_OPEN_PAREN
3856 && token->type != CPP_DOT
3857 && token->type != CPP_DEREF
3858 && token->type != CPP_PLUS_PLUS
3859 && token->type != CPP_MINUS_MINUS);
3860 /* If we have a template-id, then no further lookup is
3861 required. If the template-id was for a template-class, we
3862 will sometimes have a TYPE_DECL at this point. */
3863 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3864 || TREE_CODE (id_expression) == TYPE_DECL)
3865 decl = id_expression;
3866 /* Look up the name. */
3867 else
3869 tree ambiguous_decls;
3871 /* If we already know that this lookup is ambiguous, then
3872 we've already issued an error message; there's no reason
3873 to check again. */
3874 if (id_expr_token->type == CPP_NAME
3875 && id_expr_token->ambiguous_p)
3877 cp_parser_simulate_error (parser);
3878 return error_mark_node;
3881 decl = cp_parser_lookup_name (parser, id_expression,
3882 none_type,
3883 template_p,
3884 /*is_namespace=*/false,
3885 /*check_dependency=*/true,
3886 &ambiguous_decls,
3887 id_expr_token->location);
3888 /* If the lookup was ambiguous, an error will already have
3889 been issued. */
3890 if (ambiguous_decls)
3891 return error_mark_node;
3893 PLUGIN_PRIMARY_EXPRESSION (parser, decl, cp_lexer_consume_token,
3894 cp_lexer_peek_token);
3896 /* If name lookup gives us a SCOPE_REF, then the
3897 qualifying scope was dependent. */
3898 if (TREE_CODE (decl) == SCOPE_REF)
3900 /* At this point, we do not know if DECL is a valid
3901 integral constant expression. We assume that it is
3902 in fact such an expression, so that code like:
3904 template <int N> struct A {
3905 int a[B<N>::i];
3908 is accepted. At template-instantiation time, we
3909 will check that B<N>::i is actually a constant. */
3910 return decl;
3912 /* Check to see if DECL is a local variable in a context
3913 where that is forbidden. */
3914 if (parser->local_variables_forbidden_p
3915 && local_variable_p (decl))
3917 /* It might be that we only found DECL because we are
3918 trying to be generous with pre-ISO scoping rules.
3919 For example, consider:
3921 int i;
3922 void g() {
3923 for (int i = 0; i < 10; ++i) {}
3924 extern void f(int j = i);
3927 Here, name look up will originally find the out
3928 of scope `i'. We need to issue a warning message,
3929 but then use the global `i'. */
3930 decl = check_for_out_of_scope_variable (decl);
3931 if (local_variable_p (decl))
3933 error_at (id_expr_token->location,
3934 "local variable %qD may not appear in this context",
3935 decl);
3936 return error_mark_node;
3941 decl = (finish_id_expression
3942 (id_expression, decl, parser->scope,
3943 idk,
3944 parser->integral_constant_expression_p,
3945 parser->allow_non_integral_constant_expression_p,
3946 &parser->non_integral_constant_expression_p,
3947 template_p, done, address_p,
3948 template_arg_p,
3949 &error_msg,
3950 id_expr_token->location));
3951 if (error_msg)
3952 cp_parser_error (parser, error_msg);
3953 return decl;
3956 /* Anything else is an error. */
3957 default:
3958 cp_parser_error (parser, "expected primary-expression");
3959 return error_mark_node;
3963 /* Parse an id-expression.
3965 id-expression:
3966 unqualified-id
3967 qualified-id
3969 qualified-id:
3970 :: [opt] nested-name-specifier template [opt] unqualified-id
3971 :: identifier
3972 :: operator-function-id
3973 :: template-id
3975 Return a representation of the unqualified portion of the
3976 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3977 a `::' or nested-name-specifier.
3979 Often, if the id-expression was a qualified-id, the caller will
3980 want to make a SCOPE_REF to represent the qualified-id. This
3981 function does not do this in order to avoid wastefully creating
3982 SCOPE_REFs when they are not required.
3984 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3985 `template' keyword.
3987 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3988 uninstantiated templates.
3990 If *TEMPLATE_P is non-NULL, it is set to true iff the
3991 `template' keyword is used to explicitly indicate that the entity
3992 named is a template.
3994 If DECLARATOR_P is true, the id-expression is appearing as part of
3995 a declarator, rather than as part of an expression. */
3997 static tree
3998 cp_parser_id_expression (cp_parser *parser,
3999 bool template_keyword_p,
4000 bool check_dependency_p,
4001 bool *template_p,
4002 bool declarator_p,
4003 bool optional_p)
4005 bool global_scope_p;
4006 bool nested_name_specifier_p;
4008 /* Assume the `template' keyword was not used. */
4009 if (template_p)
4010 *template_p = template_keyword_p;
4012 /* Look for the optional `::' operator. */
4013 global_scope_p
4014 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4015 != NULL_TREE);
4016 /* Look for the optional nested-name-specifier. */
4017 nested_name_specifier_p
4018 = (cp_parser_nested_name_specifier_opt (parser,
4019 /*typename_keyword_p=*/false,
4020 check_dependency_p,
4021 /*type_p=*/false,
4022 declarator_p)
4023 != NULL_TREE);
4024 /* If there is a nested-name-specifier, then we are looking at
4025 the first qualified-id production. */
4026 if (nested_name_specifier_p)
4028 tree saved_scope;
4029 tree saved_object_scope;
4030 tree saved_qualifying_scope;
4031 tree unqualified_id;
4032 bool is_template;
4034 /* See if the next token is the `template' keyword. */
4035 if (!template_p)
4036 template_p = &is_template;
4037 *template_p = cp_parser_optional_template_keyword (parser);
4038 /* Name lookup we do during the processing of the
4039 unqualified-id might obliterate SCOPE. */
4040 saved_scope = parser->scope;
4041 saved_object_scope = parser->object_scope;
4042 saved_qualifying_scope = parser->qualifying_scope;
4043 /* Process the final unqualified-id. */
4044 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4045 check_dependency_p,
4046 declarator_p,
4047 /*optional_p=*/false);
4048 /* Restore the SAVED_SCOPE for our caller. */
4049 parser->scope = saved_scope;
4050 parser->object_scope = saved_object_scope;
4051 parser->qualifying_scope = saved_qualifying_scope;
4053 return unqualified_id;
4055 /* Otherwise, if we are in global scope, then we are looking at one
4056 of the other qualified-id productions. */
4057 else if (global_scope_p)
4059 cp_token *token;
4060 tree id;
4062 /* Peek at the next token. */
4063 token = cp_lexer_peek_token (parser->lexer);
4065 /* If it's an identifier, and the next token is not a "<", then
4066 we can avoid the template-id case. This is an optimization
4067 for this common case. */
4068 if (token->type == CPP_NAME
4069 && !cp_parser_nth_token_starts_template_argument_list_p
4070 (parser, 2))
4071 return cp_parser_identifier (parser);
4073 cp_parser_parse_tentatively (parser);
4074 /* Try a template-id. */
4075 id = cp_parser_template_id (parser,
4076 /*template_keyword_p=*/false,
4077 /*check_dependency_p=*/true,
4078 declarator_p);
4079 /* If that worked, we're done. */
4080 if (cp_parser_parse_definitely (parser))
4081 return id;
4083 /* Peek at the next token. (Changes in the token buffer may
4084 have invalidated the pointer obtained above.) */
4085 token = cp_lexer_peek_token (parser->lexer);
4087 switch (token->type)
4089 case CPP_NAME:
4090 return cp_parser_identifier (parser);
4092 case CPP_KEYWORD:
4093 if (token->keyword == RID_OPERATOR)
4094 return cp_parser_operator_function_id (parser);
4095 /* Fall through. */
4097 default:
4098 cp_parser_error (parser, "expected id-expression");
4099 return error_mark_node;
4102 else
4103 return cp_parser_unqualified_id (parser, template_keyword_p,
4104 /*check_dependency_p=*/true,
4105 declarator_p,
4106 optional_p);
4109 /* Parse an unqualified-id.
4111 unqualified-id:
4112 identifier
4113 operator-function-id
4114 conversion-function-id
4115 ~ class-name
4116 template-id
4118 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4119 keyword, in a construct like `A::template ...'.
4121 Returns a representation of unqualified-id. For the `identifier'
4122 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4123 production a BIT_NOT_EXPR is returned; the operand of the
4124 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4125 other productions, see the documentation accompanying the
4126 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4127 names are looked up in uninstantiated templates. If DECLARATOR_P
4128 is true, the unqualified-id is appearing as part of a declarator,
4129 rather than as part of an expression. */
4131 static tree
4132 cp_parser_unqualified_id (cp_parser* parser,
4133 bool template_keyword_p,
4134 bool check_dependency_p,
4135 bool declarator_p,
4136 bool optional_p)
4138 cp_token *token;
4140 /* Peek at the next token. */
4141 token = cp_lexer_peek_token (parser->lexer);
4143 switch (token->type)
4145 case CPP_NAME:
4147 tree id;
4149 /* We don't know yet whether or not this will be a
4150 template-id. */
4151 cp_parser_parse_tentatively (parser);
4152 /* Try a template-id. */
4153 id = cp_parser_template_id (parser, template_keyword_p,
4154 check_dependency_p,
4155 declarator_p);
4156 /* If it worked, we're done. */
4157 if (cp_parser_parse_definitely (parser))
4158 return id;
4159 /* Otherwise, it's an ordinary identifier. */
4160 return cp_parser_identifier (parser);
4163 case CPP_TEMPLATE_ID:
4164 return cp_parser_template_id (parser, template_keyword_p,
4165 check_dependency_p,
4166 declarator_p);
4168 case CPP_COMPL:
4170 tree type_decl;
4171 tree qualifying_scope;
4172 tree object_scope;
4173 tree scope;
4174 bool done;
4176 /* Consume the `~' token. */
4177 cp_lexer_consume_token (parser->lexer);
4178 /* Parse the class-name. The standard, as written, seems to
4179 say that:
4181 template <typename T> struct S { ~S (); };
4182 template <typename T> S<T>::~S() {}
4184 is invalid, since `~' must be followed by a class-name, but
4185 `S<T>' is dependent, and so not known to be a class.
4186 That's not right; we need to look in uninstantiated
4187 templates. A further complication arises from:
4189 template <typename T> void f(T t) {
4190 t.T::~T();
4193 Here, it is not possible to look up `T' in the scope of `T'
4194 itself. We must look in both the current scope, and the
4195 scope of the containing complete expression.
4197 Yet another issue is:
4199 struct S {
4200 int S;
4201 ~S();
4204 S::~S() {}
4206 The standard does not seem to say that the `S' in `~S'
4207 should refer to the type `S' and not the data member
4208 `S::S'. */
4210 /* DR 244 says that we look up the name after the "~" in the
4211 same scope as we looked up the qualifying name. That idea
4212 isn't fully worked out; it's more complicated than that. */
4213 scope = parser->scope;
4214 object_scope = parser->object_scope;
4215 qualifying_scope = parser->qualifying_scope;
4217 /* Check for invalid scopes. */
4218 if (scope == error_mark_node)
4220 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4221 cp_lexer_consume_token (parser->lexer);
4222 return error_mark_node;
4224 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4226 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4227 error_at (token->location,
4228 "scope %qT before %<~%> is not a class-name",
4229 scope);
4230 cp_parser_simulate_error (parser);
4231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4232 cp_lexer_consume_token (parser->lexer);
4233 return error_mark_node;
4235 gcc_assert (!scope || TYPE_P (scope));
4237 /* If the name is of the form "X::~X" it's OK even if X is a
4238 typedef. */
4239 token = cp_lexer_peek_token (parser->lexer);
4240 if (scope
4241 && token->type == CPP_NAME
4242 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4243 != CPP_LESS)
4244 && (token->u.value == TYPE_IDENTIFIER (scope)
4245 || constructor_name_p (token->u.value, scope)))
4247 cp_lexer_consume_token (parser->lexer);
4248 return build_nt (BIT_NOT_EXPR, scope);
4251 /* If there was an explicit qualification (S::~T), first look
4252 in the scope given by the qualification (i.e., S).
4254 Note: in the calls to cp_parser_class_name below we pass
4255 typename_type so that lookup finds the injected-class-name
4256 rather than the constructor. */
4257 done = false;
4258 type_decl = NULL_TREE;
4259 if (scope)
4261 cp_parser_parse_tentatively (parser);
4262 type_decl = cp_parser_class_name (parser,
4263 /*typename_keyword_p=*/false,
4264 /*template_keyword_p=*/false,
4265 typename_type,
4266 /*check_dependency=*/false,
4267 /*class_head_p=*/false,
4268 declarator_p);
4269 if (cp_parser_parse_definitely (parser))
4270 done = true;
4272 /* In "N::S::~S", look in "N" as well. */
4273 if (!done && scope && qualifying_scope)
4275 cp_parser_parse_tentatively (parser);
4276 parser->scope = qualifying_scope;
4277 parser->object_scope = NULL_TREE;
4278 parser->qualifying_scope = NULL_TREE;
4279 type_decl
4280 = cp_parser_class_name (parser,
4281 /*typename_keyword_p=*/false,
4282 /*template_keyword_p=*/false,
4283 typename_type,
4284 /*check_dependency=*/false,
4285 /*class_head_p=*/false,
4286 declarator_p);
4287 if (cp_parser_parse_definitely (parser))
4288 done = true;
4290 /* In "p->S::~T", look in the scope given by "*p" as well. */
4291 else if (!done && object_scope)
4293 cp_parser_parse_tentatively (parser);
4294 parser->scope = object_scope;
4295 parser->object_scope = NULL_TREE;
4296 parser->qualifying_scope = NULL_TREE;
4297 type_decl
4298 = cp_parser_class_name (parser,
4299 /*typename_keyword_p=*/false,
4300 /*template_keyword_p=*/false,
4301 typename_type,
4302 /*check_dependency=*/false,
4303 /*class_head_p=*/false,
4304 declarator_p);
4305 if (cp_parser_parse_definitely (parser))
4306 done = true;
4308 /* Look in the surrounding context. */
4309 if (!done)
4311 parser->scope = NULL_TREE;
4312 parser->object_scope = NULL_TREE;
4313 parser->qualifying_scope = NULL_TREE;
4314 if (processing_template_decl)
4315 cp_parser_parse_tentatively (parser);
4316 type_decl
4317 = cp_parser_class_name (parser,
4318 /*typename_keyword_p=*/false,
4319 /*template_keyword_p=*/false,
4320 typename_type,
4321 /*check_dependency=*/false,
4322 /*class_head_p=*/false,
4323 declarator_p);
4324 if (processing_template_decl
4325 && ! cp_parser_parse_definitely (parser))
4327 /* We couldn't find a type with this name, so just accept
4328 it and check for a match at instantiation time. */
4329 type_decl = cp_parser_identifier (parser);
4330 if (type_decl != error_mark_node)
4331 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4332 return type_decl;
4335 /* If an error occurred, assume that the name of the
4336 destructor is the same as the name of the qualifying
4337 class. That allows us to keep parsing after running
4338 into ill-formed destructor names. */
4339 if (type_decl == error_mark_node && scope)
4340 return build_nt (BIT_NOT_EXPR, scope);
4341 else if (type_decl == error_mark_node)
4342 return error_mark_node;
4344 /* Check that destructor name and scope match. */
4345 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4348 error_at (token->location,
4349 "declaration of %<~%T%> as member of %qT",
4350 type_decl, scope);
4351 cp_parser_simulate_error (parser);
4352 return error_mark_node;
4355 /* [class.dtor]
4357 A typedef-name that names a class shall not be used as the
4358 identifier in the declarator for a destructor declaration. */
4359 if (declarator_p
4360 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4361 && !DECL_SELF_REFERENCE_P (type_decl)
4362 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4363 error_at (token->location,
4364 "typedef-name %qD used as destructor declarator",
4365 type_decl);
4367 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4370 case CPP_KEYWORD:
4371 if (token->keyword == RID_OPERATOR)
4373 tree id;
4375 /* This could be a template-id, so we try that first. */
4376 cp_parser_parse_tentatively (parser);
4377 /* Try a template-id. */
4378 id = cp_parser_template_id (parser, template_keyword_p,
4379 /*check_dependency_p=*/true,
4380 declarator_p);
4381 /* If that worked, we're done. */
4382 if (cp_parser_parse_definitely (parser))
4383 return id;
4384 /* We still don't know whether we're looking at an
4385 operator-function-id or a conversion-function-id. */
4386 cp_parser_parse_tentatively (parser);
4387 /* Try an operator-function-id. */
4388 id = cp_parser_operator_function_id (parser);
4389 /* If that didn't work, try a conversion-function-id. */
4390 if (!cp_parser_parse_definitely (parser))
4391 id = cp_parser_conversion_function_id (parser);
4393 return id;
4395 /* Fall through. */
4397 default:
4398 if (optional_p)
4399 return NULL_TREE;
4400 cp_parser_error (parser, "expected unqualified-id");
4401 return error_mark_node;
4405 /* Parse an (optional) nested-name-specifier.
4407 nested-name-specifier: [C++98]
4408 class-or-namespace-name :: nested-name-specifier [opt]
4409 class-or-namespace-name :: template nested-name-specifier [opt]
4411 nested-name-specifier: [C++0x]
4412 type-name ::
4413 namespace-name ::
4414 nested-name-specifier identifier ::
4415 nested-name-specifier template [opt] simple-template-id ::
4417 PARSER->SCOPE should be set appropriately before this function is
4418 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4419 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4420 in name lookups.
4422 Sets PARSER->SCOPE to the class (TYPE) or namespace
4423 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4424 it unchanged if there is no nested-name-specifier. Returns the new
4425 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4427 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4428 part of a declaration and/or decl-specifier. */
4430 static tree
4431 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4432 bool typename_keyword_p,
4433 bool check_dependency_p,
4434 bool type_p,
4435 bool is_declaration)
4437 bool success = false;
4438 cp_token_position start = 0;
4439 cp_token *token;
4441 /* Remember where the nested-name-specifier starts. */
4442 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4444 start = cp_lexer_token_position (parser->lexer, false);
4445 push_deferring_access_checks (dk_deferred);
4448 while (true)
4450 tree new_scope;
4451 tree old_scope;
4452 tree saved_qualifying_scope;
4453 bool template_keyword_p;
4455 /* Spot cases that cannot be the beginning of a
4456 nested-name-specifier. */
4457 token = cp_lexer_peek_token (parser->lexer);
4459 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4460 the already parsed nested-name-specifier. */
4461 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4463 /* Grab the nested-name-specifier and continue the loop. */
4464 cp_parser_pre_parsed_nested_name_specifier (parser);
4465 /* If we originally encountered this nested-name-specifier
4466 with IS_DECLARATION set to false, we will not have
4467 resolved TYPENAME_TYPEs, so we must do so here. */
4468 if (is_declaration
4469 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4471 new_scope = resolve_typename_type (parser->scope,
4472 /*only_current_p=*/false);
4473 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4474 parser->scope = new_scope;
4476 success = true;
4477 continue;
4480 /* Spot cases that cannot be the beginning of a
4481 nested-name-specifier. On the second and subsequent times
4482 through the loop, we look for the `template' keyword. */
4483 if (success && token->keyword == RID_TEMPLATE)
4485 /* A template-id can start a nested-name-specifier. */
4486 else if (token->type == CPP_TEMPLATE_ID)
4488 else
4490 /* If the next token is not an identifier, then it is
4491 definitely not a type-name or namespace-name. */
4492 if (token->type != CPP_NAME)
4493 break;
4494 /* If the following token is neither a `<' (to begin a
4495 template-id), nor a `::', then we are not looking at a
4496 nested-name-specifier. */
4497 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4499 if (token->type == CPP_COLON
4500 && parser->colon_corrects_to_scope_p
4501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4503 error_at (token->location,
4504 "found %<:%> in nested-name-specifier, expected %<::%>");
4505 token->type = CPP_SCOPE;
4508 if (token->type != CPP_SCOPE
4509 && !cp_parser_nth_token_starts_template_argument_list_p
4510 (parser, 2))
4511 break;
4514 /* The nested-name-specifier is optional, so we parse
4515 tentatively. */
4516 cp_parser_parse_tentatively (parser);
4518 /* Look for the optional `template' keyword, if this isn't the
4519 first time through the loop. */
4520 if (success)
4521 template_keyword_p = cp_parser_optional_template_keyword (parser);
4522 else
4523 template_keyword_p = false;
4525 /* Save the old scope since the name lookup we are about to do
4526 might destroy it. */
4527 old_scope = parser->scope;
4528 saved_qualifying_scope = parser->qualifying_scope;
4529 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4530 look up names in "X<T>::I" in order to determine that "Y" is
4531 a template. So, if we have a typename at this point, we make
4532 an effort to look through it. */
4533 if (is_declaration
4534 && !typename_keyword_p
4535 && parser->scope
4536 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4537 parser->scope = resolve_typename_type (parser->scope,
4538 /*only_current_p=*/false);
4539 /* Parse the qualifying entity. */
4540 new_scope
4541 = cp_parser_qualifying_entity (parser,
4542 typename_keyword_p,
4543 template_keyword_p,
4544 check_dependency_p,
4545 type_p,
4546 is_declaration);
4547 /* Look for the `::' token. */
4548 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4550 /* If we found what we wanted, we keep going; otherwise, we're
4551 done. */
4552 if (!cp_parser_parse_definitely (parser))
4554 bool error_p = false;
4556 /* Restore the OLD_SCOPE since it was valid before the
4557 failed attempt at finding the last
4558 class-or-namespace-name. */
4559 parser->scope = old_scope;
4560 parser->qualifying_scope = saved_qualifying_scope;
4561 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4562 break;
4563 /* If the next token is an identifier, and the one after
4564 that is a `::', then any valid interpretation would have
4565 found a class-or-namespace-name. */
4566 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4567 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4568 == CPP_SCOPE)
4569 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4570 != CPP_COMPL))
4572 token = cp_lexer_consume_token (parser->lexer);
4573 if (!error_p)
4575 if (!token->ambiguous_p)
4577 tree decl;
4578 tree ambiguous_decls;
4580 decl = cp_parser_lookup_name (parser, token->u.value,
4581 none_type,
4582 /*is_template=*/false,
4583 /*is_namespace=*/false,
4584 /*check_dependency=*/true,
4585 &ambiguous_decls,
4586 token->location);
4587 if (TREE_CODE (decl) == TEMPLATE_DECL)
4588 error_at (token->location,
4589 "%qD used without template parameters",
4590 decl);
4591 else if (ambiguous_decls)
4593 error_at (token->location,
4594 "reference to %qD is ambiguous",
4595 token->u.value);
4596 print_candidates (ambiguous_decls);
4597 decl = error_mark_node;
4599 else
4601 if (cxx_dialect != cxx98)
4602 cp_parser_name_lookup_error
4603 (parser, token->u.value, decl, NLE_NOT_CXX98,
4604 token->location);
4605 else
4606 cp_parser_name_lookup_error
4607 (parser, token->u.value, decl, NLE_CXX98,
4608 token->location);
4611 parser->scope = error_mark_node;
4612 error_p = true;
4613 /* Treat this as a successful nested-name-specifier
4614 due to:
4616 [basic.lookup.qual]
4618 If the name found is not a class-name (clause
4619 _class_) or namespace-name (_namespace.def_), the
4620 program is ill-formed. */
4621 success = true;
4623 cp_lexer_consume_token (parser->lexer);
4625 break;
4627 /* We've found one valid nested-name-specifier. */
4628 success = true;
4629 /* Name lookup always gives us a DECL. */
4630 if (TREE_CODE (new_scope) == TYPE_DECL)
4631 new_scope = TREE_TYPE (new_scope);
4632 /* Uses of "template" must be followed by actual templates. */
4633 if (template_keyword_p
4634 && !(CLASS_TYPE_P (new_scope)
4635 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4636 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4637 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4638 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4639 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4640 == TEMPLATE_ID_EXPR)))
4641 permerror (input_location, TYPE_P (new_scope)
4642 ? "%qT is not a template"
4643 : "%qD is not a template",
4644 new_scope);
4645 /* If it is a class scope, try to complete it; we are about to
4646 be looking up names inside the class. */
4647 if (TYPE_P (new_scope)
4648 /* Since checking types for dependency can be expensive,
4649 avoid doing it if the type is already complete. */
4650 && !COMPLETE_TYPE_P (new_scope)
4651 /* Do not try to complete dependent types. */
4652 && !dependent_type_p (new_scope))
4654 new_scope = complete_type (new_scope);
4655 /* If it is a typedef to current class, use the current
4656 class instead, as the typedef won't have any names inside
4657 it yet. */
4658 if (!COMPLETE_TYPE_P (new_scope)
4659 && currently_open_class (new_scope))
4660 new_scope = TYPE_MAIN_VARIANT (new_scope);
4662 /* Make sure we look in the right scope the next time through
4663 the loop. */
4664 parser->scope = new_scope;
4667 /* If parsing tentatively, replace the sequence of tokens that makes
4668 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4669 token. That way, should we re-parse the token stream, we will
4670 not have to repeat the effort required to do the parse, nor will
4671 we issue duplicate error messages. */
4672 if (success && start)
4674 cp_token *token;
4676 token = cp_lexer_token_at (parser->lexer, start);
4677 /* Reset the contents of the START token. */
4678 token->type = CPP_NESTED_NAME_SPECIFIER;
4679 /* Retrieve any deferred checks. Do not pop this access checks yet
4680 so the memory will not be reclaimed during token replacing below. */
4681 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4682 token->u.tree_check_value->value = parser->scope;
4683 token->u.tree_check_value->checks = get_deferred_access_checks ();
4684 token->u.tree_check_value->qualifying_scope =
4685 parser->qualifying_scope;
4686 token->keyword = RID_MAX;
4688 /* Purge all subsequent tokens. */
4689 cp_lexer_purge_tokens_after (parser->lexer, start);
4692 if (start)
4693 pop_to_parent_deferring_access_checks ();
4695 return success ? parser->scope : NULL_TREE;
4698 /* Parse a nested-name-specifier. See
4699 cp_parser_nested_name_specifier_opt for details. This function
4700 behaves identically, except that it will an issue an error if no
4701 nested-name-specifier is present. */
4703 static tree
4704 cp_parser_nested_name_specifier (cp_parser *parser,
4705 bool typename_keyword_p,
4706 bool check_dependency_p,
4707 bool type_p,
4708 bool is_declaration)
4710 tree scope;
4712 /* Look for the nested-name-specifier. */
4713 scope = cp_parser_nested_name_specifier_opt (parser,
4714 typename_keyword_p,
4715 check_dependency_p,
4716 type_p,
4717 is_declaration);
4718 /* If it was not present, issue an error message. */
4719 if (!scope)
4721 cp_parser_error (parser, "expected nested-name-specifier");
4722 parser->scope = NULL_TREE;
4725 return scope;
4728 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4729 this is either a class-name or a namespace-name (which corresponds
4730 to the class-or-namespace-name production in the grammar). For
4731 C++0x, it can also be a type-name that refers to an enumeration
4732 type.
4734 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4735 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4736 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4737 TYPE_P is TRUE iff the next name should be taken as a class-name,
4738 even the same name is declared to be another entity in the same
4739 scope.
4741 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4742 specified by the class-or-namespace-name. If neither is found the
4743 ERROR_MARK_NODE is returned. */
4745 static tree
4746 cp_parser_qualifying_entity (cp_parser *parser,
4747 bool typename_keyword_p,
4748 bool template_keyword_p,
4749 bool check_dependency_p,
4750 bool type_p,
4751 bool is_declaration)
4753 tree saved_scope;
4754 tree saved_qualifying_scope;
4755 tree saved_object_scope;
4756 tree scope;
4757 bool only_class_p;
4758 bool successful_parse_p;
4760 /* Before we try to parse the class-name, we must save away the
4761 current PARSER->SCOPE since cp_parser_class_name will destroy
4762 it. */
4763 saved_scope = parser->scope;
4764 saved_qualifying_scope = parser->qualifying_scope;
4765 saved_object_scope = parser->object_scope;
4766 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4767 there is no need to look for a namespace-name. */
4768 only_class_p = template_keyword_p
4769 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4770 if (!only_class_p)
4771 cp_parser_parse_tentatively (parser);
4772 scope = cp_parser_class_name (parser,
4773 typename_keyword_p,
4774 template_keyword_p,
4775 type_p ? class_type : none_type,
4776 check_dependency_p,
4777 /*class_head_p=*/false,
4778 is_declaration);
4779 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4780 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4781 if (!only_class_p
4782 && cxx_dialect != cxx98
4783 && !successful_parse_p)
4785 /* Restore the saved scope. */
4786 parser->scope = saved_scope;
4787 parser->qualifying_scope = saved_qualifying_scope;
4788 parser->object_scope = saved_object_scope;
4790 /* Parse tentatively. */
4791 cp_parser_parse_tentatively (parser);
4793 /* Parse a typedef-name or enum-name. */
4794 scope = cp_parser_nonclass_name (parser);
4796 /* "If the name found does not designate a namespace or a class,
4797 enumeration, or dependent type, the program is ill-formed."
4799 We cover classes and dependent types above and namespaces below,
4800 so this code is only looking for enums. */
4801 if (!scope || TREE_CODE (scope) != TYPE_DECL
4802 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4803 cp_parser_simulate_error (parser);
4805 successful_parse_p = cp_parser_parse_definitely (parser);
4807 /* If that didn't work, try for a namespace-name. */
4808 if (!only_class_p && !successful_parse_p)
4810 /* Restore the saved scope. */
4811 parser->scope = saved_scope;
4812 parser->qualifying_scope = saved_qualifying_scope;
4813 parser->object_scope = saved_object_scope;
4814 /* If we are not looking at an identifier followed by the scope
4815 resolution operator, then this is not part of a
4816 nested-name-specifier. (Note that this function is only used
4817 to parse the components of a nested-name-specifier.) */
4818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4819 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4820 return error_mark_node;
4821 scope = cp_parser_namespace_name (parser);
4824 return scope;
4827 /* Parse a postfix-expression.
4829 postfix-expression:
4830 primary-expression
4831 postfix-expression [ expression ]
4832 postfix-expression ( expression-list [opt] )
4833 simple-type-specifier ( expression-list [opt] )
4834 typename :: [opt] nested-name-specifier identifier
4835 ( expression-list [opt] )
4836 typename :: [opt] nested-name-specifier template [opt] template-id
4837 ( expression-list [opt] )
4838 postfix-expression . template [opt] id-expression
4839 postfix-expression -> template [opt] id-expression
4840 postfix-expression . pseudo-destructor-name
4841 postfix-expression -> pseudo-destructor-name
4842 postfix-expression ++
4843 postfix-expression --
4844 dynamic_cast < type-id > ( expression )
4845 static_cast < type-id > ( expression )
4846 reinterpret_cast < type-id > ( expression )
4847 const_cast < type-id > ( expression )
4848 typeid ( expression )
4849 typeid ( type-id )
4851 GNU Extension:
4853 postfix-expression:
4854 ( type-id ) { initializer-list , [opt] }
4856 This extension is a GNU version of the C99 compound-literal
4857 construct. (The C99 grammar uses `type-name' instead of `type-id',
4858 but they are essentially the same concept.)
4860 If ADDRESS_P is true, the postfix expression is the operand of the
4861 `&' operator. CAST_P is true if this expression is the target of a
4862 cast.
4864 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4865 class member access expressions [expr.ref].
4867 Returns a representation of the expression. */
4869 static tree
4870 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4871 bool member_access_only_p,
4872 cp_id_kind * pidk_return)
4874 cp_token *token;
4875 enum rid keyword;
4876 cp_id_kind idk = CP_ID_KIND_NONE;
4877 tree postfix_expression = NULL_TREE;
4878 bool is_member_access = false;
4880 /* Peek at the next token. */
4881 token = cp_lexer_peek_token (parser->lexer);
4882 /* Some of the productions are determined by keywords. */
4883 keyword = token->keyword;
4884 switch (keyword)
4886 case RID_DYNCAST:
4887 case RID_STATCAST:
4888 case RID_REINTCAST:
4889 case RID_CONSTCAST:
4891 tree type;
4892 tree expression;
4893 const char *saved_message;
4895 /* All of these can be handled in the same way from the point
4896 of view of parsing. Begin by consuming the token
4897 identifying the cast. */
4898 cp_lexer_consume_token (parser->lexer);
4900 /* New types cannot be defined in the cast. */
4901 saved_message = parser->type_definition_forbidden_message;
4902 parser->type_definition_forbidden_message
4903 = G_("types may not be defined in casts");
4905 /* Look for the opening `<'. */
4906 cp_parser_require (parser, CPP_LESS, RT_LESS);
4907 /* Parse the type to which we are casting. */
4908 type = cp_parser_type_id (parser);
4909 /* Look for the closing `>'. */
4910 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4911 /* Restore the old message. */
4912 parser->type_definition_forbidden_message = saved_message;
4914 /* And the expression which is being cast. */
4915 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4916 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4919 /* Only type conversions to integral or enumeration types
4920 can be used in constant-expressions. */
4921 if (!cast_valid_in_integral_constant_expression_p (type)
4922 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4923 return error_mark_node;
4925 switch (keyword)
4927 case RID_DYNCAST:
4928 postfix_expression
4929 = build_dynamic_cast (type, expression, tf_warning_or_error);
4930 break;
4931 case RID_STATCAST:
4932 postfix_expression
4933 = build_static_cast (type, expression, tf_warning_or_error);
4934 break;
4935 case RID_REINTCAST:
4936 postfix_expression
4937 = build_reinterpret_cast (type, expression,
4938 tf_warning_or_error);
4939 break;
4940 case RID_CONSTCAST:
4941 postfix_expression
4942 = build_const_cast (type, expression, tf_warning_or_error);
4943 break;
4944 default:
4945 gcc_unreachable ();
4948 break;
4950 case RID_TYPEID:
4952 tree type;
4953 const char *saved_message;
4954 bool saved_in_type_id_in_expr_p;
4956 /* Consume the `typeid' token. */
4957 cp_lexer_consume_token (parser->lexer);
4958 /* Look for the `(' token. */
4959 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4960 /* Types cannot be defined in a `typeid' expression. */
4961 saved_message = parser->type_definition_forbidden_message;
4962 parser->type_definition_forbidden_message
4963 = G_("types may not be defined in a %<typeid%> expression");
4964 /* We can't be sure yet whether we're looking at a type-id or an
4965 expression. */
4966 cp_parser_parse_tentatively (parser);
4967 /* Try a type-id first. */
4968 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4969 parser->in_type_id_in_expr_p = true;
4970 type = cp_parser_type_id (parser);
4971 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4972 /* Look for the `)' token. Otherwise, we can't be sure that
4973 we're not looking at an expression: consider `typeid (int
4974 (3))', for example. */
4975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4976 /* If all went well, simply lookup the type-id. */
4977 if (cp_parser_parse_definitely (parser))
4978 postfix_expression = get_typeid (type);
4979 /* Otherwise, fall back to the expression variant. */
4980 else
4982 tree expression;
4984 /* Look for an expression. */
4985 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4986 /* Compute its typeid. */
4987 postfix_expression = build_typeid (expression);
4988 /* Look for the `)' token. */
4989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4991 /* Restore the saved message. */
4992 parser->type_definition_forbidden_message = saved_message;
4993 /* `typeid' may not appear in an integral constant expression. */
4994 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4995 return error_mark_node;
4997 break;
4999 case RID_TYPENAME:
5001 tree type;
5002 /* The syntax permitted here is the same permitted for an
5003 elaborated-type-specifier. */
5004 type = cp_parser_elaborated_type_specifier (parser,
5005 /*is_friend=*/false,
5006 /*is_declaration=*/false);
5007 postfix_expression = cp_parser_functional_cast (parser, type);
5009 break;
5011 default:
5013 tree type;
5015 /* If the next thing is a simple-type-specifier, we may be
5016 looking at a functional cast. We could also be looking at
5017 an id-expression. So, we try the functional cast, and if
5018 that doesn't work we fall back to the primary-expression. */
5019 cp_parser_parse_tentatively (parser);
5020 /* Look for the simple-type-specifier. */
5021 type = cp_parser_simple_type_specifier (parser,
5022 /*decl_specs=*/NULL,
5023 CP_PARSER_FLAGS_NONE);
5024 /* Parse the cast itself. */
5025 if (!cp_parser_error_occurred (parser))
5026 postfix_expression
5027 = cp_parser_functional_cast (parser, type);
5028 /* If that worked, we're done. */
5029 if (cp_parser_parse_definitely (parser))
5030 break;
5032 /* If the functional-cast didn't work out, try a
5033 compound-literal. */
5034 if (cp_parser_allow_gnu_extensions_p (parser)
5035 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5037 VEC(constructor_elt,gc) *initializer_list = NULL;
5038 bool saved_in_type_id_in_expr_p;
5040 cp_parser_parse_tentatively (parser);
5041 /* Consume the `('. */
5042 cp_lexer_consume_token (parser->lexer);
5043 /* Parse the type. */
5044 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5045 parser->in_type_id_in_expr_p = true;
5046 type = cp_parser_type_id (parser);
5047 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5048 /* Look for the `)'. */
5049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5050 /* Look for the `{'. */
5051 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5052 /* If things aren't going well, there's no need to
5053 keep going. */
5054 if (!cp_parser_error_occurred (parser))
5056 bool non_constant_p;
5057 /* Parse the initializer-list. */
5058 initializer_list
5059 = cp_parser_initializer_list (parser, &non_constant_p);
5060 /* Allow a trailing `,'. */
5061 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5062 cp_lexer_consume_token (parser->lexer);
5063 /* Look for the final `}'. */
5064 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5066 /* If that worked, we're definitely looking at a
5067 compound-literal expression. */
5068 if (cp_parser_parse_definitely (parser))
5070 /* Warn the user that a compound literal is not
5071 allowed in standard C++. */
5072 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5073 /* For simplicity, we disallow compound literals in
5074 constant-expressions. We could
5075 allow compound literals of integer type, whose
5076 initializer was a constant, in constant
5077 expressions. Permitting that usage, as a further
5078 extension, would not change the meaning of any
5079 currently accepted programs. (Of course, as
5080 compound literals are not part of ISO C++, the
5081 standard has nothing to say.) */
5082 if (cp_parser_non_integral_constant_expression (parser,
5083 NIC_NCC))
5085 postfix_expression = error_mark_node;
5086 break;
5088 /* Form the representation of the compound-literal. */
5089 postfix_expression
5090 = (finish_compound_literal
5091 (type, build_constructor (init_list_type_node,
5092 initializer_list)));
5093 break;
5097 /* It must be a primary-expression. */
5098 postfix_expression
5099 = cp_parser_primary_expression (parser, address_p, cast_p,
5100 /*template_arg_p=*/false,
5101 &idk);
5103 break;
5106 /* Keep looping until the postfix-expression is complete. */
5107 while (true)
5109 if (idk == CP_ID_KIND_UNQUALIFIED
5110 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5111 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5112 /* It is not a Koenig lookup function call. */
5113 postfix_expression
5114 = unqualified_name_lookup_error (postfix_expression);
5116 /* Peek at the next token. */
5117 token = cp_lexer_peek_token (parser->lexer);
5119 switch (token->type)
5121 case CPP_OPEN_SQUARE:
5122 postfix_expression
5123 = cp_parser_postfix_open_square_expression (parser,
5124 postfix_expression,
5125 false);
5126 idk = CP_ID_KIND_NONE;
5127 is_member_access = false;
5128 break;
5130 case CPP_OPEN_PAREN:
5131 /* postfix-expression ( expression-list [opt] ) */
5133 bool koenig_p;
5134 bool is_builtin_constant_p;
5135 bool saved_integral_constant_expression_p = false;
5136 bool saved_non_integral_constant_expression_p = false;
5137 VEC(tree,gc) *args;
5139 is_member_access = false;
5141 is_builtin_constant_p
5142 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5143 if (is_builtin_constant_p)
5145 /* The whole point of __builtin_constant_p is to allow
5146 non-constant expressions to appear as arguments. */
5147 saved_integral_constant_expression_p
5148 = parser->integral_constant_expression_p;
5149 saved_non_integral_constant_expression_p
5150 = parser->non_integral_constant_expression_p;
5151 parser->integral_constant_expression_p = false;
5153 args = (cp_parser_parenthesized_expression_list
5154 (parser, non_attr,
5155 /*cast_p=*/false, /*allow_expansion_p=*/true,
5156 /*non_constant_p=*/NULL));
5157 if (is_builtin_constant_p)
5159 parser->integral_constant_expression_p
5160 = saved_integral_constant_expression_p;
5161 parser->non_integral_constant_expression_p
5162 = saved_non_integral_constant_expression_p;
5165 if (args == NULL)
5167 postfix_expression = error_mark_node;
5168 break;
5171 /* Function calls are not permitted in
5172 constant-expressions. */
5173 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5174 && cp_parser_non_integral_constant_expression (parser,
5175 NIC_FUNC_CALL))
5177 postfix_expression = error_mark_node;
5178 release_tree_vector (args);
5179 break;
5182 koenig_p = false;
5183 if (idk == CP_ID_KIND_UNQUALIFIED
5184 || idk == CP_ID_KIND_TEMPLATE_ID)
5186 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5188 if (!VEC_empty (tree, args))
5190 koenig_p = true;
5191 if (!any_type_dependent_arguments_p (args))
5192 postfix_expression
5193 = perform_koenig_lookup (postfix_expression, args,
5194 /*include_std=*/false);
5196 else
5197 postfix_expression
5198 = unqualified_fn_lookup_error (postfix_expression);
5200 /* We do not perform argument-dependent lookup if
5201 normal lookup finds a non-function, in accordance
5202 with the expected resolution of DR 218. */
5203 else if (!VEC_empty (tree, args)
5204 && is_overloaded_fn (postfix_expression))
5206 tree fn = get_first_fn (postfix_expression);
5207 fn = STRIP_TEMPLATE (fn);
5209 /* Do not do argument dependent lookup if regular
5210 lookup finds a member function or a block-scope
5211 function declaration. [basic.lookup.argdep]/3 */
5212 if (!DECL_FUNCTION_MEMBER_P (fn)
5213 && !DECL_LOCAL_FUNCTION_P (fn))
5215 koenig_p = true;
5216 if (!any_type_dependent_arguments_p (args))
5217 postfix_expression
5218 = perform_koenig_lookup (postfix_expression, args,
5219 /*include_std=*/false);
5224 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5226 tree instance = TREE_OPERAND (postfix_expression, 0);
5227 tree fn = TREE_OPERAND (postfix_expression, 1);
5229 if (processing_template_decl
5230 && (type_dependent_expression_p (instance)
5231 || (!BASELINK_P (fn)
5232 && TREE_CODE (fn) != FIELD_DECL)
5233 || type_dependent_expression_p (fn)
5234 || any_type_dependent_arguments_p (args)))
5236 postfix_expression
5237 = build_nt_call_vec (postfix_expression, args);
5238 release_tree_vector (args);
5239 break;
5242 if (BASELINK_P (fn))
5244 postfix_expression
5245 = (build_new_method_call
5246 (instance, fn, &args, NULL_TREE,
5247 (idk == CP_ID_KIND_QUALIFIED
5248 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5249 /*fn_p=*/NULL,
5250 tf_warning_or_error));
5252 else
5253 postfix_expression
5254 = finish_call_expr (postfix_expression, &args,
5255 /*disallow_virtual=*/false,
5256 /*koenig_p=*/false,
5257 tf_warning_or_error);
5259 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5260 || TREE_CODE (postfix_expression) == MEMBER_REF
5261 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5262 postfix_expression = (build_offset_ref_call_from_tree
5263 (postfix_expression, &args));
5264 else if (idk == CP_ID_KIND_QUALIFIED)
5265 /* A call to a static class member, or a namespace-scope
5266 function. */
5267 postfix_expression
5268 = finish_call_expr (postfix_expression, &args,
5269 /*disallow_virtual=*/true,
5270 koenig_p,
5271 tf_warning_or_error);
5272 else
5273 /* All other function calls. */
5274 postfix_expression
5275 = finish_call_expr (postfix_expression, &args,
5276 /*disallow_virtual=*/false,
5277 koenig_p,
5278 tf_warning_or_error);
5280 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5281 idk = CP_ID_KIND_NONE;
5283 release_tree_vector (args);
5285 break;
5287 case CPP_DOT:
5288 case CPP_DEREF:
5289 /* postfix-expression . template [opt] id-expression
5290 postfix-expression . pseudo-destructor-name
5291 postfix-expression -> template [opt] id-expression
5292 postfix-expression -> pseudo-destructor-name */
5294 /* Consume the `.' or `->' operator. */
5295 cp_lexer_consume_token (parser->lexer);
5297 postfix_expression
5298 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5299 postfix_expression,
5300 false, &idk,
5301 token->location);
5303 is_member_access = true;
5304 break;
5306 case CPP_PLUS_PLUS:
5307 /* postfix-expression ++ */
5308 /* Consume the `++' token. */
5309 cp_lexer_consume_token (parser->lexer);
5310 /* Generate a representation for the complete expression. */
5311 postfix_expression
5312 = finish_increment_expr (postfix_expression,
5313 POSTINCREMENT_EXPR);
5314 /* Increments may not appear in constant-expressions. */
5315 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5316 postfix_expression = error_mark_node;
5317 idk = CP_ID_KIND_NONE;
5318 is_member_access = false;
5319 break;
5321 case CPP_MINUS_MINUS:
5322 /* postfix-expression -- */
5323 /* Consume the `--' token. */
5324 cp_lexer_consume_token (parser->lexer);
5325 /* Generate a representation for the complete expression. */
5326 postfix_expression
5327 = finish_increment_expr (postfix_expression,
5328 POSTDECREMENT_EXPR);
5329 /* Decrements may not appear in constant-expressions. */
5330 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5331 postfix_expression = error_mark_node;
5332 idk = CP_ID_KIND_NONE;
5333 is_member_access = false;
5334 break;
5336 default:
5337 if (pidk_return != NULL)
5338 * pidk_return = idk;
5339 if (member_access_only_p)
5340 return is_member_access? postfix_expression : error_mark_node;
5341 else
5342 return postfix_expression;
5346 /* We should never get here. */
5347 gcc_unreachable ();
5348 return error_mark_node;
5351 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5352 by cp_parser_builtin_offsetof. We're looking for
5354 postfix-expression [ expression ]
5356 FOR_OFFSETOF is set if we're being called in that context, which
5357 changes how we deal with integer constant expressions. */
5359 static tree
5360 cp_parser_postfix_open_square_expression (cp_parser *parser,
5361 tree postfix_expression,
5362 bool for_offsetof)
5364 tree index;
5366 /* Consume the `[' token. */
5367 cp_lexer_consume_token (parser->lexer);
5369 /* Parse the index expression. */
5370 /* ??? For offsetof, there is a question of what to allow here. If
5371 offsetof is not being used in an integral constant expression context,
5372 then we *could* get the right answer by computing the value at runtime.
5373 If we are in an integral constant expression context, then we might
5374 could accept any constant expression; hard to say without analysis.
5375 Rather than open the barn door too wide right away, allow only integer
5376 constant expressions here. */
5377 if (for_offsetof)
5378 index = cp_parser_constant_expression (parser, false, NULL);
5379 else
5380 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5382 /* Look for the closing `]'. */
5383 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5385 /* Build the ARRAY_REF. */
5386 postfix_expression = grok_array_decl (postfix_expression, index);
5388 /* When not doing offsetof, array references are not permitted in
5389 constant-expressions. */
5390 if (!for_offsetof
5391 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5392 postfix_expression = error_mark_node;
5394 return postfix_expression;
5397 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5398 by cp_parser_builtin_offsetof. We're looking for
5400 postfix-expression . template [opt] id-expression
5401 postfix-expression . pseudo-destructor-name
5402 postfix-expression -> template [opt] id-expression
5403 postfix-expression -> pseudo-destructor-name
5405 FOR_OFFSETOF is set if we're being called in that context. That sorta
5406 limits what of the above we'll actually accept, but nevermind.
5407 TOKEN_TYPE is the "." or "->" token, which will already have been
5408 removed from the stream. */
5410 static tree
5411 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5412 enum cpp_ttype token_type,
5413 tree postfix_expression,
5414 bool for_offsetof, cp_id_kind *idk,
5415 location_t location)
5417 tree name;
5418 bool dependent_p;
5419 bool pseudo_destructor_p;
5420 tree scope = NULL_TREE;
5422 /* If this is a `->' operator, dereference the pointer. */
5423 if (token_type == CPP_DEREF)
5424 postfix_expression = build_x_arrow (postfix_expression);
5425 /* Check to see whether or not the expression is type-dependent. */
5426 dependent_p = type_dependent_expression_p (postfix_expression);
5427 /* The identifier following the `->' or `.' is not qualified. */
5428 parser->scope = NULL_TREE;
5429 parser->qualifying_scope = NULL_TREE;
5430 parser->object_scope = NULL_TREE;
5431 *idk = CP_ID_KIND_NONE;
5433 /* Enter the scope corresponding to the type of the object
5434 given by the POSTFIX_EXPRESSION. */
5435 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5437 scope = TREE_TYPE (postfix_expression);
5438 /* According to the standard, no expression should ever have
5439 reference type. Unfortunately, we do not currently match
5440 the standard in this respect in that our internal representation
5441 of an expression may have reference type even when the standard
5442 says it does not. Therefore, we have to manually obtain the
5443 underlying type here. */
5444 scope = non_reference (scope);
5445 /* The type of the POSTFIX_EXPRESSION must be complete. */
5446 if (scope == unknown_type_node)
5448 error_at (location, "%qE does not have class type",
5449 postfix_expression);
5450 scope = NULL_TREE;
5452 else
5453 scope = complete_type_or_else (scope, NULL_TREE);
5454 /* Let the name lookup machinery know that we are processing a
5455 class member access expression. */
5456 parser->context->object_type = scope;
5457 /* If something went wrong, we want to be able to discern that case,
5458 as opposed to the case where there was no SCOPE due to the type
5459 of expression being dependent. */
5460 if (!scope)
5461 scope = error_mark_node;
5462 /* If the SCOPE was erroneous, make the various semantic analysis
5463 functions exit quickly -- and without issuing additional error
5464 messages. */
5465 if (scope == error_mark_node)
5466 postfix_expression = error_mark_node;
5469 /* Assume this expression is not a pseudo-destructor access. */
5470 pseudo_destructor_p = false;
5472 /* If the SCOPE is a scalar type, then, if this is a valid program,
5473 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5474 is type dependent, it can be pseudo-destructor-name or something else.
5475 Try to parse it as pseudo-destructor-name first. */
5476 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5478 tree s;
5479 tree type;
5481 cp_parser_parse_tentatively (parser);
5482 /* Parse the pseudo-destructor-name. */
5483 s = NULL_TREE;
5484 cp_parser_pseudo_destructor_name (parser, &s, &type);
5485 if (dependent_p
5486 && (cp_parser_error_occurred (parser)
5487 || TREE_CODE (type) != TYPE_DECL
5488 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5489 cp_parser_abort_tentative_parse (parser);
5490 else if (cp_parser_parse_definitely (parser))
5492 pseudo_destructor_p = true;
5493 postfix_expression
5494 = finish_pseudo_destructor_expr (postfix_expression,
5495 s, TREE_TYPE (type));
5499 if (!pseudo_destructor_p)
5501 /* If the SCOPE is not a scalar type, we are looking at an
5502 ordinary class member access expression, rather than a
5503 pseudo-destructor-name. */
5504 bool template_p;
5505 cp_token *token = cp_lexer_peek_token (parser->lexer);
5506 /* Parse the id-expression. */
5507 name = (cp_parser_id_expression
5508 (parser,
5509 cp_parser_optional_template_keyword (parser),
5510 /*check_dependency_p=*/true,
5511 &template_p,
5512 /*declarator_p=*/false,
5513 /*optional_p=*/false));
5514 /* In general, build a SCOPE_REF if the member name is qualified.
5515 However, if the name was not dependent and has already been
5516 resolved; there is no need to build the SCOPE_REF. For example;
5518 struct X { void f(); };
5519 template <typename T> void f(T* t) { t->X::f(); }
5521 Even though "t" is dependent, "X::f" is not and has been resolved
5522 to a BASELINK; there is no need to include scope information. */
5524 /* But we do need to remember that there was an explicit scope for
5525 virtual function calls. */
5526 if (parser->scope)
5527 *idk = CP_ID_KIND_QUALIFIED;
5529 /* If the name is a template-id that names a type, we will get a
5530 TYPE_DECL here. That is invalid code. */
5531 if (TREE_CODE (name) == TYPE_DECL)
5533 error_at (token->location, "invalid use of %qD", name);
5534 postfix_expression = error_mark_node;
5536 else
5538 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5540 name = build_qualified_name (/*type=*/NULL_TREE,
5541 parser->scope,
5542 name,
5543 template_p);
5544 parser->scope = NULL_TREE;
5545 parser->qualifying_scope = NULL_TREE;
5546 parser->object_scope = NULL_TREE;
5548 if (scope && name && BASELINK_P (name))
5549 adjust_result_of_qualified_name_lookup
5550 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5551 postfix_expression
5552 = finish_class_member_access_expr (postfix_expression, name,
5553 template_p,
5554 tf_warning_or_error);
5558 /* We no longer need to look up names in the scope of the object on
5559 the left-hand side of the `.' or `->' operator. */
5560 parser->context->object_type = NULL_TREE;
5562 /* Outside of offsetof, these operators may not appear in
5563 constant-expressions. */
5564 if (!for_offsetof
5565 && (cp_parser_non_integral_constant_expression
5566 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5567 postfix_expression = error_mark_node;
5569 return postfix_expression;
5572 /* Parse a parenthesized expression-list.
5574 expression-list:
5575 assignment-expression
5576 expression-list, assignment-expression
5578 attribute-list:
5579 expression-list
5580 identifier
5581 identifier, expression-list
5583 CAST_P is true if this expression is the target of a cast.
5585 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5586 argument pack.
5588 Returns a vector of trees. Each element is a representation of an
5589 assignment-expression. NULL is returned if the ( and or ) are
5590 missing. An empty, but allocated, vector is returned on no
5591 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5592 if we are parsing an attribute list for an attribute that wants a
5593 plain identifier argument, normal_attr for an attribute that wants
5594 an expression, or non_attr if we aren't parsing an attribute list. If
5595 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5596 not all of the expressions in the list were constant. */
5598 static VEC(tree,gc) *
5599 cp_parser_parenthesized_expression_list (cp_parser* parser,
5600 int is_attribute_list,
5601 bool cast_p,
5602 bool allow_expansion_p,
5603 bool *non_constant_p)
5605 VEC(tree,gc) *expression_list;
5606 bool fold_expr_p = is_attribute_list != non_attr;
5607 tree identifier = NULL_TREE;
5608 bool saved_greater_than_is_operator_p;
5610 /* Assume all the expressions will be constant. */
5611 if (non_constant_p)
5612 *non_constant_p = false;
5614 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5615 return NULL;
5617 expression_list = make_tree_vector ();
5619 /* Within a parenthesized expression, a `>' token is always
5620 the greater-than operator. */
5621 saved_greater_than_is_operator_p
5622 = parser->greater_than_is_operator_p;
5623 parser->greater_than_is_operator_p = true;
5625 /* Consume expressions until there are no more. */
5626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5627 while (true)
5629 tree expr;
5631 /* At the beginning of attribute lists, check to see if the
5632 next token is an identifier. */
5633 if (is_attribute_list == id_attr
5634 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5636 cp_token *token;
5638 /* Consume the identifier. */
5639 token = cp_lexer_consume_token (parser->lexer);
5640 /* Save the identifier. */
5641 identifier = token->u.value;
5643 else
5645 bool expr_non_constant_p;
5647 /* Parse the next assignment-expression. */
5648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5650 /* A braced-init-list. */
5651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5652 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5653 if (non_constant_p && expr_non_constant_p)
5654 *non_constant_p = true;
5656 else if (non_constant_p)
5658 expr = (cp_parser_constant_expression
5659 (parser, /*allow_non_constant_p=*/true,
5660 &expr_non_constant_p));
5661 if (expr_non_constant_p)
5662 *non_constant_p = true;
5664 else
5665 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5667 if (fold_expr_p)
5668 expr = fold_non_dependent_expr (expr);
5670 /* If we have an ellipsis, then this is an expression
5671 expansion. */
5672 if (allow_expansion_p
5673 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5675 /* Consume the `...'. */
5676 cp_lexer_consume_token (parser->lexer);
5678 /* Build the argument pack. */
5679 expr = make_pack_expansion (expr);
5682 /* Add it to the list. We add error_mark_node
5683 expressions to the list, so that we can still tell if
5684 the correct form for a parenthesized expression-list
5685 is found. That gives better errors. */
5686 VEC_safe_push (tree, gc, expression_list, expr);
5688 if (expr == error_mark_node)
5689 goto skip_comma;
5692 /* After the first item, attribute lists look the same as
5693 expression lists. */
5694 is_attribute_list = non_attr;
5696 get_comma:;
5697 /* If the next token isn't a `,', then we are done. */
5698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5699 break;
5701 /* Otherwise, consume the `,' and keep going. */
5702 cp_lexer_consume_token (parser->lexer);
5705 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5707 int ending;
5709 skip_comma:;
5710 /* We try and resync to an unnested comma, as that will give the
5711 user better diagnostics. */
5712 ending = cp_parser_skip_to_closing_parenthesis (parser,
5713 /*recovering=*/true,
5714 /*or_comma=*/true,
5715 /*consume_paren=*/true);
5716 if (ending < 0)
5717 goto get_comma;
5718 if (!ending)
5720 parser->greater_than_is_operator_p
5721 = saved_greater_than_is_operator_p;
5722 return NULL;
5726 parser->greater_than_is_operator_p
5727 = saved_greater_than_is_operator_p;
5729 if (identifier)
5730 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5732 return expression_list;
5735 /* Parse a pseudo-destructor-name.
5737 pseudo-destructor-name:
5738 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5739 :: [opt] nested-name-specifier template template-id :: ~ type-name
5740 :: [opt] nested-name-specifier [opt] ~ type-name
5742 If either of the first two productions is used, sets *SCOPE to the
5743 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5744 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5745 or ERROR_MARK_NODE if the parse fails. */
5747 static void
5748 cp_parser_pseudo_destructor_name (cp_parser* parser,
5749 tree* scope,
5750 tree* type)
5752 bool nested_name_specifier_p;
5754 /* Assume that things will not work out. */
5755 *type = error_mark_node;
5757 /* Look for the optional `::' operator. */
5758 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5759 /* Look for the optional nested-name-specifier. */
5760 nested_name_specifier_p
5761 = (cp_parser_nested_name_specifier_opt (parser,
5762 /*typename_keyword_p=*/false,
5763 /*check_dependency_p=*/true,
5764 /*type_p=*/false,
5765 /*is_declaration=*/false)
5766 != NULL_TREE);
5767 /* Now, if we saw a nested-name-specifier, we might be doing the
5768 second production. */
5769 if (nested_name_specifier_p
5770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5772 /* Consume the `template' keyword. */
5773 cp_lexer_consume_token (parser->lexer);
5774 /* Parse the template-id. */
5775 cp_parser_template_id (parser,
5776 /*template_keyword_p=*/true,
5777 /*check_dependency_p=*/false,
5778 /*is_declaration=*/true);
5779 /* Look for the `::' token. */
5780 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5782 /* If the next token is not a `~', then there might be some
5783 additional qualification. */
5784 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5786 /* At this point, we're looking for "type-name :: ~". The type-name
5787 must not be a class-name, since this is a pseudo-destructor. So,
5788 it must be either an enum-name, or a typedef-name -- both of which
5789 are just identifiers. So, we peek ahead to check that the "::"
5790 and "~" tokens are present; if they are not, then we can avoid
5791 calling type_name. */
5792 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5793 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5794 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5796 cp_parser_error (parser, "non-scalar type");
5797 return;
5800 /* Look for the type-name. */
5801 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5802 if (*scope == error_mark_node)
5803 return;
5805 /* Look for the `::' token. */
5806 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5808 else
5809 *scope = NULL_TREE;
5811 /* Look for the `~'. */
5812 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5813 /* Look for the type-name again. We are not responsible for
5814 checking that it matches the first type-name. */
5815 *type = cp_parser_nonclass_name (parser);
5818 /* Parse a unary-expression.
5820 unary-expression:
5821 postfix-expression
5822 ++ cast-expression
5823 -- cast-expression
5824 unary-operator cast-expression
5825 sizeof unary-expression
5826 sizeof ( type-id )
5827 alignof ( type-id ) [C++0x]
5828 new-expression
5829 delete-expression
5831 GNU Extensions:
5833 unary-expression:
5834 __extension__ cast-expression
5835 __alignof__ unary-expression
5836 __alignof__ ( type-id )
5837 alignof unary-expression [C++0x]
5838 __real__ cast-expression
5839 __imag__ cast-expression
5840 && identifier
5842 ADDRESS_P is true iff the unary-expression is appearing as the
5843 operand of the `&' operator. CAST_P is true if this expression is
5844 the target of a cast.
5846 Returns a representation of the expression. */
5848 static tree
5849 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5850 cp_id_kind * pidk)
5852 cp_token *token;
5853 enum tree_code unary_operator;
5855 /* Peek at the next token. */
5856 token = cp_lexer_peek_token (parser->lexer);
5857 /* Some keywords give away the kind of expression. */
5858 if (token->type == CPP_KEYWORD)
5860 enum rid keyword = token->keyword;
5862 switch (keyword)
5864 case RID_ALIGNOF:
5865 case RID_SIZEOF:
5867 tree operand;
5868 enum tree_code op;
5870 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5871 /* Consume the token. */
5872 cp_lexer_consume_token (parser->lexer);
5873 /* Parse the operand. */
5874 operand = cp_parser_sizeof_operand (parser, keyword);
5876 if (TYPE_P (operand))
5877 return cxx_sizeof_or_alignof_type (operand, op, true);
5878 else
5880 /* ISO C++ defines alignof only with types, not with
5881 expressions. So pedwarn if alignof is used with a non-
5882 type expression. However, __alignof__ is ok. */
5883 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5884 pedwarn (token->location, OPT_pedantic,
5885 "ISO C++ does not allow %<alignof%> "
5886 "with a non-type");
5888 return cxx_sizeof_or_alignof_expr (operand, op, true);
5892 case RID_NEW:
5893 return cp_parser_new_expression (parser);
5895 case RID_DELETE:
5896 return cp_parser_delete_expression (parser);
5898 case RID_EXTENSION:
5900 /* The saved value of the PEDANTIC flag. */
5901 int saved_pedantic;
5902 tree expr;
5904 /* Save away the PEDANTIC flag. */
5905 cp_parser_extension_opt (parser, &saved_pedantic);
5906 /* Parse the cast-expression. */
5907 expr = cp_parser_simple_cast_expression (parser);
5908 /* Restore the PEDANTIC flag. */
5909 pedantic = saved_pedantic;
5911 return expr;
5914 case RID_REALPART:
5915 case RID_IMAGPART:
5917 tree expression;
5919 /* Consume the `__real__' or `__imag__' token. */
5920 cp_lexer_consume_token (parser->lexer);
5921 /* Parse the cast-expression. */
5922 expression = cp_parser_simple_cast_expression (parser);
5923 /* Create the complete representation. */
5924 return build_x_unary_op ((keyword == RID_REALPART
5925 ? REALPART_EXPR : IMAGPART_EXPR),
5926 expression,
5927 tf_warning_or_error);
5929 break;
5931 case RID_NOEXCEPT:
5933 tree expr;
5934 const char *saved_message;
5935 bool saved_integral_constant_expression_p;
5936 bool saved_non_integral_constant_expression_p;
5937 bool saved_greater_than_is_operator_p;
5939 cp_lexer_consume_token (parser->lexer);
5940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5942 saved_message = parser->type_definition_forbidden_message;
5943 parser->type_definition_forbidden_message
5944 = G_("types may not be defined in %<noexcept%> expressions");
5946 saved_integral_constant_expression_p
5947 = parser->integral_constant_expression_p;
5948 saved_non_integral_constant_expression_p
5949 = parser->non_integral_constant_expression_p;
5950 parser->integral_constant_expression_p = false;
5952 saved_greater_than_is_operator_p
5953 = parser->greater_than_is_operator_p;
5954 parser->greater_than_is_operator_p = true;
5956 ++cp_unevaluated_operand;
5957 ++c_inhibit_evaluation_warnings;
5958 expr = cp_parser_expression (parser, false, NULL);
5959 --c_inhibit_evaluation_warnings;
5960 --cp_unevaluated_operand;
5962 parser->greater_than_is_operator_p
5963 = saved_greater_than_is_operator_p;
5965 parser->integral_constant_expression_p
5966 = saved_integral_constant_expression_p;
5967 parser->non_integral_constant_expression_p
5968 = saved_non_integral_constant_expression_p;
5970 parser->type_definition_forbidden_message = saved_message;
5972 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5973 return finish_noexcept_expr (expr, tf_warning_or_error);
5976 default:
5977 break;
5981 /* Look for the `:: new' and `:: delete', which also signal the
5982 beginning of a new-expression, or delete-expression,
5983 respectively. If the next token is `::', then it might be one of
5984 these. */
5985 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5987 enum rid keyword;
5989 /* See if the token after the `::' is one of the keywords in
5990 which we're interested. */
5991 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5992 /* If it's `new', we have a new-expression. */
5993 if (keyword == RID_NEW)
5994 return cp_parser_new_expression (parser);
5995 /* Similarly, for `delete'. */
5996 else if (keyword == RID_DELETE)
5997 return cp_parser_delete_expression (parser);
6000 /* Look for a unary operator. */
6001 unary_operator = cp_parser_unary_operator (token);
6002 /* The `++' and `--' operators can be handled similarly, even though
6003 they are not technically unary-operators in the grammar. */
6004 if (unary_operator == ERROR_MARK)
6006 if (token->type == CPP_PLUS_PLUS)
6007 unary_operator = PREINCREMENT_EXPR;
6008 else if (token->type == CPP_MINUS_MINUS)
6009 unary_operator = PREDECREMENT_EXPR;
6010 /* Handle the GNU address-of-label extension. */
6011 else if (cp_parser_allow_gnu_extensions_p (parser)
6012 && token->type == CPP_AND_AND)
6014 tree identifier;
6015 tree expression;
6016 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6018 /* Consume the '&&' token. */
6019 cp_lexer_consume_token (parser->lexer);
6020 /* Look for the identifier. */
6021 identifier = cp_parser_identifier (parser);
6022 /* Create an expression representing the address. */
6023 expression = finish_label_address_expr (identifier, loc);
6024 if (cp_parser_non_integral_constant_expression (parser,
6025 NIC_ADDR_LABEL))
6026 expression = error_mark_node;
6027 return expression;
6030 if (unary_operator != ERROR_MARK)
6032 tree cast_expression;
6033 tree expression = error_mark_node;
6034 non_integral_constant non_constant_p = NIC_NONE;
6036 /* Consume the operator token. */
6037 token = cp_lexer_consume_token (parser->lexer);
6038 /* Parse the cast-expression. */
6039 cast_expression
6040 = cp_parser_cast_expression (parser,
6041 unary_operator == ADDR_EXPR,
6042 /*cast_p=*/false, pidk);
6043 /* Now, build an appropriate representation. */
6044 switch (unary_operator)
6046 case INDIRECT_REF:
6047 non_constant_p = NIC_STAR;
6048 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6049 tf_warning_or_error);
6050 break;
6052 case ADDR_EXPR:
6053 non_constant_p = NIC_ADDR;
6054 /* Fall through. */
6055 case BIT_NOT_EXPR:
6056 expression = build_x_unary_op (unary_operator, cast_expression,
6057 tf_warning_or_error);
6058 break;
6060 case PREINCREMENT_EXPR:
6061 case PREDECREMENT_EXPR:
6062 non_constant_p = unary_operator == PREINCREMENT_EXPR
6063 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6064 /* Fall through. */
6065 case UNARY_PLUS_EXPR:
6066 case NEGATE_EXPR:
6067 case TRUTH_NOT_EXPR:
6068 expression = finish_unary_op_expr (unary_operator, cast_expression);
6069 break;
6071 default:
6072 gcc_unreachable ();
6075 if (non_constant_p != NIC_NONE
6076 && cp_parser_non_integral_constant_expression (parser,
6077 non_constant_p))
6078 expression = error_mark_node;
6080 return expression;
6083 return cp_parser_postfix_expression (parser, address_p, cast_p,
6084 /*member_access_only_p=*/false,
6085 pidk);
6088 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6089 unary-operator, the corresponding tree code is returned. */
6091 static enum tree_code
6092 cp_parser_unary_operator (cp_token* token)
6094 switch (token->type)
6096 case CPP_MULT:
6097 return INDIRECT_REF;
6099 case CPP_AND:
6100 return ADDR_EXPR;
6102 case CPP_PLUS:
6103 return UNARY_PLUS_EXPR;
6105 case CPP_MINUS:
6106 return NEGATE_EXPR;
6108 case CPP_NOT:
6109 return TRUTH_NOT_EXPR;
6111 case CPP_COMPL:
6112 return BIT_NOT_EXPR;
6114 default:
6115 return ERROR_MARK;
6119 /* Parse a new-expression.
6121 new-expression:
6122 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6123 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6125 Returns a representation of the expression. */
6127 static tree
6128 cp_parser_new_expression (cp_parser* parser)
6130 bool global_scope_p;
6131 VEC(tree,gc) *placement;
6132 tree type;
6133 VEC(tree,gc) *initializer;
6134 tree nelts;
6135 tree ret;
6137 /* Look for the optional `::' operator. */
6138 global_scope_p
6139 = (cp_parser_global_scope_opt (parser,
6140 /*current_scope_valid_p=*/false)
6141 != NULL_TREE);
6142 /* Look for the `new' operator. */
6143 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6144 /* There's no easy way to tell a new-placement from the
6145 `( type-id )' construct. */
6146 cp_parser_parse_tentatively (parser);
6147 /* Look for a new-placement. */
6148 placement = cp_parser_new_placement (parser);
6149 /* If that didn't work out, there's no new-placement. */
6150 if (!cp_parser_parse_definitely (parser))
6152 if (placement != NULL)
6153 release_tree_vector (placement);
6154 placement = NULL;
6157 /* If the next token is a `(', then we have a parenthesized
6158 type-id. */
6159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6161 cp_token *token;
6162 /* Consume the `('. */
6163 cp_lexer_consume_token (parser->lexer);
6164 /* Parse the type-id. */
6165 type = cp_parser_type_id (parser);
6166 /* Look for the closing `)'. */
6167 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6168 token = cp_lexer_peek_token (parser->lexer);
6169 /* There should not be a direct-new-declarator in this production,
6170 but GCC used to allowed this, so we check and emit a sensible error
6171 message for this case. */
6172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6174 error_at (token->location,
6175 "array bound forbidden after parenthesized type-id");
6176 inform (token->location,
6177 "try removing the parentheses around the type-id");
6178 cp_parser_direct_new_declarator (parser);
6180 nelts = NULL_TREE;
6182 /* Otherwise, there must be a new-type-id. */
6183 else
6184 type = cp_parser_new_type_id (parser, &nelts);
6186 /* If the next token is a `(' or '{', then we have a new-initializer. */
6187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6188 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6189 initializer = cp_parser_new_initializer (parser);
6190 else
6191 initializer = NULL;
6193 /* A new-expression may not appear in an integral constant
6194 expression. */
6195 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6196 ret = error_mark_node;
6197 else
6199 /* Create a representation of the new-expression. */
6200 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6201 tf_warning_or_error);
6204 if (placement != NULL)
6205 release_tree_vector (placement);
6206 if (initializer != NULL)
6207 release_tree_vector (initializer);
6209 return ret;
6212 /* Parse a new-placement.
6214 new-placement:
6215 ( expression-list )
6217 Returns the same representation as for an expression-list. */
6219 static VEC(tree,gc) *
6220 cp_parser_new_placement (cp_parser* parser)
6222 VEC(tree,gc) *expression_list;
6224 /* Parse the expression-list. */
6225 expression_list = (cp_parser_parenthesized_expression_list
6226 (parser, non_attr, /*cast_p=*/false,
6227 /*allow_expansion_p=*/true,
6228 /*non_constant_p=*/NULL));
6230 return expression_list;
6233 /* Parse a new-type-id.
6235 new-type-id:
6236 type-specifier-seq new-declarator [opt]
6238 Returns the TYPE allocated. If the new-type-id indicates an array
6239 type, *NELTS is set to the number of elements in the last array
6240 bound; the TYPE will not include the last array bound. */
6242 static tree
6243 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6245 cp_decl_specifier_seq type_specifier_seq;
6246 cp_declarator *new_declarator;
6247 cp_declarator *declarator;
6248 cp_declarator *outer_declarator;
6249 const char *saved_message;
6250 tree type;
6252 /* The type-specifier sequence must not contain type definitions.
6253 (It cannot contain declarations of new types either, but if they
6254 are not definitions we will catch that because they are not
6255 complete.) */
6256 saved_message = parser->type_definition_forbidden_message;
6257 parser->type_definition_forbidden_message
6258 = G_("types may not be defined in a new-type-id");
6259 /* Parse the type-specifier-seq. */
6260 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6261 /*is_trailing_return=*/false,
6262 &type_specifier_seq);
6263 /* Restore the old message. */
6264 parser->type_definition_forbidden_message = saved_message;
6265 /* Parse the new-declarator. */
6266 new_declarator = cp_parser_new_declarator_opt (parser);
6268 /* Determine the number of elements in the last array dimension, if
6269 any. */
6270 *nelts = NULL_TREE;
6271 /* Skip down to the last array dimension. */
6272 declarator = new_declarator;
6273 outer_declarator = NULL;
6274 while (declarator && (declarator->kind == cdk_pointer
6275 || declarator->kind == cdk_ptrmem))
6277 outer_declarator = declarator;
6278 declarator = declarator->declarator;
6280 while (declarator
6281 && declarator->kind == cdk_array
6282 && declarator->declarator
6283 && declarator->declarator->kind == cdk_array)
6285 outer_declarator = declarator;
6286 declarator = declarator->declarator;
6289 if (declarator && declarator->kind == cdk_array)
6291 *nelts = declarator->u.array.bounds;
6292 if (*nelts == error_mark_node)
6293 *nelts = integer_one_node;
6295 if (outer_declarator)
6296 outer_declarator->declarator = declarator->declarator;
6297 else
6298 new_declarator = NULL;
6301 type = groktypename (&type_specifier_seq, new_declarator, false);
6302 return type;
6305 /* Parse an (optional) new-declarator.
6307 new-declarator:
6308 ptr-operator new-declarator [opt]
6309 direct-new-declarator
6311 Returns the declarator. */
6313 static cp_declarator *
6314 cp_parser_new_declarator_opt (cp_parser* parser)
6316 enum tree_code code;
6317 tree type;
6318 cp_cv_quals cv_quals;
6320 /* We don't know if there's a ptr-operator next, or not. */
6321 cp_parser_parse_tentatively (parser);
6322 /* Look for a ptr-operator. */
6323 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6324 /* If that worked, look for more new-declarators. */
6325 if (cp_parser_parse_definitely (parser))
6327 cp_declarator *declarator;
6329 /* Parse another optional declarator. */
6330 declarator = cp_parser_new_declarator_opt (parser);
6332 return cp_parser_make_indirect_declarator
6333 (code, type, cv_quals, declarator);
6336 /* If the next token is a `[', there is a direct-new-declarator. */
6337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6338 return cp_parser_direct_new_declarator (parser);
6340 return NULL;
6343 /* Parse a direct-new-declarator.
6345 direct-new-declarator:
6346 [ expression ]
6347 direct-new-declarator [constant-expression]
6351 static cp_declarator *
6352 cp_parser_direct_new_declarator (cp_parser* parser)
6354 cp_declarator *declarator = NULL;
6356 while (true)
6358 tree expression;
6360 /* Look for the opening `['. */
6361 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6362 /* The first expression is not required to be constant. */
6363 if (!declarator)
6365 cp_token *token = cp_lexer_peek_token (parser->lexer);
6366 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6367 /* The standard requires that the expression have integral
6368 type. DR 74 adds enumeration types. We believe that the
6369 real intent is that these expressions be handled like the
6370 expression in a `switch' condition, which also allows
6371 classes with a single conversion to integral or
6372 enumeration type. */
6373 if (!processing_template_decl)
6375 expression
6376 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6377 expression,
6378 /*complain=*/true);
6379 if (!expression)
6381 error_at (token->location,
6382 "expression in new-declarator must have integral "
6383 "or enumeration type");
6384 expression = error_mark_node;
6388 /* But all the other expressions must be. */
6389 else
6390 expression
6391 = cp_parser_constant_expression (parser,
6392 /*allow_non_constant=*/false,
6393 NULL);
6394 /* Look for the closing `]'. */
6395 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6397 /* Add this bound to the declarator. */
6398 declarator = make_array_declarator (declarator, expression);
6400 /* If the next token is not a `[', then there are no more
6401 bounds. */
6402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6403 break;
6406 return declarator;
6409 /* Parse a new-initializer.
6411 new-initializer:
6412 ( expression-list [opt] )
6413 braced-init-list
6415 Returns a representation of the expression-list. */
6417 static VEC(tree,gc) *
6418 cp_parser_new_initializer (cp_parser* parser)
6420 VEC(tree,gc) *expression_list;
6422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6424 tree t;
6425 bool expr_non_constant_p;
6426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6427 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6428 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6429 expression_list = make_tree_vector_single (t);
6431 else
6432 expression_list = (cp_parser_parenthesized_expression_list
6433 (parser, non_attr, /*cast_p=*/false,
6434 /*allow_expansion_p=*/true,
6435 /*non_constant_p=*/NULL));
6437 return expression_list;
6440 /* Parse a delete-expression.
6442 delete-expression:
6443 :: [opt] delete cast-expression
6444 :: [opt] delete [ ] cast-expression
6446 Returns a representation of the expression. */
6448 static tree
6449 cp_parser_delete_expression (cp_parser* parser)
6451 bool global_scope_p;
6452 bool array_p;
6453 tree expression;
6455 /* Look for the optional `::' operator. */
6456 global_scope_p
6457 = (cp_parser_global_scope_opt (parser,
6458 /*current_scope_valid_p=*/false)
6459 != NULL_TREE);
6460 /* Look for the `delete' keyword. */
6461 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6462 /* See if the array syntax is in use. */
6463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6465 /* Consume the `[' token. */
6466 cp_lexer_consume_token (parser->lexer);
6467 /* Look for the `]' token. */
6468 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6469 /* Remember that this is the `[]' construct. */
6470 array_p = true;
6472 else
6473 array_p = false;
6475 /* Parse the cast-expression. */
6476 expression = cp_parser_simple_cast_expression (parser);
6478 /* A delete-expression may not appear in an integral constant
6479 expression. */
6480 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6481 return error_mark_node;
6483 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6486 /* Returns true if TOKEN may start a cast-expression and false
6487 otherwise. */
6489 static bool
6490 cp_parser_token_starts_cast_expression (cp_token *token)
6492 switch (token->type)
6494 case CPP_COMMA:
6495 case CPP_SEMICOLON:
6496 case CPP_QUERY:
6497 case CPP_COLON:
6498 case CPP_CLOSE_SQUARE:
6499 case CPP_CLOSE_PAREN:
6500 case CPP_CLOSE_BRACE:
6501 case CPP_DOT:
6502 case CPP_DOT_STAR:
6503 case CPP_DEREF:
6504 case CPP_DEREF_STAR:
6505 case CPP_DIV:
6506 case CPP_MOD:
6507 case CPP_LSHIFT:
6508 case CPP_RSHIFT:
6509 case CPP_LESS:
6510 case CPP_GREATER:
6511 case CPP_LESS_EQ:
6512 case CPP_GREATER_EQ:
6513 case CPP_EQ_EQ:
6514 case CPP_NOT_EQ:
6515 case CPP_EQ:
6516 case CPP_MULT_EQ:
6517 case CPP_DIV_EQ:
6518 case CPP_MOD_EQ:
6519 case CPP_PLUS_EQ:
6520 case CPP_MINUS_EQ:
6521 case CPP_RSHIFT_EQ:
6522 case CPP_LSHIFT_EQ:
6523 case CPP_AND_EQ:
6524 case CPP_XOR_EQ:
6525 case CPP_OR_EQ:
6526 case CPP_XOR:
6527 case CPP_OR:
6528 case CPP_OR_OR:
6529 case CPP_EOF:
6530 return false;
6532 case CPP_OPEN_SQUARE:
6533 PLUGIN_TOKEN_STARTS_CAST_EXPR;
6534 return false;
6536 default:
6537 return true;
6541 /* Parse a cast-expression.
6543 cast-expression:
6544 unary-expression
6545 ( type-id ) cast-expression
6547 ADDRESS_P is true iff the unary-expression is appearing as the
6548 operand of the `&' operator. CAST_P is true if this expression is
6549 the target of a cast.
6551 Returns a representation of the expression. */
6553 static tree
6554 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6555 cp_id_kind * pidk)
6557 /* If it's a `(', then we might be looking at a cast. */
6558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6560 tree type = NULL_TREE;
6561 tree expr = NULL_TREE;
6562 bool compound_literal_p;
6563 const char *saved_message;
6565 /* There's no way to know yet whether or not this is a cast.
6566 For example, `(int (3))' is a unary-expression, while `(int)
6567 3' is a cast. So, we resort to parsing tentatively. */
6568 cp_parser_parse_tentatively (parser);
6569 /* Types may not be defined in a cast. */
6570 saved_message = parser->type_definition_forbidden_message;
6571 parser->type_definition_forbidden_message
6572 = G_("types may not be defined in casts");
6573 /* Consume the `('. */
6574 cp_lexer_consume_token (parser->lexer);
6575 /* A very tricky bit is that `(struct S) { 3 }' is a
6576 compound-literal (which we permit in C++ as an extension).
6577 But, that construct is not a cast-expression -- it is a
6578 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6579 is legal; if the compound-literal were a cast-expression,
6580 you'd need an extra set of parentheses.) But, if we parse
6581 the type-id, and it happens to be a class-specifier, then we
6582 will commit to the parse at that point, because we cannot
6583 undo the action that is done when creating a new class. So,
6584 then we cannot back up and do a postfix-expression.
6586 Therefore, we scan ahead to the closing `)', and check to see
6587 if the token after the `)' is a `{'. If so, we are not
6588 looking at a cast-expression.
6590 Save tokens so that we can put them back. */
6591 cp_lexer_save_tokens (parser->lexer);
6592 /* Skip tokens until the next token is a closing parenthesis.
6593 If we find the closing `)', and the next token is a `{', then
6594 we are looking at a compound-literal. */
6595 compound_literal_p
6596 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6597 /*consume_paren=*/true)
6598 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6599 /* Roll back the tokens we skipped. */
6600 cp_lexer_rollback_tokens (parser->lexer);
6601 /* If we were looking at a compound-literal, simulate an error
6602 so that the call to cp_parser_parse_definitely below will
6603 fail. */
6604 if (compound_literal_p)
6605 cp_parser_simulate_error (parser);
6606 else
6608 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6609 parser->in_type_id_in_expr_p = true;
6610 /* Look for the type-id. */
6611 type = cp_parser_type_id (parser);
6612 /* Look for the closing `)'. */
6613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6614 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6617 /* Restore the saved message. */
6618 parser->type_definition_forbidden_message = saved_message;
6620 /* At this point this can only be either a cast or a
6621 parenthesized ctor such as `(T ())' that looks like a cast to
6622 function returning T. */
6623 if (!cp_parser_error_occurred (parser)
6624 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6625 (parser->lexer)))
6627 cp_parser_parse_definitely (parser);
6628 expr = cp_parser_cast_expression (parser,
6629 /*address_p=*/false,
6630 /*cast_p=*/true, pidk);
6632 /* Warn about old-style casts, if so requested. */
6633 if (warn_old_style_cast
6634 && !in_system_header
6635 && !VOID_TYPE_P (type)
6636 && current_lang_name != lang_name_c)
6637 warning (OPT_Wold_style_cast, "use of old-style cast");
6639 /* Only type conversions to integral or enumeration types
6640 can be used in constant-expressions. */
6641 if (!cast_valid_in_integral_constant_expression_p (type)
6642 && cp_parser_non_integral_constant_expression (parser,
6643 NIC_CAST))
6644 return error_mark_node;
6646 /* Perform the cast. */
6647 expr = build_c_cast (input_location, type, expr);
6648 return expr;
6650 else
6651 cp_parser_abort_tentative_parse (parser);
6654 /* If we get here, then it's not a cast, so it must be a
6655 unary-expression. */
6656 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6659 /* Parse a binary expression of the general form:
6661 pm-expression:
6662 cast-expression
6663 pm-expression .* cast-expression
6664 pm-expression ->* cast-expression
6666 multiplicative-expression:
6667 pm-expression
6668 multiplicative-expression * pm-expression
6669 multiplicative-expression / pm-expression
6670 multiplicative-expression % pm-expression
6672 additive-expression:
6673 multiplicative-expression
6674 additive-expression + multiplicative-expression
6675 additive-expression - multiplicative-expression
6677 shift-expression:
6678 additive-expression
6679 shift-expression << additive-expression
6680 shift-expression >> additive-expression
6682 relational-expression:
6683 shift-expression
6684 relational-expression < shift-expression
6685 relational-expression > shift-expression
6686 relational-expression <= shift-expression
6687 relational-expression >= shift-expression
6689 GNU Extension:
6691 relational-expression:
6692 relational-expression <? shift-expression
6693 relational-expression >? shift-expression
6695 equality-expression:
6696 relational-expression
6697 equality-expression == relational-expression
6698 equality-expression != relational-expression
6700 and-expression:
6701 equality-expression
6702 and-expression & equality-expression
6704 exclusive-or-expression:
6705 and-expression
6706 exclusive-or-expression ^ and-expression
6708 inclusive-or-expression:
6709 exclusive-or-expression
6710 inclusive-or-expression | exclusive-or-expression
6712 logical-and-expression:
6713 inclusive-or-expression
6714 logical-and-expression && inclusive-or-expression
6716 logical-or-expression:
6717 logical-and-expression
6718 logical-or-expression || logical-and-expression
6720 All these are implemented with a single function like:
6722 binary-expression:
6723 simple-cast-expression
6724 binary-expression <token> binary-expression
6726 CAST_P is true if this expression is the target of a cast.
6728 The binops_by_token map is used to get the tree codes for each <token> type.
6729 binary-expressions are associated according to a precedence table. */
6731 #define TOKEN_PRECEDENCE(token) \
6732 (((token->type == CPP_GREATER \
6733 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6734 && !parser->greater_than_is_operator_p) \
6735 ? PREC_NOT_OPERATOR \
6736 : binops_by_token[token->type].prec)
6738 static tree
6739 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6740 bool no_toplevel_fold_p,
6741 enum cp_parser_prec prec,
6742 cp_id_kind * pidk)
6744 cp_parser_expression_stack stack;
6745 cp_parser_expression_stack_entry *sp = &stack[0];
6746 tree lhs, rhs;
6747 cp_token *token;
6748 enum tree_code tree_type, lhs_type, rhs_type;
6749 enum cp_parser_prec new_prec, lookahead_prec;
6750 bool overloaded_p;
6752 /* Parse the first expression. */
6753 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6754 lhs_type = ERROR_MARK;
6756 for (;;)
6758 /* Get an operator token. */
6759 token = cp_lexer_peek_token (parser->lexer);
6761 if (warn_cxx0x_compat
6762 && token->type == CPP_RSHIFT
6763 && !parser->greater_than_is_operator_p)
6765 if (warning_at (token->location, OPT_Wc__0x_compat,
6766 "%<>>%> operator will be treated as"
6767 " two right angle brackets in C++0x"))
6768 inform (token->location,
6769 "suggest parentheses around %<>>%> expression");
6772 new_prec = TOKEN_PRECEDENCE (token);
6774 /* Popping an entry off the stack means we completed a subexpression:
6775 - either we found a token which is not an operator (`>' where it is not
6776 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6777 will happen repeatedly;
6778 - or, we found an operator which has lower priority. This is the case
6779 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6780 parsing `3 * 4'. */
6781 if (new_prec <= prec)
6783 if (sp == stack)
6784 break;
6785 else
6786 goto pop;
6789 get_rhs:
6790 tree_type = binops_by_token[token->type].tree_type;
6792 /* We used the operator token. */
6793 cp_lexer_consume_token (parser->lexer);
6795 /* For "false && x" or "true || x", x will never be executed;
6796 disable warnings while evaluating it. */
6797 if (tree_type == TRUTH_ANDIF_EXPR)
6798 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6799 else if (tree_type == TRUTH_ORIF_EXPR)
6800 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6802 /* Extract another operand. It may be the RHS of this expression
6803 or the LHS of a new, higher priority expression. */
6804 rhs = cp_parser_simple_cast_expression (parser);
6805 rhs_type = ERROR_MARK;
6807 /* Get another operator token. Look up its precedence to avoid
6808 building a useless (immediately popped) stack entry for common
6809 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6810 token = cp_lexer_peek_token (parser->lexer);
6811 lookahead_prec = TOKEN_PRECEDENCE (token);
6812 if (lookahead_prec > new_prec)
6814 /* ... and prepare to parse the RHS of the new, higher priority
6815 expression. Since precedence levels on the stack are
6816 monotonically increasing, we do not have to care about
6817 stack overflows. */
6818 sp->prec = prec;
6819 sp->tree_type = tree_type;
6820 sp->lhs = lhs;
6821 sp->lhs_type = lhs_type;
6822 sp++;
6823 lhs = rhs;
6824 lhs_type = rhs_type;
6825 prec = new_prec;
6826 new_prec = lookahead_prec;
6827 goto get_rhs;
6829 pop:
6830 lookahead_prec = new_prec;
6831 /* If the stack is not empty, we have parsed into LHS the right side
6832 (`4' in the example above) of an expression we had suspended.
6833 We can use the information on the stack to recover the LHS (`3')
6834 from the stack together with the tree code (`MULT_EXPR'), and
6835 the precedence of the higher level subexpression
6836 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6837 which will be used to actually build the additive expression. */
6838 --sp;
6839 prec = sp->prec;
6840 tree_type = sp->tree_type;
6841 rhs = lhs;
6842 rhs_type = lhs_type;
6843 lhs = sp->lhs;
6844 lhs_type = sp->lhs_type;
6847 /* Undo the disabling of warnings done above. */
6848 if (tree_type == TRUTH_ANDIF_EXPR)
6849 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6850 else if (tree_type == TRUTH_ORIF_EXPR)
6851 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6853 overloaded_p = false;
6854 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6855 ERROR_MARK for everything that is not a binary expression.
6856 This makes warn_about_parentheses miss some warnings that
6857 involve unary operators. For unary expressions we should
6858 pass the correct tree_code unless the unary expression was
6859 surrounded by parentheses.
6861 if (no_toplevel_fold_p
6862 && lookahead_prec <= prec
6863 && sp == stack
6864 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6865 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6866 else
6867 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6868 &overloaded_p, tf_warning_or_error);
6869 lhs_type = tree_type;
6871 /* If the binary operator required the use of an overloaded operator,
6872 then this expression cannot be an integral constant-expression.
6873 An overloaded operator can be used even if both operands are
6874 otherwise permissible in an integral constant-expression if at
6875 least one of the operands is of enumeration type. */
6877 if (overloaded_p
6878 && cp_parser_non_integral_constant_expression (parser,
6879 NIC_OVERLOADED))
6880 return error_mark_node;
6883 return lhs;
6887 /* Parse the `? expression : assignment-expression' part of a
6888 conditional-expression. The LOGICAL_OR_EXPR is the
6889 logical-or-expression that started the conditional-expression.
6890 Returns a representation of the entire conditional-expression.
6892 This routine is used by cp_parser_assignment_expression.
6894 ? expression : assignment-expression
6896 GNU Extensions:
6898 ? : assignment-expression */
6900 static tree
6901 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6903 tree expr;
6904 tree assignment_expr;
6905 struct cp_token *token;
6907 /* Consume the `?' token. */
6908 cp_lexer_consume_token (parser->lexer);
6909 token = cp_lexer_peek_token (parser->lexer);
6910 if (cp_parser_allow_gnu_extensions_p (parser)
6911 && token->type == CPP_COLON)
6913 pedwarn (token->location, OPT_pedantic,
6914 "ISO C++ does not allow ?: with omitted middle operand");
6915 /* Implicit true clause. */
6916 expr = NULL_TREE;
6917 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6918 warn_for_omitted_condop (token->location, logical_or_expr);
6920 else
6922 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6923 parser->colon_corrects_to_scope_p = false;
6924 /* Parse the expression. */
6925 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6926 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6927 c_inhibit_evaluation_warnings +=
6928 ((logical_or_expr == truthvalue_true_node)
6929 - (logical_or_expr == truthvalue_false_node));
6930 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6933 /* The next token should be a `:'. */
6934 cp_parser_require (parser, CPP_COLON, RT_COLON);
6935 /* Parse the assignment-expression. */
6936 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6937 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6939 /* Build the conditional-expression. */
6940 return build_x_conditional_expr (logical_or_expr,
6941 expr,
6942 assignment_expr,
6943 tf_warning_or_error);
6946 /* Parse an assignment-expression.
6948 assignment-expression:
6949 conditional-expression
6950 logical-or-expression assignment-operator assignment_expression
6951 throw-expression
6953 CAST_P is true if this expression is the target of a cast.
6955 Returns a representation for the expression. */
6957 static tree
6958 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6959 cp_id_kind * pidk)
6961 tree expr;
6963 /* If the next token is the `throw' keyword, then we're looking at
6964 a throw-expression. */
6965 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6966 expr = cp_parser_throw_expression (parser);
6967 /* Otherwise, it must be that we are looking at a
6968 logical-or-expression. */
6969 else
6971 /* Parse the binary expressions (logical-or-expression). */
6972 expr = cp_parser_binary_expression (parser, cast_p, false,
6973 PREC_NOT_OPERATOR, pidk);
6974 /* If the next token is a `?' then we're actually looking at a
6975 conditional-expression. */
6976 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6977 return cp_parser_question_colon_clause (parser, expr);
6978 else
6980 enum tree_code assignment_operator;
6982 /* If it's an assignment-operator, we're using the second
6983 production. */
6984 assignment_operator
6985 = cp_parser_assignment_operator_opt (parser);
6986 if (assignment_operator != ERROR_MARK)
6988 bool non_constant_p;
6990 /* Parse the right-hand side of the assignment. */
6991 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6993 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6994 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6996 /* An assignment may not appear in a
6997 constant-expression. */
6998 if (cp_parser_non_integral_constant_expression (parser,
6999 NIC_ASSIGNMENT))
7000 return error_mark_node;
7001 /* Build the assignment expression. */
7002 expr = build_x_modify_expr (expr,
7003 assignment_operator,
7004 rhs,
7005 tf_warning_or_error);
7010 return expr;
7013 /* Parse an (optional) assignment-operator.
7015 assignment-operator: one of
7016 = *= /= %= += -= >>= <<= &= ^= |=
7018 GNU Extension:
7020 assignment-operator: one of
7021 <?= >?=
7023 If the next token is an assignment operator, the corresponding tree
7024 code is returned, and the token is consumed. For example, for
7025 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7026 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7027 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7028 operator, ERROR_MARK is returned. */
7030 static enum tree_code
7031 cp_parser_assignment_operator_opt (cp_parser* parser)
7033 enum tree_code op;
7034 cp_token *token;
7036 /* Peek at the next token. */
7037 token = cp_lexer_peek_token (parser->lexer);
7039 switch (token->type)
7041 case CPP_EQ:
7042 op = NOP_EXPR;
7043 break;
7045 case CPP_MULT_EQ:
7046 op = MULT_EXPR;
7047 break;
7049 case CPP_DIV_EQ:
7050 op = TRUNC_DIV_EXPR;
7051 break;
7053 case CPP_MOD_EQ:
7054 op = TRUNC_MOD_EXPR;
7055 break;
7057 case CPP_PLUS_EQ:
7058 op = PLUS_EXPR;
7059 break;
7061 case CPP_MINUS_EQ:
7062 op = MINUS_EXPR;
7063 break;
7065 case CPP_RSHIFT_EQ:
7066 op = RSHIFT_EXPR;
7067 break;
7069 case CPP_LSHIFT_EQ:
7070 op = LSHIFT_EXPR;
7071 break;
7073 case CPP_AND_EQ:
7074 op = BIT_AND_EXPR;
7075 break;
7077 case CPP_XOR_EQ:
7078 op = BIT_XOR_EXPR;
7079 break;
7081 case CPP_OR_EQ:
7082 op = BIT_IOR_EXPR;
7083 break;
7085 default:
7086 /* Nothing else is an assignment operator. */
7087 op = ERROR_MARK;
7090 /* If it was an assignment operator, consume it. */
7091 if (op != ERROR_MARK)
7092 cp_lexer_consume_token (parser->lexer);
7094 return op;
7097 /* Parse an expression.
7099 expression:
7100 assignment-expression
7101 expression , assignment-expression
7103 CAST_P is true if this expression is the target of a cast.
7105 Returns a representation of the expression. */
7107 static tree
7108 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7110 tree expression = NULL_TREE;
7112 while (true)
7114 tree assignment_expression;
7116 /* Parse the next assignment-expression. */
7117 assignment_expression
7118 = cp_parser_assignment_expression (parser, cast_p, pidk);
7119 /* If this is the first assignment-expression, we can just
7120 save it away. */
7121 if (!expression)
7122 expression = assignment_expression;
7123 else
7124 expression = build_x_compound_expr (expression,
7125 assignment_expression,
7126 tf_warning_or_error);
7127 /* If the next token is not a comma, then we are done with the
7128 expression. */
7129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7130 break;
7131 /* Consume the `,'. */
7132 cp_lexer_consume_token (parser->lexer);
7133 /* A comma operator cannot appear in a constant-expression. */
7134 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7135 expression = error_mark_node;
7138 return expression;
7141 /* Parse a constant-expression.
7143 constant-expression:
7144 conditional-expression
7146 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7147 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7148 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7149 is false, NON_CONSTANT_P should be NULL. */
7151 static tree
7152 cp_parser_constant_expression (cp_parser* parser,
7153 bool allow_non_constant_p,
7154 bool *non_constant_p)
7156 bool saved_integral_constant_expression_p;
7157 bool saved_allow_non_integral_constant_expression_p;
7158 bool saved_non_integral_constant_expression_p;
7159 tree expression;
7161 /* It might seem that we could simply parse the
7162 conditional-expression, and then check to see if it were
7163 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7164 one that the compiler can figure out is constant, possibly after
7165 doing some simplifications or optimizations. The standard has a
7166 precise definition of constant-expression, and we must honor
7167 that, even though it is somewhat more restrictive.
7169 For example:
7171 int i[(2, 3)];
7173 is not a legal declaration, because `(2, 3)' is not a
7174 constant-expression. The `,' operator is forbidden in a
7175 constant-expression. However, GCC's constant-folding machinery
7176 will fold this operation to an INTEGER_CST for `3'. */
7178 /* Save the old settings. */
7179 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7180 saved_allow_non_integral_constant_expression_p
7181 = parser->allow_non_integral_constant_expression_p;
7182 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7183 /* We are now parsing a constant-expression. */
7184 parser->integral_constant_expression_p = true;
7185 parser->allow_non_integral_constant_expression_p
7186 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7187 parser->non_integral_constant_expression_p = false;
7188 /* Although the grammar says "conditional-expression", we parse an
7189 "assignment-expression", which also permits "throw-expression"
7190 and the use of assignment operators. In the case that
7191 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7192 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7193 actually essential that we look for an assignment-expression.
7194 For example, cp_parser_initializer_clauses uses this function to
7195 determine whether a particular assignment-expression is in fact
7196 constant. */
7197 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7198 /* Restore the old settings. */
7199 parser->integral_constant_expression_p
7200 = saved_integral_constant_expression_p;
7201 parser->allow_non_integral_constant_expression_p
7202 = saved_allow_non_integral_constant_expression_p;
7203 if (allow_non_constant_p)
7204 *non_constant_p = parser->non_integral_constant_expression_p;
7205 else if (parser->non_integral_constant_expression_p
7206 && cxx_dialect < cxx0x)
7207 expression = error_mark_node;
7208 parser->non_integral_constant_expression_p
7209 = saved_non_integral_constant_expression_p;
7211 return expression;
7214 /* Parse __builtin_offsetof.
7216 offsetof-expression:
7217 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7219 offsetof-member-designator:
7220 id-expression
7221 | offsetof-member-designator "." id-expression
7222 | offsetof-member-designator "[" expression "]"
7223 | offsetof-member-designator "->" id-expression */
7225 static tree
7226 cp_parser_builtin_offsetof (cp_parser *parser)
7228 int save_ice_p, save_non_ice_p;
7229 tree type, expr;
7230 cp_id_kind dummy;
7231 cp_token *token;
7233 /* We're about to accept non-integral-constant things, but will
7234 definitely yield an integral constant expression. Save and
7235 restore these values around our local parsing. */
7236 save_ice_p = parser->integral_constant_expression_p;
7237 save_non_ice_p = parser->non_integral_constant_expression_p;
7239 /* Consume the "__builtin_offsetof" token. */
7240 cp_lexer_consume_token (parser->lexer);
7241 /* Consume the opening `('. */
7242 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7243 /* Parse the type-id. */
7244 type = cp_parser_type_id (parser);
7245 /* Look for the `,'. */
7246 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7247 token = cp_lexer_peek_token (parser->lexer);
7249 /* Build the (type *)null that begins the traditional offsetof macro. */
7250 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7251 tf_warning_or_error);
7253 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7254 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7255 true, &dummy, token->location);
7256 while (true)
7258 token = cp_lexer_peek_token (parser->lexer);
7259 switch (token->type)
7261 case CPP_OPEN_SQUARE:
7262 /* offsetof-member-designator "[" expression "]" */
7263 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7264 break;
7266 case CPP_DEREF:
7267 /* offsetof-member-designator "->" identifier */
7268 expr = grok_array_decl (expr, integer_zero_node);
7269 /* FALLTHRU */
7271 case CPP_DOT:
7272 /* offsetof-member-designator "." identifier */
7273 cp_lexer_consume_token (parser->lexer);
7274 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7275 expr, true, &dummy,
7276 token->location);
7277 break;
7279 case CPP_CLOSE_PAREN:
7280 /* Consume the ")" token. */
7281 cp_lexer_consume_token (parser->lexer);
7282 goto success;
7284 default:
7285 /* Error. We know the following require will fail, but
7286 that gives the proper error message. */
7287 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7288 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7289 expr = error_mark_node;
7290 goto failure;
7294 success:
7295 /* If we're processing a template, we can't finish the semantics yet.
7296 Otherwise we can fold the entire expression now. */
7297 if (processing_template_decl)
7298 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7299 else
7300 expr = finish_offsetof (expr);
7302 failure:
7303 parser->integral_constant_expression_p = save_ice_p;
7304 parser->non_integral_constant_expression_p = save_non_ice_p;
7306 return expr;
7309 /* Parse a trait expression. */
7311 static tree
7312 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7314 cp_trait_kind kind;
7315 tree type1, type2 = NULL_TREE;
7316 bool binary = false;
7317 cp_decl_specifier_seq decl_specs;
7319 switch (keyword)
7321 case RID_HAS_NOTHROW_ASSIGN:
7322 kind = CPTK_HAS_NOTHROW_ASSIGN;
7323 break;
7324 case RID_HAS_NOTHROW_CONSTRUCTOR:
7325 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7326 break;
7327 case RID_HAS_NOTHROW_COPY:
7328 kind = CPTK_HAS_NOTHROW_COPY;
7329 break;
7330 case RID_HAS_TRIVIAL_ASSIGN:
7331 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7332 break;
7333 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7334 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7335 break;
7336 case RID_HAS_TRIVIAL_COPY:
7337 kind = CPTK_HAS_TRIVIAL_COPY;
7338 break;
7339 case RID_HAS_TRIVIAL_DESTRUCTOR:
7340 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7341 break;
7342 case RID_HAS_VIRTUAL_DESTRUCTOR:
7343 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7344 break;
7345 case RID_IS_ABSTRACT:
7346 kind = CPTK_IS_ABSTRACT;
7347 break;
7348 case RID_IS_BASE_OF:
7349 kind = CPTK_IS_BASE_OF;
7350 binary = true;
7351 break;
7352 case RID_IS_CLASS:
7353 kind = CPTK_IS_CLASS;
7354 break;
7355 case RID_IS_CONVERTIBLE_TO:
7356 kind = CPTK_IS_CONVERTIBLE_TO;
7357 binary = true;
7358 break;
7359 case RID_IS_EMPTY:
7360 kind = CPTK_IS_EMPTY;
7361 break;
7362 case RID_IS_ENUM:
7363 kind = CPTK_IS_ENUM;
7364 break;
7365 case RID_IS_POD:
7366 kind = CPTK_IS_POD;
7367 break;
7368 case RID_IS_POLYMORPHIC:
7369 kind = CPTK_IS_POLYMORPHIC;
7370 break;
7371 case RID_IS_STD_LAYOUT:
7372 kind = CPTK_IS_STD_LAYOUT;
7373 break;
7374 case RID_IS_TRIVIAL:
7375 kind = CPTK_IS_TRIVIAL;
7376 break;
7377 case RID_IS_UNION:
7378 kind = CPTK_IS_UNION;
7379 break;
7380 case RID_IS_LITERAL_TYPE:
7381 kind = CPTK_IS_LITERAL_TYPE;
7382 break;
7383 default:
7384 gcc_unreachable ();
7387 /* Consume the token. */
7388 cp_lexer_consume_token (parser->lexer);
7390 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7392 type1 = cp_parser_type_id (parser);
7394 if (type1 == error_mark_node)
7395 return error_mark_node;
7397 /* Build a trivial decl-specifier-seq. */
7398 clear_decl_specs (&decl_specs);
7399 decl_specs.type = type1;
7401 /* Call grokdeclarator to figure out what type this is. */
7402 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7403 /*initialized=*/0, /*attrlist=*/NULL);
7405 if (binary)
7407 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7409 type2 = cp_parser_type_id (parser);
7411 if (type2 == error_mark_node)
7412 return error_mark_node;
7414 /* Build a trivial decl-specifier-seq. */
7415 clear_decl_specs (&decl_specs);
7416 decl_specs.type = type2;
7418 /* Call grokdeclarator to figure out what type this is. */
7419 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7420 /*initialized=*/0, /*attrlist=*/NULL);
7423 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7425 /* Complete the trait expression, which may mean either processing
7426 the trait expr now or saving it for template instantiation. */
7427 return finish_trait_expr (kind, type1, type2);
7430 /* Lambdas that appear in variable initializer or default argument scope
7431 get that in their mangling, so we need to record it. We might as well
7432 use the count for function and namespace scopes as well. */
7433 static GTY(()) tree lambda_scope;
7434 static GTY(()) int lambda_count;
7435 typedef struct GTY(()) tree_int
7437 tree t;
7438 int i;
7439 } tree_int;
7440 DEF_VEC_O(tree_int);
7441 DEF_VEC_ALLOC_O(tree_int,gc);
7442 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7444 static void
7445 start_lambda_scope (tree decl)
7447 tree_int ti;
7448 gcc_assert (decl);
7449 /* Once we're inside a function, we ignore other scopes and just push
7450 the function again so that popping works properly. */
7451 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7452 decl = current_function_decl;
7453 ti.t = lambda_scope;
7454 ti.i = lambda_count;
7455 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7456 if (lambda_scope != decl)
7458 /* Don't reset the count if we're still in the same function. */
7459 lambda_scope = decl;
7460 lambda_count = 0;
7464 static void
7465 record_lambda_scope (tree lambda)
7467 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7468 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7471 static void
7472 finish_lambda_scope (void)
7474 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7475 if (lambda_scope != p->t)
7477 lambda_scope = p->t;
7478 lambda_count = p->i;
7480 VEC_pop (tree_int, lambda_scope_stack);
7483 /* Parse a lambda expression.
7485 lambda-expression:
7486 lambda-introducer lambda-declarator [opt] compound-statement
7488 Returns a representation of the expression. */
7490 static tree
7491 cp_parser_lambda_expression (cp_parser* parser)
7493 tree lambda_expr = build_lambda_expr ();
7494 tree type;
7496 LAMBDA_EXPR_LOCATION (lambda_expr)
7497 = cp_lexer_peek_token (parser->lexer)->location;
7499 if (cp_unevaluated_operand)
7500 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7501 "lambda-expression in unevaluated context");
7503 /* We may be in the middle of deferred access check. Disable
7504 it now. */
7505 push_deferring_access_checks (dk_no_deferred);
7507 cp_parser_lambda_introducer (parser, lambda_expr);
7509 type = begin_lambda_type (lambda_expr);
7511 record_lambda_scope (lambda_expr);
7513 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7514 determine_visibility (TYPE_NAME (type));
7516 /* Now that we've started the type, add the capture fields for any
7517 explicit captures. */
7518 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7521 /* Inside the class, surrounding template-parameter-lists do not apply. */
7522 unsigned int saved_num_template_parameter_lists
7523 = parser->num_template_parameter_lists;
7525 parser->num_template_parameter_lists = 0;
7527 /* By virtue of defining a local class, a lambda expression has access to
7528 the private variables of enclosing classes. */
7530 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7532 cp_parser_lambda_body (parser, lambda_expr);
7534 /* The capture list was built up in reverse order; fix that now. */
7536 tree newlist = NULL_TREE;
7537 tree elt, next;
7539 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7540 elt; elt = next)
7542 tree field = TREE_PURPOSE (elt);
7543 char *buf;
7545 next = TREE_CHAIN (elt);
7546 TREE_CHAIN (elt) = newlist;
7547 newlist = elt;
7549 /* Also add __ to the beginning of the field name so that code
7550 outside the lambda body can't see the captured name. We could
7551 just remove the name entirely, but this is more useful for
7552 debugging. */
7553 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7554 /* The 'this' capture already starts with __. */
7555 continue;
7557 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7558 buf[1] = buf[0] = '_';
7559 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7560 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7561 DECL_NAME (field) = get_identifier (buf);
7563 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7566 maybe_add_lambda_conv_op (type);
7568 type = finish_struct (type, /*attributes=*/NULL_TREE);
7570 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7573 pop_deferring_access_checks ();
7575 return build_lambda_object (lambda_expr);
7578 /* Parse the beginning of a lambda expression.
7580 lambda-introducer:
7581 [ lambda-capture [opt] ]
7583 LAMBDA_EXPR is the current representation of the lambda expression. */
7585 static void
7586 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7588 /* Need commas after the first capture. */
7589 bool first = true;
7591 /* Eat the leading `['. */
7592 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7594 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7595 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7596 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7597 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7598 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7599 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7601 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7603 cp_lexer_consume_token (parser->lexer);
7604 first = false;
7607 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7609 cp_token* capture_token;
7610 tree capture_id;
7611 tree capture_init_expr;
7612 cp_id_kind idk = CP_ID_KIND_NONE;
7613 bool explicit_init_p = false;
7615 enum capture_kind_type
7617 BY_COPY,
7618 BY_REFERENCE
7620 enum capture_kind_type capture_kind = BY_COPY;
7622 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7624 error ("expected end of capture-list");
7625 return;
7628 if (first)
7629 first = false;
7630 else
7631 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7633 /* Possibly capture `this'. */
7634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7636 cp_lexer_consume_token (parser->lexer);
7637 add_capture (lambda_expr,
7638 /*id=*/get_identifier ("__this"),
7639 /*initializer=*/finish_this_expr(),
7640 /*by_reference_p=*/false,
7641 explicit_init_p);
7642 continue;
7645 /* Remember whether we want to capture as a reference or not. */
7646 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7648 capture_kind = BY_REFERENCE;
7649 cp_lexer_consume_token (parser->lexer);
7652 /* Get the identifier. */
7653 capture_token = cp_lexer_peek_token (parser->lexer);
7654 capture_id = cp_parser_identifier (parser);
7656 if (capture_id == error_mark_node)
7657 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7658 delimiters, but I modified this to stop on unnested ']' as well. It
7659 was already changed to stop on unnested '}', so the
7660 "closing_parenthesis" name is no more misleading with my change. */
7662 cp_parser_skip_to_closing_parenthesis (parser,
7663 /*recovering=*/true,
7664 /*or_comma=*/true,
7665 /*consume_paren=*/true);
7666 break;
7669 /* Find the initializer for this capture. */
7670 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7672 /* An explicit expression exists. */
7673 cp_lexer_consume_token (parser->lexer);
7674 pedwarn (input_location, OPT_pedantic,
7675 "ISO C++ does not allow initializers "
7676 "in lambda expression capture lists");
7677 capture_init_expr = cp_parser_assignment_expression (parser,
7678 /*cast_p=*/true,
7679 &idk);
7680 explicit_init_p = true;
7682 else
7684 const char* error_msg;
7686 /* Turn the identifier into an id-expression. */
7687 capture_init_expr
7688 = cp_parser_lookup_name
7689 (parser,
7690 capture_id,
7691 none_type,
7692 /*is_template=*/false,
7693 /*is_namespace=*/false,
7694 /*check_dependency=*/true,
7695 /*ambiguous_decls=*/NULL,
7696 capture_token->location);
7698 capture_init_expr
7699 = finish_id_expression
7700 (capture_id,
7701 capture_init_expr,
7702 parser->scope,
7703 &idk,
7704 /*integral_constant_expression_p=*/false,
7705 /*allow_non_integral_constant_expression_p=*/false,
7706 /*non_integral_constant_expression_p=*/NULL,
7707 /*template_p=*/false,
7708 /*done=*/true,
7709 /*address_p=*/false,
7710 /*template_arg_p=*/false,
7711 &error_msg,
7712 capture_token->location);
7715 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7716 capture_init_expr
7717 = unqualified_name_lookup_error (capture_init_expr);
7719 add_capture (lambda_expr,
7720 capture_id,
7721 capture_init_expr,
7722 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7723 explicit_init_p);
7726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7729 /* Parse the (optional) middle of a lambda expression.
7731 lambda-declarator:
7732 ( parameter-declaration-clause [opt] )
7733 attribute-specifier [opt]
7734 mutable [opt]
7735 exception-specification [opt]
7736 lambda-return-type-clause [opt]
7738 LAMBDA_EXPR is the current representation of the lambda expression. */
7740 static void
7741 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7743 /* 5.1.1.4 of the standard says:
7744 If a lambda-expression does not include a lambda-declarator, it is as if
7745 the lambda-declarator were ().
7746 This means an empty parameter list, no attributes, and no exception
7747 specification. */
7748 tree param_list = void_list_node;
7749 tree attributes = NULL_TREE;
7750 tree exception_spec = NULL_TREE;
7751 tree t;
7753 /* The lambda-declarator is optional, but must begin with an opening
7754 parenthesis if present. */
7755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7757 cp_lexer_consume_token (parser->lexer);
7759 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7761 /* Parse parameters. */
7762 param_list = cp_parser_parameter_declaration_clause (parser);
7764 /* Default arguments shall not be specified in the
7765 parameter-declaration-clause of a lambda-declarator. */
7766 for (t = param_list; t; t = TREE_CHAIN (t))
7767 if (TREE_PURPOSE (t))
7768 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7769 "default argument specified for lambda parameter");
7771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7773 attributes = cp_parser_attributes_opt (parser);
7775 /* Parse optional `mutable' keyword. */
7776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7778 cp_lexer_consume_token (parser->lexer);
7779 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7782 /* Parse optional exception specification. */
7783 exception_spec = cp_parser_exception_specification_opt (parser);
7785 /* Parse optional trailing return type. */
7786 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7788 cp_lexer_consume_token (parser->lexer);
7789 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7792 /* The function parameters must be in scope all the way until after the
7793 trailing-return-type in case of decltype. */
7794 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7795 pop_binding (DECL_NAME (t), t);
7797 leave_scope ();
7800 /* Create the function call operator.
7802 Messing with declarators like this is no uglier than building up the
7803 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7804 other code. */
7806 cp_decl_specifier_seq return_type_specs;
7807 cp_declarator* declarator;
7808 tree fco;
7809 int quals;
7810 void *p;
7812 clear_decl_specs (&return_type_specs);
7813 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7814 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7815 else
7816 /* Maybe we will deduce the return type later, but we can use void
7817 as a placeholder return type anyways. */
7818 return_type_specs.type = void_type_node;
7820 p = obstack_alloc (&declarator_obstack, 0);
7822 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7823 sfk_none);
7825 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7826 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7827 declarator = make_call_declarator (declarator, param_list, quals,
7828 exception_spec,
7829 /*late_return_type=*/NULL_TREE);
7830 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7832 fco = grokmethod (&return_type_specs,
7833 declarator,
7834 attributes);
7835 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7836 DECL_ARTIFICIAL (fco) = 1;
7838 finish_member_declaration (fco);
7840 obstack_free (&declarator_obstack, p);
7844 /* Parse the body of a lambda expression, which is simply
7846 compound-statement
7848 but which requires special handling.
7849 LAMBDA_EXPR is the current representation of the lambda expression. */
7851 static void
7852 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7854 bool nested = (current_function_decl != NULL_TREE);
7855 if (nested)
7856 push_function_context ();
7858 /* Finish the function call operator
7859 - class_specifier
7860 + late_parsing_for_member
7861 + function_definition_after_declarator
7862 + ctor_initializer_opt_and_function_body */
7864 tree fco = lambda_function (lambda_expr);
7865 tree body;
7866 bool done = false;
7868 /* Let the front end know that we are going to be defining this
7869 function. */
7870 start_preparsed_function (fco,
7871 NULL_TREE,
7872 SF_PRE_PARSED | SF_INCLASS_INLINE);
7874 start_lambda_scope (fco);
7875 body = begin_function_body ();
7877 /* 5.1.1.4 of the standard says:
7878 If a lambda-expression does not include a trailing-return-type, it
7879 is as if the trailing-return-type denotes the following type:
7880 * if the compound-statement is of the form
7881 { return attribute-specifier [opt] expression ; }
7882 the type of the returned expression after lvalue-to-rvalue
7883 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7884 (_conv.array_ 4.2), and function-to-pointer conversion
7885 (_conv.func_ 4.3);
7886 * otherwise, void. */
7888 /* In a lambda that has neither a lambda-return-type-clause
7889 nor a deducible form, errors should be reported for return statements
7890 in the body. Since we used void as the placeholder return type, parsing
7891 the body as usual will give such desired behavior. */
7892 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7893 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7894 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7895 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7897 tree compound_stmt;
7898 tree expr = NULL_TREE;
7899 cp_id_kind idk = CP_ID_KIND_NONE;
7901 /* Parse tentatively in case there's more after the initial return
7902 statement. */
7903 cp_parser_parse_tentatively (parser);
7905 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7906 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7908 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7910 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7911 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7913 if (cp_parser_parse_definitely (parser))
7915 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7917 compound_stmt = begin_compound_stmt (0);
7918 /* Will get error here if type not deduced yet. */
7919 finish_return_stmt (expr);
7920 finish_compound_stmt (compound_stmt);
7922 done = true;
7926 if (!done)
7928 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7929 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7930 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7931 cp_parser_compound_stmt does not pass it. */
7932 cp_parser_function_body (parser);
7933 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7936 finish_function_body (body);
7937 finish_lambda_scope ();
7939 /* Finish the function and generate code for it if necessary. */
7940 expand_or_defer_fn (finish_function (/*inline*/2));
7943 if (nested)
7944 pop_function_context();
7947 /* Statements [gram.stmt.stmt] */
7949 /* Parse a statement.
7951 statement:
7952 labeled-statement
7953 expression-statement
7954 compound-statement
7955 selection-statement
7956 iteration-statement
7957 jump-statement
7958 declaration-statement
7959 try-block
7961 IN_COMPOUND is true when the statement is nested inside a
7962 cp_parser_compound_statement; this matters for certain pragmas.
7964 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7965 is a (possibly labeled) if statement which is not enclosed in braces
7966 and has an else clause. This is used to implement -Wparentheses. */
7968 static void
7969 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7970 bool in_compound, bool *if_p)
7972 tree statement;
7973 cp_token *token;
7974 location_t statement_location;
7976 restart:
7977 if (if_p != NULL)
7978 *if_p = false;
7979 /* There is no statement yet. */
7980 statement = NULL_TREE;
7981 /* Peek at the next token. */
7982 token = cp_lexer_peek_token (parser->lexer);
7983 /* Remember the location of the first token in the statement. */
7984 statement_location = token->location;
7985 /* If this is a keyword, then that will often determine what kind of
7986 statement we have. */
7987 if (token->type == CPP_KEYWORD)
7989 enum rid keyword = token->keyword;
7991 switch (keyword)
7993 case RID_CASE:
7994 case RID_DEFAULT:
7995 /* Looks like a labeled-statement with a case label.
7996 Parse the label, and then use tail recursion to parse
7997 the statement. */
7998 cp_parser_label_for_labeled_statement (parser);
7999 goto restart;
8001 case RID_IF:
8002 case RID_SWITCH:
8003 statement = cp_parser_selection_statement (parser, if_p);
8004 break;
8006 case RID_WHILE:
8007 case RID_DO:
8008 case RID_FOR:
8009 statement = cp_parser_iteration_statement (parser);
8010 break;
8012 case RID_BREAK:
8013 case RID_CONTINUE:
8014 case RID_RETURN:
8015 case RID_GOTO:
8016 statement = cp_parser_jump_statement (parser);
8017 break;
8019 PLUGIN_STATEMENT
8021 case RID_TRY:
8022 statement = cp_parser_try_block (parser);
8023 break;
8025 case RID_NAMESPACE:
8026 /* This must be a namespace alias definition. */
8027 cp_parser_declaration_statement (parser);
8028 return;
8030 default:
8031 /* It might be a keyword like `int' that can start a
8032 declaration-statement. */
8033 break;
8036 else if (token->type == CPP_NAME)
8038 /* If the next token is a `:', then we are looking at a
8039 labeled-statement. */
8040 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8041 if (token->type == CPP_COLON)
8043 /* Looks like a labeled-statement with an ordinary label.
8044 Parse the label, and then use tail recursion to parse
8045 the statement. */
8046 cp_parser_label_for_labeled_statement (parser);
8047 goto restart;
8050 /* Anything that starts with a `{' must be a compound-statement. */
8051 else if (token->type == CPP_OPEN_BRACE)
8052 statement = cp_parser_compound_statement (parser, NULL, false);
8053 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8054 a statement all its own. */
8055 else if (token->type == CPP_PRAGMA)
8057 /* Only certain OpenMP pragmas are attached to statements, and thus
8058 are considered statements themselves. All others are not. In
8059 the context of a compound, accept the pragma as a "statement" and
8060 return so that we can check for a close brace. Otherwise we
8061 require a real statement and must go back and read one. */
8062 if (in_compound)
8063 cp_parser_pragma (parser, pragma_compound);
8064 else if (!cp_parser_pragma (parser, pragma_stmt))
8065 goto restart;
8066 return;
8068 else if (token->type == CPP_EOF)
8070 cp_parser_error (parser, "expected statement");
8071 return;
8074 /* Everything else must be a declaration-statement or an
8075 expression-statement. Try for the declaration-statement
8076 first, unless we are looking at a `;', in which case we know that
8077 we have an expression-statement. */
8078 if (!statement)
8080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8082 cp_parser_parse_tentatively (parser);
8083 /* Try to parse the declaration-statement. */
8084 cp_parser_declaration_statement (parser);
8085 /* If that worked, we're done. */
8086 if (cp_parser_parse_definitely (parser))
8087 return;
8089 /* Look for an expression-statement instead. */
8090 statement = cp_parser_expression_statement (parser, in_statement_expr);
8093 /* Set the line number for the statement. */
8094 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8095 SET_EXPR_LOCATION (statement, statement_location);
8098 /* Parse the label for a labeled-statement, i.e.
8100 identifier :
8101 case constant-expression :
8102 default :
8104 GNU Extension:
8105 case constant-expression ... constant-expression : statement
8107 When a label is parsed without errors, the label is added to the
8108 parse tree by the finish_* functions, so this function doesn't
8109 have to return the label. */
8111 static void
8112 cp_parser_label_for_labeled_statement (cp_parser* parser)
8114 cp_token *token;
8115 tree label = NULL_TREE;
8116 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8118 /* The next token should be an identifier. */
8119 token = cp_lexer_peek_token (parser->lexer);
8120 if (token->type != CPP_NAME
8121 && token->type != CPP_KEYWORD)
8123 cp_parser_error (parser, "expected labeled-statement");
8124 return;
8127 parser->colon_corrects_to_scope_p = false;
8128 switch (token->keyword)
8130 case RID_CASE:
8132 tree expr, expr_hi;
8133 cp_token *ellipsis;
8135 /* Consume the `case' token. */
8136 cp_lexer_consume_token (parser->lexer);
8137 /* Parse the constant-expression. */
8138 expr = cp_parser_constant_expression (parser,
8139 /*allow_non_constant_p=*/false,
8140 NULL);
8142 ellipsis = cp_lexer_peek_token (parser->lexer);
8143 if (ellipsis->type == CPP_ELLIPSIS)
8145 /* Consume the `...' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 expr_hi =
8148 cp_parser_constant_expression (parser,
8149 /*allow_non_constant_p=*/false,
8150 NULL);
8151 /* We don't need to emit warnings here, as the common code
8152 will do this for us. */
8154 else
8155 expr_hi = NULL_TREE;
8157 if (parser->in_switch_statement_p)
8158 finish_case_label (token->location, expr, expr_hi);
8159 else
8160 error_at (token->location,
8161 "case label %qE not within a switch statement",
8162 expr);
8164 break;
8166 case RID_DEFAULT:
8167 /* Consume the `default' token. */
8168 cp_lexer_consume_token (parser->lexer);
8170 if (parser->in_switch_statement_p)
8171 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8172 else
8173 error_at (token->location, "case label not within a switch statement");
8174 break;
8176 default:
8177 /* Anything else must be an ordinary label. */
8178 label = finish_label_stmt (cp_parser_identifier (parser));
8179 break;
8182 /* Require the `:' token. */
8183 cp_parser_require (parser, CPP_COLON, RT_COLON);
8185 /* An ordinary label may optionally be followed by attributes.
8186 However, this is only permitted if the attributes are then
8187 followed by a semicolon. This is because, for backward
8188 compatibility, when parsing
8189 lab: __attribute__ ((unused)) int i;
8190 we want the attribute to attach to "i", not "lab". */
8191 if (label != NULL_TREE
8192 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8194 tree attrs;
8196 cp_parser_parse_tentatively (parser);
8197 attrs = cp_parser_attributes_opt (parser);
8198 if (attrs == NULL_TREE
8199 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8200 cp_parser_abort_tentative_parse (parser);
8201 else if (!cp_parser_parse_definitely (parser))
8203 else
8204 cplus_decl_attributes (&label, attrs, 0);
8207 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8210 /* Parse an expression-statement.
8212 expression-statement:
8213 expression [opt] ;
8215 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8216 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8217 indicates whether this expression-statement is part of an
8218 expression statement. */
8220 static tree
8221 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8223 tree statement = NULL_TREE;
8224 cp_token *token = cp_lexer_peek_token (parser->lexer);
8226 /* If the next token is a ';', then there is no expression
8227 statement. */
8228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8229 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8231 /* Give a helpful message for "A<T>::type t;" and the like. */
8232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8233 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8235 if (TREE_CODE (statement) == SCOPE_REF)
8236 error_at (token->location, "need %<typename%> before %qE because "
8237 "%qT is a dependent scope",
8238 statement, TREE_OPERAND (statement, 0));
8239 else if (is_overloaded_fn (statement)
8240 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8242 /* A::A a; */
8243 tree fn = get_first_fn (statement);
8244 error_at (token->location,
8245 "%<%T::%D%> names the constructor, not the type",
8246 DECL_CONTEXT (fn), DECL_NAME (fn));
8250 /* Consume the final `;'. */
8251 cp_parser_consume_semicolon_at_end_of_statement (parser);
8253 if (in_statement_expr
8254 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8255 /* This is the final expression statement of a statement
8256 expression. */
8257 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8258 else if (statement)
8259 statement = finish_expr_stmt (statement);
8260 else
8261 finish_stmt ();
8263 return statement;
8266 /* Parse a compound-statement.
8268 compound-statement:
8269 { statement-seq [opt] }
8271 GNU extension:
8273 compound-statement:
8274 { label-declaration-seq [opt] statement-seq [opt] }
8276 label-declaration-seq:
8277 label-declaration
8278 label-declaration-seq label-declaration
8280 Returns a tree representing the statement. */
8282 static tree
8283 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8284 bool in_try)
8286 tree compound_stmt;
8288 /* Consume the `{'. */
8289 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8290 return error_mark_node;
8291 /* Begin the compound-statement. */
8292 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8293 /* If the next keyword is `__label__' we have a label declaration. */
8294 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8295 cp_parser_label_declaration (parser);
8296 /* Parse an (optional) statement-seq. */
8297 cp_parser_statement_seq_opt (parser, in_statement_expr);
8298 /* Finish the compound-statement. */
8299 finish_compound_stmt (compound_stmt);
8300 /* Consume the `}'. */
8301 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8303 return compound_stmt;
8306 /* Parse an (optional) statement-seq.
8308 statement-seq:
8309 statement
8310 statement-seq [opt] statement */
8312 static void
8313 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8315 /* Scan statements until there aren't any more. */
8316 while (true)
8318 cp_token *token = cp_lexer_peek_token (parser->lexer);
8320 /* If we are looking at a `}', then we have run out of
8321 statements; the same is true if we have reached the end
8322 of file, or have stumbled upon a stray '@end'. */
8323 if (token->type == CPP_CLOSE_BRACE
8324 || token->type == CPP_EOF
8325 || token->type == CPP_PRAGMA_EOL
8326 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8327 break;
8329 /* If we are in a compound statement and find 'else' then
8330 something went wrong. */
8331 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8333 if (parser->in_statement & IN_IF_STMT)
8334 break;
8335 else
8337 token = cp_lexer_consume_token (parser->lexer);
8338 error_at (token->location, "%<else%> without a previous %<if%>");
8342 /* Parse the statement. */
8343 cp_parser_statement (parser, in_statement_expr, true, NULL);
8347 /* Parse a selection-statement.
8349 selection-statement:
8350 if ( condition ) statement
8351 if ( condition ) statement else statement
8352 switch ( condition ) statement
8354 Returns the new IF_STMT or SWITCH_STMT.
8356 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8357 is a (possibly labeled) if statement which is not enclosed in
8358 braces and has an else clause. This is used to implement
8359 -Wparentheses. */
8361 static tree
8362 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8364 cp_token *token;
8365 enum rid keyword;
8367 if (if_p != NULL)
8368 *if_p = false;
8370 /* Peek at the next token. */
8371 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8373 /* See what kind of keyword it is. */
8374 keyword = token->keyword;
8375 switch (keyword)
8377 case RID_IF:
8378 case RID_SWITCH:
8380 tree statement;
8381 tree condition;
8383 /* Look for the `('. */
8384 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8386 cp_parser_skip_to_end_of_statement (parser);
8387 return error_mark_node;
8390 /* Begin the selection-statement. */
8391 if (keyword == RID_IF)
8392 statement = begin_if_stmt ();
8393 else
8394 statement = begin_switch_stmt ();
8396 /* Parse the condition. */
8397 condition = cp_parser_condition (parser);
8398 /* Look for the `)'. */
8399 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8400 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8401 /*consume_paren=*/true);
8403 if (keyword == RID_IF)
8405 bool nested_if;
8406 unsigned char in_statement;
8408 /* Add the condition. */
8409 finish_if_stmt_cond (condition, statement);
8411 /* Parse the then-clause. */
8412 in_statement = parser->in_statement;
8413 parser->in_statement |= IN_IF_STMT;
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8417 add_stmt (build_empty_stmt (loc));
8418 cp_lexer_consume_token (parser->lexer);
8419 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8420 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8421 "empty body in an %<if%> statement");
8422 nested_if = false;
8424 else
8425 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8426 parser->in_statement = in_statement;
8428 finish_then_clause (statement);
8430 /* If the next token is `else', parse the else-clause. */
8431 if (cp_lexer_next_token_is_keyword (parser->lexer,
8432 RID_ELSE))
8434 /* Consume the `else' keyword. */
8435 cp_lexer_consume_token (parser->lexer);
8436 begin_else_clause (statement);
8437 /* Parse the else-clause. */
8438 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8440 location_t loc;
8441 loc = cp_lexer_peek_token (parser->lexer)->location;
8442 warning_at (loc,
8443 OPT_Wempty_body, "suggest braces around "
8444 "empty body in an %<else%> statement");
8445 add_stmt (build_empty_stmt (loc));
8446 cp_lexer_consume_token (parser->lexer);
8448 else
8449 cp_parser_implicitly_scoped_statement (parser, NULL);
8451 finish_else_clause (statement);
8453 /* If we are currently parsing a then-clause, then
8454 IF_P will not be NULL. We set it to true to
8455 indicate that this if statement has an else clause.
8456 This may trigger the Wparentheses warning below
8457 when we get back up to the parent if statement. */
8458 if (if_p != NULL)
8459 *if_p = true;
8461 else
8463 /* This if statement does not have an else clause. If
8464 NESTED_IF is true, then the then-clause is an if
8465 statement which does have an else clause. We warn
8466 about the potential ambiguity. */
8467 if (nested_if)
8468 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8469 "suggest explicit braces to avoid ambiguous"
8470 " %<else%>");
8473 /* Now we're all done with the if-statement. */
8474 finish_if_stmt (statement);
8476 else
8478 bool in_switch_statement_p;
8479 unsigned char in_statement;
8481 /* Add the condition. */
8482 finish_switch_cond (condition, statement);
8484 /* Parse the body of the switch-statement. */
8485 in_switch_statement_p = parser->in_switch_statement_p;
8486 in_statement = parser->in_statement;
8487 parser->in_switch_statement_p = true;
8488 parser->in_statement |= IN_SWITCH_STMT;
8489 cp_parser_implicitly_scoped_statement (parser, NULL);
8490 parser->in_switch_statement_p = in_switch_statement_p;
8491 parser->in_statement = in_statement;
8493 /* Now we're all done with the switch-statement. */
8494 finish_switch_stmt (statement);
8497 return statement;
8499 break;
8501 default:
8502 cp_parser_error (parser, "expected selection-statement");
8503 return error_mark_node;
8507 /* Parse a condition.
8509 condition:
8510 expression
8511 type-specifier-seq declarator = initializer-clause
8512 type-specifier-seq declarator braced-init-list
8514 GNU Extension:
8516 condition:
8517 type-specifier-seq declarator asm-specification [opt]
8518 attributes [opt] = assignment-expression
8520 Returns the expression that should be tested. */
8522 static tree
8523 cp_parser_condition (cp_parser* parser)
8525 cp_decl_specifier_seq type_specifiers;
8526 const char *saved_message;
8527 int declares_class_or_enum;
8529 /* Try the declaration first. */
8530 cp_parser_parse_tentatively (parser);
8531 /* New types are not allowed in the type-specifier-seq for a
8532 condition. */
8533 saved_message = parser->type_definition_forbidden_message;
8534 parser->type_definition_forbidden_message
8535 = G_("types may not be defined in conditions");
8536 /* Parse the type-specifier-seq. */
8537 cp_parser_decl_specifier_seq (parser,
8538 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8539 &type_specifiers,
8540 &declares_class_or_enum);
8541 /* Restore the saved message. */
8542 parser->type_definition_forbidden_message = saved_message;
8543 /* If all is well, we might be looking at a declaration. */
8544 if (!cp_parser_error_occurred (parser))
8546 tree decl;
8547 tree asm_specification;
8548 tree attributes;
8549 cp_declarator *declarator;
8550 tree initializer = NULL_TREE;
8552 /* Parse the declarator. */
8553 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8554 /*ctor_dtor_or_conv_p=*/NULL,
8555 /*parenthesized_p=*/NULL,
8556 /*member_p=*/false);
8557 /* Parse the attributes. */
8558 attributes = cp_parser_attributes_opt (parser);
8559 /* Parse the asm-specification. */
8560 asm_specification = cp_parser_asm_specification_opt (parser);
8561 /* If the next token is not an `=' or '{', then we might still be
8562 looking at an expression. For example:
8564 if (A(a).x)
8566 looks like a decl-specifier-seq and a declarator -- but then
8567 there is no `=', so this is an expression. */
8568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8570 cp_parser_simulate_error (parser);
8572 /* If we did see an `=' or '{', then we are looking at a declaration
8573 for sure. */
8574 if (cp_parser_parse_definitely (parser))
8576 tree pushed_scope;
8577 bool non_constant_p;
8578 bool flags = LOOKUP_ONLYCONVERTING;
8580 /* Create the declaration. */
8581 decl = start_decl (declarator, &type_specifiers,
8582 /*initialized_p=*/true,
8583 attributes, /*prefix_attributes=*/NULL_TREE,
8584 &pushed_scope);
8586 /* Parse the initializer. */
8587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8589 initializer = cp_parser_braced_list (parser, &non_constant_p);
8590 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8591 flags = 0;
8593 else
8595 /* Consume the `='. */
8596 cp_parser_require (parser, CPP_EQ, RT_EQ);
8597 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8599 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8600 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8602 if (!non_constant_p)
8603 initializer = fold_non_dependent_expr (initializer);
8605 /* Process the initializer. */
8606 cp_finish_decl (decl,
8607 initializer, !non_constant_p,
8608 asm_specification,
8609 flags);
8611 if (pushed_scope)
8612 pop_scope (pushed_scope);
8614 return convert_from_reference (decl);
8617 /* If we didn't even get past the declarator successfully, we are
8618 definitely not looking at a declaration. */
8619 else
8620 cp_parser_abort_tentative_parse (parser);
8622 /* Otherwise, we are looking at an expression. */
8623 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8626 /* Parses a for-statement or range-for-statement until the closing ')',
8627 not included. */
8629 static tree
8630 cp_parser_for (cp_parser *parser)
8632 tree init, scope, decl;
8633 bool is_range_for;
8635 /* Begin the for-statement. */
8636 scope = begin_for_scope (&init);
8638 /* Parse the initialization. */
8639 is_range_for = cp_parser_for_init_statement (parser, &decl);
8641 if (is_range_for)
8642 return cp_parser_range_for (parser, scope, init, decl);
8643 else
8644 return cp_parser_c_for (parser, scope, init);
8647 static tree
8648 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8650 /* Normal for loop */
8651 tree condition = NULL_TREE;
8652 tree expression = NULL_TREE;
8653 tree stmt;
8655 stmt = begin_for_stmt (scope, init);
8656 /* The for-init-statement has already been parsed in
8657 cp_parser_for_init_statement, so no work is needed here. */
8658 finish_for_init_stmt (stmt);
8660 /* If there's a condition, process it. */
8661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8662 condition = cp_parser_condition (parser);
8663 finish_for_cond (condition, stmt);
8664 /* Look for the `;'. */
8665 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8667 /* If there's an expression, process it. */
8668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8669 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8670 finish_for_expr (expression, stmt);
8672 return stmt;
8675 /* Tries to parse a range-based for-statement:
8677 range-based-for:
8678 decl-specifier-seq declarator : expression
8680 The decl-specifier-seq declarator and the `:' are already parsed by
8681 cp_parser_for_init_statement. If processing_template_decl it returns a
8682 newly created RANGE_FOR_STMT; if not, it is converted to a
8683 regular FOR_STMT. */
8685 static tree
8686 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8688 tree stmt, range_expr;
8690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8692 bool expr_non_constant_p;
8693 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8695 else
8696 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8698 /* If in template, STMT is converted to a normal for-statement
8699 at instantiation. If not, it is done just ahead. */
8700 if (processing_template_decl)
8702 stmt = begin_range_for_stmt (scope, init);
8703 finish_range_for_decl (stmt, range_decl, range_expr);
8705 else
8707 stmt = begin_for_stmt (scope, init);
8708 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8710 return stmt;
8713 /* Converts a range-based for-statement into a normal
8714 for-statement, as per the definition.
8716 for (RANGE_DECL : RANGE_EXPR)
8717 BLOCK
8719 should be equivalent to:
8722 auto &&__range = RANGE_EXPR;
8723 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8724 __begin != __end;
8725 ++__begin)
8727 RANGE_DECL = *__begin;
8728 BLOCK
8732 If RANGE_EXPR is an array:
8733 BEGIN_EXPR = __range
8734 END_EXPR = __range + ARRAY_SIZE(__range)
8735 Else:
8736 BEGIN_EXPR = begin(__range)
8737 END_EXPR = end(__range);
8739 When calling begin()/end() we must use argument dependent
8740 lookup, but always considering 'std' as an associated namespace. */
8742 tree
8743 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8745 tree range_type, range_temp;
8746 tree begin, end;
8747 tree iter_type, begin_expr, end_expr;
8748 tree condition, expression;
8750 if (range_decl == error_mark_node || range_expr == error_mark_node)
8751 /* If an error happened previously do nothing or else a lot of
8752 unhelpful errors would be issued. */
8753 begin_expr = end_expr = iter_type = error_mark_node;
8754 else
8756 /* Find out the type deduced by the declaration
8757 * `auto &&__range = range_expr' */
8758 range_type = cp_build_reference_type (make_auto (), true);
8759 range_type = do_auto_deduction (range_type, range_expr,
8760 type_uses_auto (range_type));
8762 /* Create the __range variable */
8763 range_temp = build_decl (input_location, VAR_DECL,
8764 get_identifier ("__for_range"), range_type);
8765 TREE_USED (range_temp) = 1;
8766 DECL_ARTIFICIAL (range_temp) = 1;
8767 pushdecl (range_temp);
8768 cp_finish_decl (range_temp, range_expr,
8769 /*is_constant_init*/false, NULL_TREE,
8770 LOOKUP_ONLYCONVERTING);
8772 range_temp = convert_from_reference (range_temp);
8774 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8776 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8777 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8778 begin_expr = range_temp;
8779 end_expr
8780 = build_binary_op (input_location, PLUS_EXPR,
8781 range_temp,
8782 array_type_nelts_top (TREE_TYPE (range_temp)),
8785 else
8787 /* If it is not an array, we must call begin(__range)/end__range() */
8788 VEC(tree,gc) *vec;
8790 begin_expr = get_identifier ("begin");
8791 vec = make_tree_vector ();
8792 VEC_safe_push (tree, gc, vec, range_temp);
8793 begin_expr = perform_koenig_lookup (begin_expr, vec,
8794 /*include_std=*/true);
8795 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8796 tf_warning_or_error);
8797 release_tree_vector (vec);
8799 end_expr = get_identifier ("end");
8800 vec = make_tree_vector ();
8801 VEC_safe_push (tree, gc, vec, range_temp);
8802 end_expr = perform_koenig_lookup (end_expr, vec,
8803 /*include_std=*/true);
8804 end_expr = finish_call_expr (end_expr, &vec, false, true,
8805 tf_warning_or_error);
8806 release_tree_vector (vec);
8808 /* The unqualified type of the __begin and __end temporaries should
8809 * be the same as required by the multiple auto declaration */
8810 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8811 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8812 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8813 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8817 /* The new for initialization statement */
8818 begin = build_decl (input_location, VAR_DECL,
8819 get_identifier ("__for_begin"), iter_type);
8820 TREE_USED (begin) = 1;
8821 DECL_ARTIFICIAL (begin) = 1;
8822 pushdecl (begin);
8823 cp_finish_decl (begin, begin_expr,
8824 /*is_constant_init*/false, NULL_TREE,
8825 LOOKUP_ONLYCONVERTING);
8827 end = build_decl (input_location, VAR_DECL,
8828 get_identifier ("__for_end"), iter_type);
8829 TREE_USED (end) = 1;
8830 DECL_ARTIFICIAL (end) = 1;
8831 pushdecl (end);
8832 cp_finish_decl (end, end_expr,
8833 /*is_constant_init*/false, NULL_TREE,
8834 LOOKUP_ONLYCONVERTING);
8836 finish_for_init_stmt (statement);
8838 /* The new for condition */
8839 condition = build_x_binary_op (NE_EXPR,
8840 begin, ERROR_MARK,
8841 end, ERROR_MARK,
8842 NULL, tf_warning_or_error);
8843 finish_for_cond (condition, statement);
8845 /* The new increment expression */
8846 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8847 finish_for_expr (expression, statement);
8849 /* The declaration is initialized with *__begin inside the loop body */
8850 cp_finish_decl (range_decl,
8851 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8852 /*is_constant_init*/false, NULL_TREE,
8853 LOOKUP_ONLYCONVERTING);
8855 return statement;
8859 /* Parse an iteration-statement.
8861 iteration-statement:
8862 while ( condition ) statement
8863 do statement while ( expression ) ;
8864 for ( for-init-statement condition [opt] ; expression [opt] )
8865 statement
8867 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8869 static tree
8870 cp_parser_iteration_statement (cp_parser* parser)
8872 cp_token *token;
8873 enum rid keyword;
8874 tree statement;
8875 unsigned char in_statement;
8877 /* Peek at the next token. */
8878 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8879 if (!token)
8880 return error_mark_node;
8882 /* Remember whether or not we are already within an iteration
8883 statement. */
8884 in_statement = parser->in_statement;
8886 /* See what kind of keyword it is. */
8887 keyword = token->keyword;
8888 switch (keyword)
8890 case RID_WHILE:
8892 tree condition;
8894 /* Begin the while-statement. */
8895 statement = begin_while_stmt ();
8896 /* Look for the `('. */
8897 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8898 /* Parse the condition. */
8899 condition = cp_parser_condition (parser);
8900 finish_while_stmt_cond (condition, statement);
8901 /* Look for the `)'. */
8902 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8903 /* Parse the dependent statement. */
8904 parser->in_statement = IN_ITERATION_STMT;
8905 cp_parser_already_scoped_statement (parser);
8906 parser->in_statement = in_statement;
8907 /* We're done with the while-statement. */
8908 finish_while_stmt (statement);
8910 break;
8912 case RID_DO:
8914 tree expression;
8916 /* Begin the do-statement. */
8917 statement = begin_do_stmt ();
8918 /* Parse the body of the do-statement. */
8919 parser->in_statement = IN_ITERATION_STMT;
8920 cp_parser_implicitly_scoped_statement (parser, NULL);
8921 parser->in_statement = in_statement;
8922 finish_do_body (statement);
8923 /* Look for the `while' keyword. */
8924 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8925 /* Look for the `('. */
8926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8927 /* Parse the expression. */
8928 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8929 /* We're done with the do-statement. */
8930 finish_do_stmt (expression, statement);
8931 /* Look for the `)'. */
8932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8933 /* Look for the `;'. */
8934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8936 break;
8938 case RID_FOR:
8940 /* Look for the `('. */
8941 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8943 statement = cp_parser_for (parser);
8945 /* Look for the `)'. */
8946 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8948 /* Parse the body of the for-statement. */
8949 parser->in_statement = IN_ITERATION_STMT;
8950 cp_parser_already_scoped_statement (parser);
8951 parser->in_statement = in_statement;
8953 /* We're done with the for-statement. */
8954 finish_for_stmt (statement);
8956 break;
8958 default:
8959 cp_parser_error (parser, "expected iteration-statement");
8960 statement = error_mark_node;
8961 break;
8964 return statement;
8967 /* Parse a for-init-statement or the declarator of a range-based-for.
8968 Returns true if a range-based-for declaration is seen.
8970 for-init-statement:
8971 expression-statement
8972 simple-declaration */
8974 static bool
8975 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8977 /* If the next token is a `;', then we have an empty
8978 expression-statement. Grammatically, this is also a
8979 simple-declaration, but an invalid one, because it does not
8980 declare anything. Therefore, if we did not handle this case
8981 specially, we would issue an error message about an invalid
8982 declaration. */
8983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8985 bool is_range_for = false;
8986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8988 parser->colon_corrects_to_scope_p = false;
8990 /* We're going to speculatively look for a declaration, falling back
8991 to an expression, if necessary. */
8992 cp_parser_parse_tentatively (parser);
8993 /* Parse the declaration. */
8994 cp_parser_simple_declaration (parser,
8995 /*function_definition_allowed_p=*/false,
8996 decl);
8997 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8998 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9000 /* It is a range-for, consume the ':' */
9001 cp_lexer_consume_token (parser->lexer);
9002 is_range_for = true;
9003 if (cxx_dialect < cxx0x)
9005 error_at (cp_lexer_peek_token (parser->lexer)->location,
9006 "range-based-for loops are not allowed "
9007 "in C++98 mode");
9008 *decl = error_mark_node;
9011 else
9012 /* The ';' is not consumed yet because we told
9013 cp_parser_simple_declaration not to. */
9014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9016 if (cp_parser_parse_definitely (parser))
9017 return is_range_for;
9018 /* If the tentative parse failed, then we shall need to look for an
9019 expression-statement. */
9021 /* If we are here, it is an expression-statement. */
9022 cp_parser_expression_statement (parser, NULL_TREE);
9023 return false;
9026 /* Parse a jump-statement.
9028 jump-statement:
9029 break ;
9030 continue ;
9031 return expression [opt] ;
9032 return braced-init-list ;
9033 goto identifier ;
9035 GNU extension:
9037 jump-statement:
9038 goto * expression ;
9040 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9042 static tree
9043 cp_parser_jump_statement (cp_parser* parser)
9045 tree statement = error_mark_node;
9046 cp_token *token;
9047 enum rid keyword;
9048 unsigned char in_statement;
9050 /* Peek at the next token. */
9051 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9052 if (!token)
9053 return error_mark_node;
9055 /* See what kind of keyword it is. */
9056 keyword = token->keyword;
9057 switch (keyword)
9059 case RID_BREAK:
9060 in_statement = parser->in_statement & ~IN_IF_STMT;
9061 switch (in_statement)
9063 case 0:
9064 error_at (token->location, "break statement not within loop or switch");
9065 break;
9066 default:
9067 gcc_assert ((in_statement & IN_SWITCH_STMT)
9068 || in_statement == IN_ITERATION_STMT);
9069 statement = finish_break_stmt ();
9070 break;
9071 case IN_OMP_BLOCK:
9072 error_at (token->location, "invalid exit from OpenMP structured block");
9073 break;
9074 case IN_OMP_FOR:
9075 error_at (token->location, "break statement used with OpenMP for loop");
9076 break;
9078 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9079 break;
9081 case RID_CONTINUE:
9082 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9084 case 0:
9085 error_at (token->location, "continue statement not within a loop");
9086 break;
9087 case IN_ITERATION_STMT:
9088 case IN_OMP_FOR:
9089 statement = finish_continue_stmt ();
9090 break;
9091 case IN_OMP_BLOCK:
9092 error_at (token->location, "invalid exit from OpenMP structured block");
9093 break;
9094 default:
9095 gcc_unreachable ();
9097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9098 break;
9100 case RID_RETURN:
9102 tree expr;
9103 bool expr_non_constant_p;
9105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9107 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9108 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9110 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9111 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9112 else
9113 /* If the next token is a `;', then there is no
9114 expression. */
9115 expr = NULL_TREE;
9116 /* Build the return-statement. */
9117 statement = finish_return_stmt (expr);
9118 /* Look for the final `;'. */
9119 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9121 break;
9123 case RID_GOTO:
9124 /* Create the goto-statement. */
9125 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9127 /* Issue a warning about this use of a GNU extension. */
9128 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9129 /* Consume the '*' token. */
9130 cp_lexer_consume_token (parser->lexer);
9131 /* Parse the dependent expression. */
9132 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9134 else
9135 finish_goto_stmt (cp_parser_identifier (parser));
9136 /* Look for the final `;'. */
9137 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9138 break;
9140 default:
9141 cp_parser_error (parser, "expected jump-statement");
9142 break;
9145 return statement;
9148 /* Parse a declaration-statement.
9150 declaration-statement:
9151 block-declaration */
9153 static void
9154 cp_parser_declaration_statement (cp_parser* parser)
9156 void *p;
9158 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9159 p = obstack_alloc (&declarator_obstack, 0);
9161 /* Parse the block-declaration. */
9162 cp_parser_block_declaration (parser, /*statement_p=*/true);
9164 /* Free any declarators allocated. */
9165 obstack_free (&declarator_obstack, p);
9167 /* Finish off the statement. */
9168 finish_stmt ();
9171 /* Some dependent statements (like `if (cond) statement'), are
9172 implicitly in their own scope. In other words, if the statement is
9173 a single statement (as opposed to a compound-statement), it is
9174 none-the-less treated as if it were enclosed in braces. Any
9175 declarations appearing in the dependent statement are out of scope
9176 after control passes that point. This function parses a statement,
9177 but ensures that is in its own scope, even if it is not a
9178 compound-statement.
9180 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9181 is a (possibly labeled) if statement which is not enclosed in
9182 braces and has an else clause. This is used to implement
9183 -Wparentheses.
9185 Returns the new statement. */
9187 static tree
9188 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9190 tree statement;
9192 if (if_p != NULL)
9193 *if_p = false;
9195 /* Mark if () ; with a special NOP_EXPR. */
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9199 cp_lexer_consume_token (parser->lexer);
9200 statement = add_stmt (build_empty_stmt (loc));
9202 /* if a compound is opened, we simply parse the statement directly. */
9203 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9204 statement = cp_parser_compound_statement (parser, NULL, false);
9205 /* If the token is not a `{', then we must take special action. */
9206 else
9208 /* Create a compound-statement. */
9209 statement = begin_compound_stmt (0);
9210 /* Parse the dependent-statement. */
9211 cp_parser_statement (parser, NULL_TREE, false, if_p);
9212 /* Finish the dummy compound-statement. */
9213 finish_compound_stmt (statement);
9216 /* Return the statement. */
9217 return statement;
9220 /* For some dependent statements (like `while (cond) statement'), we
9221 have already created a scope. Therefore, even if the dependent
9222 statement is a compound-statement, we do not want to create another
9223 scope. */
9225 static void
9226 cp_parser_already_scoped_statement (cp_parser* parser)
9228 /* If the token is a `{', then we must take special action. */
9229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9230 cp_parser_statement (parser, NULL_TREE, false, NULL);
9231 else
9233 /* Avoid calling cp_parser_compound_statement, so that we
9234 don't create a new scope. Do everything else by hand. */
9235 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9236 /* If the next keyword is `__label__' we have a label declaration. */
9237 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9238 cp_parser_label_declaration (parser);
9239 /* Parse an (optional) statement-seq. */
9240 cp_parser_statement_seq_opt (parser, NULL_TREE);
9241 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9245 /* Declarations [gram.dcl.dcl] */
9247 /* Parse an optional declaration-sequence.
9249 declaration-seq:
9250 declaration
9251 declaration-seq declaration */
9253 static void
9254 cp_parser_declaration_seq_opt (cp_parser* parser)
9256 while (true)
9258 cp_token *token;
9260 token = cp_lexer_peek_token (parser->lexer);
9262 if (token->type == CPP_CLOSE_BRACE
9263 || token->type == CPP_EOF
9264 || token->type == CPP_PRAGMA_EOL)
9265 break;
9267 if (token->type == CPP_SEMICOLON)
9269 /* A declaration consisting of a single semicolon is
9270 invalid. Allow it unless we're being pedantic. */
9271 cp_lexer_consume_token (parser->lexer);
9272 if (!in_system_header)
9273 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9274 continue;
9277 /* If we're entering or exiting a region that's implicitly
9278 extern "C", modify the lang context appropriately. */
9279 if (!parser->implicit_extern_c && token->implicit_extern_c)
9281 push_lang_context (lang_name_c);
9282 parser->implicit_extern_c = true;
9284 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9286 pop_lang_context ();
9287 parser->implicit_extern_c = false;
9290 if (token->type == CPP_PRAGMA)
9292 /* A top-level declaration can consist solely of a #pragma.
9293 A nested declaration cannot, so this is done here and not
9294 in cp_parser_declaration. (A #pragma at block scope is
9295 handled in cp_parser_statement.) */
9296 cp_parser_pragma (parser, pragma_external);
9297 continue;
9300 /* Parse the declaration itself. */
9301 cp_parser_declaration (parser);
9305 /* Parse a declaration.
9307 declaration:
9308 block-declaration
9309 function-definition
9310 template-declaration
9311 explicit-instantiation
9312 explicit-specialization
9313 linkage-specification
9314 namespace-definition
9316 GNU extension:
9318 declaration:
9319 __extension__ declaration */
9321 static void
9322 cp_parser_declaration (cp_parser* parser)
9324 cp_token token1;
9325 cp_token token2;
9326 int saved_pedantic;
9327 void *p;
9329 /* Check for the `__extension__' keyword. */
9330 if (cp_parser_extension_opt (parser, &saved_pedantic))
9332 /* Parse the qualified declaration. */
9333 cp_parser_declaration (parser);
9334 /* Restore the PEDANTIC flag. */
9335 pedantic = saved_pedantic;
9337 return;
9340 /* Try to figure out what kind of declaration is present. */
9341 token1 = *cp_lexer_peek_token (parser->lexer);
9343 if (token1.type != CPP_EOF)
9344 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9345 else
9347 token2.type = CPP_EOF;
9348 token2.keyword = RID_MAX;
9351 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9352 p = obstack_alloc (&declarator_obstack, 0);
9354 /* If the next token is `extern' and the following token is a string
9355 literal, then we have a linkage specification. */
9356 if (token1.keyword == RID_EXTERN
9357 && cp_parser_is_string_literal (&token2))
9358 cp_parser_linkage_specification (parser);
9359 /* If the next token is `template', then we have either a template
9360 declaration, an explicit instantiation, or an explicit
9361 specialization. */
9362 else if (token1.keyword == RID_TEMPLATE)
9364 /* `template <>' indicates a template specialization. */
9365 if (token2.type == CPP_LESS
9366 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9367 cp_parser_explicit_specialization (parser);
9368 /* `template <' indicates a template declaration. */
9369 else if (token2.type == CPP_LESS)
9370 cp_parser_template_declaration (parser, /*member_p=*/false);
9371 /* Anything else must be an explicit instantiation. */
9372 else
9373 cp_parser_explicit_instantiation (parser);
9375 /* If the next token is `export', then we have a template
9376 declaration. */
9377 else if (token1.keyword == RID_EXPORT)
9378 cp_parser_template_declaration (parser, /*member_p=*/false);
9379 /* If the next token is `extern', 'static' or 'inline' and the one
9380 after that is `template', we have a GNU extended explicit
9381 instantiation directive. */
9382 else if (cp_parser_allow_gnu_extensions_p (parser)
9383 && (token1.keyword == RID_EXTERN
9384 || token1.keyword == RID_STATIC
9385 || token1.keyword == RID_INLINE)
9386 && token2.keyword == RID_TEMPLATE)
9387 cp_parser_explicit_instantiation (parser);
9388 /* If the next token is `namespace', check for a named or unnamed
9389 namespace definition. */
9390 else if (token1.keyword == RID_NAMESPACE
9391 && (/* A named namespace definition. */
9392 (token2.type == CPP_NAME
9393 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9394 != CPP_EQ))
9395 /* An unnamed namespace definition. */
9396 || token2.type == CPP_OPEN_BRACE
9397 || token2.keyword == RID_ATTRIBUTE))
9398 cp_parser_namespace_definition (parser);
9399 /* An inline (associated) namespace definition. */
9400 else if (token1.keyword == RID_INLINE
9401 && token2.keyword == RID_NAMESPACE)
9402 cp_parser_namespace_definition (parser);
9403 PLUGIN_DECLARATION
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,
9504 /*maybe_range_for_decl*/NULL);
9507 /* Parse a simple-declaration.
9509 simple-declaration:
9510 decl-specifier-seq [opt] init-declarator-list [opt] ;
9512 init-declarator-list:
9513 init-declarator
9514 init-declarator-list , init-declarator
9516 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9517 function-definition as a simple-declaration.
9519 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9520 parsed declaration if it is an uninitialized single declarator not followed
9521 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9522 if present, will not be consumed. */
9524 static void
9525 cp_parser_simple_declaration (cp_parser* parser,
9526 bool function_definition_allowed_p,
9527 tree *maybe_range_for_decl)
9529 cp_decl_specifier_seq decl_specifiers;
9530 int declares_class_or_enum;
9531 bool saw_declarator;
9533 if (maybe_range_for_decl)
9534 *maybe_range_for_decl = NULL_TREE;
9536 /* Defer access checks until we know what is being declared; the
9537 checks for names appearing in the decl-specifier-seq should be
9538 done as if we were in the scope of the thing being declared. */
9539 push_deferring_access_checks (dk_deferred);
9541 /* Parse the decl-specifier-seq. We have to keep track of whether
9542 or not the decl-specifier-seq declares a named class or
9543 enumeration type, since that is the only case in which the
9544 init-declarator-list is allowed to be empty.
9546 [dcl.dcl]
9548 In a simple-declaration, the optional init-declarator-list can be
9549 omitted only when declaring a class or enumeration, that is when
9550 the decl-specifier-seq contains either a class-specifier, an
9551 elaborated-type-specifier, or an enum-specifier. */
9552 cp_parser_decl_specifier_seq (parser,
9553 CP_PARSER_FLAGS_OPTIONAL,
9554 &decl_specifiers,
9555 &declares_class_or_enum);
9556 /* We no longer need to defer access checks. */
9557 stop_deferring_access_checks ();
9559 /* In a block scope, a valid declaration must always have a
9560 decl-specifier-seq. By not trying to parse declarators, we can
9561 resolve the declaration/expression ambiguity more quickly. */
9562 if (!function_definition_allowed_p
9563 && !decl_specifiers.any_specifiers_p)
9565 cp_parser_error (parser, "expected declaration");
9566 goto done;
9569 /* If the next two tokens are both identifiers, the code is
9570 erroneous. The usual cause of this situation is code like:
9572 T t;
9574 where "T" should name a type -- but does not. */
9575 if (!decl_specifiers.any_type_specifiers_p
9576 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9578 /* If parsing tentatively, we should commit; we really are
9579 looking at a declaration. */
9580 cp_parser_commit_to_tentative_parse (parser);
9581 /* Give up. */
9582 goto done;
9585 /* If we have seen at least one decl-specifier, and the next token
9586 is not a parenthesis, then we must be looking at a declaration.
9587 (After "int (" we might be looking at a functional cast.) */
9588 if (decl_specifiers.any_specifiers_p
9589 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9590 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9591 && !cp_parser_error_occurred (parser))
9592 cp_parser_commit_to_tentative_parse (parser);
9594 /* Keep going until we hit the `;' at the end of the simple
9595 declaration. */
9596 saw_declarator = false;
9597 while (cp_lexer_next_token_is_not (parser->lexer,
9598 CPP_SEMICOLON))
9600 cp_token *token;
9601 bool function_definition_p;
9602 tree decl;
9604 if (saw_declarator)
9606 /* If we are processing next declarator, coma is expected */
9607 token = cp_lexer_peek_token (parser->lexer);
9608 gcc_assert (token->type == CPP_COMMA);
9609 cp_lexer_consume_token (parser->lexer);
9610 if (maybe_range_for_decl)
9611 *maybe_range_for_decl = error_mark_node;
9613 else
9614 saw_declarator = true;
9616 /* Parse the init-declarator. */
9617 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9618 /*checks=*/NULL,
9619 function_definition_allowed_p,
9620 /*member_p=*/false,
9621 declares_class_or_enum,
9622 &function_definition_p,
9623 maybe_range_for_decl);
9624 /* If an error occurred while parsing tentatively, exit quickly.
9625 (That usually happens when in the body of a function; each
9626 statement is treated as a declaration-statement until proven
9627 otherwise.) */
9628 if (cp_parser_error_occurred (parser))
9629 goto done;
9630 /* Handle function definitions specially. */
9631 if (function_definition_p)
9633 /* If the next token is a `,', then we are probably
9634 processing something like:
9636 void f() {}, *p;
9638 which is erroneous. */
9639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9641 cp_token *token = cp_lexer_peek_token (parser->lexer);
9642 error_at (token->location,
9643 "mixing"
9644 " declarations and function-definitions is forbidden");
9646 /* Otherwise, we're done with the list of declarators. */
9647 else
9649 pop_deferring_access_checks ();
9650 return;
9653 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9654 *maybe_range_for_decl = decl;
9655 /* The next token should be either a `,' or a `;'. */
9656 token = cp_lexer_peek_token (parser->lexer);
9657 /* If it's a `,', there are more declarators to come. */
9658 if (token->type == CPP_COMMA)
9659 /* will be consumed next time around */;
9660 /* If it's a `;', we are done. */
9661 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9662 break;
9663 /* Anything else is an error. */
9664 else
9666 /* If we have already issued an error message we don't need
9667 to issue another one. */
9668 if (decl != error_mark_node
9669 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9670 cp_parser_error (parser, "expected %<,%> or %<;%>");
9671 /* Skip tokens until we reach the end of the statement. */
9672 cp_parser_skip_to_end_of_statement (parser);
9673 /* If the next token is now a `;', consume it. */
9674 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9675 cp_lexer_consume_token (parser->lexer);
9676 goto done;
9678 /* After the first time around, a function-definition is not
9679 allowed -- even if it was OK at first. For example:
9681 int i, f() {}
9683 is not valid. */
9684 function_definition_allowed_p = false;
9687 /* Issue an error message if no declarators are present, and the
9688 decl-specifier-seq does not itself declare a class or
9689 enumeration. */
9690 if (!saw_declarator)
9692 if (cp_parser_declares_only_class_p (parser))
9693 shadow_tag (&decl_specifiers);
9694 /* Perform any deferred access checks. */
9695 perform_deferred_access_checks ();
9698 /* Consume the `;'. */
9699 if (!maybe_range_for_decl)
9700 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9702 done:
9703 pop_deferring_access_checks ();
9706 /* Parse a decl-specifier-seq.
9708 decl-specifier-seq:
9709 decl-specifier-seq [opt] decl-specifier
9711 decl-specifier:
9712 storage-class-specifier
9713 type-specifier
9714 function-specifier
9715 friend
9716 typedef
9718 GNU Extension:
9720 decl-specifier:
9721 attributes
9723 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9725 The parser flags FLAGS is used to control type-specifier parsing.
9727 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9728 flags:
9730 1: one of the decl-specifiers is an elaborated-type-specifier
9731 (i.e., a type declaration)
9732 2: one of the decl-specifiers is an enum-specifier or a
9733 class-specifier (i.e., a type definition)
9737 static void
9738 cp_parser_decl_specifier_seq (cp_parser* parser,
9739 cp_parser_flags flags,
9740 cp_decl_specifier_seq *decl_specs,
9741 int* declares_class_or_enum)
9743 bool constructor_possible_p = !parser->in_declarator_p;
9744 cp_token *start_token = NULL;
9746 /* Clear DECL_SPECS. */
9747 clear_decl_specs (decl_specs);
9749 /* Assume no class or enumeration type is declared. */
9750 *declares_class_or_enum = 0;
9752 /* Keep reading specifiers until there are no more to read. */
9753 while (true)
9755 bool constructor_p;
9756 bool found_decl_spec;
9757 cp_token *token;
9759 /* Peek at the next token. */
9760 token = cp_lexer_peek_token (parser->lexer);
9762 /* Save the first token of the decl spec list for error
9763 reporting. */
9764 if (!start_token)
9765 start_token = token;
9766 /* Handle attributes. */
9767 if (token->keyword == RID_ATTRIBUTE)
9769 /* Parse the attributes. */
9770 decl_specs->attributes
9771 = chainon (decl_specs->attributes,
9772 cp_parser_attributes_opt (parser));
9773 continue;
9775 /* Assume we will find a decl-specifier keyword. */
9776 found_decl_spec = true;
9777 /* If the next token is an appropriate keyword, we can simply
9778 add it to the list. */
9779 switch (token->keyword)
9781 /* decl-specifier:
9782 friend
9783 constexpr */
9784 case RID_FRIEND:
9785 if (!at_class_scope_p ())
9787 error_at (token->location, "%<friend%> used outside of class");
9788 cp_lexer_purge_token (parser->lexer);
9790 else
9792 ++decl_specs->specs[(int) ds_friend];
9793 /* Consume the token. */
9794 cp_lexer_consume_token (parser->lexer);
9796 break;
9798 case RID_CONSTEXPR:
9799 ++decl_specs->specs[(int) ds_constexpr];
9800 cp_lexer_consume_token (parser->lexer);
9801 break;
9803 /* function-specifier:
9804 inline
9805 virtual
9806 explicit */
9807 case RID_INLINE:
9808 case RID_VIRTUAL:
9809 case RID_EXPLICIT:
9810 cp_parser_function_specifier_opt (parser, decl_specs);
9811 break;
9813 /* decl-specifier:
9814 typedef */
9815 case RID_TYPEDEF:
9816 ++decl_specs->specs[(int) ds_typedef];
9817 /* Consume the token. */
9818 cp_lexer_consume_token (parser->lexer);
9819 /* A constructor declarator cannot appear in a typedef. */
9820 constructor_possible_p = false;
9821 /* The "typedef" keyword can only occur in a declaration; we
9822 may as well commit at this point. */
9823 cp_parser_commit_to_tentative_parse (parser);
9825 if (decl_specs->storage_class != sc_none)
9826 decl_specs->conflicting_specifiers_p = true;
9827 break;
9829 /* storage-class-specifier:
9830 auto
9831 register
9832 static
9833 extern
9834 mutable
9836 GNU Extension:
9837 thread */
9838 case RID_AUTO:
9839 if (cxx_dialect == cxx98)
9841 /* Consume the token. */
9842 cp_lexer_consume_token (parser->lexer);
9844 /* Complain about `auto' as a storage specifier, if
9845 we're complaining about C++0x compatibility. */
9846 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9847 " will change meaning in C++0x; please remove it");
9849 /* Set the storage class anyway. */
9850 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9851 token->location);
9853 else
9854 /* C++0x auto type-specifier. */
9855 found_decl_spec = false;
9856 break;
9858 case RID_REGISTER:
9859 case RID_STATIC:
9860 case RID_EXTERN:
9861 case RID_MUTABLE:
9862 /* Consume the token. */
9863 cp_lexer_consume_token (parser->lexer);
9864 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9865 token->location);
9866 break;
9867 case RID_THREAD:
9868 /* Consume the token. */
9869 cp_lexer_consume_token (parser->lexer);
9870 ++decl_specs->specs[(int) ds_thread];
9871 break;
9873 default:
9874 /* We did not yet find a decl-specifier yet. */
9875 found_decl_spec = false;
9876 break;
9879 if (found_decl_spec
9880 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9881 && token->keyword != RID_CONSTEXPR)
9882 error ("decl-specifier invalid in condition");
9884 /* Constructors are a special case. The `S' in `S()' is not a
9885 decl-specifier; it is the beginning of the declarator. */
9886 constructor_p
9887 = (!found_decl_spec
9888 && constructor_possible_p
9889 && (cp_parser_constructor_declarator_p
9890 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9892 /* If we don't have a DECL_SPEC yet, then we must be looking at
9893 a type-specifier. */
9894 if (!found_decl_spec && !constructor_p)
9896 int decl_spec_declares_class_or_enum;
9897 bool is_cv_qualifier;
9898 tree type_spec;
9900 type_spec
9901 = cp_parser_type_specifier (parser, flags,
9902 decl_specs,
9903 /*is_declaration=*/true,
9904 &decl_spec_declares_class_or_enum,
9905 &is_cv_qualifier);
9906 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9908 /* If this type-specifier referenced a user-defined type
9909 (a typedef, class-name, etc.), then we can't allow any
9910 more such type-specifiers henceforth.
9912 [dcl.spec]
9914 The longest sequence of decl-specifiers that could
9915 possibly be a type name is taken as the
9916 decl-specifier-seq of a declaration. The sequence shall
9917 be self-consistent as described below.
9919 [dcl.type]
9921 As a general rule, at most one type-specifier is allowed
9922 in the complete decl-specifier-seq of a declaration. The
9923 only exceptions are the following:
9925 -- const or volatile can be combined with any other
9926 type-specifier.
9928 -- signed or unsigned can be combined with char, long,
9929 short, or int.
9931 -- ..
9933 Example:
9935 typedef char* Pc;
9936 void g (const int Pc);
9938 Here, Pc is *not* part of the decl-specifier seq; it's
9939 the declarator. Therefore, once we see a type-specifier
9940 (other than a cv-qualifier), we forbid any additional
9941 user-defined types. We *do* still allow things like `int
9942 int' to be considered a decl-specifier-seq, and issue the
9943 error message later. */
9944 if (type_spec && !is_cv_qualifier)
9945 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9946 /* A constructor declarator cannot follow a type-specifier. */
9947 if (type_spec)
9949 constructor_possible_p = false;
9950 found_decl_spec = true;
9951 if (!is_cv_qualifier)
9952 decl_specs->any_type_specifiers_p = true;
9956 /* If we still do not have a DECL_SPEC, then there are no more
9957 decl-specifiers. */
9958 if (!found_decl_spec)
9959 break;
9961 decl_specs->any_specifiers_p = true;
9962 /* After we see one decl-specifier, further decl-specifiers are
9963 always optional. */
9964 flags |= CP_PARSER_FLAGS_OPTIONAL;
9967 cp_parser_check_decl_spec (decl_specs, start_token->location);
9969 /* Don't allow a friend specifier with a class definition. */
9970 if (decl_specs->specs[(int) ds_friend] != 0
9971 && (*declares_class_or_enum & 2))
9972 error_at (start_token->location,
9973 "class definition may not be declared a friend");
9976 /* Parse an (optional) storage-class-specifier.
9978 storage-class-specifier:
9979 auto
9980 register
9981 static
9982 extern
9983 mutable
9985 GNU Extension:
9987 storage-class-specifier:
9988 thread
9990 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9992 static tree
9993 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9995 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9997 case RID_AUTO:
9998 if (cxx_dialect != cxx98)
9999 return NULL_TREE;
10000 /* Fall through for C++98. */
10002 case RID_REGISTER:
10003 case RID_STATIC:
10004 case RID_EXTERN:
10005 case RID_MUTABLE:
10006 case RID_THREAD:
10007 /* Consume the token. */
10008 return cp_lexer_consume_token (parser->lexer)->u.value;
10010 default:
10011 return NULL_TREE;
10015 /* Parse an (optional) function-specifier.
10017 function-specifier:
10018 inline
10019 virtual
10020 explicit
10022 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10023 Updates DECL_SPECS, if it is non-NULL. */
10025 static tree
10026 cp_parser_function_specifier_opt (cp_parser* parser,
10027 cp_decl_specifier_seq *decl_specs)
10029 cp_token *token = cp_lexer_peek_token (parser->lexer);
10030 switch (token->keyword)
10032 case RID_INLINE:
10033 if (decl_specs)
10034 ++decl_specs->specs[(int) ds_inline];
10035 break;
10037 case RID_VIRTUAL:
10038 /* 14.5.2.3 [temp.mem]
10040 A member function template shall not be virtual. */
10041 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10042 error_at (token->location, "templates may not be %<virtual%>");
10043 else if (decl_specs)
10044 ++decl_specs->specs[(int) ds_virtual];
10045 break;
10047 case RID_EXPLICIT:
10048 if (decl_specs)
10049 ++decl_specs->specs[(int) ds_explicit];
10050 break;
10052 default:
10053 return NULL_TREE;
10056 /* Consume the token. */
10057 return cp_lexer_consume_token (parser->lexer)->u.value;
10060 /* Parse a linkage-specification.
10062 linkage-specification:
10063 extern string-literal { declaration-seq [opt] }
10064 extern string-literal declaration */
10066 static void
10067 cp_parser_linkage_specification (cp_parser* parser)
10069 tree linkage;
10071 /* Look for the `extern' keyword. */
10072 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10074 /* Look for the string-literal. */
10075 linkage = cp_parser_string_literal (parser, false, false);
10077 /* Transform the literal into an identifier. If the literal is a
10078 wide-character string, or contains embedded NULs, then we can't
10079 handle it as the user wants. */
10080 if (strlen (TREE_STRING_POINTER (linkage))
10081 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10083 cp_parser_error (parser, "invalid linkage-specification");
10084 /* Assume C++ linkage. */
10085 linkage = lang_name_cplusplus;
10087 else
10088 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10090 /* We're now using the new linkage. */
10091 push_lang_context (linkage);
10093 /* If the next token is a `{', then we're using the first
10094 production. */
10095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10097 /* Consume the `{' token. */
10098 cp_lexer_consume_token (parser->lexer);
10099 /* Parse the declarations. */
10100 cp_parser_declaration_seq_opt (parser);
10101 /* Look for the closing `}'. */
10102 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10104 /* Otherwise, there's just one declaration. */
10105 else
10107 bool saved_in_unbraced_linkage_specification_p;
10109 saved_in_unbraced_linkage_specification_p
10110 = parser->in_unbraced_linkage_specification_p;
10111 parser->in_unbraced_linkage_specification_p = true;
10112 cp_parser_declaration (parser);
10113 parser->in_unbraced_linkage_specification_p
10114 = saved_in_unbraced_linkage_specification_p;
10117 /* We're done with the linkage-specification. */
10118 pop_lang_context ();
10121 /* Parse a static_assert-declaration.
10123 static_assert-declaration:
10124 static_assert ( constant-expression , string-literal ) ;
10126 If MEMBER_P, this static_assert is a class member. */
10128 static void
10129 cp_parser_static_assert(cp_parser *parser, bool member_p)
10131 tree condition;
10132 tree message;
10133 cp_token *token;
10134 location_t saved_loc;
10136 /* Peek at the `static_assert' token so we can keep track of exactly
10137 where the static assertion started. */
10138 token = cp_lexer_peek_token (parser->lexer);
10139 saved_loc = token->location;
10141 /* Look for the `static_assert' keyword. */
10142 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10143 RT_STATIC_ASSERT))
10144 return;
10146 /* We know we are in a static assertion; commit to any tentative
10147 parse. */
10148 if (cp_parser_parsing_tentatively (parser))
10149 cp_parser_commit_to_tentative_parse (parser);
10151 /* Parse the `(' starting the static assertion condition. */
10152 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10154 /* Parse the constant-expression. */
10155 condition =
10156 cp_parser_constant_expression (parser,
10157 /*allow_non_constant_p=*/false,
10158 /*non_constant_p=*/NULL);
10160 /* Parse the separating `,'. */
10161 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10163 /* Parse the string-literal message. */
10164 message = cp_parser_string_literal (parser,
10165 /*translate=*/false,
10166 /*wide_ok=*/true);
10168 /* A `)' completes the static assertion. */
10169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10170 cp_parser_skip_to_closing_parenthesis (parser,
10171 /*recovering=*/true,
10172 /*or_comma=*/false,
10173 /*consume_paren=*/true);
10175 /* A semicolon terminates the declaration. */
10176 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10178 /* Complete the static assertion, which may mean either processing
10179 the static assert now or saving it for template instantiation. */
10180 finish_static_assert (condition, message, saved_loc, member_p);
10183 /* Parse a `decltype' type. Returns the type.
10185 simple-type-specifier:
10186 decltype ( expression ) */
10188 static tree
10189 cp_parser_decltype (cp_parser *parser)
10191 tree expr;
10192 bool id_expression_or_member_access_p = false;
10193 const char *saved_message;
10194 bool saved_integral_constant_expression_p;
10195 bool saved_non_integral_constant_expression_p;
10196 cp_token *id_expr_start_token;
10198 /* Look for the `decltype' token. */
10199 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10200 return error_mark_node;
10202 /* Types cannot be defined in a `decltype' expression. Save away the
10203 old message. */
10204 saved_message = parser->type_definition_forbidden_message;
10206 /* And create the new one. */
10207 parser->type_definition_forbidden_message
10208 = G_("types may not be defined in %<decltype%> expressions");
10210 /* The restrictions on constant-expressions do not apply inside
10211 decltype expressions. */
10212 saved_integral_constant_expression_p
10213 = parser->integral_constant_expression_p;
10214 saved_non_integral_constant_expression_p
10215 = parser->non_integral_constant_expression_p;
10216 parser->integral_constant_expression_p = false;
10218 /* Do not actually evaluate the expression. */
10219 ++cp_unevaluated_operand;
10221 /* Do not warn about problems with the expression. */
10222 ++c_inhibit_evaluation_warnings;
10224 /* Parse the opening `('. */
10225 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10226 return error_mark_node;
10228 /* First, try parsing an id-expression. */
10229 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10230 cp_parser_parse_tentatively (parser);
10231 expr = cp_parser_id_expression (parser,
10232 /*template_keyword_p=*/false,
10233 /*check_dependency_p=*/true,
10234 /*template_p=*/NULL,
10235 /*declarator_p=*/false,
10236 /*optional_p=*/false);
10238 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10240 bool non_integral_constant_expression_p = false;
10241 tree id_expression = expr;
10242 cp_id_kind idk;
10243 const char *error_msg;
10245 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10246 /* Lookup the name we got back from the id-expression. */
10247 expr = cp_parser_lookup_name (parser, expr,
10248 none_type,
10249 /*is_template=*/false,
10250 /*is_namespace=*/false,
10251 /*check_dependency=*/true,
10252 /*ambiguous_decls=*/NULL,
10253 id_expr_start_token->location);
10255 if (expr
10256 && expr != error_mark_node
10257 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10258 && TREE_CODE (expr) != TYPE_DECL
10259 && (TREE_CODE (expr) != BIT_NOT_EXPR
10260 || !TYPE_P (TREE_OPERAND (expr, 0)))
10261 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10263 /* Complete lookup of the id-expression. */
10264 expr = (finish_id_expression
10265 (id_expression, expr, parser->scope, &idk,
10266 /*integral_constant_expression_p=*/false,
10267 /*allow_non_integral_constant_expression_p=*/true,
10268 &non_integral_constant_expression_p,
10269 /*template_p=*/false,
10270 /*done=*/true,
10271 /*address_p=*/false,
10272 /*template_arg_p=*/false,
10273 &error_msg,
10274 id_expr_start_token->location));
10276 if (expr == error_mark_node)
10277 /* We found an id-expression, but it was something that we
10278 should not have found. This is an error, not something
10279 we can recover from, so note that we found an
10280 id-expression and we'll recover as gracefully as
10281 possible. */
10282 id_expression_or_member_access_p = true;
10285 if (expr
10286 && expr != error_mark_node
10287 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10288 /* We have an id-expression. */
10289 id_expression_or_member_access_p = true;
10292 if (!id_expression_or_member_access_p)
10294 /* Abort the id-expression parse. */
10295 cp_parser_abort_tentative_parse (parser);
10297 /* Parsing tentatively, again. */
10298 cp_parser_parse_tentatively (parser);
10300 /* Parse a class member access. */
10301 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10302 /*cast_p=*/false,
10303 /*member_access_only_p=*/true, NULL);
10305 if (expr
10306 && expr != error_mark_node
10307 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10308 /* We have an id-expression. */
10309 id_expression_or_member_access_p = true;
10312 if (id_expression_or_member_access_p)
10313 /* We have parsed the complete id-expression or member access. */
10314 cp_parser_parse_definitely (parser);
10315 else
10317 bool saved_greater_than_is_operator_p;
10319 /* Abort our attempt to parse an id-expression or member access
10320 expression. */
10321 cp_parser_abort_tentative_parse (parser);
10323 /* Within a parenthesized expression, a `>' token is always
10324 the greater-than operator. */
10325 saved_greater_than_is_operator_p
10326 = parser->greater_than_is_operator_p;
10327 parser->greater_than_is_operator_p = true;
10329 /* Parse a full expression. */
10330 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10332 /* The `>' token might be the end of a template-id or
10333 template-parameter-list now. */
10334 parser->greater_than_is_operator_p
10335 = saved_greater_than_is_operator_p;
10338 /* Go back to evaluating expressions. */
10339 --cp_unevaluated_operand;
10340 --c_inhibit_evaluation_warnings;
10342 /* Restore the old message and the integral constant expression
10343 flags. */
10344 parser->type_definition_forbidden_message = saved_message;
10345 parser->integral_constant_expression_p
10346 = saved_integral_constant_expression_p;
10347 parser->non_integral_constant_expression_p
10348 = saved_non_integral_constant_expression_p;
10350 if (expr == error_mark_node)
10352 /* Skip everything up to the closing `)'. */
10353 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10354 /*consume_paren=*/true);
10355 return error_mark_node;
10358 /* Parse to the closing `)'. */
10359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10361 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10362 /*consume_paren=*/true);
10363 return error_mark_node;
10366 return finish_decltype_type (expr, id_expression_or_member_access_p);
10369 /* Special member functions [gram.special] */
10371 /* Parse a conversion-function-id.
10373 conversion-function-id:
10374 operator conversion-type-id
10376 Returns an IDENTIFIER_NODE representing the operator. */
10378 static tree
10379 cp_parser_conversion_function_id (cp_parser* parser)
10381 tree type;
10382 tree saved_scope;
10383 tree saved_qualifying_scope;
10384 tree saved_object_scope;
10385 tree pushed_scope = NULL_TREE;
10387 /* Look for the `operator' token. */
10388 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10389 return error_mark_node;
10390 /* When we parse the conversion-type-id, the current scope will be
10391 reset. However, we need that information in able to look up the
10392 conversion function later, so we save it here. */
10393 saved_scope = parser->scope;
10394 saved_qualifying_scope = parser->qualifying_scope;
10395 saved_object_scope = parser->object_scope;
10396 /* We must enter the scope of the class so that the names of
10397 entities declared within the class are available in the
10398 conversion-type-id. For example, consider:
10400 struct S {
10401 typedef int I;
10402 operator I();
10405 S::operator I() { ... }
10407 In order to see that `I' is a type-name in the definition, we
10408 must be in the scope of `S'. */
10409 if (saved_scope)
10410 pushed_scope = push_scope (saved_scope);
10411 /* Parse the conversion-type-id. */
10412 type = cp_parser_conversion_type_id (parser);
10413 /* Leave the scope of the class, if any. */
10414 if (pushed_scope)
10415 pop_scope (pushed_scope);
10416 /* Restore the saved scope. */
10417 parser->scope = saved_scope;
10418 parser->qualifying_scope = saved_qualifying_scope;
10419 parser->object_scope = saved_object_scope;
10420 /* If the TYPE is invalid, indicate failure. */
10421 if (type == error_mark_node)
10422 return error_mark_node;
10423 return mangle_conv_op_name_for_type (type);
10426 /* Parse a conversion-type-id:
10428 conversion-type-id:
10429 type-specifier-seq conversion-declarator [opt]
10431 Returns the TYPE specified. */
10433 static tree
10434 cp_parser_conversion_type_id (cp_parser* parser)
10436 tree attributes;
10437 cp_decl_specifier_seq type_specifiers;
10438 cp_declarator *declarator;
10439 tree type_specified;
10441 /* Parse the attributes. */
10442 attributes = cp_parser_attributes_opt (parser);
10443 /* Parse the type-specifiers. */
10444 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10445 /*is_trailing_return=*/false,
10446 &type_specifiers);
10447 /* If that didn't work, stop. */
10448 if (type_specifiers.type == error_mark_node)
10449 return error_mark_node;
10450 /* Parse the conversion-declarator. */
10451 declarator = cp_parser_conversion_declarator_opt (parser);
10453 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10454 /*initialized=*/0, &attributes);
10455 if (attributes)
10456 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10458 /* Don't give this error when parsing tentatively. This happens to
10459 work because we always parse this definitively once. */
10460 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10461 && type_uses_auto (type_specified))
10463 error ("invalid use of %<auto%> in conversion operator");
10464 return error_mark_node;
10467 return type_specified;
10470 /* Parse an (optional) conversion-declarator.
10472 conversion-declarator:
10473 ptr-operator conversion-declarator [opt]
10477 static cp_declarator *
10478 cp_parser_conversion_declarator_opt (cp_parser* parser)
10480 enum tree_code code;
10481 tree class_type;
10482 cp_cv_quals cv_quals;
10484 /* We don't know if there's a ptr-operator next, or not. */
10485 cp_parser_parse_tentatively (parser);
10486 /* Try the ptr-operator. */
10487 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10488 /* If it worked, look for more conversion-declarators. */
10489 if (cp_parser_parse_definitely (parser))
10491 cp_declarator *declarator;
10493 /* Parse another optional declarator. */
10494 declarator = cp_parser_conversion_declarator_opt (parser);
10496 return cp_parser_make_indirect_declarator
10497 (code, class_type, cv_quals, declarator);
10500 return NULL;
10503 /* Parse an (optional) ctor-initializer.
10505 ctor-initializer:
10506 : mem-initializer-list
10508 Returns TRUE iff the ctor-initializer was actually present. */
10510 static bool
10511 cp_parser_ctor_initializer_opt (cp_parser* parser)
10513 /* If the next token is not a `:', then there is no
10514 ctor-initializer. */
10515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10517 /* Do default initialization of any bases and members. */
10518 if (DECL_CONSTRUCTOR_P (current_function_decl))
10519 finish_mem_initializers (NULL_TREE);
10521 return false;
10524 /* Consume the `:' token. */
10525 cp_lexer_consume_token (parser->lexer);
10526 /* And the mem-initializer-list. */
10527 cp_parser_mem_initializer_list (parser);
10529 return true;
10532 /* Parse a mem-initializer-list.
10534 mem-initializer-list:
10535 mem-initializer ... [opt]
10536 mem-initializer ... [opt] , mem-initializer-list */
10538 static void
10539 cp_parser_mem_initializer_list (cp_parser* parser)
10541 tree mem_initializer_list = NULL_TREE;
10542 cp_token *token = cp_lexer_peek_token (parser->lexer);
10544 /* Let the semantic analysis code know that we are starting the
10545 mem-initializer-list. */
10546 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10547 error_at (token->location,
10548 "only constructors take member initializers");
10550 /* Loop through the list. */
10551 while (true)
10553 tree mem_initializer;
10555 token = cp_lexer_peek_token (parser->lexer);
10556 /* Parse the mem-initializer. */
10557 mem_initializer = cp_parser_mem_initializer (parser);
10558 /* If the next token is a `...', we're expanding member initializers. */
10559 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10561 /* Consume the `...'. */
10562 cp_lexer_consume_token (parser->lexer);
10564 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10565 can be expanded but members cannot. */
10566 if (mem_initializer != error_mark_node
10567 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10569 error_at (token->location,
10570 "cannot expand initializer for member %<%D%>",
10571 TREE_PURPOSE (mem_initializer));
10572 mem_initializer = error_mark_node;
10575 /* Construct the pack expansion type. */
10576 if (mem_initializer != error_mark_node)
10577 mem_initializer = make_pack_expansion (mem_initializer);
10579 /* Add it to the list, unless it was erroneous. */
10580 if (mem_initializer != error_mark_node)
10582 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10583 mem_initializer_list = mem_initializer;
10585 /* If the next token is not a `,', we're done. */
10586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10587 break;
10588 /* Consume the `,' token. */
10589 cp_lexer_consume_token (parser->lexer);
10592 /* Perform semantic analysis. */
10593 if (DECL_CONSTRUCTOR_P (current_function_decl))
10594 finish_mem_initializers (mem_initializer_list);
10597 /* Parse a mem-initializer.
10599 mem-initializer:
10600 mem-initializer-id ( expression-list [opt] )
10601 mem-initializer-id braced-init-list
10603 GNU extension:
10605 mem-initializer:
10606 ( expression-list [opt] )
10608 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10609 class) or FIELD_DECL (for a non-static data member) to initialize;
10610 the TREE_VALUE is the expression-list. An empty initialization
10611 list is represented by void_list_node. */
10613 static tree
10614 cp_parser_mem_initializer (cp_parser* parser)
10616 tree mem_initializer_id;
10617 tree expression_list;
10618 tree member;
10619 cp_token *token = cp_lexer_peek_token (parser->lexer);
10621 /* Find out what is being initialized. */
10622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10624 permerror (token->location,
10625 "anachronistic old-style base class initializer");
10626 mem_initializer_id = NULL_TREE;
10628 else
10630 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10631 if (mem_initializer_id == error_mark_node)
10632 return mem_initializer_id;
10634 member = expand_member_init (mem_initializer_id);
10635 if (member && !DECL_P (member))
10636 in_base_initializer = 1;
10638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10640 bool expr_non_constant_p;
10641 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10642 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10643 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10644 expression_list = build_tree_list (NULL_TREE, expression_list);
10646 else
10648 VEC(tree,gc)* vec;
10649 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10650 /*cast_p=*/false,
10651 /*allow_expansion_p=*/true,
10652 /*non_constant_p=*/NULL);
10653 if (vec == NULL)
10654 return error_mark_node;
10655 expression_list = build_tree_list_vec (vec);
10656 release_tree_vector (vec);
10659 if (expression_list == error_mark_node)
10660 return error_mark_node;
10661 if (!expression_list)
10662 expression_list = void_type_node;
10664 in_base_initializer = 0;
10666 return member ? build_tree_list (member, expression_list) : error_mark_node;
10669 /* Parse a mem-initializer-id.
10671 mem-initializer-id:
10672 :: [opt] nested-name-specifier [opt] class-name
10673 identifier
10675 Returns a TYPE indicating the class to be initializer for the first
10676 production. Returns an IDENTIFIER_NODE indicating the data member
10677 to be initialized for the second production. */
10679 static tree
10680 cp_parser_mem_initializer_id (cp_parser* parser)
10682 bool global_scope_p;
10683 bool nested_name_specifier_p;
10684 bool template_p = false;
10685 tree id;
10687 cp_token *token = cp_lexer_peek_token (parser->lexer);
10689 /* `typename' is not allowed in this context ([temp.res]). */
10690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10692 error_at (token->location,
10693 "keyword %<typename%> not allowed in this context (a qualified "
10694 "member initializer is implicitly a type)");
10695 cp_lexer_consume_token (parser->lexer);
10697 /* Look for the optional `::' operator. */
10698 global_scope_p
10699 = (cp_parser_global_scope_opt (parser,
10700 /*current_scope_valid_p=*/false)
10701 != NULL_TREE);
10702 /* Look for the optional nested-name-specifier. The simplest way to
10703 implement:
10705 [temp.res]
10707 The keyword `typename' is not permitted in a base-specifier or
10708 mem-initializer; in these contexts a qualified name that
10709 depends on a template-parameter is implicitly assumed to be a
10710 type name.
10712 is to assume that we have seen the `typename' keyword at this
10713 point. */
10714 nested_name_specifier_p
10715 = (cp_parser_nested_name_specifier_opt (parser,
10716 /*typename_keyword_p=*/true,
10717 /*check_dependency_p=*/true,
10718 /*type_p=*/true,
10719 /*is_declaration=*/true)
10720 != NULL_TREE);
10721 if (nested_name_specifier_p)
10722 template_p = cp_parser_optional_template_keyword (parser);
10723 /* If there is a `::' operator or a nested-name-specifier, then we
10724 are definitely looking for a class-name. */
10725 if (global_scope_p || nested_name_specifier_p)
10726 return cp_parser_class_name (parser,
10727 /*typename_keyword_p=*/true,
10728 /*template_keyword_p=*/template_p,
10729 typename_type,
10730 /*check_dependency_p=*/true,
10731 /*class_head_p=*/false,
10732 /*is_declaration=*/true);
10733 /* Otherwise, we could also be looking for an ordinary identifier. */
10734 cp_parser_parse_tentatively (parser);
10735 /* Try a class-name. */
10736 id = cp_parser_class_name (parser,
10737 /*typename_keyword_p=*/true,
10738 /*template_keyword_p=*/false,
10739 none_type,
10740 /*check_dependency_p=*/true,
10741 /*class_head_p=*/false,
10742 /*is_declaration=*/true);
10743 /* If we found one, we're done. */
10744 if (cp_parser_parse_definitely (parser))
10745 return id;
10746 /* Otherwise, look for an ordinary identifier. */
10747 return cp_parser_identifier (parser);
10750 /* Overloading [gram.over] */
10752 /* Parse an operator-function-id.
10754 operator-function-id:
10755 operator operator
10757 Returns an IDENTIFIER_NODE for the operator which is a
10758 human-readable spelling of the identifier, e.g., `operator +'. */
10760 static tree
10761 cp_parser_operator_function_id (cp_parser* parser)
10763 /* Look for the `operator' keyword. */
10764 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10765 return error_mark_node;
10766 /* And then the name of the operator itself. */
10767 return cp_parser_operator (parser);
10770 /* Parse an operator.
10772 operator:
10773 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10774 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10775 || ++ -- , ->* -> () []
10777 GNU Extensions:
10779 operator:
10780 <? >? <?= >?=
10782 Returns an IDENTIFIER_NODE for the operator which is a
10783 human-readable spelling of the identifier, e.g., `operator +'. */
10785 static tree
10786 cp_parser_operator (cp_parser* parser)
10788 tree id = NULL_TREE;
10789 cp_token *token;
10791 /* Peek at the next token. */
10792 token = cp_lexer_peek_token (parser->lexer);
10793 /* Figure out which operator we have. */
10794 switch (token->type)
10796 case CPP_KEYWORD:
10798 enum tree_code op;
10800 /* The keyword should be either `new' or `delete'. */
10801 if (token->keyword == RID_NEW)
10802 op = NEW_EXPR;
10803 else if (token->keyword == RID_DELETE)
10804 op = DELETE_EXPR;
10805 else
10806 break;
10808 /* Consume the `new' or `delete' token. */
10809 cp_lexer_consume_token (parser->lexer);
10811 /* Peek at the next token. */
10812 token = cp_lexer_peek_token (parser->lexer);
10813 /* If it's a `[' token then this is the array variant of the
10814 operator. */
10815 if (token->type == CPP_OPEN_SQUARE)
10817 /* Consume the `[' token. */
10818 cp_lexer_consume_token (parser->lexer);
10819 /* Look for the `]' token. */
10820 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10821 id = ansi_opname (op == NEW_EXPR
10822 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10824 /* Otherwise, we have the non-array variant. */
10825 else
10826 id = ansi_opname (op);
10828 return id;
10831 case CPP_PLUS:
10832 id = ansi_opname (PLUS_EXPR);
10833 break;
10835 case CPP_MINUS:
10836 id = ansi_opname (MINUS_EXPR);
10837 break;
10839 case CPP_MULT:
10840 id = ansi_opname (MULT_EXPR);
10841 break;
10843 case CPP_DIV:
10844 id = ansi_opname (TRUNC_DIV_EXPR);
10845 break;
10847 case CPP_MOD:
10848 id = ansi_opname (TRUNC_MOD_EXPR);
10849 break;
10851 case CPP_XOR:
10852 id = ansi_opname (BIT_XOR_EXPR);
10853 break;
10855 case CPP_AND:
10856 id = ansi_opname (BIT_AND_EXPR);
10857 break;
10859 case CPP_OR:
10860 id = ansi_opname (BIT_IOR_EXPR);
10861 break;
10863 case CPP_COMPL:
10864 id = ansi_opname (BIT_NOT_EXPR);
10865 break;
10867 case CPP_NOT:
10868 id = ansi_opname (TRUTH_NOT_EXPR);
10869 break;
10871 case CPP_EQ:
10872 id = ansi_assopname (NOP_EXPR);
10873 break;
10875 case CPP_LESS:
10876 id = ansi_opname (LT_EXPR);
10877 break;
10879 case CPP_GREATER:
10880 id = ansi_opname (GT_EXPR);
10881 break;
10883 case CPP_PLUS_EQ:
10884 id = ansi_assopname (PLUS_EXPR);
10885 break;
10887 case CPP_MINUS_EQ:
10888 id = ansi_assopname (MINUS_EXPR);
10889 break;
10891 case CPP_MULT_EQ:
10892 id = ansi_assopname (MULT_EXPR);
10893 break;
10895 case CPP_DIV_EQ:
10896 id = ansi_assopname (TRUNC_DIV_EXPR);
10897 break;
10899 case CPP_MOD_EQ:
10900 id = ansi_assopname (TRUNC_MOD_EXPR);
10901 break;
10903 case CPP_XOR_EQ:
10904 id = ansi_assopname (BIT_XOR_EXPR);
10905 break;
10907 case CPP_AND_EQ:
10908 id = ansi_assopname (BIT_AND_EXPR);
10909 break;
10911 case CPP_OR_EQ:
10912 id = ansi_assopname (BIT_IOR_EXPR);
10913 break;
10915 case CPP_LSHIFT:
10916 id = ansi_opname (LSHIFT_EXPR);
10917 break;
10919 case CPP_RSHIFT:
10920 id = ansi_opname (RSHIFT_EXPR);
10921 break;
10923 case CPP_LSHIFT_EQ:
10924 id = ansi_assopname (LSHIFT_EXPR);
10925 break;
10927 case CPP_RSHIFT_EQ:
10928 id = ansi_assopname (RSHIFT_EXPR);
10929 break;
10931 case CPP_EQ_EQ:
10932 id = ansi_opname (EQ_EXPR);
10933 break;
10935 case CPP_NOT_EQ:
10936 id = ansi_opname (NE_EXPR);
10937 break;
10939 case CPP_LESS_EQ:
10940 id = ansi_opname (LE_EXPR);
10941 break;
10943 case CPP_GREATER_EQ:
10944 id = ansi_opname (GE_EXPR);
10945 break;
10947 case CPP_AND_AND:
10948 id = ansi_opname (TRUTH_ANDIF_EXPR);
10949 break;
10951 case CPP_OR_OR:
10952 id = ansi_opname (TRUTH_ORIF_EXPR);
10953 break;
10955 case CPP_PLUS_PLUS:
10956 id = ansi_opname (POSTINCREMENT_EXPR);
10957 break;
10959 case CPP_MINUS_MINUS:
10960 id = ansi_opname (PREDECREMENT_EXPR);
10961 break;
10963 case CPP_COMMA:
10964 id = ansi_opname (COMPOUND_EXPR);
10965 break;
10967 case CPP_DEREF_STAR:
10968 id = ansi_opname (MEMBER_REF);
10969 break;
10971 case CPP_DEREF:
10972 id = ansi_opname (COMPONENT_REF);
10973 break;
10975 case CPP_OPEN_PAREN:
10976 /* Consume the `('. */
10977 cp_lexer_consume_token (parser->lexer);
10978 /* Look for the matching `)'. */
10979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10980 return ansi_opname (CALL_EXPR);
10982 case CPP_OPEN_SQUARE:
10983 /* Consume the `['. */
10984 cp_lexer_consume_token (parser->lexer);
10985 /* Look for the matching `]'. */
10986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10987 return ansi_opname (ARRAY_REF);
10989 default:
10990 /* Anything else is an error. */
10991 break;
10994 /* If we have selected an identifier, we need to consume the
10995 operator token. */
10996 if (id)
10997 cp_lexer_consume_token (parser->lexer);
10998 /* Otherwise, no valid operator name was present. */
10999 else
11001 cp_parser_error (parser, "expected operator");
11002 id = error_mark_node;
11005 return id;
11008 /* Parse a template-declaration.
11010 template-declaration:
11011 export [opt] template < template-parameter-list > declaration
11013 If MEMBER_P is TRUE, this template-declaration occurs within a
11014 class-specifier.
11016 The grammar rule given by the standard isn't correct. What
11017 is really meant is:
11019 template-declaration:
11020 export [opt] template-parameter-list-seq
11021 decl-specifier-seq [opt] init-declarator [opt] ;
11022 export [opt] template-parameter-list-seq
11023 function-definition
11025 template-parameter-list-seq:
11026 template-parameter-list-seq [opt]
11027 template < template-parameter-list > */
11029 static void
11030 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11032 /* Check for `export'. */
11033 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11035 /* Consume the `export' token. */
11036 cp_lexer_consume_token (parser->lexer);
11037 /* Warn that we do not support `export'. */
11038 warning (0, "keyword %<export%> not implemented, and will be ignored");
11041 cp_parser_template_declaration_after_export (parser, member_p);
11044 /* Parse a template-parameter-list.
11046 template-parameter-list:
11047 template-parameter
11048 template-parameter-list , template-parameter
11050 Returns a TREE_LIST. Each node represents a template parameter.
11051 The nodes are connected via their TREE_CHAINs. */
11053 static tree
11054 cp_parser_template_parameter_list (cp_parser* parser)
11056 tree parameter_list = NULL_TREE;
11058 begin_template_parm_list ();
11060 /* The loop below parses the template parms. We first need to know
11061 the total number of template parms to be able to compute proper
11062 canonical types of each dependent type. So after the loop, when
11063 we know the total number of template parms,
11064 end_template_parm_list computes the proper canonical types and
11065 fixes up the dependent types accordingly. */
11066 while (true)
11068 tree parameter;
11069 bool is_non_type;
11070 bool is_parameter_pack;
11071 location_t parm_loc;
11073 /* Parse the template-parameter. */
11074 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11075 parameter = cp_parser_template_parameter (parser,
11076 &is_non_type,
11077 &is_parameter_pack);
11078 /* Add it to the list. */
11079 if (parameter != error_mark_node)
11080 parameter_list = process_template_parm (parameter_list,
11081 parm_loc,
11082 parameter,
11083 is_non_type,
11084 is_parameter_pack,
11086 else
11088 tree err_parm = build_tree_list (parameter, parameter);
11089 parameter_list = chainon (parameter_list, err_parm);
11092 /* If the next token is not a `,', we're done. */
11093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11094 break;
11095 /* Otherwise, consume the `,' token. */
11096 cp_lexer_consume_token (parser->lexer);
11099 return end_template_parm_list (parameter_list);
11102 /* Parse a template-parameter.
11104 template-parameter:
11105 type-parameter
11106 parameter-declaration
11108 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11109 the parameter. The TREE_PURPOSE is the default value, if any.
11110 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11111 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11112 set to true iff this parameter is a parameter pack. */
11114 static tree
11115 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11116 bool *is_parameter_pack)
11118 cp_token *token;
11119 cp_parameter_declarator *parameter_declarator;
11120 cp_declarator *id_declarator;
11121 tree parm;
11123 /* Assume it is a type parameter or a template parameter. */
11124 *is_non_type = false;
11125 /* Assume it not a parameter pack. */
11126 *is_parameter_pack = false;
11127 /* Peek at the next token. */
11128 token = cp_lexer_peek_token (parser->lexer);
11129 /* If it is `class' or `template', we have a type-parameter. */
11130 if (token->keyword == RID_TEMPLATE)
11131 return cp_parser_type_parameter (parser, is_parameter_pack);
11132 /* If it is `class' or `typename' we do not know yet whether it is a
11133 type parameter or a non-type parameter. Consider:
11135 template <typename T, typename T::X X> ...
11139 template <class C, class D*> ...
11141 Here, the first parameter is a type parameter, and the second is
11142 a non-type parameter. We can tell by looking at the token after
11143 the identifier -- if it is a `,', `=', or `>' then we have a type
11144 parameter. */
11145 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11147 /* Peek at the token after `class' or `typename'. */
11148 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11149 /* If it's an ellipsis, we have a template type parameter
11150 pack. */
11151 if (token->type == CPP_ELLIPSIS)
11152 return cp_parser_type_parameter (parser, is_parameter_pack);
11153 /* If it's an identifier, skip it. */
11154 if (token->type == CPP_NAME)
11155 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11156 /* Now, see if the token looks like the end of a template
11157 parameter. */
11158 if (token->type == CPP_COMMA
11159 || token->type == CPP_EQ
11160 || token->type == CPP_GREATER)
11161 return cp_parser_type_parameter (parser, is_parameter_pack);
11164 /* Otherwise, it is a non-type parameter.
11166 [temp.param]
11168 When parsing a default template-argument for a non-type
11169 template-parameter, the first non-nested `>' is taken as the end
11170 of the template parameter-list rather than a greater-than
11171 operator. */
11172 *is_non_type = true;
11173 parameter_declarator
11174 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11175 /*parenthesized_p=*/NULL);
11177 /* If the parameter declaration is marked as a parameter pack, set
11178 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11179 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11180 grokdeclarator. */
11181 if (parameter_declarator
11182 && parameter_declarator->declarator
11183 && parameter_declarator->declarator->parameter_pack_p)
11185 *is_parameter_pack = true;
11186 parameter_declarator->declarator->parameter_pack_p = false;
11189 /* If the next token is an ellipsis, and we don't already have it
11190 marked as a parameter pack, then we have a parameter pack (that
11191 has no declarator). */
11192 if (!*is_parameter_pack
11193 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11194 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11196 /* Consume the `...'. */
11197 cp_lexer_consume_token (parser->lexer);
11198 maybe_warn_variadic_templates ();
11200 *is_parameter_pack = true;
11202 /* We might end up with a pack expansion as the type of the non-type
11203 template parameter, in which case this is a non-type template
11204 parameter pack. */
11205 else if (parameter_declarator
11206 && parameter_declarator->decl_specifiers.type
11207 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11209 *is_parameter_pack = true;
11210 parameter_declarator->decl_specifiers.type =
11211 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11214 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11216 /* Parameter packs cannot have default arguments. However, a
11217 user may try to do so, so we'll parse them and give an
11218 appropriate diagnostic here. */
11220 /* Consume the `='. */
11221 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11222 cp_lexer_consume_token (parser->lexer);
11224 /* Find the name of the parameter pack. */
11225 id_declarator = parameter_declarator->declarator;
11226 while (id_declarator && id_declarator->kind != cdk_id)
11227 id_declarator = id_declarator->declarator;
11229 if (id_declarator && id_declarator->kind == cdk_id)
11230 error_at (start_token->location,
11231 "template parameter pack %qD cannot have a default argument",
11232 id_declarator->u.id.unqualified_name);
11233 else
11234 error_at (start_token->location,
11235 "template parameter pack cannot have a default argument");
11237 /* Parse the default argument, but throw away the result. */
11238 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11241 parm = grokdeclarator (parameter_declarator->declarator,
11242 &parameter_declarator->decl_specifiers,
11243 TPARM, /*initialized=*/0,
11244 /*attrlist=*/NULL);
11245 if (parm == error_mark_node)
11246 return error_mark_node;
11248 return build_tree_list (parameter_declarator->default_argument, parm);
11251 /* Parse a type-parameter.
11253 type-parameter:
11254 class identifier [opt]
11255 class identifier [opt] = type-id
11256 typename identifier [opt]
11257 typename identifier [opt] = type-id
11258 template < template-parameter-list > class identifier [opt]
11259 template < template-parameter-list > class identifier [opt]
11260 = id-expression
11262 GNU Extension (variadic templates):
11264 type-parameter:
11265 class ... identifier [opt]
11266 typename ... identifier [opt]
11268 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11269 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11270 the declaration of the parameter.
11272 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11274 static tree
11275 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11277 cp_token *token;
11278 tree parameter;
11280 /* Look for a keyword to tell us what kind of parameter this is. */
11281 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11282 if (!token)
11283 return error_mark_node;
11285 switch (token->keyword)
11287 case RID_CLASS:
11288 case RID_TYPENAME:
11290 tree identifier;
11291 tree default_argument;
11293 /* If the next token is an ellipsis, we have a template
11294 argument pack. */
11295 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11297 /* Consume the `...' token. */
11298 cp_lexer_consume_token (parser->lexer);
11299 maybe_warn_variadic_templates ();
11301 *is_parameter_pack = true;
11304 /* If the next token is an identifier, then it names the
11305 parameter. */
11306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11307 identifier = cp_parser_identifier (parser);
11308 else
11309 identifier = NULL_TREE;
11311 /* Create the parameter. */
11312 parameter = finish_template_type_parm (class_type_node, identifier);
11314 /* If the next token is an `=', we have a default argument. */
11315 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11317 /* Consume the `=' token. */
11318 cp_lexer_consume_token (parser->lexer);
11319 /* Parse the default-argument. */
11320 push_deferring_access_checks (dk_no_deferred);
11321 default_argument = cp_parser_type_id (parser);
11323 /* Template parameter packs cannot have default
11324 arguments. */
11325 if (*is_parameter_pack)
11327 if (identifier)
11328 error_at (token->location,
11329 "template parameter pack %qD cannot have a "
11330 "default argument", identifier);
11331 else
11332 error_at (token->location,
11333 "template parameter packs cannot have "
11334 "default arguments");
11335 default_argument = NULL_TREE;
11337 pop_deferring_access_checks ();
11339 else
11340 default_argument = NULL_TREE;
11342 /* Create the combined representation of the parameter and the
11343 default argument. */
11344 parameter = build_tree_list (default_argument, parameter);
11346 break;
11348 case RID_TEMPLATE:
11350 tree identifier;
11351 tree default_argument;
11353 /* Look for the `<'. */
11354 cp_parser_require (parser, CPP_LESS, RT_LESS);
11355 /* Parse the template-parameter-list. */
11356 cp_parser_template_parameter_list (parser);
11357 /* Look for the `>'. */
11358 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11359 /* Look for the `class' keyword. */
11360 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11361 /* If the next token is an ellipsis, we have a template
11362 argument pack. */
11363 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11365 /* Consume the `...' token. */
11366 cp_lexer_consume_token (parser->lexer);
11367 maybe_warn_variadic_templates ();
11369 *is_parameter_pack = true;
11371 /* If the next token is an `=', then there is a
11372 default-argument. If the next token is a `>', we are at
11373 the end of the parameter-list. If the next token is a `,',
11374 then we are at the end of this parameter. */
11375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11376 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11377 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11379 identifier = cp_parser_identifier (parser);
11380 /* Treat invalid names as if the parameter were nameless. */
11381 if (identifier == error_mark_node)
11382 identifier = NULL_TREE;
11384 else
11385 identifier = NULL_TREE;
11387 /* Create the template parameter. */
11388 parameter = finish_template_template_parm (class_type_node,
11389 identifier);
11391 /* If the next token is an `=', then there is a
11392 default-argument. */
11393 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11395 bool is_template;
11397 /* Consume the `='. */
11398 cp_lexer_consume_token (parser->lexer);
11399 /* Parse the id-expression. */
11400 push_deferring_access_checks (dk_no_deferred);
11401 /* save token before parsing the id-expression, for error
11402 reporting */
11403 token = cp_lexer_peek_token (parser->lexer);
11404 default_argument
11405 = cp_parser_id_expression (parser,
11406 /*template_keyword_p=*/false,
11407 /*check_dependency_p=*/true,
11408 /*template_p=*/&is_template,
11409 /*declarator_p=*/false,
11410 /*optional_p=*/false);
11411 if (TREE_CODE (default_argument) == TYPE_DECL)
11412 /* If the id-expression was a template-id that refers to
11413 a template-class, we already have the declaration here,
11414 so no further lookup is needed. */
11416 else
11417 /* Look up the name. */
11418 default_argument
11419 = cp_parser_lookup_name (parser, default_argument,
11420 none_type,
11421 /*is_template=*/is_template,
11422 /*is_namespace=*/false,
11423 /*check_dependency=*/true,
11424 /*ambiguous_decls=*/NULL,
11425 token->location);
11426 /* See if the default argument is valid. */
11427 default_argument
11428 = check_template_template_default_arg (default_argument);
11430 /* Template parameter packs cannot have default
11431 arguments. */
11432 if (*is_parameter_pack)
11434 if (identifier)
11435 error_at (token->location,
11436 "template parameter pack %qD cannot "
11437 "have a default argument",
11438 identifier);
11439 else
11440 error_at (token->location, "template parameter packs cannot "
11441 "have default arguments");
11442 default_argument = NULL_TREE;
11444 pop_deferring_access_checks ();
11446 else
11447 default_argument = NULL_TREE;
11449 /* Create the combined representation of the parameter and the
11450 default argument. */
11451 parameter = build_tree_list (default_argument, parameter);
11453 break;
11455 default:
11456 gcc_unreachable ();
11457 break;
11460 return parameter;
11463 /* Parse a template-id.
11465 template-id:
11466 template-name < template-argument-list [opt] >
11468 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11469 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11470 returned. Otherwise, if the template-name names a function, or set
11471 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11472 names a class, returns a TYPE_DECL for the specialization.
11474 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11475 uninstantiated templates. */
11477 static tree
11478 cp_parser_template_id (cp_parser *parser,
11479 bool template_keyword_p,
11480 bool check_dependency_p,
11481 bool is_declaration)
11483 int i;
11484 tree templ;
11485 tree arguments;
11486 tree template_id;
11487 cp_token_position start_of_id = 0;
11488 deferred_access_check *chk;
11489 VEC (deferred_access_check,gc) *access_check;
11490 cp_token *next_token = NULL, *next_token_2 = NULL;
11491 bool is_identifier;
11493 /* If the next token corresponds to a template-id, there is no need
11494 to reparse it. */
11495 next_token = cp_lexer_peek_token (parser->lexer);
11496 if (next_token->type == CPP_TEMPLATE_ID)
11498 struct tree_check *check_value;
11500 /* Get the stored value. */
11501 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11502 /* Perform any access checks that were deferred. */
11503 access_check = check_value->checks;
11504 if (access_check)
11506 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11507 perform_or_defer_access_check (chk->binfo,
11508 chk->decl,
11509 chk->diag_decl);
11511 /* Return the stored value. */
11512 return check_value->value;
11515 /* Avoid performing name lookup if there is no possibility of
11516 finding a template-id. */
11517 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11518 || (next_token->type == CPP_NAME
11519 && !cp_parser_nth_token_starts_template_argument_list_p
11520 (parser, 2)))
11522 cp_parser_error (parser, "expected template-id");
11523 return error_mark_node;
11526 /* Remember where the template-id starts. */
11527 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11528 start_of_id = cp_lexer_token_position (parser->lexer, false);
11530 push_deferring_access_checks (dk_deferred);
11532 /* Parse the template-name. */
11533 is_identifier = false;
11534 templ = cp_parser_template_name (parser, template_keyword_p,
11535 check_dependency_p,
11536 is_declaration,
11537 &is_identifier);
11538 if (templ == error_mark_node || is_identifier)
11540 pop_deferring_access_checks ();
11541 return templ;
11544 /* If we find the sequence `[:' after a template-name, it's probably
11545 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11546 parse correctly the argument list. */
11547 next_token = cp_lexer_peek_token (parser->lexer);
11548 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11549 if (next_token->type == CPP_OPEN_SQUARE
11550 && next_token->flags & DIGRAPH
11551 && next_token_2->type == CPP_COLON
11552 && !(next_token_2->flags & PREV_WHITE))
11554 cp_parser_parse_tentatively (parser);
11555 /* Change `:' into `::'. */
11556 next_token_2->type = CPP_SCOPE;
11557 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11558 CPP_LESS. */
11559 cp_lexer_consume_token (parser->lexer);
11561 /* Parse the arguments. */
11562 arguments = cp_parser_enclosed_template_argument_list (parser);
11563 if (!cp_parser_parse_definitely (parser))
11565 /* If we couldn't parse an argument list, then we revert our changes
11566 and return simply an error. Maybe this is not a template-id
11567 after all. */
11568 next_token_2->type = CPP_COLON;
11569 cp_parser_error (parser, "expected %<<%>");
11570 pop_deferring_access_checks ();
11571 return error_mark_node;
11573 /* Otherwise, emit an error about the invalid digraph, but continue
11574 parsing because we got our argument list. */
11575 if (permerror (next_token->location,
11576 "%<<::%> cannot begin a template-argument list"))
11578 static bool hint = false;
11579 inform (next_token->location,
11580 "%<<:%> is an alternate spelling for %<[%>."
11581 " Insert whitespace between %<<%> and %<::%>");
11582 if (!hint && !flag_permissive)
11584 inform (next_token->location, "(if you use %<-fpermissive%>"
11585 " G++ will accept your code)");
11586 hint = true;
11590 else
11592 /* Look for the `<' that starts the template-argument-list. */
11593 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11595 pop_deferring_access_checks ();
11596 return error_mark_node;
11598 /* Parse the arguments. */
11599 arguments = cp_parser_enclosed_template_argument_list (parser);
11602 /* Build a representation of the specialization. */
11603 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11604 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11605 else if (DECL_CLASS_TEMPLATE_P (templ)
11606 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11608 bool entering_scope;
11609 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11610 template (rather than some instantiation thereof) only if
11611 is not nested within some other construct. For example, in
11612 "template <typename T> void f(T) { A<T>::", A<T> is just an
11613 instantiation of A. */
11614 entering_scope = (template_parm_scope_p ()
11615 && cp_lexer_next_token_is (parser->lexer,
11616 CPP_SCOPE));
11617 template_id
11618 = finish_template_type (templ, arguments, entering_scope);
11620 else
11622 /* If it's not a class-template or a template-template, it should be
11623 a function-template. */
11624 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11625 || TREE_CODE (templ) == OVERLOAD
11626 || BASELINK_P (templ)));
11628 template_id = lookup_template_function (templ, arguments);
11631 /* If parsing tentatively, replace the sequence of tokens that makes
11632 up the template-id with a CPP_TEMPLATE_ID token. That way,
11633 should we re-parse the token stream, we will not have to repeat
11634 the effort required to do the parse, nor will we issue duplicate
11635 error messages about problems during instantiation of the
11636 template. */
11637 if (start_of_id)
11639 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11641 /* Reset the contents of the START_OF_ID token. */
11642 token->type = CPP_TEMPLATE_ID;
11643 /* Retrieve any deferred checks. Do not pop this access checks yet
11644 so the memory will not be reclaimed during token replacing below. */
11645 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11646 token->u.tree_check_value->value = template_id;
11647 token->u.tree_check_value->checks = get_deferred_access_checks ();
11648 token->keyword = RID_MAX;
11650 /* Purge all subsequent tokens. */
11651 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11653 /* ??? Can we actually assume that, if template_id ==
11654 error_mark_node, we will have issued a diagnostic to the
11655 user, as opposed to simply marking the tentative parse as
11656 failed? */
11657 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11658 error_at (token->location, "parse error in template argument list");
11661 pop_deferring_access_checks ();
11662 return template_id;
11665 /* Parse a template-name.
11667 template-name:
11668 identifier
11670 The standard should actually say:
11672 template-name:
11673 identifier
11674 operator-function-id
11676 A defect report has been filed about this issue.
11678 A conversion-function-id cannot be a template name because they cannot
11679 be part of a template-id. In fact, looking at this code:
11681 a.operator K<int>()
11683 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11684 It is impossible to call a templated conversion-function-id with an
11685 explicit argument list, since the only allowed template parameter is
11686 the type to which it is converting.
11688 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11689 `template' keyword, in a construction like:
11691 T::template f<3>()
11693 In that case `f' is taken to be a template-name, even though there
11694 is no way of knowing for sure.
11696 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11697 name refers to a set of overloaded functions, at least one of which
11698 is a template, or an IDENTIFIER_NODE with the name of the template,
11699 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11700 names are looked up inside uninstantiated templates. */
11702 static tree
11703 cp_parser_template_name (cp_parser* parser,
11704 bool template_keyword_p,
11705 bool check_dependency_p,
11706 bool is_declaration,
11707 bool *is_identifier)
11709 tree identifier;
11710 tree decl;
11711 tree fns;
11712 cp_token *token = cp_lexer_peek_token (parser->lexer);
11714 /* If the next token is `operator', then we have either an
11715 operator-function-id or a conversion-function-id. */
11716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11718 /* We don't know whether we're looking at an
11719 operator-function-id or a conversion-function-id. */
11720 cp_parser_parse_tentatively (parser);
11721 /* Try an operator-function-id. */
11722 identifier = cp_parser_operator_function_id (parser);
11723 /* If that didn't work, try a conversion-function-id. */
11724 if (!cp_parser_parse_definitely (parser))
11726 cp_parser_error (parser, "expected template-name");
11727 return error_mark_node;
11730 /* Look for the identifier. */
11731 else
11732 identifier = cp_parser_identifier (parser);
11734 /* If we didn't find an identifier, we don't have a template-id. */
11735 if (identifier == error_mark_node)
11736 return error_mark_node;
11738 /* If the name immediately followed the `template' keyword, then it
11739 is a template-name. However, if the next token is not `<', then
11740 we do not treat it as a template-name, since it is not being used
11741 as part of a template-id. This enables us to handle constructs
11742 like:
11744 template <typename T> struct S { S(); };
11745 template <typename T> S<T>::S();
11747 correctly. We would treat `S' as a template -- if it were `S<T>'
11748 -- but we do not if there is no `<'. */
11750 if (processing_template_decl
11751 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11753 /* In a declaration, in a dependent context, we pretend that the
11754 "template" keyword was present in order to improve error
11755 recovery. For example, given:
11757 template <typename T> void f(T::X<int>);
11759 we want to treat "X<int>" as a template-id. */
11760 if (is_declaration
11761 && !template_keyword_p
11762 && parser->scope && TYPE_P (parser->scope)
11763 && check_dependency_p
11764 && dependent_scope_p (parser->scope)
11765 /* Do not do this for dtors (or ctors), since they never
11766 need the template keyword before their name. */
11767 && !constructor_name_p (identifier, parser->scope))
11769 cp_token_position start = 0;
11771 /* Explain what went wrong. */
11772 error_at (token->location, "non-template %qD used as template",
11773 identifier);
11774 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11775 parser->scope, identifier);
11776 /* If parsing tentatively, find the location of the "<" token. */
11777 if (cp_parser_simulate_error (parser))
11778 start = cp_lexer_token_position (parser->lexer, true);
11779 /* Parse the template arguments so that we can issue error
11780 messages about them. */
11781 cp_lexer_consume_token (parser->lexer);
11782 cp_parser_enclosed_template_argument_list (parser);
11783 /* Skip tokens until we find a good place from which to
11784 continue parsing. */
11785 cp_parser_skip_to_closing_parenthesis (parser,
11786 /*recovering=*/true,
11787 /*or_comma=*/true,
11788 /*consume_paren=*/false);
11789 /* If parsing tentatively, permanently remove the
11790 template argument list. That will prevent duplicate
11791 error messages from being issued about the missing
11792 "template" keyword. */
11793 if (start)
11794 cp_lexer_purge_tokens_after (parser->lexer, start);
11795 if (is_identifier)
11796 *is_identifier = true;
11797 return identifier;
11800 /* If the "template" keyword is present, then there is generally
11801 no point in doing name-lookup, so we just return IDENTIFIER.
11802 But, if the qualifying scope is non-dependent then we can
11803 (and must) do name-lookup normally. */
11804 if (template_keyword_p
11805 && (!parser->scope
11806 || (TYPE_P (parser->scope)
11807 && dependent_type_p (parser->scope))))
11808 return identifier;
11811 /* Look up the name. */
11812 decl = cp_parser_lookup_name (parser, identifier,
11813 none_type,
11814 /*is_template=*/true,
11815 /*is_namespace=*/false,
11816 check_dependency_p,
11817 /*ambiguous_decls=*/NULL,
11818 token->location);
11820 /* If DECL is a template, then the name was a template-name. */
11821 if (TREE_CODE (decl) == TEMPLATE_DECL)
11823 else
11825 tree fn = NULL_TREE;
11827 /* The standard does not explicitly indicate whether a name that
11828 names a set of overloaded declarations, some of which are
11829 templates, is a template-name. However, such a name should
11830 be a template-name; otherwise, there is no way to form a
11831 template-id for the overloaded templates. */
11832 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11833 if (TREE_CODE (fns) == OVERLOAD)
11834 for (fn = fns; fn; fn = OVL_NEXT (fn))
11835 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11836 break;
11838 if (!fn)
11840 /* The name does not name a template. */
11841 cp_parser_error (parser, "expected template-name");
11842 return error_mark_node;
11846 /* If DECL is dependent, and refers to a function, then just return
11847 its name; we will look it up again during template instantiation. */
11848 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11850 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11851 if (TYPE_P (scope) && dependent_type_p (scope))
11852 return identifier;
11855 return decl;
11858 /* Parse a template-argument-list.
11860 template-argument-list:
11861 template-argument ... [opt]
11862 template-argument-list , template-argument ... [opt]
11864 Returns a TREE_VEC containing the arguments. */
11866 static tree
11867 cp_parser_template_argument_list (cp_parser* parser)
11869 tree fixed_args[10];
11870 unsigned n_args = 0;
11871 unsigned alloced = 10;
11872 tree *arg_ary = fixed_args;
11873 tree vec;
11874 bool saved_in_template_argument_list_p;
11875 bool saved_ice_p;
11876 bool saved_non_ice_p;
11878 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11879 parser->in_template_argument_list_p = true;
11880 /* Even if the template-id appears in an integral
11881 constant-expression, the contents of the argument list do
11882 not. */
11883 saved_ice_p = parser->integral_constant_expression_p;
11884 parser->integral_constant_expression_p = false;
11885 saved_non_ice_p = parser->non_integral_constant_expression_p;
11886 parser->non_integral_constant_expression_p = false;
11887 /* Parse the arguments. */
11890 tree argument;
11892 if (n_args)
11893 /* Consume the comma. */
11894 cp_lexer_consume_token (parser->lexer);
11896 /* Parse the template-argument. */
11897 argument = cp_parser_template_argument (parser);
11899 /* If the next token is an ellipsis, we're expanding a template
11900 argument pack. */
11901 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11903 if (argument == error_mark_node)
11905 cp_token *token = cp_lexer_peek_token (parser->lexer);
11906 error_at (token->location,
11907 "expected parameter pack before %<...%>");
11909 /* Consume the `...' token. */
11910 cp_lexer_consume_token (parser->lexer);
11912 /* Make the argument into a TYPE_PACK_EXPANSION or
11913 EXPR_PACK_EXPANSION. */
11914 argument = make_pack_expansion (argument);
11917 if (n_args == alloced)
11919 alloced *= 2;
11921 if (arg_ary == fixed_args)
11923 arg_ary = XNEWVEC (tree, alloced);
11924 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11926 else
11927 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11929 arg_ary[n_args++] = argument;
11931 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11933 vec = make_tree_vec (n_args);
11935 while (n_args--)
11936 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11938 if (arg_ary != fixed_args)
11939 free (arg_ary);
11940 parser->non_integral_constant_expression_p = saved_non_ice_p;
11941 parser->integral_constant_expression_p = saved_ice_p;
11942 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11943 #ifdef ENABLE_CHECKING
11944 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11945 #endif
11946 return vec;
11949 /* Parse a template-argument.
11951 template-argument:
11952 assignment-expression
11953 type-id
11954 id-expression
11956 The representation is that of an assignment-expression, type-id, or
11957 id-expression -- except that the qualified id-expression is
11958 evaluated, so that the value returned is either a DECL or an
11959 OVERLOAD.
11961 Although the standard says "assignment-expression", it forbids
11962 throw-expressions or assignments in the template argument.
11963 Therefore, we use "conditional-expression" instead. */
11965 static tree
11966 cp_parser_template_argument (cp_parser* parser)
11968 tree argument;
11969 bool template_p;
11970 bool address_p;
11971 bool maybe_type_id = false;
11972 cp_token *token = NULL, *argument_start_token = NULL;
11973 cp_id_kind idk;
11975 /* There's really no way to know what we're looking at, so we just
11976 try each alternative in order.
11978 [temp.arg]
11980 In a template-argument, an ambiguity between a type-id and an
11981 expression is resolved to a type-id, regardless of the form of
11982 the corresponding template-parameter.
11984 Therefore, we try a type-id first. */
11985 cp_parser_parse_tentatively (parser);
11986 argument = cp_parser_template_type_arg (parser);
11987 /* If there was no error parsing the type-id but the next token is a
11988 '>>', our behavior depends on which dialect of C++ we're
11989 parsing. In C++98, we probably found a typo for '> >'. But there
11990 are type-id which are also valid expressions. For instance:
11992 struct X { int operator >> (int); };
11993 template <int V> struct Foo {};
11994 Foo<X () >> 5> r;
11996 Here 'X()' is a valid type-id of a function type, but the user just
11997 wanted to write the expression "X() >> 5". Thus, we remember that we
11998 found a valid type-id, but we still try to parse the argument as an
11999 expression to see what happens.
12001 In C++0x, the '>>' will be considered two separate '>'
12002 tokens. */
12003 if (!cp_parser_error_occurred (parser)
12004 && cxx_dialect == cxx98
12005 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12007 maybe_type_id = true;
12008 cp_parser_abort_tentative_parse (parser);
12010 else
12012 /* If the next token isn't a `,' or a `>', then this argument wasn't
12013 really finished. This means that the argument is not a valid
12014 type-id. */
12015 if (!cp_parser_next_token_ends_template_argument_p (parser))
12016 cp_parser_error (parser, "expected template-argument");
12017 /* If that worked, we're done. */
12018 if (cp_parser_parse_definitely (parser))
12019 return argument;
12021 /* We're still not sure what the argument will be. */
12022 cp_parser_parse_tentatively (parser);
12023 /* Try a template. */
12024 argument_start_token = cp_lexer_peek_token (parser->lexer);
12025 argument = cp_parser_id_expression (parser,
12026 /*template_keyword_p=*/false,
12027 /*check_dependency_p=*/true,
12028 &template_p,
12029 /*declarator_p=*/false,
12030 /*optional_p=*/false);
12031 /* If the next token isn't a `,' or a `>', then this argument wasn't
12032 really finished. */
12033 if (!cp_parser_next_token_ends_template_argument_p (parser))
12034 cp_parser_error (parser, "expected template-argument");
12035 if (!cp_parser_error_occurred (parser))
12037 /* Figure out what is being referred to. If the id-expression
12038 was for a class template specialization, then we will have a
12039 TYPE_DECL at this point. There is no need to do name lookup
12040 at this point in that case. */
12041 if (TREE_CODE (argument) != TYPE_DECL)
12042 argument = cp_parser_lookup_name (parser, argument,
12043 none_type,
12044 /*is_template=*/template_p,
12045 /*is_namespace=*/false,
12046 /*check_dependency=*/true,
12047 /*ambiguous_decls=*/NULL,
12048 argument_start_token->location);
12049 if (TREE_CODE (argument) != TEMPLATE_DECL
12050 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12051 cp_parser_error (parser, "expected template-name");
12053 if (cp_parser_parse_definitely (parser))
12054 return argument;
12055 /* It must be a non-type argument. There permitted cases are given
12056 in [temp.arg.nontype]:
12058 -- an integral constant-expression of integral or enumeration
12059 type; or
12061 -- the name of a non-type template-parameter; or
12063 -- the name of an object or function with external linkage...
12065 -- the address of an object or function with external linkage...
12067 -- a pointer to member... */
12068 /* Look for a non-type template parameter. */
12069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12071 cp_parser_parse_tentatively (parser);
12072 argument = cp_parser_primary_expression (parser,
12073 /*address_p=*/false,
12074 /*cast_p=*/false,
12075 /*template_arg_p=*/true,
12076 &idk);
12077 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12078 || !cp_parser_next_token_ends_template_argument_p (parser))
12079 cp_parser_simulate_error (parser);
12080 if (cp_parser_parse_definitely (parser))
12081 return argument;
12084 /* If the next token is "&", the argument must be the address of an
12085 object or function with external linkage. */
12086 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12087 if (address_p)
12088 cp_lexer_consume_token (parser->lexer);
12089 /* See if we might have an id-expression. */
12090 token = cp_lexer_peek_token (parser->lexer);
12091 if (token->type == CPP_NAME
12092 || token->keyword == RID_OPERATOR
12093 || token->type == CPP_SCOPE
12094 || token->type == CPP_TEMPLATE_ID
12095 || token->type == CPP_NESTED_NAME_SPECIFIER)
12097 cp_parser_parse_tentatively (parser);
12098 argument = cp_parser_primary_expression (parser,
12099 address_p,
12100 /*cast_p=*/false,
12101 /*template_arg_p=*/true,
12102 &idk);
12103 if (cp_parser_error_occurred (parser)
12104 || !cp_parser_next_token_ends_template_argument_p (parser))
12105 cp_parser_abort_tentative_parse (parser);
12106 else
12108 tree probe;
12110 if (TREE_CODE (argument) == INDIRECT_REF)
12112 gcc_assert (REFERENCE_REF_P (argument));
12113 argument = TREE_OPERAND (argument, 0);
12116 /* If we're in a template, we represent a qualified-id referring
12117 to a static data member as a SCOPE_REF even if the scope isn't
12118 dependent so that we can check access control later. */
12119 probe = argument;
12120 if (TREE_CODE (probe) == SCOPE_REF)
12121 probe = TREE_OPERAND (probe, 1);
12122 if (TREE_CODE (probe) == VAR_DECL)
12124 /* A variable without external linkage might still be a
12125 valid constant-expression, so no error is issued here
12126 if the external-linkage check fails. */
12127 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12128 cp_parser_simulate_error (parser);
12130 else if (is_overloaded_fn (argument))
12131 /* All overloaded functions are allowed; if the external
12132 linkage test does not pass, an error will be issued
12133 later. */
12135 else if (address_p
12136 && (TREE_CODE (argument) == OFFSET_REF
12137 || TREE_CODE (argument) == SCOPE_REF))
12138 /* A pointer-to-member. */
12140 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12142 else
12143 cp_parser_simulate_error (parser);
12145 if (cp_parser_parse_definitely (parser))
12147 if (address_p)
12148 argument = build_x_unary_op (ADDR_EXPR, argument,
12149 tf_warning_or_error);
12150 return argument;
12154 /* If the argument started with "&", there are no other valid
12155 alternatives at this point. */
12156 if (address_p)
12158 cp_parser_error (parser, "invalid non-type template argument");
12159 return error_mark_node;
12162 /* If the argument wasn't successfully parsed as a type-id followed
12163 by '>>', the argument can only be a constant expression now.
12164 Otherwise, we try parsing the constant-expression tentatively,
12165 because the argument could really be a type-id. */
12166 if (maybe_type_id)
12167 cp_parser_parse_tentatively (parser);
12168 argument = cp_parser_constant_expression (parser,
12169 /*allow_non_constant_p=*/false,
12170 /*non_constant_p=*/NULL);
12171 argument = fold_non_dependent_expr (argument);
12172 if (!maybe_type_id)
12173 return argument;
12174 if (!cp_parser_next_token_ends_template_argument_p (parser))
12175 cp_parser_error (parser, "expected template-argument");
12176 if (cp_parser_parse_definitely (parser))
12177 return argument;
12178 /* We did our best to parse the argument as a non type-id, but that
12179 was the only alternative that matched (albeit with a '>' after
12180 it). We can assume it's just a typo from the user, and a
12181 diagnostic will then be issued. */
12182 return cp_parser_template_type_arg (parser);
12185 /* Parse an explicit-instantiation.
12187 explicit-instantiation:
12188 template declaration
12190 Although the standard says `declaration', what it really means is:
12192 explicit-instantiation:
12193 template decl-specifier-seq [opt] declarator [opt] ;
12195 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12196 supposed to be allowed. A defect report has been filed about this
12197 issue.
12199 GNU Extension:
12201 explicit-instantiation:
12202 storage-class-specifier template
12203 decl-specifier-seq [opt] declarator [opt] ;
12204 function-specifier template
12205 decl-specifier-seq [opt] declarator [opt] ; */
12207 static void
12208 cp_parser_explicit_instantiation (cp_parser* parser)
12210 int declares_class_or_enum;
12211 cp_decl_specifier_seq decl_specifiers;
12212 tree extension_specifier = NULL_TREE;
12214 /* Look for an (optional) storage-class-specifier or
12215 function-specifier. */
12216 if (cp_parser_allow_gnu_extensions_p (parser))
12218 extension_specifier
12219 = cp_parser_storage_class_specifier_opt (parser);
12220 if (!extension_specifier)
12221 extension_specifier
12222 = cp_parser_function_specifier_opt (parser,
12223 /*decl_specs=*/NULL);
12226 /* Look for the `template' keyword. */
12227 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12228 /* Let the front end know that we are processing an explicit
12229 instantiation. */
12230 begin_explicit_instantiation ();
12231 /* [temp.explicit] says that we are supposed to ignore access
12232 control while processing explicit instantiation directives. */
12233 push_deferring_access_checks (dk_no_check);
12234 /* Parse a decl-specifier-seq. */
12235 cp_parser_decl_specifier_seq (parser,
12236 CP_PARSER_FLAGS_OPTIONAL,
12237 &decl_specifiers,
12238 &declares_class_or_enum);
12239 /* If there was exactly one decl-specifier, and it declared a class,
12240 and there's no declarator, then we have an explicit type
12241 instantiation. */
12242 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12244 tree type;
12246 type = check_tag_decl (&decl_specifiers);
12247 /* Turn access control back on for names used during
12248 template instantiation. */
12249 pop_deferring_access_checks ();
12250 if (type)
12251 do_type_instantiation (type, extension_specifier,
12252 /*complain=*/tf_error);
12254 else
12256 cp_declarator *declarator;
12257 tree decl;
12259 /* Parse the declarator. */
12260 declarator
12261 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12262 /*ctor_dtor_or_conv_p=*/NULL,
12263 /*parenthesized_p=*/NULL,
12264 /*member_p=*/false);
12265 if (declares_class_or_enum & 2)
12266 cp_parser_check_for_definition_in_return_type (declarator,
12267 decl_specifiers.type,
12268 decl_specifiers.type_location);
12269 if (declarator != cp_error_declarator)
12271 if (decl_specifiers.specs[(int)ds_inline])
12272 permerror (input_location, "explicit instantiation shall not use"
12273 " %<inline%> specifier");
12274 if (decl_specifiers.specs[(int)ds_constexpr])
12275 permerror (input_location, "explicit instantiation shall not use"
12276 " %<constexpr%> specifier");
12278 decl = grokdeclarator (declarator, &decl_specifiers,
12279 NORMAL, 0, &decl_specifiers.attributes);
12280 /* Turn access control back on for names used during
12281 template instantiation. */
12282 pop_deferring_access_checks ();
12283 /* Do the explicit instantiation. */
12284 do_decl_instantiation (decl, extension_specifier);
12286 else
12288 pop_deferring_access_checks ();
12289 /* Skip the body of the explicit instantiation. */
12290 cp_parser_skip_to_end_of_statement (parser);
12293 /* We're done with the instantiation. */
12294 end_explicit_instantiation ();
12296 cp_parser_consume_semicolon_at_end_of_statement (parser);
12299 /* Parse an explicit-specialization.
12301 explicit-specialization:
12302 template < > declaration
12304 Although the standard says `declaration', what it really means is:
12306 explicit-specialization:
12307 template <> decl-specifier [opt] init-declarator [opt] ;
12308 template <> function-definition
12309 template <> explicit-specialization
12310 template <> template-declaration */
12312 static void
12313 cp_parser_explicit_specialization (cp_parser* parser)
12315 bool need_lang_pop;
12316 cp_token *token = cp_lexer_peek_token (parser->lexer);
12318 /* Look for the `template' keyword. */
12319 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12320 /* Look for the `<'. */
12321 cp_parser_require (parser, CPP_LESS, RT_LESS);
12322 /* Look for the `>'. */
12323 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12324 /* We have processed another parameter list. */
12325 ++parser->num_template_parameter_lists;
12326 /* [temp]
12328 A template ... explicit specialization ... shall not have C
12329 linkage. */
12330 if (current_lang_name == lang_name_c)
12332 error_at (token->location, "template specialization with C linkage");
12333 /* Give it C++ linkage to avoid confusing other parts of the
12334 front end. */
12335 push_lang_context (lang_name_cplusplus);
12336 need_lang_pop = true;
12338 else
12339 need_lang_pop = false;
12340 /* Let the front end know that we are beginning a specialization. */
12341 if (!begin_specialization ())
12343 end_specialization ();
12344 return;
12347 /* If the next keyword is `template', we need to figure out whether
12348 or not we're looking a template-declaration. */
12349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12351 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12352 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12353 cp_parser_template_declaration_after_export (parser,
12354 /*member_p=*/false);
12355 else
12356 cp_parser_explicit_specialization (parser);
12358 else
12359 /* Parse the dependent declaration. */
12360 cp_parser_single_declaration (parser,
12361 /*checks=*/NULL,
12362 /*member_p=*/false,
12363 /*explicit_specialization_p=*/true,
12364 /*friend_p=*/NULL);
12365 /* We're done with the specialization. */
12366 end_specialization ();
12367 /* For the erroneous case of a template with C linkage, we pushed an
12368 implicit C++ linkage scope; exit that scope now. */
12369 if (need_lang_pop)
12370 pop_lang_context ();
12371 /* We're done with this parameter list. */
12372 --parser->num_template_parameter_lists;
12375 /* Parse a type-specifier.
12377 type-specifier:
12378 simple-type-specifier
12379 class-specifier
12380 enum-specifier
12381 elaborated-type-specifier
12382 cv-qualifier
12384 GNU Extension:
12386 type-specifier:
12387 __complex__
12389 Returns a representation of the type-specifier. For a
12390 class-specifier, enum-specifier, or elaborated-type-specifier, a
12391 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12393 The parser flags FLAGS is used to control type-specifier parsing.
12395 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12396 in a decl-specifier-seq.
12398 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12399 class-specifier, enum-specifier, or elaborated-type-specifier, then
12400 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12401 if a type is declared; 2 if it is defined. Otherwise, it is set to
12402 zero.
12404 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12405 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12406 is set to FALSE. */
12408 static tree
12409 cp_parser_type_specifier (cp_parser* parser,
12410 cp_parser_flags flags,
12411 cp_decl_specifier_seq *decl_specs,
12412 bool is_declaration,
12413 int* declares_class_or_enum,
12414 bool* is_cv_qualifier)
12416 tree type_spec = NULL_TREE;
12417 cp_token *token;
12418 enum rid keyword;
12419 cp_decl_spec ds = ds_last;
12421 /* Assume this type-specifier does not declare a new type. */
12422 if (declares_class_or_enum)
12423 *declares_class_or_enum = 0;
12424 /* And that it does not specify a cv-qualifier. */
12425 if (is_cv_qualifier)
12426 *is_cv_qualifier = false;
12427 /* Peek at the next token. */
12428 token = cp_lexer_peek_token (parser->lexer);
12430 /* If we're looking at a keyword, we can use that to guide the
12431 production we choose. */
12432 keyword = token->keyword;
12433 switch (keyword)
12435 case RID_ENUM:
12436 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12437 goto elaborated_type_specifier;
12439 /* Look for the enum-specifier. */
12440 type_spec = cp_parser_enum_specifier (parser);
12441 /* If that worked, we're done. */
12442 if (type_spec)
12444 if (declares_class_or_enum)
12445 *declares_class_or_enum = 2;
12446 if (decl_specs)
12447 cp_parser_set_decl_spec_type (decl_specs,
12448 type_spec,
12449 token->location,
12450 /*user_defined_p=*/true);
12451 return type_spec;
12453 else
12454 goto elaborated_type_specifier;
12456 /* Any of these indicate either a class-specifier, or an
12457 elaborated-type-specifier. */
12458 case RID_CLASS:
12459 case RID_STRUCT:
12460 case RID_UNION:
12461 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12462 goto elaborated_type_specifier;
12464 /* Parse tentatively so that we can back up if we don't find a
12465 class-specifier. */
12466 cp_parser_parse_tentatively (parser);
12467 /* Look for the class-specifier. */
12468 type_spec = cp_parser_class_specifier (parser);
12469 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12470 /* If that worked, we're done. */
12471 if (cp_parser_parse_definitely (parser))
12473 if (declares_class_or_enum)
12474 *declares_class_or_enum = 2;
12475 if (decl_specs)
12476 cp_parser_set_decl_spec_type (decl_specs,
12477 type_spec,
12478 token->location,
12479 /*user_defined_p=*/true);
12480 return type_spec;
12483 /* Fall through. */
12484 elaborated_type_specifier:
12485 /* We're declaring (not defining) a class or enum. */
12486 if (declares_class_or_enum)
12487 *declares_class_or_enum = 1;
12489 /* Fall through. */
12490 case RID_TYPENAME:
12491 /* Look for an elaborated-type-specifier. */
12492 type_spec
12493 = (cp_parser_elaborated_type_specifier
12494 (parser,
12495 decl_specs && decl_specs->specs[(int) ds_friend],
12496 is_declaration));
12497 if (decl_specs)
12498 cp_parser_set_decl_spec_type (decl_specs,
12499 type_spec,
12500 token->location,
12501 /*user_defined_p=*/true);
12502 return type_spec;
12504 case RID_CONST:
12505 ds = ds_const;
12506 if (is_cv_qualifier)
12507 *is_cv_qualifier = true;
12508 break;
12510 case RID_VOLATILE:
12511 ds = ds_volatile;
12512 if (is_cv_qualifier)
12513 *is_cv_qualifier = true;
12514 break;
12516 case RID_RESTRICT:
12517 ds = ds_restrict;
12518 if (is_cv_qualifier)
12519 *is_cv_qualifier = true;
12520 break;
12522 case RID_COMPLEX:
12523 /* The `__complex__' keyword is a GNU extension. */
12524 ds = ds_complex;
12525 break;
12527 default:
12528 break;
12531 /* Handle simple keywords. */
12532 if (ds != ds_last)
12534 if (decl_specs)
12536 ++decl_specs->specs[(int)ds];
12537 decl_specs->any_specifiers_p = true;
12539 return cp_lexer_consume_token (parser->lexer)->u.value;
12542 /* If we do not already have a type-specifier, assume we are looking
12543 at a simple-type-specifier. */
12544 type_spec = cp_parser_simple_type_specifier (parser,
12545 decl_specs,
12546 flags);
12548 /* If we didn't find a type-specifier, and a type-specifier was not
12549 optional in this context, issue an error message. */
12550 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12552 cp_parser_error (parser, "expected type specifier");
12553 return error_mark_node;
12556 return type_spec;
12559 /* Parse a simple-type-specifier.
12561 simple-type-specifier:
12562 :: [opt] nested-name-specifier [opt] type-name
12563 :: [opt] nested-name-specifier template template-id
12564 char
12565 wchar_t
12566 bool
12567 short
12569 long
12570 signed
12571 unsigned
12572 float
12573 double
12574 void
12576 C++0x Extension:
12578 simple-type-specifier:
12579 auto
12580 decltype ( expression )
12581 char16_t
12582 char32_t
12584 GNU Extension:
12586 simple-type-specifier:
12587 __int128
12588 __typeof__ unary-expression
12589 __typeof__ ( type-id )
12591 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12592 appropriately updated. */
12594 static tree
12595 cp_parser_simple_type_specifier (cp_parser* parser,
12596 cp_decl_specifier_seq *decl_specs,
12597 cp_parser_flags flags)
12599 tree type = NULL_TREE;
12600 cp_token *token;
12602 /* Peek at the next token. */
12603 token = cp_lexer_peek_token (parser->lexer);
12605 /* If we're looking at a keyword, things are easy. */
12606 switch (token->keyword)
12608 case RID_CHAR:
12609 if (decl_specs)
12610 decl_specs->explicit_char_p = true;
12611 type = char_type_node;
12612 break;
12613 case RID_CHAR16:
12614 type = char16_type_node;
12615 break;
12616 case RID_CHAR32:
12617 type = char32_type_node;
12618 break;
12619 case RID_WCHAR:
12620 type = wchar_type_node;
12621 break;
12622 case RID_BOOL:
12623 type = boolean_type_node;
12624 break;
12625 case RID_SHORT:
12626 if (decl_specs)
12627 ++decl_specs->specs[(int) ds_short];
12628 type = short_integer_type_node;
12629 break;
12630 case RID_INT:
12631 if (decl_specs)
12632 decl_specs->explicit_int_p = true;
12633 type = integer_type_node;
12634 break;
12635 case RID_INT128:
12636 if (!int128_integer_type_node)
12637 break;
12638 if (decl_specs)
12639 decl_specs->explicit_int128_p = true;
12640 type = int128_integer_type_node;
12641 break;
12642 case RID_LONG:
12643 if (decl_specs)
12644 ++decl_specs->specs[(int) ds_long];
12645 type = long_integer_type_node;
12646 break;
12647 case RID_SIGNED:
12648 if (decl_specs)
12649 ++decl_specs->specs[(int) ds_signed];
12650 type = integer_type_node;
12651 break;
12652 case RID_UNSIGNED:
12653 if (decl_specs)
12654 ++decl_specs->specs[(int) ds_unsigned];
12655 type = unsigned_type_node;
12656 break;
12657 case RID_FLOAT:
12658 type = float_type_node;
12659 break;
12660 case RID_DOUBLE:
12661 type = double_type_node;
12662 break;
12663 case RID_VOID:
12664 type = void_type_node;
12665 break;
12667 case RID_AUTO:
12668 maybe_warn_cpp0x (CPP0X_AUTO);
12669 type = make_auto ();
12670 break;
12672 case RID_DECLTYPE:
12673 /* Parse the `decltype' type. */
12674 type = cp_parser_decltype (parser);
12676 if (decl_specs)
12677 cp_parser_set_decl_spec_type (decl_specs, type,
12678 token->location,
12679 /*user_defined_p=*/true);
12681 return type;
12683 case RID_TYPEOF:
12684 /* Consume the `typeof' token. */
12685 cp_lexer_consume_token (parser->lexer);
12686 /* Parse the operand to `typeof'. */
12687 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12688 /* If it is not already a TYPE, take its type. */
12689 if (!TYPE_P (type))
12690 type = finish_typeof (type);
12692 if (decl_specs)
12693 cp_parser_set_decl_spec_type (decl_specs, type,
12694 token->location,
12695 /*user_defined_p=*/true);
12697 return type;
12699 default:
12700 break;
12703 /* If the type-specifier was for a built-in type, we're done. */
12704 if (type)
12706 /* Record the type. */
12707 if (decl_specs
12708 && (token->keyword != RID_SIGNED
12709 && token->keyword != RID_UNSIGNED
12710 && token->keyword != RID_SHORT
12711 && token->keyword != RID_LONG))
12712 cp_parser_set_decl_spec_type (decl_specs,
12713 type,
12714 token->location,
12715 /*user_defined=*/false);
12716 if (decl_specs)
12717 decl_specs->any_specifiers_p = true;
12719 /* Consume the token. */
12720 cp_lexer_consume_token (parser->lexer);
12722 /* There is no valid C++ program where a non-template type is
12723 followed by a "<". That usually indicates that the user thought
12724 that the type was a template. */
12725 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12727 return TYPE_NAME (type);
12730 /* The type-specifier must be a user-defined type. */
12731 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12733 bool qualified_p;
12734 bool global_p;
12736 /* Don't gobble tokens or issue error messages if this is an
12737 optional type-specifier. */
12738 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12739 cp_parser_parse_tentatively (parser);
12741 /* Look for the optional `::' operator. */
12742 global_p
12743 = (cp_parser_global_scope_opt (parser,
12744 /*current_scope_valid_p=*/false)
12745 != NULL_TREE);
12746 /* Look for the nested-name specifier. */
12747 qualified_p
12748 = (cp_parser_nested_name_specifier_opt (parser,
12749 /*typename_keyword_p=*/false,
12750 /*check_dependency_p=*/true,
12751 /*type_p=*/false,
12752 /*is_declaration=*/false)
12753 != NULL_TREE);
12754 token = cp_lexer_peek_token (parser->lexer);
12755 /* If we have seen a nested-name-specifier, and the next token
12756 is `template', then we are using the template-id production. */
12757 if (parser->scope
12758 && cp_parser_optional_template_keyword (parser))
12760 /* Look for the template-id. */
12761 type = cp_parser_template_id (parser,
12762 /*template_keyword_p=*/true,
12763 /*check_dependency_p=*/true,
12764 /*is_declaration=*/false);
12765 /* If the template-id did not name a type, we are out of
12766 luck. */
12767 if (TREE_CODE (type) != TYPE_DECL)
12769 cp_parser_error (parser, "expected template-id for type");
12770 type = NULL_TREE;
12773 /* Otherwise, look for a type-name. */
12774 else
12775 type = cp_parser_type_name (parser);
12776 /* Keep track of all name-lookups performed in class scopes. */
12777 if (type
12778 && !global_p
12779 && !qualified_p
12780 && TREE_CODE (type) == TYPE_DECL
12781 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12782 maybe_note_name_used_in_class (DECL_NAME (type), type);
12783 /* If it didn't work out, we don't have a TYPE. */
12784 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12785 && !cp_parser_parse_definitely (parser))
12786 type = NULL_TREE;
12787 if (type && decl_specs)
12788 cp_parser_set_decl_spec_type (decl_specs, type,
12789 token->location,
12790 /*user_defined=*/true);
12793 /* If we didn't get a type-name, issue an error message. */
12794 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12796 cp_parser_error (parser, "expected type-name");
12797 return error_mark_node;
12800 if (type && type != error_mark_node)
12802 PLUGIN_SIMPLE_TYPE_SPECIFIER;
12804 /* There is no valid C++ program where a non-template type is
12805 followed by a "<". That usually indicates that the user
12806 thought that the type was a template. */
12807 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12808 token->location);
12811 return type;
12814 /* Parse a type-name.
12816 type-name:
12817 class-name
12818 enum-name
12819 typedef-name
12821 enum-name:
12822 identifier
12824 typedef-name:
12825 identifier
12827 Returns a TYPE_DECL for the type. */
12829 static tree
12830 cp_parser_type_name (cp_parser* parser)
12832 tree type_decl;
12834 /* We can't know yet whether it is a class-name or not. */
12835 cp_parser_parse_tentatively (parser);
12836 /* Try a class-name. */
12837 type_decl = cp_parser_class_name (parser,
12838 /*typename_keyword_p=*/false,
12839 /*template_keyword_p=*/false,
12840 none_type,
12841 /*check_dependency_p=*/true,
12842 /*class_head_p=*/false,
12843 /*is_declaration=*/false);
12844 /* If it's not a class-name, keep looking. */
12845 if (!cp_parser_parse_definitely (parser))
12847 /* It must be a typedef-name or an enum-name. */
12848 return cp_parser_nonclass_name (parser);
12851 return type_decl;
12854 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12856 enum-name:
12857 identifier
12859 typedef-name:
12860 identifier
12862 Returns a TYPE_DECL for the type. */
12864 static tree
12865 cp_parser_nonclass_name (cp_parser* parser)
12867 tree type_decl;
12868 tree identifier;
12870 cp_token *token = cp_lexer_peek_token (parser->lexer);
12871 identifier = cp_parser_identifier (parser);
12872 if (identifier == error_mark_node)
12873 return error_mark_node;
12875 /* Look up the type-name. */
12876 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12878 PLUGIN_NONCLASS_NAME1;
12880 /* Issue an error if we did not find a type-name. */
12881 if (TREE_CODE (type_decl) != TYPE_DECL
12882 PLUGIN_NONCLASS_NAME)
12884 if (!cp_parser_simulate_error (parser))
12885 cp_parser_name_lookup_error (parser, identifier, type_decl,
12886 NLE_TYPE, token->location);
12887 return error_mark_node;
12889 /* Remember that the name was used in the definition of the
12890 current class so that we can check later to see if the
12891 meaning would have been different after the class was
12892 entirely defined. */
12893 else if (type_decl != error_mark_node
12894 && !parser->scope)
12895 maybe_note_name_used_in_class (identifier, type_decl);
12897 return type_decl;
12900 /* Parse an elaborated-type-specifier. Note that the grammar given
12901 here incorporates the resolution to DR68.
12903 elaborated-type-specifier:
12904 class-key :: [opt] nested-name-specifier [opt] identifier
12905 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12906 enum-key :: [opt] nested-name-specifier [opt] identifier
12907 typename :: [opt] nested-name-specifier identifier
12908 typename :: [opt] nested-name-specifier template [opt]
12909 template-id
12911 GNU extension:
12913 elaborated-type-specifier:
12914 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12915 class-key attributes :: [opt] nested-name-specifier [opt]
12916 template [opt] template-id
12917 enum attributes :: [opt] nested-name-specifier [opt] identifier
12919 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12920 declared `friend'. If IS_DECLARATION is TRUE, then this
12921 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12922 something is being declared.
12924 Returns the TYPE specified. */
12926 static tree
12927 cp_parser_elaborated_type_specifier (cp_parser* parser,
12928 bool is_friend,
12929 bool is_declaration)
12931 enum tag_types tag_type;
12932 tree identifier;
12933 tree type = NULL_TREE;
12934 tree attributes = NULL_TREE;
12935 tree globalscope;
12936 cp_token *token = NULL;
12938 /* See if we're looking at the `enum' keyword. */
12939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12941 /* Consume the `enum' token. */
12942 cp_lexer_consume_token (parser->lexer);
12943 /* Remember that it's an enumeration type. */
12944 tag_type = enum_type;
12945 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12946 enums) is used here. */
12947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12948 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12950 pedwarn (input_location, 0, "elaborated-type-specifier "
12951 "for a scoped enum must not use the %<%D%> keyword",
12952 cp_lexer_peek_token (parser->lexer)->u.value);
12953 /* Consume the `struct' or `class' and parse it anyway. */
12954 cp_lexer_consume_token (parser->lexer);
12956 /* Parse the attributes. */
12957 attributes = cp_parser_attributes_opt (parser);
12959 /* Or, it might be `typename'. */
12960 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12961 RID_TYPENAME))
12963 /* Consume the `typename' token. */
12964 cp_lexer_consume_token (parser->lexer);
12965 /* Remember that it's a `typename' type. */
12966 tag_type = typename_type;
12968 /* Otherwise it must be a class-key. */
12969 else
12971 tag_type = cp_parser_class_key (parser);
12972 if (tag_type == none_type)
12973 return error_mark_node;
12974 /* Parse the attributes. */
12975 attributes = cp_parser_attributes_opt (parser);
12978 /* Look for the `::' operator. */
12979 globalscope = cp_parser_global_scope_opt (parser,
12980 /*current_scope_valid_p=*/false);
12981 /* Look for the nested-name-specifier. */
12982 if (tag_type == typename_type && !globalscope)
12984 if (!cp_parser_nested_name_specifier (parser,
12985 /*typename_keyword_p=*/true,
12986 /*check_dependency_p=*/true,
12987 /*type_p=*/true,
12988 is_declaration))
12989 return error_mark_node;
12991 else
12992 /* Even though `typename' is not present, the proposed resolution
12993 to Core Issue 180 says that in `class A<T>::B', `B' should be
12994 considered a type-name, even if `A<T>' is dependent. */
12995 cp_parser_nested_name_specifier_opt (parser,
12996 /*typename_keyword_p=*/true,
12997 /*check_dependency_p=*/true,
12998 /*type_p=*/true,
12999 is_declaration);
13000 /* For everything but enumeration types, consider a template-id.
13001 For an enumeration type, consider only a plain identifier. */
13002 if (tag_type != enum_type)
13004 bool template_p = false;
13005 tree decl;
13007 /* Allow the `template' keyword. */
13008 template_p = cp_parser_optional_template_keyword (parser);
13009 /* If we didn't see `template', we don't know if there's a
13010 template-id or not. */
13011 if (!template_p)
13012 cp_parser_parse_tentatively (parser);
13013 /* Parse the template-id. */
13014 token = cp_lexer_peek_token (parser->lexer);
13015 decl = cp_parser_template_id (parser, template_p,
13016 /*check_dependency_p=*/true,
13017 is_declaration);
13018 /* If we didn't find a template-id, look for an ordinary
13019 identifier. */
13020 if (!template_p && !cp_parser_parse_definitely (parser))
13022 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13023 in effect, then we must assume that, upon instantiation, the
13024 template will correspond to a class. */
13025 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13026 && tag_type == typename_type)
13027 type = make_typename_type (parser->scope, decl,
13028 typename_type,
13029 /*complain=*/tf_error);
13030 /* If the `typename' keyword is in effect and DECL is not a type
13031 decl. Then type is non existant. */
13032 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13033 type = NULL_TREE;
13034 else
13035 type = TREE_TYPE (decl);
13038 if (!type)
13040 token = cp_lexer_peek_token (parser->lexer);
13041 identifier = cp_parser_identifier (parser);
13043 if (identifier == error_mark_node)
13045 parser->scope = NULL_TREE;
13046 return error_mark_node;
13049 /* For a `typename', we needn't call xref_tag. */
13050 if (tag_type == typename_type
13051 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13052 return cp_parser_make_typename_type (parser, parser->scope,
13053 identifier,
13054 token->location);
13055 /* Look up a qualified name in the usual way. */
13056 if (parser->scope)
13058 tree decl;
13059 tree ambiguous_decls;
13061 decl = cp_parser_lookup_name (parser, identifier,
13062 tag_type,
13063 /*is_template=*/false,
13064 /*is_namespace=*/false,
13065 /*check_dependency=*/true,
13066 &ambiguous_decls,
13067 token->location);
13069 /* If the lookup was ambiguous, an error will already have been
13070 issued. */
13071 if (ambiguous_decls)
13072 return error_mark_node;
13074 /* If we are parsing friend declaration, DECL may be a
13075 TEMPLATE_DECL tree node here. However, we need to check
13076 whether this TEMPLATE_DECL results in valid code. Consider
13077 the following example:
13079 namespace N {
13080 template <class T> class C {};
13082 class X {
13083 template <class T> friend class N::C; // #1, valid code
13085 template <class T> class Y {
13086 friend class N::C; // #2, invalid code
13089 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13090 name lookup of `N::C'. We see that friend declaration must
13091 be template for the code to be valid. Note that
13092 processing_template_decl does not work here since it is
13093 always 1 for the above two cases. */
13095 decl = (cp_parser_maybe_treat_template_as_class
13096 (decl, /*tag_name_p=*/is_friend
13097 && parser->num_template_parameter_lists));
13099 if (TREE_CODE (decl) != TYPE_DECL)
13101 cp_parser_diagnose_invalid_type_name (parser,
13102 parser->scope,
13103 identifier,
13104 token->location);
13105 return error_mark_node;
13108 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13110 bool allow_template = (parser->num_template_parameter_lists
13111 || DECL_SELF_REFERENCE_P (decl));
13112 type = check_elaborated_type_specifier (tag_type, decl,
13113 allow_template);
13115 if (type == error_mark_node)
13116 return error_mark_node;
13119 /* Forward declarations of nested types, such as
13121 class C1::C2;
13122 class C1::C2::C3;
13124 are invalid unless all components preceding the final '::'
13125 are complete. If all enclosing types are complete, these
13126 declarations become merely pointless.
13128 Invalid forward declarations of nested types are errors
13129 caught elsewhere in parsing. Those that are pointless arrive
13130 here. */
13132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13133 && !is_friend && !processing_explicit_instantiation)
13134 warning (0, "declaration %qD does not declare anything", decl);
13136 type = TREE_TYPE (decl);
13138 else
13140 /* An elaborated-type-specifier sometimes introduces a new type and
13141 sometimes names an existing type. Normally, the rule is that it
13142 introduces a new type only if there is not an existing type of
13143 the same name already in scope. For example, given:
13145 struct S {};
13146 void f() { struct S s; }
13148 the `struct S' in the body of `f' is the same `struct S' as in
13149 the global scope; the existing definition is used. However, if
13150 there were no global declaration, this would introduce a new
13151 local class named `S'.
13153 An exception to this rule applies to the following code:
13155 namespace N { struct S; }
13157 Here, the elaborated-type-specifier names a new type
13158 unconditionally; even if there is already an `S' in the
13159 containing scope this declaration names a new type.
13160 This exception only applies if the elaborated-type-specifier
13161 forms the complete declaration:
13163 [class.name]
13165 A declaration consisting solely of `class-key identifier ;' is
13166 either a redeclaration of the name in the current scope or a
13167 forward declaration of the identifier as a class name. It
13168 introduces the name into the current scope.
13170 We are in this situation precisely when the next token is a `;'.
13172 An exception to the exception is that a `friend' declaration does
13173 *not* name a new type; i.e., given:
13175 struct S { friend struct T; };
13177 `T' is not a new type in the scope of `S'.
13179 Also, `new struct S' or `sizeof (struct S)' never results in the
13180 definition of a new type; a new type can only be declared in a
13181 declaration context. */
13183 tag_scope ts;
13184 bool template_p;
13186 if (is_friend)
13187 /* Friends have special name lookup rules. */
13188 ts = ts_within_enclosing_non_class;
13189 else if (is_declaration
13190 && cp_lexer_next_token_is (parser->lexer,
13191 CPP_SEMICOLON))
13192 /* This is a `class-key identifier ;' */
13193 ts = ts_current;
13194 else
13195 ts = ts_global;
13197 template_p =
13198 (parser->num_template_parameter_lists
13199 && (cp_parser_next_token_starts_class_definition_p (parser)
13200 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13201 /* An unqualified name was used to reference this type, so
13202 there were no qualifying templates. */
13203 if (!cp_parser_check_template_parameters (parser,
13204 /*num_templates=*/0,
13205 token->location,
13206 /*declarator=*/NULL))
13207 return error_mark_node;
13208 type = xref_tag (tag_type, identifier, ts, template_p);
13212 if (type == error_mark_node)
13213 return error_mark_node;
13215 /* Allow attributes on forward declarations of classes. */
13216 if (attributes)
13218 if (TREE_CODE (type) == TYPENAME_TYPE)
13219 warning (OPT_Wattributes,
13220 "attributes ignored on uninstantiated type");
13221 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13222 && ! processing_explicit_instantiation)
13223 warning (OPT_Wattributes,
13224 "attributes ignored on template instantiation");
13225 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13226 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13227 else
13228 warning (OPT_Wattributes,
13229 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13232 if (tag_type != enum_type)
13233 cp_parser_check_class_key (tag_type, type);
13235 /* A "<" cannot follow an elaborated type specifier. If that
13236 happens, the user was probably trying to form a template-id. */
13237 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13239 return type;
13242 /* Parse an enum-specifier.
13244 enum-specifier:
13245 enum-head { enumerator-list [opt] }
13247 enum-head:
13248 enum-key identifier [opt] enum-base [opt]
13249 enum-key nested-name-specifier identifier enum-base [opt]
13251 enum-key:
13252 enum
13253 enum class [C++0x]
13254 enum struct [C++0x]
13256 enum-base: [C++0x]
13257 : type-specifier-seq
13259 opaque-enum-specifier:
13260 enum-key identifier enum-base [opt] ;
13262 GNU Extensions:
13263 enum-key attributes[opt] identifier [opt] enum-base [opt]
13264 { enumerator-list [opt] }attributes[opt]
13266 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13267 if the token stream isn't an enum-specifier after all. */
13269 static tree
13270 cp_parser_enum_specifier (cp_parser* parser)
13272 tree identifier;
13273 tree type = NULL_TREE;
13274 tree prev_scope;
13275 tree nested_name_specifier = NULL_TREE;
13276 tree attributes;
13277 bool scoped_enum_p = false;
13278 bool has_underlying_type = false;
13279 bool nested_being_defined = false;
13280 bool new_value_list = false;
13281 bool is_new_type = false;
13282 bool is_anonymous = false;
13283 tree underlying_type = NULL_TREE;
13284 cp_token *type_start_token = NULL;
13285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13287 parser->colon_corrects_to_scope_p = false;
13289 /* Parse tentatively so that we can back up if we don't find a
13290 enum-specifier. */
13291 cp_parser_parse_tentatively (parser);
13293 /* Caller guarantees that the current token is 'enum', an identifier
13294 possibly follows, and the token after that is an opening brace.
13295 If we don't have an identifier, fabricate an anonymous name for
13296 the enumeration being defined. */
13297 cp_lexer_consume_token (parser->lexer);
13299 /* Parse the "class" or "struct", which indicates a scoped
13300 enumeration type in C++0x. */
13301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13302 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13304 if (cxx_dialect < cxx0x)
13305 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13307 /* Consume the `struct' or `class' token. */
13308 cp_lexer_consume_token (parser->lexer);
13310 scoped_enum_p = true;
13313 attributes = cp_parser_attributes_opt (parser);
13315 /* Clear the qualification. */
13316 parser->scope = NULL_TREE;
13317 parser->qualifying_scope = NULL_TREE;
13318 parser->object_scope = NULL_TREE;
13320 /* Figure out in what scope the declaration is being placed. */
13321 prev_scope = current_scope ();
13323 type_start_token = cp_lexer_peek_token (parser->lexer);
13325 push_deferring_access_checks (dk_no_check);
13326 nested_name_specifier
13327 = cp_parser_nested_name_specifier_opt (parser,
13328 /*typename_keyword_p=*/true,
13329 /*check_dependency_p=*/false,
13330 /*type_p=*/false,
13331 /*is_declaration=*/false);
13333 if (nested_name_specifier)
13335 tree name;
13337 identifier = cp_parser_identifier (parser);
13338 name = cp_parser_lookup_name (parser, identifier,
13339 enum_type,
13340 /*is_template=*/false,
13341 /*is_namespace=*/false,
13342 /*check_dependency=*/true,
13343 /*ambiguous_decls=*/NULL,
13344 input_location);
13345 if (name)
13347 type = TREE_TYPE (name);
13348 if (TREE_CODE (type) == TYPENAME_TYPE)
13350 /* Are template enums allowed in ISO? */
13351 if (template_parm_scope_p ())
13352 pedwarn (type_start_token->location, OPT_pedantic,
13353 "%qD is an enumeration template", name);
13354 /* ignore a typename reference, for it will be solved by name
13355 in start_enum. */
13356 type = NULL_TREE;
13359 else
13360 error_at (type_start_token->location,
13361 "%qD is not an enumerator-name", identifier);
13363 else
13365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13366 identifier = cp_parser_identifier (parser);
13367 else
13369 identifier = make_anon_name ();
13370 is_anonymous = true;
13373 pop_deferring_access_checks ();
13375 /* Check for the `:' that denotes a specified underlying type in C++0x.
13376 Note that a ':' could also indicate a bitfield width, however. */
13377 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13379 cp_decl_specifier_seq type_specifiers;
13381 /* Consume the `:'. */
13382 cp_lexer_consume_token (parser->lexer);
13384 /* Parse the type-specifier-seq. */
13385 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13386 /*is_trailing_return=*/false,
13387 &type_specifiers);
13389 /* At this point this is surely not elaborated type specifier. */
13390 if (!cp_parser_parse_definitely (parser))
13391 return NULL_TREE;
13393 if (cxx_dialect < cxx0x)
13394 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13396 has_underlying_type = true;
13398 /* If that didn't work, stop. */
13399 if (type_specifiers.type != error_mark_node)
13401 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13402 /*initialized=*/0, NULL);
13403 if (underlying_type == error_mark_node)
13404 underlying_type = NULL_TREE;
13408 /* Look for the `{' but don't consume it yet. */
13409 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13411 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13413 cp_parser_error (parser, "expected %<{%>");
13414 if (has_underlying_type)
13416 type = NULL_TREE;
13417 goto out;
13420 /* An opaque-enum-specifier must have a ';' here. */
13421 if ((scoped_enum_p || underlying_type)
13422 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13424 cp_parser_error (parser, "expected %<;%> or %<{%>");
13425 if (has_underlying_type)
13427 type = NULL_TREE;
13428 goto out;
13433 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13434 return NULL_TREE;
13436 if (nested_name_specifier)
13438 if (CLASS_TYPE_P (nested_name_specifier))
13440 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13441 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13442 push_scope (nested_name_specifier);
13444 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13446 push_nested_namespace (nested_name_specifier);
13450 /* Issue an error message if type-definitions are forbidden here. */
13451 if (!cp_parser_check_type_definition (parser))
13452 type = error_mark_node;
13453 else
13454 /* Create the new type. We do this before consuming the opening
13455 brace so the enum will be recorded as being on the line of its
13456 tag (or the 'enum' keyword, if there is no tag). */
13457 type = start_enum (identifier, type, underlying_type,
13458 scoped_enum_p, &is_new_type);
13460 /* If the next token is not '{' it is an opaque-enum-specifier or an
13461 elaborated-type-specifier. */
13462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13464 if (nested_name_specifier)
13466 /* The following catches invalid code such as:
13467 enum class S<int>::E { A, B, C }; */
13468 if (!processing_specialization
13469 && CLASS_TYPE_P (nested_name_specifier)
13470 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13471 error_at (type_start_token->location, "cannot add an enumerator "
13472 "list to a template instantiation");
13474 /* If that scope does not contain the scope in which the
13475 class was originally declared, the program is invalid. */
13476 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13478 if (at_namespace_scope_p ())
13479 error_at (type_start_token->location,
13480 "declaration of %qD in namespace %qD which does not "
13481 "enclose %qD",
13482 type, prev_scope, nested_name_specifier);
13483 else
13484 error_at (type_start_token->location,
13485 "declaration of %qD in %qD which does not enclose %qD",
13486 type, prev_scope, nested_name_specifier);
13487 type = error_mark_node;
13491 if (scoped_enum_p)
13492 begin_scope (sk_scoped_enum, type);
13494 /* Consume the opening brace. */
13495 cp_lexer_consume_token (parser->lexer);
13497 if (type == error_mark_node)
13498 ; /* Nothing to add */
13499 else if (OPAQUE_ENUM_P (type)
13500 || (cxx_dialect > cxx98 && processing_specialization))
13502 new_value_list = true;
13503 SET_OPAQUE_ENUM_P (type, false);
13504 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13506 else
13508 error_at (type_start_token->location, "multiple definition of %q#T", type);
13509 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13510 "previous definition here");
13511 type = error_mark_node;
13514 if (type == error_mark_node)
13515 cp_parser_skip_to_end_of_block_or_statement (parser);
13516 /* If the next token is not '}', then there are some enumerators. */
13517 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13518 cp_parser_enumerator_list (parser, type);
13520 /* Consume the final '}'. */
13521 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13523 if (scoped_enum_p)
13524 finish_scope ();
13526 else
13528 /* If a ';' follows, then it is an opaque-enum-specifier
13529 and additional restrictions apply. */
13530 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13532 if (is_anonymous)
13533 error_at (type_start_token->location,
13534 "opaque-enum-specifier without name");
13535 else if (nested_name_specifier)
13536 error_at (type_start_token->location,
13537 "opaque-enum-specifier must use a simple identifier");
13541 /* Look for trailing attributes to apply to this enumeration, and
13542 apply them if appropriate. */
13543 if (cp_parser_allow_gnu_extensions_p (parser))
13545 tree trailing_attr = cp_parser_attributes_opt (parser);
13546 trailing_attr = chainon (trailing_attr, attributes);
13547 cplus_decl_attributes (&type,
13548 trailing_attr,
13549 (int) ATTR_FLAG_TYPE_IN_PLACE);
13552 /* Finish up the enumeration. */
13553 if (type != error_mark_node)
13555 if (new_value_list)
13556 finish_enum_value_list (type);
13557 if (is_new_type)
13558 finish_enum (type);
13561 if (nested_name_specifier)
13563 if (CLASS_TYPE_P (nested_name_specifier))
13565 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13566 pop_scope (nested_name_specifier);
13568 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13570 pop_nested_namespace (nested_name_specifier);
13573 out:
13574 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13575 return type;
13578 /* Parse an enumerator-list. The enumerators all have the indicated
13579 TYPE.
13581 enumerator-list:
13582 enumerator-definition
13583 enumerator-list , enumerator-definition */
13585 static void
13586 cp_parser_enumerator_list (cp_parser* parser, tree type)
13588 while (true)
13590 /* Parse an enumerator-definition. */
13591 cp_parser_enumerator_definition (parser, type);
13593 /* If the next token is not a ',', we've reached the end of
13594 the list. */
13595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13596 break;
13597 /* Otherwise, consume the `,' and keep going. */
13598 cp_lexer_consume_token (parser->lexer);
13599 /* If the next token is a `}', there is a trailing comma. */
13600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13602 if (!in_system_header)
13603 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13604 break;
13609 /* Parse an enumerator-definition. The enumerator has the indicated
13610 TYPE.
13612 enumerator-definition:
13613 enumerator
13614 enumerator = constant-expression
13616 enumerator:
13617 identifier */
13619 static void
13620 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13622 tree identifier;
13623 tree value;
13624 location_t loc;
13626 /* Save the input location because we are interested in the location
13627 of the identifier and not the location of the explicit value. */
13628 loc = cp_lexer_peek_token (parser->lexer)->location;
13630 /* Look for the identifier. */
13631 identifier = cp_parser_identifier (parser);
13632 if (identifier == error_mark_node)
13633 return;
13635 /* If the next token is an '=', then there is an explicit value. */
13636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13638 /* Consume the `=' token. */
13639 cp_lexer_consume_token (parser->lexer);
13640 /* Parse the value. */
13641 value = cp_parser_constant_expression (parser,
13642 /*allow_non_constant_p=*/false,
13643 NULL);
13645 else
13646 value = NULL_TREE;
13648 /* If we are processing a template, make sure the initializer of the
13649 enumerator doesn't contain any bare template parameter pack. */
13650 if (check_for_bare_parameter_packs (value))
13651 value = error_mark_node;
13653 /* Create the enumerator. */
13654 build_enumerator (identifier, value, type, loc);
13657 /* Parse a namespace-name.
13659 namespace-name:
13660 original-namespace-name
13661 namespace-alias
13663 Returns the NAMESPACE_DECL for the namespace. */
13665 static tree
13666 cp_parser_namespace_name (cp_parser* parser)
13668 tree identifier;
13669 tree namespace_decl;
13671 cp_token *token = cp_lexer_peek_token (parser->lexer);
13673 /* Get the name of the namespace. */
13674 identifier = cp_parser_identifier (parser);
13675 if (identifier == error_mark_node)
13676 return error_mark_node;
13678 /* Look up the identifier in the currently active scope. Look only
13679 for namespaces, due to:
13681 [basic.lookup.udir]
13683 When looking up a namespace-name in a using-directive or alias
13684 definition, only namespace names are considered.
13686 And:
13688 [basic.lookup.qual]
13690 During the lookup of a name preceding the :: scope resolution
13691 operator, object, function, and enumerator names are ignored.
13693 (Note that cp_parser_qualifying_entity only calls this
13694 function if the token after the name is the scope resolution
13695 operator.) */
13696 namespace_decl = cp_parser_lookup_name (parser, identifier,
13697 none_type,
13698 /*is_template=*/false,
13699 /*is_namespace=*/true,
13700 /*check_dependency=*/true,
13701 /*ambiguous_decls=*/NULL,
13702 token->location);
13703 /* If it's not a namespace, issue an error. */
13704 if (namespace_decl == error_mark_node
13705 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13707 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13708 error_at (token->location, "%qD is not a namespace-name", identifier);
13709 cp_parser_error (parser, "expected namespace-name");
13710 namespace_decl = error_mark_node;
13713 return namespace_decl;
13716 /* Parse a namespace-definition.
13718 namespace-definition:
13719 named-namespace-definition
13720 unnamed-namespace-definition
13722 named-namespace-definition:
13723 original-namespace-definition
13724 extension-namespace-definition
13726 original-namespace-definition:
13727 namespace identifier { namespace-body }
13729 extension-namespace-definition:
13730 namespace original-namespace-name { namespace-body }
13732 unnamed-namespace-definition:
13733 namespace { namespace-body } */
13735 static void
13736 cp_parser_namespace_definition (cp_parser* parser)
13738 tree identifier, attribs;
13739 bool has_visibility;
13740 bool is_inline;
13742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13744 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13745 is_inline = true;
13746 cp_lexer_consume_token (parser->lexer);
13748 else
13749 is_inline = false;
13751 /* Look for the `namespace' keyword. */
13752 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13754 /* Get the name of the namespace. We do not attempt to distinguish
13755 between an original-namespace-definition and an
13756 extension-namespace-definition at this point. The semantic
13757 analysis routines are responsible for that. */
13758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13759 identifier = cp_parser_identifier (parser);
13760 else
13761 identifier = NULL_TREE;
13763 /* Parse any specified attributes. */
13764 attribs = cp_parser_attributes_opt (parser);
13766 /* Look for the `{' to start the namespace. */
13767 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13768 /* Start the namespace. */
13769 push_namespace (identifier);
13771 /* "inline namespace" is equivalent to a stub namespace definition
13772 followed by a strong using directive. */
13773 if (is_inline)
13775 tree name_space = current_namespace;
13776 /* Set up namespace association. */
13777 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13778 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13779 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13780 /* Import the contents of the inline namespace. */
13781 pop_namespace ();
13782 do_using_directive (name_space);
13783 push_namespace (identifier);
13786 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13788 /* Parse the body of the namespace. */
13789 cp_parser_namespace_body (parser);
13791 if (has_visibility)
13792 pop_visibility (1);
13794 /* Finish the namespace. */
13795 pop_namespace ();
13796 /* Look for the final `}'. */
13797 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13800 /* Parse a namespace-body.
13802 namespace-body:
13803 declaration-seq [opt] */
13805 static void
13806 cp_parser_namespace_body (cp_parser* parser)
13808 cp_parser_declaration_seq_opt (parser);
13811 /* Parse a namespace-alias-definition.
13813 namespace-alias-definition:
13814 namespace identifier = qualified-namespace-specifier ; */
13816 static void
13817 cp_parser_namespace_alias_definition (cp_parser* parser)
13819 tree identifier;
13820 tree namespace_specifier;
13822 cp_token *token = cp_lexer_peek_token (parser->lexer);
13824 /* Look for the `namespace' keyword. */
13825 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13826 /* Look for the identifier. */
13827 identifier = cp_parser_identifier (parser);
13828 if (identifier == error_mark_node)
13829 return;
13830 /* Look for the `=' token. */
13831 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13832 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13834 error_at (token->location, "%<namespace%> definition is not allowed here");
13835 /* Skip the definition. */
13836 cp_lexer_consume_token (parser->lexer);
13837 if (cp_parser_skip_to_closing_brace (parser))
13838 cp_lexer_consume_token (parser->lexer);
13839 return;
13841 cp_parser_require (parser, CPP_EQ, RT_EQ);
13842 /* Look for the qualified-namespace-specifier. */
13843 namespace_specifier
13844 = cp_parser_qualified_namespace_specifier (parser);
13845 /* Look for the `;' token. */
13846 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13848 /* Register the alias in the symbol table. */
13849 do_namespace_alias (identifier, namespace_specifier);
13852 /* Parse a qualified-namespace-specifier.
13854 qualified-namespace-specifier:
13855 :: [opt] nested-name-specifier [opt] namespace-name
13857 Returns a NAMESPACE_DECL corresponding to the specified
13858 namespace. */
13860 static tree
13861 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13863 /* Look for the optional `::'. */
13864 cp_parser_global_scope_opt (parser,
13865 /*current_scope_valid_p=*/false);
13867 /* Look for the optional nested-name-specifier. */
13868 cp_parser_nested_name_specifier_opt (parser,
13869 /*typename_keyword_p=*/false,
13870 /*check_dependency_p=*/true,
13871 /*type_p=*/false,
13872 /*is_declaration=*/true);
13874 return cp_parser_namespace_name (parser);
13877 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13878 access declaration.
13880 using-declaration:
13881 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13882 using :: unqualified-id ;
13884 access-declaration:
13885 qualified-id ;
13889 static bool
13890 cp_parser_using_declaration (cp_parser* parser,
13891 bool access_declaration_p)
13893 cp_token *token;
13894 bool typename_p = false;
13895 bool global_scope_p;
13896 tree decl;
13897 tree identifier;
13898 tree qscope;
13900 if (access_declaration_p)
13901 cp_parser_parse_tentatively (parser);
13902 else
13904 /* Look for the `using' keyword. */
13905 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13907 /* Peek at the next token. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 /* See if it's `typename'. */
13910 if (token->keyword == RID_TYPENAME)
13912 /* Remember that we've seen it. */
13913 typename_p = true;
13914 /* Consume the `typename' token. */
13915 cp_lexer_consume_token (parser->lexer);
13919 /* Look for the optional global scope qualification. */
13920 global_scope_p
13921 = (cp_parser_global_scope_opt (parser,
13922 /*current_scope_valid_p=*/false)
13923 != NULL_TREE);
13925 /* If we saw `typename', or didn't see `::', then there must be a
13926 nested-name-specifier present. */
13927 if (typename_p || !global_scope_p)
13928 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13929 /*check_dependency_p=*/true,
13930 /*type_p=*/false,
13931 /*is_declaration=*/true);
13932 /* Otherwise, we could be in either of the two productions. In that
13933 case, treat the nested-name-specifier as optional. */
13934 else
13935 qscope = cp_parser_nested_name_specifier_opt (parser,
13936 /*typename_keyword_p=*/false,
13937 /*check_dependency_p=*/true,
13938 /*type_p=*/false,
13939 /*is_declaration=*/true);
13940 if (!qscope)
13941 qscope = global_namespace;
13943 if (access_declaration_p && cp_parser_error_occurred (parser))
13944 /* Something has already gone wrong; there's no need to parse
13945 further. Since an error has occurred, the return value of
13946 cp_parser_parse_definitely will be false, as required. */
13947 return cp_parser_parse_definitely (parser);
13949 token = cp_lexer_peek_token (parser->lexer);
13950 /* Parse the unqualified-id. */
13951 identifier = cp_parser_unqualified_id (parser,
13952 /*template_keyword_p=*/false,
13953 /*check_dependency_p=*/true,
13954 /*declarator_p=*/true,
13955 /*optional_p=*/false);
13957 if (access_declaration_p)
13959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13960 cp_parser_simulate_error (parser);
13961 if (!cp_parser_parse_definitely (parser))
13962 return false;
13965 /* The function we call to handle a using-declaration is different
13966 depending on what scope we are in. */
13967 if (qscope == error_mark_node || identifier == error_mark_node)
13969 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13970 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13971 /* [namespace.udecl]
13973 A using declaration shall not name a template-id. */
13974 error_at (token->location,
13975 "a template-id may not appear in a using-declaration");
13976 else
13978 if (at_class_scope_p ())
13980 /* Create the USING_DECL. */
13981 decl = do_class_using_decl (parser->scope, identifier);
13983 if (check_for_bare_parameter_packs (decl))
13984 return false;
13985 else
13986 /* Add it to the list of members in this class. */
13987 finish_member_declaration (decl);
13989 else
13991 decl = cp_parser_lookup_name_simple (parser,
13992 identifier,
13993 token->location);
13994 if (decl == error_mark_node)
13995 cp_parser_name_lookup_error (parser, identifier,
13996 decl, NLE_NULL,
13997 token->location);
13998 else if (check_for_bare_parameter_packs (decl))
13999 return false;
14000 else if (!at_namespace_scope_p ())
14001 do_local_using_decl (decl, qscope, identifier);
14002 else
14003 do_toplevel_using_decl (decl, qscope, identifier);
14007 /* Look for the final `;'. */
14008 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14010 return true;
14013 /* Parse a using-directive.
14015 using-directive:
14016 using namespace :: [opt] nested-name-specifier [opt]
14017 namespace-name ; */
14019 static void
14020 cp_parser_using_directive (cp_parser* parser)
14022 tree namespace_decl;
14023 tree attribs;
14025 /* Look for the `using' keyword. */
14026 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14027 /* And the `namespace' keyword. */
14028 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14029 /* Look for the optional `::' operator. */
14030 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14031 /* And the optional nested-name-specifier. */
14032 cp_parser_nested_name_specifier_opt (parser,
14033 /*typename_keyword_p=*/false,
14034 /*check_dependency_p=*/true,
14035 /*type_p=*/false,
14036 /*is_declaration=*/true);
14037 /* Get the namespace being used. */
14038 namespace_decl = cp_parser_namespace_name (parser);
14039 /* And any specified attributes. */
14040 attribs = cp_parser_attributes_opt (parser);
14041 /* Update the symbol table. */
14042 parse_using_directive (namespace_decl, attribs);
14043 /* Look for the final `;'. */
14044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14047 /* Parse an asm-definition.
14049 asm-definition:
14050 asm ( string-literal ) ;
14052 GNU Extension:
14054 asm-definition:
14055 asm volatile [opt] ( string-literal ) ;
14056 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14057 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14058 : asm-operand-list [opt] ) ;
14059 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14060 : asm-operand-list [opt]
14061 : asm-clobber-list [opt] ) ;
14062 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14063 : asm-clobber-list [opt]
14064 : asm-goto-list ) ; */
14066 static void
14067 cp_parser_asm_definition (cp_parser* parser)
14069 tree string;
14070 tree outputs = NULL_TREE;
14071 tree inputs = NULL_TREE;
14072 tree clobbers = NULL_TREE;
14073 tree labels = NULL_TREE;
14074 tree asm_stmt;
14075 bool volatile_p = false;
14076 bool extended_p = false;
14077 bool invalid_inputs_p = false;
14078 bool invalid_outputs_p = false;
14079 bool goto_p = false;
14080 required_token missing = RT_NONE;
14082 /* Look for the `asm' keyword. */
14083 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14084 /* See if the next token is `volatile'. */
14085 if (cp_parser_allow_gnu_extensions_p (parser)
14086 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14088 /* Remember that we saw the `volatile' keyword. */
14089 volatile_p = true;
14090 /* Consume the token. */
14091 cp_lexer_consume_token (parser->lexer);
14093 if (cp_parser_allow_gnu_extensions_p (parser)
14094 && parser->in_function_body
14095 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14097 /* Remember that we saw the `goto' keyword. */
14098 goto_p = true;
14099 /* Consume the token. */
14100 cp_lexer_consume_token (parser->lexer);
14102 /* Look for the opening `('. */
14103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14104 return;
14105 /* Look for the string. */
14106 string = cp_parser_string_literal (parser, false, false);
14107 if (string == error_mark_node)
14109 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14110 /*consume_paren=*/true);
14111 return;
14114 /* If we're allowing GNU extensions, check for the extended assembly
14115 syntax. Unfortunately, the `:' tokens need not be separated by
14116 a space in C, and so, for compatibility, we tolerate that here
14117 too. Doing that means that we have to treat the `::' operator as
14118 two `:' tokens. */
14119 if (cp_parser_allow_gnu_extensions_p (parser)
14120 && parser->in_function_body
14121 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14122 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14124 bool inputs_p = false;
14125 bool clobbers_p = false;
14126 bool labels_p = false;
14128 /* The extended syntax was used. */
14129 extended_p = true;
14131 /* Look for outputs. */
14132 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14134 /* Consume the `:'. */
14135 cp_lexer_consume_token (parser->lexer);
14136 /* Parse the output-operands. */
14137 if (cp_lexer_next_token_is_not (parser->lexer,
14138 CPP_COLON)
14139 && cp_lexer_next_token_is_not (parser->lexer,
14140 CPP_SCOPE)
14141 && cp_lexer_next_token_is_not (parser->lexer,
14142 CPP_CLOSE_PAREN)
14143 && !goto_p)
14144 outputs = cp_parser_asm_operand_list (parser);
14146 if (outputs == error_mark_node)
14147 invalid_outputs_p = true;
14149 /* If the next token is `::', there are no outputs, and the
14150 next token is the beginning of the inputs. */
14151 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14152 /* The inputs are coming next. */
14153 inputs_p = true;
14155 /* Look for inputs. */
14156 if (inputs_p
14157 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14159 /* Consume the `:' or `::'. */
14160 cp_lexer_consume_token (parser->lexer);
14161 /* Parse the output-operands. */
14162 if (cp_lexer_next_token_is_not (parser->lexer,
14163 CPP_COLON)
14164 && cp_lexer_next_token_is_not (parser->lexer,
14165 CPP_SCOPE)
14166 && cp_lexer_next_token_is_not (parser->lexer,
14167 CPP_CLOSE_PAREN))
14168 inputs = cp_parser_asm_operand_list (parser);
14170 if (inputs == error_mark_node)
14171 invalid_inputs_p = true;
14173 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14174 /* The clobbers are coming next. */
14175 clobbers_p = true;
14177 /* Look for clobbers. */
14178 if (clobbers_p
14179 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14181 clobbers_p = true;
14182 /* Consume the `:' or `::'. */
14183 cp_lexer_consume_token (parser->lexer);
14184 /* Parse the clobbers. */
14185 if (cp_lexer_next_token_is_not (parser->lexer,
14186 CPP_COLON)
14187 && cp_lexer_next_token_is_not (parser->lexer,
14188 CPP_CLOSE_PAREN))
14189 clobbers = cp_parser_asm_clobber_list (parser);
14191 else if (goto_p
14192 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14193 /* The labels are coming next. */
14194 labels_p = true;
14196 /* Look for labels. */
14197 if (labels_p
14198 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14200 labels_p = true;
14201 /* Consume the `:' or `::'. */
14202 cp_lexer_consume_token (parser->lexer);
14203 /* Parse the labels. */
14204 labels = cp_parser_asm_label_list (parser);
14207 if (goto_p && !labels_p)
14208 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14210 else if (goto_p)
14211 missing = RT_COLON_SCOPE;
14213 /* Look for the closing `)'. */
14214 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14215 missing ? missing : RT_CLOSE_PAREN))
14216 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14217 /*consume_paren=*/true);
14218 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14220 if (!invalid_inputs_p && !invalid_outputs_p)
14222 /* Create the ASM_EXPR. */
14223 if (parser->in_function_body)
14225 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14226 inputs, clobbers, labels);
14227 /* If the extended syntax was not used, mark the ASM_EXPR. */
14228 if (!extended_p)
14230 tree temp = asm_stmt;
14231 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14232 temp = TREE_OPERAND (temp, 0);
14234 ASM_INPUT_P (temp) = 1;
14237 else
14238 cgraph_add_asm_node (string);
14242 /* Declarators [gram.dcl.decl] */
14244 /* Parse an init-declarator.
14246 init-declarator:
14247 declarator initializer [opt]
14249 GNU Extension:
14251 init-declarator:
14252 declarator asm-specification [opt] attributes [opt] initializer [opt]
14254 function-definition:
14255 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14256 function-body
14257 decl-specifier-seq [opt] declarator function-try-block
14259 GNU Extension:
14261 function-definition:
14262 __extension__ function-definition
14264 The DECL_SPECIFIERS apply to this declarator. Returns a
14265 representation of the entity declared. If MEMBER_P is TRUE, then
14266 this declarator appears in a class scope. The new DECL created by
14267 this declarator is returned.
14269 The CHECKS are access checks that should be performed once we know
14270 what entity is being declared (and, therefore, what classes have
14271 befriended it).
14273 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14274 for a function-definition here as well. If the declarator is a
14275 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14276 be TRUE upon return. By that point, the function-definition will
14277 have been completely parsed.
14279 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14280 is FALSE.
14282 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14283 parsed declaration if it is an uninitialized single declarator not followed
14284 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14285 if present, will not be consumed. If returned, this declarator will be
14286 created with SD_INITIALIZED but will not call cp_finish_decl. */
14288 static tree
14289 cp_parser_init_declarator (cp_parser* parser,
14290 cp_decl_specifier_seq *decl_specifiers,
14291 VEC (deferred_access_check,gc)* checks,
14292 bool function_definition_allowed_p,
14293 bool member_p,
14294 int declares_class_or_enum,
14295 bool* function_definition_p,
14296 tree* maybe_range_for_decl)
14298 cp_token *token = NULL, *asm_spec_start_token = NULL,
14299 *attributes_start_token = NULL;
14300 cp_declarator *declarator;
14301 tree prefix_attributes;
14302 tree attributes;
14303 tree asm_specification;
14304 tree initializer;
14305 tree decl = NULL_TREE;
14306 tree scope;
14307 int is_initialized;
14308 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14309 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14310 "(...)". */
14311 enum cpp_ttype initialization_kind;
14312 bool is_direct_init = false;
14313 bool is_non_constant_init;
14314 int ctor_dtor_or_conv_p;
14315 bool friend_p;
14316 tree pushed_scope = NULL;
14317 bool range_for_decl_p = false;
14319 /* Gather the attributes that were provided with the
14320 decl-specifiers. */
14321 prefix_attributes = decl_specifiers->attributes;
14323 /* Assume that this is not the declarator for a function
14324 definition. */
14325 if (function_definition_p)
14326 *function_definition_p = false;
14328 /* Defer access checks while parsing the declarator; we cannot know
14329 what names are accessible until we know what is being
14330 declared. */
14331 resume_deferring_access_checks ();
14333 /* Parse the declarator. */
14334 token = cp_lexer_peek_token (parser->lexer);
14335 declarator
14336 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14337 &ctor_dtor_or_conv_p,
14338 /*parenthesized_p=*/NULL,
14339 /*member_p=*/false);
14340 /* Gather up the deferred checks. */
14341 stop_deferring_access_checks ();
14343 /* If the DECLARATOR was erroneous, there's no need to go
14344 further. */
14345 if (declarator == cp_error_declarator)
14346 return error_mark_node;
14348 /* Check that the number of template-parameter-lists is OK. */
14349 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14350 token->location))
14351 return error_mark_node;
14353 if (declares_class_or_enum & 2)
14354 cp_parser_check_for_definition_in_return_type (declarator,
14355 decl_specifiers->type,
14356 decl_specifiers->type_location);
14358 /* Figure out what scope the entity declared by the DECLARATOR is
14359 located in. `grokdeclarator' sometimes changes the scope, so
14360 we compute it now. */
14361 scope = get_scope_of_declarator (declarator);
14363 /* Perform any lookups in the declared type which were thought to be
14364 dependent, but are not in the scope of the declarator. */
14365 decl_specifiers->type
14366 = maybe_update_decl_type (decl_specifiers->type, scope);
14368 /* If we're allowing GNU extensions, look for an asm-specification
14369 and attributes. */
14370 if (cp_parser_allow_gnu_extensions_p (parser))
14372 /* Look for an asm-specification. */
14373 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14374 asm_specification = cp_parser_asm_specification_opt (parser);
14375 /* And attributes. */
14376 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14377 attributes = cp_parser_attributes_opt (parser);
14379 else
14381 asm_specification = NULL_TREE;
14382 attributes = NULL_TREE;
14385 /* Peek at the next token. */
14386 token = cp_lexer_peek_token (parser->lexer);
14387 /* Check to see if the token indicates the start of a
14388 function-definition. */
14389 if (function_declarator_p (declarator)
14390 && cp_parser_token_starts_function_definition_p (token))
14392 if (!function_definition_allowed_p)
14394 /* If a function-definition should not appear here, issue an
14395 error message. */
14396 cp_parser_error (parser,
14397 "a function-definition is not allowed here");
14398 return error_mark_node;
14400 else
14402 location_t func_brace_location
14403 = cp_lexer_peek_token (parser->lexer)->location;
14405 /* Neither attributes nor an asm-specification are allowed
14406 on a function-definition. */
14407 if (asm_specification)
14408 error_at (asm_spec_start_token->location,
14409 "an asm-specification is not allowed "
14410 "on a function-definition");
14411 if (attributes)
14412 error_at (attributes_start_token->location,
14413 "attributes are not allowed on a function-definition");
14414 /* This is a function-definition. */
14415 *function_definition_p = true;
14417 /* Parse the function definition. */
14418 if (member_p)
14419 decl = cp_parser_save_member_function_body (parser,
14420 decl_specifiers,
14421 declarator,
14422 prefix_attributes);
14423 else
14424 decl
14425 = (cp_parser_function_definition_from_specifiers_and_declarator
14426 (parser, decl_specifiers, prefix_attributes, declarator));
14428 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14430 /* This is where the prologue starts... */
14431 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14432 = func_brace_location;
14435 return decl;
14439 /* [dcl.dcl]
14441 Only in function declarations for constructors, destructors, and
14442 type conversions can the decl-specifier-seq be omitted.
14444 We explicitly postpone this check past the point where we handle
14445 function-definitions because we tolerate function-definitions
14446 that are missing their return types in some modes. */
14447 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14449 cp_parser_error (parser,
14450 "expected constructor, destructor, or type conversion");
14451 return error_mark_node;
14454 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14455 if (token->type == CPP_EQ
14456 || token->type == CPP_OPEN_PAREN
14457 || token->type == CPP_OPEN_BRACE)
14459 is_initialized = SD_INITIALIZED;
14460 initialization_kind = token->type;
14461 if (maybe_range_for_decl)
14462 *maybe_range_for_decl = error_mark_node;
14464 if (token->type == CPP_EQ
14465 && function_declarator_p (declarator))
14467 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14468 if (t2->keyword == RID_DEFAULT)
14469 is_initialized = SD_DEFAULTED;
14470 else if (t2->keyword == RID_DELETE)
14471 is_initialized = SD_DELETED;
14474 else
14476 /* If the init-declarator isn't initialized and isn't followed by a
14477 `,' or `;', it's not a valid init-declarator. */
14478 if (token->type != CPP_COMMA
14479 && token->type != CPP_SEMICOLON)
14481 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14482 range_for_decl_p = true;
14483 else
14485 cp_parser_error (parser, "expected initializer");
14486 return error_mark_node;
14489 is_initialized = SD_UNINITIALIZED;
14490 initialization_kind = CPP_EOF;
14493 /* Because start_decl has side-effects, we should only call it if we
14494 know we're going ahead. By this point, we know that we cannot
14495 possibly be looking at any other construct. */
14496 cp_parser_commit_to_tentative_parse (parser);
14498 /* If the decl specifiers were bad, issue an error now that we're
14499 sure this was intended to be a declarator. Then continue
14500 declaring the variable(s), as int, to try to cut down on further
14501 errors. */
14502 if (decl_specifiers->any_specifiers_p
14503 && decl_specifiers->type == error_mark_node)
14505 cp_parser_error (parser, "invalid type in declaration");
14506 decl_specifiers->type = integer_type_node;
14509 /* Check to see whether or not this declaration is a friend. */
14510 friend_p = cp_parser_friend_p (decl_specifiers);
14512 /* Enter the newly declared entry in the symbol table. If we're
14513 processing a declaration in a class-specifier, we wait until
14514 after processing the initializer. */
14515 if (!member_p)
14517 if (parser->in_unbraced_linkage_specification_p)
14518 decl_specifiers->storage_class = sc_extern;
14519 decl = start_decl (declarator, decl_specifiers,
14520 range_for_decl_p? SD_INITIALIZED : is_initialized,
14521 attributes, prefix_attributes,
14522 &pushed_scope);
14523 /* Adjust location of decl if declarator->id_loc is more appropriate:
14524 set, and decl wasn't merged with another decl, in which case its
14525 location would be different from input_location, and more accurate. */
14526 if (DECL_P (decl)
14527 && declarator->id_loc != UNKNOWN_LOCATION
14528 && DECL_SOURCE_LOCATION (decl) == input_location)
14529 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14531 else if (scope)
14532 /* Enter the SCOPE. That way unqualified names appearing in the
14533 initializer will be looked up in SCOPE. */
14534 pushed_scope = push_scope (scope);
14536 /* Perform deferred access control checks, now that we know in which
14537 SCOPE the declared entity resides. */
14538 if (!member_p && decl)
14540 tree saved_current_function_decl = NULL_TREE;
14542 /* If the entity being declared is a function, pretend that we
14543 are in its scope. If it is a `friend', it may have access to
14544 things that would not otherwise be accessible. */
14545 if (TREE_CODE (decl) == FUNCTION_DECL)
14547 saved_current_function_decl = current_function_decl;
14548 current_function_decl = decl;
14551 /* Perform access checks for template parameters. */
14552 cp_parser_perform_template_parameter_access_checks (checks);
14554 /* Perform the access control checks for the declarator and the
14555 decl-specifiers. */
14556 perform_deferred_access_checks ();
14558 /* Restore the saved value. */
14559 if (TREE_CODE (decl) == FUNCTION_DECL)
14560 current_function_decl = saved_current_function_decl;
14563 /* Parse the initializer. */
14564 initializer = NULL_TREE;
14565 is_direct_init = false;
14566 is_non_constant_init = true;
14567 if (is_initialized)
14569 if (function_declarator_p (declarator))
14571 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14572 if (initialization_kind == CPP_EQ)
14573 initializer = cp_parser_pure_specifier (parser);
14574 else
14576 /* If the declaration was erroneous, we don't really
14577 know what the user intended, so just silently
14578 consume the initializer. */
14579 if (decl != error_mark_node)
14580 error_at (initializer_start_token->location,
14581 "initializer provided for function");
14582 cp_parser_skip_to_closing_parenthesis (parser,
14583 /*recovering=*/true,
14584 /*or_comma=*/false,
14585 /*consume_paren=*/true);
14588 else
14590 /* We want to record the extra mangling scope for in-class
14591 initializers of class members and initializers of static data
14592 member templates. The former is a C++0x feature which isn't
14593 implemented yet, and I expect it will involve deferring
14594 parsing of the initializer until end of class as with default
14595 arguments. So right here we only handle the latter. */
14596 if (!member_p && processing_template_decl)
14597 start_lambda_scope (decl);
14598 initializer = cp_parser_initializer (parser,
14599 &is_direct_init,
14600 &is_non_constant_init);
14601 if (!member_p && processing_template_decl)
14602 finish_lambda_scope ();
14606 /* The old parser allows attributes to appear after a parenthesized
14607 initializer. Mark Mitchell proposed removing this functionality
14608 on the GCC mailing lists on 2002-08-13. This parser accepts the
14609 attributes -- but ignores them. */
14610 if (cp_parser_allow_gnu_extensions_p (parser)
14611 && initialization_kind == CPP_OPEN_PAREN)
14612 if (cp_parser_attributes_opt (parser))
14613 warning (OPT_Wattributes,
14614 "attributes after parenthesized initializer ignored");
14616 /* For an in-class declaration, use `grokfield' to create the
14617 declaration. */
14618 if (member_p)
14620 if (pushed_scope)
14622 pop_scope (pushed_scope);
14623 pushed_scope = false;
14625 decl = grokfield (declarator, decl_specifiers,
14626 initializer, !is_non_constant_init,
14627 /*asmspec=*/NULL_TREE,
14628 prefix_attributes);
14629 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14630 cp_parser_save_default_args (parser, decl);
14633 /* Finish processing the declaration. But, skip friend
14634 declarations. */
14635 if (!friend_p && decl && decl != error_mark_node && !range_for_decl_p)
14637 cp_finish_decl (decl,
14638 initializer, !is_non_constant_init,
14639 asm_specification,
14640 /* If the initializer is in parentheses, then this is
14641 a direct-initialization, which means that an
14642 `explicit' constructor is OK. Otherwise, an
14643 `explicit' constructor cannot be used. */
14644 ((is_direct_init || !is_initialized)
14645 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14647 else if ((cxx_dialect != cxx98) && friend_p
14648 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14649 /* Core issue #226 (C++0x only): A default template-argument
14650 shall not be specified in a friend class template
14651 declaration. */
14652 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14653 /*is_partial=*/0, /*is_friend_decl=*/1);
14655 if (!friend_p && pushed_scope)
14656 pop_scope (pushed_scope);
14658 return decl;
14661 /* Parse a declarator.
14663 declarator:
14664 direct-declarator
14665 ptr-operator declarator
14667 abstract-declarator:
14668 ptr-operator abstract-declarator [opt]
14669 direct-abstract-declarator
14671 GNU Extensions:
14673 declarator:
14674 attributes [opt] direct-declarator
14675 attributes [opt] ptr-operator declarator
14677 abstract-declarator:
14678 attributes [opt] ptr-operator abstract-declarator [opt]
14679 attributes [opt] direct-abstract-declarator
14681 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14682 detect constructor, destructor or conversion operators. It is set
14683 to -1 if the declarator is a name, and +1 if it is a
14684 function. Otherwise it is set to zero. Usually you just want to
14685 test for >0, but internally the negative value is used.
14687 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14688 a decl-specifier-seq unless it declares a constructor, destructor,
14689 or conversion. It might seem that we could check this condition in
14690 semantic analysis, rather than parsing, but that makes it difficult
14691 to handle something like `f()'. We want to notice that there are
14692 no decl-specifiers, and therefore realize that this is an
14693 expression, not a declaration.)
14695 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14696 the declarator is a direct-declarator of the form "(...)".
14698 MEMBER_P is true iff this declarator is a member-declarator. */
14700 static cp_declarator *
14701 cp_parser_declarator (cp_parser* parser,
14702 cp_parser_declarator_kind dcl_kind,
14703 int* ctor_dtor_or_conv_p,
14704 bool* parenthesized_p,
14705 bool member_p)
14707 cp_declarator *declarator;
14708 enum tree_code code;
14709 cp_cv_quals cv_quals;
14710 tree class_type;
14711 tree attributes = NULL_TREE;
14713 /* Assume this is not a constructor, destructor, or type-conversion
14714 operator. */
14715 if (ctor_dtor_or_conv_p)
14716 *ctor_dtor_or_conv_p = 0;
14718 if (cp_parser_allow_gnu_extensions_p (parser))
14719 attributes = cp_parser_attributes_opt (parser);
14721 /* Check for the ptr-operator production. */
14722 cp_parser_parse_tentatively (parser);
14723 /* Parse the ptr-operator. */
14724 code = cp_parser_ptr_operator (parser,
14725 &class_type,
14726 &cv_quals);
14727 /* If that worked, then we have a ptr-operator. */
14728 if (cp_parser_parse_definitely (parser))
14730 /* If a ptr-operator was found, then this declarator was not
14731 parenthesized. */
14732 if (parenthesized_p)
14733 *parenthesized_p = true;
14734 /* The dependent declarator is optional if we are parsing an
14735 abstract-declarator. */
14736 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14737 cp_parser_parse_tentatively (parser);
14739 /* Parse the dependent declarator. */
14740 declarator = cp_parser_declarator (parser, dcl_kind,
14741 /*ctor_dtor_or_conv_p=*/NULL,
14742 /*parenthesized_p=*/NULL,
14743 /*member_p=*/false);
14745 /* If we are parsing an abstract-declarator, we must handle the
14746 case where the dependent declarator is absent. */
14747 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14748 && !cp_parser_parse_definitely (parser))
14749 declarator = NULL;
14751 declarator = cp_parser_make_indirect_declarator
14752 (code, class_type, cv_quals, declarator);
14754 /* Everything else is a direct-declarator. */
14755 else
14757 if (parenthesized_p)
14758 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14759 CPP_OPEN_PAREN);
14760 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14761 ctor_dtor_or_conv_p,
14762 member_p);
14765 if (attributes && declarator && declarator != cp_error_declarator)
14766 declarator->attributes = attributes;
14768 return declarator;
14771 /* Parse a direct-declarator or direct-abstract-declarator.
14773 direct-declarator:
14774 declarator-id
14775 direct-declarator ( parameter-declaration-clause )
14776 cv-qualifier-seq [opt]
14777 exception-specification [opt]
14778 direct-declarator [ constant-expression [opt] ]
14779 ( declarator )
14781 direct-abstract-declarator:
14782 direct-abstract-declarator [opt]
14783 ( parameter-declaration-clause )
14784 cv-qualifier-seq [opt]
14785 exception-specification [opt]
14786 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14787 ( abstract-declarator )
14789 Returns a representation of the declarator. DCL_KIND is
14790 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14791 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14792 we are parsing a direct-declarator. It is
14793 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14794 of ambiguity we prefer an abstract declarator, as per
14795 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14796 cp_parser_declarator. */
14798 static cp_declarator *
14799 cp_parser_direct_declarator (cp_parser* parser,
14800 cp_parser_declarator_kind dcl_kind,
14801 int* ctor_dtor_or_conv_p,
14802 bool member_p)
14804 cp_token *token;
14805 cp_declarator *declarator = NULL;
14806 tree scope = NULL_TREE;
14807 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14808 bool saved_in_declarator_p = parser->in_declarator_p;
14809 bool first = true;
14810 tree pushed_scope = NULL_TREE;
14812 while (true)
14814 /* Peek at the next token. */
14815 token = cp_lexer_peek_token (parser->lexer);
14816 if (token->type == CPP_OPEN_PAREN)
14818 /* This is either a parameter-declaration-clause, or a
14819 parenthesized declarator. When we know we are parsing a
14820 named declarator, it must be a parenthesized declarator
14821 if FIRST is true. For instance, `(int)' is a
14822 parameter-declaration-clause, with an omitted
14823 direct-abstract-declarator. But `((*))', is a
14824 parenthesized abstract declarator. Finally, when T is a
14825 template parameter `(T)' is a
14826 parameter-declaration-clause, and not a parenthesized
14827 named declarator.
14829 We first try and parse a parameter-declaration-clause,
14830 and then try a nested declarator (if FIRST is true).
14832 It is not an error for it not to be a
14833 parameter-declaration-clause, even when FIRST is
14834 false. Consider,
14836 int i (int);
14837 int i (3);
14839 The first is the declaration of a function while the
14840 second is the definition of a variable, including its
14841 initializer.
14843 Having seen only the parenthesis, we cannot know which of
14844 these two alternatives should be selected. Even more
14845 complex are examples like:
14847 int i (int (a));
14848 int i (int (3));
14850 The former is a function-declaration; the latter is a
14851 variable initialization.
14853 Thus again, we try a parameter-declaration-clause, and if
14854 that fails, we back out and return. */
14856 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14858 tree params;
14859 unsigned saved_num_template_parameter_lists;
14860 bool is_declarator = false;
14861 tree t;
14863 /* In a member-declarator, the only valid interpretation
14864 of a parenthesis is the start of a
14865 parameter-declaration-clause. (It is invalid to
14866 initialize a static data member with a parenthesized
14867 initializer; only the "=" form of initialization is
14868 permitted.) */
14869 if (!member_p)
14870 cp_parser_parse_tentatively (parser);
14872 /* Consume the `('. */
14873 cp_lexer_consume_token (parser->lexer);
14874 if (first)
14876 /* If this is going to be an abstract declarator, we're
14877 in a declarator and we can't have default args. */
14878 parser->default_arg_ok_p = false;
14879 parser->in_declarator_p = true;
14882 /* Inside the function parameter list, surrounding
14883 template-parameter-lists do not apply. */
14884 saved_num_template_parameter_lists
14885 = parser->num_template_parameter_lists;
14886 parser->num_template_parameter_lists = 0;
14888 begin_scope (sk_function_parms, NULL_TREE);
14890 /* Parse the parameter-declaration-clause. */
14891 params = cp_parser_parameter_declaration_clause (parser);
14893 parser->num_template_parameter_lists
14894 = saved_num_template_parameter_lists;
14896 /* If all went well, parse the cv-qualifier-seq and the
14897 exception-specification. */
14898 if (member_p || cp_parser_parse_definitely (parser))
14900 cp_cv_quals cv_quals;
14901 tree exception_specification;
14902 tree late_return;
14904 is_declarator = true;
14906 if (ctor_dtor_or_conv_p)
14907 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14908 first = false;
14909 /* Consume the `)'. */
14910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14912 /* Parse the cv-qualifier-seq. */
14913 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14914 /* And the exception-specification. */
14915 exception_specification
14916 = cp_parser_exception_specification_opt (parser);
14918 late_return
14919 = cp_parser_late_return_type_opt (parser);
14921 /* Create the function-declarator. */
14922 declarator = make_call_declarator (declarator,
14923 params,
14924 cv_quals,
14925 exception_specification,
14926 late_return);
14927 /* Any subsequent parameter lists are to do with
14928 return type, so are not those of the declared
14929 function. */
14930 parser->default_arg_ok_p = false;
14933 /* Remove the function parms from scope. */
14934 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14935 pop_binding (DECL_NAME (t), t);
14936 leave_scope();
14938 if (is_declarator)
14939 /* Repeat the main loop. */
14940 continue;
14943 /* If this is the first, we can try a parenthesized
14944 declarator. */
14945 if (first)
14947 bool saved_in_type_id_in_expr_p;
14949 parser->default_arg_ok_p = saved_default_arg_ok_p;
14950 parser->in_declarator_p = saved_in_declarator_p;
14952 /* Consume the `('. */
14953 cp_lexer_consume_token (parser->lexer);
14954 /* Parse the nested declarator. */
14955 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14956 parser->in_type_id_in_expr_p = true;
14957 declarator
14958 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14959 /*parenthesized_p=*/NULL,
14960 member_p);
14961 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14962 first = false;
14963 /* Expect a `)'. */
14964 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14965 declarator = cp_error_declarator;
14966 if (declarator == cp_error_declarator)
14967 break;
14969 goto handle_declarator;
14971 /* Otherwise, we must be done. */
14972 else
14973 break;
14975 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14976 && token->type == CPP_OPEN_SQUARE)
14978 /* Parse an array-declarator. */
14979 tree bounds;
14981 if (ctor_dtor_or_conv_p)
14982 *ctor_dtor_or_conv_p = 0;
14984 first = false;
14985 parser->default_arg_ok_p = false;
14986 parser->in_declarator_p = true;
14987 /* Consume the `['. */
14988 cp_lexer_consume_token (parser->lexer);
14989 /* Peek at the next token. */
14990 token = cp_lexer_peek_token (parser->lexer);
14991 /* If the next token is `]', then there is no
14992 constant-expression. */
14993 if (token->type != CPP_CLOSE_SQUARE)
14995 bool non_constant_p;
14997 bounds
14998 = cp_parser_constant_expression (parser,
14999 /*allow_non_constant=*/true,
15000 &non_constant_p);
15001 if (!non_constant_p || cxx_dialect >= cxx0x)
15002 /* OK */;
15003 /* Normally, the array bound must be an integral constant
15004 expression. However, as an extension, we allow VLAs
15005 in function scopes as long as they aren't part of a
15006 parameter declaration. */
15007 else if (!parser->in_function_body
15008 || current_binding_level->kind == sk_function_parms)
15010 cp_parser_error (parser,
15011 "array bound is not an integer constant");
15012 bounds = error_mark_node;
15014 else if (processing_template_decl && !error_operand_p (bounds))
15016 /* Remember this wasn't a constant-expression. */
15017 bounds = build_nop (TREE_TYPE (bounds), bounds);
15018 TREE_SIDE_EFFECTS (bounds) = 1;
15021 else
15022 bounds = NULL_TREE;
15023 /* Look for the closing `]'. */
15024 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15026 declarator = cp_error_declarator;
15027 break;
15030 declarator = make_array_declarator (declarator, bounds);
15032 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15035 tree qualifying_scope;
15036 tree unqualified_name;
15037 special_function_kind sfk;
15038 bool abstract_ok;
15039 bool pack_expansion_p = false;
15040 cp_token *declarator_id_start_token;
15042 /* Parse a declarator-id */
15043 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15044 if (abstract_ok)
15046 cp_parser_parse_tentatively (parser);
15048 /* If we see an ellipsis, we should be looking at a
15049 parameter pack. */
15050 if (token->type == CPP_ELLIPSIS)
15052 /* Consume the `...' */
15053 cp_lexer_consume_token (parser->lexer);
15055 pack_expansion_p = true;
15059 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15060 unqualified_name
15061 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15062 qualifying_scope = parser->scope;
15063 if (abstract_ok)
15065 bool okay = false;
15067 if (!unqualified_name && pack_expansion_p)
15069 /* Check whether an error occurred. */
15070 okay = !cp_parser_error_occurred (parser);
15072 /* We already consumed the ellipsis to mark a
15073 parameter pack, but we have no way to report it,
15074 so abort the tentative parse. We will be exiting
15075 immediately anyway. */
15076 cp_parser_abort_tentative_parse (parser);
15078 else
15079 okay = cp_parser_parse_definitely (parser);
15081 if (!okay)
15082 unqualified_name = error_mark_node;
15083 else if (unqualified_name
15084 && (qualifying_scope
15085 || (TREE_CODE (unqualified_name)
15086 != IDENTIFIER_NODE)))
15088 cp_parser_error (parser, "expected unqualified-id");
15089 unqualified_name = error_mark_node;
15093 if (!unqualified_name)
15094 return NULL;
15095 if (unqualified_name == error_mark_node)
15097 declarator = cp_error_declarator;
15098 pack_expansion_p = false;
15099 declarator->parameter_pack_p = false;
15100 break;
15103 if (qualifying_scope && at_namespace_scope_p ()
15104 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15106 /* In the declaration of a member of a template class
15107 outside of the class itself, the SCOPE will sometimes
15108 be a TYPENAME_TYPE. For example, given:
15110 template <typename T>
15111 int S<T>::R::i = 3;
15113 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15114 this context, we must resolve S<T>::R to an ordinary
15115 type, rather than a typename type.
15117 The reason we normally avoid resolving TYPENAME_TYPEs
15118 is that a specialization of `S' might render
15119 `S<T>::R' not a type. However, if `S' is
15120 specialized, then this `i' will not be used, so there
15121 is no harm in resolving the types here. */
15122 tree type;
15124 /* Resolve the TYPENAME_TYPE. */
15125 type = resolve_typename_type (qualifying_scope,
15126 /*only_current_p=*/false);
15127 /* If that failed, the declarator is invalid. */
15128 if (TREE_CODE (type) == TYPENAME_TYPE)
15130 if (typedef_variant_p (type))
15131 error_at (declarator_id_start_token->location,
15132 "cannot define member of dependent typedef "
15133 "%qT", type);
15134 else
15135 error_at (declarator_id_start_token->location,
15136 "%<%T::%E%> is not a type",
15137 TYPE_CONTEXT (qualifying_scope),
15138 TYPE_IDENTIFIER (qualifying_scope));
15140 qualifying_scope = type;
15143 sfk = sfk_none;
15145 if (unqualified_name)
15147 tree class_type;
15149 if (qualifying_scope
15150 && CLASS_TYPE_P (qualifying_scope))
15151 class_type = qualifying_scope;
15152 else
15153 class_type = current_class_type;
15155 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15157 tree name_type = TREE_TYPE (unqualified_name);
15158 if (class_type && same_type_p (name_type, class_type))
15160 if (qualifying_scope
15161 && CLASSTYPE_USE_TEMPLATE (name_type))
15163 error_at (declarator_id_start_token->location,
15164 "invalid use of constructor as a template");
15165 inform (declarator_id_start_token->location,
15166 "use %<%T::%D%> instead of %<%T::%D%> to "
15167 "name the constructor in a qualified name",
15168 class_type,
15169 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15170 class_type, name_type);
15171 declarator = cp_error_declarator;
15172 break;
15174 else
15175 unqualified_name = constructor_name (class_type);
15177 else
15179 /* We do not attempt to print the declarator
15180 here because we do not have enough
15181 information about its original syntactic
15182 form. */
15183 cp_parser_error (parser, "invalid declarator");
15184 declarator = cp_error_declarator;
15185 break;
15189 if (class_type)
15191 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15192 sfk = sfk_destructor;
15193 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15194 sfk = sfk_conversion;
15195 else if (/* There's no way to declare a constructor
15196 for an anonymous type, even if the type
15197 got a name for linkage purposes. */
15198 !TYPE_WAS_ANONYMOUS (class_type)
15199 && constructor_name_p (unqualified_name,
15200 class_type))
15202 unqualified_name = constructor_name (class_type);
15203 sfk = sfk_constructor;
15205 else if (is_overloaded_fn (unqualified_name)
15206 && DECL_CONSTRUCTOR_P (get_first_fn
15207 (unqualified_name)))
15208 sfk = sfk_constructor;
15210 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15211 *ctor_dtor_or_conv_p = -1;
15214 declarator = make_id_declarator (qualifying_scope,
15215 unqualified_name,
15216 sfk);
15217 declarator->id_loc = token->location;
15218 declarator->parameter_pack_p = pack_expansion_p;
15220 if (pack_expansion_p)
15221 maybe_warn_variadic_templates ();
15224 handle_declarator:;
15225 scope = get_scope_of_declarator (declarator);
15226 if (scope)
15227 /* Any names that appear after the declarator-id for a
15228 member are looked up in the containing scope. */
15229 pushed_scope = push_scope (scope);
15230 parser->in_declarator_p = true;
15231 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15232 || (declarator && declarator->kind == cdk_id))
15233 /* Default args are only allowed on function
15234 declarations. */
15235 parser->default_arg_ok_p = saved_default_arg_ok_p;
15236 else
15237 parser->default_arg_ok_p = false;
15239 first = false;
15241 /* We're done. */
15242 else
15243 break;
15246 /* For an abstract declarator, we might wind up with nothing at this
15247 point. That's an error; the declarator is not optional. */
15248 if (!declarator)
15249 cp_parser_error (parser, "expected declarator");
15251 /* If we entered a scope, we must exit it now. */
15252 if (pushed_scope)
15253 pop_scope (pushed_scope);
15255 parser->default_arg_ok_p = saved_default_arg_ok_p;
15256 parser->in_declarator_p = saved_in_declarator_p;
15258 return declarator;
15261 /* Parse a ptr-operator.
15263 ptr-operator:
15264 * cv-qualifier-seq [opt]
15266 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15268 GNU Extension:
15270 ptr-operator:
15271 & cv-qualifier-seq [opt]
15273 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15274 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15275 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15276 filled in with the TYPE containing the member. *CV_QUALS is
15277 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15278 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15279 Note that the tree codes returned by this function have nothing
15280 to do with the types of trees that will be eventually be created
15281 to represent the pointer or reference type being parsed. They are
15282 just constants with suggestive names. */
15283 static enum tree_code
15284 cp_parser_ptr_operator (cp_parser* parser,
15285 tree* type,
15286 cp_cv_quals *cv_quals)
15288 enum tree_code code = ERROR_MARK;
15289 cp_token *token;
15291 /* Assume that it's not a pointer-to-member. */
15292 *type = NULL_TREE;
15293 /* And that there are no cv-qualifiers. */
15294 *cv_quals = TYPE_UNQUALIFIED;
15296 /* Peek at the next token. */
15297 token = cp_lexer_peek_token (parser->lexer);
15299 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15300 if (token->type == CPP_MULT)
15301 code = INDIRECT_REF;
15302 else if (token->type == CPP_AND)
15303 code = ADDR_EXPR;
15304 else if ((cxx_dialect != cxx98) &&
15305 token->type == CPP_AND_AND) /* C++0x only */
15306 code = NON_LVALUE_EXPR;
15308 if (code != ERROR_MARK)
15310 /* Consume the `*', `&' or `&&'. */
15311 cp_lexer_consume_token (parser->lexer);
15313 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15314 `&', if we are allowing GNU extensions. (The only qualifier
15315 that can legally appear after `&' is `restrict', but that is
15316 enforced during semantic analysis. */
15317 if (code == INDIRECT_REF
15318 || cp_parser_allow_gnu_extensions_p (parser))
15319 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15321 else
15323 /* Try the pointer-to-member case. */
15324 cp_parser_parse_tentatively (parser);
15325 /* Look for the optional `::' operator. */
15326 cp_parser_global_scope_opt (parser,
15327 /*current_scope_valid_p=*/false);
15328 /* Look for the nested-name specifier. */
15329 token = cp_lexer_peek_token (parser->lexer);
15330 cp_parser_nested_name_specifier (parser,
15331 /*typename_keyword_p=*/false,
15332 /*check_dependency_p=*/true,
15333 /*type_p=*/false,
15334 /*is_declaration=*/false);
15335 /* If we found it, and the next token is a `*', then we are
15336 indeed looking at a pointer-to-member operator. */
15337 if (!cp_parser_error_occurred (parser)
15338 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15340 /* Indicate that the `*' operator was used. */
15341 code = INDIRECT_REF;
15343 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15344 error_at (token->location, "%qD is a namespace", parser->scope);
15345 else
15347 /* The type of which the member is a member is given by the
15348 current SCOPE. */
15349 *type = parser->scope;
15350 /* The next name will not be qualified. */
15351 parser->scope = NULL_TREE;
15352 parser->qualifying_scope = NULL_TREE;
15353 parser->object_scope = NULL_TREE;
15354 /* Look for the optional cv-qualifier-seq. */
15355 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15358 /* If that didn't work we don't have a ptr-operator. */
15359 if (!cp_parser_parse_definitely (parser))
15360 cp_parser_error (parser, "expected ptr-operator");
15363 return code;
15366 /* Parse an (optional) cv-qualifier-seq.
15368 cv-qualifier-seq:
15369 cv-qualifier cv-qualifier-seq [opt]
15371 cv-qualifier:
15372 const
15373 volatile
15375 GNU Extension:
15377 cv-qualifier:
15378 __restrict__
15380 Returns a bitmask representing the cv-qualifiers. */
15382 static cp_cv_quals
15383 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15385 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15387 while (true)
15389 cp_token *token;
15390 cp_cv_quals cv_qualifier;
15392 /* Peek at the next token. */
15393 token = cp_lexer_peek_token (parser->lexer);
15394 /* See if it's a cv-qualifier. */
15395 switch (token->keyword)
15397 case RID_CONST:
15398 cv_qualifier = TYPE_QUAL_CONST;
15399 break;
15401 case RID_VOLATILE:
15402 cv_qualifier = TYPE_QUAL_VOLATILE;
15403 break;
15405 case RID_RESTRICT:
15406 cv_qualifier = TYPE_QUAL_RESTRICT;
15407 break;
15409 default:
15410 cv_qualifier = TYPE_UNQUALIFIED;
15411 break;
15414 if (!cv_qualifier)
15415 break;
15417 if (cv_quals & cv_qualifier)
15419 error_at (token->location, "duplicate cv-qualifier");
15420 cp_lexer_purge_token (parser->lexer);
15422 else
15424 cp_lexer_consume_token (parser->lexer);
15425 cv_quals |= cv_qualifier;
15429 return cv_quals;
15432 /* Parse a late-specified return type, if any. This is not a separate
15433 non-terminal, but part of a function declarator, which looks like
15435 -> trailing-type-specifier-seq abstract-declarator(opt)
15437 Returns the type indicated by the type-id. */
15439 static tree
15440 cp_parser_late_return_type_opt (cp_parser* parser)
15442 cp_token *token;
15444 /* Peek at the next token. */
15445 token = cp_lexer_peek_token (parser->lexer);
15446 /* A late-specified return type is indicated by an initial '->'. */
15447 if (token->type != CPP_DEREF)
15448 return NULL_TREE;
15450 /* Consume the ->. */
15451 cp_lexer_consume_token (parser->lexer);
15453 return cp_parser_trailing_type_id (parser);
15456 /* Parse a declarator-id.
15458 declarator-id:
15459 id-expression
15460 :: [opt] nested-name-specifier [opt] type-name
15462 In the `id-expression' case, the value returned is as for
15463 cp_parser_id_expression if the id-expression was an unqualified-id.
15464 If the id-expression was a qualified-id, then a SCOPE_REF is
15465 returned. The first operand is the scope (either a NAMESPACE_DECL
15466 or TREE_TYPE), but the second is still just a representation of an
15467 unqualified-id. */
15469 static tree
15470 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15472 tree id;
15473 /* The expression must be an id-expression. Assume that qualified
15474 names are the names of types so that:
15476 template <class T>
15477 int S<T>::R::i = 3;
15479 will work; we must treat `S<T>::R' as the name of a type.
15480 Similarly, assume that qualified names are templates, where
15481 required, so that:
15483 template <class T>
15484 int S<T>::R<T>::i = 3;
15486 will work, too. */
15487 id = cp_parser_id_expression (parser,
15488 /*template_keyword_p=*/false,
15489 /*check_dependency_p=*/false,
15490 /*template_p=*/NULL,
15491 /*declarator_p=*/true,
15492 optional_p);
15493 if (id && BASELINK_P (id))
15494 id = BASELINK_FUNCTIONS (id);
15495 return id;
15498 /* Parse a type-id.
15500 type-id:
15501 type-specifier-seq abstract-declarator [opt]
15503 Returns the TYPE specified. */
15505 static tree
15506 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15507 bool is_trailing_return)
15509 cp_decl_specifier_seq type_specifier_seq;
15510 cp_declarator *abstract_declarator;
15512 /* Parse the type-specifier-seq. */
15513 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15514 is_trailing_return,
15515 &type_specifier_seq);
15516 if (type_specifier_seq.type == error_mark_node)
15517 return error_mark_node;
15519 /* There might or might not be an abstract declarator. */
15520 cp_parser_parse_tentatively (parser);
15521 /* Look for the declarator. */
15522 abstract_declarator
15523 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15524 /*parenthesized_p=*/NULL,
15525 /*member_p=*/false);
15526 /* Check to see if there really was a declarator. */
15527 if (!cp_parser_parse_definitely (parser))
15528 abstract_declarator = NULL;
15530 if (type_specifier_seq.type
15531 && type_uses_auto (type_specifier_seq.type))
15533 /* A type-id with type 'auto' is only ok if the abstract declarator
15534 is a function declarator with a late-specified return type. */
15535 if (abstract_declarator
15536 && abstract_declarator->kind == cdk_function
15537 && abstract_declarator->u.function.late_return_type)
15538 /* OK */;
15539 else
15541 error ("invalid use of %<auto%>");
15542 return error_mark_node;
15546 return groktypename (&type_specifier_seq, abstract_declarator,
15547 is_template_arg);
15550 static tree cp_parser_type_id (cp_parser *parser)
15552 return cp_parser_type_id_1 (parser, false, false);
15555 static tree cp_parser_template_type_arg (cp_parser *parser)
15557 return cp_parser_type_id_1 (parser, true, false);
15560 static tree cp_parser_trailing_type_id (cp_parser *parser)
15562 return cp_parser_type_id_1 (parser, false, true);
15565 /* Parse a type-specifier-seq.
15567 type-specifier-seq:
15568 type-specifier type-specifier-seq [opt]
15570 GNU extension:
15572 type-specifier-seq:
15573 attributes type-specifier-seq [opt]
15575 If IS_DECLARATION is true, we are at the start of a "condition" or
15576 exception-declaration, so we might be followed by a declarator-id.
15578 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15579 i.e. we've just seen "->".
15581 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15583 static void
15584 cp_parser_type_specifier_seq (cp_parser* parser,
15585 bool is_declaration,
15586 bool is_trailing_return,
15587 cp_decl_specifier_seq *type_specifier_seq)
15589 bool seen_type_specifier = false;
15590 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15591 cp_token *start_token = NULL;
15593 /* Clear the TYPE_SPECIFIER_SEQ. */
15594 clear_decl_specs (type_specifier_seq);
15596 /* In the context of a trailing return type, enum E { } is an
15597 elaborated-type-specifier followed by a function-body, not an
15598 enum-specifier. */
15599 if (is_trailing_return)
15600 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15602 /* Parse the type-specifiers and attributes. */
15603 while (true)
15605 tree type_specifier;
15606 bool is_cv_qualifier;
15608 /* Check for attributes first. */
15609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15611 type_specifier_seq->attributes =
15612 chainon (type_specifier_seq->attributes,
15613 cp_parser_attributes_opt (parser));
15614 continue;
15617 /* record the token of the beginning of the type specifier seq,
15618 for error reporting purposes*/
15619 if (!start_token)
15620 start_token = cp_lexer_peek_token (parser->lexer);
15622 /* Look for the type-specifier. */
15623 type_specifier = cp_parser_type_specifier (parser,
15624 flags,
15625 type_specifier_seq,
15626 /*is_declaration=*/false,
15627 NULL,
15628 &is_cv_qualifier);
15629 if (!type_specifier)
15631 /* If the first type-specifier could not be found, this is not a
15632 type-specifier-seq at all. */
15633 if (!seen_type_specifier)
15635 cp_parser_error (parser, "expected type-specifier");
15636 type_specifier_seq->type = error_mark_node;
15637 return;
15639 /* If subsequent type-specifiers could not be found, the
15640 type-specifier-seq is complete. */
15641 break;
15644 seen_type_specifier = true;
15645 /* The standard says that a condition can be:
15647 type-specifier-seq declarator = assignment-expression
15649 However, given:
15651 struct S {};
15652 if (int S = ...)
15654 we should treat the "S" as a declarator, not as a
15655 type-specifier. The standard doesn't say that explicitly for
15656 type-specifier-seq, but it does say that for
15657 decl-specifier-seq in an ordinary declaration. Perhaps it
15658 would be clearer just to allow a decl-specifier-seq here, and
15659 then add a semantic restriction that if any decl-specifiers
15660 that are not type-specifiers appear, the program is invalid. */
15661 if (is_declaration && !is_cv_qualifier)
15662 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15665 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15668 /* Parse a parameter-declaration-clause.
15670 parameter-declaration-clause:
15671 parameter-declaration-list [opt] ... [opt]
15672 parameter-declaration-list , ...
15674 Returns a representation for the parameter declarations. A return
15675 value of NULL indicates a parameter-declaration-clause consisting
15676 only of an ellipsis. */
15678 static tree
15679 cp_parser_parameter_declaration_clause (cp_parser* parser)
15681 tree parameters;
15682 cp_token *token;
15683 bool ellipsis_p;
15684 bool is_error;
15686 /* Peek at the next token. */
15687 token = cp_lexer_peek_token (parser->lexer);
15688 /* Check for trivial parameter-declaration-clauses. */
15689 if (token->type == CPP_ELLIPSIS)
15691 /* Consume the `...' token. */
15692 cp_lexer_consume_token (parser->lexer);
15693 return NULL_TREE;
15695 else if (token->type == CPP_CLOSE_PAREN)
15696 /* There are no parameters. */
15698 #ifndef NO_IMPLICIT_EXTERN_C
15699 if (in_system_header && current_class_type == NULL
15700 && current_lang_name == lang_name_c)
15701 return NULL_TREE;
15702 else
15703 #endif
15704 return void_list_node;
15706 /* Check for `(void)', too, which is a special case. */
15707 else if (token->keyword == RID_VOID
15708 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15709 == CPP_CLOSE_PAREN))
15711 /* Consume the `void' token. */
15712 cp_lexer_consume_token (parser->lexer);
15713 /* There are no parameters. */
15714 return void_list_node;
15717 /* Parse the parameter-declaration-list. */
15718 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15719 /* If a parse error occurred while parsing the
15720 parameter-declaration-list, then the entire
15721 parameter-declaration-clause is erroneous. */
15722 if (is_error)
15723 return NULL;
15725 /* Peek at the next token. */
15726 token = cp_lexer_peek_token (parser->lexer);
15727 /* If it's a `,', the clause should terminate with an ellipsis. */
15728 if (token->type == CPP_COMMA)
15730 /* Consume the `,'. */
15731 cp_lexer_consume_token (parser->lexer);
15732 /* Expect an ellipsis. */
15733 ellipsis_p
15734 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15736 /* It might also be `...' if the optional trailing `,' was
15737 omitted. */
15738 else if (token->type == CPP_ELLIPSIS)
15740 /* Consume the `...' token. */
15741 cp_lexer_consume_token (parser->lexer);
15742 /* And remember that we saw it. */
15743 ellipsis_p = true;
15745 else
15746 ellipsis_p = false;
15748 /* Finish the parameter list. */
15749 if (!ellipsis_p)
15750 parameters = chainon (parameters, void_list_node);
15752 return parameters;
15755 /* Parse a parameter-declaration-list.
15757 parameter-declaration-list:
15758 parameter-declaration
15759 parameter-declaration-list , parameter-declaration
15761 Returns a representation of the parameter-declaration-list, as for
15762 cp_parser_parameter_declaration_clause. However, the
15763 `void_list_node' is never appended to the list. Upon return,
15764 *IS_ERROR will be true iff an error occurred. */
15766 static tree
15767 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15769 tree parameters = NULL_TREE;
15770 tree *tail = &parameters;
15771 bool saved_in_unbraced_linkage_specification_p;
15772 int index = 0;
15774 /* Assume all will go well. */
15775 *is_error = false;
15776 /* The special considerations that apply to a function within an
15777 unbraced linkage specifications do not apply to the parameters
15778 to the function. */
15779 saved_in_unbraced_linkage_specification_p
15780 = parser->in_unbraced_linkage_specification_p;
15781 parser->in_unbraced_linkage_specification_p = false;
15783 /* Look for more parameters. */
15784 while (true)
15786 cp_parameter_declarator *parameter;
15787 tree decl = error_mark_node;
15788 bool parenthesized_p;
15789 /* Parse the parameter. */
15790 parameter
15791 = cp_parser_parameter_declaration (parser,
15792 /*template_parm_p=*/false,
15793 &parenthesized_p);
15795 /* We don't know yet if the enclosing context is deprecated, so wait
15796 and warn in grokparms if appropriate. */
15797 deprecated_state = DEPRECATED_SUPPRESS;
15799 if (parameter)
15800 decl = grokdeclarator (parameter->declarator,
15801 &parameter->decl_specifiers,
15802 PARM,
15803 parameter->default_argument != NULL_TREE,
15804 &parameter->decl_specifiers.attributes);
15806 deprecated_state = DEPRECATED_NORMAL;
15808 /* If a parse error occurred parsing the parameter declaration,
15809 then the entire parameter-declaration-list is erroneous. */
15810 if (decl == error_mark_node)
15812 *is_error = true;
15813 parameters = error_mark_node;
15814 break;
15817 if (parameter->decl_specifiers.attributes)
15818 cplus_decl_attributes (&decl,
15819 parameter->decl_specifiers.attributes,
15821 if (DECL_NAME (decl))
15822 decl = pushdecl (decl);
15824 if (decl != error_mark_node)
15826 retrofit_lang_decl (decl);
15827 DECL_PARM_INDEX (decl) = ++index;
15830 /* Add the new parameter to the list. */
15831 *tail = build_tree_list (parameter->default_argument, decl);
15832 tail = &TREE_CHAIN (*tail);
15834 /* Peek at the next token. */
15835 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15836 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15837 /* These are for Objective-C++ */
15838 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15839 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15840 /* The parameter-declaration-list is complete. */
15841 break;
15842 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15844 cp_token *token;
15846 /* Peek at the next token. */
15847 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15848 /* If it's an ellipsis, then the list is complete. */
15849 if (token->type == CPP_ELLIPSIS)
15850 break;
15851 /* Otherwise, there must be more parameters. Consume the
15852 `,'. */
15853 cp_lexer_consume_token (parser->lexer);
15854 /* When parsing something like:
15856 int i(float f, double d)
15858 we can tell after seeing the declaration for "f" that we
15859 are not looking at an initialization of a variable "i",
15860 but rather at the declaration of a function "i".
15862 Due to the fact that the parsing of template arguments
15863 (as specified to a template-id) requires backtracking we
15864 cannot use this technique when inside a template argument
15865 list. */
15866 if (!parser->in_template_argument_list_p
15867 && !parser->in_type_id_in_expr_p
15868 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15869 /* However, a parameter-declaration of the form
15870 "foat(f)" (which is a valid declaration of a
15871 parameter "f") can also be interpreted as an
15872 expression (the conversion of "f" to "float"). */
15873 && !parenthesized_p)
15874 cp_parser_commit_to_tentative_parse (parser);
15876 else
15878 cp_parser_error (parser, "expected %<,%> or %<...%>");
15879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15880 cp_parser_skip_to_closing_parenthesis (parser,
15881 /*recovering=*/true,
15882 /*or_comma=*/false,
15883 /*consume_paren=*/false);
15884 break;
15888 parser->in_unbraced_linkage_specification_p
15889 = saved_in_unbraced_linkage_specification_p;
15891 return parameters;
15894 /* Parse a parameter declaration.
15896 parameter-declaration:
15897 decl-specifier-seq ... [opt] declarator
15898 decl-specifier-seq declarator = assignment-expression
15899 decl-specifier-seq ... [opt] abstract-declarator [opt]
15900 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15902 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15903 declares a template parameter. (In that case, a non-nested `>'
15904 token encountered during the parsing of the assignment-expression
15905 is not interpreted as a greater-than operator.)
15907 Returns a representation of the parameter, or NULL if an error
15908 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15909 true iff the declarator is of the form "(p)". */
15911 static cp_parameter_declarator *
15912 cp_parser_parameter_declaration (cp_parser *parser,
15913 bool template_parm_p,
15914 bool *parenthesized_p)
15916 int declares_class_or_enum;
15917 cp_decl_specifier_seq decl_specifiers;
15918 cp_declarator *declarator;
15919 tree default_argument;
15920 cp_token *token = NULL, *declarator_token_start = NULL;
15921 const char *saved_message;
15923 /* In a template parameter, `>' is not an operator.
15925 [temp.param]
15927 When parsing a default template-argument for a non-type
15928 template-parameter, the first non-nested `>' is taken as the end
15929 of the template parameter-list rather than a greater-than
15930 operator. */
15932 /* Type definitions may not appear in parameter types. */
15933 saved_message = parser->type_definition_forbidden_message;
15934 parser->type_definition_forbidden_message
15935 = G_("types may not be defined in parameter types");
15937 /* Parse the declaration-specifiers. */
15938 cp_parser_decl_specifier_seq (parser,
15939 CP_PARSER_FLAGS_NONE,
15940 &decl_specifiers,
15941 &declares_class_or_enum);
15943 /* Complain about missing 'typename' or other invalid type names. */
15944 if (!decl_specifiers.any_type_specifiers_p)
15945 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15947 /* If an error occurred, there's no reason to attempt to parse the
15948 rest of the declaration. */
15949 if (cp_parser_error_occurred (parser))
15951 parser->type_definition_forbidden_message = saved_message;
15952 return NULL;
15955 /* Peek at the next token. */
15956 token = cp_lexer_peek_token (parser->lexer);
15958 /* If the next token is a `)', `,', `=', `>', or `...', then there
15959 is no declarator. However, when variadic templates are enabled,
15960 there may be a declarator following `...'. */
15961 if (token->type == CPP_CLOSE_PAREN
15962 || token->type == CPP_COMMA
15963 || token->type == CPP_EQ
15964 || token->type == CPP_GREATER)
15966 declarator = NULL;
15967 if (parenthesized_p)
15968 *parenthesized_p = false;
15970 /* Otherwise, there should be a declarator. */
15971 else
15973 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15974 parser->default_arg_ok_p = false;
15976 /* After seeing a decl-specifier-seq, if the next token is not a
15977 "(", there is no possibility that the code is a valid
15978 expression. Therefore, if parsing tentatively, we commit at
15979 this point. */
15980 if (!parser->in_template_argument_list_p
15981 /* In an expression context, having seen:
15983 (int((char ...
15985 we cannot be sure whether we are looking at a
15986 function-type (taking a "char" as a parameter) or a cast
15987 of some object of type "char" to "int". */
15988 && !parser->in_type_id_in_expr_p
15989 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15990 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15991 cp_parser_commit_to_tentative_parse (parser);
15992 /* Parse the declarator. */
15993 declarator_token_start = token;
15994 declarator = cp_parser_declarator (parser,
15995 CP_PARSER_DECLARATOR_EITHER,
15996 /*ctor_dtor_or_conv_p=*/NULL,
15997 parenthesized_p,
15998 /*member_p=*/false);
15999 parser->default_arg_ok_p = saved_default_arg_ok_p;
16000 /* After the declarator, allow more attributes. */
16001 decl_specifiers.attributes
16002 = chainon (decl_specifiers.attributes,
16003 cp_parser_attributes_opt (parser));
16006 /* If the next token is an ellipsis, and we have not seen a
16007 declarator name, and the type of the declarator contains parameter
16008 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16009 a parameter pack expansion expression. Otherwise, leave the
16010 ellipsis for a C-style variadic function. */
16011 token = cp_lexer_peek_token (parser->lexer);
16012 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16014 tree type = decl_specifiers.type;
16016 if (type && DECL_P (type))
16017 type = TREE_TYPE (type);
16019 if (type
16020 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16021 && declarator_can_be_parameter_pack (declarator)
16022 && (!declarator || !declarator->parameter_pack_p)
16023 && uses_parameter_packs (type))
16025 /* Consume the `...'. */
16026 cp_lexer_consume_token (parser->lexer);
16027 maybe_warn_variadic_templates ();
16029 /* Build a pack expansion type */
16030 if (declarator)
16031 declarator->parameter_pack_p = true;
16032 else
16033 decl_specifiers.type = make_pack_expansion (type);
16037 /* The restriction on defining new types applies only to the type
16038 of the parameter, not to the default argument. */
16039 parser->type_definition_forbidden_message = saved_message;
16041 /* If the next token is `=', then process a default argument. */
16042 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16044 /* Consume the `='. */
16045 cp_lexer_consume_token (parser->lexer);
16047 /* If we are defining a class, then the tokens that make up the
16048 default argument must be saved and processed later. */
16049 if (!template_parm_p && at_class_scope_p ()
16050 && TYPE_BEING_DEFINED (current_class_type)
16051 && !LAMBDA_TYPE_P (current_class_type))
16053 unsigned depth = 0;
16054 int maybe_template_id = 0;
16055 cp_token *first_token;
16056 cp_token *token;
16058 /* Add tokens until we have processed the entire default
16059 argument. We add the range [first_token, token). */
16060 first_token = cp_lexer_peek_token (parser->lexer);
16061 while (true)
16063 bool done = false;
16065 /* Peek at the next token. */
16066 token = cp_lexer_peek_token (parser->lexer);
16067 /* What we do depends on what token we have. */
16068 switch (token->type)
16070 /* In valid code, a default argument must be
16071 immediately followed by a `,' `)', or `...'. */
16072 case CPP_COMMA:
16073 if (depth == 0 && maybe_template_id)
16075 /* If we've seen a '<', we might be in a
16076 template-argument-list. Until Core issue 325 is
16077 resolved, we don't know how this situation ought
16078 to be handled, so try to DTRT. We check whether
16079 what comes after the comma is a valid parameter
16080 declaration list. If it is, then the comma ends
16081 the default argument; otherwise the default
16082 argument continues. */
16083 bool error = false;
16084 tree t;
16086 /* Set ITALP so cp_parser_parameter_declaration_list
16087 doesn't decide to commit to this parse. */
16088 bool saved_italp = parser->in_template_argument_list_p;
16089 parser->in_template_argument_list_p = true;
16091 cp_parser_parse_tentatively (parser);
16092 cp_lexer_consume_token (parser->lexer);
16093 begin_scope (sk_function_parms, NULL_TREE);
16094 cp_parser_parameter_declaration_list (parser, &error);
16095 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16096 pop_binding (DECL_NAME (t), t);
16097 leave_scope ();
16098 if (!cp_parser_error_occurred (parser) && !error)
16099 done = true;
16100 cp_parser_abort_tentative_parse (parser);
16102 parser->in_template_argument_list_p = saved_italp;
16103 break;
16105 case CPP_CLOSE_PAREN:
16106 case CPP_ELLIPSIS:
16107 /* If we run into a non-nested `;', `}', or `]',
16108 then the code is invalid -- but the default
16109 argument is certainly over. */
16110 case CPP_SEMICOLON:
16111 case CPP_CLOSE_BRACE:
16112 case CPP_CLOSE_SQUARE:
16113 if (depth == 0)
16114 done = true;
16115 /* Update DEPTH, if necessary. */
16116 else if (token->type == CPP_CLOSE_PAREN
16117 || token->type == CPP_CLOSE_BRACE
16118 || token->type == CPP_CLOSE_SQUARE)
16119 --depth;
16120 break;
16122 case CPP_OPEN_PAREN:
16123 case CPP_OPEN_SQUARE:
16124 case CPP_OPEN_BRACE:
16125 ++depth;
16126 break;
16128 case CPP_LESS:
16129 if (depth == 0)
16130 /* This might be the comparison operator, or it might
16131 start a template argument list. */
16132 ++maybe_template_id;
16133 break;
16135 case CPP_RSHIFT:
16136 if (cxx_dialect == cxx98)
16137 break;
16138 /* Fall through for C++0x, which treats the `>>'
16139 operator like two `>' tokens in certain
16140 cases. */
16142 case CPP_GREATER:
16143 if (depth == 0)
16145 /* This might be an operator, or it might close a
16146 template argument list. But if a previous '<'
16147 started a template argument list, this will have
16148 closed it, so we can't be in one anymore. */
16149 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16150 if (maybe_template_id < 0)
16151 maybe_template_id = 0;
16153 break;
16155 /* If we run out of tokens, issue an error message. */
16156 case CPP_EOF:
16157 case CPP_PRAGMA_EOL:
16158 error_at (token->location, "file ends in default argument");
16159 done = true;
16160 break;
16162 case CPP_NAME:
16163 case CPP_SCOPE:
16164 /* In these cases, we should look for template-ids.
16165 For example, if the default argument is
16166 `X<int, double>()', we need to do name lookup to
16167 figure out whether or not `X' is a template; if
16168 so, the `,' does not end the default argument.
16170 That is not yet done. */
16171 break;
16173 default:
16174 break;
16177 /* If we've reached the end, stop. */
16178 if (done)
16179 break;
16181 /* Add the token to the token block. */
16182 token = cp_lexer_consume_token (parser->lexer);
16185 /* Create a DEFAULT_ARG to represent the unparsed default
16186 argument. */
16187 default_argument = make_node (DEFAULT_ARG);
16188 DEFARG_TOKENS (default_argument)
16189 = cp_token_cache_new (first_token, token);
16190 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16192 /* Outside of a class definition, we can just parse the
16193 assignment-expression. */
16194 else
16196 token = cp_lexer_peek_token (parser->lexer);
16197 default_argument
16198 = cp_parser_default_argument (parser, template_parm_p);
16201 if (!parser->default_arg_ok_p)
16203 if (flag_permissive)
16204 warning (0, "deprecated use of default argument for parameter of non-function");
16205 else
16207 error_at (token->location,
16208 "default arguments are only "
16209 "permitted for function parameters");
16210 default_argument = NULL_TREE;
16213 else if ((declarator && declarator->parameter_pack_p)
16214 || (decl_specifiers.type
16215 && PACK_EXPANSION_P (decl_specifiers.type)))
16217 /* Find the name of the parameter pack. */
16218 cp_declarator *id_declarator = declarator;
16219 while (id_declarator && id_declarator->kind != cdk_id)
16220 id_declarator = id_declarator->declarator;
16222 if (id_declarator && id_declarator->kind == cdk_id)
16223 error_at (declarator_token_start->location,
16224 template_parm_p
16225 ? "template parameter pack %qD"
16226 " cannot have a default argument"
16227 : "parameter pack %qD cannot have a default argument",
16228 id_declarator->u.id.unqualified_name);
16229 else
16230 error_at (declarator_token_start->location,
16231 template_parm_p
16232 ? "template parameter pack cannot have a default argument"
16233 : "parameter pack cannot have a default argument");
16235 default_argument = NULL_TREE;
16238 else
16239 default_argument = NULL_TREE;
16241 return make_parameter_declarator (&decl_specifiers,
16242 declarator,
16243 default_argument);
16246 /* Parse a default argument and return it.
16248 TEMPLATE_PARM_P is true if this is a default argument for a
16249 non-type template parameter. */
16250 static tree
16251 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16253 tree default_argument = NULL_TREE;
16254 bool saved_greater_than_is_operator_p;
16255 bool saved_local_variables_forbidden_p;
16257 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16258 set correctly. */
16259 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16260 parser->greater_than_is_operator_p = !template_parm_p;
16261 /* Local variable names (and the `this' keyword) may not
16262 appear in a default argument. */
16263 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16264 parser->local_variables_forbidden_p = true;
16265 /* Parse the assignment-expression. */
16266 if (template_parm_p)
16267 push_deferring_access_checks (dk_no_deferred);
16268 default_argument
16269 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16270 if (template_parm_p)
16271 pop_deferring_access_checks ();
16272 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16273 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16275 return default_argument;
16278 /* Parse a function-body.
16280 function-body:
16281 compound_statement */
16283 static void
16284 cp_parser_function_body (cp_parser *parser)
16286 cp_parser_compound_statement (parser, NULL, false);
16289 /* Parse a ctor-initializer-opt followed by a function-body. Return
16290 true if a ctor-initializer was present. */
16292 static bool
16293 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16295 tree body, list;
16296 bool ctor_initializer_p;
16297 const bool check_body_p =
16298 DECL_CONSTRUCTOR_P (current_function_decl)
16299 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16300 tree last = NULL;
16302 /* Begin the function body. */
16303 body = begin_function_body ();
16304 /* Parse the optional ctor-initializer. */
16305 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16307 /* If we're parsing a constexpr constructor definition, we need
16308 to check that the constructor body is indeed empty. However,
16309 before we get to cp_parser_function_body lot of junk has been
16310 generated, so we can't just check that we have an empty block.
16311 Rather we take a snapshot of the outermost block, and check whether
16312 cp_parser_function_body changed its state. */
16313 if (check_body_p)
16315 list = body;
16316 if (TREE_CODE (list) == BIND_EXPR)
16317 list = BIND_EXPR_BODY (list);
16318 if (TREE_CODE (list) == STATEMENT_LIST
16319 && STATEMENT_LIST_TAIL (list) != NULL)
16320 last = STATEMENT_LIST_TAIL (list)->stmt;
16322 /* Parse the function-body. */
16323 cp_parser_function_body (parser);
16324 if (check_body_p)
16325 check_constexpr_ctor_body (last, list);
16326 /* Finish the function body. */
16327 finish_function_body (body);
16329 return ctor_initializer_p;
16332 /* Parse an initializer.
16334 initializer:
16335 = initializer-clause
16336 ( expression-list )
16338 Returns an expression representing the initializer. If no
16339 initializer is present, NULL_TREE is returned.
16341 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16342 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16343 set to TRUE if there is no initializer present. If there is an
16344 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16345 is set to true; otherwise it is set to false. */
16347 static tree
16348 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16349 bool* non_constant_p)
16351 cp_token *token;
16352 tree init;
16354 /* Peek at the next token. */
16355 token = cp_lexer_peek_token (parser->lexer);
16357 /* Let our caller know whether or not this initializer was
16358 parenthesized. */
16359 *is_direct_init = (token->type != CPP_EQ);
16360 /* Assume that the initializer is constant. */
16361 *non_constant_p = false;
16363 if (token->type == CPP_EQ)
16365 /* Consume the `='. */
16366 cp_lexer_consume_token (parser->lexer);
16367 /* Parse the initializer-clause. */
16368 init = cp_parser_initializer_clause (parser, non_constant_p);
16370 else if (token->type == CPP_OPEN_PAREN)
16372 VEC(tree,gc) *vec;
16373 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16374 /*cast_p=*/false,
16375 /*allow_expansion_p=*/true,
16376 non_constant_p);
16377 if (vec == NULL)
16378 return error_mark_node;
16379 init = build_tree_list_vec (vec);
16380 release_tree_vector (vec);
16382 else if (token->type == CPP_OPEN_BRACE)
16384 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16385 init = cp_parser_braced_list (parser, non_constant_p);
16386 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16388 else
16390 /* Anything else is an error. */
16391 cp_parser_error (parser, "expected initializer");
16392 init = error_mark_node;
16395 return init;
16398 /* Parse an initializer-clause.
16400 initializer-clause:
16401 assignment-expression
16402 braced-init-list
16404 Returns an expression representing the initializer.
16406 If the `assignment-expression' production is used the value
16407 returned is simply a representation for the expression.
16409 Otherwise, calls cp_parser_braced_list. */
16411 static tree
16412 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16414 tree initializer;
16416 /* Assume the expression is constant. */
16417 *non_constant_p = false;
16419 /* If it is not a `{', then we are looking at an
16420 assignment-expression. */
16421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16423 initializer
16424 = cp_parser_constant_expression (parser,
16425 /*allow_non_constant_p=*/true,
16426 non_constant_p);
16427 if (!*non_constant_p)
16429 /* We only want to fold if this is really a constant
16430 expression. FIXME Actually, we don't want to fold here, but in
16431 cp_finish_decl. */
16432 tree folded = fold_non_dependent_expr (initializer);
16433 folded = maybe_constant_value (folded);
16434 if (TREE_CONSTANT (folded))
16435 initializer = folded;
16438 else
16439 initializer = cp_parser_braced_list (parser, non_constant_p);
16441 return initializer;
16444 /* Parse a brace-enclosed initializer list.
16446 braced-init-list:
16447 { initializer-list , [opt] }
16450 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16451 the elements of the initializer-list (or NULL, if the last
16452 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16453 NULL_TREE. There is no way to detect whether or not the optional
16454 trailing `,' was provided. NON_CONSTANT_P is as for
16455 cp_parser_initializer. */
16457 static tree
16458 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16460 tree initializer;
16462 /* Consume the `{' token. */
16463 cp_lexer_consume_token (parser->lexer);
16464 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16465 initializer = make_node (CONSTRUCTOR);
16466 /* If it's not a `}', then there is a non-trivial initializer. */
16467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16469 /* Parse the initializer list. */
16470 CONSTRUCTOR_ELTS (initializer)
16471 = cp_parser_initializer_list (parser, non_constant_p);
16472 /* A trailing `,' token is allowed. */
16473 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16474 cp_lexer_consume_token (parser->lexer);
16476 /* Now, there should be a trailing `}'. */
16477 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16478 TREE_TYPE (initializer) = init_list_type_node;
16479 return initializer;
16482 /* Parse an initializer-list.
16484 initializer-list:
16485 initializer-clause ... [opt]
16486 initializer-list , initializer-clause ... [opt]
16488 GNU Extension:
16490 initializer-list:
16491 identifier : initializer-clause
16492 initializer-list, identifier : initializer-clause
16494 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16495 for the initializer. If the INDEX of the elt is non-NULL, it is the
16496 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16497 as for cp_parser_initializer. */
16499 static VEC(constructor_elt,gc) *
16500 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16502 VEC(constructor_elt,gc) *v = NULL;
16504 /* Assume all of the expressions are constant. */
16505 *non_constant_p = false;
16507 /* Parse the rest of the list. */
16508 while (true)
16510 cp_token *token;
16511 tree identifier;
16512 tree initializer;
16513 bool clause_non_constant_p;
16515 /* If the next token is an identifier and the following one is a
16516 colon, we are looking at the GNU designated-initializer
16517 syntax. */
16518 if (cp_parser_allow_gnu_extensions_p (parser)
16519 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16520 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16522 /* Warn the user that they are using an extension. */
16523 pedwarn (input_location, OPT_pedantic,
16524 "ISO C++ does not allow designated initializers");
16525 /* Consume the identifier. */
16526 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16527 /* Consume the `:'. */
16528 cp_lexer_consume_token (parser->lexer);
16530 else
16531 identifier = NULL_TREE;
16533 /* Parse the initializer. */
16534 initializer = cp_parser_initializer_clause (parser,
16535 &clause_non_constant_p);
16536 /* If any clause is non-constant, so is the entire initializer. */
16537 if (clause_non_constant_p)
16538 *non_constant_p = true;
16540 /* If we have an ellipsis, this is an initializer pack
16541 expansion. */
16542 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16544 /* Consume the `...'. */
16545 cp_lexer_consume_token (parser->lexer);
16547 /* Turn the initializer into an initializer expansion. */
16548 initializer = make_pack_expansion (initializer);
16551 /* Add it to the vector. */
16552 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16554 /* If the next token is not a comma, we have reached the end of
16555 the list. */
16556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16557 break;
16559 /* Peek at the next token. */
16560 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16561 /* If the next token is a `}', then we're still done. An
16562 initializer-clause can have a trailing `,' after the
16563 initializer-list and before the closing `}'. */
16564 if (token->type == CPP_CLOSE_BRACE)
16565 break;
16567 /* Consume the `,' token. */
16568 cp_lexer_consume_token (parser->lexer);
16571 return v;
16574 /* Classes [gram.class] */
16576 /* Parse a class-name.
16578 class-name:
16579 identifier
16580 template-id
16582 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16583 to indicate that names looked up in dependent types should be
16584 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16585 keyword has been used to indicate that the name that appears next
16586 is a template. TAG_TYPE indicates the explicit tag given before
16587 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16588 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16589 is the class being defined in a class-head.
16591 Returns the TYPE_DECL representing the class. */
16593 static tree
16594 cp_parser_class_name (cp_parser *parser,
16595 bool typename_keyword_p,
16596 bool template_keyword_p,
16597 enum tag_types tag_type,
16598 bool check_dependency_p,
16599 bool class_head_p,
16600 bool is_declaration)
16602 tree decl;
16603 tree scope;
16604 bool typename_p;
16605 cp_token *token;
16606 tree identifier = NULL_TREE;
16608 /* All class-names start with an identifier. */
16609 token = cp_lexer_peek_token (parser->lexer);
16610 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16612 cp_parser_error (parser, "expected class-name");
16613 return error_mark_node;
16616 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16617 to a template-id, so we save it here. */
16618 scope = parser->scope;
16619 if (scope == error_mark_node)
16620 return error_mark_node;
16622 /* Any name names a type if we're following the `typename' keyword
16623 in a qualified name where the enclosing scope is type-dependent. */
16624 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16625 && dependent_type_p (scope));
16626 /* Handle the common case (an identifier, but not a template-id)
16627 efficiently. */
16628 if (token->type == CPP_NAME
16629 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16631 cp_token *identifier_token;
16632 bool ambiguous_p;
16634 /* Look for the identifier. */
16635 identifier_token = cp_lexer_peek_token (parser->lexer);
16636 ambiguous_p = identifier_token->ambiguous_p;
16637 identifier = cp_parser_identifier (parser);
16638 /* If the next token isn't an identifier, we are certainly not
16639 looking at a class-name. */
16640 if (identifier == error_mark_node)
16641 decl = error_mark_node;
16642 /* If we know this is a type-name, there's no need to look it
16643 up. */
16644 else if (typename_p)
16645 decl = identifier;
16646 else
16648 tree ambiguous_decls;
16649 /* If we already know that this lookup is ambiguous, then
16650 we've already issued an error message; there's no reason
16651 to check again. */
16652 if (ambiguous_p)
16654 cp_parser_simulate_error (parser);
16655 return error_mark_node;
16657 /* If the next token is a `::', then the name must be a type
16658 name.
16660 [basic.lookup.qual]
16662 During the lookup for a name preceding the :: scope
16663 resolution operator, object, function, and enumerator
16664 names are ignored. */
16665 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16666 tag_type = typename_type;
16667 /* Look up the name. */
16668 decl = cp_parser_lookup_name (parser, identifier,
16669 tag_type,
16670 /*is_template=*/false,
16671 /*is_namespace=*/false,
16672 check_dependency_p,
16673 &ambiguous_decls,
16674 identifier_token->location);
16675 if (ambiguous_decls)
16677 if (cp_parser_parsing_tentatively (parser))
16678 cp_parser_simulate_error (parser);
16679 return error_mark_node;
16683 else
16685 /* Try a template-id. */
16686 decl = cp_parser_template_id (parser, template_keyword_p,
16687 check_dependency_p,
16688 is_declaration);
16689 if (decl == error_mark_node)
16690 return error_mark_node;
16693 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16695 /* If this is a typename, create a TYPENAME_TYPE. */
16696 if (typename_p && decl != error_mark_node)
16698 decl = make_typename_type (scope, decl, typename_type,
16699 /*complain=*/tf_error);
16700 if (decl != error_mark_node)
16701 decl = TYPE_NAME (decl);
16704 /* Check to see that it is really the name of a class. */
16705 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16706 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16707 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16708 /* Situations like this:
16710 template <typename T> struct A {
16711 typename T::template X<int>::I i;
16714 are problematic. Is `T::template X<int>' a class-name? The
16715 standard does not seem to be definitive, but there is no other
16716 valid interpretation of the following `::'. Therefore, those
16717 names are considered class-names. */
16719 decl = make_typename_type (scope, decl, tag_type, tf_error);
16720 if (decl != error_mark_node)
16721 decl = TYPE_NAME (decl);
16723 else if (TREE_CODE (decl) != TYPE_DECL
16724 || TREE_TYPE (decl) == error_mark_node
16725 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16726 PLUGIN_CLASS_NAME)
16727 decl = error_mark_node;
16729 if (decl == error_mark_node)
16730 cp_parser_error (parser, "expected class-name");
16731 else if (identifier && !parser->scope)
16732 maybe_note_name_used_in_class (identifier, decl);
16734 return decl;
16737 /* Parse a class-specifier.
16739 class-specifier:
16740 class-head { member-specification [opt] }
16742 Returns the TREE_TYPE representing the class. */
16744 static tree
16745 cp_parser_class_specifier (cp_parser* parser)
16747 tree type;
16748 tree attributes = NULL_TREE;
16749 bool nested_name_specifier_p;
16750 unsigned saved_num_template_parameter_lists;
16751 bool saved_in_function_body;
16752 bool saved_in_unbraced_linkage_specification_p;
16753 tree old_scope = NULL_TREE;
16754 tree scope = NULL_TREE;
16755 tree bases;
16757 push_deferring_access_checks (dk_no_deferred);
16759 /* Parse the class-head. */
16760 type = cp_parser_class_head (parser,
16761 &nested_name_specifier_p,
16762 &attributes,
16763 &bases);
16764 /* If the class-head was a semantic disaster, skip the entire body
16765 of the class. */
16766 if (!type)
16768 cp_parser_skip_to_end_of_block_or_statement (parser);
16769 pop_deferring_access_checks ();
16770 return error_mark_node;
16773 /* Look for the `{'. */
16774 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16776 pop_deferring_access_checks ();
16777 return error_mark_node;
16780 /* Process the base classes. If they're invalid, skip the
16781 entire class body. */
16782 if (!xref_basetypes (type, bases))
16784 /* Consuming the closing brace yields better error messages
16785 later on. */
16786 if (cp_parser_skip_to_closing_brace (parser))
16787 cp_lexer_consume_token (parser->lexer);
16788 pop_deferring_access_checks ();
16789 return error_mark_node;
16792 /* Issue an error message if type-definitions are forbidden here. */
16793 cp_parser_check_type_definition (parser);
16794 /* Remember that we are defining one more class. */
16795 ++parser->num_classes_being_defined;
16796 /* Inside the class, surrounding template-parameter-lists do not
16797 apply. */
16798 saved_num_template_parameter_lists
16799 = parser->num_template_parameter_lists;
16800 parser->num_template_parameter_lists = 0;
16801 /* We are not in a function body. */
16802 saved_in_function_body = parser->in_function_body;
16803 parser->in_function_body = false;
16804 /* We are not immediately inside an extern "lang" block. */
16805 saved_in_unbraced_linkage_specification_p
16806 = parser->in_unbraced_linkage_specification_p;
16807 parser->in_unbraced_linkage_specification_p = false;
16809 /* Start the class. */
16810 if (nested_name_specifier_p)
16812 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16813 old_scope = push_inner_scope (scope);
16815 type = begin_class_definition (type, attributes);
16817 if (type == error_mark_node)
16818 /* If the type is erroneous, skip the entire body of the class. */
16819 cp_parser_skip_to_closing_brace (parser);
16820 else
16821 /* Parse the member-specification. */
16822 cp_parser_member_specification_opt (parser);
16824 /* Look for the trailing `}'. */
16825 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16826 /* Look for trailing attributes to apply to this class. */
16827 if (cp_parser_allow_gnu_extensions_p (parser))
16828 attributes = cp_parser_attributes_opt (parser);
16829 if (type != error_mark_node)
16830 type = finish_struct (type, attributes);
16831 if (nested_name_specifier_p)
16832 pop_inner_scope (old_scope, scope);
16834 /* We've finished a type definition. Check for the common syntax
16835 error of forgetting a semicolon after the definition. We need to
16836 be careful, as we can't just check for not-a-semicolon and be done
16837 with it; the user might have typed:
16839 class X { } c = ...;
16840 class X { } *p = ...;
16842 and so forth. Instead, enumerate all the possible tokens that
16843 might follow this production; if we don't see one of them, then
16844 complain and silently insert the semicolon. */
16846 cp_token *token = cp_lexer_peek_token (parser->lexer);
16847 bool want_semicolon = true;
16849 switch (token->type)
16851 case CPP_NAME:
16852 case CPP_SEMICOLON:
16853 case CPP_MULT:
16854 case CPP_AND:
16855 case CPP_OPEN_PAREN:
16856 case CPP_CLOSE_PAREN:
16857 case CPP_COMMA:
16858 want_semicolon = false;
16859 break;
16861 /* While it's legal for type qualifiers and storage class
16862 specifiers to follow type definitions in the grammar, only
16863 compiler testsuites contain code like that. Assume that if
16864 we see such code, then what we're really seeing is a case
16865 like:
16867 class X { }
16868 const <type> var = ...;
16872 class Y { }
16873 static <type> func (...) ...
16875 i.e. the qualifier or specifier applies to the next
16876 declaration. To do so, however, we need to look ahead one
16877 more token to see if *that* token is a type specifier.
16879 This code could be improved to handle:
16881 class Z { }
16882 static const <type> var = ...; */
16883 case CPP_KEYWORD:
16884 if (keyword_is_decl_specifier (token->keyword))
16886 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16888 /* Handling user-defined types here would be nice, but very
16889 tricky. */
16890 want_semicolon
16891 = (lookahead->type == CPP_KEYWORD
16892 && keyword_begins_type_specifier (lookahead->keyword));
16894 break;
16895 default:
16896 break;
16899 /* If we don't have a type, then something is very wrong and we
16900 shouldn't try to do anything clever. */
16901 if (TYPE_P (type) && want_semicolon)
16903 cp_token_position prev
16904 = cp_lexer_previous_token_position (parser->lexer);
16905 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16906 location_t loc = prev_token->location;
16908 if (CLASSTYPE_DECLARED_CLASS (type))
16909 error_at (loc, "expected %<;%> after class definition");
16910 else if (TREE_CODE (type) == RECORD_TYPE)
16911 error_at (loc, "expected %<;%> after struct definition");
16912 else if (TREE_CODE (type) == UNION_TYPE)
16913 error_at (loc, "expected %<;%> after union definition");
16914 else
16915 gcc_unreachable ();
16917 /* Unget one token and smash it to look as though we encountered
16918 a semicolon in the input stream. */
16919 cp_lexer_set_token_position (parser->lexer, prev);
16920 token = cp_lexer_peek_token (parser->lexer);
16921 token->type = CPP_SEMICOLON;
16922 token->keyword = RID_MAX;
16926 /* If this class is not itself within the scope of another class,
16927 then we need to parse the bodies of all of the queued function
16928 definitions. Note that the queued functions defined in a class
16929 are not always processed immediately following the
16930 class-specifier for that class. Consider:
16932 struct A {
16933 struct B { void f() { sizeof (A); } };
16936 If `f' were processed before the processing of `A' were
16937 completed, there would be no way to compute the size of `A'.
16938 Note that the nesting we are interested in here is lexical --
16939 not the semantic nesting given by TYPE_CONTEXT. In particular,
16940 for:
16942 struct A { struct B; };
16943 struct A::B { void f() { } };
16945 there is no need to delay the parsing of `A::B::f'. */
16946 if (--parser->num_classes_being_defined == 0)
16948 tree fn;
16949 tree class_type = NULL_TREE;
16950 tree pushed_scope = NULL_TREE;
16951 unsigned ix;
16952 cp_default_arg_entry *e;
16954 /* In a first pass, parse default arguments to the functions.
16955 Then, in a second pass, parse the bodies of the functions.
16956 This two-phased approach handles cases like:
16958 struct S {
16959 void f() { g(); }
16960 void g(int i = 3);
16964 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16965 ix, e)
16967 fn = e->decl;
16968 /* If there are default arguments that have not yet been processed,
16969 take care of them now. */
16970 if (class_type != e->class_type)
16972 if (pushed_scope)
16973 pop_scope (pushed_scope);
16974 class_type = e->class_type;
16975 pushed_scope = push_scope (class_type);
16977 /* Make sure that any template parameters are in scope. */
16978 maybe_begin_member_template_processing (fn);
16979 /* Parse the default argument expressions. */
16980 cp_parser_late_parsing_default_args (parser, fn);
16981 /* Remove any template parameters from the symbol table. */
16982 maybe_end_member_template_processing ();
16984 if (pushed_scope)
16985 pop_scope (pushed_scope);
16986 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16987 /* Now parse the body of the functions. */
16988 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16989 cp_parser_late_parsing_for_member (parser, fn);
16990 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16993 /* Put back any saved access checks. */
16994 pop_deferring_access_checks ();
16996 /* Restore saved state. */
16997 parser->in_function_body = saved_in_function_body;
16998 parser->num_template_parameter_lists
16999 = saved_num_template_parameter_lists;
17000 parser->in_unbraced_linkage_specification_p
17001 = saved_in_unbraced_linkage_specification_p;
17003 return type;
17006 /* Parse a class-head.
17008 class-head:
17009 class-key identifier [opt] base-clause [opt]
17010 class-key nested-name-specifier identifier base-clause [opt]
17011 class-key nested-name-specifier [opt] template-id
17012 base-clause [opt]
17014 GNU Extensions:
17015 class-key attributes identifier [opt] base-clause [opt]
17016 class-key attributes nested-name-specifier identifier base-clause [opt]
17017 class-key attributes nested-name-specifier [opt] template-id
17018 base-clause [opt]
17020 Upon return BASES is initialized to the list of base classes (or
17021 NULL, if there are none) in the same form returned by
17022 cp_parser_base_clause.
17024 Returns the TYPE of the indicated class. Sets
17025 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17026 involving a nested-name-specifier was used, and FALSE otherwise.
17028 Returns error_mark_node if this is not a class-head.
17030 Returns NULL_TREE if the class-head is syntactically valid, but
17031 semantically invalid in a way that means we should skip the entire
17032 body of the class. */
17034 static tree
17035 cp_parser_class_head (cp_parser* parser,
17036 bool* nested_name_specifier_p,
17037 tree *attributes_p,
17038 tree *bases)
17040 tree nested_name_specifier;
17041 enum tag_types class_key;
17042 tree id = NULL_TREE;
17043 tree type = NULL_TREE;
17044 tree attributes;
17045 bool template_id_p = false;
17046 bool qualified_p = false;
17047 bool invalid_nested_name_p = false;
17048 bool invalid_explicit_specialization_p = false;
17049 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17050 tree pushed_scope = NULL_TREE;
17051 unsigned num_templates;
17052 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17053 /* Assume no nested-name-specifier will be present. */
17054 *nested_name_specifier_p = false;
17055 /* Assume no template parameter lists will be used in defining the
17056 type. */
17057 num_templates = 0;
17058 parser->colon_corrects_to_scope_p = false;
17060 *bases = NULL_TREE;
17062 /* Look for the class-key. */
17063 class_key = cp_parser_class_key (parser);
17064 if (class_key == none_type)
17065 return error_mark_node;
17067 /* Parse the attributes. */
17068 attributes = cp_parser_attributes_opt (parser);
17070 /* If the next token is `::', that is invalid -- but sometimes
17071 people do try to write:
17073 struct ::S {};
17075 Handle this gracefully by accepting the extra qualifier, and then
17076 issuing an error about it later if this really is a
17077 class-head. If it turns out just to be an elaborated type
17078 specifier, remain silent. */
17079 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17080 qualified_p = true;
17082 push_deferring_access_checks (dk_no_check);
17084 /* Determine the name of the class. Begin by looking for an
17085 optional nested-name-specifier. */
17086 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17087 nested_name_specifier
17088 = cp_parser_nested_name_specifier_opt (parser,
17089 /*typename_keyword_p=*/false,
17090 /*check_dependency_p=*/false,
17091 /*type_p=*/false,
17092 /*is_declaration=*/false);
17093 /* If there was a nested-name-specifier, then there *must* be an
17094 identifier. */
17095 if (nested_name_specifier)
17097 type_start_token = cp_lexer_peek_token (parser->lexer);
17098 /* Although the grammar says `identifier', it really means
17099 `class-name' or `template-name'. You are only allowed to
17100 define a class that has already been declared with this
17101 syntax.
17103 The proposed resolution for Core Issue 180 says that wherever
17104 you see `class T::X' you should treat `X' as a type-name.
17106 It is OK to define an inaccessible class; for example:
17108 class A { class B; };
17109 class A::B {};
17111 We do not know if we will see a class-name, or a
17112 template-name. We look for a class-name first, in case the
17113 class-name is a template-id; if we looked for the
17114 template-name first we would stop after the template-name. */
17115 cp_parser_parse_tentatively (parser);
17116 type = cp_parser_class_name (parser,
17117 /*typename_keyword_p=*/false,
17118 /*template_keyword_p=*/false,
17119 class_type,
17120 /*check_dependency_p=*/false,
17121 /*class_head_p=*/true,
17122 /*is_declaration=*/false);
17123 /* If that didn't work, ignore the nested-name-specifier. */
17124 if (!cp_parser_parse_definitely (parser))
17126 invalid_nested_name_p = true;
17127 type_start_token = cp_lexer_peek_token (parser->lexer);
17128 id = cp_parser_identifier (parser);
17129 if (id == error_mark_node)
17130 id = NULL_TREE;
17132 /* If we could not find a corresponding TYPE, treat this
17133 declaration like an unqualified declaration. */
17134 if (type == error_mark_node)
17135 nested_name_specifier = NULL_TREE;
17136 /* Otherwise, count the number of templates used in TYPE and its
17137 containing scopes. */
17138 else
17140 tree scope;
17142 for (scope = TREE_TYPE (type);
17143 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17144 scope = (TYPE_P (scope)
17145 ? TYPE_CONTEXT (scope)
17146 : DECL_CONTEXT (scope)))
17147 if (TYPE_P (scope)
17148 && CLASS_TYPE_P (scope)
17149 && CLASSTYPE_TEMPLATE_INFO (scope)
17150 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17152 ++num_templates;
17155 /* Otherwise, the identifier is optional. */
17156 else
17158 /* We don't know whether what comes next is a template-id,
17159 an identifier, or nothing at all. */
17160 cp_parser_parse_tentatively (parser);
17161 /* Check for a template-id. */
17162 type_start_token = cp_lexer_peek_token (parser->lexer);
17163 id = cp_parser_template_id (parser,
17164 /*template_keyword_p=*/false,
17165 /*check_dependency_p=*/true,
17166 /*is_declaration=*/true);
17167 /* If that didn't work, it could still be an identifier. */
17168 if (!cp_parser_parse_definitely (parser))
17170 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17172 type_start_token = cp_lexer_peek_token (parser->lexer);
17173 id = cp_parser_identifier (parser);
17175 else
17176 id = NULL_TREE;
17178 else
17180 template_id_p = true;
17181 ++num_templates;
17185 pop_deferring_access_checks ();
17187 if (id)
17188 cp_parser_check_for_invalid_template_id (parser, id,
17189 type_start_token->location);
17191 /* If it's not a `:' or a `{' then we can't really be looking at a
17192 class-head, since a class-head only appears as part of a
17193 class-specifier. We have to detect this situation before calling
17194 xref_tag, since that has irreversible side-effects. */
17195 if (!cp_parser_next_token_starts_class_definition_p (parser))
17197 cp_parser_error (parser, "expected %<{%> or %<:%>");
17198 type = error_mark_node;
17199 goto out;
17202 /* At this point, we're going ahead with the class-specifier, even
17203 if some other problem occurs. */
17204 cp_parser_commit_to_tentative_parse (parser);
17205 /* Issue the error about the overly-qualified name now. */
17206 if (qualified_p)
17208 cp_parser_error (parser,
17209 "global qualification of class name is invalid");
17210 type = error_mark_node;
17211 goto out;
17213 else if (invalid_nested_name_p)
17215 cp_parser_error (parser,
17216 "qualified name does not name a class");
17217 type = error_mark_node;
17218 goto out;
17220 else if (nested_name_specifier)
17222 tree scope;
17224 /* Reject typedef-names in class heads. */
17225 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17227 error_at (type_start_token->location,
17228 "invalid class name in declaration of %qD",
17229 type);
17230 type = NULL_TREE;
17231 goto done;
17234 /* Figure out in what scope the declaration is being placed. */
17235 scope = current_scope ();
17236 /* If that scope does not contain the scope in which the
17237 class was originally declared, the program is invalid. */
17238 if (scope && !is_ancestor (scope, nested_name_specifier))
17240 if (at_namespace_scope_p ())
17241 error_at (type_start_token->location,
17242 "declaration of %qD in namespace %qD which does not "
17243 "enclose %qD",
17244 type, scope, nested_name_specifier);
17245 else
17246 error_at (type_start_token->location,
17247 "declaration of %qD in %qD which does not enclose %qD",
17248 type, scope, nested_name_specifier);
17249 type = NULL_TREE;
17250 goto done;
17252 /* [dcl.meaning]
17254 A declarator-id shall not be qualified except for the
17255 definition of a ... nested class outside of its class
17256 ... [or] the definition or explicit instantiation of a
17257 class member of a namespace outside of its namespace. */
17258 if (scope == nested_name_specifier)
17260 permerror (nested_name_specifier_token_start->location,
17261 "extra qualification not allowed");
17262 nested_name_specifier = NULL_TREE;
17263 num_templates = 0;
17266 /* An explicit-specialization must be preceded by "template <>". If
17267 it is not, try to recover gracefully. */
17268 if (at_namespace_scope_p ()
17269 && parser->num_template_parameter_lists == 0
17270 && template_id_p)
17272 error_at (type_start_token->location,
17273 "an explicit specialization must be preceded by %<template <>%>");
17274 invalid_explicit_specialization_p = true;
17275 /* Take the same action that would have been taken by
17276 cp_parser_explicit_specialization. */
17277 ++parser->num_template_parameter_lists;
17278 begin_specialization ();
17280 /* There must be no "return" statements between this point and the
17281 end of this function; set "type "to the correct return value and
17282 use "goto done;" to return. */
17283 /* Make sure that the right number of template parameters were
17284 present. */
17285 if (!cp_parser_check_template_parameters (parser, num_templates,
17286 type_start_token->location,
17287 /*declarator=*/NULL))
17289 /* If something went wrong, there is no point in even trying to
17290 process the class-definition. */
17291 type = NULL_TREE;
17292 goto done;
17295 /* Look up the type. */
17296 if (template_id_p)
17298 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17299 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17300 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17302 error_at (type_start_token->location,
17303 "function template %qD redeclared as a class template", id);
17304 type = error_mark_node;
17306 else
17308 type = TREE_TYPE (id);
17309 type = maybe_process_partial_specialization (type);
17311 if (nested_name_specifier)
17312 pushed_scope = push_scope (nested_name_specifier);
17314 else if (nested_name_specifier)
17316 tree class_type;
17318 /* Given:
17320 template <typename T> struct S { struct T };
17321 template <typename T> struct S<T>::T { };
17323 we will get a TYPENAME_TYPE when processing the definition of
17324 `S::T'. We need to resolve it to the actual type before we
17325 try to define it. */
17326 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17328 class_type = resolve_typename_type (TREE_TYPE (type),
17329 /*only_current_p=*/false);
17330 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17331 type = TYPE_NAME (class_type);
17332 else
17334 cp_parser_error (parser, "could not resolve typename type");
17335 type = error_mark_node;
17339 if (maybe_process_partial_specialization (TREE_TYPE (type))
17340 == error_mark_node)
17342 type = NULL_TREE;
17343 goto done;
17346 class_type = current_class_type;
17347 /* Enter the scope indicated by the nested-name-specifier. */
17348 pushed_scope = push_scope (nested_name_specifier);
17349 /* Get the canonical version of this type. */
17350 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17351 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17352 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17354 type = push_template_decl (type);
17355 if (type == error_mark_node)
17357 type = NULL_TREE;
17358 goto done;
17362 type = TREE_TYPE (type);
17363 *nested_name_specifier_p = true;
17365 else /* The name is not a nested name. */
17367 /* If the class was unnamed, create a dummy name. */
17368 if (!id)
17369 id = make_anon_name ();
17370 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17371 parser->num_template_parameter_lists);
17374 /* Indicate whether this class was declared as a `class' or as a
17375 `struct'. */
17376 if (TREE_CODE (type) == RECORD_TYPE)
17377 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17378 cp_parser_check_class_key (class_key, type);
17380 /* If this type was already complete, and we see another definition,
17381 that's an error. */
17382 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17384 error_at (type_start_token->location, "redefinition of %q#T",
17385 type);
17386 error_at (type_start_token->location, "previous definition of %q+#T",
17387 type);
17388 type = NULL_TREE;
17389 goto done;
17391 else if (type == error_mark_node)
17392 type = NULL_TREE;
17394 /* We will have entered the scope containing the class; the names of
17395 base classes should be looked up in that context. For example:
17397 struct A { struct B {}; struct C; };
17398 struct A::C : B {};
17400 is valid. */
17402 /* Get the list of base-classes, if there is one. */
17403 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17404 *bases = cp_parser_base_clause (parser);
17406 done:
17407 /* Leave the scope given by the nested-name-specifier. We will
17408 enter the class scope itself while processing the members. */
17409 if (pushed_scope)
17410 pop_scope (pushed_scope);
17412 if (invalid_explicit_specialization_p)
17414 end_specialization ();
17415 --parser->num_template_parameter_lists;
17418 if (type)
17419 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17420 *attributes_p = attributes;
17421 out:
17422 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17423 return type;
17426 /* Parse a class-key.
17428 class-key:
17429 class
17430 struct
17431 union
17433 Returns the kind of class-key specified, or none_type to indicate
17434 error. */
17436 static enum tag_types
17437 cp_parser_class_key (cp_parser* parser)
17439 cp_token *token;
17440 enum tag_types tag_type;
17442 /* Look for the class-key. */
17443 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17444 if (!token)
17445 return none_type;
17447 /* Check to see if the TOKEN is a class-key. */
17448 tag_type = cp_parser_token_is_class_key (token);
17449 if (!tag_type)
17450 cp_parser_error (parser, "expected class-key");
17451 return tag_type;
17454 /* Parse an (optional) member-specification.
17456 member-specification:
17457 member-declaration member-specification [opt]
17458 access-specifier : member-specification [opt] */
17460 static void
17461 cp_parser_member_specification_opt (cp_parser* parser)
17463 while (true)
17465 cp_token *token;
17466 enum rid keyword;
17468 /* Peek at the next token. */
17469 token = cp_lexer_peek_token (parser->lexer);
17470 /* If it's a `}', or EOF then we've seen all the members. */
17471 if (token->type == CPP_CLOSE_BRACE
17472 || token->type == CPP_EOF
17473 || token->type == CPP_PRAGMA_EOL)
17474 break;
17476 /* See if this token is a keyword. */
17477 keyword = token->keyword;
17478 switch (keyword)
17480 case RID_PUBLIC:
17481 case RID_PROTECTED:
17482 case RID_PRIVATE:
17483 /* Consume the access-specifier. */
17484 cp_lexer_consume_token (parser->lexer);
17485 /* Remember which access-specifier is active. */
17486 current_access_specifier = token->u.value;
17487 /* Look for the `:'. */
17488 cp_parser_require (parser, CPP_COLON, RT_COLON);
17489 break;
17491 default:
17492 /* Accept #pragmas at class scope. */
17493 if (token->type == CPP_PRAGMA)
17495 cp_parser_pragma (parser, pragma_external);
17496 break;
17499 /* Otherwise, the next construction must be a
17500 member-declaration. */
17501 cp_parser_member_declaration (parser);
17506 /* Parse a member-declaration.
17508 member-declaration:
17509 decl-specifier-seq [opt] member-declarator-list [opt] ;
17510 function-definition ; [opt]
17511 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17512 using-declaration
17513 template-declaration
17515 member-declarator-list:
17516 member-declarator
17517 member-declarator-list , member-declarator
17519 member-declarator:
17520 declarator pure-specifier [opt]
17521 declarator constant-initializer [opt]
17522 identifier [opt] : constant-expression
17524 GNU Extensions:
17526 member-declaration:
17527 __extension__ member-declaration
17529 member-declarator:
17530 declarator attributes [opt] pure-specifier [opt]
17531 declarator attributes [opt] constant-initializer [opt]
17532 identifier [opt] attributes [opt] : constant-expression
17534 C++0x Extensions:
17536 member-declaration:
17537 static_assert-declaration */
17539 static void
17540 cp_parser_member_declaration (cp_parser* parser)
17542 cp_decl_specifier_seq decl_specifiers;
17543 tree prefix_attributes;
17544 tree decl;
17545 int declares_class_or_enum;
17546 bool friend_p;
17547 cp_token *token = NULL;
17548 cp_token *decl_spec_token_start = NULL;
17549 cp_token *initializer_token_start = NULL;
17550 int saved_pedantic;
17551 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17553 /* Check for the `__extension__' keyword. */
17554 if (cp_parser_extension_opt (parser, &saved_pedantic))
17556 /* Recurse. */
17557 cp_parser_member_declaration (parser);
17558 /* Restore the old value of the PEDANTIC flag. */
17559 pedantic = saved_pedantic;
17561 return;
17564 /* Check for a template-declaration. */
17565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17567 /* An explicit specialization here is an error condition, and we
17568 expect the specialization handler to detect and report this. */
17569 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17570 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17571 cp_parser_explicit_specialization (parser);
17572 else
17573 cp_parser_template_declaration (parser, /*member_p=*/true);
17575 return;
17578 /* Check for a using-declaration. */
17579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17581 /* Parse the using-declaration. */
17582 cp_parser_using_declaration (parser,
17583 /*access_declaration_p=*/false);
17584 return;
17587 PLUGIN_MEMBER_DECLARATION;
17589 /* If the next token is `static_assert' we have a static assertion. */
17590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17592 cp_parser_static_assert (parser, /*member_p=*/true);
17593 return;
17596 parser->colon_corrects_to_scope_p = false;
17598 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17599 goto out;
17601 /* Parse the decl-specifier-seq. */
17602 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17603 cp_parser_decl_specifier_seq (parser,
17604 CP_PARSER_FLAGS_OPTIONAL,
17605 &decl_specifiers,
17606 &declares_class_or_enum);
17607 prefix_attributes = decl_specifiers.attributes;
17608 decl_specifiers.attributes = NULL_TREE;
17609 /* Check for an invalid type-name. */
17610 if (!decl_specifiers.any_type_specifiers_p
17611 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17612 goto out;
17613 /* If there is no declarator, then the decl-specifier-seq should
17614 specify a type. */
17615 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17617 /* If there was no decl-specifier-seq, and the next token is a
17618 `;', then we have something like:
17620 struct S { ; };
17622 [class.mem]
17624 Each member-declaration shall declare at least one member
17625 name of the class. */
17626 if (!decl_specifiers.any_specifiers_p)
17628 cp_token *token = cp_lexer_peek_token (parser->lexer);
17629 if (!in_system_header_at (token->location))
17630 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17632 else
17634 tree type;
17636 /* See if this declaration is a friend. */
17637 friend_p = cp_parser_friend_p (&decl_specifiers);
17638 /* If there were decl-specifiers, check to see if there was
17639 a class-declaration. */
17640 type = check_tag_decl (&decl_specifiers);
17641 /* Nested classes have already been added to the class, but
17642 a `friend' needs to be explicitly registered. */
17643 if (friend_p)
17645 /* If the `friend' keyword was present, the friend must
17646 be introduced with a class-key. */
17647 if (!declares_class_or_enum)
17648 error_at (decl_spec_token_start->location,
17649 "a class-key must be used when declaring a friend");
17650 /* In this case:
17652 template <typename T> struct A {
17653 friend struct A<T>::B;
17656 A<T>::B will be represented by a TYPENAME_TYPE, and
17657 therefore not recognized by check_tag_decl. */
17658 if (!type
17659 && decl_specifiers.type
17660 && TYPE_P (decl_specifiers.type))
17661 type = decl_specifiers.type;
17662 if (!type || !TYPE_P (type))
17663 error_at (decl_spec_token_start->location,
17664 "friend declaration does not name a class or "
17665 "function");
17666 else
17667 make_friend_class (current_class_type, type,
17668 /*complain=*/true);
17670 /* If there is no TYPE, an error message will already have
17671 been issued. */
17672 else if (!type || type == error_mark_node)
17674 /* An anonymous aggregate has to be handled specially; such
17675 a declaration really declares a data member (with a
17676 particular type), as opposed to a nested class. */
17677 else if (ANON_AGGR_TYPE_P (type))
17679 /* Remove constructors and such from TYPE, now that we
17680 know it is an anonymous aggregate. */
17681 fixup_anonymous_aggr (type);
17682 /* And make the corresponding data member. */
17683 decl = build_decl (decl_spec_token_start->location,
17684 FIELD_DECL, NULL_TREE, type);
17685 /* Add it to the class. */
17686 finish_member_declaration (decl);
17688 else
17689 cp_parser_check_access_in_redeclaration
17690 (TYPE_NAME (type),
17691 decl_spec_token_start->location);
17694 else
17696 bool assume_semicolon = false;
17698 /* See if these declarations will be friends. */
17699 friend_p = cp_parser_friend_p (&decl_specifiers);
17701 /* Keep going until we hit the `;' at the end of the
17702 declaration. */
17703 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17705 tree attributes = NULL_TREE;
17706 tree first_attribute;
17708 /* Peek at the next token. */
17709 token = cp_lexer_peek_token (parser->lexer);
17711 /* Check for a bitfield declaration. */
17712 if (token->type == CPP_COLON
17713 || (token->type == CPP_NAME
17714 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17715 == CPP_COLON))
17717 tree identifier;
17718 tree width;
17720 /* Get the name of the bitfield. Note that we cannot just
17721 check TOKEN here because it may have been invalidated by
17722 the call to cp_lexer_peek_nth_token above. */
17723 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17724 identifier = cp_parser_identifier (parser);
17725 else
17726 identifier = NULL_TREE;
17728 /* Consume the `:' token. */
17729 cp_lexer_consume_token (parser->lexer);
17730 /* Get the width of the bitfield. */
17731 width
17732 = cp_parser_constant_expression (parser,
17733 /*allow_non_constant=*/false,
17734 NULL);
17736 /* Look for attributes that apply to the bitfield. */
17737 attributes = cp_parser_attributes_opt (parser);
17738 /* Remember which attributes are prefix attributes and
17739 which are not. */
17740 first_attribute = attributes;
17741 /* Combine the attributes. */
17742 attributes = chainon (prefix_attributes, attributes);
17744 /* Create the bitfield declaration. */
17745 decl = grokbitfield (identifier
17746 ? make_id_declarator (NULL_TREE,
17747 identifier,
17748 sfk_none)
17749 : NULL,
17750 &decl_specifiers,
17751 width,
17752 attributes);
17754 else
17756 cp_declarator *declarator;
17757 tree initializer;
17758 tree asm_specification;
17759 int ctor_dtor_or_conv_p;
17761 /* Parse the declarator. */
17762 declarator
17763 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17764 &ctor_dtor_or_conv_p,
17765 /*parenthesized_p=*/NULL,
17766 /*member_p=*/true);
17768 /* If something went wrong parsing the declarator, make sure
17769 that we at least consume some tokens. */
17770 if (declarator == cp_error_declarator)
17772 /* Skip to the end of the statement. */
17773 cp_parser_skip_to_end_of_statement (parser);
17774 /* If the next token is not a semicolon, that is
17775 probably because we just skipped over the body of
17776 a function. So, we consume a semicolon if
17777 present, but do not issue an error message if it
17778 is not present. */
17779 if (cp_lexer_next_token_is (parser->lexer,
17780 CPP_SEMICOLON))
17781 cp_lexer_consume_token (parser->lexer);
17782 goto out;
17785 if (declares_class_or_enum & 2)
17786 cp_parser_check_for_definition_in_return_type
17787 (declarator, decl_specifiers.type,
17788 decl_specifiers.type_location);
17790 /* Look for an asm-specification. */
17791 asm_specification = cp_parser_asm_specification_opt (parser);
17792 /* Look for attributes that apply to the declaration. */
17793 attributes = cp_parser_attributes_opt (parser);
17794 /* Remember which attributes are prefix attributes and
17795 which are not. */
17796 first_attribute = attributes;
17797 /* Combine the attributes. */
17798 attributes = chainon (prefix_attributes, attributes);
17800 /* If it's an `=', then we have a constant-initializer or a
17801 pure-specifier. It is not correct to parse the
17802 initializer before registering the member declaration
17803 since the member declaration should be in scope while
17804 its initializer is processed. However, the rest of the
17805 front end does not yet provide an interface that allows
17806 us to handle this correctly. */
17807 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17809 /* In [class.mem]:
17811 A pure-specifier shall be used only in the declaration of
17812 a virtual function.
17814 A member-declarator can contain a constant-initializer
17815 only if it declares a static member of integral or
17816 enumeration type.
17818 Therefore, if the DECLARATOR is for a function, we look
17819 for a pure-specifier; otherwise, we look for a
17820 constant-initializer. When we call `grokfield', it will
17821 perform more stringent semantics checks. */
17822 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17823 if (function_declarator_p (declarator))
17824 initializer = cp_parser_pure_specifier (parser);
17825 else
17826 /* Parse the initializer. */
17827 initializer = cp_parser_constant_initializer (parser);
17829 /* Otherwise, there is no initializer. */
17830 else
17831 initializer = NULL_TREE;
17833 /* See if we are probably looking at a function
17834 definition. We are certainly not looking at a
17835 member-declarator. Calling `grokfield' has
17836 side-effects, so we must not do it unless we are sure
17837 that we are looking at a member-declarator. */
17838 if (cp_parser_token_starts_function_definition_p
17839 (cp_lexer_peek_token (parser->lexer)))
17841 /* The grammar does not allow a pure-specifier to be
17842 used when a member function is defined. (It is
17843 possible that this fact is an oversight in the
17844 standard, since a pure function may be defined
17845 outside of the class-specifier. */
17846 if (initializer)
17847 error_at (initializer_token_start->location,
17848 "pure-specifier on function-definition");
17849 decl = cp_parser_save_member_function_body (parser,
17850 &decl_specifiers,
17851 declarator,
17852 attributes);
17853 /* If the member was not a friend, declare it here. */
17854 if (!friend_p)
17855 finish_member_declaration (decl);
17856 /* Peek at the next token. */
17857 token = cp_lexer_peek_token (parser->lexer);
17858 /* If the next token is a semicolon, consume it. */
17859 if (token->type == CPP_SEMICOLON)
17860 cp_lexer_consume_token (parser->lexer);
17861 goto out;
17863 else
17864 if (declarator->kind == cdk_function)
17865 declarator->id_loc = token->location;
17866 /* Create the declaration. */
17867 decl = grokfield (declarator, &decl_specifiers,
17868 initializer, /*init_const_expr_p=*/true,
17869 asm_specification,
17870 attributes);
17873 /* Reset PREFIX_ATTRIBUTES. */
17874 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17875 attributes = TREE_CHAIN (attributes);
17876 if (attributes)
17877 TREE_CHAIN (attributes) = NULL_TREE;
17879 /* If there is any qualification still in effect, clear it
17880 now; we will be starting fresh with the next declarator. */
17881 parser->scope = NULL_TREE;
17882 parser->qualifying_scope = NULL_TREE;
17883 parser->object_scope = NULL_TREE;
17884 /* If it's a `,', then there are more declarators. */
17885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17886 cp_lexer_consume_token (parser->lexer);
17887 /* If the next token isn't a `;', then we have a parse error. */
17888 else if (cp_lexer_next_token_is_not (parser->lexer,
17889 CPP_SEMICOLON))
17891 /* The next token might be a ways away from where the
17892 actual semicolon is missing. Find the previous token
17893 and use that for our error position. */
17894 cp_token *token = cp_lexer_previous_token (parser->lexer);
17895 error_at (token->location,
17896 "expected %<;%> at end of member declaration");
17898 /* Assume that the user meant to provide a semicolon. If
17899 we were to cp_parser_skip_to_end_of_statement, we might
17900 skip to a semicolon inside a member function definition
17901 and issue nonsensical error messages. */
17902 assume_semicolon = true;
17905 if (decl)
17907 /* Add DECL to the list of members. */
17908 if (!friend_p)
17909 finish_member_declaration (decl);
17911 if (TREE_CODE (decl) == FUNCTION_DECL)
17912 cp_parser_save_default_args (parser, decl);
17915 if (assume_semicolon)
17916 goto out;
17920 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17921 out:
17922 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17925 /* Parse a pure-specifier.
17927 pure-specifier:
17930 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17931 Otherwise, ERROR_MARK_NODE is returned. */
17933 static tree
17934 cp_parser_pure_specifier (cp_parser* parser)
17936 cp_token *token;
17938 /* Look for the `=' token. */
17939 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17940 return error_mark_node;
17941 /* Look for the `0' token. */
17942 token = cp_lexer_peek_token (parser->lexer);
17944 if (token->type == CPP_EOF
17945 || token->type == CPP_PRAGMA_EOL)
17946 return error_mark_node;
17948 cp_lexer_consume_token (parser->lexer);
17950 /* Accept = default or = delete in c++0x mode. */
17951 if (token->keyword == RID_DEFAULT
17952 || token->keyword == RID_DELETE)
17954 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17955 return token->u.value;
17958 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17959 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17961 cp_parser_error (parser,
17962 "invalid pure specifier (only %<= 0%> is allowed)");
17963 cp_parser_skip_to_end_of_statement (parser);
17964 return error_mark_node;
17966 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17968 error_at (token->location, "templates may not be %<virtual%>");
17969 return error_mark_node;
17972 return integer_zero_node;
17975 /* Parse a constant-initializer.
17977 constant-initializer:
17978 = constant-expression
17980 Returns a representation of the constant-expression. */
17982 static tree
17983 cp_parser_constant_initializer (cp_parser* parser)
17985 /* Look for the `=' token. */
17986 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17987 return error_mark_node;
17989 /* It is invalid to write:
17991 struct S { static const int i = { 7 }; };
17994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17996 cp_parser_error (parser,
17997 "a brace-enclosed initializer is not allowed here");
17998 /* Consume the opening brace. */
17999 cp_lexer_consume_token (parser->lexer);
18000 /* Skip the initializer. */
18001 cp_parser_skip_to_closing_brace (parser);
18002 /* Look for the trailing `}'. */
18003 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18005 return error_mark_node;
18008 return cp_parser_constant_expression (parser,
18009 /*allow_non_constant=*/false,
18010 NULL);
18013 /* Derived classes [gram.class.derived] */
18015 /* Parse a base-clause.
18017 base-clause:
18018 : base-specifier-list
18020 base-specifier-list:
18021 base-specifier ... [opt]
18022 base-specifier-list , base-specifier ... [opt]
18024 Returns a TREE_LIST representing the base-classes, in the order in
18025 which they were declared. The representation of each node is as
18026 described by cp_parser_base_specifier.
18028 In the case that no bases are specified, this function will return
18029 NULL_TREE, not ERROR_MARK_NODE. */
18031 static tree
18032 cp_parser_base_clause (cp_parser* parser)
18034 tree bases = NULL_TREE;
18036 /* Look for the `:' that begins the list. */
18037 cp_parser_require (parser, CPP_COLON, RT_COLON);
18039 /* Scan the base-specifier-list. */
18040 while (true)
18042 cp_token *token;
18043 tree base;
18044 bool pack_expansion_p = false;
18046 /* Look for the base-specifier. */
18047 base = cp_parser_base_specifier (parser);
18048 /* Look for the (optional) ellipsis. */
18049 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18051 /* Consume the `...'. */
18052 cp_lexer_consume_token (parser->lexer);
18054 pack_expansion_p = true;
18057 /* Add BASE to the front of the list. */
18058 if (base != error_mark_node)
18060 if (pack_expansion_p)
18061 /* Make this a pack expansion type. */
18062 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18065 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18067 TREE_CHAIN (base) = bases;
18068 bases = base;
18071 /* Peek at the next token. */
18072 token = cp_lexer_peek_token (parser->lexer);
18073 /* If it's not a comma, then the list is complete. */
18074 if (token->type != CPP_COMMA)
18075 break;
18076 /* Consume the `,'. */
18077 cp_lexer_consume_token (parser->lexer);
18080 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18081 base class had a qualified name. However, the next name that
18082 appears is certainly not qualified. */
18083 parser->scope = NULL_TREE;
18084 parser->qualifying_scope = NULL_TREE;
18085 parser->object_scope = NULL_TREE;
18087 return nreverse (bases);
18090 /* Parse a base-specifier.
18092 base-specifier:
18093 :: [opt] nested-name-specifier [opt] class-name
18094 virtual access-specifier [opt] :: [opt] nested-name-specifier
18095 [opt] class-name
18096 access-specifier virtual [opt] :: [opt] nested-name-specifier
18097 [opt] class-name
18099 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18100 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18101 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18102 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18104 static tree
18105 cp_parser_base_specifier (cp_parser* parser)
18107 cp_token *token;
18108 bool done = false;
18109 bool virtual_p = false;
18110 bool duplicate_virtual_error_issued_p = false;
18111 bool duplicate_access_error_issued_p = false;
18112 bool class_scope_p, template_p;
18113 tree access = access_default_node;
18114 tree type;
18116 /* Process the optional `virtual' and `access-specifier'. */
18117 while (!done)
18119 /* Peek at the next token. */
18120 token = cp_lexer_peek_token (parser->lexer);
18121 /* Process `virtual'. */
18122 switch (token->keyword)
18124 case RID_VIRTUAL:
18125 /* If `virtual' appears more than once, issue an error. */
18126 if (virtual_p && !duplicate_virtual_error_issued_p)
18128 cp_parser_error (parser,
18129 "%<virtual%> specified more than once in base-specified");
18130 duplicate_virtual_error_issued_p = true;
18133 virtual_p = true;
18135 /* Consume the `virtual' token. */
18136 cp_lexer_consume_token (parser->lexer);
18138 break;
18140 case RID_PUBLIC:
18141 case RID_PROTECTED:
18142 case RID_PRIVATE:
18143 /* If more than one access specifier appears, issue an
18144 error. */
18145 if (access != access_default_node
18146 && !duplicate_access_error_issued_p)
18148 cp_parser_error (parser,
18149 "more than one access specifier in base-specified");
18150 duplicate_access_error_issued_p = true;
18153 access = ridpointers[(int) token->keyword];
18155 /* Consume the access-specifier. */
18156 cp_lexer_consume_token (parser->lexer);
18158 break;
18160 default:
18161 done = true;
18162 break;
18165 /* It is not uncommon to see programs mechanically, erroneously, use
18166 the 'typename' keyword to denote (dependent) qualified types
18167 as base classes. */
18168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18170 token = cp_lexer_peek_token (parser->lexer);
18171 if (!processing_template_decl)
18172 error_at (token->location,
18173 "keyword %<typename%> not allowed outside of templates");
18174 else
18175 error_at (token->location,
18176 "keyword %<typename%> not allowed in this context "
18177 "(the base class is implicitly a type)");
18178 cp_lexer_consume_token (parser->lexer);
18181 /* Look for the optional `::' operator. */
18182 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18183 /* Look for the nested-name-specifier. The simplest way to
18184 implement:
18186 [temp.res]
18188 The keyword `typename' is not permitted in a base-specifier or
18189 mem-initializer; in these contexts a qualified name that
18190 depends on a template-parameter is implicitly assumed to be a
18191 type name.
18193 is to pretend that we have seen the `typename' keyword at this
18194 point. */
18195 cp_parser_nested_name_specifier_opt (parser,
18196 /*typename_keyword_p=*/true,
18197 /*check_dependency_p=*/true,
18198 typename_type,
18199 /*is_declaration=*/true);
18200 /* If the base class is given by a qualified name, assume that names
18201 we see are type names or templates, as appropriate. */
18202 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18203 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18205 /* Finally, look for the class-name. */
18206 type = cp_parser_class_name (parser,
18207 class_scope_p,
18208 template_p,
18209 typename_type,
18210 /*check_dependency_p=*/true,
18211 /*class_head_p=*/false,
18212 /*is_declaration=*/true);
18214 if (type == error_mark_node)
18215 return error_mark_node;
18217 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18220 /* Exception handling [gram.exception] */
18222 /* Parse an (optional) exception-specification.
18224 exception-specification:
18225 throw ( type-id-list [opt] )
18227 Returns a TREE_LIST representing the exception-specification. The
18228 TREE_VALUE of each node is a type. */
18230 static tree
18231 cp_parser_exception_specification_opt (cp_parser* parser)
18233 cp_token *token;
18234 tree type_id_list;
18235 const char *saved_message;
18237 /* Peek at the next token. */
18238 token = cp_lexer_peek_token (parser->lexer);
18240 /* Is it a noexcept-specification? */
18241 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18243 tree expr;
18244 cp_lexer_consume_token (parser->lexer);
18246 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18248 cp_lexer_consume_token (parser->lexer);
18250 /* Types may not be defined in an exception-specification. */
18251 saved_message = parser->type_definition_forbidden_message;
18252 parser->type_definition_forbidden_message
18253 = G_("types may not be defined in an exception-specification");
18255 expr = cp_parser_constant_expression (parser, false, NULL);
18257 /* Restore the saved message. */
18258 parser->type_definition_forbidden_message = saved_message;
18260 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18262 else
18263 expr = boolean_true_node;
18265 return build_noexcept_spec (expr, tf_warning_or_error);
18268 /* If it's not `throw', then there's no exception-specification. */
18269 if (!cp_parser_is_keyword (token, RID_THROW))
18270 return NULL_TREE;
18272 #if 0
18273 /* Enable this once a lot of code has transitioned to noexcept? */
18274 if (cxx_dialect == cxx0x && !in_system_header)
18275 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18276 "deprecated in C++0x; use %<noexcept%> instead");
18277 #endif
18279 /* Consume the `throw'. */
18280 cp_lexer_consume_token (parser->lexer);
18282 /* Look for the `('. */
18283 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18285 /* Peek at the next token. */
18286 token = cp_lexer_peek_token (parser->lexer);
18287 /* If it's not a `)', then there is a type-id-list. */
18288 if (token->type != CPP_CLOSE_PAREN)
18290 /* Types may not be defined in an exception-specification. */
18291 saved_message = parser->type_definition_forbidden_message;
18292 parser->type_definition_forbidden_message
18293 = G_("types may not be defined in an exception-specification");
18294 /* Parse the type-id-list. */
18295 type_id_list = cp_parser_type_id_list (parser);
18296 /* Restore the saved message. */
18297 parser->type_definition_forbidden_message = saved_message;
18299 else
18300 type_id_list = empty_except_spec;
18302 /* Look for the `)'. */
18303 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18305 return type_id_list;
18308 /* Parse an (optional) type-id-list.
18310 type-id-list:
18311 type-id ... [opt]
18312 type-id-list , type-id ... [opt]
18314 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18315 in the order that the types were presented. */
18317 static tree
18318 cp_parser_type_id_list (cp_parser* parser)
18320 tree types = NULL_TREE;
18322 while (true)
18324 cp_token *token;
18325 tree type;
18327 /* Get the next type-id. */
18328 type = cp_parser_type_id (parser);
18329 /* Parse the optional ellipsis. */
18330 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18332 /* Consume the `...'. */
18333 cp_lexer_consume_token (parser->lexer);
18335 /* Turn the type into a pack expansion expression. */
18336 type = make_pack_expansion (type);
18338 /* Add it to the list. */
18339 types = add_exception_specifier (types, type, /*complain=*/1);
18340 /* Peek at the next token. */
18341 token = cp_lexer_peek_token (parser->lexer);
18342 /* If it is not a `,', we are done. */
18343 if (token->type != CPP_COMMA)
18344 break;
18345 /* Consume the `,'. */
18346 cp_lexer_consume_token (parser->lexer);
18349 return nreverse (types);
18352 /* Parse a try-block.
18354 try-block:
18355 try compound-statement handler-seq */
18357 static tree
18358 cp_parser_try_block (cp_parser* parser)
18360 tree try_block;
18362 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18363 try_block = begin_try_block ();
18364 cp_parser_compound_statement (parser, NULL, true);
18365 finish_try_block (try_block);
18366 cp_parser_handler_seq (parser);
18367 finish_handler_sequence (try_block);
18369 return try_block;
18372 /* Parse a function-try-block.
18374 function-try-block:
18375 try ctor-initializer [opt] function-body handler-seq */
18377 static bool
18378 cp_parser_function_try_block (cp_parser* parser)
18380 tree compound_stmt;
18381 tree try_block;
18382 bool ctor_initializer_p;
18384 /* Look for the `try' keyword. */
18385 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18386 return false;
18387 /* Let the rest of the front end know where we are. */
18388 try_block = begin_function_try_block (&compound_stmt);
18389 /* Parse the function-body. */
18390 ctor_initializer_p
18391 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18392 /* We're done with the `try' part. */
18393 finish_function_try_block (try_block);
18394 /* Parse the handlers. */
18395 cp_parser_handler_seq (parser);
18396 /* We're done with the handlers. */
18397 finish_function_handler_sequence (try_block, compound_stmt);
18399 return ctor_initializer_p;
18402 /* Parse a handler-seq.
18404 handler-seq:
18405 handler handler-seq [opt] */
18407 static void
18408 cp_parser_handler_seq (cp_parser* parser)
18410 while (true)
18412 cp_token *token;
18414 /* Parse the handler. */
18415 cp_parser_handler (parser);
18416 /* Peek at the next token. */
18417 token = cp_lexer_peek_token (parser->lexer);
18418 /* If it's not `catch' then there are no more handlers. */
18419 if (!cp_parser_is_keyword (token, RID_CATCH))
18420 break;
18424 /* Parse a handler.
18426 handler:
18427 catch ( exception-declaration ) compound-statement */
18429 static void
18430 cp_parser_handler (cp_parser* parser)
18432 tree handler;
18433 tree declaration;
18435 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18436 handler = begin_handler ();
18437 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18438 declaration = cp_parser_exception_declaration (parser);
18439 finish_handler_parms (declaration, handler);
18440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18441 cp_parser_compound_statement (parser, NULL, false);
18442 finish_handler (handler);
18445 /* Parse an exception-declaration.
18447 exception-declaration:
18448 type-specifier-seq declarator
18449 type-specifier-seq abstract-declarator
18450 type-specifier-seq
18453 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18454 ellipsis variant is used. */
18456 static tree
18457 cp_parser_exception_declaration (cp_parser* parser)
18459 cp_decl_specifier_seq type_specifiers;
18460 cp_declarator *declarator;
18461 const char *saved_message;
18463 /* If it's an ellipsis, it's easy to handle. */
18464 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18466 /* Consume the `...' token. */
18467 cp_lexer_consume_token (parser->lexer);
18468 return NULL_TREE;
18471 /* Types may not be defined in exception-declarations. */
18472 saved_message = parser->type_definition_forbidden_message;
18473 parser->type_definition_forbidden_message
18474 = G_("types may not be defined in exception-declarations");
18476 /* Parse the type-specifier-seq. */
18477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18478 /*is_trailing_return=*/false,
18479 &type_specifiers);
18480 /* If it's a `)', then there is no declarator. */
18481 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18482 declarator = NULL;
18483 else
18484 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18485 /*ctor_dtor_or_conv_p=*/NULL,
18486 /*parenthesized_p=*/NULL,
18487 /*member_p=*/false);
18489 /* Restore the saved message. */
18490 parser->type_definition_forbidden_message = saved_message;
18492 if (!type_specifiers.any_specifiers_p)
18493 return error_mark_node;
18495 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18498 /* Parse a throw-expression.
18500 throw-expression:
18501 throw assignment-expression [opt]
18503 Returns a THROW_EXPR representing the throw-expression. */
18505 static tree
18506 cp_parser_throw_expression (cp_parser* parser)
18508 tree expression;
18509 cp_token* token;
18511 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18512 token = cp_lexer_peek_token (parser->lexer);
18513 /* Figure out whether or not there is an assignment-expression
18514 following the "throw" keyword. */
18515 if (token->type == CPP_COMMA
18516 || token->type == CPP_SEMICOLON
18517 || token->type == CPP_CLOSE_PAREN
18518 || token->type == CPP_CLOSE_SQUARE
18519 || token->type == CPP_CLOSE_BRACE
18520 || token->type == CPP_COLON)
18521 expression = NULL_TREE;
18522 else
18523 expression = cp_parser_assignment_expression (parser,
18524 /*cast_p=*/false, NULL);
18526 return build_throw (expression);
18529 /* GNU Extensions */
18531 /* Parse an (optional) asm-specification.
18533 asm-specification:
18534 asm ( string-literal )
18536 If the asm-specification is present, returns a STRING_CST
18537 corresponding to the string-literal. Otherwise, returns
18538 NULL_TREE. */
18540 static tree
18541 cp_parser_asm_specification_opt (cp_parser* parser)
18543 cp_token *token;
18544 tree asm_specification;
18546 /* Peek at the next token. */
18547 token = cp_lexer_peek_token (parser->lexer);
18548 /* If the next token isn't the `asm' keyword, then there's no
18549 asm-specification. */
18550 if (!cp_parser_is_keyword (token, RID_ASM))
18551 return NULL_TREE;
18553 /* Consume the `asm' token. */
18554 cp_lexer_consume_token (parser->lexer);
18555 /* Look for the `('. */
18556 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18558 /* Look for the string-literal. */
18559 asm_specification = cp_parser_string_literal (parser, false, false);
18561 /* Look for the `)'. */
18562 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18564 return asm_specification;
18567 /* Parse an asm-operand-list.
18569 asm-operand-list:
18570 asm-operand
18571 asm-operand-list , asm-operand
18573 asm-operand:
18574 string-literal ( expression )
18575 [ string-literal ] string-literal ( expression )
18577 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18578 each node is the expression. The TREE_PURPOSE is itself a
18579 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18580 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18581 is a STRING_CST for the string literal before the parenthesis. Returns
18582 ERROR_MARK_NODE if any of the operands are invalid. */
18584 static tree
18585 cp_parser_asm_operand_list (cp_parser* parser)
18587 tree asm_operands = NULL_TREE;
18588 bool invalid_operands = false;
18590 while (true)
18592 tree string_literal;
18593 tree expression;
18594 tree name;
18596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18598 /* Consume the `[' token. */
18599 cp_lexer_consume_token (parser->lexer);
18600 /* Read the operand name. */
18601 name = cp_parser_identifier (parser);
18602 if (name != error_mark_node)
18603 name = build_string (IDENTIFIER_LENGTH (name),
18604 IDENTIFIER_POINTER (name));
18605 /* Look for the closing `]'. */
18606 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18608 else
18609 name = NULL_TREE;
18610 /* Look for the string-literal. */
18611 string_literal = cp_parser_string_literal (parser, false, false);
18613 /* Look for the `('. */
18614 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18615 /* Parse the expression. */
18616 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18617 /* Look for the `)'. */
18618 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18620 if (name == error_mark_node
18621 || string_literal == error_mark_node
18622 || expression == error_mark_node)
18623 invalid_operands = true;
18625 /* Add this operand to the list. */
18626 asm_operands = tree_cons (build_tree_list (name, string_literal),
18627 expression,
18628 asm_operands);
18629 /* If the next token is not a `,', there are no more
18630 operands. */
18631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18632 break;
18633 /* Consume the `,'. */
18634 cp_lexer_consume_token (parser->lexer);
18637 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18640 /* Parse an asm-clobber-list.
18642 asm-clobber-list:
18643 string-literal
18644 asm-clobber-list , string-literal
18646 Returns a TREE_LIST, indicating the clobbers in the order that they
18647 appeared. The TREE_VALUE of each node is a STRING_CST. */
18649 static tree
18650 cp_parser_asm_clobber_list (cp_parser* parser)
18652 tree clobbers = NULL_TREE;
18654 while (true)
18656 tree string_literal;
18658 /* Look for the string literal. */
18659 string_literal = cp_parser_string_literal (parser, false, false);
18660 /* Add it to the list. */
18661 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18662 /* If the next token is not a `,', then the list is
18663 complete. */
18664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18665 break;
18666 /* Consume the `,' token. */
18667 cp_lexer_consume_token (parser->lexer);
18670 return clobbers;
18673 /* Parse an asm-label-list.
18675 asm-label-list:
18676 identifier
18677 asm-label-list , identifier
18679 Returns a TREE_LIST, indicating the labels in the order that they
18680 appeared. The TREE_VALUE of each node is a label. */
18682 static tree
18683 cp_parser_asm_label_list (cp_parser* parser)
18685 tree labels = NULL_TREE;
18687 while (true)
18689 tree identifier, label, name;
18691 /* Look for the identifier. */
18692 identifier = cp_parser_identifier (parser);
18693 if (!error_operand_p (identifier))
18695 label = lookup_label (identifier);
18696 if (TREE_CODE (label) == LABEL_DECL)
18698 TREE_USED (label) = 1;
18699 check_goto (label);
18700 name = build_string (IDENTIFIER_LENGTH (identifier),
18701 IDENTIFIER_POINTER (identifier));
18702 labels = tree_cons (name, label, labels);
18705 /* If the next token is not a `,', then the list is
18706 complete. */
18707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18708 break;
18709 /* Consume the `,' token. */
18710 cp_lexer_consume_token (parser->lexer);
18713 return nreverse (labels);
18716 /* Parse an (optional) series of attributes.
18718 attributes:
18719 attributes attribute
18721 attribute:
18722 __attribute__ (( attribute-list [opt] ))
18724 The return value is as for cp_parser_attribute_list. */
18726 static tree
18727 cp_parser_attributes_opt (cp_parser* parser)
18729 tree attributes = NULL_TREE;
18731 while (true)
18733 cp_token *token;
18734 tree attribute_list;
18736 /* Peek at the next token. */
18737 token = cp_lexer_peek_token (parser->lexer);
18738 /* If it's not `__attribute__', then we're done. */
18739 if (token->keyword != RID_ATTRIBUTE)
18740 break;
18742 /* Consume the `__attribute__' keyword. */
18743 cp_lexer_consume_token (parser->lexer);
18744 /* Look for the two `(' tokens. */
18745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18746 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18748 /* Peek at the next token. */
18749 token = cp_lexer_peek_token (parser->lexer);
18750 if (token->type != CPP_CLOSE_PAREN)
18751 /* Parse the attribute-list. */
18752 attribute_list = cp_parser_attribute_list (parser);
18753 else
18754 /* If the next token is a `)', then there is no attribute
18755 list. */
18756 attribute_list = NULL;
18758 /* Look for the two `)' tokens. */
18759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18762 /* Add these new attributes to the list. */
18763 attributes = chainon (attributes, attribute_list);
18766 return attributes;
18769 /* Parse an attribute-list.
18771 attribute-list:
18772 attribute
18773 attribute-list , attribute
18775 attribute:
18776 identifier
18777 identifier ( identifier )
18778 identifier ( identifier , expression-list )
18779 identifier ( expression-list )
18781 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18782 to an attribute. The TREE_PURPOSE of each node is the identifier
18783 indicating which attribute is in use. The TREE_VALUE represents
18784 the arguments, if any. */
18786 static tree
18787 cp_parser_attribute_list (cp_parser* parser)
18789 tree attribute_list = NULL_TREE;
18790 bool save_translate_strings_p = parser->translate_strings_p;
18792 parser->translate_strings_p = false;
18793 while (true)
18795 cp_token *token;
18796 tree identifier;
18797 tree attribute;
18799 /* Look for the identifier. We also allow keywords here; for
18800 example `__attribute__ ((const))' is legal. */
18801 token = cp_lexer_peek_token (parser->lexer);
18802 if (token->type == CPP_NAME
18803 || token->type == CPP_KEYWORD)
18805 tree arguments = NULL_TREE;
18807 /* Consume the token. */
18808 token = cp_lexer_consume_token (parser->lexer);
18810 /* Save away the identifier that indicates which attribute
18811 this is. */
18812 identifier = (token->type == CPP_KEYWORD)
18813 /* For keywords, use the canonical spelling, not the
18814 parsed identifier. */
18815 ? ridpointers[(int) token->keyword]
18816 : token->u.value;
18818 attribute = build_tree_list (identifier, NULL_TREE);
18820 /* Peek at the next token. */
18821 token = cp_lexer_peek_token (parser->lexer);
18822 /* If it's an `(', then parse the attribute arguments. */
18823 if (token->type == CPP_OPEN_PAREN)
18825 VEC(tree,gc) *vec;
18826 int attr_flag = (attribute_takes_identifier_p (identifier)
18827 ? id_attr : normal_attr);
18828 vec = cp_parser_parenthesized_expression_list
18829 (parser, attr_flag, /*cast_p=*/false,
18830 /*allow_expansion_p=*/false,
18831 /*non_constant_p=*/NULL);
18832 if (vec == NULL)
18833 arguments = error_mark_node;
18834 else
18836 arguments = build_tree_list_vec (vec);
18837 release_tree_vector (vec);
18839 /* Save the arguments away. */
18840 TREE_VALUE (attribute) = arguments;
18843 if (arguments != error_mark_node)
18845 /* Add this attribute to the list. */
18846 TREE_CHAIN (attribute) = attribute_list;
18847 attribute_list = attribute;
18850 token = cp_lexer_peek_token (parser->lexer);
18852 /* Now, look for more attributes. If the next token isn't a
18853 `,', we're done. */
18854 if (token->type != CPP_COMMA)
18855 break;
18857 /* Consume the comma and keep going. */
18858 cp_lexer_consume_token (parser->lexer);
18860 parser->translate_strings_p = save_translate_strings_p;
18862 /* We built up the list in reverse order. */
18863 return nreverse (attribute_list);
18866 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18867 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18868 current value of the PEDANTIC flag, regardless of whether or not
18869 the `__extension__' keyword is present. The caller is responsible
18870 for restoring the value of the PEDANTIC flag. */
18872 static bool
18873 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18875 /* Save the old value of the PEDANTIC flag. */
18876 *saved_pedantic = pedantic;
18878 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18880 /* Consume the `__extension__' token. */
18881 cp_lexer_consume_token (parser->lexer);
18882 /* We're not being pedantic while the `__extension__' keyword is
18883 in effect. */
18884 pedantic = 0;
18886 return true;
18889 return false;
18892 /* Parse a label declaration.
18894 label-declaration:
18895 __label__ label-declarator-seq ;
18897 label-declarator-seq:
18898 identifier , label-declarator-seq
18899 identifier */
18901 static void
18902 cp_parser_label_declaration (cp_parser* parser)
18904 /* Look for the `__label__' keyword. */
18905 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18907 while (true)
18909 tree identifier;
18911 /* Look for an identifier. */
18912 identifier = cp_parser_identifier (parser);
18913 /* If we failed, stop. */
18914 if (identifier == error_mark_node)
18915 break;
18916 /* Declare it as a label. */
18917 finish_label_decl (identifier);
18918 /* If the next token is a `;', stop. */
18919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18920 break;
18921 /* Look for the `,' separating the label declarations. */
18922 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18925 /* Look for the final `;'. */
18926 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18929 /* Support Functions */
18931 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18932 NAME should have one of the representations used for an
18933 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18934 is returned. If PARSER->SCOPE is a dependent type, then a
18935 SCOPE_REF is returned.
18937 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18938 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18939 was formed. Abstractly, such entities should not be passed to this
18940 function, because they do not need to be looked up, but it is
18941 simpler to check for this special case here, rather than at the
18942 call-sites.
18944 In cases not explicitly covered above, this function returns a
18945 DECL, OVERLOAD, or baselink representing the result of the lookup.
18946 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18947 is returned.
18949 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18950 (e.g., "struct") that was used. In that case bindings that do not
18951 refer to types are ignored.
18953 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18954 ignored.
18956 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18957 are ignored.
18959 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18960 types.
18962 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18963 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18964 NULL_TREE otherwise. */
18966 static tree
18967 cp_parser_lookup_name (cp_parser *parser, tree name,
18968 enum tag_types tag_type,
18969 bool is_template,
18970 bool is_namespace,
18971 bool check_dependency,
18972 tree *ambiguous_decls,
18973 location_t name_location)
18975 int flags = 0;
18976 tree decl;
18977 tree object_type = parser->context->object_type;
18979 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18980 flags |= LOOKUP_COMPLAIN;
18982 /* Assume that the lookup will be unambiguous. */
18983 if (ambiguous_decls)
18984 *ambiguous_decls = NULL_TREE;
18986 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18987 no longer valid. Note that if we are parsing tentatively, and
18988 the parse fails, OBJECT_TYPE will be automatically restored. */
18989 parser->context->object_type = NULL_TREE;
18991 if (name == error_mark_node)
18992 return error_mark_node;
18994 /* A template-id has already been resolved; there is no lookup to
18995 do. */
18996 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18997 return name;
18998 if (BASELINK_P (name))
19000 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19001 == TEMPLATE_ID_EXPR);
19002 return name;
19005 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19006 it should already have been checked to make sure that the name
19007 used matches the type being destroyed. */
19008 if (TREE_CODE (name) == BIT_NOT_EXPR)
19010 tree type;
19012 /* Figure out to which type this destructor applies. */
19013 if (parser->scope)
19014 type = parser->scope;
19015 else if (object_type)
19016 type = object_type;
19017 else
19018 type = current_class_type;
19019 /* If that's not a class type, there is no destructor. */
19020 if (!type || !CLASS_TYPE_P (type))
19021 return error_mark_node;
19022 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19023 lazily_declare_fn (sfk_destructor, type);
19024 if (!CLASSTYPE_DESTRUCTORS (type))
19025 return error_mark_node;
19026 /* If it was a class type, return the destructor. */
19027 return CLASSTYPE_DESTRUCTORS (type);
19030 /* By this point, the NAME should be an ordinary identifier. If
19031 the id-expression was a qualified name, the qualifying scope is
19032 stored in PARSER->SCOPE at this point. */
19033 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19035 /* Perform the lookup. */
19036 if (parser->scope)
19038 bool dependent_p;
19040 if (parser->scope == error_mark_node)
19041 return error_mark_node;
19043 /* If the SCOPE is dependent, the lookup must be deferred until
19044 the template is instantiated -- unless we are explicitly
19045 looking up names in uninstantiated templates. Even then, we
19046 cannot look up the name if the scope is not a class type; it
19047 might, for example, be a template type parameter. */
19048 dependent_p = (TYPE_P (parser->scope)
19049 && dependent_scope_p (parser->scope));
19050 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19051 && dependent_p)
19052 /* Defer lookup. */
19053 decl = error_mark_node;
19054 else
19056 tree pushed_scope = NULL_TREE;
19058 /* If PARSER->SCOPE is a dependent type, then it must be a
19059 class type, and we must not be checking dependencies;
19060 otherwise, we would have processed this lookup above. So
19061 that PARSER->SCOPE is not considered a dependent base by
19062 lookup_member, we must enter the scope here. */
19063 if (dependent_p)
19064 pushed_scope = push_scope (parser->scope);
19066 /* If the PARSER->SCOPE is a template specialization, it
19067 may be instantiated during name lookup. In that case,
19068 errors may be issued. Even if we rollback the current
19069 tentative parse, those errors are valid. */
19070 decl = lookup_qualified_name (parser->scope, name,
19071 tag_type != none_type,
19072 /*complain=*/true);
19074 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19075 lookup result and the nested-name-specifier nominates a class C:
19076 * if the name specified after the nested-name-specifier, when
19077 looked up in C, is the injected-class-name of C (Clause 9), or
19078 * if the name specified after the nested-name-specifier is the
19079 same as the identifier or the simple-template-id's template-
19080 name in the last component of the nested-name-specifier,
19081 the name is instead considered to name the constructor of
19082 class C. [ Note: for example, the constructor is not an
19083 acceptable lookup result in an elaborated-type-specifier so
19084 the constructor would not be used in place of the
19085 injected-class-name. --end note ] Such a constructor name
19086 shall be used only in the declarator-id of a declaration that
19087 names a constructor or in a using-declaration. */
19088 if (tag_type == none_type
19089 && DECL_SELF_REFERENCE_P (decl)
19090 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19091 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19092 tag_type != none_type,
19093 /*complain=*/true);
19095 /* If we have a single function from a using decl, pull it out. */
19096 if (TREE_CODE (decl) == OVERLOAD
19097 && !really_overloaded_fn (decl))
19098 decl = OVL_FUNCTION (decl);
19100 if (pushed_scope)
19101 pop_scope (pushed_scope);
19104 /* If the scope is a dependent type and either we deferred lookup or
19105 we did lookup but didn't find the name, rememeber the name. */
19106 if (decl == error_mark_node && TYPE_P (parser->scope)
19107 && dependent_type_p (parser->scope))
19109 if (tag_type)
19111 tree type;
19113 /* The resolution to Core Issue 180 says that `struct
19114 A::B' should be considered a type-name, even if `A'
19115 is dependent. */
19116 type = make_typename_type (parser->scope, name, tag_type,
19117 /*complain=*/tf_error);
19118 decl = TYPE_NAME (type);
19120 else if (is_template
19121 && (cp_parser_next_token_ends_template_argument_p (parser)
19122 || cp_lexer_next_token_is (parser->lexer,
19123 CPP_CLOSE_PAREN)))
19124 decl = make_unbound_class_template (parser->scope,
19125 name, NULL_TREE,
19126 /*complain=*/tf_error);
19127 else
19128 decl = build_qualified_name (/*type=*/NULL_TREE,
19129 parser->scope, name,
19130 is_template);
19132 parser->qualifying_scope = parser->scope;
19133 parser->object_scope = NULL_TREE;
19135 else if (object_type)
19137 tree object_decl = NULL_TREE;
19138 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19139 OBJECT_TYPE is not a class. */
19140 if (CLASS_TYPE_P (object_type))
19141 /* If the OBJECT_TYPE is a template specialization, it may
19142 be instantiated during name lookup. In that case, errors
19143 may be issued. Even if we rollback the current tentative
19144 parse, those errors are valid. */
19145 object_decl = lookup_member (object_type,
19146 name,
19147 /*protect=*/0,
19148 tag_type != none_type);
19149 /* Look it up in the enclosing context, too. */
19150 decl = lookup_name_real (name, tag_type != none_type,
19151 /*nonclass=*/0,
19152 /*block_p=*/true, is_namespace, flags);
19153 parser->object_scope = object_type;
19154 parser->qualifying_scope = NULL_TREE;
19155 if (object_decl)
19156 decl = object_decl;
19158 else
19160 decl = lookup_name_real (name, tag_type != none_type,
19161 /*nonclass=*/0,
19162 /*block_p=*/true, is_namespace, flags);
19163 parser->qualifying_scope = NULL_TREE;
19164 parser->object_scope = NULL_TREE;
19167 /* If the lookup failed, let our caller know. */
19168 if (!decl || decl == error_mark_node)
19169 return error_mark_node;
19171 /* Pull out the template from an injected-class-name (or multiple). */
19172 if (is_template)
19173 decl = maybe_get_template_decl_from_type_decl (decl);
19175 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19176 if (TREE_CODE (decl) == TREE_LIST)
19178 if (ambiguous_decls)
19179 *ambiguous_decls = decl;
19180 /* The error message we have to print is too complicated for
19181 cp_parser_error, so we incorporate its actions directly. */
19182 if (!cp_parser_simulate_error (parser))
19184 error_at (name_location, "reference to %qD is ambiguous",
19185 name);
19186 print_candidates (decl);
19188 return error_mark_node;
19191 gcc_assert (DECL_P (decl)
19192 || TREE_CODE (decl) == OVERLOAD
19193 || TREE_CODE (decl) == SCOPE_REF
19194 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19195 || BASELINK_P (decl));
19197 /* If we have resolved the name of a member declaration, check to
19198 see if the declaration is accessible. When the name resolves to
19199 set of overloaded functions, accessibility is checked when
19200 overload resolution is done.
19202 During an explicit instantiation, access is not checked at all,
19203 as per [temp.explicit]. */
19204 if (DECL_P (decl))
19205 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19207 return decl;
19210 /* Like cp_parser_lookup_name, but for use in the typical case where
19211 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19212 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19214 static tree
19215 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19217 return cp_parser_lookup_name (parser, name,
19218 none_type,
19219 /*is_template=*/false,
19220 /*is_namespace=*/false,
19221 /*check_dependency=*/true,
19222 /*ambiguous_decls=*/NULL,
19223 location);
19226 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19227 the current context, return the TYPE_DECL. If TAG_NAME_P is
19228 true, the DECL indicates the class being defined in a class-head,
19229 or declared in an elaborated-type-specifier.
19231 Otherwise, return DECL. */
19233 static tree
19234 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19236 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19237 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19239 struct A {
19240 template <typename T> struct B;
19243 template <typename T> struct A::B {};
19245 Similarly, in an elaborated-type-specifier:
19247 namespace N { struct X{}; }
19249 struct A {
19250 template <typename T> friend struct N::X;
19253 However, if the DECL refers to a class type, and we are in
19254 the scope of the class, then the name lookup automatically
19255 finds the TYPE_DECL created by build_self_reference rather
19256 than a TEMPLATE_DECL. For example, in:
19258 template <class T> struct S {
19259 S s;
19262 there is no need to handle such case. */
19264 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19265 return DECL_TEMPLATE_RESULT (decl);
19267 return decl;
19270 /* If too many, or too few, template-parameter lists apply to the
19271 declarator, issue an error message. Returns TRUE if all went well,
19272 and FALSE otherwise. */
19274 static bool
19275 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19276 cp_declarator *declarator,
19277 location_t declarator_location)
19279 unsigned num_templates;
19281 /* We haven't seen any classes that involve template parameters yet. */
19282 num_templates = 0;
19284 switch (declarator->kind)
19286 case cdk_id:
19287 if (declarator->u.id.qualifying_scope)
19289 tree scope;
19291 scope = declarator->u.id.qualifying_scope;
19293 while (scope && CLASS_TYPE_P (scope))
19295 /* You're supposed to have one `template <...>'
19296 for every template class, but you don't need one
19297 for a full specialization. For example:
19299 template <class T> struct S{};
19300 template <> struct S<int> { void f(); };
19301 void S<int>::f () {}
19303 is correct; there shouldn't be a `template <>' for
19304 the definition of `S<int>::f'. */
19305 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19306 /* If SCOPE does not have template information of any
19307 kind, then it is not a template, nor is it nested
19308 within a template. */
19309 break;
19310 if (explicit_class_specialization_p (scope))
19311 break;
19312 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19313 ++num_templates;
19315 scope = TYPE_CONTEXT (scope);
19318 else if (TREE_CODE (declarator->u.id.unqualified_name)
19319 == TEMPLATE_ID_EXPR)
19320 /* If the DECLARATOR has the form `X<y>' then it uses one
19321 additional level of template parameters. */
19322 ++num_templates;
19324 return cp_parser_check_template_parameters
19325 (parser, num_templates, declarator_location, declarator);
19328 case cdk_function:
19329 case cdk_array:
19330 case cdk_pointer:
19331 case cdk_reference:
19332 case cdk_ptrmem:
19333 return (cp_parser_check_declarator_template_parameters
19334 (parser, declarator->declarator, declarator_location));
19336 case cdk_error:
19337 return true;
19339 default:
19340 gcc_unreachable ();
19342 return false;
19345 /* NUM_TEMPLATES were used in the current declaration. If that is
19346 invalid, return FALSE and issue an error messages. Otherwise,
19347 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19348 declarator and we can print more accurate diagnostics. */
19350 static bool
19351 cp_parser_check_template_parameters (cp_parser* parser,
19352 unsigned num_templates,
19353 location_t location,
19354 cp_declarator *declarator)
19356 /* If there are the same number of template classes and parameter
19357 lists, that's OK. */
19358 if (parser->num_template_parameter_lists == num_templates)
19359 return true;
19360 /* If there are more, but only one more, then we are referring to a
19361 member template. That's OK too. */
19362 if (parser->num_template_parameter_lists == num_templates + 1)
19363 return true;
19364 /* If there are more template classes than parameter lists, we have
19365 something like:
19367 template <class T> void S<T>::R<T>::f (); */
19368 if (parser->num_template_parameter_lists < num_templates)
19370 if (declarator && !current_function_decl)
19371 error_at (location, "specializing member %<%T::%E%> "
19372 "requires %<template<>%> syntax",
19373 declarator->u.id.qualifying_scope,
19374 declarator->u.id.unqualified_name);
19375 else if (declarator)
19376 error_at (location, "invalid declaration of %<%T::%E%>",
19377 declarator->u.id.qualifying_scope,
19378 declarator->u.id.unqualified_name);
19379 else
19380 error_at (location, "too few template-parameter-lists");
19381 return false;
19383 /* Otherwise, there are too many template parameter lists. We have
19384 something like:
19386 template <class T> template <class U> void S::f(); */
19387 error_at (location, "too many template-parameter-lists");
19388 return false;
19391 /* Parse an optional `::' token indicating that the following name is
19392 from the global namespace. If so, PARSER->SCOPE is set to the
19393 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19394 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19395 Returns the new value of PARSER->SCOPE, if the `::' token is
19396 present, and NULL_TREE otherwise. */
19398 static tree
19399 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19401 cp_token *token;
19403 /* Peek at the next token. */
19404 token = cp_lexer_peek_token (parser->lexer);
19405 /* If we're looking at a `::' token then we're starting from the
19406 global namespace, not our current location. */
19407 if (token->type == CPP_SCOPE)
19409 /* Consume the `::' token. */
19410 cp_lexer_consume_token (parser->lexer);
19411 /* Set the SCOPE so that we know where to start the lookup. */
19412 parser->scope = global_namespace;
19413 parser->qualifying_scope = global_namespace;
19414 parser->object_scope = NULL_TREE;
19416 return parser->scope;
19418 else if (!current_scope_valid_p)
19420 parser->scope = NULL_TREE;
19421 parser->qualifying_scope = NULL_TREE;
19422 parser->object_scope = NULL_TREE;
19425 return NULL_TREE;
19428 /* Returns TRUE if the upcoming token sequence is the start of a
19429 constructor declarator. If FRIEND_P is true, the declarator is
19430 preceded by the `friend' specifier. */
19432 static bool
19433 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19435 bool constructor_p;
19436 tree nested_name_specifier;
19437 cp_token *next_token;
19439 /* The common case is that this is not a constructor declarator, so
19440 try to avoid doing lots of work if at all possible. It's not
19441 valid declare a constructor at function scope. */
19442 if (parser->in_function_body)
19443 return false;
19444 /* And only certain tokens can begin a constructor declarator. */
19445 next_token = cp_lexer_peek_token (parser->lexer);
19446 if (next_token->type != CPP_NAME
19447 && next_token->type != CPP_SCOPE
19448 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19449 && next_token->type != CPP_TEMPLATE_ID)
19450 return false;
19452 /* Parse tentatively; we are going to roll back all of the tokens
19453 consumed here. */
19454 cp_parser_parse_tentatively (parser);
19455 /* Assume that we are looking at a constructor declarator. */
19456 constructor_p = true;
19458 /* Look for the optional `::' operator. */
19459 cp_parser_global_scope_opt (parser,
19460 /*current_scope_valid_p=*/false);
19461 /* Look for the nested-name-specifier. */
19462 nested_name_specifier
19463 = (cp_parser_nested_name_specifier_opt (parser,
19464 /*typename_keyword_p=*/false,
19465 /*check_dependency_p=*/false,
19466 /*type_p=*/false,
19467 /*is_declaration=*/false));
19468 /* Outside of a class-specifier, there must be a
19469 nested-name-specifier. */
19470 if (!nested_name_specifier &&
19471 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19472 || friend_p))
19473 constructor_p = false;
19474 else if (nested_name_specifier == error_mark_node)
19475 constructor_p = false;
19477 /* If we have a class scope, this is easy; DR 147 says that S::S always
19478 names the constructor, and no other qualified name could. */
19479 if (constructor_p && nested_name_specifier
19480 && TYPE_P (nested_name_specifier))
19482 tree id = cp_parser_unqualified_id (parser,
19483 /*template_keyword_p=*/false,
19484 /*check_dependency_p=*/false,
19485 /*declarator_p=*/true,
19486 /*optional_p=*/false);
19487 if (is_overloaded_fn (id))
19488 id = DECL_NAME (get_first_fn (id));
19489 if (!constructor_name_p (id, nested_name_specifier))
19490 constructor_p = false;
19492 /* If we still think that this might be a constructor-declarator,
19493 look for a class-name. */
19494 else if (constructor_p)
19496 /* If we have:
19498 template <typename T> struct S {
19499 S();
19502 we must recognize that the nested `S' names a class. */
19503 tree type_decl;
19504 type_decl = cp_parser_class_name (parser,
19505 /*typename_keyword_p=*/false,
19506 /*template_keyword_p=*/false,
19507 none_type,
19508 /*check_dependency_p=*/false,
19509 /*class_head_p=*/false,
19510 /*is_declaration=*/false);
19511 /* If there was no class-name, then this is not a constructor. */
19512 constructor_p = !cp_parser_error_occurred (parser);
19514 /* If we're still considering a constructor, we have to see a `(',
19515 to begin the parameter-declaration-clause, followed by either a
19516 `)', an `...', or a decl-specifier. We need to check for a
19517 type-specifier to avoid being fooled into thinking that:
19519 S (f) (int);
19521 is a constructor. (It is actually a function named `f' that
19522 takes one parameter (of type `int') and returns a value of type
19523 `S'. */
19524 if (constructor_p
19525 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19526 constructor_p = false;
19528 if (constructor_p
19529 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19530 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19531 /* A parameter declaration begins with a decl-specifier,
19532 which is either the "attribute" keyword, a storage class
19533 specifier, or (usually) a type-specifier. */
19534 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19536 tree type;
19537 tree pushed_scope = NULL_TREE;
19538 unsigned saved_num_template_parameter_lists;
19540 /* Names appearing in the type-specifier should be looked up
19541 in the scope of the class. */
19542 if (current_class_type)
19543 type = NULL_TREE;
19544 else
19546 type = TREE_TYPE (type_decl);
19547 if (TREE_CODE (type) == TYPENAME_TYPE)
19549 type = resolve_typename_type (type,
19550 /*only_current_p=*/false);
19551 if (TREE_CODE (type) == TYPENAME_TYPE)
19553 cp_parser_abort_tentative_parse (parser);
19554 return false;
19557 pushed_scope = push_scope (type);
19560 /* Inside the constructor parameter list, surrounding
19561 template-parameter-lists do not apply. */
19562 saved_num_template_parameter_lists
19563 = parser->num_template_parameter_lists;
19564 parser->num_template_parameter_lists = 0;
19566 /* Look for the type-specifier. */
19567 cp_parser_type_specifier (parser,
19568 CP_PARSER_FLAGS_NONE,
19569 /*decl_specs=*/NULL,
19570 /*is_declarator=*/true,
19571 /*declares_class_or_enum=*/NULL,
19572 /*is_cv_qualifier=*/NULL);
19574 parser->num_template_parameter_lists
19575 = saved_num_template_parameter_lists;
19577 /* Leave the scope of the class. */
19578 if (pushed_scope)
19579 pop_scope (pushed_scope);
19581 constructor_p = !cp_parser_error_occurred (parser);
19585 /* We did not really want to consume any tokens. */
19586 cp_parser_abort_tentative_parse (parser);
19588 return constructor_p;
19591 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19592 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19593 they must be performed once we are in the scope of the function.
19595 Returns the function defined. */
19597 static tree
19598 cp_parser_function_definition_from_specifiers_and_declarator
19599 (cp_parser* parser,
19600 cp_decl_specifier_seq *decl_specifiers,
19601 tree attributes,
19602 const cp_declarator *declarator)
19604 tree fn;
19605 bool success_p;
19607 /* Begin the function-definition. */
19608 success_p = start_function (decl_specifiers, declarator, attributes);
19610 /* The things we're about to see are not directly qualified by any
19611 template headers we've seen thus far. */
19612 reset_specialization ();
19614 /* If there were names looked up in the decl-specifier-seq that we
19615 did not check, check them now. We must wait until we are in the
19616 scope of the function to perform the checks, since the function
19617 might be a friend. */
19618 perform_deferred_access_checks ();
19620 if (!success_p)
19622 /* Skip the entire function. */
19623 cp_parser_skip_to_end_of_block_or_statement (parser);
19624 fn = error_mark_node;
19626 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19628 /* Seen already, skip it. An error message has already been output. */
19629 cp_parser_skip_to_end_of_block_or_statement (parser);
19630 fn = current_function_decl;
19631 current_function_decl = NULL_TREE;
19632 /* If this is a function from a class, pop the nested class. */
19633 if (current_class_name)
19634 pop_nested_class ();
19636 else
19637 fn = cp_parser_function_definition_after_declarator (parser,
19638 /*inline_p=*/false);
19640 return fn;
19643 /* Parse the part of a function-definition that follows the
19644 declarator. INLINE_P is TRUE iff this function is an inline
19645 function defined within a class-specifier.
19647 Returns the function defined. */
19649 static tree
19650 cp_parser_function_definition_after_declarator (cp_parser* parser,
19651 bool inline_p)
19653 tree fn;
19654 bool ctor_initializer_p = false;
19655 bool saved_in_unbraced_linkage_specification_p;
19656 bool saved_in_function_body;
19657 unsigned saved_num_template_parameter_lists;
19658 cp_token *token;
19660 saved_in_function_body = parser->in_function_body;
19661 parser->in_function_body = true;
19662 /* If the next token is `return', then the code may be trying to
19663 make use of the "named return value" extension that G++ used to
19664 support. */
19665 token = cp_lexer_peek_token (parser->lexer);
19666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19668 /* Consume the `return' keyword. */
19669 cp_lexer_consume_token (parser->lexer);
19670 /* Look for the identifier that indicates what value is to be
19671 returned. */
19672 cp_parser_identifier (parser);
19673 /* Issue an error message. */
19674 error_at (token->location,
19675 "named return values are no longer supported");
19676 /* Skip tokens until we reach the start of the function body. */
19677 while (true)
19679 cp_token *token = cp_lexer_peek_token (parser->lexer);
19680 if (token->type == CPP_OPEN_BRACE
19681 || token->type == CPP_EOF
19682 || token->type == CPP_PRAGMA_EOL)
19683 break;
19684 cp_lexer_consume_token (parser->lexer);
19687 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19688 anything declared inside `f'. */
19689 saved_in_unbraced_linkage_specification_p
19690 = parser->in_unbraced_linkage_specification_p;
19691 parser->in_unbraced_linkage_specification_p = false;
19692 /* Inside the function, surrounding template-parameter-lists do not
19693 apply. */
19694 saved_num_template_parameter_lists
19695 = parser->num_template_parameter_lists;
19696 parser->num_template_parameter_lists = 0;
19698 start_lambda_scope (current_function_decl);
19700 /* If the next token is `try', then we are looking at a
19701 function-try-block. */
19702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19703 ctor_initializer_p = cp_parser_function_try_block (parser);
19704 /* A function-try-block includes the function-body, so we only do
19705 this next part if we're not processing a function-try-block. */
19706 else
19707 ctor_initializer_p
19708 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19710 finish_lambda_scope ();
19712 /* Finish the function. */
19713 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19714 (inline_p ? 2 : 0));
19715 /* Generate code for it, if necessary. */
19716 expand_or_defer_fn (fn);
19717 /* Restore the saved values. */
19718 parser->in_unbraced_linkage_specification_p
19719 = saved_in_unbraced_linkage_specification_p;
19720 parser->num_template_parameter_lists
19721 = saved_num_template_parameter_lists;
19722 parser->in_function_body = saved_in_function_body;
19724 return fn;
19727 /* Parse a template-declaration, assuming that the `export' (and
19728 `extern') keywords, if present, has already been scanned. MEMBER_P
19729 is as for cp_parser_template_declaration. */
19731 static void
19732 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19734 tree decl = NULL_TREE;
19735 VEC (deferred_access_check,gc) *checks;
19736 tree parameter_list;
19737 bool friend_p = false;
19738 bool need_lang_pop;
19739 cp_token *token;
19741 /* Look for the `template' keyword. */
19742 token = cp_lexer_peek_token (parser->lexer);
19743 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19744 return;
19746 /* And the `<'. */
19747 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19748 return;
19749 if (at_class_scope_p () && current_function_decl)
19751 /* 14.5.2.2 [temp.mem]
19753 A local class shall not have member templates. */
19754 error_at (token->location,
19755 "invalid declaration of member template in local class");
19756 cp_parser_skip_to_end_of_block_or_statement (parser);
19757 return;
19759 /* [temp]
19761 A template ... shall not have C linkage. */
19762 if (current_lang_name == lang_name_c)
19764 error_at (token->location, "template with C linkage");
19765 /* Give it C++ linkage to avoid confusing other parts of the
19766 front end. */
19767 push_lang_context (lang_name_cplusplus);
19768 need_lang_pop = true;
19770 else
19771 need_lang_pop = false;
19773 /* We cannot perform access checks on the template parameter
19774 declarations until we know what is being declared, just as we
19775 cannot check the decl-specifier list. */
19776 push_deferring_access_checks (dk_deferred);
19778 /* If the next token is `>', then we have an invalid
19779 specialization. Rather than complain about an invalid template
19780 parameter, issue an error message here. */
19781 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19783 cp_parser_error (parser, "invalid explicit specialization");
19784 begin_specialization ();
19785 parameter_list = NULL_TREE;
19787 else
19789 /* Parse the template parameters. */
19790 parameter_list = cp_parser_template_parameter_list (parser);
19791 fixup_template_parms ();
19794 /* Get the deferred access checks from the parameter list. These
19795 will be checked once we know what is being declared, as for a
19796 member template the checks must be performed in the scope of the
19797 class containing the member. */
19798 checks = get_deferred_access_checks ();
19800 /* Look for the `>'. */
19801 cp_parser_skip_to_end_of_template_parameter_list (parser);
19802 /* We just processed one more parameter list. */
19803 ++parser->num_template_parameter_lists;
19804 /* If the next token is `template', there are more template
19805 parameters. */
19806 if (cp_lexer_next_token_is_keyword (parser->lexer,
19807 RID_TEMPLATE))
19808 cp_parser_template_declaration_after_export (parser, member_p);
19809 else
19811 /* There are no access checks when parsing a template, as we do not
19812 know if a specialization will be a friend. */
19813 push_deferring_access_checks (dk_no_check);
19814 token = cp_lexer_peek_token (parser->lexer);
19815 decl = cp_parser_single_declaration (parser,
19816 checks,
19817 member_p,
19818 /*explicit_specialization_p=*/false,
19819 &friend_p);
19820 pop_deferring_access_checks ();
19822 /* If this is a member template declaration, let the front
19823 end know. */
19824 if (member_p && !friend_p && decl)
19826 if (TREE_CODE (decl) == TYPE_DECL)
19827 cp_parser_check_access_in_redeclaration (decl, token->location);
19829 decl = finish_member_template_decl (decl);
19831 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19832 make_friend_class (current_class_type, TREE_TYPE (decl),
19833 /*complain=*/true);
19835 /* We are done with the current parameter list. */
19836 --parser->num_template_parameter_lists;
19838 pop_deferring_access_checks ();
19840 /* Finish up. */
19841 finish_template_decl (parameter_list);
19843 /* Register member declarations. */
19844 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19845 finish_member_declaration (decl);
19846 /* For the erroneous case of a template with C linkage, we pushed an
19847 implicit C++ linkage scope; exit that scope now. */
19848 if (need_lang_pop)
19849 pop_lang_context ();
19850 /* If DECL is a function template, we must return to parse it later.
19851 (Even though there is no definition, there might be default
19852 arguments that need handling.) */
19853 if (member_p && decl
19854 && (TREE_CODE (decl) == FUNCTION_DECL
19855 || DECL_FUNCTION_TEMPLATE_P (decl)))
19856 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19859 /* Perform the deferred access checks from a template-parameter-list.
19860 CHECKS is a TREE_LIST of access checks, as returned by
19861 get_deferred_access_checks. */
19863 static void
19864 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19866 ++processing_template_parmlist;
19867 perform_access_checks (checks);
19868 --processing_template_parmlist;
19871 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19872 `function-definition' sequence. MEMBER_P is true, this declaration
19873 appears in a class scope.
19875 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19876 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19878 static tree
19879 cp_parser_single_declaration (cp_parser* parser,
19880 VEC (deferred_access_check,gc)* checks,
19881 bool member_p,
19882 bool explicit_specialization_p,
19883 bool* friend_p)
19885 int declares_class_or_enum;
19886 tree decl = NULL_TREE;
19887 cp_decl_specifier_seq decl_specifiers;
19888 bool function_definition_p = false;
19889 cp_token *decl_spec_token_start;
19891 /* This function is only used when processing a template
19892 declaration. */
19893 gcc_assert (innermost_scope_kind () == sk_template_parms
19894 || innermost_scope_kind () == sk_template_spec);
19896 /* Defer access checks until we know what is being declared. */
19897 push_deferring_access_checks (dk_deferred);
19899 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19900 alternative. */
19901 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19902 cp_parser_decl_specifier_seq (parser,
19903 CP_PARSER_FLAGS_OPTIONAL,
19904 &decl_specifiers,
19905 &declares_class_or_enum);
19906 if (friend_p)
19907 *friend_p = cp_parser_friend_p (&decl_specifiers);
19909 /* There are no template typedefs. */
19910 if (decl_specifiers.specs[(int) ds_typedef])
19912 error_at (decl_spec_token_start->location,
19913 "template declaration of %<typedef%>");
19914 decl = error_mark_node;
19917 /* Gather up the access checks that occurred the
19918 decl-specifier-seq. */
19919 stop_deferring_access_checks ();
19921 /* Check for the declaration of a template class. */
19922 if (declares_class_or_enum)
19924 if (cp_parser_declares_only_class_p (parser))
19926 decl = shadow_tag (&decl_specifiers);
19928 /* In this case:
19930 struct C {
19931 friend template <typename T> struct A<T>::B;
19934 A<T>::B will be represented by a TYPENAME_TYPE, and
19935 therefore not recognized by shadow_tag. */
19936 if (friend_p && *friend_p
19937 && !decl
19938 && decl_specifiers.type
19939 && TYPE_P (decl_specifiers.type))
19940 decl = decl_specifiers.type;
19942 if (decl && decl != error_mark_node)
19943 decl = TYPE_NAME (decl);
19944 else
19945 decl = error_mark_node;
19947 /* Perform access checks for template parameters. */
19948 cp_parser_perform_template_parameter_access_checks (checks);
19952 /* Complain about missing 'typename' or other invalid type names. */
19953 if (!decl_specifiers.any_type_specifiers_p)
19954 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19956 /* If it's not a template class, try for a template function. If
19957 the next token is a `;', then this declaration does not declare
19958 anything. But, if there were errors in the decl-specifiers, then
19959 the error might well have come from an attempted class-specifier.
19960 In that case, there's no need to warn about a missing declarator. */
19961 if (!decl
19962 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19963 || decl_specifiers.type != error_mark_node))
19965 decl = cp_parser_init_declarator (parser,
19966 &decl_specifiers,
19967 checks,
19968 /*function_definition_allowed_p=*/true,
19969 member_p,
19970 declares_class_or_enum,
19971 &function_definition_p,
19972 NULL);
19974 /* 7.1.1-1 [dcl.stc]
19976 A storage-class-specifier shall not be specified in an explicit
19977 specialization... */
19978 if (decl
19979 && explicit_specialization_p
19980 && decl_specifiers.storage_class != sc_none)
19982 error_at (decl_spec_token_start->location,
19983 "explicit template specialization cannot have a storage class");
19984 decl = error_mark_node;
19988 pop_deferring_access_checks ();
19990 /* Clear any current qualification; whatever comes next is the start
19991 of something new. */
19992 parser->scope = NULL_TREE;
19993 parser->qualifying_scope = NULL_TREE;
19994 parser->object_scope = NULL_TREE;
19995 /* Look for a trailing `;' after the declaration. */
19996 if (!function_definition_p
19997 && (decl == error_mark_node
19998 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19999 cp_parser_skip_to_end_of_block_or_statement (parser);
20001 return decl;
20004 /* Parse a cast-expression that is not the operand of a unary "&". */
20006 static tree
20007 cp_parser_simple_cast_expression (cp_parser *parser)
20009 return cp_parser_cast_expression (parser, /*address_p=*/false,
20010 /*cast_p=*/false, NULL);
20013 /* Parse a functional cast to TYPE. Returns an expression
20014 representing the cast. */
20016 static tree
20017 cp_parser_functional_cast (cp_parser* parser, tree type)
20019 VEC(tree,gc) *vec;
20020 tree expression_list;
20021 tree cast;
20022 bool nonconst_p;
20024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20027 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20028 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20029 if (TREE_CODE (type) == TYPE_DECL)
20030 type = TREE_TYPE (type);
20031 return finish_compound_literal (type, expression_list);
20035 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20036 /*cast_p=*/true,
20037 /*allow_expansion_p=*/true,
20038 /*non_constant_p=*/NULL);
20039 if (vec == NULL)
20040 expression_list = error_mark_node;
20041 else
20043 expression_list = build_tree_list_vec (vec);
20044 release_tree_vector (vec);
20047 cast = build_functional_cast (type, expression_list,
20048 tf_warning_or_error);
20049 /* [expr.const]/1: In an integral constant expression "only type
20050 conversions to integral or enumeration type can be used". */
20051 if (TREE_CODE (type) == TYPE_DECL)
20052 type = TREE_TYPE (type);
20053 if (cast != error_mark_node
20054 && !cast_valid_in_integral_constant_expression_p (type)
20055 && cp_parser_non_integral_constant_expression (parser,
20056 NIC_CONSTRUCTOR))
20057 return error_mark_node;
20058 return cast;
20061 /* Save the tokens that make up the body of a member function defined
20062 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20063 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20064 specifiers applied to the declaration. Returns the FUNCTION_DECL
20065 for the member function. */
20067 static tree
20068 cp_parser_save_member_function_body (cp_parser* parser,
20069 cp_decl_specifier_seq *decl_specifiers,
20070 cp_declarator *declarator,
20071 tree attributes)
20073 cp_token *first;
20074 cp_token *last;
20075 tree fn;
20077 /* Create the FUNCTION_DECL. */
20078 fn = grokmethod (decl_specifiers, declarator, attributes);
20079 /* If something went badly wrong, bail out now. */
20080 if (fn == error_mark_node)
20082 /* If there's a function-body, skip it. */
20083 if (cp_parser_token_starts_function_definition_p
20084 (cp_lexer_peek_token (parser->lexer)))
20085 cp_parser_skip_to_end_of_block_or_statement (parser);
20086 return error_mark_node;
20089 /* Remember it, if there default args to post process. */
20090 cp_parser_save_default_args (parser, fn);
20092 /* Save away the tokens that make up the body of the
20093 function. */
20094 first = parser->lexer->next_token;
20095 /* We can have braced-init-list mem-initializers before the fn body. */
20096 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20098 cp_lexer_consume_token (parser->lexer);
20099 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20100 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20102 /* cache_group will stop after an un-nested { } pair, too. */
20103 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20104 break;
20106 /* variadic mem-inits have ... after the ')'. */
20107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20108 cp_lexer_consume_token (parser->lexer);
20111 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20112 /* Handle function try blocks. */
20113 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20114 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20115 last = parser->lexer->next_token;
20117 /* Save away the inline definition; we will process it when the
20118 class is complete. */
20119 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20120 DECL_PENDING_INLINE_P (fn) = 1;
20122 /* We need to know that this was defined in the class, so that
20123 friend templates are handled correctly. */
20124 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20126 /* Add FN to the queue of functions to be parsed later. */
20127 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20129 return fn;
20132 /* Parse a template-argument-list, as well as the trailing ">" (but
20133 not the opening ">"). See cp_parser_template_argument_list for the
20134 return value. */
20136 static tree
20137 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20139 tree arguments;
20140 tree saved_scope;
20141 tree saved_qualifying_scope;
20142 tree saved_object_scope;
20143 bool saved_greater_than_is_operator_p;
20144 int saved_unevaluated_operand;
20145 int saved_inhibit_evaluation_warnings;
20147 /* [temp.names]
20149 When parsing a template-id, the first non-nested `>' is taken as
20150 the end of the template-argument-list rather than a greater-than
20151 operator. */
20152 saved_greater_than_is_operator_p
20153 = parser->greater_than_is_operator_p;
20154 parser->greater_than_is_operator_p = false;
20155 /* Parsing the argument list may modify SCOPE, so we save it
20156 here. */
20157 saved_scope = parser->scope;
20158 saved_qualifying_scope = parser->qualifying_scope;
20159 saved_object_scope = parser->object_scope;
20160 /* We need to evaluate the template arguments, even though this
20161 template-id may be nested within a "sizeof". */
20162 saved_unevaluated_operand = cp_unevaluated_operand;
20163 cp_unevaluated_operand = 0;
20164 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20165 c_inhibit_evaluation_warnings = 0;
20166 /* Parse the template-argument-list itself. */
20167 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20168 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20169 arguments = NULL_TREE;
20170 else
20171 arguments = cp_parser_template_argument_list (parser);
20172 /* Look for the `>' that ends the template-argument-list. If we find
20173 a '>>' instead, it's probably just a typo. */
20174 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20176 if (cxx_dialect != cxx98)
20178 /* In C++0x, a `>>' in a template argument list or cast
20179 expression is considered to be two separate `>'
20180 tokens. So, change the current token to a `>', but don't
20181 consume it: it will be consumed later when the outer
20182 template argument list (or cast expression) is parsed.
20183 Note that this replacement of `>' for `>>' is necessary
20184 even if we are parsing tentatively: in the tentative
20185 case, after calling
20186 cp_parser_enclosed_template_argument_list we will always
20187 throw away all of the template arguments and the first
20188 closing `>', either because the template argument list
20189 was erroneous or because we are replacing those tokens
20190 with a CPP_TEMPLATE_ID token. The second `>' (which will
20191 not have been thrown away) is needed either to close an
20192 outer template argument list or to complete a new-style
20193 cast. */
20194 cp_token *token = cp_lexer_peek_token (parser->lexer);
20195 token->type = CPP_GREATER;
20197 else if (!saved_greater_than_is_operator_p)
20199 /* If we're in a nested template argument list, the '>>' has
20200 to be a typo for '> >'. We emit the error message, but we
20201 continue parsing and we push a '>' as next token, so that
20202 the argument list will be parsed correctly. Note that the
20203 global source location is still on the token before the
20204 '>>', so we need to say explicitly where we want it. */
20205 cp_token *token = cp_lexer_peek_token (parser->lexer);
20206 error_at (token->location, "%<>>%> should be %<> >%> "
20207 "within a nested template argument list");
20209 token->type = CPP_GREATER;
20211 else
20213 /* If this is not a nested template argument list, the '>>'
20214 is a typo for '>'. Emit an error message and continue.
20215 Same deal about the token location, but here we can get it
20216 right by consuming the '>>' before issuing the diagnostic. */
20217 cp_token *token = cp_lexer_consume_token (parser->lexer);
20218 error_at (token->location,
20219 "spurious %<>>%>, use %<>%> to terminate "
20220 "a template argument list");
20223 else
20224 cp_parser_skip_to_end_of_template_parameter_list (parser);
20225 /* The `>' token might be a greater-than operator again now. */
20226 parser->greater_than_is_operator_p
20227 = saved_greater_than_is_operator_p;
20228 /* Restore the SAVED_SCOPE. */
20229 parser->scope = saved_scope;
20230 parser->qualifying_scope = saved_qualifying_scope;
20231 parser->object_scope = saved_object_scope;
20232 cp_unevaluated_operand = saved_unevaluated_operand;
20233 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20235 return arguments;
20238 /* MEMBER_FUNCTION is a member function, or a friend. If default
20239 arguments, or the body of the function have not yet been parsed,
20240 parse them now. */
20242 static void
20243 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20245 /* If this member is a template, get the underlying
20246 FUNCTION_DECL. */
20247 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20248 member_function = DECL_TEMPLATE_RESULT (member_function);
20250 /* There should not be any class definitions in progress at this
20251 point; the bodies of members are only parsed outside of all class
20252 definitions. */
20253 gcc_assert (parser->num_classes_being_defined == 0);
20254 /* While we're parsing the member functions we might encounter more
20255 classes. We want to handle them right away, but we don't want
20256 them getting mixed up with functions that are currently in the
20257 queue. */
20258 push_unparsed_function_queues (parser);
20260 /* Make sure that any template parameters are in scope. */
20261 maybe_begin_member_template_processing (member_function);
20263 /* If the body of the function has not yet been parsed, parse it
20264 now. */
20265 if (DECL_PENDING_INLINE_P (member_function))
20267 tree function_scope;
20268 cp_token_cache *tokens;
20270 /* The function is no longer pending; we are processing it. */
20271 tokens = DECL_PENDING_INLINE_INFO (member_function);
20272 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20273 DECL_PENDING_INLINE_P (member_function) = 0;
20275 /* If this is a local class, enter the scope of the containing
20276 function. */
20277 function_scope = current_function_decl;
20278 if (function_scope)
20279 push_function_context ();
20281 /* Push the body of the function onto the lexer stack. */
20282 cp_parser_push_lexer_for_tokens (parser, tokens);
20284 /* Let the front end know that we going to be defining this
20285 function. */
20286 start_preparsed_function (member_function, NULL_TREE,
20287 SF_PRE_PARSED | SF_INCLASS_INLINE);
20289 /* Don't do access checking if it is a templated function. */
20290 if (processing_template_decl)
20291 push_deferring_access_checks (dk_no_check);
20293 /* Now, parse the body of the function. */
20294 cp_parser_function_definition_after_declarator (parser,
20295 /*inline_p=*/true);
20297 if (processing_template_decl)
20298 pop_deferring_access_checks ();
20300 /* Leave the scope of the containing function. */
20301 if (function_scope)
20302 pop_function_context ();
20303 cp_parser_pop_lexer (parser);
20306 /* Remove any template parameters from the symbol table. */
20307 maybe_end_member_template_processing ();
20309 /* Restore the queue. */
20310 pop_unparsed_function_queues (parser);
20313 /* If DECL contains any default args, remember it on the unparsed
20314 functions queue. */
20316 static void
20317 cp_parser_save_default_args (cp_parser* parser, tree decl)
20319 tree probe;
20321 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20322 probe;
20323 probe = TREE_CHAIN (probe))
20324 if (TREE_PURPOSE (probe))
20326 cp_default_arg_entry *entry
20327 = VEC_safe_push (cp_default_arg_entry, gc,
20328 unparsed_funs_with_default_args, NULL);
20329 entry->class_type = current_class_type;
20330 entry->decl = decl;
20331 break;
20335 /* FN is a FUNCTION_DECL which may contains a parameter with an
20336 unparsed DEFAULT_ARG. Parse the default args now. This function
20337 assumes that the current scope is the scope in which the default
20338 argument should be processed. */
20340 static void
20341 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20343 bool saved_local_variables_forbidden_p;
20344 tree parm, parmdecl;
20346 /* While we're parsing the default args, we might (due to the
20347 statement expression extension) encounter more classes. We want
20348 to handle them right away, but we don't want them getting mixed
20349 up with default args that are currently in the queue. */
20350 push_unparsed_function_queues (parser);
20352 /* Local variable names (and the `this' keyword) may not appear
20353 in a default argument. */
20354 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20355 parser->local_variables_forbidden_p = true;
20357 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20358 parmdecl = DECL_ARGUMENTS (fn);
20359 parm && parm != void_list_node;
20360 parm = TREE_CHAIN (parm),
20361 parmdecl = DECL_CHAIN (parmdecl))
20363 cp_token_cache *tokens;
20364 tree default_arg = TREE_PURPOSE (parm);
20365 tree parsed_arg;
20366 VEC(tree,gc) *insts;
20367 tree copy;
20368 unsigned ix;
20370 if (!default_arg)
20371 continue;
20373 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20374 /* This can happen for a friend declaration for a function
20375 already declared with default arguments. */
20376 continue;
20378 /* Push the saved tokens for the default argument onto the parser's
20379 lexer stack. */
20380 tokens = DEFARG_TOKENS (default_arg);
20381 cp_parser_push_lexer_for_tokens (parser, tokens);
20383 start_lambda_scope (parmdecl);
20385 /* Parse the assignment-expression. */
20386 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20387 if (parsed_arg == error_mark_node)
20389 cp_parser_pop_lexer (parser);
20390 continue;
20393 if (!processing_template_decl)
20394 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20396 TREE_PURPOSE (parm) = parsed_arg;
20398 /* Update any instantiations we've already created. */
20399 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20400 VEC_iterate (tree, insts, ix, copy); ix++)
20401 TREE_PURPOSE (copy) = parsed_arg;
20403 finish_lambda_scope ();
20405 /* If the token stream has not been completely used up, then
20406 there was extra junk after the end of the default
20407 argument. */
20408 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20409 cp_parser_error (parser, "expected %<,%>");
20411 /* Revert to the main lexer. */
20412 cp_parser_pop_lexer (parser);
20415 /* Make sure no default arg is missing. */
20416 check_default_args (fn);
20418 /* Restore the state of local_variables_forbidden_p. */
20419 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20421 /* Restore the queue. */
20422 pop_unparsed_function_queues (parser);
20425 /* Parse the operand of `sizeof' (or a similar operator). Returns
20426 either a TYPE or an expression, depending on the form of the
20427 input. The KEYWORD indicates which kind of expression we have
20428 encountered. */
20430 static tree
20431 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20433 tree expr = NULL_TREE;
20434 const char *saved_message;
20435 char *tmp;
20436 bool saved_integral_constant_expression_p;
20437 bool saved_non_integral_constant_expression_p;
20438 bool pack_expansion_p = false;
20440 /* Types cannot be defined in a `sizeof' expression. Save away the
20441 old message. */
20442 saved_message = parser->type_definition_forbidden_message;
20443 /* And create the new one. */
20444 tmp = concat ("types may not be defined in %<",
20445 IDENTIFIER_POINTER (ridpointers[keyword]),
20446 "%> expressions", NULL);
20447 parser->type_definition_forbidden_message = tmp;
20449 /* The restrictions on constant-expressions do not apply inside
20450 sizeof expressions. */
20451 saved_integral_constant_expression_p
20452 = parser->integral_constant_expression_p;
20453 saved_non_integral_constant_expression_p
20454 = parser->non_integral_constant_expression_p;
20455 parser->integral_constant_expression_p = false;
20457 /* If it's a `...', then we are computing the length of a parameter
20458 pack. */
20459 if (keyword == RID_SIZEOF
20460 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20462 /* Consume the `...'. */
20463 cp_lexer_consume_token (parser->lexer);
20464 maybe_warn_variadic_templates ();
20466 /* Note that this is an expansion. */
20467 pack_expansion_p = true;
20470 /* Do not actually evaluate the expression. */
20471 ++cp_unevaluated_operand;
20472 ++c_inhibit_evaluation_warnings;
20473 /* If it's a `(', then we might be looking at the type-id
20474 construction. */
20475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20477 tree type;
20478 bool saved_in_type_id_in_expr_p;
20480 /* We can't be sure yet whether we're looking at a type-id or an
20481 expression. */
20482 cp_parser_parse_tentatively (parser);
20483 /* Consume the `('. */
20484 cp_lexer_consume_token (parser->lexer);
20485 /* Parse the type-id. */
20486 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20487 parser->in_type_id_in_expr_p = true;
20488 type = cp_parser_type_id (parser);
20489 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20490 /* Now, look for the trailing `)'. */
20491 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20492 /* If all went well, then we're done. */
20493 if (cp_parser_parse_definitely (parser))
20495 cp_decl_specifier_seq decl_specs;
20497 /* Build a trivial decl-specifier-seq. */
20498 clear_decl_specs (&decl_specs);
20499 decl_specs.type = type;
20501 /* Call grokdeclarator to figure out what type this is. */
20502 expr = grokdeclarator (NULL,
20503 &decl_specs,
20504 TYPENAME,
20505 /*initialized=*/0,
20506 /*attrlist=*/NULL);
20510 /* If the type-id production did not work out, then we must be
20511 looking at the unary-expression production. */
20512 if (!expr)
20513 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20514 /*cast_p=*/false, NULL);
20516 if (pack_expansion_p)
20517 /* Build a pack expansion. */
20518 expr = make_pack_expansion (expr);
20520 /* Go back to evaluating expressions. */
20521 --cp_unevaluated_operand;
20522 --c_inhibit_evaluation_warnings;
20524 /* Free the message we created. */
20525 free (tmp);
20526 /* And restore the old one. */
20527 parser->type_definition_forbidden_message = saved_message;
20528 parser->integral_constant_expression_p
20529 = saved_integral_constant_expression_p;
20530 parser->non_integral_constant_expression_p
20531 = saved_non_integral_constant_expression_p;
20533 return expr;
20536 /* If the current declaration has no declarator, return true. */
20538 static bool
20539 cp_parser_declares_only_class_p (cp_parser *parser)
20541 /* If the next token is a `;' or a `,' then there is no
20542 declarator. */
20543 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20544 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20547 /* Update the DECL_SPECS to reflect the storage class indicated by
20548 KEYWORD. */
20550 static void
20551 cp_parser_set_storage_class (cp_parser *parser,
20552 cp_decl_specifier_seq *decl_specs,
20553 enum rid keyword,
20554 location_t location)
20556 cp_storage_class storage_class;
20558 if (parser->in_unbraced_linkage_specification_p)
20560 error_at (location, "invalid use of %qD in linkage specification",
20561 ridpointers[keyword]);
20562 return;
20564 else if (decl_specs->storage_class != sc_none)
20566 decl_specs->conflicting_specifiers_p = true;
20567 return;
20570 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20571 && decl_specs->specs[(int) ds_thread])
20573 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20574 decl_specs->specs[(int) ds_thread] = 0;
20577 switch (keyword)
20579 case RID_AUTO:
20580 storage_class = sc_auto;
20581 break;
20582 case RID_REGISTER:
20583 storage_class = sc_register;
20584 break;
20585 case RID_STATIC:
20586 storage_class = sc_static;
20587 break;
20588 case RID_EXTERN:
20589 storage_class = sc_extern;
20590 break;
20591 case RID_MUTABLE:
20592 storage_class = sc_mutable;
20593 break;
20594 default:
20595 gcc_unreachable ();
20597 decl_specs->storage_class = storage_class;
20599 /* A storage class specifier cannot be applied alongside a typedef
20600 specifier. If there is a typedef specifier present then set
20601 conflicting_specifiers_p which will trigger an error later
20602 on in grokdeclarator. */
20603 if (decl_specs->specs[(int)ds_typedef])
20604 decl_specs->conflicting_specifiers_p = true;
20607 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20608 is true, the type is a user-defined type; otherwise it is a
20609 built-in type specified by a keyword. */
20611 static void
20612 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20613 tree type_spec,
20614 location_t location,
20615 bool user_defined_p)
20617 decl_specs->any_specifiers_p = true;
20619 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20620 (with, for example, in "typedef int wchar_t;") we remember that
20621 this is what happened. In system headers, we ignore these
20622 declarations so that G++ can work with system headers that are not
20623 C++-safe. */
20624 if (decl_specs->specs[(int) ds_typedef]
20625 && !user_defined_p
20626 && (type_spec == boolean_type_node
20627 || type_spec == char16_type_node
20628 || type_spec == char32_type_node
20629 || type_spec == wchar_type_node)
20630 && (decl_specs->type
20631 || decl_specs->specs[(int) ds_long]
20632 || decl_specs->specs[(int) ds_short]
20633 || decl_specs->specs[(int) ds_unsigned]
20634 || decl_specs->specs[(int) ds_signed]))
20636 decl_specs->redefined_builtin_type = type_spec;
20637 if (!decl_specs->type)
20639 decl_specs->type = type_spec;
20640 decl_specs->user_defined_type_p = false;
20641 decl_specs->type_location = location;
20644 else if (decl_specs->type)
20645 decl_specs->multiple_types_p = true;
20646 else
20648 decl_specs->type = type_spec;
20649 decl_specs->user_defined_type_p = user_defined_p;
20650 decl_specs->redefined_builtin_type = NULL_TREE;
20651 decl_specs->type_location = location;
20655 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20656 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20658 static bool
20659 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20661 return decl_specifiers->specs[(int) ds_friend] != 0;
20664 /* Issue an error message indicating that TOKEN_DESC was expected.
20665 If KEYWORD is true, it indicated this function is called by
20666 cp_parser_require_keword and the required token can only be
20667 a indicated keyword. */
20669 static void
20670 cp_parser_required_error (cp_parser *parser,
20671 required_token token_desc,
20672 bool keyword)
20674 switch (token_desc)
20676 case RT_NEW:
20677 cp_parser_error (parser, "expected %<new%>");
20678 return;
20679 case RT_DELETE:
20680 cp_parser_error (parser, "expected %<delete%>");
20681 return;
20682 case RT_RETURN:
20683 cp_parser_error (parser, "expected %<return%>");
20684 return;
20685 case RT_WHILE:
20686 cp_parser_error (parser, "expected %<while%>");
20687 return;
20688 case RT_EXTERN:
20689 cp_parser_error (parser, "expected %<extern%>");
20690 return;
20691 case RT_STATIC_ASSERT:
20692 cp_parser_error (parser, "expected %<static_assert%>");
20693 return;
20694 case RT_DECLTYPE:
20695 cp_parser_error (parser, "expected %<decltype%>");
20696 return;
20697 case RT_OPERATOR:
20698 cp_parser_error (parser, "expected %<operator%>");
20699 return;
20700 case RT_CLASS:
20701 cp_parser_error (parser, "expected %<class%>");
20702 return;
20703 case RT_TEMPLATE:
20704 cp_parser_error (parser, "expected %<template%>");
20705 return;
20706 case RT_NAMESPACE:
20707 cp_parser_error (parser, "expected %<namespace%>");
20708 return;
20709 case RT_USING:
20710 cp_parser_error (parser, "expected %<using%>");
20711 return;
20712 case RT_ASM:
20713 cp_parser_error (parser, "expected %<asm%>");
20714 return;
20715 case RT_TRY:
20716 cp_parser_error (parser, "expected %<try%>");
20717 return;
20718 case RT_CATCH:
20719 cp_parser_error (parser, "expected %<catch%>");
20720 return;
20721 case RT_THROW:
20722 cp_parser_error (parser, "expected %<throw%>");
20723 return;
20724 case RT_LABEL:
20725 cp_parser_error (parser, "expected %<__label__%>");
20726 return;
20727 case RT_AT_TRY:
20728 cp_parser_error (parser, "expected %<@try%>");
20729 return;
20730 case RT_AT_SYNCHRONIZED:
20731 cp_parser_error (parser, "expected %<@synchronized%>");
20732 return;
20733 case RT_AT_THROW:
20734 cp_parser_error (parser, "expected %<@throw%>");
20735 return;
20736 default:
20737 break;
20739 if (!keyword)
20741 switch (token_desc)
20743 case RT_SEMICOLON:
20744 cp_parser_error (parser, "expected %<;%>");
20745 return;
20746 case RT_OPEN_PAREN:
20747 cp_parser_error (parser, "expected %<(%>");
20748 return;
20749 case RT_CLOSE_BRACE:
20750 cp_parser_error (parser, "expected %<}%>");
20751 return;
20752 case RT_OPEN_BRACE:
20753 cp_parser_error (parser, "expected %<{%>");
20754 return;
20755 case RT_CLOSE_SQUARE:
20756 cp_parser_error (parser, "expected %<]%>");
20757 return;
20758 case RT_OPEN_SQUARE:
20759 cp_parser_error (parser, "expected %<[%>");
20760 return;
20761 case RT_COMMA:
20762 cp_parser_error (parser, "expected %<,%>");
20763 return;
20764 case RT_SCOPE:
20765 cp_parser_error (parser, "expected %<::%>");
20766 return;
20767 case RT_LESS:
20768 cp_parser_error (parser, "expected %<<%>");
20769 return;
20770 case RT_GREATER:
20771 cp_parser_error (parser, "expected %<>%>");
20772 return;
20773 case RT_EQ:
20774 cp_parser_error (parser, "expected %<=%>");
20775 return;
20776 case RT_ELLIPSIS:
20777 cp_parser_error (parser, "expected %<...%>");
20778 return;
20779 case RT_MULT:
20780 cp_parser_error (parser, "expected %<*%>");
20781 return;
20782 case RT_COMPL:
20783 cp_parser_error (parser, "expected %<~%>");
20784 return;
20785 case RT_COLON:
20786 cp_parser_error (parser, "expected %<:%>");
20787 return;
20788 case RT_COLON_SCOPE:
20789 cp_parser_error (parser, "expected %<:%> or %<::%>");
20790 return;
20791 case RT_CLOSE_PAREN:
20792 cp_parser_error (parser, "expected %<)%>");
20793 return;
20794 case RT_COMMA_CLOSE_PAREN:
20795 cp_parser_error (parser, "expected %<,%> or %<)%>");
20796 return;
20797 case RT_PRAGMA_EOL:
20798 cp_parser_error (parser, "expected end of line");
20799 return;
20800 case RT_NAME:
20801 cp_parser_error (parser, "expected identifier");
20802 return;
20803 case RT_SELECT:
20804 cp_parser_error (parser, "expected selection-statement");
20805 return;
20806 case RT_INTERATION:
20807 cp_parser_error (parser, "expected iteration-statement");
20808 return;
20809 case RT_JUMP:
20810 cp_parser_error (parser, "expected jump-statement");
20811 return;
20812 case RT_CLASS_KEY:
20813 cp_parser_error (parser, "expected class-key");
20814 return;
20815 case RT_CLASS_TYPENAME_TEMPLATE:
20816 cp_parser_error (parser,
20817 "expected %<class%>, %<typename%>, or %<template%>");
20818 return;
20819 default:
20820 gcc_unreachable ();
20823 else
20824 gcc_unreachable ();
20829 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20830 issue an error message indicating that TOKEN_DESC was expected.
20832 Returns the token consumed, if the token had the appropriate type.
20833 Otherwise, returns NULL. */
20835 static cp_token *
20836 cp_parser_require (cp_parser* parser,
20837 enum cpp_ttype type,
20838 required_token token_desc)
20840 if (cp_lexer_next_token_is (parser->lexer, type))
20841 return cp_lexer_consume_token (parser->lexer);
20842 else
20844 /* Output the MESSAGE -- unless we're parsing tentatively. */
20845 if (!cp_parser_simulate_error (parser))
20846 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20847 return NULL;
20851 /* An error message is produced if the next token is not '>'.
20852 All further tokens are skipped until the desired token is
20853 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20855 static void
20856 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20858 /* Current level of '< ... >'. */
20859 unsigned level = 0;
20860 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20861 unsigned nesting_depth = 0;
20863 /* Are we ready, yet? If not, issue error message. */
20864 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20865 return;
20867 /* Skip tokens until the desired token is found. */
20868 while (true)
20870 /* Peek at the next token. */
20871 switch (cp_lexer_peek_token (parser->lexer)->type)
20873 case CPP_LESS:
20874 if (!nesting_depth)
20875 ++level;
20876 break;
20878 case CPP_RSHIFT:
20879 if (cxx_dialect == cxx98)
20880 /* C++0x views the `>>' operator as two `>' tokens, but
20881 C++98 does not. */
20882 break;
20883 else if (!nesting_depth && level-- == 0)
20885 /* We've hit a `>>' where the first `>' closes the
20886 template argument list, and the second `>' is
20887 spurious. Just consume the `>>' and stop; we've
20888 already produced at least one error. */
20889 cp_lexer_consume_token (parser->lexer);
20890 return;
20892 /* Fall through for C++0x, so we handle the second `>' in
20893 the `>>'. */
20895 case CPP_GREATER:
20896 if (!nesting_depth && level-- == 0)
20898 /* We've reached the token we want, consume it and stop. */
20899 cp_lexer_consume_token (parser->lexer);
20900 return;
20902 break;
20904 case CPP_OPEN_PAREN:
20905 case CPP_OPEN_SQUARE:
20906 ++nesting_depth;
20907 break;
20909 case CPP_CLOSE_PAREN:
20910 case CPP_CLOSE_SQUARE:
20911 if (nesting_depth-- == 0)
20912 return;
20913 break;
20915 case CPP_EOF:
20916 case CPP_PRAGMA_EOL:
20917 case CPP_SEMICOLON:
20918 case CPP_OPEN_BRACE:
20919 case CPP_CLOSE_BRACE:
20920 /* The '>' was probably forgotten, don't look further. */
20921 return;
20923 default:
20924 break;
20927 /* Consume this token. */
20928 cp_lexer_consume_token (parser->lexer);
20932 /* If the next token is the indicated keyword, consume it. Otherwise,
20933 issue an error message indicating that TOKEN_DESC was expected.
20935 Returns the token consumed, if the token had the appropriate type.
20936 Otherwise, returns NULL. */
20938 static cp_token *
20939 cp_parser_require_keyword (cp_parser* parser,
20940 enum rid keyword,
20941 required_token token_desc)
20943 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20945 if (token && token->keyword != keyword)
20947 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20948 return NULL;
20951 return token;
20954 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20955 function-definition. */
20957 static bool
20958 cp_parser_token_starts_function_definition_p (cp_token* token)
20960 return (/* An ordinary function-body begins with an `{'. */
20961 token->type == CPP_OPEN_BRACE
20962 /* A ctor-initializer begins with a `:'. */
20963 || token->type == CPP_COLON
20964 /* A function-try-block begins with `try'. */
20965 || token->keyword == RID_TRY
20966 /* The named return value extension begins with `return'. */
20967 || token->keyword == RID_RETURN);
20970 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20971 definition. */
20973 static bool
20974 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20976 cp_token *token;
20978 token = cp_lexer_peek_token (parser->lexer);
20979 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20982 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20983 C++0x) ending a template-argument. */
20985 static bool
20986 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20988 cp_token *token;
20990 token = cp_lexer_peek_token (parser->lexer);
20991 return (token->type == CPP_COMMA
20992 || token->type == CPP_GREATER
20993 || token->type == CPP_ELLIPSIS
20994 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20997 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20998 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21000 static bool
21001 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21002 size_t n)
21004 cp_token *token;
21006 token = cp_lexer_peek_nth_token (parser->lexer, n);
21007 if (token->type == CPP_LESS)
21008 return true;
21009 /* Check for the sequence `<::' in the original code. It would be lexed as
21010 `[:', where `[' is a digraph, and there is no whitespace before
21011 `:'. */
21012 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21014 cp_token *token2;
21015 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21016 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21017 return true;
21019 return false;
21022 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21023 or none_type otherwise. */
21025 static enum tag_types
21026 cp_parser_token_is_class_key (cp_token* token)
21028 switch (token->keyword)
21030 case RID_CLASS:
21031 return class_type;
21032 case RID_STRUCT:
21033 return record_type;
21034 case RID_UNION:
21035 return union_type;
21037 default:
21038 return none_type;
21042 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21044 static void
21045 cp_parser_check_class_key (enum tag_types class_key, tree type)
21047 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21048 permerror (input_location, "%qs tag used in naming %q#T",
21049 class_key == union_type ? "union"
21050 : class_key == record_type ? "struct" : "class",
21051 type);
21054 /* Issue an error message if DECL is redeclared with different
21055 access than its original declaration [class.access.spec/3].
21056 This applies to nested classes and nested class templates.
21057 [class.mem/1]. */
21059 static void
21060 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21062 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21063 return;
21065 if ((TREE_PRIVATE (decl)
21066 != (current_access_specifier == access_private_node))
21067 || (TREE_PROTECTED (decl)
21068 != (current_access_specifier == access_protected_node)))
21069 error_at (location, "%qD redeclared with different access", decl);
21072 /* Look for the `template' keyword, as a syntactic disambiguator.
21073 Return TRUE iff it is present, in which case it will be
21074 consumed. */
21076 static bool
21077 cp_parser_optional_template_keyword (cp_parser *parser)
21079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21081 /* The `template' keyword can only be used within templates;
21082 outside templates the parser can always figure out what is a
21083 template and what is not. */
21084 if (!processing_template_decl)
21086 cp_token *token = cp_lexer_peek_token (parser->lexer);
21087 error_at (token->location,
21088 "%<template%> (as a disambiguator) is only allowed "
21089 "within templates");
21090 /* If this part of the token stream is rescanned, the same
21091 error message would be generated. So, we purge the token
21092 from the stream. */
21093 cp_lexer_purge_token (parser->lexer);
21094 return false;
21096 else
21098 /* Consume the `template' keyword. */
21099 cp_lexer_consume_token (parser->lexer);
21100 return true;
21104 return false;
21107 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21108 set PARSER->SCOPE, and perform other related actions. */
21110 static void
21111 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21113 int i;
21114 struct tree_check *check_value;
21115 deferred_access_check *chk;
21116 VEC (deferred_access_check,gc) *checks;
21118 /* Get the stored value. */
21119 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21120 /* Perform any access checks that were deferred. */
21121 checks = check_value->checks;
21122 if (checks)
21124 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21125 perform_or_defer_access_check (chk->binfo,
21126 chk->decl,
21127 chk->diag_decl);
21129 /* Set the scope from the stored value. */
21130 parser->scope = check_value->value;
21131 parser->qualifying_scope = check_value->qualifying_scope;
21132 parser->object_scope = NULL_TREE;
21135 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21136 encounter the end of a block before what we were looking for. */
21138 static bool
21139 cp_parser_cache_group (cp_parser *parser,
21140 enum cpp_ttype end,
21141 unsigned depth)
21143 while (true)
21145 cp_token *token = cp_lexer_peek_token (parser->lexer);
21147 /* Abort a parenthesized expression if we encounter a semicolon. */
21148 if ((end == CPP_CLOSE_PAREN || depth == 0)
21149 && token->type == CPP_SEMICOLON)
21150 return true;
21151 /* If we've reached the end of the file, stop. */
21152 if (token->type == CPP_EOF
21153 || (end != CPP_PRAGMA_EOL
21154 && token->type == CPP_PRAGMA_EOL))
21155 return true;
21156 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21157 /* We've hit the end of an enclosing block, so there's been some
21158 kind of syntax error. */
21159 return true;
21161 /* Consume the token. */
21162 cp_lexer_consume_token (parser->lexer);
21163 /* See if it starts a new group. */
21164 if (token->type == CPP_OPEN_BRACE)
21166 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21167 /* In theory this should probably check end == '}', but
21168 cp_parser_save_member_function_body needs it to exit
21169 after either '}' or ')' when called with ')'. */
21170 if (depth == 0)
21171 return false;
21173 else if (token->type == CPP_OPEN_PAREN)
21175 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21176 if (depth == 0 && end == CPP_CLOSE_PAREN)
21177 return false;
21179 else if (token->type == CPP_PRAGMA)
21180 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21181 else if (token->type == end)
21182 return false;
21186 /* Begin parsing tentatively. We always save tokens while parsing
21187 tentatively so that if the tentative parsing fails we can restore the
21188 tokens. */
21190 static void
21191 cp_parser_parse_tentatively (cp_parser* parser)
21193 /* Enter a new parsing context. */
21194 parser->context = cp_parser_context_new (parser->context);
21195 /* Begin saving tokens. */
21196 cp_lexer_save_tokens (parser->lexer);
21197 /* In order to avoid repetitive access control error messages,
21198 access checks are queued up until we are no longer parsing
21199 tentatively. */
21200 push_deferring_access_checks (dk_deferred);
21203 /* Commit to the currently active tentative parse. */
21205 static void
21206 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21208 cp_parser_context *context;
21209 cp_lexer *lexer;
21211 /* Mark all of the levels as committed. */
21212 lexer = parser->lexer;
21213 for (context = parser->context; context->next; context = context->next)
21215 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21216 break;
21217 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21218 while (!cp_lexer_saving_tokens (lexer))
21219 lexer = lexer->next;
21220 cp_lexer_commit_tokens (lexer);
21224 /* Abort the currently active tentative parse. All consumed tokens
21225 will be rolled back, and no diagnostics will be issued. */
21227 static void
21228 cp_parser_abort_tentative_parse (cp_parser* parser)
21230 cp_parser_simulate_error (parser);
21231 /* Now, pretend that we want to see if the construct was
21232 successfully parsed. */
21233 cp_parser_parse_definitely (parser);
21236 /* Stop parsing tentatively. If a parse error has occurred, restore the
21237 token stream. Otherwise, commit to the tokens we have consumed.
21238 Returns true if no error occurred; false otherwise. */
21240 static bool
21241 cp_parser_parse_definitely (cp_parser* parser)
21243 bool error_occurred;
21244 cp_parser_context *context;
21246 /* Remember whether or not an error occurred, since we are about to
21247 destroy that information. */
21248 error_occurred = cp_parser_error_occurred (parser);
21249 /* Remove the topmost context from the stack. */
21250 context = parser->context;
21251 parser->context = context->next;
21252 /* If no parse errors occurred, commit to the tentative parse. */
21253 if (!error_occurred)
21255 /* Commit to the tokens read tentatively, unless that was
21256 already done. */
21257 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21258 cp_lexer_commit_tokens (parser->lexer);
21260 pop_to_parent_deferring_access_checks ();
21262 /* Otherwise, if errors occurred, roll back our state so that things
21263 are just as they were before we began the tentative parse. */
21264 else
21266 cp_lexer_rollback_tokens (parser->lexer);
21267 pop_deferring_access_checks ();
21269 /* Add the context to the front of the free list. */
21270 context->next = cp_parser_context_free_list;
21271 cp_parser_context_free_list = context;
21273 return !error_occurred;
21276 /* Returns true if we are parsing tentatively and are not committed to
21277 this tentative parse. */
21279 static bool
21280 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21282 return (cp_parser_parsing_tentatively (parser)
21283 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21286 /* Returns nonzero iff an error has occurred during the most recent
21287 tentative parse. */
21289 static bool
21290 cp_parser_error_occurred (cp_parser* parser)
21292 return (cp_parser_parsing_tentatively (parser)
21293 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21296 /* Returns nonzero if GNU extensions are allowed. */
21298 static bool
21299 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21301 return parser->allow_gnu_extensions_p;
21305 /* Objective-C++ Productions */
21307 #include "../objcp/plugin/parser.c"
21310 /* OpenMP 2.5 parsing routines. */
21312 /* Returns name of the next clause.
21313 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21314 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21315 returned and the token is consumed. */
21317 static pragma_omp_clause
21318 cp_parser_omp_clause_name (cp_parser *parser)
21320 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21323 result = PRAGMA_OMP_CLAUSE_IF;
21324 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21325 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21326 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21327 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21328 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21330 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21331 const char *p = IDENTIFIER_POINTER (id);
21333 switch (p[0])
21335 case 'c':
21336 if (!strcmp ("collapse", p))
21337 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21338 else if (!strcmp ("copyin", p))
21339 result = PRAGMA_OMP_CLAUSE_COPYIN;
21340 else if (!strcmp ("copyprivate", p))
21341 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21342 break;
21343 case 'f':
21344 if (!strcmp ("firstprivate", p))
21345 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21346 break;
21347 case 'l':
21348 if (!strcmp ("lastprivate", p))
21349 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21350 break;
21351 case 'n':
21352 if (!strcmp ("nowait", p))
21353 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21354 else if (!strcmp ("num_threads", p))
21355 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21356 break;
21357 case 'o':
21358 if (!strcmp ("ordered", p))
21359 result = PRAGMA_OMP_CLAUSE_ORDERED;
21360 break;
21361 case 'r':
21362 if (!strcmp ("reduction", p))
21363 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21364 break;
21365 case 's':
21366 if (!strcmp ("schedule", p))
21367 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21368 else if (!strcmp ("shared", p))
21369 result = PRAGMA_OMP_CLAUSE_SHARED;
21370 break;
21371 case 'u':
21372 if (!strcmp ("untied", p))
21373 result = PRAGMA_OMP_CLAUSE_UNTIED;
21374 break;
21378 if (result != PRAGMA_OMP_CLAUSE_NONE)
21379 cp_lexer_consume_token (parser->lexer);
21381 return result;
21384 /* Validate that a clause of the given type does not already exist. */
21386 static void
21387 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21388 const char *name, location_t location)
21390 tree c;
21392 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21393 if (OMP_CLAUSE_CODE (c) == code)
21395 error_at (location, "too many %qs clauses", name);
21396 break;
21400 /* OpenMP 2.5:
21401 variable-list:
21402 identifier
21403 variable-list , identifier
21405 In addition, we match a closing parenthesis. An opening parenthesis
21406 will have been consumed by the caller.
21408 If KIND is nonzero, create the appropriate node and install the decl
21409 in OMP_CLAUSE_DECL and add the node to the head of the list.
21411 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21412 return the list created. */
21414 static tree
21415 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21416 tree list)
21418 cp_token *token;
21419 while (1)
21421 tree name, decl;
21423 token = cp_lexer_peek_token (parser->lexer);
21424 name = cp_parser_id_expression (parser, /*template_p=*/false,
21425 /*check_dependency_p=*/true,
21426 /*template_p=*/NULL,
21427 /*declarator_p=*/false,
21428 /*optional_p=*/false);
21429 if (name == error_mark_node)
21430 goto skip_comma;
21432 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21433 if (decl == error_mark_node)
21434 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21435 token->location);
21436 else if (kind != 0)
21438 tree u = build_omp_clause (token->location, kind);
21439 OMP_CLAUSE_DECL (u) = decl;
21440 OMP_CLAUSE_CHAIN (u) = list;
21441 list = u;
21443 else
21444 list = tree_cons (decl, NULL_TREE, list);
21446 get_comma:
21447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21448 break;
21449 cp_lexer_consume_token (parser->lexer);
21452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21454 int ending;
21456 /* Try to resync to an unnested comma. Copied from
21457 cp_parser_parenthesized_expression_list. */
21458 skip_comma:
21459 ending = cp_parser_skip_to_closing_parenthesis (parser,
21460 /*recovering=*/true,
21461 /*or_comma=*/true,
21462 /*consume_paren=*/true);
21463 if (ending < 0)
21464 goto get_comma;
21467 return list;
21470 /* Similarly, but expect leading and trailing parenthesis. This is a very
21471 common case for omp clauses. */
21473 static tree
21474 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21476 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21477 return cp_parser_omp_var_list_no_open (parser, kind, list);
21478 return list;
21481 /* OpenMP 3.0:
21482 collapse ( constant-expression ) */
21484 static tree
21485 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21487 tree c, num;
21488 location_t loc;
21489 HOST_WIDE_INT n;
21491 loc = cp_lexer_peek_token (parser->lexer)->location;
21492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21493 return list;
21495 num = cp_parser_constant_expression (parser, false, NULL);
21497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21499 /*or_comma=*/false,
21500 /*consume_paren=*/true);
21502 if (num == error_mark_node)
21503 return list;
21504 num = fold_non_dependent_expr (num);
21505 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21506 || !host_integerp (num, 0)
21507 || (n = tree_low_cst (num, 0)) <= 0
21508 || (int) n != n)
21510 error_at (loc, "collapse argument needs positive constant integer expression");
21511 return list;
21514 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21515 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21516 OMP_CLAUSE_CHAIN (c) = list;
21517 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21519 return c;
21522 /* OpenMP 2.5:
21523 default ( shared | none ) */
21525 static tree
21526 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21528 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21529 tree c;
21531 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21532 return list;
21533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21536 const char *p = IDENTIFIER_POINTER (id);
21538 switch (p[0])
21540 case 'n':
21541 if (strcmp ("none", p) != 0)
21542 goto invalid_kind;
21543 kind = OMP_CLAUSE_DEFAULT_NONE;
21544 break;
21546 case 's':
21547 if (strcmp ("shared", p) != 0)
21548 goto invalid_kind;
21549 kind = OMP_CLAUSE_DEFAULT_SHARED;
21550 break;
21552 default:
21553 goto invalid_kind;
21556 cp_lexer_consume_token (parser->lexer);
21558 else
21560 invalid_kind:
21561 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21564 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21565 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21566 /*or_comma=*/false,
21567 /*consume_paren=*/true);
21569 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21570 return list;
21572 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21573 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21574 OMP_CLAUSE_CHAIN (c) = list;
21575 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21577 return c;
21580 /* OpenMP 2.5:
21581 if ( expression ) */
21583 static tree
21584 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21586 tree t, c;
21588 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21589 return list;
21591 t = cp_parser_condition (parser);
21593 if (t == error_mark_node
21594 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21595 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21596 /*or_comma=*/false,
21597 /*consume_paren=*/true);
21599 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21601 c = build_omp_clause (location, OMP_CLAUSE_IF);
21602 OMP_CLAUSE_IF_EXPR (c) = t;
21603 OMP_CLAUSE_CHAIN (c) = list;
21605 return c;
21608 /* OpenMP 2.5:
21609 nowait */
21611 static tree
21612 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21613 tree list, location_t location)
21615 tree c;
21617 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21619 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21620 OMP_CLAUSE_CHAIN (c) = list;
21621 return c;
21624 /* OpenMP 2.5:
21625 num_threads ( expression ) */
21627 static tree
21628 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21629 location_t location)
21631 tree t, c;
21633 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21634 return list;
21636 t = cp_parser_expression (parser, false, NULL);
21638 if (t == error_mark_node
21639 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21640 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21641 /*or_comma=*/false,
21642 /*consume_paren=*/true);
21644 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21645 "num_threads", location);
21647 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21648 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21649 OMP_CLAUSE_CHAIN (c) = list;
21651 return c;
21654 /* OpenMP 2.5:
21655 ordered */
21657 static tree
21658 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21659 tree list, location_t location)
21661 tree c;
21663 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21664 "ordered", location);
21666 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21667 OMP_CLAUSE_CHAIN (c) = list;
21668 return c;
21671 /* OpenMP 2.5:
21672 reduction ( reduction-operator : variable-list )
21674 reduction-operator:
21675 One of: + * - & ^ | && || */
21677 static tree
21678 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21680 enum tree_code code;
21681 tree nlist, c;
21683 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21684 return list;
21686 switch (cp_lexer_peek_token (parser->lexer)->type)
21688 case CPP_PLUS:
21689 code = PLUS_EXPR;
21690 break;
21691 case CPP_MULT:
21692 code = MULT_EXPR;
21693 break;
21694 case CPP_MINUS:
21695 code = MINUS_EXPR;
21696 break;
21697 case CPP_AND:
21698 code = BIT_AND_EXPR;
21699 break;
21700 case CPP_XOR:
21701 code = BIT_XOR_EXPR;
21702 break;
21703 case CPP_OR:
21704 code = BIT_IOR_EXPR;
21705 break;
21706 case CPP_AND_AND:
21707 code = TRUTH_ANDIF_EXPR;
21708 break;
21709 case CPP_OR_OR:
21710 code = TRUTH_ORIF_EXPR;
21711 break;
21712 default:
21713 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21714 "%<|%>, %<&&%>, or %<||%>");
21715 resync_fail:
21716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21717 /*or_comma=*/false,
21718 /*consume_paren=*/true);
21719 return list;
21721 cp_lexer_consume_token (parser->lexer);
21723 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21724 goto resync_fail;
21726 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21727 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21728 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21730 return nlist;
21733 /* OpenMP 2.5:
21734 schedule ( schedule-kind )
21735 schedule ( schedule-kind , expression )
21737 schedule-kind:
21738 static | dynamic | guided | runtime | auto */
21740 static tree
21741 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21743 tree c, t;
21745 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21746 return list;
21748 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21753 const char *p = IDENTIFIER_POINTER (id);
21755 switch (p[0])
21757 case 'd':
21758 if (strcmp ("dynamic", p) != 0)
21759 goto invalid_kind;
21760 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21761 break;
21763 case 'g':
21764 if (strcmp ("guided", p) != 0)
21765 goto invalid_kind;
21766 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21767 break;
21769 case 'r':
21770 if (strcmp ("runtime", p) != 0)
21771 goto invalid_kind;
21772 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21773 break;
21775 default:
21776 goto invalid_kind;
21779 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21780 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21781 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21782 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21783 else
21784 goto invalid_kind;
21785 cp_lexer_consume_token (parser->lexer);
21787 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21789 cp_token *token;
21790 cp_lexer_consume_token (parser->lexer);
21792 token = cp_lexer_peek_token (parser->lexer);
21793 t = cp_parser_assignment_expression (parser, false, NULL);
21795 if (t == error_mark_node)
21796 goto resync_fail;
21797 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21798 error_at (token->location, "schedule %<runtime%> does not take "
21799 "a %<chunk_size%> parameter");
21800 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21801 error_at (token->location, "schedule %<auto%> does not take "
21802 "a %<chunk_size%> parameter");
21803 else
21804 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21806 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21807 goto resync_fail;
21809 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
21810 goto resync_fail;
21812 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21813 OMP_CLAUSE_CHAIN (c) = list;
21814 return c;
21816 invalid_kind:
21817 cp_parser_error (parser, "invalid schedule kind");
21818 resync_fail:
21819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21820 /*or_comma=*/false,
21821 /*consume_paren=*/true);
21822 return list;
21825 /* OpenMP 3.0:
21826 untied */
21828 static tree
21829 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21830 tree list, location_t location)
21832 tree c;
21834 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21836 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21837 OMP_CLAUSE_CHAIN (c) = list;
21838 return c;
21841 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21842 is a bitmask in MASK. Return the list of clauses found; the result
21843 of clause default goes in *pdefault. */
21845 static tree
21846 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21847 const char *where, cp_token *pragma_tok)
21849 tree clauses = NULL;
21850 bool first = true;
21851 cp_token *token = NULL;
21853 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21855 pragma_omp_clause c_kind;
21856 const char *c_name;
21857 tree prev = clauses;
21859 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21860 cp_lexer_consume_token (parser->lexer);
21862 token = cp_lexer_peek_token (parser->lexer);
21863 c_kind = cp_parser_omp_clause_name (parser);
21864 first = false;
21866 switch (c_kind)
21868 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21869 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21870 token->location);
21871 c_name = "collapse";
21872 break;
21873 case PRAGMA_OMP_CLAUSE_COPYIN:
21874 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21875 c_name = "copyin";
21876 break;
21877 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21878 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21879 clauses);
21880 c_name = "copyprivate";
21881 break;
21882 case PRAGMA_OMP_CLAUSE_DEFAULT:
21883 clauses = cp_parser_omp_clause_default (parser, clauses,
21884 token->location);
21885 c_name = "default";
21886 break;
21887 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21888 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21889 clauses);
21890 c_name = "firstprivate";
21891 break;
21892 case PRAGMA_OMP_CLAUSE_IF:
21893 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21894 c_name = "if";
21895 break;
21896 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21898 clauses);
21899 c_name = "lastprivate";
21900 break;
21901 case PRAGMA_OMP_CLAUSE_NOWAIT:
21902 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21903 c_name = "nowait";
21904 break;
21905 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21906 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21907 token->location);
21908 c_name = "num_threads";
21909 break;
21910 case PRAGMA_OMP_CLAUSE_ORDERED:
21911 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21912 token->location);
21913 c_name = "ordered";
21914 break;
21915 case PRAGMA_OMP_CLAUSE_PRIVATE:
21916 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21917 clauses);
21918 c_name = "private";
21919 break;
21920 case PRAGMA_OMP_CLAUSE_REDUCTION:
21921 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21922 c_name = "reduction";
21923 break;
21924 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21925 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21926 token->location);
21927 c_name = "schedule";
21928 break;
21929 case PRAGMA_OMP_CLAUSE_SHARED:
21930 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21931 clauses);
21932 c_name = "shared";
21933 break;
21934 case PRAGMA_OMP_CLAUSE_UNTIED:
21935 clauses = cp_parser_omp_clause_untied (parser, clauses,
21936 token->location);
21937 c_name = "nowait";
21938 break;
21939 default:
21940 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21941 goto saw_error;
21944 if (((mask >> c_kind) & 1) == 0)
21946 /* Remove the invalid clause(s) from the list to avoid
21947 confusing the rest of the compiler. */
21948 clauses = prev;
21949 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21952 saw_error:
21953 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21954 return finish_omp_clauses (clauses);
21957 /* OpenMP 2.5:
21958 structured-block:
21959 statement
21961 In practice, we're also interested in adding the statement to an
21962 outer node. So it is convenient if we work around the fact that
21963 cp_parser_statement calls add_stmt. */
21965 static unsigned
21966 cp_parser_begin_omp_structured_block (cp_parser *parser)
21968 unsigned save = parser->in_statement;
21970 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21971 This preserves the "not within loop or switch" style error messages
21972 for nonsense cases like
21973 void foo() {
21974 #pragma omp single
21975 break;
21978 if (parser->in_statement)
21979 parser->in_statement = IN_OMP_BLOCK;
21981 return save;
21984 static void
21985 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21987 parser->in_statement = save;
21990 static tree
21991 cp_parser_omp_structured_block (cp_parser *parser)
21993 tree stmt = begin_omp_structured_block ();
21994 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21996 cp_parser_statement (parser, NULL_TREE, false, NULL);
21998 cp_parser_end_omp_structured_block (parser, save);
21999 return finish_omp_structured_block (stmt);
22002 /* OpenMP 2.5:
22003 # pragma omp atomic new-line
22004 expression-stmt
22006 expression-stmt:
22007 x binop= expr | x++ | ++x | x-- | --x
22008 binop:
22009 +, *, -, /, &, ^, |, <<, >>
22011 where x is an lvalue expression with scalar type. */
22013 static void
22014 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22016 tree lhs, rhs;
22017 enum tree_code code;
22019 cp_parser_require_pragma_eol (parser, pragma_tok);
22021 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22022 /*cast_p=*/false, NULL);
22023 switch (TREE_CODE (lhs))
22025 case ERROR_MARK:
22026 goto saw_error;
22028 case PREINCREMENT_EXPR:
22029 case POSTINCREMENT_EXPR:
22030 lhs = TREE_OPERAND (lhs, 0);
22031 code = PLUS_EXPR;
22032 rhs = integer_one_node;
22033 break;
22035 case PREDECREMENT_EXPR:
22036 case POSTDECREMENT_EXPR:
22037 lhs = TREE_OPERAND (lhs, 0);
22038 code = MINUS_EXPR;
22039 rhs = integer_one_node;
22040 break;
22042 case COMPOUND_EXPR:
22043 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
22044 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
22045 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
22046 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
22047 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
22048 (TREE_OPERAND (lhs, 1), 0), 0)))
22049 == BOOLEAN_TYPE)
22050 /* Undo effects of boolean_increment for post {in,de}crement. */
22051 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
22052 /* FALLTHRU */
22053 case MODIFY_EXPR:
22054 if (TREE_CODE (lhs) == MODIFY_EXPR
22055 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
22057 /* Undo effects of boolean_increment. */
22058 if (integer_onep (TREE_OPERAND (lhs, 1)))
22060 /* This is pre or post increment. */
22061 rhs = TREE_OPERAND (lhs, 1);
22062 lhs = TREE_OPERAND (lhs, 0);
22063 code = NOP_EXPR;
22064 break;
22067 /* FALLTHRU */
22068 default:
22069 switch (cp_lexer_peek_token (parser->lexer)->type)
22071 case CPP_MULT_EQ:
22072 code = MULT_EXPR;
22073 break;
22074 case CPP_DIV_EQ:
22075 code = TRUNC_DIV_EXPR;
22076 break;
22077 case CPP_PLUS_EQ:
22078 code = PLUS_EXPR;
22079 break;
22080 case CPP_MINUS_EQ:
22081 code = MINUS_EXPR;
22082 break;
22083 case CPP_LSHIFT_EQ:
22084 code = LSHIFT_EXPR;
22085 break;
22086 case CPP_RSHIFT_EQ:
22087 code = RSHIFT_EXPR;
22088 break;
22089 case CPP_AND_EQ:
22090 code = BIT_AND_EXPR;
22091 break;
22092 case CPP_OR_EQ:
22093 code = BIT_IOR_EXPR;
22094 break;
22095 case CPP_XOR_EQ:
22096 code = BIT_XOR_EXPR;
22097 break;
22098 default:
22099 cp_parser_error (parser,
22100 "invalid operator for %<#pragma omp atomic%>");
22101 goto saw_error;
22103 cp_lexer_consume_token (parser->lexer);
22105 rhs = cp_parser_expression (parser, false, NULL);
22106 if (rhs == error_mark_node)
22107 goto saw_error;
22108 break;
22110 finish_omp_atomic (code, lhs, rhs);
22111 cp_parser_consume_semicolon_at_end_of_statement (parser);
22112 return;
22114 saw_error:
22115 cp_parser_skip_to_end_of_block_or_statement (parser);
22119 /* OpenMP 2.5:
22120 # pragma omp barrier new-line */
22122 static void
22123 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22125 cp_parser_require_pragma_eol (parser, pragma_tok);
22126 finish_omp_barrier ();
22129 /* OpenMP 2.5:
22130 # pragma omp critical [(name)] new-line
22131 structured-block */
22133 static tree
22134 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22136 tree stmt, name = NULL;
22138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22140 cp_lexer_consume_token (parser->lexer);
22142 name = cp_parser_identifier (parser);
22144 if (name == error_mark_node
22145 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22146 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22147 /*or_comma=*/false,
22148 /*consume_paren=*/true);
22149 if (name == error_mark_node)
22150 name = NULL;
22152 cp_parser_require_pragma_eol (parser, pragma_tok);
22154 stmt = cp_parser_omp_structured_block (parser);
22155 return c_finish_omp_critical (input_location, stmt, name);
22158 /* OpenMP 2.5:
22159 # pragma omp flush flush-vars[opt] new-line
22161 flush-vars:
22162 ( variable-list ) */
22164 static void
22165 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22168 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22169 cp_parser_require_pragma_eol (parser, pragma_tok);
22171 finish_omp_flush ();
22174 /* Helper function, to parse omp for increment expression. */
22176 static tree
22177 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22179 tree cond = cp_parser_binary_expression (parser, false, true,
22180 PREC_NOT_OPERATOR, NULL);
22181 bool overloaded_p;
22183 if (cond == error_mark_node
22184 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22186 cp_parser_skip_to_end_of_statement (parser);
22187 return error_mark_node;
22190 switch (TREE_CODE (cond))
22192 case GT_EXPR:
22193 case GE_EXPR:
22194 case LT_EXPR:
22195 case LE_EXPR:
22196 break;
22197 default:
22198 return error_mark_node;
22201 /* If decl is an iterator, preserve LHS and RHS of the relational
22202 expr until finish_omp_for. */
22203 if (decl
22204 && (type_dependent_expression_p (decl)
22205 || CLASS_TYPE_P (TREE_TYPE (decl))))
22206 return cond;
22208 return build_x_binary_op (TREE_CODE (cond),
22209 TREE_OPERAND (cond, 0), ERROR_MARK,
22210 TREE_OPERAND (cond, 1), ERROR_MARK,
22211 &overloaded_p, tf_warning_or_error);
22214 /* Helper function, to parse omp for increment expression. */
22216 static tree
22217 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22219 cp_token *token = cp_lexer_peek_token (parser->lexer);
22220 enum tree_code op;
22221 tree lhs, rhs;
22222 cp_id_kind idk;
22223 bool decl_first;
22225 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22227 op = (token->type == CPP_PLUS_PLUS
22228 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22229 cp_lexer_consume_token (parser->lexer);
22230 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22231 if (lhs != decl)
22232 return error_mark_node;
22233 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22236 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22237 if (lhs != decl)
22238 return error_mark_node;
22240 token = cp_lexer_peek_token (parser->lexer);
22241 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22243 op = (token->type == CPP_PLUS_PLUS
22244 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22245 cp_lexer_consume_token (parser->lexer);
22246 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22249 op = cp_parser_assignment_operator_opt (parser);
22250 if (op == ERROR_MARK)
22251 return error_mark_node;
22253 if (op != NOP_EXPR)
22255 rhs = cp_parser_assignment_expression (parser, false, NULL);
22256 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22257 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22260 lhs = cp_parser_binary_expression (parser, false, false,
22261 PREC_ADDITIVE_EXPRESSION, NULL);
22262 token = cp_lexer_peek_token (parser->lexer);
22263 decl_first = lhs == decl;
22264 if (decl_first)
22265 lhs = NULL_TREE;
22266 if (token->type != CPP_PLUS
22267 && token->type != CPP_MINUS)
22268 return error_mark_node;
22272 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22273 cp_lexer_consume_token (parser->lexer);
22274 rhs = cp_parser_binary_expression (parser, false, false,
22275 PREC_ADDITIVE_EXPRESSION, NULL);
22276 token = cp_lexer_peek_token (parser->lexer);
22277 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22279 if (lhs == NULL_TREE)
22281 if (op == PLUS_EXPR)
22282 lhs = rhs;
22283 else
22284 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22286 else
22287 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22288 NULL, tf_warning_or_error);
22291 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22293 if (!decl_first)
22295 if (rhs != decl || op == MINUS_EXPR)
22296 return error_mark_node;
22297 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22299 else
22300 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22302 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22305 /* Parse the restricted form of the for statement allowed by OpenMP. */
22307 static tree
22308 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22310 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22311 tree real_decl, initv, condv, incrv, declv;
22312 tree this_pre_body, cl;
22313 location_t loc_first;
22314 bool collapse_err = false;
22315 int i, collapse = 1, nbraces = 0;
22316 VEC(tree,gc) *for_block = make_tree_vector ();
22318 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22319 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22320 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22322 gcc_assert (collapse >= 1);
22324 declv = make_tree_vec (collapse);
22325 initv = make_tree_vec (collapse);
22326 condv = make_tree_vec (collapse);
22327 incrv = make_tree_vec (collapse);
22329 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22331 for (i = 0; i < collapse; i++)
22333 int bracecount = 0;
22334 bool add_private_clause = false;
22335 location_t loc;
22337 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22339 cp_parser_error (parser, "for statement expected");
22340 return NULL;
22342 loc = cp_lexer_consume_token (parser->lexer)->location;
22344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22345 return NULL;
22347 init = decl = real_decl = NULL;
22348 this_pre_body = push_stmt_list ();
22349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22351 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22353 init-expr:
22354 var = lb
22355 integer-type var = lb
22356 random-access-iterator-type var = lb
22357 pointer-type var = lb
22359 cp_decl_specifier_seq type_specifiers;
22361 /* First, try to parse as an initialized declaration. See
22362 cp_parser_condition, from whence the bulk of this is copied. */
22364 cp_parser_parse_tentatively (parser);
22365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22366 /*is_trailing_return=*/false,
22367 &type_specifiers);
22368 if (cp_parser_parse_definitely (parser))
22370 /* If parsing a type specifier seq succeeded, then this
22371 MUST be a initialized declaration. */
22372 tree asm_specification, attributes;
22373 cp_declarator *declarator;
22375 declarator = cp_parser_declarator (parser,
22376 CP_PARSER_DECLARATOR_NAMED,
22377 /*ctor_dtor_or_conv_p=*/NULL,
22378 /*parenthesized_p=*/NULL,
22379 /*member_p=*/false);
22380 attributes = cp_parser_attributes_opt (parser);
22381 asm_specification = cp_parser_asm_specification_opt (parser);
22383 if (declarator == cp_error_declarator)
22384 cp_parser_skip_to_end_of_statement (parser);
22386 else
22388 tree pushed_scope, auto_node;
22390 decl = start_decl (declarator, &type_specifiers,
22391 SD_INITIALIZED, attributes,
22392 /*prefix_attributes=*/NULL_TREE,
22393 &pushed_scope);
22395 auto_node = type_uses_auto (TREE_TYPE (decl));
22396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22398 if (cp_lexer_next_token_is (parser->lexer,
22399 CPP_OPEN_PAREN))
22400 error ("parenthesized initialization is not allowed in "
22401 "OpenMP %<for%> loop");
22402 else
22403 /* Trigger an error. */
22404 cp_parser_require (parser, CPP_EQ, RT_EQ);
22406 init = error_mark_node;
22407 cp_parser_skip_to_end_of_statement (parser);
22409 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22410 || type_dependent_expression_p (decl)
22411 || auto_node)
22413 bool is_direct_init, is_non_constant_init;
22415 init = cp_parser_initializer (parser,
22416 &is_direct_init,
22417 &is_non_constant_init);
22419 if (auto_node && describable_type (init))
22421 TREE_TYPE (decl)
22422 = do_auto_deduction (TREE_TYPE (decl), init,
22423 auto_node);
22425 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22426 && !type_dependent_expression_p (decl))
22427 goto non_class;
22430 cp_finish_decl (decl, init, !is_non_constant_init,
22431 asm_specification,
22432 LOOKUP_ONLYCONVERTING);
22433 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22435 VEC_safe_push (tree, gc, for_block, this_pre_body);
22436 init = NULL_TREE;
22438 else
22439 init = pop_stmt_list (this_pre_body);
22440 this_pre_body = NULL_TREE;
22442 else
22444 /* Consume '='. */
22445 cp_lexer_consume_token (parser->lexer);
22446 init = cp_parser_assignment_expression (parser, false, NULL);
22448 non_class:
22449 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22450 init = error_mark_node;
22451 else
22452 cp_finish_decl (decl, NULL_TREE,
22453 /*init_const_expr_p=*/false,
22454 asm_specification,
22455 LOOKUP_ONLYCONVERTING);
22458 if (pushed_scope)
22459 pop_scope (pushed_scope);
22462 else
22464 cp_id_kind idk;
22465 /* If parsing a type specifier sequence failed, then
22466 this MUST be a simple expression. */
22467 cp_parser_parse_tentatively (parser);
22468 decl = cp_parser_primary_expression (parser, false, false,
22469 false, &idk);
22470 if (!cp_parser_error_occurred (parser)
22471 && decl
22472 && DECL_P (decl)
22473 && CLASS_TYPE_P (TREE_TYPE (decl)))
22475 tree rhs;
22477 cp_parser_parse_definitely (parser);
22478 cp_parser_require (parser, CPP_EQ, RT_EQ);
22479 rhs = cp_parser_assignment_expression (parser, false, NULL);
22480 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22481 rhs,
22482 tf_warning_or_error));
22483 add_private_clause = true;
22485 else
22487 decl = NULL;
22488 cp_parser_abort_tentative_parse (parser);
22489 init = cp_parser_expression (parser, false, NULL);
22490 if (init)
22492 if (TREE_CODE (init) == MODIFY_EXPR
22493 || TREE_CODE (init) == MODOP_EXPR)
22494 real_decl = TREE_OPERAND (init, 0);
22499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22500 if (this_pre_body)
22502 this_pre_body = pop_stmt_list (this_pre_body);
22503 if (pre_body)
22505 tree t = pre_body;
22506 pre_body = push_stmt_list ();
22507 add_stmt (t);
22508 add_stmt (this_pre_body);
22509 pre_body = pop_stmt_list (pre_body);
22511 else
22512 pre_body = this_pre_body;
22515 if (decl)
22516 real_decl = decl;
22517 if (par_clauses != NULL && real_decl != NULL_TREE)
22519 tree *c;
22520 for (c = par_clauses; *c ; )
22521 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22522 && OMP_CLAUSE_DECL (*c) == real_decl)
22524 error_at (loc, "iteration variable %qD"
22525 " should not be firstprivate", real_decl);
22526 *c = OMP_CLAUSE_CHAIN (*c);
22528 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22529 && OMP_CLAUSE_DECL (*c) == real_decl)
22531 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22532 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22533 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22534 OMP_CLAUSE_DECL (l) = real_decl;
22535 OMP_CLAUSE_CHAIN (l) = clauses;
22536 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22537 clauses = l;
22538 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22539 CP_OMP_CLAUSE_INFO (*c) = NULL;
22540 add_private_clause = false;
22542 else
22544 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22545 && OMP_CLAUSE_DECL (*c) == real_decl)
22546 add_private_clause = false;
22547 c = &OMP_CLAUSE_CHAIN (*c);
22551 if (add_private_clause)
22553 tree c;
22554 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22556 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22557 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22558 && OMP_CLAUSE_DECL (c) == decl)
22559 break;
22560 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22561 && OMP_CLAUSE_DECL (c) == decl)
22562 error_at (loc, "iteration variable %qD "
22563 "should not be firstprivate",
22564 decl);
22565 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22566 && OMP_CLAUSE_DECL (c) == decl)
22567 error_at (loc, "iteration variable %qD should not be reduction",
22568 decl);
22570 if (c == NULL)
22572 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22573 OMP_CLAUSE_DECL (c) = decl;
22574 c = finish_omp_clauses (c);
22575 if (c)
22577 OMP_CLAUSE_CHAIN (c) = clauses;
22578 clauses = c;
22583 cond = NULL;
22584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22585 cond = cp_parser_omp_for_cond (parser, decl);
22586 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22588 incr = NULL;
22589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22591 /* If decl is an iterator, preserve the operator on decl
22592 until finish_omp_for. */
22593 if (decl
22594 && (type_dependent_expression_p (decl)
22595 || CLASS_TYPE_P (TREE_TYPE (decl))))
22596 incr = cp_parser_omp_for_incr (parser, decl);
22597 else
22598 incr = cp_parser_expression (parser, false, NULL);
22601 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22603 /*or_comma=*/false,
22604 /*consume_paren=*/true);
22606 TREE_VEC_ELT (declv, i) = decl;
22607 TREE_VEC_ELT (initv, i) = init;
22608 TREE_VEC_ELT (condv, i) = cond;
22609 TREE_VEC_ELT (incrv, i) = incr;
22611 if (i == collapse - 1)
22612 break;
22614 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22615 in between the collapsed for loops to be still considered perfectly
22616 nested. Hopefully the final version clarifies this.
22617 For now handle (multiple) {'s and empty statements. */
22618 cp_parser_parse_tentatively (parser);
22621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22622 break;
22623 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22625 cp_lexer_consume_token (parser->lexer);
22626 bracecount++;
22628 else if (bracecount
22629 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22630 cp_lexer_consume_token (parser->lexer);
22631 else
22633 loc = cp_lexer_peek_token (parser->lexer)->location;
22634 error_at (loc, "not enough collapsed for loops");
22635 collapse_err = true;
22636 cp_parser_abort_tentative_parse (parser);
22637 declv = NULL_TREE;
22638 break;
22641 while (1);
22643 if (declv)
22645 cp_parser_parse_definitely (parser);
22646 nbraces += bracecount;
22650 /* Note that we saved the original contents of this flag when we entered
22651 the structured block, and so we don't need to re-save it here. */
22652 parser->in_statement = IN_OMP_FOR;
22654 /* Note that the grammar doesn't call for a structured block here,
22655 though the loop as a whole is a structured block. */
22656 body = push_stmt_list ();
22657 cp_parser_statement (parser, NULL_TREE, false, NULL);
22658 body = pop_stmt_list (body);
22660 if (declv == NULL_TREE)
22661 ret = NULL_TREE;
22662 else
22663 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22664 pre_body, clauses);
22666 while (nbraces)
22668 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22670 cp_lexer_consume_token (parser->lexer);
22671 nbraces--;
22673 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22674 cp_lexer_consume_token (parser->lexer);
22675 else
22677 if (!collapse_err)
22679 error_at (cp_lexer_peek_token (parser->lexer)->location,
22680 "collapsed loops not perfectly nested");
22682 collapse_err = true;
22683 cp_parser_statement_seq_opt (parser, NULL);
22684 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22685 break;
22689 while (!VEC_empty (tree, for_block))
22690 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
22691 release_tree_vector (for_block);
22693 return ret;
22696 /* OpenMP 2.5:
22697 #pragma omp for for-clause[optseq] new-line
22698 for-loop */
22700 #define OMP_FOR_CLAUSE_MASK \
22701 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22702 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22703 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22704 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22705 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22706 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22707 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22708 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22710 static tree
22711 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22713 tree clauses, sb, ret;
22714 unsigned int save;
22716 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22717 "#pragma omp for", pragma_tok);
22719 sb = begin_omp_structured_block ();
22720 save = cp_parser_begin_omp_structured_block (parser);
22722 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22724 cp_parser_end_omp_structured_block (parser, save);
22725 add_stmt (finish_omp_structured_block (sb));
22727 return ret;
22730 /* OpenMP 2.5:
22731 # pragma omp master new-line
22732 structured-block */
22734 static tree
22735 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22737 cp_parser_require_pragma_eol (parser, pragma_tok);
22738 return c_finish_omp_master (input_location,
22739 cp_parser_omp_structured_block (parser));
22742 /* OpenMP 2.5:
22743 # pragma omp ordered new-line
22744 structured-block */
22746 static tree
22747 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22749 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22750 cp_parser_require_pragma_eol (parser, pragma_tok);
22751 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22754 /* OpenMP 2.5:
22756 section-scope:
22757 { section-sequence }
22759 section-sequence:
22760 section-directive[opt] structured-block
22761 section-sequence section-directive structured-block */
22763 static tree
22764 cp_parser_omp_sections_scope (cp_parser *parser)
22766 tree stmt, substmt;
22767 bool error_suppress = false;
22768 cp_token *tok;
22770 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22771 return NULL_TREE;
22773 stmt = push_stmt_list ();
22775 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22777 unsigned save;
22779 substmt = begin_omp_structured_block ();
22780 save = cp_parser_begin_omp_structured_block (parser);
22782 while (1)
22784 cp_parser_statement (parser, NULL_TREE, false, NULL);
22786 tok = cp_lexer_peek_token (parser->lexer);
22787 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22788 break;
22789 if (tok->type == CPP_CLOSE_BRACE)
22790 break;
22791 if (tok->type == CPP_EOF)
22792 break;
22795 cp_parser_end_omp_structured_block (parser, save);
22796 substmt = finish_omp_structured_block (substmt);
22797 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22798 add_stmt (substmt);
22801 while (1)
22803 tok = cp_lexer_peek_token (parser->lexer);
22804 if (tok->type == CPP_CLOSE_BRACE)
22805 break;
22806 if (tok->type == CPP_EOF)
22807 break;
22809 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22811 cp_lexer_consume_token (parser->lexer);
22812 cp_parser_require_pragma_eol (parser, tok);
22813 error_suppress = false;
22815 else if (!error_suppress)
22817 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22818 error_suppress = true;
22821 substmt = cp_parser_omp_structured_block (parser);
22822 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22823 add_stmt (substmt);
22825 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22827 substmt = pop_stmt_list (stmt);
22829 stmt = make_node (OMP_SECTIONS);
22830 TREE_TYPE (stmt) = void_type_node;
22831 OMP_SECTIONS_BODY (stmt) = substmt;
22833 add_stmt (stmt);
22834 return stmt;
22837 /* OpenMP 2.5:
22838 # pragma omp sections sections-clause[optseq] newline
22839 sections-scope */
22841 #define OMP_SECTIONS_CLAUSE_MASK \
22842 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22843 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22844 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22845 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22846 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22848 static tree
22849 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22851 tree clauses, ret;
22853 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22854 "#pragma omp sections", pragma_tok);
22856 ret = cp_parser_omp_sections_scope (parser);
22857 if (ret)
22858 OMP_SECTIONS_CLAUSES (ret) = clauses;
22860 return ret;
22863 /* OpenMP 2.5:
22864 # pragma parallel parallel-clause new-line
22865 # pragma parallel for parallel-for-clause new-line
22866 # pragma parallel sections parallel-sections-clause new-line */
22868 #define OMP_PARALLEL_CLAUSE_MASK \
22869 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22870 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22871 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22872 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22873 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22874 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22875 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22876 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22878 static tree
22879 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22881 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22882 const char *p_name = "#pragma omp parallel";
22883 tree stmt, clauses, par_clause, ws_clause, block;
22884 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22885 unsigned int save;
22886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22890 cp_lexer_consume_token (parser->lexer);
22891 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22892 p_name = "#pragma omp parallel for";
22893 mask |= OMP_FOR_CLAUSE_MASK;
22894 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22896 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22898 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22899 const char *p = IDENTIFIER_POINTER (id);
22900 if (strcmp (p, "sections") == 0)
22902 cp_lexer_consume_token (parser->lexer);
22903 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22904 p_name = "#pragma omp parallel sections";
22905 mask |= OMP_SECTIONS_CLAUSE_MASK;
22906 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22910 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22911 block = begin_omp_parallel ();
22912 save = cp_parser_begin_omp_structured_block (parser);
22914 switch (p_kind)
22916 case PRAGMA_OMP_PARALLEL:
22917 cp_parser_statement (parser, NULL_TREE, false, NULL);
22918 par_clause = clauses;
22919 break;
22921 case PRAGMA_OMP_PARALLEL_FOR:
22922 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22923 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22924 break;
22926 case PRAGMA_OMP_PARALLEL_SECTIONS:
22927 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22928 stmt = cp_parser_omp_sections_scope (parser);
22929 if (stmt)
22930 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22931 break;
22933 default:
22934 gcc_unreachable ();
22937 cp_parser_end_omp_structured_block (parser, save);
22938 stmt = finish_omp_parallel (par_clause, block);
22939 if (p_kind != PRAGMA_OMP_PARALLEL)
22940 OMP_PARALLEL_COMBINED (stmt) = 1;
22941 return stmt;
22944 /* OpenMP 2.5:
22945 # pragma omp single single-clause[optseq] new-line
22946 structured-block */
22948 #define OMP_SINGLE_CLAUSE_MASK \
22949 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22950 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22951 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22952 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22954 static tree
22955 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22957 tree stmt = make_node (OMP_SINGLE);
22958 TREE_TYPE (stmt) = void_type_node;
22960 OMP_SINGLE_CLAUSES (stmt)
22961 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22962 "#pragma omp single", pragma_tok);
22963 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22965 return add_stmt (stmt);
22968 /* OpenMP 3.0:
22969 # pragma omp task task-clause[optseq] new-line
22970 structured-block */
22972 #define OMP_TASK_CLAUSE_MASK \
22973 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22974 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22975 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22976 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22977 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22978 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22980 static tree
22981 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22983 tree clauses, block;
22984 unsigned int save;
22986 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22987 "#pragma omp task", pragma_tok);
22988 block = begin_omp_task ();
22989 save = cp_parser_begin_omp_structured_block (parser);
22990 cp_parser_statement (parser, NULL_TREE, false, NULL);
22991 cp_parser_end_omp_structured_block (parser, save);
22992 return finish_omp_task (clauses, block);
22995 /* OpenMP 3.0:
22996 # pragma omp taskwait new-line */
22998 static void
22999 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23001 cp_parser_require_pragma_eol (parser, pragma_tok);
23002 finish_omp_taskwait ();
23005 /* OpenMP 2.5:
23006 # pragma omp threadprivate (variable-list) */
23008 static void
23009 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23011 tree vars;
23013 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23014 cp_parser_require_pragma_eol (parser, pragma_tok);
23016 finish_omp_threadprivate (vars);
23019 /* Main entry point to OpenMP statement pragmas. */
23021 static void
23022 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23024 tree stmt;
23026 switch (pragma_tok->pragma_kind)
23028 case PRAGMA_OMP_ATOMIC:
23029 cp_parser_omp_atomic (parser, pragma_tok);
23030 return;
23031 case PRAGMA_OMP_CRITICAL:
23032 stmt = cp_parser_omp_critical (parser, pragma_tok);
23033 break;
23034 case PRAGMA_OMP_FOR:
23035 stmt = cp_parser_omp_for (parser, pragma_tok);
23036 break;
23037 case PRAGMA_OMP_MASTER:
23038 stmt = cp_parser_omp_master (parser, pragma_tok);
23039 break;
23040 case PRAGMA_OMP_ORDERED:
23041 stmt = cp_parser_omp_ordered (parser, pragma_tok);
23042 break;
23043 case PRAGMA_OMP_PARALLEL:
23044 stmt = cp_parser_omp_parallel (parser, pragma_tok);
23045 break;
23046 case PRAGMA_OMP_SECTIONS:
23047 stmt = cp_parser_omp_sections (parser, pragma_tok);
23048 break;
23049 case PRAGMA_OMP_SINGLE:
23050 stmt = cp_parser_omp_single (parser, pragma_tok);
23051 break;
23052 case PRAGMA_OMP_TASK:
23053 stmt = cp_parser_omp_task (parser, pragma_tok);
23054 break;
23055 default:
23056 gcc_unreachable ();
23059 if (stmt)
23060 SET_EXPR_LOCATION (stmt, pragma_tok->location);
23063 /* The parser. */
23065 static GTY (()) cp_parser *the_parser;
23068 /* Special handling for the first token or line in the file. The first
23069 thing in the file might be #pragma GCC pch_preprocess, which loads a
23070 PCH file, which is a GC collection point. So we need to handle this
23071 first pragma without benefit of an existing lexer structure.
23073 Always returns one token to the caller in *FIRST_TOKEN. This is
23074 either the true first token of the file, or the first token after
23075 the initial pragma. */
23077 static void
23078 cp_parser_initial_pragma (cp_token *first_token)
23080 tree name = NULL;
23082 cp_lexer_get_preprocessor_token (NULL, first_token);
23083 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23084 return;
23086 cp_lexer_get_preprocessor_token (NULL, first_token);
23087 if (first_token->type == CPP_STRING)
23089 name = first_token->u.value;
23091 cp_lexer_get_preprocessor_token (NULL, first_token);
23092 if (first_token->type != CPP_PRAGMA_EOL)
23093 error_at (first_token->location,
23094 "junk at end of %<#pragma GCC pch_preprocess%>");
23096 else
23097 error_at (first_token->location, "expected string literal");
23099 /* Skip to the end of the pragma. */
23100 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23101 cp_lexer_get_preprocessor_token (NULL, first_token);
23103 /* Now actually load the PCH file. */
23104 if (name)
23105 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23107 /* Read one more token to return to our caller. We have to do this
23108 after reading the PCH file in, since its pointers have to be
23109 live. */
23110 cp_lexer_get_preprocessor_token (NULL, first_token);
23113 /* Normal parsing of a pragma token. Here we can (and must) use the
23114 regular lexer. */
23116 static bool
23117 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23119 cp_token *pragma_tok;
23120 unsigned int id;
23122 pragma_tok = cp_lexer_consume_token (parser->lexer);
23123 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23124 parser->lexer->in_pragma = true;
23126 id = pragma_tok->pragma_kind;
23127 switch (id)
23129 case PRAGMA_GCC_PCH_PREPROCESS:
23130 error_at (pragma_tok->location,
23131 "%<#pragma GCC pch_preprocess%> must be first");
23132 break;
23134 case PRAGMA_OMP_BARRIER:
23135 switch (context)
23137 case pragma_compound:
23138 cp_parser_omp_barrier (parser, pragma_tok);
23139 return false;
23140 case pragma_stmt:
23141 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23142 "used in compound statements");
23143 break;
23144 default:
23145 goto bad_stmt;
23147 break;
23149 case PRAGMA_OMP_FLUSH:
23150 switch (context)
23152 case pragma_compound:
23153 cp_parser_omp_flush (parser, pragma_tok);
23154 return false;
23155 case pragma_stmt:
23156 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23157 "used in compound statements");
23158 break;
23159 default:
23160 goto bad_stmt;
23162 break;
23164 case PRAGMA_OMP_TASKWAIT:
23165 switch (context)
23167 case pragma_compound:
23168 cp_parser_omp_taskwait (parser, pragma_tok);
23169 return false;
23170 case pragma_stmt:
23171 error_at (pragma_tok->location,
23172 "%<#pragma omp taskwait%> may only be "
23173 "used in compound statements");
23174 break;
23175 default:
23176 goto bad_stmt;
23178 break;
23180 case PRAGMA_OMP_THREADPRIVATE:
23181 cp_parser_omp_threadprivate (parser, pragma_tok);
23182 return false;
23184 case PRAGMA_OMP_ATOMIC:
23185 case PRAGMA_OMP_CRITICAL:
23186 case PRAGMA_OMP_FOR:
23187 case PRAGMA_OMP_MASTER:
23188 case PRAGMA_OMP_ORDERED:
23189 case PRAGMA_OMP_PARALLEL:
23190 case PRAGMA_OMP_SECTIONS:
23191 case PRAGMA_OMP_SINGLE:
23192 case PRAGMA_OMP_TASK:
23193 if (context == pragma_external)
23194 goto bad_stmt;
23195 cp_parser_omp_construct (parser, pragma_tok);
23196 return true;
23198 case PRAGMA_OMP_SECTION:
23199 error_at (pragma_tok->location,
23200 "%<#pragma omp section%> may only be used in "
23201 "%<#pragma omp sections%> construct");
23202 break;
23204 default:
23205 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23206 c_invoke_pragma_handler (id);
23207 break;
23209 bad_stmt:
23210 cp_parser_error (parser, "expected declaration specifiers");
23211 break;
23214 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23215 return false;
23218 /* The interface the pragma parsers have to the lexer. */
23220 enum cpp_ttype
23221 pragma_lex (tree *value)
23223 cp_token *tok;
23224 enum cpp_ttype ret;
23226 tok = cp_lexer_peek_token (the_parser->lexer);
23228 ret = tok->type;
23229 *value = tok->u.value;
23231 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23232 ret = CPP_EOF;
23233 else if (ret == CPP_STRING)
23234 *value = cp_parser_string_literal (the_parser, false, false);
23235 else
23237 cp_lexer_consume_token (the_parser->lexer);
23238 if (ret == CPP_KEYWORD)
23239 ret = CPP_NAME;
23242 return ret;
23246 /* External interface. */
23248 /* Parse one entire translation unit. */
23250 void
23251 c_parse_file (void)
23253 static bool already_called = false;
23255 if (already_called)
23257 sorry ("inter-module optimizations not implemented for C++");
23258 return;
23260 already_called = true;
23262 the_parser = cp_parser_new ();
23263 push_deferring_access_checks (flag_access_control
23264 ? dk_no_deferred : dk_no_check);
23265 cp_parser_translation_unit (the_parser);
23266 the_parser = NULL;
23269 #include "gt-cp-parser.h"