Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / cp / parser.c
blob43e91b034cf236ddfa99a0c3890d03b7cca43758
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 "c-family/c-objc.h"
39 #include "plugin.h"
42 /* The lexer. */
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
52 tree value;
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
60 /* A C++ token. */
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
68 /* Token flags. */
69 unsigned char flags;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
79 location_t location;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
94 static cp_token eof_token =
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
102 it is created. */
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
109 buffer. */
110 size_t buffer_length;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
123 tokens. */
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
129 /* True if we should output debugging information. */
130 bool debugging_p;
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
134 bool in_pragma;
135 } cp_lexer;
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
141 a lexer. */
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
149 } cp_token_cache;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153 NIC_NONE,
154 /* floating-point literal */
155 NIC_FLOAT,
156 /* %<this%> */
157 NIC_THIS,
158 /* %<__FUNCTION__%> */
159 NIC_FUNC_NAME,
160 /* %<__PRETTY_FUNCTION__%> */
161 NIC_PRETTY_FUNC,
162 /* %<__func__%> */
163 NIC_C99_FUNC,
164 /* "%<va_arg%> */
165 NIC_VA_ARG,
166 /* a cast */
167 NIC_CAST,
168 /* %<typeid%> operator */
169 NIC_TYPEID,
170 /* non-constant compound literals */
171 NIC_NCC,
172 /* a function call */
173 NIC_FUNC_CALL,
174 /* an increment */
175 NIC_INC,
176 /* an decrement */
177 NIC_DEC,
178 /* an array reference */
179 NIC_ARRAY_REF,
180 /* %<->%> */
181 NIC_ARROW,
182 /* %<.%> */
183 NIC_POINT,
184 /* the address of a label */
185 NIC_ADDR_LABEL,
186 /* %<*%> */
187 NIC_STAR,
188 /* %<&%> */
189 NIC_ADDR,
190 /* %<++%> */
191 NIC_PREINCREMENT,
192 /* %<--%> */
193 NIC_PREDECREMENT,
194 /* %<new%> */
195 NIC_NEW,
196 /* %<delete%> */
197 NIC_DEL,
198 /* calls to overloaded operators */
199 NIC_OVERLOADED,
200 /* an assignment */
201 NIC_ASSIGNMENT,
202 /* a comma operator */
203 NIC_COMMA,
204 /* a call to a constructor */
205 NIC_CONSTRUCTOR
206 } non_integral_constant;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210 /* NULL */
211 NLE_NULL,
212 /* is not a type */
213 NLE_TYPE,
214 /* is not a class or namespace */
215 NLE_CXX98,
216 /* is not a class, namespace, or enumeration */
217 NLE_NOT_CXX98
218 } name_lookup_error;
220 /* The various kinds of required token */
221 typedef enum required_token {
222 RT_NONE,
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
229 RT_COMMA, /* ',' */
230 RT_SCOPE, /* '::' */
231 RT_LESS, /* '<' */
232 RT_GREATER, /* '>' */
233 RT_EQ, /* '=' */
234 RT_ELLIPSIS, /* '...' */
235 RT_MULT, /* '*' */
236 RT_COMPL, /* '~' */
237 RT_COLON, /* ':' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
244 /* The type is CPP_KEYWORD */
245 RT_NEW, /* new */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
257 RT_ASM, /* asm */
258 RT_TRY, /* try */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
273 /* Prototypes. */
275 static cp_lexer *cp_lexer_new_main
276 (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280 (cp_lexer *);
281 static int cp_lexer_saving_tokens
282 (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284 (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290 (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300 (cp_lexer *);
301 static void cp_lexer_purge_token
302 (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306 (cp_lexer *);
307 static void cp_lexer_commit_tokens
308 (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310 (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315 (cp_lexer *);
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
333 static void cp_parser_initial_pragma
334 (cp_token *);
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
364 /* Variables. */
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376 preprocessor. */
378 static cp_lexer *
379 cp_lexer_new_main (void)
381 cp_token first_token;
382 cp_lexer *lexer;
383 cp_token *pos;
384 size_t alloc;
385 size_t space;
386 cp_token *buffer;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
409 /* Put the first token in the buffer. */
410 space = alloc;
411 pos = buffer;
412 *pos = first_token;
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
417 pos++;
418 if (!--space)
420 space = alloc;
421 alloc *= 2;
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
425 cp_lexer_get_preprocessor_token (lexer, pos);
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
434 done_lexing = true;
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
437 return lexer;
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
462 #endif
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
465 return lexer;
468 /* Frees all resources associated with LEXER. */
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
473 if (lexer->buffer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
476 ggc_free (lexer);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
502 return pos;
505 static inline void
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
508 lexer->next_token = cp_lexer_token_at (lexer, pos);
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
514 if (lexer->next_token == &eof_token)
515 return lexer->last_token - 1;
516 else
517 return cp_lexer_token_position (lexer, true);
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
523 cp_token_position tp = cp_lexer_previous_token_position (lexer);
525 return cp_lexer_token_at (lexer, tp);
528 /* nonzero if we are presently saving tokens. */
530 static inline int
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
533 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
536 /* Store the next token from the preprocessor in *TOKEN. Return true
537 if we reach EOF. If LEXER is NULL, assume we are handling an
538 initial #pragma pch_preprocess, and thus want the lexer to return
539 processed strings. */
541 static void
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
544 static int is_extern_c = 0;
546 /* Get a new token from the preprocessor. */
547 token->type
548 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550 token->keyword = RID_MAX;
551 token->pragma_kind = PRAGMA_NONE;
553 /* On some systems, some header files are surrounded by an
554 implicit extern "C" block. Set a flag in the token if it
555 comes from such a header. */
556 is_extern_c += pending_lang_change;
557 pending_lang_change = 0;
558 token->implicit_extern_c = is_extern_c > 0;
560 /* Check to see if this token is a keyword. */
561 if (token->type == CPP_NAME)
563 if (C_IS_RESERVED_WORD (token->u.value))
565 /* Mark this token as a keyword. */
566 token->type = CPP_KEYWORD;
567 /* Record which keyword. */
568 token->keyword = C_RID_CODE (token->u.value);
570 else
572 if (warn_cxx0x_compat
573 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
576 /* Warn about the C++0x keyword (but still treat it as
577 an identifier). */
578 warning (OPT_Wc__0x_compat,
579 "identifier %qE will become a keyword in C++0x",
580 token->u.value);
582 /* Clear out the C_RID_CODE so we don't warn about this
583 particular identifier-turned-keyword again. */
584 C_SET_RID_CODE (token->u.value, RID_MAX);
587 token->ambiguous_p = false;
588 token->keyword = RID_MAX;
591 else if (token->type == CPP_AT_NAME)
593 /* This only happens in Objective-C++; it must be a keyword. */
594 token->type = CPP_KEYWORD;
595 switch (C_RID_CODE (token->u.value))
597 /* Replace 'class' with '@class', 'private' with '@private',
598 etc. This prevents confusion with the C++ keyword
599 'class', and makes the tokens consistent with other
600 Objective-C 'AT' keywords. For example '@class' is
601 reported as RID_AT_CLASS which is consistent with
602 '@synchronized', which is reported as
603 RID_AT_SYNCHRONIZED.
605 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
606 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
607 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
609 case RID_THROW: token->keyword = RID_AT_THROW; break;
610 case RID_TRY: token->keyword = RID_AT_TRY; break;
611 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
612 default: token->keyword = C_RID_CODE (token->u.value);
615 else if (token->type == CPP_PRAGMA)
617 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
618 token->pragma_kind = ((enum pragma_kind)
619 TREE_INT_CST_LOW (token->u.value));
620 token->u.value = NULL_TREE;
624 /* Update the globals input_location and the input file stack from TOKEN. */
625 static inline void
626 cp_lexer_set_source_position_from_token (cp_token *token)
628 if (token->type != CPP_EOF)
630 input_location = token->location;
634 /* Return a pointer to the next token in the token stream, but do not
635 consume it. */
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
640 if (cp_lexer_debugging_p (lexer))
642 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644 putc ('\n', cp_lexer_debug_stream);
646 return lexer->next_token;
649 /* Return true if the next token has the indicated TYPE. */
651 static inline bool
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
654 return cp_lexer_peek_token (lexer)->type == type;
657 /* Return true if the next token does not have the indicated TYPE. */
659 static inline bool
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
662 return !cp_lexer_next_token_is (lexer, type);
665 /* Return true if the next token is the indicated KEYWORD. */
667 static inline bool
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
670 return cp_lexer_peek_token (lexer)->keyword == keyword;
673 /* Return true if the next token is not the indicated KEYWORD. */
675 static inline bool
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
678 return cp_lexer_peek_token (lexer)->keyword != keyword;
681 /* Return true if the next token is a keyword for a decl-specifier. */
683 static bool
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
686 cp_token *token;
688 token = cp_lexer_peek_token (lexer);
689 switch (token->keyword)
691 /* auto specifier: storage-class-specifier in C++,
692 simple-type-specifier in C++0x. */
693 case RID_AUTO:
694 /* Storage classes. */
695 case RID_REGISTER:
696 case RID_STATIC:
697 case RID_EXTERN:
698 case RID_MUTABLE:
699 case RID_THREAD:
700 /* Elaborated type specifiers. */
701 case RID_ENUM:
702 case RID_CLASS:
703 case RID_STRUCT:
704 case RID_UNION:
705 case RID_TYPENAME:
706 /* Simple type specifiers. */
707 case RID_CHAR:
708 case RID_CHAR16:
709 case RID_CHAR32:
710 case RID_WCHAR:
711 case RID_BOOL:
712 case RID_SHORT:
713 case RID_INT:
714 case RID_LONG:
715 case RID_INT128:
716 case RID_SIGNED:
717 case RID_UNSIGNED:
718 case RID_FLOAT:
719 case RID_DOUBLE:
720 case RID_VOID:
721 /* GNU extensions. */
722 case RID_ATTRIBUTE:
723 case RID_TYPEOF:
724 /* C++0x extensions. */
725 case RID_DECLTYPE:
726 return true;
728 default:
729 return false;
733 /* Return a pointer to the Nth token in the token stream. If N is 1,
734 then this is precisely equivalent to cp_lexer_peek_token (except
735 that it is not inline). One would like to disallow that case, but
736 there is one case (cp_parser_nth_token_starts_template_id) where
737 the caller passes a variable for N and it might be 1. */
739 static cp_token *
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
742 cp_token *token;
744 /* N is 1-based, not zero-based. */
745 gcc_assert (n > 0);
747 if (cp_lexer_debugging_p (lexer))
748 fprintf (cp_lexer_debug_stream,
749 "cp_lexer: peeking ahead %ld at token: ", (long)n);
751 --n;
752 token = lexer->next_token;
753 gcc_assert (!n || token != &eof_token);
754 while (n != 0)
756 ++token;
757 if (token == lexer->last_token)
759 token = &eof_token;
760 break;
763 if (token->type != CPP_PURGED)
764 --n;
767 if (cp_lexer_debugging_p (lexer))
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 putc ('\n', cp_lexer_debug_stream);
773 return token;
776 /* Return the next token, and advance the lexer's next_token pointer
777 to point to the next non-purged token. */
779 static cp_token *
780 cp_lexer_consume_token (cp_lexer* lexer)
782 cp_token *token = lexer->next_token;
784 gcc_assert (token != &eof_token);
785 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
789 lexer->next_token++;
790 if (lexer->next_token == lexer->last_token)
792 lexer->next_token = &eof_token;
793 break;
797 while (lexer->next_token->type == CPP_PURGED);
799 cp_lexer_set_source_position_from_token (token);
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer))
804 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805 cp_lexer_print_token (cp_lexer_debug_stream, token);
806 putc ('\n', cp_lexer_debug_stream);
809 return token;
812 /* Permanently remove the next token from the token stream, and
813 advance the next_token pointer to refer to the next non-purged
814 token. */
816 static void
817 cp_lexer_purge_token (cp_lexer *lexer)
819 cp_token *tok = lexer->next_token;
821 gcc_assert (tok != &eof_token);
822 tok->type = CPP_PURGED;
823 tok->location = UNKNOWN_LOCATION;
824 tok->u.value = NULL_TREE;
825 tok->keyword = RID_MAX;
829 tok++;
830 if (tok == lexer->last_token)
832 tok = &eof_token;
833 break;
836 while (tok->type == CPP_PURGED);
837 lexer->next_token = tok;
840 /* Permanently remove all tokens after TOK, up to, but not
841 including, the token that will be returned next by
842 cp_lexer_peek_token. */
844 static void
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
847 cp_token *peek = lexer->next_token;
849 if (peek == &eof_token)
850 peek = lexer->last_token;
852 gcc_assert (tok < peek);
854 for ( tok += 1; tok != peek; tok += 1)
856 tok->type = CPP_PURGED;
857 tok->location = UNKNOWN_LOCATION;
858 tok->u.value = NULL_TREE;
859 tok->keyword = RID_MAX;
863 /* Begin saving tokens. All tokens consumed after this point will be
864 preserved. */
866 static void
867 cp_lexer_save_tokens (cp_lexer* lexer)
869 /* Provide debugging output. */
870 if (cp_lexer_debugging_p (lexer))
871 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
873 VEC_safe_push (cp_token_position, heap,
874 lexer->saved_tokens, lexer->next_token);
877 /* Commit to the portion of the token stream most recently saved. */
879 static void
880 cp_lexer_commit_tokens (cp_lexer* lexer)
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
886 VEC_pop (cp_token_position, lexer->saved_tokens);
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890 to the token stream. Stop saving tokens. */
892 static void
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
895 /* Provide debugging output. */
896 if (cp_lexer_debugging_p (lexer))
897 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
899 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
904 #ifdef ENABLE_CHECKING
906 static void
907 cp_lexer_print_token (FILE * stream, cp_token *token)
909 /* We don't use cpp_type2name here because the parser defines
910 a few tokens of its own. */
911 static const char *const token_names[] = {
912 /* cpplib-defined token types */
913 #define OP(e, s) #e,
914 #define TK(e, s) #e,
915 TTYPE_TABLE
916 #undef OP
917 #undef TK
918 /* C++ parser token types - see "Manifest constants", above. */
919 "KEYWORD",
920 "TEMPLATE_ID",
921 "NESTED_NAME_SPECIFIER",
922 "PURGED"
925 /* If we have a name for the token, print it out. Otherwise, we
926 simply give the numeric code. */
927 gcc_assert (token->type < ARRAY_SIZE(token_names));
928 fputs (token_names[token->type], stream);
930 /* For some tokens, print the associated data. */
931 switch (token->type)
933 case CPP_KEYWORD:
934 /* Some keywords have a value that is not an IDENTIFIER_NODE.
935 For example, `struct' is mapped to an INTEGER_CST. */
936 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
937 break;
938 /* else fall through */
939 case CPP_NAME:
940 fputs (IDENTIFIER_POINTER (token->u.value), stream);
941 break;
943 case CPP_STRING:
944 case CPP_STRING16:
945 case CPP_STRING32:
946 case CPP_WSTRING:
947 case CPP_UTF8STRING:
948 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
949 break;
951 default:
952 break;
956 /* Start emitting debugging information. */
958 static void
959 cp_lexer_start_debugging (cp_lexer* lexer)
961 lexer->debugging_p = true;
964 /* Stop emitting debugging information. */
966 static void
967 cp_lexer_stop_debugging (cp_lexer* lexer)
969 lexer->debugging_p = false;
972 #endif /* ENABLE_CHECKING */
974 /* Create a new cp_token_cache, representing a range of tokens. */
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
979 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980 cache->first = first;
981 cache->last = last;
982 return cache;
986 /* Decl-specifiers. */
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
990 static void
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
993 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
996 /* Declarators. */
998 /* Nothing other than the parser should be creating declarators;
999 declarators are a semi-syntactic representation of C++ entities.
1000 Other parts of the front end that need to create entities (like
1001 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1003 static cp_declarator *make_call_declarator
1004 (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006 (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008 (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010 (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012 (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014 (cp_cv_quals, tree, cp_declarator *);
1016 /* An erroneous declarator. */
1017 static cp_declarator *cp_error_declarator;
1019 /* The obstack on which declarators and related data structures are
1020 allocated. */
1021 static struct obstack declarator_obstack;
1023 /* Alloc BYTES from the declarator memory pool. */
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1028 return obstack_alloc (&declarator_obstack, bytes);
1031 /* Allocate a declarator of the indicated KIND. Clear fields that are
1032 common to all declarators. */
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1037 cp_declarator *declarator;
1039 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040 declarator->kind = kind;
1041 declarator->attributes = NULL_TREE;
1042 declarator->declarator = NULL;
1043 declarator->parameter_pack_p = false;
1044 declarator->id_loc = UNKNOWN_LOCATION;
1046 return declarator;
1049 /* Make a declarator for a generalized identifier. If
1050 QUALIFYING_SCOPE is non-NULL, the identifier is
1051 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1053 is, if any. */
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057 special_function_kind sfk)
1059 cp_declarator *declarator;
1061 /* It is valid to write:
1063 class C { void f(); };
1064 typedef C D;
1065 void D::f();
1067 The standard is not clear about whether `typedef const C D' is
1068 legal; as of 2002-09-15 the committee is considering that
1069 question. EDG 3.0 allows that syntax. Therefore, we do as
1070 well. */
1071 if (qualifying_scope && TYPE_P (qualifying_scope))
1072 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1074 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1078 declarator = make_declarator (cdk_id);
1079 declarator->u.id.qualifying_scope = qualifying_scope;
1080 declarator->u.id.unqualified_name = unqualified_name;
1081 declarator->u.id.sfk = sfk;
1083 return declarator;
1086 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1087 of modifiers such as const or volatile to apply to the pointer
1088 type, represented as identifiers. */
1090 cp_declarator *
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1093 cp_declarator *declarator;
1095 declarator = make_declarator (cdk_pointer);
1096 declarator->declarator = target;
1097 declarator->u.pointer.qualifiers = cv_qualifiers;
1098 declarator->u.pointer.class_type = NULL_TREE;
1099 if (target)
1101 declarator->id_loc = target->id_loc;
1102 declarator->parameter_pack_p = target->parameter_pack_p;
1103 target->parameter_pack_p = false;
1105 else
1106 declarator->parameter_pack_p = false;
1108 return declarator;
1111 /* Like make_pointer_declarator -- but for references. */
1113 cp_declarator *
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1115 bool rvalue_ref)
1117 cp_declarator *declarator;
1119 declarator = make_declarator (cdk_reference);
1120 declarator->declarator = target;
1121 declarator->u.reference.qualifiers = cv_qualifiers;
1122 declarator->u.reference.rvalue_ref = rvalue_ref;
1123 if (target)
1125 declarator->id_loc = target->id_loc;
1126 declarator->parameter_pack_p = target->parameter_pack_p;
1127 target->parameter_pack_p = false;
1129 else
1130 declarator->parameter_pack_p = false;
1132 return declarator;
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136 member of CLASS_TYPE. */
1138 cp_declarator *
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140 cp_declarator *pointee)
1142 cp_declarator *declarator;
1144 declarator = make_declarator (cdk_ptrmem);
1145 declarator->declarator = pointee;
1146 declarator->u.pointer.qualifiers = cv_qualifiers;
1147 declarator->u.pointer.class_type = class_type;
1149 if (pointee)
1151 declarator->parameter_pack_p = pointee->parameter_pack_p;
1152 pointee->parameter_pack_p = false;
1154 else
1155 declarator->parameter_pack_p = false;
1157 return declarator;
1160 /* Make a declarator for the function given by TARGET, with the
1161 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1162 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1163 indicates what exceptions can be thrown. */
1165 cp_declarator *
1166 make_call_declarator (cp_declarator *target,
1167 tree parms,
1168 cp_cv_quals cv_qualifiers,
1169 tree exception_specification,
1170 tree late_return_type)
1172 cp_declarator *declarator;
1174 declarator = make_declarator (cdk_function);
1175 declarator->declarator = target;
1176 declarator->u.function.parameters = parms;
1177 declarator->u.function.qualifiers = cv_qualifiers;
1178 declarator->u.function.exception_specification = exception_specification;
1179 declarator->u.function.late_return_type = late_return_type;
1180 if (target)
1182 declarator->id_loc = target->id_loc;
1183 declarator->parameter_pack_p = target->parameter_pack_p;
1184 target->parameter_pack_p = false;
1186 else
1187 declarator->parameter_pack_p = false;
1189 return declarator;
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193 defined by ELEMENT. */
1195 cp_declarator *
1196 make_array_declarator (cp_declarator *element, tree bounds)
1198 cp_declarator *declarator;
1200 declarator = make_declarator (cdk_array);
1201 declarator->declarator = element;
1202 declarator->u.array.bounds = bounds;
1203 if (element)
1205 declarator->id_loc = element->id_loc;
1206 declarator->parameter_pack_p = element->parameter_pack_p;
1207 element->parameter_pack_p = false;
1209 else
1210 declarator->parameter_pack_p = false;
1212 return declarator;
1215 /* Determine whether the declarator we've seen so far can be a
1216 parameter pack, when followed by an ellipsis. */
1217 static bool
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1220 /* Search for a declarator name, or any other declarator that goes
1221 after the point where the ellipsis could appear in a parameter
1222 pack. If we find any of these, then this declarator can not be
1223 made into a parameter pack. */
1224 bool found = false;
1225 while (declarator && !found)
1227 switch ((int)declarator->kind)
1229 case cdk_id:
1230 case cdk_array:
1231 found = true;
1232 break;
1234 case cdk_error:
1235 return true;
1237 default:
1238 declarator = declarator->declarator;
1239 break;
1243 return !found;
1246 cp_parameter_declarator *no_parameters;
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249 DECLARATOR and DEFAULT_ARGUMENT. */
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253 cp_declarator *declarator,
1254 tree default_argument)
1256 cp_parameter_declarator *parameter;
1258 parameter = ((cp_parameter_declarator *)
1259 alloc_declarator (sizeof (cp_parameter_declarator)));
1260 parameter->next = NULL;
1261 if (decl_specifiers)
1262 parameter->decl_specifiers = *decl_specifiers;
1263 else
1264 clear_decl_specs (&parameter->decl_specifiers);
1265 parameter->declarator = declarator;
1266 parameter->default_argument = default_argument;
1267 parameter->ellipsis_p = false;
1269 return parameter;
1272 /* Returns true iff DECLARATOR is a declaration for a function. */
1274 static bool
1275 function_declarator_p (const cp_declarator *declarator)
1277 while (declarator)
1279 if (declarator->kind == cdk_function
1280 && declarator->declarator->kind == cdk_id)
1281 return true;
1282 if (declarator->kind == cdk_id
1283 || declarator->kind == cdk_error)
1284 return false;
1285 declarator = declarator->declarator;
1287 return false;
1290 /* The parser. */
1292 /* Overview
1293 --------
1295 A cp_parser parses the token stream as specified by the C++
1296 grammar. Its job is purely parsing, not semantic analysis. For
1297 example, the parser breaks the token stream into declarators,
1298 expressions, statements, and other similar syntactic constructs.
1299 It does not check that the types of the expressions on either side
1300 of an assignment-statement are compatible, or that a function is
1301 not declared with a parameter of type `void'.
1303 The parser invokes routines elsewhere in the compiler to perform
1304 semantic analysis and to build up the abstract syntax tree for the
1305 code processed.
1307 The parser (and the template instantiation code, which is, in a
1308 way, a close relative of parsing) are the only parts of the
1309 compiler that should be calling push_scope and pop_scope, or
1310 related functions. The parser (and template instantiation code)
1311 keeps track of what scope is presently active; everything else
1312 should simply honor that. (The code that generates static
1313 initializers may also need to set the scope, in order to check
1314 access control correctly when emitting the initializers.)
1316 Methodology
1317 -----------
1319 The parser is of the standard recursive-descent variety. Upcoming
1320 tokens in the token stream are examined in order to determine which
1321 production to use when parsing a non-terminal. Some C++ constructs
1322 require arbitrary look ahead to disambiguate. For example, it is
1323 impossible, in the general case, to tell whether a statement is an
1324 expression or declaration without scanning the entire statement.
1325 Therefore, the parser is capable of "parsing tentatively." When the
1326 parser is not sure what construct comes next, it enters this mode.
1327 Then, while we attempt to parse the construct, the parser queues up
1328 error messages, rather than issuing them immediately, and saves the
1329 tokens it consumes. If the construct is parsed successfully, the
1330 parser "commits", i.e., it issues any queued error messages and
1331 the tokens that were being preserved are permanently discarded.
1332 If, however, the construct is not parsed successfully, the parser
1333 rolls back its state completely so that it can resume parsing using
1334 a different alternative.
1336 Future Improvements
1337 -------------------
1339 The performance of the parser could probably be improved substantially.
1340 We could often eliminate the need to parse tentatively by looking ahead
1341 a little bit. In some places, this approach might not entirely eliminate
1342 the need to parse tentatively, but it might still speed up the average
1343 case. */
1345 /* Flags that are passed to some parsing functions. These values can
1346 be bitwise-ored together. */
1348 enum
1350 /* No flags. */
1351 CP_PARSER_FLAGS_NONE = 0x0,
1352 /* The construct is optional. If it is not present, then no error
1353 should be issued. */
1354 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355 /* When parsing a type-specifier, treat user-defined type-names
1356 as non-type identifiers. */
1357 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358 /* When parsing a type-specifier, do not try to parse a class-specifier
1359 or enum-specifier. */
1360 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361 /* When parsing a decl-specifier-seq, only allow type-specifier or
1362 constexpr. */
1363 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1366 /* This type is used for parameters and variables which hold
1367 combinations of the above flags. */
1368 typedef int cp_parser_flags;
1370 /* The different kinds of declarators we want to parse. */
1372 typedef enum cp_parser_declarator_kind
1374 /* We want an abstract declarator. */
1375 CP_PARSER_DECLARATOR_ABSTRACT,
1376 /* We want a named declarator. */
1377 CP_PARSER_DECLARATOR_NAMED,
1378 /* We don't mind, but the name must be an unqualified-id. */
1379 CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1382 /* The precedence values used to parse binary expressions. The minimum value
1383 of PREC must be 1, because zero is reserved to quickly discriminate
1384 binary operators from other tokens. */
1386 enum cp_parser_prec
1388 PREC_NOT_OPERATOR,
1389 PREC_LOGICAL_OR_EXPRESSION,
1390 PREC_LOGICAL_AND_EXPRESSION,
1391 PREC_INCLUSIVE_OR_EXPRESSION,
1392 PREC_EXCLUSIVE_OR_EXPRESSION,
1393 PREC_AND_EXPRESSION,
1394 PREC_EQUALITY_EXPRESSION,
1395 PREC_RELATIONAL_EXPRESSION,
1396 PREC_SHIFT_EXPRESSION,
1397 PREC_ADDITIVE_EXPRESSION,
1398 PREC_MULTIPLICATIVE_EXPRESSION,
1399 PREC_PM_EXPRESSION,
1400 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404 precedence value. */
1406 typedef struct cp_parser_binary_operations_map_node
1408 /* The token type. */
1409 enum cpp_ttype token_type;
1410 /* The corresponding tree code. */
1411 enum tree_code tree_type;
1412 /* The precedence of this operator. */
1413 enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1416 /* The status of a tentative parse. */
1418 typedef enum cp_parser_status_kind
1420 /* No errors have occurred. */
1421 CP_PARSER_STATUS_KIND_NO_ERROR,
1422 /* An error has occurred. */
1423 CP_PARSER_STATUS_KIND_ERROR,
1424 /* We are committed to this tentative parse, whether or not an error
1425 has occurred. */
1426 CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1429 typedef struct cp_parser_expression_stack_entry
1431 /* Left hand side of the binary operation we are currently
1432 parsing. */
1433 tree lhs;
1434 /* Original tree code for left hand side, if it was a binary
1435 expression itself (used for -Wparentheses). */
1436 enum tree_code lhs_type;
1437 /* Tree code for the binary operation we are parsing. */
1438 enum tree_code tree_type;
1439 /* Precedence of the binary operation we are parsing. */
1440 enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1443 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1444 entries because precedence levels on the stack are monotonically
1445 increasing. */
1446 typedef struct cp_parser_expression_stack_entry
1447 cp_parser_expression_stack[NUM_PREC_VALUES];
1449 /* Context that is saved and restored when parsing tentatively. */
1450 typedef struct GTY (()) cp_parser_context {
1451 /* If this is a tentative parsing context, the status of the
1452 tentative parse. */
1453 enum cp_parser_status_kind status;
1454 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1455 that are looked up in this context must be looked up both in the
1456 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457 the context of the containing expression. */
1458 tree object_type;
1460 /* The next parsing context in the stack. */
1461 struct cp_parser_context *next;
1462 } cp_parser_context;
1464 /* Prototypes. */
1466 /* Constructors and destructors. */
1468 static cp_parser_context *cp_parser_context_new
1469 (cp_parser_context *);
1471 /* Class variables. */
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476 Transformed into an associative array (binops_by_token) by
1477 cp_parser_new. */
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1483 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1487 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1490 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1493 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1498 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1501 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1503 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1505 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1507 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1509 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1512 /* The same as binops, but initialized by cp_parser_new so that
1513 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1514 for speed. */
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1517 /* Constructors and destructors. */
1519 /* Construct a new context. The context below this one on the stack
1520 is given by NEXT. */
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1525 cp_parser_context *context;
1527 /* Allocate the storage. */
1528 if (cp_parser_context_free_list != NULL)
1530 /* Pull the first entry from the free list. */
1531 context = cp_parser_context_free_list;
1532 cp_parser_context_free_list = context->next;
1533 memset (context, 0, sizeof (*context));
1535 else
1536 context = ggc_alloc_cleared_cp_parser_context ();
1538 /* No errors have occurred yet in this context. */
1539 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540 /* If this is not the bottommost context, copy information that we
1541 need from the previous context. */
1542 if (next)
1544 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545 expression, then we are parsing one in this context, too. */
1546 context->object_type = next->object_type;
1547 /* Thread the stack. */
1548 context->next = next;
1551 return context;
1554 /* An entry in a queue of function arguments that require post-processing. */
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557 /* The current_class_type when we parsed this arg. */
1558 tree class_type;
1560 /* The function decl itself. */
1561 tree decl;
1562 } cp_default_arg_entry;
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1567 /* An entry in a stack for member functions of local classes. */
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570 /* Functions with default arguments that require post-processing.
1571 Functions appear in this list in declaration order. */
1572 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1574 /* Functions with defintions that require post-processing. Functions
1575 appear in this list in declaration order. */
1576 VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1582 /* The cp_parser structure represents the C++ parser. */
1584 typedef struct GTY(()) cp_parser {
1585 /* The lexer from which we are obtaining tokens. */
1586 cp_lexer *lexer;
1588 /* The scope in which names should be looked up. If NULL_TREE, then
1589 we look up names in the scope that is currently open in the
1590 source program. If non-NULL, this is either a TYPE or
1591 NAMESPACE_DECL for the scope in which we should look. It can
1592 also be ERROR_MARK, when we've parsed a bogus scope.
1594 This value is not cleared automatically after a name is looked
1595 up, so we must be careful to clear it before starting a new look
1596 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1597 will look up `Z' in the scope of `X', rather than the current
1598 scope.) Unfortunately, it is difficult to tell when name lookup
1599 is complete, because we sometimes peek at a token, look it up,
1600 and then decide not to consume it. */
1601 tree scope;
1603 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604 last lookup took place. OBJECT_SCOPE is used if an expression
1605 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606 respectively. QUALIFYING_SCOPE is used for an expression of the
1607 form "X::Y"; it refers to X. */
1608 tree object_scope;
1609 tree qualifying_scope;
1611 /* A stack of parsing contexts. All but the bottom entry on the
1612 stack will be tentative contexts.
1614 We parse tentatively in order to determine which construct is in
1615 use in some situations. For example, in order to determine
1616 whether a statement is an expression-statement or a
1617 declaration-statement we parse it tentatively as a
1618 declaration-statement. If that fails, we then reparse the same
1619 token stream as an expression-statement. */
1620 cp_parser_context *context;
1622 /* True if we are parsing GNU C++. If this flag is not set, then
1623 GNU extensions are not recognized. */
1624 bool allow_gnu_extensions_p;
1626 /* TRUE if the `>' token should be interpreted as the greater-than
1627 operator. FALSE if it is the end of a template-id or
1628 template-parameter-list. In C++0x mode, this flag also applies to
1629 `>>' tokens, which are viewed as two consecutive `>' tokens when
1630 this flag is FALSE. */
1631 bool greater_than_is_operator_p;
1633 /* TRUE if default arguments are allowed within a parameter list
1634 that starts at this point. FALSE if only a gnu extension makes
1635 them permissible. */
1636 bool default_arg_ok_p;
1638 /* TRUE if we are parsing an integral constant-expression. See
1639 [expr.const] for a precise definition. */
1640 bool integral_constant_expression_p;
1642 /* TRUE if we are parsing an integral constant-expression -- but a
1643 non-constant expression should be permitted as well. This flag
1644 is used when parsing an array bound so that GNU variable-length
1645 arrays are tolerated. */
1646 bool allow_non_integral_constant_expression_p;
1648 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649 been seen that makes the expression non-constant. */
1650 bool non_integral_constant_expression_p;
1652 /* TRUE if local variable names and `this' are forbidden in the
1653 current context. */
1654 bool local_variables_forbidden_p;
1656 /* TRUE if the declaration we are parsing is part of a
1657 linkage-specification of the form `extern string-literal
1658 declaration'. */
1659 bool in_unbraced_linkage_specification_p;
1661 /* TRUE if we are presently parsing a declarator, after the
1662 direct-declarator. */
1663 bool in_declarator_p;
1665 /* TRUE if we are presently parsing a template-argument-list. */
1666 bool in_template_argument_list_p;
1668 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669 to IN_OMP_BLOCK if parsing OpenMP structured block and
1670 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1671 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672 iteration-statement, OpenMP block or loop within that switch. */
1673 #define IN_SWITCH_STMT 1
1674 #define IN_ITERATION_STMT 2
1675 #define IN_OMP_BLOCK 4
1676 #define IN_OMP_FOR 8
1677 #define IN_IF_STMT 16
1678 unsigned char in_statement;
1680 /* TRUE if we are presently parsing the body of a switch statement.
1681 Note that this doesn't quite overlap with in_statement above.
1682 The difference relates to giving the right sets of error messages:
1683 "case not in switch" vs "break statement used with OpenMP...". */
1684 bool in_switch_statement_p;
1686 /* TRUE if we are parsing a type-id in an expression context. In
1687 such a situation, both "type (expr)" and "type (type)" are valid
1688 alternatives. */
1689 bool in_type_id_in_expr_p;
1691 /* TRUE if we are currently in a header file where declarations are
1692 implicitly extern "C". */
1693 bool implicit_extern_c;
1695 /* TRUE if strings in expressions should be translated to the execution
1696 character set. */
1697 bool translate_strings_p;
1699 /* TRUE if we are presently parsing the body of a function, but not
1700 a local class. */
1701 bool in_function_body;
1703 /* TRUE if we can auto-correct a colon to a scope operator. */
1704 bool colon_corrects_to_scope_p;
1706 /* If non-NULL, then we are parsing a construct where new type
1707 definitions are not permitted. The string stored here will be
1708 issued as an error message if a type is defined. */
1709 const char *type_definition_forbidden_message;
1711 /* A stack used for member functions of local classes. The lists
1712 contained in an individual entry can only be processed once the
1713 outermost class being defined is complete. */
1714 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1716 /* The number of classes whose definitions are currently in
1717 progress. */
1718 unsigned num_classes_being_defined;
1720 /* The number of template parameter lists that apply directly to the
1721 current declaration. */
1722 unsigned num_template_parameter_lists;
1723 } cp_parser;
1725 /* Managing the unparsed function queues. */
1727 #define unparsed_funs_with_default_args \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1729 #define unparsed_funs_with_definitions \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1732 static void
1733 push_unparsed_function_queues (cp_parser *parser)
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1748 /* Prototypes. */
1750 /* Constructors and destructors. */
1752 static cp_parser *cp_parser_new
1753 (void);
1755 /* Routines to parse various constructs.
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1767 /* Lexical conventions [gram.lex] */
1769 static tree cp_parser_identifier
1770 (cp_parser *);
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1774 /* Basic concepts [gram.basic] */
1776 static bool cp_parser_translation_unit
1777 (cp_parser *);
1779 /* Expressions [gram.expr] */
1781 static tree cp_parser_primary_expression
1782 (cp_parser *, bool, bool, bool, cp_id_kind *);
1783 static tree cp_parser_id_expression
1784 (cp_parser *, bool, bool, bool *, bool, bool);
1785 static tree cp_parser_unqualified_id
1786 (cp_parser *, bool, bool, bool, bool);
1787 static tree cp_parser_nested_name_specifier_opt
1788 (cp_parser *, bool, bool, bool, bool);
1789 static tree cp_parser_nested_name_specifier
1790 (cp_parser *, bool, bool, bool, bool);
1791 static tree cp_parser_qualifying_entity
1792 (cp_parser *, bool, bool, bool, bool, bool);
1793 static tree cp_parser_postfix_expression
1794 (cp_parser *, bool, bool, bool, cp_id_kind *);
1795 static tree cp_parser_postfix_open_square_expression
1796 (cp_parser *, tree, bool);
1797 static tree cp_parser_postfix_dot_deref_expression
1798 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1799 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1800 (cp_parser *, int, bool, bool, bool *);
1801 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1802 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1803 static void cp_parser_pseudo_destructor_name
1804 (cp_parser *, tree *, tree *);
1805 static tree cp_parser_unary_expression
1806 (cp_parser *, bool, bool, cp_id_kind *);
1807 static enum tree_code cp_parser_unary_operator
1808 (cp_token *);
1809 static tree cp_parser_new_expression
1810 (cp_parser *);
1811 static VEC(tree,gc) *cp_parser_new_placement
1812 (cp_parser *);
1813 static tree cp_parser_new_type_id
1814 (cp_parser *, tree *);
1815 static cp_declarator *cp_parser_new_declarator_opt
1816 (cp_parser *);
1817 static cp_declarator *cp_parser_direct_new_declarator
1818 (cp_parser *);
1819 static VEC(tree,gc) *cp_parser_new_initializer
1820 (cp_parser *);
1821 static tree cp_parser_delete_expression
1822 (cp_parser *);
1823 static tree cp_parser_cast_expression
1824 (cp_parser *, bool, bool, cp_id_kind *);
1825 static tree cp_parser_binary_expression
1826 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1827 static tree cp_parser_question_colon_clause
1828 (cp_parser *, tree);
1829 static tree cp_parser_assignment_expression
1830 (cp_parser *, bool, cp_id_kind *);
1831 static enum tree_code cp_parser_assignment_operator_opt
1832 (cp_parser *);
1833 static tree cp_parser_expression
1834 (cp_parser *, bool, cp_id_kind *);
1835 static tree cp_parser_constant_expression
1836 (cp_parser *, bool, bool *);
1837 static tree cp_parser_builtin_offsetof
1838 (cp_parser *);
1839 static tree cp_parser_lambda_expression
1840 (cp_parser *);
1841 static void cp_parser_lambda_introducer
1842 (cp_parser *, tree);
1843 static void cp_parser_lambda_declarator_opt
1844 (cp_parser *, tree);
1845 static void cp_parser_lambda_body
1846 (cp_parser *, tree);
1848 /* Statements [gram.stmt.stmt] */
1850 static void cp_parser_statement
1851 (cp_parser *, tree, bool, bool *);
1852 static void cp_parser_label_for_labeled_statement
1853 (cp_parser *);
1854 static tree cp_parser_expression_statement
1855 (cp_parser *, tree);
1856 static tree cp_parser_compound_statement
1857 (cp_parser *, tree, bool);
1858 static void cp_parser_statement_seq_opt
1859 (cp_parser *, tree);
1860 static tree cp_parser_selection_statement
1861 (cp_parser *, bool *);
1862 static tree cp_parser_condition
1863 (cp_parser *);
1864 static tree cp_parser_iteration_statement
1865 (cp_parser *);
1866 static void cp_parser_for_init_statement
1867 (cp_parser *);
1868 static tree cp_parser_c_for
1869 (cp_parser *);
1870 static tree cp_parser_range_for
1871 (cp_parser *);
1872 static tree cp_parser_jump_statement
1873 (cp_parser *);
1874 static void cp_parser_declaration_statement
1875 (cp_parser *);
1877 static tree cp_parser_implicitly_scoped_statement
1878 (cp_parser *, bool *);
1879 static void cp_parser_already_scoped_statement
1880 (cp_parser *);
1882 /* Declarations [gram.dcl.dcl] */
1884 static void cp_parser_declaration_seq_opt
1885 (cp_parser *);
1886 static void cp_parser_declaration
1887 (cp_parser *);
1888 static void cp_parser_block_declaration
1889 (cp_parser *, bool);
1890 static void cp_parser_simple_declaration
1891 (cp_parser *, bool);
1892 static void cp_parser_decl_specifier_seq
1893 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1894 static tree cp_parser_storage_class_specifier_opt
1895 (cp_parser *);
1896 static tree cp_parser_function_specifier_opt
1897 (cp_parser *, cp_decl_specifier_seq *);
1898 static tree cp_parser_type_specifier
1899 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1900 int *, bool *);
1901 static tree cp_parser_simple_type_specifier
1902 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1903 static tree cp_parser_type_name
1904 (cp_parser *);
1905 static tree cp_parser_nonclass_name
1906 (cp_parser* parser);
1907 static tree cp_parser_elaborated_type_specifier
1908 (cp_parser *, bool, bool);
1909 static tree cp_parser_enum_specifier
1910 (cp_parser *);
1911 static void cp_parser_enumerator_list
1912 (cp_parser *, tree);
1913 static void cp_parser_enumerator_definition
1914 (cp_parser *, tree);
1915 static tree cp_parser_namespace_name
1916 (cp_parser *);
1917 static void cp_parser_namespace_definition
1918 (cp_parser *);
1919 static void cp_parser_namespace_body
1920 (cp_parser *);
1921 static tree cp_parser_qualified_namespace_specifier
1922 (cp_parser *);
1923 static void cp_parser_namespace_alias_definition
1924 (cp_parser *);
1925 static bool cp_parser_using_declaration
1926 (cp_parser *, bool);
1927 static void cp_parser_using_directive
1928 (cp_parser *);
1929 static void cp_parser_asm_definition
1930 (cp_parser *);
1931 static void cp_parser_linkage_specification
1932 (cp_parser *);
1933 static void cp_parser_static_assert
1934 (cp_parser *, bool);
1935 static tree cp_parser_decltype
1936 (cp_parser *);
1938 /* Declarators [gram.dcl.decl] */
1940 static tree cp_parser_init_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1942 static cp_declarator *cp_parser_declarator
1943 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1944 static cp_declarator *cp_parser_direct_declarator
1945 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1946 static enum tree_code cp_parser_ptr_operator
1947 (cp_parser *, tree *, cp_cv_quals *);
1948 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1949 (cp_parser *);
1950 static tree cp_parser_late_return_type_opt
1951 (cp_parser *);
1952 static tree cp_parser_declarator_id
1953 (cp_parser *, bool);
1954 static tree cp_parser_type_id
1955 (cp_parser *);
1956 static tree cp_parser_template_type_arg
1957 (cp_parser *);
1958 static tree cp_parser_trailing_type_id (cp_parser *);
1959 static tree cp_parser_type_id_1
1960 (cp_parser *, bool, bool);
1961 static void cp_parser_type_specifier_seq
1962 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1963 static tree cp_parser_parameter_declaration_clause
1964 (cp_parser *);
1965 static tree cp_parser_parameter_declaration_list
1966 (cp_parser *, bool *);
1967 static cp_parameter_declarator *cp_parser_parameter_declaration
1968 (cp_parser *, bool, bool *);
1969 static tree cp_parser_default_argument
1970 (cp_parser *, bool);
1971 static void cp_parser_function_body
1972 (cp_parser *);
1973 static tree cp_parser_initializer
1974 (cp_parser *, bool *, bool *);
1975 static tree cp_parser_initializer_clause
1976 (cp_parser *, bool *);
1977 static tree cp_parser_braced_list
1978 (cp_parser*, bool*);
1979 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1980 (cp_parser *, bool *);
1982 static bool cp_parser_ctor_initializer_opt_and_function_body
1983 (cp_parser *);
1985 /* Classes [gram.class] */
1987 static tree cp_parser_class_name
1988 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1989 static tree cp_parser_class_specifier
1990 (cp_parser *);
1991 static tree cp_parser_class_head
1992 (cp_parser *, bool *, tree *, tree *);
1993 static enum tag_types cp_parser_class_key
1994 (cp_parser *);
1995 static void cp_parser_member_specification_opt
1996 (cp_parser *);
1997 static void cp_parser_member_declaration
1998 (cp_parser *);
1999 static tree cp_parser_pure_specifier
2000 (cp_parser *);
2001 static tree cp_parser_constant_initializer
2002 (cp_parser *);
2004 /* Derived classes [gram.class.derived] */
2006 static tree cp_parser_base_clause
2007 (cp_parser *);
2008 static tree cp_parser_base_specifier
2009 (cp_parser *);
2011 /* Special member functions [gram.special] */
2013 static tree cp_parser_conversion_function_id
2014 (cp_parser *);
2015 static tree cp_parser_conversion_type_id
2016 (cp_parser *);
2017 static cp_declarator *cp_parser_conversion_declarator_opt
2018 (cp_parser *);
2019 static bool cp_parser_ctor_initializer_opt
2020 (cp_parser *);
2021 static void cp_parser_mem_initializer_list
2022 (cp_parser *);
2023 static tree cp_parser_mem_initializer
2024 (cp_parser *);
2025 static tree cp_parser_mem_initializer_id
2026 (cp_parser *);
2028 /* Overloading [gram.over] */
2030 static tree cp_parser_operator_function_id
2031 (cp_parser *);
2032 static tree cp_parser_operator
2033 (cp_parser *);
2035 /* Templates [gram.temp] */
2037 static void cp_parser_template_declaration
2038 (cp_parser *, bool);
2039 static tree cp_parser_template_parameter_list
2040 (cp_parser *);
2041 static tree cp_parser_template_parameter
2042 (cp_parser *, bool *, bool *);
2043 static tree cp_parser_type_parameter
2044 (cp_parser *, bool *);
2045 static tree cp_parser_template_id
2046 (cp_parser *, bool, bool, bool);
2047 static tree cp_parser_template_name
2048 (cp_parser *, bool, bool, bool, bool *);
2049 static tree cp_parser_template_argument_list
2050 (cp_parser *);
2051 static tree cp_parser_template_argument
2052 (cp_parser *);
2053 static void cp_parser_explicit_instantiation
2054 (cp_parser *);
2055 static void cp_parser_explicit_specialization
2056 (cp_parser *);
2058 /* Exception handling [gram.exception] */
2060 static tree cp_parser_try_block
2061 (cp_parser *);
2062 static bool cp_parser_function_try_block
2063 (cp_parser *);
2064 static void cp_parser_handler_seq
2065 (cp_parser *);
2066 static void cp_parser_handler
2067 (cp_parser *);
2068 static tree cp_parser_exception_declaration
2069 (cp_parser *);
2070 static tree cp_parser_throw_expression
2071 (cp_parser *);
2072 static tree cp_parser_exception_specification_opt
2073 (cp_parser *);
2074 static tree cp_parser_type_id_list
2075 (cp_parser *);
2077 /* GNU Extensions */
2079 static tree cp_parser_asm_specification_opt
2080 (cp_parser *);
2081 static tree cp_parser_asm_operand_list
2082 (cp_parser *);
2083 static tree cp_parser_asm_clobber_list
2084 (cp_parser *);
2085 static tree cp_parser_asm_label_list
2086 (cp_parser *);
2087 static tree cp_parser_attributes_opt
2088 (cp_parser *);
2089 static tree cp_parser_attribute_list
2090 (cp_parser *);
2091 static bool cp_parser_extension_opt
2092 (cp_parser *, int *);
2093 static void cp_parser_label_declaration
2094 (cp_parser *);
2096 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2097 static bool cp_parser_pragma
2098 (cp_parser *, enum pragma_context);
2100 /* Objective-C++ Productions */
2102 static tree cp_parser_objc_message_receiver
2103 (cp_parser *);
2104 static tree cp_parser_objc_message_args
2105 (cp_parser *);
2106 static tree cp_parser_objc_message_expression
2107 (cp_parser *);
2108 static tree cp_parser_objc_encode_expression
2109 (cp_parser *);
2110 static tree cp_parser_objc_defs_expression
2111 (cp_parser *);
2112 static tree cp_parser_objc_protocol_expression
2113 (cp_parser *);
2114 static tree cp_parser_objc_selector_expression
2115 (cp_parser *);
2116 static tree cp_parser_objc_expression
2117 (cp_parser *);
2118 static bool cp_parser_objc_selector_p
2119 (enum cpp_ttype);
2120 static tree cp_parser_objc_selector
2121 (cp_parser *);
2122 static tree cp_parser_objc_protocol_refs_opt
2123 (cp_parser *);
2124 static void cp_parser_objc_declaration
2125 (cp_parser *, tree);
2126 static tree cp_parser_objc_statement
2127 (cp_parser *);
2128 static bool cp_parser_objc_valid_prefix_attributes
2129 (cp_parser *, tree *);
2130 static void cp_parser_objc_at_property_declaration
2131 (cp_parser *) ;
2132 static void cp_parser_objc_at_synthesize_declaration
2133 (cp_parser *) ;
2134 static void cp_parser_objc_at_dynamic_declaration
2135 (cp_parser *) ;
2136 static tree cp_parser_objc_struct_declaration
2137 (cp_parser *) ;
2139 /* Utility Routines */
2141 static tree cp_parser_lookup_name
2142 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2143 static tree cp_parser_lookup_name_simple
2144 (cp_parser *, tree, location_t);
2145 static tree cp_parser_maybe_treat_template_as_class
2146 (tree, bool);
2147 static bool cp_parser_check_declarator_template_parameters
2148 (cp_parser *, cp_declarator *, location_t);
2149 static bool cp_parser_check_template_parameters
2150 (cp_parser *, unsigned, location_t, cp_declarator *);
2151 static tree cp_parser_simple_cast_expression
2152 (cp_parser *);
2153 static tree cp_parser_global_scope_opt
2154 (cp_parser *, bool);
2155 static bool cp_parser_constructor_declarator_p
2156 (cp_parser *, bool);
2157 static tree cp_parser_function_definition_from_specifiers_and_declarator
2158 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2159 static tree cp_parser_function_definition_after_declarator
2160 (cp_parser *, bool);
2161 static void cp_parser_template_declaration_after_export
2162 (cp_parser *, bool);
2163 static void cp_parser_perform_template_parameter_access_checks
2164 (VEC (deferred_access_check,gc)*);
2165 static tree cp_parser_single_declaration
2166 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2167 static tree cp_parser_functional_cast
2168 (cp_parser *, tree);
2169 static tree cp_parser_save_member_function_body
2170 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2171 static tree cp_parser_enclosed_template_argument_list
2172 (cp_parser *);
2173 static void cp_parser_save_default_args
2174 (cp_parser *, tree);
2175 static void cp_parser_late_parsing_for_member
2176 (cp_parser *, tree);
2177 static void cp_parser_late_parsing_default_args
2178 (cp_parser *, tree);
2179 static tree cp_parser_sizeof_operand
2180 (cp_parser *, enum rid);
2181 static tree cp_parser_trait_expr
2182 (cp_parser *, enum rid);
2183 static bool cp_parser_declares_only_class_p
2184 (cp_parser *);
2185 static void cp_parser_set_storage_class
2186 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2187 static void cp_parser_set_decl_spec_type
2188 (cp_decl_specifier_seq *, tree, location_t, bool);
2189 static bool cp_parser_friend_p
2190 (const cp_decl_specifier_seq *);
2191 static void cp_parser_required_error
2192 (cp_parser *, required_token, bool);
2193 static cp_token *cp_parser_require
2194 (cp_parser *, enum cpp_ttype, required_token);
2195 static cp_token *cp_parser_require_keyword
2196 (cp_parser *, enum rid, required_token);
2197 static bool cp_parser_token_starts_function_definition_p
2198 (cp_token *);
2199 static bool cp_parser_next_token_starts_class_definition_p
2200 (cp_parser *);
2201 static bool cp_parser_next_token_ends_template_argument_p
2202 (cp_parser *);
2203 static bool cp_parser_nth_token_starts_template_argument_list_p
2204 (cp_parser *, size_t);
2205 static enum tag_types cp_parser_token_is_class_key
2206 (cp_token *);
2207 static void cp_parser_check_class_key
2208 (enum tag_types, tree type);
2209 static void cp_parser_check_access_in_redeclaration
2210 (tree type, location_t location);
2211 static bool cp_parser_optional_template_keyword
2212 (cp_parser *);
2213 static void cp_parser_pre_parsed_nested_name_specifier
2214 (cp_parser *);
2215 static bool cp_parser_cache_group
2216 (cp_parser *, enum cpp_ttype, unsigned);
2217 static void cp_parser_parse_tentatively
2218 (cp_parser *);
2219 static void cp_parser_commit_to_tentative_parse
2220 (cp_parser *);
2221 static void cp_parser_abort_tentative_parse
2222 (cp_parser *);
2223 static bool cp_parser_parse_definitely
2224 (cp_parser *);
2225 static inline bool cp_parser_parsing_tentatively
2226 (cp_parser *);
2227 static bool cp_parser_uncommitted_to_tentative_parse_p
2228 (cp_parser *);
2229 static void cp_parser_error
2230 (cp_parser *, const char *);
2231 static void cp_parser_name_lookup_error
2232 (cp_parser *, tree, tree, name_lookup_error, location_t);
2233 static bool cp_parser_simulate_error
2234 (cp_parser *);
2235 static bool cp_parser_check_type_definition
2236 (cp_parser *);
2237 static void cp_parser_check_for_definition_in_return_type
2238 (cp_declarator *, tree, location_t type_location);
2239 static void cp_parser_check_for_invalid_template_id
2240 (cp_parser *, tree, location_t location);
2241 static bool cp_parser_non_integral_constant_expression
2242 (cp_parser *, non_integral_constant);
2243 static void cp_parser_diagnose_invalid_type_name
2244 (cp_parser *, tree, tree, location_t);
2245 static bool cp_parser_parse_and_diagnose_invalid_type_name
2246 (cp_parser *);
2247 static int cp_parser_skip_to_closing_parenthesis
2248 (cp_parser *, bool, bool, bool);
2249 static void cp_parser_skip_to_end_of_statement
2250 (cp_parser *);
2251 static void cp_parser_consume_semicolon_at_end_of_statement
2252 (cp_parser *);
2253 static void cp_parser_skip_to_end_of_block_or_statement
2254 (cp_parser *);
2255 static bool cp_parser_skip_to_closing_brace
2256 (cp_parser *);
2257 static void cp_parser_skip_to_end_of_template_parameter_list
2258 (cp_parser *);
2259 static void cp_parser_skip_to_pragma_eol
2260 (cp_parser*, cp_token *);
2261 static bool cp_parser_error_occurred
2262 (cp_parser *);
2263 static bool cp_parser_allow_gnu_extensions_p
2264 (cp_parser *);
2265 static bool cp_parser_is_string_literal
2266 (cp_token *);
2267 static bool cp_parser_is_keyword
2268 (cp_token *, enum rid);
2269 static tree cp_parser_make_typename_type
2270 (cp_parser *, tree, tree, location_t location);
2271 static cp_declarator * cp_parser_make_indirect_declarator
2272 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2274 /* Returns nonzero if we are parsing tentatively. */
2276 static inline bool
2277 cp_parser_parsing_tentatively (cp_parser* parser)
2279 return parser->context->next != NULL;
2282 /* Returns nonzero if TOKEN is a string literal. */
2284 static bool
2285 cp_parser_is_string_literal (cp_token* token)
2287 return (token->type == CPP_STRING ||
2288 token->type == CPP_STRING16 ||
2289 token->type == CPP_STRING32 ||
2290 token->type == CPP_WSTRING ||
2291 token->type == CPP_UTF8STRING);
2294 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2296 static bool
2297 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2299 return token->keyword == keyword;
2302 /* If not parsing tentatively, issue a diagnostic of the form
2303 FILE:LINE: MESSAGE before TOKEN
2304 where TOKEN is the next token in the input stream. MESSAGE
2305 (specified by the caller) is usually of the form "expected
2306 OTHER-TOKEN". */
2308 static void
2309 cp_parser_error (cp_parser* parser, const char* gmsgid)
2311 if (!cp_parser_simulate_error (parser))
2313 cp_token *token = cp_lexer_peek_token (parser->lexer);
2314 /* This diagnostic makes more sense if it is tagged to the line
2315 of the token we just peeked at. */
2316 cp_lexer_set_source_position_from_token (token);
2318 if (token->type == CPP_PRAGMA)
2320 error_at (token->location,
2321 "%<#pragma%> is not allowed here");
2322 cp_parser_skip_to_pragma_eol (parser, token);
2323 return;
2326 c_parse_error (gmsgid,
2327 /* Because c_parser_error does not understand
2328 CPP_KEYWORD, keywords are treated like
2329 identifiers. */
2330 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2331 token->u.value, token->flags);
2335 /* Issue an error about name-lookup failing. NAME is the
2336 IDENTIFIER_NODE DECL is the result of
2337 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2338 the thing that we hoped to find. */
2340 static void
2341 cp_parser_name_lookup_error (cp_parser* parser,
2342 tree name,
2343 tree decl,
2344 name_lookup_error desired,
2345 location_t location)
2347 /* If name lookup completely failed, tell the user that NAME was not
2348 declared. */
2349 if (decl == error_mark_node)
2351 if (parser->scope && parser->scope != global_namespace)
2352 error_at (location, "%<%E::%E%> has not been declared",
2353 parser->scope, name);
2354 else if (parser->scope == global_namespace)
2355 error_at (location, "%<::%E%> has not been declared", name);
2356 else if (parser->object_scope
2357 && !CLASS_TYPE_P (parser->object_scope))
2358 error_at (location, "request for member %qE in non-class type %qT",
2359 name, parser->object_scope);
2360 else if (parser->object_scope)
2361 error_at (location, "%<%T::%E%> has not been declared",
2362 parser->object_scope, name);
2363 else
2364 error_at (location, "%qE has not been declared", name);
2366 else if (parser->scope && parser->scope != global_namespace)
2368 switch (desired)
2370 case NLE_TYPE:
2371 error_at (location, "%<%E::%E%> is not a type",
2372 parser->scope, name);
2373 break;
2374 case NLE_CXX98:
2375 error_at (location, "%<%E::%E%> is not a class or namespace",
2376 parser->scope, name);
2377 break;
2378 case NLE_NOT_CXX98:
2379 error_at (location,
2380 "%<%E::%E%> is not a class, namespace, or enumeration",
2381 parser->scope, name);
2382 break;
2383 default:
2384 gcc_unreachable ();
2388 else if (parser->scope == global_namespace)
2390 switch (desired)
2392 case NLE_TYPE:
2393 error_at (location, "%<::%E%> is not a type", name);
2394 break;
2395 case NLE_CXX98:
2396 error_at (location, "%<::%E%> is not a class or namespace", name);
2397 break;
2398 case NLE_NOT_CXX98:
2399 error_at (location,
2400 "%<::%E%> is not a class, namespace, or enumeration",
2401 name);
2402 break;
2403 default:
2404 gcc_unreachable ();
2407 else
2409 switch (desired)
2411 case NLE_TYPE:
2412 error_at (location, "%qE is not a type", name);
2413 break;
2414 case NLE_CXX98:
2415 error_at (location, "%qE is not a class or namespace", name);
2416 break;
2417 case NLE_NOT_CXX98:
2418 error_at (location,
2419 "%qE is not a class, namespace, or enumeration", name);
2420 break;
2421 default:
2422 gcc_unreachable ();
2427 /* If we are parsing tentatively, remember that an error has occurred
2428 during this tentative parse. Returns true if the error was
2429 simulated; false if a message should be issued by the caller. */
2431 static bool
2432 cp_parser_simulate_error (cp_parser* parser)
2434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2436 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2437 return true;
2439 return false;
2442 /* Check for repeated decl-specifiers. */
2444 static void
2445 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2446 location_t location)
2448 int ds;
2450 for (ds = ds_first; ds != ds_last; ++ds)
2452 unsigned count = decl_specs->specs[ds];
2453 if (count < 2)
2454 continue;
2455 /* The "long" specifier is a special case because of "long long". */
2456 if (ds == ds_long)
2458 if (count > 2)
2459 error_at (location, "%<long long long%> is too long for GCC");
2460 else
2461 pedwarn_cxx98 (location, OPT_Wlong_long,
2462 "ISO C++ 1998 does not support %<long long%>");
2464 else if (count > 1)
2466 static const char *const decl_spec_names[] = {
2467 "signed",
2468 "unsigned",
2469 "short",
2470 "long",
2471 "const",
2472 "volatile",
2473 "restrict",
2474 "inline",
2475 "virtual",
2476 "explicit",
2477 "friend",
2478 "typedef",
2479 "constexpr",
2480 "__complex",
2481 "__thread"
2483 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2488 /* This function is called when a type is defined. If type
2489 definitions are forbidden at this point, an error message is
2490 issued. */
2492 static bool
2493 cp_parser_check_type_definition (cp_parser* parser)
2495 /* If types are forbidden here, issue a message. */
2496 if (parser->type_definition_forbidden_message)
2498 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2499 in the message need to be interpreted. */
2500 error (parser->type_definition_forbidden_message);
2501 return false;
2503 return true;
2506 /* This function is called when the DECLARATOR is processed. The TYPE
2507 was a type defined in the decl-specifiers. If it is invalid to
2508 define a type in the decl-specifiers for DECLARATOR, an error is
2509 issued. TYPE_LOCATION is the location of TYPE and is used
2510 for error reporting. */
2512 static void
2513 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2514 tree type, location_t type_location)
2516 /* [dcl.fct] forbids type definitions in return types.
2517 Unfortunately, it's not easy to know whether or not we are
2518 processing a return type until after the fact. */
2519 while (declarator
2520 && (declarator->kind == cdk_pointer
2521 || declarator->kind == cdk_reference
2522 || declarator->kind == cdk_ptrmem))
2523 declarator = declarator->declarator;
2524 if (declarator
2525 && declarator->kind == cdk_function)
2527 error_at (type_location,
2528 "new types may not be defined in a return type");
2529 inform (type_location,
2530 "(perhaps a semicolon is missing after the definition of %qT)",
2531 type);
2535 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2536 "<" in any valid C++ program. If the next token is indeed "<",
2537 issue a message warning the user about what appears to be an
2538 invalid attempt to form a template-id. LOCATION is the location
2539 of the type-specifier (TYPE) */
2541 static void
2542 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2543 tree type, location_t location)
2545 cp_token_position start = 0;
2547 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2549 if (TYPE_P (type))
2550 error_at (location, "%qT is not a template", type);
2551 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2552 error_at (location, "%qE is not a template", type);
2553 else
2554 error_at (location, "invalid template-id");
2555 /* Remember the location of the invalid "<". */
2556 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2557 start = cp_lexer_token_position (parser->lexer, true);
2558 /* Consume the "<". */
2559 cp_lexer_consume_token (parser->lexer);
2560 /* Parse the template arguments. */
2561 cp_parser_enclosed_template_argument_list (parser);
2562 /* Permanently remove the invalid template arguments so that
2563 this error message is not issued again. */
2564 if (start)
2565 cp_lexer_purge_tokens_after (parser->lexer, start);
2569 /* If parsing an integral constant-expression, issue an error message
2570 about the fact that THING appeared and return true. Otherwise,
2571 return false. In either case, set
2572 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2574 static bool
2575 cp_parser_non_integral_constant_expression (cp_parser *parser,
2576 non_integral_constant thing)
2578 parser->non_integral_constant_expression_p = true;
2579 if (parser->integral_constant_expression_p)
2581 if (!parser->allow_non_integral_constant_expression_p)
2583 const char *msg = NULL;
2584 switch (thing)
2586 case NIC_FLOAT:
2587 error ("floating-point literal "
2588 "cannot appear in a constant-expression");
2589 return true;
2590 case NIC_CAST:
2591 error ("a cast to a type other than an integral or "
2592 "enumeration type cannot appear in a "
2593 "constant-expression");
2594 return true;
2595 case NIC_TYPEID:
2596 error ("%<typeid%> operator "
2597 "cannot appear in a constant-expression");
2598 return true;
2599 case NIC_NCC:
2600 error ("non-constant compound literals "
2601 "cannot appear in a constant-expression");
2602 return true;
2603 case NIC_FUNC_CALL:
2604 error ("a function call "
2605 "cannot appear in a constant-expression");
2606 return true;
2607 case NIC_INC:
2608 error ("an increment "
2609 "cannot appear in a constant-expression");
2610 return true;
2611 case NIC_DEC:
2612 error ("an decrement "
2613 "cannot appear in a constant-expression");
2614 return true;
2615 case NIC_ARRAY_REF:
2616 error ("an array reference "
2617 "cannot appear in a constant-expression");
2618 return true;
2619 case NIC_ADDR_LABEL:
2620 error ("the address of a label "
2621 "cannot appear in a constant-expression");
2622 return true;
2623 case NIC_OVERLOADED:
2624 error ("calls to overloaded operators "
2625 "cannot appear in a constant-expression");
2626 return true;
2627 case NIC_ASSIGNMENT:
2628 error ("an assignment cannot appear in a constant-expression");
2629 return true;
2630 case NIC_COMMA:
2631 error ("a comma operator "
2632 "cannot appear in a constant-expression");
2633 return true;
2634 case NIC_CONSTRUCTOR:
2635 error ("a call to a constructor "
2636 "cannot appear in a constant-expression");
2637 return true;
2638 case NIC_THIS:
2639 msg = "this";
2640 break;
2641 case NIC_FUNC_NAME:
2642 msg = "__FUNCTION__";
2643 break;
2644 case NIC_PRETTY_FUNC:
2645 msg = "__PRETTY_FUNCTION__";
2646 break;
2647 case NIC_C99_FUNC:
2648 msg = "__func__";
2649 break;
2650 case NIC_VA_ARG:
2651 msg = "va_arg";
2652 break;
2653 case NIC_ARROW:
2654 msg = "->";
2655 break;
2656 case NIC_POINT:
2657 msg = ".";
2658 break;
2659 case NIC_STAR:
2660 msg = "*";
2661 break;
2662 case NIC_ADDR:
2663 msg = "&";
2664 break;
2665 case NIC_PREINCREMENT:
2666 msg = "++";
2667 break;
2668 case NIC_PREDECREMENT:
2669 msg = "--";
2670 break;
2671 case NIC_NEW:
2672 msg = "new";
2673 break;
2674 case NIC_DEL:
2675 msg = "delete";
2676 break;
2677 default:
2678 gcc_unreachable ();
2680 if (msg)
2681 error ("%qs cannot appear in a constant-expression", msg);
2682 return true;
2685 return false;
2688 /* Emit a diagnostic for an invalid type name. SCOPE is the
2689 qualifying scope (or NULL, if none) for ID. This function commits
2690 to the current active tentative parse, if any. (Otherwise, the
2691 problematic construct might be encountered again later, resulting
2692 in duplicate error messages.) LOCATION is the location of ID. */
2694 static void
2695 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2696 tree scope, tree id,
2697 location_t location)
2699 tree decl, old_scope;
2700 /* Try to lookup the identifier. */
2701 old_scope = parser->scope;
2702 parser->scope = scope;
2703 decl = cp_parser_lookup_name_simple (parser, id, location);
2704 parser->scope = old_scope;
2705 /* If the lookup found a template-name, it means that the user forgot
2706 to specify an argument list. Emit a useful error message. */
2707 if (TREE_CODE (decl) == TEMPLATE_DECL)
2708 error_at (location,
2709 "invalid use of template-name %qE without an argument list",
2710 decl);
2711 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2712 error_at (location, "invalid use of destructor %qD as a type", id);
2713 else if (TREE_CODE (decl) == TYPE_DECL)
2714 /* Something like 'unsigned A a;' */
2715 error_at (location, "invalid combination of multiple type-specifiers");
2716 else if (!parser->scope)
2718 /* Issue an error message. */
2719 error_at (location, "%qE does not name a type", id);
2720 /* If we're in a template class, it's possible that the user was
2721 referring to a type from a base class. For example:
2723 template <typename T> struct A { typedef T X; };
2724 template <typename T> struct B : public A<T> { X x; };
2726 The user should have said "typename A<T>::X". */
2727 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2728 inform (location, "C++0x %<constexpr%> only available with "
2729 "-std=c++0x or -std=gnu++0x");
2730 else if (processing_template_decl && current_class_type
2731 && TYPE_BINFO (current_class_type))
2733 tree b;
2735 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2737 b = TREE_CHAIN (b))
2739 tree base_type = BINFO_TYPE (b);
2740 if (CLASS_TYPE_P (base_type)
2741 && dependent_type_p (base_type))
2743 tree field;
2744 /* Go from a particular instantiation of the
2745 template (which will have an empty TYPE_FIELDs),
2746 to the main version. */
2747 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2748 for (field = TYPE_FIELDS (base_type);
2749 field;
2750 field = DECL_CHAIN (field))
2751 if (TREE_CODE (field) == TYPE_DECL
2752 && DECL_NAME (field) == id)
2754 inform (location,
2755 "(perhaps %<typename %T::%E%> was intended)",
2756 BINFO_TYPE (b), id);
2757 break;
2759 if (field)
2760 break;
2765 /* Here we diagnose qualified-ids where the scope is actually correct,
2766 but the identifier does not resolve to a valid type name. */
2767 else if (parser->scope != error_mark_node)
2769 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2770 error_at (location, "%qE in namespace %qE does not name a type",
2771 id, parser->scope);
2772 else if (CLASS_TYPE_P (parser->scope)
2773 && constructor_name_p (id, parser->scope))
2775 /* A<T>::A<T>() */
2776 error_at (location, "%<%T::%E%> names the constructor, not"
2777 " the type", parser->scope, id);
2778 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2779 error_at (location, "and %qT has no template constructors",
2780 parser->scope);
2782 else if (TYPE_P (parser->scope)
2783 && dependent_scope_p (parser->scope))
2784 error_at (location, "need %<typename%> before %<%T::%E%> because "
2785 "%qT is a dependent scope",
2786 parser->scope, id, parser->scope);
2787 else if (TYPE_P (parser->scope))
2788 error_at (location, "%qE in class %qT does not name a type",
2789 id, parser->scope);
2790 else
2791 gcc_unreachable ();
2793 cp_parser_commit_to_tentative_parse (parser);
2796 /* Check for a common situation where a type-name should be present,
2797 but is not, and issue a sensible error message. Returns true if an
2798 invalid type-name was detected.
2800 The situation handled by this function are variable declarations of the
2801 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2802 Usually, `ID' should name a type, but if we got here it means that it
2803 does not. We try to emit the best possible error message depending on
2804 how exactly the id-expression looks like. */
2806 static bool
2807 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2809 tree id;
2810 cp_token *token = cp_lexer_peek_token (parser->lexer);
2812 /* Avoid duplicate error about ambiguous lookup. */
2813 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2815 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2816 if (next->type == CPP_NAME && next->ambiguous_p)
2817 goto out;
2820 cp_parser_parse_tentatively (parser);
2821 id = cp_parser_id_expression (parser,
2822 /*template_keyword_p=*/false,
2823 /*check_dependency_p=*/true,
2824 /*template_p=*/NULL,
2825 /*declarator_p=*/true,
2826 /*optional_p=*/false);
2827 /* If the next token is a (, this is a function with no explicit return
2828 type, i.e. constructor, destructor or conversion op. */
2829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2830 || TREE_CODE (id) == TYPE_DECL)
2832 cp_parser_abort_tentative_parse (parser);
2833 return false;
2835 if (!cp_parser_parse_definitely (parser))
2836 return false;
2838 /* Emit a diagnostic for the invalid type. */
2839 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2840 id, token->location);
2841 out:
2842 /* If we aren't in the middle of a declarator (i.e. in a
2843 parameter-declaration-clause), skip to the end of the declaration;
2844 there's no point in trying to process it. */
2845 if (!parser->in_declarator_p)
2846 cp_parser_skip_to_end_of_block_or_statement (parser);
2847 return true;
2850 /* Consume tokens up to, and including, the next non-nested closing `)'.
2851 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2852 are doing error recovery. Returns -1 if OR_COMMA is true and we
2853 found an unnested comma. */
2855 static int
2856 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2857 bool recovering,
2858 bool or_comma,
2859 bool consume_paren)
2861 unsigned paren_depth = 0;
2862 unsigned brace_depth = 0;
2863 unsigned square_depth = 0;
2865 if (recovering && !or_comma
2866 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2867 return 0;
2869 while (true)
2871 cp_token * token = cp_lexer_peek_token (parser->lexer);
2873 switch (token->type)
2875 case CPP_EOF:
2876 case CPP_PRAGMA_EOL:
2877 /* If we've run out of tokens, then there is no closing `)'. */
2878 return 0;
2880 /* This is good for lambda expression capture-lists. */
2881 case CPP_OPEN_SQUARE:
2882 ++square_depth;
2883 break;
2884 case CPP_CLOSE_SQUARE:
2885 if (!square_depth--)
2886 return 0;
2887 break;
2889 case CPP_SEMICOLON:
2890 /* This matches the processing in skip_to_end_of_statement. */
2891 if (!brace_depth)
2892 return 0;
2893 break;
2895 case CPP_OPEN_BRACE:
2896 ++brace_depth;
2897 break;
2898 case CPP_CLOSE_BRACE:
2899 if (!brace_depth--)
2900 return 0;
2901 break;
2903 case CPP_COMMA:
2904 if (recovering && or_comma && !brace_depth && !paren_depth
2905 && !square_depth)
2906 return -1;
2907 break;
2909 case CPP_OPEN_PAREN:
2910 if (!brace_depth)
2911 ++paren_depth;
2912 break;
2914 case CPP_CLOSE_PAREN:
2915 if (!brace_depth && !paren_depth--)
2917 if (consume_paren)
2918 cp_lexer_consume_token (parser->lexer);
2919 return 1;
2921 break;
2923 default:
2924 break;
2927 /* Consume the token. */
2928 cp_lexer_consume_token (parser->lexer);
2932 /* Consume tokens until we reach the end of the current statement.
2933 Normally, that will be just before consuming a `;'. However, if a
2934 non-nested `}' comes first, then we stop before consuming that. */
2936 static void
2937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2939 unsigned nesting_depth = 0;
2941 while (true)
2943 cp_token *token = cp_lexer_peek_token (parser->lexer);
2945 switch (token->type)
2947 case CPP_EOF:
2948 case CPP_PRAGMA_EOL:
2949 /* If we've run out of tokens, stop. */
2950 return;
2952 case CPP_SEMICOLON:
2953 /* If the next token is a `;', we have reached the end of the
2954 statement. */
2955 if (!nesting_depth)
2956 return;
2957 break;
2959 case CPP_CLOSE_BRACE:
2960 /* If this is a non-nested '}', stop before consuming it.
2961 That way, when confronted with something like:
2963 { 3 + }
2965 we stop before consuming the closing '}', even though we
2966 have not yet reached a `;'. */
2967 if (nesting_depth == 0)
2968 return;
2970 /* If it is the closing '}' for a block that we have
2971 scanned, stop -- but only after consuming the token.
2972 That way given:
2974 void f g () { ... }
2975 typedef int I;
2977 we will stop after the body of the erroneously declared
2978 function, but before consuming the following `typedef'
2979 declaration. */
2980 if (--nesting_depth == 0)
2982 cp_lexer_consume_token (parser->lexer);
2983 return;
2986 case CPP_OPEN_BRACE:
2987 ++nesting_depth;
2988 break;
2990 default:
2991 break;
2994 /* Consume the token. */
2995 cp_lexer_consume_token (parser->lexer);
2999 /* This function is called at the end of a statement or declaration.
3000 If the next token is a semicolon, it is consumed; otherwise, error
3001 recovery is attempted. */
3003 static void
3004 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3006 /* Look for the trailing `;'. */
3007 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3009 /* If there is additional (erroneous) input, skip to the end of
3010 the statement. */
3011 cp_parser_skip_to_end_of_statement (parser);
3012 /* If the next token is now a `;', consume it. */
3013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3014 cp_lexer_consume_token (parser->lexer);
3018 /* Skip tokens until we have consumed an entire block, or until we
3019 have consumed a non-nested `;'. */
3021 static void
3022 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3024 int nesting_depth = 0;
3026 while (nesting_depth >= 0)
3028 cp_token *token = cp_lexer_peek_token (parser->lexer);
3030 switch (token->type)
3032 case CPP_EOF:
3033 case CPP_PRAGMA_EOL:
3034 /* If we've run out of tokens, stop. */
3035 return;
3037 case CPP_SEMICOLON:
3038 /* Stop if this is an unnested ';'. */
3039 if (!nesting_depth)
3040 nesting_depth = -1;
3041 break;
3043 case CPP_CLOSE_BRACE:
3044 /* Stop if this is an unnested '}', or closes the outermost
3045 nesting level. */
3046 nesting_depth--;
3047 if (nesting_depth < 0)
3048 return;
3049 if (!nesting_depth)
3050 nesting_depth = -1;
3051 break;
3053 case CPP_OPEN_BRACE:
3054 /* Nest. */
3055 nesting_depth++;
3056 break;
3058 default:
3059 break;
3062 /* Consume the token. */
3063 cp_lexer_consume_token (parser->lexer);
3067 /* Skip tokens until a non-nested closing curly brace is the next
3068 token, or there are no more tokens. Return true in the first case,
3069 false otherwise. */
3071 static bool
3072 cp_parser_skip_to_closing_brace (cp_parser *parser)
3074 unsigned nesting_depth = 0;
3076 while (true)
3078 cp_token *token = cp_lexer_peek_token (parser->lexer);
3080 switch (token->type)
3082 case CPP_EOF:
3083 case CPP_PRAGMA_EOL:
3084 /* If we've run out of tokens, stop. */
3085 return false;
3087 case CPP_CLOSE_BRACE:
3088 /* If the next token is a non-nested `}', then we have reached
3089 the end of the current block. */
3090 if (nesting_depth-- == 0)
3091 return true;
3092 break;
3094 case CPP_OPEN_BRACE:
3095 /* If it the next token is a `{', then we are entering a new
3096 block. Consume the entire block. */
3097 ++nesting_depth;
3098 break;
3100 default:
3101 break;
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser->lexer);
3109 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3110 parameter is the PRAGMA token, allowing us to purge the entire pragma
3111 sequence. */
3113 static void
3114 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3116 cp_token *token;
3118 parser->lexer->in_pragma = false;
3121 token = cp_lexer_consume_token (parser->lexer);
3122 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3124 /* Ensure that the pragma is not parsed again. */
3125 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3128 /* Require pragma end of line, resyncing with it as necessary. The
3129 arguments are as for cp_parser_skip_to_pragma_eol. */
3131 static void
3132 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3134 parser->lexer->in_pragma = false;
3135 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3139 /* This is a simple wrapper around make_typename_type. When the id is
3140 an unresolved identifier node, we can provide a superior diagnostic
3141 using cp_parser_diagnose_invalid_type_name. */
3143 static tree
3144 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3145 tree id, location_t id_location)
3147 tree result;
3148 if (TREE_CODE (id) == IDENTIFIER_NODE)
3150 result = make_typename_type (scope, id, typename_type,
3151 /*complain=*/tf_none);
3152 if (result == error_mark_node)
3153 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3154 return result;
3156 return make_typename_type (scope, id, typename_type, tf_error);
3159 /* This is a wrapper around the
3160 make_{pointer,ptrmem,reference}_declarator functions that decides
3161 which one to call based on the CODE and CLASS_TYPE arguments. The
3162 CODE argument should be one of the values returned by
3163 cp_parser_ptr_operator. */
3164 static cp_declarator *
3165 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3166 cp_cv_quals cv_qualifiers,
3167 cp_declarator *target)
3169 if (code == ERROR_MARK)
3170 return cp_error_declarator;
3172 if (code == INDIRECT_REF)
3173 if (class_type == NULL_TREE)
3174 return make_pointer_declarator (cv_qualifiers, target);
3175 else
3176 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3177 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3178 return make_reference_declarator (cv_qualifiers, target, false);
3179 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3180 return make_reference_declarator (cv_qualifiers, target, true);
3181 gcc_unreachable ();
3184 /* Create a new C++ parser. */
3186 static cp_parser *
3187 cp_parser_new (void)
3189 cp_parser *parser;
3190 cp_lexer *lexer;
3191 unsigned i;
3193 /* cp_lexer_new_main is called before doing GC allocation because
3194 cp_lexer_new_main might load a PCH file. */
3195 lexer = cp_lexer_new_main ();
3197 /* Initialize the binops_by_token so that we can get the tree
3198 directly from the token. */
3199 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3200 binops_by_token[binops[i].token_type] = binops[i];
3202 parser = ggc_alloc_cleared_cp_parser ();
3203 parser->lexer = lexer;
3204 parser->context = cp_parser_context_new (NULL);
3206 /* For now, we always accept GNU extensions. */
3207 parser->allow_gnu_extensions_p = 1;
3209 /* The `>' token is a greater-than operator, not the end of a
3210 template-id. */
3211 parser->greater_than_is_operator_p = true;
3213 parser->default_arg_ok_p = true;
3215 /* We are not parsing a constant-expression. */
3216 parser->integral_constant_expression_p = false;
3217 parser->allow_non_integral_constant_expression_p = false;
3218 parser->non_integral_constant_expression_p = false;
3220 /* Local variable names are not forbidden. */
3221 parser->local_variables_forbidden_p = false;
3223 /* We are not processing an `extern "C"' declaration. */
3224 parser->in_unbraced_linkage_specification_p = false;
3226 /* We are not processing a declarator. */
3227 parser->in_declarator_p = false;
3229 /* We are not processing a template-argument-list. */
3230 parser->in_template_argument_list_p = false;
3232 /* We are not in an iteration statement. */
3233 parser->in_statement = 0;
3235 /* We are not in a switch statement. */
3236 parser->in_switch_statement_p = false;
3238 /* We are not parsing a type-id inside an expression. */
3239 parser->in_type_id_in_expr_p = false;
3241 /* Declarations aren't implicitly extern "C". */
3242 parser->implicit_extern_c = false;
3244 /* String literals should be translated to the execution character set. */
3245 parser->translate_strings_p = true;
3247 /* We are not parsing a function body. */
3248 parser->in_function_body = false;
3250 /* We can correct until told otherwise. */
3251 parser->colon_corrects_to_scope_p = true;
3253 /* The unparsed function queue is empty. */
3254 push_unparsed_function_queues (parser);
3256 /* There are no classes being defined. */
3257 parser->num_classes_being_defined = 0;
3259 /* No template parameters apply. */
3260 parser->num_template_parameter_lists = 0;
3262 return parser;
3265 /* Create a cp_lexer structure which will emit the tokens in CACHE
3266 and push it onto the parser's lexer stack. This is used for delayed
3267 parsing of in-class method bodies and default arguments, and should
3268 not be confused with tentative parsing. */
3269 static void
3270 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3272 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3273 lexer->next = parser->lexer;
3274 parser->lexer = lexer;
3276 /* Move the current source position to that of the first token in the
3277 new lexer. */
3278 cp_lexer_set_source_position_from_token (lexer->next_token);
3281 /* Pop the top lexer off the parser stack. This is never used for the
3282 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3283 static void
3284 cp_parser_pop_lexer (cp_parser *parser)
3286 cp_lexer *lexer = parser->lexer;
3287 parser->lexer = lexer->next;
3288 cp_lexer_destroy (lexer);
3290 /* Put the current source position back where it was before this
3291 lexer was pushed. */
3292 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3295 /* Lexical conventions [gram.lex] */
3297 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3298 identifier. */
3300 static tree
3301 cp_parser_identifier (cp_parser* parser)
3303 cp_token *token;
3305 /* Look for the identifier. */
3306 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3307 /* Return the value. */
3308 return token ? token->u.value : error_mark_node;
3311 /* Parse a sequence of adjacent string constants. Returns a
3312 TREE_STRING representing the combined, nul-terminated string
3313 constant. If TRANSLATE is true, translate the string to the
3314 execution character set. If WIDE_OK is true, a wide string is
3315 invalid here.
3317 C++98 [lex.string] says that if a narrow string literal token is
3318 adjacent to a wide string literal token, the behavior is undefined.
3319 However, C99 6.4.5p4 says that this results in a wide string literal.
3320 We follow C99 here, for consistency with the C front end.
3322 This code is largely lifted from lex_string() in c-lex.c.
3324 FUTURE: ObjC++ will need to handle @-strings here. */
3325 static tree
3326 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3328 tree value;
3329 size_t count;
3330 struct obstack str_ob;
3331 cpp_string str, istr, *strs;
3332 cp_token *tok;
3333 enum cpp_ttype type;
3335 tok = cp_lexer_peek_token (parser->lexer);
3336 if (!cp_parser_is_string_literal (tok))
3338 cp_parser_error (parser, "expected string-literal");
3339 return error_mark_node;
3342 type = tok->type;
3344 /* Try to avoid the overhead of creating and destroying an obstack
3345 for the common case of just one string. */
3346 if (!cp_parser_is_string_literal
3347 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3349 cp_lexer_consume_token (parser->lexer);
3351 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3352 str.len = TREE_STRING_LENGTH (tok->u.value);
3353 count = 1;
3355 strs = &str;
3357 else
3359 gcc_obstack_init (&str_ob);
3360 count = 0;
3364 cp_lexer_consume_token (parser->lexer);
3365 count++;
3366 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3367 str.len = TREE_STRING_LENGTH (tok->u.value);
3369 if (type != tok->type)
3371 if (type == CPP_STRING)
3372 type = tok->type;
3373 else if (tok->type != CPP_STRING)
3374 error_at (tok->location,
3375 "unsupported non-standard concatenation "
3376 "of string literals");
3379 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3381 tok = cp_lexer_peek_token (parser->lexer);
3383 while (cp_parser_is_string_literal (tok));
3385 strs = (cpp_string *) obstack_finish (&str_ob);
3388 if (type != CPP_STRING && !wide_ok)
3390 cp_parser_error (parser, "a wide string is invalid in this context");
3391 type = CPP_STRING;
3394 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3395 (parse_in, strs, count, &istr, type))
3397 value = build_string (istr.len, (const char *)istr.text);
3398 free (CONST_CAST (unsigned char *, istr.text));
3400 switch (type)
3402 default:
3403 case CPP_STRING:
3404 case CPP_UTF8STRING:
3405 TREE_TYPE (value) = char_array_type_node;
3406 break;
3407 case CPP_STRING16:
3408 TREE_TYPE (value) = char16_array_type_node;
3409 break;
3410 case CPP_STRING32:
3411 TREE_TYPE (value) = char32_array_type_node;
3412 break;
3413 case CPP_WSTRING:
3414 TREE_TYPE (value) = wchar_array_type_node;
3415 break;
3418 value = fix_string_type (value);
3420 else
3421 /* cpp_interpret_string has issued an error. */
3422 value = error_mark_node;
3424 if (count > 1)
3425 obstack_free (&str_ob, 0);
3427 return value;
3431 /* Basic concepts [gram.basic] */
3433 /* Parse a translation-unit.
3435 translation-unit:
3436 declaration-seq [opt]
3438 Returns TRUE if all went well. */
3440 static bool
3441 cp_parser_translation_unit (cp_parser* parser)
3443 /* The address of the first non-permanent object on the declarator
3444 obstack. */
3445 static void *declarator_obstack_base;
3447 bool success;
3449 /* Create the declarator obstack, if necessary. */
3450 if (!cp_error_declarator)
3452 gcc_obstack_init (&declarator_obstack);
3453 /* Create the error declarator. */
3454 cp_error_declarator = make_declarator (cdk_error);
3455 /* Create the empty parameter list. */
3456 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3457 /* Remember where the base of the declarator obstack lies. */
3458 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3461 cp_parser_declaration_seq_opt (parser);
3463 /* If there are no tokens left then all went well. */
3464 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3466 /* Get rid of the token array; we don't need it any more. */
3467 cp_lexer_destroy (parser->lexer);
3468 parser->lexer = NULL;
3470 /* This file might have been a context that's implicitly extern
3471 "C". If so, pop the lang context. (Only relevant for PCH.) */
3472 if (parser->implicit_extern_c)
3474 pop_lang_context ();
3475 parser->implicit_extern_c = false;
3478 /* Finish up. */
3479 finish_translation_unit ();
3481 success = true;
3483 else
3485 cp_parser_error (parser, "expected declaration");
3486 success = false;
3489 /* Make sure the declarator obstack was fully cleaned up. */
3490 gcc_assert (obstack_next_free (&declarator_obstack)
3491 == declarator_obstack_base);
3493 /* All went well. */
3494 return success;
3497 /* Expressions [gram.expr] */
3499 /* Parse a primary-expression.
3501 primary-expression:
3502 literal
3503 this
3504 ( expression )
3505 id-expression
3507 GNU Extensions:
3509 primary-expression:
3510 ( compound-statement )
3511 __builtin_va_arg ( assignment-expression , type-id )
3512 __builtin_offsetof ( type-id , offsetof-expression )
3514 C++ Extensions:
3515 __has_nothrow_assign ( type-id )
3516 __has_nothrow_constructor ( type-id )
3517 __has_nothrow_copy ( type-id )
3518 __has_trivial_assign ( type-id )
3519 __has_trivial_constructor ( type-id )
3520 __has_trivial_copy ( type-id )
3521 __has_trivial_destructor ( type-id )
3522 __has_virtual_destructor ( type-id )
3523 __is_abstract ( type-id )
3524 __is_base_of ( type-id , type-id )
3525 __is_class ( type-id )
3526 __is_convertible_to ( type-id , type-id )
3527 __is_empty ( type-id )
3528 __is_enum ( type-id )
3529 __is_pod ( type-id )
3530 __is_polymorphic ( type-id )
3531 __is_union ( type-id )
3533 Objective-C++ Extension:
3535 primary-expression:
3536 objc-expression
3538 literal:
3539 __null
3541 ADDRESS_P is true iff this expression was immediately preceded by
3542 "&" and therefore might denote a pointer-to-member. CAST_P is true
3543 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3544 true iff this expression is a template argument.
3546 Returns a representation of the expression. Upon return, *IDK
3547 indicates what kind of id-expression (if any) was present. */
3549 static tree
3550 cp_parser_primary_expression (cp_parser *parser,
3551 bool address_p,
3552 bool cast_p,
3553 bool template_arg_p,
3554 cp_id_kind *idk)
3556 cp_token *token = NULL;
3558 /* Assume the primary expression is not an id-expression. */
3559 *idk = CP_ID_KIND_NONE;
3561 /* Peek at the next token. */
3562 token = cp_lexer_peek_token (parser->lexer);
3563 switch (token->type)
3565 /* literal:
3566 integer-literal
3567 character-literal
3568 floating-literal
3569 string-literal
3570 boolean-literal */
3571 case CPP_CHAR:
3572 case CPP_CHAR16:
3573 case CPP_CHAR32:
3574 case CPP_WCHAR:
3575 case CPP_NUMBER:
3576 token = cp_lexer_consume_token (parser->lexer);
3577 if (TREE_CODE (token->u.value) == FIXED_CST)
3579 error_at (token->location,
3580 "fixed-point types not supported in C++");
3581 return error_mark_node;
3583 /* Floating-point literals are only allowed in an integral
3584 constant expression if they are cast to an integral or
3585 enumeration type. */
3586 if (TREE_CODE (token->u.value) == REAL_CST
3587 && parser->integral_constant_expression_p
3588 && pedantic)
3590 /* CAST_P will be set even in invalid code like "int(2.7 +
3591 ...)". Therefore, we have to check that the next token
3592 is sure to end the cast. */
3593 if (cast_p)
3595 cp_token *next_token;
3597 next_token = cp_lexer_peek_token (parser->lexer);
3598 if (/* The comma at the end of an
3599 enumerator-definition. */
3600 next_token->type != CPP_COMMA
3601 /* The curly brace at the end of an enum-specifier. */
3602 && next_token->type != CPP_CLOSE_BRACE
3603 /* The end of a statement. */
3604 && next_token->type != CPP_SEMICOLON
3605 /* The end of the cast-expression. */
3606 && next_token->type != CPP_CLOSE_PAREN
3607 /* The end of an array bound. */
3608 && next_token->type != CPP_CLOSE_SQUARE
3609 /* The closing ">" in a template-argument-list. */
3610 && (next_token->type != CPP_GREATER
3611 || parser->greater_than_is_operator_p)
3612 /* C++0x only: A ">>" treated like two ">" tokens,
3613 in a template-argument-list. */
3614 && (next_token->type != CPP_RSHIFT
3615 || (cxx_dialect == cxx98)
3616 || parser->greater_than_is_operator_p))
3617 cast_p = false;
3620 /* If we are within a cast, then the constraint that the
3621 cast is to an integral or enumeration type will be
3622 checked at that point. If we are not within a cast, then
3623 this code is invalid. */
3624 if (!cast_p)
3625 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3627 return token->u.value;
3629 case CPP_STRING:
3630 case CPP_STRING16:
3631 case CPP_STRING32:
3632 case CPP_WSTRING:
3633 case CPP_UTF8STRING:
3634 /* ??? Should wide strings be allowed when parser->translate_strings_p
3635 is false (i.e. in attributes)? If not, we can kill the third
3636 argument to cp_parser_string_literal. */
3637 return cp_parser_string_literal (parser,
3638 parser->translate_strings_p,
3639 true);
3641 case CPP_OPEN_PAREN:
3643 tree expr;
3644 bool saved_greater_than_is_operator_p;
3646 /* Consume the `('. */
3647 cp_lexer_consume_token (parser->lexer);
3648 /* Within a parenthesized expression, a `>' token is always
3649 the greater-than operator. */
3650 saved_greater_than_is_operator_p
3651 = parser->greater_than_is_operator_p;
3652 parser->greater_than_is_operator_p = true;
3653 /* If we see `( { ' then we are looking at the beginning of
3654 a GNU statement-expression. */
3655 if (cp_parser_allow_gnu_extensions_p (parser)
3656 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3658 /* Statement-expressions are not allowed by the standard. */
3659 pedwarn (token->location, OPT_pedantic,
3660 "ISO C++ forbids braced-groups within expressions");
3662 /* And they're not allowed outside of a function-body; you
3663 cannot, for example, write:
3665 int i = ({ int j = 3; j + 1; });
3667 at class or namespace scope. */
3668 if (!parser->in_function_body
3669 || parser->in_template_argument_list_p)
3671 error_at (token->location,
3672 "statement-expressions are not allowed outside "
3673 "functions nor in template-argument lists");
3674 cp_parser_skip_to_end_of_block_or_statement (parser);
3675 expr = error_mark_node;
3677 else
3679 /* Start the statement-expression. */
3680 expr = begin_stmt_expr ();
3681 /* Parse the compound-statement. */
3682 cp_parser_compound_statement (parser, expr, false);
3683 /* Finish up. */
3684 expr = finish_stmt_expr (expr, false);
3687 else
3689 /* Parse the parenthesized expression. */
3690 expr = cp_parser_expression (parser, cast_p, idk);
3691 /* Let the front end know that this expression was
3692 enclosed in parentheses. This matters in case, for
3693 example, the expression is of the form `A::B', since
3694 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3695 not. */
3696 finish_parenthesized_expr (expr);
3698 /* The `>' token might be the end of a template-id or
3699 template-parameter-list now. */
3700 parser->greater_than_is_operator_p
3701 = saved_greater_than_is_operator_p;
3702 /* Consume the `)'. */
3703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3704 cp_parser_skip_to_end_of_statement (parser);
3706 return expr;
3709 case CPP_OPEN_SQUARE:
3710 if (c_dialect_objc ())
3711 /* We have an Objective-C++ message. */
3712 return cp_parser_objc_expression (parser);
3713 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3714 return cp_parser_lambda_expression (parser);
3716 case CPP_OBJC_STRING:
3717 if (c_dialect_objc ())
3718 /* We have an Objective-C++ string literal. */
3719 return cp_parser_objc_expression (parser);
3720 cp_parser_error (parser, "expected primary-expression");
3721 return error_mark_node;
3723 case CPP_KEYWORD:
3724 switch (token->keyword)
3726 /* These two are the boolean literals. */
3727 case RID_TRUE:
3728 cp_lexer_consume_token (parser->lexer);
3729 return boolean_true_node;
3730 case RID_FALSE:
3731 cp_lexer_consume_token (parser->lexer);
3732 return boolean_false_node;
3734 /* The `__null' literal. */
3735 case RID_NULL:
3736 cp_lexer_consume_token (parser->lexer);
3737 return null_node;
3739 /* The `nullptr' literal. */
3740 case RID_NULLPTR:
3741 cp_lexer_consume_token (parser->lexer);
3742 return nullptr_node;
3744 /* Recognize the `this' keyword. */
3745 case RID_THIS:
3746 cp_lexer_consume_token (parser->lexer);
3747 if (parser->local_variables_forbidden_p)
3749 error_at (token->location,
3750 "%<this%> may not be used in this context");
3751 return error_mark_node;
3753 /* Pointers cannot appear in constant-expressions. */
3754 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3755 return error_mark_node;
3756 return finish_this_expr ();
3758 /* The `operator' keyword can be the beginning of an
3759 id-expression. */
3760 case RID_OPERATOR:
3761 goto id_expression;
3763 case RID_FUNCTION_NAME:
3764 case RID_PRETTY_FUNCTION_NAME:
3765 case RID_C99_FUNCTION_NAME:
3767 non_integral_constant name;
3769 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3770 __func__ are the names of variables -- but they are
3771 treated specially. Therefore, they are handled here,
3772 rather than relying on the generic id-expression logic
3773 below. Grammatically, these names are id-expressions.
3775 Consume the token. */
3776 token = cp_lexer_consume_token (parser->lexer);
3778 switch (token->keyword)
3780 case RID_FUNCTION_NAME:
3781 name = NIC_FUNC_NAME;
3782 break;
3783 case RID_PRETTY_FUNCTION_NAME:
3784 name = NIC_PRETTY_FUNC;
3785 break;
3786 case RID_C99_FUNCTION_NAME:
3787 name = NIC_C99_FUNC;
3788 break;
3789 default:
3790 gcc_unreachable ();
3793 if (cp_parser_non_integral_constant_expression (parser, name))
3794 return error_mark_node;
3796 /* Look up the name. */
3797 return finish_fname (token->u.value);
3800 case RID_VA_ARG:
3802 tree expression;
3803 tree type;
3805 /* The `__builtin_va_arg' construct is used to handle
3806 `va_arg'. Consume the `__builtin_va_arg' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Look for the opening `('. */
3809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3810 /* Now, parse the assignment-expression. */
3811 expression = cp_parser_assignment_expression (parser,
3812 /*cast_p=*/false, NULL);
3813 /* Look for the `,'. */
3814 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3815 /* Parse the type-id. */
3816 type = cp_parser_type_id (parser);
3817 /* Look for the closing `)'. */
3818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3819 /* Using `va_arg' in a constant-expression is not
3820 allowed. */
3821 if (cp_parser_non_integral_constant_expression (parser,
3822 NIC_VA_ARG))
3823 return error_mark_node;
3824 return build_x_va_arg (expression, type);
3827 case RID_OFFSETOF:
3828 return cp_parser_builtin_offsetof (parser);
3830 case RID_HAS_NOTHROW_ASSIGN:
3831 case RID_HAS_NOTHROW_CONSTRUCTOR:
3832 case RID_HAS_NOTHROW_COPY:
3833 case RID_HAS_TRIVIAL_ASSIGN:
3834 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3835 case RID_HAS_TRIVIAL_COPY:
3836 case RID_HAS_TRIVIAL_DESTRUCTOR:
3837 case RID_HAS_VIRTUAL_DESTRUCTOR:
3838 case RID_IS_ABSTRACT:
3839 case RID_IS_BASE_OF:
3840 case RID_IS_CLASS:
3841 case RID_IS_CONVERTIBLE_TO:
3842 case RID_IS_EMPTY:
3843 case RID_IS_ENUM:
3844 case RID_IS_POD:
3845 case RID_IS_POLYMORPHIC:
3846 case RID_IS_STD_LAYOUT:
3847 case RID_IS_TRIVIAL:
3848 case RID_IS_UNION:
3849 case RID_IS_LITERAL_TYPE:
3850 return cp_parser_trait_expr (parser, token->keyword);
3852 /* Objective-C++ expressions. */
3853 case RID_AT_ENCODE:
3854 case RID_AT_PROTOCOL:
3855 case RID_AT_SELECTOR:
3856 return cp_parser_objc_expression (parser);
3858 case RID_TEMPLATE:
3859 if (parser->in_function_body
3860 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861 == CPP_LESS))
3863 error_at (token->location,
3864 "a template declaration cannot appear at block scope");
3865 cp_parser_skip_to_end_of_block_or_statement (parser);
3866 return error_mark_node;
3868 default:
3869 cp_parser_error (parser, "expected primary-expression");
3870 return error_mark_node;
3873 /* An id-expression can start with either an identifier, a
3874 `::' as the beginning of a qualified-id, or the "operator"
3875 keyword. */
3876 case CPP_NAME:
3877 case CPP_SCOPE:
3878 case CPP_TEMPLATE_ID:
3879 case CPP_NESTED_NAME_SPECIFIER:
3881 tree id_expression;
3882 tree decl;
3883 const char *error_msg;
3884 bool template_p;
3885 bool done;
3886 cp_token *id_expr_token;
3888 id_expression:
3889 /* Parse the id-expression. */
3890 id_expression
3891 = cp_parser_id_expression (parser,
3892 /*template_keyword_p=*/false,
3893 /*check_dependency_p=*/true,
3894 &template_p,
3895 /*declarator_p=*/false,
3896 /*optional_p=*/false);
3897 if (id_expression == error_mark_node)
3898 return error_mark_node;
3899 id_expr_token = token;
3900 token = cp_lexer_peek_token (parser->lexer);
3901 done = (token->type != CPP_OPEN_SQUARE
3902 && token->type != CPP_OPEN_PAREN
3903 && token->type != CPP_DOT
3904 && token->type != CPP_DEREF
3905 && token->type != CPP_PLUS_PLUS
3906 && token->type != CPP_MINUS_MINUS);
3907 /* If we have a template-id, then no further lookup is
3908 required. If the template-id was for a template-class, we
3909 will sometimes have a TYPE_DECL at this point. */
3910 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3911 || TREE_CODE (id_expression) == TYPE_DECL)
3912 decl = id_expression;
3913 /* Look up the name. */
3914 else
3916 tree ambiguous_decls;
3918 /* If we already know that this lookup is ambiguous, then
3919 we've already issued an error message; there's no reason
3920 to check again. */
3921 if (id_expr_token->type == CPP_NAME
3922 && id_expr_token->ambiguous_p)
3924 cp_parser_simulate_error (parser);
3925 return error_mark_node;
3928 decl = cp_parser_lookup_name (parser, id_expression,
3929 none_type,
3930 template_p,
3931 /*is_namespace=*/false,
3932 /*check_dependency=*/true,
3933 &ambiguous_decls,
3934 id_expr_token->location);
3935 /* If the lookup was ambiguous, an error will already have
3936 been issued. */
3937 if (ambiguous_decls)
3938 return error_mark_node;
3940 /* In Objective-C++, we may have an Objective-C 2.0
3941 dot-syntax for classes here. */
3942 if (c_dialect_objc ()
3943 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3944 && TREE_CODE (decl) == TYPE_DECL
3945 && objc_is_class_name (decl))
3947 tree component;
3948 cp_lexer_consume_token (parser->lexer);
3949 component = cp_parser_identifier (parser);
3950 if (component == error_mark_node)
3951 return error_mark_node;
3953 return objc_build_class_component_ref (id_expression, component);
3956 /* In Objective-C++, an instance variable (ivar) may be preferred
3957 to whatever cp_parser_lookup_name() found. */
3958 decl = objc_lookup_ivar (decl, id_expression);
3960 /* If name lookup gives us a SCOPE_REF, then the
3961 qualifying scope was dependent. */
3962 if (TREE_CODE (decl) == SCOPE_REF)
3964 /* At this point, we do not know if DECL is a valid
3965 integral constant expression. We assume that it is
3966 in fact such an expression, so that code like:
3968 template <int N> struct A {
3969 int a[B<N>::i];
3972 is accepted. At template-instantiation time, we
3973 will check that B<N>::i is actually a constant. */
3974 return decl;
3976 /* Check to see if DECL is a local variable in a context
3977 where that is forbidden. */
3978 if (parser->local_variables_forbidden_p
3979 && local_variable_p (decl))
3981 /* It might be that we only found DECL because we are
3982 trying to be generous with pre-ISO scoping rules.
3983 For example, consider:
3985 int i;
3986 void g() {
3987 for (int i = 0; i < 10; ++i) {}
3988 extern void f(int j = i);
3991 Here, name look up will originally find the out
3992 of scope `i'. We need to issue a warning message,
3993 but then use the global `i'. */
3994 decl = check_for_out_of_scope_variable (decl);
3995 if (local_variable_p (decl))
3997 error_at (id_expr_token->location,
3998 "local variable %qD may not appear in this context",
3999 decl);
4000 return error_mark_node;
4005 decl = (finish_id_expression
4006 (id_expression, decl, parser->scope,
4007 idk,
4008 parser->integral_constant_expression_p,
4009 parser->allow_non_integral_constant_expression_p,
4010 &parser->non_integral_constant_expression_p,
4011 template_p, done, address_p,
4012 template_arg_p,
4013 &error_msg,
4014 id_expr_token->location));
4015 if (error_msg)
4016 cp_parser_error (parser, error_msg);
4017 return decl;
4020 /* Anything else is an error. */
4021 default:
4022 cp_parser_error (parser, "expected primary-expression");
4023 return error_mark_node;
4027 /* Parse an id-expression.
4029 id-expression:
4030 unqualified-id
4031 qualified-id
4033 qualified-id:
4034 :: [opt] nested-name-specifier template [opt] unqualified-id
4035 :: identifier
4036 :: operator-function-id
4037 :: template-id
4039 Return a representation of the unqualified portion of the
4040 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4041 a `::' or nested-name-specifier.
4043 Often, if the id-expression was a qualified-id, the caller will
4044 want to make a SCOPE_REF to represent the qualified-id. This
4045 function does not do this in order to avoid wastefully creating
4046 SCOPE_REFs when they are not required.
4048 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4049 `template' keyword.
4051 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4052 uninstantiated templates.
4054 If *TEMPLATE_P is non-NULL, it is set to true iff the
4055 `template' keyword is used to explicitly indicate that the entity
4056 named is a template.
4058 If DECLARATOR_P is true, the id-expression is appearing as part of
4059 a declarator, rather than as part of an expression. */
4061 static tree
4062 cp_parser_id_expression (cp_parser *parser,
4063 bool template_keyword_p,
4064 bool check_dependency_p,
4065 bool *template_p,
4066 bool declarator_p,
4067 bool optional_p)
4069 bool global_scope_p;
4070 bool nested_name_specifier_p;
4072 /* Assume the `template' keyword was not used. */
4073 if (template_p)
4074 *template_p = template_keyword_p;
4076 /* Look for the optional `::' operator. */
4077 global_scope_p
4078 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4079 != NULL_TREE);
4080 /* Look for the optional nested-name-specifier. */
4081 nested_name_specifier_p
4082 = (cp_parser_nested_name_specifier_opt (parser,
4083 /*typename_keyword_p=*/false,
4084 check_dependency_p,
4085 /*type_p=*/false,
4086 declarator_p)
4087 != NULL_TREE);
4088 /* If there is a nested-name-specifier, then we are looking at
4089 the first qualified-id production. */
4090 if (nested_name_specifier_p)
4092 tree saved_scope;
4093 tree saved_object_scope;
4094 tree saved_qualifying_scope;
4095 tree unqualified_id;
4096 bool is_template;
4098 /* See if the next token is the `template' keyword. */
4099 if (!template_p)
4100 template_p = &is_template;
4101 *template_p = cp_parser_optional_template_keyword (parser);
4102 /* Name lookup we do during the processing of the
4103 unqualified-id might obliterate SCOPE. */
4104 saved_scope = parser->scope;
4105 saved_object_scope = parser->object_scope;
4106 saved_qualifying_scope = parser->qualifying_scope;
4107 /* Process the final unqualified-id. */
4108 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4109 check_dependency_p,
4110 declarator_p,
4111 /*optional_p=*/false);
4112 /* Restore the SAVED_SCOPE for our caller. */
4113 parser->scope = saved_scope;
4114 parser->object_scope = saved_object_scope;
4115 parser->qualifying_scope = saved_qualifying_scope;
4117 return unqualified_id;
4119 /* Otherwise, if we are in global scope, then we are looking at one
4120 of the other qualified-id productions. */
4121 else if (global_scope_p)
4123 cp_token *token;
4124 tree id;
4126 /* Peek at the next token. */
4127 token = cp_lexer_peek_token (parser->lexer);
4129 /* If it's an identifier, and the next token is not a "<", then
4130 we can avoid the template-id case. This is an optimization
4131 for this common case. */
4132 if (token->type == CPP_NAME
4133 && !cp_parser_nth_token_starts_template_argument_list_p
4134 (parser, 2))
4135 return cp_parser_identifier (parser);
4137 cp_parser_parse_tentatively (parser);
4138 /* Try a template-id. */
4139 id = cp_parser_template_id (parser,
4140 /*template_keyword_p=*/false,
4141 /*check_dependency_p=*/true,
4142 declarator_p);
4143 /* If that worked, we're done. */
4144 if (cp_parser_parse_definitely (parser))
4145 return id;
4147 /* Peek at the next token. (Changes in the token buffer may
4148 have invalidated the pointer obtained above.) */
4149 token = cp_lexer_peek_token (parser->lexer);
4151 switch (token->type)
4153 case CPP_NAME:
4154 return cp_parser_identifier (parser);
4156 case CPP_KEYWORD:
4157 if (token->keyword == RID_OPERATOR)
4158 return cp_parser_operator_function_id (parser);
4159 /* Fall through. */
4161 default:
4162 cp_parser_error (parser, "expected id-expression");
4163 return error_mark_node;
4166 else
4167 return cp_parser_unqualified_id (parser, template_keyword_p,
4168 /*check_dependency_p=*/true,
4169 declarator_p,
4170 optional_p);
4173 /* Parse an unqualified-id.
4175 unqualified-id:
4176 identifier
4177 operator-function-id
4178 conversion-function-id
4179 ~ class-name
4180 template-id
4182 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4183 keyword, in a construct like `A::template ...'.
4185 Returns a representation of unqualified-id. For the `identifier'
4186 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4187 production a BIT_NOT_EXPR is returned; the operand of the
4188 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4189 other productions, see the documentation accompanying the
4190 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4191 names are looked up in uninstantiated templates. If DECLARATOR_P
4192 is true, the unqualified-id is appearing as part of a declarator,
4193 rather than as part of an expression. */
4195 static tree
4196 cp_parser_unqualified_id (cp_parser* parser,
4197 bool template_keyword_p,
4198 bool check_dependency_p,
4199 bool declarator_p,
4200 bool optional_p)
4202 cp_token *token;
4204 /* Peek at the next token. */
4205 token = cp_lexer_peek_token (parser->lexer);
4207 switch (token->type)
4209 case CPP_NAME:
4211 tree id;
4213 /* We don't know yet whether or not this will be a
4214 template-id. */
4215 cp_parser_parse_tentatively (parser);
4216 /* Try a template-id. */
4217 id = cp_parser_template_id (parser, template_keyword_p,
4218 check_dependency_p,
4219 declarator_p);
4220 /* If it worked, we're done. */
4221 if (cp_parser_parse_definitely (parser))
4222 return id;
4223 /* Otherwise, it's an ordinary identifier. */
4224 return cp_parser_identifier (parser);
4227 case CPP_TEMPLATE_ID:
4228 return cp_parser_template_id (parser, template_keyword_p,
4229 check_dependency_p,
4230 declarator_p);
4232 case CPP_COMPL:
4234 tree type_decl;
4235 tree qualifying_scope;
4236 tree object_scope;
4237 tree scope;
4238 bool done;
4240 /* Consume the `~' token. */
4241 cp_lexer_consume_token (parser->lexer);
4242 /* Parse the class-name. The standard, as written, seems to
4243 say that:
4245 template <typename T> struct S { ~S (); };
4246 template <typename T> S<T>::~S() {}
4248 is invalid, since `~' must be followed by a class-name, but
4249 `S<T>' is dependent, and so not known to be a class.
4250 That's not right; we need to look in uninstantiated
4251 templates. A further complication arises from:
4253 template <typename T> void f(T t) {
4254 t.T::~T();
4257 Here, it is not possible to look up `T' in the scope of `T'
4258 itself. We must look in both the current scope, and the
4259 scope of the containing complete expression.
4261 Yet another issue is:
4263 struct S {
4264 int S;
4265 ~S();
4268 S::~S() {}
4270 The standard does not seem to say that the `S' in `~S'
4271 should refer to the type `S' and not the data member
4272 `S::S'. */
4274 /* DR 244 says that we look up the name after the "~" in the
4275 same scope as we looked up the qualifying name. That idea
4276 isn't fully worked out; it's more complicated than that. */
4277 scope = parser->scope;
4278 object_scope = parser->object_scope;
4279 qualifying_scope = parser->qualifying_scope;
4281 /* Check for invalid scopes. */
4282 if (scope == error_mark_node)
4284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4285 cp_lexer_consume_token (parser->lexer);
4286 return error_mark_node;
4288 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4290 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4291 error_at (token->location,
4292 "scope %qT before %<~%> is not a class-name",
4293 scope);
4294 cp_parser_simulate_error (parser);
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4296 cp_lexer_consume_token (parser->lexer);
4297 return error_mark_node;
4299 gcc_assert (!scope || TYPE_P (scope));
4301 /* If the name is of the form "X::~X" it's OK even if X is a
4302 typedef. */
4303 token = cp_lexer_peek_token (parser->lexer);
4304 if (scope
4305 && token->type == CPP_NAME
4306 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4307 != CPP_LESS)
4308 && (token->u.value == TYPE_IDENTIFIER (scope)
4309 || constructor_name_p (token->u.value, scope)))
4311 cp_lexer_consume_token (parser->lexer);
4312 return build_nt (BIT_NOT_EXPR, scope);
4315 /* If there was an explicit qualification (S::~T), first look
4316 in the scope given by the qualification (i.e., S).
4318 Note: in the calls to cp_parser_class_name below we pass
4319 typename_type so that lookup finds the injected-class-name
4320 rather than the constructor. */
4321 done = false;
4322 type_decl = NULL_TREE;
4323 if (scope)
4325 cp_parser_parse_tentatively (parser);
4326 type_decl = cp_parser_class_name (parser,
4327 /*typename_keyword_p=*/false,
4328 /*template_keyword_p=*/false,
4329 typename_type,
4330 /*check_dependency=*/false,
4331 /*class_head_p=*/false,
4332 declarator_p);
4333 if (cp_parser_parse_definitely (parser))
4334 done = true;
4336 /* In "N::S::~S", look in "N" as well. */
4337 if (!done && scope && qualifying_scope)
4339 cp_parser_parse_tentatively (parser);
4340 parser->scope = qualifying_scope;
4341 parser->object_scope = NULL_TREE;
4342 parser->qualifying_scope = NULL_TREE;
4343 type_decl
4344 = cp_parser_class_name (parser,
4345 /*typename_keyword_p=*/false,
4346 /*template_keyword_p=*/false,
4347 typename_type,
4348 /*check_dependency=*/false,
4349 /*class_head_p=*/false,
4350 declarator_p);
4351 if (cp_parser_parse_definitely (parser))
4352 done = true;
4354 /* In "p->S::~T", look in the scope given by "*p" as well. */
4355 else if (!done && object_scope)
4357 cp_parser_parse_tentatively (parser);
4358 parser->scope = object_scope;
4359 parser->object_scope = NULL_TREE;
4360 parser->qualifying_scope = NULL_TREE;
4361 type_decl
4362 = cp_parser_class_name (parser,
4363 /*typename_keyword_p=*/false,
4364 /*template_keyword_p=*/false,
4365 typename_type,
4366 /*check_dependency=*/false,
4367 /*class_head_p=*/false,
4368 declarator_p);
4369 if (cp_parser_parse_definitely (parser))
4370 done = true;
4372 /* Look in the surrounding context. */
4373 if (!done)
4375 parser->scope = NULL_TREE;
4376 parser->object_scope = NULL_TREE;
4377 parser->qualifying_scope = NULL_TREE;
4378 if (processing_template_decl)
4379 cp_parser_parse_tentatively (parser);
4380 type_decl
4381 = cp_parser_class_name (parser,
4382 /*typename_keyword_p=*/false,
4383 /*template_keyword_p=*/false,
4384 typename_type,
4385 /*check_dependency=*/false,
4386 /*class_head_p=*/false,
4387 declarator_p);
4388 if (processing_template_decl
4389 && ! cp_parser_parse_definitely (parser))
4391 /* We couldn't find a type with this name, so just accept
4392 it and check for a match at instantiation time. */
4393 type_decl = cp_parser_identifier (parser);
4394 if (type_decl != error_mark_node)
4395 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4396 return type_decl;
4399 /* If an error occurred, assume that the name of the
4400 destructor is the same as the name of the qualifying
4401 class. That allows us to keep parsing after running
4402 into ill-formed destructor names. */
4403 if (type_decl == error_mark_node && scope)
4404 return build_nt (BIT_NOT_EXPR, scope);
4405 else if (type_decl == error_mark_node)
4406 return error_mark_node;
4408 /* Check that destructor name and scope match. */
4409 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412 error_at (token->location,
4413 "declaration of %<~%T%> as member of %qT",
4414 type_decl, scope);
4415 cp_parser_simulate_error (parser);
4416 return error_mark_node;
4419 /* [class.dtor]
4421 A typedef-name that names a class shall not be used as the
4422 identifier in the declarator for a destructor declaration. */
4423 if (declarator_p
4424 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4425 && !DECL_SELF_REFERENCE_P (type_decl)
4426 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4427 error_at (token->location,
4428 "typedef-name %qD used as destructor declarator",
4429 type_decl);
4431 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4434 case CPP_KEYWORD:
4435 if (token->keyword == RID_OPERATOR)
4437 tree id;
4439 /* This could be a template-id, so we try that first. */
4440 cp_parser_parse_tentatively (parser);
4441 /* Try a template-id. */
4442 id = cp_parser_template_id (parser, template_keyword_p,
4443 /*check_dependency_p=*/true,
4444 declarator_p);
4445 /* If that worked, we're done. */
4446 if (cp_parser_parse_definitely (parser))
4447 return id;
4448 /* We still don't know whether we're looking at an
4449 operator-function-id or a conversion-function-id. */
4450 cp_parser_parse_tentatively (parser);
4451 /* Try an operator-function-id. */
4452 id = cp_parser_operator_function_id (parser);
4453 /* If that didn't work, try a conversion-function-id. */
4454 if (!cp_parser_parse_definitely (parser))
4455 id = cp_parser_conversion_function_id (parser);
4457 return id;
4459 /* Fall through. */
4461 default:
4462 if (optional_p)
4463 return NULL_TREE;
4464 cp_parser_error (parser, "expected unqualified-id");
4465 return error_mark_node;
4469 /* Parse an (optional) nested-name-specifier.
4471 nested-name-specifier: [C++98]
4472 class-or-namespace-name :: nested-name-specifier [opt]
4473 class-or-namespace-name :: template nested-name-specifier [opt]
4475 nested-name-specifier: [C++0x]
4476 type-name ::
4477 namespace-name ::
4478 nested-name-specifier identifier ::
4479 nested-name-specifier template [opt] simple-template-id ::
4481 PARSER->SCOPE should be set appropriately before this function is
4482 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4483 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4484 in name lookups.
4486 Sets PARSER->SCOPE to the class (TYPE) or namespace
4487 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4488 it unchanged if there is no nested-name-specifier. Returns the new
4489 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4491 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4492 part of a declaration and/or decl-specifier. */
4494 static tree
4495 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4496 bool typename_keyword_p,
4497 bool check_dependency_p,
4498 bool type_p,
4499 bool is_declaration)
4501 bool success = false;
4502 cp_token_position start = 0;
4503 cp_token *token;
4505 /* Remember where the nested-name-specifier starts. */
4506 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4508 start = cp_lexer_token_position (parser->lexer, false);
4509 push_deferring_access_checks (dk_deferred);
4512 while (true)
4514 tree new_scope;
4515 tree old_scope;
4516 tree saved_qualifying_scope;
4517 bool template_keyword_p;
4519 /* Spot cases that cannot be the beginning of a
4520 nested-name-specifier. */
4521 token = cp_lexer_peek_token (parser->lexer);
4523 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4524 the already parsed nested-name-specifier. */
4525 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4527 /* Grab the nested-name-specifier and continue the loop. */
4528 cp_parser_pre_parsed_nested_name_specifier (parser);
4529 /* If we originally encountered this nested-name-specifier
4530 with IS_DECLARATION set to false, we will not have
4531 resolved TYPENAME_TYPEs, so we must do so here. */
4532 if (is_declaration
4533 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4535 new_scope = resolve_typename_type (parser->scope,
4536 /*only_current_p=*/false);
4537 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4538 parser->scope = new_scope;
4540 success = true;
4541 continue;
4544 /* Spot cases that cannot be the beginning of a
4545 nested-name-specifier. On the second and subsequent times
4546 through the loop, we look for the `template' keyword. */
4547 if (success && token->keyword == RID_TEMPLATE)
4549 /* A template-id can start a nested-name-specifier. */
4550 else if (token->type == CPP_TEMPLATE_ID)
4552 else
4554 /* If the next token is not an identifier, then it is
4555 definitely not a type-name or namespace-name. */
4556 if (token->type != CPP_NAME)
4557 break;
4558 /* If the following token is neither a `<' (to begin a
4559 template-id), nor a `::', then we are not looking at a
4560 nested-name-specifier. */
4561 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4563 if (token->type == CPP_COLON
4564 && parser->colon_corrects_to_scope_p
4565 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4567 error_at (token->location,
4568 "found %<:%> in nested-name-specifier, expected %<::%>");
4569 token->type = CPP_SCOPE;
4572 if (token->type != CPP_SCOPE
4573 && !cp_parser_nth_token_starts_template_argument_list_p
4574 (parser, 2))
4575 break;
4578 /* The nested-name-specifier is optional, so we parse
4579 tentatively. */
4580 cp_parser_parse_tentatively (parser);
4582 /* Look for the optional `template' keyword, if this isn't the
4583 first time through the loop. */
4584 if (success)
4585 template_keyword_p = cp_parser_optional_template_keyword (parser);
4586 else
4587 template_keyword_p = false;
4589 /* Save the old scope since the name lookup we are about to do
4590 might destroy it. */
4591 old_scope = parser->scope;
4592 saved_qualifying_scope = parser->qualifying_scope;
4593 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4594 look up names in "X<T>::I" in order to determine that "Y" is
4595 a template. So, if we have a typename at this point, we make
4596 an effort to look through it. */
4597 if (is_declaration
4598 && !typename_keyword_p
4599 && parser->scope
4600 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4601 parser->scope = resolve_typename_type (parser->scope,
4602 /*only_current_p=*/false);
4603 /* Parse the qualifying entity. */
4604 new_scope
4605 = cp_parser_qualifying_entity (parser,
4606 typename_keyword_p,
4607 template_keyword_p,
4608 check_dependency_p,
4609 type_p,
4610 is_declaration);
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4614 /* If we found what we wanted, we keep going; otherwise, we're
4615 done. */
4616 if (!cp_parser_parse_definitely (parser))
4618 bool error_p = false;
4620 /* Restore the OLD_SCOPE since it was valid before the
4621 failed attempt at finding the last
4622 class-or-namespace-name. */
4623 parser->scope = old_scope;
4624 parser->qualifying_scope = saved_qualifying_scope;
4625 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4626 break;
4627 /* If the next token is an identifier, and the one after
4628 that is a `::', then any valid interpretation would have
4629 found a class-or-namespace-name. */
4630 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4631 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4632 == CPP_SCOPE)
4633 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4634 != CPP_COMPL))
4636 token = cp_lexer_consume_token (parser->lexer);
4637 if (!error_p)
4639 if (!token->ambiguous_p)
4641 tree decl;
4642 tree ambiguous_decls;
4644 decl = cp_parser_lookup_name (parser, token->u.value,
4645 none_type,
4646 /*is_template=*/false,
4647 /*is_namespace=*/false,
4648 /*check_dependency=*/true,
4649 &ambiguous_decls,
4650 token->location);
4651 if (TREE_CODE (decl) == TEMPLATE_DECL)
4652 error_at (token->location,
4653 "%qD used without template parameters",
4654 decl);
4655 else if (ambiguous_decls)
4657 error_at (token->location,
4658 "reference to %qD is ambiguous",
4659 token->u.value);
4660 print_candidates (ambiguous_decls);
4661 decl = error_mark_node;
4663 else
4665 if (cxx_dialect != cxx98)
4666 cp_parser_name_lookup_error
4667 (parser, token->u.value, decl, NLE_NOT_CXX98,
4668 token->location);
4669 else
4670 cp_parser_name_lookup_error
4671 (parser, token->u.value, decl, NLE_CXX98,
4672 token->location);
4675 parser->scope = error_mark_node;
4676 error_p = true;
4677 /* Treat this as a successful nested-name-specifier
4678 due to:
4680 [basic.lookup.qual]
4682 If the name found is not a class-name (clause
4683 _class_) or namespace-name (_namespace.def_), the
4684 program is ill-formed. */
4685 success = true;
4687 cp_lexer_consume_token (parser->lexer);
4689 break;
4691 /* We've found one valid nested-name-specifier. */
4692 success = true;
4693 /* Name lookup always gives us a DECL. */
4694 if (TREE_CODE (new_scope) == TYPE_DECL)
4695 new_scope = TREE_TYPE (new_scope);
4696 /* Uses of "template" must be followed by actual templates. */
4697 if (template_keyword_p
4698 && !(CLASS_TYPE_P (new_scope)
4699 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4700 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4701 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4702 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4703 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4704 == TEMPLATE_ID_EXPR)))
4705 permerror (input_location, TYPE_P (new_scope)
4706 ? "%qT is not a template"
4707 : "%qD is not a template",
4708 new_scope);
4709 /* If it is a class scope, try to complete it; we are about to
4710 be looking up names inside the class. */
4711 if (TYPE_P (new_scope)
4712 /* Since checking types for dependency can be expensive,
4713 avoid doing it if the type is already complete. */
4714 && !COMPLETE_TYPE_P (new_scope)
4715 /* Do not try to complete dependent types. */
4716 && !dependent_type_p (new_scope))
4718 new_scope = complete_type (new_scope);
4719 /* If it is a typedef to current class, use the current
4720 class instead, as the typedef won't have any names inside
4721 it yet. */
4722 if (!COMPLETE_TYPE_P (new_scope)
4723 && currently_open_class (new_scope))
4724 new_scope = TYPE_MAIN_VARIANT (new_scope);
4726 /* Make sure we look in the right scope the next time through
4727 the loop. */
4728 parser->scope = new_scope;
4731 /* If parsing tentatively, replace the sequence of tokens that makes
4732 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4733 token. That way, should we re-parse the token stream, we will
4734 not have to repeat the effort required to do the parse, nor will
4735 we issue duplicate error messages. */
4736 if (success && start)
4738 cp_token *token;
4740 token = cp_lexer_token_at (parser->lexer, start);
4741 /* Reset the contents of the START token. */
4742 token->type = CPP_NESTED_NAME_SPECIFIER;
4743 /* Retrieve any deferred checks. Do not pop this access checks yet
4744 so the memory will not be reclaimed during token replacing below. */
4745 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4746 token->u.tree_check_value->value = parser->scope;
4747 token->u.tree_check_value->checks = get_deferred_access_checks ();
4748 token->u.tree_check_value->qualifying_scope =
4749 parser->qualifying_scope;
4750 token->keyword = RID_MAX;
4752 /* Purge all subsequent tokens. */
4753 cp_lexer_purge_tokens_after (parser->lexer, start);
4756 if (start)
4757 pop_to_parent_deferring_access_checks ();
4759 return success ? parser->scope : NULL_TREE;
4762 /* Parse a nested-name-specifier. See
4763 cp_parser_nested_name_specifier_opt for details. This function
4764 behaves identically, except that it will an issue an error if no
4765 nested-name-specifier is present. */
4767 static tree
4768 cp_parser_nested_name_specifier (cp_parser *parser,
4769 bool typename_keyword_p,
4770 bool check_dependency_p,
4771 bool type_p,
4772 bool is_declaration)
4774 tree scope;
4776 /* Look for the nested-name-specifier. */
4777 scope = cp_parser_nested_name_specifier_opt (parser,
4778 typename_keyword_p,
4779 check_dependency_p,
4780 type_p,
4781 is_declaration);
4782 /* If it was not present, issue an error message. */
4783 if (!scope)
4785 cp_parser_error (parser, "expected nested-name-specifier");
4786 parser->scope = NULL_TREE;
4789 return scope;
4792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4793 this is either a class-name or a namespace-name (which corresponds
4794 to the class-or-namespace-name production in the grammar). For
4795 C++0x, it can also be a type-name that refers to an enumeration
4796 type.
4798 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4799 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4800 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4801 TYPE_P is TRUE iff the next name should be taken as a class-name,
4802 even the same name is declared to be another entity in the same
4803 scope.
4805 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4806 specified by the class-or-namespace-name. If neither is found the
4807 ERROR_MARK_NODE is returned. */
4809 static tree
4810 cp_parser_qualifying_entity (cp_parser *parser,
4811 bool typename_keyword_p,
4812 bool template_keyword_p,
4813 bool check_dependency_p,
4814 bool type_p,
4815 bool is_declaration)
4817 tree saved_scope;
4818 tree saved_qualifying_scope;
4819 tree saved_object_scope;
4820 tree scope;
4821 bool only_class_p;
4822 bool successful_parse_p;
4824 /* Before we try to parse the class-name, we must save away the
4825 current PARSER->SCOPE since cp_parser_class_name will destroy
4826 it. */
4827 saved_scope = parser->scope;
4828 saved_qualifying_scope = parser->qualifying_scope;
4829 saved_object_scope = parser->object_scope;
4830 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4831 there is no need to look for a namespace-name. */
4832 only_class_p = template_keyword_p
4833 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4834 if (!only_class_p)
4835 cp_parser_parse_tentatively (parser);
4836 scope = cp_parser_class_name (parser,
4837 typename_keyword_p,
4838 template_keyword_p,
4839 type_p ? class_type : none_type,
4840 check_dependency_p,
4841 /*class_head_p=*/false,
4842 is_declaration);
4843 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4844 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4845 if (!only_class_p
4846 && cxx_dialect != cxx98
4847 && !successful_parse_p)
4849 /* Restore the saved scope. */
4850 parser->scope = saved_scope;
4851 parser->qualifying_scope = saved_qualifying_scope;
4852 parser->object_scope = saved_object_scope;
4854 /* Parse tentatively. */
4855 cp_parser_parse_tentatively (parser);
4857 /* Parse a typedef-name or enum-name. */
4858 scope = cp_parser_nonclass_name (parser);
4860 /* "If the name found does not designate a namespace or a class,
4861 enumeration, or dependent type, the program is ill-formed."
4863 We cover classes and dependent types above and namespaces below,
4864 so this code is only looking for enums. */
4865 if (!scope || TREE_CODE (scope) != TYPE_DECL
4866 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4867 cp_parser_simulate_error (parser);
4869 successful_parse_p = cp_parser_parse_definitely (parser);
4871 /* If that didn't work, try for a namespace-name. */
4872 if (!only_class_p && !successful_parse_p)
4874 /* Restore the saved scope. */
4875 parser->scope = saved_scope;
4876 parser->qualifying_scope = saved_qualifying_scope;
4877 parser->object_scope = saved_object_scope;
4878 /* If we are not looking at an identifier followed by the scope
4879 resolution operator, then this is not part of a
4880 nested-name-specifier. (Note that this function is only used
4881 to parse the components of a nested-name-specifier.) */
4882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4883 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4884 return error_mark_node;
4885 scope = cp_parser_namespace_name (parser);
4888 return scope;
4891 /* Parse a postfix-expression.
4893 postfix-expression:
4894 primary-expression
4895 postfix-expression [ expression ]
4896 postfix-expression ( expression-list [opt] )
4897 simple-type-specifier ( expression-list [opt] )
4898 typename :: [opt] nested-name-specifier identifier
4899 ( expression-list [opt] )
4900 typename :: [opt] nested-name-specifier template [opt] template-id
4901 ( expression-list [opt] )
4902 postfix-expression . template [opt] id-expression
4903 postfix-expression -> template [opt] id-expression
4904 postfix-expression . pseudo-destructor-name
4905 postfix-expression -> pseudo-destructor-name
4906 postfix-expression ++
4907 postfix-expression --
4908 dynamic_cast < type-id > ( expression )
4909 static_cast < type-id > ( expression )
4910 reinterpret_cast < type-id > ( expression )
4911 const_cast < type-id > ( expression )
4912 typeid ( expression )
4913 typeid ( type-id )
4915 GNU Extension:
4917 postfix-expression:
4918 ( type-id ) { initializer-list , [opt] }
4920 This extension is a GNU version of the C99 compound-literal
4921 construct. (The C99 grammar uses `type-name' instead of `type-id',
4922 but they are essentially the same concept.)
4924 If ADDRESS_P is true, the postfix expression is the operand of the
4925 `&' operator. CAST_P is true if this expression is the target of a
4926 cast.
4928 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4929 class member access expressions [expr.ref].
4931 Returns a representation of the expression. */
4933 static tree
4934 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4935 bool member_access_only_p,
4936 cp_id_kind * pidk_return)
4938 cp_token *token;
4939 enum rid keyword;
4940 cp_id_kind idk = CP_ID_KIND_NONE;
4941 tree postfix_expression = NULL_TREE;
4942 bool is_member_access = false;
4944 /* Peek at the next token. */
4945 token = cp_lexer_peek_token (parser->lexer);
4946 /* Some of the productions are determined by keywords. */
4947 keyword = token->keyword;
4948 switch (keyword)
4950 case RID_DYNCAST:
4951 case RID_STATCAST:
4952 case RID_REINTCAST:
4953 case RID_CONSTCAST:
4955 tree type;
4956 tree expression;
4957 const char *saved_message;
4959 /* All of these can be handled in the same way from the point
4960 of view of parsing. Begin by consuming the token
4961 identifying the cast. */
4962 cp_lexer_consume_token (parser->lexer);
4964 /* New types cannot be defined in the cast. */
4965 saved_message = parser->type_definition_forbidden_message;
4966 parser->type_definition_forbidden_message
4967 = G_("types may not be defined in casts");
4969 /* Look for the opening `<'. */
4970 cp_parser_require (parser, CPP_LESS, RT_LESS);
4971 /* Parse the type to which we are casting. */
4972 type = cp_parser_type_id (parser);
4973 /* Look for the closing `>'. */
4974 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4975 /* Restore the old message. */
4976 parser->type_definition_forbidden_message = saved_message;
4978 /* And the expression which is being cast. */
4979 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4980 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4983 /* Only type conversions to integral or enumeration types
4984 can be used in constant-expressions. */
4985 if (!cast_valid_in_integral_constant_expression_p (type)
4986 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4987 return error_mark_node;
4989 switch (keyword)
4991 case RID_DYNCAST:
4992 postfix_expression
4993 = build_dynamic_cast (type, expression, tf_warning_or_error);
4994 break;
4995 case RID_STATCAST:
4996 postfix_expression
4997 = build_static_cast (type, expression, tf_warning_or_error);
4998 break;
4999 case RID_REINTCAST:
5000 postfix_expression
5001 = build_reinterpret_cast (type, expression,
5002 tf_warning_or_error);
5003 break;
5004 case RID_CONSTCAST:
5005 postfix_expression
5006 = build_const_cast (type, expression, tf_warning_or_error);
5007 break;
5008 default:
5009 gcc_unreachable ();
5012 break;
5014 case RID_TYPEID:
5016 tree type;
5017 const char *saved_message;
5018 bool saved_in_type_id_in_expr_p;
5020 /* Consume the `typeid' token. */
5021 cp_lexer_consume_token (parser->lexer);
5022 /* Look for the `(' token. */
5023 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5024 /* Types cannot be defined in a `typeid' expression. */
5025 saved_message = parser->type_definition_forbidden_message;
5026 parser->type_definition_forbidden_message
5027 = G_("types may not be defined in a %<typeid%> expression");
5028 /* We can't be sure yet whether we're looking at a type-id or an
5029 expression. */
5030 cp_parser_parse_tentatively (parser);
5031 /* Try a type-id first. */
5032 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5033 parser->in_type_id_in_expr_p = true;
5034 type = cp_parser_type_id (parser);
5035 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5036 /* Look for the `)' token. Otherwise, we can't be sure that
5037 we're not looking at an expression: consider `typeid (int
5038 (3))', for example. */
5039 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5040 /* If all went well, simply lookup the type-id. */
5041 if (cp_parser_parse_definitely (parser))
5042 postfix_expression = get_typeid (type);
5043 /* Otherwise, fall back to the expression variant. */
5044 else
5046 tree expression;
5048 /* Look for an expression. */
5049 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5050 /* Compute its typeid. */
5051 postfix_expression = build_typeid (expression);
5052 /* Look for the `)' token. */
5053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5055 /* Restore the saved message. */
5056 parser->type_definition_forbidden_message = saved_message;
5057 /* `typeid' may not appear in an integral constant expression. */
5058 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5059 return error_mark_node;
5061 break;
5063 case RID_TYPENAME:
5065 tree type;
5066 /* The syntax permitted here is the same permitted for an
5067 elaborated-type-specifier. */
5068 type = cp_parser_elaborated_type_specifier (parser,
5069 /*is_friend=*/false,
5070 /*is_declaration=*/false);
5071 postfix_expression = cp_parser_functional_cast (parser, type);
5073 break;
5075 default:
5077 tree type;
5079 /* If the next thing is a simple-type-specifier, we may be
5080 looking at a functional cast. We could also be looking at
5081 an id-expression. So, we try the functional cast, and if
5082 that doesn't work we fall back to the primary-expression. */
5083 cp_parser_parse_tentatively (parser);
5084 /* Look for the simple-type-specifier. */
5085 type = cp_parser_simple_type_specifier (parser,
5086 /*decl_specs=*/NULL,
5087 CP_PARSER_FLAGS_NONE);
5088 /* Parse the cast itself. */
5089 if (!cp_parser_error_occurred (parser))
5090 postfix_expression
5091 = cp_parser_functional_cast (parser, type);
5092 /* If that worked, we're done. */
5093 if (cp_parser_parse_definitely (parser))
5094 break;
5096 /* If the functional-cast didn't work out, try a
5097 compound-literal. */
5098 if (cp_parser_allow_gnu_extensions_p (parser)
5099 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5101 VEC(constructor_elt,gc) *initializer_list = NULL;
5102 bool saved_in_type_id_in_expr_p;
5104 cp_parser_parse_tentatively (parser);
5105 /* Consume the `('. */
5106 cp_lexer_consume_token (parser->lexer);
5107 /* Parse the type. */
5108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5109 parser->in_type_id_in_expr_p = true;
5110 type = cp_parser_type_id (parser);
5111 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5112 /* Look for the `)'. */
5113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5114 /* Look for the `{'. */
5115 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5116 /* If things aren't going well, there's no need to
5117 keep going. */
5118 if (!cp_parser_error_occurred (parser))
5120 bool non_constant_p;
5121 /* Parse the initializer-list. */
5122 initializer_list
5123 = cp_parser_initializer_list (parser, &non_constant_p);
5124 /* Allow a trailing `,'. */
5125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5126 cp_lexer_consume_token (parser->lexer);
5127 /* Look for the final `}'. */
5128 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5130 /* If that worked, we're definitely looking at a
5131 compound-literal expression. */
5132 if (cp_parser_parse_definitely (parser))
5134 /* Warn the user that a compound literal is not
5135 allowed in standard C++. */
5136 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5137 /* For simplicity, we disallow compound literals in
5138 constant-expressions. We could
5139 allow compound literals of integer type, whose
5140 initializer was a constant, in constant
5141 expressions. Permitting that usage, as a further
5142 extension, would not change the meaning of any
5143 currently accepted programs. (Of course, as
5144 compound literals are not part of ISO C++, the
5145 standard has nothing to say.) */
5146 if (cp_parser_non_integral_constant_expression (parser,
5147 NIC_NCC))
5149 postfix_expression = error_mark_node;
5150 break;
5152 /* Form the representation of the compound-literal. */
5153 postfix_expression
5154 = (finish_compound_literal
5155 (type, build_constructor (init_list_type_node,
5156 initializer_list)));
5157 break;
5161 /* It must be a primary-expression. */
5162 postfix_expression
5163 = cp_parser_primary_expression (parser, address_p, cast_p,
5164 /*template_arg_p=*/false,
5165 &idk);
5167 break;
5170 /* Keep looping until the postfix-expression is complete. */
5171 while (true)
5173 if (idk == CP_ID_KIND_UNQUALIFIED
5174 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5175 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5176 /* It is not a Koenig lookup function call. */
5177 postfix_expression
5178 = unqualified_name_lookup_error (postfix_expression);
5180 /* Peek at the next token. */
5181 token = cp_lexer_peek_token (parser->lexer);
5183 switch (token->type)
5185 case CPP_OPEN_SQUARE:
5186 postfix_expression
5187 = cp_parser_postfix_open_square_expression (parser,
5188 postfix_expression,
5189 false);
5190 idk = CP_ID_KIND_NONE;
5191 is_member_access = false;
5192 break;
5194 case CPP_OPEN_PAREN:
5195 /* postfix-expression ( expression-list [opt] ) */
5197 bool koenig_p;
5198 bool is_builtin_constant_p;
5199 bool saved_integral_constant_expression_p = false;
5200 bool saved_non_integral_constant_expression_p = false;
5201 VEC(tree,gc) *args;
5203 is_member_access = false;
5205 is_builtin_constant_p
5206 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5207 if (is_builtin_constant_p)
5209 /* The whole point of __builtin_constant_p is to allow
5210 non-constant expressions to appear as arguments. */
5211 saved_integral_constant_expression_p
5212 = parser->integral_constant_expression_p;
5213 saved_non_integral_constant_expression_p
5214 = parser->non_integral_constant_expression_p;
5215 parser->integral_constant_expression_p = false;
5217 args = (cp_parser_parenthesized_expression_list
5218 (parser, non_attr,
5219 /*cast_p=*/false, /*allow_expansion_p=*/true,
5220 /*non_constant_p=*/NULL));
5221 if (is_builtin_constant_p)
5223 parser->integral_constant_expression_p
5224 = saved_integral_constant_expression_p;
5225 parser->non_integral_constant_expression_p
5226 = saved_non_integral_constant_expression_p;
5229 if (args == NULL)
5231 postfix_expression = error_mark_node;
5232 break;
5235 /* Function calls are not permitted in
5236 constant-expressions. */
5237 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5238 && cp_parser_non_integral_constant_expression (parser,
5239 NIC_FUNC_CALL))
5241 postfix_expression = error_mark_node;
5242 release_tree_vector (args);
5243 break;
5246 koenig_p = false;
5247 if (idk == CP_ID_KIND_UNQUALIFIED
5248 || idk == CP_ID_KIND_TEMPLATE_ID)
5250 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5252 if (!VEC_empty (tree, args))
5254 koenig_p = true;
5255 if (!any_type_dependent_arguments_p (args))
5256 postfix_expression
5257 = perform_koenig_lookup (postfix_expression, args,
5258 /*include_std=*/false);
5260 else
5261 postfix_expression
5262 = unqualified_fn_lookup_error (postfix_expression);
5264 /* We do not perform argument-dependent lookup if
5265 normal lookup finds a non-function, in accordance
5266 with the expected resolution of DR 218. */
5267 else if (!VEC_empty (tree, args)
5268 && is_overloaded_fn (postfix_expression))
5270 tree fn = get_first_fn (postfix_expression);
5271 fn = STRIP_TEMPLATE (fn);
5273 /* Do not do argument dependent lookup if regular
5274 lookup finds a member function or a block-scope
5275 function declaration. [basic.lookup.argdep]/3 */
5276 if (!DECL_FUNCTION_MEMBER_P (fn)
5277 && !DECL_LOCAL_FUNCTION_P (fn))
5279 koenig_p = true;
5280 if (!any_type_dependent_arguments_p (args))
5281 postfix_expression
5282 = perform_koenig_lookup (postfix_expression, args,
5283 /*include_std=*/false);
5288 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5290 tree instance = TREE_OPERAND (postfix_expression, 0);
5291 tree fn = TREE_OPERAND (postfix_expression, 1);
5293 if (processing_template_decl
5294 && (type_dependent_expression_p (instance)
5295 || (!BASELINK_P (fn)
5296 && TREE_CODE (fn) != FIELD_DECL)
5297 || type_dependent_expression_p (fn)
5298 || any_type_dependent_arguments_p (args)))
5300 postfix_expression
5301 = build_nt_call_vec (postfix_expression, args);
5302 release_tree_vector (args);
5303 break;
5306 if (BASELINK_P (fn))
5308 postfix_expression
5309 = (build_new_method_call
5310 (instance, fn, &args, NULL_TREE,
5311 (idk == CP_ID_KIND_QUALIFIED
5312 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5313 /*fn_p=*/NULL,
5314 tf_warning_or_error));
5316 else
5317 postfix_expression
5318 = finish_call_expr (postfix_expression, &args,
5319 /*disallow_virtual=*/false,
5320 /*koenig_p=*/false,
5321 tf_warning_or_error);
5323 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5324 || TREE_CODE (postfix_expression) == MEMBER_REF
5325 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5326 postfix_expression = (build_offset_ref_call_from_tree
5327 (postfix_expression, &args));
5328 else if (idk == CP_ID_KIND_QUALIFIED)
5329 /* A call to a static class member, or a namespace-scope
5330 function. */
5331 postfix_expression
5332 = finish_call_expr (postfix_expression, &args,
5333 /*disallow_virtual=*/true,
5334 koenig_p,
5335 tf_warning_or_error);
5336 else
5337 /* All other function calls. */
5338 postfix_expression
5339 = finish_call_expr (postfix_expression, &args,
5340 /*disallow_virtual=*/false,
5341 koenig_p,
5342 tf_warning_or_error);
5344 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5345 idk = CP_ID_KIND_NONE;
5347 release_tree_vector (args);
5349 break;
5351 case CPP_DOT:
5352 case CPP_DEREF:
5353 /* postfix-expression . template [opt] id-expression
5354 postfix-expression . pseudo-destructor-name
5355 postfix-expression -> template [opt] id-expression
5356 postfix-expression -> pseudo-destructor-name */
5358 /* Consume the `.' or `->' operator. */
5359 cp_lexer_consume_token (parser->lexer);
5361 postfix_expression
5362 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5363 postfix_expression,
5364 false, &idk,
5365 token->location);
5367 is_member_access = true;
5368 break;
5370 case CPP_PLUS_PLUS:
5371 /* postfix-expression ++ */
5372 /* Consume the `++' token. */
5373 cp_lexer_consume_token (parser->lexer);
5374 /* Generate a representation for the complete expression. */
5375 postfix_expression
5376 = finish_increment_expr (postfix_expression,
5377 POSTINCREMENT_EXPR);
5378 /* Increments may not appear in constant-expressions. */
5379 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5380 postfix_expression = error_mark_node;
5381 idk = CP_ID_KIND_NONE;
5382 is_member_access = false;
5383 break;
5385 case CPP_MINUS_MINUS:
5386 /* postfix-expression -- */
5387 /* Consume the `--' token. */
5388 cp_lexer_consume_token (parser->lexer);
5389 /* Generate a representation for the complete expression. */
5390 postfix_expression
5391 = finish_increment_expr (postfix_expression,
5392 POSTDECREMENT_EXPR);
5393 /* Decrements may not appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5395 postfix_expression = error_mark_node;
5396 idk = CP_ID_KIND_NONE;
5397 is_member_access = false;
5398 break;
5400 default:
5401 if (pidk_return != NULL)
5402 * pidk_return = idk;
5403 if (member_access_only_p)
5404 return is_member_access? postfix_expression : error_mark_node;
5405 else
5406 return postfix_expression;
5410 /* We should never get here. */
5411 gcc_unreachable ();
5412 return error_mark_node;
5415 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5416 by cp_parser_builtin_offsetof. We're looking for
5418 postfix-expression [ expression ]
5420 FOR_OFFSETOF is set if we're being called in that context, which
5421 changes how we deal with integer constant expressions. */
5423 static tree
5424 cp_parser_postfix_open_square_expression (cp_parser *parser,
5425 tree postfix_expression,
5426 bool for_offsetof)
5428 tree index;
5430 /* Consume the `[' token. */
5431 cp_lexer_consume_token (parser->lexer);
5433 /* Parse the index expression. */
5434 /* ??? For offsetof, there is a question of what to allow here. If
5435 offsetof is not being used in an integral constant expression context,
5436 then we *could* get the right answer by computing the value at runtime.
5437 If we are in an integral constant expression context, then we might
5438 could accept any constant expression; hard to say without analysis.
5439 Rather than open the barn door too wide right away, allow only integer
5440 constant expressions here. */
5441 if (for_offsetof)
5442 index = cp_parser_constant_expression (parser, false, NULL);
5443 else
5444 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5446 /* Look for the closing `]'. */
5447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5449 /* Build the ARRAY_REF. */
5450 postfix_expression = grok_array_decl (postfix_expression, index);
5452 /* When not doing offsetof, array references are not permitted in
5453 constant-expressions. */
5454 if (!for_offsetof
5455 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5456 postfix_expression = error_mark_node;
5458 return postfix_expression;
5461 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5462 by cp_parser_builtin_offsetof. We're looking for
5464 postfix-expression . template [opt] id-expression
5465 postfix-expression . pseudo-destructor-name
5466 postfix-expression -> template [opt] id-expression
5467 postfix-expression -> pseudo-destructor-name
5469 FOR_OFFSETOF is set if we're being called in that context. That sorta
5470 limits what of the above we'll actually accept, but nevermind.
5471 TOKEN_TYPE is the "." or "->" token, which will already have been
5472 removed from the stream. */
5474 static tree
5475 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5476 enum cpp_ttype token_type,
5477 tree postfix_expression,
5478 bool for_offsetof, cp_id_kind *idk,
5479 location_t location)
5481 tree name;
5482 bool dependent_p;
5483 bool pseudo_destructor_p;
5484 tree scope = NULL_TREE;
5486 /* If this is a `->' operator, dereference the pointer. */
5487 if (token_type == CPP_DEREF)
5488 postfix_expression = build_x_arrow (postfix_expression);
5489 /* Check to see whether or not the expression is type-dependent. */
5490 dependent_p = type_dependent_expression_p (postfix_expression);
5491 /* The identifier following the `->' or `.' is not qualified. */
5492 parser->scope = NULL_TREE;
5493 parser->qualifying_scope = NULL_TREE;
5494 parser->object_scope = NULL_TREE;
5495 *idk = CP_ID_KIND_NONE;
5497 /* Enter the scope corresponding to the type of the object
5498 given by the POSTFIX_EXPRESSION. */
5499 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5501 scope = TREE_TYPE (postfix_expression);
5502 /* According to the standard, no expression should ever have
5503 reference type. Unfortunately, we do not currently match
5504 the standard in this respect in that our internal representation
5505 of an expression may have reference type even when the standard
5506 says it does not. Therefore, we have to manually obtain the
5507 underlying type here. */
5508 scope = non_reference (scope);
5509 /* The type of the POSTFIX_EXPRESSION must be complete. */
5510 if (scope == unknown_type_node)
5512 error_at (location, "%qE does not have class type",
5513 postfix_expression);
5514 scope = NULL_TREE;
5516 else
5517 scope = complete_type_or_else (scope, NULL_TREE);
5518 /* Let the name lookup machinery know that we are processing a
5519 class member access expression. */
5520 parser->context->object_type = scope;
5521 /* If something went wrong, we want to be able to discern that case,
5522 as opposed to the case where there was no SCOPE due to the type
5523 of expression being dependent. */
5524 if (!scope)
5525 scope = error_mark_node;
5526 /* If the SCOPE was erroneous, make the various semantic analysis
5527 functions exit quickly -- and without issuing additional error
5528 messages. */
5529 if (scope == error_mark_node)
5530 postfix_expression = error_mark_node;
5533 /* Assume this expression is not a pseudo-destructor access. */
5534 pseudo_destructor_p = false;
5536 /* If the SCOPE is a scalar type, then, if this is a valid program,
5537 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5538 is type dependent, it can be pseudo-destructor-name or something else.
5539 Try to parse it as pseudo-destructor-name first. */
5540 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5542 tree s;
5543 tree type;
5545 cp_parser_parse_tentatively (parser);
5546 /* Parse the pseudo-destructor-name. */
5547 s = NULL_TREE;
5548 cp_parser_pseudo_destructor_name (parser, &s, &type);
5549 if (dependent_p
5550 && (cp_parser_error_occurred (parser)
5551 || TREE_CODE (type) != TYPE_DECL
5552 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5553 cp_parser_abort_tentative_parse (parser);
5554 else if (cp_parser_parse_definitely (parser))
5556 pseudo_destructor_p = true;
5557 postfix_expression
5558 = finish_pseudo_destructor_expr (postfix_expression,
5559 s, TREE_TYPE (type));
5563 if (!pseudo_destructor_p)
5565 /* If the SCOPE is not a scalar type, we are looking at an
5566 ordinary class member access expression, rather than a
5567 pseudo-destructor-name. */
5568 bool template_p;
5569 cp_token *token = cp_lexer_peek_token (parser->lexer);
5570 /* Parse the id-expression. */
5571 name = (cp_parser_id_expression
5572 (parser,
5573 cp_parser_optional_template_keyword (parser),
5574 /*check_dependency_p=*/true,
5575 &template_p,
5576 /*declarator_p=*/false,
5577 /*optional_p=*/false));
5578 /* In general, build a SCOPE_REF if the member name is qualified.
5579 However, if the name was not dependent and has already been
5580 resolved; there is no need to build the SCOPE_REF. For example;
5582 struct X { void f(); };
5583 template <typename T> void f(T* t) { t->X::f(); }
5585 Even though "t" is dependent, "X::f" is not and has been resolved
5586 to a BASELINK; there is no need to include scope information. */
5588 /* But we do need to remember that there was an explicit scope for
5589 virtual function calls. */
5590 if (parser->scope)
5591 *idk = CP_ID_KIND_QUALIFIED;
5593 /* If the name is a template-id that names a type, we will get a
5594 TYPE_DECL here. That is invalid code. */
5595 if (TREE_CODE (name) == TYPE_DECL)
5597 error_at (token->location, "invalid use of %qD", name);
5598 postfix_expression = error_mark_node;
5600 else
5602 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5604 name = build_qualified_name (/*type=*/NULL_TREE,
5605 parser->scope,
5606 name,
5607 template_p);
5608 parser->scope = NULL_TREE;
5609 parser->qualifying_scope = NULL_TREE;
5610 parser->object_scope = NULL_TREE;
5612 if (scope && name && BASELINK_P (name))
5613 adjust_result_of_qualified_name_lookup
5614 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5615 postfix_expression
5616 = finish_class_member_access_expr (postfix_expression, name,
5617 template_p,
5618 tf_warning_or_error);
5622 /* We no longer need to look up names in the scope of the object on
5623 the left-hand side of the `.' or `->' operator. */
5624 parser->context->object_type = NULL_TREE;
5626 /* Outside of offsetof, these operators may not appear in
5627 constant-expressions. */
5628 if (!for_offsetof
5629 && (cp_parser_non_integral_constant_expression
5630 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5631 postfix_expression = error_mark_node;
5633 return postfix_expression;
5636 /* Parse a parenthesized expression-list.
5638 expression-list:
5639 assignment-expression
5640 expression-list, assignment-expression
5642 attribute-list:
5643 expression-list
5644 identifier
5645 identifier, expression-list
5647 CAST_P is true if this expression is the target of a cast.
5649 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5650 argument pack.
5652 Returns a vector of trees. Each element is a representation of an
5653 assignment-expression. NULL is returned if the ( and or ) are
5654 missing. An empty, but allocated, vector is returned on no
5655 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5656 if we are parsing an attribute list for an attribute that wants a
5657 plain identifier argument, normal_attr for an attribute that wants
5658 an expression, or non_attr if we aren't parsing an attribute list. If
5659 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5660 not all of the expressions in the list were constant. */
5662 static VEC(tree,gc) *
5663 cp_parser_parenthesized_expression_list (cp_parser* parser,
5664 int is_attribute_list,
5665 bool cast_p,
5666 bool allow_expansion_p,
5667 bool *non_constant_p)
5669 VEC(tree,gc) *expression_list;
5670 bool fold_expr_p = is_attribute_list != non_attr;
5671 tree identifier = NULL_TREE;
5672 bool saved_greater_than_is_operator_p;
5674 /* Assume all the expressions will be constant. */
5675 if (non_constant_p)
5676 *non_constant_p = false;
5678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5679 return NULL;
5681 expression_list = make_tree_vector ();
5683 /* Within a parenthesized expression, a `>' token is always
5684 the greater-than operator. */
5685 saved_greater_than_is_operator_p
5686 = parser->greater_than_is_operator_p;
5687 parser->greater_than_is_operator_p = true;
5689 /* Consume expressions until there are no more. */
5690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5691 while (true)
5693 tree expr;
5695 /* At the beginning of attribute lists, check to see if the
5696 next token is an identifier. */
5697 if (is_attribute_list == id_attr
5698 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5700 cp_token *token;
5702 /* Consume the identifier. */
5703 token = cp_lexer_consume_token (parser->lexer);
5704 /* Save the identifier. */
5705 identifier = token->u.value;
5707 else
5709 bool expr_non_constant_p;
5711 /* Parse the next assignment-expression. */
5712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5714 /* A braced-init-list. */
5715 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5716 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5717 if (non_constant_p && expr_non_constant_p)
5718 *non_constant_p = true;
5720 else if (non_constant_p)
5722 expr = (cp_parser_constant_expression
5723 (parser, /*allow_non_constant_p=*/true,
5724 &expr_non_constant_p));
5725 if (expr_non_constant_p)
5726 *non_constant_p = true;
5728 else
5729 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5731 if (fold_expr_p)
5732 expr = fold_non_dependent_expr (expr);
5734 /* If we have an ellipsis, then this is an expression
5735 expansion. */
5736 if (allow_expansion_p
5737 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5739 /* Consume the `...'. */
5740 cp_lexer_consume_token (parser->lexer);
5742 /* Build the argument pack. */
5743 expr = make_pack_expansion (expr);
5746 /* Add it to the list. We add error_mark_node
5747 expressions to the list, so that we can still tell if
5748 the correct form for a parenthesized expression-list
5749 is found. That gives better errors. */
5750 VEC_safe_push (tree, gc, expression_list, expr);
5752 if (expr == error_mark_node)
5753 goto skip_comma;
5756 /* After the first item, attribute lists look the same as
5757 expression lists. */
5758 is_attribute_list = non_attr;
5760 get_comma:;
5761 /* If the next token isn't a `,', then we are done. */
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5763 break;
5765 /* Otherwise, consume the `,' and keep going. */
5766 cp_lexer_consume_token (parser->lexer);
5769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5771 int ending;
5773 skip_comma:;
5774 /* We try and resync to an unnested comma, as that will give the
5775 user better diagnostics. */
5776 ending = cp_parser_skip_to_closing_parenthesis (parser,
5777 /*recovering=*/true,
5778 /*or_comma=*/true,
5779 /*consume_paren=*/true);
5780 if (ending < 0)
5781 goto get_comma;
5782 if (!ending)
5784 parser->greater_than_is_operator_p
5785 = saved_greater_than_is_operator_p;
5786 return NULL;
5790 parser->greater_than_is_operator_p
5791 = saved_greater_than_is_operator_p;
5793 if (identifier)
5794 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5796 return expression_list;
5799 /* Parse a pseudo-destructor-name.
5801 pseudo-destructor-name:
5802 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5803 :: [opt] nested-name-specifier template template-id :: ~ type-name
5804 :: [opt] nested-name-specifier [opt] ~ type-name
5806 If either of the first two productions is used, sets *SCOPE to the
5807 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5808 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5809 or ERROR_MARK_NODE if the parse fails. */
5811 static void
5812 cp_parser_pseudo_destructor_name (cp_parser* parser,
5813 tree* scope,
5814 tree* type)
5816 bool nested_name_specifier_p;
5818 /* Assume that things will not work out. */
5819 *type = error_mark_node;
5821 /* Look for the optional `::' operator. */
5822 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5823 /* Look for the optional nested-name-specifier. */
5824 nested_name_specifier_p
5825 = (cp_parser_nested_name_specifier_opt (parser,
5826 /*typename_keyword_p=*/false,
5827 /*check_dependency_p=*/true,
5828 /*type_p=*/false,
5829 /*is_declaration=*/false)
5830 != NULL_TREE);
5831 /* Now, if we saw a nested-name-specifier, we might be doing the
5832 second production. */
5833 if (nested_name_specifier_p
5834 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5836 /* Consume the `template' keyword. */
5837 cp_lexer_consume_token (parser->lexer);
5838 /* Parse the template-id. */
5839 cp_parser_template_id (parser,
5840 /*template_keyword_p=*/true,
5841 /*check_dependency_p=*/false,
5842 /*is_declaration=*/true);
5843 /* Look for the `::' token. */
5844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5846 /* If the next token is not a `~', then there might be some
5847 additional qualification. */
5848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5850 /* At this point, we're looking for "type-name :: ~". The type-name
5851 must not be a class-name, since this is a pseudo-destructor. So,
5852 it must be either an enum-name, or a typedef-name -- both of which
5853 are just identifiers. So, we peek ahead to check that the "::"
5854 and "~" tokens are present; if they are not, then we can avoid
5855 calling type_name. */
5856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5860 cp_parser_error (parser, "non-scalar type");
5861 return;
5864 /* Look for the type-name. */
5865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5866 if (*scope == error_mark_node)
5867 return;
5869 /* Look for the `::' token. */
5870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5872 else
5873 *scope = NULL_TREE;
5875 /* Look for the `~'. */
5876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5877 /* Look for the type-name again. We are not responsible for
5878 checking that it matches the first type-name. */
5879 *type = cp_parser_nonclass_name (parser);
5882 /* Parse a unary-expression.
5884 unary-expression:
5885 postfix-expression
5886 ++ cast-expression
5887 -- cast-expression
5888 unary-operator cast-expression
5889 sizeof unary-expression
5890 sizeof ( type-id )
5891 alignof ( type-id ) [C++0x]
5892 new-expression
5893 delete-expression
5895 GNU Extensions:
5897 unary-expression:
5898 __extension__ cast-expression
5899 __alignof__ unary-expression
5900 __alignof__ ( type-id )
5901 alignof unary-expression [C++0x]
5902 __real__ cast-expression
5903 __imag__ cast-expression
5904 && identifier
5906 ADDRESS_P is true iff the unary-expression is appearing as the
5907 operand of the `&' operator. CAST_P is true if this expression is
5908 the target of a cast.
5910 Returns a representation of the expression. */
5912 static tree
5913 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5914 cp_id_kind * pidk)
5916 cp_token *token;
5917 enum tree_code unary_operator;
5919 /* Peek at the next token. */
5920 token = cp_lexer_peek_token (parser->lexer);
5921 /* Some keywords give away the kind of expression. */
5922 if (token->type == CPP_KEYWORD)
5924 enum rid keyword = token->keyword;
5926 switch (keyword)
5928 case RID_ALIGNOF:
5929 case RID_SIZEOF:
5931 tree operand;
5932 enum tree_code op;
5934 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5935 /* Consume the token. */
5936 cp_lexer_consume_token (parser->lexer);
5937 /* Parse the operand. */
5938 operand = cp_parser_sizeof_operand (parser, keyword);
5940 if (TYPE_P (operand))
5941 return cxx_sizeof_or_alignof_type (operand, op, true);
5942 else
5944 /* ISO C++ defines alignof only with types, not with
5945 expressions. So pedwarn if alignof is used with a non-
5946 type expression. However, __alignof__ is ok. */
5947 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5948 pedwarn (token->location, OPT_pedantic,
5949 "ISO C++ does not allow %<alignof%> "
5950 "with a non-type");
5952 return cxx_sizeof_or_alignof_expr (operand, op, true);
5956 case RID_NEW:
5957 return cp_parser_new_expression (parser);
5959 case RID_DELETE:
5960 return cp_parser_delete_expression (parser);
5962 case RID_EXTENSION:
5964 /* The saved value of the PEDANTIC flag. */
5965 int saved_pedantic;
5966 tree expr;
5968 /* Save away the PEDANTIC flag. */
5969 cp_parser_extension_opt (parser, &saved_pedantic);
5970 /* Parse the cast-expression. */
5971 expr = cp_parser_simple_cast_expression (parser);
5972 /* Restore the PEDANTIC flag. */
5973 pedantic = saved_pedantic;
5975 return expr;
5978 case RID_REALPART:
5979 case RID_IMAGPART:
5981 tree expression;
5983 /* Consume the `__real__' or `__imag__' token. */
5984 cp_lexer_consume_token (parser->lexer);
5985 /* Parse the cast-expression. */
5986 expression = cp_parser_simple_cast_expression (parser);
5987 /* Create the complete representation. */
5988 return build_x_unary_op ((keyword == RID_REALPART
5989 ? REALPART_EXPR : IMAGPART_EXPR),
5990 expression,
5991 tf_warning_or_error);
5993 break;
5995 case RID_NOEXCEPT:
5997 tree expr;
5998 const char *saved_message;
5999 bool saved_integral_constant_expression_p;
6000 bool saved_non_integral_constant_expression_p;
6001 bool saved_greater_than_is_operator_p;
6003 cp_lexer_consume_token (parser->lexer);
6004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6006 saved_message = parser->type_definition_forbidden_message;
6007 parser->type_definition_forbidden_message
6008 = G_("types may not be defined in %<noexcept%> expressions");
6010 saved_integral_constant_expression_p
6011 = parser->integral_constant_expression_p;
6012 saved_non_integral_constant_expression_p
6013 = parser->non_integral_constant_expression_p;
6014 parser->integral_constant_expression_p = false;
6016 saved_greater_than_is_operator_p
6017 = parser->greater_than_is_operator_p;
6018 parser->greater_than_is_operator_p = true;
6020 ++cp_unevaluated_operand;
6021 ++c_inhibit_evaluation_warnings;
6022 expr = cp_parser_expression (parser, false, NULL);
6023 --c_inhibit_evaluation_warnings;
6024 --cp_unevaluated_operand;
6026 parser->greater_than_is_operator_p
6027 = saved_greater_than_is_operator_p;
6029 parser->integral_constant_expression_p
6030 = saved_integral_constant_expression_p;
6031 parser->non_integral_constant_expression_p
6032 = saved_non_integral_constant_expression_p;
6034 parser->type_definition_forbidden_message = saved_message;
6036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6037 return finish_noexcept_expr (expr, tf_warning_or_error);
6040 default:
6041 break;
6045 /* Look for the `:: new' and `:: delete', which also signal the
6046 beginning of a new-expression, or delete-expression,
6047 respectively. If the next token is `::', then it might be one of
6048 these. */
6049 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6051 enum rid keyword;
6053 /* See if the token after the `::' is one of the keywords in
6054 which we're interested. */
6055 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6056 /* If it's `new', we have a new-expression. */
6057 if (keyword == RID_NEW)
6058 return cp_parser_new_expression (parser);
6059 /* Similarly, for `delete'. */
6060 else if (keyword == RID_DELETE)
6061 return cp_parser_delete_expression (parser);
6064 /* Look for a unary operator. */
6065 unary_operator = cp_parser_unary_operator (token);
6066 /* The `++' and `--' operators can be handled similarly, even though
6067 they are not technically unary-operators in the grammar. */
6068 if (unary_operator == ERROR_MARK)
6070 if (token->type == CPP_PLUS_PLUS)
6071 unary_operator = PREINCREMENT_EXPR;
6072 else if (token->type == CPP_MINUS_MINUS)
6073 unary_operator = PREDECREMENT_EXPR;
6074 /* Handle the GNU address-of-label extension. */
6075 else if (cp_parser_allow_gnu_extensions_p (parser)
6076 && token->type == CPP_AND_AND)
6078 tree identifier;
6079 tree expression;
6080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6082 /* Consume the '&&' token. */
6083 cp_lexer_consume_token (parser->lexer);
6084 /* Look for the identifier. */
6085 identifier = cp_parser_identifier (parser);
6086 /* Create an expression representing the address. */
6087 expression = finish_label_address_expr (identifier, loc);
6088 if (cp_parser_non_integral_constant_expression (parser,
6089 NIC_ADDR_LABEL))
6090 expression = error_mark_node;
6091 return expression;
6094 if (unary_operator != ERROR_MARK)
6096 tree cast_expression;
6097 tree expression = error_mark_node;
6098 non_integral_constant non_constant_p = NIC_NONE;
6100 /* Consume the operator token. */
6101 token = cp_lexer_consume_token (parser->lexer);
6102 /* Parse the cast-expression. */
6103 cast_expression
6104 = cp_parser_cast_expression (parser,
6105 unary_operator == ADDR_EXPR,
6106 /*cast_p=*/false, pidk);
6107 /* Now, build an appropriate representation. */
6108 switch (unary_operator)
6110 case INDIRECT_REF:
6111 non_constant_p = NIC_STAR;
6112 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6113 tf_warning_or_error);
6114 break;
6116 case ADDR_EXPR:
6117 non_constant_p = NIC_ADDR;
6118 /* Fall through. */
6119 case BIT_NOT_EXPR:
6120 expression = build_x_unary_op (unary_operator, cast_expression,
6121 tf_warning_or_error);
6122 break;
6124 case PREINCREMENT_EXPR:
6125 case PREDECREMENT_EXPR:
6126 non_constant_p = unary_operator == PREINCREMENT_EXPR
6127 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6128 /* Fall through. */
6129 case UNARY_PLUS_EXPR:
6130 case NEGATE_EXPR:
6131 case TRUTH_NOT_EXPR:
6132 expression = finish_unary_op_expr (unary_operator, cast_expression);
6133 break;
6135 default:
6136 gcc_unreachable ();
6139 if (non_constant_p != NIC_NONE
6140 && cp_parser_non_integral_constant_expression (parser,
6141 non_constant_p))
6142 expression = error_mark_node;
6144 return expression;
6147 return cp_parser_postfix_expression (parser, address_p, cast_p,
6148 /*member_access_only_p=*/false,
6149 pidk);
6152 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6153 unary-operator, the corresponding tree code is returned. */
6155 static enum tree_code
6156 cp_parser_unary_operator (cp_token* token)
6158 switch (token->type)
6160 case CPP_MULT:
6161 return INDIRECT_REF;
6163 case CPP_AND:
6164 return ADDR_EXPR;
6166 case CPP_PLUS:
6167 return UNARY_PLUS_EXPR;
6169 case CPP_MINUS:
6170 return NEGATE_EXPR;
6172 case CPP_NOT:
6173 return TRUTH_NOT_EXPR;
6175 case CPP_COMPL:
6176 return BIT_NOT_EXPR;
6178 default:
6179 return ERROR_MARK;
6183 /* Parse a new-expression.
6185 new-expression:
6186 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6187 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6189 Returns a representation of the expression. */
6191 static tree
6192 cp_parser_new_expression (cp_parser* parser)
6194 bool global_scope_p;
6195 VEC(tree,gc) *placement;
6196 tree type;
6197 VEC(tree,gc) *initializer;
6198 tree nelts;
6199 tree ret;
6201 /* Look for the optional `::' operator. */
6202 global_scope_p
6203 = (cp_parser_global_scope_opt (parser,
6204 /*current_scope_valid_p=*/false)
6205 != NULL_TREE);
6206 /* Look for the `new' operator. */
6207 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6208 /* There's no easy way to tell a new-placement from the
6209 `( type-id )' construct. */
6210 cp_parser_parse_tentatively (parser);
6211 /* Look for a new-placement. */
6212 placement = cp_parser_new_placement (parser);
6213 /* If that didn't work out, there's no new-placement. */
6214 if (!cp_parser_parse_definitely (parser))
6216 if (placement != NULL)
6217 release_tree_vector (placement);
6218 placement = NULL;
6221 /* If the next token is a `(', then we have a parenthesized
6222 type-id. */
6223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6225 cp_token *token;
6226 /* Consume the `('. */
6227 cp_lexer_consume_token (parser->lexer);
6228 /* Parse the type-id. */
6229 type = cp_parser_type_id (parser);
6230 /* Look for the closing `)'. */
6231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6232 token = cp_lexer_peek_token (parser->lexer);
6233 /* There should not be a direct-new-declarator in this production,
6234 but GCC used to allowed this, so we check and emit a sensible error
6235 message for this case. */
6236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6238 error_at (token->location,
6239 "array bound forbidden after parenthesized type-id");
6240 inform (token->location,
6241 "try removing the parentheses around the type-id");
6242 cp_parser_direct_new_declarator (parser);
6244 nelts = NULL_TREE;
6246 /* Otherwise, there must be a new-type-id. */
6247 else
6248 type = cp_parser_new_type_id (parser, &nelts);
6250 /* If the next token is a `(' or '{', then we have a new-initializer. */
6251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6252 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6253 initializer = cp_parser_new_initializer (parser);
6254 else
6255 initializer = NULL;
6257 /* A new-expression may not appear in an integral constant
6258 expression. */
6259 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6260 ret = error_mark_node;
6261 else
6263 /* Create a representation of the new-expression. */
6264 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6265 tf_warning_or_error);
6268 if (placement != NULL)
6269 release_tree_vector (placement);
6270 if (initializer != NULL)
6271 release_tree_vector (initializer);
6273 return ret;
6276 /* Parse a new-placement.
6278 new-placement:
6279 ( expression-list )
6281 Returns the same representation as for an expression-list. */
6283 static VEC(tree,gc) *
6284 cp_parser_new_placement (cp_parser* parser)
6286 VEC(tree,gc) *expression_list;
6288 /* Parse the expression-list. */
6289 expression_list = (cp_parser_parenthesized_expression_list
6290 (parser, non_attr, /*cast_p=*/false,
6291 /*allow_expansion_p=*/true,
6292 /*non_constant_p=*/NULL));
6294 return expression_list;
6297 /* Parse a new-type-id.
6299 new-type-id:
6300 type-specifier-seq new-declarator [opt]
6302 Returns the TYPE allocated. If the new-type-id indicates an array
6303 type, *NELTS is set to the number of elements in the last array
6304 bound; the TYPE will not include the last array bound. */
6306 static tree
6307 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6309 cp_decl_specifier_seq type_specifier_seq;
6310 cp_declarator *new_declarator;
6311 cp_declarator *declarator;
6312 cp_declarator *outer_declarator;
6313 const char *saved_message;
6314 tree type;
6316 /* The type-specifier sequence must not contain type definitions.
6317 (It cannot contain declarations of new types either, but if they
6318 are not definitions we will catch that because they are not
6319 complete.) */
6320 saved_message = parser->type_definition_forbidden_message;
6321 parser->type_definition_forbidden_message
6322 = G_("types may not be defined in a new-type-id");
6323 /* Parse the type-specifier-seq. */
6324 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6325 /*is_trailing_return=*/false,
6326 &type_specifier_seq);
6327 /* Restore the old message. */
6328 parser->type_definition_forbidden_message = saved_message;
6329 /* Parse the new-declarator. */
6330 new_declarator = cp_parser_new_declarator_opt (parser);
6332 /* Determine the number of elements in the last array dimension, if
6333 any. */
6334 *nelts = NULL_TREE;
6335 /* Skip down to the last array dimension. */
6336 declarator = new_declarator;
6337 outer_declarator = NULL;
6338 while (declarator && (declarator->kind == cdk_pointer
6339 || declarator->kind == cdk_ptrmem))
6341 outer_declarator = declarator;
6342 declarator = declarator->declarator;
6344 while (declarator
6345 && declarator->kind == cdk_array
6346 && declarator->declarator
6347 && declarator->declarator->kind == cdk_array)
6349 outer_declarator = declarator;
6350 declarator = declarator->declarator;
6353 if (declarator && declarator->kind == cdk_array)
6355 *nelts = declarator->u.array.bounds;
6356 if (*nelts == error_mark_node)
6357 *nelts = integer_one_node;
6359 if (outer_declarator)
6360 outer_declarator->declarator = declarator->declarator;
6361 else
6362 new_declarator = NULL;
6365 type = groktypename (&type_specifier_seq, new_declarator, false);
6366 return type;
6369 /* Parse an (optional) new-declarator.
6371 new-declarator:
6372 ptr-operator new-declarator [opt]
6373 direct-new-declarator
6375 Returns the declarator. */
6377 static cp_declarator *
6378 cp_parser_new_declarator_opt (cp_parser* parser)
6380 enum tree_code code;
6381 tree type;
6382 cp_cv_quals cv_quals;
6384 /* We don't know if there's a ptr-operator next, or not. */
6385 cp_parser_parse_tentatively (parser);
6386 /* Look for a ptr-operator. */
6387 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6388 /* If that worked, look for more new-declarators. */
6389 if (cp_parser_parse_definitely (parser))
6391 cp_declarator *declarator;
6393 /* Parse another optional declarator. */
6394 declarator = cp_parser_new_declarator_opt (parser);
6396 return cp_parser_make_indirect_declarator
6397 (code, type, cv_quals, declarator);
6400 /* If the next token is a `[', there is a direct-new-declarator. */
6401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6402 return cp_parser_direct_new_declarator (parser);
6404 return NULL;
6407 /* Parse a direct-new-declarator.
6409 direct-new-declarator:
6410 [ expression ]
6411 direct-new-declarator [constant-expression]
6415 static cp_declarator *
6416 cp_parser_direct_new_declarator (cp_parser* parser)
6418 cp_declarator *declarator = NULL;
6420 while (true)
6422 tree expression;
6424 /* Look for the opening `['. */
6425 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6426 /* The first expression is not required to be constant. */
6427 if (!declarator)
6429 cp_token *token = cp_lexer_peek_token (parser->lexer);
6430 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6431 /* The standard requires that the expression have integral
6432 type. DR 74 adds enumeration types. We believe that the
6433 real intent is that these expressions be handled like the
6434 expression in a `switch' condition, which also allows
6435 classes with a single conversion to integral or
6436 enumeration type. */
6437 if (!processing_template_decl)
6439 expression
6440 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6441 expression,
6442 /*complain=*/true);
6443 if (!expression)
6445 error_at (token->location,
6446 "expression in new-declarator must have integral "
6447 "or enumeration type");
6448 expression = error_mark_node;
6452 /* But all the other expressions must be. */
6453 else
6454 expression
6455 = cp_parser_constant_expression (parser,
6456 /*allow_non_constant=*/false,
6457 NULL);
6458 /* Look for the closing `]'. */
6459 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6461 /* Add this bound to the declarator. */
6462 declarator = make_array_declarator (declarator, expression);
6464 /* If the next token is not a `[', then there are no more
6465 bounds. */
6466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6467 break;
6470 return declarator;
6473 /* Parse a new-initializer.
6475 new-initializer:
6476 ( expression-list [opt] )
6477 braced-init-list
6479 Returns a representation of the expression-list. */
6481 static VEC(tree,gc) *
6482 cp_parser_new_initializer (cp_parser* parser)
6484 VEC(tree,gc) *expression_list;
6486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6488 tree t;
6489 bool expr_non_constant_p;
6490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6491 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6492 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6493 expression_list = make_tree_vector_single (t);
6495 else
6496 expression_list = (cp_parser_parenthesized_expression_list
6497 (parser, non_attr, /*cast_p=*/false,
6498 /*allow_expansion_p=*/true,
6499 /*non_constant_p=*/NULL));
6501 return expression_list;
6504 /* Parse a delete-expression.
6506 delete-expression:
6507 :: [opt] delete cast-expression
6508 :: [opt] delete [ ] cast-expression
6510 Returns a representation of the expression. */
6512 static tree
6513 cp_parser_delete_expression (cp_parser* parser)
6515 bool global_scope_p;
6516 bool array_p;
6517 tree expression;
6519 /* Look for the optional `::' operator. */
6520 global_scope_p
6521 = (cp_parser_global_scope_opt (parser,
6522 /*current_scope_valid_p=*/false)
6523 != NULL_TREE);
6524 /* Look for the `delete' keyword. */
6525 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6526 /* See if the array syntax is in use. */
6527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6529 /* Consume the `[' token. */
6530 cp_lexer_consume_token (parser->lexer);
6531 /* Look for the `]' token. */
6532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6533 /* Remember that this is the `[]' construct. */
6534 array_p = true;
6536 else
6537 array_p = false;
6539 /* Parse the cast-expression. */
6540 expression = cp_parser_simple_cast_expression (parser);
6542 /* A delete-expression may not appear in an integral constant
6543 expression. */
6544 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6545 return error_mark_node;
6547 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6550 /* Returns true if TOKEN may start a cast-expression and false
6551 otherwise. */
6553 static bool
6554 cp_parser_token_starts_cast_expression (cp_token *token)
6556 switch (token->type)
6558 case CPP_COMMA:
6559 case CPP_SEMICOLON:
6560 case CPP_QUERY:
6561 case CPP_COLON:
6562 case CPP_CLOSE_SQUARE:
6563 case CPP_CLOSE_PAREN:
6564 case CPP_CLOSE_BRACE:
6565 case CPP_DOT:
6566 case CPP_DOT_STAR:
6567 case CPP_DEREF:
6568 case CPP_DEREF_STAR:
6569 case CPP_DIV:
6570 case CPP_MOD:
6571 case CPP_LSHIFT:
6572 case CPP_RSHIFT:
6573 case CPP_LESS:
6574 case CPP_GREATER:
6575 case CPP_LESS_EQ:
6576 case CPP_GREATER_EQ:
6577 case CPP_EQ_EQ:
6578 case CPP_NOT_EQ:
6579 case CPP_EQ:
6580 case CPP_MULT_EQ:
6581 case CPP_DIV_EQ:
6582 case CPP_MOD_EQ:
6583 case CPP_PLUS_EQ:
6584 case CPP_MINUS_EQ:
6585 case CPP_RSHIFT_EQ:
6586 case CPP_LSHIFT_EQ:
6587 case CPP_AND_EQ:
6588 case CPP_XOR_EQ:
6589 case CPP_OR_EQ:
6590 case CPP_XOR:
6591 case CPP_OR:
6592 case CPP_OR_OR:
6593 case CPP_EOF:
6594 return false;
6596 /* '[' may start a primary-expression in obj-c++. */
6597 case CPP_OPEN_SQUARE:
6598 return c_dialect_objc ();
6600 default:
6601 return true;
6605 /* Parse a cast-expression.
6607 cast-expression:
6608 unary-expression
6609 ( type-id ) cast-expression
6611 ADDRESS_P is true iff the unary-expression is appearing as the
6612 operand of the `&' operator. CAST_P is true if this expression is
6613 the target of a cast.
6615 Returns a representation of the expression. */
6617 static tree
6618 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6619 cp_id_kind * pidk)
6621 /* If it's a `(', then we might be looking at a cast. */
6622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6624 tree type = NULL_TREE;
6625 tree expr = NULL_TREE;
6626 bool compound_literal_p;
6627 const char *saved_message;
6629 /* There's no way to know yet whether or not this is a cast.
6630 For example, `(int (3))' is a unary-expression, while `(int)
6631 3' is a cast. So, we resort to parsing tentatively. */
6632 cp_parser_parse_tentatively (parser);
6633 /* Types may not be defined in a cast. */
6634 saved_message = parser->type_definition_forbidden_message;
6635 parser->type_definition_forbidden_message
6636 = G_("types may not be defined in casts");
6637 /* Consume the `('. */
6638 cp_lexer_consume_token (parser->lexer);
6639 /* A very tricky bit is that `(struct S) { 3 }' is a
6640 compound-literal (which we permit in C++ as an extension).
6641 But, that construct is not a cast-expression -- it is a
6642 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6643 is legal; if the compound-literal were a cast-expression,
6644 you'd need an extra set of parentheses.) But, if we parse
6645 the type-id, and it happens to be a class-specifier, then we
6646 will commit to the parse at that point, because we cannot
6647 undo the action that is done when creating a new class. So,
6648 then we cannot back up and do a postfix-expression.
6650 Therefore, we scan ahead to the closing `)', and check to see
6651 if the token after the `)' is a `{'. If so, we are not
6652 looking at a cast-expression.
6654 Save tokens so that we can put them back. */
6655 cp_lexer_save_tokens (parser->lexer);
6656 /* Skip tokens until the next token is a closing parenthesis.
6657 If we find the closing `)', and the next token is a `{', then
6658 we are looking at a compound-literal. */
6659 compound_literal_p
6660 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6661 /*consume_paren=*/true)
6662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6663 /* Roll back the tokens we skipped. */
6664 cp_lexer_rollback_tokens (parser->lexer);
6665 /* If we were looking at a compound-literal, simulate an error
6666 so that the call to cp_parser_parse_definitely below will
6667 fail. */
6668 if (compound_literal_p)
6669 cp_parser_simulate_error (parser);
6670 else
6672 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6673 parser->in_type_id_in_expr_p = true;
6674 /* Look for the type-id. */
6675 type = cp_parser_type_id (parser);
6676 /* Look for the closing `)'. */
6677 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6681 /* Restore the saved message. */
6682 parser->type_definition_forbidden_message = saved_message;
6684 /* At this point this can only be either a cast or a
6685 parenthesized ctor such as `(T ())' that looks like a cast to
6686 function returning T. */
6687 if (!cp_parser_error_occurred (parser)
6688 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6689 (parser->lexer)))
6691 cp_parser_parse_definitely (parser);
6692 expr = cp_parser_cast_expression (parser,
6693 /*address_p=*/false,
6694 /*cast_p=*/true, pidk);
6696 /* Warn about old-style casts, if so requested. */
6697 if (warn_old_style_cast
6698 && !in_system_header
6699 && !VOID_TYPE_P (type)
6700 && current_lang_name != lang_name_c)
6701 warning (OPT_Wold_style_cast, "use of old-style cast");
6703 /* Only type conversions to integral or enumeration types
6704 can be used in constant-expressions. */
6705 if (!cast_valid_in_integral_constant_expression_p (type)
6706 && cp_parser_non_integral_constant_expression (parser,
6707 NIC_CAST))
6708 return error_mark_node;
6710 /* Perform the cast. */
6711 expr = build_c_cast (input_location, type, expr);
6712 return expr;
6714 else
6715 cp_parser_abort_tentative_parse (parser);
6718 /* If we get here, then it's not a cast, so it must be a
6719 unary-expression. */
6720 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6723 /* Parse a binary expression of the general form:
6725 pm-expression:
6726 cast-expression
6727 pm-expression .* cast-expression
6728 pm-expression ->* cast-expression
6730 multiplicative-expression:
6731 pm-expression
6732 multiplicative-expression * pm-expression
6733 multiplicative-expression / pm-expression
6734 multiplicative-expression % pm-expression
6736 additive-expression:
6737 multiplicative-expression
6738 additive-expression + multiplicative-expression
6739 additive-expression - multiplicative-expression
6741 shift-expression:
6742 additive-expression
6743 shift-expression << additive-expression
6744 shift-expression >> additive-expression
6746 relational-expression:
6747 shift-expression
6748 relational-expression < shift-expression
6749 relational-expression > shift-expression
6750 relational-expression <= shift-expression
6751 relational-expression >= shift-expression
6753 GNU Extension:
6755 relational-expression:
6756 relational-expression <? shift-expression
6757 relational-expression >? shift-expression
6759 equality-expression:
6760 relational-expression
6761 equality-expression == relational-expression
6762 equality-expression != relational-expression
6764 and-expression:
6765 equality-expression
6766 and-expression & equality-expression
6768 exclusive-or-expression:
6769 and-expression
6770 exclusive-or-expression ^ and-expression
6772 inclusive-or-expression:
6773 exclusive-or-expression
6774 inclusive-or-expression | exclusive-or-expression
6776 logical-and-expression:
6777 inclusive-or-expression
6778 logical-and-expression && inclusive-or-expression
6780 logical-or-expression:
6781 logical-and-expression
6782 logical-or-expression || logical-and-expression
6784 All these are implemented with a single function like:
6786 binary-expression:
6787 simple-cast-expression
6788 binary-expression <token> binary-expression
6790 CAST_P is true if this expression is the target of a cast.
6792 The binops_by_token map is used to get the tree codes for each <token> type.
6793 binary-expressions are associated according to a precedence table. */
6795 #define TOKEN_PRECEDENCE(token) \
6796 (((token->type == CPP_GREATER \
6797 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6798 && !parser->greater_than_is_operator_p) \
6799 ? PREC_NOT_OPERATOR \
6800 : binops_by_token[token->type].prec)
6802 static tree
6803 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6804 bool no_toplevel_fold_p,
6805 enum cp_parser_prec prec,
6806 cp_id_kind * pidk)
6808 cp_parser_expression_stack stack;
6809 cp_parser_expression_stack_entry *sp = &stack[0];
6810 tree lhs, rhs;
6811 cp_token *token;
6812 enum tree_code tree_type, lhs_type, rhs_type;
6813 enum cp_parser_prec new_prec, lookahead_prec;
6814 bool overloaded_p;
6816 /* Parse the first expression. */
6817 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6818 lhs_type = ERROR_MARK;
6820 for (;;)
6822 /* Get an operator token. */
6823 token = cp_lexer_peek_token (parser->lexer);
6825 if (warn_cxx0x_compat
6826 && token->type == CPP_RSHIFT
6827 && !parser->greater_than_is_operator_p)
6829 if (warning_at (token->location, OPT_Wc__0x_compat,
6830 "%<>>%> operator will be treated as"
6831 " two right angle brackets in C++0x"))
6832 inform (token->location,
6833 "suggest parentheses around %<>>%> expression");
6836 new_prec = TOKEN_PRECEDENCE (token);
6838 /* Popping an entry off the stack means we completed a subexpression:
6839 - either we found a token which is not an operator (`>' where it is not
6840 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6841 will happen repeatedly;
6842 - or, we found an operator which has lower priority. This is the case
6843 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6844 parsing `3 * 4'. */
6845 if (new_prec <= prec)
6847 if (sp == stack)
6848 break;
6849 else
6850 goto pop;
6853 get_rhs:
6854 tree_type = binops_by_token[token->type].tree_type;
6856 /* We used the operator token. */
6857 cp_lexer_consume_token (parser->lexer);
6859 /* For "false && x" or "true || x", x will never be executed;
6860 disable warnings while evaluating it. */
6861 if (tree_type == TRUTH_ANDIF_EXPR)
6862 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6863 else if (tree_type == TRUTH_ORIF_EXPR)
6864 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6866 /* Extract another operand. It may be the RHS of this expression
6867 or the LHS of a new, higher priority expression. */
6868 rhs = cp_parser_simple_cast_expression (parser);
6869 rhs_type = ERROR_MARK;
6871 /* Get another operator token. Look up its precedence to avoid
6872 building a useless (immediately popped) stack entry for common
6873 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6874 token = cp_lexer_peek_token (parser->lexer);
6875 lookahead_prec = TOKEN_PRECEDENCE (token);
6876 if (lookahead_prec > new_prec)
6878 /* ... and prepare to parse the RHS of the new, higher priority
6879 expression. Since precedence levels on the stack are
6880 monotonically increasing, we do not have to care about
6881 stack overflows. */
6882 sp->prec = prec;
6883 sp->tree_type = tree_type;
6884 sp->lhs = lhs;
6885 sp->lhs_type = lhs_type;
6886 sp++;
6887 lhs = rhs;
6888 lhs_type = rhs_type;
6889 prec = new_prec;
6890 new_prec = lookahead_prec;
6891 goto get_rhs;
6893 pop:
6894 lookahead_prec = new_prec;
6895 /* If the stack is not empty, we have parsed into LHS the right side
6896 (`4' in the example above) of an expression we had suspended.
6897 We can use the information on the stack to recover the LHS (`3')
6898 from the stack together with the tree code (`MULT_EXPR'), and
6899 the precedence of the higher level subexpression
6900 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6901 which will be used to actually build the additive expression. */
6902 --sp;
6903 prec = sp->prec;
6904 tree_type = sp->tree_type;
6905 rhs = lhs;
6906 rhs_type = lhs_type;
6907 lhs = sp->lhs;
6908 lhs_type = sp->lhs_type;
6911 /* Undo the disabling of warnings done above. */
6912 if (tree_type == TRUTH_ANDIF_EXPR)
6913 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6914 else if (tree_type == TRUTH_ORIF_EXPR)
6915 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6917 overloaded_p = false;
6918 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6919 ERROR_MARK for everything that is not a binary expression.
6920 This makes warn_about_parentheses miss some warnings that
6921 involve unary operators. For unary expressions we should
6922 pass the correct tree_code unless the unary expression was
6923 surrounded by parentheses.
6925 if (no_toplevel_fold_p
6926 && lookahead_prec <= prec
6927 && sp == stack
6928 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6929 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6930 else
6931 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6932 &overloaded_p, tf_warning_or_error);
6933 lhs_type = tree_type;
6935 /* If the binary operator required the use of an overloaded operator,
6936 then this expression cannot be an integral constant-expression.
6937 An overloaded operator can be used even if both operands are
6938 otherwise permissible in an integral constant-expression if at
6939 least one of the operands is of enumeration type. */
6941 if (overloaded_p
6942 && cp_parser_non_integral_constant_expression (parser,
6943 NIC_OVERLOADED))
6944 return error_mark_node;
6947 return lhs;
6951 /* Parse the `? expression : assignment-expression' part of a
6952 conditional-expression. The LOGICAL_OR_EXPR is the
6953 logical-or-expression that started the conditional-expression.
6954 Returns a representation of the entire conditional-expression.
6956 This routine is used by cp_parser_assignment_expression.
6958 ? expression : assignment-expression
6960 GNU Extensions:
6962 ? : assignment-expression */
6964 static tree
6965 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6967 tree expr;
6968 tree assignment_expr;
6969 struct cp_token *token;
6971 /* Consume the `?' token. */
6972 cp_lexer_consume_token (parser->lexer);
6973 token = cp_lexer_peek_token (parser->lexer);
6974 if (cp_parser_allow_gnu_extensions_p (parser)
6975 && token->type == CPP_COLON)
6977 pedwarn (token->location, OPT_pedantic,
6978 "ISO C++ does not allow ?: with omitted middle operand");
6979 /* Implicit true clause. */
6980 expr = NULL_TREE;
6981 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6982 warn_for_omitted_condop (token->location, logical_or_expr);
6984 else
6986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6987 parser->colon_corrects_to_scope_p = false;
6988 /* Parse the expression. */
6989 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6990 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6991 c_inhibit_evaluation_warnings +=
6992 ((logical_or_expr == truthvalue_true_node)
6993 - (logical_or_expr == truthvalue_false_node));
6994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6997 /* The next token should be a `:'. */
6998 cp_parser_require (parser, CPP_COLON, RT_COLON);
6999 /* Parse the assignment-expression. */
7000 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7001 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7003 /* Build the conditional-expression. */
7004 return build_x_conditional_expr (logical_or_expr,
7005 expr,
7006 assignment_expr,
7007 tf_warning_or_error);
7010 /* Parse an assignment-expression.
7012 assignment-expression:
7013 conditional-expression
7014 logical-or-expression assignment-operator assignment_expression
7015 throw-expression
7017 CAST_P is true if this expression is the target of a cast.
7019 Returns a representation for the expression. */
7021 static tree
7022 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7023 cp_id_kind * pidk)
7025 tree expr;
7027 /* If the next token is the `throw' keyword, then we're looking at
7028 a throw-expression. */
7029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7030 expr = cp_parser_throw_expression (parser);
7031 /* Otherwise, it must be that we are looking at a
7032 logical-or-expression. */
7033 else
7035 /* Parse the binary expressions (logical-or-expression). */
7036 expr = cp_parser_binary_expression (parser, cast_p, false,
7037 PREC_NOT_OPERATOR, pidk);
7038 /* If the next token is a `?' then we're actually looking at a
7039 conditional-expression. */
7040 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7041 return cp_parser_question_colon_clause (parser, expr);
7042 else
7044 enum tree_code assignment_operator;
7046 /* If it's an assignment-operator, we're using the second
7047 production. */
7048 assignment_operator
7049 = cp_parser_assignment_operator_opt (parser);
7050 if (assignment_operator != ERROR_MARK)
7052 bool non_constant_p;
7054 /* Parse the right-hand side of the assignment. */
7055 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7057 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7058 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7060 /* An assignment may not appear in a
7061 constant-expression. */
7062 if (cp_parser_non_integral_constant_expression (parser,
7063 NIC_ASSIGNMENT))
7064 return error_mark_node;
7065 /* Build the assignment expression. */
7066 expr = build_x_modify_expr (expr,
7067 assignment_operator,
7068 rhs,
7069 tf_warning_or_error);
7074 return expr;
7077 /* Parse an (optional) assignment-operator.
7079 assignment-operator: one of
7080 = *= /= %= += -= >>= <<= &= ^= |=
7082 GNU Extension:
7084 assignment-operator: one of
7085 <?= >?=
7087 If the next token is an assignment operator, the corresponding tree
7088 code is returned, and the token is consumed. For example, for
7089 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7090 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7091 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7092 operator, ERROR_MARK is returned. */
7094 static enum tree_code
7095 cp_parser_assignment_operator_opt (cp_parser* parser)
7097 enum tree_code op;
7098 cp_token *token;
7100 /* Peek at the next token. */
7101 token = cp_lexer_peek_token (parser->lexer);
7103 switch (token->type)
7105 case CPP_EQ:
7106 op = NOP_EXPR;
7107 break;
7109 case CPP_MULT_EQ:
7110 op = MULT_EXPR;
7111 break;
7113 case CPP_DIV_EQ:
7114 op = TRUNC_DIV_EXPR;
7115 break;
7117 case CPP_MOD_EQ:
7118 op = TRUNC_MOD_EXPR;
7119 break;
7121 case CPP_PLUS_EQ:
7122 op = PLUS_EXPR;
7123 break;
7125 case CPP_MINUS_EQ:
7126 op = MINUS_EXPR;
7127 break;
7129 case CPP_RSHIFT_EQ:
7130 op = RSHIFT_EXPR;
7131 break;
7133 case CPP_LSHIFT_EQ:
7134 op = LSHIFT_EXPR;
7135 break;
7137 case CPP_AND_EQ:
7138 op = BIT_AND_EXPR;
7139 break;
7141 case CPP_XOR_EQ:
7142 op = BIT_XOR_EXPR;
7143 break;
7145 case CPP_OR_EQ:
7146 op = BIT_IOR_EXPR;
7147 break;
7149 default:
7150 /* Nothing else is an assignment operator. */
7151 op = ERROR_MARK;
7154 /* If it was an assignment operator, consume it. */
7155 if (op != ERROR_MARK)
7156 cp_lexer_consume_token (parser->lexer);
7158 return op;
7161 /* Parse an expression.
7163 expression:
7164 assignment-expression
7165 expression , assignment-expression
7167 CAST_P is true if this expression is the target of a cast.
7169 Returns a representation of the expression. */
7171 static tree
7172 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7174 tree expression = NULL_TREE;
7176 while (true)
7178 tree assignment_expression;
7180 /* Parse the next assignment-expression. */
7181 assignment_expression
7182 = cp_parser_assignment_expression (parser, cast_p, pidk);
7183 /* If this is the first assignment-expression, we can just
7184 save it away. */
7185 if (!expression)
7186 expression = assignment_expression;
7187 else
7188 expression = build_x_compound_expr (expression,
7189 assignment_expression,
7190 tf_warning_or_error);
7191 /* If the next token is not a comma, then we are done with the
7192 expression. */
7193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7194 break;
7195 /* Consume the `,'. */
7196 cp_lexer_consume_token (parser->lexer);
7197 /* A comma operator cannot appear in a constant-expression. */
7198 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7199 expression = error_mark_node;
7202 return expression;
7205 /* Parse a constant-expression.
7207 constant-expression:
7208 conditional-expression
7210 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7211 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7212 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7213 is false, NON_CONSTANT_P should be NULL. */
7215 static tree
7216 cp_parser_constant_expression (cp_parser* parser,
7217 bool allow_non_constant_p,
7218 bool *non_constant_p)
7220 bool saved_integral_constant_expression_p;
7221 bool saved_allow_non_integral_constant_expression_p;
7222 bool saved_non_integral_constant_expression_p;
7223 tree expression;
7225 /* It might seem that we could simply parse the
7226 conditional-expression, and then check to see if it were
7227 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7228 one that the compiler can figure out is constant, possibly after
7229 doing some simplifications or optimizations. The standard has a
7230 precise definition of constant-expression, and we must honor
7231 that, even though it is somewhat more restrictive.
7233 For example:
7235 int i[(2, 3)];
7237 is not a legal declaration, because `(2, 3)' is not a
7238 constant-expression. The `,' operator is forbidden in a
7239 constant-expression. However, GCC's constant-folding machinery
7240 will fold this operation to an INTEGER_CST for `3'. */
7242 /* Save the old settings. */
7243 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7244 saved_allow_non_integral_constant_expression_p
7245 = parser->allow_non_integral_constant_expression_p;
7246 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7247 /* We are now parsing a constant-expression. */
7248 parser->integral_constant_expression_p = true;
7249 parser->allow_non_integral_constant_expression_p
7250 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7251 parser->non_integral_constant_expression_p = false;
7252 /* Although the grammar says "conditional-expression", we parse an
7253 "assignment-expression", which also permits "throw-expression"
7254 and the use of assignment operators. In the case that
7255 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7256 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7257 actually essential that we look for an assignment-expression.
7258 For example, cp_parser_initializer_clauses uses this function to
7259 determine whether a particular assignment-expression is in fact
7260 constant. */
7261 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7262 /* Restore the old settings. */
7263 parser->integral_constant_expression_p
7264 = saved_integral_constant_expression_p;
7265 parser->allow_non_integral_constant_expression_p
7266 = saved_allow_non_integral_constant_expression_p;
7267 if (allow_non_constant_p)
7268 *non_constant_p = parser->non_integral_constant_expression_p;
7269 else if (parser->non_integral_constant_expression_p
7270 && cxx_dialect < cxx0x)
7271 expression = error_mark_node;
7272 parser->non_integral_constant_expression_p
7273 = saved_non_integral_constant_expression_p;
7275 return expression;
7278 /* Parse __builtin_offsetof.
7280 offsetof-expression:
7281 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7283 offsetof-member-designator:
7284 id-expression
7285 | offsetof-member-designator "." id-expression
7286 | offsetof-member-designator "[" expression "]"
7287 | offsetof-member-designator "->" id-expression */
7289 static tree
7290 cp_parser_builtin_offsetof (cp_parser *parser)
7292 int save_ice_p, save_non_ice_p;
7293 tree type, expr;
7294 cp_id_kind dummy;
7295 cp_token *token;
7297 /* We're about to accept non-integral-constant things, but will
7298 definitely yield an integral constant expression. Save and
7299 restore these values around our local parsing. */
7300 save_ice_p = parser->integral_constant_expression_p;
7301 save_non_ice_p = parser->non_integral_constant_expression_p;
7303 /* Consume the "__builtin_offsetof" token. */
7304 cp_lexer_consume_token (parser->lexer);
7305 /* Consume the opening `('. */
7306 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7307 /* Parse the type-id. */
7308 type = cp_parser_type_id (parser);
7309 /* Look for the `,'. */
7310 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7311 token = cp_lexer_peek_token (parser->lexer);
7313 /* Build the (type *)null that begins the traditional offsetof macro. */
7314 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7315 tf_warning_or_error);
7317 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7318 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7319 true, &dummy, token->location);
7320 while (true)
7322 token = cp_lexer_peek_token (parser->lexer);
7323 switch (token->type)
7325 case CPP_OPEN_SQUARE:
7326 /* offsetof-member-designator "[" expression "]" */
7327 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7328 break;
7330 case CPP_DEREF:
7331 /* offsetof-member-designator "->" identifier */
7332 expr = grok_array_decl (expr, integer_zero_node);
7333 /* FALLTHRU */
7335 case CPP_DOT:
7336 /* offsetof-member-designator "." identifier */
7337 cp_lexer_consume_token (parser->lexer);
7338 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7339 expr, true, &dummy,
7340 token->location);
7341 break;
7343 case CPP_CLOSE_PAREN:
7344 /* Consume the ")" token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 goto success;
7348 default:
7349 /* Error. We know the following require will fail, but
7350 that gives the proper error message. */
7351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7352 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7353 expr = error_mark_node;
7354 goto failure;
7358 success:
7359 /* If we're processing a template, we can't finish the semantics yet.
7360 Otherwise we can fold the entire expression now. */
7361 if (processing_template_decl)
7362 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7363 else
7364 expr = finish_offsetof (expr);
7366 failure:
7367 parser->integral_constant_expression_p = save_ice_p;
7368 parser->non_integral_constant_expression_p = save_non_ice_p;
7370 return expr;
7373 /* Parse a trait expression. */
7375 static tree
7376 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7378 cp_trait_kind kind;
7379 tree type1, type2 = NULL_TREE;
7380 bool binary = false;
7381 cp_decl_specifier_seq decl_specs;
7383 switch (keyword)
7385 case RID_HAS_NOTHROW_ASSIGN:
7386 kind = CPTK_HAS_NOTHROW_ASSIGN;
7387 break;
7388 case RID_HAS_NOTHROW_CONSTRUCTOR:
7389 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7390 break;
7391 case RID_HAS_NOTHROW_COPY:
7392 kind = CPTK_HAS_NOTHROW_COPY;
7393 break;
7394 case RID_HAS_TRIVIAL_ASSIGN:
7395 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7396 break;
7397 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7398 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7399 break;
7400 case RID_HAS_TRIVIAL_COPY:
7401 kind = CPTK_HAS_TRIVIAL_COPY;
7402 break;
7403 case RID_HAS_TRIVIAL_DESTRUCTOR:
7404 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7405 break;
7406 case RID_HAS_VIRTUAL_DESTRUCTOR:
7407 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7408 break;
7409 case RID_IS_ABSTRACT:
7410 kind = CPTK_IS_ABSTRACT;
7411 break;
7412 case RID_IS_BASE_OF:
7413 kind = CPTK_IS_BASE_OF;
7414 binary = true;
7415 break;
7416 case RID_IS_CLASS:
7417 kind = CPTK_IS_CLASS;
7418 break;
7419 case RID_IS_CONVERTIBLE_TO:
7420 kind = CPTK_IS_CONVERTIBLE_TO;
7421 binary = true;
7422 break;
7423 case RID_IS_EMPTY:
7424 kind = CPTK_IS_EMPTY;
7425 break;
7426 case RID_IS_ENUM:
7427 kind = CPTK_IS_ENUM;
7428 break;
7429 case RID_IS_POD:
7430 kind = CPTK_IS_POD;
7431 break;
7432 case RID_IS_POLYMORPHIC:
7433 kind = CPTK_IS_POLYMORPHIC;
7434 break;
7435 case RID_IS_STD_LAYOUT:
7436 kind = CPTK_IS_STD_LAYOUT;
7437 break;
7438 case RID_IS_TRIVIAL:
7439 kind = CPTK_IS_TRIVIAL;
7440 break;
7441 case RID_IS_UNION:
7442 kind = CPTK_IS_UNION;
7443 break;
7444 case RID_IS_LITERAL_TYPE:
7445 kind = CPTK_IS_LITERAL_TYPE;
7446 break;
7447 default:
7448 gcc_unreachable ();
7451 /* Consume the token. */
7452 cp_lexer_consume_token (parser->lexer);
7454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7456 type1 = cp_parser_type_id (parser);
7458 if (type1 == error_mark_node)
7459 return error_mark_node;
7461 /* Build a trivial decl-specifier-seq. */
7462 clear_decl_specs (&decl_specs);
7463 decl_specs.type = type1;
7465 /* Call grokdeclarator to figure out what type this is. */
7466 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7467 /*initialized=*/0, /*attrlist=*/NULL);
7469 if (binary)
7471 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7473 type2 = cp_parser_type_id (parser);
7475 if (type2 == error_mark_node)
7476 return error_mark_node;
7478 /* Build a trivial decl-specifier-seq. */
7479 clear_decl_specs (&decl_specs);
7480 decl_specs.type = type2;
7482 /* Call grokdeclarator to figure out what type this is. */
7483 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7484 /*initialized=*/0, /*attrlist=*/NULL);
7487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7489 /* Complete the trait expression, which may mean either processing
7490 the trait expr now or saving it for template instantiation. */
7491 return finish_trait_expr (kind, type1, type2);
7494 /* Lambdas that appear in variable initializer or default argument scope
7495 get that in their mangling, so we need to record it. We might as well
7496 use the count for function and namespace scopes as well. */
7497 static GTY(()) tree lambda_scope;
7498 static GTY(()) int lambda_count;
7499 typedef struct GTY(()) tree_int
7501 tree t;
7502 int i;
7503 } tree_int;
7504 DEF_VEC_O(tree_int);
7505 DEF_VEC_ALLOC_O(tree_int,gc);
7506 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7508 static void
7509 start_lambda_scope (tree decl)
7511 tree_int ti;
7512 gcc_assert (decl);
7513 /* Once we're inside a function, we ignore other scopes and just push
7514 the function again so that popping works properly. */
7515 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7516 decl = current_function_decl;
7517 ti.t = lambda_scope;
7518 ti.i = lambda_count;
7519 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7520 if (lambda_scope != decl)
7522 /* Don't reset the count if we're still in the same function. */
7523 lambda_scope = decl;
7524 lambda_count = 0;
7528 static void
7529 record_lambda_scope (tree lambda)
7531 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7532 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7535 static void
7536 finish_lambda_scope (void)
7538 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7539 if (lambda_scope != p->t)
7541 lambda_scope = p->t;
7542 lambda_count = p->i;
7544 VEC_pop (tree_int, lambda_scope_stack);
7547 /* Parse a lambda expression.
7549 lambda-expression:
7550 lambda-introducer lambda-declarator [opt] compound-statement
7552 Returns a representation of the expression. */
7554 static tree
7555 cp_parser_lambda_expression (cp_parser* parser)
7557 tree lambda_expr = build_lambda_expr ();
7558 tree type;
7560 LAMBDA_EXPR_LOCATION (lambda_expr)
7561 = cp_lexer_peek_token (parser->lexer)->location;
7563 if (cp_unevaluated_operand)
7564 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7565 "lambda-expression in unevaluated context");
7567 /* We may be in the middle of deferred access check. Disable
7568 it now. */
7569 push_deferring_access_checks (dk_no_deferred);
7571 cp_parser_lambda_introducer (parser, lambda_expr);
7573 type = begin_lambda_type (lambda_expr);
7575 record_lambda_scope (lambda_expr);
7577 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7578 determine_visibility (TYPE_NAME (type));
7580 /* Now that we've started the type, add the capture fields for any
7581 explicit captures. */
7582 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7585 /* Inside the class, surrounding template-parameter-lists do not apply. */
7586 unsigned int saved_num_template_parameter_lists
7587 = parser->num_template_parameter_lists;
7589 parser->num_template_parameter_lists = 0;
7591 /* By virtue of defining a local class, a lambda expression has access to
7592 the private variables of enclosing classes. */
7594 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7596 cp_parser_lambda_body (parser, lambda_expr);
7598 /* The capture list was built up in reverse order; fix that now. */
7600 tree newlist = NULL_TREE;
7601 tree elt, next;
7603 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7604 elt; elt = next)
7606 tree field = TREE_PURPOSE (elt);
7607 char *buf;
7609 next = TREE_CHAIN (elt);
7610 TREE_CHAIN (elt) = newlist;
7611 newlist = elt;
7613 /* Also add __ to the beginning of the field name so that code
7614 outside the lambda body can't see the captured name. We could
7615 just remove the name entirely, but this is more useful for
7616 debugging. */
7617 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7618 /* The 'this' capture already starts with __. */
7619 continue;
7621 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7622 buf[1] = buf[0] = '_';
7623 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7624 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7625 DECL_NAME (field) = get_identifier (buf);
7627 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7630 maybe_add_lambda_conv_op (type);
7632 type = finish_struct (type, /*attributes=*/NULL_TREE);
7634 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7637 pop_deferring_access_checks ();
7639 return build_lambda_object (lambda_expr);
7642 /* Parse the beginning of a lambda expression.
7644 lambda-introducer:
7645 [ lambda-capture [opt] ]
7647 LAMBDA_EXPR is the current representation of the lambda expression. */
7649 static void
7650 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7652 /* Need commas after the first capture. */
7653 bool first = true;
7655 /* Eat the leading `['. */
7656 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7658 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7659 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7660 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7661 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7662 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7663 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7665 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7667 cp_lexer_consume_token (parser->lexer);
7668 first = false;
7671 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7673 cp_token* capture_token;
7674 tree capture_id;
7675 tree capture_init_expr;
7676 cp_id_kind idk = CP_ID_KIND_NONE;
7677 bool explicit_init_p = false;
7679 enum capture_kind_type
7681 BY_COPY,
7682 BY_REFERENCE
7684 enum capture_kind_type capture_kind = BY_COPY;
7686 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7688 error ("expected end of capture-list");
7689 return;
7692 if (first)
7693 first = false;
7694 else
7695 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7697 /* Possibly capture `this'. */
7698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7700 cp_lexer_consume_token (parser->lexer);
7701 add_capture (lambda_expr,
7702 /*id=*/get_identifier ("__this"),
7703 /*initializer=*/finish_this_expr(),
7704 /*by_reference_p=*/false,
7705 explicit_init_p);
7706 continue;
7709 /* Remember whether we want to capture as a reference or not. */
7710 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7712 capture_kind = BY_REFERENCE;
7713 cp_lexer_consume_token (parser->lexer);
7716 /* Get the identifier. */
7717 capture_token = cp_lexer_peek_token (parser->lexer);
7718 capture_id = cp_parser_identifier (parser);
7720 if (capture_id == error_mark_node)
7721 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7722 delimiters, but I modified this to stop on unnested ']' as well. It
7723 was already changed to stop on unnested '}', so the
7724 "closing_parenthesis" name is no more misleading with my change. */
7726 cp_parser_skip_to_closing_parenthesis (parser,
7727 /*recovering=*/true,
7728 /*or_comma=*/true,
7729 /*consume_paren=*/true);
7730 break;
7733 /* Find the initializer for this capture. */
7734 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7736 /* An explicit expression exists. */
7737 cp_lexer_consume_token (parser->lexer);
7738 pedwarn (input_location, OPT_pedantic,
7739 "ISO C++ does not allow initializers "
7740 "in lambda expression capture lists");
7741 capture_init_expr = cp_parser_assignment_expression (parser,
7742 /*cast_p=*/true,
7743 &idk);
7744 explicit_init_p = true;
7746 else
7748 const char* error_msg;
7750 /* Turn the identifier into an id-expression. */
7751 capture_init_expr
7752 = cp_parser_lookup_name
7753 (parser,
7754 capture_id,
7755 none_type,
7756 /*is_template=*/false,
7757 /*is_namespace=*/false,
7758 /*check_dependency=*/true,
7759 /*ambiguous_decls=*/NULL,
7760 capture_token->location);
7762 capture_init_expr
7763 = finish_id_expression
7764 (capture_id,
7765 capture_init_expr,
7766 parser->scope,
7767 &idk,
7768 /*integral_constant_expression_p=*/false,
7769 /*allow_non_integral_constant_expression_p=*/false,
7770 /*non_integral_constant_expression_p=*/NULL,
7771 /*template_p=*/false,
7772 /*done=*/true,
7773 /*address_p=*/false,
7774 /*template_arg_p=*/false,
7775 &error_msg,
7776 capture_token->location);
7779 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7780 capture_init_expr
7781 = unqualified_name_lookup_error (capture_init_expr);
7783 add_capture (lambda_expr,
7784 capture_id,
7785 capture_init_expr,
7786 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7787 explicit_init_p);
7790 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7793 /* Parse the (optional) middle of a lambda expression.
7795 lambda-declarator:
7796 ( parameter-declaration-clause [opt] )
7797 attribute-specifier [opt]
7798 mutable [opt]
7799 exception-specification [opt]
7800 lambda-return-type-clause [opt]
7802 LAMBDA_EXPR is the current representation of the lambda expression. */
7804 static void
7805 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7807 /* 5.1.1.4 of the standard says:
7808 If a lambda-expression does not include a lambda-declarator, it is as if
7809 the lambda-declarator were ().
7810 This means an empty parameter list, no attributes, and no exception
7811 specification. */
7812 tree param_list = void_list_node;
7813 tree attributes = NULL_TREE;
7814 tree exception_spec = NULL_TREE;
7815 tree t;
7817 /* The lambda-declarator is optional, but must begin with an opening
7818 parenthesis if present. */
7819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7821 cp_lexer_consume_token (parser->lexer);
7823 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7825 /* Parse parameters. */
7826 param_list = cp_parser_parameter_declaration_clause (parser);
7828 /* Default arguments shall not be specified in the
7829 parameter-declaration-clause of a lambda-declarator. */
7830 for (t = param_list; t; t = TREE_CHAIN (t))
7831 if (TREE_PURPOSE (t))
7832 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7833 "default argument specified for lambda parameter");
7835 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7837 attributes = cp_parser_attributes_opt (parser);
7839 /* Parse optional `mutable' keyword. */
7840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7842 cp_lexer_consume_token (parser->lexer);
7843 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7846 /* Parse optional exception specification. */
7847 exception_spec = cp_parser_exception_specification_opt (parser);
7849 /* Parse optional trailing return type. */
7850 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7852 cp_lexer_consume_token (parser->lexer);
7853 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7856 /* The function parameters must be in scope all the way until after the
7857 trailing-return-type in case of decltype. */
7858 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7859 pop_binding (DECL_NAME (t), t);
7861 leave_scope ();
7864 /* Create the function call operator.
7866 Messing with declarators like this is no uglier than building up the
7867 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7868 other code. */
7870 cp_decl_specifier_seq return_type_specs;
7871 cp_declarator* declarator;
7872 tree fco;
7873 int quals;
7874 void *p;
7876 clear_decl_specs (&return_type_specs);
7877 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7878 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7879 else
7880 /* Maybe we will deduce the return type later, but we can use void
7881 as a placeholder return type anyways. */
7882 return_type_specs.type = void_type_node;
7884 p = obstack_alloc (&declarator_obstack, 0);
7886 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7887 sfk_none);
7889 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7890 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7891 declarator = make_call_declarator (declarator, param_list, quals,
7892 exception_spec,
7893 /*late_return_type=*/NULL_TREE);
7894 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7896 fco = grokmethod (&return_type_specs,
7897 declarator,
7898 attributes);
7899 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7900 DECL_ARTIFICIAL (fco) = 1;
7902 finish_member_declaration (fco);
7904 obstack_free (&declarator_obstack, p);
7908 /* Parse the body of a lambda expression, which is simply
7910 compound-statement
7912 but which requires special handling.
7913 LAMBDA_EXPR is the current representation of the lambda expression. */
7915 static void
7916 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7918 bool nested = (current_function_decl != NULL_TREE);
7919 if (nested)
7920 push_function_context ();
7922 /* Finish the function call operator
7923 - class_specifier
7924 + late_parsing_for_member
7925 + function_definition_after_declarator
7926 + ctor_initializer_opt_and_function_body */
7928 tree fco = lambda_function (lambda_expr);
7929 tree body;
7930 bool done = false;
7932 /* Let the front end know that we are going to be defining this
7933 function. */
7934 start_preparsed_function (fco,
7935 NULL_TREE,
7936 SF_PRE_PARSED | SF_INCLASS_INLINE);
7938 start_lambda_scope (fco);
7939 body = begin_function_body ();
7941 /* 5.1.1.4 of the standard says:
7942 If a lambda-expression does not include a trailing-return-type, it
7943 is as if the trailing-return-type denotes the following type:
7944 * if the compound-statement is of the form
7945 { return attribute-specifier [opt] expression ; }
7946 the type of the returned expression after lvalue-to-rvalue
7947 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7948 (_conv.array_ 4.2), and function-to-pointer conversion
7949 (_conv.func_ 4.3);
7950 * otherwise, void. */
7952 /* In a lambda that has neither a lambda-return-type-clause
7953 nor a deducible form, errors should be reported for return statements
7954 in the body. Since we used void as the placeholder return type, parsing
7955 the body as usual will give such desired behavior. */
7956 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7957 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7958 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7959 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7961 tree compound_stmt;
7962 tree expr = NULL_TREE;
7963 cp_id_kind idk = CP_ID_KIND_NONE;
7965 /* Parse tentatively in case there's more after the initial return
7966 statement. */
7967 cp_parser_parse_tentatively (parser);
7969 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7970 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7972 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7977 if (cp_parser_parse_definitely (parser))
7979 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7981 compound_stmt = begin_compound_stmt (0);
7982 /* Will get error here if type not deduced yet. */
7983 finish_return_stmt (expr);
7984 finish_compound_stmt (compound_stmt);
7986 done = true;
7990 if (!done)
7992 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7993 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7994 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7995 cp_parser_compound_stmt does not pass it. */
7996 cp_parser_function_body (parser);
7997 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8000 finish_function_body (body);
8001 finish_lambda_scope ();
8003 /* Finish the function and generate code for it if necessary. */
8004 expand_or_defer_fn (finish_function (/*inline*/2));
8007 if (nested)
8008 pop_function_context();
8011 /* Statements [gram.stmt.stmt] */
8013 /* Parse a statement.
8015 statement:
8016 labeled-statement
8017 expression-statement
8018 compound-statement
8019 selection-statement
8020 iteration-statement
8021 jump-statement
8022 declaration-statement
8023 try-block
8025 IN_COMPOUND is true when the statement is nested inside a
8026 cp_parser_compound_statement; this matters for certain pragmas.
8028 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8029 is a (possibly labeled) if statement which is not enclosed in braces
8030 and has an else clause. This is used to implement -Wparentheses. */
8032 static void
8033 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8034 bool in_compound, bool *if_p)
8036 tree statement;
8037 cp_token *token;
8038 location_t statement_location;
8040 restart:
8041 if (if_p != NULL)
8042 *if_p = false;
8043 /* There is no statement yet. */
8044 statement = NULL_TREE;
8045 /* Peek at the next token. */
8046 token = cp_lexer_peek_token (parser->lexer);
8047 /* Remember the location of the first token in the statement. */
8048 statement_location = token->location;
8049 /* If this is a keyword, then that will often determine what kind of
8050 statement we have. */
8051 if (token->type == CPP_KEYWORD)
8053 enum rid keyword = token->keyword;
8055 switch (keyword)
8057 case RID_CASE:
8058 case RID_DEFAULT:
8059 /* Looks like a labeled-statement with a case label.
8060 Parse the label, and then use tail recursion to parse
8061 the statement. */
8062 cp_parser_label_for_labeled_statement (parser);
8063 goto restart;
8065 case RID_IF:
8066 case RID_SWITCH:
8067 statement = cp_parser_selection_statement (parser, if_p);
8068 break;
8070 case RID_WHILE:
8071 case RID_DO:
8072 case RID_FOR:
8073 statement = cp_parser_iteration_statement (parser);
8074 break;
8076 case RID_BREAK:
8077 case RID_CONTINUE:
8078 case RID_RETURN:
8079 case RID_GOTO:
8080 statement = cp_parser_jump_statement (parser);
8081 break;
8083 /* Objective-C++ exception-handling constructs. */
8084 case RID_AT_TRY:
8085 case RID_AT_CATCH:
8086 case RID_AT_FINALLY:
8087 case RID_AT_SYNCHRONIZED:
8088 case RID_AT_THROW:
8089 statement = cp_parser_objc_statement (parser);
8090 break;
8092 case RID_TRY:
8093 statement = cp_parser_try_block (parser);
8094 break;
8096 case RID_NAMESPACE:
8097 /* This must be a namespace alias definition. */
8098 cp_parser_declaration_statement (parser);
8099 return;
8101 default:
8102 /* It might be a keyword like `int' that can start a
8103 declaration-statement. */
8104 break;
8107 else if (token->type == CPP_NAME)
8109 /* If the next token is a `:', then we are looking at a
8110 labeled-statement. */
8111 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8112 if (token->type == CPP_COLON)
8114 /* Looks like a labeled-statement with an ordinary label.
8115 Parse the label, and then use tail recursion to parse
8116 the statement. */
8117 cp_parser_label_for_labeled_statement (parser);
8118 goto restart;
8121 /* Anything that starts with a `{' must be a compound-statement. */
8122 else if (token->type == CPP_OPEN_BRACE)
8123 statement = cp_parser_compound_statement (parser, NULL, false);
8124 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8125 a statement all its own. */
8126 else if (token->type == CPP_PRAGMA)
8128 /* Only certain OpenMP pragmas are attached to statements, and thus
8129 are considered statements themselves. All others are not. In
8130 the context of a compound, accept the pragma as a "statement" and
8131 return so that we can check for a close brace. Otherwise we
8132 require a real statement and must go back and read one. */
8133 if (in_compound)
8134 cp_parser_pragma (parser, pragma_compound);
8135 else if (!cp_parser_pragma (parser, pragma_stmt))
8136 goto restart;
8137 return;
8139 else if (token->type == CPP_EOF)
8141 cp_parser_error (parser, "expected statement");
8142 return;
8145 /* Everything else must be a declaration-statement or an
8146 expression-statement. Try for the declaration-statement
8147 first, unless we are looking at a `;', in which case we know that
8148 we have an expression-statement. */
8149 if (!statement)
8151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8153 cp_parser_parse_tentatively (parser);
8154 /* Try to parse the declaration-statement. */
8155 cp_parser_declaration_statement (parser);
8156 /* If that worked, we're done. */
8157 if (cp_parser_parse_definitely (parser))
8158 return;
8160 /* Look for an expression-statement instead. */
8161 statement = cp_parser_expression_statement (parser, in_statement_expr);
8164 /* Set the line number for the statement. */
8165 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8166 SET_EXPR_LOCATION (statement, statement_location);
8169 /* Parse the label for a labeled-statement, i.e.
8171 identifier :
8172 case constant-expression :
8173 default :
8175 GNU Extension:
8176 case constant-expression ... constant-expression : statement
8178 When a label is parsed without errors, the label is added to the
8179 parse tree by the finish_* functions, so this function doesn't
8180 have to return the label. */
8182 static void
8183 cp_parser_label_for_labeled_statement (cp_parser* parser)
8185 cp_token *token;
8186 tree label = NULL_TREE;
8187 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8189 /* The next token should be an identifier. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191 if (token->type != CPP_NAME
8192 && token->type != CPP_KEYWORD)
8194 cp_parser_error (parser, "expected labeled-statement");
8195 return;
8198 parser->colon_corrects_to_scope_p = false;
8199 switch (token->keyword)
8201 case RID_CASE:
8203 tree expr, expr_hi;
8204 cp_token *ellipsis;
8206 /* Consume the `case' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Parse the constant-expression. */
8209 expr = cp_parser_constant_expression (parser,
8210 /*allow_non_constant_p=*/false,
8211 NULL);
8213 ellipsis = cp_lexer_peek_token (parser->lexer);
8214 if (ellipsis->type == CPP_ELLIPSIS)
8216 /* Consume the `...' token. */
8217 cp_lexer_consume_token (parser->lexer);
8218 expr_hi =
8219 cp_parser_constant_expression (parser,
8220 /*allow_non_constant_p=*/false,
8221 NULL);
8222 /* We don't need to emit warnings here, as the common code
8223 will do this for us. */
8225 else
8226 expr_hi = NULL_TREE;
8228 if (parser->in_switch_statement_p)
8229 finish_case_label (token->location, expr, expr_hi);
8230 else
8231 error_at (token->location,
8232 "case label %qE not within a switch statement",
8233 expr);
8235 break;
8237 case RID_DEFAULT:
8238 /* Consume the `default' token. */
8239 cp_lexer_consume_token (parser->lexer);
8241 if (parser->in_switch_statement_p)
8242 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8243 else
8244 error_at (token->location, "case label not within a switch statement");
8245 break;
8247 default:
8248 /* Anything else must be an ordinary label. */
8249 label = finish_label_stmt (cp_parser_identifier (parser));
8250 break;
8253 /* Require the `:' token. */
8254 cp_parser_require (parser, CPP_COLON, RT_COLON);
8256 /* An ordinary label may optionally be followed by attributes.
8257 However, this is only permitted if the attributes are then
8258 followed by a semicolon. This is because, for backward
8259 compatibility, when parsing
8260 lab: __attribute__ ((unused)) int i;
8261 we want the attribute to attach to "i", not "lab". */
8262 if (label != NULL_TREE
8263 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8265 tree attrs;
8267 cp_parser_parse_tentatively (parser);
8268 attrs = cp_parser_attributes_opt (parser);
8269 if (attrs == NULL_TREE
8270 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8271 cp_parser_abort_tentative_parse (parser);
8272 else if (!cp_parser_parse_definitely (parser))
8274 else
8275 cplus_decl_attributes (&label, attrs, 0);
8278 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8281 /* Parse an expression-statement.
8283 expression-statement:
8284 expression [opt] ;
8286 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8287 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8288 indicates whether this expression-statement is part of an
8289 expression statement. */
8291 static tree
8292 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8294 tree statement = NULL_TREE;
8295 cp_token *token = cp_lexer_peek_token (parser->lexer);
8297 /* If the next token is a ';', then there is no expression
8298 statement. */
8299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8300 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8302 /* Give a helpful message for "A<T>::type t;" and the like. */
8303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8304 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8306 if (TREE_CODE (statement) == SCOPE_REF)
8307 error_at (token->location, "need %<typename%> before %qE because "
8308 "%qT is a dependent scope",
8309 statement, TREE_OPERAND (statement, 0));
8310 else if (is_overloaded_fn (statement)
8311 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8313 /* A::A a; */
8314 tree fn = get_first_fn (statement);
8315 error_at (token->location,
8316 "%<%T::%D%> names the constructor, not the type",
8317 DECL_CONTEXT (fn), DECL_NAME (fn));
8321 /* Consume the final `;'. */
8322 cp_parser_consume_semicolon_at_end_of_statement (parser);
8324 if (in_statement_expr
8325 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8326 /* This is the final expression statement of a statement
8327 expression. */
8328 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8329 else if (statement)
8330 statement = finish_expr_stmt (statement);
8331 else
8332 finish_stmt ();
8334 return statement;
8337 /* Parse a compound-statement.
8339 compound-statement:
8340 { statement-seq [opt] }
8342 GNU extension:
8344 compound-statement:
8345 { label-declaration-seq [opt] statement-seq [opt] }
8347 label-declaration-seq:
8348 label-declaration
8349 label-declaration-seq label-declaration
8351 Returns a tree representing the statement. */
8353 static tree
8354 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8355 bool in_try)
8357 tree compound_stmt;
8359 /* Consume the `{'. */
8360 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8361 return error_mark_node;
8362 /* Begin the compound-statement. */
8363 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8364 /* If the next keyword is `__label__' we have a label declaration. */
8365 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8366 cp_parser_label_declaration (parser);
8367 /* Parse an (optional) statement-seq. */
8368 cp_parser_statement_seq_opt (parser, in_statement_expr);
8369 /* Finish the compound-statement. */
8370 finish_compound_stmt (compound_stmt);
8371 /* Consume the `}'. */
8372 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8374 return compound_stmt;
8377 /* Parse an (optional) statement-seq.
8379 statement-seq:
8380 statement
8381 statement-seq [opt] statement */
8383 static void
8384 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8386 /* Scan statements until there aren't any more. */
8387 while (true)
8389 cp_token *token = cp_lexer_peek_token (parser->lexer);
8391 /* If we are looking at a `}', then we have run out of
8392 statements; the same is true if we have reached the end
8393 of file, or have stumbled upon a stray '@end'. */
8394 if (token->type == CPP_CLOSE_BRACE
8395 || token->type == CPP_EOF
8396 || token->type == CPP_PRAGMA_EOL
8397 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8398 break;
8400 /* If we are in a compound statement and find 'else' then
8401 something went wrong. */
8402 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8404 if (parser->in_statement & IN_IF_STMT)
8405 break;
8406 else
8408 token = cp_lexer_consume_token (parser->lexer);
8409 error_at (token->location, "%<else%> without a previous %<if%>");
8413 /* Parse the statement. */
8414 cp_parser_statement (parser, in_statement_expr, true, NULL);
8418 /* Parse a selection-statement.
8420 selection-statement:
8421 if ( condition ) statement
8422 if ( condition ) statement else statement
8423 switch ( condition ) statement
8425 Returns the new IF_STMT or SWITCH_STMT.
8427 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8428 is a (possibly labeled) if statement which is not enclosed in
8429 braces and has an else clause. This is used to implement
8430 -Wparentheses. */
8432 static tree
8433 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8435 cp_token *token;
8436 enum rid keyword;
8438 if (if_p != NULL)
8439 *if_p = false;
8441 /* Peek at the next token. */
8442 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8444 /* See what kind of keyword it is. */
8445 keyword = token->keyword;
8446 switch (keyword)
8448 case RID_IF:
8449 case RID_SWITCH:
8451 tree statement;
8452 tree condition;
8454 /* Look for the `('. */
8455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8457 cp_parser_skip_to_end_of_statement (parser);
8458 return error_mark_node;
8461 /* Begin the selection-statement. */
8462 if (keyword == RID_IF)
8463 statement = begin_if_stmt ();
8464 else
8465 statement = begin_switch_stmt ();
8467 /* Parse the condition. */
8468 condition = cp_parser_condition (parser);
8469 /* Look for the `)'. */
8470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8471 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8472 /*consume_paren=*/true);
8474 if (keyword == RID_IF)
8476 bool nested_if;
8477 unsigned char in_statement;
8479 /* Add the condition. */
8480 finish_if_stmt_cond (condition, statement);
8482 /* Parse the then-clause. */
8483 in_statement = parser->in_statement;
8484 parser->in_statement |= IN_IF_STMT;
8485 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8488 add_stmt (build_empty_stmt (loc));
8489 cp_lexer_consume_token (parser->lexer);
8490 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8491 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8492 "empty body in an %<if%> statement");
8493 nested_if = false;
8495 else
8496 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8497 parser->in_statement = in_statement;
8499 finish_then_clause (statement);
8501 /* If the next token is `else', parse the else-clause. */
8502 if (cp_lexer_next_token_is_keyword (parser->lexer,
8503 RID_ELSE))
8505 /* Consume the `else' keyword. */
8506 cp_lexer_consume_token (parser->lexer);
8507 begin_else_clause (statement);
8508 /* Parse the else-clause. */
8509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8511 location_t loc;
8512 loc = cp_lexer_peek_token (parser->lexer)->location;
8513 warning_at (loc,
8514 OPT_Wempty_body, "suggest braces around "
8515 "empty body in an %<else%> statement");
8516 add_stmt (build_empty_stmt (loc));
8517 cp_lexer_consume_token (parser->lexer);
8519 else
8520 cp_parser_implicitly_scoped_statement (parser, NULL);
8522 finish_else_clause (statement);
8524 /* If we are currently parsing a then-clause, then
8525 IF_P will not be NULL. We set it to true to
8526 indicate that this if statement has an else clause.
8527 This may trigger the Wparentheses warning below
8528 when we get back up to the parent if statement. */
8529 if (if_p != NULL)
8530 *if_p = true;
8532 else
8534 /* This if statement does not have an else clause. If
8535 NESTED_IF is true, then the then-clause is an if
8536 statement which does have an else clause. We warn
8537 about the potential ambiguity. */
8538 if (nested_if)
8539 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8540 "suggest explicit braces to avoid ambiguous"
8541 " %<else%>");
8544 /* Now we're all done with the if-statement. */
8545 finish_if_stmt (statement);
8547 else
8549 bool in_switch_statement_p;
8550 unsigned char in_statement;
8552 /* Add the condition. */
8553 finish_switch_cond (condition, statement);
8555 /* Parse the body of the switch-statement. */
8556 in_switch_statement_p = parser->in_switch_statement_p;
8557 in_statement = parser->in_statement;
8558 parser->in_switch_statement_p = true;
8559 parser->in_statement |= IN_SWITCH_STMT;
8560 cp_parser_implicitly_scoped_statement (parser, NULL);
8561 parser->in_switch_statement_p = in_switch_statement_p;
8562 parser->in_statement = in_statement;
8564 /* Now we're all done with the switch-statement. */
8565 finish_switch_stmt (statement);
8568 return statement;
8570 break;
8572 default:
8573 cp_parser_error (parser, "expected selection-statement");
8574 return error_mark_node;
8578 /* Parse a condition.
8580 condition:
8581 expression
8582 type-specifier-seq declarator = initializer-clause
8583 type-specifier-seq declarator braced-init-list
8585 GNU Extension:
8587 condition:
8588 type-specifier-seq declarator asm-specification [opt]
8589 attributes [opt] = assignment-expression
8591 Returns the expression that should be tested. */
8593 static tree
8594 cp_parser_condition (cp_parser* parser)
8596 cp_decl_specifier_seq type_specifiers;
8597 const char *saved_message;
8598 int declares_class_or_enum;
8600 /* Try the declaration first. */
8601 cp_parser_parse_tentatively (parser);
8602 /* New types are not allowed in the type-specifier-seq for a
8603 condition. */
8604 saved_message = parser->type_definition_forbidden_message;
8605 parser->type_definition_forbidden_message
8606 = G_("types may not be defined in conditions");
8607 /* Parse the type-specifier-seq. */
8608 cp_parser_decl_specifier_seq (parser,
8609 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8610 &type_specifiers,
8611 &declares_class_or_enum);
8612 /* Restore the saved message. */
8613 parser->type_definition_forbidden_message = saved_message;
8614 /* If all is well, we might be looking at a declaration. */
8615 if (!cp_parser_error_occurred (parser))
8617 tree decl;
8618 tree asm_specification;
8619 tree attributes;
8620 cp_declarator *declarator;
8621 tree initializer = NULL_TREE;
8623 /* Parse the declarator. */
8624 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8625 /*ctor_dtor_or_conv_p=*/NULL,
8626 /*parenthesized_p=*/NULL,
8627 /*member_p=*/false);
8628 /* Parse the attributes. */
8629 attributes = cp_parser_attributes_opt (parser);
8630 /* Parse the asm-specification. */
8631 asm_specification = cp_parser_asm_specification_opt (parser);
8632 /* If the next token is not an `=' or '{', then we might still be
8633 looking at an expression. For example:
8635 if (A(a).x)
8637 looks like a decl-specifier-seq and a declarator -- but then
8638 there is no `=', so this is an expression. */
8639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8640 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8641 cp_parser_simulate_error (parser);
8643 /* If we did see an `=' or '{', then we are looking at a declaration
8644 for sure. */
8645 if (cp_parser_parse_definitely (parser))
8647 tree pushed_scope;
8648 bool non_constant_p;
8649 bool flags = LOOKUP_ONLYCONVERTING;
8651 /* Create the declaration. */
8652 decl = start_decl (declarator, &type_specifiers,
8653 /*initialized_p=*/true,
8654 attributes, /*prefix_attributes=*/NULL_TREE,
8655 &pushed_scope);
8657 /* Parse the initializer. */
8658 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8660 initializer = cp_parser_braced_list (parser, &non_constant_p);
8661 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8662 flags = 0;
8664 else
8666 /* Consume the `='. */
8667 cp_parser_require (parser, CPP_EQ, RT_EQ);
8668 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8670 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8671 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8673 if (!non_constant_p)
8674 initializer = fold_non_dependent_expr (initializer);
8676 /* Process the initializer. */
8677 cp_finish_decl (decl,
8678 initializer, !non_constant_p,
8679 asm_specification,
8680 flags);
8682 if (pushed_scope)
8683 pop_scope (pushed_scope);
8685 return convert_from_reference (decl);
8688 /* If we didn't even get past the declarator successfully, we are
8689 definitely not looking at a declaration. */
8690 else
8691 cp_parser_abort_tentative_parse (parser);
8693 /* Otherwise, we are looking at an expression. */
8694 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8697 /* Parses a traditional for-statement until the closing ')', not included. */
8699 static tree
8700 cp_parser_c_for (cp_parser *parser)
8702 /* Normal for loop */
8703 tree stmt;
8704 tree condition = NULL_TREE;
8705 tree expression = NULL_TREE;
8707 /* Begin the for-statement. */
8708 stmt = begin_for_stmt ();
8710 /* Parse the initialization. */
8711 cp_parser_for_init_statement (parser);
8712 finish_for_init_stmt (stmt);
8714 /* If there's a condition, process it. */
8715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8716 condition = cp_parser_condition (parser);
8717 finish_for_cond (condition, stmt);
8718 /* Look for the `;'. */
8719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8721 /* If there's an expression, process it. */
8722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8723 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8724 finish_for_expr (expression, stmt);
8726 return stmt;
8729 /* Tries to parse a range-based for-statement:
8731 range-based-for:
8732 type-specifier-seq declarator : expression
8734 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8735 expression. Note that the *DECL is returned unfinished, so
8736 later you should call cp_finish_decl().
8738 Returns TRUE iff a range-based for is parsed. */
8740 static tree
8741 cp_parser_range_for (cp_parser *parser)
8743 tree stmt, range_decl, range_expr;
8744 cp_decl_specifier_seq type_specifiers;
8745 cp_declarator *declarator;
8746 const char *saved_message;
8747 tree attributes, pushed_scope;
8748 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8750 parser->colon_corrects_to_scope_p = false;
8751 cp_parser_parse_tentatively (parser);
8752 /* New types are not allowed in the type-specifier-seq for a
8753 range-based for loop. */
8754 saved_message = parser->type_definition_forbidden_message;
8755 parser->type_definition_forbidden_message
8756 = G_("types may not be defined in range-based for loops");
8757 /* Parse the type-specifier-seq. */
8758 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8759 /*is_trailing_return=*/false,
8760 &type_specifiers);
8761 /* Restore the saved message. */
8762 parser->type_definition_forbidden_message = saved_message;
8763 /* If all is well, we might be looking at a declaration. */
8764 if (cp_parser_error_occurred (parser))
8766 cp_parser_abort_tentative_parse (parser);
8767 stmt = NULL_TREE;
8768 goto out;
8770 /* Parse the declarator. */
8771 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8772 /*ctor_dtor_or_conv_p=*/NULL,
8773 /*parenthesized_p=*/NULL,
8774 /*member_p=*/false);
8775 /* Parse the attributes. */
8776 attributes = cp_parser_attributes_opt (parser);
8777 /* The next token should be `:'. */
8778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8779 cp_parser_simulate_error (parser);
8781 /* Check if it is a range-based for */
8782 if (!cp_parser_parse_definitely (parser))
8783 return NULL_TREE;
8785 cp_parser_require (parser, CPP_COLON, RT_COLON);
8786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8788 bool expr_non_constant_p;
8789 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8791 else
8792 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8794 /* If in template, STMT is converted to a normal for-statements
8795 at instantiation. If not, it is done just ahead. */
8796 if (processing_template_decl)
8797 stmt = begin_range_for_stmt ();
8798 else
8799 stmt = begin_for_stmt ();
8801 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8802 range_decl = start_decl (declarator, &type_specifiers,
8803 /*initialized_p=*/SD_INITIALIZED,
8804 attributes, /*prefix_attributes=*/NULL_TREE,
8805 &pushed_scope);
8806 /* No scope allowed here */
8807 pop_scope (pushed_scope);
8809 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8810 finish_range_for_decl (stmt, range_decl, range_expr);
8811 else
8812 /* Convert the range-based for loop into a normal for-statement. */
8813 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8815 out:
8816 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8817 return stmt;
8820 /* Converts a range-based for-statement into a normal
8821 for-statement, as per the definition.
8823 for (RANGE_DECL : RANGE_EXPR)
8824 BLOCK
8826 should be equivalent to:
8829 auto &&__range = RANGE_EXPR;
8830 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8831 __begin != __end;
8832 ++__begin)
8834 RANGE_DECL = *__begin;
8835 BLOCK
8839 If RANGE_EXPR is an array:
8840 BEGIN_EXPR = __range
8841 END_EXPR = __range + ARRAY_SIZE(__range)
8842 Else:
8843 BEGIN_EXPR = begin(__range)
8844 END_EXPR = end(__range);
8846 When calling begin()/end() we must use argument dependent
8847 lookup, but always considering 'std' as an associated namespace. */
8849 tree
8850 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8852 tree range_type, range_temp;
8853 tree begin, end;
8854 tree iter_type, begin_expr, end_expr;
8855 tree condition, expression;
8857 /* Find out the type deduced by the declaration
8858 * `auto &&__range = range_expr' */
8859 range_type = cp_build_reference_type (make_auto (), true);
8860 range_type = do_auto_deduction (range_type, range_expr,
8861 type_uses_auto (range_type));
8863 /* Create the __range variable */
8864 range_temp = build_decl (input_location, VAR_DECL,
8865 get_identifier ("__for_range"), range_type);
8866 TREE_USED (range_temp) = 1;
8867 DECL_ARTIFICIAL (range_temp) = 1;
8868 pushdecl (range_temp);
8869 cp_finish_decl (range_temp, range_expr,
8870 /*is_constant_init*/false, NULL_TREE,
8871 LOOKUP_ONLYCONVERTING);
8873 range_temp = convert_from_reference (range_temp);
8875 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8877 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8878 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8879 begin_expr = range_temp;
8880 end_expr
8881 = build_binary_op (input_location, PLUS_EXPR,
8882 range_temp,
8883 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8885 else
8887 /* If it is not an array, we must call begin(__range)/end__range() */
8888 VEC(tree,gc) *vec;
8890 begin_expr = get_identifier ("begin");
8891 vec = make_tree_vector ();
8892 VEC_safe_push (tree, gc, vec, range_temp);
8893 begin_expr = perform_koenig_lookup (begin_expr, vec,
8894 /*include_std=*/true);
8895 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8896 tf_warning_or_error);
8897 release_tree_vector (vec);
8899 end_expr = get_identifier ("end");
8900 vec = make_tree_vector ();
8901 VEC_safe_push (tree, gc, vec, range_temp);
8902 end_expr = perform_koenig_lookup (end_expr, vec,
8903 /*include_std=*/true);
8904 end_expr = finish_call_expr (end_expr, &vec, false, true,
8905 tf_warning_or_error);
8906 release_tree_vector (vec);
8908 /* The unqualified type of the __begin and __end temporaries should
8909 * be the same as required by the multiple auto declaration */
8910 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8911 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8912 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8913 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8916 /* The new for initialization statement */
8917 begin = build_decl (input_location, VAR_DECL,
8918 get_identifier ("__for_begin"), iter_type);
8919 TREE_USED (begin) = 1;
8920 DECL_ARTIFICIAL (begin) = 1;
8921 pushdecl (begin);
8922 cp_finish_decl (begin, begin_expr,
8923 /*is_constant_init*/false, NULL_TREE,
8924 LOOKUP_ONLYCONVERTING);
8926 end = build_decl (input_location, VAR_DECL,
8927 get_identifier ("__for_end"), iter_type);
8928 TREE_USED (end) = 1;
8929 DECL_ARTIFICIAL (end) = 1;
8930 pushdecl (end);
8931 cp_finish_decl (end, end_expr,
8932 /*is_constant_init*/false, NULL_TREE,
8933 LOOKUP_ONLYCONVERTING);
8935 finish_for_init_stmt (statement);
8937 /* The new for condition */
8938 condition = build_x_binary_op (NE_EXPR,
8939 begin, ERROR_MARK,
8940 end, ERROR_MARK,
8941 NULL, tf_warning_or_error);
8942 finish_for_cond (condition, statement);
8944 /* The new increment expression */
8945 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8946 finish_for_expr (expression, statement);
8948 /* The declaration is initialized with *__begin inside the loop body */
8949 cp_finish_decl (range_decl,
8950 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8951 /*is_constant_init*/false, NULL_TREE,
8952 LOOKUP_ONLYCONVERTING);
8954 return statement;
8958 /* Parse an iteration-statement.
8960 iteration-statement:
8961 while ( condition ) statement
8962 do statement while ( expression ) ;
8963 for ( for-init-statement condition [opt] ; expression [opt] )
8964 statement
8966 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8968 static tree
8969 cp_parser_iteration_statement (cp_parser* parser)
8971 cp_token *token;
8972 enum rid keyword;
8973 tree statement;
8974 unsigned char in_statement;
8976 /* Peek at the next token. */
8977 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8978 if (!token)
8979 return error_mark_node;
8981 /* Remember whether or not we are already within an iteration
8982 statement. */
8983 in_statement = parser->in_statement;
8985 /* See what kind of keyword it is. */
8986 keyword = token->keyword;
8987 switch (keyword)
8989 case RID_WHILE:
8991 tree condition;
8993 /* Begin the while-statement. */
8994 statement = begin_while_stmt ();
8995 /* Look for the `('. */
8996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8997 /* Parse the condition. */
8998 condition = cp_parser_condition (parser);
8999 finish_while_stmt_cond (condition, statement);
9000 /* Look for the `)'. */
9001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9002 /* Parse the dependent statement. */
9003 parser->in_statement = IN_ITERATION_STMT;
9004 cp_parser_already_scoped_statement (parser);
9005 parser->in_statement = in_statement;
9006 /* We're done with the while-statement. */
9007 finish_while_stmt (statement);
9009 break;
9011 case RID_DO:
9013 tree expression;
9015 /* Begin the do-statement. */
9016 statement = begin_do_stmt ();
9017 /* Parse the body of the do-statement. */
9018 parser->in_statement = IN_ITERATION_STMT;
9019 cp_parser_implicitly_scoped_statement (parser, NULL);
9020 parser->in_statement = in_statement;
9021 finish_do_body (statement);
9022 /* Look for the `while' keyword. */
9023 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9024 /* Look for the `('. */
9025 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9026 /* Parse the expression. */
9027 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9028 /* We're done with the do-statement. */
9029 finish_do_stmt (expression, statement);
9030 /* Look for the `)'. */
9031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9032 /* Look for the `;'. */
9033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9035 break;
9037 case RID_FOR:
9039 /* Look for the `('. */
9040 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9042 if (cxx_dialect == cxx0x)
9043 statement = cp_parser_range_for (parser);
9044 else
9045 statement = NULL_TREE;
9046 if (statement == NULL_TREE)
9047 statement = cp_parser_c_for (parser);
9049 /* Look for the `)'. */
9050 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9052 /* Parse the body of the for-statement. */
9053 parser->in_statement = IN_ITERATION_STMT;
9054 cp_parser_already_scoped_statement (parser);
9055 parser->in_statement = in_statement;
9057 /* We're done with the for-statement. */
9058 finish_for_stmt (statement);
9060 break;
9062 default:
9063 cp_parser_error (parser, "expected iteration-statement");
9064 statement = error_mark_node;
9065 break;
9068 return statement;
9071 /* Parse a for-init-statement.
9073 for-init-statement:
9074 expression-statement
9075 simple-declaration */
9077 static void
9078 cp_parser_for_init_statement (cp_parser* parser)
9080 /* If the next token is a `;', then we have an empty
9081 expression-statement. Grammatically, this is also a
9082 simple-declaration, but an invalid one, because it does not
9083 declare anything. Therefore, if we did not handle this case
9084 specially, we would issue an error message about an invalid
9085 declaration. */
9086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9088 /* We're going to speculatively look for a declaration, falling back
9089 to an expression, if necessary. */
9090 cp_parser_parse_tentatively (parser);
9091 /* Parse the declaration. */
9092 cp_parser_simple_declaration (parser,
9093 /*function_definition_allowed_p=*/false);
9094 /* If the tentative parse failed, then we shall need to look for an
9095 expression-statement. */
9096 if (cp_parser_parse_definitely (parser))
9097 return;
9100 cp_parser_expression_statement (parser, NULL_TREE);
9103 /* Parse a jump-statement.
9105 jump-statement:
9106 break ;
9107 continue ;
9108 return expression [opt] ;
9109 return braced-init-list ;
9110 goto identifier ;
9112 GNU extension:
9114 jump-statement:
9115 goto * expression ;
9117 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9119 static tree
9120 cp_parser_jump_statement (cp_parser* parser)
9122 tree statement = error_mark_node;
9123 cp_token *token;
9124 enum rid keyword;
9125 unsigned char in_statement;
9127 /* Peek at the next token. */
9128 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9129 if (!token)
9130 return error_mark_node;
9132 /* See what kind of keyword it is. */
9133 keyword = token->keyword;
9134 switch (keyword)
9136 case RID_BREAK:
9137 in_statement = parser->in_statement & ~IN_IF_STMT;
9138 switch (in_statement)
9140 case 0:
9141 error_at (token->location, "break statement not within loop or switch");
9142 break;
9143 default:
9144 gcc_assert ((in_statement & IN_SWITCH_STMT)
9145 || in_statement == IN_ITERATION_STMT);
9146 statement = finish_break_stmt ();
9147 break;
9148 case IN_OMP_BLOCK:
9149 error_at (token->location, "invalid exit from OpenMP structured block");
9150 break;
9151 case IN_OMP_FOR:
9152 error_at (token->location, "break statement used with OpenMP for loop");
9153 break;
9155 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9156 break;
9158 case RID_CONTINUE:
9159 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9161 case 0:
9162 error_at (token->location, "continue statement not within a loop");
9163 break;
9164 case IN_ITERATION_STMT:
9165 case IN_OMP_FOR:
9166 statement = finish_continue_stmt ();
9167 break;
9168 case IN_OMP_BLOCK:
9169 error_at (token->location, "invalid exit from OpenMP structured block");
9170 break;
9171 default:
9172 gcc_unreachable ();
9174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9175 break;
9177 case RID_RETURN:
9179 tree expr;
9180 bool expr_non_constant_p;
9182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9184 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9185 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9187 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9188 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9189 else
9190 /* If the next token is a `;', then there is no
9191 expression. */
9192 expr = NULL_TREE;
9193 /* Build the return-statement. */
9194 statement = finish_return_stmt (expr);
9195 /* Look for the final `;'. */
9196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9198 break;
9200 case RID_GOTO:
9201 /* Create the goto-statement. */
9202 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9204 /* Issue a warning about this use of a GNU extension. */
9205 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9206 /* Consume the '*' token. */
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Parse the dependent expression. */
9209 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9211 else
9212 finish_goto_stmt (cp_parser_identifier (parser));
9213 /* Look for the final `;'. */
9214 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9215 break;
9217 default:
9218 cp_parser_error (parser, "expected jump-statement");
9219 break;
9222 return statement;
9225 /* Parse a declaration-statement.
9227 declaration-statement:
9228 block-declaration */
9230 static void
9231 cp_parser_declaration_statement (cp_parser* parser)
9233 void *p;
9235 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9236 p = obstack_alloc (&declarator_obstack, 0);
9238 /* Parse the block-declaration. */
9239 cp_parser_block_declaration (parser, /*statement_p=*/true);
9241 /* Free any declarators allocated. */
9242 obstack_free (&declarator_obstack, p);
9244 /* Finish off the statement. */
9245 finish_stmt ();
9248 /* Some dependent statements (like `if (cond) statement'), are
9249 implicitly in their own scope. In other words, if the statement is
9250 a single statement (as opposed to a compound-statement), it is
9251 none-the-less treated as if it were enclosed in braces. Any
9252 declarations appearing in the dependent statement are out of scope
9253 after control passes that point. This function parses a statement,
9254 but ensures that is in its own scope, even if it is not a
9255 compound-statement.
9257 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9258 is a (possibly labeled) if statement which is not enclosed in
9259 braces and has an else clause. This is used to implement
9260 -Wparentheses.
9262 Returns the new statement. */
9264 static tree
9265 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9267 tree statement;
9269 if (if_p != NULL)
9270 *if_p = false;
9272 /* Mark if () ; with a special NOP_EXPR. */
9273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9275 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9276 cp_lexer_consume_token (parser->lexer);
9277 statement = add_stmt (build_empty_stmt (loc));
9279 /* if a compound is opened, we simply parse the statement directly. */
9280 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9281 statement = cp_parser_compound_statement (parser, NULL, false);
9282 /* If the token is not a `{', then we must take special action. */
9283 else
9285 /* Create a compound-statement. */
9286 statement = begin_compound_stmt (0);
9287 /* Parse the dependent-statement. */
9288 cp_parser_statement (parser, NULL_TREE, false, if_p);
9289 /* Finish the dummy compound-statement. */
9290 finish_compound_stmt (statement);
9293 /* Return the statement. */
9294 return statement;
9297 /* For some dependent statements (like `while (cond) statement'), we
9298 have already created a scope. Therefore, even if the dependent
9299 statement is a compound-statement, we do not want to create another
9300 scope. */
9302 static void
9303 cp_parser_already_scoped_statement (cp_parser* parser)
9305 /* If the token is a `{', then we must take special action. */
9306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9307 cp_parser_statement (parser, NULL_TREE, false, NULL);
9308 else
9310 /* Avoid calling cp_parser_compound_statement, so that we
9311 don't create a new scope. Do everything else by hand. */
9312 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9313 /* If the next keyword is `__label__' we have a label declaration. */
9314 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9315 cp_parser_label_declaration (parser);
9316 /* Parse an (optional) statement-seq. */
9317 cp_parser_statement_seq_opt (parser, NULL_TREE);
9318 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9322 /* Declarations [gram.dcl.dcl] */
9324 /* Parse an optional declaration-sequence.
9326 declaration-seq:
9327 declaration
9328 declaration-seq declaration */
9330 static void
9331 cp_parser_declaration_seq_opt (cp_parser* parser)
9333 while (true)
9335 cp_token *token;
9337 token = cp_lexer_peek_token (parser->lexer);
9339 if (token->type == CPP_CLOSE_BRACE
9340 || token->type == CPP_EOF
9341 || token->type == CPP_PRAGMA_EOL)
9342 break;
9344 if (token->type == CPP_SEMICOLON)
9346 /* A declaration consisting of a single semicolon is
9347 invalid. Allow it unless we're being pedantic. */
9348 cp_lexer_consume_token (parser->lexer);
9349 if (!in_system_header)
9350 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9351 continue;
9354 /* If we're entering or exiting a region that's implicitly
9355 extern "C", modify the lang context appropriately. */
9356 if (!parser->implicit_extern_c && token->implicit_extern_c)
9358 push_lang_context (lang_name_c);
9359 parser->implicit_extern_c = true;
9361 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9363 pop_lang_context ();
9364 parser->implicit_extern_c = false;
9367 if (token->type == CPP_PRAGMA)
9369 /* A top-level declaration can consist solely of a #pragma.
9370 A nested declaration cannot, so this is done here and not
9371 in cp_parser_declaration. (A #pragma at block scope is
9372 handled in cp_parser_statement.) */
9373 cp_parser_pragma (parser, pragma_external);
9374 continue;
9377 /* Parse the declaration itself. */
9378 cp_parser_declaration (parser);
9382 /* Parse a declaration.
9384 declaration:
9385 block-declaration
9386 function-definition
9387 template-declaration
9388 explicit-instantiation
9389 explicit-specialization
9390 linkage-specification
9391 namespace-definition
9393 GNU extension:
9395 declaration:
9396 __extension__ declaration */
9398 static void
9399 cp_parser_declaration (cp_parser* parser)
9401 cp_token token1;
9402 cp_token token2;
9403 int saved_pedantic;
9404 void *p;
9405 tree attributes = NULL_TREE;
9407 /* Check for the `__extension__' keyword. */
9408 if (cp_parser_extension_opt (parser, &saved_pedantic))
9410 /* Parse the qualified declaration. */
9411 cp_parser_declaration (parser);
9412 /* Restore the PEDANTIC flag. */
9413 pedantic = saved_pedantic;
9415 return;
9418 /* Try to figure out what kind of declaration is present. */
9419 token1 = *cp_lexer_peek_token (parser->lexer);
9421 if (token1.type != CPP_EOF)
9422 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9423 else
9425 token2.type = CPP_EOF;
9426 token2.keyword = RID_MAX;
9429 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9430 p = obstack_alloc (&declarator_obstack, 0);
9432 /* If the next token is `extern' and the following token is a string
9433 literal, then we have a linkage specification. */
9434 if (token1.keyword == RID_EXTERN
9435 && cp_parser_is_string_literal (&token2))
9436 cp_parser_linkage_specification (parser);
9437 /* If the next token is `template', then we have either a template
9438 declaration, an explicit instantiation, or an explicit
9439 specialization. */
9440 else if (token1.keyword == RID_TEMPLATE)
9442 /* `template <>' indicates a template specialization. */
9443 if (token2.type == CPP_LESS
9444 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9445 cp_parser_explicit_specialization (parser);
9446 /* `template <' indicates a template declaration. */
9447 else if (token2.type == CPP_LESS)
9448 cp_parser_template_declaration (parser, /*member_p=*/false);
9449 /* Anything else must be an explicit instantiation. */
9450 else
9451 cp_parser_explicit_instantiation (parser);
9453 /* If the next token is `export', then we have a template
9454 declaration. */
9455 else if (token1.keyword == RID_EXPORT)
9456 cp_parser_template_declaration (parser, /*member_p=*/false);
9457 /* If the next token is `extern', 'static' or 'inline' and the one
9458 after that is `template', we have a GNU extended explicit
9459 instantiation directive. */
9460 else if (cp_parser_allow_gnu_extensions_p (parser)
9461 && (token1.keyword == RID_EXTERN
9462 || token1.keyword == RID_STATIC
9463 || token1.keyword == RID_INLINE)
9464 && token2.keyword == RID_TEMPLATE)
9465 cp_parser_explicit_instantiation (parser);
9466 /* If the next token is `namespace', check for a named or unnamed
9467 namespace definition. */
9468 else if (token1.keyword == RID_NAMESPACE
9469 && (/* A named namespace definition. */
9470 (token2.type == CPP_NAME
9471 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9472 != CPP_EQ))
9473 /* An unnamed namespace definition. */
9474 || token2.type == CPP_OPEN_BRACE
9475 || token2.keyword == RID_ATTRIBUTE))
9476 cp_parser_namespace_definition (parser);
9477 /* An inline (associated) namespace definition. */
9478 else if (token1.keyword == RID_INLINE
9479 && token2.keyword == RID_NAMESPACE)
9480 cp_parser_namespace_definition (parser);
9481 /* Objective-C++ declaration/definition. */
9482 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9483 cp_parser_objc_declaration (parser, NULL_TREE);
9484 else if (c_dialect_objc ()
9485 && token1.keyword == RID_ATTRIBUTE
9486 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9487 cp_parser_objc_declaration (parser, attributes);
9488 /* We must have either a block declaration or a function
9489 definition. */
9490 else
9491 /* Try to parse a block-declaration, or a function-definition. */
9492 cp_parser_block_declaration (parser, /*statement_p=*/false);
9494 /* Free any declarators allocated. */
9495 obstack_free (&declarator_obstack, p);
9498 /* Parse a block-declaration.
9500 block-declaration:
9501 simple-declaration
9502 asm-definition
9503 namespace-alias-definition
9504 using-declaration
9505 using-directive
9507 GNU Extension:
9509 block-declaration:
9510 __extension__ block-declaration
9512 C++0x Extension:
9514 block-declaration:
9515 static_assert-declaration
9517 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9518 part of a declaration-statement. */
9520 static void
9521 cp_parser_block_declaration (cp_parser *parser,
9522 bool statement_p)
9524 cp_token *token1;
9525 int saved_pedantic;
9527 /* Check for the `__extension__' keyword. */
9528 if (cp_parser_extension_opt (parser, &saved_pedantic))
9530 /* Parse the qualified declaration. */
9531 cp_parser_block_declaration (parser, statement_p);
9532 /* Restore the PEDANTIC flag. */
9533 pedantic = saved_pedantic;
9535 return;
9538 /* Peek at the next token to figure out which kind of declaration is
9539 present. */
9540 token1 = cp_lexer_peek_token (parser->lexer);
9542 /* If the next keyword is `asm', we have an asm-definition. */
9543 if (token1->keyword == RID_ASM)
9545 if (statement_p)
9546 cp_parser_commit_to_tentative_parse (parser);
9547 cp_parser_asm_definition (parser);
9549 /* If the next keyword is `namespace', we have a
9550 namespace-alias-definition. */
9551 else if (token1->keyword == RID_NAMESPACE)
9552 cp_parser_namespace_alias_definition (parser);
9553 /* If the next keyword is `using', we have either a
9554 using-declaration or a using-directive. */
9555 else if (token1->keyword == RID_USING)
9557 cp_token *token2;
9559 if (statement_p)
9560 cp_parser_commit_to_tentative_parse (parser);
9561 /* If the token after `using' is `namespace', then we have a
9562 using-directive. */
9563 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9564 if (token2->keyword == RID_NAMESPACE)
9565 cp_parser_using_directive (parser);
9566 /* Otherwise, it's a using-declaration. */
9567 else
9568 cp_parser_using_declaration (parser,
9569 /*access_declaration_p=*/false);
9571 /* If the next keyword is `__label__' we have a misplaced label
9572 declaration. */
9573 else if (token1->keyword == RID_LABEL)
9575 cp_lexer_consume_token (parser->lexer);
9576 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9577 cp_parser_skip_to_end_of_statement (parser);
9578 /* If the next token is now a `;', consume it. */
9579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9580 cp_lexer_consume_token (parser->lexer);
9582 /* If the next token is `static_assert' we have a static assertion. */
9583 else if (token1->keyword == RID_STATIC_ASSERT)
9584 cp_parser_static_assert (parser, /*member_p=*/false);
9585 /* Anything else must be a simple-declaration. */
9586 else
9587 cp_parser_simple_declaration (parser, !statement_p);
9590 /* Parse a simple-declaration.
9592 simple-declaration:
9593 decl-specifier-seq [opt] init-declarator-list [opt] ;
9595 init-declarator-list:
9596 init-declarator
9597 init-declarator-list , init-declarator
9599 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9600 function-definition as a simple-declaration. */
9602 static void
9603 cp_parser_simple_declaration (cp_parser* parser,
9604 bool function_definition_allowed_p)
9606 cp_decl_specifier_seq decl_specifiers;
9607 int declares_class_or_enum;
9608 bool saw_declarator;
9610 /* Defer access checks until we know what is being declared; the
9611 checks for names appearing in the decl-specifier-seq should be
9612 done as if we were in the scope of the thing being declared. */
9613 push_deferring_access_checks (dk_deferred);
9615 /* Parse the decl-specifier-seq. We have to keep track of whether
9616 or not the decl-specifier-seq declares a named class or
9617 enumeration type, since that is the only case in which the
9618 init-declarator-list is allowed to be empty.
9620 [dcl.dcl]
9622 In a simple-declaration, the optional init-declarator-list can be
9623 omitted only when declaring a class or enumeration, that is when
9624 the decl-specifier-seq contains either a class-specifier, an
9625 elaborated-type-specifier, or an enum-specifier. */
9626 cp_parser_decl_specifier_seq (parser,
9627 CP_PARSER_FLAGS_OPTIONAL,
9628 &decl_specifiers,
9629 &declares_class_or_enum);
9630 /* We no longer need to defer access checks. */
9631 stop_deferring_access_checks ();
9633 /* In a block scope, a valid declaration must always have a
9634 decl-specifier-seq. By not trying to parse declarators, we can
9635 resolve the declaration/expression ambiguity more quickly. */
9636 if (!function_definition_allowed_p
9637 && !decl_specifiers.any_specifiers_p)
9639 cp_parser_error (parser, "expected declaration");
9640 goto done;
9643 /* If the next two tokens are both identifiers, the code is
9644 erroneous. The usual cause of this situation is code like:
9646 T t;
9648 where "T" should name a type -- but does not. */
9649 if (!decl_specifiers.any_type_specifiers_p
9650 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9652 /* If parsing tentatively, we should commit; we really are
9653 looking at a declaration. */
9654 cp_parser_commit_to_tentative_parse (parser);
9655 /* Give up. */
9656 goto done;
9659 /* If we have seen at least one decl-specifier, and the next token
9660 is not a parenthesis, then we must be looking at a declaration.
9661 (After "int (" we might be looking at a functional cast.) */
9662 if (decl_specifiers.any_specifiers_p
9663 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9665 && !cp_parser_error_occurred (parser))
9666 cp_parser_commit_to_tentative_parse (parser);
9668 /* Keep going until we hit the `;' at the end of the simple
9669 declaration. */
9670 saw_declarator = false;
9671 while (cp_lexer_next_token_is_not (parser->lexer,
9672 CPP_SEMICOLON))
9674 cp_token *token;
9675 bool function_definition_p;
9676 tree decl;
9678 if (saw_declarator)
9680 /* If we are processing next declarator, coma is expected */
9681 token = cp_lexer_peek_token (parser->lexer);
9682 gcc_assert (token->type == CPP_COMMA);
9683 cp_lexer_consume_token (parser->lexer);
9685 else
9686 saw_declarator = true;
9688 /* Parse the init-declarator. */
9689 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9690 /*checks=*/NULL,
9691 function_definition_allowed_p,
9692 /*member_p=*/false,
9693 declares_class_or_enum,
9694 &function_definition_p);
9695 /* If an error occurred while parsing tentatively, exit quickly.
9696 (That usually happens when in the body of a function; each
9697 statement is treated as a declaration-statement until proven
9698 otherwise.) */
9699 if (cp_parser_error_occurred (parser))
9700 goto done;
9701 /* Handle function definitions specially. */
9702 if (function_definition_p)
9704 /* If the next token is a `,', then we are probably
9705 processing something like:
9707 void f() {}, *p;
9709 which is erroneous. */
9710 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9712 cp_token *token = cp_lexer_peek_token (parser->lexer);
9713 error_at (token->location,
9714 "mixing"
9715 " declarations and function-definitions is forbidden");
9717 /* Otherwise, we're done with the list of declarators. */
9718 else
9720 pop_deferring_access_checks ();
9721 return;
9724 /* The next token should be either a `,' or a `;'. */
9725 token = cp_lexer_peek_token (parser->lexer);
9726 /* If it's a `,', there are more declarators to come. */
9727 if (token->type == CPP_COMMA)
9728 /* will be consumed next time around */;
9729 /* If it's a `;', we are done. */
9730 else if (token->type == CPP_SEMICOLON)
9731 break;
9732 /* Anything else is an error. */
9733 else
9735 /* If we have already issued an error message we don't need
9736 to issue another one. */
9737 if (decl != error_mark_node
9738 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9739 cp_parser_error (parser, "expected %<,%> or %<;%>");
9740 /* Skip tokens until we reach the end of the statement. */
9741 cp_parser_skip_to_end_of_statement (parser);
9742 /* If the next token is now a `;', consume it. */
9743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9744 cp_lexer_consume_token (parser->lexer);
9745 goto done;
9747 /* After the first time around, a function-definition is not
9748 allowed -- even if it was OK at first. For example:
9750 int i, f() {}
9752 is not valid. */
9753 function_definition_allowed_p = false;
9756 /* Issue an error message if no declarators are present, and the
9757 decl-specifier-seq does not itself declare a class or
9758 enumeration. */
9759 if (!saw_declarator)
9761 if (cp_parser_declares_only_class_p (parser))
9762 shadow_tag (&decl_specifiers);
9763 /* Perform any deferred access checks. */
9764 perform_deferred_access_checks ();
9767 /* Consume the `;'. */
9768 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9770 done:
9771 pop_deferring_access_checks ();
9774 /* Parse a decl-specifier-seq.
9776 decl-specifier-seq:
9777 decl-specifier-seq [opt] decl-specifier
9779 decl-specifier:
9780 storage-class-specifier
9781 type-specifier
9782 function-specifier
9783 friend
9784 typedef
9786 GNU Extension:
9788 decl-specifier:
9789 attributes
9791 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9793 The parser flags FLAGS is used to control type-specifier parsing.
9795 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9796 flags:
9798 1: one of the decl-specifiers is an elaborated-type-specifier
9799 (i.e., a type declaration)
9800 2: one of the decl-specifiers is an enum-specifier or a
9801 class-specifier (i.e., a type definition)
9805 static void
9806 cp_parser_decl_specifier_seq (cp_parser* parser,
9807 cp_parser_flags flags,
9808 cp_decl_specifier_seq *decl_specs,
9809 int* declares_class_or_enum)
9811 bool constructor_possible_p = !parser->in_declarator_p;
9812 cp_token *start_token = NULL;
9814 /* Clear DECL_SPECS. */
9815 clear_decl_specs (decl_specs);
9817 /* Assume no class or enumeration type is declared. */
9818 *declares_class_or_enum = 0;
9820 /* Keep reading specifiers until there are no more to read. */
9821 while (true)
9823 bool constructor_p;
9824 bool found_decl_spec;
9825 cp_token *token;
9827 /* Peek at the next token. */
9828 token = cp_lexer_peek_token (parser->lexer);
9830 /* Save the first token of the decl spec list for error
9831 reporting. */
9832 if (!start_token)
9833 start_token = token;
9834 /* Handle attributes. */
9835 if (token->keyword == RID_ATTRIBUTE)
9837 /* Parse the attributes. */
9838 decl_specs->attributes
9839 = chainon (decl_specs->attributes,
9840 cp_parser_attributes_opt (parser));
9841 continue;
9843 /* Assume we will find a decl-specifier keyword. */
9844 found_decl_spec = true;
9845 /* If the next token is an appropriate keyword, we can simply
9846 add it to the list. */
9847 switch (token->keyword)
9849 /* decl-specifier:
9850 friend
9851 constexpr */
9852 case RID_FRIEND:
9853 if (!at_class_scope_p ())
9855 error_at (token->location, "%<friend%> used outside of class");
9856 cp_lexer_purge_token (parser->lexer);
9858 else
9860 ++decl_specs->specs[(int) ds_friend];
9861 /* Consume the token. */
9862 cp_lexer_consume_token (parser->lexer);
9864 break;
9866 case RID_CONSTEXPR:
9867 ++decl_specs->specs[(int) ds_constexpr];
9868 cp_lexer_consume_token (parser->lexer);
9869 break;
9871 /* function-specifier:
9872 inline
9873 virtual
9874 explicit */
9875 case RID_INLINE:
9876 case RID_VIRTUAL:
9877 case RID_EXPLICIT:
9878 cp_parser_function_specifier_opt (parser, decl_specs);
9879 break;
9881 /* decl-specifier:
9882 typedef */
9883 case RID_TYPEDEF:
9884 ++decl_specs->specs[(int) ds_typedef];
9885 /* Consume the token. */
9886 cp_lexer_consume_token (parser->lexer);
9887 /* A constructor declarator cannot appear in a typedef. */
9888 constructor_possible_p = false;
9889 /* The "typedef" keyword can only occur in a declaration; we
9890 may as well commit at this point. */
9891 cp_parser_commit_to_tentative_parse (parser);
9893 if (decl_specs->storage_class != sc_none)
9894 decl_specs->conflicting_specifiers_p = true;
9895 break;
9897 /* storage-class-specifier:
9898 auto
9899 register
9900 static
9901 extern
9902 mutable
9904 GNU Extension:
9905 thread */
9906 case RID_AUTO:
9907 if (cxx_dialect == cxx98)
9909 /* Consume the token. */
9910 cp_lexer_consume_token (parser->lexer);
9912 /* Complain about `auto' as a storage specifier, if
9913 we're complaining about C++0x compatibility. */
9914 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9915 " will change meaning in C++0x; please remove it");
9917 /* Set the storage class anyway. */
9918 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9919 token->location);
9921 else
9922 /* C++0x auto type-specifier. */
9923 found_decl_spec = false;
9924 break;
9926 case RID_REGISTER:
9927 case RID_STATIC:
9928 case RID_EXTERN:
9929 case RID_MUTABLE:
9930 /* Consume the token. */
9931 cp_lexer_consume_token (parser->lexer);
9932 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9933 token->location);
9934 break;
9935 case RID_THREAD:
9936 /* Consume the token. */
9937 cp_lexer_consume_token (parser->lexer);
9938 ++decl_specs->specs[(int) ds_thread];
9939 break;
9941 default:
9942 /* We did not yet find a decl-specifier yet. */
9943 found_decl_spec = false;
9944 break;
9947 if (found_decl_spec
9948 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9949 && token->keyword != RID_CONSTEXPR)
9950 error ("decl-specifier invalid in condition");
9952 /* Constructors are a special case. The `S' in `S()' is not a
9953 decl-specifier; it is the beginning of the declarator. */
9954 constructor_p
9955 = (!found_decl_spec
9956 && constructor_possible_p
9957 && (cp_parser_constructor_declarator_p
9958 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9960 /* If we don't have a DECL_SPEC yet, then we must be looking at
9961 a type-specifier. */
9962 if (!found_decl_spec && !constructor_p)
9964 int decl_spec_declares_class_or_enum;
9965 bool is_cv_qualifier;
9966 tree type_spec;
9968 type_spec
9969 = cp_parser_type_specifier (parser, flags,
9970 decl_specs,
9971 /*is_declaration=*/true,
9972 &decl_spec_declares_class_or_enum,
9973 &is_cv_qualifier);
9974 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9976 /* If this type-specifier referenced a user-defined type
9977 (a typedef, class-name, etc.), then we can't allow any
9978 more such type-specifiers henceforth.
9980 [dcl.spec]
9982 The longest sequence of decl-specifiers that could
9983 possibly be a type name is taken as the
9984 decl-specifier-seq of a declaration. The sequence shall
9985 be self-consistent as described below.
9987 [dcl.type]
9989 As a general rule, at most one type-specifier is allowed
9990 in the complete decl-specifier-seq of a declaration. The
9991 only exceptions are the following:
9993 -- const or volatile can be combined with any other
9994 type-specifier.
9996 -- signed or unsigned can be combined with char, long,
9997 short, or int.
9999 -- ..
10001 Example:
10003 typedef char* Pc;
10004 void g (const int Pc);
10006 Here, Pc is *not* part of the decl-specifier seq; it's
10007 the declarator. Therefore, once we see a type-specifier
10008 (other than a cv-qualifier), we forbid any additional
10009 user-defined types. We *do* still allow things like `int
10010 int' to be considered a decl-specifier-seq, and issue the
10011 error message later. */
10012 if (type_spec && !is_cv_qualifier)
10013 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10014 /* A constructor declarator cannot follow a type-specifier. */
10015 if (type_spec)
10017 constructor_possible_p = false;
10018 found_decl_spec = true;
10019 if (!is_cv_qualifier)
10020 decl_specs->any_type_specifiers_p = true;
10024 /* If we still do not have a DECL_SPEC, then there are no more
10025 decl-specifiers. */
10026 if (!found_decl_spec)
10027 break;
10029 decl_specs->any_specifiers_p = true;
10030 /* After we see one decl-specifier, further decl-specifiers are
10031 always optional. */
10032 flags |= CP_PARSER_FLAGS_OPTIONAL;
10035 cp_parser_check_decl_spec (decl_specs, start_token->location);
10037 /* Don't allow a friend specifier with a class definition. */
10038 if (decl_specs->specs[(int) ds_friend] != 0
10039 && (*declares_class_or_enum & 2))
10040 error_at (start_token->location,
10041 "class definition may not be declared a friend");
10044 /* Parse an (optional) storage-class-specifier.
10046 storage-class-specifier:
10047 auto
10048 register
10049 static
10050 extern
10051 mutable
10053 GNU Extension:
10055 storage-class-specifier:
10056 thread
10058 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10060 static tree
10061 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10063 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10065 case RID_AUTO:
10066 if (cxx_dialect != cxx98)
10067 return NULL_TREE;
10068 /* Fall through for C++98. */
10070 case RID_REGISTER:
10071 case RID_STATIC:
10072 case RID_EXTERN:
10073 case RID_MUTABLE:
10074 case RID_THREAD:
10075 /* Consume the token. */
10076 return cp_lexer_consume_token (parser->lexer)->u.value;
10078 default:
10079 return NULL_TREE;
10083 /* Parse an (optional) function-specifier.
10085 function-specifier:
10086 inline
10087 virtual
10088 explicit
10090 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10091 Updates DECL_SPECS, if it is non-NULL. */
10093 static tree
10094 cp_parser_function_specifier_opt (cp_parser* parser,
10095 cp_decl_specifier_seq *decl_specs)
10097 cp_token *token = cp_lexer_peek_token (parser->lexer);
10098 switch (token->keyword)
10100 case RID_INLINE:
10101 if (decl_specs)
10102 ++decl_specs->specs[(int) ds_inline];
10103 break;
10105 case RID_VIRTUAL:
10106 /* 14.5.2.3 [temp.mem]
10108 A member function template shall not be virtual. */
10109 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10110 error_at (token->location, "templates may not be %<virtual%>");
10111 else if (decl_specs)
10112 ++decl_specs->specs[(int) ds_virtual];
10113 break;
10115 case RID_EXPLICIT:
10116 if (decl_specs)
10117 ++decl_specs->specs[(int) ds_explicit];
10118 break;
10120 default:
10121 return NULL_TREE;
10124 /* Consume the token. */
10125 return cp_lexer_consume_token (parser->lexer)->u.value;
10128 /* Parse a linkage-specification.
10130 linkage-specification:
10131 extern string-literal { declaration-seq [opt] }
10132 extern string-literal declaration */
10134 static void
10135 cp_parser_linkage_specification (cp_parser* parser)
10137 tree linkage;
10139 /* Look for the `extern' keyword. */
10140 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10142 /* Look for the string-literal. */
10143 linkage = cp_parser_string_literal (parser, false, false);
10145 /* Transform the literal into an identifier. If the literal is a
10146 wide-character string, or contains embedded NULs, then we can't
10147 handle it as the user wants. */
10148 if (strlen (TREE_STRING_POINTER (linkage))
10149 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10151 cp_parser_error (parser, "invalid linkage-specification");
10152 /* Assume C++ linkage. */
10153 linkage = lang_name_cplusplus;
10155 else
10156 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10158 /* We're now using the new linkage. */
10159 push_lang_context (linkage);
10161 /* If the next token is a `{', then we're using the first
10162 production. */
10163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10165 /* Consume the `{' token. */
10166 cp_lexer_consume_token (parser->lexer);
10167 /* Parse the declarations. */
10168 cp_parser_declaration_seq_opt (parser);
10169 /* Look for the closing `}'. */
10170 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10172 /* Otherwise, there's just one declaration. */
10173 else
10175 bool saved_in_unbraced_linkage_specification_p;
10177 saved_in_unbraced_linkage_specification_p
10178 = parser->in_unbraced_linkage_specification_p;
10179 parser->in_unbraced_linkage_specification_p = true;
10180 cp_parser_declaration (parser);
10181 parser->in_unbraced_linkage_specification_p
10182 = saved_in_unbraced_linkage_specification_p;
10185 /* We're done with the linkage-specification. */
10186 pop_lang_context ();
10189 /* Parse a static_assert-declaration.
10191 static_assert-declaration:
10192 static_assert ( constant-expression , string-literal ) ;
10194 If MEMBER_P, this static_assert is a class member. */
10196 static void
10197 cp_parser_static_assert(cp_parser *parser, bool member_p)
10199 tree condition;
10200 tree message;
10201 cp_token *token;
10202 location_t saved_loc;
10204 /* Peek at the `static_assert' token so we can keep track of exactly
10205 where the static assertion started. */
10206 token = cp_lexer_peek_token (parser->lexer);
10207 saved_loc = token->location;
10209 /* Look for the `static_assert' keyword. */
10210 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10211 RT_STATIC_ASSERT))
10212 return;
10214 /* We know we are in a static assertion; commit to any tentative
10215 parse. */
10216 if (cp_parser_parsing_tentatively (parser))
10217 cp_parser_commit_to_tentative_parse (parser);
10219 /* Parse the `(' starting the static assertion condition. */
10220 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10222 /* Parse the constant-expression. */
10223 condition =
10224 cp_parser_constant_expression (parser,
10225 /*allow_non_constant_p=*/false,
10226 /*non_constant_p=*/NULL);
10228 /* Parse the separating `,'. */
10229 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10231 /* Parse the string-literal message. */
10232 message = cp_parser_string_literal (parser,
10233 /*translate=*/false,
10234 /*wide_ok=*/true);
10236 /* A `)' completes the static assertion. */
10237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10238 cp_parser_skip_to_closing_parenthesis (parser,
10239 /*recovering=*/true,
10240 /*or_comma=*/false,
10241 /*consume_paren=*/true);
10243 /* A semicolon terminates the declaration. */
10244 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10246 /* Complete the static assertion, which may mean either processing
10247 the static assert now or saving it for template instantiation. */
10248 finish_static_assert (condition, message, saved_loc, member_p);
10251 /* Parse a `decltype' type. Returns the type.
10253 simple-type-specifier:
10254 decltype ( expression ) */
10256 static tree
10257 cp_parser_decltype (cp_parser *parser)
10259 tree expr;
10260 bool id_expression_or_member_access_p = false;
10261 const char *saved_message;
10262 bool saved_integral_constant_expression_p;
10263 bool saved_non_integral_constant_expression_p;
10264 cp_token *id_expr_start_token;
10266 /* Look for the `decltype' token. */
10267 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10268 return error_mark_node;
10270 /* Types cannot be defined in a `decltype' expression. Save away the
10271 old message. */
10272 saved_message = parser->type_definition_forbidden_message;
10274 /* And create the new one. */
10275 parser->type_definition_forbidden_message
10276 = G_("types may not be defined in %<decltype%> expressions");
10278 /* The restrictions on constant-expressions do not apply inside
10279 decltype expressions. */
10280 saved_integral_constant_expression_p
10281 = parser->integral_constant_expression_p;
10282 saved_non_integral_constant_expression_p
10283 = parser->non_integral_constant_expression_p;
10284 parser->integral_constant_expression_p = false;
10286 /* Do not actually evaluate the expression. */
10287 ++cp_unevaluated_operand;
10289 /* Do not warn about problems with the expression. */
10290 ++c_inhibit_evaluation_warnings;
10292 /* Parse the opening `('. */
10293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10294 return error_mark_node;
10296 /* First, try parsing an id-expression. */
10297 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10298 cp_parser_parse_tentatively (parser);
10299 expr = cp_parser_id_expression (parser,
10300 /*template_keyword_p=*/false,
10301 /*check_dependency_p=*/true,
10302 /*template_p=*/NULL,
10303 /*declarator_p=*/false,
10304 /*optional_p=*/false);
10306 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10308 bool non_integral_constant_expression_p = false;
10309 tree id_expression = expr;
10310 cp_id_kind idk;
10311 const char *error_msg;
10313 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10314 /* Lookup the name we got back from the id-expression. */
10315 expr = cp_parser_lookup_name (parser, expr,
10316 none_type,
10317 /*is_template=*/false,
10318 /*is_namespace=*/false,
10319 /*check_dependency=*/true,
10320 /*ambiguous_decls=*/NULL,
10321 id_expr_start_token->location);
10323 if (expr
10324 && expr != error_mark_node
10325 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10326 && TREE_CODE (expr) != TYPE_DECL
10327 && (TREE_CODE (expr) != BIT_NOT_EXPR
10328 || !TYPE_P (TREE_OPERAND (expr, 0)))
10329 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10331 /* Complete lookup of the id-expression. */
10332 expr = (finish_id_expression
10333 (id_expression, expr, parser->scope, &idk,
10334 /*integral_constant_expression_p=*/false,
10335 /*allow_non_integral_constant_expression_p=*/true,
10336 &non_integral_constant_expression_p,
10337 /*template_p=*/false,
10338 /*done=*/true,
10339 /*address_p=*/false,
10340 /*template_arg_p=*/false,
10341 &error_msg,
10342 id_expr_start_token->location));
10344 if (expr == error_mark_node)
10345 /* We found an id-expression, but it was something that we
10346 should not have found. This is an error, not something
10347 we can recover from, so note that we found an
10348 id-expression and we'll recover as gracefully as
10349 possible. */
10350 id_expression_or_member_access_p = true;
10353 if (expr
10354 && expr != error_mark_node
10355 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10356 /* We have an id-expression. */
10357 id_expression_or_member_access_p = true;
10360 if (!id_expression_or_member_access_p)
10362 /* Abort the id-expression parse. */
10363 cp_parser_abort_tentative_parse (parser);
10365 /* Parsing tentatively, again. */
10366 cp_parser_parse_tentatively (parser);
10368 /* Parse a class member access. */
10369 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10370 /*cast_p=*/false,
10371 /*member_access_only_p=*/true, NULL);
10373 if (expr
10374 && expr != error_mark_node
10375 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10376 /* We have an id-expression. */
10377 id_expression_or_member_access_p = true;
10380 if (id_expression_or_member_access_p)
10381 /* We have parsed the complete id-expression or member access. */
10382 cp_parser_parse_definitely (parser);
10383 else
10385 bool saved_greater_than_is_operator_p;
10387 /* Abort our attempt to parse an id-expression or member access
10388 expression. */
10389 cp_parser_abort_tentative_parse (parser);
10391 /* Within a parenthesized expression, a `>' token is always
10392 the greater-than operator. */
10393 saved_greater_than_is_operator_p
10394 = parser->greater_than_is_operator_p;
10395 parser->greater_than_is_operator_p = true;
10397 /* Parse a full expression. */
10398 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10400 /* The `>' token might be the end of a template-id or
10401 template-parameter-list now. */
10402 parser->greater_than_is_operator_p
10403 = saved_greater_than_is_operator_p;
10406 /* Go back to evaluating expressions. */
10407 --cp_unevaluated_operand;
10408 --c_inhibit_evaluation_warnings;
10410 /* Restore the old message and the integral constant expression
10411 flags. */
10412 parser->type_definition_forbidden_message = saved_message;
10413 parser->integral_constant_expression_p
10414 = saved_integral_constant_expression_p;
10415 parser->non_integral_constant_expression_p
10416 = saved_non_integral_constant_expression_p;
10418 if (expr == error_mark_node)
10420 /* Skip everything up to the closing `)'. */
10421 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10422 /*consume_paren=*/true);
10423 return error_mark_node;
10426 /* Parse to the closing `)'. */
10427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10429 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10430 /*consume_paren=*/true);
10431 return error_mark_node;
10434 return finish_decltype_type (expr, id_expression_or_member_access_p);
10437 /* Special member functions [gram.special] */
10439 /* Parse a conversion-function-id.
10441 conversion-function-id:
10442 operator conversion-type-id
10444 Returns an IDENTIFIER_NODE representing the operator. */
10446 static tree
10447 cp_parser_conversion_function_id (cp_parser* parser)
10449 tree type;
10450 tree saved_scope;
10451 tree saved_qualifying_scope;
10452 tree saved_object_scope;
10453 tree pushed_scope = NULL_TREE;
10455 /* Look for the `operator' token. */
10456 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10457 return error_mark_node;
10458 /* When we parse the conversion-type-id, the current scope will be
10459 reset. However, we need that information in able to look up the
10460 conversion function later, so we save it here. */
10461 saved_scope = parser->scope;
10462 saved_qualifying_scope = parser->qualifying_scope;
10463 saved_object_scope = parser->object_scope;
10464 /* We must enter the scope of the class so that the names of
10465 entities declared within the class are available in the
10466 conversion-type-id. For example, consider:
10468 struct S {
10469 typedef int I;
10470 operator I();
10473 S::operator I() { ... }
10475 In order to see that `I' is a type-name in the definition, we
10476 must be in the scope of `S'. */
10477 if (saved_scope)
10478 pushed_scope = push_scope (saved_scope);
10479 /* Parse the conversion-type-id. */
10480 type = cp_parser_conversion_type_id (parser);
10481 /* Leave the scope of the class, if any. */
10482 if (pushed_scope)
10483 pop_scope (pushed_scope);
10484 /* Restore the saved scope. */
10485 parser->scope = saved_scope;
10486 parser->qualifying_scope = saved_qualifying_scope;
10487 parser->object_scope = saved_object_scope;
10488 /* If the TYPE is invalid, indicate failure. */
10489 if (type == error_mark_node)
10490 return error_mark_node;
10491 return mangle_conv_op_name_for_type (type);
10494 /* Parse a conversion-type-id:
10496 conversion-type-id:
10497 type-specifier-seq conversion-declarator [opt]
10499 Returns the TYPE specified. */
10501 static tree
10502 cp_parser_conversion_type_id (cp_parser* parser)
10504 tree attributes;
10505 cp_decl_specifier_seq type_specifiers;
10506 cp_declarator *declarator;
10507 tree type_specified;
10509 /* Parse the attributes. */
10510 attributes = cp_parser_attributes_opt (parser);
10511 /* Parse the type-specifiers. */
10512 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10513 /*is_trailing_return=*/false,
10514 &type_specifiers);
10515 /* If that didn't work, stop. */
10516 if (type_specifiers.type == error_mark_node)
10517 return error_mark_node;
10518 /* Parse the conversion-declarator. */
10519 declarator = cp_parser_conversion_declarator_opt (parser);
10521 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10522 /*initialized=*/0, &attributes);
10523 if (attributes)
10524 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10526 /* Don't give this error when parsing tentatively. This happens to
10527 work because we always parse this definitively once. */
10528 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10529 && type_uses_auto (type_specified))
10531 error ("invalid use of %<auto%> in conversion operator");
10532 return error_mark_node;
10535 return type_specified;
10538 /* Parse an (optional) conversion-declarator.
10540 conversion-declarator:
10541 ptr-operator conversion-declarator [opt]
10545 static cp_declarator *
10546 cp_parser_conversion_declarator_opt (cp_parser* parser)
10548 enum tree_code code;
10549 tree class_type;
10550 cp_cv_quals cv_quals;
10552 /* We don't know if there's a ptr-operator next, or not. */
10553 cp_parser_parse_tentatively (parser);
10554 /* Try the ptr-operator. */
10555 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10556 /* If it worked, look for more conversion-declarators. */
10557 if (cp_parser_parse_definitely (parser))
10559 cp_declarator *declarator;
10561 /* Parse another optional declarator. */
10562 declarator = cp_parser_conversion_declarator_opt (parser);
10564 return cp_parser_make_indirect_declarator
10565 (code, class_type, cv_quals, declarator);
10568 return NULL;
10571 /* Parse an (optional) ctor-initializer.
10573 ctor-initializer:
10574 : mem-initializer-list
10576 Returns TRUE iff the ctor-initializer was actually present. */
10578 static bool
10579 cp_parser_ctor_initializer_opt (cp_parser* parser)
10581 /* If the next token is not a `:', then there is no
10582 ctor-initializer. */
10583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10585 /* Do default initialization of any bases and members. */
10586 if (DECL_CONSTRUCTOR_P (current_function_decl))
10587 finish_mem_initializers (NULL_TREE);
10589 return false;
10592 /* Consume the `:' token. */
10593 cp_lexer_consume_token (parser->lexer);
10594 /* And the mem-initializer-list. */
10595 cp_parser_mem_initializer_list (parser);
10597 return true;
10600 /* Parse a mem-initializer-list.
10602 mem-initializer-list:
10603 mem-initializer ... [opt]
10604 mem-initializer ... [opt] , mem-initializer-list */
10606 static void
10607 cp_parser_mem_initializer_list (cp_parser* parser)
10609 tree mem_initializer_list = NULL_TREE;
10610 cp_token *token = cp_lexer_peek_token (parser->lexer);
10612 /* Let the semantic analysis code know that we are starting the
10613 mem-initializer-list. */
10614 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10615 error_at (token->location,
10616 "only constructors take member initializers");
10618 /* Loop through the list. */
10619 while (true)
10621 tree mem_initializer;
10623 token = cp_lexer_peek_token (parser->lexer);
10624 /* Parse the mem-initializer. */
10625 mem_initializer = cp_parser_mem_initializer (parser);
10626 /* If the next token is a `...', we're expanding member initializers. */
10627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10629 /* Consume the `...'. */
10630 cp_lexer_consume_token (parser->lexer);
10632 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10633 can be expanded but members cannot. */
10634 if (mem_initializer != error_mark_node
10635 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10637 error_at (token->location,
10638 "cannot expand initializer for member %<%D%>",
10639 TREE_PURPOSE (mem_initializer));
10640 mem_initializer = error_mark_node;
10643 /* Construct the pack expansion type. */
10644 if (mem_initializer != error_mark_node)
10645 mem_initializer = make_pack_expansion (mem_initializer);
10647 /* Add it to the list, unless it was erroneous. */
10648 if (mem_initializer != error_mark_node)
10650 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10651 mem_initializer_list = mem_initializer;
10653 /* If the next token is not a `,', we're done. */
10654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10655 break;
10656 /* Consume the `,' token. */
10657 cp_lexer_consume_token (parser->lexer);
10660 /* Perform semantic analysis. */
10661 if (DECL_CONSTRUCTOR_P (current_function_decl))
10662 finish_mem_initializers (mem_initializer_list);
10665 /* Parse a mem-initializer.
10667 mem-initializer:
10668 mem-initializer-id ( expression-list [opt] )
10669 mem-initializer-id braced-init-list
10671 GNU extension:
10673 mem-initializer:
10674 ( expression-list [opt] )
10676 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10677 class) or FIELD_DECL (for a non-static data member) to initialize;
10678 the TREE_VALUE is the expression-list. An empty initialization
10679 list is represented by void_list_node. */
10681 static tree
10682 cp_parser_mem_initializer (cp_parser* parser)
10684 tree mem_initializer_id;
10685 tree expression_list;
10686 tree member;
10687 cp_token *token = cp_lexer_peek_token (parser->lexer);
10689 /* Find out what is being initialized. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10692 permerror (token->location,
10693 "anachronistic old-style base class initializer");
10694 mem_initializer_id = NULL_TREE;
10696 else
10698 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10699 if (mem_initializer_id == error_mark_node)
10700 return mem_initializer_id;
10702 member = expand_member_init (mem_initializer_id);
10703 if (member && !DECL_P (member))
10704 in_base_initializer = 1;
10706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10708 bool expr_non_constant_p;
10709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10710 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10711 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10712 expression_list = build_tree_list (NULL_TREE, expression_list);
10714 else
10716 VEC(tree,gc)* vec;
10717 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10718 /*cast_p=*/false,
10719 /*allow_expansion_p=*/true,
10720 /*non_constant_p=*/NULL);
10721 if (vec == NULL)
10722 return error_mark_node;
10723 expression_list = build_tree_list_vec (vec);
10724 release_tree_vector (vec);
10727 if (expression_list == error_mark_node)
10728 return error_mark_node;
10729 if (!expression_list)
10730 expression_list = void_type_node;
10732 in_base_initializer = 0;
10734 return member ? build_tree_list (member, expression_list) : error_mark_node;
10737 /* Parse a mem-initializer-id.
10739 mem-initializer-id:
10740 :: [opt] nested-name-specifier [opt] class-name
10741 identifier
10743 Returns a TYPE indicating the class to be initializer for the first
10744 production. Returns an IDENTIFIER_NODE indicating the data member
10745 to be initialized for the second production. */
10747 static tree
10748 cp_parser_mem_initializer_id (cp_parser* parser)
10750 bool global_scope_p;
10751 bool nested_name_specifier_p;
10752 bool template_p = false;
10753 tree id;
10755 cp_token *token = cp_lexer_peek_token (parser->lexer);
10757 /* `typename' is not allowed in this context ([temp.res]). */
10758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10760 error_at (token->location,
10761 "keyword %<typename%> not allowed in this context (a qualified "
10762 "member initializer is implicitly a type)");
10763 cp_lexer_consume_token (parser->lexer);
10765 /* Look for the optional `::' operator. */
10766 global_scope_p
10767 = (cp_parser_global_scope_opt (parser,
10768 /*current_scope_valid_p=*/false)
10769 != NULL_TREE);
10770 /* Look for the optional nested-name-specifier. The simplest way to
10771 implement:
10773 [temp.res]
10775 The keyword `typename' is not permitted in a base-specifier or
10776 mem-initializer; in these contexts a qualified name that
10777 depends on a template-parameter is implicitly assumed to be a
10778 type name.
10780 is to assume that we have seen the `typename' keyword at this
10781 point. */
10782 nested_name_specifier_p
10783 = (cp_parser_nested_name_specifier_opt (parser,
10784 /*typename_keyword_p=*/true,
10785 /*check_dependency_p=*/true,
10786 /*type_p=*/true,
10787 /*is_declaration=*/true)
10788 != NULL_TREE);
10789 if (nested_name_specifier_p)
10790 template_p = cp_parser_optional_template_keyword (parser);
10791 /* If there is a `::' operator or a nested-name-specifier, then we
10792 are definitely looking for a class-name. */
10793 if (global_scope_p || nested_name_specifier_p)
10794 return cp_parser_class_name (parser,
10795 /*typename_keyword_p=*/true,
10796 /*template_keyword_p=*/template_p,
10797 typename_type,
10798 /*check_dependency_p=*/true,
10799 /*class_head_p=*/false,
10800 /*is_declaration=*/true);
10801 /* Otherwise, we could also be looking for an ordinary identifier. */
10802 cp_parser_parse_tentatively (parser);
10803 /* Try a class-name. */
10804 id = cp_parser_class_name (parser,
10805 /*typename_keyword_p=*/true,
10806 /*template_keyword_p=*/false,
10807 none_type,
10808 /*check_dependency_p=*/true,
10809 /*class_head_p=*/false,
10810 /*is_declaration=*/true);
10811 /* If we found one, we're done. */
10812 if (cp_parser_parse_definitely (parser))
10813 return id;
10814 /* Otherwise, look for an ordinary identifier. */
10815 return cp_parser_identifier (parser);
10818 /* Overloading [gram.over] */
10820 /* Parse an operator-function-id.
10822 operator-function-id:
10823 operator operator
10825 Returns an IDENTIFIER_NODE for the operator which is a
10826 human-readable spelling of the identifier, e.g., `operator +'. */
10828 static tree
10829 cp_parser_operator_function_id (cp_parser* parser)
10831 /* Look for the `operator' keyword. */
10832 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10833 return error_mark_node;
10834 /* And then the name of the operator itself. */
10835 return cp_parser_operator (parser);
10838 /* Parse an operator.
10840 operator:
10841 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10842 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10843 || ++ -- , ->* -> () []
10845 GNU Extensions:
10847 operator:
10848 <? >? <?= >?=
10850 Returns an IDENTIFIER_NODE for the operator which is a
10851 human-readable spelling of the identifier, e.g., `operator +'. */
10853 static tree
10854 cp_parser_operator (cp_parser* parser)
10856 tree id = NULL_TREE;
10857 cp_token *token;
10859 /* Peek at the next token. */
10860 token = cp_lexer_peek_token (parser->lexer);
10861 /* Figure out which operator we have. */
10862 switch (token->type)
10864 case CPP_KEYWORD:
10866 enum tree_code op;
10868 /* The keyword should be either `new' or `delete'. */
10869 if (token->keyword == RID_NEW)
10870 op = NEW_EXPR;
10871 else if (token->keyword == RID_DELETE)
10872 op = DELETE_EXPR;
10873 else
10874 break;
10876 /* Consume the `new' or `delete' token. */
10877 cp_lexer_consume_token (parser->lexer);
10879 /* Peek at the next token. */
10880 token = cp_lexer_peek_token (parser->lexer);
10881 /* If it's a `[' token then this is the array variant of the
10882 operator. */
10883 if (token->type == CPP_OPEN_SQUARE)
10885 /* Consume the `[' token. */
10886 cp_lexer_consume_token (parser->lexer);
10887 /* Look for the `]' token. */
10888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10889 id = ansi_opname (op == NEW_EXPR
10890 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10892 /* Otherwise, we have the non-array variant. */
10893 else
10894 id = ansi_opname (op);
10896 return id;
10899 case CPP_PLUS:
10900 id = ansi_opname (PLUS_EXPR);
10901 break;
10903 case CPP_MINUS:
10904 id = ansi_opname (MINUS_EXPR);
10905 break;
10907 case CPP_MULT:
10908 id = ansi_opname (MULT_EXPR);
10909 break;
10911 case CPP_DIV:
10912 id = ansi_opname (TRUNC_DIV_EXPR);
10913 break;
10915 case CPP_MOD:
10916 id = ansi_opname (TRUNC_MOD_EXPR);
10917 break;
10919 case CPP_XOR:
10920 id = ansi_opname (BIT_XOR_EXPR);
10921 break;
10923 case CPP_AND:
10924 id = ansi_opname (BIT_AND_EXPR);
10925 break;
10927 case CPP_OR:
10928 id = ansi_opname (BIT_IOR_EXPR);
10929 break;
10931 case CPP_COMPL:
10932 id = ansi_opname (BIT_NOT_EXPR);
10933 break;
10935 case CPP_NOT:
10936 id = ansi_opname (TRUTH_NOT_EXPR);
10937 break;
10939 case CPP_EQ:
10940 id = ansi_assopname (NOP_EXPR);
10941 break;
10943 case CPP_LESS:
10944 id = ansi_opname (LT_EXPR);
10945 break;
10947 case CPP_GREATER:
10948 id = ansi_opname (GT_EXPR);
10949 break;
10951 case CPP_PLUS_EQ:
10952 id = ansi_assopname (PLUS_EXPR);
10953 break;
10955 case CPP_MINUS_EQ:
10956 id = ansi_assopname (MINUS_EXPR);
10957 break;
10959 case CPP_MULT_EQ:
10960 id = ansi_assopname (MULT_EXPR);
10961 break;
10963 case CPP_DIV_EQ:
10964 id = ansi_assopname (TRUNC_DIV_EXPR);
10965 break;
10967 case CPP_MOD_EQ:
10968 id = ansi_assopname (TRUNC_MOD_EXPR);
10969 break;
10971 case CPP_XOR_EQ:
10972 id = ansi_assopname (BIT_XOR_EXPR);
10973 break;
10975 case CPP_AND_EQ:
10976 id = ansi_assopname (BIT_AND_EXPR);
10977 break;
10979 case CPP_OR_EQ:
10980 id = ansi_assopname (BIT_IOR_EXPR);
10981 break;
10983 case CPP_LSHIFT:
10984 id = ansi_opname (LSHIFT_EXPR);
10985 break;
10987 case CPP_RSHIFT:
10988 id = ansi_opname (RSHIFT_EXPR);
10989 break;
10991 case CPP_LSHIFT_EQ:
10992 id = ansi_assopname (LSHIFT_EXPR);
10993 break;
10995 case CPP_RSHIFT_EQ:
10996 id = ansi_assopname (RSHIFT_EXPR);
10997 break;
10999 case CPP_EQ_EQ:
11000 id = ansi_opname (EQ_EXPR);
11001 break;
11003 case CPP_NOT_EQ:
11004 id = ansi_opname (NE_EXPR);
11005 break;
11007 case CPP_LESS_EQ:
11008 id = ansi_opname (LE_EXPR);
11009 break;
11011 case CPP_GREATER_EQ:
11012 id = ansi_opname (GE_EXPR);
11013 break;
11015 case CPP_AND_AND:
11016 id = ansi_opname (TRUTH_ANDIF_EXPR);
11017 break;
11019 case CPP_OR_OR:
11020 id = ansi_opname (TRUTH_ORIF_EXPR);
11021 break;
11023 case CPP_PLUS_PLUS:
11024 id = ansi_opname (POSTINCREMENT_EXPR);
11025 break;
11027 case CPP_MINUS_MINUS:
11028 id = ansi_opname (PREDECREMENT_EXPR);
11029 break;
11031 case CPP_COMMA:
11032 id = ansi_opname (COMPOUND_EXPR);
11033 break;
11035 case CPP_DEREF_STAR:
11036 id = ansi_opname (MEMBER_REF);
11037 break;
11039 case CPP_DEREF:
11040 id = ansi_opname (COMPONENT_REF);
11041 break;
11043 case CPP_OPEN_PAREN:
11044 /* Consume the `('. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* Look for the matching `)'. */
11047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11048 return ansi_opname (CALL_EXPR);
11050 case CPP_OPEN_SQUARE:
11051 /* Consume the `['. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Look for the matching `]'. */
11054 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11055 return ansi_opname (ARRAY_REF);
11057 default:
11058 /* Anything else is an error. */
11059 break;
11062 /* If we have selected an identifier, we need to consume the
11063 operator token. */
11064 if (id)
11065 cp_lexer_consume_token (parser->lexer);
11066 /* Otherwise, no valid operator name was present. */
11067 else
11069 cp_parser_error (parser, "expected operator");
11070 id = error_mark_node;
11073 return id;
11076 /* Parse a template-declaration.
11078 template-declaration:
11079 export [opt] template < template-parameter-list > declaration
11081 If MEMBER_P is TRUE, this template-declaration occurs within a
11082 class-specifier.
11084 The grammar rule given by the standard isn't correct. What
11085 is really meant is:
11087 template-declaration:
11088 export [opt] template-parameter-list-seq
11089 decl-specifier-seq [opt] init-declarator [opt] ;
11090 export [opt] template-parameter-list-seq
11091 function-definition
11093 template-parameter-list-seq:
11094 template-parameter-list-seq [opt]
11095 template < template-parameter-list > */
11097 static void
11098 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11100 /* Check for `export'. */
11101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11103 /* Consume the `export' token. */
11104 cp_lexer_consume_token (parser->lexer);
11105 /* Warn that we do not support `export'. */
11106 warning (0, "keyword %<export%> not implemented, and will be ignored");
11109 cp_parser_template_declaration_after_export (parser, member_p);
11112 /* Parse a template-parameter-list.
11114 template-parameter-list:
11115 template-parameter
11116 template-parameter-list , template-parameter
11118 Returns a TREE_LIST. Each node represents a template parameter.
11119 The nodes are connected via their TREE_CHAINs. */
11121 static tree
11122 cp_parser_template_parameter_list (cp_parser* parser)
11124 tree parameter_list = NULL_TREE;
11126 begin_template_parm_list ();
11128 /* The loop below parses the template parms. We first need to know
11129 the total number of template parms to be able to compute proper
11130 canonical types of each dependent type. So after the loop, when
11131 we know the total number of template parms,
11132 end_template_parm_list computes the proper canonical types and
11133 fixes up the dependent types accordingly. */
11134 while (true)
11136 tree parameter;
11137 bool is_non_type;
11138 bool is_parameter_pack;
11139 location_t parm_loc;
11141 /* Parse the template-parameter. */
11142 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11143 parameter = cp_parser_template_parameter (parser,
11144 &is_non_type,
11145 &is_parameter_pack);
11146 /* Add it to the list. */
11147 if (parameter != error_mark_node)
11148 parameter_list = process_template_parm (parameter_list,
11149 parm_loc,
11150 parameter,
11151 is_non_type,
11152 is_parameter_pack,
11154 else
11156 tree err_parm = build_tree_list (parameter, parameter);
11157 parameter_list = chainon (parameter_list, err_parm);
11160 /* If the next token is not a `,', we're done. */
11161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11162 break;
11163 /* Otherwise, consume the `,' token. */
11164 cp_lexer_consume_token (parser->lexer);
11167 return end_template_parm_list (parameter_list);
11170 /* Parse a template-parameter.
11172 template-parameter:
11173 type-parameter
11174 parameter-declaration
11176 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11177 the parameter. The TREE_PURPOSE is the default value, if any.
11178 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11179 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11180 set to true iff this parameter is a parameter pack. */
11182 static tree
11183 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11184 bool *is_parameter_pack)
11186 cp_token *token;
11187 cp_parameter_declarator *parameter_declarator;
11188 cp_declarator *id_declarator;
11189 tree parm;
11191 /* Assume it is a type parameter or a template parameter. */
11192 *is_non_type = false;
11193 /* Assume it not a parameter pack. */
11194 *is_parameter_pack = false;
11195 /* Peek at the next token. */
11196 token = cp_lexer_peek_token (parser->lexer);
11197 /* If it is `class' or `template', we have a type-parameter. */
11198 if (token->keyword == RID_TEMPLATE)
11199 return cp_parser_type_parameter (parser, is_parameter_pack);
11200 /* If it is `class' or `typename' we do not know yet whether it is a
11201 type parameter or a non-type parameter. Consider:
11203 template <typename T, typename T::X X> ...
11207 template <class C, class D*> ...
11209 Here, the first parameter is a type parameter, and the second is
11210 a non-type parameter. We can tell by looking at the token after
11211 the identifier -- if it is a `,', `=', or `>' then we have a type
11212 parameter. */
11213 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11215 /* Peek at the token after `class' or `typename'. */
11216 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11217 /* If it's an ellipsis, we have a template type parameter
11218 pack. */
11219 if (token->type == CPP_ELLIPSIS)
11220 return cp_parser_type_parameter (parser, is_parameter_pack);
11221 /* If it's an identifier, skip it. */
11222 if (token->type == CPP_NAME)
11223 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11224 /* Now, see if the token looks like the end of a template
11225 parameter. */
11226 if (token->type == CPP_COMMA
11227 || token->type == CPP_EQ
11228 || token->type == CPP_GREATER)
11229 return cp_parser_type_parameter (parser, is_parameter_pack);
11232 /* Otherwise, it is a non-type parameter.
11234 [temp.param]
11236 When parsing a default template-argument for a non-type
11237 template-parameter, the first non-nested `>' is taken as the end
11238 of the template parameter-list rather than a greater-than
11239 operator. */
11240 *is_non_type = true;
11241 parameter_declarator
11242 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11243 /*parenthesized_p=*/NULL);
11245 /* If the parameter declaration is marked as a parameter pack, set
11246 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11247 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11248 grokdeclarator. */
11249 if (parameter_declarator
11250 && parameter_declarator->declarator
11251 && parameter_declarator->declarator->parameter_pack_p)
11253 *is_parameter_pack = true;
11254 parameter_declarator->declarator->parameter_pack_p = false;
11257 /* If the next token is an ellipsis, and we don't already have it
11258 marked as a parameter pack, then we have a parameter pack (that
11259 has no declarator). */
11260 if (!*is_parameter_pack
11261 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11262 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11264 /* Consume the `...'. */
11265 cp_lexer_consume_token (parser->lexer);
11266 maybe_warn_variadic_templates ();
11268 *is_parameter_pack = true;
11270 /* We might end up with a pack expansion as the type of the non-type
11271 template parameter, in which case this is a non-type template
11272 parameter pack. */
11273 else if (parameter_declarator
11274 && parameter_declarator->decl_specifiers.type
11275 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11277 *is_parameter_pack = true;
11278 parameter_declarator->decl_specifiers.type =
11279 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11282 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11284 /* Parameter packs cannot have default arguments. However, a
11285 user may try to do so, so we'll parse them and give an
11286 appropriate diagnostic here. */
11288 /* Consume the `='. */
11289 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11290 cp_lexer_consume_token (parser->lexer);
11292 /* Find the name of the parameter pack. */
11293 id_declarator = parameter_declarator->declarator;
11294 while (id_declarator && id_declarator->kind != cdk_id)
11295 id_declarator = id_declarator->declarator;
11297 if (id_declarator && id_declarator->kind == cdk_id)
11298 error_at (start_token->location,
11299 "template parameter pack %qD cannot have a default argument",
11300 id_declarator->u.id.unqualified_name);
11301 else
11302 error_at (start_token->location,
11303 "template parameter pack cannot have a default argument");
11305 /* Parse the default argument, but throw away the result. */
11306 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11309 parm = grokdeclarator (parameter_declarator->declarator,
11310 &parameter_declarator->decl_specifiers,
11311 TPARM, /*initialized=*/0,
11312 /*attrlist=*/NULL);
11313 if (parm == error_mark_node)
11314 return error_mark_node;
11316 return build_tree_list (parameter_declarator->default_argument, parm);
11319 /* Parse a type-parameter.
11321 type-parameter:
11322 class identifier [opt]
11323 class identifier [opt] = type-id
11324 typename identifier [opt]
11325 typename identifier [opt] = type-id
11326 template < template-parameter-list > class identifier [opt]
11327 template < template-parameter-list > class identifier [opt]
11328 = id-expression
11330 GNU Extension (variadic templates):
11332 type-parameter:
11333 class ... identifier [opt]
11334 typename ... identifier [opt]
11336 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11337 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11338 the declaration of the parameter.
11340 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11342 static tree
11343 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11345 cp_token *token;
11346 tree parameter;
11348 /* Look for a keyword to tell us what kind of parameter this is. */
11349 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11350 if (!token)
11351 return error_mark_node;
11353 switch (token->keyword)
11355 case RID_CLASS:
11356 case RID_TYPENAME:
11358 tree identifier;
11359 tree default_argument;
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;
11372 /* If the next token is an identifier, then it names the
11373 parameter. */
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11375 identifier = cp_parser_identifier (parser);
11376 else
11377 identifier = NULL_TREE;
11379 /* Create the parameter. */
11380 parameter = finish_template_type_parm (class_type_node, identifier);
11382 /* If the next token is an `=', we have a default argument. */
11383 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11385 /* Consume the `=' token. */
11386 cp_lexer_consume_token (parser->lexer);
11387 /* Parse the default-argument. */
11388 push_deferring_access_checks (dk_no_deferred);
11389 default_argument = cp_parser_type_id (parser);
11391 /* Template parameter packs cannot have default
11392 arguments. */
11393 if (*is_parameter_pack)
11395 if (identifier)
11396 error_at (token->location,
11397 "template parameter pack %qD cannot have a "
11398 "default argument", identifier);
11399 else
11400 error_at (token->location,
11401 "template parameter packs cannot have "
11402 "default arguments");
11403 default_argument = NULL_TREE;
11405 pop_deferring_access_checks ();
11407 else
11408 default_argument = NULL_TREE;
11410 /* Create the combined representation of the parameter and the
11411 default argument. */
11412 parameter = build_tree_list (default_argument, parameter);
11414 break;
11416 case RID_TEMPLATE:
11418 tree identifier;
11419 tree default_argument;
11421 /* Look for the `<'. */
11422 cp_parser_require (parser, CPP_LESS, RT_LESS);
11423 /* Parse the template-parameter-list. */
11424 cp_parser_template_parameter_list (parser);
11425 /* Look for the `>'. */
11426 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11427 /* Look for the `class' keyword. */
11428 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11429 /* If the next token is an ellipsis, we have a template
11430 argument pack. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11433 /* Consume the `...' token. */
11434 cp_lexer_consume_token (parser->lexer);
11435 maybe_warn_variadic_templates ();
11437 *is_parameter_pack = true;
11439 /* If the next token is an `=', then there is a
11440 default-argument. If the next token is a `>', we are at
11441 the end of the parameter-list. If the next token is a `,',
11442 then we are at the end of this parameter. */
11443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11444 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11445 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11447 identifier = cp_parser_identifier (parser);
11448 /* Treat invalid names as if the parameter were nameless. */
11449 if (identifier == error_mark_node)
11450 identifier = NULL_TREE;
11452 else
11453 identifier = NULL_TREE;
11455 /* Create the template parameter. */
11456 parameter = finish_template_template_parm (class_type_node,
11457 identifier);
11459 /* If the next token is an `=', then there is a
11460 default-argument. */
11461 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11463 bool is_template;
11465 /* Consume the `='. */
11466 cp_lexer_consume_token (parser->lexer);
11467 /* Parse the id-expression. */
11468 push_deferring_access_checks (dk_no_deferred);
11469 /* save token before parsing the id-expression, for error
11470 reporting */
11471 token = cp_lexer_peek_token (parser->lexer);
11472 default_argument
11473 = cp_parser_id_expression (parser,
11474 /*template_keyword_p=*/false,
11475 /*check_dependency_p=*/true,
11476 /*template_p=*/&is_template,
11477 /*declarator_p=*/false,
11478 /*optional_p=*/false);
11479 if (TREE_CODE (default_argument) == TYPE_DECL)
11480 /* If the id-expression was a template-id that refers to
11481 a template-class, we already have the declaration here,
11482 so no further lookup is needed. */
11484 else
11485 /* Look up the name. */
11486 default_argument
11487 = cp_parser_lookup_name (parser, default_argument,
11488 none_type,
11489 /*is_template=*/is_template,
11490 /*is_namespace=*/false,
11491 /*check_dependency=*/true,
11492 /*ambiguous_decls=*/NULL,
11493 token->location);
11494 /* See if the default argument is valid. */
11495 default_argument
11496 = check_template_template_default_arg (default_argument);
11498 /* Template parameter packs cannot have default
11499 arguments. */
11500 if (*is_parameter_pack)
11502 if (identifier)
11503 error_at (token->location,
11504 "template parameter pack %qD cannot "
11505 "have a default argument",
11506 identifier);
11507 else
11508 error_at (token->location, "template parameter packs cannot "
11509 "have default arguments");
11510 default_argument = NULL_TREE;
11512 pop_deferring_access_checks ();
11514 else
11515 default_argument = NULL_TREE;
11517 /* Create the combined representation of the parameter and the
11518 default argument. */
11519 parameter = build_tree_list (default_argument, parameter);
11521 break;
11523 default:
11524 gcc_unreachable ();
11525 break;
11528 return parameter;
11531 /* Parse a template-id.
11533 template-id:
11534 template-name < template-argument-list [opt] >
11536 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11537 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11538 returned. Otherwise, if the template-name names a function, or set
11539 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11540 names a class, returns a TYPE_DECL for the specialization.
11542 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11543 uninstantiated templates. */
11545 static tree
11546 cp_parser_template_id (cp_parser *parser,
11547 bool template_keyword_p,
11548 bool check_dependency_p,
11549 bool is_declaration)
11551 int i;
11552 tree templ;
11553 tree arguments;
11554 tree template_id;
11555 cp_token_position start_of_id = 0;
11556 deferred_access_check *chk;
11557 VEC (deferred_access_check,gc) *access_check;
11558 cp_token *next_token = NULL, *next_token_2 = NULL;
11559 bool is_identifier;
11561 /* If the next token corresponds to a template-id, there is no need
11562 to reparse it. */
11563 next_token = cp_lexer_peek_token (parser->lexer);
11564 if (next_token->type == CPP_TEMPLATE_ID)
11566 struct tree_check *check_value;
11568 /* Get the stored value. */
11569 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11570 /* Perform any access checks that were deferred. */
11571 access_check = check_value->checks;
11572 if (access_check)
11574 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11575 perform_or_defer_access_check (chk->binfo,
11576 chk->decl,
11577 chk->diag_decl);
11579 /* Return the stored value. */
11580 return check_value->value;
11583 /* Avoid performing name lookup if there is no possibility of
11584 finding a template-id. */
11585 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11586 || (next_token->type == CPP_NAME
11587 && !cp_parser_nth_token_starts_template_argument_list_p
11588 (parser, 2)))
11590 cp_parser_error (parser, "expected template-id");
11591 return error_mark_node;
11594 /* Remember where the template-id starts. */
11595 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11596 start_of_id = cp_lexer_token_position (parser->lexer, false);
11598 push_deferring_access_checks (dk_deferred);
11600 /* Parse the template-name. */
11601 is_identifier = false;
11602 templ = cp_parser_template_name (parser, template_keyword_p,
11603 check_dependency_p,
11604 is_declaration,
11605 &is_identifier);
11606 if (templ == error_mark_node || is_identifier)
11608 pop_deferring_access_checks ();
11609 return templ;
11612 /* If we find the sequence `[:' after a template-name, it's probably
11613 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11614 parse correctly the argument list. */
11615 next_token = cp_lexer_peek_token (parser->lexer);
11616 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11617 if (next_token->type == CPP_OPEN_SQUARE
11618 && next_token->flags & DIGRAPH
11619 && next_token_2->type == CPP_COLON
11620 && !(next_token_2->flags & PREV_WHITE))
11622 cp_parser_parse_tentatively (parser);
11623 /* Change `:' into `::'. */
11624 next_token_2->type = CPP_SCOPE;
11625 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11626 CPP_LESS. */
11627 cp_lexer_consume_token (parser->lexer);
11629 /* Parse the arguments. */
11630 arguments = cp_parser_enclosed_template_argument_list (parser);
11631 if (!cp_parser_parse_definitely (parser))
11633 /* If we couldn't parse an argument list, then we revert our changes
11634 and return simply an error. Maybe this is not a template-id
11635 after all. */
11636 next_token_2->type = CPP_COLON;
11637 cp_parser_error (parser, "expected %<<%>");
11638 pop_deferring_access_checks ();
11639 return error_mark_node;
11641 /* Otherwise, emit an error about the invalid digraph, but continue
11642 parsing because we got our argument list. */
11643 if (permerror (next_token->location,
11644 "%<<::%> cannot begin a template-argument list"))
11646 static bool hint = false;
11647 inform (next_token->location,
11648 "%<<:%> is an alternate spelling for %<[%>."
11649 " Insert whitespace between %<<%> and %<::%>");
11650 if (!hint && !flag_permissive)
11652 inform (next_token->location, "(if you use %<-fpermissive%>"
11653 " G++ will accept your code)");
11654 hint = true;
11658 else
11660 /* Look for the `<' that starts the template-argument-list. */
11661 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11663 pop_deferring_access_checks ();
11664 return error_mark_node;
11666 /* Parse the arguments. */
11667 arguments = cp_parser_enclosed_template_argument_list (parser);
11670 /* Build a representation of the specialization. */
11671 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11672 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11673 else if (DECL_CLASS_TEMPLATE_P (templ)
11674 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11676 bool entering_scope;
11677 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11678 template (rather than some instantiation thereof) only if
11679 is not nested within some other construct. For example, in
11680 "template <typename T> void f(T) { A<T>::", A<T> is just an
11681 instantiation of A. */
11682 entering_scope = (template_parm_scope_p ()
11683 && cp_lexer_next_token_is (parser->lexer,
11684 CPP_SCOPE));
11685 template_id
11686 = finish_template_type (templ, arguments, entering_scope);
11688 else
11690 /* If it's not a class-template or a template-template, it should be
11691 a function-template. */
11692 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11693 || TREE_CODE (templ) == OVERLOAD
11694 || BASELINK_P (templ)));
11696 template_id = lookup_template_function (templ, arguments);
11699 /* If parsing tentatively, replace the sequence of tokens that makes
11700 up the template-id with a CPP_TEMPLATE_ID token. That way,
11701 should we re-parse the token stream, we will not have to repeat
11702 the effort required to do the parse, nor will we issue duplicate
11703 error messages about problems during instantiation of the
11704 template. */
11705 if (start_of_id)
11707 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11709 /* Reset the contents of the START_OF_ID token. */
11710 token->type = CPP_TEMPLATE_ID;
11711 /* Retrieve any deferred checks. Do not pop this access checks yet
11712 so the memory will not be reclaimed during token replacing below. */
11713 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11714 token->u.tree_check_value->value = template_id;
11715 token->u.tree_check_value->checks = get_deferred_access_checks ();
11716 token->keyword = RID_MAX;
11718 /* Purge all subsequent tokens. */
11719 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11721 /* ??? Can we actually assume that, if template_id ==
11722 error_mark_node, we will have issued a diagnostic to the
11723 user, as opposed to simply marking the tentative parse as
11724 failed? */
11725 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11726 error_at (token->location, "parse error in template argument list");
11729 pop_deferring_access_checks ();
11730 return template_id;
11733 /* Parse a template-name.
11735 template-name:
11736 identifier
11738 The standard should actually say:
11740 template-name:
11741 identifier
11742 operator-function-id
11744 A defect report has been filed about this issue.
11746 A conversion-function-id cannot be a template name because they cannot
11747 be part of a template-id. In fact, looking at this code:
11749 a.operator K<int>()
11751 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11752 It is impossible to call a templated conversion-function-id with an
11753 explicit argument list, since the only allowed template parameter is
11754 the type to which it is converting.
11756 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11757 `template' keyword, in a construction like:
11759 T::template f<3>()
11761 In that case `f' is taken to be a template-name, even though there
11762 is no way of knowing for sure.
11764 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11765 name refers to a set of overloaded functions, at least one of which
11766 is a template, or an IDENTIFIER_NODE with the name of the template,
11767 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11768 names are looked up inside uninstantiated templates. */
11770 static tree
11771 cp_parser_template_name (cp_parser* parser,
11772 bool template_keyword_p,
11773 bool check_dependency_p,
11774 bool is_declaration,
11775 bool *is_identifier)
11777 tree identifier;
11778 tree decl;
11779 tree fns;
11780 cp_token *token = cp_lexer_peek_token (parser->lexer);
11782 /* If the next token is `operator', then we have either an
11783 operator-function-id or a conversion-function-id. */
11784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11786 /* We don't know whether we're looking at an
11787 operator-function-id or a conversion-function-id. */
11788 cp_parser_parse_tentatively (parser);
11789 /* Try an operator-function-id. */
11790 identifier = cp_parser_operator_function_id (parser);
11791 /* If that didn't work, try a conversion-function-id. */
11792 if (!cp_parser_parse_definitely (parser))
11794 cp_parser_error (parser, "expected template-name");
11795 return error_mark_node;
11798 /* Look for the identifier. */
11799 else
11800 identifier = cp_parser_identifier (parser);
11802 /* If we didn't find an identifier, we don't have a template-id. */
11803 if (identifier == error_mark_node)
11804 return error_mark_node;
11806 /* If the name immediately followed the `template' keyword, then it
11807 is a template-name. However, if the next token is not `<', then
11808 we do not treat it as a template-name, since it is not being used
11809 as part of a template-id. This enables us to handle constructs
11810 like:
11812 template <typename T> struct S { S(); };
11813 template <typename T> S<T>::S();
11815 correctly. We would treat `S' as a template -- if it were `S<T>'
11816 -- but we do not if there is no `<'. */
11818 if (processing_template_decl
11819 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11821 /* In a declaration, in a dependent context, we pretend that the
11822 "template" keyword was present in order to improve error
11823 recovery. For example, given:
11825 template <typename T> void f(T::X<int>);
11827 we want to treat "X<int>" as a template-id. */
11828 if (is_declaration
11829 && !template_keyword_p
11830 && parser->scope && TYPE_P (parser->scope)
11831 && check_dependency_p
11832 && dependent_scope_p (parser->scope)
11833 /* Do not do this for dtors (or ctors), since they never
11834 need the template keyword before their name. */
11835 && !constructor_name_p (identifier, parser->scope))
11837 cp_token_position start = 0;
11839 /* Explain what went wrong. */
11840 error_at (token->location, "non-template %qD used as template",
11841 identifier);
11842 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11843 parser->scope, identifier);
11844 /* If parsing tentatively, find the location of the "<" token. */
11845 if (cp_parser_simulate_error (parser))
11846 start = cp_lexer_token_position (parser->lexer, true);
11847 /* Parse the template arguments so that we can issue error
11848 messages about them. */
11849 cp_lexer_consume_token (parser->lexer);
11850 cp_parser_enclosed_template_argument_list (parser);
11851 /* Skip tokens until we find a good place from which to
11852 continue parsing. */
11853 cp_parser_skip_to_closing_parenthesis (parser,
11854 /*recovering=*/true,
11855 /*or_comma=*/true,
11856 /*consume_paren=*/false);
11857 /* If parsing tentatively, permanently remove the
11858 template argument list. That will prevent duplicate
11859 error messages from being issued about the missing
11860 "template" keyword. */
11861 if (start)
11862 cp_lexer_purge_tokens_after (parser->lexer, start);
11863 if (is_identifier)
11864 *is_identifier = true;
11865 return identifier;
11868 /* If the "template" keyword is present, then there is generally
11869 no point in doing name-lookup, so we just return IDENTIFIER.
11870 But, if the qualifying scope is non-dependent then we can
11871 (and must) do name-lookup normally. */
11872 if (template_keyword_p
11873 && (!parser->scope
11874 || (TYPE_P (parser->scope)
11875 && dependent_type_p (parser->scope))))
11876 return identifier;
11879 /* Look up the name. */
11880 decl = cp_parser_lookup_name (parser, identifier,
11881 none_type,
11882 /*is_template=*/true,
11883 /*is_namespace=*/false,
11884 check_dependency_p,
11885 /*ambiguous_decls=*/NULL,
11886 token->location);
11888 /* If DECL is a template, then the name was a template-name. */
11889 if (TREE_CODE (decl) == TEMPLATE_DECL)
11891 else
11893 tree fn = NULL_TREE;
11895 /* The standard does not explicitly indicate whether a name that
11896 names a set of overloaded declarations, some of which are
11897 templates, is a template-name. However, such a name should
11898 be a template-name; otherwise, there is no way to form a
11899 template-id for the overloaded templates. */
11900 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11901 if (TREE_CODE (fns) == OVERLOAD)
11902 for (fn = fns; fn; fn = OVL_NEXT (fn))
11903 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11904 break;
11906 if (!fn)
11908 /* The name does not name a template. */
11909 cp_parser_error (parser, "expected template-name");
11910 return error_mark_node;
11914 /* If DECL is dependent, and refers to a function, then just return
11915 its name; we will look it up again during template instantiation. */
11916 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11918 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11919 if (TYPE_P (scope) && dependent_type_p (scope))
11920 return identifier;
11923 return decl;
11926 /* Parse a template-argument-list.
11928 template-argument-list:
11929 template-argument ... [opt]
11930 template-argument-list , template-argument ... [opt]
11932 Returns a TREE_VEC containing the arguments. */
11934 static tree
11935 cp_parser_template_argument_list (cp_parser* parser)
11937 tree fixed_args[10];
11938 unsigned n_args = 0;
11939 unsigned alloced = 10;
11940 tree *arg_ary = fixed_args;
11941 tree vec;
11942 bool saved_in_template_argument_list_p;
11943 bool saved_ice_p;
11944 bool saved_non_ice_p;
11946 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11947 parser->in_template_argument_list_p = true;
11948 /* Even if the template-id appears in an integral
11949 constant-expression, the contents of the argument list do
11950 not. */
11951 saved_ice_p = parser->integral_constant_expression_p;
11952 parser->integral_constant_expression_p = false;
11953 saved_non_ice_p = parser->non_integral_constant_expression_p;
11954 parser->non_integral_constant_expression_p = false;
11955 /* Parse the arguments. */
11958 tree argument;
11960 if (n_args)
11961 /* Consume the comma. */
11962 cp_lexer_consume_token (parser->lexer);
11964 /* Parse the template-argument. */
11965 argument = cp_parser_template_argument (parser);
11967 /* If the next token is an ellipsis, we're expanding a template
11968 argument pack. */
11969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11971 if (argument == error_mark_node)
11973 cp_token *token = cp_lexer_peek_token (parser->lexer);
11974 error_at (token->location,
11975 "expected parameter pack before %<...%>");
11977 /* Consume the `...' token. */
11978 cp_lexer_consume_token (parser->lexer);
11980 /* Make the argument into a TYPE_PACK_EXPANSION or
11981 EXPR_PACK_EXPANSION. */
11982 argument = make_pack_expansion (argument);
11985 if (n_args == alloced)
11987 alloced *= 2;
11989 if (arg_ary == fixed_args)
11991 arg_ary = XNEWVEC (tree, alloced);
11992 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11994 else
11995 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11997 arg_ary[n_args++] = argument;
11999 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12001 vec = make_tree_vec (n_args);
12003 while (n_args--)
12004 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12006 if (arg_ary != fixed_args)
12007 free (arg_ary);
12008 parser->non_integral_constant_expression_p = saved_non_ice_p;
12009 parser->integral_constant_expression_p = saved_ice_p;
12010 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12011 #ifdef ENABLE_CHECKING
12012 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12013 #endif
12014 return vec;
12017 /* Parse a template-argument.
12019 template-argument:
12020 assignment-expression
12021 type-id
12022 id-expression
12024 The representation is that of an assignment-expression, type-id, or
12025 id-expression -- except that the qualified id-expression is
12026 evaluated, so that the value returned is either a DECL or an
12027 OVERLOAD.
12029 Although the standard says "assignment-expression", it forbids
12030 throw-expressions or assignments in the template argument.
12031 Therefore, we use "conditional-expression" instead. */
12033 static tree
12034 cp_parser_template_argument (cp_parser* parser)
12036 tree argument;
12037 bool template_p;
12038 bool address_p;
12039 bool maybe_type_id = false;
12040 cp_token *token = NULL, *argument_start_token = NULL;
12041 cp_id_kind idk;
12043 /* There's really no way to know what we're looking at, so we just
12044 try each alternative in order.
12046 [temp.arg]
12048 In a template-argument, an ambiguity between a type-id and an
12049 expression is resolved to a type-id, regardless of the form of
12050 the corresponding template-parameter.
12052 Therefore, we try a type-id first. */
12053 cp_parser_parse_tentatively (parser);
12054 argument = cp_parser_template_type_arg (parser);
12055 /* If there was no error parsing the type-id but the next token is a
12056 '>>', our behavior depends on which dialect of C++ we're
12057 parsing. In C++98, we probably found a typo for '> >'. But there
12058 are type-id which are also valid expressions. For instance:
12060 struct X { int operator >> (int); };
12061 template <int V> struct Foo {};
12062 Foo<X () >> 5> r;
12064 Here 'X()' is a valid type-id of a function type, but the user just
12065 wanted to write the expression "X() >> 5". Thus, we remember that we
12066 found a valid type-id, but we still try to parse the argument as an
12067 expression to see what happens.
12069 In C++0x, the '>>' will be considered two separate '>'
12070 tokens. */
12071 if (!cp_parser_error_occurred (parser)
12072 && cxx_dialect == cxx98
12073 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12075 maybe_type_id = true;
12076 cp_parser_abort_tentative_parse (parser);
12078 else
12080 /* If the next token isn't a `,' or a `>', then this argument wasn't
12081 really finished. This means that the argument is not a valid
12082 type-id. */
12083 if (!cp_parser_next_token_ends_template_argument_p (parser))
12084 cp_parser_error (parser, "expected template-argument");
12085 /* If that worked, we're done. */
12086 if (cp_parser_parse_definitely (parser))
12087 return argument;
12089 /* We're still not sure what the argument will be. */
12090 cp_parser_parse_tentatively (parser);
12091 /* Try a template. */
12092 argument_start_token = cp_lexer_peek_token (parser->lexer);
12093 argument = cp_parser_id_expression (parser,
12094 /*template_keyword_p=*/false,
12095 /*check_dependency_p=*/true,
12096 &template_p,
12097 /*declarator_p=*/false,
12098 /*optional_p=*/false);
12099 /* If the next token isn't a `,' or a `>', then this argument wasn't
12100 really finished. */
12101 if (!cp_parser_next_token_ends_template_argument_p (parser))
12102 cp_parser_error (parser, "expected template-argument");
12103 if (!cp_parser_error_occurred (parser))
12105 /* Figure out what is being referred to. If the id-expression
12106 was for a class template specialization, then we will have a
12107 TYPE_DECL at this point. There is no need to do name lookup
12108 at this point in that case. */
12109 if (TREE_CODE (argument) != TYPE_DECL)
12110 argument = cp_parser_lookup_name (parser, argument,
12111 none_type,
12112 /*is_template=*/template_p,
12113 /*is_namespace=*/false,
12114 /*check_dependency=*/true,
12115 /*ambiguous_decls=*/NULL,
12116 argument_start_token->location);
12117 if (TREE_CODE (argument) != TEMPLATE_DECL
12118 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12119 cp_parser_error (parser, "expected template-name");
12121 if (cp_parser_parse_definitely (parser))
12122 return argument;
12123 /* It must be a non-type argument. There permitted cases are given
12124 in [temp.arg.nontype]:
12126 -- an integral constant-expression of integral or enumeration
12127 type; or
12129 -- the name of a non-type template-parameter; or
12131 -- the name of an object or function with external linkage...
12133 -- the address of an object or function with external linkage...
12135 -- a pointer to member... */
12136 /* Look for a non-type template parameter. */
12137 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12139 cp_parser_parse_tentatively (parser);
12140 argument = cp_parser_primary_expression (parser,
12141 /*address_p=*/false,
12142 /*cast_p=*/false,
12143 /*template_arg_p=*/true,
12144 &idk);
12145 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12146 || !cp_parser_next_token_ends_template_argument_p (parser))
12147 cp_parser_simulate_error (parser);
12148 if (cp_parser_parse_definitely (parser))
12149 return argument;
12152 /* If the next token is "&", the argument must be the address of an
12153 object or function with external linkage. */
12154 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12155 if (address_p)
12156 cp_lexer_consume_token (parser->lexer);
12157 /* See if we might have an id-expression. */
12158 token = cp_lexer_peek_token (parser->lexer);
12159 if (token->type == CPP_NAME
12160 || token->keyword == RID_OPERATOR
12161 || token->type == CPP_SCOPE
12162 || token->type == CPP_TEMPLATE_ID
12163 || token->type == CPP_NESTED_NAME_SPECIFIER)
12165 cp_parser_parse_tentatively (parser);
12166 argument = cp_parser_primary_expression (parser,
12167 address_p,
12168 /*cast_p=*/false,
12169 /*template_arg_p=*/true,
12170 &idk);
12171 if (cp_parser_error_occurred (parser)
12172 || !cp_parser_next_token_ends_template_argument_p (parser))
12173 cp_parser_abort_tentative_parse (parser);
12174 else
12176 tree probe;
12178 if (TREE_CODE (argument) == INDIRECT_REF)
12180 gcc_assert (REFERENCE_REF_P (argument));
12181 argument = TREE_OPERAND (argument, 0);
12184 /* If we're in a template, we represent a qualified-id referring
12185 to a static data member as a SCOPE_REF even if the scope isn't
12186 dependent so that we can check access control later. */
12187 probe = argument;
12188 if (TREE_CODE (probe) == SCOPE_REF)
12189 probe = TREE_OPERAND (probe, 1);
12190 if (TREE_CODE (probe) == VAR_DECL)
12192 /* A variable without external linkage might still be a
12193 valid constant-expression, so no error is issued here
12194 if the external-linkage check fails. */
12195 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12196 cp_parser_simulate_error (parser);
12198 else if (is_overloaded_fn (argument))
12199 /* All overloaded functions are allowed; if the external
12200 linkage test does not pass, an error will be issued
12201 later. */
12203 else if (address_p
12204 && (TREE_CODE (argument) == OFFSET_REF
12205 || TREE_CODE (argument) == SCOPE_REF))
12206 /* A pointer-to-member. */
12208 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12210 else
12211 cp_parser_simulate_error (parser);
12213 if (cp_parser_parse_definitely (parser))
12215 if (address_p)
12216 argument = build_x_unary_op (ADDR_EXPR, argument,
12217 tf_warning_or_error);
12218 return argument;
12222 /* If the argument started with "&", there are no other valid
12223 alternatives at this point. */
12224 if (address_p)
12226 cp_parser_error (parser, "invalid non-type template argument");
12227 return error_mark_node;
12230 /* If the argument wasn't successfully parsed as a type-id followed
12231 by '>>', the argument can only be a constant expression now.
12232 Otherwise, we try parsing the constant-expression tentatively,
12233 because the argument could really be a type-id. */
12234 if (maybe_type_id)
12235 cp_parser_parse_tentatively (parser);
12236 argument = cp_parser_constant_expression (parser,
12237 /*allow_non_constant_p=*/false,
12238 /*non_constant_p=*/NULL);
12239 argument = fold_non_dependent_expr (argument);
12240 if (!maybe_type_id)
12241 return argument;
12242 if (!cp_parser_next_token_ends_template_argument_p (parser))
12243 cp_parser_error (parser, "expected template-argument");
12244 if (cp_parser_parse_definitely (parser))
12245 return argument;
12246 /* We did our best to parse the argument as a non type-id, but that
12247 was the only alternative that matched (albeit with a '>' after
12248 it). We can assume it's just a typo from the user, and a
12249 diagnostic will then be issued. */
12250 return cp_parser_template_type_arg (parser);
12253 /* Parse an explicit-instantiation.
12255 explicit-instantiation:
12256 template declaration
12258 Although the standard says `declaration', what it really means is:
12260 explicit-instantiation:
12261 template decl-specifier-seq [opt] declarator [opt] ;
12263 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12264 supposed to be allowed. A defect report has been filed about this
12265 issue.
12267 GNU Extension:
12269 explicit-instantiation:
12270 storage-class-specifier template
12271 decl-specifier-seq [opt] declarator [opt] ;
12272 function-specifier template
12273 decl-specifier-seq [opt] declarator [opt] ; */
12275 static void
12276 cp_parser_explicit_instantiation (cp_parser* parser)
12278 int declares_class_or_enum;
12279 cp_decl_specifier_seq decl_specifiers;
12280 tree extension_specifier = NULL_TREE;
12282 /* Look for an (optional) storage-class-specifier or
12283 function-specifier. */
12284 if (cp_parser_allow_gnu_extensions_p (parser))
12286 extension_specifier
12287 = cp_parser_storage_class_specifier_opt (parser);
12288 if (!extension_specifier)
12289 extension_specifier
12290 = cp_parser_function_specifier_opt (parser,
12291 /*decl_specs=*/NULL);
12294 /* Look for the `template' keyword. */
12295 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12296 /* Let the front end know that we are processing an explicit
12297 instantiation. */
12298 begin_explicit_instantiation ();
12299 /* [temp.explicit] says that we are supposed to ignore access
12300 control while processing explicit instantiation directives. */
12301 push_deferring_access_checks (dk_no_check);
12302 /* Parse a decl-specifier-seq. */
12303 cp_parser_decl_specifier_seq (parser,
12304 CP_PARSER_FLAGS_OPTIONAL,
12305 &decl_specifiers,
12306 &declares_class_or_enum);
12307 /* If there was exactly one decl-specifier, and it declared a class,
12308 and there's no declarator, then we have an explicit type
12309 instantiation. */
12310 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12312 tree type;
12314 type = check_tag_decl (&decl_specifiers);
12315 /* Turn access control back on for names used during
12316 template instantiation. */
12317 pop_deferring_access_checks ();
12318 if (type)
12319 do_type_instantiation (type, extension_specifier,
12320 /*complain=*/tf_error);
12322 else
12324 cp_declarator *declarator;
12325 tree decl;
12327 /* Parse the declarator. */
12328 declarator
12329 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12330 /*ctor_dtor_or_conv_p=*/NULL,
12331 /*parenthesized_p=*/NULL,
12332 /*member_p=*/false);
12333 if (declares_class_or_enum & 2)
12334 cp_parser_check_for_definition_in_return_type (declarator,
12335 decl_specifiers.type,
12336 decl_specifiers.type_location);
12337 if (declarator != cp_error_declarator)
12339 if (decl_specifiers.specs[(int)ds_inline])
12340 permerror (input_location, "explicit instantiation shall not use"
12341 " %<inline%> specifier");
12342 if (decl_specifiers.specs[(int)ds_constexpr])
12343 permerror (input_location, "explicit instantiation shall not use"
12344 " %<constexpr%> specifier");
12346 decl = grokdeclarator (declarator, &decl_specifiers,
12347 NORMAL, 0, &decl_specifiers.attributes);
12348 /* Turn access control back on for names used during
12349 template instantiation. */
12350 pop_deferring_access_checks ();
12351 /* Do the explicit instantiation. */
12352 do_decl_instantiation (decl, extension_specifier);
12354 else
12356 pop_deferring_access_checks ();
12357 /* Skip the body of the explicit instantiation. */
12358 cp_parser_skip_to_end_of_statement (parser);
12361 /* We're done with the instantiation. */
12362 end_explicit_instantiation ();
12364 cp_parser_consume_semicolon_at_end_of_statement (parser);
12367 /* Parse an explicit-specialization.
12369 explicit-specialization:
12370 template < > declaration
12372 Although the standard says `declaration', what it really means is:
12374 explicit-specialization:
12375 template <> decl-specifier [opt] init-declarator [opt] ;
12376 template <> function-definition
12377 template <> explicit-specialization
12378 template <> template-declaration */
12380 static void
12381 cp_parser_explicit_specialization (cp_parser* parser)
12383 bool need_lang_pop;
12384 cp_token *token = cp_lexer_peek_token (parser->lexer);
12386 /* Look for the `template' keyword. */
12387 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12388 /* Look for the `<'. */
12389 cp_parser_require (parser, CPP_LESS, RT_LESS);
12390 /* Look for the `>'. */
12391 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12392 /* We have processed another parameter list. */
12393 ++parser->num_template_parameter_lists;
12394 /* [temp]
12396 A template ... explicit specialization ... shall not have C
12397 linkage. */
12398 if (current_lang_name == lang_name_c)
12400 error_at (token->location, "template specialization with C linkage");
12401 /* Give it C++ linkage to avoid confusing other parts of the
12402 front end. */
12403 push_lang_context (lang_name_cplusplus);
12404 need_lang_pop = true;
12406 else
12407 need_lang_pop = false;
12408 /* Let the front end know that we are beginning a specialization. */
12409 if (!begin_specialization ())
12411 end_specialization ();
12412 return;
12415 /* If the next keyword is `template', we need to figure out whether
12416 or not we're looking a template-declaration. */
12417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12419 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12420 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12421 cp_parser_template_declaration_after_export (parser,
12422 /*member_p=*/false);
12423 else
12424 cp_parser_explicit_specialization (parser);
12426 else
12427 /* Parse the dependent declaration. */
12428 cp_parser_single_declaration (parser,
12429 /*checks=*/NULL,
12430 /*member_p=*/false,
12431 /*explicit_specialization_p=*/true,
12432 /*friend_p=*/NULL);
12433 /* We're done with the specialization. */
12434 end_specialization ();
12435 /* For the erroneous case of a template with C linkage, we pushed an
12436 implicit C++ linkage scope; exit that scope now. */
12437 if (need_lang_pop)
12438 pop_lang_context ();
12439 /* We're done with this parameter list. */
12440 --parser->num_template_parameter_lists;
12443 /* Parse a type-specifier.
12445 type-specifier:
12446 simple-type-specifier
12447 class-specifier
12448 enum-specifier
12449 elaborated-type-specifier
12450 cv-qualifier
12452 GNU Extension:
12454 type-specifier:
12455 __complex__
12457 Returns a representation of the type-specifier. For a
12458 class-specifier, enum-specifier, or elaborated-type-specifier, a
12459 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12461 The parser flags FLAGS is used to control type-specifier parsing.
12463 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12464 in a decl-specifier-seq.
12466 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12467 class-specifier, enum-specifier, or elaborated-type-specifier, then
12468 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12469 if a type is declared; 2 if it is defined. Otherwise, it is set to
12470 zero.
12472 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12473 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12474 is set to FALSE. */
12476 static tree
12477 cp_parser_type_specifier (cp_parser* parser,
12478 cp_parser_flags flags,
12479 cp_decl_specifier_seq *decl_specs,
12480 bool is_declaration,
12481 int* declares_class_or_enum,
12482 bool* is_cv_qualifier)
12484 tree type_spec = NULL_TREE;
12485 cp_token *token;
12486 enum rid keyword;
12487 cp_decl_spec ds = ds_last;
12489 /* Assume this type-specifier does not declare a new type. */
12490 if (declares_class_or_enum)
12491 *declares_class_or_enum = 0;
12492 /* And that it does not specify a cv-qualifier. */
12493 if (is_cv_qualifier)
12494 *is_cv_qualifier = false;
12495 /* Peek at the next token. */
12496 token = cp_lexer_peek_token (parser->lexer);
12498 /* If we're looking at a keyword, we can use that to guide the
12499 production we choose. */
12500 keyword = token->keyword;
12501 switch (keyword)
12503 case RID_ENUM:
12504 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12505 goto elaborated_type_specifier;
12507 /* Look for the enum-specifier. */
12508 type_spec = cp_parser_enum_specifier (parser);
12509 /* If that worked, we're done. */
12510 if (type_spec)
12512 if (declares_class_or_enum)
12513 *declares_class_or_enum = 2;
12514 if (decl_specs)
12515 cp_parser_set_decl_spec_type (decl_specs,
12516 type_spec,
12517 token->location,
12518 /*user_defined_p=*/true);
12519 return type_spec;
12521 else
12522 goto elaborated_type_specifier;
12524 /* Any of these indicate either a class-specifier, or an
12525 elaborated-type-specifier. */
12526 case RID_CLASS:
12527 case RID_STRUCT:
12528 case RID_UNION:
12529 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12530 goto elaborated_type_specifier;
12532 /* Parse tentatively so that we can back up if we don't find a
12533 class-specifier. */
12534 cp_parser_parse_tentatively (parser);
12535 /* Look for the class-specifier. */
12536 type_spec = cp_parser_class_specifier (parser);
12537 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12538 /* If that worked, we're done. */
12539 if (cp_parser_parse_definitely (parser))
12541 if (declares_class_or_enum)
12542 *declares_class_or_enum = 2;
12543 if (decl_specs)
12544 cp_parser_set_decl_spec_type (decl_specs,
12545 type_spec,
12546 token->location,
12547 /*user_defined_p=*/true);
12548 return type_spec;
12551 /* Fall through. */
12552 elaborated_type_specifier:
12553 /* We're declaring (not defining) a class or enum. */
12554 if (declares_class_or_enum)
12555 *declares_class_or_enum = 1;
12557 /* Fall through. */
12558 case RID_TYPENAME:
12559 /* Look for an elaborated-type-specifier. */
12560 type_spec
12561 = (cp_parser_elaborated_type_specifier
12562 (parser,
12563 decl_specs && decl_specs->specs[(int) ds_friend],
12564 is_declaration));
12565 if (decl_specs)
12566 cp_parser_set_decl_spec_type (decl_specs,
12567 type_spec,
12568 token->location,
12569 /*user_defined_p=*/true);
12570 return type_spec;
12572 case RID_CONST:
12573 ds = ds_const;
12574 if (is_cv_qualifier)
12575 *is_cv_qualifier = true;
12576 break;
12578 case RID_VOLATILE:
12579 ds = ds_volatile;
12580 if (is_cv_qualifier)
12581 *is_cv_qualifier = true;
12582 break;
12584 case RID_RESTRICT:
12585 ds = ds_restrict;
12586 if (is_cv_qualifier)
12587 *is_cv_qualifier = true;
12588 break;
12590 case RID_COMPLEX:
12591 /* The `__complex__' keyword is a GNU extension. */
12592 ds = ds_complex;
12593 break;
12595 default:
12596 break;
12599 /* Handle simple keywords. */
12600 if (ds != ds_last)
12602 if (decl_specs)
12604 ++decl_specs->specs[(int)ds];
12605 decl_specs->any_specifiers_p = true;
12607 return cp_lexer_consume_token (parser->lexer)->u.value;
12610 /* If we do not already have a type-specifier, assume we are looking
12611 at a simple-type-specifier. */
12612 type_spec = cp_parser_simple_type_specifier (parser,
12613 decl_specs,
12614 flags);
12616 /* If we didn't find a type-specifier, and a type-specifier was not
12617 optional in this context, issue an error message. */
12618 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12620 cp_parser_error (parser, "expected type specifier");
12621 return error_mark_node;
12624 return type_spec;
12627 /* Parse a simple-type-specifier.
12629 simple-type-specifier:
12630 :: [opt] nested-name-specifier [opt] type-name
12631 :: [opt] nested-name-specifier template template-id
12632 char
12633 wchar_t
12634 bool
12635 short
12637 long
12638 signed
12639 unsigned
12640 float
12641 double
12642 void
12644 C++0x Extension:
12646 simple-type-specifier:
12647 auto
12648 decltype ( expression )
12649 char16_t
12650 char32_t
12652 GNU Extension:
12654 simple-type-specifier:
12655 __int128
12656 __typeof__ unary-expression
12657 __typeof__ ( type-id )
12659 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12660 appropriately updated. */
12662 static tree
12663 cp_parser_simple_type_specifier (cp_parser* parser,
12664 cp_decl_specifier_seq *decl_specs,
12665 cp_parser_flags flags)
12667 tree type = NULL_TREE;
12668 cp_token *token;
12670 /* Peek at the next token. */
12671 token = cp_lexer_peek_token (parser->lexer);
12673 /* If we're looking at a keyword, things are easy. */
12674 switch (token->keyword)
12676 case RID_CHAR:
12677 if (decl_specs)
12678 decl_specs->explicit_char_p = true;
12679 type = char_type_node;
12680 break;
12681 case RID_CHAR16:
12682 type = char16_type_node;
12683 break;
12684 case RID_CHAR32:
12685 type = char32_type_node;
12686 break;
12687 case RID_WCHAR:
12688 type = wchar_type_node;
12689 break;
12690 case RID_BOOL:
12691 type = boolean_type_node;
12692 break;
12693 case RID_SHORT:
12694 if (decl_specs)
12695 ++decl_specs->specs[(int) ds_short];
12696 type = short_integer_type_node;
12697 break;
12698 case RID_INT:
12699 if (decl_specs)
12700 decl_specs->explicit_int_p = true;
12701 type = integer_type_node;
12702 break;
12703 case RID_INT128:
12704 if (!int128_integer_type_node)
12705 break;
12706 if (decl_specs)
12707 decl_specs->explicit_int128_p = true;
12708 type = int128_integer_type_node;
12709 break;
12710 case RID_LONG:
12711 if (decl_specs)
12712 ++decl_specs->specs[(int) ds_long];
12713 type = long_integer_type_node;
12714 break;
12715 case RID_SIGNED:
12716 if (decl_specs)
12717 ++decl_specs->specs[(int) ds_signed];
12718 type = integer_type_node;
12719 break;
12720 case RID_UNSIGNED:
12721 if (decl_specs)
12722 ++decl_specs->specs[(int) ds_unsigned];
12723 type = unsigned_type_node;
12724 break;
12725 case RID_FLOAT:
12726 type = float_type_node;
12727 break;
12728 case RID_DOUBLE:
12729 type = double_type_node;
12730 break;
12731 case RID_VOID:
12732 type = void_type_node;
12733 break;
12735 case RID_AUTO:
12736 maybe_warn_cpp0x (CPP0X_AUTO);
12737 type = make_auto ();
12738 break;
12740 case RID_DECLTYPE:
12741 /* Parse the `decltype' type. */
12742 type = cp_parser_decltype (parser);
12744 if (decl_specs)
12745 cp_parser_set_decl_spec_type (decl_specs, type,
12746 token->location,
12747 /*user_defined_p=*/true);
12749 return type;
12751 case RID_TYPEOF:
12752 /* Consume the `typeof' token. */
12753 cp_lexer_consume_token (parser->lexer);
12754 /* Parse the operand to `typeof'. */
12755 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12756 /* If it is not already a TYPE, take its type. */
12757 if (!TYPE_P (type))
12758 type = finish_typeof (type);
12760 if (decl_specs)
12761 cp_parser_set_decl_spec_type (decl_specs, type,
12762 token->location,
12763 /*user_defined_p=*/true);
12765 return type;
12767 default:
12768 break;
12771 /* If the type-specifier was for a built-in type, we're done. */
12772 if (type)
12774 /* Record the type. */
12775 if (decl_specs
12776 && (token->keyword != RID_SIGNED
12777 && token->keyword != RID_UNSIGNED
12778 && token->keyword != RID_SHORT
12779 && token->keyword != RID_LONG))
12780 cp_parser_set_decl_spec_type (decl_specs,
12781 type,
12782 token->location,
12783 /*user_defined=*/false);
12784 if (decl_specs)
12785 decl_specs->any_specifiers_p = true;
12787 /* Consume the token. */
12788 cp_lexer_consume_token (parser->lexer);
12790 /* There is no valid C++ program where a non-template type is
12791 followed by a "<". That usually indicates that the user thought
12792 that the type was a template. */
12793 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12795 return TYPE_NAME (type);
12798 /* The type-specifier must be a user-defined type. */
12799 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12801 bool qualified_p;
12802 bool global_p;
12804 /* Don't gobble tokens or issue error messages if this is an
12805 optional type-specifier. */
12806 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12807 cp_parser_parse_tentatively (parser);
12809 /* Look for the optional `::' operator. */
12810 global_p
12811 = (cp_parser_global_scope_opt (parser,
12812 /*current_scope_valid_p=*/false)
12813 != NULL_TREE);
12814 /* Look for the nested-name specifier. */
12815 qualified_p
12816 = (cp_parser_nested_name_specifier_opt (parser,
12817 /*typename_keyword_p=*/false,
12818 /*check_dependency_p=*/true,
12819 /*type_p=*/false,
12820 /*is_declaration=*/false)
12821 != NULL_TREE);
12822 token = cp_lexer_peek_token (parser->lexer);
12823 /* If we have seen a nested-name-specifier, and the next token
12824 is `template', then we are using the template-id production. */
12825 if (parser->scope
12826 && cp_parser_optional_template_keyword (parser))
12828 /* Look for the template-id. */
12829 type = cp_parser_template_id (parser,
12830 /*template_keyword_p=*/true,
12831 /*check_dependency_p=*/true,
12832 /*is_declaration=*/false);
12833 /* If the template-id did not name a type, we are out of
12834 luck. */
12835 if (TREE_CODE (type) != TYPE_DECL)
12837 cp_parser_error (parser, "expected template-id for type");
12838 type = NULL_TREE;
12841 /* Otherwise, look for a type-name. */
12842 else
12843 type = cp_parser_type_name (parser);
12844 /* Keep track of all name-lookups performed in class scopes. */
12845 if (type
12846 && !global_p
12847 && !qualified_p
12848 && TREE_CODE (type) == TYPE_DECL
12849 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12850 maybe_note_name_used_in_class (DECL_NAME (type), type);
12851 /* If it didn't work out, we don't have a TYPE. */
12852 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12853 && !cp_parser_parse_definitely (parser))
12854 type = NULL_TREE;
12855 if (type && decl_specs)
12856 cp_parser_set_decl_spec_type (decl_specs, type,
12857 token->location,
12858 /*user_defined=*/true);
12861 /* If we didn't get a type-name, issue an error message. */
12862 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12864 cp_parser_error (parser, "expected type-name");
12865 return error_mark_node;
12868 if (type && type != error_mark_node)
12870 /* See if TYPE is an Objective-C type, and if so, parse and
12871 accept any protocol references following it. Do this before
12872 the cp_parser_check_for_invalid_template_id() call, because
12873 Objective-C types can be followed by '<...>' which would
12874 enclose protocol names rather than template arguments, and so
12875 everything is fine. */
12876 if (c_dialect_objc () && !parser->scope
12877 && (objc_is_id (type) || objc_is_class_name (type)))
12879 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12880 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12882 /* Clobber the "unqualified" type previously entered into
12883 DECL_SPECS with the new, improved protocol-qualified version. */
12884 if (decl_specs)
12885 decl_specs->type = qual_type;
12887 return qual_type;
12890 /* There is no valid C++ program where a non-template type is
12891 followed by a "<". That usually indicates that the user
12892 thought that the type was a template. */
12893 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12894 token->location);
12897 return type;
12900 /* Parse a type-name.
12902 type-name:
12903 class-name
12904 enum-name
12905 typedef-name
12907 enum-name:
12908 identifier
12910 typedef-name:
12911 identifier
12913 Returns a TYPE_DECL for the type. */
12915 static tree
12916 cp_parser_type_name (cp_parser* parser)
12918 tree type_decl;
12920 /* We can't know yet whether it is a class-name or not. */
12921 cp_parser_parse_tentatively (parser);
12922 /* Try a class-name. */
12923 type_decl = cp_parser_class_name (parser,
12924 /*typename_keyword_p=*/false,
12925 /*template_keyword_p=*/false,
12926 none_type,
12927 /*check_dependency_p=*/true,
12928 /*class_head_p=*/false,
12929 /*is_declaration=*/false);
12930 /* If it's not a class-name, keep looking. */
12931 if (!cp_parser_parse_definitely (parser))
12933 /* It must be a typedef-name or an enum-name. */
12934 return cp_parser_nonclass_name (parser);
12937 return type_decl;
12940 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12942 enum-name:
12943 identifier
12945 typedef-name:
12946 identifier
12948 Returns a TYPE_DECL for the type. */
12950 static tree
12951 cp_parser_nonclass_name (cp_parser* parser)
12953 tree type_decl;
12954 tree identifier;
12956 cp_token *token = cp_lexer_peek_token (parser->lexer);
12957 identifier = cp_parser_identifier (parser);
12958 if (identifier == error_mark_node)
12959 return error_mark_node;
12961 /* Look up the type-name. */
12962 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12964 if (TREE_CODE (type_decl) != TYPE_DECL
12965 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12967 /* See if this is an Objective-C type. */
12968 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12969 tree type = objc_get_protocol_qualified_type (identifier, protos);
12970 if (type)
12971 type_decl = TYPE_NAME (type);
12974 /* Issue an error if we did not find a type-name. */
12975 if (TREE_CODE (type_decl) != TYPE_DECL
12976 /* In Objective-C, we have the complication that class names are
12977 normally type names and start declarations (eg, the
12978 "NSObject" in "NSObject *object;"), but can be used in an
12979 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12980 is an expression. So, a classname followed by a dot is not a
12981 valid type-name. */
12982 || (objc_is_class_name (TREE_TYPE (type_decl))
12983 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12985 if (!cp_parser_simulate_error (parser))
12986 cp_parser_name_lookup_error (parser, identifier, type_decl,
12987 NLE_TYPE, token->location);
12988 return error_mark_node;
12990 /* Remember that the name was used in the definition of the
12991 current class so that we can check later to see if the
12992 meaning would have been different after the class was
12993 entirely defined. */
12994 else if (type_decl != error_mark_node
12995 && !parser->scope)
12996 maybe_note_name_used_in_class (identifier, type_decl);
12998 return type_decl;
13001 /* Parse an elaborated-type-specifier. Note that the grammar given
13002 here incorporates the resolution to DR68.
13004 elaborated-type-specifier:
13005 class-key :: [opt] nested-name-specifier [opt] identifier
13006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13007 enum-key :: [opt] nested-name-specifier [opt] identifier
13008 typename :: [opt] nested-name-specifier identifier
13009 typename :: [opt] nested-name-specifier template [opt]
13010 template-id
13012 GNU extension:
13014 elaborated-type-specifier:
13015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13016 class-key attributes :: [opt] nested-name-specifier [opt]
13017 template [opt] template-id
13018 enum attributes :: [opt] nested-name-specifier [opt] identifier
13020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13021 declared `friend'. If IS_DECLARATION is TRUE, then this
13022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13023 something is being declared.
13025 Returns the TYPE specified. */
13027 static tree
13028 cp_parser_elaborated_type_specifier (cp_parser* parser,
13029 bool is_friend,
13030 bool is_declaration)
13032 enum tag_types tag_type;
13033 tree identifier;
13034 tree type = NULL_TREE;
13035 tree attributes = NULL_TREE;
13036 tree globalscope;
13037 cp_token *token = NULL;
13039 /* See if we're looking at the `enum' keyword. */
13040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13042 /* Consume the `enum' token. */
13043 cp_lexer_consume_token (parser->lexer);
13044 /* Remember that it's an enumeration type. */
13045 tag_type = enum_type;
13046 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13047 enums) is used here. */
13048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13049 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13051 pedwarn (input_location, 0, "elaborated-type-specifier "
13052 "for a scoped enum must not use the %<%D%> keyword",
13053 cp_lexer_peek_token (parser->lexer)->u.value);
13054 /* Consume the `struct' or `class' and parse it anyway. */
13055 cp_lexer_consume_token (parser->lexer);
13057 /* Parse the attributes. */
13058 attributes = cp_parser_attributes_opt (parser);
13060 /* Or, it might be `typename'. */
13061 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13062 RID_TYPENAME))
13064 /* Consume the `typename' token. */
13065 cp_lexer_consume_token (parser->lexer);
13066 /* Remember that it's a `typename' type. */
13067 tag_type = typename_type;
13069 /* Otherwise it must be a class-key. */
13070 else
13072 tag_type = cp_parser_class_key (parser);
13073 if (tag_type == none_type)
13074 return error_mark_node;
13075 /* Parse the attributes. */
13076 attributes = cp_parser_attributes_opt (parser);
13079 /* Look for the `::' operator. */
13080 globalscope = cp_parser_global_scope_opt (parser,
13081 /*current_scope_valid_p=*/false);
13082 /* Look for the nested-name-specifier. */
13083 if (tag_type == typename_type && !globalscope)
13085 if (!cp_parser_nested_name_specifier (parser,
13086 /*typename_keyword_p=*/true,
13087 /*check_dependency_p=*/true,
13088 /*type_p=*/true,
13089 is_declaration))
13090 return error_mark_node;
13092 else
13093 /* Even though `typename' is not present, the proposed resolution
13094 to Core Issue 180 says that in `class A<T>::B', `B' should be
13095 considered a type-name, even if `A<T>' is dependent. */
13096 cp_parser_nested_name_specifier_opt (parser,
13097 /*typename_keyword_p=*/true,
13098 /*check_dependency_p=*/true,
13099 /*type_p=*/true,
13100 is_declaration);
13101 /* For everything but enumeration types, consider a template-id.
13102 For an enumeration type, consider only a plain identifier. */
13103 if (tag_type != enum_type)
13105 bool template_p = false;
13106 tree decl;
13108 /* Allow the `template' keyword. */
13109 template_p = cp_parser_optional_template_keyword (parser);
13110 /* If we didn't see `template', we don't know if there's a
13111 template-id or not. */
13112 if (!template_p)
13113 cp_parser_parse_tentatively (parser);
13114 /* Parse the template-id. */
13115 token = cp_lexer_peek_token (parser->lexer);
13116 decl = cp_parser_template_id (parser, template_p,
13117 /*check_dependency_p=*/true,
13118 is_declaration);
13119 /* If we didn't find a template-id, look for an ordinary
13120 identifier. */
13121 if (!template_p && !cp_parser_parse_definitely (parser))
13123 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13124 in effect, then we must assume that, upon instantiation, the
13125 template will correspond to a class. */
13126 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13127 && tag_type == typename_type)
13128 type = make_typename_type (parser->scope, decl,
13129 typename_type,
13130 /*complain=*/tf_error);
13131 /* If the `typename' keyword is in effect and DECL is not a type
13132 decl. Then type is non existant. */
13133 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13134 type = NULL_TREE;
13135 else
13136 type = TREE_TYPE (decl);
13139 if (!type)
13141 token = cp_lexer_peek_token (parser->lexer);
13142 identifier = cp_parser_identifier (parser);
13144 if (identifier == error_mark_node)
13146 parser->scope = NULL_TREE;
13147 return error_mark_node;
13150 /* For a `typename', we needn't call xref_tag. */
13151 if (tag_type == typename_type
13152 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13153 return cp_parser_make_typename_type (parser, parser->scope,
13154 identifier,
13155 token->location);
13156 /* Look up a qualified name in the usual way. */
13157 if (parser->scope)
13159 tree decl;
13160 tree ambiguous_decls;
13162 decl = cp_parser_lookup_name (parser, identifier,
13163 tag_type,
13164 /*is_template=*/false,
13165 /*is_namespace=*/false,
13166 /*check_dependency=*/true,
13167 &ambiguous_decls,
13168 token->location);
13170 /* If the lookup was ambiguous, an error will already have been
13171 issued. */
13172 if (ambiguous_decls)
13173 return error_mark_node;
13175 /* If we are parsing friend declaration, DECL may be a
13176 TEMPLATE_DECL tree node here. However, we need to check
13177 whether this TEMPLATE_DECL results in valid code. Consider
13178 the following example:
13180 namespace N {
13181 template <class T> class C {};
13183 class X {
13184 template <class T> friend class N::C; // #1, valid code
13186 template <class T> class Y {
13187 friend class N::C; // #2, invalid code
13190 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13191 name lookup of `N::C'. We see that friend declaration must
13192 be template for the code to be valid. Note that
13193 processing_template_decl does not work here since it is
13194 always 1 for the above two cases. */
13196 decl = (cp_parser_maybe_treat_template_as_class
13197 (decl, /*tag_name_p=*/is_friend
13198 && parser->num_template_parameter_lists));
13200 if (TREE_CODE (decl) != TYPE_DECL)
13202 cp_parser_diagnose_invalid_type_name (parser,
13203 parser->scope,
13204 identifier,
13205 token->location);
13206 return error_mark_node;
13209 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13211 bool allow_template = (parser->num_template_parameter_lists
13212 || DECL_SELF_REFERENCE_P (decl));
13213 type = check_elaborated_type_specifier (tag_type, decl,
13214 allow_template);
13216 if (type == error_mark_node)
13217 return error_mark_node;
13220 /* Forward declarations of nested types, such as
13222 class C1::C2;
13223 class C1::C2::C3;
13225 are invalid unless all components preceding the final '::'
13226 are complete. If all enclosing types are complete, these
13227 declarations become merely pointless.
13229 Invalid forward declarations of nested types are errors
13230 caught elsewhere in parsing. Those that are pointless arrive
13231 here. */
13233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13234 && !is_friend && !processing_explicit_instantiation)
13235 warning (0, "declaration %qD does not declare anything", decl);
13237 type = TREE_TYPE (decl);
13239 else
13241 /* An elaborated-type-specifier sometimes introduces a new type and
13242 sometimes names an existing type. Normally, the rule is that it
13243 introduces a new type only if there is not an existing type of
13244 the same name already in scope. For example, given:
13246 struct S {};
13247 void f() { struct S s; }
13249 the `struct S' in the body of `f' is the same `struct S' as in
13250 the global scope; the existing definition is used. However, if
13251 there were no global declaration, this would introduce a new
13252 local class named `S'.
13254 An exception to this rule applies to the following code:
13256 namespace N { struct S; }
13258 Here, the elaborated-type-specifier names a new type
13259 unconditionally; even if there is already an `S' in the
13260 containing scope this declaration names a new type.
13261 This exception only applies if the elaborated-type-specifier
13262 forms the complete declaration:
13264 [class.name]
13266 A declaration consisting solely of `class-key identifier ;' is
13267 either a redeclaration of the name in the current scope or a
13268 forward declaration of the identifier as a class name. It
13269 introduces the name into the current scope.
13271 We are in this situation precisely when the next token is a `;'.
13273 An exception to the exception is that a `friend' declaration does
13274 *not* name a new type; i.e., given:
13276 struct S { friend struct T; };
13278 `T' is not a new type in the scope of `S'.
13280 Also, `new struct S' or `sizeof (struct S)' never results in the
13281 definition of a new type; a new type can only be declared in a
13282 declaration context. */
13284 tag_scope ts;
13285 bool template_p;
13287 if (is_friend)
13288 /* Friends have special name lookup rules. */
13289 ts = ts_within_enclosing_non_class;
13290 else if (is_declaration
13291 && cp_lexer_next_token_is (parser->lexer,
13292 CPP_SEMICOLON))
13293 /* This is a `class-key identifier ;' */
13294 ts = ts_current;
13295 else
13296 ts = ts_global;
13298 template_p =
13299 (parser->num_template_parameter_lists
13300 && (cp_parser_next_token_starts_class_definition_p (parser)
13301 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13302 /* An unqualified name was used to reference this type, so
13303 there were no qualifying templates. */
13304 if (!cp_parser_check_template_parameters (parser,
13305 /*num_templates=*/0,
13306 token->location,
13307 /*declarator=*/NULL))
13308 return error_mark_node;
13309 type = xref_tag (tag_type, identifier, ts, template_p);
13313 if (type == error_mark_node)
13314 return error_mark_node;
13316 /* Allow attributes on forward declarations of classes. */
13317 if (attributes)
13319 if (TREE_CODE (type) == TYPENAME_TYPE)
13320 warning (OPT_Wattributes,
13321 "attributes ignored on uninstantiated type");
13322 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13323 && ! processing_explicit_instantiation)
13324 warning (OPT_Wattributes,
13325 "attributes ignored on template instantiation");
13326 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13327 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13328 else
13329 warning (OPT_Wattributes,
13330 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13333 if (tag_type != enum_type)
13334 cp_parser_check_class_key (tag_type, type);
13336 /* A "<" cannot follow an elaborated type specifier. If that
13337 happens, the user was probably trying to form a template-id. */
13338 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13340 return type;
13343 /* Parse an enum-specifier.
13345 enum-specifier:
13346 enum-head { enumerator-list [opt] }
13348 enum-head:
13349 enum-key identifier [opt] enum-base [opt]
13350 enum-key nested-name-specifier identifier enum-base [opt]
13352 enum-key:
13353 enum
13354 enum class [C++0x]
13355 enum struct [C++0x]
13357 enum-base: [C++0x]
13358 : type-specifier-seq
13360 opaque-enum-specifier:
13361 enum-key identifier enum-base [opt] ;
13363 GNU Extensions:
13364 enum-key attributes[opt] identifier [opt] enum-base [opt]
13365 { enumerator-list [opt] }attributes[opt]
13367 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13368 if the token stream isn't an enum-specifier after all. */
13370 static tree
13371 cp_parser_enum_specifier (cp_parser* parser)
13373 tree identifier;
13374 tree type = NULL_TREE;
13375 tree prev_scope;
13376 tree nested_name_specifier = NULL_TREE;
13377 tree attributes;
13378 bool scoped_enum_p = false;
13379 bool has_underlying_type = false;
13380 bool nested_being_defined = false;
13381 bool new_value_list = false;
13382 bool is_new_type = false;
13383 bool is_anonymous = false;
13384 tree underlying_type = NULL_TREE;
13385 cp_token *type_start_token = NULL;
13386 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13388 parser->colon_corrects_to_scope_p = false;
13390 /* Parse tentatively so that we can back up if we don't find a
13391 enum-specifier. */
13392 cp_parser_parse_tentatively (parser);
13394 /* Caller guarantees that the current token is 'enum', an identifier
13395 possibly follows, and the token after that is an opening brace.
13396 If we don't have an identifier, fabricate an anonymous name for
13397 the enumeration being defined. */
13398 cp_lexer_consume_token (parser->lexer);
13400 /* Parse the "class" or "struct", which indicates a scoped
13401 enumeration type in C++0x. */
13402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13403 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13405 if (cxx_dialect < cxx0x)
13406 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13408 /* Consume the `struct' or `class' token. */
13409 cp_lexer_consume_token (parser->lexer);
13411 scoped_enum_p = true;
13414 attributes = cp_parser_attributes_opt (parser);
13416 /* Clear the qualification. */
13417 parser->scope = NULL_TREE;
13418 parser->qualifying_scope = NULL_TREE;
13419 parser->object_scope = NULL_TREE;
13421 /* Figure out in what scope the declaration is being placed. */
13422 prev_scope = current_scope ();
13424 type_start_token = cp_lexer_peek_token (parser->lexer);
13426 push_deferring_access_checks (dk_no_check);
13427 nested_name_specifier
13428 = cp_parser_nested_name_specifier_opt (parser,
13429 /*typename_keyword_p=*/true,
13430 /*check_dependency_p=*/false,
13431 /*type_p=*/false,
13432 /*is_declaration=*/false);
13434 if (nested_name_specifier)
13436 tree name;
13438 identifier = cp_parser_identifier (parser);
13439 name = cp_parser_lookup_name (parser, identifier,
13440 enum_type,
13441 /*is_template=*/false,
13442 /*is_namespace=*/false,
13443 /*check_dependency=*/true,
13444 /*ambiguous_decls=*/NULL,
13445 input_location);
13446 if (name)
13448 type = TREE_TYPE (name);
13449 if (TREE_CODE (type) == TYPENAME_TYPE)
13451 /* Are template enums allowed in ISO? */
13452 if (template_parm_scope_p ())
13453 pedwarn (type_start_token->location, OPT_pedantic,
13454 "%qD is an enumeration template", name);
13455 /* ignore a typename reference, for it will be solved by name
13456 in start_enum. */
13457 type = NULL_TREE;
13460 else
13461 error_at (type_start_token->location,
13462 "%qD is not an enumerator-name", identifier);
13464 else
13466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13467 identifier = cp_parser_identifier (parser);
13468 else
13470 identifier = make_anon_name ();
13471 is_anonymous = true;
13474 pop_deferring_access_checks ();
13476 /* Check for the `:' that denotes a specified underlying type in C++0x.
13477 Note that a ':' could also indicate a bitfield width, however. */
13478 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13480 cp_decl_specifier_seq type_specifiers;
13482 /* Consume the `:'. */
13483 cp_lexer_consume_token (parser->lexer);
13485 /* Parse the type-specifier-seq. */
13486 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13487 /*is_trailing_return=*/false,
13488 &type_specifiers);
13490 /* At this point this is surely not elaborated type specifier. */
13491 if (!cp_parser_parse_definitely (parser))
13492 return NULL_TREE;
13494 if (cxx_dialect < cxx0x)
13495 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13497 has_underlying_type = true;
13499 /* If that didn't work, stop. */
13500 if (type_specifiers.type != error_mark_node)
13502 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13503 /*initialized=*/0, NULL);
13504 if (underlying_type == error_mark_node)
13505 underlying_type = NULL_TREE;
13509 /* Look for the `{' but don't consume it yet. */
13510 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13512 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13514 cp_parser_error (parser, "expected %<{%>");
13515 if (has_underlying_type)
13517 type = NULL_TREE;
13518 goto out;
13521 /* An opaque-enum-specifier must have a ';' here. */
13522 if ((scoped_enum_p || underlying_type)
13523 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13525 cp_parser_error (parser, "expected %<;%> or %<{%>");
13526 if (has_underlying_type)
13528 type = NULL_TREE;
13529 goto out;
13534 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13535 return NULL_TREE;
13537 if (nested_name_specifier)
13539 if (CLASS_TYPE_P (nested_name_specifier))
13541 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13542 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13543 push_scope (nested_name_specifier);
13545 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13547 push_nested_namespace (nested_name_specifier);
13551 /* Issue an error message if type-definitions are forbidden here. */
13552 if (!cp_parser_check_type_definition (parser))
13553 type = error_mark_node;
13554 else
13555 /* Create the new type. We do this before consuming the opening
13556 brace so the enum will be recorded as being on the line of its
13557 tag (or the 'enum' keyword, if there is no tag). */
13558 type = start_enum (identifier, type, underlying_type,
13559 scoped_enum_p, &is_new_type);
13561 /* If the next token is not '{' it is an opaque-enum-specifier or an
13562 elaborated-type-specifier. */
13563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13565 if (nested_name_specifier)
13567 /* The following catches invalid code such as:
13568 enum class S<int>::E { A, B, C }; */
13569 if (!processing_specialization
13570 && CLASS_TYPE_P (nested_name_specifier)
13571 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13572 error_at (type_start_token->location, "cannot add an enumerator "
13573 "list to a template instantiation");
13575 /* If that scope does not contain the scope in which the
13576 class was originally declared, the program is invalid. */
13577 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13579 if (at_namespace_scope_p ())
13580 error_at (type_start_token->location,
13581 "declaration of %qD in namespace %qD which does not "
13582 "enclose %qD",
13583 type, prev_scope, nested_name_specifier);
13584 else
13585 error_at (type_start_token->location,
13586 "declaration of %qD in %qD which does not enclose %qD",
13587 type, prev_scope, nested_name_specifier);
13588 type = error_mark_node;
13592 if (scoped_enum_p)
13593 begin_scope (sk_scoped_enum, type);
13595 /* Consume the opening brace. */
13596 cp_lexer_consume_token (parser->lexer);
13598 if (type == error_mark_node)
13599 ; /* Nothing to add */
13600 else if (OPAQUE_ENUM_P (type)
13601 || (cxx_dialect > cxx98 && processing_specialization))
13603 new_value_list = true;
13604 SET_OPAQUE_ENUM_P (type, false);
13605 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13607 else
13609 error_at (type_start_token->location, "multiple definition of %q#T", type);
13610 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13611 "previous definition here");
13612 type = error_mark_node;
13615 if (type == error_mark_node)
13616 cp_parser_skip_to_end_of_block_or_statement (parser);
13617 /* If the next token is not '}', then there are some enumerators. */
13618 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13619 cp_parser_enumerator_list (parser, type);
13621 /* Consume the final '}'. */
13622 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13624 if (scoped_enum_p)
13625 finish_scope ();
13627 else
13629 /* If a ';' follows, then it is an opaque-enum-specifier
13630 and additional restrictions apply. */
13631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13633 if (is_anonymous)
13634 error_at (type_start_token->location,
13635 "opaque-enum-specifier without name");
13636 else if (nested_name_specifier)
13637 error_at (type_start_token->location,
13638 "opaque-enum-specifier must use a simple identifier");
13642 /* Look for trailing attributes to apply to this enumeration, and
13643 apply them if appropriate. */
13644 if (cp_parser_allow_gnu_extensions_p (parser))
13646 tree trailing_attr = cp_parser_attributes_opt (parser);
13647 trailing_attr = chainon (trailing_attr, attributes);
13648 cplus_decl_attributes (&type,
13649 trailing_attr,
13650 (int) ATTR_FLAG_TYPE_IN_PLACE);
13653 /* Finish up the enumeration. */
13654 if (type != error_mark_node)
13656 if (new_value_list)
13657 finish_enum_value_list (type);
13658 if (is_new_type)
13659 finish_enum (type);
13662 if (nested_name_specifier)
13664 if (CLASS_TYPE_P (nested_name_specifier))
13666 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13667 pop_scope (nested_name_specifier);
13669 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13671 pop_nested_namespace (nested_name_specifier);
13674 out:
13675 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13676 return type;
13679 /* Parse an enumerator-list. The enumerators all have the indicated
13680 TYPE.
13682 enumerator-list:
13683 enumerator-definition
13684 enumerator-list , enumerator-definition */
13686 static void
13687 cp_parser_enumerator_list (cp_parser* parser, tree type)
13689 while (true)
13691 /* Parse an enumerator-definition. */
13692 cp_parser_enumerator_definition (parser, type);
13694 /* If the next token is not a ',', we've reached the end of
13695 the list. */
13696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13697 break;
13698 /* Otherwise, consume the `,' and keep going. */
13699 cp_lexer_consume_token (parser->lexer);
13700 /* If the next token is a `}', there is a trailing comma. */
13701 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13703 if (!in_system_header)
13704 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13705 break;
13710 /* Parse an enumerator-definition. The enumerator has the indicated
13711 TYPE.
13713 enumerator-definition:
13714 enumerator
13715 enumerator = constant-expression
13717 enumerator:
13718 identifier */
13720 static void
13721 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13723 tree identifier;
13724 tree value;
13725 location_t loc;
13727 /* Save the input location because we are interested in the location
13728 of the identifier and not the location of the explicit value. */
13729 loc = cp_lexer_peek_token (parser->lexer)->location;
13731 /* Look for the identifier. */
13732 identifier = cp_parser_identifier (parser);
13733 if (identifier == error_mark_node)
13734 return;
13736 /* If the next token is an '=', then there is an explicit value. */
13737 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13739 /* Consume the `=' token. */
13740 cp_lexer_consume_token (parser->lexer);
13741 /* Parse the value. */
13742 value = cp_parser_constant_expression (parser,
13743 /*allow_non_constant_p=*/false,
13744 NULL);
13746 else
13747 value = NULL_TREE;
13749 /* If we are processing a template, make sure the initializer of the
13750 enumerator doesn't contain any bare template parameter pack. */
13751 if (check_for_bare_parameter_packs (value))
13752 value = error_mark_node;
13754 /* Create the enumerator. */
13755 build_enumerator (identifier, value, type, loc);
13758 /* Parse a namespace-name.
13760 namespace-name:
13761 original-namespace-name
13762 namespace-alias
13764 Returns the NAMESPACE_DECL for the namespace. */
13766 static tree
13767 cp_parser_namespace_name (cp_parser* parser)
13769 tree identifier;
13770 tree namespace_decl;
13772 cp_token *token = cp_lexer_peek_token (parser->lexer);
13774 /* Get the name of the namespace. */
13775 identifier = cp_parser_identifier (parser);
13776 if (identifier == error_mark_node)
13777 return error_mark_node;
13779 /* Look up the identifier in the currently active scope. Look only
13780 for namespaces, due to:
13782 [basic.lookup.udir]
13784 When looking up a namespace-name in a using-directive or alias
13785 definition, only namespace names are considered.
13787 And:
13789 [basic.lookup.qual]
13791 During the lookup of a name preceding the :: scope resolution
13792 operator, object, function, and enumerator names are ignored.
13794 (Note that cp_parser_qualifying_entity only calls this
13795 function if the token after the name is the scope resolution
13796 operator.) */
13797 namespace_decl = cp_parser_lookup_name (parser, identifier,
13798 none_type,
13799 /*is_template=*/false,
13800 /*is_namespace=*/true,
13801 /*check_dependency=*/true,
13802 /*ambiguous_decls=*/NULL,
13803 token->location);
13804 /* If it's not a namespace, issue an error. */
13805 if (namespace_decl == error_mark_node
13806 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13808 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13809 error_at (token->location, "%qD is not a namespace-name", identifier);
13810 cp_parser_error (parser, "expected namespace-name");
13811 namespace_decl = error_mark_node;
13814 return namespace_decl;
13817 /* Parse a namespace-definition.
13819 namespace-definition:
13820 named-namespace-definition
13821 unnamed-namespace-definition
13823 named-namespace-definition:
13824 original-namespace-definition
13825 extension-namespace-definition
13827 original-namespace-definition:
13828 namespace identifier { namespace-body }
13830 extension-namespace-definition:
13831 namespace original-namespace-name { namespace-body }
13833 unnamed-namespace-definition:
13834 namespace { namespace-body } */
13836 static void
13837 cp_parser_namespace_definition (cp_parser* parser)
13839 tree identifier, attribs;
13840 bool has_visibility;
13841 bool is_inline;
13843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13845 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13846 is_inline = true;
13847 cp_lexer_consume_token (parser->lexer);
13849 else
13850 is_inline = false;
13852 /* Look for the `namespace' keyword. */
13853 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13855 /* Get the name of the namespace. We do not attempt to distinguish
13856 between an original-namespace-definition and an
13857 extension-namespace-definition at this point. The semantic
13858 analysis routines are responsible for that. */
13859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13860 identifier = cp_parser_identifier (parser);
13861 else
13862 identifier = NULL_TREE;
13864 /* Parse any specified attributes. */
13865 attribs = cp_parser_attributes_opt (parser);
13867 /* Look for the `{' to start the namespace. */
13868 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13869 /* Start the namespace. */
13870 push_namespace (identifier);
13872 /* "inline namespace" is equivalent to a stub namespace definition
13873 followed by a strong using directive. */
13874 if (is_inline)
13876 tree name_space = current_namespace;
13877 /* Set up namespace association. */
13878 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13879 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13880 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13881 /* Import the contents of the inline namespace. */
13882 pop_namespace ();
13883 do_using_directive (name_space);
13884 push_namespace (identifier);
13887 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13889 /* Parse the body of the namespace. */
13890 cp_parser_namespace_body (parser);
13892 if (has_visibility)
13893 pop_visibility (1);
13895 /* Finish the namespace. */
13896 pop_namespace ();
13897 /* Look for the final `}'. */
13898 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13901 /* Parse a namespace-body.
13903 namespace-body:
13904 declaration-seq [opt] */
13906 static void
13907 cp_parser_namespace_body (cp_parser* parser)
13909 cp_parser_declaration_seq_opt (parser);
13912 /* Parse a namespace-alias-definition.
13914 namespace-alias-definition:
13915 namespace identifier = qualified-namespace-specifier ; */
13917 static void
13918 cp_parser_namespace_alias_definition (cp_parser* parser)
13920 tree identifier;
13921 tree namespace_specifier;
13923 cp_token *token = cp_lexer_peek_token (parser->lexer);
13925 /* Look for the `namespace' keyword. */
13926 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13927 /* Look for the identifier. */
13928 identifier = cp_parser_identifier (parser);
13929 if (identifier == error_mark_node)
13930 return;
13931 /* Look for the `=' token. */
13932 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13933 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13935 error_at (token->location, "%<namespace%> definition is not allowed here");
13936 /* Skip the definition. */
13937 cp_lexer_consume_token (parser->lexer);
13938 if (cp_parser_skip_to_closing_brace (parser))
13939 cp_lexer_consume_token (parser->lexer);
13940 return;
13942 cp_parser_require (parser, CPP_EQ, RT_EQ);
13943 /* Look for the qualified-namespace-specifier. */
13944 namespace_specifier
13945 = cp_parser_qualified_namespace_specifier (parser);
13946 /* Look for the `;' token. */
13947 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13949 /* Register the alias in the symbol table. */
13950 do_namespace_alias (identifier, namespace_specifier);
13953 /* Parse a qualified-namespace-specifier.
13955 qualified-namespace-specifier:
13956 :: [opt] nested-name-specifier [opt] namespace-name
13958 Returns a NAMESPACE_DECL corresponding to the specified
13959 namespace. */
13961 static tree
13962 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13964 /* Look for the optional `::'. */
13965 cp_parser_global_scope_opt (parser,
13966 /*current_scope_valid_p=*/false);
13968 /* Look for the optional nested-name-specifier. */
13969 cp_parser_nested_name_specifier_opt (parser,
13970 /*typename_keyword_p=*/false,
13971 /*check_dependency_p=*/true,
13972 /*type_p=*/false,
13973 /*is_declaration=*/true);
13975 return cp_parser_namespace_name (parser);
13978 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13979 access declaration.
13981 using-declaration:
13982 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13983 using :: unqualified-id ;
13985 access-declaration:
13986 qualified-id ;
13990 static bool
13991 cp_parser_using_declaration (cp_parser* parser,
13992 bool access_declaration_p)
13994 cp_token *token;
13995 bool typename_p = false;
13996 bool global_scope_p;
13997 tree decl;
13998 tree identifier;
13999 tree qscope;
14001 if (access_declaration_p)
14002 cp_parser_parse_tentatively (parser);
14003 else
14005 /* Look for the `using' keyword. */
14006 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14008 /* Peek at the next token. */
14009 token = cp_lexer_peek_token (parser->lexer);
14010 /* See if it's `typename'. */
14011 if (token->keyword == RID_TYPENAME)
14013 /* Remember that we've seen it. */
14014 typename_p = true;
14015 /* Consume the `typename' token. */
14016 cp_lexer_consume_token (parser->lexer);
14020 /* Look for the optional global scope qualification. */
14021 global_scope_p
14022 = (cp_parser_global_scope_opt (parser,
14023 /*current_scope_valid_p=*/false)
14024 != NULL_TREE);
14026 /* If we saw `typename', or didn't see `::', then there must be a
14027 nested-name-specifier present. */
14028 if (typename_p || !global_scope_p)
14029 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14030 /*check_dependency_p=*/true,
14031 /*type_p=*/false,
14032 /*is_declaration=*/true);
14033 /* Otherwise, we could be in either of the two productions. In that
14034 case, treat the nested-name-specifier as optional. */
14035 else
14036 qscope = cp_parser_nested_name_specifier_opt (parser,
14037 /*typename_keyword_p=*/false,
14038 /*check_dependency_p=*/true,
14039 /*type_p=*/false,
14040 /*is_declaration=*/true);
14041 if (!qscope)
14042 qscope = global_namespace;
14044 if (access_declaration_p && cp_parser_error_occurred (parser))
14045 /* Something has already gone wrong; there's no need to parse
14046 further. Since an error has occurred, the return value of
14047 cp_parser_parse_definitely will be false, as required. */
14048 return cp_parser_parse_definitely (parser);
14050 token = cp_lexer_peek_token (parser->lexer);
14051 /* Parse the unqualified-id. */
14052 identifier = cp_parser_unqualified_id (parser,
14053 /*template_keyword_p=*/false,
14054 /*check_dependency_p=*/true,
14055 /*declarator_p=*/true,
14056 /*optional_p=*/false);
14058 if (access_declaration_p)
14060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14061 cp_parser_simulate_error (parser);
14062 if (!cp_parser_parse_definitely (parser))
14063 return false;
14066 /* The function we call to handle a using-declaration is different
14067 depending on what scope we are in. */
14068 if (qscope == error_mark_node || identifier == error_mark_node)
14070 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14071 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14072 /* [namespace.udecl]
14074 A using declaration shall not name a template-id. */
14075 error_at (token->location,
14076 "a template-id may not appear in a using-declaration");
14077 else
14079 if (at_class_scope_p ())
14081 /* Create the USING_DECL. */
14082 decl = do_class_using_decl (parser->scope, identifier);
14084 if (check_for_bare_parameter_packs (decl))
14085 return false;
14086 else
14087 /* Add it to the list of members in this class. */
14088 finish_member_declaration (decl);
14090 else
14092 decl = cp_parser_lookup_name_simple (parser,
14093 identifier,
14094 token->location);
14095 if (decl == error_mark_node)
14096 cp_parser_name_lookup_error (parser, identifier,
14097 decl, NLE_NULL,
14098 token->location);
14099 else if (check_for_bare_parameter_packs (decl))
14100 return false;
14101 else if (!at_namespace_scope_p ())
14102 do_local_using_decl (decl, qscope, identifier);
14103 else
14104 do_toplevel_using_decl (decl, qscope, identifier);
14108 /* Look for the final `;'. */
14109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14111 return true;
14114 /* Parse a using-directive.
14116 using-directive:
14117 using namespace :: [opt] nested-name-specifier [opt]
14118 namespace-name ; */
14120 static void
14121 cp_parser_using_directive (cp_parser* parser)
14123 tree namespace_decl;
14124 tree attribs;
14126 /* Look for the `using' keyword. */
14127 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14128 /* And the `namespace' keyword. */
14129 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14130 /* Look for the optional `::' operator. */
14131 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14132 /* And the optional nested-name-specifier. */
14133 cp_parser_nested_name_specifier_opt (parser,
14134 /*typename_keyword_p=*/false,
14135 /*check_dependency_p=*/true,
14136 /*type_p=*/false,
14137 /*is_declaration=*/true);
14138 /* Get the namespace being used. */
14139 namespace_decl = cp_parser_namespace_name (parser);
14140 /* And any specified attributes. */
14141 attribs = cp_parser_attributes_opt (parser);
14142 /* Update the symbol table. */
14143 parse_using_directive (namespace_decl, attribs);
14144 /* Look for the final `;'. */
14145 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14148 /* Parse an asm-definition.
14150 asm-definition:
14151 asm ( string-literal ) ;
14153 GNU Extension:
14155 asm-definition:
14156 asm volatile [opt] ( string-literal ) ;
14157 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14158 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14159 : asm-operand-list [opt] ) ;
14160 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14161 : asm-operand-list [opt]
14162 : asm-clobber-list [opt] ) ;
14163 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14164 : asm-clobber-list [opt]
14165 : asm-goto-list ) ; */
14167 static void
14168 cp_parser_asm_definition (cp_parser* parser)
14170 tree string;
14171 tree outputs = NULL_TREE;
14172 tree inputs = NULL_TREE;
14173 tree clobbers = NULL_TREE;
14174 tree labels = NULL_TREE;
14175 tree asm_stmt;
14176 bool volatile_p = false;
14177 bool extended_p = false;
14178 bool invalid_inputs_p = false;
14179 bool invalid_outputs_p = false;
14180 bool goto_p = false;
14181 required_token missing = RT_NONE;
14183 /* Look for the `asm' keyword. */
14184 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14185 /* See if the next token is `volatile'. */
14186 if (cp_parser_allow_gnu_extensions_p (parser)
14187 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14189 /* Remember that we saw the `volatile' keyword. */
14190 volatile_p = true;
14191 /* Consume the token. */
14192 cp_lexer_consume_token (parser->lexer);
14194 if (cp_parser_allow_gnu_extensions_p (parser)
14195 && parser->in_function_body
14196 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14198 /* Remember that we saw the `goto' keyword. */
14199 goto_p = true;
14200 /* Consume the token. */
14201 cp_lexer_consume_token (parser->lexer);
14203 /* Look for the opening `('. */
14204 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14205 return;
14206 /* Look for the string. */
14207 string = cp_parser_string_literal (parser, false, false);
14208 if (string == error_mark_node)
14210 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14211 /*consume_paren=*/true);
14212 return;
14215 /* If we're allowing GNU extensions, check for the extended assembly
14216 syntax. Unfortunately, the `:' tokens need not be separated by
14217 a space in C, and so, for compatibility, we tolerate that here
14218 too. Doing that means that we have to treat the `::' operator as
14219 two `:' tokens. */
14220 if (cp_parser_allow_gnu_extensions_p (parser)
14221 && parser->in_function_body
14222 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14223 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14225 bool inputs_p = false;
14226 bool clobbers_p = false;
14227 bool labels_p = false;
14229 /* The extended syntax was used. */
14230 extended_p = true;
14232 /* Look for outputs. */
14233 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14235 /* Consume the `:'. */
14236 cp_lexer_consume_token (parser->lexer);
14237 /* Parse the output-operands. */
14238 if (cp_lexer_next_token_is_not (parser->lexer,
14239 CPP_COLON)
14240 && cp_lexer_next_token_is_not (parser->lexer,
14241 CPP_SCOPE)
14242 && cp_lexer_next_token_is_not (parser->lexer,
14243 CPP_CLOSE_PAREN)
14244 && !goto_p)
14245 outputs = cp_parser_asm_operand_list (parser);
14247 if (outputs == error_mark_node)
14248 invalid_outputs_p = true;
14250 /* If the next token is `::', there are no outputs, and the
14251 next token is the beginning of the inputs. */
14252 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14253 /* The inputs are coming next. */
14254 inputs_p = true;
14256 /* Look for inputs. */
14257 if (inputs_p
14258 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14260 /* Consume the `:' or `::'. */
14261 cp_lexer_consume_token (parser->lexer);
14262 /* Parse the output-operands. */
14263 if (cp_lexer_next_token_is_not (parser->lexer,
14264 CPP_COLON)
14265 && cp_lexer_next_token_is_not (parser->lexer,
14266 CPP_SCOPE)
14267 && cp_lexer_next_token_is_not (parser->lexer,
14268 CPP_CLOSE_PAREN))
14269 inputs = cp_parser_asm_operand_list (parser);
14271 if (inputs == error_mark_node)
14272 invalid_inputs_p = true;
14274 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14275 /* The clobbers are coming next. */
14276 clobbers_p = true;
14278 /* Look for clobbers. */
14279 if (clobbers_p
14280 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14282 clobbers_p = true;
14283 /* Consume the `:' or `::'. */
14284 cp_lexer_consume_token (parser->lexer);
14285 /* Parse the clobbers. */
14286 if (cp_lexer_next_token_is_not (parser->lexer,
14287 CPP_COLON)
14288 && cp_lexer_next_token_is_not (parser->lexer,
14289 CPP_CLOSE_PAREN))
14290 clobbers = cp_parser_asm_clobber_list (parser);
14292 else if (goto_p
14293 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14294 /* The labels are coming next. */
14295 labels_p = true;
14297 /* Look for labels. */
14298 if (labels_p
14299 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14301 labels_p = true;
14302 /* Consume the `:' or `::'. */
14303 cp_lexer_consume_token (parser->lexer);
14304 /* Parse the labels. */
14305 labels = cp_parser_asm_label_list (parser);
14308 if (goto_p && !labels_p)
14309 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14311 else if (goto_p)
14312 missing = RT_COLON_SCOPE;
14314 /* Look for the closing `)'. */
14315 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14316 missing ? missing : RT_CLOSE_PAREN))
14317 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14318 /*consume_paren=*/true);
14319 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14321 if (!invalid_inputs_p && !invalid_outputs_p)
14323 /* Create the ASM_EXPR. */
14324 if (parser->in_function_body)
14326 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14327 inputs, clobbers, labels);
14328 /* If the extended syntax was not used, mark the ASM_EXPR. */
14329 if (!extended_p)
14331 tree temp = asm_stmt;
14332 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14333 temp = TREE_OPERAND (temp, 0);
14335 ASM_INPUT_P (temp) = 1;
14338 else
14339 cgraph_add_asm_node (string);
14343 /* Declarators [gram.dcl.decl] */
14345 /* Parse an init-declarator.
14347 init-declarator:
14348 declarator initializer [opt]
14350 GNU Extension:
14352 init-declarator:
14353 declarator asm-specification [opt] attributes [opt] initializer [opt]
14355 function-definition:
14356 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14357 function-body
14358 decl-specifier-seq [opt] declarator function-try-block
14360 GNU Extension:
14362 function-definition:
14363 __extension__ function-definition
14365 The DECL_SPECIFIERS apply to this declarator. Returns a
14366 representation of the entity declared. If MEMBER_P is TRUE, then
14367 this declarator appears in a class scope. The new DECL created by
14368 this declarator is returned.
14370 The CHECKS are access checks that should be performed once we know
14371 what entity is being declared (and, therefore, what classes have
14372 befriended it).
14374 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14375 for a function-definition here as well. If the declarator is a
14376 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14377 be TRUE upon return. By that point, the function-definition will
14378 have been completely parsed.
14380 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14381 is FALSE. */
14383 static tree
14384 cp_parser_init_declarator (cp_parser* parser,
14385 cp_decl_specifier_seq *decl_specifiers,
14386 VEC (deferred_access_check,gc)* checks,
14387 bool function_definition_allowed_p,
14388 bool member_p,
14389 int declares_class_or_enum,
14390 bool* function_definition_p)
14392 cp_token *token = NULL, *asm_spec_start_token = NULL,
14393 *attributes_start_token = NULL;
14394 cp_declarator *declarator;
14395 tree prefix_attributes;
14396 tree attributes;
14397 tree asm_specification;
14398 tree initializer;
14399 tree decl = NULL_TREE;
14400 tree scope;
14401 int is_initialized;
14402 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14403 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14404 "(...)". */
14405 enum cpp_ttype initialization_kind;
14406 bool is_direct_init = false;
14407 bool is_non_constant_init;
14408 int ctor_dtor_or_conv_p;
14409 bool friend_p;
14410 tree pushed_scope = NULL;
14412 /* Gather the attributes that were provided with the
14413 decl-specifiers. */
14414 prefix_attributes = decl_specifiers->attributes;
14416 /* Assume that this is not the declarator for a function
14417 definition. */
14418 if (function_definition_p)
14419 *function_definition_p = false;
14421 /* Defer access checks while parsing the declarator; we cannot know
14422 what names are accessible until we know what is being
14423 declared. */
14424 resume_deferring_access_checks ();
14426 /* Parse the declarator. */
14427 token = cp_lexer_peek_token (parser->lexer);
14428 declarator
14429 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14430 &ctor_dtor_or_conv_p,
14431 /*parenthesized_p=*/NULL,
14432 /*member_p=*/false);
14433 /* Gather up the deferred checks. */
14434 stop_deferring_access_checks ();
14436 /* If the DECLARATOR was erroneous, there's no need to go
14437 further. */
14438 if (declarator == cp_error_declarator)
14439 return error_mark_node;
14441 /* Check that the number of template-parameter-lists is OK. */
14442 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14443 token->location))
14444 return error_mark_node;
14446 if (declares_class_or_enum & 2)
14447 cp_parser_check_for_definition_in_return_type (declarator,
14448 decl_specifiers->type,
14449 decl_specifiers->type_location);
14451 /* Figure out what scope the entity declared by the DECLARATOR is
14452 located in. `grokdeclarator' sometimes changes the scope, so
14453 we compute it now. */
14454 scope = get_scope_of_declarator (declarator);
14456 /* Perform any lookups in the declared type which were thought to be
14457 dependent, but are not in the scope of the declarator. */
14458 decl_specifiers->type
14459 = maybe_update_decl_type (decl_specifiers->type, scope);
14461 /* If we're allowing GNU extensions, look for an asm-specification
14462 and attributes. */
14463 if (cp_parser_allow_gnu_extensions_p (parser))
14465 /* Look for an asm-specification. */
14466 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14467 asm_specification = cp_parser_asm_specification_opt (parser);
14468 /* And attributes. */
14469 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14470 attributes = cp_parser_attributes_opt (parser);
14472 else
14474 asm_specification = NULL_TREE;
14475 attributes = NULL_TREE;
14478 /* Peek at the next token. */
14479 token = cp_lexer_peek_token (parser->lexer);
14480 /* Check to see if the token indicates the start of a
14481 function-definition. */
14482 if (function_declarator_p (declarator)
14483 && cp_parser_token_starts_function_definition_p (token))
14485 if (!function_definition_allowed_p)
14487 /* If a function-definition should not appear here, issue an
14488 error message. */
14489 cp_parser_error (parser,
14490 "a function-definition is not allowed here");
14491 return error_mark_node;
14493 else
14495 location_t func_brace_location
14496 = cp_lexer_peek_token (parser->lexer)->location;
14498 /* Neither attributes nor an asm-specification are allowed
14499 on a function-definition. */
14500 if (asm_specification)
14501 error_at (asm_spec_start_token->location,
14502 "an asm-specification is not allowed "
14503 "on a function-definition");
14504 if (attributes)
14505 error_at (attributes_start_token->location,
14506 "attributes are not allowed on a function-definition");
14507 /* This is a function-definition. */
14508 *function_definition_p = true;
14510 /* Parse the function definition. */
14511 if (member_p)
14512 decl = cp_parser_save_member_function_body (parser,
14513 decl_specifiers,
14514 declarator,
14515 prefix_attributes);
14516 else
14517 decl
14518 = (cp_parser_function_definition_from_specifiers_and_declarator
14519 (parser, decl_specifiers, prefix_attributes, declarator));
14521 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14523 /* This is where the prologue starts... */
14524 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14525 = func_brace_location;
14528 return decl;
14532 /* [dcl.dcl]
14534 Only in function declarations for constructors, destructors, and
14535 type conversions can the decl-specifier-seq be omitted.
14537 We explicitly postpone this check past the point where we handle
14538 function-definitions because we tolerate function-definitions
14539 that are missing their return types in some modes. */
14540 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14542 cp_parser_error (parser,
14543 "expected constructor, destructor, or type conversion");
14544 return error_mark_node;
14547 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14548 if (token->type == CPP_EQ
14549 || token->type == CPP_OPEN_PAREN
14550 || token->type == CPP_OPEN_BRACE)
14552 is_initialized = SD_INITIALIZED;
14553 initialization_kind = token->type;
14555 if (token->type == CPP_EQ
14556 && function_declarator_p (declarator))
14558 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14559 if (t2->keyword == RID_DEFAULT)
14560 is_initialized = SD_DEFAULTED;
14561 else if (t2->keyword == RID_DELETE)
14562 is_initialized = SD_DELETED;
14565 else
14567 /* If the init-declarator isn't initialized and isn't followed by a
14568 `,' or `;', it's not a valid init-declarator. */
14569 if (token->type != CPP_COMMA
14570 && token->type != CPP_SEMICOLON)
14572 cp_parser_error (parser, "expected initializer");
14573 return error_mark_node;
14575 is_initialized = SD_UNINITIALIZED;
14576 initialization_kind = CPP_EOF;
14579 /* Because start_decl has side-effects, we should only call it if we
14580 know we're going ahead. By this point, we know that we cannot
14581 possibly be looking at any other construct. */
14582 cp_parser_commit_to_tentative_parse (parser);
14584 /* If the decl specifiers were bad, issue an error now that we're
14585 sure this was intended to be a declarator. Then continue
14586 declaring the variable(s), as int, to try to cut down on further
14587 errors. */
14588 if (decl_specifiers->any_specifiers_p
14589 && decl_specifiers->type == error_mark_node)
14591 cp_parser_error (parser, "invalid type in declaration");
14592 decl_specifiers->type = integer_type_node;
14595 /* Check to see whether or not this declaration is a friend. */
14596 friend_p = cp_parser_friend_p (decl_specifiers);
14598 /* Enter the newly declared entry in the symbol table. If we're
14599 processing a declaration in a class-specifier, we wait until
14600 after processing the initializer. */
14601 if (!member_p)
14603 if (parser->in_unbraced_linkage_specification_p)
14604 decl_specifiers->storage_class = sc_extern;
14605 decl = start_decl (declarator, decl_specifiers,
14606 is_initialized, attributes, prefix_attributes,
14607 &pushed_scope);
14608 /* Adjust location of decl if declarator->id_loc is more appropriate:
14609 set, and decl wasn't merged with another decl, in which case its
14610 location would be different from input_location, and more accurate. */
14611 if (DECL_P (decl)
14612 && declarator->id_loc != UNKNOWN_LOCATION
14613 && DECL_SOURCE_LOCATION (decl) == input_location)
14614 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14616 else if (scope)
14617 /* Enter the SCOPE. That way unqualified names appearing in the
14618 initializer will be looked up in SCOPE. */
14619 pushed_scope = push_scope (scope);
14621 /* Perform deferred access control checks, now that we know in which
14622 SCOPE the declared entity resides. */
14623 if (!member_p && decl)
14625 tree saved_current_function_decl = NULL_TREE;
14627 /* If the entity being declared is a function, pretend that we
14628 are in its scope. If it is a `friend', it may have access to
14629 things that would not otherwise be accessible. */
14630 if (TREE_CODE (decl) == FUNCTION_DECL)
14632 saved_current_function_decl = current_function_decl;
14633 current_function_decl = decl;
14636 /* Perform access checks for template parameters. */
14637 cp_parser_perform_template_parameter_access_checks (checks);
14639 /* Perform the access control checks for the declarator and the
14640 decl-specifiers. */
14641 perform_deferred_access_checks ();
14643 /* Restore the saved value. */
14644 if (TREE_CODE (decl) == FUNCTION_DECL)
14645 current_function_decl = saved_current_function_decl;
14648 /* Parse the initializer. */
14649 initializer = NULL_TREE;
14650 is_direct_init = false;
14651 is_non_constant_init = true;
14652 if (is_initialized)
14654 if (function_declarator_p (declarator))
14656 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14657 if (initialization_kind == CPP_EQ)
14658 initializer = cp_parser_pure_specifier (parser);
14659 else
14661 /* If the declaration was erroneous, we don't really
14662 know what the user intended, so just silently
14663 consume the initializer. */
14664 if (decl != error_mark_node)
14665 error_at (initializer_start_token->location,
14666 "initializer provided for function");
14667 cp_parser_skip_to_closing_parenthesis (parser,
14668 /*recovering=*/true,
14669 /*or_comma=*/false,
14670 /*consume_paren=*/true);
14673 else
14675 /* We want to record the extra mangling scope for in-class
14676 initializers of class members and initializers of static data
14677 member templates. The former is a C++0x feature which isn't
14678 implemented yet, and I expect it will involve deferring
14679 parsing of the initializer until end of class as with default
14680 arguments. So right here we only handle the latter. */
14681 if (!member_p && processing_template_decl)
14682 start_lambda_scope (decl);
14683 initializer = cp_parser_initializer (parser,
14684 &is_direct_init,
14685 &is_non_constant_init);
14686 if (!member_p && processing_template_decl)
14687 finish_lambda_scope ();
14691 /* The old parser allows attributes to appear after a parenthesized
14692 initializer. Mark Mitchell proposed removing this functionality
14693 on the GCC mailing lists on 2002-08-13. This parser accepts the
14694 attributes -- but ignores them. */
14695 if (cp_parser_allow_gnu_extensions_p (parser)
14696 && initialization_kind == CPP_OPEN_PAREN)
14697 if (cp_parser_attributes_opt (parser))
14698 warning (OPT_Wattributes,
14699 "attributes after parenthesized initializer ignored");
14701 /* For an in-class declaration, use `grokfield' to create the
14702 declaration. */
14703 if (member_p)
14705 if (pushed_scope)
14707 pop_scope (pushed_scope);
14708 pushed_scope = false;
14710 decl = grokfield (declarator, decl_specifiers,
14711 initializer, !is_non_constant_init,
14712 /*asmspec=*/NULL_TREE,
14713 prefix_attributes);
14714 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14715 cp_parser_save_default_args (parser, decl);
14718 /* Finish processing the declaration. But, skip friend
14719 declarations. */
14720 if (!friend_p && decl && decl != error_mark_node)
14722 cp_finish_decl (decl,
14723 initializer, !is_non_constant_init,
14724 asm_specification,
14725 /* If the initializer is in parentheses, then this is
14726 a direct-initialization, which means that an
14727 `explicit' constructor is OK. Otherwise, an
14728 `explicit' constructor cannot be used. */
14729 ((is_direct_init || !is_initialized)
14730 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14732 else if ((cxx_dialect != cxx98) && friend_p
14733 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14734 /* Core issue #226 (C++0x only): A default template-argument
14735 shall not be specified in a friend class template
14736 declaration. */
14737 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14738 /*is_partial=*/0, /*is_friend_decl=*/1);
14740 if (!friend_p && pushed_scope)
14741 pop_scope (pushed_scope);
14743 return decl;
14746 /* Parse a declarator.
14748 declarator:
14749 direct-declarator
14750 ptr-operator declarator
14752 abstract-declarator:
14753 ptr-operator abstract-declarator [opt]
14754 direct-abstract-declarator
14756 GNU Extensions:
14758 declarator:
14759 attributes [opt] direct-declarator
14760 attributes [opt] ptr-operator declarator
14762 abstract-declarator:
14763 attributes [opt] ptr-operator abstract-declarator [opt]
14764 attributes [opt] direct-abstract-declarator
14766 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14767 detect constructor, destructor or conversion operators. It is set
14768 to -1 if the declarator is a name, and +1 if it is a
14769 function. Otherwise it is set to zero. Usually you just want to
14770 test for >0, but internally the negative value is used.
14772 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14773 a decl-specifier-seq unless it declares a constructor, destructor,
14774 or conversion. It might seem that we could check this condition in
14775 semantic analysis, rather than parsing, but that makes it difficult
14776 to handle something like `f()'. We want to notice that there are
14777 no decl-specifiers, and therefore realize that this is an
14778 expression, not a declaration.)
14780 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14781 the declarator is a direct-declarator of the form "(...)".
14783 MEMBER_P is true iff this declarator is a member-declarator. */
14785 static cp_declarator *
14786 cp_parser_declarator (cp_parser* parser,
14787 cp_parser_declarator_kind dcl_kind,
14788 int* ctor_dtor_or_conv_p,
14789 bool* parenthesized_p,
14790 bool member_p)
14792 cp_declarator *declarator;
14793 enum tree_code code;
14794 cp_cv_quals cv_quals;
14795 tree class_type;
14796 tree attributes = NULL_TREE;
14798 /* Assume this is not a constructor, destructor, or type-conversion
14799 operator. */
14800 if (ctor_dtor_or_conv_p)
14801 *ctor_dtor_or_conv_p = 0;
14803 if (cp_parser_allow_gnu_extensions_p (parser))
14804 attributes = cp_parser_attributes_opt (parser);
14806 /* Check for the ptr-operator production. */
14807 cp_parser_parse_tentatively (parser);
14808 /* Parse the ptr-operator. */
14809 code = cp_parser_ptr_operator (parser,
14810 &class_type,
14811 &cv_quals);
14812 /* If that worked, then we have a ptr-operator. */
14813 if (cp_parser_parse_definitely (parser))
14815 /* If a ptr-operator was found, then this declarator was not
14816 parenthesized. */
14817 if (parenthesized_p)
14818 *parenthesized_p = true;
14819 /* The dependent declarator is optional if we are parsing an
14820 abstract-declarator. */
14821 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14822 cp_parser_parse_tentatively (parser);
14824 /* Parse the dependent declarator. */
14825 declarator = cp_parser_declarator (parser, dcl_kind,
14826 /*ctor_dtor_or_conv_p=*/NULL,
14827 /*parenthesized_p=*/NULL,
14828 /*member_p=*/false);
14830 /* If we are parsing an abstract-declarator, we must handle the
14831 case where the dependent declarator is absent. */
14832 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14833 && !cp_parser_parse_definitely (parser))
14834 declarator = NULL;
14836 declarator = cp_parser_make_indirect_declarator
14837 (code, class_type, cv_quals, declarator);
14839 /* Everything else is a direct-declarator. */
14840 else
14842 if (parenthesized_p)
14843 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14844 CPP_OPEN_PAREN);
14845 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14846 ctor_dtor_or_conv_p,
14847 member_p);
14850 if (attributes && declarator && declarator != cp_error_declarator)
14851 declarator->attributes = attributes;
14853 return declarator;
14856 /* Parse a direct-declarator or direct-abstract-declarator.
14858 direct-declarator:
14859 declarator-id
14860 direct-declarator ( parameter-declaration-clause )
14861 cv-qualifier-seq [opt]
14862 exception-specification [opt]
14863 direct-declarator [ constant-expression [opt] ]
14864 ( declarator )
14866 direct-abstract-declarator:
14867 direct-abstract-declarator [opt]
14868 ( parameter-declaration-clause )
14869 cv-qualifier-seq [opt]
14870 exception-specification [opt]
14871 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14872 ( abstract-declarator )
14874 Returns a representation of the declarator. DCL_KIND is
14875 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14876 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14877 we are parsing a direct-declarator. It is
14878 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14879 of ambiguity we prefer an abstract declarator, as per
14880 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14881 cp_parser_declarator. */
14883 static cp_declarator *
14884 cp_parser_direct_declarator (cp_parser* parser,
14885 cp_parser_declarator_kind dcl_kind,
14886 int* ctor_dtor_or_conv_p,
14887 bool member_p)
14889 cp_token *token;
14890 cp_declarator *declarator = NULL;
14891 tree scope = NULL_TREE;
14892 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14893 bool saved_in_declarator_p = parser->in_declarator_p;
14894 bool first = true;
14895 tree pushed_scope = NULL_TREE;
14897 while (true)
14899 /* Peek at the next token. */
14900 token = cp_lexer_peek_token (parser->lexer);
14901 if (token->type == CPP_OPEN_PAREN)
14903 /* This is either a parameter-declaration-clause, or a
14904 parenthesized declarator. When we know we are parsing a
14905 named declarator, it must be a parenthesized declarator
14906 if FIRST is true. For instance, `(int)' is a
14907 parameter-declaration-clause, with an omitted
14908 direct-abstract-declarator. But `((*))', is a
14909 parenthesized abstract declarator. Finally, when T is a
14910 template parameter `(T)' is a
14911 parameter-declaration-clause, and not a parenthesized
14912 named declarator.
14914 We first try and parse a parameter-declaration-clause,
14915 and then try a nested declarator (if FIRST is true).
14917 It is not an error for it not to be a
14918 parameter-declaration-clause, even when FIRST is
14919 false. Consider,
14921 int i (int);
14922 int i (3);
14924 The first is the declaration of a function while the
14925 second is the definition of a variable, including its
14926 initializer.
14928 Having seen only the parenthesis, we cannot know which of
14929 these two alternatives should be selected. Even more
14930 complex are examples like:
14932 int i (int (a));
14933 int i (int (3));
14935 The former is a function-declaration; the latter is a
14936 variable initialization.
14938 Thus again, we try a parameter-declaration-clause, and if
14939 that fails, we back out and return. */
14941 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14943 tree params;
14944 unsigned saved_num_template_parameter_lists;
14945 bool is_declarator = false;
14946 tree t;
14948 /* In a member-declarator, the only valid interpretation
14949 of a parenthesis is the start of a
14950 parameter-declaration-clause. (It is invalid to
14951 initialize a static data member with a parenthesized
14952 initializer; only the "=" form of initialization is
14953 permitted.) */
14954 if (!member_p)
14955 cp_parser_parse_tentatively (parser);
14957 /* Consume the `('. */
14958 cp_lexer_consume_token (parser->lexer);
14959 if (first)
14961 /* If this is going to be an abstract declarator, we're
14962 in a declarator and we can't have default args. */
14963 parser->default_arg_ok_p = false;
14964 parser->in_declarator_p = true;
14967 /* Inside the function parameter list, surrounding
14968 template-parameter-lists do not apply. */
14969 saved_num_template_parameter_lists
14970 = parser->num_template_parameter_lists;
14971 parser->num_template_parameter_lists = 0;
14973 begin_scope (sk_function_parms, NULL_TREE);
14975 /* Parse the parameter-declaration-clause. */
14976 params = cp_parser_parameter_declaration_clause (parser);
14978 parser->num_template_parameter_lists
14979 = saved_num_template_parameter_lists;
14981 /* If all went well, parse the cv-qualifier-seq and the
14982 exception-specification. */
14983 if (member_p || cp_parser_parse_definitely (parser))
14985 cp_cv_quals cv_quals;
14986 tree exception_specification;
14987 tree late_return;
14989 is_declarator = true;
14991 if (ctor_dtor_or_conv_p)
14992 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14993 first = false;
14994 /* Consume the `)'. */
14995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14997 /* Parse the cv-qualifier-seq. */
14998 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14999 /* And the exception-specification. */
15000 exception_specification
15001 = cp_parser_exception_specification_opt (parser);
15003 late_return
15004 = cp_parser_late_return_type_opt (parser);
15006 /* Create the function-declarator. */
15007 declarator = make_call_declarator (declarator,
15008 params,
15009 cv_quals,
15010 exception_specification,
15011 late_return);
15012 /* Any subsequent parameter lists are to do with
15013 return type, so are not those of the declared
15014 function. */
15015 parser->default_arg_ok_p = false;
15018 /* Remove the function parms from scope. */
15019 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15020 pop_binding (DECL_NAME (t), t);
15021 leave_scope();
15023 if (is_declarator)
15024 /* Repeat the main loop. */
15025 continue;
15028 /* If this is the first, we can try a parenthesized
15029 declarator. */
15030 if (first)
15032 bool saved_in_type_id_in_expr_p;
15034 parser->default_arg_ok_p = saved_default_arg_ok_p;
15035 parser->in_declarator_p = saved_in_declarator_p;
15037 /* Consume the `('. */
15038 cp_lexer_consume_token (parser->lexer);
15039 /* Parse the nested declarator. */
15040 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15041 parser->in_type_id_in_expr_p = true;
15042 declarator
15043 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15044 /*parenthesized_p=*/NULL,
15045 member_p);
15046 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15047 first = false;
15048 /* Expect a `)'. */
15049 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15050 declarator = cp_error_declarator;
15051 if (declarator == cp_error_declarator)
15052 break;
15054 goto handle_declarator;
15056 /* Otherwise, we must be done. */
15057 else
15058 break;
15060 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15061 && token->type == CPP_OPEN_SQUARE)
15063 /* Parse an array-declarator. */
15064 tree bounds;
15066 if (ctor_dtor_or_conv_p)
15067 *ctor_dtor_or_conv_p = 0;
15069 first = false;
15070 parser->default_arg_ok_p = false;
15071 parser->in_declarator_p = true;
15072 /* Consume the `['. */
15073 cp_lexer_consume_token (parser->lexer);
15074 /* Peek at the next token. */
15075 token = cp_lexer_peek_token (parser->lexer);
15076 /* If the next token is `]', then there is no
15077 constant-expression. */
15078 if (token->type != CPP_CLOSE_SQUARE)
15080 bool non_constant_p;
15082 bounds
15083 = cp_parser_constant_expression (parser,
15084 /*allow_non_constant=*/true,
15085 &non_constant_p);
15086 if (!non_constant_p || cxx_dialect >= cxx0x)
15087 /* OK */;
15088 /* Normally, the array bound must be an integral constant
15089 expression. However, as an extension, we allow VLAs
15090 in function scopes as long as they aren't part of a
15091 parameter declaration. */
15092 else if (!parser->in_function_body
15093 || current_binding_level->kind == sk_function_parms)
15095 cp_parser_error (parser,
15096 "array bound is not an integer constant");
15097 bounds = error_mark_node;
15099 else if (processing_template_decl && !error_operand_p (bounds))
15101 /* Remember this wasn't a constant-expression. */
15102 bounds = build_nop (TREE_TYPE (bounds), bounds);
15103 TREE_SIDE_EFFECTS (bounds) = 1;
15106 else
15107 bounds = NULL_TREE;
15108 /* Look for the closing `]'. */
15109 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15111 declarator = cp_error_declarator;
15112 break;
15115 declarator = make_array_declarator (declarator, bounds);
15117 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15120 tree qualifying_scope;
15121 tree unqualified_name;
15122 special_function_kind sfk;
15123 bool abstract_ok;
15124 bool pack_expansion_p = false;
15125 cp_token *declarator_id_start_token;
15127 /* Parse a declarator-id */
15128 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15129 if (abstract_ok)
15131 cp_parser_parse_tentatively (parser);
15133 /* If we see an ellipsis, we should be looking at a
15134 parameter pack. */
15135 if (token->type == CPP_ELLIPSIS)
15137 /* Consume the `...' */
15138 cp_lexer_consume_token (parser->lexer);
15140 pack_expansion_p = true;
15144 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15145 unqualified_name
15146 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15147 qualifying_scope = parser->scope;
15148 if (abstract_ok)
15150 bool okay = false;
15152 if (!unqualified_name && pack_expansion_p)
15154 /* Check whether an error occurred. */
15155 okay = !cp_parser_error_occurred (parser);
15157 /* We already consumed the ellipsis to mark a
15158 parameter pack, but we have no way to report it,
15159 so abort the tentative parse. We will be exiting
15160 immediately anyway. */
15161 cp_parser_abort_tentative_parse (parser);
15163 else
15164 okay = cp_parser_parse_definitely (parser);
15166 if (!okay)
15167 unqualified_name = error_mark_node;
15168 else if (unqualified_name
15169 && (qualifying_scope
15170 || (TREE_CODE (unqualified_name)
15171 != IDENTIFIER_NODE)))
15173 cp_parser_error (parser, "expected unqualified-id");
15174 unqualified_name = error_mark_node;
15178 if (!unqualified_name)
15179 return NULL;
15180 if (unqualified_name == error_mark_node)
15182 declarator = cp_error_declarator;
15183 pack_expansion_p = false;
15184 declarator->parameter_pack_p = false;
15185 break;
15188 if (qualifying_scope && at_namespace_scope_p ()
15189 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15191 /* In the declaration of a member of a template class
15192 outside of the class itself, the SCOPE will sometimes
15193 be a TYPENAME_TYPE. For example, given:
15195 template <typename T>
15196 int S<T>::R::i = 3;
15198 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15199 this context, we must resolve S<T>::R to an ordinary
15200 type, rather than a typename type.
15202 The reason we normally avoid resolving TYPENAME_TYPEs
15203 is that a specialization of `S' might render
15204 `S<T>::R' not a type. However, if `S' is
15205 specialized, then this `i' will not be used, so there
15206 is no harm in resolving the types here. */
15207 tree type;
15209 /* Resolve the TYPENAME_TYPE. */
15210 type = resolve_typename_type (qualifying_scope,
15211 /*only_current_p=*/false);
15212 /* If that failed, the declarator is invalid. */
15213 if (TREE_CODE (type) == TYPENAME_TYPE)
15215 if (typedef_variant_p (type))
15216 error_at (declarator_id_start_token->location,
15217 "cannot define member of dependent typedef "
15218 "%qT", type);
15219 else
15220 error_at (declarator_id_start_token->location,
15221 "%<%T::%E%> is not a type",
15222 TYPE_CONTEXT (qualifying_scope),
15223 TYPE_IDENTIFIER (qualifying_scope));
15225 qualifying_scope = type;
15228 sfk = sfk_none;
15230 if (unqualified_name)
15232 tree class_type;
15234 if (qualifying_scope
15235 && CLASS_TYPE_P (qualifying_scope))
15236 class_type = qualifying_scope;
15237 else
15238 class_type = current_class_type;
15240 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15242 tree name_type = TREE_TYPE (unqualified_name);
15243 if (class_type && same_type_p (name_type, class_type))
15245 if (qualifying_scope
15246 && CLASSTYPE_USE_TEMPLATE (name_type))
15248 error_at (declarator_id_start_token->location,
15249 "invalid use of constructor as a template");
15250 inform (declarator_id_start_token->location,
15251 "use %<%T::%D%> instead of %<%T::%D%> to "
15252 "name the constructor in a qualified name",
15253 class_type,
15254 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15255 class_type, name_type);
15256 declarator = cp_error_declarator;
15257 break;
15259 else
15260 unqualified_name = constructor_name (class_type);
15262 else
15264 /* We do not attempt to print the declarator
15265 here because we do not have enough
15266 information about its original syntactic
15267 form. */
15268 cp_parser_error (parser, "invalid declarator");
15269 declarator = cp_error_declarator;
15270 break;
15274 if (class_type)
15276 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15277 sfk = sfk_destructor;
15278 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15279 sfk = sfk_conversion;
15280 else if (/* There's no way to declare a constructor
15281 for an anonymous type, even if the type
15282 got a name for linkage purposes. */
15283 !TYPE_WAS_ANONYMOUS (class_type)
15284 && constructor_name_p (unqualified_name,
15285 class_type))
15287 unqualified_name = constructor_name (class_type);
15288 sfk = sfk_constructor;
15290 else if (is_overloaded_fn (unqualified_name)
15291 && DECL_CONSTRUCTOR_P (get_first_fn
15292 (unqualified_name)))
15293 sfk = sfk_constructor;
15295 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15296 *ctor_dtor_or_conv_p = -1;
15299 declarator = make_id_declarator (qualifying_scope,
15300 unqualified_name,
15301 sfk);
15302 declarator->id_loc = token->location;
15303 declarator->parameter_pack_p = pack_expansion_p;
15305 if (pack_expansion_p)
15306 maybe_warn_variadic_templates ();
15309 handle_declarator:;
15310 scope = get_scope_of_declarator (declarator);
15311 if (scope)
15312 /* Any names that appear after the declarator-id for a
15313 member are looked up in the containing scope. */
15314 pushed_scope = push_scope (scope);
15315 parser->in_declarator_p = true;
15316 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15317 || (declarator && declarator->kind == cdk_id))
15318 /* Default args are only allowed on function
15319 declarations. */
15320 parser->default_arg_ok_p = saved_default_arg_ok_p;
15321 else
15322 parser->default_arg_ok_p = false;
15324 first = false;
15326 /* We're done. */
15327 else
15328 break;
15331 /* For an abstract declarator, we might wind up with nothing at this
15332 point. That's an error; the declarator is not optional. */
15333 if (!declarator)
15334 cp_parser_error (parser, "expected declarator");
15336 /* If we entered a scope, we must exit it now. */
15337 if (pushed_scope)
15338 pop_scope (pushed_scope);
15340 parser->default_arg_ok_p = saved_default_arg_ok_p;
15341 parser->in_declarator_p = saved_in_declarator_p;
15343 return declarator;
15346 /* Parse a ptr-operator.
15348 ptr-operator:
15349 * cv-qualifier-seq [opt]
15351 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15353 GNU Extension:
15355 ptr-operator:
15356 & cv-qualifier-seq [opt]
15358 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15359 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15360 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15361 filled in with the TYPE containing the member. *CV_QUALS is
15362 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15363 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15364 Note that the tree codes returned by this function have nothing
15365 to do with the types of trees that will be eventually be created
15366 to represent the pointer or reference type being parsed. They are
15367 just constants with suggestive names. */
15368 static enum tree_code
15369 cp_parser_ptr_operator (cp_parser* parser,
15370 tree* type,
15371 cp_cv_quals *cv_quals)
15373 enum tree_code code = ERROR_MARK;
15374 cp_token *token;
15376 /* Assume that it's not a pointer-to-member. */
15377 *type = NULL_TREE;
15378 /* And that there are no cv-qualifiers. */
15379 *cv_quals = TYPE_UNQUALIFIED;
15381 /* Peek at the next token. */
15382 token = cp_lexer_peek_token (parser->lexer);
15384 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15385 if (token->type == CPP_MULT)
15386 code = INDIRECT_REF;
15387 else if (token->type == CPP_AND)
15388 code = ADDR_EXPR;
15389 else if ((cxx_dialect != cxx98) &&
15390 token->type == CPP_AND_AND) /* C++0x only */
15391 code = NON_LVALUE_EXPR;
15393 if (code != ERROR_MARK)
15395 /* Consume the `*', `&' or `&&'. */
15396 cp_lexer_consume_token (parser->lexer);
15398 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15399 `&', if we are allowing GNU extensions. (The only qualifier
15400 that can legally appear after `&' is `restrict', but that is
15401 enforced during semantic analysis. */
15402 if (code == INDIRECT_REF
15403 || cp_parser_allow_gnu_extensions_p (parser))
15404 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15406 else
15408 /* Try the pointer-to-member case. */
15409 cp_parser_parse_tentatively (parser);
15410 /* Look for the optional `::' operator. */
15411 cp_parser_global_scope_opt (parser,
15412 /*current_scope_valid_p=*/false);
15413 /* Look for the nested-name specifier. */
15414 token = cp_lexer_peek_token (parser->lexer);
15415 cp_parser_nested_name_specifier (parser,
15416 /*typename_keyword_p=*/false,
15417 /*check_dependency_p=*/true,
15418 /*type_p=*/false,
15419 /*is_declaration=*/false);
15420 /* If we found it, and the next token is a `*', then we are
15421 indeed looking at a pointer-to-member operator. */
15422 if (!cp_parser_error_occurred (parser)
15423 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15425 /* Indicate that the `*' operator was used. */
15426 code = INDIRECT_REF;
15428 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15429 error_at (token->location, "%qD is a namespace", parser->scope);
15430 else
15432 /* The type of which the member is a member is given by the
15433 current SCOPE. */
15434 *type = parser->scope;
15435 /* The next name will not be qualified. */
15436 parser->scope = NULL_TREE;
15437 parser->qualifying_scope = NULL_TREE;
15438 parser->object_scope = NULL_TREE;
15439 /* Look for the optional cv-qualifier-seq. */
15440 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15443 /* If that didn't work we don't have a ptr-operator. */
15444 if (!cp_parser_parse_definitely (parser))
15445 cp_parser_error (parser, "expected ptr-operator");
15448 return code;
15451 /* Parse an (optional) cv-qualifier-seq.
15453 cv-qualifier-seq:
15454 cv-qualifier cv-qualifier-seq [opt]
15456 cv-qualifier:
15457 const
15458 volatile
15460 GNU Extension:
15462 cv-qualifier:
15463 __restrict__
15465 Returns a bitmask representing the cv-qualifiers. */
15467 static cp_cv_quals
15468 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15470 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15472 while (true)
15474 cp_token *token;
15475 cp_cv_quals cv_qualifier;
15477 /* Peek at the next token. */
15478 token = cp_lexer_peek_token (parser->lexer);
15479 /* See if it's a cv-qualifier. */
15480 switch (token->keyword)
15482 case RID_CONST:
15483 cv_qualifier = TYPE_QUAL_CONST;
15484 break;
15486 case RID_VOLATILE:
15487 cv_qualifier = TYPE_QUAL_VOLATILE;
15488 break;
15490 case RID_RESTRICT:
15491 cv_qualifier = TYPE_QUAL_RESTRICT;
15492 break;
15494 default:
15495 cv_qualifier = TYPE_UNQUALIFIED;
15496 break;
15499 if (!cv_qualifier)
15500 break;
15502 if (cv_quals & cv_qualifier)
15504 error_at (token->location, "duplicate cv-qualifier");
15505 cp_lexer_purge_token (parser->lexer);
15507 else
15509 cp_lexer_consume_token (parser->lexer);
15510 cv_quals |= cv_qualifier;
15514 return cv_quals;
15517 /* Parse a late-specified return type, if any. This is not a separate
15518 non-terminal, but part of a function declarator, which looks like
15520 -> trailing-type-specifier-seq abstract-declarator(opt)
15522 Returns the type indicated by the type-id. */
15524 static tree
15525 cp_parser_late_return_type_opt (cp_parser* parser)
15527 cp_token *token;
15529 /* Peek at the next token. */
15530 token = cp_lexer_peek_token (parser->lexer);
15531 /* A late-specified return type is indicated by an initial '->'. */
15532 if (token->type != CPP_DEREF)
15533 return NULL_TREE;
15535 /* Consume the ->. */
15536 cp_lexer_consume_token (parser->lexer);
15538 return cp_parser_trailing_type_id (parser);
15541 /* Parse a declarator-id.
15543 declarator-id:
15544 id-expression
15545 :: [opt] nested-name-specifier [opt] type-name
15547 In the `id-expression' case, the value returned is as for
15548 cp_parser_id_expression if the id-expression was an unqualified-id.
15549 If the id-expression was a qualified-id, then a SCOPE_REF is
15550 returned. The first operand is the scope (either a NAMESPACE_DECL
15551 or TREE_TYPE), but the second is still just a representation of an
15552 unqualified-id. */
15554 static tree
15555 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15557 tree id;
15558 /* The expression must be an id-expression. Assume that qualified
15559 names are the names of types so that:
15561 template <class T>
15562 int S<T>::R::i = 3;
15564 will work; we must treat `S<T>::R' as the name of a type.
15565 Similarly, assume that qualified names are templates, where
15566 required, so that:
15568 template <class T>
15569 int S<T>::R<T>::i = 3;
15571 will work, too. */
15572 id = cp_parser_id_expression (parser,
15573 /*template_keyword_p=*/false,
15574 /*check_dependency_p=*/false,
15575 /*template_p=*/NULL,
15576 /*declarator_p=*/true,
15577 optional_p);
15578 if (id && BASELINK_P (id))
15579 id = BASELINK_FUNCTIONS (id);
15580 return id;
15583 /* Parse a type-id.
15585 type-id:
15586 type-specifier-seq abstract-declarator [opt]
15588 Returns the TYPE specified. */
15590 static tree
15591 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15592 bool is_trailing_return)
15594 cp_decl_specifier_seq type_specifier_seq;
15595 cp_declarator *abstract_declarator;
15597 /* Parse the type-specifier-seq. */
15598 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15599 is_trailing_return,
15600 &type_specifier_seq);
15601 if (type_specifier_seq.type == error_mark_node)
15602 return error_mark_node;
15604 /* There might or might not be an abstract declarator. */
15605 cp_parser_parse_tentatively (parser);
15606 /* Look for the declarator. */
15607 abstract_declarator
15608 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15609 /*parenthesized_p=*/NULL,
15610 /*member_p=*/false);
15611 /* Check to see if there really was a declarator. */
15612 if (!cp_parser_parse_definitely (parser))
15613 abstract_declarator = NULL;
15615 if (type_specifier_seq.type
15616 && type_uses_auto (type_specifier_seq.type))
15618 /* A type-id with type 'auto' is only ok if the abstract declarator
15619 is a function declarator with a late-specified return type. */
15620 if (abstract_declarator
15621 && abstract_declarator->kind == cdk_function
15622 && abstract_declarator->u.function.late_return_type)
15623 /* OK */;
15624 else
15626 error ("invalid use of %<auto%>");
15627 return error_mark_node;
15631 return groktypename (&type_specifier_seq, abstract_declarator,
15632 is_template_arg);
15635 static tree cp_parser_type_id (cp_parser *parser)
15637 return cp_parser_type_id_1 (parser, false, false);
15640 static tree cp_parser_template_type_arg (cp_parser *parser)
15642 return cp_parser_type_id_1 (parser, true, false);
15645 static tree cp_parser_trailing_type_id (cp_parser *parser)
15647 return cp_parser_type_id_1 (parser, false, true);
15650 /* Parse a type-specifier-seq.
15652 type-specifier-seq:
15653 type-specifier type-specifier-seq [opt]
15655 GNU extension:
15657 type-specifier-seq:
15658 attributes type-specifier-seq [opt]
15660 If IS_DECLARATION is true, we are at the start of a "condition" or
15661 exception-declaration, so we might be followed by a declarator-id.
15663 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15664 i.e. we've just seen "->".
15666 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15668 static void
15669 cp_parser_type_specifier_seq (cp_parser* parser,
15670 bool is_declaration,
15671 bool is_trailing_return,
15672 cp_decl_specifier_seq *type_specifier_seq)
15674 bool seen_type_specifier = false;
15675 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15676 cp_token *start_token = NULL;
15678 /* Clear the TYPE_SPECIFIER_SEQ. */
15679 clear_decl_specs (type_specifier_seq);
15681 /* In the context of a trailing return type, enum E { } is an
15682 elaborated-type-specifier followed by a function-body, not an
15683 enum-specifier. */
15684 if (is_trailing_return)
15685 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15687 /* Parse the type-specifiers and attributes. */
15688 while (true)
15690 tree type_specifier;
15691 bool is_cv_qualifier;
15693 /* Check for attributes first. */
15694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15696 type_specifier_seq->attributes =
15697 chainon (type_specifier_seq->attributes,
15698 cp_parser_attributes_opt (parser));
15699 continue;
15702 /* record the token of the beginning of the type specifier seq,
15703 for error reporting purposes*/
15704 if (!start_token)
15705 start_token = cp_lexer_peek_token (parser->lexer);
15707 /* Look for the type-specifier. */
15708 type_specifier = cp_parser_type_specifier (parser,
15709 flags,
15710 type_specifier_seq,
15711 /*is_declaration=*/false,
15712 NULL,
15713 &is_cv_qualifier);
15714 if (!type_specifier)
15716 /* If the first type-specifier could not be found, this is not a
15717 type-specifier-seq at all. */
15718 if (!seen_type_specifier)
15720 cp_parser_error (parser, "expected type-specifier");
15721 type_specifier_seq->type = error_mark_node;
15722 return;
15724 /* If subsequent type-specifiers could not be found, the
15725 type-specifier-seq is complete. */
15726 break;
15729 seen_type_specifier = true;
15730 /* The standard says that a condition can be:
15732 type-specifier-seq declarator = assignment-expression
15734 However, given:
15736 struct S {};
15737 if (int S = ...)
15739 we should treat the "S" as a declarator, not as a
15740 type-specifier. The standard doesn't say that explicitly for
15741 type-specifier-seq, but it does say that for
15742 decl-specifier-seq in an ordinary declaration. Perhaps it
15743 would be clearer just to allow a decl-specifier-seq here, and
15744 then add a semantic restriction that if any decl-specifiers
15745 that are not type-specifiers appear, the program is invalid. */
15746 if (is_declaration && !is_cv_qualifier)
15747 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15750 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15753 /* Parse a parameter-declaration-clause.
15755 parameter-declaration-clause:
15756 parameter-declaration-list [opt] ... [opt]
15757 parameter-declaration-list , ...
15759 Returns a representation for the parameter declarations. A return
15760 value of NULL indicates a parameter-declaration-clause consisting
15761 only of an ellipsis. */
15763 static tree
15764 cp_parser_parameter_declaration_clause (cp_parser* parser)
15766 tree parameters;
15767 cp_token *token;
15768 bool ellipsis_p;
15769 bool is_error;
15771 /* Peek at the next token. */
15772 token = cp_lexer_peek_token (parser->lexer);
15773 /* Check for trivial parameter-declaration-clauses. */
15774 if (token->type == CPP_ELLIPSIS)
15776 /* Consume the `...' token. */
15777 cp_lexer_consume_token (parser->lexer);
15778 return NULL_TREE;
15780 else if (token->type == CPP_CLOSE_PAREN)
15781 /* There are no parameters. */
15783 #ifndef NO_IMPLICIT_EXTERN_C
15784 if (in_system_header && current_class_type == NULL
15785 && current_lang_name == lang_name_c)
15786 return NULL_TREE;
15787 else
15788 #endif
15789 return void_list_node;
15791 /* Check for `(void)', too, which is a special case. */
15792 else if (token->keyword == RID_VOID
15793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15794 == CPP_CLOSE_PAREN))
15796 /* Consume the `void' token. */
15797 cp_lexer_consume_token (parser->lexer);
15798 /* There are no parameters. */
15799 return void_list_node;
15802 /* Parse the parameter-declaration-list. */
15803 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15804 /* If a parse error occurred while parsing the
15805 parameter-declaration-list, then the entire
15806 parameter-declaration-clause is erroneous. */
15807 if (is_error)
15808 return NULL;
15810 /* Peek at the next token. */
15811 token = cp_lexer_peek_token (parser->lexer);
15812 /* If it's a `,', the clause should terminate with an ellipsis. */
15813 if (token->type == CPP_COMMA)
15815 /* Consume the `,'. */
15816 cp_lexer_consume_token (parser->lexer);
15817 /* Expect an ellipsis. */
15818 ellipsis_p
15819 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15821 /* It might also be `...' if the optional trailing `,' was
15822 omitted. */
15823 else if (token->type == CPP_ELLIPSIS)
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser->lexer);
15827 /* And remember that we saw it. */
15828 ellipsis_p = true;
15830 else
15831 ellipsis_p = false;
15833 /* Finish the parameter list. */
15834 if (!ellipsis_p)
15835 parameters = chainon (parameters, void_list_node);
15837 return parameters;
15840 /* Parse a parameter-declaration-list.
15842 parameter-declaration-list:
15843 parameter-declaration
15844 parameter-declaration-list , parameter-declaration
15846 Returns a representation of the parameter-declaration-list, as for
15847 cp_parser_parameter_declaration_clause. However, the
15848 `void_list_node' is never appended to the list. Upon return,
15849 *IS_ERROR will be true iff an error occurred. */
15851 static tree
15852 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15854 tree parameters = NULL_TREE;
15855 tree *tail = &parameters;
15856 bool saved_in_unbraced_linkage_specification_p;
15857 int index = 0;
15859 /* Assume all will go well. */
15860 *is_error = false;
15861 /* The special considerations that apply to a function within an
15862 unbraced linkage specifications do not apply to the parameters
15863 to the function. */
15864 saved_in_unbraced_linkage_specification_p
15865 = parser->in_unbraced_linkage_specification_p;
15866 parser->in_unbraced_linkage_specification_p = false;
15868 /* Look for more parameters. */
15869 while (true)
15871 cp_parameter_declarator *parameter;
15872 tree decl = error_mark_node;
15873 bool parenthesized_p;
15874 /* Parse the parameter. */
15875 parameter
15876 = cp_parser_parameter_declaration (parser,
15877 /*template_parm_p=*/false,
15878 &parenthesized_p);
15880 /* We don't know yet if the enclosing context is deprecated, so wait
15881 and warn in grokparms if appropriate. */
15882 deprecated_state = DEPRECATED_SUPPRESS;
15884 if (parameter)
15885 decl = grokdeclarator (parameter->declarator,
15886 &parameter->decl_specifiers,
15887 PARM,
15888 parameter->default_argument != NULL_TREE,
15889 &parameter->decl_specifiers.attributes);
15891 deprecated_state = DEPRECATED_NORMAL;
15893 /* If a parse error occurred parsing the parameter declaration,
15894 then the entire parameter-declaration-list is erroneous. */
15895 if (decl == error_mark_node)
15897 *is_error = true;
15898 parameters = error_mark_node;
15899 break;
15902 if (parameter->decl_specifiers.attributes)
15903 cplus_decl_attributes (&decl,
15904 parameter->decl_specifiers.attributes,
15906 if (DECL_NAME (decl))
15907 decl = pushdecl (decl);
15909 if (decl != error_mark_node)
15911 retrofit_lang_decl (decl);
15912 DECL_PARM_INDEX (decl) = ++index;
15915 /* Add the new parameter to the list. */
15916 *tail = build_tree_list (parameter->default_argument, decl);
15917 tail = &TREE_CHAIN (*tail);
15919 /* Peek at the next token. */
15920 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15921 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15922 /* These are for Objective-C++ */
15923 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15924 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15925 /* The parameter-declaration-list is complete. */
15926 break;
15927 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15929 cp_token *token;
15931 /* Peek at the next token. */
15932 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15933 /* If it's an ellipsis, then the list is complete. */
15934 if (token->type == CPP_ELLIPSIS)
15935 break;
15936 /* Otherwise, there must be more parameters. Consume the
15937 `,'. */
15938 cp_lexer_consume_token (parser->lexer);
15939 /* When parsing something like:
15941 int i(float f, double d)
15943 we can tell after seeing the declaration for "f" that we
15944 are not looking at an initialization of a variable "i",
15945 but rather at the declaration of a function "i".
15947 Due to the fact that the parsing of template arguments
15948 (as specified to a template-id) requires backtracking we
15949 cannot use this technique when inside a template argument
15950 list. */
15951 if (!parser->in_template_argument_list_p
15952 && !parser->in_type_id_in_expr_p
15953 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15954 /* However, a parameter-declaration of the form
15955 "foat(f)" (which is a valid declaration of a
15956 parameter "f") can also be interpreted as an
15957 expression (the conversion of "f" to "float"). */
15958 && !parenthesized_p)
15959 cp_parser_commit_to_tentative_parse (parser);
15961 else
15963 cp_parser_error (parser, "expected %<,%> or %<...%>");
15964 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15965 cp_parser_skip_to_closing_parenthesis (parser,
15966 /*recovering=*/true,
15967 /*or_comma=*/false,
15968 /*consume_paren=*/false);
15969 break;
15973 parser->in_unbraced_linkage_specification_p
15974 = saved_in_unbraced_linkage_specification_p;
15976 return parameters;
15979 /* Parse a parameter declaration.
15981 parameter-declaration:
15982 decl-specifier-seq ... [opt] declarator
15983 decl-specifier-seq declarator = assignment-expression
15984 decl-specifier-seq ... [opt] abstract-declarator [opt]
15985 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15987 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15988 declares a template parameter. (In that case, a non-nested `>'
15989 token encountered during the parsing of the assignment-expression
15990 is not interpreted as a greater-than operator.)
15992 Returns a representation of the parameter, or NULL if an error
15993 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15994 true iff the declarator is of the form "(p)". */
15996 static cp_parameter_declarator *
15997 cp_parser_parameter_declaration (cp_parser *parser,
15998 bool template_parm_p,
15999 bool *parenthesized_p)
16001 int declares_class_or_enum;
16002 cp_decl_specifier_seq decl_specifiers;
16003 cp_declarator *declarator;
16004 tree default_argument;
16005 cp_token *token = NULL, *declarator_token_start = NULL;
16006 const char *saved_message;
16008 /* In a template parameter, `>' is not an operator.
16010 [temp.param]
16012 When parsing a default template-argument for a non-type
16013 template-parameter, the first non-nested `>' is taken as the end
16014 of the template parameter-list rather than a greater-than
16015 operator. */
16017 /* Type definitions may not appear in parameter types. */
16018 saved_message = parser->type_definition_forbidden_message;
16019 parser->type_definition_forbidden_message
16020 = G_("types may not be defined in parameter types");
16022 /* Parse the declaration-specifiers. */
16023 cp_parser_decl_specifier_seq (parser,
16024 CP_PARSER_FLAGS_NONE,
16025 &decl_specifiers,
16026 &declares_class_or_enum);
16028 /* Complain about missing 'typename' or other invalid type names. */
16029 if (!decl_specifiers.any_type_specifiers_p)
16030 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16032 /* If an error occurred, there's no reason to attempt to parse the
16033 rest of the declaration. */
16034 if (cp_parser_error_occurred (parser))
16036 parser->type_definition_forbidden_message = saved_message;
16037 return NULL;
16040 /* Peek at the next token. */
16041 token = cp_lexer_peek_token (parser->lexer);
16043 /* If the next token is a `)', `,', `=', `>', or `...', then there
16044 is no declarator. However, when variadic templates are enabled,
16045 there may be a declarator following `...'. */
16046 if (token->type == CPP_CLOSE_PAREN
16047 || token->type == CPP_COMMA
16048 || token->type == CPP_EQ
16049 || token->type == CPP_GREATER)
16051 declarator = NULL;
16052 if (parenthesized_p)
16053 *parenthesized_p = false;
16055 /* Otherwise, there should be a declarator. */
16056 else
16058 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16059 parser->default_arg_ok_p = false;
16061 /* After seeing a decl-specifier-seq, if the next token is not a
16062 "(", there is no possibility that the code is a valid
16063 expression. Therefore, if parsing tentatively, we commit at
16064 this point. */
16065 if (!parser->in_template_argument_list_p
16066 /* In an expression context, having seen:
16068 (int((char ...
16070 we cannot be sure whether we are looking at a
16071 function-type (taking a "char" as a parameter) or a cast
16072 of some object of type "char" to "int". */
16073 && !parser->in_type_id_in_expr_p
16074 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16075 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16076 cp_parser_commit_to_tentative_parse (parser);
16077 /* Parse the declarator. */
16078 declarator_token_start = token;
16079 declarator = cp_parser_declarator (parser,
16080 CP_PARSER_DECLARATOR_EITHER,
16081 /*ctor_dtor_or_conv_p=*/NULL,
16082 parenthesized_p,
16083 /*member_p=*/false);
16084 parser->default_arg_ok_p = saved_default_arg_ok_p;
16085 /* After the declarator, allow more attributes. */
16086 decl_specifiers.attributes
16087 = chainon (decl_specifiers.attributes,
16088 cp_parser_attributes_opt (parser));
16091 /* If the next token is an ellipsis, and we have not seen a
16092 declarator name, and the type of the declarator contains parameter
16093 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16094 a parameter pack expansion expression. Otherwise, leave the
16095 ellipsis for a C-style variadic function. */
16096 token = cp_lexer_peek_token (parser->lexer);
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16099 tree type = decl_specifiers.type;
16101 if (type && DECL_P (type))
16102 type = TREE_TYPE (type);
16104 if (type
16105 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16106 && declarator_can_be_parameter_pack (declarator)
16107 && (!declarator || !declarator->parameter_pack_p)
16108 && uses_parameter_packs (type))
16110 /* Consume the `...'. */
16111 cp_lexer_consume_token (parser->lexer);
16112 maybe_warn_variadic_templates ();
16114 /* Build a pack expansion type */
16115 if (declarator)
16116 declarator->parameter_pack_p = true;
16117 else
16118 decl_specifiers.type = make_pack_expansion (type);
16122 /* The restriction on defining new types applies only to the type
16123 of the parameter, not to the default argument. */
16124 parser->type_definition_forbidden_message = saved_message;
16126 /* If the next token is `=', then process a default argument. */
16127 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16129 /* Consume the `='. */
16130 cp_lexer_consume_token (parser->lexer);
16132 /* If we are defining a class, then the tokens that make up the
16133 default argument must be saved and processed later. */
16134 if (!template_parm_p && at_class_scope_p ()
16135 && TYPE_BEING_DEFINED (current_class_type)
16136 && !LAMBDA_TYPE_P (current_class_type))
16138 unsigned depth = 0;
16139 int maybe_template_id = 0;
16140 cp_token *first_token;
16141 cp_token *token;
16143 /* Add tokens until we have processed the entire default
16144 argument. We add the range [first_token, token). */
16145 first_token = cp_lexer_peek_token (parser->lexer);
16146 while (true)
16148 bool done = false;
16150 /* Peek at the next token. */
16151 token = cp_lexer_peek_token (parser->lexer);
16152 /* What we do depends on what token we have. */
16153 switch (token->type)
16155 /* In valid code, a default argument must be
16156 immediately followed by a `,' `)', or `...'. */
16157 case CPP_COMMA:
16158 if (depth == 0 && maybe_template_id)
16160 /* If we've seen a '<', we might be in a
16161 template-argument-list. Until Core issue 325 is
16162 resolved, we don't know how this situation ought
16163 to be handled, so try to DTRT. We check whether
16164 what comes after the comma is a valid parameter
16165 declaration list. If it is, then the comma ends
16166 the default argument; otherwise the default
16167 argument continues. */
16168 bool error = false;
16169 tree t;
16171 /* Set ITALP so cp_parser_parameter_declaration_list
16172 doesn't decide to commit to this parse. */
16173 bool saved_italp = parser->in_template_argument_list_p;
16174 parser->in_template_argument_list_p = true;
16176 cp_parser_parse_tentatively (parser);
16177 cp_lexer_consume_token (parser->lexer);
16178 begin_scope (sk_function_parms, NULL_TREE);
16179 cp_parser_parameter_declaration_list (parser, &error);
16180 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16181 pop_binding (DECL_NAME (t), t);
16182 leave_scope ();
16183 if (!cp_parser_error_occurred (parser) && !error)
16184 done = true;
16185 cp_parser_abort_tentative_parse (parser);
16187 parser->in_template_argument_list_p = saved_italp;
16188 break;
16190 case CPP_CLOSE_PAREN:
16191 case CPP_ELLIPSIS:
16192 /* If we run into a non-nested `;', `}', or `]',
16193 then the code is invalid -- but the default
16194 argument is certainly over. */
16195 case CPP_SEMICOLON:
16196 case CPP_CLOSE_BRACE:
16197 case CPP_CLOSE_SQUARE:
16198 if (depth == 0)
16199 done = true;
16200 /* Update DEPTH, if necessary. */
16201 else if (token->type == CPP_CLOSE_PAREN
16202 || token->type == CPP_CLOSE_BRACE
16203 || token->type == CPP_CLOSE_SQUARE)
16204 --depth;
16205 break;
16207 case CPP_OPEN_PAREN:
16208 case CPP_OPEN_SQUARE:
16209 case CPP_OPEN_BRACE:
16210 ++depth;
16211 break;
16213 case CPP_LESS:
16214 if (depth == 0)
16215 /* This might be the comparison operator, or it might
16216 start a template argument list. */
16217 ++maybe_template_id;
16218 break;
16220 case CPP_RSHIFT:
16221 if (cxx_dialect == cxx98)
16222 break;
16223 /* Fall through for C++0x, which treats the `>>'
16224 operator like two `>' tokens in certain
16225 cases. */
16227 case CPP_GREATER:
16228 if (depth == 0)
16230 /* This might be an operator, or it might close a
16231 template argument list. But if a previous '<'
16232 started a template argument list, this will have
16233 closed it, so we can't be in one anymore. */
16234 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16235 if (maybe_template_id < 0)
16236 maybe_template_id = 0;
16238 break;
16240 /* If we run out of tokens, issue an error message. */
16241 case CPP_EOF:
16242 case CPP_PRAGMA_EOL:
16243 error_at (token->location, "file ends in default argument");
16244 done = true;
16245 break;
16247 case CPP_NAME:
16248 case CPP_SCOPE:
16249 /* In these cases, we should look for template-ids.
16250 For example, if the default argument is
16251 `X<int, double>()', we need to do name lookup to
16252 figure out whether or not `X' is a template; if
16253 so, the `,' does not end the default argument.
16255 That is not yet done. */
16256 break;
16258 default:
16259 break;
16262 /* If we've reached the end, stop. */
16263 if (done)
16264 break;
16266 /* Add the token to the token block. */
16267 token = cp_lexer_consume_token (parser->lexer);
16270 /* Create a DEFAULT_ARG to represent the unparsed default
16271 argument. */
16272 default_argument = make_node (DEFAULT_ARG);
16273 DEFARG_TOKENS (default_argument)
16274 = cp_token_cache_new (first_token, token);
16275 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16277 /* Outside of a class definition, we can just parse the
16278 assignment-expression. */
16279 else
16281 token = cp_lexer_peek_token (parser->lexer);
16282 default_argument
16283 = cp_parser_default_argument (parser, template_parm_p);
16286 if (!parser->default_arg_ok_p)
16288 if (flag_permissive)
16289 warning (0, "deprecated use of default argument for parameter of non-function");
16290 else
16292 error_at (token->location,
16293 "default arguments are only "
16294 "permitted for function parameters");
16295 default_argument = NULL_TREE;
16298 else if ((declarator && declarator->parameter_pack_p)
16299 || (decl_specifiers.type
16300 && PACK_EXPANSION_P (decl_specifiers.type)))
16302 /* Find the name of the parameter pack. */
16303 cp_declarator *id_declarator = declarator;
16304 while (id_declarator && id_declarator->kind != cdk_id)
16305 id_declarator = id_declarator->declarator;
16307 if (id_declarator && id_declarator->kind == cdk_id)
16308 error_at (declarator_token_start->location,
16309 template_parm_p
16310 ? "template parameter pack %qD"
16311 " cannot have a default argument"
16312 : "parameter pack %qD cannot have a default argument",
16313 id_declarator->u.id.unqualified_name);
16314 else
16315 error_at (declarator_token_start->location,
16316 template_parm_p
16317 ? "template parameter pack cannot have a default argument"
16318 : "parameter pack cannot have a default argument");
16320 default_argument = NULL_TREE;
16323 else
16324 default_argument = NULL_TREE;
16326 return make_parameter_declarator (&decl_specifiers,
16327 declarator,
16328 default_argument);
16331 /* Parse a default argument and return it.
16333 TEMPLATE_PARM_P is true if this is a default argument for a
16334 non-type template parameter. */
16335 static tree
16336 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16338 tree default_argument = NULL_TREE;
16339 bool saved_greater_than_is_operator_p;
16340 bool saved_local_variables_forbidden_p;
16342 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16343 set correctly. */
16344 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16345 parser->greater_than_is_operator_p = !template_parm_p;
16346 /* Local variable names (and the `this' keyword) may not
16347 appear in a default argument. */
16348 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16349 parser->local_variables_forbidden_p = true;
16350 /* Parse the assignment-expression. */
16351 if (template_parm_p)
16352 push_deferring_access_checks (dk_no_deferred);
16353 default_argument
16354 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16355 if (template_parm_p)
16356 pop_deferring_access_checks ();
16357 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16358 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16360 return default_argument;
16363 /* Parse a function-body.
16365 function-body:
16366 compound_statement */
16368 static void
16369 cp_parser_function_body (cp_parser *parser)
16371 cp_parser_compound_statement (parser, NULL, false);
16374 /* Parse a ctor-initializer-opt followed by a function-body. Return
16375 true if a ctor-initializer was present. */
16377 static bool
16378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16380 tree body, list;
16381 bool ctor_initializer_p;
16382 const bool check_body_p =
16383 DECL_CONSTRUCTOR_P (current_function_decl)
16384 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16385 tree last = NULL;
16387 /* Begin the function body. */
16388 body = begin_function_body ();
16389 /* Parse the optional ctor-initializer. */
16390 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16392 /* If we're parsing a constexpr constructor definition, we need
16393 to check that the constructor body is indeed empty. However,
16394 before we get to cp_parser_function_body lot of junk has been
16395 generated, so we can't just check that we have an empty block.
16396 Rather we take a snapshot of the outermost block, and check whether
16397 cp_parser_function_body changed its state. */
16398 if (check_body_p)
16400 list = body;
16401 if (TREE_CODE (list) == BIND_EXPR)
16402 list = BIND_EXPR_BODY (list);
16403 if (TREE_CODE (list) == STATEMENT_LIST
16404 && STATEMENT_LIST_TAIL (list) != NULL)
16405 last = STATEMENT_LIST_TAIL (list)->stmt;
16407 /* Parse the function-body. */
16408 cp_parser_function_body (parser);
16409 if (check_body_p)
16410 check_constexpr_ctor_body (last, list);
16411 /* Finish the function body. */
16412 finish_function_body (body);
16414 return ctor_initializer_p;
16417 /* Parse an initializer.
16419 initializer:
16420 = initializer-clause
16421 ( expression-list )
16423 Returns an expression representing the initializer. If no
16424 initializer is present, NULL_TREE is returned.
16426 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16427 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16428 set to TRUE if there is no initializer present. If there is an
16429 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16430 is set to true; otherwise it is set to false. */
16432 static tree
16433 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16434 bool* non_constant_p)
16436 cp_token *token;
16437 tree init;
16439 /* Peek at the next token. */
16440 token = cp_lexer_peek_token (parser->lexer);
16442 /* Let our caller know whether or not this initializer was
16443 parenthesized. */
16444 *is_direct_init = (token->type != CPP_EQ);
16445 /* Assume that the initializer is constant. */
16446 *non_constant_p = false;
16448 if (token->type == CPP_EQ)
16450 /* Consume the `='. */
16451 cp_lexer_consume_token (parser->lexer);
16452 /* Parse the initializer-clause. */
16453 init = cp_parser_initializer_clause (parser, non_constant_p);
16455 else if (token->type == CPP_OPEN_PAREN)
16457 VEC(tree,gc) *vec;
16458 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16459 /*cast_p=*/false,
16460 /*allow_expansion_p=*/true,
16461 non_constant_p);
16462 if (vec == NULL)
16463 return error_mark_node;
16464 init = build_tree_list_vec (vec);
16465 release_tree_vector (vec);
16467 else if (token->type == CPP_OPEN_BRACE)
16469 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16470 init = cp_parser_braced_list (parser, non_constant_p);
16471 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16473 else
16475 /* Anything else is an error. */
16476 cp_parser_error (parser, "expected initializer");
16477 init = error_mark_node;
16480 return init;
16483 /* Parse an initializer-clause.
16485 initializer-clause:
16486 assignment-expression
16487 braced-init-list
16489 Returns an expression representing the initializer.
16491 If the `assignment-expression' production is used the value
16492 returned is simply a representation for the expression.
16494 Otherwise, calls cp_parser_braced_list. */
16496 static tree
16497 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16499 tree initializer;
16501 /* Assume the expression is constant. */
16502 *non_constant_p = false;
16504 /* If it is not a `{', then we are looking at an
16505 assignment-expression. */
16506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16508 initializer
16509 = cp_parser_constant_expression (parser,
16510 /*allow_non_constant_p=*/true,
16511 non_constant_p);
16512 if (!*non_constant_p)
16514 /* We only want to fold if this is really a constant
16515 expression. FIXME Actually, we don't want to fold here, but in
16516 cp_finish_decl. */
16517 tree folded = fold_non_dependent_expr (initializer);
16518 folded = maybe_constant_value (folded);
16519 if (TREE_CONSTANT (folded))
16520 initializer = folded;
16523 else
16524 initializer = cp_parser_braced_list (parser, non_constant_p);
16526 return initializer;
16529 /* Parse a brace-enclosed initializer list.
16531 braced-init-list:
16532 { initializer-list , [opt] }
16535 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16536 the elements of the initializer-list (or NULL, if the last
16537 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16538 NULL_TREE. There is no way to detect whether or not the optional
16539 trailing `,' was provided. NON_CONSTANT_P is as for
16540 cp_parser_initializer. */
16542 static tree
16543 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16545 tree initializer;
16547 /* Consume the `{' token. */
16548 cp_lexer_consume_token (parser->lexer);
16549 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16550 initializer = make_node (CONSTRUCTOR);
16551 /* If it's not a `}', then there is a non-trivial initializer. */
16552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16554 /* Parse the initializer list. */
16555 CONSTRUCTOR_ELTS (initializer)
16556 = cp_parser_initializer_list (parser, non_constant_p);
16557 /* A trailing `,' token is allowed. */
16558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16559 cp_lexer_consume_token (parser->lexer);
16561 /* Now, there should be a trailing `}'. */
16562 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16563 TREE_TYPE (initializer) = init_list_type_node;
16564 return initializer;
16567 /* Parse an initializer-list.
16569 initializer-list:
16570 initializer-clause ... [opt]
16571 initializer-list , initializer-clause ... [opt]
16573 GNU Extension:
16575 initializer-list:
16576 identifier : initializer-clause
16577 initializer-list, identifier : initializer-clause
16579 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16580 for the initializer. If the INDEX of the elt is non-NULL, it is the
16581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16582 as for cp_parser_initializer. */
16584 static VEC(constructor_elt,gc) *
16585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16587 VEC(constructor_elt,gc) *v = NULL;
16589 /* Assume all of the expressions are constant. */
16590 *non_constant_p = false;
16592 /* Parse the rest of the list. */
16593 while (true)
16595 cp_token *token;
16596 tree identifier;
16597 tree initializer;
16598 bool clause_non_constant_p;
16600 /* If the next token is an identifier and the following one is a
16601 colon, we are looking at the GNU designated-initializer
16602 syntax. */
16603 if (cp_parser_allow_gnu_extensions_p (parser)
16604 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16605 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16607 /* Warn the user that they are using an extension. */
16608 pedwarn (input_location, OPT_pedantic,
16609 "ISO C++ does not allow designated initializers");
16610 /* Consume the identifier. */
16611 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16612 /* Consume the `:'. */
16613 cp_lexer_consume_token (parser->lexer);
16615 else
16616 identifier = NULL_TREE;
16618 /* Parse the initializer. */
16619 initializer = cp_parser_initializer_clause (parser,
16620 &clause_non_constant_p);
16621 /* If any clause is non-constant, so is the entire initializer. */
16622 if (clause_non_constant_p)
16623 *non_constant_p = true;
16625 /* If we have an ellipsis, this is an initializer pack
16626 expansion. */
16627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16629 /* Consume the `...'. */
16630 cp_lexer_consume_token (parser->lexer);
16632 /* Turn the initializer into an initializer expansion. */
16633 initializer = make_pack_expansion (initializer);
16636 /* Add it to the vector. */
16637 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16639 /* If the next token is not a comma, we have reached the end of
16640 the list. */
16641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16642 break;
16644 /* Peek at the next token. */
16645 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16646 /* If the next token is a `}', then we're still done. An
16647 initializer-clause can have a trailing `,' after the
16648 initializer-list and before the closing `}'. */
16649 if (token->type == CPP_CLOSE_BRACE)
16650 break;
16652 /* Consume the `,' token. */
16653 cp_lexer_consume_token (parser->lexer);
16656 return v;
16659 /* Classes [gram.class] */
16661 /* Parse a class-name.
16663 class-name:
16664 identifier
16665 template-id
16667 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16668 to indicate that names looked up in dependent types should be
16669 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16670 keyword has been used to indicate that the name that appears next
16671 is a template. TAG_TYPE indicates the explicit tag given before
16672 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16673 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16674 is the class being defined in a class-head.
16676 Returns the TYPE_DECL representing the class. */
16678 static tree
16679 cp_parser_class_name (cp_parser *parser,
16680 bool typename_keyword_p,
16681 bool template_keyword_p,
16682 enum tag_types tag_type,
16683 bool check_dependency_p,
16684 bool class_head_p,
16685 bool is_declaration)
16687 tree decl;
16688 tree scope;
16689 bool typename_p;
16690 cp_token *token;
16691 tree identifier = NULL_TREE;
16693 /* All class-names start with an identifier. */
16694 token = cp_lexer_peek_token (parser->lexer);
16695 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16697 cp_parser_error (parser, "expected class-name");
16698 return error_mark_node;
16701 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16702 to a template-id, so we save it here. */
16703 scope = parser->scope;
16704 if (scope == error_mark_node)
16705 return error_mark_node;
16707 /* Any name names a type if we're following the `typename' keyword
16708 in a qualified name where the enclosing scope is type-dependent. */
16709 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16710 && dependent_type_p (scope));
16711 /* Handle the common case (an identifier, but not a template-id)
16712 efficiently. */
16713 if (token->type == CPP_NAME
16714 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16716 cp_token *identifier_token;
16717 bool ambiguous_p;
16719 /* Look for the identifier. */
16720 identifier_token = cp_lexer_peek_token (parser->lexer);
16721 ambiguous_p = identifier_token->ambiguous_p;
16722 identifier = cp_parser_identifier (parser);
16723 /* If the next token isn't an identifier, we are certainly not
16724 looking at a class-name. */
16725 if (identifier == error_mark_node)
16726 decl = error_mark_node;
16727 /* If we know this is a type-name, there's no need to look it
16728 up. */
16729 else if (typename_p)
16730 decl = identifier;
16731 else
16733 tree ambiguous_decls;
16734 /* If we already know that this lookup is ambiguous, then
16735 we've already issued an error message; there's no reason
16736 to check again. */
16737 if (ambiguous_p)
16739 cp_parser_simulate_error (parser);
16740 return error_mark_node;
16742 /* If the next token is a `::', then the name must be a type
16743 name.
16745 [basic.lookup.qual]
16747 During the lookup for a name preceding the :: scope
16748 resolution operator, object, function, and enumerator
16749 names are ignored. */
16750 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16751 tag_type = typename_type;
16752 /* Look up the name. */
16753 decl = cp_parser_lookup_name (parser, identifier,
16754 tag_type,
16755 /*is_template=*/false,
16756 /*is_namespace=*/false,
16757 check_dependency_p,
16758 &ambiguous_decls,
16759 identifier_token->location);
16760 if (ambiguous_decls)
16762 if (cp_parser_parsing_tentatively (parser))
16763 cp_parser_simulate_error (parser);
16764 return error_mark_node;
16768 else
16770 /* Try a template-id. */
16771 decl = cp_parser_template_id (parser, template_keyword_p,
16772 check_dependency_p,
16773 is_declaration);
16774 if (decl == error_mark_node)
16775 return error_mark_node;
16778 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16780 /* If this is a typename, create a TYPENAME_TYPE. */
16781 if (typename_p && decl != error_mark_node)
16783 decl = make_typename_type (scope, decl, typename_type,
16784 /*complain=*/tf_error);
16785 if (decl != error_mark_node)
16786 decl = TYPE_NAME (decl);
16789 /* Check to see that it is really the name of a class. */
16790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16791 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16793 /* Situations like this:
16795 template <typename T> struct A {
16796 typename T::template X<int>::I i;
16799 are problematic. Is `T::template X<int>' a class-name? The
16800 standard does not seem to be definitive, but there is no other
16801 valid interpretation of the following `::'. Therefore, those
16802 names are considered class-names. */
16804 decl = make_typename_type (scope, decl, tag_type, tf_error);
16805 if (decl != error_mark_node)
16806 decl = TYPE_NAME (decl);
16808 else if (TREE_CODE (decl) != TYPE_DECL
16809 || TREE_TYPE (decl) == error_mark_node
16810 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16811 /* In Objective-C 2.0, a classname followed by '.' starts a
16812 dot-syntax expression, and it's not a type-name. */
16813 || (c_dialect_objc ()
16814 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16815 && objc_is_class_name (decl)))
16816 decl = error_mark_node;
16818 if (decl == error_mark_node)
16819 cp_parser_error (parser, "expected class-name");
16820 else if (identifier && !parser->scope)
16821 maybe_note_name_used_in_class (identifier, decl);
16823 return decl;
16826 /* Parse a class-specifier.
16828 class-specifier:
16829 class-head { member-specification [opt] }
16831 Returns the TREE_TYPE representing the class. */
16833 static tree
16834 cp_parser_class_specifier (cp_parser* parser)
16836 tree type;
16837 tree attributes = NULL_TREE;
16838 bool nested_name_specifier_p;
16839 unsigned saved_num_template_parameter_lists;
16840 bool saved_in_function_body;
16841 bool saved_in_unbraced_linkage_specification_p;
16842 tree old_scope = NULL_TREE;
16843 tree scope = NULL_TREE;
16844 tree bases;
16846 push_deferring_access_checks (dk_no_deferred);
16848 /* Parse the class-head. */
16849 type = cp_parser_class_head (parser,
16850 &nested_name_specifier_p,
16851 &attributes,
16852 &bases);
16853 /* If the class-head was a semantic disaster, skip the entire body
16854 of the class. */
16855 if (!type)
16857 cp_parser_skip_to_end_of_block_or_statement (parser);
16858 pop_deferring_access_checks ();
16859 return error_mark_node;
16862 /* Look for the `{'. */
16863 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16865 pop_deferring_access_checks ();
16866 return error_mark_node;
16869 /* Process the base classes. If they're invalid, skip the
16870 entire class body. */
16871 if (!xref_basetypes (type, bases))
16873 /* Consuming the closing brace yields better error messages
16874 later on. */
16875 if (cp_parser_skip_to_closing_brace (parser))
16876 cp_lexer_consume_token (parser->lexer);
16877 pop_deferring_access_checks ();
16878 return error_mark_node;
16881 /* Issue an error message if type-definitions are forbidden here. */
16882 cp_parser_check_type_definition (parser);
16883 /* Remember that we are defining one more class. */
16884 ++parser->num_classes_being_defined;
16885 /* Inside the class, surrounding template-parameter-lists do not
16886 apply. */
16887 saved_num_template_parameter_lists
16888 = parser->num_template_parameter_lists;
16889 parser->num_template_parameter_lists = 0;
16890 /* We are not in a function body. */
16891 saved_in_function_body = parser->in_function_body;
16892 parser->in_function_body = false;
16893 /* We are not immediately inside an extern "lang" block. */
16894 saved_in_unbraced_linkage_specification_p
16895 = parser->in_unbraced_linkage_specification_p;
16896 parser->in_unbraced_linkage_specification_p = false;
16898 /* Start the class. */
16899 if (nested_name_specifier_p)
16901 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16902 old_scope = push_inner_scope (scope);
16904 type = begin_class_definition (type, attributes);
16906 if (type == error_mark_node)
16907 /* If the type is erroneous, skip the entire body of the class. */
16908 cp_parser_skip_to_closing_brace (parser);
16909 else
16910 /* Parse the member-specification. */
16911 cp_parser_member_specification_opt (parser);
16913 /* Look for the trailing `}'. */
16914 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16915 /* Look for trailing attributes to apply to this class. */
16916 if (cp_parser_allow_gnu_extensions_p (parser))
16917 attributes = cp_parser_attributes_opt (parser);
16918 if (type != error_mark_node)
16919 type = finish_struct (type, attributes);
16920 if (nested_name_specifier_p)
16921 pop_inner_scope (old_scope, scope);
16923 /* We've finished a type definition. Check for the common syntax
16924 error of forgetting a semicolon after the definition. We need to
16925 be careful, as we can't just check for not-a-semicolon and be done
16926 with it; the user might have typed:
16928 class X { } c = ...;
16929 class X { } *p = ...;
16931 and so forth. Instead, enumerate all the possible tokens that
16932 might follow this production; if we don't see one of them, then
16933 complain and silently insert the semicolon. */
16935 cp_token *token = cp_lexer_peek_token (parser->lexer);
16936 bool want_semicolon = true;
16938 switch (token->type)
16940 case CPP_NAME:
16941 case CPP_SEMICOLON:
16942 case CPP_MULT:
16943 case CPP_AND:
16944 case CPP_OPEN_PAREN:
16945 case CPP_CLOSE_PAREN:
16946 case CPP_COMMA:
16947 want_semicolon = false;
16948 break;
16950 /* While it's legal for type qualifiers and storage class
16951 specifiers to follow type definitions in the grammar, only
16952 compiler testsuites contain code like that. Assume that if
16953 we see such code, then what we're really seeing is a case
16954 like:
16956 class X { }
16957 const <type> var = ...;
16961 class Y { }
16962 static <type> func (...) ...
16964 i.e. the qualifier or specifier applies to the next
16965 declaration. To do so, however, we need to look ahead one
16966 more token to see if *that* token is a type specifier.
16968 This code could be improved to handle:
16970 class Z { }
16971 static const <type> var = ...; */
16972 case CPP_KEYWORD:
16973 if (keyword_is_storage_class_specifier (token->keyword)
16974 || keyword_is_type_qualifier (token->keyword))
16976 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16978 if (lookahead->type == CPP_KEYWORD
16979 && !keyword_begins_type_specifier (lookahead->keyword))
16980 want_semicolon = false;
16981 else if (lookahead->type == CPP_NAME)
16982 /* Handling user-defined types here would be nice, but
16983 very tricky. */
16984 want_semicolon = false;
16986 break;
16987 default:
16988 break;
16991 /* If we don't have a type, then something is very wrong and we
16992 shouldn't try to do anything clever. */
16993 if (TYPE_P (type) && want_semicolon)
16995 cp_token_position prev
16996 = cp_lexer_previous_token_position (parser->lexer);
16997 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16998 location_t loc = prev_token->location;
17000 if (CLASSTYPE_DECLARED_CLASS (type))
17001 error_at (loc, "expected %<;%> after class definition");
17002 else if (TREE_CODE (type) == RECORD_TYPE)
17003 error_at (loc, "expected %<;%> after struct definition");
17004 else if (TREE_CODE (type) == UNION_TYPE)
17005 error_at (loc, "expected %<;%> after union definition");
17006 else
17007 gcc_unreachable ();
17009 /* Unget one token and smash it to look as though we encountered
17010 a semicolon in the input stream. */
17011 cp_lexer_set_token_position (parser->lexer, prev);
17012 token = cp_lexer_peek_token (parser->lexer);
17013 token->type = CPP_SEMICOLON;
17014 token->keyword = RID_MAX;
17018 /* If this class is not itself within the scope of another class,
17019 then we need to parse the bodies of all of the queued function
17020 definitions. Note that the queued functions defined in a class
17021 are not always processed immediately following the
17022 class-specifier for that class. Consider:
17024 struct A {
17025 struct B { void f() { sizeof (A); } };
17028 If `f' were processed before the processing of `A' were
17029 completed, there would be no way to compute the size of `A'.
17030 Note that the nesting we are interested in here is lexical --
17031 not the semantic nesting given by TYPE_CONTEXT. In particular,
17032 for:
17034 struct A { struct B; };
17035 struct A::B { void f() { } };
17037 there is no need to delay the parsing of `A::B::f'. */
17038 if (--parser->num_classes_being_defined == 0)
17040 tree fn;
17041 tree class_type = NULL_TREE;
17042 tree pushed_scope = NULL_TREE;
17043 unsigned ix;
17044 cp_default_arg_entry *e;
17046 /* In a first pass, parse default arguments to the functions.
17047 Then, in a second pass, parse the bodies of the functions.
17048 This two-phased approach handles cases like:
17050 struct S {
17051 void f() { g(); }
17052 void g(int i = 3);
17056 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17057 ix, e)
17059 fn = e->decl;
17060 /* If there are default arguments that have not yet been processed,
17061 take care of them now. */
17062 if (class_type != e->class_type)
17064 if (pushed_scope)
17065 pop_scope (pushed_scope);
17066 class_type = e->class_type;
17067 pushed_scope = push_scope (class_type);
17069 /* Make sure that any template parameters are in scope. */
17070 maybe_begin_member_template_processing (fn);
17071 /* Parse the default argument expressions. */
17072 cp_parser_late_parsing_default_args (parser, fn);
17073 /* Remove any template parameters from the symbol table. */
17074 maybe_end_member_template_processing ();
17076 if (pushed_scope)
17077 pop_scope (pushed_scope);
17078 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17079 /* Now parse the body of the functions. */
17080 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17081 cp_parser_late_parsing_for_member (parser, fn);
17082 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17085 /* Put back any saved access checks. */
17086 pop_deferring_access_checks ();
17088 /* Restore saved state. */
17089 parser->in_function_body = saved_in_function_body;
17090 parser->num_template_parameter_lists
17091 = saved_num_template_parameter_lists;
17092 parser->in_unbraced_linkage_specification_p
17093 = saved_in_unbraced_linkage_specification_p;
17095 return type;
17098 /* Parse a class-head.
17100 class-head:
17101 class-key identifier [opt] base-clause [opt]
17102 class-key nested-name-specifier identifier base-clause [opt]
17103 class-key nested-name-specifier [opt] template-id
17104 base-clause [opt]
17106 GNU Extensions:
17107 class-key attributes identifier [opt] base-clause [opt]
17108 class-key attributes nested-name-specifier identifier base-clause [opt]
17109 class-key attributes nested-name-specifier [opt] template-id
17110 base-clause [opt]
17112 Upon return BASES is initialized to the list of base classes (or
17113 NULL, if there are none) in the same form returned by
17114 cp_parser_base_clause.
17116 Returns the TYPE of the indicated class. Sets
17117 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17118 involving a nested-name-specifier was used, and FALSE otherwise.
17120 Returns error_mark_node if this is not a class-head.
17122 Returns NULL_TREE if the class-head is syntactically valid, but
17123 semantically invalid in a way that means we should skip the entire
17124 body of the class. */
17126 static tree
17127 cp_parser_class_head (cp_parser* parser,
17128 bool* nested_name_specifier_p,
17129 tree *attributes_p,
17130 tree *bases)
17132 tree nested_name_specifier;
17133 enum tag_types class_key;
17134 tree id = NULL_TREE;
17135 tree type = NULL_TREE;
17136 tree attributes;
17137 bool template_id_p = false;
17138 bool qualified_p = false;
17139 bool invalid_nested_name_p = false;
17140 bool invalid_explicit_specialization_p = false;
17141 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17142 tree pushed_scope = NULL_TREE;
17143 unsigned num_templates;
17144 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17145 /* Assume no nested-name-specifier will be present. */
17146 *nested_name_specifier_p = false;
17147 /* Assume no template parameter lists will be used in defining the
17148 type. */
17149 num_templates = 0;
17150 parser->colon_corrects_to_scope_p = false;
17152 *bases = NULL_TREE;
17154 /* Look for the class-key. */
17155 class_key = cp_parser_class_key (parser);
17156 if (class_key == none_type)
17157 return error_mark_node;
17159 /* Parse the attributes. */
17160 attributes = cp_parser_attributes_opt (parser);
17162 /* If the next token is `::', that is invalid -- but sometimes
17163 people do try to write:
17165 struct ::S {};
17167 Handle this gracefully by accepting the extra qualifier, and then
17168 issuing an error about it later if this really is a
17169 class-head. If it turns out just to be an elaborated type
17170 specifier, remain silent. */
17171 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17172 qualified_p = true;
17174 push_deferring_access_checks (dk_no_check);
17176 /* Determine the name of the class. Begin by looking for an
17177 optional nested-name-specifier. */
17178 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17179 nested_name_specifier
17180 = cp_parser_nested_name_specifier_opt (parser,
17181 /*typename_keyword_p=*/false,
17182 /*check_dependency_p=*/false,
17183 /*type_p=*/false,
17184 /*is_declaration=*/false);
17185 /* If there was a nested-name-specifier, then there *must* be an
17186 identifier. */
17187 if (nested_name_specifier)
17189 type_start_token = cp_lexer_peek_token (parser->lexer);
17190 /* Although the grammar says `identifier', it really means
17191 `class-name' or `template-name'. You are only allowed to
17192 define a class that has already been declared with this
17193 syntax.
17195 The proposed resolution for Core Issue 180 says that wherever
17196 you see `class T::X' you should treat `X' as a type-name.
17198 It is OK to define an inaccessible class; for example:
17200 class A { class B; };
17201 class A::B {};
17203 We do not know if we will see a class-name, or a
17204 template-name. We look for a class-name first, in case the
17205 class-name is a template-id; if we looked for the
17206 template-name first we would stop after the template-name. */
17207 cp_parser_parse_tentatively (parser);
17208 type = cp_parser_class_name (parser,
17209 /*typename_keyword_p=*/false,
17210 /*template_keyword_p=*/false,
17211 class_type,
17212 /*check_dependency_p=*/false,
17213 /*class_head_p=*/true,
17214 /*is_declaration=*/false);
17215 /* If that didn't work, ignore the nested-name-specifier. */
17216 if (!cp_parser_parse_definitely (parser))
17218 invalid_nested_name_p = true;
17219 type_start_token = cp_lexer_peek_token (parser->lexer);
17220 id = cp_parser_identifier (parser);
17221 if (id == error_mark_node)
17222 id = NULL_TREE;
17224 /* If we could not find a corresponding TYPE, treat this
17225 declaration like an unqualified declaration. */
17226 if (type == error_mark_node)
17227 nested_name_specifier = NULL_TREE;
17228 /* Otherwise, count the number of templates used in TYPE and its
17229 containing scopes. */
17230 else
17232 tree scope;
17234 for (scope = TREE_TYPE (type);
17235 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17236 scope = (TYPE_P (scope)
17237 ? TYPE_CONTEXT (scope)
17238 : DECL_CONTEXT (scope)))
17239 if (TYPE_P (scope)
17240 && CLASS_TYPE_P (scope)
17241 && CLASSTYPE_TEMPLATE_INFO (scope)
17242 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17243 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17244 ++num_templates;
17247 /* Otherwise, the identifier is optional. */
17248 else
17250 /* We don't know whether what comes next is a template-id,
17251 an identifier, or nothing at all. */
17252 cp_parser_parse_tentatively (parser);
17253 /* Check for a template-id. */
17254 type_start_token = cp_lexer_peek_token (parser->lexer);
17255 id = cp_parser_template_id (parser,
17256 /*template_keyword_p=*/false,
17257 /*check_dependency_p=*/true,
17258 /*is_declaration=*/true);
17259 /* If that didn't work, it could still be an identifier. */
17260 if (!cp_parser_parse_definitely (parser))
17262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17264 type_start_token = cp_lexer_peek_token (parser->lexer);
17265 id = cp_parser_identifier (parser);
17267 else
17268 id = NULL_TREE;
17270 else
17272 template_id_p = true;
17273 ++num_templates;
17277 pop_deferring_access_checks ();
17279 if (id)
17280 cp_parser_check_for_invalid_template_id (parser, id,
17281 type_start_token->location);
17283 /* If it's not a `:' or a `{' then we can't really be looking at a
17284 class-head, since a class-head only appears as part of a
17285 class-specifier. We have to detect this situation before calling
17286 xref_tag, since that has irreversible side-effects. */
17287 if (!cp_parser_next_token_starts_class_definition_p (parser))
17289 cp_parser_error (parser, "expected %<{%> or %<:%>");
17290 type = error_mark_node;
17291 goto out;
17294 /* At this point, we're going ahead with the class-specifier, even
17295 if some other problem occurs. */
17296 cp_parser_commit_to_tentative_parse (parser);
17297 /* Issue the error about the overly-qualified name now. */
17298 if (qualified_p)
17300 cp_parser_error (parser,
17301 "global qualification of class name is invalid");
17302 type = error_mark_node;
17303 goto out;
17305 else if (invalid_nested_name_p)
17307 cp_parser_error (parser,
17308 "qualified name does not name a class");
17309 type = error_mark_node;
17310 goto out;
17312 else if (nested_name_specifier)
17314 tree scope;
17316 /* Reject typedef-names in class heads. */
17317 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17319 error_at (type_start_token->location,
17320 "invalid class name in declaration of %qD",
17321 type);
17322 type = NULL_TREE;
17323 goto done;
17326 /* Figure out in what scope the declaration is being placed. */
17327 scope = current_scope ();
17328 /* If that scope does not contain the scope in which the
17329 class was originally declared, the program is invalid. */
17330 if (scope && !is_ancestor (scope, nested_name_specifier))
17332 if (at_namespace_scope_p ())
17333 error_at (type_start_token->location,
17334 "declaration of %qD in namespace %qD which does not "
17335 "enclose %qD",
17336 type, scope, nested_name_specifier);
17337 else
17338 error_at (type_start_token->location,
17339 "declaration of %qD in %qD which does not enclose %qD",
17340 type, scope, nested_name_specifier);
17341 type = NULL_TREE;
17342 goto done;
17344 /* [dcl.meaning]
17346 A declarator-id shall not be qualified except for the
17347 definition of a ... nested class outside of its class
17348 ... [or] the definition or explicit instantiation of a
17349 class member of a namespace outside of its namespace. */
17350 if (scope == nested_name_specifier)
17352 permerror (nested_name_specifier_token_start->location,
17353 "extra qualification not allowed");
17354 nested_name_specifier = NULL_TREE;
17355 num_templates = 0;
17358 /* An explicit-specialization must be preceded by "template <>". If
17359 it is not, try to recover gracefully. */
17360 if (at_namespace_scope_p ()
17361 && parser->num_template_parameter_lists == 0
17362 && template_id_p)
17364 error_at (type_start_token->location,
17365 "an explicit specialization must be preceded by %<template <>%>");
17366 invalid_explicit_specialization_p = true;
17367 /* Take the same action that would have been taken by
17368 cp_parser_explicit_specialization. */
17369 ++parser->num_template_parameter_lists;
17370 begin_specialization ();
17372 /* There must be no "return" statements between this point and the
17373 end of this function; set "type "to the correct return value and
17374 use "goto done;" to return. */
17375 /* Make sure that the right number of template parameters were
17376 present. */
17377 if (!cp_parser_check_template_parameters (parser, num_templates,
17378 type_start_token->location,
17379 /*declarator=*/NULL))
17381 /* If something went wrong, there is no point in even trying to
17382 process the class-definition. */
17383 type = NULL_TREE;
17384 goto done;
17387 /* Look up the type. */
17388 if (template_id_p)
17390 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17391 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17392 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17394 error_at (type_start_token->location,
17395 "function template %qD redeclared as a class template", id);
17396 type = error_mark_node;
17398 else
17400 type = TREE_TYPE (id);
17401 type = maybe_process_partial_specialization (type);
17403 if (nested_name_specifier)
17404 pushed_scope = push_scope (nested_name_specifier);
17406 else if (nested_name_specifier)
17408 tree class_type;
17410 /* Given:
17412 template <typename T> struct S { struct T };
17413 template <typename T> struct S<T>::T { };
17415 we will get a TYPENAME_TYPE when processing the definition of
17416 `S::T'. We need to resolve it to the actual type before we
17417 try to define it. */
17418 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17420 class_type = resolve_typename_type (TREE_TYPE (type),
17421 /*only_current_p=*/false);
17422 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17423 type = TYPE_NAME (class_type);
17424 else
17426 cp_parser_error (parser, "could not resolve typename type");
17427 type = error_mark_node;
17431 if (maybe_process_partial_specialization (TREE_TYPE (type))
17432 == error_mark_node)
17434 type = NULL_TREE;
17435 goto done;
17438 class_type = current_class_type;
17439 /* Enter the scope indicated by the nested-name-specifier. */
17440 pushed_scope = push_scope (nested_name_specifier);
17441 /* Get the canonical version of this type. */
17442 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17443 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17444 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17446 type = push_template_decl (type);
17447 if (type == error_mark_node)
17449 type = NULL_TREE;
17450 goto done;
17454 type = TREE_TYPE (type);
17455 *nested_name_specifier_p = true;
17457 else /* The name is not a nested name. */
17459 /* If the class was unnamed, create a dummy name. */
17460 if (!id)
17461 id = make_anon_name ();
17462 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17463 parser->num_template_parameter_lists);
17466 /* Indicate whether this class was declared as a `class' or as a
17467 `struct'. */
17468 if (TREE_CODE (type) == RECORD_TYPE)
17469 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17470 cp_parser_check_class_key (class_key, type);
17472 /* If this type was already complete, and we see another definition,
17473 that's an error. */
17474 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17476 error_at (type_start_token->location, "redefinition of %q#T",
17477 type);
17478 error_at (type_start_token->location, "previous definition of %q+#T",
17479 type);
17480 type = NULL_TREE;
17481 goto done;
17483 else if (type == error_mark_node)
17484 type = NULL_TREE;
17486 /* We will have entered the scope containing the class; the names of
17487 base classes should be looked up in that context. For example:
17489 struct A { struct B {}; struct C; };
17490 struct A::C : B {};
17492 is valid. */
17494 /* Get the list of base-classes, if there is one. */
17495 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17496 *bases = cp_parser_base_clause (parser);
17498 done:
17499 /* Leave the scope given by the nested-name-specifier. We will
17500 enter the class scope itself while processing the members. */
17501 if (pushed_scope)
17502 pop_scope (pushed_scope);
17504 if (invalid_explicit_specialization_p)
17506 end_specialization ();
17507 --parser->num_template_parameter_lists;
17510 if (type)
17511 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17512 *attributes_p = attributes;
17513 out:
17514 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17515 return type;
17518 /* Parse a class-key.
17520 class-key:
17521 class
17522 struct
17523 union
17525 Returns the kind of class-key specified, or none_type to indicate
17526 error. */
17528 static enum tag_types
17529 cp_parser_class_key (cp_parser* parser)
17531 cp_token *token;
17532 enum tag_types tag_type;
17534 /* Look for the class-key. */
17535 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17536 if (!token)
17537 return none_type;
17539 /* Check to see if the TOKEN is a class-key. */
17540 tag_type = cp_parser_token_is_class_key (token);
17541 if (!tag_type)
17542 cp_parser_error (parser, "expected class-key");
17543 return tag_type;
17546 /* Parse an (optional) member-specification.
17548 member-specification:
17549 member-declaration member-specification [opt]
17550 access-specifier : member-specification [opt] */
17552 static void
17553 cp_parser_member_specification_opt (cp_parser* parser)
17555 while (true)
17557 cp_token *token;
17558 enum rid keyword;
17560 /* Peek at the next token. */
17561 token = cp_lexer_peek_token (parser->lexer);
17562 /* If it's a `}', or EOF then we've seen all the members. */
17563 if (token->type == CPP_CLOSE_BRACE
17564 || token->type == CPP_EOF
17565 || token->type == CPP_PRAGMA_EOL)
17566 break;
17568 /* See if this token is a keyword. */
17569 keyword = token->keyword;
17570 switch (keyword)
17572 case RID_PUBLIC:
17573 case RID_PROTECTED:
17574 case RID_PRIVATE:
17575 /* Consume the access-specifier. */
17576 cp_lexer_consume_token (parser->lexer);
17577 /* Remember which access-specifier is active. */
17578 current_access_specifier = token->u.value;
17579 /* Look for the `:'. */
17580 cp_parser_require (parser, CPP_COLON, RT_COLON);
17581 break;
17583 default:
17584 /* Accept #pragmas at class scope. */
17585 if (token->type == CPP_PRAGMA)
17587 cp_parser_pragma (parser, pragma_external);
17588 break;
17591 /* Otherwise, the next construction must be a
17592 member-declaration. */
17593 cp_parser_member_declaration (parser);
17598 /* Parse a member-declaration.
17600 member-declaration:
17601 decl-specifier-seq [opt] member-declarator-list [opt] ;
17602 function-definition ; [opt]
17603 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17604 using-declaration
17605 template-declaration
17607 member-declarator-list:
17608 member-declarator
17609 member-declarator-list , member-declarator
17611 member-declarator:
17612 declarator pure-specifier [opt]
17613 declarator constant-initializer [opt]
17614 identifier [opt] : constant-expression
17616 GNU Extensions:
17618 member-declaration:
17619 __extension__ member-declaration
17621 member-declarator:
17622 declarator attributes [opt] pure-specifier [opt]
17623 declarator attributes [opt] constant-initializer [opt]
17624 identifier [opt] attributes [opt] : constant-expression
17626 C++0x Extensions:
17628 member-declaration:
17629 static_assert-declaration */
17631 static void
17632 cp_parser_member_declaration (cp_parser* parser)
17634 cp_decl_specifier_seq decl_specifiers;
17635 tree prefix_attributes;
17636 tree decl;
17637 int declares_class_or_enum;
17638 bool friend_p;
17639 cp_token *token = NULL;
17640 cp_token *decl_spec_token_start = NULL;
17641 cp_token *initializer_token_start = NULL;
17642 int saved_pedantic;
17643 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17645 /* Check for the `__extension__' keyword. */
17646 if (cp_parser_extension_opt (parser, &saved_pedantic))
17648 /* Recurse. */
17649 cp_parser_member_declaration (parser);
17650 /* Restore the old value of the PEDANTIC flag. */
17651 pedantic = saved_pedantic;
17653 return;
17656 /* Check for a template-declaration. */
17657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17659 /* An explicit specialization here is an error condition, and we
17660 expect the specialization handler to detect and report this. */
17661 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17662 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17663 cp_parser_explicit_specialization (parser);
17664 else
17665 cp_parser_template_declaration (parser, /*member_p=*/true);
17667 return;
17670 /* Check for a using-declaration. */
17671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17673 /* Parse the using-declaration. */
17674 cp_parser_using_declaration (parser,
17675 /*access_declaration_p=*/false);
17676 return;
17679 /* Check for @defs. */
17680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17682 tree ivar, member;
17683 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17684 ivar = ivar_chains;
17685 while (ivar)
17687 member = ivar;
17688 ivar = TREE_CHAIN (member);
17689 TREE_CHAIN (member) = NULL_TREE;
17690 finish_member_declaration (member);
17692 return;
17695 /* If the next token is `static_assert' we have a static assertion. */
17696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17698 cp_parser_static_assert (parser, /*member_p=*/true);
17699 return;
17702 parser->colon_corrects_to_scope_p = false;
17704 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17705 goto out;
17707 /* Parse the decl-specifier-seq. */
17708 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17709 cp_parser_decl_specifier_seq (parser,
17710 CP_PARSER_FLAGS_OPTIONAL,
17711 &decl_specifiers,
17712 &declares_class_or_enum);
17713 prefix_attributes = decl_specifiers.attributes;
17714 decl_specifiers.attributes = NULL_TREE;
17715 /* Check for an invalid type-name. */
17716 if (!decl_specifiers.any_type_specifiers_p
17717 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17718 goto out;
17719 /* If there is no declarator, then the decl-specifier-seq should
17720 specify a type. */
17721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17723 /* If there was no decl-specifier-seq, and the next token is a
17724 `;', then we have something like:
17726 struct S { ; };
17728 [class.mem]
17730 Each member-declaration shall declare at least one member
17731 name of the class. */
17732 if (!decl_specifiers.any_specifiers_p)
17734 cp_token *token = cp_lexer_peek_token (parser->lexer);
17735 if (!in_system_header_at (token->location))
17736 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17738 else
17740 tree type;
17742 /* See if this declaration is a friend. */
17743 friend_p = cp_parser_friend_p (&decl_specifiers);
17744 /* If there were decl-specifiers, check to see if there was
17745 a class-declaration. */
17746 type = check_tag_decl (&decl_specifiers);
17747 /* Nested classes have already been added to the class, but
17748 a `friend' needs to be explicitly registered. */
17749 if (friend_p)
17751 /* If the `friend' keyword was present, the friend must
17752 be introduced with a class-key. */
17753 if (!declares_class_or_enum)
17754 error_at (decl_spec_token_start->location,
17755 "a class-key must be used when declaring a friend");
17756 /* In this case:
17758 template <typename T> struct A {
17759 friend struct A<T>::B;
17762 A<T>::B will be represented by a TYPENAME_TYPE, and
17763 therefore not recognized by check_tag_decl. */
17764 if (!type
17765 && decl_specifiers.type
17766 && TYPE_P (decl_specifiers.type))
17767 type = decl_specifiers.type;
17768 if (!type || !TYPE_P (type))
17769 error_at (decl_spec_token_start->location,
17770 "friend declaration does not name a class or "
17771 "function");
17772 else
17773 make_friend_class (current_class_type, type,
17774 /*complain=*/true);
17776 /* If there is no TYPE, an error message will already have
17777 been issued. */
17778 else if (!type || type == error_mark_node)
17780 /* An anonymous aggregate has to be handled specially; such
17781 a declaration really declares a data member (with a
17782 particular type), as opposed to a nested class. */
17783 else if (ANON_AGGR_TYPE_P (type))
17785 /* Remove constructors and such from TYPE, now that we
17786 know it is an anonymous aggregate. */
17787 fixup_anonymous_aggr (type);
17788 /* And make the corresponding data member. */
17789 decl = build_decl (decl_spec_token_start->location,
17790 FIELD_DECL, NULL_TREE, type);
17791 /* Add it to the class. */
17792 finish_member_declaration (decl);
17794 else
17795 cp_parser_check_access_in_redeclaration
17796 (TYPE_NAME (type),
17797 decl_spec_token_start->location);
17800 else
17802 bool assume_semicolon = false;
17804 /* See if these declarations will be friends. */
17805 friend_p = cp_parser_friend_p (&decl_specifiers);
17807 /* Keep going until we hit the `;' at the end of the
17808 declaration. */
17809 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17811 tree attributes = NULL_TREE;
17812 tree first_attribute;
17814 /* Peek at the next token. */
17815 token = cp_lexer_peek_token (parser->lexer);
17817 /* Check for a bitfield declaration. */
17818 if (token->type == CPP_COLON
17819 || (token->type == CPP_NAME
17820 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17821 == CPP_COLON))
17823 tree identifier;
17824 tree width;
17826 /* Get the name of the bitfield. Note that we cannot just
17827 check TOKEN here because it may have been invalidated by
17828 the call to cp_lexer_peek_nth_token above. */
17829 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17830 identifier = cp_parser_identifier (parser);
17831 else
17832 identifier = NULL_TREE;
17834 /* Consume the `:' token. */
17835 cp_lexer_consume_token (parser->lexer);
17836 /* Get the width of the bitfield. */
17837 width
17838 = cp_parser_constant_expression (parser,
17839 /*allow_non_constant=*/false,
17840 NULL);
17842 /* Look for attributes that apply to the bitfield. */
17843 attributes = cp_parser_attributes_opt (parser);
17844 /* Remember which attributes are prefix attributes and
17845 which are not. */
17846 first_attribute = attributes;
17847 /* Combine the attributes. */
17848 attributes = chainon (prefix_attributes, attributes);
17850 /* Create the bitfield declaration. */
17851 decl = grokbitfield (identifier
17852 ? make_id_declarator (NULL_TREE,
17853 identifier,
17854 sfk_none)
17855 : NULL,
17856 &decl_specifiers,
17857 width,
17858 attributes);
17860 else
17862 cp_declarator *declarator;
17863 tree initializer;
17864 tree asm_specification;
17865 int ctor_dtor_or_conv_p;
17867 /* Parse the declarator. */
17868 declarator
17869 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17870 &ctor_dtor_or_conv_p,
17871 /*parenthesized_p=*/NULL,
17872 /*member_p=*/true);
17874 /* If something went wrong parsing the declarator, make sure
17875 that we at least consume some tokens. */
17876 if (declarator == cp_error_declarator)
17878 /* Skip to the end of the statement. */
17879 cp_parser_skip_to_end_of_statement (parser);
17880 /* If the next token is not a semicolon, that is
17881 probably because we just skipped over the body of
17882 a function. So, we consume a semicolon if
17883 present, but do not issue an error message if it
17884 is not present. */
17885 if (cp_lexer_next_token_is (parser->lexer,
17886 CPP_SEMICOLON))
17887 cp_lexer_consume_token (parser->lexer);
17888 goto out;
17891 if (declares_class_or_enum & 2)
17892 cp_parser_check_for_definition_in_return_type
17893 (declarator, decl_specifiers.type,
17894 decl_specifiers.type_location);
17896 /* Look for an asm-specification. */
17897 asm_specification = cp_parser_asm_specification_opt (parser);
17898 /* Look for attributes that apply to the declaration. */
17899 attributes = cp_parser_attributes_opt (parser);
17900 /* Remember which attributes are prefix attributes and
17901 which are not. */
17902 first_attribute = attributes;
17903 /* Combine the attributes. */
17904 attributes = chainon (prefix_attributes, attributes);
17906 /* If it's an `=', then we have a constant-initializer or a
17907 pure-specifier. It is not correct to parse the
17908 initializer before registering the member declaration
17909 since the member declaration should be in scope while
17910 its initializer is processed. However, the rest of the
17911 front end does not yet provide an interface that allows
17912 us to handle this correctly. */
17913 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17915 /* In [class.mem]:
17917 A pure-specifier shall be used only in the declaration of
17918 a virtual function.
17920 A member-declarator can contain a constant-initializer
17921 only if it declares a static member of integral or
17922 enumeration type.
17924 Therefore, if the DECLARATOR is for a function, we look
17925 for a pure-specifier; otherwise, we look for a
17926 constant-initializer. When we call `grokfield', it will
17927 perform more stringent semantics checks. */
17928 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17929 if (function_declarator_p (declarator))
17930 initializer = cp_parser_pure_specifier (parser);
17931 else
17932 /* Parse the initializer. */
17933 initializer = cp_parser_constant_initializer (parser);
17935 /* Otherwise, there is no initializer. */
17936 else
17937 initializer = NULL_TREE;
17939 /* See if we are probably looking at a function
17940 definition. We are certainly not looking at a
17941 member-declarator. Calling `grokfield' has
17942 side-effects, so we must not do it unless we are sure
17943 that we are looking at a member-declarator. */
17944 if (cp_parser_token_starts_function_definition_p
17945 (cp_lexer_peek_token (parser->lexer)))
17947 /* The grammar does not allow a pure-specifier to be
17948 used when a member function is defined. (It is
17949 possible that this fact is an oversight in the
17950 standard, since a pure function may be defined
17951 outside of the class-specifier. */
17952 if (initializer)
17953 error_at (initializer_token_start->location,
17954 "pure-specifier on function-definition");
17955 decl = cp_parser_save_member_function_body (parser,
17956 &decl_specifiers,
17957 declarator,
17958 attributes);
17959 /* If the member was not a friend, declare it here. */
17960 if (!friend_p)
17961 finish_member_declaration (decl);
17962 /* Peek at the next token. */
17963 token = cp_lexer_peek_token (parser->lexer);
17964 /* If the next token is a semicolon, consume it. */
17965 if (token->type == CPP_SEMICOLON)
17966 cp_lexer_consume_token (parser->lexer);
17967 goto out;
17969 else
17970 if (declarator->kind == cdk_function)
17971 declarator->id_loc = token->location;
17972 /* Create the declaration. */
17973 decl = grokfield (declarator, &decl_specifiers,
17974 initializer, /*init_const_expr_p=*/true,
17975 asm_specification,
17976 attributes);
17979 /* Reset PREFIX_ATTRIBUTES. */
17980 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17981 attributes = TREE_CHAIN (attributes);
17982 if (attributes)
17983 TREE_CHAIN (attributes) = NULL_TREE;
17985 /* If there is any qualification still in effect, clear it
17986 now; we will be starting fresh with the next declarator. */
17987 parser->scope = NULL_TREE;
17988 parser->qualifying_scope = NULL_TREE;
17989 parser->object_scope = NULL_TREE;
17990 /* If it's a `,', then there are more declarators. */
17991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17992 cp_lexer_consume_token (parser->lexer);
17993 /* If the next token isn't a `;', then we have a parse error. */
17994 else if (cp_lexer_next_token_is_not (parser->lexer,
17995 CPP_SEMICOLON))
17997 /* The next token might be a ways away from where the
17998 actual semicolon is missing. Find the previous token
17999 and use that for our error position. */
18000 cp_token *token = cp_lexer_previous_token (parser->lexer);
18001 error_at (token->location,
18002 "expected %<;%> at end of member declaration");
18004 /* Assume that the user meant to provide a semicolon. If
18005 we were to cp_parser_skip_to_end_of_statement, we might
18006 skip to a semicolon inside a member function definition
18007 and issue nonsensical error messages. */
18008 assume_semicolon = true;
18011 if (decl)
18013 /* Add DECL to the list of members. */
18014 if (!friend_p)
18015 finish_member_declaration (decl);
18017 if (TREE_CODE (decl) == FUNCTION_DECL)
18018 cp_parser_save_default_args (parser, decl);
18021 if (assume_semicolon)
18022 goto out;
18026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18027 out:
18028 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18031 /* Parse a pure-specifier.
18033 pure-specifier:
18036 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18037 Otherwise, ERROR_MARK_NODE is returned. */
18039 static tree
18040 cp_parser_pure_specifier (cp_parser* parser)
18042 cp_token *token;
18044 /* Look for the `=' token. */
18045 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18046 return error_mark_node;
18047 /* Look for the `0' token. */
18048 token = cp_lexer_peek_token (parser->lexer);
18050 if (token->type == CPP_EOF
18051 || token->type == CPP_PRAGMA_EOL)
18052 return error_mark_node;
18054 cp_lexer_consume_token (parser->lexer);
18056 /* Accept = default or = delete in c++0x mode. */
18057 if (token->keyword == RID_DEFAULT
18058 || token->keyword == RID_DELETE)
18060 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18061 return token->u.value;
18064 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18065 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18067 cp_parser_error (parser,
18068 "invalid pure specifier (only %<= 0%> is allowed)");
18069 cp_parser_skip_to_end_of_statement (parser);
18070 return error_mark_node;
18072 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18074 error_at (token->location, "templates may not be %<virtual%>");
18075 return error_mark_node;
18078 return integer_zero_node;
18081 /* Parse a constant-initializer.
18083 constant-initializer:
18084 = constant-expression
18086 Returns a representation of the constant-expression. */
18088 static tree
18089 cp_parser_constant_initializer (cp_parser* parser)
18091 /* Look for the `=' token. */
18092 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18093 return error_mark_node;
18095 /* It is invalid to write:
18097 struct S { static const int i = { 7 }; };
18100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18102 cp_parser_error (parser,
18103 "a brace-enclosed initializer is not allowed here");
18104 /* Consume the opening brace. */
18105 cp_lexer_consume_token (parser->lexer);
18106 /* Skip the initializer. */
18107 cp_parser_skip_to_closing_brace (parser);
18108 /* Look for the trailing `}'. */
18109 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18111 return error_mark_node;
18114 return cp_parser_constant_expression (parser,
18115 /*allow_non_constant=*/false,
18116 NULL);
18119 /* Derived classes [gram.class.derived] */
18121 /* Parse a base-clause.
18123 base-clause:
18124 : base-specifier-list
18126 base-specifier-list:
18127 base-specifier ... [opt]
18128 base-specifier-list , base-specifier ... [opt]
18130 Returns a TREE_LIST representing the base-classes, in the order in
18131 which they were declared. The representation of each node is as
18132 described by cp_parser_base_specifier.
18134 In the case that no bases are specified, this function will return
18135 NULL_TREE, not ERROR_MARK_NODE. */
18137 static tree
18138 cp_parser_base_clause (cp_parser* parser)
18140 tree bases = NULL_TREE;
18142 /* Look for the `:' that begins the list. */
18143 cp_parser_require (parser, CPP_COLON, RT_COLON);
18145 /* Scan the base-specifier-list. */
18146 while (true)
18148 cp_token *token;
18149 tree base;
18150 bool pack_expansion_p = false;
18152 /* Look for the base-specifier. */
18153 base = cp_parser_base_specifier (parser);
18154 /* Look for the (optional) ellipsis. */
18155 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18157 /* Consume the `...'. */
18158 cp_lexer_consume_token (parser->lexer);
18160 pack_expansion_p = true;
18163 /* Add BASE to the front of the list. */
18164 if (base != error_mark_node)
18166 if (pack_expansion_p)
18167 /* Make this a pack expansion type. */
18168 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18171 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18173 TREE_CHAIN (base) = bases;
18174 bases = base;
18177 /* Peek at the next token. */
18178 token = cp_lexer_peek_token (parser->lexer);
18179 /* If it's not a comma, then the list is complete. */
18180 if (token->type != CPP_COMMA)
18181 break;
18182 /* Consume the `,'. */
18183 cp_lexer_consume_token (parser->lexer);
18186 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18187 base class had a qualified name. However, the next name that
18188 appears is certainly not qualified. */
18189 parser->scope = NULL_TREE;
18190 parser->qualifying_scope = NULL_TREE;
18191 parser->object_scope = NULL_TREE;
18193 return nreverse (bases);
18196 /* Parse a base-specifier.
18198 base-specifier:
18199 :: [opt] nested-name-specifier [opt] class-name
18200 virtual access-specifier [opt] :: [opt] nested-name-specifier
18201 [opt] class-name
18202 access-specifier virtual [opt] :: [opt] nested-name-specifier
18203 [opt] class-name
18205 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18206 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18207 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18208 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18210 static tree
18211 cp_parser_base_specifier (cp_parser* parser)
18213 cp_token *token;
18214 bool done = false;
18215 bool virtual_p = false;
18216 bool duplicate_virtual_error_issued_p = false;
18217 bool duplicate_access_error_issued_p = false;
18218 bool class_scope_p, template_p;
18219 tree access = access_default_node;
18220 tree type;
18222 /* Process the optional `virtual' and `access-specifier'. */
18223 while (!done)
18225 /* Peek at the next token. */
18226 token = cp_lexer_peek_token (parser->lexer);
18227 /* Process `virtual'. */
18228 switch (token->keyword)
18230 case RID_VIRTUAL:
18231 /* If `virtual' appears more than once, issue an error. */
18232 if (virtual_p && !duplicate_virtual_error_issued_p)
18234 cp_parser_error (parser,
18235 "%<virtual%> specified more than once in base-specified");
18236 duplicate_virtual_error_issued_p = true;
18239 virtual_p = true;
18241 /* Consume the `virtual' token. */
18242 cp_lexer_consume_token (parser->lexer);
18244 break;
18246 case RID_PUBLIC:
18247 case RID_PROTECTED:
18248 case RID_PRIVATE:
18249 /* If more than one access specifier appears, issue an
18250 error. */
18251 if (access != access_default_node
18252 && !duplicate_access_error_issued_p)
18254 cp_parser_error (parser,
18255 "more than one access specifier in base-specified");
18256 duplicate_access_error_issued_p = true;
18259 access = ridpointers[(int) token->keyword];
18261 /* Consume the access-specifier. */
18262 cp_lexer_consume_token (parser->lexer);
18264 break;
18266 default:
18267 done = true;
18268 break;
18271 /* It is not uncommon to see programs mechanically, erroneously, use
18272 the 'typename' keyword to denote (dependent) qualified types
18273 as base classes. */
18274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18276 token = cp_lexer_peek_token (parser->lexer);
18277 if (!processing_template_decl)
18278 error_at (token->location,
18279 "keyword %<typename%> not allowed outside of templates");
18280 else
18281 error_at (token->location,
18282 "keyword %<typename%> not allowed in this context "
18283 "(the base class is implicitly a type)");
18284 cp_lexer_consume_token (parser->lexer);
18287 /* Look for the optional `::' operator. */
18288 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18289 /* Look for the nested-name-specifier. The simplest way to
18290 implement:
18292 [temp.res]
18294 The keyword `typename' is not permitted in a base-specifier or
18295 mem-initializer; in these contexts a qualified name that
18296 depends on a template-parameter is implicitly assumed to be a
18297 type name.
18299 is to pretend that we have seen the `typename' keyword at this
18300 point. */
18301 cp_parser_nested_name_specifier_opt (parser,
18302 /*typename_keyword_p=*/true,
18303 /*check_dependency_p=*/true,
18304 typename_type,
18305 /*is_declaration=*/true);
18306 /* If the base class is given by a qualified name, assume that names
18307 we see are type names or templates, as appropriate. */
18308 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18309 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18311 /* Finally, look for the class-name. */
18312 type = cp_parser_class_name (parser,
18313 class_scope_p,
18314 template_p,
18315 typename_type,
18316 /*check_dependency_p=*/true,
18317 /*class_head_p=*/false,
18318 /*is_declaration=*/true);
18320 if (type == error_mark_node)
18321 return error_mark_node;
18323 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18326 /* Exception handling [gram.exception] */
18328 /* Parse an (optional) exception-specification.
18330 exception-specification:
18331 throw ( type-id-list [opt] )
18333 Returns a TREE_LIST representing the exception-specification. The
18334 TREE_VALUE of each node is a type. */
18336 static tree
18337 cp_parser_exception_specification_opt (cp_parser* parser)
18339 cp_token *token;
18340 tree type_id_list;
18341 const char *saved_message;
18343 /* Peek at the next token. */
18344 token = cp_lexer_peek_token (parser->lexer);
18346 /* Is it a noexcept-specification? */
18347 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18349 tree expr;
18350 cp_lexer_consume_token (parser->lexer);
18352 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18354 cp_lexer_consume_token (parser->lexer);
18356 /* Types may not be defined in an exception-specification. */
18357 saved_message = parser->type_definition_forbidden_message;
18358 parser->type_definition_forbidden_message
18359 = G_("types may not be defined in an exception-specification");
18361 expr = cp_parser_constant_expression (parser, false, NULL);
18363 /* Restore the saved message. */
18364 parser->type_definition_forbidden_message = saved_message;
18366 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18368 else
18369 expr = boolean_true_node;
18371 return build_noexcept_spec (expr, tf_warning_or_error);
18374 /* If it's not `throw', then there's no exception-specification. */
18375 if (!cp_parser_is_keyword (token, RID_THROW))
18376 return NULL_TREE;
18378 #if 0
18379 /* Enable this once a lot of code has transitioned to noexcept? */
18380 if (cxx_dialect == cxx0x && !in_system_header)
18381 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18382 "deprecated in C++0x; use %<noexcept%> instead");
18383 #endif
18385 /* Consume the `throw'. */
18386 cp_lexer_consume_token (parser->lexer);
18388 /* Look for the `('. */
18389 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18391 /* Peek at the next token. */
18392 token = cp_lexer_peek_token (parser->lexer);
18393 /* If it's not a `)', then there is a type-id-list. */
18394 if (token->type != CPP_CLOSE_PAREN)
18396 /* Types may not be defined in an exception-specification. */
18397 saved_message = parser->type_definition_forbidden_message;
18398 parser->type_definition_forbidden_message
18399 = G_("types may not be defined in an exception-specification");
18400 /* Parse the type-id-list. */
18401 type_id_list = cp_parser_type_id_list (parser);
18402 /* Restore the saved message. */
18403 parser->type_definition_forbidden_message = saved_message;
18405 else
18406 type_id_list = empty_except_spec;
18408 /* Look for the `)'. */
18409 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18411 return type_id_list;
18414 /* Parse an (optional) type-id-list.
18416 type-id-list:
18417 type-id ... [opt]
18418 type-id-list , type-id ... [opt]
18420 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18421 in the order that the types were presented. */
18423 static tree
18424 cp_parser_type_id_list (cp_parser* parser)
18426 tree types = NULL_TREE;
18428 while (true)
18430 cp_token *token;
18431 tree type;
18433 /* Get the next type-id. */
18434 type = cp_parser_type_id (parser);
18435 /* Parse the optional ellipsis. */
18436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18438 /* Consume the `...'. */
18439 cp_lexer_consume_token (parser->lexer);
18441 /* Turn the type into a pack expansion expression. */
18442 type = make_pack_expansion (type);
18444 /* Add it to the list. */
18445 types = add_exception_specifier (types, type, /*complain=*/1);
18446 /* Peek at the next token. */
18447 token = cp_lexer_peek_token (parser->lexer);
18448 /* If it is not a `,', we are done. */
18449 if (token->type != CPP_COMMA)
18450 break;
18451 /* Consume the `,'. */
18452 cp_lexer_consume_token (parser->lexer);
18455 return nreverse (types);
18458 /* Parse a try-block.
18460 try-block:
18461 try compound-statement handler-seq */
18463 static tree
18464 cp_parser_try_block (cp_parser* parser)
18466 tree try_block;
18468 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18469 try_block = begin_try_block ();
18470 cp_parser_compound_statement (parser, NULL, true);
18471 finish_try_block (try_block);
18472 cp_parser_handler_seq (parser);
18473 finish_handler_sequence (try_block);
18475 return try_block;
18478 /* Parse a function-try-block.
18480 function-try-block:
18481 try ctor-initializer [opt] function-body handler-seq */
18483 static bool
18484 cp_parser_function_try_block (cp_parser* parser)
18486 tree compound_stmt;
18487 tree try_block;
18488 bool ctor_initializer_p;
18490 /* Look for the `try' keyword. */
18491 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18492 return false;
18493 /* Let the rest of the front end know where we are. */
18494 try_block = begin_function_try_block (&compound_stmt);
18495 /* Parse the function-body. */
18496 ctor_initializer_p
18497 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18498 /* We're done with the `try' part. */
18499 finish_function_try_block (try_block);
18500 /* Parse the handlers. */
18501 cp_parser_handler_seq (parser);
18502 /* We're done with the handlers. */
18503 finish_function_handler_sequence (try_block, compound_stmt);
18505 return ctor_initializer_p;
18508 /* Parse a handler-seq.
18510 handler-seq:
18511 handler handler-seq [opt] */
18513 static void
18514 cp_parser_handler_seq (cp_parser* parser)
18516 while (true)
18518 cp_token *token;
18520 /* Parse the handler. */
18521 cp_parser_handler (parser);
18522 /* Peek at the next token. */
18523 token = cp_lexer_peek_token (parser->lexer);
18524 /* If it's not `catch' then there are no more handlers. */
18525 if (!cp_parser_is_keyword (token, RID_CATCH))
18526 break;
18530 /* Parse a handler.
18532 handler:
18533 catch ( exception-declaration ) compound-statement */
18535 static void
18536 cp_parser_handler (cp_parser* parser)
18538 tree handler;
18539 tree declaration;
18541 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18542 handler = begin_handler ();
18543 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18544 declaration = cp_parser_exception_declaration (parser);
18545 finish_handler_parms (declaration, handler);
18546 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18547 cp_parser_compound_statement (parser, NULL, false);
18548 finish_handler (handler);
18551 /* Parse an exception-declaration.
18553 exception-declaration:
18554 type-specifier-seq declarator
18555 type-specifier-seq abstract-declarator
18556 type-specifier-seq
18559 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18560 ellipsis variant is used. */
18562 static tree
18563 cp_parser_exception_declaration (cp_parser* parser)
18565 cp_decl_specifier_seq type_specifiers;
18566 cp_declarator *declarator;
18567 const char *saved_message;
18569 /* If it's an ellipsis, it's easy to handle. */
18570 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18572 /* Consume the `...' token. */
18573 cp_lexer_consume_token (parser->lexer);
18574 return NULL_TREE;
18577 /* Types may not be defined in exception-declarations. */
18578 saved_message = parser->type_definition_forbidden_message;
18579 parser->type_definition_forbidden_message
18580 = G_("types may not be defined in exception-declarations");
18582 /* Parse the type-specifier-seq. */
18583 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18584 /*is_trailing_return=*/false,
18585 &type_specifiers);
18586 /* If it's a `)', then there is no declarator. */
18587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18588 declarator = NULL;
18589 else
18590 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18591 /*ctor_dtor_or_conv_p=*/NULL,
18592 /*parenthesized_p=*/NULL,
18593 /*member_p=*/false);
18595 /* Restore the saved message. */
18596 parser->type_definition_forbidden_message = saved_message;
18598 if (!type_specifiers.any_specifiers_p)
18599 return error_mark_node;
18601 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18604 /* Parse a throw-expression.
18606 throw-expression:
18607 throw assignment-expression [opt]
18609 Returns a THROW_EXPR representing the throw-expression. */
18611 static tree
18612 cp_parser_throw_expression (cp_parser* parser)
18614 tree expression;
18615 cp_token* token;
18617 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18618 token = cp_lexer_peek_token (parser->lexer);
18619 /* Figure out whether or not there is an assignment-expression
18620 following the "throw" keyword. */
18621 if (token->type == CPP_COMMA
18622 || token->type == CPP_SEMICOLON
18623 || token->type == CPP_CLOSE_PAREN
18624 || token->type == CPP_CLOSE_SQUARE
18625 || token->type == CPP_CLOSE_BRACE
18626 || token->type == CPP_COLON)
18627 expression = NULL_TREE;
18628 else
18629 expression = cp_parser_assignment_expression (parser,
18630 /*cast_p=*/false, NULL);
18632 return build_throw (expression);
18635 /* GNU Extensions */
18637 /* Parse an (optional) asm-specification.
18639 asm-specification:
18640 asm ( string-literal )
18642 If the asm-specification is present, returns a STRING_CST
18643 corresponding to the string-literal. Otherwise, returns
18644 NULL_TREE. */
18646 static tree
18647 cp_parser_asm_specification_opt (cp_parser* parser)
18649 cp_token *token;
18650 tree asm_specification;
18652 /* Peek at the next token. */
18653 token = cp_lexer_peek_token (parser->lexer);
18654 /* If the next token isn't the `asm' keyword, then there's no
18655 asm-specification. */
18656 if (!cp_parser_is_keyword (token, RID_ASM))
18657 return NULL_TREE;
18659 /* Consume the `asm' token. */
18660 cp_lexer_consume_token (parser->lexer);
18661 /* Look for the `('. */
18662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18664 /* Look for the string-literal. */
18665 asm_specification = cp_parser_string_literal (parser, false, false);
18667 /* Look for the `)'. */
18668 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18670 return asm_specification;
18673 /* Parse an asm-operand-list.
18675 asm-operand-list:
18676 asm-operand
18677 asm-operand-list , asm-operand
18679 asm-operand:
18680 string-literal ( expression )
18681 [ string-literal ] string-literal ( expression )
18683 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18684 each node is the expression. The TREE_PURPOSE is itself a
18685 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18686 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18687 is a STRING_CST for the string literal before the parenthesis. Returns
18688 ERROR_MARK_NODE if any of the operands are invalid. */
18690 static tree
18691 cp_parser_asm_operand_list (cp_parser* parser)
18693 tree asm_operands = NULL_TREE;
18694 bool invalid_operands = false;
18696 while (true)
18698 tree string_literal;
18699 tree expression;
18700 tree name;
18702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18704 /* Consume the `[' token. */
18705 cp_lexer_consume_token (parser->lexer);
18706 /* Read the operand name. */
18707 name = cp_parser_identifier (parser);
18708 if (name != error_mark_node)
18709 name = build_string (IDENTIFIER_LENGTH (name),
18710 IDENTIFIER_POINTER (name));
18711 /* Look for the closing `]'. */
18712 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18714 else
18715 name = NULL_TREE;
18716 /* Look for the string-literal. */
18717 string_literal = cp_parser_string_literal (parser, false, false);
18719 /* Look for the `('. */
18720 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18721 /* Parse the expression. */
18722 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18723 /* Look for the `)'. */
18724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18726 if (name == error_mark_node
18727 || string_literal == error_mark_node
18728 || expression == error_mark_node)
18729 invalid_operands = true;
18731 /* Add this operand to the list. */
18732 asm_operands = tree_cons (build_tree_list (name, string_literal),
18733 expression,
18734 asm_operands);
18735 /* If the next token is not a `,', there are no more
18736 operands. */
18737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18738 break;
18739 /* Consume the `,'. */
18740 cp_lexer_consume_token (parser->lexer);
18743 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18746 /* Parse an asm-clobber-list.
18748 asm-clobber-list:
18749 string-literal
18750 asm-clobber-list , string-literal
18752 Returns a TREE_LIST, indicating the clobbers in the order that they
18753 appeared. The TREE_VALUE of each node is a STRING_CST. */
18755 static tree
18756 cp_parser_asm_clobber_list (cp_parser* parser)
18758 tree clobbers = NULL_TREE;
18760 while (true)
18762 tree string_literal;
18764 /* Look for the string literal. */
18765 string_literal = cp_parser_string_literal (parser, false, false);
18766 /* Add it to the list. */
18767 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18768 /* If the next token is not a `,', then the list is
18769 complete. */
18770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18771 break;
18772 /* Consume the `,' token. */
18773 cp_lexer_consume_token (parser->lexer);
18776 return clobbers;
18779 /* Parse an asm-label-list.
18781 asm-label-list:
18782 identifier
18783 asm-label-list , identifier
18785 Returns a TREE_LIST, indicating the labels in the order that they
18786 appeared. The TREE_VALUE of each node is a label. */
18788 static tree
18789 cp_parser_asm_label_list (cp_parser* parser)
18791 tree labels = NULL_TREE;
18793 while (true)
18795 tree identifier, label, name;
18797 /* Look for the identifier. */
18798 identifier = cp_parser_identifier (parser);
18799 if (!error_operand_p (identifier))
18801 label = lookup_label (identifier);
18802 if (TREE_CODE (label) == LABEL_DECL)
18804 TREE_USED (label) = 1;
18805 check_goto (label);
18806 name = build_string (IDENTIFIER_LENGTH (identifier),
18807 IDENTIFIER_POINTER (identifier));
18808 labels = tree_cons (name, label, labels);
18811 /* If the next token is not a `,', then the list is
18812 complete. */
18813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18814 break;
18815 /* Consume the `,' token. */
18816 cp_lexer_consume_token (parser->lexer);
18819 return nreverse (labels);
18822 /* Parse an (optional) series of attributes.
18824 attributes:
18825 attributes attribute
18827 attribute:
18828 __attribute__ (( attribute-list [opt] ))
18830 The return value is as for cp_parser_attribute_list. */
18832 static tree
18833 cp_parser_attributes_opt (cp_parser* parser)
18835 tree attributes = NULL_TREE;
18837 while (true)
18839 cp_token *token;
18840 tree attribute_list;
18842 /* Peek at the next token. */
18843 token = cp_lexer_peek_token (parser->lexer);
18844 /* If it's not `__attribute__', then we're done. */
18845 if (token->keyword != RID_ATTRIBUTE)
18846 break;
18848 /* Consume the `__attribute__' keyword. */
18849 cp_lexer_consume_token (parser->lexer);
18850 /* Look for the two `(' tokens. */
18851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18854 /* Peek at the next token. */
18855 token = cp_lexer_peek_token (parser->lexer);
18856 if (token->type != CPP_CLOSE_PAREN)
18857 /* Parse the attribute-list. */
18858 attribute_list = cp_parser_attribute_list (parser);
18859 else
18860 /* If the next token is a `)', then there is no attribute
18861 list. */
18862 attribute_list = NULL;
18864 /* Look for the two `)' tokens. */
18865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18866 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18868 /* Add these new attributes to the list. */
18869 attributes = chainon (attributes, attribute_list);
18872 return attributes;
18875 /* Parse an attribute-list.
18877 attribute-list:
18878 attribute
18879 attribute-list , attribute
18881 attribute:
18882 identifier
18883 identifier ( identifier )
18884 identifier ( identifier , expression-list )
18885 identifier ( expression-list )
18887 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18888 to an attribute. The TREE_PURPOSE of each node is the identifier
18889 indicating which attribute is in use. The TREE_VALUE represents
18890 the arguments, if any. */
18892 static tree
18893 cp_parser_attribute_list (cp_parser* parser)
18895 tree attribute_list = NULL_TREE;
18896 bool save_translate_strings_p = parser->translate_strings_p;
18898 parser->translate_strings_p = false;
18899 while (true)
18901 cp_token *token;
18902 tree identifier;
18903 tree attribute;
18905 /* Look for the identifier. We also allow keywords here; for
18906 example `__attribute__ ((const))' is legal. */
18907 token = cp_lexer_peek_token (parser->lexer);
18908 if (token->type == CPP_NAME
18909 || token->type == CPP_KEYWORD)
18911 tree arguments = NULL_TREE;
18913 /* Consume the token. */
18914 token = cp_lexer_consume_token (parser->lexer);
18916 /* Save away the identifier that indicates which attribute
18917 this is. */
18918 identifier = (token->type == CPP_KEYWORD)
18919 /* For keywords, use the canonical spelling, not the
18920 parsed identifier. */
18921 ? ridpointers[(int) token->keyword]
18922 : token->u.value;
18924 attribute = build_tree_list (identifier, NULL_TREE);
18926 /* Peek at the next token. */
18927 token = cp_lexer_peek_token (parser->lexer);
18928 /* If it's an `(', then parse the attribute arguments. */
18929 if (token->type == CPP_OPEN_PAREN)
18931 VEC(tree,gc) *vec;
18932 int attr_flag = (attribute_takes_identifier_p (identifier)
18933 ? id_attr : normal_attr);
18934 vec = cp_parser_parenthesized_expression_list
18935 (parser, attr_flag, /*cast_p=*/false,
18936 /*allow_expansion_p=*/false,
18937 /*non_constant_p=*/NULL);
18938 if (vec == NULL)
18939 arguments = error_mark_node;
18940 else
18942 arguments = build_tree_list_vec (vec);
18943 release_tree_vector (vec);
18945 /* Save the arguments away. */
18946 TREE_VALUE (attribute) = arguments;
18949 if (arguments != error_mark_node)
18951 /* Add this attribute to the list. */
18952 TREE_CHAIN (attribute) = attribute_list;
18953 attribute_list = attribute;
18956 token = cp_lexer_peek_token (parser->lexer);
18958 /* Now, look for more attributes. If the next token isn't a
18959 `,', we're done. */
18960 if (token->type != CPP_COMMA)
18961 break;
18963 /* Consume the comma and keep going. */
18964 cp_lexer_consume_token (parser->lexer);
18966 parser->translate_strings_p = save_translate_strings_p;
18968 /* We built up the list in reverse order. */
18969 return nreverse (attribute_list);
18972 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18973 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18974 current value of the PEDANTIC flag, regardless of whether or not
18975 the `__extension__' keyword is present. The caller is responsible
18976 for restoring the value of the PEDANTIC flag. */
18978 static bool
18979 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18981 /* Save the old value of the PEDANTIC flag. */
18982 *saved_pedantic = pedantic;
18984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18986 /* Consume the `__extension__' token. */
18987 cp_lexer_consume_token (parser->lexer);
18988 /* We're not being pedantic while the `__extension__' keyword is
18989 in effect. */
18990 pedantic = 0;
18992 return true;
18995 return false;
18998 /* Parse a label declaration.
19000 label-declaration:
19001 __label__ label-declarator-seq ;
19003 label-declarator-seq:
19004 identifier , label-declarator-seq
19005 identifier */
19007 static void
19008 cp_parser_label_declaration (cp_parser* parser)
19010 /* Look for the `__label__' keyword. */
19011 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19013 while (true)
19015 tree identifier;
19017 /* Look for an identifier. */
19018 identifier = cp_parser_identifier (parser);
19019 /* If we failed, stop. */
19020 if (identifier == error_mark_node)
19021 break;
19022 /* Declare it as a label. */
19023 finish_label_decl (identifier);
19024 /* If the next token is a `;', stop. */
19025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19026 break;
19027 /* Look for the `,' separating the label declarations. */
19028 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19031 /* Look for the final `;'. */
19032 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19035 /* Support Functions */
19037 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19038 NAME should have one of the representations used for an
19039 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19040 is returned. If PARSER->SCOPE is a dependent type, then a
19041 SCOPE_REF is returned.
19043 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19044 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19045 was formed. Abstractly, such entities should not be passed to this
19046 function, because they do not need to be looked up, but it is
19047 simpler to check for this special case here, rather than at the
19048 call-sites.
19050 In cases not explicitly covered above, this function returns a
19051 DECL, OVERLOAD, or baselink representing the result of the lookup.
19052 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19053 is returned.
19055 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19056 (e.g., "struct") that was used. In that case bindings that do not
19057 refer to types are ignored.
19059 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19060 ignored.
19062 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19063 are ignored.
19065 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19066 types.
19068 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19069 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19070 NULL_TREE otherwise. */
19072 static tree
19073 cp_parser_lookup_name (cp_parser *parser, tree name,
19074 enum tag_types tag_type,
19075 bool is_template,
19076 bool is_namespace,
19077 bool check_dependency,
19078 tree *ambiguous_decls,
19079 location_t name_location)
19081 int flags = 0;
19082 tree decl;
19083 tree object_type = parser->context->object_type;
19085 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19086 flags |= LOOKUP_COMPLAIN;
19088 /* Assume that the lookup will be unambiguous. */
19089 if (ambiguous_decls)
19090 *ambiguous_decls = NULL_TREE;
19092 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19093 no longer valid. Note that if we are parsing tentatively, and
19094 the parse fails, OBJECT_TYPE will be automatically restored. */
19095 parser->context->object_type = NULL_TREE;
19097 if (name == error_mark_node)
19098 return error_mark_node;
19100 /* A template-id has already been resolved; there is no lookup to
19101 do. */
19102 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19103 return name;
19104 if (BASELINK_P (name))
19106 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19107 == TEMPLATE_ID_EXPR);
19108 return name;
19111 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19112 it should already have been checked to make sure that the name
19113 used matches the type being destroyed. */
19114 if (TREE_CODE (name) == BIT_NOT_EXPR)
19116 tree type;
19118 /* Figure out to which type this destructor applies. */
19119 if (parser->scope)
19120 type = parser->scope;
19121 else if (object_type)
19122 type = object_type;
19123 else
19124 type = current_class_type;
19125 /* If that's not a class type, there is no destructor. */
19126 if (!type || !CLASS_TYPE_P (type))
19127 return error_mark_node;
19128 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19129 lazily_declare_fn (sfk_destructor, type);
19130 if (!CLASSTYPE_DESTRUCTORS (type))
19131 return error_mark_node;
19132 /* If it was a class type, return the destructor. */
19133 return CLASSTYPE_DESTRUCTORS (type);
19136 /* By this point, the NAME should be an ordinary identifier. If
19137 the id-expression was a qualified name, the qualifying scope is
19138 stored in PARSER->SCOPE at this point. */
19139 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19141 /* Perform the lookup. */
19142 if (parser->scope)
19144 bool dependent_p;
19146 if (parser->scope == error_mark_node)
19147 return error_mark_node;
19149 /* If the SCOPE is dependent, the lookup must be deferred until
19150 the template is instantiated -- unless we are explicitly
19151 looking up names in uninstantiated templates. Even then, we
19152 cannot look up the name if the scope is not a class type; it
19153 might, for example, be a template type parameter. */
19154 dependent_p = (TYPE_P (parser->scope)
19155 && dependent_scope_p (parser->scope));
19156 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19157 && dependent_p)
19158 /* Defer lookup. */
19159 decl = error_mark_node;
19160 else
19162 tree pushed_scope = NULL_TREE;
19164 /* If PARSER->SCOPE is a dependent type, then it must be a
19165 class type, and we must not be checking dependencies;
19166 otherwise, we would have processed this lookup above. So
19167 that PARSER->SCOPE is not considered a dependent base by
19168 lookup_member, we must enter the scope here. */
19169 if (dependent_p)
19170 pushed_scope = push_scope (parser->scope);
19172 /* If the PARSER->SCOPE is a template specialization, it
19173 may be instantiated during name lookup. In that case,
19174 errors may be issued. Even if we rollback the current
19175 tentative parse, those errors are valid. */
19176 decl = lookup_qualified_name (parser->scope, name,
19177 tag_type != none_type,
19178 /*complain=*/true);
19180 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19181 lookup result and the nested-name-specifier nominates a class C:
19182 * if the name specified after the nested-name-specifier, when
19183 looked up in C, is the injected-class-name of C (Clause 9), or
19184 * if the name specified after the nested-name-specifier is the
19185 same as the identifier or the simple-template-id's template-
19186 name in the last component of the nested-name-specifier,
19187 the name is instead considered to name the constructor of
19188 class C. [ Note: for example, the constructor is not an
19189 acceptable lookup result in an elaborated-type-specifier so
19190 the constructor would not be used in place of the
19191 injected-class-name. --end note ] Such a constructor name
19192 shall be used only in the declarator-id of a declaration that
19193 names a constructor or in a using-declaration. */
19194 if (tag_type == none_type
19195 && DECL_SELF_REFERENCE_P (decl)
19196 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19197 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19198 tag_type != none_type,
19199 /*complain=*/true);
19201 /* If we have a single function from a using decl, pull it out. */
19202 if (TREE_CODE (decl) == OVERLOAD
19203 && !really_overloaded_fn (decl))
19204 decl = OVL_FUNCTION (decl);
19206 if (pushed_scope)
19207 pop_scope (pushed_scope);
19210 /* If the scope is a dependent type and either we deferred lookup or
19211 we did lookup but didn't find the name, rememeber the name. */
19212 if (decl == error_mark_node && TYPE_P (parser->scope)
19213 && dependent_type_p (parser->scope))
19215 if (tag_type)
19217 tree type;
19219 /* The resolution to Core Issue 180 says that `struct
19220 A::B' should be considered a type-name, even if `A'
19221 is dependent. */
19222 type = make_typename_type (parser->scope, name, tag_type,
19223 /*complain=*/tf_error);
19224 decl = TYPE_NAME (type);
19226 else if (is_template
19227 && (cp_parser_next_token_ends_template_argument_p (parser)
19228 || cp_lexer_next_token_is (parser->lexer,
19229 CPP_CLOSE_PAREN)))
19230 decl = make_unbound_class_template (parser->scope,
19231 name, NULL_TREE,
19232 /*complain=*/tf_error);
19233 else
19234 decl = build_qualified_name (/*type=*/NULL_TREE,
19235 parser->scope, name,
19236 is_template);
19238 parser->qualifying_scope = parser->scope;
19239 parser->object_scope = NULL_TREE;
19241 else if (object_type)
19243 tree object_decl = NULL_TREE;
19244 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19245 OBJECT_TYPE is not a class. */
19246 if (CLASS_TYPE_P (object_type))
19247 /* If the OBJECT_TYPE is a template specialization, it may
19248 be instantiated during name lookup. In that case, errors
19249 may be issued. Even if we rollback the current tentative
19250 parse, those errors are valid. */
19251 object_decl = lookup_member (object_type,
19252 name,
19253 /*protect=*/0,
19254 tag_type != none_type);
19255 /* Look it up in the enclosing context, too. */
19256 decl = lookup_name_real (name, tag_type != none_type,
19257 /*nonclass=*/0,
19258 /*block_p=*/true, is_namespace, flags);
19259 parser->object_scope = object_type;
19260 parser->qualifying_scope = NULL_TREE;
19261 if (object_decl)
19262 decl = object_decl;
19264 else
19266 decl = lookup_name_real (name, tag_type != none_type,
19267 /*nonclass=*/0,
19268 /*block_p=*/true, is_namespace, flags);
19269 parser->qualifying_scope = NULL_TREE;
19270 parser->object_scope = NULL_TREE;
19273 /* If the lookup failed, let our caller know. */
19274 if (!decl || decl == error_mark_node)
19275 return error_mark_node;
19277 /* Pull out the template from an injected-class-name (or multiple). */
19278 if (is_template)
19279 decl = maybe_get_template_decl_from_type_decl (decl);
19281 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19282 if (TREE_CODE (decl) == TREE_LIST)
19284 if (ambiguous_decls)
19285 *ambiguous_decls = decl;
19286 /* The error message we have to print is too complicated for
19287 cp_parser_error, so we incorporate its actions directly. */
19288 if (!cp_parser_simulate_error (parser))
19290 error_at (name_location, "reference to %qD is ambiguous",
19291 name);
19292 print_candidates (decl);
19294 return error_mark_node;
19297 gcc_assert (DECL_P (decl)
19298 || TREE_CODE (decl) == OVERLOAD
19299 || TREE_CODE (decl) == SCOPE_REF
19300 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19301 || BASELINK_P (decl));
19303 /* If we have resolved the name of a member declaration, check to
19304 see if the declaration is accessible. When the name resolves to
19305 set of overloaded functions, accessibility is checked when
19306 overload resolution is done.
19308 During an explicit instantiation, access is not checked at all,
19309 as per [temp.explicit]. */
19310 if (DECL_P (decl))
19311 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19313 return decl;
19316 /* Like cp_parser_lookup_name, but for use in the typical case where
19317 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19318 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19320 static tree
19321 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19323 return cp_parser_lookup_name (parser, name,
19324 none_type,
19325 /*is_template=*/false,
19326 /*is_namespace=*/false,
19327 /*check_dependency=*/true,
19328 /*ambiguous_decls=*/NULL,
19329 location);
19332 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19333 the current context, return the TYPE_DECL. If TAG_NAME_P is
19334 true, the DECL indicates the class being defined in a class-head,
19335 or declared in an elaborated-type-specifier.
19337 Otherwise, return DECL. */
19339 static tree
19340 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19342 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19343 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19345 struct A {
19346 template <typename T> struct B;
19349 template <typename T> struct A::B {};
19351 Similarly, in an elaborated-type-specifier:
19353 namespace N { struct X{}; }
19355 struct A {
19356 template <typename T> friend struct N::X;
19359 However, if the DECL refers to a class type, and we are in
19360 the scope of the class, then the name lookup automatically
19361 finds the TYPE_DECL created by build_self_reference rather
19362 than a TEMPLATE_DECL. For example, in:
19364 template <class T> struct S {
19365 S s;
19368 there is no need to handle such case. */
19370 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19371 return DECL_TEMPLATE_RESULT (decl);
19373 return decl;
19376 /* If too many, or too few, template-parameter lists apply to the
19377 declarator, issue an error message. Returns TRUE if all went well,
19378 and FALSE otherwise. */
19380 static bool
19381 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19382 cp_declarator *declarator,
19383 location_t declarator_location)
19385 unsigned num_templates;
19387 /* We haven't seen any classes that involve template parameters yet. */
19388 num_templates = 0;
19390 switch (declarator->kind)
19392 case cdk_id:
19393 if (declarator->u.id.qualifying_scope)
19395 tree scope;
19397 scope = declarator->u.id.qualifying_scope;
19399 while (scope && CLASS_TYPE_P (scope))
19401 /* You're supposed to have one `template <...>'
19402 for every template class, but you don't need one
19403 for a full specialization. For example:
19405 template <class T> struct S{};
19406 template <> struct S<int> { void f(); };
19407 void S<int>::f () {}
19409 is correct; there shouldn't be a `template <>' for
19410 the definition of `S<int>::f'. */
19411 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19412 /* If SCOPE does not have template information of any
19413 kind, then it is not a template, nor is it nested
19414 within a template. */
19415 break;
19416 if (explicit_class_specialization_p (scope))
19417 break;
19418 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19419 ++num_templates;
19421 scope = TYPE_CONTEXT (scope);
19424 else if (TREE_CODE (declarator->u.id.unqualified_name)
19425 == TEMPLATE_ID_EXPR)
19426 /* If the DECLARATOR has the form `X<y>' then it uses one
19427 additional level of template parameters. */
19428 ++num_templates;
19430 return cp_parser_check_template_parameters
19431 (parser, num_templates, declarator_location, declarator);
19434 case cdk_function:
19435 case cdk_array:
19436 case cdk_pointer:
19437 case cdk_reference:
19438 case cdk_ptrmem:
19439 return (cp_parser_check_declarator_template_parameters
19440 (parser, declarator->declarator, declarator_location));
19442 case cdk_error:
19443 return true;
19445 default:
19446 gcc_unreachable ();
19448 return false;
19451 /* NUM_TEMPLATES were used in the current declaration. If that is
19452 invalid, return FALSE and issue an error messages. Otherwise,
19453 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19454 declarator and we can print more accurate diagnostics. */
19456 static bool
19457 cp_parser_check_template_parameters (cp_parser* parser,
19458 unsigned num_templates,
19459 location_t location,
19460 cp_declarator *declarator)
19462 /* If there are the same number of template classes and parameter
19463 lists, that's OK. */
19464 if (parser->num_template_parameter_lists == num_templates)
19465 return true;
19466 /* If there are more, but only one more, then we are referring to a
19467 member template. That's OK too. */
19468 if (parser->num_template_parameter_lists == num_templates + 1)
19469 return true;
19470 /* If there are more template classes than parameter lists, we have
19471 something like:
19473 template <class T> void S<T>::R<T>::f (); */
19474 if (parser->num_template_parameter_lists < num_templates)
19476 if (declarator && !current_function_decl)
19477 error_at (location, "specializing member %<%T::%E%> "
19478 "requires %<template<>%> syntax",
19479 declarator->u.id.qualifying_scope,
19480 declarator->u.id.unqualified_name);
19481 else if (declarator)
19482 error_at (location, "invalid declaration of %<%T::%E%>",
19483 declarator->u.id.qualifying_scope,
19484 declarator->u.id.unqualified_name);
19485 else
19486 error_at (location, "too few template-parameter-lists");
19487 return false;
19489 /* Otherwise, there are too many template parameter lists. We have
19490 something like:
19492 template <class T> template <class U> void S::f(); */
19493 error_at (location, "too many template-parameter-lists");
19494 return false;
19497 /* Parse an optional `::' token indicating that the following name is
19498 from the global namespace. If so, PARSER->SCOPE is set to the
19499 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19500 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19501 Returns the new value of PARSER->SCOPE, if the `::' token is
19502 present, and NULL_TREE otherwise. */
19504 static tree
19505 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19507 cp_token *token;
19509 /* Peek at the next token. */
19510 token = cp_lexer_peek_token (parser->lexer);
19511 /* If we're looking at a `::' token then we're starting from the
19512 global namespace, not our current location. */
19513 if (token->type == CPP_SCOPE)
19515 /* Consume the `::' token. */
19516 cp_lexer_consume_token (parser->lexer);
19517 /* Set the SCOPE so that we know where to start the lookup. */
19518 parser->scope = global_namespace;
19519 parser->qualifying_scope = global_namespace;
19520 parser->object_scope = NULL_TREE;
19522 return parser->scope;
19524 else if (!current_scope_valid_p)
19526 parser->scope = NULL_TREE;
19527 parser->qualifying_scope = NULL_TREE;
19528 parser->object_scope = NULL_TREE;
19531 return NULL_TREE;
19534 /* Returns TRUE if the upcoming token sequence is the start of a
19535 constructor declarator. If FRIEND_P is true, the declarator is
19536 preceded by the `friend' specifier. */
19538 static bool
19539 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19541 bool constructor_p;
19542 tree nested_name_specifier;
19543 cp_token *next_token;
19545 /* The common case is that this is not a constructor declarator, so
19546 try to avoid doing lots of work if at all possible. It's not
19547 valid declare a constructor at function scope. */
19548 if (parser->in_function_body)
19549 return false;
19550 /* And only certain tokens can begin a constructor declarator. */
19551 next_token = cp_lexer_peek_token (parser->lexer);
19552 if (next_token->type != CPP_NAME
19553 && next_token->type != CPP_SCOPE
19554 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19555 && next_token->type != CPP_TEMPLATE_ID)
19556 return false;
19558 /* Parse tentatively; we are going to roll back all of the tokens
19559 consumed here. */
19560 cp_parser_parse_tentatively (parser);
19561 /* Assume that we are looking at a constructor declarator. */
19562 constructor_p = true;
19564 /* Look for the optional `::' operator. */
19565 cp_parser_global_scope_opt (parser,
19566 /*current_scope_valid_p=*/false);
19567 /* Look for the nested-name-specifier. */
19568 nested_name_specifier
19569 = (cp_parser_nested_name_specifier_opt (parser,
19570 /*typename_keyword_p=*/false,
19571 /*check_dependency_p=*/false,
19572 /*type_p=*/false,
19573 /*is_declaration=*/false));
19574 /* Outside of a class-specifier, there must be a
19575 nested-name-specifier. */
19576 if (!nested_name_specifier &&
19577 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19578 || friend_p))
19579 constructor_p = false;
19580 else if (nested_name_specifier == error_mark_node)
19581 constructor_p = false;
19583 /* If we have a class scope, this is easy; DR 147 says that S::S always
19584 names the constructor, and no other qualified name could. */
19585 if (constructor_p && nested_name_specifier
19586 && TYPE_P (nested_name_specifier))
19588 tree id = cp_parser_unqualified_id (parser,
19589 /*template_keyword_p=*/false,
19590 /*check_dependency_p=*/false,
19591 /*declarator_p=*/true,
19592 /*optional_p=*/false);
19593 if (is_overloaded_fn (id))
19594 id = DECL_NAME (get_first_fn (id));
19595 if (!constructor_name_p (id, nested_name_specifier))
19596 constructor_p = false;
19598 /* If we still think that this might be a constructor-declarator,
19599 look for a class-name. */
19600 else if (constructor_p)
19602 /* If we have:
19604 template <typename T> struct S {
19605 S();
19608 we must recognize that the nested `S' names a class. */
19609 tree type_decl;
19610 type_decl = cp_parser_class_name (parser,
19611 /*typename_keyword_p=*/false,
19612 /*template_keyword_p=*/false,
19613 none_type,
19614 /*check_dependency_p=*/false,
19615 /*class_head_p=*/false,
19616 /*is_declaration=*/false);
19617 /* If there was no class-name, then this is not a constructor. */
19618 constructor_p = !cp_parser_error_occurred (parser);
19620 /* If we're still considering a constructor, we have to see a `(',
19621 to begin the parameter-declaration-clause, followed by either a
19622 `)', an `...', or a decl-specifier. We need to check for a
19623 type-specifier to avoid being fooled into thinking that:
19625 S (f) (int);
19627 is a constructor. (It is actually a function named `f' that
19628 takes one parameter (of type `int') and returns a value of type
19629 `S'. */
19630 if (constructor_p
19631 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19632 constructor_p = false;
19634 if (constructor_p
19635 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19636 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19637 /* A parameter declaration begins with a decl-specifier,
19638 which is either the "attribute" keyword, a storage class
19639 specifier, or (usually) a type-specifier. */
19640 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19642 tree type;
19643 tree pushed_scope = NULL_TREE;
19644 unsigned saved_num_template_parameter_lists;
19646 /* Names appearing in the type-specifier should be looked up
19647 in the scope of the class. */
19648 if (current_class_type)
19649 type = NULL_TREE;
19650 else
19652 type = TREE_TYPE (type_decl);
19653 if (TREE_CODE (type) == TYPENAME_TYPE)
19655 type = resolve_typename_type (type,
19656 /*only_current_p=*/false);
19657 if (TREE_CODE (type) == TYPENAME_TYPE)
19659 cp_parser_abort_tentative_parse (parser);
19660 return false;
19663 pushed_scope = push_scope (type);
19666 /* Inside the constructor parameter list, surrounding
19667 template-parameter-lists do not apply. */
19668 saved_num_template_parameter_lists
19669 = parser->num_template_parameter_lists;
19670 parser->num_template_parameter_lists = 0;
19672 /* Look for the type-specifier. */
19673 cp_parser_type_specifier (parser,
19674 CP_PARSER_FLAGS_NONE,
19675 /*decl_specs=*/NULL,
19676 /*is_declarator=*/true,
19677 /*declares_class_or_enum=*/NULL,
19678 /*is_cv_qualifier=*/NULL);
19680 parser->num_template_parameter_lists
19681 = saved_num_template_parameter_lists;
19683 /* Leave the scope of the class. */
19684 if (pushed_scope)
19685 pop_scope (pushed_scope);
19687 constructor_p = !cp_parser_error_occurred (parser);
19691 /* We did not really want to consume any tokens. */
19692 cp_parser_abort_tentative_parse (parser);
19694 return constructor_p;
19697 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19698 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19699 they must be performed once we are in the scope of the function.
19701 Returns the function defined. */
19703 static tree
19704 cp_parser_function_definition_from_specifiers_and_declarator
19705 (cp_parser* parser,
19706 cp_decl_specifier_seq *decl_specifiers,
19707 tree attributes,
19708 const cp_declarator *declarator)
19710 tree fn;
19711 bool success_p;
19713 /* Begin the function-definition. */
19714 success_p = start_function (decl_specifiers, declarator, attributes);
19716 /* The things we're about to see are not directly qualified by any
19717 template headers we've seen thus far. */
19718 reset_specialization ();
19720 /* If there were names looked up in the decl-specifier-seq that we
19721 did not check, check them now. We must wait until we are in the
19722 scope of the function to perform the checks, since the function
19723 might be a friend. */
19724 perform_deferred_access_checks ();
19726 if (!success_p)
19728 /* Skip the entire function. */
19729 cp_parser_skip_to_end_of_block_or_statement (parser);
19730 fn = error_mark_node;
19732 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19734 /* Seen already, skip it. An error message has already been output. */
19735 cp_parser_skip_to_end_of_block_or_statement (parser);
19736 fn = current_function_decl;
19737 current_function_decl = NULL_TREE;
19738 /* If this is a function from a class, pop the nested class. */
19739 if (current_class_name)
19740 pop_nested_class ();
19742 else
19743 fn = cp_parser_function_definition_after_declarator (parser,
19744 /*inline_p=*/false);
19746 return fn;
19749 /* Parse the part of a function-definition that follows the
19750 declarator. INLINE_P is TRUE iff this function is an inline
19751 function defined within a class-specifier.
19753 Returns the function defined. */
19755 static tree
19756 cp_parser_function_definition_after_declarator (cp_parser* parser,
19757 bool inline_p)
19759 tree fn;
19760 bool ctor_initializer_p = false;
19761 bool saved_in_unbraced_linkage_specification_p;
19762 bool saved_in_function_body;
19763 unsigned saved_num_template_parameter_lists;
19764 cp_token *token;
19766 saved_in_function_body = parser->in_function_body;
19767 parser->in_function_body = true;
19768 /* If the next token is `return', then the code may be trying to
19769 make use of the "named return value" extension that G++ used to
19770 support. */
19771 token = cp_lexer_peek_token (parser->lexer);
19772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19774 /* Consume the `return' keyword. */
19775 cp_lexer_consume_token (parser->lexer);
19776 /* Look for the identifier that indicates what value is to be
19777 returned. */
19778 cp_parser_identifier (parser);
19779 /* Issue an error message. */
19780 error_at (token->location,
19781 "named return values are no longer supported");
19782 /* Skip tokens until we reach the start of the function body. */
19783 while (true)
19785 cp_token *token = cp_lexer_peek_token (parser->lexer);
19786 if (token->type == CPP_OPEN_BRACE
19787 || token->type == CPP_EOF
19788 || token->type == CPP_PRAGMA_EOL)
19789 break;
19790 cp_lexer_consume_token (parser->lexer);
19793 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19794 anything declared inside `f'. */
19795 saved_in_unbraced_linkage_specification_p
19796 = parser->in_unbraced_linkage_specification_p;
19797 parser->in_unbraced_linkage_specification_p = false;
19798 /* Inside the function, surrounding template-parameter-lists do not
19799 apply. */
19800 saved_num_template_parameter_lists
19801 = parser->num_template_parameter_lists;
19802 parser->num_template_parameter_lists = 0;
19804 start_lambda_scope (current_function_decl);
19806 /* If the next token is `try', then we are looking at a
19807 function-try-block. */
19808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19809 ctor_initializer_p = cp_parser_function_try_block (parser);
19810 /* A function-try-block includes the function-body, so we only do
19811 this next part if we're not processing a function-try-block. */
19812 else
19813 ctor_initializer_p
19814 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19816 finish_lambda_scope ();
19818 /* Finish the function. */
19819 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19820 (inline_p ? 2 : 0));
19821 /* Generate code for it, if necessary. */
19822 expand_or_defer_fn (fn);
19823 /* Restore the saved values. */
19824 parser->in_unbraced_linkage_specification_p
19825 = saved_in_unbraced_linkage_specification_p;
19826 parser->num_template_parameter_lists
19827 = saved_num_template_parameter_lists;
19828 parser->in_function_body = saved_in_function_body;
19830 return fn;
19833 /* Parse a template-declaration, assuming that the `export' (and
19834 `extern') keywords, if present, has already been scanned. MEMBER_P
19835 is as for cp_parser_template_declaration. */
19837 static void
19838 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19840 tree decl = NULL_TREE;
19841 VEC (deferred_access_check,gc) *checks;
19842 tree parameter_list;
19843 bool friend_p = false;
19844 bool need_lang_pop;
19845 cp_token *token;
19847 /* Look for the `template' keyword. */
19848 token = cp_lexer_peek_token (parser->lexer);
19849 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19850 return;
19852 /* And the `<'. */
19853 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19854 return;
19855 if (at_class_scope_p () && current_function_decl)
19857 /* 14.5.2.2 [temp.mem]
19859 A local class shall not have member templates. */
19860 error_at (token->location,
19861 "invalid declaration of member template in local class");
19862 cp_parser_skip_to_end_of_block_or_statement (parser);
19863 return;
19865 /* [temp]
19867 A template ... shall not have C linkage. */
19868 if (current_lang_name == lang_name_c)
19870 error_at (token->location, "template with C linkage");
19871 /* Give it C++ linkage to avoid confusing other parts of the
19872 front end. */
19873 push_lang_context (lang_name_cplusplus);
19874 need_lang_pop = true;
19876 else
19877 need_lang_pop = false;
19879 /* We cannot perform access checks on the template parameter
19880 declarations until we know what is being declared, just as we
19881 cannot check the decl-specifier list. */
19882 push_deferring_access_checks (dk_deferred);
19884 /* If the next token is `>', then we have an invalid
19885 specialization. Rather than complain about an invalid template
19886 parameter, issue an error message here. */
19887 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19889 cp_parser_error (parser, "invalid explicit specialization");
19890 begin_specialization ();
19891 parameter_list = NULL_TREE;
19893 else
19894 /* Parse the template parameters. */
19895 parameter_list = cp_parser_template_parameter_list (parser);
19897 /* Get the deferred access checks from the parameter list. These
19898 will be checked once we know what is being declared, as for a
19899 member template the checks must be performed in the scope of the
19900 class containing the member. */
19901 checks = get_deferred_access_checks ();
19903 /* Look for the `>'. */
19904 cp_parser_skip_to_end_of_template_parameter_list (parser);
19905 /* We just processed one more parameter list. */
19906 ++parser->num_template_parameter_lists;
19907 /* If the next token is `template', there are more template
19908 parameters. */
19909 if (cp_lexer_next_token_is_keyword (parser->lexer,
19910 RID_TEMPLATE))
19911 cp_parser_template_declaration_after_export (parser, member_p);
19912 else
19914 /* There are no access checks when parsing a template, as we do not
19915 know if a specialization will be a friend. */
19916 push_deferring_access_checks (dk_no_check);
19917 token = cp_lexer_peek_token (parser->lexer);
19918 decl = cp_parser_single_declaration (parser,
19919 checks,
19920 member_p,
19921 /*explicit_specialization_p=*/false,
19922 &friend_p);
19923 pop_deferring_access_checks ();
19925 /* If this is a member template declaration, let the front
19926 end know. */
19927 if (member_p && !friend_p && decl)
19929 if (TREE_CODE (decl) == TYPE_DECL)
19930 cp_parser_check_access_in_redeclaration (decl, token->location);
19932 decl = finish_member_template_decl (decl);
19934 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19935 make_friend_class (current_class_type, TREE_TYPE (decl),
19936 /*complain=*/true);
19938 /* We are done with the current parameter list. */
19939 --parser->num_template_parameter_lists;
19941 pop_deferring_access_checks ();
19943 /* Finish up. */
19944 finish_template_decl (parameter_list);
19946 /* Register member declarations. */
19947 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19948 finish_member_declaration (decl);
19949 /* For the erroneous case of a template with C linkage, we pushed an
19950 implicit C++ linkage scope; exit that scope now. */
19951 if (need_lang_pop)
19952 pop_lang_context ();
19953 /* If DECL is a function template, we must return to parse it later.
19954 (Even though there is no definition, there might be default
19955 arguments that need handling.) */
19956 if (member_p && decl
19957 && (TREE_CODE (decl) == FUNCTION_DECL
19958 || DECL_FUNCTION_TEMPLATE_P (decl)))
19959 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19962 /* Perform the deferred access checks from a template-parameter-list.
19963 CHECKS is a TREE_LIST of access checks, as returned by
19964 get_deferred_access_checks. */
19966 static void
19967 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19969 ++processing_template_parmlist;
19970 perform_access_checks (checks);
19971 --processing_template_parmlist;
19974 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19975 `function-definition' sequence. MEMBER_P is true, this declaration
19976 appears in a class scope.
19978 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19979 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19981 static tree
19982 cp_parser_single_declaration (cp_parser* parser,
19983 VEC (deferred_access_check,gc)* checks,
19984 bool member_p,
19985 bool explicit_specialization_p,
19986 bool* friend_p)
19988 int declares_class_or_enum;
19989 tree decl = NULL_TREE;
19990 cp_decl_specifier_seq decl_specifiers;
19991 bool function_definition_p = false;
19992 cp_token *decl_spec_token_start;
19994 /* This function is only used when processing a template
19995 declaration. */
19996 gcc_assert (innermost_scope_kind () == sk_template_parms
19997 || innermost_scope_kind () == sk_template_spec);
19999 /* Defer access checks until we know what is being declared. */
20000 push_deferring_access_checks (dk_deferred);
20002 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20003 alternative. */
20004 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20005 cp_parser_decl_specifier_seq (parser,
20006 CP_PARSER_FLAGS_OPTIONAL,
20007 &decl_specifiers,
20008 &declares_class_or_enum);
20009 if (friend_p)
20010 *friend_p = cp_parser_friend_p (&decl_specifiers);
20012 /* There are no template typedefs. */
20013 if (decl_specifiers.specs[(int) ds_typedef])
20015 error_at (decl_spec_token_start->location,
20016 "template declaration of %<typedef%>");
20017 decl = error_mark_node;
20020 /* Gather up the access checks that occurred the
20021 decl-specifier-seq. */
20022 stop_deferring_access_checks ();
20024 /* Check for the declaration of a template class. */
20025 if (declares_class_or_enum)
20027 if (cp_parser_declares_only_class_p (parser))
20029 decl = shadow_tag (&decl_specifiers);
20031 /* In this case:
20033 struct C {
20034 friend template <typename T> struct A<T>::B;
20037 A<T>::B will be represented by a TYPENAME_TYPE, and
20038 therefore not recognized by shadow_tag. */
20039 if (friend_p && *friend_p
20040 && !decl
20041 && decl_specifiers.type
20042 && TYPE_P (decl_specifiers.type))
20043 decl = decl_specifiers.type;
20045 if (decl && decl != error_mark_node)
20046 decl = TYPE_NAME (decl);
20047 else
20048 decl = error_mark_node;
20050 /* Perform access checks for template parameters. */
20051 cp_parser_perform_template_parameter_access_checks (checks);
20055 /* Complain about missing 'typename' or other invalid type names. */
20056 if (!decl_specifiers.any_type_specifiers_p)
20057 cp_parser_parse_and_diagnose_invalid_type_name (parser);
20059 /* If it's not a template class, try for a template function. If
20060 the next token is a `;', then this declaration does not declare
20061 anything. But, if there were errors in the decl-specifiers, then
20062 the error might well have come from an attempted class-specifier.
20063 In that case, there's no need to warn about a missing declarator. */
20064 if (!decl
20065 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20066 || decl_specifiers.type != error_mark_node))
20068 decl = cp_parser_init_declarator (parser,
20069 &decl_specifiers,
20070 checks,
20071 /*function_definition_allowed_p=*/true,
20072 member_p,
20073 declares_class_or_enum,
20074 &function_definition_p);
20076 /* 7.1.1-1 [dcl.stc]
20078 A storage-class-specifier shall not be specified in an explicit
20079 specialization... */
20080 if (decl
20081 && explicit_specialization_p
20082 && decl_specifiers.storage_class != sc_none)
20084 error_at (decl_spec_token_start->location,
20085 "explicit template specialization cannot have a storage class");
20086 decl = error_mark_node;
20090 pop_deferring_access_checks ();
20092 /* Clear any current qualification; whatever comes next is the start
20093 of something new. */
20094 parser->scope = NULL_TREE;
20095 parser->qualifying_scope = NULL_TREE;
20096 parser->object_scope = NULL_TREE;
20097 /* Look for a trailing `;' after the declaration. */
20098 if (!function_definition_p
20099 && (decl == error_mark_node
20100 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20101 cp_parser_skip_to_end_of_block_or_statement (parser);
20103 return decl;
20106 /* Parse a cast-expression that is not the operand of a unary "&". */
20108 static tree
20109 cp_parser_simple_cast_expression (cp_parser *parser)
20111 return cp_parser_cast_expression (parser, /*address_p=*/false,
20112 /*cast_p=*/false, NULL);
20115 /* Parse a functional cast to TYPE. Returns an expression
20116 representing the cast. */
20118 static tree
20119 cp_parser_functional_cast (cp_parser* parser, tree type)
20121 VEC(tree,gc) *vec;
20122 tree expression_list;
20123 tree cast;
20124 bool nonconst_p;
20126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20128 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20129 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20130 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20131 if (TREE_CODE (type) == TYPE_DECL)
20132 type = TREE_TYPE (type);
20133 return finish_compound_literal (type, expression_list);
20137 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20138 /*cast_p=*/true,
20139 /*allow_expansion_p=*/true,
20140 /*non_constant_p=*/NULL);
20141 if (vec == NULL)
20142 expression_list = error_mark_node;
20143 else
20145 expression_list = build_tree_list_vec (vec);
20146 release_tree_vector (vec);
20149 cast = build_functional_cast (type, expression_list,
20150 tf_warning_or_error);
20151 /* [expr.const]/1: In an integral constant expression "only type
20152 conversions to integral or enumeration type can be used". */
20153 if (TREE_CODE (type) == TYPE_DECL)
20154 type = TREE_TYPE (type);
20155 if (cast != error_mark_node
20156 && !cast_valid_in_integral_constant_expression_p (type)
20157 && cp_parser_non_integral_constant_expression (parser,
20158 NIC_CONSTRUCTOR))
20159 return error_mark_node;
20160 return cast;
20163 /* Save the tokens that make up the body of a member function defined
20164 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20165 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20166 specifiers applied to the declaration. Returns the FUNCTION_DECL
20167 for the member function. */
20169 static tree
20170 cp_parser_save_member_function_body (cp_parser* parser,
20171 cp_decl_specifier_seq *decl_specifiers,
20172 cp_declarator *declarator,
20173 tree attributes)
20175 cp_token *first;
20176 cp_token *last;
20177 tree fn;
20179 /* Create the FUNCTION_DECL. */
20180 fn = grokmethod (decl_specifiers, declarator, attributes);
20181 /* If something went badly wrong, bail out now. */
20182 if (fn == error_mark_node)
20184 /* If there's a function-body, skip it. */
20185 if (cp_parser_token_starts_function_definition_p
20186 (cp_lexer_peek_token (parser->lexer)))
20187 cp_parser_skip_to_end_of_block_or_statement (parser);
20188 return error_mark_node;
20191 /* Remember it, if there default args to post process. */
20192 cp_parser_save_default_args (parser, fn);
20194 /* Save away the tokens that make up the body of the
20195 function. */
20196 first = parser->lexer->next_token;
20197 /* We can have braced-init-list mem-initializers before the fn body. */
20198 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20200 cp_lexer_consume_token (parser->lexer);
20201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20202 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20204 /* cache_group will stop after an un-nested { } pair, too. */
20205 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20206 break;
20208 /* variadic mem-inits have ... after the ')'. */
20209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20210 cp_lexer_consume_token (parser->lexer);
20213 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20214 /* Handle function try blocks. */
20215 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20216 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20217 last = parser->lexer->next_token;
20219 /* Save away the inline definition; we will process it when the
20220 class is complete. */
20221 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20222 DECL_PENDING_INLINE_P (fn) = 1;
20224 /* We need to know that this was defined in the class, so that
20225 friend templates are handled correctly. */
20226 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20228 /* Add FN to the queue of functions to be parsed later. */
20229 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20231 return fn;
20234 /* Parse a template-argument-list, as well as the trailing ">" (but
20235 not the opening ">"). See cp_parser_template_argument_list for the
20236 return value. */
20238 static tree
20239 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20241 tree arguments;
20242 tree saved_scope;
20243 tree saved_qualifying_scope;
20244 tree saved_object_scope;
20245 bool saved_greater_than_is_operator_p;
20246 int saved_unevaluated_operand;
20247 int saved_inhibit_evaluation_warnings;
20249 /* [temp.names]
20251 When parsing a template-id, the first non-nested `>' is taken as
20252 the end of the template-argument-list rather than a greater-than
20253 operator. */
20254 saved_greater_than_is_operator_p
20255 = parser->greater_than_is_operator_p;
20256 parser->greater_than_is_operator_p = false;
20257 /* Parsing the argument list may modify SCOPE, so we save it
20258 here. */
20259 saved_scope = parser->scope;
20260 saved_qualifying_scope = parser->qualifying_scope;
20261 saved_object_scope = parser->object_scope;
20262 /* We need to evaluate the template arguments, even though this
20263 template-id may be nested within a "sizeof". */
20264 saved_unevaluated_operand = cp_unevaluated_operand;
20265 cp_unevaluated_operand = 0;
20266 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20267 c_inhibit_evaluation_warnings = 0;
20268 /* Parse the template-argument-list itself. */
20269 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20270 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20271 arguments = NULL_TREE;
20272 else
20273 arguments = cp_parser_template_argument_list (parser);
20274 /* Look for the `>' that ends the template-argument-list. If we find
20275 a '>>' instead, it's probably just a typo. */
20276 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20278 if (cxx_dialect != cxx98)
20280 /* In C++0x, a `>>' in a template argument list or cast
20281 expression is considered to be two separate `>'
20282 tokens. So, change the current token to a `>', but don't
20283 consume it: it will be consumed later when the outer
20284 template argument list (or cast expression) is parsed.
20285 Note that this replacement of `>' for `>>' is necessary
20286 even if we are parsing tentatively: in the tentative
20287 case, after calling
20288 cp_parser_enclosed_template_argument_list we will always
20289 throw away all of the template arguments and the first
20290 closing `>', either because the template argument list
20291 was erroneous or because we are replacing those tokens
20292 with a CPP_TEMPLATE_ID token. The second `>' (which will
20293 not have been thrown away) is needed either to close an
20294 outer template argument list or to complete a new-style
20295 cast. */
20296 cp_token *token = cp_lexer_peek_token (parser->lexer);
20297 token->type = CPP_GREATER;
20299 else if (!saved_greater_than_is_operator_p)
20301 /* If we're in a nested template argument list, the '>>' has
20302 to be a typo for '> >'. We emit the error message, but we
20303 continue parsing and we push a '>' as next token, so that
20304 the argument list will be parsed correctly. Note that the
20305 global source location is still on the token before the
20306 '>>', so we need to say explicitly where we want it. */
20307 cp_token *token = cp_lexer_peek_token (parser->lexer);
20308 error_at (token->location, "%<>>%> should be %<> >%> "
20309 "within a nested template argument list");
20311 token->type = CPP_GREATER;
20313 else
20315 /* If this is not a nested template argument list, the '>>'
20316 is a typo for '>'. Emit an error message and continue.
20317 Same deal about the token location, but here we can get it
20318 right by consuming the '>>' before issuing the diagnostic. */
20319 cp_token *token = cp_lexer_consume_token (parser->lexer);
20320 error_at (token->location,
20321 "spurious %<>>%>, use %<>%> to terminate "
20322 "a template argument list");
20325 else
20326 cp_parser_skip_to_end_of_template_parameter_list (parser);
20327 /* The `>' token might be a greater-than operator again now. */
20328 parser->greater_than_is_operator_p
20329 = saved_greater_than_is_operator_p;
20330 /* Restore the SAVED_SCOPE. */
20331 parser->scope = saved_scope;
20332 parser->qualifying_scope = saved_qualifying_scope;
20333 parser->object_scope = saved_object_scope;
20334 cp_unevaluated_operand = saved_unevaluated_operand;
20335 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20337 return arguments;
20340 /* MEMBER_FUNCTION is a member function, or a friend. If default
20341 arguments, or the body of the function have not yet been parsed,
20342 parse them now. */
20344 static void
20345 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20347 /* If this member is a template, get the underlying
20348 FUNCTION_DECL. */
20349 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20350 member_function = DECL_TEMPLATE_RESULT (member_function);
20352 /* There should not be any class definitions in progress at this
20353 point; the bodies of members are only parsed outside of all class
20354 definitions. */
20355 gcc_assert (parser->num_classes_being_defined == 0);
20356 /* While we're parsing the member functions we might encounter more
20357 classes. We want to handle them right away, but we don't want
20358 them getting mixed up with functions that are currently in the
20359 queue. */
20360 push_unparsed_function_queues (parser);
20362 /* Make sure that any template parameters are in scope. */
20363 maybe_begin_member_template_processing (member_function);
20365 /* If the body of the function has not yet been parsed, parse it
20366 now. */
20367 if (DECL_PENDING_INLINE_P (member_function))
20369 tree function_scope;
20370 cp_token_cache *tokens;
20372 /* The function is no longer pending; we are processing it. */
20373 tokens = DECL_PENDING_INLINE_INFO (member_function);
20374 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20375 DECL_PENDING_INLINE_P (member_function) = 0;
20377 /* If this is a local class, enter the scope of the containing
20378 function. */
20379 function_scope = current_function_decl;
20380 if (function_scope)
20381 push_function_context ();
20383 /* Push the body of the function onto the lexer stack. */
20384 cp_parser_push_lexer_for_tokens (parser, tokens);
20386 /* Let the front end know that we going to be defining this
20387 function. */
20388 start_preparsed_function (member_function, NULL_TREE,
20389 SF_PRE_PARSED | SF_INCLASS_INLINE);
20391 /* Don't do access checking if it is a templated function. */
20392 if (processing_template_decl)
20393 push_deferring_access_checks (dk_no_check);
20395 /* Now, parse the body of the function. */
20396 cp_parser_function_definition_after_declarator (parser,
20397 /*inline_p=*/true);
20399 if (processing_template_decl)
20400 pop_deferring_access_checks ();
20402 /* Leave the scope of the containing function. */
20403 if (function_scope)
20404 pop_function_context ();
20405 cp_parser_pop_lexer (parser);
20408 /* Remove any template parameters from the symbol table. */
20409 maybe_end_member_template_processing ();
20411 /* Restore the queue. */
20412 pop_unparsed_function_queues (parser);
20415 /* If DECL contains any default args, remember it on the unparsed
20416 functions queue. */
20418 static void
20419 cp_parser_save_default_args (cp_parser* parser, tree decl)
20421 tree probe;
20423 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20424 probe;
20425 probe = TREE_CHAIN (probe))
20426 if (TREE_PURPOSE (probe))
20428 cp_default_arg_entry *entry
20429 = VEC_safe_push (cp_default_arg_entry, gc,
20430 unparsed_funs_with_default_args, NULL);
20431 entry->class_type = current_class_type;
20432 entry->decl = decl;
20433 break;
20437 /* FN is a FUNCTION_DECL which may contains a parameter with an
20438 unparsed DEFAULT_ARG. Parse the default args now. This function
20439 assumes that the current scope is the scope in which the default
20440 argument should be processed. */
20442 static void
20443 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20445 bool saved_local_variables_forbidden_p;
20446 tree parm, parmdecl;
20448 /* While we're parsing the default args, we might (due to the
20449 statement expression extension) encounter more classes. We want
20450 to handle them right away, but we don't want them getting mixed
20451 up with default args that are currently in the queue. */
20452 push_unparsed_function_queues (parser);
20454 /* Local variable names (and the `this' keyword) may not appear
20455 in a default argument. */
20456 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20457 parser->local_variables_forbidden_p = true;
20459 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20460 parmdecl = DECL_ARGUMENTS (fn);
20461 parm && parm != void_list_node;
20462 parm = TREE_CHAIN (parm),
20463 parmdecl = DECL_CHAIN (parmdecl))
20465 cp_token_cache *tokens;
20466 tree default_arg = TREE_PURPOSE (parm);
20467 tree parsed_arg;
20468 VEC(tree,gc) *insts;
20469 tree copy;
20470 unsigned ix;
20472 if (!default_arg)
20473 continue;
20475 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20476 /* This can happen for a friend declaration for a function
20477 already declared with default arguments. */
20478 continue;
20480 /* Push the saved tokens for the default argument onto the parser's
20481 lexer stack. */
20482 tokens = DEFARG_TOKENS (default_arg);
20483 cp_parser_push_lexer_for_tokens (parser, tokens);
20485 start_lambda_scope (parmdecl);
20487 /* Parse the assignment-expression. */
20488 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20489 if (parsed_arg == error_mark_node)
20491 cp_parser_pop_lexer (parser);
20492 continue;
20495 if (!processing_template_decl)
20496 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20498 TREE_PURPOSE (parm) = parsed_arg;
20500 /* Update any instantiations we've already created. */
20501 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20502 VEC_iterate (tree, insts, ix, copy); ix++)
20503 TREE_PURPOSE (copy) = parsed_arg;
20505 finish_lambda_scope ();
20507 /* If the token stream has not been completely used up, then
20508 there was extra junk after the end of the default
20509 argument. */
20510 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20511 cp_parser_error (parser, "expected %<,%>");
20513 /* Revert to the main lexer. */
20514 cp_parser_pop_lexer (parser);
20517 /* Make sure no default arg is missing. */
20518 check_default_args (fn);
20520 /* Restore the state of local_variables_forbidden_p. */
20521 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20523 /* Restore the queue. */
20524 pop_unparsed_function_queues (parser);
20527 /* Parse the operand of `sizeof' (or a similar operator). Returns
20528 either a TYPE or an expression, depending on the form of the
20529 input. The KEYWORD indicates which kind of expression we have
20530 encountered. */
20532 static tree
20533 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20535 tree expr = NULL_TREE;
20536 const char *saved_message;
20537 char *tmp;
20538 bool saved_integral_constant_expression_p;
20539 bool saved_non_integral_constant_expression_p;
20540 bool pack_expansion_p = false;
20542 /* Types cannot be defined in a `sizeof' expression. Save away the
20543 old message. */
20544 saved_message = parser->type_definition_forbidden_message;
20545 /* And create the new one. */
20546 tmp = concat ("types may not be defined in %<",
20547 IDENTIFIER_POINTER (ridpointers[keyword]),
20548 "%> expressions", NULL);
20549 parser->type_definition_forbidden_message = tmp;
20551 /* The restrictions on constant-expressions do not apply inside
20552 sizeof expressions. */
20553 saved_integral_constant_expression_p
20554 = parser->integral_constant_expression_p;
20555 saved_non_integral_constant_expression_p
20556 = parser->non_integral_constant_expression_p;
20557 parser->integral_constant_expression_p = false;
20559 /* If it's a `...', then we are computing the length of a parameter
20560 pack. */
20561 if (keyword == RID_SIZEOF
20562 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20564 /* Consume the `...'. */
20565 cp_lexer_consume_token (parser->lexer);
20566 maybe_warn_variadic_templates ();
20568 /* Note that this is an expansion. */
20569 pack_expansion_p = true;
20572 /* Do not actually evaluate the expression. */
20573 ++cp_unevaluated_operand;
20574 ++c_inhibit_evaluation_warnings;
20575 /* If it's a `(', then we might be looking at the type-id
20576 construction. */
20577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20579 tree type;
20580 bool saved_in_type_id_in_expr_p;
20582 /* We can't be sure yet whether we're looking at a type-id or an
20583 expression. */
20584 cp_parser_parse_tentatively (parser);
20585 /* Consume the `('. */
20586 cp_lexer_consume_token (parser->lexer);
20587 /* Parse the type-id. */
20588 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20589 parser->in_type_id_in_expr_p = true;
20590 type = cp_parser_type_id (parser);
20591 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20592 /* Now, look for the trailing `)'. */
20593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20594 /* If all went well, then we're done. */
20595 if (cp_parser_parse_definitely (parser))
20597 cp_decl_specifier_seq decl_specs;
20599 /* Build a trivial decl-specifier-seq. */
20600 clear_decl_specs (&decl_specs);
20601 decl_specs.type = type;
20603 /* Call grokdeclarator to figure out what type this is. */
20604 expr = grokdeclarator (NULL,
20605 &decl_specs,
20606 TYPENAME,
20607 /*initialized=*/0,
20608 /*attrlist=*/NULL);
20612 /* If the type-id production did not work out, then we must be
20613 looking at the unary-expression production. */
20614 if (!expr)
20615 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20616 /*cast_p=*/false, NULL);
20618 if (pack_expansion_p)
20619 /* Build a pack expansion. */
20620 expr = make_pack_expansion (expr);
20622 /* Go back to evaluating expressions. */
20623 --cp_unevaluated_operand;
20624 --c_inhibit_evaluation_warnings;
20626 /* Free the message we created. */
20627 free (tmp);
20628 /* And restore the old one. */
20629 parser->type_definition_forbidden_message = saved_message;
20630 parser->integral_constant_expression_p
20631 = saved_integral_constant_expression_p;
20632 parser->non_integral_constant_expression_p
20633 = saved_non_integral_constant_expression_p;
20635 return expr;
20638 /* If the current declaration has no declarator, return true. */
20640 static bool
20641 cp_parser_declares_only_class_p (cp_parser *parser)
20643 /* If the next token is a `;' or a `,' then there is no
20644 declarator. */
20645 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20646 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20649 /* Update the DECL_SPECS to reflect the storage class indicated by
20650 KEYWORD. */
20652 static void
20653 cp_parser_set_storage_class (cp_parser *parser,
20654 cp_decl_specifier_seq *decl_specs,
20655 enum rid keyword,
20656 location_t location)
20658 cp_storage_class storage_class;
20660 if (parser->in_unbraced_linkage_specification_p)
20662 error_at (location, "invalid use of %qD in linkage specification",
20663 ridpointers[keyword]);
20664 return;
20666 else if (decl_specs->storage_class != sc_none)
20668 decl_specs->conflicting_specifiers_p = true;
20669 return;
20672 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20673 && decl_specs->specs[(int) ds_thread])
20675 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20676 decl_specs->specs[(int) ds_thread] = 0;
20679 switch (keyword)
20681 case RID_AUTO:
20682 storage_class = sc_auto;
20683 break;
20684 case RID_REGISTER:
20685 storage_class = sc_register;
20686 break;
20687 case RID_STATIC:
20688 storage_class = sc_static;
20689 break;
20690 case RID_EXTERN:
20691 storage_class = sc_extern;
20692 break;
20693 case RID_MUTABLE:
20694 storage_class = sc_mutable;
20695 break;
20696 default:
20697 gcc_unreachable ();
20699 decl_specs->storage_class = storage_class;
20701 /* A storage class specifier cannot be applied alongside a typedef
20702 specifier. If there is a typedef specifier present then set
20703 conflicting_specifiers_p which will trigger an error later
20704 on in grokdeclarator. */
20705 if (decl_specs->specs[(int)ds_typedef])
20706 decl_specs->conflicting_specifiers_p = true;
20709 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20710 is true, the type is a user-defined type; otherwise it is a
20711 built-in type specified by a keyword. */
20713 static void
20714 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20715 tree type_spec,
20716 location_t location,
20717 bool user_defined_p)
20719 decl_specs->any_specifiers_p = true;
20721 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20722 (with, for example, in "typedef int wchar_t;") we remember that
20723 this is what happened. In system headers, we ignore these
20724 declarations so that G++ can work with system headers that are not
20725 C++-safe. */
20726 if (decl_specs->specs[(int) ds_typedef]
20727 && !user_defined_p
20728 && (type_spec == boolean_type_node
20729 || type_spec == char16_type_node
20730 || type_spec == char32_type_node
20731 || type_spec == wchar_type_node)
20732 && (decl_specs->type
20733 || decl_specs->specs[(int) ds_long]
20734 || decl_specs->specs[(int) ds_short]
20735 || decl_specs->specs[(int) ds_unsigned]
20736 || decl_specs->specs[(int) ds_signed]))
20738 decl_specs->redefined_builtin_type = type_spec;
20739 if (!decl_specs->type)
20741 decl_specs->type = type_spec;
20742 decl_specs->user_defined_type_p = false;
20743 decl_specs->type_location = location;
20746 else if (decl_specs->type)
20747 decl_specs->multiple_types_p = true;
20748 else
20750 decl_specs->type = type_spec;
20751 decl_specs->user_defined_type_p = user_defined_p;
20752 decl_specs->redefined_builtin_type = NULL_TREE;
20753 decl_specs->type_location = location;
20757 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20758 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20760 static bool
20761 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20763 return decl_specifiers->specs[(int) ds_friend] != 0;
20766 /* Issue an error message indicating that TOKEN_DESC was expected.
20767 If KEYWORD is true, it indicated this function is called by
20768 cp_parser_require_keword and the required token can only be
20769 a indicated keyword. */
20771 static void
20772 cp_parser_required_error (cp_parser *parser,
20773 required_token token_desc,
20774 bool keyword)
20776 switch (token_desc)
20778 case RT_NEW:
20779 cp_parser_error (parser, "expected %<new%>");
20780 return;
20781 case RT_DELETE:
20782 cp_parser_error (parser, "expected %<delete%>");
20783 return;
20784 case RT_RETURN:
20785 cp_parser_error (parser, "expected %<return%>");
20786 return;
20787 case RT_WHILE:
20788 cp_parser_error (parser, "expected %<while%>");
20789 return;
20790 case RT_EXTERN:
20791 cp_parser_error (parser, "expected %<extern%>");
20792 return;
20793 case RT_STATIC_ASSERT:
20794 cp_parser_error (parser, "expected %<static_assert%>");
20795 return;
20796 case RT_DECLTYPE:
20797 cp_parser_error (parser, "expected %<decltype%>");
20798 return;
20799 case RT_OPERATOR:
20800 cp_parser_error (parser, "expected %<operator%>");
20801 return;
20802 case RT_CLASS:
20803 cp_parser_error (parser, "expected %<class%>");
20804 return;
20805 case RT_TEMPLATE:
20806 cp_parser_error (parser, "expected %<template%>");
20807 return;
20808 case RT_NAMESPACE:
20809 cp_parser_error (parser, "expected %<namespace%>");
20810 return;
20811 case RT_USING:
20812 cp_parser_error (parser, "expected %<using%>");
20813 return;
20814 case RT_ASM:
20815 cp_parser_error (parser, "expected %<asm%>");
20816 return;
20817 case RT_TRY:
20818 cp_parser_error (parser, "expected %<try%>");
20819 return;
20820 case RT_CATCH:
20821 cp_parser_error (parser, "expected %<catch%>");
20822 return;
20823 case RT_THROW:
20824 cp_parser_error (parser, "expected %<throw%>");
20825 return;
20826 case RT_LABEL:
20827 cp_parser_error (parser, "expected %<__label__%>");
20828 return;
20829 case RT_AT_TRY:
20830 cp_parser_error (parser, "expected %<@try%>");
20831 return;
20832 case RT_AT_SYNCHRONIZED:
20833 cp_parser_error (parser, "expected %<@synchronized%>");
20834 return;
20835 case RT_AT_THROW:
20836 cp_parser_error (parser, "expected %<@throw%>");
20837 return;
20838 default:
20839 break;
20841 if (!keyword)
20843 switch (token_desc)
20845 case RT_SEMICOLON:
20846 cp_parser_error (parser, "expected %<;%>");
20847 return;
20848 case RT_OPEN_PAREN:
20849 cp_parser_error (parser, "expected %<(%>");
20850 return;
20851 case RT_CLOSE_BRACE:
20852 cp_parser_error (parser, "expected %<}%>");
20853 return;
20854 case RT_OPEN_BRACE:
20855 cp_parser_error (parser, "expected %<{%>");
20856 return;
20857 case RT_CLOSE_SQUARE:
20858 cp_parser_error (parser, "expected %<]%>");
20859 return;
20860 case RT_OPEN_SQUARE:
20861 cp_parser_error (parser, "expected %<[%>");
20862 return;
20863 case RT_COMMA:
20864 cp_parser_error (parser, "expected %<,%>");
20865 return;
20866 case RT_SCOPE:
20867 cp_parser_error (parser, "expected %<::%>");
20868 return;
20869 case RT_LESS:
20870 cp_parser_error (parser, "expected %<<%>");
20871 return;
20872 case RT_GREATER:
20873 cp_parser_error (parser, "expected %<>%>");
20874 return;
20875 case RT_EQ:
20876 cp_parser_error (parser, "expected %<=%>");
20877 return;
20878 case RT_ELLIPSIS:
20879 cp_parser_error (parser, "expected %<...%>");
20880 return;
20881 case RT_MULT:
20882 cp_parser_error (parser, "expected %<*%>");
20883 return;
20884 case RT_COMPL:
20885 cp_parser_error (parser, "expected %<~%>");
20886 return;
20887 case RT_COLON:
20888 cp_parser_error (parser, "expected %<:%>");
20889 return;
20890 case RT_COLON_SCOPE:
20891 cp_parser_error (parser, "expected %<:%> or %<::%>");
20892 return;
20893 case RT_CLOSE_PAREN:
20894 cp_parser_error (parser, "expected %<)%>");
20895 return;
20896 case RT_COMMA_CLOSE_PAREN:
20897 cp_parser_error (parser, "expected %<,%> or %<)%>");
20898 return;
20899 case RT_PRAGMA_EOL:
20900 cp_parser_error (parser, "expected end of line");
20901 return;
20902 case RT_NAME:
20903 cp_parser_error (parser, "expected identifier");
20904 return;
20905 case RT_SELECT:
20906 cp_parser_error (parser, "expected selection-statement");
20907 return;
20908 case RT_INTERATION:
20909 cp_parser_error (parser, "expected iteration-statement");
20910 return;
20911 case RT_JUMP:
20912 cp_parser_error (parser, "expected jump-statement");
20913 return;
20914 case RT_CLASS_KEY:
20915 cp_parser_error (parser, "expected class-key");
20916 return;
20917 case RT_CLASS_TYPENAME_TEMPLATE:
20918 cp_parser_error (parser,
20919 "expected %<class%>, %<typename%>, or %<template%>");
20920 return;
20921 default:
20922 gcc_unreachable ();
20925 else
20926 gcc_unreachable ();
20931 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20932 issue an error message indicating that TOKEN_DESC was expected.
20934 Returns the token consumed, if the token had the appropriate type.
20935 Otherwise, returns NULL. */
20937 static cp_token *
20938 cp_parser_require (cp_parser* parser,
20939 enum cpp_ttype type,
20940 required_token token_desc)
20942 if (cp_lexer_next_token_is (parser->lexer, type))
20943 return cp_lexer_consume_token (parser->lexer);
20944 else
20946 /* Output the MESSAGE -- unless we're parsing tentatively. */
20947 if (!cp_parser_simulate_error (parser))
20948 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20949 return NULL;
20953 /* An error message is produced if the next token is not '>'.
20954 All further tokens are skipped until the desired token is
20955 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20957 static void
20958 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20960 /* Current level of '< ... >'. */
20961 unsigned level = 0;
20962 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20963 unsigned nesting_depth = 0;
20965 /* Are we ready, yet? If not, issue error message. */
20966 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20967 return;
20969 /* Skip tokens until the desired token is found. */
20970 while (true)
20972 /* Peek at the next token. */
20973 switch (cp_lexer_peek_token (parser->lexer)->type)
20975 case CPP_LESS:
20976 if (!nesting_depth)
20977 ++level;
20978 break;
20980 case CPP_RSHIFT:
20981 if (cxx_dialect == cxx98)
20982 /* C++0x views the `>>' operator as two `>' tokens, but
20983 C++98 does not. */
20984 break;
20985 else if (!nesting_depth && level-- == 0)
20987 /* We've hit a `>>' where the first `>' closes the
20988 template argument list, and the second `>' is
20989 spurious. Just consume the `>>' and stop; we've
20990 already produced at least one error. */
20991 cp_lexer_consume_token (parser->lexer);
20992 return;
20994 /* Fall through for C++0x, so we handle the second `>' in
20995 the `>>'. */
20997 case CPP_GREATER:
20998 if (!nesting_depth && level-- == 0)
21000 /* We've reached the token we want, consume it and stop. */
21001 cp_lexer_consume_token (parser->lexer);
21002 return;
21004 break;
21006 case CPP_OPEN_PAREN:
21007 case CPP_OPEN_SQUARE:
21008 ++nesting_depth;
21009 break;
21011 case CPP_CLOSE_PAREN:
21012 case CPP_CLOSE_SQUARE:
21013 if (nesting_depth-- == 0)
21014 return;
21015 break;
21017 case CPP_EOF:
21018 case CPP_PRAGMA_EOL:
21019 case CPP_SEMICOLON:
21020 case CPP_OPEN_BRACE:
21021 case CPP_CLOSE_BRACE:
21022 /* The '>' was probably forgotten, don't look further. */
21023 return;
21025 default:
21026 break;
21029 /* Consume this token. */
21030 cp_lexer_consume_token (parser->lexer);
21034 /* If the next token is the indicated keyword, consume it. Otherwise,
21035 issue an error message indicating that TOKEN_DESC was expected.
21037 Returns the token consumed, if the token had the appropriate type.
21038 Otherwise, returns NULL. */
21040 static cp_token *
21041 cp_parser_require_keyword (cp_parser* parser,
21042 enum rid keyword,
21043 required_token token_desc)
21045 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21047 if (token && token->keyword != keyword)
21049 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21050 return NULL;
21053 return token;
21056 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21057 function-definition. */
21059 static bool
21060 cp_parser_token_starts_function_definition_p (cp_token* token)
21062 return (/* An ordinary function-body begins with an `{'. */
21063 token->type == CPP_OPEN_BRACE
21064 /* A ctor-initializer begins with a `:'. */
21065 || token->type == CPP_COLON
21066 /* A function-try-block begins with `try'. */
21067 || token->keyword == RID_TRY
21068 /* The named return value extension begins with `return'. */
21069 || token->keyword == RID_RETURN);
21072 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21073 definition. */
21075 static bool
21076 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21078 cp_token *token;
21080 token = cp_lexer_peek_token (parser->lexer);
21081 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21084 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21085 C++0x) ending a template-argument. */
21087 static bool
21088 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21090 cp_token *token;
21092 token = cp_lexer_peek_token (parser->lexer);
21093 return (token->type == CPP_COMMA
21094 || token->type == CPP_GREATER
21095 || token->type == CPP_ELLIPSIS
21096 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21099 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21100 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21102 static bool
21103 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21104 size_t n)
21106 cp_token *token;
21108 token = cp_lexer_peek_nth_token (parser->lexer, n);
21109 if (token->type == CPP_LESS)
21110 return true;
21111 /* Check for the sequence `<::' in the original code. It would be lexed as
21112 `[:', where `[' is a digraph, and there is no whitespace before
21113 `:'. */
21114 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21116 cp_token *token2;
21117 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21118 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21119 return true;
21121 return false;
21124 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21125 or none_type otherwise. */
21127 static enum tag_types
21128 cp_parser_token_is_class_key (cp_token* token)
21130 switch (token->keyword)
21132 case RID_CLASS:
21133 return class_type;
21134 case RID_STRUCT:
21135 return record_type;
21136 case RID_UNION:
21137 return union_type;
21139 default:
21140 return none_type;
21144 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21146 static void
21147 cp_parser_check_class_key (enum tag_types class_key, tree type)
21149 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21150 permerror (input_location, "%qs tag used in naming %q#T",
21151 class_key == union_type ? "union"
21152 : class_key == record_type ? "struct" : "class",
21153 type);
21156 /* Issue an error message if DECL is redeclared with different
21157 access than its original declaration [class.access.spec/3].
21158 This applies to nested classes and nested class templates.
21159 [class.mem/1]. */
21161 static void
21162 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21164 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21165 return;
21167 if ((TREE_PRIVATE (decl)
21168 != (current_access_specifier == access_private_node))
21169 || (TREE_PROTECTED (decl)
21170 != (current_access_specifier == access_protected_node)))
21171 error_at (location, "%qD redeclared with different access", decl);
21174 /* Look for the `template' keyword, as a syntactic disambiguator.
21175 Return TRUE iff it is present, in which case it will be
21176 consumed. */
21178 static bool
21179 cp_parser_optional_template_keyword (cp_parser *parser)
21181 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21183 /* The `template' keyword can only be used within templates;
21184 outside templates the parser can always figure out what is a
21185 template and what is not. */
21186 if (!processing_template_decl)
21188 cp_token *token = cp_lexer_peek_token (parser->lexer);
21189 error_at (token->location,
21190 "%<template%> (as a disambiguator) is only allowed "
21191 "within templates");
21192 /* If this part of the token stream is rescanned, the same
21193 error message would be generated. So, we purge the token
21194 from the stream. */
21195 cp_lexer_purge_token (parser->lexer);
21196 return false;
21198 else
21200 /* Consume the `template' keyword. */
21201 cp_lexer_consume_token (parser->lexer);
21202 return true;
21206 return false;
21209 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21210 set PARSER->SCOPE, and perform other related actions. */
21212 static void
21213 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21215 int i;
21216 struct tree_check *check_value;
21217 deferred_access_check *chk;
21218 VEC (deferred_access_check,gc) *checks;
21220 /* Get the stored value. */
21221 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21222 /* Perform any access checks that were deferred. */
21223 checks = check_value->checks;
21224 if (checks)
21226 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21227 perform_or_defer_access_check (chk->binfo,
21228 chk->decl,
21229 chk->diag_decl);
21231 /* Set the scope from the stored value. */
21232 parser->scope = check_value->value;
21233 parser->qualifying_scope = check_value->qualifying_scope;
21234 parser->object_scope = NULL_TREE;
21237 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21238 encounter the end of a block before what we were looking for. */
21240 static bool
21241 cp_parser_cache_group (cp_parser *parser,
21242 enum cpp_ttype end,
21243 unsigned depth)
21245 while (true)
21247 cp_token *token = cp_lexer_peek_token (parser->lexer);
21249 /* Abort a parenthesized expression if we encounter a semicolon. */
21250 if ((end == CPP_CLOSE_PAREN || depth == 0)
21251 && token->type == CPP_SEMICOLON)
21252 return true;
21253 /* If we've reached the end of the file, stop. */
21254 if (token->type == CPP_EOF
21255 || (end != CPP_PRAGMA_EOL
21256 && token->type == CPP_PRAGMA_EOL))
21257 return true;
21258 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21259 /* We've hit the end of an enclosing block, so there's been some
21260 kind of syntax error. */
21261 return true;
21263 /* Consume the token. */
21264 cp_lexer_consume_token (parser->lexer);
21265 /* See if it starts a new group. */
21266 if (token->type == CPP_OPEN_BRACE)
21268 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21269 /* In theory this should probably check end == '}', but
21270 cp_parser_save_member_function_body needs it to exit
21271 after either '}' or ')' when called with ')'. */
21272 if (depth == 0)
21273 return false;
21275 else if (token->type == CPP_OPEN_PAREN)
21277 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21278 if (depth == 0 && end == CPP_CLOSE_PAREN)
21279 return false;
21281 else if (token->type == CPP_PRAGMA)
21282 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21283 else if (token->type == end)
21284 return false;
21288 /* Begin parsing tentatively. We always save tokens while parsing
21289 tentatively so that if the tentative parsing fails we can restore the
21290 tokens. */
21292 static void
21293 cp_parser_parse_tentatively (cp_parser* parser)
21295 /* Enter a new parsing context. */
21296 parser->context = cp_parser_context_new (parser->context);
21297 /* Begin saving tokens. */
21298 cp_lexer_save_tokens (parser->lexer);
21299 /* In order to avoid repetitive access control error messages,
21300 access checks are queued up until we are no longer parsing
21301 tentatively. */
21302 push_deferring_access_checks (dk_deferred);
21305 /* Commit to the currently active tentative parse. */
21307 static void
21308 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21310 cp_parser_context *context;
21311 cp_lexer *lexer;
21313 /* Mark all of the levels as committed. */
21314 lexer = parser->lexer;
21315 for (context = parser->context; context->next; context = context->next)
21317 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21318 break;
21319 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21320 while (!cp_lexer_saving_tokens (lexer))
21321 lexer = lexer->next;
21322 cp_lexer_commit_tokens (lexer);
21326 /* Abort the currently active tentative parse. All consumed tokens
21327 will be rolled back, and no diagnostics will be issued. */
21329 static void
21330 cp_parser_abort_tentative_parse (cp_parser* parser)
21332 cp_parser_simulate_error (parser);
21333 /* Now, pretend that we want to see if the construct was
21334 successfully parsed. */
21335 cp_parser_parse_definitely (parser);
21338 /* Stop parsing tentatively. If a parse error has occurred, restore the
21339 token stream. Otherwise, commit to the tokens we have consumed.
21340 Returns true if no error occurred; false otherwise. */
21342 static bool
21343 cp_parser_parse_definitely (cp_parser* parser)
21345 bool error_occurred;
21346 cp_parser_context *context;
21348 /* Remember whether or not an error occurred, since we are about to
21349 destroy that information. */
21350 error_occurred = cp_parser_error_occurred (parser);
21351 /* Remove the topmost context from the stack. */
21352 context = parser->context;
21353 parser->context = context->next;
21354 /* If no parse errors occurred, commit to the tentative parse. */
21355 if (!error_occurred)
21357 /* Commit to the tokens read tentatively, unless that was
21358 already done. */
21359 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21360 cp_lexer_commit_tokens (parser->lexer);
21362 pop_to_parent_deferring_access_checks ();
21364 /* Otherwise, if errors occurred, roll back our state so that things
21365 are just as they were before we began the tentative parse. */
21366 else
21368 cp_lexer_rollback_tokens (parser->lexer);
21369 pop_deferring_access_checks ();
21371 /* Add the context to the front of the free list. */
21372 context->next = cp_parser_context_free_list;
21373 cp_parser_context_free_list = context;
21375 return !error_occurred;
21378 /* Returns true if we are parsing tentatively and are not committed to
21379 this tentative parse. */
21381 static bool
21382 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21384 return (cp_parser_parsing_tentatively (parser)
21385 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21388 /* Returns nonzero iff an error has occurred during the most recent
21389 tentative parse. */
21391 static bool
21392 cp_parser_error_occurred (cp_parser* parser)
21394 return (cp_parser_parsing_tentatively (parser)
21395 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21398 /* Returns nonzero if GNU extensions are allowed. */
21400 static bool
21401 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21403 return parser->allow_gnu_extensions_p;
21406 /* Objective-C++ Productions */
21409 /* Parse an Objective-C expression, which feeds into a primary-expression
21410 above.
21412 objc-expression:
21413 objc-message-expression
21414 objc-string-literal
21415 objc-encode-expression
21416 objc-protocol-expression
21417 objc-selector-expression
21419 Returns a tree representation of the expression. */
21421 static tree
21422 cp_parser_objc_expression (cp_parser* parser)
21424 /* Try to figure out what kind of declaration is present. */
21425 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21427 switch (kwd->type)
21429 case CPP_OPEN_SQUARE:
21430 return cp_parser_objc_message_expression (parser);
21432 case CPP_OBJC_STRING:
21433 kwd = cp_lexer_consume_token (parser->lexer);
21434 return objc_build_string_object (kwd->u.value);
21436 case CPP_KEYWORD:
21437 switch (kwd->keyword)
21439 case RID_AT_ENCODE:
21440 return cp_parser_objc_encode_expression (parser);
21442 case RID_AT_PROTOCOL:
21443 return cp_parser_objc_protocol_expression (parser);
21445 case RID_AT_SELECTOR:
21446 return cp_parser_objc_selector_expression (parser);
21448 default:
21449 break;
21451 default:
21452 error_at (kwd->location,
21453 "misplaced %<@%D%> Objective-C++ construct",
21454 kwd->u.value);
21455 cp_parser_skip_to_end_of_block_or_statement (parser);
21458 return error_mark_node;
21461 /* Parse an Objective-C message expression.
21463 objc-message-expression:
21464 [ objc-message-receiver objc-message-args ]
21466 Returns a representation of an Objective-C message. */
21468 static tree
21469 cp_parser_objc_message_expression (cp_parser* parser)
21471 tree receiver, messageargs;
21473 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21474 receiver = cp_parser_objc_message_receiver (parser);
21475 messageargs = cp_parser_objc_message_args (parser);
21476 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21478 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21481 /* Parse an objc-message-receiver.
21483 objc-message-receiver:
21484 expression
21485 simple-type-specifier
21487 Returns a representation of the type or expression. */
21489 static tree
21490 cp_parser_objc_message_receiver (cp_parser* parser)
21492 tree rcv;
21494 /* An Objective-C message receiver may be either (1) a type
21495 or (2) an expression. */
21496 cp_parser_parse_tentatively (parser);
21497 rcv = cp_parser_expression (parser, false, NULL);
21499 if (cp_parser_parse_definitely (parser))
21500 return rcv;
21502 rcv = cp_parser_simple_type_specifier (parser,
21503 /*decl_specs=*/NULL,
21504 CP_PARSER_FLAGS_NONE);
21506 return objc_get_class_reference (rcv);
21509 /* Parse the arguments and selectors comprising an Objective-C message.
21511 objc-message-args:
21512 objc-selector
21513 objc-selector-args
21514 objc-selector-args , objc-comma-args
21516 objc-selector-args:
21517 objc-selector [opt] : assignment-expression
21518 objc-selector-args objc-selector [opt] : assignment-expression
21520 objc-comma-args:
21521 assignment-expression
21522 objc-comma-args , assignment-expression
21524 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21525 selector arguments and TREE_VALUE containing a list of comma
21526 arguments. */
21528 static tree
21529 cp_parser_objc_message_args (cp_parser* parser)
21531 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21532 bool maybe_unary_selector_p = true;
21533 cp_token *token = cp_lexer_peek_token (parser->lexer);
21535 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21537 tree selector = NULL_TREE, arg;
21539 if (token->type != CPP_COLON)
21540 selector = cp_parser_objc_selector (parser);
21542 /* Detect if we have a unary selector. */
21543 if (maybe_unary_selector_p
21544 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21545 return build_tree_list (selector, NULL_TREE);
21547 maybe_unary_selector_p = false;
21548 cp_parser_require (parser, CPP_COLON, RT_COLON);
21549 arg = cp_parser_assignment_expression (parser, false, NULL);
21551 sel_args
21552 = chainon (sel_args,
21553 build_tree_list (selector, arg));
21555 token = cp_lexer_peek_token (parser->lexer);
21558 /* Handle non-selector arguments, if any. */
21559 while (token->type == CPP_COMMA)
21561 tree arg;
21563 cp_lexer_consume_token (parser->lexer);
21564 arg = cp_parser_assignment_expression (parser, false, NULL);
21566 addl_args
21567 = chainon (addl_args,
21568 build_tree_list (NULL_TREE, arg));
21570 token = cp_lexer_peek_token (parser->lexer);
21573 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21575 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21576 return build_tree_list (error_mark_node, error_mark_node);
21579 return build_tree_list (sel_args, addl_args);
21582 /* Parse an Objective-C encode expression.
21584 objc-encode-expression:
21585 @encode objc-typename
21587 Returns an encoded representation of the type argument. */
21589 static tree
21590 cp_parser_objc_encode_expression (cp_parser* parser)
21592 tree type;
21593 cp_token *token;
21595 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21596 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21597 token = cp_lexer_peek_token (parser->lexer);
21598 type = complete_type (cp_parser_type_id (parser));
21599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21601 if (!type)
21603 error_at (token->location,
21604 "%<@encode%> must specify a type as an argument");
21605 return error_mark_node;
21608 /* This happens if we find @encode(T) (where T is a template
21609 typename or something dependent on a template typename) when
21610 parsing a template. In that case, we can't compile it
21611 immediately, but we rather create an AT_ENCODE_EXPR which will
21612 need to be instantiated when the template is used.
21614 if (dependent_type_p (type))
21616 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21617 TREE_READONLY (value) = 1;
21618 return value;
21621 return objc_build_encode_expr (type);
21624 /* Parse an Objective-C @defs expression. */
21626 static tree
21627 cp_parser_objc_defs_expression (cp_parser *parser)
21629 tree name;
21631 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21632 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21633 name = cp_parser_identifier (parser);
21634 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21636 return objc_get_class_ivars (name);
21639 /* Parse an Objective-C protocol expression.
21641 objc-protocol-expression:
21642 @protocol ( identifier )
21644 Returns a representation of the protocol expression. */
21646 static tree
21647 cp_parser_objc_protocol_expression (cp_parser* parser)
21649 tree proto;
21651 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21653 proto = cp_parser_identifier (parser);
21654 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21656 return objc_build_protocol_expr (proto);
21659 /* Parse an Objective-C selector expression.
21661 objc-selector-expression:
21662 @selector ( objc-method-signature )
21664 objc-method-signature:
21665 objc-selector
21666 objc-selector-seq
21668 objc-selector-seq:
21669 objc-selector :
21670 objc-selector-seq objc-selector :
21672 Returns a representation of the method selector. */
21674 static tree
21675 cp_parser_objc_selector_expression (cp_parser* parser)
21677 tree sel_seq = NULL_TREE;
21678 bool maybe_unary_selector_p = true;
21679 cp_token *token;
21680 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21682 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21684 token = cp_lexer_peek_token (parser->lexer);
21686 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21687 || token->type == CPP_SCOPE)
21689 tree selector = NULL_TREE;
21691 if (token->type != CPP_COLON
21692 || token->type == CPP_SCOPE)
21693 selector = cp_parser_objc_selector (parser);
21695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21696 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21698 /* Detect if we have a unary selector. */
21699 if (maybe_unary_selector_p)
21701 sel_seq = selector;
21702 goto finish_selector;
21704 else
21706 cp_parser_error (parser, "expected %<:%>");
21709 maybe_unary_selector_p = false;
21710 token = cp_lexer_consume_token (parser->lexer);
21712 if (token->type == CPP_SCOPE)
21714 sel_seq
21715 = chainon (sel_seq,
21716 build_tree_list (selector, NULL_TREE));
21717 sel_seq
21718 = chainon (sel_seq,
21719 build_tree_list (NULL_TREE, NULL_TREE));
21721 else
21722 sel_seq
21723 = chainon (sel_seq,
21724 build_tree_list (selector, NULL_TREE));
21726 token = cp_lexer_peek_token (parser->lexer);
21729 finish_selector:
21730 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21732 return objc_build_selector_expr (loc, sel_seq);
21735 /* Parse a list of identifiers.
21737 objc-identifier-list:
21738 identifier
21739 objc-identifier-list , identifier
21741 Returns a TREE_LIST of identifier nodes. */
21743 static tree
21744 cp_parser_objc_identifier_list (cp_parser* parser)
21746 tree identifier;
21747 tree list;
21748 cp_token *sep;
21750 identifier = cp_parser_identifier (parser);
21751 if (identifier == error_mark_node)
21752 return error_mark_node;
21754 list = build_tree_list (NULL_TREE, identifier);
21755 sep = cp_lexer_peek_token (parser->lexer);
21757 while (sep->type == CPP_COMMA)
21759 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21760 identifier = cp_parser_identifier (parser);
21761 if (identifier == error_mark_node)
21762 return list;
21764 list = chainon (list, build_tree_list (NULL_TREE,
21765 identifier));
21766 sep = cp_lexer_peek_token (parser->lexer);
21769 return list;
21772 /* Parse an Objective-C alias declaration.
21774 objc-alias-declaration:
21775 @compatibility_alias identifier identifier ;
21777 This function registers the alias mapping with the Objective-C front end.
21778 It returns nothing. */
21780 static void
21781 cp_parser_objc_alias_declaration (cp_parser* parser)
21783 tree alias, orig;
21785 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21786 alias = cp_parser_identifier (parser);
21787 orig = cp_parser_identifier (parser);
21788 objc_declare_alias (alias, orig);
21789 cp_parser_consume_semicolon_at_end_of_statement (parser);
21792 /* Parse an Objective-C class forward-declaration.
21794 objc-class-declaration:
21795 @class objc-identifier-list ;
21797 The function registers the forward declarations with the Objective-C
21798 front end. It returns nothing. */
21800 static void
21801 cp_parser_objc_class_declaration (cp_parser* parser)
21803 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21804 objc_declare_class (cp_parser_objc_identifier_list (parser));
21805 cp_parser_consume_semicolon_at_end_of_statement (parser);
21808 /* Parse a list of Objective-C protocol references.
21810 objc-protocol-refs-opt:
21811 objc-protocol-refs [opt]
21813 objc-protocol-refs:
21814 < objc-identifier-list >
21816 Returns a TREE_LIST of identifiers, if any. */
21818 static tree
21819 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21821 tree protorefs = NULL_TREE;
21823 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21825 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21826 protorefs = cp_parser_objc_identifier_list (parser);
21827 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21830 return protorefs;
21833 /* Parse a Objective-C visibility specification. */
21835 static void
21836 cp_parser_objc_visibility_spec (cp_parser* parser)
21838 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21840 switch (vis->keyword)
21842 case RID_AT_PRIVATE:
21843 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21844 break;
21845 case RID_AT_PROTECTED:
21846 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21847 break;
21848 case RID_AT_PUBLIC:
21849 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21850 break;
21851 case RID_AT_PACKAGE:
21852 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21853 break;
21854 default:
21855 return;
21858 /* Eat '@private'/'@protected'/'@public'. */
21859 cp_lexer_consume_token (parser->lexer);
21862 /* Parse an Objective-C method type. Return 'true' if it is a class
21863 (+) method, and 'false' if it is an instance (-) method. */
21865 static inline bool
21866 cp_parser_objc_method_type (cp_parser* parser)
21868 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21869 return true;
21870 else
21871 return false;
21874 /* Parse an Objective-C protocol qualifier. */
21876 static tree
21877 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21879 tree quals = NULL_TREE, node;
21880 cp_token *token = cp_lexer_peek_token (parser->lexer);
21882 node = token->u.value;
21884 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21885 && (node == ridpointers [(int) RID_IN]
21886 || node == ridpointers [(int) RID_OUT]
21887 || node == ridpointers [(int) RID_INOUT]
21888 || node == ridpointers [(int) RID_BYCOPY]
21889 || node == ridpointers [(int) RID_BYREF]
21890 || node == ridpointers [(int) RID_ONEWAY]))
21892 quals = tree_cons (NULL_TREE, node, quals);
21893 cp_lexer_consume_token (parser->lexer);
21894 token = cp_lexer_peek_token (parser->lexer);
21895 node = token->u.value;
21898 return quals;
21901 /* Parse an Objective-C typename. */
21903 static tree
21904 cp_parser_objc_typename (cp_parser* parser)
21906 tree type_name = NULL_TREE;
21908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21910 tree proto_quals, cp_type = NULL_TREE;
21912 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21913 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21915 /* An ObjC type name may consist of just protocol qualifiers, in which
21916 case the type shall default to 'id'. */
21917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21918 cp_type = cp_parser_type_id (parser);
21920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21921 type_name = build_tree_list (proto_quals, cp_type);
21924 return type_name;
21927 /* Check to see if TYPE refers to an Objective-C selector name. */
21929 static bool
21930 cp_parser_objc_selector_p (enum cpp_ttype type)
21932 return (type == CPP_NAME || type == CPP_KEYWORD
21933 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21934 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21935 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21936 || type == CPP_XOR || type == CPP_XOR_EQ);
21939 /* Parse an Objective-C selector. */
21941 static tree
21942 cp_parser_objc_selector (cp_parser* parser)
21944 cp_token *token = cp_lexer_consume_token (parser->lexer);
21946 if (!cp_parser_objc_selector_p (token->type))
21948 error_at (token->location, "invalid Objective-C++ selector name");
21949 return error_mark_node;
21952 /* C++ operator names are allowed to appear in ObjC selectors. */
21953 switch (token->type)
21955 case CPP_AND_AND: return get_identifier ("and");
21956 case CPP_AND_EQ: return get_identifier ("and_eq");
21957 case CPP_AND: return get_identifier ("bitand");
21958 case CPP_OR: return get_identifier ("bitor");
21959 case CPP_COMPL: return get_identifier ("compl");
21960 case CPP_NOT: return get_identifier ("not");
21961 case CPP_NOT_EQ: return get_identifier ("not_eq");
21962 case CPP_OR_OR: return get_identifier ("or");
21963 case CPP_OR_EQ: return get_identifier ("or_eq");
21964 case CPP_XOR: return get_identifier ("xor");
21965 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21966 default: return token->u.value;
21970 /* Parse an Objective-C params list. */
21972 static tree
21973 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21975 tree params = NULL_TREE;
21976 bool maybe_unary_selector_p = true;
21977 cp_token *token = cp_lexer_peek_token (parser->lexer);
21979 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21981 tree selector = NULL_TREE, type_name, identifier;
21982 tree parm_attr = NULL_TREE;
21984 if (token->keyword == RID_ATTRIBUTE)
21985 break;
21987 if (token->type != CPP_COLON)
21988 selector = cp_parser_objc_selector (parser);
21990 /* Detect if we have a unary selector. */
21991 if (maybe_unary_selector_p
21992 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21994 params = selector; /* Might be followed by attributes. */
21995 break;
21998 maybe_unary_selector_p = false;
21999 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22001 /* Something went quite wrong. There should be a colon
22002 here, but there is not. Stop parsing parameters. */
22003 break;
22005 type_name = cp_parser_objc_typename (parser);
22006 /* New ObjC allows attributes on parameters too. */
22007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22008 parm_attr = cp_parser_attributes_opt (parser);
22009 identifier = cp_parser_identifier (parser);
22011 params
22012 = chainon (params,
22013 objc_build_keyword_decl (selector,
22014 type_name,
22015 identifier,
22016 parm_attr));
22018 token = cp_lexer_peek_token (parser->lexer);
22021 if (params == NULL_TREE)
22023 cp_parser_error (parser, "objective-c++ method declaration is expected");
22024 return error_mark_node;
22027 /* We allow tail attributes for the method. */
22028 if (token->keyword == RID_ATTRIBUTE)
22030 *attributes = cp_parser_attributes_opt (parser);
22031 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22032 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22033 return params;
22034 cp_parser_error (parser,
22035 "method attributes must be specified at the end");
22036 return error_mark_node;
22039 if (params == NULL_TREE)
22041 cp_parser_error (parser, "objective-c++ method declaration is expected");
22042 return error_mark_node;
22044 return params;
22047 /* Parse the non-keyword Objective-C params. */
22049 static tree
22050 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22051 tree* attributes)
22053 tree params = make_node (TREE_LIST);
22054 cp_token *token = cp_lexer_peek_token (parser->lexer);
22055 *ellipsisp = false; /* Initially, assume no ellipsis. */
22057 while (token->type == CPP_COMMA)
22059 cp_parameter_declarator *parmdecl;
22060 tree parm;
22062 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22063 token = cp_lexer_peek_token (parser->lexer);
22065 if (token->type == CPP_ELLIPSIS)
22067 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22068 *ellipsisp = true;
22069 token = cp_lexer_peek_token (parser->lexer);
22070 break;
22073 /* TODO: parse attributes for tail parameters. */
22074 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22075 parm = grokdeclarator (parmdecl->declarator,
22076 &parmdecl->decl_specifiers,
22077 PARM, /*initialized=*/0,
22078 /*attrlist=*/NULL);
22080 chainon (params, build_tree_list (NULL_TREE, parm));
22081 token = cp_lexer_peek_token (parser->lexer);
22084 /* We allow tail attributes for the method. */
22085 if (token->keyword == RID_ATTRIBUTE)
22087 if (*attributes == NULL_TREE)
22089 *attributes = cp_parser_attributes_opt (parser);
22090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22091 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22092 return params;
22094 else
22095 /* We have an error, but parse the attributes, so that we can
22096 carry on. */
22097 *attributes = cp_parser_attributes_opt (parser);
22099 cp_parser_error (parser,
22100 "method attributes must be specified at the end");
22101 return error_mark_node;
22104 return params;
22107 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22109 static void
22110 cp_parser_objc_interstitial_code (cp_parser* parser)
22112 cp_token *token = cp_lexer_peek_token (parser->lexer);
22114 /* If the next token is `extern' and the following token is a string
22115 literal, then we have a linkage specification. */
22116 if (token->keyword == RID_EXTERN
22117 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22118 cp_parser_linkage_specification (parser);
22119 /* Handle #pragma, if any. */
22120 else if (token->type == CPP_PRAGMA)
22121 cp_parser_pragma (parser, pragma_external);
22122 /* Allow stray semicolons. */
22123 else if (token->type == CPP_SEMICOLON)
22124 cp_lexer_consume_token (parser->lexer);
22125 /* Mark methods as optional or required, when building protocols. */
22126 else if (token->keyword == RID_AT_OPTIONAL)
22128 cp_lexer_consume_token (parser->lexer);
22129 objc_set_method_opt (true);
22131 else if (token->keyword == RID_AT_REQUIRED)
22133 cp_lexer_consume_token (parser->lexer);
22134 objc_set_method_opt (false);
22136 else if (token->keyword == RID_NAMESPACE)
22137 cp_parser_namespace_definition (parser);
22138 /* Other stray characters must generate errors. */
22139 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22141 cp_lexer_consume_token (parser->lexer);
22142 error ("stray %qs between Objective-C++ methods",
22143 token->type == CPP_OPEN_BRACE ? "{" : "}");
22145 /* Finally, try to parse a block-declaration, or a function-definition. */
22146 else
22147 cp_parser_block_declaration (parser, /*statement_p=*/false);
22150 /* Parse a method signature. */
22152 static tree
22153 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22155 tree rettype, kwdparms, optparms;
22156 bool ellipsis = false;
22157 bool is_class_method;
22159 is_class_method = cp_parser_objc_method_type (parser);
22160 rettype = cp_parser_objc_typename (parser);
22161 *attributes = NULL_TREE;
22162 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22163 if (kwdparms == error_mark_node)
22164 return error_mark_node;
22165 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22166 if (optparms == error_mark_node)
22167 return error_mark_node;
22169 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22172 static bool
22173 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22175 tree tattr;
22176 cp_lexer_save_tokens (parser->lexer);
22177 tattr = cp_parser_attributes_opt (parser);
22178 gcc_assert (tattr) ;
22180 /* If the attributes are followed by a method introducer, this is not allowed.
22181 Dump the attributes and flag the situation. */
22182 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22183 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22184 return true;
22186 /* Otherwise, the attributes introduce some interstitial code, possibly so
22187 rewind to allow that check. */
22188 cp_lexer_rollback_tokens (parser->lexer);
22189 return false;
22192 /* Parse an Objective-C method prototype list. */
22194 static void
22195 cp_parser_objc_method_prototype_list (cp_parser* parser)
22197 cp_token *token = cp_lexer_peek_token (parser->lexer);
22199 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22201 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22203 tree attributes, sig;
22204 bool is_class_method;
22205 if (token->type == CPP_PLUS)
22206 is_class_method = true;
22207 else
22208 is_class_method = false;
22209 sig = cp_parser_objc_method_signature (parser, &attributes);
22210 if (sig == error_mark_node)
22212 cp_parser_skip_to_end_of_block_or_statement (parser);
22213 token = cp_lexer_peek_token (parser->lexer);
22214 continue;
22216 objc_add_method_declaration (is_class_method, sig, attributes);
22217 cp_parser_consume_semicolon_at_end_of_statement (parser);
22219 else if (token->keyword == RID_AT_PROPERTY)
22220 cp_parser_objc_at_property_declaration (parser);
22221 else if (token->keyword == RID_ATTRIBUTE
22222 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22223 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22224 OPT_Wattributes,
22225 "prefix attributes are ignored for methods");
22226 else
22227 /* Allow for interspersed non-ObjC++ code. */
22228 cp_parser_objc_interstitial_code (parser);
22230 token = cp_lexer_peek_token (parser->lexer);
22233 if (token->type != CPP_EOF)
22234 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22235 else
22236 cp_parser_error (parser, "expected %<@end%>");
22238 objc_finish_interface ();
22241 /* Parse an Objective-C method definition list. */
22243 static void
22244 cp_parser_objc_method_definition_list (cp_parser* parser)
22246 cp_token *token = cp_lexer_peek_token (parser->lexer);
22248 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22250 tree meth;
22252 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22254 cp_token *ptk;
22255 tree sig, attribute;
22256 bool is_class_method;
22257 if (token->type == CPP_PLUS)
22258 is_class_method = true;
22259 else
22260 is_class_method = false;
22261 push_deferring_access_checks (dk_deferred);
22262 sig = cp_parser_objc_method_signature (parser, &attribute);
22263 if (sig == error_mark_node)
22265 cp_parser_skip_to_end_of_block_or_statement (parser);
22266 token = cp_lexer_peek_token (parser->lexer);
22267 continue;
22269 objc_start_method_definition (is_class_method, sig, attribute);
22271 /* For historical reasons, we accept an optional semicolon. */
22272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22273 cp_lexer_consume_token (parser->lexer);
22275 ptk = cp_lexer_peek_token (parser->lexer);
22276 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22277 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22279 perform_deferred_access_checks ();
22280 stop_deferring_access_checks ();
22281 meth = cp_parser_function_definition_after_declarator (parser,
22282 false);
22283 pop_deferring_access_checks ();
22284 objc_finish_method_definition (meth);
22287 /* The following case will be removed once @synthesize is
22288 completely implemented. */
22289 else if (token->keyword == RID_AT_PROPERTY)
22290 cp_parser_objc_at_property_declaration (parser);
22291 else if (token->keyword == RID_AT_SYNTHESIZE)
22292 cp_parser_objc_at_synthesize_declaration (parser);
22293 else if (token->keyword == RID_AT_DYNAMIC)
22294 cp_parser_objc_at_dynamic_declaration (parser);
22295 else if (token->keyword == RID_ATTRIBUTE
22296 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22297 warning_at (token->location, OPT_Wattributes,
22298 "prefix attributes are ignored for methods");
22299 else
22300 /* Allow for interspersed non-ObjC++ code. */
22301 cp_parser_objc_interstitial_code (parser);
22303 token = cp_lexer_peek_token (parser->lexer);
22306 if (token->type != CPP_EOF)
22307 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22308 else
22309 cp_parser_error (parser, "expected %<@end%>");
22311 objc_finish_implementation ();
22314 /* Parse Objective-C ivars. */
22316 static void
22317 cp_parser_objc_class_ivars (cp_parser* parser)
22319 cp_token *token = cp_lexer_peek_token (parser->lexer);
22321 if (token->type != CPP_OPEN_BRACE)
22322 return; /* No ivars specified. */
22324 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22325 token = cp_lexer_peek_token (parser->lexer);
22327 while (token->type != CPP_CLOSE_BRACE
22328 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22330 cp_decl_specifier_seq declspecs;
22331 int decl_class_or_enum_p;
22332 tree prefix_attributes;
22334 cp_parser_objc_visibility_spec (parser);
22336 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22337 break;
22339 cp_parser_decl_specifier_seq (parser,
22340 CP_PARSER_FLAGS_OPTIONAL,
22341 &declspecs,
22342 &decl_class_or_enum_p);
22344 /* auto, register, static, extern, mutable. */
22345 if (declspecs.storage_class != sc_none)
22347 cp_parser_error (parser, "invalid type for instance variable");
22348 declspecs.storage_class = sc_none;
22351 /* __thread. */
22352 if (declspecs.specs[(int) ds_thread])
22354 cp_parser_error (parser, "invalid type for instance variable");
22355 declspecs.specs[(int) ds_thread] = 0;
22358 /* typedef. */
22359 if (declspecs.specs[(int) ds_typedef])
22361 cp_parser_error (parser, "invalid type for instance variable");
22362 declspecs.specs[(int) ds_typedef] = 0;
22365 prefix_attributes = declspecs.attributes;
22366 declspecs.attributes = NULL_TREE;
22368 /* Keep going until we hit the `;' at the end of the
22369 declaration. */
22370 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22372 tree width = NULL_TREE, attributes, first_attribute, decl;
22373 cp_declarator *declarator = NULL;
22374 int ctor_dtor_or_conv_p;
22376 /* Check for a (possibly unnamed) bitfield declaration. */
22377 token = cp_lexer_peek_token (parser->lexer);
22378 if (token->type == CPP_COLON)
22379 goto eat_colon;
22381 if (token->type == CPP_NAME
22382 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22383 == CPP_COLON))
22385 /* Get the name of the bitfield. */
22386 declarator = make_id_declarator (NULL_TREE,
22387 cp_parser_identifier (parser),
22388 sfk_none);
22390 eat_colon:
22391 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22392 /* Get the width of the bitfield. */
22393 width
22394 = cp_parser_constant_expression (parser,
22395 /*allow_non_constant=*/false,
22396 NULL);
22398 else
22400 /* Parse the declarator. */
22401 declarator
22402 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22403 &ctor_dtor_or_conv_p,
22404 /*parenthesized_p=*/NULL,
22405 /*member_p=*/false);
22408 /* Look for attributes that apply to the ivar. */
22409 attributes = cp_parser_attributes_opt (parser);
22410 /* Remember which attributes are prefix attributes and
22411 which are not. */
22412 first_attribute = attributes;
22413 /* Combine the attributes. */
22414 attributes = chainon (prefix_attributes, attributes);
22416 if (width)
22417 /* Create the bitfield declaration. */
22418 decl = grokbitfield (declarator, &declspecs,
22419 width,
22420 attributes);
22421 else
22422 decl = grokfield (declarator, &declspecs,
22423 NULL_TREE, /*init_const_expr_p=*/false,
22424 NULL_TREE, attributes);
22426 /* Add the instance variable. */
22427 objc_add_instance_variable (decl);
22429 /* Reset PREFIX_ATTRIBUTES. */
22430 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22431 attributes = TREE_CHAIN (attributes);
22432 if (attributes)
22433 TREE_CHAIN (attributes) = NULL_TREE;
22435 token = cp_lexer_peek_token (parser->lexer);
22437 if (token->type == CPP_COMMA)
22439 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22440 continue;
22442 break;
22445 cp_parser_consume_semicolon_at_end_of_statement (parser);
22446 token = cp_lexer_peek_token (parser->lexer);
22449 if (token->keyword == RID_AT_END)
22450 cp_parser_error (parser, "expected %<}%>");
22452 /* Do not consume the RID_AT_END, so it will be read again as terminating
22453 the @interface of @implementation. */
22454 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22455 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22457 /* For historical reasons, we accept an optional semicolon. */
22458 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22459 cp_lexer_consume_token (parser->lexer);
22462 /* Parse an Objective-C protocol declaration. */
22464 static void
22465 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22467 tree proto, protorefs;
22468 cp_token *tok;
22470 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22473 tok = cp_lexer_peek_token (parser->lexer);
22474 error_at (tok->location, "identifier expected after %<@protocol%>");
22475 goto finish;
22478 /* See if we have a forward declaration or a definition. */
22479 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22481 /* Try a forward declaration first. */
22482 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22484 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22485 attributes);
22486 finish:
22487 cp_parser_consume_semicolon_at_end_of_statement (parser);
22490 /* Ok, we got a full-fledged definition (or at least should). */
22491 else
22493 proto = cp_parser_identifier (parser);
22494 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22495 objc_start_protocol (proto, protorefs, attributes);
22496 cp_parser_objc_method_prototype_list (parser);
22500 /* Parse an Objective-C superclass or category. */
22502 static void
22503 cp_parser_objc_superclass_or_category (cp_parser *parser,
22504 bool iface_p,
22505 tree *super,
22506 tree *categ, bool *is_class_extension)
22508 cp_token *next = cp_lexer_peek_token (parser->lexer);
22510 *super = *categ = NULL_TREE;
22511 *is_class_extension = false;
22512 if (next->type == CPP_COLON)
22514 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22515 *super = cp_parser_identifier (parser);
22517 else if (next->type == CPP_OPEN_PAREN)
22519 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22521 /* If there is no category name, and this is an @interface, we
22522 have a class extension. */
22523 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22525 *categ = NULL_TREE;
22526 *is_class_extension = true;
22528 else
22529 *categ = cp_parser_identifier (parser);
22531 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22535 /* Parse an Objective-C class interface. */
22537 static void
22538 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22540 tree name, super, categ, protos;
22541 bool is_class_extension;
22543 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22544 name = cp_parser_identifier (parser);
22545 if (name == error_mark_node)
22547 /* It's hard to recover because even if valid @interface stuff
22548 is to follow, we can't compile it (or validate it) if we
22549 don't even know which class it refers to. Let's assume this
22550 was a stray '@interface' token in the stream and skip it.
22552 return;
22554 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22555 &is_class_extension);
22556 protos = cp_parser_objc_protocol_refs_opt (parser);
22558 /* We have either a class or a category on our hands. */
22559 if (categ || is_class_extension)
22560 objc_start_category_interface (name, categ, protos, attributes);
22561 else
22563 objc_start_class_interface (name, super, protos, attributes);
22564 /* Handle instance variable declarations, if any. */
22565 cp_parser_objc_class_ivars (parser);
22566 objc_continue_interface ();
22569 cp_parser_objc_method_prototype_list (parser);
22572 /* Parse an Objective-C class implementation. */
22574 static void
22575 cp_parser_objc_class_implementation (cp_parser* parser)
22577 tree name, super, categ;
22578 bool is_class_extension;
22580 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22581 name = cp_parser_identifier (parser);
22582 if (name == error_mark_node)
22584 /* It's hard to recover because even if valid @implementation
22585 stuff is to follow, we can't compile it (or validate it) if
22586 we don't even know which class it refers to. Let's assume
22587 this was a stray '@implementation' token in the stream and
22588 skip it.
22590 return;
22592 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22593 &is_class_extension);
22595 /* We have either a class or a category on our hands. */
22596 if (categ)
22597 objc_start_category_implementation (name, categ);
22598 else
22600 objc_start_class_implementation (name, super);
22601 /* Handle instance variable declarations, if any. */
22602 cp_parser_objc_class_ivars (parser);
22603 objc_continue_implementation ();
22606 cp_parser_objc_method_definition_list (parser);
22609 /* Consume the @end token and finish off the implementation. */
22611 static void
22612 cp_parser_objc_end_implementation (cp_parser* parser)
22614 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22615 objc_finish_implementation ();
22618 /* Parse an Objective-C declaration. */
22620 static void
22621 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22623 /* Try to figure out what kind of declaration is present. */
22624 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22626 if (attributes)
22627 switch (kwd->keyword)
22629 case RID_AT_ALIAS:
22630 case RID_AT_CLASS:
22631 case RID_AT_END:
22632 error_at (kwd->location, "attributes may not be specified before"
22633 " the %<@%D%> Objective-C++ keyword",
22634 kwd->u.value);
22635 attributes = NULL;
22636 break;
22637 case RID_AT_IMPLEMENTATION:
22638 warning_at (kwd->location, OPT_Wattributes,
22639 "prefix attributes are ignored before %<@%D%>",
22640 kwd->u.value);
22641 attributes = NULL;
22642 default:
22643 break;
22646 switch (kwd->keyword)
22648 case RID_AT_ALIAS:
22649 cp_parser_objc_alias_declaration (parser);
22650 break;
22651 case RID_AT_CLASS:
22652 cp_parser_objc_class_declaration (parser);
22653 break;
22654 case RID_AT_PROTOCOL:
22655 cp_parser_objc_protocol_declaration (parser, attributes);
22656 break;
22657 case RID_AT_INTERFACE:
22658 cp_parser_objc_class_interface (parser, attributes);
22659 break;
22660 case RID_AT_IMPLEMENTATION:
22661 cp_parser_objc_class_implementation (parser);
22662 break;
22663 case RID_AT_END:
22664 cp_parser_objc_end_implementation (parser);
22665 break;
22666 default:
22667 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22668 kwd->u.value);
22669 cp_parser_skip_to_end_of_block_or_statement (parser);
22673 /* Parse an Objective-C try-catch-finally statement.
22675 objc-try-catch-finally-stmt:
22676 @try compound-statement objc-catch-clause-seq [opt]
22677 objc-finally-clause [opt]
22679 objc-catch-clause-seq:
22680 objc-catch-clause objc-catch-clause-seq [opt]
22682 objc-catch-clause:
22683 @catch ( objc-exception-declaration ) compound-statement
22685 objc-finally-clause:
22686 @finally compound-statement
22688 objc-exception-declaration:
22689 parameter-declaration
22690 '...'
22692 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22694 Returns NULL_TREE.
22696 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22697 for C. Keep them in sync. */
22699 static tree
22700 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22702 location_t location;
22703 tree stmt;
22705 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22706 location = cp_lexer_peek_token (parser->lexer)->location;
22707 objc_maybe_warn_exceptions (location);
22708 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22709 node, lest it get absorbed into the surrounding block. */
22710 stmt = push_stmt_list ();
22711 cp_parser_compound_statement (parser, NULL, false);
22712 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22714 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22716 cp_parameter_declarator *parm;
22717 tree parameter_declaration = error_mark_node;
22718 bool seen_open_paren = false;
22720 cp_lexer_consume_token (parser->lexer);
22721 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22722 seen_open_paren = true;
22723 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22725 /* We have "@catch (...)" (where the '...' are literally
22726 what is in the code). Skip the '...'.
22727 parameter_declaration is set to NULL_TREE, and
22728 objc_being_catch_clauses() knows that that means
22729 '...'. */
22730 cp_lexer_consume_token (parser->lexer);
22731 parameter_declaration = NULL_TREE;
22733 else
22735 /* We have "@catch (NSException *exception)" or something
22736 like that. Parse the parameter declaration. */
22737 parm = cp_parser_parameter_declaration (parser, false, NULL);
22738 if (parm == NULL)
22739 parameter_declaration = error_mark_node;
22740 else
22741 parameter_declaration = grokdeclarator (parm->declarator,
22742 &parm->decl_specifiers,
22743 PARM, /*initialized=*/0,
22744 /*attrlist=*/NULL);
22746 if (seen_open_paren)
22747 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22748 else
22750 /* If there was no open parenthesis, we are recovering from
22751 an error, and we are trying to figure out what mistake
22752 the user has made. */
22754 /* If there is an immediate closing parenthesis, the user
22755 probably forgot the opening one (ie, they typed "@catch
22756 NSException *e)". Parse the closing parenthesis and keep
22757 going. */
22758 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22759 cp_lexer_consume_token (parser->lexer);
22761 /* If these is no immediate closing parenthesis, the user
22762 probably doesn't know that parenthesis are required at
22763 all (ie, they typed "@catch NSException *e"). So, just
22764 forget about the closing parenthesis and keep going. */
22766 objc_begin_catch_clause (parameter_declaration);
22767 cp_parser_compound_statement (parser, NULL, false);
22768 objc_finish_catch_clause ();
22770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22772 cp_lexer_consume_token (parser->lexer);
22773 location = cp_lexer_peek_token (parser->lexer)->location;
22774 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22775 node, lest it get absorbed into the surrounding block. */
22776 stmt = push_stmt_list ();
22777 cp_parser_compound_statement (parser, NULL, false);
22778 objc_build_finally_clause (location, pop_stmt_list (stmt));
22781 return objc_finish_try_stmt ();
22784 /* Parse an Objective-C synchronized statement.
22786 objc-synchronized-stmt:
22787 @synchronized ( expression ) compound-statement
22789 Returns NULL_TREE. */
22791 static tree
22792 cp_parser_objc_synchronized_statement (cp_parser *parser)
22794 location_t location;
22795 tree lock, stmt;
22797 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22799 location = cp_lexer_peek_token (parser->lexer)->location;
22800 objc_maybe_warn_exceptions (location);
22801 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22802 lock = cp_parser_expression (parser, false, NULL);
22803 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22805 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22806 node, lest it get absorbed into the surrounding block. */
22807 stmt = push_stmt_list ();
22808 cp_parser_compound_statement (parser, NULL, false);
22810 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22813 /* Parse an Objective-C throw statement.
22815 objc-throw-stmt:
22816 @throw assignment-expression [opt] ;
22818 Returns a constructed '@throw' statement. */
22820 static tree
22821 cp_parser_objc_throw_statement (cp_parser *parser)
22823 tree expr = NULL_TREE;
22824 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22826 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22829 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22831 cp_parser_consume_semicolon_at_end_of_statement (parser);
22833 return objc_build_throw_stmt (loc, expr);
22836 /* Parse an Objective-C statement. */
22838 static tree
22839 cp_parser_objc_statement (cp_parser * parser)
22841 /* Try to figure out what kind of declaration is present. */
22842 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22844 switch (kwd->keyword)
22846 case RID_AT_TRY:
22847 return cp_parser_objc_try_catch_finally_statement (parser);
22848 case RID_AT_SYNCHRONIZED:
22849 return cp_parser_objc_synchronized_statement (parser);
22850 case RID_AT_THROW:
22851 return cp_parser_objc_throw_statement (parser);
22852 default:
22853 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22854 kwd->u.value);
22855 cp_parser_skip_to_end_of_block_or_statement (parser);
22858 return error_mark_node;
22861 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22862 look ahead to see if an objc keyword follows the attributes. This
22863 is to detect the use of prefix attributes on ObjC @interface and
22864 @protocol. */
22866 static bool
22867 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22869 cp_lexer_save_tokens (parser->lexer);
22870 *attrib = cp_parser_attributes_opt (parser);
22871 gcc_assert (*attrib);
22872 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22874 cp_lexer_commit_tokens (parser->lexer);
22875 return true;
22877 cp_lexer_rollback_tokens (parser->lexer);
22878 return false;
22881 /* This routine is a minimal replacement for
22882 c_parser_struct_declaration () used when parsing the list of
22883 types/names or ObjC++ properties. For example, when parsing the
22884 code
22886 @property (readonly) int a, b, c;
22888 this function is responsible for parsing "int a, int b, int c" and
22889 returning the declarations as CHAIN of DECLs.
22891 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22892 similar parsing. */
22893 static tree
22894 cp_parser_objc_struct_declaration (cp_parser *parser)
22896 tree decls = NULL_TREE;
22897 cp_decl_specifier_seq declspecs;
22898 int decl_class_or_enum_p;
22899 tree prefix_attributes;
22901 cp_parser_decl_specifier_seq (parser,
22902 CP_PARSER_FLAGS_NONE,
22903 &declspecs,
22904 &decl_class_or_enum_p);
22906 if (declspecs.type == error_mark_node)
22907 return error_mark_node;
22909 /* auto, register, static, extern, mutable. */
22910 if (declspecs.storage_class != sc_none)
22912 cp_parser_error (parser, "invalid type for property");
22913 declspecs.storage_class = sc_none;
22916 /* __thread. */
22917 if (declspecs.specs[(int) ds_thread])
22919 cp_parser_error (parser, "invalid type for property");
22920 declspecs.specs[(int) ds_thread] = 0;
22923 /* typedef. */
22924 if (declspecs.specs[(int) ds_typedef])
22926 cp_parser_error (parser, "invalid type for property");
22927 declspecs.specs[(int) ds_typedef] = 0;
22930 prefix_attributes = declspecs.attributes;
22931 declspecs.attributes = NULL_TREE;
22933 /* Keep going until we hit the `;' at the end of the declaration. */
22934 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22936 tree attributes, first_attribute, decl;
22937 cp_declarator *declarator;
22938 cp_token *token;
22940 /* Parse the declarator. */
22941 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22942 NULL, NULL, false);
22944 /* Look for attributes that apply to the ivar. */
22945 attributes = cp_parser_attributes_opt (parser);
22946 /* Remember which attributes are prefix attributes and
22947 which are not. */
22948 first_attribute = attributes;
22949 /* Combine the attributes. */
22950 attributes = chainon (prefix_attributes, attributes);
22952 decl = grokfield (declarator, &declspecs,
22953 NULL_TREE, /*init_const_expr_p=*/false,
22954 NULL_TREE, attributes);
22956 if (decl == error_mark_node || decl == NULL_TREE)
22957 return error_mark_node;
22959 /* Reset PREFIX_ATTRIBUTES. */
22960 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22961 attributes = TREE_CHAIN (attributes);
22962 if (attributes)
22963 TREE_CHAIN (attributes) = NULL_TREE;
22965 DECL_CHAIN (decl) = decls;
22966 decls = decl;
22968 token = cp_lexer_peek_token (parser->lexer);
22969 if (token->type == CPP_COMMA)
22971 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22972 continue;
22974 else
22975 break;
22977 return decls;
22980 /* Parse an Objective-C @property declaration. The syntax is:
22982 objc-property-declaration:
22983 '@property' objc-property-attributes[opt] struct-declaration ;
22985 objc-property-attributes:
22986 '(' objc-property-attribute-list ')'
22988 objc-property-attribute-list:
22989 objc-property-attribute
22990 objc-property-attribute-list, objc-property-attribute
22992 objc-property-attribute
22993 'getter' = identifier
22994 'setter' = identifier
22995 'readonly'
22996 'readwrite'
22997 'assign'
22998 'retain'
22999 'copy'
23000 'nonatomic'
23002 For example:
23003 @property NSString *name;
23004 @property (readonly) id object;
23005 @property (retain, nonatomic, getter=getTheName) id name;
23006 @property int a, b, c;
23008 PS: This function is identical to
23009 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23010 static void
23011 cp_parser_objc_at_property_declaration (cp_parser *parser)
23013 /* The following variables hold the attributes of the properties as
23014 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23015 seen. When we see an attribute, we set them to 'true' (if they
23016 are boolean properties) or to the identifier (if they have an
23017 argument, ie, for getter and setter). Note that here we only
23018 parse the list of attributes, check the syntax and accumulate the
23019 attributes that we find. objc_add_property_declaration() will
23020 then process the information. */
23021 bool property_assign = false;
23022 bool property_copy = false;
23023 tree property_getter_ident = NULL_TREE;
23024 bool property_nonatomic = false;
23025 bool property_readonly = false;
23026 bool property_readwrite = false;
23027 bool property_retain = false;
23028 tree property_setter_ident = NULL_TREE;
23030 /* 'properties' is the list of properties that we read. Usually a
23031 single one, but maybe more (eg, in "@property int a, b, c;" there
23032 are three). */
23033 tree properties;
23034 location_t loc;
23036 loc = cp_lexer_peek_token (parser->lexer)->location;
23038 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23040 /* Parse the optional attribute list... */
23041 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23043 /* Eat the '('. */
23044 cp_lexer_consume_token (parser->lexer);
23046 while (true)
23048 bool syntax_error = false;
23049 cp_token *token = cp_lexer_peek_token (parser->lexer);
23050 enum rid keyword;
23052 if (token->type != CPP_NAME)
23054 cp_parser_error (parser, "expected identifier");
23055 break;
23057 keyword = C_RID_CODE (token->u.value);
23058 cp_lexer_consume_token (parser->lexer);
23059 switch (keyword)
23061 case RID_ASSIGN: property_assign = true; break;
23062 case RID_COPY: property_copy = true; break;
23063 case RID_NONATOMIC: property_nonatomic = true; break;
23064 case RID_READONLY: property_readonly = true; break;
23065 case RID_READWRITE: property_readwrite = true; break;
23066 case RID_RETAIN: property_retain = true; break;
23068 case RID_GETTER:
23069 case RID_SETTER:
23070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23072 cp_parser_error (parser,
23073 "getter/setter/ivar attribute must be followed by %<=%>");
23074 syntax_error = true;
23075 break;
23077 cp_lexer_consume_token (parser->lexer); /* eat the = */
23078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23080 cp_parser_error (parser, "expected identifier");
23081 syntax_error = true;
23082 break;
23084 if (keyword == RID_SETTER)
23086 if (property_setter_ident != NULL_TREE)
23087 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23088 else
23089 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23090 cp_lexer_consume_token (parser->lexer);
23091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23092 cp_parser_error (parser, "setter name must terminate with %<:%>");
23093 else
23094 cp_lexer_consume_token (parser->lexer);
23096 else
23098 if (property_getter_ident != NULL_TREE)
23099 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23100 else
23101 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23102 cp_lexer_consume_token (parser->lexer);
23104 break;
23105 default:
23106 cp_parser_error (parser, "unknown property attribute");
23107 syntax_error = true;
23108 break;
23111 if (syntax_error)
23112 break;
23114 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23115 cp_lexer_consume_token (parser->lexer);
23116 else
23117 break;
23120 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23122 cp_parser_skip_to_closing_parenthesis (parser,
23123 /*recovering=*/true,
23124 /*or_comma=*/false,
23125 /*consume_paren=*/true);
23129 /* ... and the property declaration(s). */
23130 properties = cp_parser_objc_struct_declaration (parser);
23132 if (properties == error_mark_node)
23134 cp_parser_skip_to_end_of_statement (parser);
23135 /* If the next token is now a `;', consume it. */
23136 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23137 cp_lexer_consume_token (parser->lexer);
23138 return;
23141 if (properties == NULL_TREE)
23142 cp_parser_error (parser, "expected identifier");
23143 else
23145 /* Comma-separated properties are chained together in
23146 reverse order; add them one by one. */
23147 properties = nreverse (properties);
23149 for (; properties; properties = TREE_CHAIN (properties))
23150 objc_add_property_declaration (loc, copy_node (properties),
23151 property_readonly, property_readwrite,
23152 property_assign, property_retain,
23153 property_copy, property_nonatomic,
23154 property_getter_ident, property_setter_ident);
23157 cp_parser_consume_semicolon_at_end_of_statement (parser);
23160 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23162 objc-synthesize-declaration:
23163 @synthesize objc-synthesize-identifier-list ;
23165 objc-synthesize-identifier-list:
23166 objc-synthesize-identifier
23167 objc-synthesize-identifier-list, objc-synthesize-identifier
23169 objc-synthesize-identifier
23170 identifier
23171 identifier = identifier
23173 For example:
23174 @synthesize MyProperty;
23175 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23177 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23178 for C. Keep them in sync.
23180 static void
23181 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23183 tree list = NULL_TREE;
23184 location_t loc;
23185 loc = cp_lexer_peek_token (parser->lexer)->location;
23187 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23188 while (true)
23190 tree property, ivar;
23191 property = cp_parser_identifier (parser);
23192 if (property == error_mark_node)
23194 cp_parser_consume_semicolon_at_end_of_statement (parser);
23195 return;
23197 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23199 cp_lexer_consume_token (parser->lexer);
23200 ivar = cp_parser_identifier (parser);
23201 if (ivar == error_mark_node)
23203 cp_parser_consume_semicolon_at_end_of_statement (parser);
23204 return;
23207 else
23208 ivar = NULL_TREE;
23209 list = chainon (list, build_tree_list (ivar, property));
23210 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23211 cp_lexer_consume_token (parser->lexer);
23212 else
23213 break;
23215 cp_parser_consume_semicolon_at_end_of_statement (parser);
23216 objc_add_synthesize_declaration (loc, list);
23219 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23221 objc-dynamic-declaration:
23222 @dynamic identifier-list ;
23224 For example:
23225 @dynamic MyProperty;
23226 @dynamic MyProperty, AnotherProperty;
23228 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23229 for C. Keep them in sync.
23231 static void
23232 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23234 tree list = NULL_TREE;
23235 location_t loc;
23236 loc = cp_lexer_peek_token (parser->lexer)->location;
23238 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23239 while (true)
23241 tree property;
23242 property = cp_parser_identifier (parser);
23243 if (property == error_mark_node)
23245 cp_parser_consume_semicolon_at_end_of_statement (parser);
23246 return;
23248 list = chainon (list, build_tree_list (NULL, property));
23249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23250 cp_lexer_consume_token (parser->lexer);
23251 else
23252 break;
23254 cp_parser_consume_semicolon_at_end_of_statement (parser);
23255 objc_add_dynamic_declaration (loc, list);
23259 /* OpenMP 2.5 parsing routines. */
23261 /* Returns name of the next clause.
23262 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23263 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23264 returned and the token is consumed. */
23266 static pragma_omp_clause
23267 cp_parser_omp_clause_name (cp_parser *parser)
23269 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23272 result = PRAGMA_OMP_CLAUSE_IF;
23273 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23274 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23275 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23276 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23277 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23280 const char *p = IDENTIFIER_POINTER (id);
23282 switch (p[0])
23284 case 'c':
23285 if (!strcmp ("collapse", p))
23286 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23287 else if (!strcmp ("copyin", p))
23288 result = PRAGMA_OMP_CLAUSE_COPYIN;
23289 else if (!strcmp ("copyprivate", p))
23290 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23291 break;
23292 case 'f':
23293 if (!strcmp ("firstprivate", p))
23294 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23295 break;
23296 case 'l':
23297 if (!strcmp ("lastprivate", p))
23298 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23299 break;
23300 case 'n':
23301 if (!strcmp ("nowait", p))
23302 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23303 else if (!strcmp ("num_threads", p))
23304 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23305 break;
23306 case 'o':
23307 if (!strcmp ("ordered", p))
23308 result = PRAGMA_OMP_CLAUSE_ORDERED;
23309 break;
23310 case 'r':
23311 if (!strcmp ("reduction", p))
23312 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23313 break;
23314 case 's':
23315 if (!strcmp ("schedule", p))
23316 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23317 else if (!strcmp ("shared", p))
23318 result = PRAGMA_OMP_CLAUSE_SHARED;
23319 break;
23320 case 'u':
23321 if (!strcmp ("untied", p))
23322 result = PRAGMA_OMP_CLAUSE_UNTIED;
23323 break;
23327 if (result != PRAGMA_OMP_CLAUSE_NONE)
23328 cp_lexer_consume_token (parser->lexer);
23330 return result;
23333 /* Validate that a clause of the given type does not already exist. */
23335 static void
23336 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23337 const char *name, location_t location)
23339 tree c;
23341 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23342 if (OMP_CLAUSE_CODE (c) == code)
23344 error_at (location, "too many %qs clauses", name);
23345 break;
23349 /* OpenMP 2.5:
23350 variable-list:
23351 identifier
23352 variable-list , identifier
23354 In addition, we match a closing parenthesis. An opening parenthesis
23355 will have been consumed by the caller.
23357 If KIND is nonzero, create the appropriate node and install the decl
23358 in OMP_CLAUSE_DECL and add the node to the head of the list.
23360 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23361 return the list created. */
23363 static tree
23364 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23365 tree list)
23367 cp_token *token;
23368 while (1)
23370 tree name, decl;
23372 token = cp_lexer_peek_token (parser->lexer);
23373 name = cp_parser_id_expression (parser, /*template_p=*/false,
23374 /*check_dependency_p=*/true,
23375 /*template_p=*/NULL,
23376 /*declarator_p=*/false,
23377 /*optional_p=*/false);
23378 if (name == error_mark_node)
23379 goto skip_comma;
23381 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23382 if (decl == error_mark_node)
23383 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23384 token->location);
23385 else if (kind != 0)
23387 tree u = build_omp_clause (token->location, kind);
23388 OMP_CLAUSE_DECL (u) = decl;
23389 OMP_CLAUSE_CHAIN (u) = list;
23390 list = u;
23392 else
23393 list = tree_cons (decl, NULL_TREE, list);
23395 get_comma:
23396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23397 break;
23398 cp_lexer_consume_token (parser->lexer);
23401 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23403 int ending;
23405 /* Try to resync to an unnested comma. Copied from
23406 cp_parser_parenthesized_expression_list. */
23407 skip_comma:
23408 ending = cp_parser_skip_to_closing_parenthesis (parser,
23409 /*recovering=*/true,
23410 /*or_comma=*/true,
23411 /*consume_paren=*/true);
23412 if (ending < 0)
23413 goto get_comma;
23416 return list;
23419 /* Similarly, but expect leading and trailing parenthesis. This is a very
23420 common case for omp clauses. */
23422 static tree
23423 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23425 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23426 return cp_parser_omp_var_list_no_open (parser, kind, list);
23427 return list;
23430 /* OpenMP 3.0:
23431 collapse ( constant-expression ) */
23433 static tree
23434 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23436 tree c, num;
23437 location_t loc;
23438 HOST_WIDE_INT n;
23440 loc = cp_lexer_peek_token (parser->lexer)->location;
23441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23442 return list;
23444 num = cp_parser_constant_expression (parser, false, NULL);
23446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23448 /*or_comma=*/false,
23449 /*consume_paren=*/true);
23451 if (num == error_mark_node)
23452 return list;
23453 num = fold_non_dependent_expr (num);
23454 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23455 || !host_integerp (num, 0)
23456 || (n = tree_low_cst (num, 0)) <= 0
23457 || (int) n != n)
23459 error_at (loc, "collapse argument needs positive constant integer expression");
23460 return list;
23463 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23464 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23465 OMP_CLAUSE_CHAIN (c) = list;
23466 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23468 return c;
23471 /* OpenMP 2.5:
23472 default ( shared | none ) */
23474 static tree
23475 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23477 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23478 tree c;
23480 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23481 return list;
23482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23485 const char *p = IDENTIFIER_POINTER (id);
23487 switch (p[0])
23489 case 'n':
23490 if (strcmp ("none", p) != 0)
23491 goto invalid_kind;
23492 kind = OMP_CLAUSE_DEFAULT_NONE;
23493 break;
23495 case 's':
23496 if (strcmp ("shared", p) != 0)
23497 goto invalid_kind;
23498 kind = OMP_CLAUSE_DEFAULT_SHARED;
23499 break;
23501 default:
23502 goto invalid_kind;
23505 cp_lexer_consume_token (parser->lexer);
23507 else
23509 invalid_kind:
23510 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23513 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23514 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23515 /*or_comma=*/false,
23516 /*consume_paren=*/true);
23518 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23519 return list;
23521 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23522 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23523 OMP_CLAUSE_CHAIN (c) = list;
23524 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23526 return c;
23529 /* OpenMP 2.5:
23530 if ( expression ) */
23532 static tree
23533 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23535 tree t, c;
23537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23538 return list;
23540 t = cp_parser_condition (parser);
23542 if (t == error_mark_node
23543 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23545 /*or_comma=*/false,
23546 /*consume_paren=*/true);
23548 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23550 c = build_omp_clause (location, OMP_CLAUSE_IF);
23551 OMP_CLAUSE_IF_EXPR (c) = t;
23552 OMP_CLAUSE_CHAIN (c) = list;
23554 return c;
23557 /* OpenMP 2.5:
23558 nowait */
23560 static tree
23561 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23562 tree list, location_t location)
23564 tree c;
23566 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23568 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23569 OMP_CLAUSE_CHAIN (c) = list;
23570 return c;
23573 /* OpenMP 2.5:
23574 num_threads ( expression ) */
23576 static tree
23577 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23578 location_t location)
23580 tree t, c;
23582 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23583 return list;
23585 t = cp_parser_expression (parser, false, NULL);
23587 if (t == error_mark_node
23588 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23589 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23590 /*or_comma=*/false,
23591 /*consume_paren=*/true);
23593 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23594 "num_threads", location);
23596 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23597 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23598 OMP_CLAUSE_CHAIN (c) = list;
23600 return c;
23603 /* OpenMP 2.5:
23604 ordered */
23606 static tree
23607 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23608 tree list, location_t location)
23610 tree c;
23612 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23613 "ordered", location);
23615 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23616 OMP_CLAUSE_CHAIN (c) = list;
23617 return c;
23620 /* OpenMP 2.5:
23621 reduction ( reduction-operator : variable-list )
23623 reduction-operator:
23624 One of: + * - & ^ | && || */
23626 static tree
23627 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23629 enum tree_code code;
23630 tree nlist, c;
23632 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23633 return list;
23635 switch (cp_lexer_peek_token (parser->lexer)->type)
23637 case CPP_PLUS:
23638 code = PLUS_EXPR;
23639 break;
23640 case CPP_MULT:
23641 code = MULT_EXPR;
23642 break;
23643 case CPP_MINUS:
23644 code = MINUS_EXPR;
23645 break;
23646 case CPP_AND:
23647 code = BIT_AND_EXPR;
23648 break;
23649 case CPP_XOR:
23650 code = BIT_XOR_EXPR;
23651 break;
23652 case CPP_OR:
23653 code = BIT_IOR_EXPR;
23654 break;
23655 case CPP_AND_AND:
23656 code = TRUTH_ANDIF_EXPR;
23657 break;
23658 case CPP_OR_OR:
23659 code = TRUTH_ORIF_EXPR;
23660 break;
23661 default:
23662 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23663 "%<|%>, %<&&%>, or %<||%>");
23664 resync_fail:
23665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23666 /*or_comma=*/false,
23667 /*consume_paren=*/true);
23668 return list;
23670 cp_lexer_consume_token (parser->lexer);
23672 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23673 goto resync_fail;
23675 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23676 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23677 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23679 return nlist;
23682 /* OpenMP 2.5:
23683 schedule ( schedule-kind )
23684 schedule ( schedule-kind , expression )
23686 schedule-kind:
23687 static | dynamic | guided | runtime | auto */
23689 static tree
23690 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23692 tree c, t;
23694 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23695 return list;
23697 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23699 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23701 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23702 const char *p = IDENTIFIER_POINTER (id);
23704 switch (p[0])
23706 case 'd':
23707 if (strcmp ("dynamic", p) != 0)
23708 goto invalid_kind;
23709 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23710 break;
23712 case 'g':
23713 if (strcmp ("guided", p) != 0)
23714 goto invalid_kind;
23715 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23716 break;
23718 case 'r':
23719 if (strcmp ("runtime", p) != 0)
23720 goto invalid_kind;
23721 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23722 break;
23724 default:
23725 goto invalid_kind;
23728 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23729 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23730 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23731 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23732 else
23733 goto invalid_kind;
23734 cp_lexer_consume_token (parser->lexer);
23736 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23738 cp_token *token;
23739 cp_lexer_consume_token (parser->lexer);
23741 token = cp_lexer_peek_token (parser->lexer);
23742 t = cp_parser_assignment_expression (parser, false, NULL);
23744 if (t == error_mark_node)
23745 goto resync_fail;
23746 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23747 error_at (token->location, "schedule %<runtime%> does not take "
23748 "a %<chunk_size%> parameter");
23749 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23750 error_at (token->location, "schedule %<auto%> does not take "
23751 "a %<chunk_size%> parameter");
23752 else
23753 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23756 goto resync_fail;
23758 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23759 goto resync_fail;
23761 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23762 OMP_CLAUSE_CHAIN (c) = list;
23763 return c;
23765 invalid_kind:
23766 cp_parser_error (parser, "invalid schedule kind");
23767 resync_fail:
23768 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23769 /*or_comma=*/false,
23770 /*consume_paren=*/true);
23771 return list;
23774 /* OpenMP 3.0:
23775 untied */
23777 static tree
23778 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23779 tree list, location_t location)
23781 tree c;
23783 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23785 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23786 OMP_CLAUSE_CHAIN (c) = list;
23787 return c;
23790 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23791 is a bitmask in MASK. Return the list of clauses found; the result
23792 of clause default goes in *pdefault. */
23794 static tree
23795 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23796 const char *where, cp_token *pragma_tok)
23798 tree clauses = NULL;
23799 bool first = true;
23800 cp_token *token = NULL;
23802 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23804 pragma_omp_clause c_kind;
23805 const char *c_name;
23806 tree prev = clauses;
23808 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23809 cp_lexer_consume_token (parser->lexer);
23811 token = cp_lexer_peek_token (parser->lexer);
23812 c_kind = cp_parser_omp_clause_name (parser);
23813 first = false;
23815 switch (c_kind)
23817 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23818 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23819 token->location);
23820 c_name = "collapse";
23821 break;
23822 case PRAGMA_OMP_CLAUSE_COPYIN:
23823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23824 c_name = "copyin";
23825 break;
23826 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23827 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23828 clauses);
23829 c_name = "copyprivate";
23830 break;
23831 case PRAGMA_OMP_CLAUSE_DEFAULT:
23832 clauses = cp_parser_omp_clause_default (parser, clauses,
23833 token->location);
23834 c_name = "default";
23835 break;
23836 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23837 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23838 clauses);
23839 c_name = "firstprivate";
23840 break;
23841 case PRAGMA_OMP_CLAUSE_IF:
23842 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23843 c_name = "if";
23844 break;
23845 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23846 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23847 clauses);
23848 c_name = "lastprivate";
23849 break;
23850 case PRAGMA_OMP_CLAUSE_NOWAIT:
23851 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23852 c_name = "nowait";
23853 break;
23854 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23855 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23856 token->location);
23857 c_name = "num_threads";
23858 break;
23859 case PRAGMA_OMP_CLAUSE_ORDERED:
23860 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23861 token->location);
23862 c_name = "ordered";
23863 break;
23864 case PRAGMA_OMP_CLAUSE_PRIVATE:
23865 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23866 clauses);
23867 c_name = "private";
23868 break;
23869 case PRAGMA_OMP_CLAUSE_REDUCTION:
23870 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23871 c_name = "reduction";
23872 break;
23873 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23874 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23875 token->location);
23876 c_name = "schedule";
23877 break;
23878 case PRAGMA_OMP_CLAUSE_SHARED:
23879 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23880 clauses);
23881 c_name = "shared";
23882 break;
23883 case PRAGMA_OMP_CLAUSE_UNTIED:
23884 clauses = cp_parser_omp_clause_untied (parser, clauses,
23885 token->location);
23886 c_name = "nowait";
23887 break;
23888 default:
23889 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23890 goto saw_error;
23893 if (((mask >> c_kind) & 1) == 0)
23895 /* Remove the invalid clause(s) from the list to avoid
23896 confusing the rest of the compiler. */
23897 clauses = prev;
23898 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23901 saw_error:
23902 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23903 return finish_omp_clauses (clauses);
23906 /* OpenMP 2.5:
23907 structured-block:
23908 statement
23910 In practice, we're also interested in adding the statement to an
23911 outer node. So it is convenient if we work around the fact that
23912 cp_parser_statement calls add_stmt. */
23914 static unsigned
23915 cp_parser_begin_omp_structured_block (cp_parser *parser)
23917 unsigned save = parser->in_statement;
23919 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23920 This preserves the "not within loop or switch" style error messages
23921 for nonsense cases like
23922 void foo() {
23923 #pragma omp single
23924 break;
23927 if (parser->in_statement)
23928 parser->in_statement = IN_OMP_BLOCK;
23930 return save;
23933 static void
23934 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23936 parser->in_statement = save;
23939 static tree
23940 cp_parser_omp_structured_block (cp_parser *parser)
23942 tree stmt = begin_omp_structured_block ();
23943 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23945 cp_parser_statement (parser, NULL_TREE, false, NULL);
23947 cp_parser_end_omp_structured_block (parser, save);
23948 return finish_omp_structured_block (stmt);
23951 /* OpenMP 2.5:
23952 # pragma omp atomic new-line
23953 expression-stmt
23955 expression-stmt:
23956 x binop= expr | x++ | ++x | x-- | --x
23957 binop:
23958 +, *, -, /, &, ^, |, <<, >>
23960 where x is an lvalue expression with scalar type. */
23962 static void
23963 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23965 tree lhs, rhs;
23966 enum tree_code code;
23968 cp_parser_require_pragma_eol (parser, pragma_tok);
23970 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23971 /*cast_p=*/false, NULL);
23972 switch (TREE_CODE (lhs))
23974 case ERROR_MARK:
23975 goto saw_error;
23977 case PREINCREMENT_EXPR:
23978 case POSTINCREMENT_EXPR:
23979 lhs = TREE_OPERAND (lhs, 0);
23980 code = PLUS_EXPR;
23981 rhs = integer_one_node;
23982 break;
23984 case PREDECREMENT_EXPR:
23985 case POSTDECREMENT_EXPR:
23986 lhs = TREE_OPERAND (lhs, 0);
23987 code = MINUS_EXPR;
23988 rhs = integer_one_node;
23989 break;
23991 case COMPOUND_EXPR:
23992 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23993 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23994 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23995 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23996 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23997 (TREE_OPERAND (lhs, 1), 0), 0)))
23998 == BOOLEAN_TYPE)
23999 /* Undo effects of boolean_increment for post {in,de}crement. */
24000 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24001 /* FALLTHRU */
24002 case MODIFY_EXPR:
24003 if (TREE_CODE (lhs) == MODIFY_EXPR
24004 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24006 /* Undo effects of boolean_increment. */
24007 if (integer_onep (TREE_OPERAND (lhs, 1)))
24009 /* This is pre or post increment. */
24010 rhs = TREE_OPERAND (lhs, 1);
24011 lhs = TREE_OPERAND (lhs, 0);
24012 code = NOP_EXPR;
24013 break;
24016 /* FALLTHRU */
24017 default:
24018 switch (cp_lexer_peek_token (parser->lexer)->type)
24020 case CPP_MULT_EQ:
24021 code = MULT_EXPR;
24022 break;
24023 case CPP_DIV_EQ:
24024 code = TRUNC_DIV_EXPR;
24025 break;
24026 case CPP_PLUS_EQ:
24027 code = PLUS_EXPR;
24028 break;
24029 case CPP_MINUS_EQ:
24030 code = MINUS_EXPR;
24031 break;
24032 case CPP_LSHIFT_EQ:
24033 code = LSHIFT_EXPR;
24034 break;
24035 case CPP_RSHIFT_EQ:
24036 code = RSHIFT_EXPR;
24037 break;
24038 case CPP_AND_EQ:
24039 code = BIT_AND_EXPR;
24040 break;
24041 case CPP_OR_EQ:
24042 code = BIT_IOR_EXPR;
24043 break;
24044 case CPP_XOR_EQ:
24045 code = BIT_XOR_EXPR;
24046 break;
24047 default:
24048 cp_parser_error (parser,
24049 "invalid operator for %<#pragma omp atomic%>");
24050 goto saw_error;
24052 cp_lexer_consume_token (parser->lexer);
24054 rhs = cp_parser_expression (parser, false, NULL);
24055 if (rhs == error_mark_node)
24056 goto saw_error;
24057 break;
24059 finish_omp_atomic (code, lhs, rhs);
24060 cp_parser_consume_semicolon_at_end_of_statement (parser);
24061 return;
24063 saw_error:
24064 cp_parser_skip_to_end_of_block_or_statement (parser);
24068 /* OpenMP 2.5:
24069 # pragma omp barrier new-line */
24071 static void
24072 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24074 cp_parser_require_pragma_eol (parser, pragma_tok);
24075 finish_omp_barrier ();
24078 /* OpenMP 2.5:
24079 # pragma omp critical [(name)] new-line
24080 structured-block */
24082 static tree
24083 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24085 tree stmt, name = NULL;
24087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24089 cp_lexer_consume_token (parser->lexer);
24091 name = cp_parser_identifier (parser);
24093 if (name == error_mark_node
24094 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24095 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24096 /*or_comma=*/false,
24097 /*consume_paren=*/true);
24098 if (name == error_mark_node)
24099 name = NULL;
24101 cp_parser_require_pragma_eol (parser, pragma_tok);
24103 stmt = cp_parser_omp_structured_block (parser);
24104 return c_finish_omp_critical (input_location, stmt, name);
24107 /* OpenMP 2.5:
24108 # pragma omp flush flush-vars[opt] new-line
24110 flush-vars:
24111 ( variable-list ) */
24113 static void
24114 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24117 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24118 cp_parser_require_pragma_eol (parser, pragma_tok);
24120 finish_omp_flush ();
24123 /* Helper function, to parse omp for increment expression. */
24125 static tree
24126 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24128 tree cond = cp_parser_binary_expression (parser, false, true,
24129 PREC_NOT_OPERATOR, NULL);
24130 bool overloaded_p;
24132 if (cond == error_mark_node
24133 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24135 cp_parser_skip_to_end_of_statement (parser);
24136 return error_mark_node;
24139 switch (TREE_CODE (cond))
24141 case GT_EXPR:
24142 case GE_EXPR:
24143 case LT_EXPR:
24144 case LE_EXPR:
24145 break;
24146 default:
24147 return error_mark_node;
24150 /* If decl is an iterator, preserve LHS and RHS of the relational
24151 expr until finish_omp_for. */
24152 if (decl
24153 && (type_dependent_expression_p (decl)
24154 || CLASS_TYPE_P (TREE_TYPE (decl))))
24155 return cond;
24157 return build_x_binary_op (TREE_CODE (cond),
24158 TREE_OPERAND (cond, 0), ERROR_MARK,
24159 TREE_OPERAND (cond, 1), ERROR_MARK,
24160 &overloaded_p, tf_warning_or_error);
24163 /* Helper function, to parse omp for increment expression. */
24165 static tree
24166 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24168 cp_token *token = cp_lexer_peek_token (parser->lexer);
24169 enum tree_code op;
24170 tree lhs, rhs;
24171 cp_id_kind idk;
24172 bool decl_first;
24174 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24176 op = (token->type == CPP_PLUS_PLUS
24177 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24178 cp_lexer_consume_token (parser->lexer);
24179 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24180 if (lhs != decl)
24181 return error_mark_node;
24182 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24185 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24186 if (lhs != decl)
24187 return error_mark_node;
24189 token = cp_lexer_peek_token (parser->lexer);
24190 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24192 op = (token->type == CPP_PLUS_PLUS
24193 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24194 cp_lexer_consume_token (parser->lexer);
24195 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24198 op = cp_parser_assignment_operator_opt (parser);
24199 if (op == ERROR_MARK)
24200 return error_mark_node;
24202 if (op != NOP_EXPR)
24204 rhs = cp_parser_assignment_expression (parser, false, NULL);
24205 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24206 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24209 lhs = cp_parser_binary_expression (parser, false, false,
24210 PREC_ADDITIVE_EXPRESSION, NULL);
24211 token = cp_lexer_peek_token (parser->lexer);
24212 decl_first = lhs == decl;
24213 if (decl_first)
24214 lhs = NULL_TREE;
24215 if (token->type != CPP_PLUS
24216 && token->type != CPP_MINUS)
24217 return error_mark_node;
24221 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24222 cp_lexer_consume_token (parser->lexer);
24223 rhs = cp_parser_binary_expression (parser, false, false,
24224 PREC_ADDITIVE_EXPRESSION, NULL);
24225 token = cp_lexer_peek_token (parser->lexer);
24226 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24228 if (lhs == NULL_TREE)
24230 if (op == PLUS_EXPR)
24231 lhs = rhs;
24232 else
24233 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24235 else
24236 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24237 NULL, tf_warning_or_error);
24240 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24242 if (!decl_first)
24244 if (rhs != decl || op == MINUS_EXPR)
24245 return error_mark_node;
24246 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24248 else
24249 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24251 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24254 /* Parse the restricted form of the for statement allowed by OpenMP. */
24256 static tree
24257 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24259 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24260 tree real_decl, initv, condv, incrv, declv;
24261 tree this_pre_body, cl;
24262 location_t loc_first;
24263 bool collapse_err = false;
24264 int i, collapse = 1, nbraces = 0;
24265 VEC(tree,gc) *for_block = make_tree_vector ();
24267 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24268 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24269 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24271 gcc_assert (collapse >= 1);
24273 declv = make_tree_vec (collapse);
24274 initv = make_tree_vec (collapse);
24275 condv = make_tree_vec (collapse);
24276 incrv = make_tree_vec (collapse);
24278 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24280 for (i = 0; i < collapse; i++)
24282 int bracecount = 0;
24283 bool add_private_clause = false;
24284 location_t loc;
24286 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24288 cp_parser_error (parser, "for statement expected");
24289 return NULL;
24291 loc = cp_lexer_consume_token (parser->lexer)->location;
24293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24294 return NULL;
24296 init = decl = real_decl = NULL;
24297 this_pre_body = push_stmt_list ();
24298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24300 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24302 init-expr:
24303 var = lb
24304 integer-type var = lb
24305 random-access-iterator-type var = lb
24306 pointer-type var = lb
24308 cp_decl_specifier_seq type_specifiers;
24310 /* First, try to parse as an initialized declaration. See
24311 cp_parser_condition, from whence the bulk of this is copied. */
24313 cp_parser_parse_tentatively (parser);
24314 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24315 /*is_trailing_return=*/false,
24316 &type_specifiers);
24317 if (cp_parser_parse_definitely (parser))
24319 /* If parsing a type specifier seq succeeded, then this
24320 MUST be a initialized declaration. */
24321 tree asm_specification, attributes;
24322 cp_declarator *declarator;
24324 declarator = cp_parser_declarator (parser,
24325 CP_PARSER_DECLARATOR_NAMED,
24326 /*ctor_dtor_or_conv_p=*/NULL,
24327 /*parenthesized_p=*/NULL,
24328 /*member_p=*/false);
24329 attributes = cp_parser_attributes_opt (parser);
24330 asm_specification = cp_parser_asm_specification_opt (parser);
24332 if (declarator == cp_error_declarator)
24333 cp_parser_skip_to_end_of_statement (parser);
24335 else
24337 tree pushed_scope, auto_node;
24339 decl = start_decl (declarator, &type_specifiers,
24340 SD_INITIALIZED, attributes,
24341 /*prefix_attributes=*/NULL_TREE,
24342 &pushed_scope);
24344 auto_node = type_uses_auto (TREE_TYPE (decl));
24345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24347 if (cp_lexer_next_token_is (parser->lexer,
24348 CPP_OPEN_PAREN))
24349 error ("parenthesized initialization is not allowed in "
24350 "OpenMP %<for%> loop");
24351 else
24352 /* Trigger an error. */
24353 cp_parser_require (parser, CPP_EQ, RT_EQ);
24355 init = error_mark_node;
24356 cp_parser_skip_to_end_of_statement (parser);
24358 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24359 || type_dependent_expression_p (decl)
24360 || auto_node)
24362 bool is_direct_init, is_non_constant_init;
24364 init = cp_parser_initializer (parser,
24365 &is_direct_init,
24366 &is_non_constant_init);
24368 if (auto_node && describable_type (init))
24370 TREE_TYPE (decl)
24371 = do_auto_deduction (TREE_TYPE (decl), init,
24372 auto_node);
24374 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24375 && !type_dependent_expression_p (decl))
24376 goto non_class;
24379 cp_finish_decl (decl, init, !is_non_constant_init,
24380 asm_specification,
24381 LOOKUP_ONLYCONVERTING);
24382 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24384 VEC_safe_push (tree, gc, for_block, this_pre_body);
24385 init = NULL_TREE;
24387 else
24388 init = pop_stmt_list (this_pre_body);
24389 this_pre_body = NULL_TREE;
24391 else
24393 /* Consume '='. */
24394 cp_lexer_consume_token (parser->lexer);
24395 init = cp_parser_assignment_expression (parser, false, NULL);
24397 non_class:
24398 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24399 init = error_mark_node;
24400 else
24401 cp_finish_decl (decl, NULL_TREE,
24402 /*init_const_expr_p=*/false,
24403 asm_specification,
24404 LOOKUP_ONLYCONVERTING);
24407 if (pushed_scope)
24408 pop_scope (pushed_scope);
24411 else
24413 cp_id_kind idk;
24414 /* If parsing a type specifier sequence failed, then
24415 this MUST be a simple expression. */
24416 cp_parser_parse_tentatively (parser);
24417 decl = cp_parser_primary_expression (parser, false, false,
24418 false, &idk);
24419 if (!cp_parser_error_occurred (parser)
24420 && decl
24421 && DECL_P (decl)
24422 && CLASS_TYPE_P (TREE_TYPE (decl)))
24424 tree rhs;
24426 cp_parser_parse_definitely (parser);
24427 cp_parser_require (parser, CPP_EQ, RT_EQ);
24428 rhs = cp_parser_assignment_expression (parser, false, NULL);
24429 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24430 rhs,
24431 tf_warning_or_error));
24432 add_private_clause = true;
24434 else
24436 decl = NULL;
24437 cp_parser_abort_tentative_parse (parser);
24438 init = cp_parser_expression (parser, false, NULL);
24439 if (init)
24441 if (TREE_CODE (init) == MODIFY_EXPR
24442 || TREE_CODE (init) == MODOP_EXPR)
24443 real_decl = TREE_OPERAND (init, 0);
24448 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24449 if (this_pre_body)
24451 this_pre_body = pop_stmt_list (this_pre_body);
24452 if (pre_body)
24454 tree t = pre_body;
24455 pre_body = push_stmt_list ();
24456 add_stmt (t);
24457 add_stmt (this_pre_body);
24458 pre_body = pop_stmt_list (pre_body);
24460 else
24461 pre_body = this_pre_body;
24464 if (decl)
24465 real_decl = decl;
24466 if (par_clauses != NULL && real_decl != NULL_TREE)
24468 tree *c;
24469 for (c = par_clauses; *c ; )
24470 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24471 && OMP_CLAUSE_DECL (*c) == real_decl)
24473 error_at (loc, "iteration variable %qD"
24474 " should not be firstprivate", real_decl);
24475 *c = OMP_CLAUSE_CHAIN (*c);
24477 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24478 && OMP_CLAUSE_DECL (*c) == real_decl)
24480 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24481 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24482 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24483 OMP_CLAUSE_DECL (l) = real_decl;
24484 OMP_CLAUSE_CHAIN (l) = clauses;
24485 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24486 clauses = l;
24487 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24488 CP_OMP_CLAUSE_INFO (*c) = NULL;
24489 add_private_clause = false;
24491 else
24493 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24494 && OMP_CLAUSE_DECL (*c) == real_decl)
24495 add_private_clause = false;
24496 c = &OMP_CLAUSE_CHAIN (*c);
24500 if (add_private_clause)
24502 tree c;
24503 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24505 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24507 && OMP_CLAUSE_DECL (c) == decl)
24508 break;
24509 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24510 && OMP_CLAUSE_DECL (c) == decl)
24511 error_at (loc, "iteration variable %qD "
24512 "should not be firstprivate",
24513 decl);
24514 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24515 && OMP_CLAUSE_DECL (c) == decl)
24516 error_at (loc, "iteration variable %qD should not be reduction",
24517 decl);
24519 if (c == NULL)
24521 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24522 OMP_CLAUSE_DECL (c) = decl;
24523 c = finish_omp_clauses (c);
24524 if (c)
24526 OMP_CLAUSE_CHAIN (c) = clauses;
24527 clauses = c;
24532 cond = NULL;
24533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24534 cond = cp_parser_omp_for_cond (parser, decl);
24535 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24537 incr = NULL;
24538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24540 /* If decl is an iterator, preserve the operator on decl
24541 until finish_omp_for. */
24542 if (decl
24543 && (type_dependent_expression_p (decl)
24544 || CLASS_TYPE_P (TREE_TYPE (decl))))
24545 incr = cp_parser_omp_for_incr (parser, decl);
24546 else
24547 incr = cp_parser_expression (parser, false, NULL);
24550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24552 /*or_comma=*/false,
24553 /*consume_paren=*/true);
24555 TREE_VEC_ELT (declv, i) = decl;
24556 TREE_VEC_ELT (initv, i) = init;
24557 TREE_VEC_ELT (condv, i) = cond;
24558 TREE_VEC_ELT (incrv, i) = incr;
24560 if (i == collapse - 1)
24561 break;
24563 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24564 in between the collapsed for loops to be still considered perfectly
24565 nested. Hopefully the final version clarifies this.
24566 For now handle (multiple) {'s and empty statements. */
24567 cp_parser_parse_tentatively (parser);
24570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24571 break;
24572 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24574 cp_lexer_consume_token (parser->lexer);
24575 bracecount++;
24577 else if (bracecount
24578 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24579 cp_lexer_consume_token (parser->lexer);
24580 else
24582 loc = cp_lexer_peek_token (parser->lexer)->location;
24583 error_at (loc, "not enough collapsed for loops");
24584 collapse_err = true;
24585 cp_parser_abort_tentative_parse (parser);
24586 declv = NULL_TREE;
24587 break;
24590 while (1);
24592 if (declv)
24594 cp_parser_parse_definitely (parser);
24595 nbraces += bracecount;
24599 /* Note that we saved the original contents of this flag when we entered
24600 the structured block, and so we don't need to re-save it here. */
24601 parser->in_statement = IN_OMP_FOR;
24603 /* Note that the grammar doesn't call for a structured block here,
24604 though the loop as a whole is a structured block. */
24605 body = push_stmt_list ();
24606 cp_parser_statement (parser, NULL_TREE, false, NULL);
24607 body = pop_stmt_list (body);
24609 if (declv == NULL_TREE)
24610 ret = NULL_TREE;
24611 else
24612 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24613 pre_body, clauses);
24615 while (nbraces)
24617 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24619 cp_lexer_consume_token (parser->lexer);
24620 nbraces--;
24622 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24623 cp_lexer_consume_token (parser->lexer);
24624 else
24626 if (!collapse_err)
24628 error_at (cp_lexer_peek_token (parser->lexer)->location,
24629 "collapsed loops not perfectly nested");
24631 collapse_err = true;
24632 cp_parser_statement_seq_opt (parser, NULL);
24633 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24634 break;
24638 while (!VEC_empty (tree, for_block))
24639 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24640 release_tree_vector (for_block);
24642 return ret;
24645 /* OpenMP 2.5:
24646 #pragma omp for for-clause[optseq] new-line
24647 for-loop */
24649 #define OMP_FOR_CLAUSE_MASK \
24650 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24651 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24652 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24653 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24654 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24655 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24656 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24657 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24659 static tree
24660 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24662 tree clauses, sb, ret;
24663 unsigned int save;
24665 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24666 "#pragma omp for", pragma_tok);
24668 sb = begin_omp_structured_block ();
24669 save = cp_parser_begin_omp_structured_block (parser);
24671 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24673 cp_parser_end_omp_structured_block (parser, save);
24674 add_stmt (finish_omp_structured_block (sb));
24676 return ret;
24679 /* OpenMP 2.5:
24680 # pragma omp master new-line
24681 structured-block */
24683 static tree
24684 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24686 cp_parser_require_pragma_eol (parser, pragma_tok);
24687 return c_finish_omp_master (input_location,
24688 cp_parser_omp_structured_block (parser));
24691 /* OpenMP 2.5:
24692 # pragma omp ordered new-line
24693 structured-block */
24695 static tree
24696 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24698 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24699 cp_parser_require_pragma_eol (parser, pragma_tok);
24700 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24703 /* OpenMP 2.5:
24705 section-scope:
24706 { section-sequence }
24708 section-sequence:
24709 section-directive[opt] structured-block
24710 section-sequence section-directive structured-block */
24712 static tree
24713 cp_parser_omp_sections_scope (cp_parser *parser)
24715 tree stmt, substmt;
24716 bool error_suppress = false;
24717 cp_token *tok;
24719 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24720 return NULL_TREE;
24722 stmt = push_stmt_list ();
24724 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24726 unsigned save;
24728 substmt = begin_omp_structured_block ();
24729 save = cp_parser_begin_omp_structured_block (parser);
24731 while (1)
24733 cp_parser_statement (parser, NULL_TREE, false, NULL);
24735 tok = cp_lexer_peek_token (parser->lexer);
24736 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24737 break;
24738 if (tok->type == CPP_CLOSE_BRACE)
24739 break;
24740 if (tok->type == CPP_EOF)
24741 break;
24744 cp_parser_end_omp_structured_block (parser, save);
24745 substmt = finish_omp_structured_block (substmt);
24746 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24747 add_stmt (substmt);
24750 while (1)
24752 tok = cp_lexer_peek_token (parser->lexer);
24753 if (tok->type == CPP_CLOSE_BRACE)
24754 break;
24755 if (tok->type == CPP_EOF)
24756 break;
24758 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24760 cp_lexer_consume_token (parser->lexer);
24761 cp_parser_require_pragma_eol (parser, tok);
24762 error_suppress = false;
24764 else if (!error_suppress)
24766 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24767 error_suppress = true;
24770 substmt = cp_parser_omp_structured_block (parser);
24771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24772 add_stmt (substmt);
24774 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24776 substmt = pop_stmt_list (stmt);
24778 stmt = make_node (OMP_SECTIONS);
24779 TREE_TYPE (stmt) = void_type_node;
24780 OMP_SECTIONS_BODY (stmt) = substmt;
24782 add_stmt (stmt);
24783 return stmt;
24786 /* OpenMP 2.5:
24787 # pragma omp sections sections-clause[optseq] newline
24788 sections-scope */
24790 #define OMP_SECTIONS_CLAUSE_MASK \
24791 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24792 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24793 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24794 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24795 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24797 static tree
24798 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24800 tree clauses, ret;
24802 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24803 "#pragma omp sections", pragma_tok);
24805 ret = cp_parser_omp_sections_scope (parser);
24806 if (ret)
24807 OMP_SECTIONS_CLAUSES (ret) = clauses;
24809 return ret;
24812 /* OpenMP 2.5:
24813 # pragma parallel parallel-clause new-line
24814 # pragma parallel for parallel-for-clause new-line
24815 # pragma parallel sections parallel-sections-clause new-line */
24817 #define OMP_PARALLEL_CLAUSE_MASK \
24818 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24819 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24820 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24821 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24822 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24823 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24824 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24825 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24827 static tree
24828 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24830 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24831 const char *p_name = "#pragma omp parallel";
24832 tree stmt, clauses, par_clause, ws_clause, block;
24833 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24834 unsigned int save;
24835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24839 cp_lexer_consume_token (parser->lexer);
24840 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24841 p_name = "#pragma omp parallel for";
24842 mask |= OMP_FOR_CLAUSE_MASK;
24843 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24845 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24848 const char *p = IDENTIFIER_POINTER (id);
24849 if (strcmp (p, "sections") == 0)
24851 cp_lexer_consume_token (parser->lexer);
24852 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24853 p_name = "#pragma omp parallel sections";
24854 mask |= OMP_SECTIONS_CLAUSE_MASK;
24855 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24859 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24860 block = begin_omp_parallel ();
24861 save = cp_parser_begin_omp_structured_block (parser);
24863 switch (p_kind)
24865 case PRAGMA_OMP_PARALLEL:
24866 cp_parser_statement (parser, NULL_TREE, false, NULL);
24867 par_clause = clauses;
24868 break;
24870 case PRAGMA_OMP_PARALLEL_FOR:
24871 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24872 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24873 break;
24875 case PRAGMA_OMP_PARALLEL_SECTIONS:
24876 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24877 stmt = cp_parser_omp_sections_scope (parser);
24878 if (stmt)
24879 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24880 break;
24882 default:
24883 gcc_unreachable ();
24886 cp_parser_end_omp_structured_block (parser, save);
24887 stmt = finish_omp_parallel (par_clause, block);
24888 if (p_kind != PRAGMA_OMP_PARALLEL)
24889 OMP_PARALLEL_COMBINED (stmt) = 1;
24890 return stmt;
24893 /* OpenMP 2.5:
24894 # pragma omp single single-clause[optseq] new-line
24895 structured-block */
24897 #define OMP_SINGLE_CLAUSE_MASK \
24898 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24899 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24900 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24901 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24903 static tree
24904 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24906 tree stmt = make_node (OMP_SINGLE);
24907 TREE_TYPE (stmt) = void_type_node;
24909 OMP_SINGLE_CLAUSES (stmt)
24910 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24911 "#pragma omp single", pragma_tok);
24912 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24914 return add_stmt (stmt);
24917 /* OpenMP 3.0:
24918 # pragma omp task task-clause[optseq] new-line
24919 structured-block */
24921 #define OMP_TASK_CLAUSE_MASK \
24922 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24923 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24924 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24925 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24926 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24927 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24929 static tree
24930 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24932 tree clauses, block;
24933 unsigned int save;
24935 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24936 "#pragma omp task", pragma_tok);
24937 block = begin_omp_task ();
24938 save = cp_parser_begin_omp_structured_block (parser);
24939 cp_parser_statement (parser, NULL_TREE, false, NULL);
24940 cp_parser_end_omp_structured_block (parser, save);
24941 return finish_omp_task (clauses, block);
24944 /* OpenMP 3.0:
24945 # pragma omp taskwait new-line */
24947 static void
24948 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24950 cp_parser_require_pragma_eol (parser, pragma_tok);
24951 finish_omp_taskwait ();
24954 /* OpenMP 2.5:
24955 # pragma omp threadprivate (variable-list) */
24957 static void
24958 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24960 tree vars;
24962 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24963 cp_parser_require_pragma_eol (parser, pragma_tok);
24965 finish_omp_threadprivate (vars);
24968 /* Main entry point to OpenMP statement pragmas. */
24970 static void
24971 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24973 tree stmt;
24975 switch (pragma_tok->pragma_kind)
24977 case PRAGMA_OMP_ATOMIC:
24978 cp_parser_omp_atomic (parser, pragma_tok);
24979 return;
24980 case PRAGMA_OMP_CRITICAL:
24981 stmt = cp_parser_omp_critical (parser, pragma_tok);
24982 break;
24983 case PRAGMA_OMP_FOR:
24984 stmt = cp_parser_omp_for (parser, pragma_tok);
24985 break;
24986 case PRAGMA_OMP_MASTER:
24987 stmt = cp_parser_omp_master (parser, pragma_tok);
24988 break;
24989 case PRAGMA_OMP_ORDERED:
24990 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24991 break;
24992 case PRAGMA_OMP_PARALLEL:
24993 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24994 break;
24995 case PRAGMA_OMP_SECTIONS:
24996 stmt = cp_parser_omp_sections (parser, pragma_tok);
24997 break;
24998 case PRAGMA_OMP_SINGLE:
24999 stmt = cp_parser_omp_single (parser, pragma_tok);
25000 break;
25001 case PRAGMA_OMP_TASK:
25002 stmt = cp_parser_omp_task (parser, pragma_tok);
25003 break;
25004 default:
25005 gcc_unreachable ();
25008 if (stmt)
25009 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25012 /* The parser. */
25014 static GTY (()) cp_parser *the_parser;
25017 /* Special handling for the first token or line in the file. The first
25018 thing in the file might be #pragma GCC pch_preprocess, which loads a
25019 PCH file, which is a GC collection point. So we need to handle this
25020 first pragma without benefit of an existing lexer structure.
25022 Always returns one token to the caller in *FIRST_TOKEN. This is
25023 either the true first token of the file, or the first token after
25024 the initial pragma. */
25026 static void
25027 cp_parser_initial_pragma (cp_token *first_token)
25029 tree name = NULL;
25031 cp_lexer_get_preprocessor_token (NULL, first_token);
25032 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25033 return;
25035 cp_lexer_get_preprocessor_token (NULL, first_token);
25036 if (first_token->type == CPP_STRING)
25038 name = first_token->u.value;
25040 cp_lexer_get_preprocessor_token (NULL, first_token);
25041 if (first_token->type != CPP_PRAGMA_EOL)
25042 error_at (first_token->location,
25043 "junk at end of %<#pragma GCC pch_preprocess%>");
25045 else
25046 error_at (first_token->location, "expected string literal");
25048 /* Skip to the end of the pragma. */
25049 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25050 cp_lexer_get_preprocessor_token (NULL, first_token);
25052 /* Now actually load the PCH file. */
25053 if (name)
25054 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25056 /* Read one more token to return to our caller. We have to do this
25057 after reading the PCH file in, since its pointers have to be
25058 live. */
25059 cp_lexer_get_preprocessor_token (NULL, first_token);
25062 /* Normal parsing of a pragma token. Here we can (and must) use the
25063 regular lexer. */
25065 static bool
25066 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25068 cp_token *pragma_tok;
25069 unsigned int id;
25071 pragma_tok = cp_lexer_consume_token (parser->lexer);
25072 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25073 parser->lexer->in_pragma = true;
25075 id = pragma_tok->pragma_kind;
25076 switch (id)
25078 case PRAGMA_GCC_PCH_PREPROCESS:
25079 error_at (pragma_tok->location,
25080 "%<#pragma GCC pch_preprocess%> must be first");
25081 break;
25083 case PRAGMA_OMP_BARRIER:
25084 switch (context)
25086 case pragma_compound:
25087 cp_parser_omp_barrier (parser, pragma_tok);
25088 return false;
25089 case pragma_stmt:
25090 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25091 "used in compound statements");
25092 break;
25093 default:
25094 goto bad_stmt;
25096 break;
25098 case PRAGMA_OMP_FLUSH:
25099 switch (context)
25101 case pragma_compound:
25102 cp_parser_omp_flush (parser, pragma_tok);
25103 return false;
25104 case pragma_stmt:
25105 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25106 "used in compound statements");
25107 break;
25108 default:
25109 goto bad_stmt;
25111 break;
25113 case PRAGMA_OMP_TASKWAIT:
25114 switch (context)
25116 case pragma_compound:
25117 cp_parser_omp_taskwait (parser, pragma_tok);
25118 return false;
25119 case pragma_stmt:
25120 error_at (pragma_tok->location,
25121 "%<#pragma omp taskwait%> may only be "
25122 "used in compound statements");
25123 break;
25124 default:
25125 goto bad_stmt;
25127 break;
25129 case PRAGMA_OMP_THREADPRIVATE:
25130 cp_parser_omp_threadprivate (parser, pragma_tok);
25131 return false;
25133 case PRAGMA_OMP_ATOMIC:
25134 case PRAGMA_OMP_CRITICAL:
25135 case PRAGMA_OMP_FOR:
25136 case PRAGMA_OMP_MASTER:
25137 case PRAGMA_OMP_ORDERED:
25138 case PRAGMA_OMP_PARALLEL:
25139 case PRAGMA_OMP_SECTIONS:
25140 case PRAGMA_OMP_SINGLE:
25141 case PRAGMA_OMP_TASK:
25142 if (context == pragma_external)
25143 goto bad_stmt;
25144 cp_parser_omp_construct (parser, pragma_tok);
25145 return true;
25147 case PRAGMA_OMP_SECTION:
25148 error_at (pragma_tok->location,
25149 "%<#pragma omp section%> may only be used in "
25150 "%<#pragma omp sections%> construct");
25151 break;
25153 default:
25154 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25155 c_invoke_pragma_handler (id);
25156 break;
25158 bad_stmt:
25159 cp_parser_error (parser, "expected declaration specifiers");
25160 break;
25163 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25164 return false;
25167 /* The interface the pragma parsers have to the lexer. */
25169 enum cpp_ttype
25170 pragma_lex (tree *value)
25172 cp_token *tok;
25173 enum cpp_ttype ret;
25175 tok = cp_lexer_peek_token (the_parser->lexer);
25177 ret = tok->type;
25178 *value = tok->u.value;
25180 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25181 ret = CPP_EOF;
25182 else if (ret == CPP_STRING)
25183 *value = cp_parser_string_literal (the_parser, false, false);
25184 else
25186 cp_lexer_consume_token (the_parser->lexer);
25187 if (ret == CPP_KEYWORD)
25188 ret = CPP_NAME;
25191 return ret;
25195 /* External interface. */
25197 /* Parse one entire translation unit. */
25199 void
25200 c_parse_file (void)
25202 static bool already_called = false;
25204 if (already_called)
25206 sorry ("inter-module optimizations not implemented for C++");
25207 return;
25209 already_called = true;
25211 the_parser = cp_parser_new ();
25212 push_deferring_access_checks (flag_access_control
25213 ? dk_no_deferred : dk_no_check);
25214 cp_parser_translation_unit (the_parser);
25215 the_parser = NULL;
25218 #include "gt-cp-parser.h"