Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / cp / parser.c
blob63566c0972de49368311e554a8900716f05a96b7
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "c-common.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 C++ token. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
56 /* Token flags. */
57 unsigned char flags;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* True for a CPP_NAME token that is not a keyword (i.e., for which
63 KEYWORD is RID_MAX) iff this name was looked up and found to be
64 ambiguous. An error has already been reported. */
65 BOOL_BITFIELD ambiguous_p : 1;
66 /* The value associated with this token, if any. */
67 tree value;
68 /* The location at which this token was found. */
69 location_t location;
70 } cp_token;
72 /* We use a stack of token pointer for saving token sets. */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
77 static const cp_token eof_token =
79 CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
82 #else
83 {0, 0}
84 #endif
87 /* The cp_lexer structure represents the C++ lexer. It is responsible
88 for managing the token stream from the preprocessor and supplying
89 it to the parser. Tokens are never added to the cp_lexer after
90 it is created. */
92 typedef struct cp_lexer GTY (())
94 /* The memory allocated for the buffer. NULL if this lexer does not
95 own the token buffer. */
96 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97 /* If the lexer owns the buffer, this is the number of tokens in the
98 buffer. */
99 size_t buffer_length;
101 /* A pointer just past the last available token. The tokens
102 in this lexer are [buffer, last_token). */
103 cp_token_position GTY ((skip)) last_token;
105 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
106 no more available tokens. */
107 cp_token_position GTY ((skip)) next_token;
109 /* A stack indicating positions at which cp_lexer_save_tokens was
110 called. The top entry is the most recent position at which we
111 began saving tokens. If the stack is non-empty, we are saving
112 tokens. */
113 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
115 /* True if we should output debugging information. */
116 bool debugging_p;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
120 } cp_lexer;
122 /* cp_token_cache is a range of tokens. There is no need to represent
123 allocate heap memory for it, since tokens are never removed from the
124 lexer's array. There is also no need for the GC to walk through
125 a cp_token_cache, since everything in here is referenced through
126 a lexer. */
128 typedef struct cp_token_cache GTY(())
130 /* The beginning of the token range. */
131 cp_token * GTY((skip)) first;
133 /* Points immediately after the last token in the range. */
134 cp_token * GTY ((skip)) last;
135 } cp_token_cache;
137 /* Prototypes. */
139 static cp_lexer *cp_lexer_new_main
140 (void);
141 static cp_lexer *cp_lexer_new_from_tokens
142 (cp_token_cache *tokens);
143 static void cp_lexer_destroy
144 (cp_lexer *);
145 static int cp_lexer_saving_tokens
146 (const cp_lexer *);
147 static cp_token_position cp_lexer_token_position
148 (cp_lexer *, bool);
149 static cp_token *cp_lexer_token_at
150 (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152 (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
154 (cp_lexer *);
155 static cp_token *cp_lexer_peek_nth_token
156 (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158 (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160 (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162 (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
164 (cp_lexer *);
165 static void cp_lexer_purge_token
166 (cp_lexer *);
167 static void cp_lexer_purge_tokens_after
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
170 (cp_lexer *);
171 static void cp_lexer_save_tokens
172 (cp_lexer *);
173 static void cp_lexer_commit_tokens
174 (cp_lexer *);
175 static void cp_lexer_rollback_tokens
176 (cp_lexer *);
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179 (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
181 (cp_lexer *);
182 static void cp_lexer_start_debugging
183 (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185 (cp_lexer *) ATTRIBUTE_UNUSED;
186 #else
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188 about passing NULL to functions that require non-NULL arguments
189 (fputs, fprintf). It will never be used, so all we need is a value
190 of the right type that's guaranteed not to be NULL. */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
196 static cp_token_cache *cp_token_cache_new
197 (cp_token *, cp_token *);
199 /* Manifest constants. */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
203 /* A token type for keywords, as opposed to ordinary identifiers. */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
206 /* A token type for template-ids. If a template-id is processed while
207 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208 the value of the CPP_TEMPLATE_ID is whatever was returned by
209 cp_parser_template_id. */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
212 /* A token type for nested-name-specifiers. If a
213 nested-name-specifier is processed while parsing tentatively, it is
214 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216 cp_parser_nested_name_specifier_opt. */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
219 /* A token type for tokens that are not tokens at all; these are used
220 to represent slots in the array where there used to be a token
221 that has now been deleted. */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
224 /* The number of token types, including C++-specific ones. */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
227 /* Variables. */
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written. */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
235 preprocessor. */
237 static cp_lexer *
238 cp_lexer_new_main (void)
240 cp_token first_token;
241 cp_lexer *lexer;
242 cp_token *pos;
243 size_t alloc;
244 size_t space;
245 cp_token *buffer;
247 /* It's possible that lexing the first token will load a PCH file,
248 which is a GC collection point. So we have to grab the first
249 token before allocating any memory. Pragmas must not be deferred
250 as -fpch-preprocess can generate a pragma to load the PCH file in
251 the preprocessed output used by -save-temps. */
252 cp_lexer_get_preprocessor_token (NULL, &first_token);
254 /* Tell cpplib we want CPP_PRAGMA tokens. */
255 cpp_get_options (parse_in)->defer_pragmas = true;
257 /* Tell c_lex not to merge string constants. */
258 c_lex_return_raw_strings = true;
260 c_common_no_more_pch ();
262 /* Allocate the memory. */
263 lexer = GGC_CNEW (cp_lexer);
265 #ifdef ENABLE_CHECKING
266 /* Initially we are not debugging. */
267 lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270 CP_SAVED_TOKEN_STACK);
272 /* Create the buffer. */
273 alloc = CP_LEXER_BUFFER_SIZE;
274 buffer = ggc_alloc (alloc * sizeof (cp_token));
276 /* Put the first token in the buffer. */
277 space = alloc;
278 pos = buffer;
279 *pos = first_token;
281 /* Get the remaining tokens from the preprocessor. */
282 while (pos->type != CPP_EOF)
284 pos++;
285 if (!--space)
287 space = alloc;
288 alloc *= 2;
289 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
290 pos = buffer + space;
292 cp_lexer_get_preprocessor_token (lexer, pos);
294 lexer->buffer = buffer;
295 lexer->buffer_length = alloc - space;
296 lexer->last_token = pos;
297 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
299 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300 direct calls to c_lex. Those callers all expect c_lex to do
301 string constant concatenation. */
302 c_lex_return_raw_strings = false;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
341 /* Frees all resources associated with LEXER. */
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
375 return pos;
378 /* nonzero if we are presently saving tokens. */
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
422 else
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else
446 token->keyword = RID_MAX;
449 /* Update the globals input_location and in_system_header from TOKEN. */
450 static inline void
451 cp_lexer_set_source_position_from_token (cp_token *token)
453 if (token->type != CPP_EOF)
455 input_location = token->location;
456 in_system_header = token->in_system_header;
460 /* Return a pointer to the next token in the token stream, but do not
461 consume it. */
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
466 if (cp_lexer_debugging_p (lexer))
468 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470 putc ('\n', cp_lexer_debug_stream);
472 return lexer->next_token;
475 /* Return true if the next token has the indicated TYPE. */
477 static inline bool
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
480 return cp_lexer_peek_token (lexer)->type == type;
483 /* Return true if the next token does not have the indicated TYPE. */
485 static inline bool
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
488 return !cp_lexer_next_token_is (lexer, type);
491 /* Return true if the next token is the indicated KEYWORD. */
493 static inline bool
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
496 cp_token *token;
498 /* Peek at the next token. */
499 token = cp_lexer_peek_token (lexer);
500 /* Check to see if it is the indicated keyword. */
501 return token->keyword == keyword;
504 static bool
505 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
507 cp_token *token;
509 token = cp_lexer_peek_token (lexer);
510 switch (token->keyword)
512 /* Storage classes. */
513 case RID_AUTO:
514 case RID_REGISTER:
515 case RID_STATIC:
516 case RID_EXTERN:
517 case RID_MUTABLE:
518 case RID_THREAD:
519 /* Elaborated type specifiers. */
520 case RID_ENUM:
521 case RID_CLASS:
522 case RID_STRUCT:
523 case RID_UNION:
524 case RID_TYPENAME:
525 /* Simple type specifiers. */
526 case RID_CHAR:
527 case RID_WCHAR:
528 case RID_BOOL:
529 case RID_SHORT:
530 case RID_INT:
531 case RID_LONG:
532 case RID_SIGNED:
533 case RID_UNSIGNED:
534 case RID_FLOAT:
535 case RID_DOUBLE:
536 case RID_VOID:
537 /* GNU extensions. */
538 case RID_ATTRIBUTE:
539 case RID_TYPEOF:
540 return true;
542 default:
543 return false;
547 /* Return a pointer to the Nth token in the token stream. If N is 1,
548 then this is precisely equivalent to cp_lexer_peek_token (except
549 that it is not inline). One would like to disallow that case, but
550 there is one case (cp_parser_nth_token_starts_template_id) where
551 the caller passes a variable for N and it might be 1. */
553 static cp_token *
554 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
556 cp_token *token;
558 /* N is 1-based, not zero-based. */
559 gcc_assert (n > 0);
561 if (cp_lexer_debugging_p (lexer))
562 fprintf (cp_lexer_debug_stream,
563 "cp_lexer: peeking ahead %ld at token: ", (long)n);
565 --n;
566 token = lexer->next_token;
567 gcc_assert (!n || token != &eof_token);
568 while (n != 0)
570 ++token;
571 if (token == lexer->last_token)
573 token = (cp_token *)&eof_token;
574 break;
577 if (token->type != CPP_PURGED)
578 --n;
581 if (cp_lexer_debugging_p (lexer))
583 cp_lexer_print_token (cp_lexer_debug_stream, token);
584 putc ('\n', cp_lexer_debug_stream);
587 return token;
590 /* Return the next token, and advance the lexer's next_token pointer
591 to point to the next non-purged token. */
593 static cp_token *
594 cp_lexer_consume_token (cp_lexer* lexer)
596 cp_token *token = lexer->next_token;
598 gcc_assert (token != &eof_token);
602 lexer->next_token++;
603 if (lexer->next_token == lexer->last_token)
605 lexer->next_token = (cp_token *)&eof_token;
606 break;
610 while (lexer->next_token->type == CPP_PURGED);
612 cp_lexer_set_source_position_from_token (token);
614 /* Provide debugging output. */
615 if (cp_lexer_debugging_p (lexer))
617 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
622 return token;
625 /* Permanently remove the next token from the token stream, and
626 advance the next_token pointer to refer to the next non-purged
627 token. */
629 static void
630 cp_lexer_purge_token (cp_lexer *lexer)
632 cp_token *tok = lexer->next_token;
634 gcc_assert (tok != &eof_token);
635 tok->type = CPP_PURGED;
636 tok->location = UNKNOWN_LOCATION;
637 tok->value = NULL_TREE;
638 tok->keyword = RID_MAX;
642 tok++;
643 if (tok == lexer->last_token)
645 tok = (cp_token *)&eof_token;
646 break;
649 while (tok->type == CPP_PURGED);
650 lexer->next_token = tok;
653 /* Permanently remove all tokens after TOK, up to, but not
654 including, the token that will be returned next by
655 cp_lexer_peek_token. */
657 static void
658 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
660 cp_token *peek = lexer->next_token;
662 if (peek == &eof_token)
663 peek = lexer->last_token;
665 gcc_assert (tok < peek);
667 for ( tok += 1; tok != peek; tok += 1)
669 tok->type = CPP_PURGED;
670 tok->location = UNKNOWN_LOCATION;
671 tok->value = NULL_TREE;
672 tok->keyword = RID_MAX;
676 /* Consume and handle a pragma token. */
677 static void
678 cp_lexer_handle_pragma (cp_lexer *lexer)
680 cpp_string s;
681 cp_token *token = cp_lexer_consume_token (lexer);
682 gcc_assert (token->type == CPP_PRAGMA);
683 gcc_assert (token->value);
685 s.len = TREE_STRING_LENGTH (token->value);
686 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
688 cpp_handle_deferred_pragma (parse_in, &s);
690 /* Clearing token->value here means that we will get an ICE if we
691 try to process this #pragma again (which should be impossible). */
692 token->value = NULL;
695 /* Begin saving tokens. All tokens consumed after this point will be
696 preserved. */
698 static void
699 cp_lexer_save_tokens (cp_lexer* lexer)
701 /* Provide debugging output. */
702 if (cp_lexer_debugging_p (lexer))
703 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
705 VEC_safe_push (cp_token_position, heap,
706 lexer->saved_tokens, lexer->next_token);
709 /* Commit to the portion of the token stream most recently saved. */
711 static void
712 cp_lexer_commit_tokens (cp_lexer* lexer)
714 /* Provide debugging output. */
715 if (cp_lexer_debugging_p (lexer))
716 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
718 VEC_pop (cp_token_position, lexer->saved_tokens);
721 /* Return all tokens saved since the last call to cp_lexer_save_tokens
722 to the token stream. Stop saving tokens. */
724 static void
725 cp_lexer_rollback_tokens (cp_lexer* lexer)
727 /* Provide debugging output. */
728 if (cp_lexer_debugging_p (lexer))
729 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
731 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
734 /* Print a representation of the TOKEN on the STREAM. */
736 #ifdef ENABLE_CHECKING
738 static void
739 cp_lexer_print_token (FILE * stream, cp_token *token)
741 /* We don't use cpp_type2name here because the parser defines
742 a few tokens of its own. */
743 static const char *const token_names[] = {
744 /* cpplib-defined token types */
745 #define OP(e, s) #e,
746 #define TK(e, s) #e,
747 TTYPE_TABLE
748 #undef OP
749 #undef TK
750 /* C++ parser token types - see "Manifest constants", above. */
751 "KEYWORD",
752 "TEMPLATE_ID",
753 "NESTED_NAME_SPECIFIER",
754 "PURGED"
757 /* If we have a name for the token, print it out. Otherwise, we
758 simply give the numeric code. */
759 gcc_assert (token->type < ARRAY_SIZE(token_names));
760 fputs (token_names[token->type], stream);
762 /* For some tokens, print the associated data. */
763 switch (token->type)
765 case CPP_KEYWORD:
766 /* Some keywords have a value that is not an IDENTIFIER_NODE.
767 For example, `struct' is mapped to an INTEGER_CST. */
768 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
769 break;
770 /* else fall through */
771 case CPP_NAME:
772 fputs (IDENTIFIER_POINTER (token->value), stream);
773 break;
775 case CPP_STRING:
776 case CPP_WSTRING:
777 case CPP_PRAGMA:
778 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
779 break;
781 default:
782 break;
786 /* Start emitting debugging information. */
788 static void
789 cp_lexer_start_debugging (cp_lexer* lexer)
791 lexer->debugging_p = true;
794 /* Stop emitting debugging information. */
796 static void
797 cp_lexer_stop_debugging (cp_lexer* lexer)
799 lexer->debugging_p = false;
802 #endif /* ENABLE_CHECKING */
804 /* Create a new cp_token_cache, representing a range of tokens. */
806 static cp_token_cache *
807 cp_token_cache_new (cp_token *first, cp_token *last)
809 cp_token_cache *cache = GGC_NEW (cp_token_cache);
810 cache->first = first;
811 cache->last = last;
812 return cache;
816 /* Decl-specifiers. */
818 static void clear_decl_specs
819 (cp_decl_specifier_seq *);
821 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
823 static void
824 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
826 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
829 /* Declarators. */
831 /* Nothing other than the parser should be creating declarators;
832 declarators are a semi-syntactic representation of C++ entities.
833 Other parts of the front end that need to create entities (like
834 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
836 static cp_declarator *make_call_declarator
837 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
838 static cp_declarator *make_array_declarator
839 (cp_declarator *, tree);
840 static cp_declarator *make_pointer_declarator
841 (cp_cv_quals, cp_declarator *);
842 static cp_declarator *make_reference_declarator
843 (cp_cv_quals, cp_declarator *);
844 static cp_parameter_declarator *make_parameter_declarator
845 (cp_decl_specifier_seq *, cp_declarator *, tree);
846 static cp_declarator *make_ptrmem_declarator
847 (cp_cv_quals, tree, cp_declarator *);
849 cp_declarator *cp_error_declarator;
851 /* The obstack on which declarators and related data structures are
852 allocated. */
853 static struct obstack declarator_obstack;
855 /* Alloc BYTES from the declarator memory pool. */
857 static inline void *
858 alloc_declarator (size_t bytes)
860 return obstack_alloc (&declarator_obstack, bytes);
863 /* Allocate a declarator of the indicated KIND. Clear fields that are
864 common to all declarators. */
866 static cp_declarator *
867 make_declarator (cp_declarator_kind kind)
869 cp_declarator *declarator;
871 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
872 declarator->kind = kind;
873 declarator->attributes = NULL_TREE;
874 declarator->declarator = NULL;
876 return declarator;
879 /* Make a declarator for a generalized identifier. If
880 QUALIFYING_SCOPE is non-NULL, the identifier is
881 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
882 UNQUALIFIED_NAME. SFK indicates the kind of special function this
883 is, if any. */
885 static cp_declarator *
886 make_id_declarator (tree qualifying_scope, tree unqualified_name,
887 special_function_kind sfk)
889 cp_declarator *declarator;
891 /* It is valid to write:
893 class C { void f(); };
894 typedef C D;
895 void D::f();
897 The standard is not clear about whether `typedef const C D' is
898 legal; as of 2002-09-15 the committee is considering that
899 question. EDG 3.0 allows that syntax. Therefore, we do as
900 well. */
901 if (qualifying_scope && TYPE_P (qualifying_scope))
902 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
904 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
905 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
906 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
908 declarator = make_declarator (cdk_id);
909 declarator->u.id.qualifying_scope = qualifying_scope;
910 declarator->u.id.unqualified_name = unqualified_name;
911 declarator->u.id.sfk = sfk;
913 return declarator;
916 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
917 of modifiers such as const or volatile to apply to the pointer
918 type, represented as identifiers. */
920 cp_declarator *
921 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
923 cp_declarator *declarator;
925 declarator = make_declarator (cdk_pointer);
926 declarator->declarator = target;
927 declarator->u.pointer.qualifiers = cv_qualifiers;
928 declarator->u.pointer.class_type = NULL_TREE;
930 return declarator;
933 /* Like make_pointer_declarator -- but for references. */
935 cp_declarator *
936 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 cp_declarator *declarator;
940 declarator = make_declarator (cdk_reference);
941 declarator->declarator = target;
942 declarator->u.pointer.qualifiers = cv_qualifiers;
943 declarator->u.pointer.class_type = NULL_TREE;
945 return declarator;
948 /* Like make_pointer_declarator -- but for a pointer to a non-static
949 member of CLASS_TYPE. */
951 cp_declarator *
952 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
953 cp_declarator *pointee)
955 cp_declarator *declarator;
957 declarator = make_declarator (cdk_ptrmem);
958 declarator->declarator = pointee;
959 declarator->u.pointer.qualifiers = cv_qualifiers;
960 declarator->u.pointer.class_type = class_type;
962 return declarator;
965 /* Make a declarator for the function given by TARGET, with the
966 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
967 "const"-qualified member function. The EXCEPTION_SPECIFICATION
968 indicates what exceptions can be thrown. */
970 cp_declarator *
971 make_call_declarator (cp_declarator *target,
972 cp_parameter_declarator *parms,
973 cp_cv_quals cv_qualifiers,
974 tree exception_specification)
976 cp_declarator *declarator;
978 declarator = make_declarator (cdk_function);
979 declarator->declarator = target;
980 declarator->u.function.parameters = parms;
981 declarator->u.function.qualifiers = cv_qualifiers;
982 declarator->u.function.exception_specification = exception_specification;
984 return declarator;
987 /* Make a declarator for an array of BOUNDS elements, each of which is
988 defined by ELEMENT. */
990 cp_declarator *
991 make_array_declarator (cp_declarator *element, tree bounds)
993 cp_declarator *declarator;
995 declarator = make_declarator (cdk_array);
996 declarator->declarator = element;
997 declarator->u.array.bounds = bounds;
999 return declarator;
1002 cp_parameter_declarator *no_parameters;
1004 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1005 DECLARATOR and DEFAULT_ARGUMENT. */
1007 cp_parameter_declarator *
1008 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1009 cp_declarator *declarator,
1010 tree default_argument)
1012 cp_parameter_declarator *parameter;
1014 parameter = ((cp_parameter_declarator *)
1015 alloc_declarator (sizeof (cp_parameter_declarator)));
1016 parameter->next = NULL;
1017 if (decl_specifiers)
1018 parameter->decl_specifiers = *decl_specifiers;
1019 else
1020 clear_decl_specs (&parameter->decl_specifiers);
1021 parameter->declarator = declarator;
1022 parameter->default_argument = default_argument;
1023 parameter->ellipsis_p = false;
1025 return parameter;
1028 /* Returns true iff DECLARATOR is a declaration for a function. */
1030 static bool
1031 function_declarator_p (const cp_declarator *declarator)
1033 while (declarator)
1035 if (declarator->kind == cdk_function
1036 && declarator->declarator->kind == cdk_id)
1037 return true;
1038 if (declarator->kind == cdk_id
1039 || declarator->kind == cdk_error)
1040 return false;
1041 declarator = declarator->declarator;
1043 return false;
1046 /* The parser. */
1048 /* Overview
1049 --------
1051 A cp_parser parses the token stream as specified by the C++
1052 grammar. Its job is purely parsing, not semantic analysis. For
1053 example, the parser breaks the token stream into declarators,
1054 expressions, statements, and other similar syntactic constructs.
1055 It does not check that the types of the expressions on either side
1056 of an assignment-statement are compatible, or that a function is
1057 not declared with a parameter of type `void'.
1059 The parser invokes routines elsewhere in the compiler to perform
1060 semantic analysis and to build up the abstract syntax tree for the
1061 code processed.
1063 The parser (and the template instantiation code, which is, in a
1064 way, a close relative of parsing) are the only parts of the
1065 compiler that should be calling push_scope and pop_scope, or
1066 related functions. The parser (and template instantiation code)
1067 keeps track of what scope is presently active; everything else
1068 should simply honor that. (The code that generates static
1069 initializers may also need to set the scope, in order to check
1070 access control correctly when emitting the initializers.)
1072 Methodology
1073 -----------
1075 The parser is of the standard recursive-descent variety. Upcoming
1076 tokens in the token stream are examined in order to determine which
1077 production to use when parsing a non-terminal. Some C++ constructs
1078 require arbitrary look ahead to disambiguate. For example, it is
1079 impossible, in the general case, to tell whether a statement is an
1080 expression or declaration without scanning the entire statement.
1081 Therefore, the parser is capable of "parsing tentatively." When the
1082 parser is not sure what construct comes next, it enters this mode.
1083 Then, while we attempt to parse the construct, the parser queues up
1084 error messages, rather than issuing them immediately, and saves the
1085 tokens it consumes. If the construct is parsed successfully, the
1086 parser "commits", i.e., it issues any queued error messages and
1087 the tokens that were being preserved are permanently discarded.
1088 If, however, the construct is not parsed successfully, the parser
1089 rolls back its state completely so that it can resume parsing using
1090 a different alternative.
1092 Future Improvements
1093 -------------------
1095 The performance of the parser could probably be improved substantially.
1096 We could often eliminate the need to parse tentatively by looking ahead
1097 a little bit. In some places, this approach might not entirely eliminate
1098 the need to parse tentatively, but it might still speed up the average
1099 case. */
1101 /* Flags that are passed to some parsing functions. These values can
1102 be bitwise-ored together. */
1104 typedef enum cp_parser_flags
1106 /* No flags. */
1107 CP_PARSER_FLAGS_NONE = 0x0,
1108 /* The construct is optional. If it is not present, then no error
1109 should be issued. */
1110 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1111 /* When parsing a type-specifier, do not allow user-defined types. */
1112 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1113 } cp_parser_flags;
1115 /* The different kinds of declarators we want to parse. */
1117 typedef enum cp_parser_declarator_kind
1119 /* We want an abstract declarator. */
1120 CP_PARSER_DECLARATOR_ABSTRACT,
1121 /* We want a named declarator. */
1122 CP_PARSER_DECLARATOR_NAMED,
1123 /* We don't mind, but the name must be an unqualified-id. */
1124 CP_PARSER_DECLARATOR_EITHER
1125 } cp_parser_declarator_kind;
1127 /* The precedence values used to parse binary expressions. The minimum value
1128 of PREC must be 1, because zero is reserved to quickly discriminate
1129 binary operators from other tokens. */
1131 enum cp_parser_prec
1133 PREC_NOT_OPERATOR,
1134 PREC_LOGICAL_OR_EXPRESSION,
1135 PREC_LOGICAL_AND_EXPRESSION,
1136 PREC_INCLUSIVE_OR_EXPRESSION,
1137 PREC_EXCLUSIVE_OR_EXPRESSION,
1138 PREC_AND_EXPRESSION,
1139 PREC_EQUALITY_EXPRESSION,
1140 PREC_RELATIONAL_EXPRESSION,
1141 PREC_SHIFT_EXPRESSION,
1142 PREC_ADDITIVE_EXPRESSION,
1143 PREC_MULTIPLICATIVE_EXPRESSION,
1144 PREC_PM_EXPRESSION,
1145 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1148 /* A mapping from a token type to a corresponding tree node type, with a
1149 precedence value. */
1151 typedef struct cp_parser_binary_operations_map_node
1153 /* The token type. */
1154 enum cpp_ttype token_type;
1155 /* The corresponding tree code. */
1156 enum tree_code tree_type;
1157 /* The precedence of this operator. */
1158 enum cp_parser_prec prec;
1159 } cp_parser_binary_operations_map_node;
1161 /* The status of a tentative parse. */
1163 typedef enum cp_parser_status_kind
1165 /* No errors have occurred. */
1166 CP_PARSER_STATUS_KIND_NO_ERROR,
1167 /* An error has occurred. */
1168 CP_PARSER_STATUS_KIND_ERROR,
1169 /* We are committed to this tentative parse, whether or not an error
1170 has occurred. */
1171 CP_PARSER_STATUS_KIND_COMMITTED
1172 } cp_parser_status_kind;
1174 typedef struct cp_parser_expression_stack_entry
1176 tree lhs;
1177 enum tree_code tree_type;
1178 int prec;
1179 } cp_parser_expression_stack_entry;
1181 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1182 entries because precedence levels on the stack are monotonically
1183 increasing. */
1184 typedef struct cp_parser_expression_stack_entry
1185 cp_parser_expression_stack[NUM_PREC_VALUES];
1187 /* Context that is saved and restored when parsing tentatively. */
1188 typedef struct cp_parser_context GTY (())
1190 /* If this is a tentative parsing context, the status of the
1191 tentative parse. */
1192 enum cp_parser_status_kind status;
1193 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1194 that are looked up in this context must be looked up both in the
1195 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1196 the context of the containing expression. */
1197 tree object_type;
1199 /* The next parsing context in the stack. */
1200 struct cp_parser_context *next;
1201 } cp_parser_context;
1203 /* Prototypes. */
1205 /* Constructors and destructors. */
1207 static cp_parser_context *cp_parser_context_new
1208 (cp_parser_context *);
1210 /* Class variables. */
1212 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1214 /* The operator-precedence table used by cp_parser_binary_expression.
1215 Transformed into an associative array (binops_by_token) by
1216 cp_parser_new. */
1218 static const cp_parser_binary_operations_map_node binops[] = {
1219 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1220 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1222 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1223 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1224 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1226 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1227 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1229 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1230 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1232 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1233 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1234 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1235 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1236 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1237 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1239 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1240 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1242 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1244 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1246 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1248 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1250 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1253 /* The same as binops, but initialized by cp_parser_new so that
1254 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1255 for speed. */
1256 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1258 /* Constructors and destructors. */
1260 /* Construct a new context. The context below this one on the stack
1261 is given by NEXT. */
1263 static cp_parser_context *
1264 cp_parser_context_new (cp_parser_context* next)
1266 cp_parser_context *context;
1268 /* Allocate the storage. */
1269 if (cp_parser_context_free_list != NULL)
1271 /* Pull the first entry from the free list. */
1272 context = cp_parser_context_free_list;
1273 cp_parser_context_free_list = context->next;
1274 memset (context, 0, sizeof (*context));
1276 else
1277 context = GGC_CNEW (cp_parser_context);
1279 /* No errors have occurred yet in this context. */
1280 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1281 /* If this is not the bottomost context, copy information that we
1282 need from the previous context. */
1283 if (next)
1285 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1286 expression, then we are parsing one in this context, too. */
1287 context->object_type = next->object_type;
1288 /* Thread the stack. */
1289 context->next = next;
1292 return context;
1295 /* The cp_parser structure represents the C++ parser. */
1297 typedef struct cp_parser GTY(())
1299 /* The lexer from which we are obtaining tokens. */
1300 cp_lexer *lexer;
1302 /* The scope in which names should be looked up. If NULL_TREE, then
1303 we look up names in the scope that is currently open in the
1304 source program. If non-NULL, this is either a TYPE or
1305 NAMESPACE_DECL for the scope in which we should look. It can
1306 also be ERROR_MARK, when we've parsed a bogus scope.
1308 This value is not cleared automatically after a name is looked
1309 up, so we must be careful to clear it before starting a new look
1310 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1311 will look up `Z' in the scope of `X', rather than the current
1312 scope.) Unfortunately, it is difficult to tell when name lookup
1313 is complete, because we sometimes peek at a token, look it up,
1314 and then decide not to consume it. */
1315 tree scope;
1317 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1318 last lookup took place. OBJECT_SCOPE is used if an expression
1319 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1320 respectively. QUALIFYING_SCOPE is used for an expression of the
1321 form "X::Y"; it refers to X. */
1322 tree object_scope;
1323 tree qualifying_scope;
1325 /* A stack of parsing contexts. All but the bottom entry on the
1326 stack will be tentative contexts.
1328 We parse tentatively in order to determine which construct is in
1329 use in some situations. For example, in order to determine
1330 whether a statement is an expression-statement or a
1331 declaration-statement we parse it tentatively as a
1332 declaration-statement. If that fails, we then reparse the same
1333 token stream as an expression-statement. */
1334 cp_parser_context *context;
1336 /* True if we are parsing GNU C++. If this flag is not set, then
1337 GNU extensions are not recognized. */
1338 bool allow_gnu_extensions_p;
1340 /* TRUE if the `>' token should be interpreted as the greater-than
1341 operator. FALSE if it is the end of a template-id or
1342 template-parameter-list. */
1343 bool greater_than_is_operator_p;
1345 /* TRUE if default arguments are allowed within a parameter list
1346 that starts at this point. FALSE if only a gnu extension makes
1347 them permissible. */
1348 bool default_arg_ok_p;
1350 /* TRUE if we are parsing an integral constant-expression. See
1351 [expr.const] for a precise definition. */
1352 bool integral_constant_expression_p;
1354 /* TRUE if we are parsing an integral constant-expression -- but a
1355 non-constant expression should be permitted as well. This flag
1356 is used when parsing an array bound so that GNU variable-length
1357 arrays are tolerated. */
1358 bool allow_non_integral_constant_expression_p;
1360 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1361 been seen that makes the expression non-constant. */
1362 bool non_integral_constant_expression_p;
1364 /* TRUE if local variable names and `this' are forbidden in the
1365 current context. */
1366 bool local_variables_forbidden_p;
1368 /* TRUE if the declaration we are parsing is part of a
1369 linkage-specification of the form `extern string-literal
1370 declaration'. */
1371 bool in_unbraced_linkage_specification_p;
1373 /* TRUE if we are presently parsing a declarator, after the
1374 direct-declarator. */
1375 bool in_declarator_p;
1377 /* TRUE if we are presently parsing a template-argument-list. */
1378 bool in_template_argument_list_p;
1380 /* TRUE if we are presently parsing the body of an
1381 iteration-statement. */
1382 bool in_iteration_statement_p;
1384 /* TRUE if we are presently parsing the body of a switch
1385 statement. */
1386 bool in_switch_statement_p;
1388 /* TRUE if we are parsing a type-id in an expression context. In
1389 such a situation, both "type (expr)" and "type (type)" are valid
1390 alternatives. */
1391 bool in_type_id_in_expr_p;
1393 /* TRUE if we are currently in a header file where declarations are
1394 implicitly extern "C". */
1395 bool implicit_extern_c;
1397 /* TRUE if strings in expressions should be translated to the execution
1398 character set. */
1399 bool translate_strings_p;
1401 /* TRUE if we are presently parsing the body of a function, but not
1402 a local class. */
1403 bool in_function_body;
1405 /* If non-NULL, then we are parsing a construct where new type
1406 definitions are not permitted. The string stored here will be
1407 issued as an error message if a type is defined. */
1408 const char *type_definition_forbidden_message;
1410 /* A list of lists. The outer list is a stack, used for member
1411 functions of local classes. At each level there are two sub-list,
1412 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1413 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1414 TREE_VALUE's. The functions are chained in reverse declaration
1415 order.
1417 The TREE_PURPOSE sublist contains those functions with default
1418 arguments that need post processing, and the TREE_VALUE sublist
1419 contains those functions with definitions that need post
1420 processing.
1422 These lists can only be processed once the outermost class being
1423 defined is complete. */
1424 tree unparsed_functions_queues;
1426 /* The number of classes whose definitions are currently in
1427 progress. */
1428 unsigned num_classes_being_defined;
1430 /* The number of template parameter lists that apply directly to the
1431 current declaration. */
1432 unsigned num_template_parameter_lists;
1433 } cp_parser;
1435 /* The type of a function that parses some kind of expression. */
1436 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1438 /* Prototypes. */
1440 /* Constructors and destructors. */
1442 static cp_parser *cp_parser_new
1443 (void);
1445 /* Routines to parse various constructs.
1447 Those that return `tree' will return the error_mark_node (rather
1448 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1449 Sometimes, they will return an ordinary node if error-recovery was
1450 attempted, even though a parse error occurred. So, to check
1451 whether or not a parse error occurred, you should always use
1452 cp_parser_error_occurred. If the construct is optional (indicated
1453 either by an `_opt' in the name of the function that does the
1454 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1455 the construct is not present. */
1457 /* Lexical conventions [gram.lex] */
1459 static tree cp_parser_identifier
1460 (cp_parser *);
1461 static tree cp_parser_string_literal
1462 (cp_parser *, bool, bool);
1464 /* Basic concepts [gram.basic] */
1466 static bool cp_parser_translation_unit
1467 (cp_parser *);
1469 /* Expressions [gram.expr] */
1471 static tree cp_parser_primary_expression
1472 (cp_parser *, bool, bool, bool, cp_id_kind *);
1473 static tree cp_parser_id_expression
1474 (cp_parser *, bool, bool, bool *, bool);
1475 static tree cp_parser_unqualified_id
1476 (cp_parser *, bool, bool, bool);
1477 static tree cp_parser_nested_name_specifier_opt
1478 (cp_parser *, bool, bool, bool, bool);
1479 static tree cp_parser_nested_name_specifier
1480 (cp_parser *, bool, bool, bool, bool);
1481 static tree cp_parser_class_or_namespace_name
1482 (cp_parser *, bool, bool, bool, bool, bool);
1483 static tree cp_parser_postfix_expression
1484 (cp_parser *, bool, bool);
1485 static tree cp_parser_postfix_open_square_expression
1486 (cp_parser *, tree, bool);
1487 static tree cp_parser_postfix_dot_deref_expression
1488 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1489 static tree cp_parser_parenthesized_expression_list
1490 (cp_parser *, bool, bool, bool *);
1491 static void cp_parser_pseudo_destructor_name
1492 (cp_parser *, tree *, tree *);
1493 static tree cp_parser_unary_expression
1494 (cp_parser *, bool, bool);
1495 static enum tree_code cp_parser_unary_operator
1496 (cp_token *);
1497 static tree cp_parser_new_expression
1498 (cp_parser *);
1499 static tree cp_parser_new_placement
1500 (cp_parser *);
1501 static tree cp_parser_new_type_id
1502 (cp_parser *, tree *);
1503 static cp_declarator *cp_parser_new_declarator_opt
1504 (cp_parser *);
1505 static cp_declarator *cp_parser_direct_new_declarator
1506 (cp_parser *);
1507 static tree cp_parser_new_initializer
1508 (cp_parser *);
1509 static tree cp_parser_delete_expression
1510 (cp_parser *);
1511 static tree cp_parser_cast_expression
1512 (cp_parser *, bool, bool);
1513 static tree cp_parser_binary_expression
1514 (cp_parser *, bool);
1515 static tree cp_parser_question_colon_clause
1516 (cp_parser *, tree);
1517 static tree cp_parser_assignment_expression
1518 (cp_parser *, bool);
1519 static enum tree_code cp_parser_assignment_operator_opt
1520 (cp_parser *);
1521 static tree cp_parser_expression
1522 (cp_parser *, bool);
1523 static tree cp_parser_constant_expression
1524 (cp_parser *, bool, bool *);
1525 static tree cp_parser_builtin_offsetof
1526 (cp_parser *);
1528 /* Statements [gram.stmt.stmt] */
1530 static void cp_parser_statement
1531 (cp_parser *, tree);
1532 static void cp_parser_label_for_labeled_statement
1533 (cp_parser *);
1534 static tree cp_parser_expression_statement
1535 (cp_parser *, tree);
1536 static tree cp_parser_compound_statement
1537 (cp_parser *, tree, bool);
1538 static void cp_parser_statement_seq_opt
1539 (cp_parser *, tree);
1540 static tree cp_parser_selection_statement
1541 (cp_parser *);
1542 static tree cp_parser_condition
1543 (cp_parser *);
1544 static tree cp_parser_iteration_statement
1545 (cp_parser *);
1546 static void cp_parser_for_init_statement
1547 (cp_parser *);
1548 static tree cp_parser_jump_statement
1549 (cp_parser *);
1550 static void cp_parser_declaration_statement
1551 (cp_parser *);
1553 static tree cp_parser_implicitly_scoped_statement
1554 (cp_parser *);
1555 static void cp_parser_already_scoped_statement
1556 (cp_parser *);
1558 /* Declarations [gram.dcl.dcl] */
1560 static void cp_parser_declaration_seq_opt
1561 (cp_parser *);
1562 static void cp_parser_declaration
1563 (cp_parser *);
1564 static void cp_parser_block_declaration
1565 (cp_parser *, bool);
1566 static void cp_parser_simple_declaration
1567 (cp_parser *, bool);
1568 static void cp_parser_decl_specifier_seq
1569 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1570 static tree cp_parser_storage_class_specifier_opt
1571 (cp_parser *);
1572 static tree cp_parser_function_specifier_opt
1573 (cp_parser *, cp_decl_specifier_seq *);
1574 static tree cp_parser_type_specifier
1575 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1576 int *, bool *);
1577 static tree cp_parser_simple_type_specifier
1578 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1579 static tree cp_parser_type_name
1580 (cp_parser *);
1581 static tree cp_parser_elaborated_type_specifier
1582 (cp_parser *, bool, bool);
1583 static tree cp_parser_enum_specifier
1584 (cp_parser *);
1585 static void cp_parser_enumerator_list
1586 (cp_parser *, tree);
1587 static void cp_parser_enumerator_definition
1588 (cp_parser *, tree);
1589 static tree cp_parser_namespace_name
1590 (cp_parser *);
1591 static void cp_parser_namespace_definition
1592 (cp_parser *);
1593 static void cp_parser_namespace_body
1594 (cp_parser *);
1595 static tree cp_parser_qualified_namespace_specifier
1596 (cp_parser *);
1597 static void cp_parser_namespace_alias_definition
1598 (cp_parser *);
1599 static bool cp_parser_using_declaration
1600 (cp_parser *, bool);
1601 static void cp_parser_using_directive
1602 (cp_parser *);
1603 static void cp_parser_asm_definition
1604 (cp_parser *);
1605 static void cp_parser_linkage_specification
1606 (cp_parser *);
1608 /* Declarators [gram.dcl.decl] */
1610 static tree cp_parser_init_declarator
1611 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1612 static cp_declarator *cp_parser_declarator
1613 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1614 static cp_declarator *cp_parser_direct_declarator
1615 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1616 static enum tree_code cp_parser_ptr_operator
1617 (cp_parser *, tree *, cp_cv_quals *);
1618 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1619 (cp_parser *);
1620 static tree cp_parser_declarator_id
1621 (cp_parser *);
1622 static tree cp_parser_type_id
1623 (cp_parser *);
1624 static void cp_parser_type_specifier_seq
1625 (cp_parser *, bool, cp_decl_specifier_seq *);
1626 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1627 (cp_parser *);
1628 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1629 (cp_parser *, bool *);
1630 static cp_parameter_declarator *cp_parser_parameter_declaration
1631 (cp_parser *, bool, bool *);
1632 static void cp_parser_function_body
1633 (cp_parser *);
1634 static tree cp_parser_initializer
1635 (cp_parser *, bool *, bool *);
1636 static tree cp_parser_initializer_clause
1637 (cp_parser *, bool *);
1638 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1639 (cp_parser *, bool *);
1641 static bool cp_parser_ctor_initializer_opt_and_function_body
1642 (cp_parser *);
1644 /* Classes [gram.class] */
1646 static tree cp_parser_class_name
1647 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1648 static tree cp_parser_class_specifier
1649 (cp_parser *);
1650 static tree cp_parser_class_head
1651 (cp_parser *, bool *, tree *, tree *);
1652 static enum tag_types cp_parser_class_key
1653 (cp_parser *);
1654 static void cp_parser_member_specification_opt
1655 (cp_parser *);
1656 static void cp_parser_member_declaration
1657 (cp_parser *);
1658 static tree cp_parser_pure_specifier
1659 (cp_parser *);
1660 static tree cp_parser_constant_initializer
1661 (cp_parser *);
1663 /* Derived classes [gram.class.derived] */
1665 static tree cp_parser_base_clause
1666 (cp_parser *);
1667 static tree cp_parser_base_specifier
1668 (cp_parser *);
1670 /* Special member functions [gram.special] */
1672 static tree cp_parser_conversion_function_id
1673 (cp_parser *);
1674 static tree cp_parser_conversion_type_id
1675 (cp_parser *);
1676 static cp_declarator *cp_parser_conversion_declarator_opt
1677 (cp_parser *);
1678 static bool cp_parser_ctor_initializer_opt
1679 (cp_parser *);
1680 static void cp_parser_mem_initializer_list
1681 (cp_parser *);
1682 static tree cp_parser_mem_initializer
1683 (cp_parser *);
1684 static tree cp_parser_mem_initializer_id
1685 (cp_parser *);
1687 /* Overloading [gram.over] */
1689 static tree cp_parser_operator_function_id
1690 (cp_parser *);
1691 static tree cp_parser_operator
1692 (cp_parser *);
1694 /* Templates [gram.temp] */
1696 static void cp_parser_template_declaration
1697 (cp_parser *, bool);
1698 static tree cp_parser_template_parameter_list
1699 (cp_parser *);
1700 static tree cp_parser_template_parameter
1701 (cp_parser *, bool *);
1702 static tree cp_parser_type_parameter
1703 (cp_parser *);
1704 static tree cp_parser_template_id
1705 (cp_parser *, bool, bool, bool);
1706 static tree cp_parser_template_name
1707 (cp_parser *, bool, bool, bool, bool *);
1708 static tree cp_parser_template_argument_list
1709 (cp_parser *);
1710 static tree cp_parser_template_argument
1711 (cp_parser *);
1712 static void cp_parser_explicit_instantiation
1713 (cp_parser *);
1714 static void cp_parser_explicit_specialization
1715 (cp_parser *);
1717 /* Exception handling [gram.exception] */
1719 static tree cp_parser_try_block
1720 (cp_parser *);
1721 static bool cp_parser_function_try_block
1722 (cp_parser *);
1723 static void cp_parser_handler_seq
1724 (cp_parser *);
1725 static void cp_parser_handler
1726 (cp_parser *);
1727 static tree cp_parser_exception_declaration
1728 (cp_parser *);
1729 static tree cp_parser_throw_expression
1730 (cp_parser *);
1731 static tree cp_parser_exception_specification_opt
1732 (cp_parser *);
1733 static tree cp_parser_type_id_list
1734 (cp_parser *);
1736 /* GNU Extensions */
1738 static tree cp_parser_asm_specification_opt
1739 (cp_parser *);
1740 static tree cp_parser_asm_operand_list
1741 (cp_parser *);
1742 static tree cp_parser_asm_clobber_list
1743 (cp_parser *);
1744 static tree cp_parser_attributes_opt
1745 (cp_parser *);
1746 static tree cp_parser_attribute_list
1747 (cp_parser *);
1748 static bool cp_parser_extension_opt
1749 (cp_parser *, int *);
1750 static void cp_parser_label_declaration
1751 (cp_parser *);
1753 /* Objective-C++ Productions */
1755 static tree cp_parser_objc_message_receiver
1756 (cp_parser *);
1757 static tree cp_parser_objc_message_args
1758 (cp_parser *);
1759 static tree cp_parser_objc_message_expression
1760 (cp_parser *);
1761 static tree cp_parser_objc_encode_expression
1762 (cp_parser *);
1763 static tree cp_parser_objc_defs_expression
1764 (cp_parser *);
1765 static tree cp_parser_objc_protocol_expression
1766 (cp_parser *);
1767 static tree cp_parser_objc_selector_expression
1768 (cp_parser *);
1769 static tree cp_parser_objc_expression
1770 (cp_parser *);
1771 static bool cp_parser_objc_selector_p
1772 (enum cpp_ttype);
1773 static tree cp_parser_objc_selector
1774 (cp_parser *);
1775 static tree cp_parser_objc_protocol_refs_opt
1776 (cp_parser *);
1777 static void cp_parser_objc_declaration
1778 (cp_parser *);
1779 static tree cp_parser_objc_statement
1780 (cp_parser *);
1782 /* Utility Routines */
1784 static tree cp_parser_lookup_name
1785 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1786 static tree cp_parser_lookup_name_simple
1787 (cp_parser *, tree);
1788 static tree cp_parser_maybe_treat_template_as_class
1789 (tree, bool);
1790 static bool cp_parser_check_declarator_template_parameters
1791 (cp_parser *, cp_declarator *);
1792 static bool cp_parser_check_template_parameters
1793 (cp_parser *, unsigned);
1794 static tree cp_parser_simple_cast_expression
1795 (cp_parser *);
1796 static tree cp_parser_global_scope_opt
1797 (cp_parser *, bool);
1798 static bool cp_parser_constructor_declarator_p
1799 (cp_parser *, bool);
1800 static tree cp_parser_function_definition_from_specifiers_and_declarator
1801 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1802 static tree cp_parser_function_definition_after_declarator
1803 (cp_parser *, bool);
1804 static void cp_parser_template_declaration_after_export
1805 (cp_parser *, bool);
1806 static void cp_parser_perform_template_parameter_access_checks
1807 (tree);
1808 static tree cp_parser_single_declaration
1809 (cp_parser *, tree, bool, bool *);
1810 static tree cp_parser_functional_cast
1811 (cp_parser *, tree);
1812 static tree cp_parser_save_member_function_body
1813 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1814 static tree cp_parser_enclosed_template_argument_list
1815 (cp_parser *);
1816 static void cp_parser_save_default_args
1817 (cp_parser *, tree);
1818 static void cp_parser_late_parsing_for_member
1819 (cp_parser *, tree);
1820 static void cp_parser_late_parsing_default_args
1821 (cp_parser *, tree);
1822 static tree cp_parser_sizeof_operand
1823 (cp_parser *, enum rid);
1824 static bool cp_parser_declares_only_class_p
1825 (cp_parser *);
1826 static void cp_parser_set_storage_class
1827 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1828 static void cp_parser_set_decl_spec_type
1829 (cp_decl_specifier_seq *, tree, bool);
1830 static bool cp_parser_friend_p
1831 (const cp_decl_specifier_seq *);
1832 static cp_token *cp_parser_require
1833 (cp_parser *, enum cpp_ttype, const char *);
1834 static cp_token *cp_parser_require_keyword
1835 (cp_parser *, enum rid, const char *);
1836 static bool cp_parser_token_starts_function_definition_p
1837 (cp_token *);
1838 static bool cp_parser_next_token_starts_class_definition_p
1839 (cp_parser *);
1840 static bool cp_parser_next_token_ends_template_argument_p
1841 (cp_parser *);
1842 static bool cp_parser_nth_token_starts_template_argument_list_p
1843 (cp_parser *, size_t);
1844 static enum tag_types cp_parser_token_is_class_key
1845 (cp_token *);
1846 static void cp_parser_check_class_key
1847 (enum tag_types, tree type);
1848 static void cp_parser_check_access_in_redeclaration
1849 (tree type);
1850 static bool cp_parser_optional_template_keyword
1851 (cp_parser *);
1852 static void cp_parser_pre_parsed_nested_name_specifier
1853 (cp_parser *);
1854 static void cp_parser_cache_group
1855 (cp_parser *, enum cpp_ttype, unsigned);
1856 static void cp_parser_parse_tentatively
1857 (cp_parser *);
1858 static void cp_parser_commit_to_tentative_parse
1859 (cp_parser *);
1860 static void cp_parser_abort_tentative_parse
1861 (cp_parser *);
1862 static bool cp_parser_parse_definitely
1863 (cp_parser *);
1864 static inline bool cp_parser_parsing_tentatively
1865 (cp_parser *);
1866 static bool cp_parser_uncommitted_to_tentative_parse_p
1867 (cp_parser *);
1868 static void cp_parser_error
1869 (cp_parser *, const char *);
1870 static void cp_parser_name_lookup_error
1871 (cp_parser *, tree, tree, const char *);
1872 static bool cp_parser_simulate_error
1873 (cp_parser *);
1874 static bool cp_parser_check_type_definition
1875 (cp_parser *);
1876 static void cp_parser_check_for_definition_in_return_type
1877 (cp_declarator *, tree);
1878 static void cp_parser_check_for_invalid_template_id
1879 (cp_parser *, tree);
1880 static bool cp_parser_non_integral_constant_expression
1881 (cp_parser *, const char *);
1882 static void cp_parser_diagnose_invalid_type_name
1883 (cp_parser *, tree, tree);
1884 static bool cp_parser_parse_and_diagnose_invalid_type_name
1885 (cp_parser *);
1886 static int cp_parser_skip_to_closing_parenthesis
1887 (cp_parser *, bool, bool, bool);
1888 static void cp_parser_skip_to_end_of_statement
1889 (cp_parser *);
1890 static void cp_parser_consume_semicolon_at_end_of_statement
1891 (cp_parser *);
1892 static void cp_parser_skip_to_end_of_block_or_statement
1893 (cp_parser *);
1894 static void cp_parser_skip_to_closing_brace
1895 (cp_parser *);
1896 static void cp_parser_skip_until_found
1897 (cp_parser *, enum cpp_ttype, const char *);
1898 static bool cp_parser_error_occurred
1899 (cp_parser *);
1900 static bool cp_parser_allow_gnu_extensions_p
1901 (cp_parser *);
1902 static bool cp_parser_is_string_literal
1903 (cp_token *);
1904 static bool cp_parser_is_keyword
1905 (cp_token *, enum rid);
1906 static tree cp_parser_make_typename_type
1907 (cp_parser *, tree, tree);
1909 /* Returns nonzero if we are parsing tentatively. */
1911 static inline bool
1912 cp_parser_parsing_tentatively (cp_parser* parser)
1914 return parser->context->next != NULL;
1917 /* Returns nonzero if TOKEN is a string literal. */
1919 static bool
1920 cp_parser_is_string_literal (cp_token* token)
1922 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1925 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1927 static bool
1928 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1930 return token->keyword == keyword;
1933 /* A minimum or maximum operator has been seen. As these are
1934 deprecated, issue a warning. */
1936 static inline void
1937 cp_parser_warn_min_max (void)
1939 if (warn_deprecated && !in_system_header)
1940 warning (0, "minimum/maximum operators are deprecated");
1943 /* If not parsing tentatively, issue a diagnostic of the form
1944 FILE:LINE: MESSAGE before TOKEN
1945 where TOKEN is the next token in the input stream. MESSAGE
1946 (specified by the caller) is usually of the form "expected
1947 OTHER-TOKEN". */
1949 static void
1950 cp_parser_error (cp_parser* parser, const char* message)
1952 if (!cp_parser_simulate_error (parser))
1954 cp_token *token = cp_lexer_peek_token (parser->lexer);
1955 /* This diagnostic makes more sense if it is tagged to the line
1956 of the token we just peeked at. */
1957 cp_lexer_set_source_position_from_token (token);
1958 if (token->type == CPP_PRAGMA)
1960 error ("%<#pragma%> is not allowed here");
1961 cp_lexer_purge_token (parser->lexer);
1962 return;
1964 c_parse_error (message,
1965 /* Because c_parser_error does not understand
1966 CPP_KEYWORD, keywords are treated like
1967 identifiers. */
1968 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1969 token->value);
1973 /* Issue an error about name-lookup failing. NAME is the
1974 IDENTIFIER_NODE DECL is the result of
1975 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1976 the thing that we hoped to find. */
1978 static void
1979 cp_parser_name_lookup_error (cp_parser* parser,
1980 tree name,
1981 tree decl,
1982 const char* desired)
1984 /* If name lookup completely failed, tell the user that NAME was not
1985 declared. */
1986 if (decl == error_mark_node)
1988 if (parser->scope && parser->scope != global_namespace)
1989 error ("%<%D::%D%> has not been declared",
1990 parser->scope, name);
1991 else if (parser->scope == global_namespace)
1992 error ("%<::%D%> has not been declared", name);
1993 else if (parser->object_scope
1994 && !CLASS_TYPE_P (parser->object_scope))
1995 error ("request for member %qD in non-class type %qT",
1996 name, parser->object_scope);
1997 else if (parser->object_scope)
1998 error ("%<%T::%D%> has not been declared",
1999 parser->object_scope, name);
2000 else
2001 error ("%qD has not been declared", name);
2003 else if (parser->scope && parser->scope != global_namespace)
2004 error ("%<%D::%D%> %s", parser->scope, name, desired);
2005 else if (parser->scope == global_namespace)
2006 error ("%<::%D%> %s", name, desired);
2007 else
2008 error ("%qD %s", name, desired);
2011 /* If we are parsing tentatively, remember that an error has occurred
2012 during this tentative parse. Returns true if the error was
2013 simulated; false if a message should be issued by the caller. */
2015 static bool
2016 cp_parser_simulate_error (cp_parser* parser)
2018 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2020 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2021 return true;
2023 return false;
2026 /* Check for repeated decl-specifiers. */
2028 static void
2029 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2031 cp_decl_spec ds;
2033 for (ds = ds_first; ds != ds_last; ++ds)
2035 unsigned count = decl_specs->specs[(int)ds];
2036 if (count < 2)
2037 continue;
2038 /* The "long" specifier is a special case because of "long long". */
2039 if (ds == ds_long)
2041 if (count > 2)
2042 error ("%<long long long%> is too long for GCC");
2043 else if (pedantic && !in_system_header && warn_long_long)
2044 pedwarn ("ISO C++ does not support %<long long%>");
2046 else if (count > 1)
2048 static const char *const decl_spec_names[] = {
2049 "signed",
2050 "unsigned",
2051 "short",
2052 "long",
2053 "const",
2054 "volatile",
2055 "restrict",
2056 "inline",
2057 "virtual",
2058 "explicit",
2059 "friend",
2060 "typedef",
2061 "__complex",
2062 "__thread"
2064 error ("duplicate %qs", decl_spec_names[(int)ds]);
2069 /* This function is called when a type is defined. If type
2070 definitions are forbidden at this point, an error message is
2071 issued. */
2073 static bool
2074 cp_parser_check_type_definition (cp_parser* parser)
2076 /* If types are forbidden here, issue a message. */
2077 if (parser->type_definition_forbidden_message)
2079 /* Use `%s' to print the string in case there are any escape
2080 characters in the message. */
2081 error ("%s", parser->type_definition_forbidden_message);
2082 return false;
2084 return true;
2087 /* This function is called when the DECLARATOR is processed. The TYPE
2088 was a type defined in the decl-specifiers. If it is invalid to
2089 define a type in the decl-specifiers for DECLARATOR, an error is
2090 issued. */
2092 static void
2093 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2094 tree type)
2096 /* [dcl.fct] forbids type definitions in return types.
2097 Unfortunately, it's not easy to know whether or not we are
2098 processing a return type until after the fact. */
2099 while (declarator
2100 && (declarator->kind == cdk_pointer
2101 || declarator->kind == cdk_reference
2102 || declarator->kind == cdk_ptrmem))
2103 declarator = declarator->declarator;
2104 if (declarator
2105 && declarator->kind == cdk_function)
2107 error ("new types may not be defined in a return type");
2108 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2109 type);
2113 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2114 "<" in any valid C++ program. If the next token is indeed "<",
2115 issue a message warning the user about what appears to be an
2116 invalid attempt to form a template-id. */
2118 static void
2119 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2120 tree type)
2122 cp_token_position start = 0;
2124 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2126 if (TYPE_P (type))
2127 error ("%qT is not a template", type);
2128 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2129 error ("%qE is not a template", type);
2130 else
2131 error ("invalid template-id");
2132 /* Remember the location of the invalid "<". */
2133 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2134 start = cp_lexer_token_position (parser->lexer, true);
2135 /* Consume the "<". */
2136 cp_lexer_consume_token (parser->lexer);
2137 /* Parse the template arguments. */
2138 cp_parser_enclosed_template_argument_list (parser);
2139 /* Permanently remove the invalid template arguments so that
2140 this error message is not issued again. */
2141 if (start)
2142 cp_lexer_purge_tokens_after (parser->lexer, start);
2146 /* If parsing an integral constant-expression, issue an error message
2147 about the fact that THING appeared and return true. Otherwise,
2148 return false. In either case, set
2149 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2151 static bool
2152 cp_parser_non_integral_constant_expression (cp_parser *parser,
2153 const char *thing)
2155 parser->non_integral_constant_expression_p = true;
2156 if (parser->integral_constant_expression_p)
2158 if (!parser->allow_non_integral_constant_expression_p)
2160 error ("%s cannot appear in a constant-expression", thing);
2161 return true;
2164 return false;
2167 /* Emit a diagnostic for an invalid type name. SCOPE is the
2168 qualifying scope (or NULL, if none) for ID. This function commits
2169 to the current active tentative parse, if any. (Otherwise, the
2170 problematic construct might be encountered again later, resulting
2171 in duplicate error messages.) */
2173 static void
2174 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2176 tree decl, old_scope;
2177 /* Try to lookup the identifier. */
2178 old_scope = parser->scope;
2179 parser->scope = scope;
2180 decl = cp_parser_lookup_name_simple (parser, id);
2181 parser->scope = old_scope;
2182 /* If the lookup found a template-name, it means that the user forgot
2183 to specify an argument list. Emit a useful error message. */
2184 if (TREE_CODE (decl) == TEMPLATE_DECL)
2185 error ("invalid use of template-name %qE without an argument list", decl);
2186 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2187 error ("invalid use of destructor %qD as a type", id);
2188 else if (TREE_CODE (decl) == TYPE_DECL)
2189 /* Something like 'unsigned A a;' */
2190 error ("invalid combination of multiple type-specifiers");
2191 else if (!parser->scope)
2193 /* Issue an error message. */
2194 error ("%qE does not name a type", id);
2195 /* If we're in a template class, it's possible that the user was
2196 referring to a type from a base class. For example:
2198 template <typename T> struct A { typedef T X; };
2199 template <typename T> struct B : public A<T> { X x; };
2201 The user should have said "typename A<T>::X". */
2202 if (processing_template_decl && current_class_type
2203 && TYPE_BINFO (current_class_type))
2205 tree b;
2207 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2209 b = TREE_CHAIN (b))
2211 tree base_type = BINFO_TYPE (b);
2212 if (CLASS_TYPE_P (base_type)
2213 && dependent_type_p (base_type))
2215 tree field;
2216 /* Go from a particular instantiation of the
2217 template (which will have an empty TYPE_FIELDs),
2218 to the main version. */
2219 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2220 for (field = TYPE_FIELDS (base_type);
2221 field;
2222 field = TREE_CHAIN (field))
2223 if (TREE_CODE (field) == TYPE_DECL
2224 && DECL_NAME (field) == id)
2226 inform ("(perhaps %<typename %T::%E%> was intended)",
2227 BINFO_TYPE (b), id);
2228 break;
2230 if (field)
2231 break;
2236 /* Here we diagnose qualified-ids where the scope is actually correct,
2237 but the identifier does not resolve to a valid type name. */
2238 else if (parser->scope != error_mark_node)
2240 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2241 error ("%qE in namespace %qE does not name a type",
2242 id, parser->scope);
2243 else if (TYPE_P (parser->scope))
2244 error ("%qE in class %qT does not name a type", id, parser->scope);
2245 else
2246 gcc_unreachable ();
2248 cp_parser_commit_to_tentative_parse (parser);
2251 /* Check for a common situation where a type-name should be present,
2252 but is not, and issue a sensible error message. Returns true if an
2253 invalid type-name was detected.
2255 The situation handled by this function are variable declarations of the
2256 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2257 Usually, `ID' should name a type, but if we got here it means that it
2258 does not. We try to emit the best possible error message depending on
2259 how exactly the id-expression looks like.
2262 static bool
2263 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2265 tree id;
2267 cp_parser_parse_tentatively (parser);
2268 id = cp_parser_id_expression (parser,
2269 /*template_keyword_p=*/false,
2270 /*check_dependency_p=*/true,
2271 /*template_p=*/NULL,
2272 /*declarator_p=*/true);
2273 /* After the id-expression, there should be a plain identifier,
2274 otherwise this is not a simple variable declaration. Also, if
2275 the scope is dependent, we cannot do much. */
2276 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2277 || (parser->scope && TYPE_P (parser->scope)
2278 && dependent_type_p (parser->scope)))
2280 cp_parser_abort_tentative_parse (parser);
2281 return false;
2283 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2284 return false;
2286 /* Emit a diagnostic for the invalid type. */
2287 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2288 /* Skip to the end of the declaration; there's no point in
2289 trying to process it. */
2290 cp_parser_skip_to_end_of_block_or_statement (parser);
2291 return true;
2294 /* Consume tokens up to, and including, the next non-nested closing `)'.
2295 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2296 are doing error recovery. Returns -1 if OR_COMMA is true and we
2297 found an unnested comma. */
2299 static int
2300 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2301 bool recovering,
2302 bool or_comma,
2303 bool consume_paren)
2305 unsigned paren_depth = 0;
2306 unsigned brace_depth = 0;
2307 int result;
2309 if (recovering && !or_comma
2310 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2311 return 0;
2313 while (true)
2315 cp_token *token;
2317 /* If we've run out of tokens, then there is no closing `)'. */
2318 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2320 result = 0;
2321 break;
2324 token = cp_lexer_peek_token (parser->lexer);
2326 /* This matches the processing in skip_to_end_of_statement. */
2327 if (token->type == CPP_SEMICOLON && !brace_depth)
2329 result = 0;
2330 break;
2332 if (token->type == CPP_OPEN_BRACE)
2333 ++brace_depth;
2334 if (token->type == CPP_CLOSE_BRACE)
2336 if (!brace_depth--)
2338 result = 0;
2339 break;
2342 if (recovering && or_comma && token->type == CPP_COMMA
2343 && !brace_depth && !paren_depth)
2345 result = -1;
2346 break;
2349 if (!brace_depth)
2351 /* If it is an `(', we have entered another level of nesting. */
2352 if (token->type == CPP_OPEN_PAREN)
2353 ++paren_depth;
2354 /* If it is a `)', then we might be done. */
2355 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2357 if (consume_paren)
2358 cp_lexer_consume_token (parser->lexer);
2360 result = 1;
2361 break;
2366 /* Consume the token. */
2367 cp_lexer_consume_token (parser->lexer);
2370 return result;
2373 /* Consume tokens until we reach the end of the current statement.
2374 Normally, that will be just before consuming a `;'. However, if a
2375 non-nested `}' comes first, then we stop before consuming that. */
2377 static void
2378 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2380 unsigned nesting_depth = 0;
2382 while (true)
2384 cp_token *token;
2386 /* Peek at the next token. */
2387 token = cp_lexer_peek_token (parser->lexer);
2388 /* If we've run out of tokens, stop. */
2389 if (token->type == CPP_EOF)
2390 break;
2391 /* If the next token is a `;', we have reached the end of the
2392 statement. */
2393 if (token->type == CPP_SEMICOLON && !nesting_depth)
2394 break;
2395 /* If the next token is a non-nested `}', then we have reached
2396 the end of the current block. */
2397 if (token->type == CPP_CLOSE_BRACE)
2399 /* If this is a non-nested `}', stop before consuming it.
2400 That way, when confronted with something like:
2402 { 3 + }
2404 we stop before consuming the closing `}', even though we
2405 have not yet reached a `;'. */
2406 if (nesting_depth == 0)
2407 break;
2408 /* If it is the closing `}' for a block that we have
2409 scanned, stop -- but only after consuming the token.
2410 That way given:
2412 void f g () { ... }
2413 typedef int I;
2415 we will stop after the body of the erroneously declared
2416 function, but before consuming the following `typedef'
2417 declaration. */
2418 if (--nesting_depth == 0)
2420 cp_lexer_consume_token (parser->lexer);
2421 break;
2424 /* If it the next token is a `{', then we are entering a new
2425 block. Consume the entire block. */
2426 else if (token->type == CPP_OPEN_BRACE)
2427 ++nesting_depth;
2428 /* Consume the token. */
2429 cp_lexer_consume_token (parser->lexer);
2433 /* This function is called at the end of a statement or declaration.
2434 If the next token is a semicolon, it is consumed; otherwise, error
2435 recovery is attempted. */
2437 static void
2438 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2440 /* Look for the trailing `;'. */
2441 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2443 /* If there is additional (erroneous) input, skip to the end of
2444 the statement. */
2445 cp_parser_skip_to_end_of_statement (parser);
2446 /* If the next token is now a `;', consume it. */
2447 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2448 cp_lexer_consume_token (parser->lexer);
2452 /* Skip tokens until we have consumed an entire block, or until we
2453 have consumed a non-nested `;'. */
2455 static void
2456 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2458 int nesting_depth = 0;
2460 while (nesting_depth >= 0)
2462 cp_token *token = cp_lexer_peek_token (parser->lexer);
2464 if (token->type == CPP_EOF)
2465 break;
2467 switch (token->type)
2469 case CPP_EOF:
2470 /* If we've run out of tokens, stop. */
2471 nesting_depth = -1;
2472 continue;
2474 case CPP_SEMICOLON:
2475 /* Stop if this is an unnested ';'. */
2476 if (!nesting_depth)
2477 nesting_depth = -1;
2478 break;
2480 case CPP_CLOSE_BRACE:
2481 /* Stop if this is an unnested '}', or closes the outermost
2482 nesting level. */
2483 nesting_depth--;
2484 if (!nesting_depth)
2485 nesting_depth = -1;
2486 break;
2488 case CPP_OPEN_BRACE:
2489 /* Nest. */
2490 nesting_depth++;
2491 break;
2493 default:
2494 break;
2497 /* Consume the token. */
2498 cp_lexer_consume_token (parser->lexer);
2503 /* Skip tokens until a non-nested closing curly brace is the next
2504 token. */
2506 static void
2507 cp_parser_skip_to_closing_brace (cp_parser *parser)
2509 unsigned nesting_depth = 0;
2511 while (true)
2513 cp_token *token;
2515 /* Peek at the next token. */
2516 token = cp_lexer_peek_token (parser->lexer);
2517 /* If we've run out of tokens, stop. */
2518 if (token->type == CPP_EOF)
2519 break;
2520 /* If the next token is a non-nested `}', then we have reached
2521 the end of the current block. */
2522 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2523 break;
2524 /* If it the next token is a `{', then we are entering a new
2525 block. Consume the entire block. */
2526 else if (token->type == CPP_OPEN_BRACE)
2527 ++nesting_depth;
2528 /* Consume the token. */
2529 cp_lexer_consume_token (parser->lexer);
2533 /* This is a simple wrapper around make_typename_type. When the id is
2534 an unresolved identifier node, we can provide a superior diagnostic
2535 using cp_parser_diagnose_invalid_type_name. */
2537 static tree
2538 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2540 tree result;
2541 if (TREE_CODE (id) == IDENTIFIER_NODE)
2543 result = make_typename_type (scope, id, typename_type,
2544 /*complain=*/0);
2545 if (result == error_mark_node)
2546 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2547 return result;
2549 return make_typename_type (scope, id, typename_type, tf_error);
2553 /* Create a new C++ parser. */
2555 static cp_parser *
2556 cp_parser_new (void)
2558 cp_parser *parser;
2559 cp_lexer *lexer;
2560 unsigned i;
2562 /* cp_lexer_new_main is called before calling ggc_alloc because
2563 cp_lexer_new_main might load a PCH file. */
2564 lexer = cp_lexer_new_main ();
2566 /* Initialize the binops_by_token so that we can get the tree
2567 directly from the token. */
2568 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2569 binops_by_token[binops[i].token_type] = binops[i];
2571 parser = GGC_CNEW (cp_parser);
2572 parser->lexer = lexer;
2573 parser->context = cp_parser_context_new (NULL);
2575 /* For now, we always accept GNU extensions. */
2576 parser->allow_gnu_extensions_p = 1;
2578 /* The `>' token is a greater-than operator, not the end of a
2579 template-id. */
2580 parser->greater_than_is_operator_p = true;
2582 parser->default_arg_ok_p = true;
2584 /* We are not parsing a constant-expression. */
2585 parser->integral_constant_expression_p = false;
2586 parser->allow_non_integral_constant_expression_p = false;
2587 parser->non_integral_constant_expression_p = false;
2589 /* Local variable names are not forbidden. */
2590 parser->local_variables_forbidden_p = false;
2592 /* We are not processing an `extern "C"' declaration. */
2593 parser->in_unbraced_linkage_specification_p = false;
2595 /* We are not processing a declarator. */
2596 parser->in_declarator_p = false;
2598 /* We are not processing a template-argument-list. */
2599 parser->in_template_argument_list_p = false;
2601 /* We are not in an iteration statement. */
2602 parser->in_iteration_statement_p = false;
2604 /* We are not in a switch statement. */
2605 parser->in_switch_statement_p = false;
2607 /* We are not parsing a type-id inside an expression. */
2608 parser->in_type_id_in_expr_p = false;
2610 /* Declarations aren't implicitly extern "C". */
2611 parser->implicit_extern_c = false;
2613 /* String literals should be translated to the execution character set. */
2614 parser->translate_strings_p = true;
2616 /* We are not parsing a function body. */
2617 parser->in_function_body = false;
2619 /* The unparsed function queue is empty. */
2620 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2622 /* There are no classes being defined. */
2623 parser->num_classes_being_defined = 0;
2625 /* No template parameters apply. */
2626 parser->num_template_parameter_lists = 0;
2628 return parser;
2631 /* Create a cp_lexer structure which will emit the tokens in CACHE
2632 and push it onto the parser's lexer stack. This is used for delayed
2633 parsing of in-class method bodies and default arguments, and should
2634 not be confused with tentative parsing. */
2635 static void
2636 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2638 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2639 lexer->next = parser->lexer;
2640 parser->lexer = lexer;
2642 /* Move the current source position to that of the first token in the
2643 new lexer. */
2644 cp_lexer_set_source_position_from_token (lexer->next_token);
2647 /* Pop the top lexer off the parser stack. This is never used for the
2648 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2649 static void
2650 cp_parser_pop_lexer (cp_parser *parser)
2652 cp_lexer *lexer = parser->lexer;
2653 parser->lexer = lexer->next;
2654 cp_lexer_destroy (lexer);
2656 /* Put the current source position back where it was before this
2657 lexer was pushed. */
2658 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2661 /* Lexical conventions [gram.lex] */
2663 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2664 identifier. */
2666 static tree
2667 cp_parser_identifier (cp_parser* parser)
2669 cp_token *token;
2671 /* Look for the identifier. */
2672 token = cp_parser_require (parser, CPP_NAME, "identifier");
2673 /* Return the value. */
2674 return token ? token->value : error_mark_node;
2677 /* Parse a sequence of adjacent string constants. Returns a
2678 TREE_STRING representing the combined, nul-terminated string
2679 constant. If TRANSLATE is true, translate the string to the
2680 execution character set. If WIDE_OK is true, a wide string is
2681 invalid here.
2683 C++98 [lex.string] says that if a narrow string literal token is
2684 adjacent to a wide string literal token, the behavior is undefined.
2685 However, C99 6.4.5p4 says that this results in a wide string literal.
2686 We follow C99 here, for consistency with the C front end.
2688 This code is largely lifted from lex_string() in c-lex.c.
2690 FUTURE: ObjC++ will need to handle @-strings here. */
2691 static tree
2692 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2694 tree value;
2695 bool wide = false;
2696 size_t count;
2697 struct obstack str_ob;
2698 cpp_string str, istr, *strs;
2699 cp_token *tok;
2701 tok = cp_lexer_peek_token (parser->lexer);
2702 if (!cp_parser_is_string_literal (tok))
2704 cp_parser_error (parser, "expected string-literal");
2705 return error_mark_node;
2708 /* Try to avoid the overhead of creating and destroying an obstack
2709 for the common case of just one string. */
2710 if (!cp_parser_is_string_literal
2711 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2713 cp_lexer_consume_token (parser->lexer);
2715 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2716 str.len = TREE_STRING_LENGTH (tok->value);
2717 count = 1;
2718 if (tok->type == CPP_WSTRING)
2719 wide = true;
2721 strs = &str;
2723 else
2725 gcc_obstack_init (&str_ob);
2726 count = 0;
2730 cp_lexer_consume_token (parser->lexer);
2731 count++;
2732 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2733 str.len = TREE_STRING_LENGTH (tok->value);
2734 if (tok->type == CPP_WSTRING)
2735 wide = true;
2737 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2739 tok = cp_lexer_peek_token (parser->lexer);
2741 while (cp_parser_is_string_literal (tok));
2743 strs = (cpp_string *) obstack_finish (&str_ob);
2746 if (wide && !wide_ok)
2748 cp_parser_error (parser, "a wide string is invalid in this context");
2749 wide = false;
2752 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2753 (parse_in, strs, count, &istr, wide))
2755 value = build_string (istr.len, (char *)istr.text);
2756 free ((void *)istr.text);
2758 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2759 value = fix_string_type (value);
2761 else
2762 /* cpp_interpret_string has issued an error. */
2763 value = error_mark_node;
2765 if (count > 1)
2766 obstack_free (&str_ob, 0);
2768 return value;
2772 /* Basic concepts [gram.basic] */
2774 /* Parse a translation-unit.
2776 translation-unit:
2777 declaration-seq [opt]
2779 Returns TRUE if all went well. */
2781 static bool
2782 cp_parser_translation_unit (cp_parser* parser)
2784 /* The address of the first non-permanent object on the declarator
2785 obstack. */
2786 static void *declarator_obstack_base;
2788 bool success;
2790 /* Create the declarator obstack, if necessary. */
2791 if (!cp_error_declarator)
2793 gcc_obstack_init (&declarator_obstack);
2794 /* Create the error declarator. */
2795 cp_error_declarator = make_declarator (cdk_error);
2796 /* Create the empty parameter list. */
2797 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2798 /* Remember where the base of the declarator obstack lies. */
2799 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2802 cp_parser_declaration_seq_opt (parser);
2804 /* If there are no tokens left then all went well. */
2805 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2807 /* Get rid of the token array; we don't need it any more. */
2808 cp_lexer_destroy (parser->lexer);
2809 parser->lexer = NULL;
2811 /* This file might have been a context that's implicitly extern
2812 "C". If so, pop the lang context. (Only relevant for PCH.) */
2813 if (parser->implicit_extern_c)
2815 pop_lang_context ();
2816 parser->implicit_extern_c = false;
2819 /* Finish up. */
2820 finish_translation_unit ();
2822 success = true;
2824 else
2826 cp_parser_error (parser, "expected declaration");
2827 success = false;
2830 /* Make sure the declarator obstack was fully cleaned up. */
2831 gcc_assert (obstack_next_free (&declarator_obstack)
2832 == declarator_obstack_base);
2834 /* All went well. */
2835 return success;
2838 /* Expressions [gram.expr] */
2840 /* Parse a primary-expression.
2842 primary-expression:
2843 literal
2844 this
2845 ( expression )
2846 id-expression
2848 GNU Extensions:
2850 primary-expression:
2851 ( compound-statement )
2852 __builtin_va_arg ( assignment-expression , type-id )
2854 Objective-C++ Extension:
2856 primary-expression:
2857 objc-expression
2859 literal:
2860 __null
2862 ADDRESS_P is true iff this expression was immediately preceded by
2863 "&" and therefore might denote a pointer-to-member. CAST_P is true
2864 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2865 true iff this expression is a tempalte argument.
2867 Returns a representation of the expression. Upon return, *IDK
2868 indicates what kind of id-expression (if any) was present. */
2870 static tree
2871 cp_parser_primary_expression (cp_parser *parser,
2872 bool address_p,
2873 bool cast_p,
2874 bool template_arg_p,
2875 cp_id_kind *idk)
2877 cp_token *token;
2879 /* Assume the primary expression is not an id-expression. */
2880 *idk = CP_ID_KIND_NONE;
2882 /* Peek at the next token. */
2883 token = cp_lexer_peek_token (parser->lexer);
2884 switch (token->type)
2886 /* literal:
2887 integer-literal
2888 character-literal
2889 floating-literal
2890 string-literal
2891 boolean-literal */
2892 case CPP_CHAR:
2893 case CPP_WCHAR:
2894 case CPP_NUMBER:
2895 token = cp_lexer_consume_token (parser->lexer);
2896 /* Floating-point literals are only allowed in an integral
2897 constant expression if they are cast to an integral or
2898 enumeration type. */
2899 if (TREE_CODE (token->value) == REAL_CST
2900 && parser->integral_constant_expression_p
2901 && pedantic)
2903 /* CAST_P will be set even in invalid code like "int(2.7 +
2904 ...)". Therefore, we have to check that the next token
2905 is sure to end the cast. */
2906 if (cast_p)
2908 cp_token *next_token;
2910 next_token = cp_lexer_peek_token (parser->lexer);
2911 if (/* The comma at the end of an
2912 enumerator-definition. */
2913 next_token->type != CPP_COMMA
2914 /* The curly brace at the end of an enum-specifier. */
2915 && next_token->type != CPP_CLOSE_BRACE
2916 /* The end of a statement. */
2917 && next_token->type != CPP_SEMICOLON
2918 /* The end of the cast-expression. */
2919 && next_token->type != CPP_CLOSE_PAREN
2920 /* The end of an array bound. */
2921 && next_token->type != CPP_CLOSE_SQUARE
2922 /* The closing ">" in a template-argument-list. */
2923 && (next_token->type != CPP_GREATER
2924 || parser->greater_than_is_operator_p))
2925 cast_p = false;
2928 /* If we are within a cast, then the constraint that the
2929 cast is to an integral or enumeration type will be
2930 checked at that point. If we are not within a cast, then
2931 this code is invalid. */
2932 if (!cast_p)
2933 cp_parser_non_integral_constant_expression
2934 (parser, "floating-point literal");
2936 return token->value;
2938 case CPP_STRING:
2939 case CPP_WSTRING:
2940 /* ??? Should wide strings be allowed when parser->translate_strings_p
2941 is false (i.e. in attributes)? If not, we can kill the third
2942 argument to cp_parser_string_literal. */
2943 return cp_parser_string_literal (parser,
2944 parser->translate_strings_p,
2945 true);
2947 case CPP_OPEN_PAREN:
2949 tree expr;
2950 bool saved_greater_than_is_operator_p;
2952 /* Consume the `('. */
2953 cp_lexer_consume_token (parser->lexer);
2954 /* Within a parenthesized expression, a `>' token is always
2955 the greater-than operator. */
2956 saved_greater_than_is_operator_p
2957 = parser->greater_than_is_operator_p;
2958 parser->greater_than_is_operator_p = true;
2959 /* If we see `( { ' then we are looking at the beginning of
2960 a GNU statement-expression. */
2961 if (cp_parser_allow_gnu_extensions_p (parser)
2962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2964 /* Statement-expressions are not allowed by the standard. */
2965 if (pedantic)
2966 pedwarn ("ISO C++ forbids braced-groups within expressions");
2968 /* And they're not allowed outside of a function-body; you
2969 cannot, for example, write:
2971 int i = ({ int j = 3; j + 1; });
2973 at class or namespace scope. */
2974 if (!parser->in_function_body)
2975 error ("statement-expressions are allowed only inside functions");
2976 /* Start the statement-expression. */
2977 expr = begin_stmt_expr ();
2978 /* Parse the compound-statement. */
2979 cp_parser_compound_statement (parser, expr, false);
2980 /* Finish up. */
2981 expr = finish_stmt_expr (expr, false);
2983 else
2985 /* Parse the parenthesized expression. */
2986 expr = cp_parser_expression (parser, cast_p);
2987 /* Let the front end know that this expression was
2988 enclosed in parentheses. This matters in case, for
2989 example, the expression is of the form `A::B', since
2990 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2991 not. */
2992 finish_parenthesized_expr (expr);
2994 /* The `>' token might be the end of a template-id or
2995 template-parameter-list now. */
2996 parser->greater_than_is_operator_p
2997 = saved_greater_than_is_operator_p;
2998 /* Consume the `)'. */
2999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3000 cp_parser_skip_to_end_of_statement (parser);
3002 return expr;
3005 case CPP_KEYWORD:
3006 switch (token->keyword)
3008 /* These two are the boolean literals. */
3009 case RID_TRUE:
3010 cp_lexer_consume_token (parser->lexer);
3011 return boolean_true_node;
3012 case RID_FALSE:
3013 cp_lexer_consume_token (parser->lexer);
3014 return boolean_false_node;
3016 /* The `__null' literal. */
3017 case RID_NULL:
3018 cp_lexer_consume_token (parser->lexer);
3019 return null_node;
3021 /* Recognize the `this' keyword. */
3022 case RID_THIS:
3023 cp_lexer_consume_token (parser->lexer);
3024 if (parser->local_variables_forbidden_p)
3026 error ("%<this%> may not be used in this context");
3027 return error_mark_node;
3029 /* Pointers cannot appear in constant-expressions. */
3030 if (cp_parser_non_integral_constant_expression (parser,
3031 "`this'"))
3032 return error_mark_node;
3033 return finish_this_expr ();
3035 /* The `operator' keyword can be the beginning of an
3036 id-expression. */
3037 case RID_OPERATOR:
3038 goto id_expression;
3040 case RID_FUNCTION_NAME:
3041 case RID_PRETTY_FUNCTION_NAME:
3042 case RID_C99_FUNCTION_NAME:
3043 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3044 __func__ are the names of variables -- but they are
3045 treated specially. Therefore, they are handled here,
3046 rather than relying on the generic id-expression logic
3047 below. Grammatically, these names are id-expressions.
3049 Consume the token. */
3050 token = cp_lexer_consume_token (parser->lexer);
3051 /* Look up the name. */
3052 return finish_fname (token->value);
3054 case RID_VA_ARG:
3056 tree expression;
3057 tree type;
3059 /* The `__builtin_va_arg' construct is used to handle
3060 `va_arg'. Consume the `__builtin_va_arg' token. */
3061 cp_lexer_consume_token (parser->lexer);
3062 /* Look for the opening `('. */
3063 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3064 /* Now, parse the assignment-expression. */
3065 expression = cp_parser_assignment_expression (parser,
3066 /*cast_p=*/false);
3067 /* Look for the `,'. */
3068 cp_parser_require (parser, CPP_COMMA, "`,'");
3069 /* Parse the type-id. */
3070 type = cp_parser_type_id (parser);
3071 /* Look for the closing `)'. */
3072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3073 /* Using `va_arg' in a constant-expression is not
3074 allowed. */
3075 if (cp_parser_non_integral_constant_expression (parser,
3076 "`va_arg'"))
3077 return error_mark_node;
3078 return build_x_va_arg (expression, type);
3081 case RID_OFFSETOF:
3082 return cp_parser_builtin_offsetof (parser);
3084 /* Objective-C++ expressions. */
3085 case RID_AT_ENCODE:
3086 case RID_AT_PROTOCOL:
3087 case RID_AT_SELECTOR:
3088 return cp_parser_objc_expression (parser);
3090 default:
3091 cp_parser_error (parser, "expected primary-expression");
3092 return error_mark_node;
3095 /* An id-expression can start with either an identifier, a
3096 `::' as the beginning of a qualified-id, or the "operator"
3097 keyword. */
3098 case CPP_NAME:
3099 case CPP_SCOPE:
3100 case CPP_TEMPLATE_ID:
3101 case CPP_NESTED_NAME_SPECIFIER:
3103 tree id_expression;
3104 tree decl;
3105 const char *error_msg;
3106 bool template_p;
3107 bool done;
3109 id_expression:
3110 /* Parse the id-expression. */
3111 id_expression
3112 = cp_parser_id_expression (parser,
3113 /*template_keyword_p=*/false,
3114 /*check_dependency_p=*/true,
3115 &template_p,
3116 /*declarator_p=*/false);
3117 if (id_expression == error_mark_node)
3118 return error_mark_node;
3119 token = cp_lexer_peek_token (parser->lexer);
3120 done = (token->type != CPP_OPEN_SQUARE
3121 && token->type != CPP_OPEN_PAREN
3122 && token->type != CPP_DOT
3123 && token->type != CPP_DEREF
3124 && token->type != CPP_PLUS_PLUS
3125 && token->type != CPP_MINUS_MINUS);
3126 /* If we have a template-id, then no further lookup is
3127 required. If the template-id was for a template-class, we
3128 will sometimes have a TYPE_DECL at this point. */
3129 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3130 || TREE_CODE (id_expression) == TYPE_DECL)
3131 decl = id_expression;
3132 /* Look up the name. */
3133 else
3135 tree ambiguous_decls;
3137 decl = cp_parser_lookup_name (parser, id_expression,
3138 none_type,
3139 template_p,
3140 /*is_namespace=*/false,
3141 /*check_dependency=*/true,
3142 &ambiguous_decls);
3143 /* If the lookup was ambiguous, an error will already have
3144 been issued. */
3145 if (ambiguous_decls)
3146 return error_mark_node;
3148 /* In Objective-C++, an instance variable (ivar) may be preferred
3149 to whatever cp_parser_lookup_name() found. */
3150 decl = objc_lookup_ivar (decl, id_expression);
3152 /* If name lookup gives us a SCOPE_REF, then the
3153 qualifying scope was dependent. */
3154 if (TREE_CODE (decl) == SCOPE_REF)
3155 return decl;
3156 /* Check to see if DECL is a local variable in a context
3157 where that is forbidden. */
3158 if (parser->local_variables_forbidden_p
3159 && local_variable_p (decl))
3161 /* It might be that we only found DECL because we are
3162 trying to be generous with pre-ISO scoping rules.
3163 For example, consider:
3165 int i;
3166 void g() {
3167 for (int i = 0; i < 10; ++i) {}
3168 extern void f(int j = i);
3171 Here, name look up will originally find the out
3172 of scope `i'. We need to issue a warning message,
3173 but then use the global `i'. */
3174 decl = check_for_out_of_scope_variable (decl);
3175 if (local_variable_p (decl))
3177 error ("local variable %qD may not appear in this context",
3178 decl);
3179 return error_mark_node;
3184 decl = (finish_id_expression
3185 (id_expression, decl, parser->scope,
3186 idk,
3187 parser->integral_constant_expression_p,
3188 parser->allow_non_integral_constant_expression_p,
3189 &parser->non_integral_constant_expression_p,
3190 template_p, done, address_p,
3191 template_arg_p,
3192 &error_msg));
3193 if (error_msg)
3194 cp_parser_error (parser, error_msg);
3195 return decl;
3198 /* Anything else is an error. */
3199 default:
3200 /* ...unless we have an Objective-C++ message or string literal, that is. */
3201 if (c_dialect_objc ()
3202 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3203 return cp_parser_objc_expression (parser);
3205 cp_parser_error (parser, "expected primary-expression");
3206 return error_mark_node;
3210 /* Parse an id-expression.
3212 id-expression:
3213 unqualified-id
3214 qualified-id
3216 qualified-id:
3217 :: [opt] nested-name-specifier template [opt] unqualified-id
3218 :: identifier
3219 :: operator-function-id
3220 :: template-id
3222 Return a representation of the unqualified portion of the
3223 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3224 a `::' or nested-name-specifier.
3226 Often, if the id-expression was a qualified-id, the caller will
3227 want to make a SCOPE_REF to represent the qualified-id. This
3228 function does not do this in order to avoid wastefully creating
3229 SCOPE_REFs when they are not required.
3231 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3232 `template' keyword.
3234 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3235 uninstantiated templates.
3237 If *TEMPLATE_P is non-NULL, it is set to true iff the
3238 `template' keyword is used to explicitly indicate that the entity
3239 named is a template.
3241 If DECLARATOR_P is true, the id-expression is appearing as part of
3242 a declarator, rather than as part of an expression. */
3244 static tree
3245 cp_parser_id_expression (cp_parser *parser,
3246 bool template_keyword_p,
3247 bool check_dependency_p,
3248 bool *template_p,
3249 bool declarator_p)
3251 bool global_scope_p;
3252 bool nested_name_specifier_p;
3254 /* Assume the `template' keyword was not used. */
3255 if (template_p)
3256 *template_p = template_keyword_p;
3258 /* Look for the optional `::' operator. */
3259 global_scope_p
3260 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3261 != NULL_TREE);
3262 /* Look for the optional nested-name-specifier. */
3263 nested_name_specifier_p
3264 = (cp_parser_nested_name_specifier_opt (parser,
3265 /*typename_keyword_p=*/false,
3266 check_dependency_p,
3267 /*type_p=*/false,
3268 declarator_p)
3269 != NULL_TREE);
3270 /* If there is a nested-name-specifier, then we are looking at
3271 the first qualified-id production. */
3272 if (nested_name_specifier_p)
3274 tree saved_scope;
3275 tree saved_object_scope;
3276 tree saved_qualifying_scope;
3277 tree unqualified_id;
3278 bool is_template;
3280 /* See if the next token is the `template' keyword. */
3281 if (!template_p)
3282 template_p = &is_template;
3283 *template_p = cp_parser_optional_template_keyword (parser);
3284 /* Name lookup we do during the processing of the
3285 unqualified-id might obliterate SCOPE. */
3286 saved_scope = parser->scope;
3287 saved_object_scope = parser->object_scope;
3288 saved_qualifying_scope = parser->qualifying_scope;
3289 /* Process the final unqualified-id. */
3290 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3291 check_dependency_p,
3292 declarator_p);
3293 /* Restore the SAVED_SCOPE for our caller. */
3294 parser->scope = saved_scope;
3295 parser->object_scope = saved_object_scope;
3296 parser->qualifying_scope = saved_qualifying_scope;
3298 return unqualified_id;
3300 /* Otherwise, if we are in global scope, then we are looking at one
3301 of the other qualified-id productions. */
3302 else if (global_scope_p)
3304 cp_token *token;
3305 tree id;
3307 /* Peek at the next token. */
3308 token = cp_lexer_peek_token (parser->lexer);
3310 /* If it's an identifier, and the next token is not a "<", then
3311 we can avoid the template-id case. This is an optimization
3312 for this common case. */
3313 if (token->type == CPP_NAME
3314 && !cp_parser_nth_token_starts_template_argument_list_p
3315 (parser, 2))
3316 return cp_parser_identifier (parser);
3318 cp_parser_parse_tentatively (parser);
3319 /* Try a template-id. */
3320 id = cp_parser_template_id (parser,
3321 /*template_keyword_p=*/false,
3322 /*check_dependency_p=*/true,
3323 declarator_p);
3324 /* If that worked, we're done. */
3325 if (cp_parser_parse_definitely (parser))
3326 return id;
3328 /* Peek at the next token. (Changes in the token buffer may
3329 have invalidated the pointer obtained above.) */
3330 token = cp_lexer_peek_token (parser->lexer);
3332 switch (token->type)
3334 case CPP_NAME:
3335 return cp_parser_identifier (parser);
3337 case CPP_KEYWORD:
3338 if (token->keyword == RID_OPERATOR)
3339 return cp_parser_operator_function_id (parser);
3340 /* Fall through. */
3342 default:
3343 cp_parser_error (parser, "expected id-expression");
3344 return error_mark_node;
3347 else
3348 return cp_parser_unqualified_id (parser, template_keyword_p,
3349 /*check_dependency_p=*/true,
3350 declarator_p);
3353 /* Parse an unqualified-id.
3355 unqualified-id:
3356 identifier
3357 operator-function-id
3358 conversion-function-id
3359 ~ class-name
3360 template-id
3362 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3363 keyword, in a construct like `A::template ...'.
3365 Returns a representation of unqualified-id. For the `identifier'
3366 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3367 production a BIT_NOT_EXPR is returned; the operand of the
3368 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3369 other productions, see the documentation accompanying the
3370 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3371 names are looked up in uninstantiated templates. If DECLARATOR_P
3372 is true, the unqualified-id is appearing as part of a declarator,
3373 rather than as part of an expression. */
3375 static tree
3376 cp_parser_unqualified_id (cp_parser* parser,
3377 bool template_keyword_p,
3378 bool check_dependency_p,
3379 bool declarator_p)
3381 cp_token *token;
3383 /* Peek at the next token. */
3384 token = cp_lexer_peek_token (parser->lexer);
3386 switch (token->type)
3388 case CPP_NAME:
3390 tree id;
3392 /* We don't know yet whether or not this will be a
3393 template-id. */
3394 cp_parser_parse_tentatively (parser);
3395 /* Try a template-id. */
3396 id = cp_parser_template_id (parser, template_keyword_p,
3397 check_dependency_p,
3398 declarator_p);
3399 /* If it worked, we're done. */
3400 if (cp_parser_parse_definitely (parser))
3401 return id;
3402 /* Otherwise, it's an ordinary identifier. */
3403 return cp_parser_identifier (parser);
3406 case CPP_TEMPLATE_ID:
3407 return cp_parser_template_id (parser, template_keyword_p,
3408 check_dependency_p,
3409 declarator_p);
3411 case CPP_COMPL:
3413 tree type_decl;
3414 tree qualifying_scope;
3415 tree object_scope;
3416 tree scope;
3417 bool done;
3419 /* Consume the `~' token. */
3420 cp_lexer_consume_token (parser->lexer);
3421 /* Parse the class-name. The standard, as written, seems to
3422 say that:
3424 template <typename T> struct S { ~S (); };
3425 template <typename T> S<T>::~S() {}
3427 is invalid, since `~' must be followed by a class-name, but
3428 `S<T>' is dependent, and so not known to be a class.
3429 That's not right; we need to look in uninstantiated
3430 templates. A further complication arises from:
3432 template <typename T> void f(T t) {
3433 t.T::~T();
3436 Here, it is not possible to look up `T' in the scope of `T'
3437 itself. We must look in both the current scope, and the
3438 scope of the containing complete expression.
3440 Yet another issue is:
3442 struct S {
3443 int S;
3444 ~S();
3447 S::~S() {}
3449 The standard does not seem to say that the `S' in `~S'
3450 should refer to the type `S' and not the data member
3451 `S::S'. */
3453 /* DR 244 says that we look up the name after the "~" in the
3454 same scope as we looked up the qualifying name. That idea
3455 isn't fully worked out; it's more complicated than that. */
3456 scope = parser->scope;
3457 object_scope = parser->object_scope;
3458 qualifying_scope = parser->qualifying_scope;
3460 /* Check for invalid scopes. */
3461 if (scope == error_mark_node)
3463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3464 cp_lexer_consume_token (parser->lexer);
3465 return error_mark_node;
3467 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3469 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3470 error ("scope %qT before %<~%> is not a class-name", scope);
3471 cp_parser_simulate_error (parser);
3472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3473 cp_lexer_consume_token (parser->lexer);
3474 return error_mark_node;
3476 gcc_assert (!scope || TYPE_P (scope));
3478 /* If the name is of the form "X::~X" it's OK. */
3479 token = cp_lexer_peek_token (parser->lexer);
3480 if (scope
3481 && token->type == CPP_NAME
3482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3483 == CPP_OPEN_PAREN)
3484 && constructor_name_p (token->value, scope))
3486 cp_lexer_consume_token (parser->lexer);
3487 return build_nt (BIT_NOT_EXPR, scope);
3490 /* If there was an explicit qualification (S::~T), first look
3491 in the scope given by the qualification (i.e., S). */
3492 done = false;
3493 type_decl = NULL_TREE;
3494 if (scope)
3496 cp_parser_parse_tentatively (parser);
3497 type_decl = cp_parser_class_name (parser,
3498 /*typename_keyword_p=*/false,
3499 /*template_keyword_p=*/false,
3500 none_type,
3501 /*check_dependency=*/false,
3502 /*class_head_p=*/false,
3503 declarator_p);
3504 if (cp_parser_parse_definitely (parser))
3505 done = true;
3507 /* In "N::S::~S", look in "N" as well. */
3508 if (!done && scope && qualifying_scope)
3510 cp_parser_parse_tentatively (parser);
3511 parser->scope = qualifying_scope;
3512 parser->object_scope = NULL_TREE;
3513 parser->qualifying_scope = NULL_TREE;
3514 type_decl
3515 = cp_parser_class_name (parser,
3516 /*typename_keyword_p=*/false,
3517 /*template_keyword_p=*/false,
3518 none_type,
3519 /*check_dependency=*/false,
3520 /*class_head_p=*/false,
3521 declarator_p);
3522 if (cp_parser_parse_definitely (parser))
3523 done = true;
3525 /* In "p->S::~T", look in the scope given by "*p" as well. */
3526 else if (!done && object_scope)
3528 cp_parser_parse_tentatively (parser);
3529 parser->scope = object_scope;
3530 parser->object_scope = NULL_TREE;
3531 parser->qualifying_scope = NULL_TREE;
3532 type_decl
3533 = cp_parser_class_name (parser,
3534 /*typename_keyword_p=*/false,
3535 /*template_keyword_p=*/false,
3536 none_type,
3537 /*check_dependency=*/false,
3538 /*class_head_p=*/false,
3539 declarator_p);
3540 if (cp_parser_parse_definitely (parser))
3541 done = true;
3543 /* Look in the surrounding context. */
3544 if (!done)
3546 parser->scope = NULL_TREE;
3547 parser->object_scope = NULL_TREE;
3548 parser->qualifying_scope = NULL_TREE;
3549 type_decl
3550 = cp_parser_class_name (parser,
3551 /*typename_keyword_p=*/false,
3552 /*template_keyword_p=*/false,
3553 none_type,
3554 /*check_dependency=*/false,
3555 /*class_head_p=*/false,
3556 declarator_p);
3558 /* If an error occurred, assume that the name of the
3559 destructor is the same as the name of the qualifying
3560 class. That allows us to keep parsing after running
3561 into ill-formed destructor names. */
3562 if (type_decl == error_mark_node && scope)
3563 return build_nt (BIT_NOT_EXPR, scope);
3564 else if (type_decl == error_mark_node)
3565 return error_mark_node;
3567 /* Check that destructor name and scope match. */
3568 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3570 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3571 error ("declaration of %<~%T%> as member of %qT",
3572 type_decl, scope);
3573 cp_parser_simulate_error (parser);
3574 return error_mark_node;
3577 /* [class.dtor]
3579 A typedef-name that names a class shall not be used as the
3580 identifier in the declarator for a destructor declaration. */
3581 if (declarator_p
3582 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3583 && !DECL_SELF_REFERENCE_P (type_decl)
3584 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3585 error ("typedef-name %qD used as destructor declarator",
3586 type_decl);
3588 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3591 case CPP_KEYWORD:
3592 if (token->keyword == RID_OPERATOR)
3594 tree id;
3596 /* This could be a template-id, so we try that first. */
3597 cp_parser_parse_tentatively (parser);
3598 /* Try a template-id. */
3599 id = cp_parser_template_id (parser, template_keyword_p,
3600 /*check_dependency_p=*/true,
3601 declarator_p);
3602 /* If that worked, we're done. */
3603 if (cp_parser_parse_definitely (parser))
3604 return id;
3605 /* We still don't know whether we're looking at an
3606 operator-function-id or a conversion-function-id. */
3607 cp_parser_parse_tentatively (parser);
3608 /* Try an operator-function-id. */
3609 id = cp_parser_operator_function_id (parser);
3610 /* If that didn't work, try a conversion-function-id. */
3611 if (!cp_parser_parse_definitely (parser))
3612 id = cp_parser_conversion_function_id (parser);
3614 return id;
3616 /* Fall through. */
3618 default:
3619 cp_parser_error (parser, "expected unqualified-id");
3620 return error_mark_node;
3624 /* Parse an (optional) nested-name-specifier.
3626 nested-name-specifier:
3627 class-or-namespace-name :: nested-name-specifier [opt]
3628 class-or-namespace-name :: template nested-name-specifier [opt]
3630 PARSER->SCOPE should be set appropriately before this function is
3631 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3632 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3633 in name lookups.
3635 Sets PARSER->SCOPE to the class (TYPE) or namespace
3636 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3637 it unchanged if there is no nested-name-specifier. Returns the new
3638 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3640 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3641 part of a declaration and/or decl-specifier. */
3643 static tree
3644 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3645 bool typename_keyword_p,
3646 bool check_dependency_p,
3647 bool type_p,
3648 bool is_declaration)
3650 bool success = false;
3651 cp_token_position start = 0;
3652 cp_token *token;
3654 /* Remember where the nested-name-specifier starts. */
3655 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3657 start = cp_lexer_token_position (parser->lexer, false);
3658 push_deferring_access_checks (dk_deferred);
3661 while (true)
3663 tree new_scope;
3664 tree old_scope;
3665 tree saved_qualifying_scope;
3666 bool template_keyword_p;
3668 /* Spot cases that cannot be the beginning of a
3669 nested-name-specifier. */
3670 token = cp_lexer_peek_token (parser->lexer);
3672 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3673 the already parsed nested-name-specifier. */
3674 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3676 /* Grab the nested-name-specifier and continue the loop. */
3677 cp_parser_pre_parsed_nested_name_specifier (parser);
3678 /* If we originally encountered this nested-name-specifier
3679 with IS_DECLARATION set to false, we will not have
3680 resolved TYPENAME_TYPEs, so we must do so here. */
3681 if (is_declaration
3682 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3684 new_scope = resolve_typename_type (parser->scope,
3685 /*only_current_p=*/false);
3686 if (new_scope != error_mark_node)
3687 parser->scope = new_scope;
3689 success = true;
3690 continue;
3693 /* Spot cases that cannot be the beginning of a
3694 nested-name-specifier. On the second and subsequent times
3695 through the loop, we look for the `template' keyword. */
3696 if (success && token->keyword == RID_TEMPLATE)
3698 /* A template-id can start a nested-name-specifier. */
3699 else if (token->type == CPP_TEMPLATE_ID)
3701 else
3703 /* If the next token is not an identifier, then it is
3704 definitely not a class-or-namespace-name. */
3705 if (token->type != CPP_NAME)
3706 break;
3707 /* If the following token is neither a `<' (to begin a
3708 template-id), nor a `::', then we are not looking at a
3709 nested-name-specifier. */
3710 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3711 if (token->type != CPP_SCOPE
3712 && !cp_parser_nth_token_starts_template_argument_list_p
3713 (parser, 2))
3714 break;
3717 /* The nested-name-specifier is optional, so we parse
3718 tentatively. */
3719 cp_parser_parse_tentatively (parser);
3721 /* Look for the optional `template' keyword, if this isn't the
3722 first time through the loop. */
3723 if (success)
3724 template_keyword_p = cp_parser_optional_template_keyword (parser);
3725 else
3726 template_keyword_p = false;
3728 /* Save the old scope since the name lookup we are about to do
3729 might destroy it. */
3730 old_scope = parser->scope;
3731 saved_qualifying_scope = parser->qualifying_scope;
3732 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3733 look up names in "X<T>::I" in order to determine that "Y" is
3734 a template. So, if we have a typename at this point, we make
3735 an effort to look through it. */
3736 if (is_declaration
3737 && !typename_keyword_p
3738 && parser->scope
3739 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3740 parser->scope = resolve_typename_type (parser->scope,
3741 /*only_current_p=*/false);
3742 /* Parse the qualifying entity. */
3743 new_scope
3744 = cp_parser_class_or_namespace_name (parser,
3745 typename_keyword_p,
3746 template_keyword_p,
3747 check_dependency_p,
3748 type_p,
3749 is_declaration);
3750 /* Look for the `::' token. */
3751 cp_parser_require (parser, CPP_SCOPE, "`::'");
3753 /* If we found what we wanted, we keep going; otherwise, we're
3754 done. */
3755 if (!cp_parser_parse_definitely (parser))
3757 bool error_p = false;
3759 /* Restore the OLD_SCOPE since it was valid before the
3760 failed attempt at finding the last
3761 class-or-namespace-name. */
3762 parser->scope = old_scope;
3763 parser->qualifying_scope = saved_qualifying_scope;
3764 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3765 break;
3766 /* If the next token is an identifier, and the one after
3767 that is a `::', then any valid interpretation would have
3768 found a class-or-namespace-name. */
3769 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3770 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3771 == CPP_SCOPE)
3772 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3773 != CPP_COMPL))
3775 token = cp_lexer_consume_token (parser->lexer);
3776 if (!error_p)
3778 if (!token->ambiguous_p)
3780 tree decl;
3781 tree ambiguous_decls;
3783 decl = cp_parser_lookup_name (parser, token->value,
3784 none_type,
3785 /*is_template=*/false,
3786 /*is_namespace=*/false,
3787 /*check_dependency=*/true,
3788 &ambiguous_decls);
3789 if (TREE_CODE (decl) == TEMPLATE_DECL)
3790 error ("%qD used without template parameters", decl);
3791 else if (ambiguous_decls)
3793 error ("reference to %qD is ambiguous",
3794 token->value);
3795 print_candidates (ambiguous_decls);
3796 decl = error_mark_node;
3798 else
3799 cp_parser_name_lookup_error
3800 (parser, token->value, decl,
3801 "is not a class or namespace");
3803 parser->scope = error_mark_node;
3804 error_p = true;
3805 /* Treat this as a successful nested-name-specifier
3806 due to:
3808 [basic.lookup.qual]
3810 If the name found is not a class-name (clause
3811 _class_) or namespace-name (_namespace.def_), the
3812 program is ill-formed. */
3813 success = true;
3815 cp_lexer_consume_token (parser->lexer);
3817 break;
3819 /* We've found one valid nested-name-specifier. */
3820 success = true;
3821 /* Name lookup always gives us a DECL. */
3822 if (TREE_CODE (new_scope) == TYPE_DECL)
3823 new_scope = TREE_TYPE (new_scope);
3824 /* Uses of "template" must be followed by actual templates. */
3825 if (template_keyword_p
3826 && !(CLASS_TYPE_P (new_scope)
3827 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3828 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3829 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3830 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3831 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3832 == TEMPLATE_ID_EXPR)))
3833 pedwarn (TYPE_P (new_scope)
3834 ? "%qT is not a template"
3835 : "%qD is not a template",
3836 new_scope);
3837 /* If it is a class scope, try to complete it; we are about to
3838 be looking up names inside the class. */
3839 if (TYPE_P (new_scope)
3840 /* Since checking types for dependency can be expensive,
3841 avoid doing it if the type is already complete. */
3842 && !COMPLETE_TYPE_P (new_scope)
3843 /* Do not try to complete dependent types. */
3844 && !dependent_type_p (new_scope))
3845 new_scope = complete_type (new_scope);
3846 /* Make sure we look in the right scope the next time through
3847 the loop. */
3848 parser->scope = new_scope;
3851 /* If parsing tentatively, replace the sequence of tokens that makes
3852 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3853 token. That way, should we re-parse the token stream, we will
3854 not have to repeat the effort required to do the parse, nor will
3855 we issue duplicate error messages. */
3856 if (success && start)
3858 cp_token *token;
3859 tree access_checks;
3861 token = cp_lexer_token_at (parser->lexer, start);
3862 /* Reset the contents of the START token. */
3863 token->type = CPP_NESTED_NAME_SPECIFIER;
3864 /* Retrieve any deferred checks. Do not pop this access checks yet
3865 so the memory will not be reclaimed during token replacing below. */
3866 access_checks = get_deferred_access_checks ();
3867 token->value = build_tree_list (copy_list (access_checks),
3868 parser->scope);
3869 TREE_TYPE (token->value) = parser->qualifying_scope;
3870 token->keyword = RID_MAX;
3872 /* Purge all subsequent tokens. */
3873 cp_lexer_purge_tokens_after (parser->lexer, start);
3876 if (start)
3877 pop_to_parent_deferring_access_checks ();
3879 return success ? parser->scope : NULL_TREE;
3882 /* Parse a nested-name-specifier. See
3883 cp_parser_nested_name_specifier_opt for details. This function
3884 behaves identically, except that it will an issue an error if no
3885 nested-name-specifier is present. */
3887 static tree
3888 cp_parser_nested_name_specifier (cp_parser *parser,
3889 bool typename_keyword_p,
3890 bool check_dependency_p,
3891 bool type_p,
3892 bool is_declaration)
3894 tree scope;
3896 /* Look for the nested-name-specifier. */
3897 scope = cp_parser_nested_name_specifier_opt (parser,
3898 typename_keyword_p,
3899 check_dependency_p,
3900 type_p,
3901 is_declaration);
3902 /* If it was not present, issue an error message. */
3903 if (!scope)
3905 cp_parser_error (parser, "expected nested-name-specifier");
3906 parser->scope = NULL_TREE;
3909 return scope;
3912 /* Parse a class-or-namespace-name.
3914 class-or-namespace-name:
3915 class-name
3916 namespace-name
3918 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3919 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3920 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3921 TYPE_P is TRUE iff the next name should be taken as a class-name,
3922 even the same name is declared to be another entity in the same
3923 scope.
3925 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3926 specified by the class-or-namespace-name. If neither is found the
3927 ERROR_MARK_NODE is returned. */
3929 static tree
3930 cp_parser_class_or_namespace_name (cp_parser *parser,
3931 bool typename_keyword_p,
3932 bool template_keyword_p,
3933 bool check_dependency_p,
3934 bool type_p,
3935 bool is_declaration)
3937 tree saved_scope;
3938 tree saved_qualifying_scope;
3939 tree saved_object_scope;
3940 tree scope;
3941 bool only_class_p;
3943 /* Before we try to parse the class-name, we must save away the
3944 current PARSER->SCOPE since cp_parser_class_name will destroy
3945 it. */
3946 saved_scope = parser->scope;
3947 saved_qualifying_scope = parser->qualifying_scope;
3948 saved_object_scope = parser->object_scope;
3949 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3950 there is no need to look for a namespace-name. */
3951 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3952 if (!only_class_p)
3953 cp_parser_parse_tentatively (parser);
3954 scope = cp_parser_class_name (parser,
3955 typename_keyword_p,
3956 template_keyword_p,
3957 type_p ? class_type : none_type,
3958 check_dependency_p,
3959 /*class_head_p=*/false,
3960 is_declaration);
3961 /* If that didn't work, try for a namespace-name. */
3962 if (!only_class_p && !cp_parser_parse_definitely (parser))
3964 /* Restore the saved scope. */
3965 parser->scope = saved_scope;
3966 parser->qualifying_scope = saved_qualifying_scope;
3967 parser->object_scope = saved_object_scope;
3968 /* If we are not looking at an identifier followed by the scope
3969 resolution operator, then this is not part of a
3970 nested-name-specifier. (Note that this function is only used
3971 to parse the components of a nested-name-specifier.) */
3972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3973 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3974 return error_mark_node;
3975 scope = cp_parser_namespace_name (parser);
3978 return scope;
3981 /* Parse a postfix-expression.
3983 postfix-expression:
3984 primary-expression
3985 postfix-expression [ expression ]
3986 postfix-expression ( expression-list [opt] )
3987 simple-type-specifier ( expression-list [opt] )
3988 typename :: [opt] nested-name-specifier identifier
3989 ( expression-list [opt] )
3990 typename :: [opt] nested-name-specifier template [opt] template-id
3991 ( expression-list [opt] )
3992 postfix-expression . template [opt] id-expression
3993 postfix-expression -> template [opt] id-expression
3994 postfix-expression . pseudo-destructor-name
3995 postfix-expression -> pseudo-destructor-name
3996 postfix-expression ++
3997 postfix-expression --
3998 dynamic_cast < type-id > ( expression )
3999 static_cast < type-id > ( expression )
4000 reinterpret_cast < type-id > ( expression )
4001 const_cast < type-id > ( expression )
4002 typeid ( expression )
4003 typeid ( type-id )
4005 GNU Extension:
4007 postfix-expression:
4008 ( type-id ) { initializer-list , [opt] }
4010 This extension is a GNU version of the C99 compound-literal
4011 construct. (The C99 grammar uses `type-name' instead of `type-id',
4012 but they are essentially the same concept.)
4014 If ADDRESS_P is true, the postfix expression is the operand of the
4015 `&' operator. CAST_P is true if this expression is the target of a
4016 cast.
4018 Returns a representation of the expression. */
4020 static tree
4021 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4023 cp_token *token;
4024 enum rid keyword;
4025 cp_id_kind idk = CP_ID_KIND_NONE;
4026 tree postfix_expression = NULL_TREE;
4028 /* Peek at the next token. */
4029 token = cp_lexer_peek_token (parser->lexer);
4030 /* Some of the productions are determined by keywords. */
4031 keyword = token->keyword;
4032 switch (keyword)
4034 case RID_DYNCAST:
4035 case RID_STATCAST:
4036 case RID_REINTCAST:
4037 case RID_CONSTCAST:
4039 tree type;
4040 tree expression;
4041 const char *saved_message;
4043 /* All of these can be handled in the same way from the point
4044 of view of parsing. Begin by consuming the token
4045 identifying the cast. */
4046 cp_lexer_consume_token (parser->lexer);
4048 /* New types cannot be defined in the cast. */
4049 saved_message = parser->type_definition_forbidden_message;
4050 parser->type_definition_forbidden_message
4051 = "types may not be defined in casts";
4053 /* Look for the opening `<'. */
4054 cp_parser_require (parser, CPP_LESS, "`<'");
4055 /* Parse the type to which we are casting. */
4056 type = cp_parser_type_id (parser);
4057 /* Look for the closing `>'. */
4058 cp_parser_require (parser, CPP_GREATER, "`>'");
4059 /* Restore the old message. */
4060 parser->type_definition_forbidden_message = saved_message;
4062 /* And the expression which is being cast. */
4063 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4064 expression = cp_parser_expression (parser, /*cast_p=*/true);
4065 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4067 /* Only type conversions to integral or enumeration types
4068 can be used in constant-expressions. */
4069 if (parser->integral_constant_expression_p
4070 && !dependent_type_p (type)
4071 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4072 && (cp_parser_non_integral_constant_expression
4073 (parser,
4074 "a cast to a type other than an integral or "
4075 "enumeration type")))
4076 return error_mark_node;
4078 switch (keyword)
4080 case RID_DYNCAST:
4081 postfix_expression
4082 = build_dynamic_cast (type, expression);
4083 break;
4084 case RID_STATCAST:
4085 postfix_expression
4086 = build_static_cast (type, expression);
4087 break;
4088 case RID_REINTCAST:
4089 postfix_expression
4090 = build_reinterpret_cast (type, expression);
4091 break;
4092 case RID_CONSTCAST:
4093 postfix_expression
4094 = build_const_cast (type, expression);
4095 break;
4096 default:
4097 gcc_unreachable ();
4100 break;
4102 case RID_TYPEID:
4104 tree type;
4105 const char *saved_message;
4106 bool saved_in_type_id_in_expr_p;
4108 /* Consume the `typeid' token. */
4109 cp_lexer_consume_token (parser->lexer);
4110 /* Look for the `(' token. */
4111 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4112 /* Types cannot be defined in a `typeid' expression. */
4113 saved_message = parser->type_definition_forbidden_message;
4114 parser->type_definition_forbidden_message
4115 = "types may not be defined in a `typeid\' expression";
4116 /* We can't be sure yet whether we're looking at a type-id or an
4117 expression. */
4118 cp_parser_parse_tentatively (parser);
4119 /* Try a type-id first. */
4120 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4121 parser->in_type_id_in_expr_p = true;
4122 type = cp_parser_type_id (parser);
4123 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4124 /* Look for the `)' token. Otherwise, we can't be sure that
4125 we're not looking at an expression: consider `typeid (int
4126 (3))', for example. */
4127 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4128 /* If all went well, simply lookup the type-id. */
4129 if (cp_parser_parse_definitely (parser))
4130 postfix_expression = get_typeid (type);
4131 /* Otherwise, fall back to the expression variant. */
4132 else
4134 tree expression;
4136 /* Look for an expression. */
4137 expression = cp_parser_expression (parser, /*cast_p=*/false);
4138 /* Compute its typeid. */
4139 postfix_expression = build_typeid (expression);
4140 /* Look for the `)' token. */
4141 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4143 /* `typeid' may not appear in an integral constant expression. */
4144 if (cp_parser_non_integral_constant_expression(parser,
4145 "`typeid' operator"))
4146 return error_mark_node;
4147 /* Restore the saved message. */
4148 parser->type_definition_forbidden_message = saved_message;
4150 break;
4152 case RID_TYPENAME:
4154 tree type;
4155 /* The syntax permitted here is the same permitted for an
4156 elaborated-type-specifier. */
4157 type = cp_parser_elaborated_type_specifier (parser,
4158 /*is_friend=*/false,
4159 /*is_declaration=*/false);
4160 postfix_expression = cp_parser_functional_cast (parser, type);
4162 break;
4164 default:
4166 tree type;
4168 /* If the next thing is a simple-type-specifier, we may be
4169 looking at a functional cast. We could also be looking at
4170 an id-expression. So, we try the functional cast, and if
4171 that doesn't work we fall back to the primary-expression. */
4172 cp_parser_parse_tentatively (parser);
4173 /* Look for the simple-type-specifier. */
4174 type = cp_parser_simple_type_specifier (parser,
4175 /*decl_specs=*/NULL,
4176 CP_PARSER_FLAGS_NONE);
4177 /* Parse the cast itself. */
4178 if (!cp_parser_error_occurred (parser))
4179 postfix_expression
4180 = cp_parser_functional_cast (parser, type);
4181 /* If that worked, we're done. */
4182 if (cp_parser_parse_definitely (parser))
4183 break;
4185 /* If the functional-cast didn't work out, try a
4186 compound-literal. */
4187 if (cp_parser_allow_gnu_extensions_p (parser)
4188 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4190 VEC(constructor_elt,gc) *initializer_list = NULL;
4191 bool saved_in_type_id_in_expr_p;
4193 cp_parser_parse_tentatively (parser);
4194 /* Consume the `('. */
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Parse the type. */
4197 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4198 parser->in_type_id_in_expr_p = true;
4199 type = cp_parser_type_id (parser);
4200 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4201 /* Look for the `)'. */
4202 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4203 /* Look for the `{'. */
4204 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4205 /* If things aren't going well, there's no need to
4206 keep going. */
4207 if (!cp_parser_error_occurred (parser))
4209 bool non_constant_p;
4210 /* Parse the initializer-list. */
4211 initializer_list
4212 = cp_parser_initializer_list (parser, &non_constant_p);
4213 /* Allow a trailing `,'. */
4214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4215 cp_lexer_consume_token (parser->lexer);
4216 /* Look for the final `}'. */
4217 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4219 /* If that worked, we're definitely looking at a
4220 compound-literal expression. */
4221 if (cp_parser_parse_definitely (parser))
4223 /* Warn the user that a compound literal is not
4224 allowed in standard C++. */
4225 if (pedantic)
4226 pedwarn ("ISO C++ forbids compound-literals");
4227 /* Form the representation of the compound-literal. */
4228 postfix_expression
4229 = finish_compound_literal (type, initializer_list);
4230 break;
4234 /* It must be a primary-expression. */
4235 postfix_expression
4236 = cp_parser_primary_expression (parser, address_p, cast_p,
4237 /*template_arg_p=*/false,
4238 &idk);
4240 break;
4243 /* Keep looping until the postfix-expression is complete. */
4244 while (true)
4246 if (idk == CP_ID_KIND_UNQUALIFIED
4247 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4248 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4249 /* It is not a Koenig lookup function call. */
4250 postfix_expression
4251 = unqualified_name_lookup_error (postfix_expression);
4253 /* Peek at the next token. */
4254 token = cp_lexer_peek_token (parser->lexer);
4256 switch (token->type)
4258 case CPP_OPEN_SQUARE:
4259 postfix_expression
4260 = cp_parser_postfix_open_square_expression (parser,
4261 postfix_expression,
4262 false);
4263 idk = CP_ID_KIND_NONE;
4264 break;
4266 case CPP_OPEN_PAREN:
4267 /* postfix-expression ( expression-list [opt] ) */
4269 bool koenig_p;
4270 bool is_builtin_constant_p;
4271 bool saved_integral_constant_expression_p = false;
4272 bool saved_non_integral_constant_expression_p = false;
4273 tree args;
4275 is_builtin_constant_p
4276 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4277 if (is_builtin_constant_p)
4279 /* The whole point of __builtin_constant_p is to allow
4280 non-constant expressions to appear as arguments. */
4281 saved_integral_constant_expression_p
4282 = parser->integral_constant_expression_p;
4283 saved_non_integral_constant_expression_p
4284 = parser->non_integral_constant_expression_p;
4285 parser->integral_constant_expression_p = false;
4287 args = (cp_parser_parenthesized_expression_list
4288 (parser, /*is_attribute_list=*/false,
4289 /*cast_p=*/false,
4290 /*non_constant_p=*/NULL));
4291 if (is_builtin_constant_p)
4293 parser->integral_constant_expression_p
4294 = saved_integral_constant_expression_p;
4295 parser->non_integral_constant_expression_p
4296 = saved_non_integral_constant_expression_p;
4299 if (args == error_mark_node)
4301 postfix_expression = error_mark_node;
4302 break;
4305 /* Function calls are not permitted in
4306 constant-expressions. */
4307 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4308 && cp_parser_non_integral_constant_expression (parser,
4309 "a function call"))
4311 postfix_expression = error_mark_node;
4312 break;
4315 koenig_p = false;
4316 if (idk == CP_ID_KIND_UNQUALIFIED)
4318 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4320 if (args)
4322 koenig_p = true;
4323 postfix_expression
4324 = perform_koenig_lookup (postfix_expression, args);
4326 else
4327 postfix_expression
4328 = unqualified_fn_lookup_error (postfix_expression);
4330 /* We do not perform argument-dependent lookup if
4331 normal lookup finds a non-function, in accordance
4332 with the expected resolution of DR 218. */
4333 else if (args && is_overloaded_fn (postfix_expression))
4335 tree fn = get_first_fn (postfix_expression);
4337 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4338 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4340 /* Only do argument dependent lookup if regular
4341 lookup does not find a set of member functions.
4342 [basic.lookup.koenig]/2a */
4343 if (!DECL_FUNCTION_MEMBER_P (fn))
4345 koenig_p = true;
4346 postfix_expression
4347 = perform_koenig_lookup (postfix_expression, args);
4352 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4354 tree instance = TREE_OPERAND (postfix_expression, 0);
4355 tree fn = TREE_OPERAND (postfix_expression, 1);
4357 if (processing_template_decl
4358 && (type_dependent_expression_p (instance)
4359 || (!BASELINK_P (fn)
4360 && TREE_CODE (fn) != FIELD_DECL)
4361 || type_dependent_expression_p (fn)
4362 || any_type_dependent_arguments_p (args)))
4364 postfix_expression
4365 = build_min_nt (CALL_EXPR, postfix_expression,
4366 args, NULL_TREE);
4367 break;
4370 if (BASELINK_P (fn))
4371 postfix_expression
4372 = (build_new_method_call
4373 (instance, fn, args, NULL_TREE,
4374 (idk == CP_ID_KIND_QUALIFIED
4375 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4376 else
4377 postfix_expression
4378 = finish_call_expr (postfix_expression, args,
4379 /*disallow_virtual=*/false,
4380 /*koenig_p=*/false);
4382 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4383 || TREE_CODE (postfix_expression) == MEMBER_REF
4384 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4385 postfix_expression = (build_offset_ref_call_from_tree
4386 (postfix_expression, args));
4387 else if (idk == CP_ID_KIND_QUALIFIED)
4388 /* A call to a static class member, or a namespace-scope
4389 function. */
4390 postfix_expression
4391 = finish_call_expr (postfix_expression, args,
4392 /*disallow_virtual=*/true,
4393 koenig_p);
4394 else
4395 /* All other function calls. */
4396 postfix_expression
4397 = finish_call_expr (postfix_expression, args,
4398 /*disallow_virtual=*/false,
4399 koenig_p);
4401 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4402 idk = CP_ID_KIND_NONE;
4404 break;
4406 case CPP_DOT:
4407 case CPP_DEREF:
4408 /* postfix-expression . template [opt] id-expression
4409 postfix-expression . pseudo-destructor-name
4410 postfix-expression -> template [opt] id-expression
4411 postfix-expression -> pseudo-destructor-name */
4413 /* Consume the `.' or `->' operator. */
4414 cp_lexer_consume_token (parser->lexer);
4416 postfix_expression
4417 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4418 postfix_expression,
4419 false, &idk);
4420 break;
4422 case CPP_PLUS_PLUS:
4423 /* postfix-expression ++ */
4424 /* Consume the `++' token. */
4425 cp_lexer_consume_token (parser->lexer);
4426 /* Generate a representation for the complete expression. */
4427 postfix_expression
4428 = finish_increment_expr (postfix_expression,
4429 POSTINCREMENT_EXPR);
4430 /* Increments may not appear in constant-expressions. */
4431 if (cp_parser_non_integral_constant_expression (parser,
4432 "an increment"))
4433 postfix_expression = error_mark_node;
4434 idk = CP_ID_KIND_NONE;
4435 break;
4437 case CPP_MINUS_MINUS:
4438 /* postfix-expression -- */
4439 /* Consume the `--' token. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Generate a representation for the complete expression. */
4442 postfix_expression
4443 = finish_increment_expr (postfix_expression,
4444 POSTDECREMENT_EXPR);
4445 /* Decrements may not appear in constant-expressions. */
4446 if (cp_parser_non_integral_constant_expression (parser,
4447 "a decrement"))
4448 postfix_expression = error_mark_node;
4449 idk = CP_ID_KIND_NONE;
4450 break;
4452 default:
4453 return postfix_expression;
4457 /* We should never get here. */
4458 gcc_unreachable ();
4459 return error_mark_node;
4462 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4463 by cp_parser_builtin_offsetof. We're looking for
4465 postfix-expression [ expression ]
4467 FOR_OFFSETOF is set if we're being called in that context, which
4468 changes how we deal with integer constant expressions. */
4470 static tree
4471 cp_parser_postfix_open_square_expression (cp_parser *parser,
4472 tree postfix_expression,
4473 bool for_offsetof)
4475 tree index;
4477 /* Consume the `[' token. */
4478 cp_lexer_consume_token (parser->lexer);
4480 /* Parse the index expression. */
4481 /* ??? For offsetof, there is a question of what to allow here. If
4482 offsetof is not being used in an integral constant expression context,
4483 then we *could* get the right answer by computing the value at runtime.
4484 If we are in an integral constant expression context, then we might
4485 could accept any constant expression; hard to say without analysis.
4486 Rather than open the barn door too wide right away, allow only integer
4487 constant expressions here. */
4488 if (for_offsetof)
4489 index = cp_parser_constant_expression (parser, false, NULL);
4490 else
4491 index = cp_parser_expression (parser, /*cast_p=*/false);
4493 /* Look for the closing `]'. */
4494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4496 /* Build the ARRAY_REF. */
4497 postfix_expression = grok_array_decl (postfix_expression, index);
4499 /* When not doing offsetof, array references are not permitted in
4500 constant-expressions. */
4501 if (!for_offsetof
4502 && (cp_parser_non_integral_constant_expression
4503 (parser, "an array reference")))
4504 postfix_expression = error_mark_node;
4506 return postfix_expression;
4509 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4510 by cp_parser_builtin_offsetof. We're looking for
4512 postfix-expression . template [opt] id-expression
4513 postfix-expression . pseudo-destructor-name
4514 postfix-expression -> template [opt] id-expression
4515 postfix-expression -> pseudo-destructor-name
4517 FOR_OFFSETOF is set if we're being called in that context. That sorta
4518 limits what of the above we'll actually accept, but nevermind.
4519 TOKEN_TYPE is the "." or "->" token, which will already have been
4520 removed from the stream. */
4522 static tree
4523 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4524 enum cpp_ttype token_type,
4525 tree postfix_expression,
4526 bool for_offsetof, cp_id_kind *idk)
4528 tree name;
4529 bool dependent_p;
4530 bool pseudo_destructor_p;
4531 tree scope = NULL_TREE;
4533 /* If this is a `->' operator, dereference the pointer. */
4534 if (token_type == CPP_DEREF)
4535 postfix_expression = build_x_arrow (postfix_expression);
4536 /* Check to see whether or not the expression is type-dependent. */
4537 dependent_p = type_dependent_expression_p (postfix_expression);
4538 /* The identifier following the `->' or `.' is not qualified. */
4539 parser->scope = NULL_TREE;
4540 parser->qualifying_scope = NULL_TREE;
4541 parser->object_scope = NULL_TREE;
4542 *idk = CP_ID_KIND_NONE;
4543 /* Enter the scope corresponding to the type of the object
4544 given by the POSTFIX_EXPRESSION. */
4545 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4547 scope = TREE_TYPE (postfix_expression);
4548 /* According to the standard, no expression should ever have
4549 reference type. Unfortunately, we do not currently match
4550 the standard in this respect in that our internal representation
4551 of an expression may have reference type even when the standard
4552 says it does not. Therefore, we have to manually obtain the
4553 underlying type here. */
4554 scope = non_reference (scope);
4555 /* The type of the POSTFIX_EXPRESSION must be complete. */
4556 if (scope == unknown_type_node)
4558 error ("%qE does not have class type", postfix_expression);
4559 scope = NULL_TREE;
4561 else
4562 scope = complete_type_or_else (scope, NULL_TREE);
4563 /* Let the name lookup machinery know that we are processing a
4564 class member access expression. */
4565 parser->context->object_type = scope;
4566 /* If something went wrong, we want to be able to discern that case,
4567 as opposed to the case where there was no SCOPE due to the type
4568 of expression being dependent. */
4569 if (!scope)
4570 scope = error_mark_node;
4571 /* If the SCOPE was erroneous, make the various semantic analysis
4572 functions exit quickly -- and without issuing additional error
4573 messages. */
4574 if (scope == error_mark_node)
4575 postfix_expression = error_mark_node;
4578 /* Assume this expression is not a pseudo-destructor access. */
4579 pseudo_destructor_p = false;
4581 /* If the SCOPE is a scalar type, then, if this is a valid program,
4582 we must be looking at a pseudo-destructor-name. */
4583 if (scope && SCALAR_TYPE_P (scope))
4585 tree s;
4586 tree type;
4588 cp_parser_parse_tentatively (parser);
4589 /* Parse the pseudo-destructor-name. */
4590 s = NULL_TREE;
4591 cp_parser_pseudo_destructor_name (parser, &s, &type);
4592 if (cp_parser_parse_definitely (parser))
4594 pseudo_destructor_p = true;
4595 postfix_expression
4596 = finish_pseudo_destructor_expr (postfix_expression,
4597 s, TREE_TYPE (type));
4601 if (!pseudo_destructor_p)
4603 /* If the SCOPE is not a scalar type, we are looking at an
4604 ordinary class member access expression, rather than a
4605 pseudo-destructor-name. */
4606 bool template_p;
4607 /* Parse the id-expression. */
4608 name = (cp_parser_id_expression
4609 (parser,
4610 cp_parser_optional_template_keyword (parser),
4611 /*check_dependency_p=*/true,
4612 &template_p,
4613 /*declarator_p=*/false));
4614 /* In general, build a SCOPE_REF if the member name is qualified.
4615 However, if the name was not dependent and has already been
4616 resolved; there is no need to build the SCOPE_REF. For example;
4618 struct X { void f(); };
4619 template <typename T> void f(T* t) { t->X::f(); }
4621 Even though "t" is dependent, "X::f" is not and has been resolved
4622 to a BASELINK; there is no need to include scope information. */
4624 /* But we do need to remember that there was an explicit scope for
4625 virtual function calls. */
4626 if (parser->scope)
4627 *idk = CP_ID_KIND_QUALIFIED;
4629 /* If the name is a template-id that names a type, we will get a
4630 TYPE_DECL here. That is invalid code. */
4631 if (TREE_CODE (name) == TYPE_DECL)
4633 error ("invalid use of %qD", name);
4634 postfix_expression = error_mark_node;
4636 else
4638 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4640 name = build_qualified_name (/*type=*/NULL_TREE,
4641 parser->scope,
4642 name,
4643 template_p);
4644 parser->scope = NULL_TREE;
4645 parser->qualifying_scope = NULL_TREE;
4646 parser->object_scope = NULL_TREE;
4648 if (scope && name && BASELINK_P (name))
4649 adjust_result_of_qualified_name_lookup
4650 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4651 postfix_expression
4652 = finish_class_member_access_expr (postfix_expression, name,
4653 template_p);
4657 /* We no longer need to look up names in the scope of the object on
4658 the left-hand side of the `.' or `->' operator. */
4659 parser->context->object_type = NULL_TREE;
4661 /* Outside of offsetof, these operators may not appear in
4662 constant-expressions. */
4663 if (!for_offsetof
4664 && (cp_parser_non_integral_constant_expression
4665 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4666 postfix_expression = error_mark_node;
4668 return postfix_expression;
4671 /* Parse a parenthesized expression-list.
4673 expression-list:
4674 assignment-expression
4675 expression-list, assignment-expression
4677 attribute-list:
4678 expression-list
4679 identifier
4680 identifier, expression-list
4682 CAST_P is true if this expression is the target of a cast.
4684 Returns a TREE_LIST. The TREE_VALUE of each node is a
4685 representation of an assignment-expression. Note that a TREE_LIST
4686 is returned even if there is only a single expression in the list.
4687 error_mark_node is returned if the ( and or ) are
4688 missing. NULL_TREE is returned on no expressions. The parentheses
4689 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4690 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4691 indicates whether or not all of the expressions in the list were
4692 constant. */
4694 static tree
4695 cp_parser_parenthesized_expression_list (cp_parser* parser,
4696 bool is_attribute_list,
4697 bool cast_p,
4698 bool *non_constant_p)
4700 tree expression_list = NULL_TREE;
4701 bool fold_expr_p = is_attribute_list;
4702 tree identifier = NULL_TREE;
4703 bool saved_greater_than_is_operator_p;
4705 /* Assume all the expressions will be constant. */
4706 if (non_constant_p)
4707 *non_constant_p = false;
4709 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4710 return error_mark_node;
4712 /* Within a parenthesized expression, a `>' token is always
4713 the greater-than operator. */
4714 saved_greater_than_is_operator_p
4715 = parser->greater_than_is_operator_p;
4716 parser->greater_than_is_operator_p = true;
4718 /* Consume expressions until there are no more. */
4719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4720 while (true)
4722 tree expr;
4724 /* At the beginning of attribute lists, check to see if the
4725 next token is an identifier. */
4726 if (is_attribute_list
4727 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4729 cp_token *token;
4731 /* Consume the identifier. */
4732 token = cp_lexer_consume_token (parser->lexer);
4733 /* Save the identifier. */
4734 identifier = token->value;
4736 else
4738 /* Parse the next assignment-expression. */
4739 if (non_constant_p)
4741 bool expr_non_constant_p;
4742 expr = (cp_parser_constant_expression
4743 (parser, /*allow_non_constant_p=*/true,
4744 &expr_non_constant_p));
4745 if (expr_non_constant_p)
4746 *non_constant_p = true;
4748 else
4749 expr = cp_parser_assignment_expression (parser, cast_p);
4751 if (fold_expr_p)
4752 expr = fold_non_dependent_expr (expr);
4754 /* Add it to the list. We add error_mark_node
4755 expressions to the list, so that we can still tell if
4756 the correct form for a parenthesized expression-list
4757 is found. That gives better errors. */
4758 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4760 if (expr == error_mark_node)
4761 goto skip_comma;
4764 /* After the first item, attribute lists look the same as
4765 expression lists. */
4766 is_attribute_list = false;
4768 get_comma:;
4769 /* If the next token isn't a `,', then we are done. */
4770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4771 break;
4773 /* Otherwise, consume the `,' and keep going. */
4774 cp_lexer_consume_token (parser->lexer);
4777 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4779 int ending;
4781 skip_comma:;
4782 /* We try and resync to an unnested comma, as that will give the
4783 user better diagnostics. */
4784 ending = cp_parser_skip_to_closing_parenthesis (parser,
4785 /*recovering=*/true,
4786 /*or_comma=*/true,
4787 /*consume_paren=*/true);
4788 if (ending < 0)
4789 goto get_comma;
4790 if (!ending)
4792 parser->greater_than_is_operator_p
4793 = saved_greater_than_is_operator_p;
4794 return error_mark_node;
4798 parser->greater_than_is_operator_p
4799 = saved_greater_than_is_operator_p;
4801 /* We built up the list in reverse order so we must reverse it now. */
4802 expression_list = nreverse (expression_list);
4803 if (identifier)
4804 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4806 return expression_list;
4809 /* Parse a pseudo-destructor-name.
4811 pseudo-destructor-name:
4812 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4813 :: [opt] nested-name-specifier template template-id :: ~ type-name
4814 :: [opt] nested-name-specifier [opt] ~ type-name
4816 If either of the first two productions is used, sets *SCOPE to the
4817 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4818 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4819 or ERROR_MARK_NODE if the parse fails. */
4821 static void
4822 cp_parser_pseudo_destructor_name (cp_parser* parser,
4823 tree* scope,
4824 tree* type)
4826 bool nested_name_specifier_p;
4828 /* Assume that things will not work out. */
4829 *type = error_mark_node;
4831 /* Look for the optional `::' operator. */
4832 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4833 /* Look for the optional nested-name-specifier. */
4834 nested_name_specifier_p
4835 = (cp_parser_nested_name_specifier_opt (parser,
4836 /*typename_keyword_p=*/false,
4837 /*check_dependency_p=*/true,
4838 /*type_p=*/false,
4839 /*is_declaration=*/true)
4840 != NULL_TREE);
4841 /* Now, if we saw a nested-name-specifier, we might be doing the
4842 second production. */
4843 if (nested_name_specifier_p
4844 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4846 /* Consume the `template' keyword. */
4847 cp_lexer_consume_token (parser->lexer);
4848 /* Parse the template-id. */
4849 cp_parser_template_id (parser,
4850 /*template_keyword_p=*/true,
4851 /*check_dependency_p=*/false,
4852 /*is_declaration=*/true);
4853 /* Look for the `::' token. */
4854 cp_parser_require (parser, CPP_SCOPE, "`::'");
4856 /* If the next token is not a `~', then there might be some
4857 additional qualification. */
4858 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4860 /* Look for the type-name. */
4861 *scope = TREE_TYPE (cp_parser_type_name (parser));
4863 if (*scope == error_mark_node)
4864 return;
4866 /* If we don't have ::~, then something has gone wrong. Since
4867 the only caller of this function is looking for something
4868 after `.' or `->' after a scalar type, most likely the
4869 program is trying to get a member of a non-aggregate
4870 type. */
4871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4872 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4874 cp_parser_error (parser, "request for member of non-aggregate type");
4875 return;
4878 /* Look for the `::' token. */
4879 cp_parser_require (parser, CPP_SCOPE, "`::'");
4881 else
4882 *scope = NULL_TREE;
4884 /* Look for the `~'. */
4885 cp_parser_require (parser, CPP_COMPL, "`~'");
4886 /* Look for the type-name again. We are not responsible for
4887 checking that it matches the first type-name. */
4888 *type = cp_parser_type_name (parser);
4891 /* Parse a unary-expression.
4893 unary-expression:
4894 postfix-expression
4895 ++ cast-expression
4896 -- cast-expression
4897 unary-operator cast-expression
4898 sizeof unary-expression
4899 sizeof ( type-id )
4900 new-expression
4901 delete-expression
4903 GNU Extensions:
4905 unary-expression:
4906 __extension__ cast-expression
4907 __alignof__ unary-expression
4908 __alignof__ ( type-id )
4909 __real__ cast-expression
4910 __imag__ cast-expression
4911 && identifier
4913 ADDRESS_P is true iff the unary-expression is appearing as the
4914 operand of the `&' operator. CAST_P is true if this expression is
4915 the target of a cast.
4917 Returns a representation of the expression. */
4919 static tree
4920 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4922 cp_token *token;
4923 enum tree_code unary_operator;
4925 /* Peek at the next token. */
4926 token = cp_lexer_peek_token (parser->lexer);
4927 /* Some keywords give away the kind of expression. */
4928 if (token->type == CPP_KEYWORD)
4930 enum rid keyword = token->keyword;
4932 switch (keyword)
4934 case RID_ALIGNOF:
4935 case RID_SIZEOF:
4937 tree operand;
4938 enum tree_code op;
4940 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4941 /* Consume the token. */
4942 cp_lexer_consume_token (parser->lexer);
4943 /* Parse the operand. */
4944 operand = cp_parser_sizeof_operand (parser, keyword);
4946 if (TYPE_P (operand))
4947 return cxx_sizeof_or_alignof_type (operand, op, true);
4948 else
4949 return cxx_sizeof_or_alignof_expr (operand, op);
4952 case RID_NEW:
4953 return cp_parser_new_expression (parser);
4955 case RID_DELETE:
4956 return cp_parser_delete_expression (parser);
4958 case RID_EXTENSION:
4960 /* The saved value of the PEDANTIC flag. */
4961 int saved_pedantic;
4962 tree expr;
4964 /* Save away the PEDANTIC flag. */
4965 cp_parser_extension_opt (parser, &saved_pedantic);
4966 /* Parse the cast-expression. */
4967 expr = cp_parser_simple_cast_expression (parser);
4968 /* Restore the PEDANTIC flag. */
4969 pedantic = saved_pedantic;
4971 return expr;
4974 case RID_REALPART:
4975 case RID_IMAGPART:
4977 tree expression;
4979 /* Consume the `__real__' or `__imag__' token. */
4980 cp_lexer_consume_token (parser->lexer);
4981 /* Parse the cast-expression. */
4982 expression = cp_parser_simple_cast_expression (parser);
4983 /* Create the complete representation. */
4984 return build_x_unary_op ((keyword == RID_REALPART
4985 ? REALPART_EXPR : IMAGPART_EXPR),
4986 expression);
4988 break;
4990 default:
4991 break;
4995 /* Look for the `:: new' and `:: delete', which also signal the
4996 beginning of a new-expression, or delete-expression,
4997 respectively. If the next token is `::', then it might be one of
4998 these. */
4999 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5001 enum rid keyword;
5003 /* See if the token after the `::' is one of the keywords in
5004 which we're interested. */
5005 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5006 /* If it's `new', we have a new-expression. */
5007 if (keyword == RID_NEW)
5008 return cp_parser_new_expression (parser);
5009 /* Similarly, for `delete'. */
5010 else if (keyword == RID_DELETE)
5011 return cp_parser_delete_expression (parser);
5014 /* Look for a unary operator. */
5015 unary_operator = cp_parser_unary_operator (token);
5016 /* The `++' and `--' operators can be handled similarly, even though
5017 they are not technically unary-operators in the grammar. */
5018 if (unary_operator == ERROR_MARK)
5020 if (token->type == CPP_PLUS_PLUS)
5021 unary_operator = PREINCREMENT_EXPR;
5022 else if (token->type == CPP_MINUS_MINUS)
5023 unary_operator = PREDECREMENT_EXPR;
5024 /* Handle the GNU address-of-label extension. */
5025 else if (cp_parser_allow_gnu_extensions_p (parser)
5026 && token->type == CPP_AND_AND)
5028 tree identifier;
5030 /* Consume the '&&' token. */
5031 cp_lexer_consume_token (parser->lexer);
5032 /* Look for the identifier. */
5033 identifier = cp_parser_identifier (parser);
5034 /* Create an expression representing the address. */
5035 return finish_label_address_expr (identifier);
5038 if (unary_operator != ERROR_MARK)
5040 tree cast_expression;
5041 tree expression = error_mark_node;
5042 const char *non_constant_p = NULL;
5044 /* Consume the operator token. */
5045 token = cp_lexer_consume_token (parser->lexer);
5046 /* Parse the cast-expression. */
5047 cast_expression
5048 = cp_parser_cast_expression (parser,
5049 unary_operator == ADDR_EXPR,
5050 /*cast_p=*/false);
5051 /* Now, build an appropriate representation. */
5052 switch (unary_operator)
5054 case INDIRECT_REF:
5055 non_constant_p = "`*'";
5056 expression = build_x_indirect_ref (cast_expression, "unary *");
5057 break;
5059 case ADDR_EXPR:
5060 non_constant_p = "`&'";
5061 /* Fall through. */
5062 case BIT_NOT_EXPR:
5063 expression = build_x_unary_op (unary_operator, cast_expression);
5064 break;
5066 case PREINCREMENT_EXPR:
5067 case PREDECREMENT_EXPR:
5068 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5069 ? "`++'" : "`--'");
5070 /* Fall through. */
5071 case UNARY_PLUS_EXPR:
5072 case NEGATE_EXPR:
5073 case TRUTH_NOT_EXPR:
5074 expression = finish_unary_op_expr (unary_operator, cast_expression);
5075 break;
5077 default:
5078 gcc_unreachable ();
5081 if (non_constant_p
5082 && cp_parser_non_integral_constant_expression (parser,
5083 non_constant_p))
5084 expression = error_mark_node;
5086 return expression;
5089 return cp_parser_postfix_expression (parser, address_p, cast_p);
5092 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5093 unary-operator, the corresponding tree code is returned. */
5095 static enum tree_code
5096 cp_parser_unary_operator (cp_token* token)
5098 switch (token->type)
5100 case CPP_MULT:
5101 return INDIRECT_REF;
5103 case CPP_AND:
5104 return ADDR_EXPR;
5106 case CPP_PLUS:
5107 return UNARY_PLUS_EXPR;
5109 case CPP_MINUS:
5110 return NEGATE_EXPR;
5112 case CPP_NOT:
5113 return TRUTH_NOT_EXPR;
5115 case CPP_COMPL:
5116 return BIT_NOT_EXPR;
5118 default:
5119 return ERROR_MARK;
5123 /* Parse a new-expression.
5125 new-expression:
5126 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5127 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5129 Returns a representation of the expression. */
5131 static tree
5132 cp_parser_new_expression (cp_parser* parser)
5134 bool global_scope_p;
5135 tree placement;
5136 tree type;
5137 tree initializer;
5138 tree nelts;
5140 /* Look for the optional `::' operator. */
5141 global_scope_p
5142 = (cp_parser_global_scope_opt (parser,
5143 /*current_scope_valid_p=*/false)
5144 != NULL_TREE);
5145 /* Look for the `new' operator. */
5146 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5147 /* There's no easy way to tell a new-placement from the
5148 `( type-id )' construct. */
5149 cp_parser_parse_tentatively (parser);
5150 /* Look for a new-placement. */
5151 placement = cp_parser_new_placement (parser);
5152 /* If that didn't work out, there's no new-placement. */
5153 if (!cp_parser_parse_definitely (parser))
5154 placement = NULL_TREE;
5156 /* If the next token is a `(', then we have a parenthesized
5157 type-id. */
5158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5160 /* Consume the `('. */
5161 cp_lexer_consume_token (parser->lexer);
5162 /* Parse the type-id. */
5163 type = cp_parser_type_id (parser);
5164 /* Look for the closing `)'. */
5165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5166 /* There should not be a direct-new-declarator in this production,
5167 but GCC used to allowed this, so we check and emit a sensible error
5168 message for this case. */
5169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5171 error ("array bound forbidden after parenthesized type-id");
5172 inform ("try removing the parentheses around the type-id");
5173 cp_parser_direct_new_declarator (parser);
5175 nelts = NULL_TREE;
5177 /* Otherwise, there must be a new-type-id. */
5178 else
5179 type = cp_parser_new_type_id (parser, &nelts);
5181 /* If the next token is a `(', then we have a new-initializer. */
5182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5183 initializer = cp_parser_new_initializer (parser);
5184 else
5185 initializer = NULL_TREE;
5187 /* A new-expression may not appear in an integral constant
5188 expression. */
5189 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5190 return error_mark_node;
5192 /* Create a representation of the new-expression. */
5193 return build_new (placement, type, nelts, initializer, global_scope_p);
5196 /* Parse a new-placement.
5198 new-placement:
5199 ( expression-list )
5201 Returns the same representation as for an expression-list. */
5203 static tree
5204 cp_parser_new_placement (cp_parser* parser)
5206 tree expression_list;
5208 /* Parse the expression-list. */
5209 expression_list = (cp_parser_parenthesized_expression_list
5210 (parser, false, /*cast_p=*/false,
5211 /*non_constant_p=*/NULL));
5213 return expression_list;
5216 /* Parse a new-type-id.
5218 new-type-id:
5219 type-specifier-seq new-declarator [opt]
5221 Returns the TYPE allocated. If the new-type-id indicates an array
5222 type, *NELTS is set to the number of elements in the last array
5223 bound; the TYPE will not include the last array bound. */
5225 static tree
5226 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5228 cp_decl_specifier_seq type_specifier_seq;
5229 cp_declarator *new_declarator;
5230 cp_declarator *declarator;
5231 cp_declarator *outer_declarator;
5232 const char *saved_message;
5233 tree type;
5235 /* The type-specifier sequence must not contain type definitions.
5236 (It cannot contain declarations of new types either, but if they
5237 are not definitions we will catch that because they are not
5238 complete.) */
5239 saved_message = parser->type_definition_forbidden_message;
5240 parser->type_definition_forbidden_message
5241 = "types may not be defined in a new-type-id";
5242 /* Parse the type-specifier-seq. */
5243 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5244 &type_specifier_seq);
5245 /* Restore the old message. */
5246 parser->type_definition_forbidden_message = saved_message;
5247 /* Parse the new-declarator. */
5248 new_declarator = cp_parser_new_declarator_opt (parser);
5250 /* Determine the number of elements in the last array dimension, if
5251 any. */
5252 *nelts = NULL_TREE;
5253 /* Skip down to the last array dimension. */
5254 declarator = new_declarator;
5255 outer_declarator = NULL;
5256 while (declarator && (declarator->kind == cdk_pointer
5257 || declarator->kind == cdk_ptrmem))
5259 outer_declarator = declarator;
5260 declarator = declarator->declarator;
5262 while (declarator
5263 && declarator->kind == cdk_array
5264 && declarator->declarator
5265 && declarator->declarator->kind == cdk_array)
5267 outer_declarator = declarator;
5268 declarator = declarator->declarator;
5271 if (declarator && declarator->kind == cdk_array)
5273 *nelts = declarator->u.array.bounds;
5274 if (*nelts == error_mark_node)
5275 *nelts = integer_one_node;
5277 if (outer_declarator)
5278 outer_declarator->declarator = declarator->declarator;
5279 else
5280 new_declarator = NULL;
5283 type = groktypename (&type_specifier_seq, new_declarator);
5284 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5286 *nelts = array_type_nelts_top (type);
5287 type = TREE_TYPE (type);
5289 return type;
5292 /* Parse an (optional) new-declarator.
5294 new-declarator:
5295 ptr-operator new-declarator [opt]
5296 direct-new-declarator
5298 Returns the declarator. */
5300 static cp_declarator *
5301 cp_parser_new_declarator_opt (cp_parser* parser)
5303 enum tree_code code;
5304 tree type;
5305 cp_cv_quals cv_quals;
5307 /* We don't know if there's a ptr-operator next, or not. */
5308 cp_parser_parse_tentatively (parser);
5309 /* Look for a ptr-operator. */
5310 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5311 /* If that worked, look for more new-declarators. */
5312 if (cp_parser_parse_definitely (parser))
5314 cp_declarator *declarator;
5316 /* Parse another optional declarator. */
5317 declarator = cp_parser_new_declarator_opt (parser);
5319 /* Create the representation of the declarator. */
5320 if (type)
5321 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5322 else if (code == INDIRECT_REF)
5323 declarator = make_pointer_declarator (cv_quals, declarator);
5324 else
5325 declarator = make_reference_declarator (cv_quals, declarator);
5327 return declarator;
5330 /* If the next token is a `[', there is a direct-new-declarator. */
5331 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5332 return cp_parser_direct_new_declarator (parser);
5334 return NULL;
5337 /* Parse a direct-new-declarator.
5339 direct-new-declarator:
5340 [ expression ]
5341 direct-new-declarator [constant-expression]
5345 static cp_declarator *
5346 cp_parser_direct_new_declarator (cp_parser* parser)
5348 cp_declarator *declarator = NULL;
5350 while (true)
5352 tree expression;
5354 /* Look for the opening `['. */
5355 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5356 /* The first expression is not required to be constant. */
5357 if (!declarator)
5359 expression = cp_parser_expression (parser, /*cast_p=*/false);
5360 /* The standard requires that the expression have integral
5361 type. DR 74 adds enumeration types. We believe that the
5362 real intent is that these expressions be handled like the
5363 expression in a `switch' condition, which also allows
5364 classes with a single conversion to integral or
5365 enumeration type. */
5366 if (!processing_template_decl)
5368 expression
5369 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5370 expression,
5371 /*complain=*/true);
5372 if (!expression)
5374 error ("expression in new-declarator must have integral "
5375 "or enumeration type");
5376 expression = error_mark_node;
5380 /* But all the other expressions must be. */
5381 else
5382 expression
5383 = cp_parser_constant_expression (parser,
5384 /*allow_non_constant=*/false,
5385 NULL);
5386 /* Look for the closing `]'. */
5387 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5389 /* Add this bound to the declarator. */
5390 declarator = make_array_declarator (declarator, expression);
5392 /* If the next token is not a `[', then there are no more
5393 bounds. */
5394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5395 break;
5398 return declarator;
5401 /* Parse a new-initializer.
5403 new-initializer:
5404 ( expression-list [opt] )
5406 Returns a representation of the expression-list. If there is no
5407 expression-list, VOID_ZERO_NODE is returned. */
5409 static tree
5410 cp_parser_new_initializer (cp_parser* parser)
5412 tree expression_list;
5414 expression_list = (cp_parser_parenthesized_expression_list
5415 (parser, false, /*cast_p=*/false,
5416 /*non_constant_p=*/NULL));
5417 if (!expression_list)
5418 expression_list = void_zero_node;
5420 return expression_list;
5423 /* Parse a delete-expression.
5425 delete-expression:
5426 :: [opt] delete cast-expression
5427 :: [opt] delete [ ] cast-expression
5429 Returns a representation of the expression. */
5431 static tree
5432 cp_parser_delete_expression (cp_parser* parser)
5434 bool global_scope_p;
5435 bool array_p;
5436 tree expression;
5438 /* Look for the optional `::' operator. */
5439 global_scope_p
5440 = (cp_parser_global_scope_opt (parser,
5441 /*current_scope_valid_p=*/false)
5442 != NULL_TREE);
5443 /* Look for the `delete' keyword. */
5444 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5445 /* See if the array syntax is in use. */
5446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5448 /* Consume the `[' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 /* Look for the `]' token. */
5451 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5452 /* Remember that this is the `[]' construct. */
5453 array_p = true;
5455 else
5456 array_p = false;
5458 /* Parse the cast-expression. */
5459 expression = cp_parser_simple_cast_expression (parser);
5461 /* A delete-expression may not appear in an integral constant
5462 expression. */
5463 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5464 return error_mark_node;
5466 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5469 /* Parse a cast-expression.
5471 cast-expression:
5472 unary-expression
5473 ( type-id ) cast-expression
5475 ADDRESS_P is true iff the unary-expression is appearing as the
5476 operand of the `&' operator. CAST_P is true if this expression is
5477 the target of a cast.
5479 Returns a representation of the expression. */
5481 static tree
5482 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5484 /* If it's a `(', then we might be looking at a cast. */
5485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5487 tree type = NULL_TREE;
5488 tree expr = NULL_TREE;
5489 bool compound_literal_p;
5490 const char *saved_message;
5492 /* There's no way to know yet whether or not this is a cast.
5493 For example, `(int (3))' is a unary-expression, while `(int)
5494 3' is a cast. So, we resort to parsing tentatively. */
5495 cp_parser_parse_tentatively (parser);
5496 /* Types may not be defined in a cast. */
5497 saved_message = parser->type_definition_forbidden_message;
5498 parser->type_definition_forbidden_message
5499 = "types may not be defined in casts";
5500 /* Consume the `('. */
5501 cp_lexer_consume_token (parser->lexer);
5502 /* A very tricky bit is that `(struct S) { 3 }' is a
5503 compound-literal (which we permit in C++ as an extension).
5504 But, that construct is not a cast-expression -- it is a
5505 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5506 is legal; if the compound-literal were a cast-expression,
5507 you'd need an extra set of parentheses.) But, if we parse
5508 the type-id, and it happens to be a class-specifier, then we
5509 will commit to the parse at that point, because we cannot
5510 undo the action that is done when creating a new class. So,
5511 then we cannot back up and do a postfix-expression.
5513 Therefore, we scan ahead to the closing `)', and check to see
5514 if the token after the `)' is a `{'. If so, we are not
5515 looking at a cast-expression.
5517 Save tokens so that we can put them back. */
5518 cp_lexer_save_tokens (parser->lexer);
5519 /* Skip tokens until the next token is a closing parenthesis.
5520 If we find the closing `)', and the next token is a `{', then
5521 we are looking at a compound-literal. */
5522 compound_literal_p
5523 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5524 /*consume_paren=*/true)
5525 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5526 /* Roll back the tokens we skipped. */
5527 cp_lexer_rollback_tokens (parser->lexer);
5528 /* If we were looking at a compound-literal, simulate an error
5529 so that the call to cp_parser_parse_definitely below will
5530 fail. */
5531 if (compound_literal_p)
5532 cp_parser_simulate_error (parser);
5533 else
5535 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5536 parser->in_type_id_in_expr_p = true;
5537 /* Look for the type-id. */
5538 type = cp_parser_type_id (parser);
5539 /* Look for the closing `)'. */
5540 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5541 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5544 /* Restore the saved message. */
5545 parser->type_definition_forbidden_message = saved_message;
5547 /* If ok so far, parse the dependent expression. We cannot be
5548 sure it is a cast. Consider `(T ())'. It is a parenthesized
5549 ctor of T, but looks like a cast to function returning T
5550 without a dependent expression. */
5551 if (!cp_parser_error_occurred (parser))
5552 expr = cp_parser_cast_expression (parser,
5553 /*address_p=*/false,
5554 /*cast_p=*/true);
5556 if (cp_parser_parse_definitely (parser))
5558 /* Warn about old-style casts, if so requested. */
5559 if (warn_old_style_cast
5560 && !in_system_header
5561 && !VOID_TYPE_P (type)
5562 && current_lang_name != lang_name_c)
5563 warning (0, "use of old-style cast");
5565 /* Only type conversions to integral or enumeration types
5566 can be used in constant-expressions. */
5567 if (parser->integral_constant_expression_p
5568 && !dependent_type_p (type)
5569 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5570 && (cp_parser_non_integral_constant_expression
5571 (parser,
5572 "a cast to a type other than an integral or "
5573 "enumeration type")))
5574 return error_mark_node;
5576 /* Perform the cast. */
5577 expr = build_c_cast (type, expr);
5578 return expr;
5582 /* If we get here, then it's not a cast, so it must be a
5583 unary-expression. */
5584 return cp_parser_unary_expression (parser, address_p, cast_p);
5587 /* Parse a binary expression of the general form:
5589 pm-expression:
5590 cast-expression
5591 pm-expression .* cast-expression
5592 pm-expression ->* cast-expression
5594 multiplicative-expression:
5595 pm-expression
5596 multiplicative-expression * pm-expression
5597 multiplicative-expression / pm-expression
5598 multiplicative-expression % pm-expression
5600 additive-expression:
5601 multiplicative-expression
5602 additive-expression + multiplicative-expression
5603 additive-expression - multiplicative-expression
5605 shift-expression:
5606 additive-expression
5607 shift-expression << additive-expression
5608 shift-expression >> additive-expression
5610 relational-expression:
5611 shift-expression
5612 relational-expression < shift-expression
5613 relational-expression > shift-expression
5614 relational-expression <= shift-expression
5615 relational-expression >= shift-expression
5617 GNU Extension:
5619 relational-expression:
5620 relational-expression <? shift-expression
5621 relational-expression >? shift-expression
5623 equality-expression:
5624 relational-expression
5625 equality-expression == relational-expression
5626 equality-expression != relational-expression
5628 and-expression:
5629 equality-expression
5630 and-expression & equality-expression
5632 exclusive-or-expression:
5633 and-expression
5634 exclusive-or-expression ^ and-expression
5636 inclusive-or-expression:
5637 exclusive-or-expression
5638 inclusive-or-expression | exclusive-or-expression
5640 logical-and-expression:
5641 inclusive-or-expression
5642 logical-and-expression && inclusive-or-expression
5644 logical-or-expression:
5645 logical-and-expression
5646 logical-or-expression || logical-and-expression
5648 All these are implemented with a single function like:
5650 binary-expression:
5651 simple-cast-expression
5652 binary-expression <token> binary-expression
5654 CAST_P is true if this expression is the target of a cast.
5656 The binops_by_token map is used to get the tree codes for each <token> type.
5657 binary-expressions are associated according to a precedence table. */
5659 #define TOKEN_PRECEDENCE(token) \
5660 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5661 ? PREC_NOT_OPERATOR \
5662 : binops_by_token[token->type].prec)
5664 static tree
5665 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5667 cp_parser_expression_stack stack;
5668 cp_parser_expression_stack_entry *sp = &stack[0];
5669 tree lhs, rhs;
5670 cp_token *token;
5671 enum tree_code tree_type;
5672 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5673 bool overloaded_p;
5675 /* Parse the first expression. */
5676 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5678 for (;;)
5680 /* Get an operator token. */
5681 token = cp_lexer_peek_token (parser->lexer);
5682 if (token->type == CPP_MIN || token->type == CPP_MAX)
5683 cp_parser_warn_min_max ();
5685 new_prec = TOKEN_PRECEDENCE (token);
5687 /* Popping an entry off the stack means we completed a subexpression:
5688 - either we found a token which is not an operator (`>' where it is not
5689 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5690 will happen repeatedly;
5691 - or, we found an operator which has lower priority. This is the case
5692 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5693 parsing `3 * 4'. */
5694 if (new_prec <= prec)
5696 if (sp == stack)
5697 break;
5698 else
5699 goto pop;
5702 get_rhs:
5703 tree_type = binops_by_token[token->type].tree_type;
5705 /* We used the operator token. */
5706 cp_lexer_consume_token (parser->lexer);
5708 /* Extract another operand. It may be the RHS of this expression
5709 or the LHS of a new, higher priority expression. */
5710 rhs = cp_parser_simple_cast_expression (parser);
5712 /* Get another operator token. Look up its precedence to avoid
5713 building a useless (immediately popped) stack entry for common
5714 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5715 token = cp_lexer_peek_token (parser->lexer);
5716 lookahead_prec = TOKEN_PRECEDENCE (token);
5717 if (lookahead_prec > new_prec)
5719 /* ... and prepare to parse the RHS of the new, higher priority
5720 expression. Since precedence levels on the stack are
5721 monotonically increasing, we do not have to care about
5722 stack overflows. */
5723 sp->prec = prec;
5724 sp->tree_type = tree_type;
5725 sp->lhs = lhs;
5726 sp++;
5727 lhs = rhs;
5728 prec = new_prec;
5729 new_prec = lookahead_prec;
5730 goto get_rhs;
5732 pop:
5733 /* If the stack is not empty, we have parsed into LHS the right side
5734 (`4' in the example above) of an expression we had suspended.
5735 We can use the information on the stack to recover the LHS (`3')
5736 from the stack together with the tree code (`MULT_EXPR'), and
5737 the precedence of the higher level subexpression
5738 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5739 which will be used to actually build the additive expression. */
5740 --sp;
5741 prec = sp->prec;
5742 tree_type = sp->tree_type;
5743 rhs = lhs;
5744 lhs = sp->lhs;
5747 overloaded_p = false;
5748 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5750 /* If the binary operator required the use of an overloaded operator,
5751 then this expression cannot be an integral constant-expression.
5752 An overloaded operator can be used even if both operands are
5753 otherwise permissible in an integral constant-expression if at
5754 least one of the operands is of enumeration type. */
5756 if (overloaded_p
5757 && (cp_parser_non_integral_constant_expression
5758 (parser, "calls to overloaded operators")))
5759 return error_mark_node;
5762 return lhs;
5766 /* Parse the `? expression : assignment-expression' part of a
5767 conditional-expression. The LOGICAL_OR_EXPR is the
5768 logical-or-expression that started the conditional-expression.
5769 Returns a representation of the entire conditional-expression.
5771 This routine is used by cp_parser_assignment_expression.
5773 ? expression : assignment-expression
5775 GNU Extensions:
5777 ? : assignment-expression */
5779 static tree
5780 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5782 tree expr;
5783 tree assignment_expr;
5785 /* Consume the `?' token. */
5786 cp_lexer_consume_token (parser->lexer);
5787 if (cp_parser_allow_gnu_extensions_p (parser)
5788 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5789 /* Implicit true clause. */
5790 expr = NULL_TREE;
5791 else
5792 /* Parse the expression. */
5793 expr = cp_parser_expression (parser, /*cast_p=*/false);
5795 /* The next token should be a `:'. */
5796 cp_parser_require (parser, CPP_COLON, "`:'");
5797 /* Parse the assignment-expression. */
5798 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5800 /* Build the conditional-expression. */
5801 return build_x_conditional_expr (logical_or_expr,
5802 expr,
5803 assignment_expr);
5806 /* Parse an assignment-expression.
5808 assignment-expression:
5809 conditional-expression
5810 logical-or-expression assignment-operator assignment_expression
5811 throw-expression
5813 CAST_P is true if this expression is the target of a cast.
5815 Returns a representation for the expression. */
5817 static tree
5818 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5820 tree expr;
5822 /* If the next token is the `throw' keyword, then we're looking at
5823 a throw-expression. */
5824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5825 expr = cp_parser_throw_expression (parser);
5826 /* Otherwise, it must be that we are looking at a
5827 logical-or-expression. */
5828 else
5830 /* Parse the binary expressions (logical-or-expression). */
5831 expr = cp_parser_binary_expression (parser, cast_p);
5832 /* If the next token is a `?' then we're actually looking at a
5833 conditional-expression. */
5834 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5835 return cp_parser_question_colon_clause (parser, expr);
5836 else
5838 enum tree_code assignment_operator;
5840 /* If it's an assignment-operator, we're using the second
5841 production. */
5842 assignment_operator
5843 = cp_parser_assignment_operator_opt (parser);
5844 if (assignment_operator != ERROR_MARK)
5846 tree rhs;
5848 /* Parse the right-hand side of the assignment. */
5849 rhs = cp_parser_assignment_expression (parser, cast_p);
5850 /* An assignment may not appear in a
5851 constant-expression. */
5852 if (cp_parser_non_integral_constant_expression (parser,
5853 "an assignment"))
5854 return error_mark_node;
5855 /* Build the assignment expression. */
5856 expr = build_x_modify_expr (expr,
5857 assignment_operator,
5858 rhs);
5863 return expr;
5866 /* Parse an (optional) assignment-operator.
5868 assignment-operator: one of
5869 = *= /= %= += -= >>= <<= &= ^= |=
5871 GNU Extension:
5873 assignment-operator: one of
5874 <?= >?=
5876 If the next token is an assignment operator, the corresponding tree
5877 code is returned, and the token is consumed. For example, for
5878 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5879 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5880 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5881 operator, ERROR_MARK is returned. */
5883 static enum tree_code
5884 cp_parser_assignment_operator_opt (cp_parser* parser)
5886 enum tree_code op;
5887 cp_token *token;
5889 /* Peek at the next toen. */
5890 token = cp_lexer_peek_token (parser->lexer);
5892 switch (token->type)
5894 case CPP_EQ:
5895 op = NOP_EXPR;
5896 break;
5898 case CPP_MULT_EQ:
5899 op = MULT_EXPR;
5900 break;
5902 case CPP_DIV_EQ:
5903 op = TRUNC_DIV_EXPR;
5904 break;
5906 case CPP_MOD_EQ:
5907 op = TRUNC_MOD_EXPR;
5908 break;
5910 case CPP_PLUS_EQ:
5911 op = PLUS_EXPR;
5912 break;
5914 case CPP_MINUS_EQ:
5915 op = MINUS_EXPR;
5916 break;
5918 case CPP_RSHIFT_EQ:
5919 op = RSHIFT_EXPR;
5920 break;
5922 case CPP_LSHIFT_EQ:
5923 op = LSHIFT_EXPR;
5924 break;
5926 case CPP_AND_EQ:
5927 op = BIT_AND_EXPR;
5928 break;
5930 case CPP_XOR_EQ:
5931 op = BIT_XOR_EXPR;
5932 break;
5934 case CPP_OR_EQ:
5935 op = BIT_IOR_EXPR;
5936 break;
5938 case CPP_MIN_EQ:
5939 op = MIN_EXPR;
5940 cp_parser_warn_min_max ();
5941 break;
5943 case CPP_MAX_EQ:
5944 op = MAX_EXPR;
5945 cp_parser_warn_min_max ();
5946 break;
5948 default:
5949 /* Nothing else is an assignment operator. */
5950 op = ERROR_MARK;
5953 /* If it was an assignment operator, consume it. */
5954 if (op != ERROR_MARK)
5955 cp_lexer_consume_token (parser->lexer);
5957 return op;
5960 /* Parse an expression.
5962 expression:
5963 assignment-expression
5964 expression , assignment-expression
5966 CAST_P is true if this expression is the target of a cast.
5968 Returns a representation of the expression. */
5970 static tree
5971 cp_parser_expression (cp_parser* parser, bool cast_p)
5973 tree expression = NULL_TREE;
5975 while (true)
5977 tree assignment_expression;
5979 /* Parse the next assignment-expression. */
5980 assignment_expression
5981 = cp_parser_assignment_expression (parser, cast_p);
5982 /* If this is the first assignment-expression, we can just
5983 save it away. */
5984 if (!expression)
5985 expression = assignment_expression;
5986 else
5987 expression = build_x_compound_expr (expression,
5988 assignment_expression);
5989 /* If the next token is not a comma, then we are done with the
5990 expression. */
5991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5992 break;
5993 /* Consume the `,'. */
5994 cp_lexer_consume_token (parser->lexer);
5995 /* A comma operator cannot appear in a constant-expression. */
5996 if (cp_parser_non_integral_constant_expression (parser,
5997 "a comma operator"))
5998 expression = error_mark_node;
6001 return expression;
6004 /* Parse a constant-expression.
6006 constant-expression:
6007 conditional-expression
6009 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6010 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6011 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6012 is false, NON_CONSTANT_P should be NULL. */
6014 static tree
6015 cp_parser_constant_expression (cp_parser* parser,
6016 bool allow_non_constant_p,
6017 bool *non_constant_p)
6019 bool saved_integral_constant_expression_p;
6020 bool saved_allow_non_integral_constant_expression_p;
6021 bool saved_non_integral_constant_expression_p;
6022 tree expression;
6024 /* It might seem that we could simply parse the
6025 conditional-expression, and then check to see if it were
6026 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6027 one that the compiler can figure out is constant, possibly after
6028 doing some simplifications or optimizations. The standard has a
6029 precise definition of constant-expression, and we must honor
6030 that, even though it is somewhat more restrictive.
6032 For example:
6034 int i[(2, 3)];
6036 is not a legal declaration, because `(2, 3)' is not a
6037 constant-expression. The `,' operator is forbidden in a
6038 constant-expression. However, GCC's constant-folding machinery
6039 will fold this operation to an INTEGER_CST for `3'. */
6041 /* Save the old settings. */
6042 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6043 saved_allow_non_integral_constant_expression_p
6044 = parser->allow_non_integral_constant_expression_p;
6045 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6046 /* We are now parsing a constant-expression. */
6047 parser->integral_constant_expression_p = true;
6048 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6049 parser->non_integral_constant_expression_p = false;
6050 /* Although the grammar says "conditional-expression", we parse an
6051 "assignment-expression", which also permits "throw-expression"
6052 and the use of assignment operators. In the case that
6053 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6054 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6055 actually essential that we look for an assignment-expression.
6056 For example, cp_parser_initializer_clauses uses this function to
6057 determine whether a particular assignment-expression is in fact
6058 constant. */
6059 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6060 /* Restore the old settings. */
6061 parser->integral_constant_expression_p
6062 = saved_integral_constant_expression_p;
6063 parser->allow_non_integral_constant_expression_p
6064 = saved_allow_non_integral_constant_expression_p;
6065 if (allow_non_constant_p)
6066 *non_constant_p = parser->non_integral_constant_expression_p;
6067 else if (parser->non_integral_constant_expression_p)
6068 expression = error_mark_node;
6069 parser->non_integral_constant_expression_p
6070 = saved_non_integral_constant_expression_p;
6072 return expression;
6075 /* Parse __builtin_offsetof.
6077 offsetof-expression:
6078 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6080 offsetof-member-designator:
6081 id-expression
6082 | offsetof-member-designator "." id-expression
6083 | offsetof-member-designator "[" expression "]"
6086 static tree
6087 cp_parser_builtin_offsetof (cp_parser *parser)
6089 int save_ice_p, save_non_ice_p;
6090 tree type, expr;
6091 cp_id_kind dummy;
6093 /* We're about to accept non-integral-constant things, but will
6094 definitely yield an integral constant expression. Save and
6095 restore these values around our local parsing. */
6096 save_ice_p = parser->integral_constant_expression_p;
6097 save_non_ice_p = parser->non_integral_constant_expression_p;
6099 /* Consume the "__builtin_offsetof" token. */
6100 cp_lexer_consume_token (parser->lexer);
6101 /* Consume the opening `('. */
6102 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6103 /* Parse the type-id. */
6104 type = cp_parser_type_id (parser);
6105 /* Look for the `,'. */
6106 cp_parser_require (parser, CPP_COMMA, "`,'");
6108 /* Build the (type *)null that begins the traditional offsetof macro. */
6109 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6111 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6112 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6113 true, &dummy);
6114 while (true)
6116 cp_token *token = cp_lexer_peek_token (parser->lexer);
6117 switch (token->type)
6119 case CPP_OPEN_SQUARE:
6120 /* offsetof-member-designator "[" expression "]" */
6121 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6122 break;
6124 case CPP_DOT:
6125 /* offsetof-member-designator "." identifier */
6126 cp_lexer_consume_token (parser->lexer);
6127 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6128 true, &dummy);
6129 break;
6131 case CPP_CLOSE_PAREN:
6132 /* Consume the ")" token. */
6133 cp_lexer_consume_token (parser->lexer);
6134 goto success;
6136 default:
6137 /* Error. We know the following require will fail, but
6138 that gives the proper error message. */
6139 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6140 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6141 expr = error_mark_node;
6142 goto failure;
6146 success:
6147 /* If we're processing a template, we can't finish the semantics yet.
6148 Otherwise we can fold the entire expression now. */
6149 if (processing_template_decl)
6150 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6151 else
6152 expr = finish_offsetof (expr);
6154 failure:
6155 parser->integral_constant_expression_p = save_ice_p;
6156 parser->non_integral_constant_expression_p = save_non_ice_p;
6158 return expr;
6161 /* Statements [gram.stmt.stmt] */
6163 /* Parse a statement.
6165 statement:
6166 labeled-statement
6167 expression-statement
6168 compound-statement
6169 selection-statement
6170 iteration-statement
6171 jump-statement
6172 declaration-statement
6173 try-block */
6175 static void
6176 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6178 tree statement;
6179 cp_token *token;
6180 location_t statement_location;
6182 restart:
6183 /* There is no statement yet. */
6184 statement = NULL_TREE;
6185 /* Peek at the next token. */
6186 token = cp_lexer_peek_token (parser->lexer);
6187 /* Remember the location of the first token in the statement. */
6188 statement_location = token->location;
6189 /* If this is a keyword, then that will often determine what kind of
6190 statement we have. */
6191 if (token->type == CPP_KEYWORD)
6193 enum rid keyword = token->keyword;
6195 switch (keyword)
6197 case RID_CASE:
6198 case RID_DEFAULT:
6199 /* Looks like a labeled-statement with a case label.
6200 Parse the label, and then use tail recursion to parse
6201 the statement. */
6202 cp_parser_label_for_labeled_statement (parser);
6203 goto restart;
6205 case RID_IF:
6206 case RID_SWITCH:
6207 statement = cp_parser_selection_statement (parser);
6208 break;
6210 case RID_WHILE:
6211 case RID_DO:
6212 case RID_FOR:
6213 statement = cp_parser_iteration_statement (parser);
6214 break;
6216 case RID_BREAK:
6217 case RID_CONTINUE:
6218 case RID_RETURN:
6219 case RID_GOTO:
6220 statement = cp_parser_jump_statement (parser);
6221 break;
6223 /* Objective-C++ exception-handling constructs. */
6224 case RID_AT_TRY:
6225 case RID_AT_CATCH:
6226 case RID_AT_FINALLY:
6227 case RID_AT_SYNCHRONIZED:
6228 case RID_AT_THROW:
6229 statement = cp_parser_objc_statement (parser);
6230 break;
6232 case RID_TRY:
6233 statement = cp_parser_try_block (parser);
6234 break;
6236 default:
6237 /* It might be a keyword like `int' that can start a
6238 declaration-statement. */
6239 break;
6242 else if (token->type == CPP_NAME)
6244 /* If the next token is a `:', then we are looking at a
6245 labeled-statement. */
6246 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6247 if (token->type == CPP_COLON)
6249 /* Looks like a labeled-statement with an ordinary label.
6250 Parse the label, and then use tail recursion to parse
6251 the statement. */
6252 cp_parser_label_for_labeled_statement (parser);
6253 goto restart;
6256 /* Anything that starts with a `{' must be a compound-statement. */
6257 else if (token->type == CPP_OPEN_BRACE)
6258 statement = cp_parser_compound_statement (parser, NULL, false);
6259 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6260 a statement all its own. */
6261 else if (token->type == CPP_PRAGMA)
6263 cp_lexer_handle_pragma (parser->lexer);
6264 return;
6266 else if (token->type == CPP_EOF)
6268 cp_parser_error (parser, "expected statement");
6269 return;
6272 /* Everything else must be a declaration-statement or an
6273 expression-statement. Try for the declaration-statement
6274 first, unless we are looking at a `;', in which case we know that
6275 we have an expression-statement. */
6276 if (!statement)
6278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6280 cp_parser_parse_tentatively (parser);
6281 /* Try to parse the declaration-statement. */
6282 cp_parser_declaration_statement (parser);
6283 /* If that worked, we're done. */
6284 if (cp_parser_parse_definitely (parser))
6285 return;
6287 /* Look for an expression-statement instead. */
6288 statement = cp_parser_expression_statement (parser, in_statement_expr);
6291 /* Set the line number for the statement. */
6292 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6293 SET_EXPR_LOCATION (statement, statement_location);
6296 /* Parse the label for a labeled-statement, i.e.
6298 identifier :
6299 case constant-expression :
6300 default :
6302 GNU Extension:
6303 case constant-expression ... constant-expression : statement
6305 When a label is parsed without errors, the label is added to the
6306 parse tree by the finish_* functions, so this function doesn't
6307 have to return the label. */
6309 static void
6310 cp_parser_label_for_labeled_statement (cp_parser* parser)
6312 cp_token *token;
6314 /* The next token should be an identifier. */
6315 token = cp_lexer_peek_token (parser->lexer);
6316 if (token->type != CPP_NAME
6317 && token->type != CPP_KEYWORD)
6319 cp_parser_error (parser, "expected labeled-statement");
6320 return;
6323 switch (token->keyword)
6325 case RID_CASE:
6327 tree expr, expr_hi;
6328 cp_token *ellipsis;
6330 /* Consume the `case' token. */
6331 cp_lexer_consume_token (parser->lexer);
6332 /* Parse the constant-expression. */
6333 expr = cp_parser_constant_expression (parser,
6334 /*allow_non_constant_p=*/false,
6335 NULL);
6337 ellipsis = cp_lexer_peek_token (parser->lexer);
6338 if (ellipsis->type == CPP_ELLIPSIS)
6340 /* Consume the `...' token. */
6341 cp_lexer_consume_token (parser->lexer);
6342 expr_hi =
6343 cp_parser_constant_expression (parser,
6344 /*allow_non_constant_p=*/false,
6345 NULL);
6346 /* We don't need to emit warnings here, as the common code
6347 will do this for us. */
6349 else
6350 expr_hi = NULL_TREE;
6352 if (!parser->in_switch_statement_p)
6353 error ("case label %qE not within a switch statement", expr);
6354 else
6355 finish_case_label (expr, expr_hi);
6357 break;
6359 case RID_DEFAULT:
6360 /* Consume the `default' token. */
6361 cp_lexer_consume_token (parser->lexer);
6362 if (!parser->in_switch_statement_p)
6363 error ("case label not within a switch statement");
6364 else
6365 finish_case_label (NULL_TREE, NULL_TREE);
6366 break;
6368 default:
6369 /* Anything else must be an ordinary label. */
6370 finish_label_stmt (cp_parser_identifier (parser));
6371 break;
6374 /* Require the `:' token. */
6375 cp_parser_require (parser, CPP_COLON, "`:'");
6378 /* Parse an expression-statement.
6380 expression-statement:
6381 expression [opt] ;
6383 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6384 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6385 indicates whether this expression-statement is part of an
6386 expression statement. */
6388 static tree
6389 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6391 tree statement = NULL_TREE;
6393 /* If the next token is a ';', then there is no expression
6394 statement. */
6395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6396 statement = cp_parser_expression (parser, /*cast_p=*/false);
6398 /* Consume the final `;'. */
6399 cp_parser_consume_semicolon_at_end_of_statement (parser);
6401 if (in_statement_expr
6402 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6403 /* This is the final expression statement of a statement
6404 expression. */
6405 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6406 else if (statement)
6407 statement = finish_expr_stmt (statement);
6408 else
6409 finish_stmt ();
6411 return statement;
6414 /* Parse a compound-statement.
6416 compound-statement:
6417 { statement-seq [opt] }
6419 Returns a tree representing the statement. */
6421 static tree
6422 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6423 bool in_try)
6425 tree compound_stmt;
6427 /* Consume the `{'. */
6428 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6429 return error_mark_node;
6430 /* Begin the compound-statement. */
6431 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6432 /* Parse an (optional) statement-seq. */
6433 cp_parser_statement_seq_opt (parser, in_statement_expr);
6434 /* Finish the compound-statement. */
6435 finish_compound_stmt (compound_stmt);
6436 /* Consume the `}'. */
6437 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6439 return compound_stmt;
6442 /* Parse an (optional) statement-seq.
6444 statement-seq:
6445 statement
6446 statement-seq [opt] statement */
6448 static void
6449 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6451 /* Scan statements until there aren't any more. */
6452 while (true)
6454 /* If we're looking at a `}', then we've run out of statements. */
6455 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6456 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6457 break;
6459 /* Parse the statement. */
6460 cp_parser_statement (parser, in_statement_expr);
6464 /* Parse a selection-statement.
6466 selection-statement:
6467 if ( condition ) statement
6468 if ( condition ) statement else statement
6469 switch ( condition ) statement
6471 Returns the new IF_STMT or SWITCH_STMT. */
6473 static tree
6474 cp_parser_selection_statement (cp_parser* parser)
6476 cp_token *token;
6477 enum rid keyword;
6479 /* Peek at the next token. */
6480 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6482 /* See what kind of keyword it is. */
6483 keyword = token->keyword;
6484 switch (keyword)
6486 case RID_IF:
6487 case RID_SWITCH:
6489 tree statement;
6490 tree condition;
6492 /* Look for the `('. */
6493 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6495 cp_parser_skip_to_end_of_statement (parser);
6496 return error_mark_node;
6499 /* Begin the selection-statement. */
6500 if (keyword == RID_IF)
6501 statement = begin_if_stmt ();
6502 else
6503 statement = begin_switch_stmt ();
6505 /* Parse the condition. */
6506 condition = cp_parser_condition (parser);
6507 /* Look for the `)'. */
6508 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6509 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6510 /*consume_paren=*/true);
6512 if (keyword == RID_IF)
6514 /* Add the condition. */
6515 finish_if_stmt_cond (condition, statement);
6517 /* Parse the then-clause. */
6518 cp_parser_implicitly_scoped_statement (parser);
6519 finish_then_clause (statement);
6521 /* If the next token is `else', parse the else-clause. */
6522 if (cp_lexer_next_token_is_keyword (parser->lexer,
6523 RID_ELSE))
6525 /* Consume the `else' keyword. */
6526 cp_lexer_consume_token (parser->lexer);
6527 begin_else_clause (statement);
6528 /* Parse the else-clause. */
6529 cp_parser_implicitly_scoped_statement (parser);
6530 finish_else_clause (statement);
6533 /* Now we're all done with the if-statement. */
6534 finish_if_stmt (statement);
6536 else
6538 bool in_switch_statement_p;
6540 /* Add the condition. */
6541 finish_switch_cond (condition, statement);
6543 /* Parse the body of the switch-statement. */
6544 in_switch_statement_p = parser->in_switch_statement_p;
6545 parser->in_switch_statement_p = true;
6546 cp_parser_implicitly_scoped_statement (parser);
6547 parser->in_switch_statement_p = in_switch_statement_p;
6549 /* Now we're all done with the switch-statement. */
6550 finish_switch_stmt (statement);
6553 return statement;
6555 break;
6557 default:
6558 cp_parser_error (parser, "expected selection-statement");
6559 return error_mark_node;
6563 /* Parse a condition.
6565 condition:
6566 expression
6567 type-specifier-seq declarator = assignment-expression
6569 GNU Extension:
6571 condition:
6572 type-specifier-seq declarator asm-specification [opt]
6573 attributes [opt] = assignment-expression
6575 Returns the expression that should be tested. */
6577 static tree
6578 cp_parser_condition (cp_parser* parser)
6580 cp_decl_specifier_seq type_specifiers;
6581 const char *saved_message;
6583 /* Try the declaration first. */
6584 cp_parser_parse_tentatively (parser);
6585 /* New types are not allowed in the type-specifier-seq for a
6586 condition. */
6587 saved_message = parser->type_definition_forbidden_message;
6588 parser->type_definition_forbidden_message
6589 = "types may not be defined in conditions";
6590 /* Parse the type-specifier-seq. */
6591 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6592 &type_specifiers);
6593 /* Restore the saved message. */
6594 parser->type_definition_forbidden_message = saved_message;
6595 /* If all is well, we might be looking at a declaration. */
6596 if (!cp_parser_error_occurred (parser))
6598 tree decl;
6599 tree asm_specification;
6600 tree attributes;
6601 cp_declarator *declarator;
6602 tree initializer = NULL_TREE;
6604 /* Parse the declarator. */
6605 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6606 /*ctor_dtor_or_conv_p=*/NULL,
6607 /*parenthesized_p=*/NULL,
6608 /*member_p=*/false);
6609 /* Parse the attributes. */
6610 attributes = cp_parser_attributes_opt (parser);
6611 /* Parse the asm-specification. */
6612 asm_specification = cp_parser_asm_specification_opt (parser);
6613 /* If the next token is not an `=', then we might still be
6614 looking at an expression. For example:
6616 if (A(a).x)
6618 looks like a decl-specifier-seq and a declarator -- but then
6619 there is no `=', so this is an expression. */
6620 cp_parser_require (parser, CPP_EQ, "`='");
6621 /* If we did see an `=', then we are looking at a declaration
6622 for sure. */
6623 if (cp_parser_parse_definitely (parser))
6625 tree pushed_scope;
6626 bool non_constant_p;
6628 /* Create the declaration. */
6629 decl = start_decl (declarator, &type_specifiers,
6630 /*initialized_p=*/true,
6631 attributes, /*prefix_attributes=*/NULL_TREE,
6632 &pushed_scope);
6633 /* Parse the assignment-expression. */
6634 initializer
6635 = cp_parser_constant_expression (parser,
6636 /*allow_non_constant_p=*/true,
6637 &non_constant_p);
6638 if (!non_constant_p)
6639 initializer = fold_non_dependent_expr (initializer);
6641 /* Process the initializer. */
6642 cp_finish_decl (decl,
6643 initializer, !non_constant_p,
6644 asm_specification,
6645 LOOKUP_ONLYCONVERTING);
6647 if (pushed_scope)
6648 pop_scope (pushed_scope);
6650 return convert_from_reference (decl);
6653 /* If we didn't even get past the declarator successfully, we are
6654 definitely not looking at a declaration. */
6655 else
6656 cp_parser_abort_tentative_parse (parser);
6658 /* Otherwise, we are looking at an expression. */
6659 return cp_parser_expression (parser, /*cast_p=*/false);
6662 /* Parse an iteration-statement.
6664 iteration-statement:
6665 while ( condition ) statement
6666 do statement while ( expression ) ;
6667 for ( for-init-statement condition [opt] ; expression [opt] )
6668 statement
6670 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6672 static tree
6673 cp_parser_iteration_statement (cp_parser* parser)
6675 cp_token *token;
6676 enum rid keyword;
6677 tree statement;
6678 bool in_iteration_statement_p;
6681 /* Peek at the next token. */
6682 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6683 if (!token)
6684 return error_mark_node;
6686 /* Remember whether or not we are already within an iteration
6687 statement. */
6688 in_iteration_statement_p = parser->in_iteration_statement_p;
6690 /* See what kind of keyword it is. */
6691 keyword = token->keyword;
6692 switch (keyword)
6694 case RID_WHILE:
6696 tree condition;
6698 /* Begin the while-statement. */
6699 statement = begin_while_stmt ();
6700 /* Look for the `('. */
6701 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6702 /* Parse the condition. */
6703 condition = cp_parser_condition (parser);
6704 finish_while_stmt_cond (condition, statement);
6705 /* Look for the `)'. */
6706 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6707 /* Parse the dependent statement. */
6708 parser->in_iteration_statement_p = true;
6709 cp_parser_already_scoped_statement (parser);
6710 parser->in_iteration_statement_p = in_iteration_statement_p;
6711 /* We're done with the while-statement. */
6712 finish_while_stmt (statement);
6714 break;
6716 case RID_DO:
6718 tree expression;
6720 /* Begin the do-statement. */
6721 statement = begin_do_stmt ();
6722 /* Parse the body of the do-statement. */
6723 parser->in_iteration_statement_p = true;
6724 cp_parser_implicitly_scoped_statement (parser);
6725 parser->in_iteration_statement_p = in_iteration_statement_p;
6726 finish_do_body (statement);
6727 /* Look for the `while' keyword. */
6728 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6729 /* Look for the `('. */
6730 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6731 /* Parse the expression. */
6732 expression = cp_parser_expression (parser, /*cast_p=*/false);
6733 /* We're done with the do-statement. */
6734 finish_do_stmt (expression, statement);
6735 /* Look for the `)'. */
6736 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6737 /* Look for the `;'. */
6738 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6740 break;
6742 case RID_FOR:
6744 tree condition = NULL_TREE;
6745 tree expression = NULL_TREE;
6747 /* Begin the for-statement. */
6748 statement = begin_for_stmt ();
6749 /* Look for the `('. */
6750 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6751 /* Parse the initialization. */
6752 cp_parser_for_init_statement (parser);
6753 finish_for_init_stmt (statement);
6755 /* If there's a condition, process it. */
6756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6757 condition = cp_parser_condition (parser);
6758 finish_for_cond (condition, statement);
6759 /* Look for the `;'. */
6760 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6762 /* If there's an expression, process it. */
6763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6764 expression = cp_parser_expression (parser, /*cast_p=*/false);
6765 finish_for_expr (expression, statement);
6766 /* Look for the `)'. */
6767 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6769 /* Parse the body of the for-statement. */
6770 parser->in_iteration_statement_p = true;
6771 cp_parser_already_scoped_statement (parser);
6772 parser->in_iteration_statement_p = in_iteration_statement_p;
6774 /* We're done with the for-statement. */
6775 finish_for_stmt (statement);
6777 break;
6779 default:
6780 cp_parser_error (parser, "expected iteration-statement");
6781 statement = error_mark_node;
6782 break;
6785 return statement;
6788 /* Parse a for-init-statement.
6790 for-init-statement:
6791 expression-statement
6792 simple-declaration */
6794 static void
6795 cp_parser_for_init_statement (cp_parser* parser)
6797 /* If the next token is a `;', then we have an empty
6798 expression-statement. Grammatically, this is also a
6799 simple-declaration, but an invalid one, because it does not
6800 declare anything. Therefore, if we did not handle this case
6801 specially, we would issue an error message about an invalid
6802 declaration. */
6803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6805 /* We're going to speculatively look for a declaration, falling back
6806 to an expression, if necessary. */
6807 cp_parser_parse_tentatively (parser);
6808 /* Parse the declaration. */
6809 cp_parser_simple_declaration (parser,
6810 /*function_definition_allowed_p=*/false);
6811 /* If the tentative parse failed, then we shall need to look for an
6812 expression-statement. */
6813 if (cp_parser_parse_definitely (parser))
6814 return;
6817 cp_parser_expression_statement (parser, false);
6820 /* Parse a jump-statement.
6822 jump-statement:
6823 break ;
6824 continue ;
6825 return expression [opt] ;
6826 goto identifier ;
6828 GNU extension:
6830 jump-statement:
6831 goto * expression ;
6833 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6835 static tree
6836 cp_parser_jump_statement (cp_parser* parser)
6838 tree statement = error_mark_node;
6839 cp_token *token;
6840 enum rid keyword;
6842 /* Peek at the next token. */
6843 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6844 if (!token)
6845 return error_mark_node;
6847 /* See what kind of keyword it is. */
6848 keyword = token->keyword;
6849 switch (keyword)
6851 case RID_BREAK:
6852 if (!parser->in_switch_statement_p
6853 && !parser->in_iteration_statement_p)
6855 error ("break statement not within loop or switch");
6856 statement = error_mark_node;
6858 else
6859 statement = finish_break_stmt ();
6860 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6861 break;
6863 case RID_CONTINUE:
6864 if (!parser->in_iteration_statement_p)
6866 error ("continue statement not within a loop");
6867 statement = error_mark_node;
6869 else
6870 statement = finish_continue_stmt ();
6871 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6872 break;
6874 case RID_RETURN:
6876 tree expr;
6878 /* If the next token is a `;', then there is no
6879 expression. */
6880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6881 expr = cp_parser_expression (parser, /*cast_p=*/false);
6882 else
6883 expr = NULL_TREE;
6884 /* Build the return-statement. */
6885 statement = finish_return_stmt (expr);
6886 /* Look for the final `;'. */
6887 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6889 break;
6891 case RID_GOTO:
6892 /* Create the goto-statement. */
6893 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6895 /* Issue a warning about this use of a GNU extension. */
6896 if (pedantic)
6897 pedwarn ("ISO C++ forbids computed gotos");
6898 /* Consume the '*' token. */
6899 cp_lexer_consume_token (parser->lexer);
6900 /* Parse the dependent expression. */
6901 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6903 else
6904 finish_goto_stmt (cp_parser_identifier (parser));
6905 /* Look for the final `;'. */
6906 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6907 break;
6909 default:
6910 cp_parser_error (parser, "expected jump-statement");
6911 break;
6914 return statement;
6917 /* Parse a declaration-statement.
6919 declaration-statement:
6920 block-declaration */
6922 static void
6923 cp_parser_declaration_statement (cp_parser* parser)
6925 void *p;
6927 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6928 p = obstack_alloc (&declarator_obstack, 0);
6930 /* Parse the block-declaration. */
6931 cp_parser_block_declaration (parser, /*statement_p=*/true);
6933 /* Free any declarators allocated. */
6934 obstack_free (&declarator_obstack, p);
6936 /* Finish off the statement. */
6937 finish_stmt ();
6940 /* Some dependent statements (like `if (cond) statement'), are
6941 implicitly in their own scope. In other words, if the statement is
6942 a single statement (as opposed to a compound-statement), it is
6943 none-the-less treated as if it were enclosed in braces. Any
6944 declarations appearing in the dependent statement are out of scope
6945 after control passes that point. This function parses a statement,
6946 but ensures that is in its own scope, even if it is not a
6947 compound-statement.
6949 Returns the new statement. */
6951 static tree
6952 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6954 tree statement;
6956 /* If the token is not a `{', then we must take special action. */
6957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6959 /* Create a compound-statement. */
6960 statement = begin_compound_stmt (0);
6961 /* Parse the dependent-statement. */
6962 cp_parser_statement (parser, false);
6963 /* Finish the dummy compound-statement. */
6964 finish_compound_stmt (statement);
6966 /* Otherwise, we simply parse the statement directly. */
6967 else
6968 statement = cp_parser_compound_statement (parser, NULL, false);
6970 /* Return the statement. */
6971 return statement;
6974 /* For some dependent statements (like `while (cond) statement'), we
6975 have already created a scope. Therefore, even if the dependent
6976 statement is a compound-statement, we do not want to create another
6977 scope. */
6979 static void
6980 cp_parser_already_scoped_statement (cp_parser* parser)
6982 /* If the token is a `{', then we must take special action. */
6983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6984 cp_parser_statement (parser, false);
6985 else
6987 /* Avoid calling cp_parser_compound_statement, so that we
6988 don't create a new scope. Do everything else by hand. */
6989 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6990 cp_parser_statement_seq_opt (parser, false);
6991 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6995 /* Declarations [gram.dcl.dcl] */
6997 /* Parse an optional declaration-sequence.
6999 declaration-seq:
7000 declaration
7001 declaration-seq declaration */
7003 static void
7004 cp_parser_declaration_seq_opt (cp_parser* parser)
7006 while (true)
7008 cp_token *token;
7010 token = cp_lexer_peek_token (parser->lexer);
7012 if (token->type == CPP_CLOSE_BRACE
7013 || token->type == CPP_EOF)
7014 break;
7016 if (token->type == CPP_SEMICOLON)
7018 /* A declaration consisting of a single semicolon is
7019 invalid. Allow it unless we're being pedantic. */
7020 cp_lexer_consume_token (parser->lexer);
7021 if (pedantic && !in_system_header)
7022 pedwarn ("extra %<;%>");
7023 continue;
7026 /* If we're entering or exiting a region that's implicitly
7027 extern "C", modify the lang context appropriately. */
7028 if (!parser->implicit_extern_c && token->implicit_extern_c)
7030 push_lang_context (lang_name_c);
7031 parser->implicit_extern_c = true;
7033 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7035 pop_lang_context ();
7036 parser->implicit_extern_c = false;
7039 if (token->type == CPP_PRAGMA)
7041 /* A top-level declaration can consist solely of a #pragma.
7042 A nested declaration cannot, so this is done here and not
7043 in cp_parser_declaration. (A #pragma at block scope is
7044 handled in cp_parser_statement.) */
7045 cp_lexer_handle_pragma (parser->lexer);
7046 continue;
7049 /* Parse the declaration itself. */
7050 cp_parser_declaration (parser);
7054 /* Parse a declaration.
7056 declaration:
7057 block-declaration
7058 function-definition
7059 template-declaration
7060 explicit-instantiation
7061 explicit-specialization
7062 linkage-specification
7063 namespace-definition
7065 GNU extension:
7067 declaration:
7068 __extension__ declaration */
7070 static void
7071 cp_parser_declaration (cp_parser* parser)
7073 cp_token token1;
7074 cp_token token2;
7075 int saved_pedantic;
7076 void *p;
7078 /* Check for the `__extension__' keyword. */
7079 if (cp_parser_extension_opt (parser, &saved_pedantic))
7081 /* Parse the qualified declaration. */
7082 cp_parser_declaration (parser);
7083 /* Restore the PEDANTIC flag. */
7084 pedantic = saved_pedantic;
7086 return;
7089 /* Try to figure out what kind of declaration is present. */
7090 token1 = *cp_lexer_peek_token (parser->lexer);
7092 if (token1.type != CPP_EOF)
7093 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7094 else
7095 token2.type = token2.keyword = RID_MAX;
7097 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7098 p = obstack_alloc (&declarator_obstack, 0);
7100 /* If the next token is `extern' and the following token is a string
7101 literal, then we have a linkage specification. */
7102 if (token1.keyword == RID_EXTERN
7103 && cp_parser_is_string_literal (&token2))
7104 cp_parser_linkage_specification (parser);
7105 /* If the next token is `template', then we have either a template
7106 declaration, an explicit instantiation, or an explicit
7107 specialization. */
7108 else if (token1.keyword == RID_TEMPLATE)
7110 /* `template <>' indicates a template specialization. */
7111 if (token2.type == CPP_LESS
7112 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7113 cp_parser_explicit_specialization (parser);
7114 /* `template <' indicates a template declaration. */
7115 else if (token2.type == CPP_LESS)
7116 cp_parser_template_declaration (parser, /*member_p=*/false);
7117 /* Anything else must be an explicit instantiation. */
7118 else
7119 cp_parser_explicit_instantiation (parser);
7121 /* If the next token is `export', then we have a template
7122 declaration. */
7123 else if (token1.keyword == RID_EXPORT)
7124 cp_parser_template_declaration (parser, /*member_p=*/false);
7125 /* If the next token is `extern', 'static' or 'inline' and the one
7126 after that is `template', we have a GNU extended explicit
7127 instantiation directive. */
7128 else if (cp_parser_allow_gnu_extensions_p (parser)
7129 && (token1.keyword == RID_EXTERN
7130 || token1.keyword == RID_STATIC
7131 || token1.keyword == RID_INLINE)
7132 && token2.keyword == RID_TEMPLATE)
7133 cp_parser_explicit_instantiation (parser);
7134 /* If the next token is `namespace', check for a named or unnamed
7135 namespace definition. */
7136 else if (token1.keyword == RID_NAMESPACE
7137 && (/* A named namespace definition. */
7138 (token2.type == CPP_NAME
7139 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7140 == CPP_OPEN_BRACE))
7141 /* An unnamed namespace definition. */
7142 || token2.type == CPP_OPEN_BRACE))
7143 cp_parser_namespace_definition (parser);
7144 /* Objective-C++ declaration/definition. */
7145 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7146 cp_parser_objc_declaration (parser);
7147 /* We must have either a block declaration or a function
7148 definition. */
7149 else
7150 /* Try to parse a block-declaration, or a function-definition. */
7151 cp_parser_block_declaration (parser, /*statement_p=*/false);
7153 /* Free any declarators allocated. */
7154 obstack_free (&declarator_obstack, p);
7157 /* Parse a block-declaration.
7159 block-declaration:
7160 simple-declaration
7161 asm-definition
7162 namespace-alias-definition
7163 using-declaration
7164 using-directive
7166 GNU Extension:
7168 block-declaration:
7169 __extension__ block-declaration
7170 label-declaration
7172 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7173 part of a declaration-statement. */
7175 static void
7176 cp_parser_block_declaration (cp_parser *parser,
7177 bool statement_p)
7179 cp_token *token1;
7180 int saved_pedantic;
7182 /* Check for the `__extension__' keyword. */
7183 if (cp_parser_extension_opt (parser, &saved_pedantic))
7185 /* Parse the qualified declaration. */
7186 cp_parser_block_declaration (parser, statement_p);
7187 /* Restore the PEDANTIC flag. */
7188 pedantic = saved_pedantic;
7190 return;
7193 /* Peek at the next token to figure out which kind of declaration is
7194 present. */
7195 token1 = cp_lexer_peek_token (parser->lexer);
7197 /* If the next keyword is `asm', we have an asm-definition. */
7198 if (token1->keyword == RID_ASM)
7200 if (statement_p)
7201 cp_parser_commit_to_tentative_parse (parser);
7202 cp_parser_asm_definition (parser);
7204 /* If the next keyword is `namespace', we have a
7205 namespace-alias-definition. */
7206 else if (token1->keyword == RID_NAMESPACE)
7207 cp_parser_namespace_alias_definition (parser);
7208 /* If the next keyword is `using', we have either a
7209 using-declaration or a using-directive. */
7210 else if (token1->keyword == RID_USING)
7212 cp_token *token2;
7214 if (statement_p)
7215 cp_parser_commit_to_tentative_parse (parser);
7216 /* If the token after `using' is `namespace', then we have a
7217 using-directive. */
7218 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7219 if (token2->keyword == RID_NAMESPACE)
7220 cp_parser_using_directive (parser);
7221 /* Otherwise, it's a using-declaration. */
7222 else
7223 cp_parser_using_declaration (parser,
7224 /*access_declaration_p=*/false);
7226 /* If the next keyword is `__label__' we have a label declaration. */
7227 else if (token1->keyword == RID_LABEL)
7229 if (statement_p)
7230 cp_parser_commit_to_tentative_parse (parser);
7231 cp_parser_label_declaration (parser);
7233 /* Anything else must be a simple-declaration. */
7234 else
7235 cp_parser_simple_declaration (parser, !statement_p);
7238 /* Parse a simple-declaration.
7240 simple-declaration:
7241 decl-specifier-seq [opt] init-declarator-list [opt] ;
7243 init-declarator-list:
7244 init-declarator
7245 init-declarator-list , init-declarator
7247 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7248 function-definition as a simple-declaration. */
7250 static void
7251 cp_parser_simple_declaration (cp_parser* parser,
7252 bool function_definition_allowed_p)
7254 cp_decl_specifier_seq decl_specifiers;
7255 int declares_class_or_enum;
7256 bool saw_declarator;
7258 /* Defer access checks until we know what is being declared; the
7259 checks for names appearing in the decl-specifier-seq should be
7260 done as if we were in the scope of the thing being declared. */
7261 push_deferring_access_checks (dk_deferred);
7263 /* Parse the decl-specifier-seq. We have to keep track of whether
7264 or not the decl-specifier-seq declares a named class or
7265 enumeration type, since that is the only case in which the
7266 init-declarator-list is allowed to be empty.
7268 [dcl.dcl]
7270 In a simple-declaration, the optional init-declarator-list can be
7271 omitted only when declaring a class or enumeration, that is when
7272 the decl-specifier-seq contains either a class-specifier, an
7273 elaborated-type-specifier, or an enum-specifier. */
7274 cp_parser_decl_specifier_seq (parser,
7275 CP_PARSER_FLAGS_OPTIONAL,
7276 &decl_specifiers,
7277 &declares_class_or_enum);
7278 /* We no longer need to defer access checks. */
7279 stop_deferring_access_checks ();
7281 /* In a block scope, a valid declaration must always have a
7282 decl-specifier-seq. By not trying to parse declarators, we can
7283 resolve the declaration/expression ambiguity more quickly. */
7284 if (!function_definition_allowed_p
7285 && !decl_specifiers.any_specifiers_p)
7287 cp_parser_error (parser, "expected declaration");
7288 goto done;
7291 /* If the next two tokens are both identifiers, the code is
7292 erroneous. The usual cause of this situation is code like:
7294 T t;
7296 where "T" should name a type -- but does not. */
7297 if (!decl_specifiers.type
7298 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7300 /* If parsing tentatively, we should commit; we really are
7301 looking at a declaration. */
7302 cp_parser_commit_to_tentative_parse (parser);
7303 /* Give up. */
7304 goto done;
7307 /* If we have seen at least one decl-specifier, and the next token
7308 is not a parenthesis, then we must be looking at a declaration.
7309 (After "int (" we might be looking at a functional cast.) */
7310 if (decl_specifiers.any_specifiers_p
7311 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7312 cp_parser_commit_to_tentative_parse (parser);
7314 /* Keep going until we hit the `;' at the end of the simple
7315 declaration. */
7316 saw_declarator = false;
7317 while (cp_lexer_next_token_is_not (parser->lexer,
7318 CPP_SEMICOLON))
7320 cp_token *token;
7321 bool function_definition_p;
7322 tree decl;
7324 if (saw_declarator)
7326 /* If we are processing next declarator, coma is expected */
7327 token = cp_lexer_peek_token (parser->lexer);
7328 gcc_assert (token->type == CPP_COMMA);
7329 cp_lexer_consume_token (parser->lexer);
7331 else
7332 saw_declarator = true;
7334 /* Parse the init-declarator. */
7335 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7336 /*checks=*/NULL_TREE,
7337 function_definition_allowed_p,
7338 /*member_p=*/false,
7339 declares_class_or_enum,
7340 &function_definition_p);
7341 /* If an error occurred while parsing tentatively, exit quickly.
7342 (That usually happens when in the body of a function; each
7343 statement is treated as a declaration-statement until proven
7344 otherwise.) */
7345 if (cp_parser_error_occurred (parser))
7346 goto done;
7347 /* Handle function definitions specially. */
7348 if (function_definition_p)
7350 /* If the next token is a `,', then we are probably
7351 processing something like:
7353 void f() {}, *p;
7355 which is erroneous. */
7356 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7357 error ("mixing declarations and function-definitions is forbidden");
7358 /* Otherwise, we're done with the list of declarators. */
7359 else
7361 pop_deferring_access_checks ();
7362 return;
7365 /* The next token should be either a `,' or a `;'. */
7366 token = cp_lexer_peek_token (parser->lexer);
7367 /* If it's a `,', there are more declarators to come. */
7368 if (token->type == CPP_COMMA)
7369 /* will be consumed next time around */;
7370 /* If it's a `;', we are done. */
7371 else if (token->type == CPP_SEMICOLON)
7372 break;
7373 /* Anything else is an error. */
7374 else
7376 /* If we have already issued an error message we don't need
7377 to issue another one. */
7378 if (decl != error_mark_node
7379 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7380 cp_parser_error (parser, "expected %<,%> or %<;%>");
7381 /* Skip tokens until we reach the end of the statement. */
7382 cp_parser_skip_to_end_of_statement (parser);
7383 /* If the next token is now a `;', consume it. */
7384 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7385 cp_lexer_consume_token (parser->lexer);
7386 goto done;
7388 /* After the first time around, a function-definition is not
7389 allowed -- even if it was OK at first. For example:
7391 int i, f() {}
7393 is not valid. */
7394 function_definition_allowed_p = false;
7397 /* Issue an error message if no declarators are present, and the
7398 decl-specifier-seq does not itself declare a class or
7399 enumeration. */
7400 if (!saw_declarator)
7402 if (cp_parser_declares_only_class_p (parser))
7403 shadow_tag (&decl_specifiers);
7404 /* Perform any deferred access checks. */
7405 perform_deferred_access_checks ();
7408 /* Consume the `;'. */
7409 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7411 done:
7412 pop_deferring_access_checks ();
7415 /* Parse a decl-specifier-seq.
7417 decl-specifier-seq:
7418 decl-specifier-seq [opt] decl-specifier
7420 decl-specifier:
7421 storage-class-specifier
7422 type-specifier
7423 function-specifier
7424 friend
7425 typedef
7427 GNU Extension:
7429 decl-specifier:
7430 attributes
7432 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7434 The parser flags FLAGS is used to control type-specifier parsing.
7436 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7437 flags:
7439 1: one of the decl-specifiers is an elaborated-type-specifier
7440 (i.e., a type declaration)
7441 2: one of the decl-specifiers is an enum-specifier or a
7442 class-specifier (i.e., a type definition)
7446 static void
7447 cp_parser_decl_specifier_seq (cp_parser* parser,
7448 cp_parser_flags flags,
7449 cp_decl_specifier_seq *decl_specs,
7450 int* declares_class_or_enum)
7452 bool constructor_possible_p = !parser->in_declarator_p;
7454 /* Clear DECL_SPECS. */
7455 clear_decl_specs (decl_specs);
7457 /* Assume no class or enumeration type is declared. */
7458 *declares_class_or_enum = 0;
7460 /* Keep reading specifiers until there are no more to read. */
7461 while (true)
7463 bool constructor_p;
7464 bool found_decl_spec;
7465 cp_token *token;
7467 /* Peek at the next token. */
7468 token = cp_lexer_peek_token (parser->lexer);
7469 /* Handle attributes. */
7470 if (token->keyword == RID_ATTRIBUTE)
7472 /* Parse the attributes. */
7473 decl_specs->attributes
7474 = chainon (decl_specs->attributes,
7475 cp_parser_attributes_opt (parser));
7476 continue;
7478 /* Assume we will find a decl-specifier keyword. */
7479 found_decl_spec = true;
7480 /* If the next token is an appropriate keyword, we can simply
7481 add it to the list. */
7482 switch (token->keyword)
7484 /* decl-specifier:
7485 friend */
7486 case RID_FRIEND:
7487 ++decl_specs->specs[(int) ds_friend];
7488 /* Consume the token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 break;
7492 /* function-specifier:
7493 inline
7494 virtual
7495 explicit */
7496 case RID_INLINE:
7497 case RID_VIRTUAL:
7498 case RID_EXPLICIT:
7499 cp_parser_function_specifier_opt (parser, decl_specs);
7500 break;
7502 /* decl-specifier:
7503 typedef */
7504 case RID_TYPEDEF:
7505 ++decl_specs->specs[(int) ds_typedef];
7506 /* Consume the token. */
7507 cp_lexer_consume_token (parser->lexer);
7508 /* A constructor declarator cannot appear in a typedef. */
7509 constructor_possible_p = false;
7510 /* The "typedef" keyword can only occur in a declaration; we
7511 may as well commit at this point. */
7512 cp_parser_commit_to_tentative_parse (parser);
7513 break;
7515 /* storage-class-specifier:
7516 auto
7517 register
7518 static
7519 extern
7520 mutable
7522 GNU Extension:
7523 thread */
7524 case RID_AUTO:
7525 case RID_REGISTER:
7526 case RID_STATIC:
7527 case RID_EXTERN:
7528 case RID_MUTABLE:
7529 /* Consume the token. */
7530 cp_lexer_consume_token (parser->lexer);
7531 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7532 break;
7533 case RID_THREAD:
7534 /* Consume the token. */
7535 cp_lexer_consume_token (parser->lexer);
7536 ++decl_specs->specs[(int) ds_thread];
7537 break;
7539 default:
7540 /* We did not yet find a decl-specifier yet. */
7541 found_decl_spec = false;
7542 break;
7545 /* Constructors are a special case. The `S' in `S()' is not a
7546 decl-specifier; it is the beginning of the declarator. */
7547 constructor_p
7548 = (!found_decl_spec
7549 && constructor_possible_p
7550 && (cp_parser_constructor_declarator_p
7551 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7553 /* If we don't have a DECL_SPEC yet, then we must be looking at
7554 a type-specifier. */
7555 if (!found_decl_spec && !constructor_p)
7557 int decl_spec_declares_class_or_enum;
7558 bool is_cv_qualifier;
7559 tree type_spec;
7561 type_spec
7562 = cp_parser_type_specifier (parser, flags,
7563 decl_specs,
7564 /*is_declaration=*/true,
7565 &decl_spec_declares_class_or_enum,
7566 &is_cv_qualifier);
7568 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7570 /* If this type-specifier referenced a user-defined type
7571 (a typedef, class-name, etc.), then we can't allow any
7572 more such type-specifiers henceforth.
7574 [dcl.spec]
7576 The longest sequence of decl-specifiers that could
7577 possibly be a type name is taken as the
7578 decl-specifier-seq of a declaration. The sequence shall
7579 be self-consistent as described below.
7581 [dcl.type]
7583 As a general rule, at most one type-specifier is allowed
7584 in the complete decl-specifier-seq of a declaration. The
7585 only exceptions are the following:
7587 -- const or volatile can be combined with any other
7588 type-specifier.
7590 -- signed or unsigned can be combined with char, long,
7591 short, or int.
7593 -- ..
7595 Example:
7597 typedef char* Pc;
7598 void g (const int Pc);
7600 Here, Pc is *not* part of the decl-specifier seq; it's
7601 the declarator. Therefore, once we see a type-specifier
7602 (other than a cv-qualifier), we forbid any additional
7603 user-defined types. We *do* still allow things like `int
7604 int' to be considered a decl-specifier-seq, and issue the
7605 error message later. */
7606 if (type_spec && !is_cv_qualifier)
7607 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7608 /* A constructor declarator cannot follow a type-specifier. */
7609 if (type_spec)
7611 constructor_possible_p = false;
7612 found_decl_spec = true;
7616 /* If we still do not have a DECL_SPEC, then there are no more
7617 decl-specifiers. */
7618 if (!found_decl_spec)
7619 break;
7621 decl_specs->any_specifiers_p = true;
7622 /* After we see one decl-specifier, further decl-specifiers are
7623 always optional. */
7624 flags |= CP_PARSER_FLAGS_OPTIONAL;
7627 cp_parser_check_decl_spec (decl_specs);
7629 /* Don't allow a friend specifier with a class definition. */
7630 if (decl_specs->specs[(int) ds_friend] != 0
7631 && (*declares_class_or_enum & 2))
7632 error ("class definition may not be declared a friend");
7635 /* Parse an (optional) storage-class-specifier.
7637 storage-class-specifier:
7638 auto
7639 register
7640 static
7641 extern
7642 mutable
7644 GNU Extension:
7646 storage-class-specifier:
7647 thread
7649 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7651 static tree
7652 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7654 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7656 case RID_AUTO:
7657 case RID_REGISTER:
7658 case RID_STATIC:
7659 case RID_EXTERN:
7660 case RID_MUTABLE:
7661 case RID_THREAD:
7662 /* Consume the token. */
7663 return cp_lexer_consume_token (parser->lexer)->value;
7665 default:
7666 return NULL_TREE;
7670 /* Parse an (optional) function-specifier.
7672 function-specifier:
7673 inline
7674 virtual
7675 explicit
7677 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7678 Updates DECL_SPECS, if it is non-NULL. */
7680 static tree
7681 cp_parser_function_specifier_opt (cp_parser* parser,
7682 cp_decl_specifier_seq *decl_specs)
7684 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7686 case RID_INLINE:
7687 if (decl_specs)
7688 ++decl_specs->specs[(int) ds_inline];
7689 break;
7691 case RID_VIRTUAL:
7692 if (decl_specs)
7693 ++decl_specs->specs[(int) ds_virtual];
7694 break;
7696 case RID_EXPLICIT:
7697 if (decl_specs)
7698 ++decl_specs->specs[(int) ds_explicit];
7699 break;
7701 default:
7702 return NULL_TREE;
7705 /* Consume the token. */
7706 return cp_lexer_consume_token (parser->lexer)->value;
7709 /* Parse a linkage-specification.
7711 linkage-specification:
7712 extern string-literal { declaration-seq [opt] }
7713 extern string-literal declaration */
7715 static void
7716 cp_parser_linkage_specification (cp_parser* parser)
7718 tree linkage;
7720 /* Look for the `extern' keyword. */
7721 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7723 /* Look for the string-literal. */
7724 linkage = cp_parser_string_literal (parser, false, false);
7726 /* Transform the literal into an identifier. If the literal is a
7727 wide-character string, or contains embedded NULs, then we can't
7728 handle it as the user wants. */
7729 if (strlen (TREE_STRING_POINTER (linkage))
7730 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7732 cp_parser_error (parser, "invalid linkage-specification");
7733 /* Assume C++ linkage. */
7734 linkage = lang_name_cplusplus;
7736 else
7737 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7739 /* We're now using the new linkage. */
7740 push_lang_context (linkage);
7742 /* If the next token is a `{', then we're using the first
7743 production. */
7744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7746 /* Consume the `{' token. */
7747 cp_lexer_consume_token (parser->lexer);
7748 /* Parse the declarations. */
7749 cp_parser_declaration_seq_opt (parser);
7750 /* Look for the closing `}'. */
7751 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7753 /* Otherwise, there's just one declaration. */
7754 else
7756 bool saved_in_unbraced_linkage_specification_p;
7758 saved_in_unbraced_linkage_specification_p
7759 = parser->in_unbraced_linkage_specification_p;
7760 parser->in_unbraced_linkage_specification_p = true;
7761 cp_parser_declaration (parser);
7762 parser->in_unbraced_linkage_specification_p
7763 = saved_in_unbraced_linkage_specification_p;
7766 /* We're done with the linkage-specification. */
7767 pop_lang_context ();
7770 /* Special member functions [gram.special] */
7772 /* Parse a conversion-function-id.
7774 conversion-function-id:
7775 operator conversion-type-id
7777 Returns an IDENTIFIER_NODE representing the operator. */
7779 static tree
7780 cp_parser_conversion_function_id (cp_parser* parser)
7782 tree type;
7783 tree saved_scope;
7784 tree saved_qualifying_scope;
7785 tree saved_object_scope;
7786 tree pushed_scope = NULL_TREE;
7788 /* Look for the `operator' token. */
7789 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7790 return error_mark_node;
7791 /* When we parse the conversion-type-id, the current scope will be
7792 reset. However, we need that information in able to look up the
7793 conversion function later, so we save it here. */
7794 saved_scope = parser->scope;
7795 saved_qualifying_scope = parser->qualifying_scope;
7796 saved_object_scope = parser->object_scope;
7797 /* We must enter the scope of the class so that the names of
7798 entities declared within the class are available in the
7799 conversion-type-id. For example, consider:
7801 struct S {
7802 typedef int I;
7803 operator I();
7806 S::operator I() { ... }
7808 In order to see that `I' is a type-name in the definition, we
7809 must be in the scope of `S'. */
7810 if (saved_scope)
7811 pushed_scope = push_scope (saved_scope);
7812 /* Parse the conversion-type-id. */
7813 type = cp_parser_conversion_type_id (parser);
7814 /* Leave the scope of the class, if any. */
7815 if (pushed_scope)
7816 pop_scope (pushed_scope);
7817 /* Restore the saved scope. */
7818 parser->scope = saved_scope;
7819 parser->qualifying_scope = saved_qualifying_scope;
7820 parser->object_scope = saved_object_scope;
7821 /* If the TYPE is invalid, indicate failure. */
7822 if (type == error_mark_node)
7823 return error_mark_node;
7824 return mangle_conv_op_name_for_type (type);
7827 /* Parse a conversion-type-id:
7829 conversion-type-id:
7830 type-specifier-seq conversion-declarator [opt]
7832 Returns the TYPE specified. */
7834 static tree
7835 cp_parser_conversion_type_id (cp_parser* parser)
7837 tree attributes;
7838 cp_decl_specifier_seq type_specifiers;
7839 cp_declarator *declarator;
7840 tree type_specified;
7842 /* Parse the attributes. */
7843 attributes = cp_parser_attributes_opt (parser);
7844 /* Parse the type-specifiers. */
7845 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7846 &type_specifiers);
7847 /* If that didn't work, stop. */
7848 if (type_specifiers.type == error_mark_node)
7849 return error_mark_node;
7850 /* Parse the conversion-declarator. */
7851 declarator = cp_parser_conversion_declarator_opt (parser);
7853 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7854 /*initialized=*/0, &attributes);
7855 if (attributes)
7856 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7857 return type_specified;
7860 /* Parse an (optional) conversion-declarator.
7862 conversion-declarator:
7863 ptr-operator conversion-declarator [opt]
7867 static cp_declarator *
7868 cp_parser_conversion_declarator_opt (cp_parser* parser)
7870 enum tree_code code;
7871 tree class_type;
7872 cp_cv_quals cv_quals;
7874 /* We don't know if there's a ptr-operator next, or not. */
7875 cp_parser_parse_tentatively (parser);
7876 /* Try the ptr-operator. */
7877 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7878 /* If it worked, look for more conversion-declarators. */
7879 if (cp_parser_parse_definitely (parser))
7881 cp_declarator *declarator;
7883 /* Parse another optional declarator. */
7884 declarator = cp_parser_conversion_declarator_opt (parser);
7886 /* Create the representation of the declarator. */
7887 if (class_type)
7888 declarator = make_ptrmem_declarator (cv_quals, class_type,
7889 declarator);
7890 else if (code == INDIRECT_REF)
7891 declarator = make_pointer_declarator (cv_quals, declarator);
7892 else
7893 declarator = make_reference_declarator (cv_quals, declarator);
7895 return declarator;
7898 return NULL;
7901 /* Parse an (optional) ctor-initializer.
7903 ctor-initializer:
7904 : mem-initializer-list
7906 Returns TRUE iff the ctor-initializer was actually present. */
7908 static bool
7909 cp_parser_ctor_initializer_opt (cp_parser* parser)
7911 /* If the next token is not a `:', then there is no
7912 ctor-initializer. */
7913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7915 /* Do default initialization of any bases and members. */
7916 if (DECL_CONSTRUCTOR_P (current_function_decl))
7917 finish_mem_initializers (NULL_TREE);
7919 return false;
7922 /* Consume the `:' token. */
7923 cp_lexer_consume_token (parser->lexer);
7924 /* And the mem-initializer-list. */
7925 cp_parser_mem_initializer_list (parser);
7927 return true;
7930 /* Parse a mem-initializer-list.
7932 mem-initializer-list:
7933 mem-initializer
7934 mem-initializer , mem-initializer-list */
7936 static void
7937 cp_parser_mem_initializer_list (cp_parser* parser)
7939 tree mem_initializer_list = NULL_TREE;
7941 /* Let the semantic analysis code know that we are starting the
7942 mem-initializer-list. */
7943 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7944 error ("only constructors take base initializers");
7946 /* Loop through the list. */
7947 while (true)
7949 tree mem_initializer;
7951 /* Parse the mem-initializer. */
7952 mem_initializer = cp_parser_mem_initializer (parser);
7953 /* Add it to the list, unless it was erroneous. */
7954 if (mem_initializer != error_mark_node)
7956 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7957 mem_initializer_list = mem_initializer;
7959 /* If the next token is not a `,', we're done. */
7960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7961 break;
7962 /* Consume the `,' token. */
7963 cp_lexer_consume_token (parser->lexer);
7966 /* Perform semantic analysis. */
7967 if (DECL_CONSTRUCTOR_P (current_function_decl))
7968 finish_mem_initializers (mem_initializer_list);
7971 /* Parse a mem-initializer.
7973 mem-initializer:
7974 mem-initializer-id ( expression-list [opt] )
7976 GNU extension:
7978 mem-initializer:
7979 ( expression-list [opt] )
7981 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7982 class) or FIELD_DECL (for a non-static data member) to initialize;
7983 the TREE_VALUE is the expression-list. An empty initialization
7984 list is represented by void_list_node. */
7986 static tree
7987 cp_parser_mem_initializer (cp_parser* parser)
7989 tree mem_initializer_id;
7990 tree expression_list;
7991 tree member;
7993 /* Find out what is being initialized. */
7994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7996 pedwarn ("anachronistic old-style base class initializer");
7997 mem_initializer_id = NULL_TREE;
7999 else
8000 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8001 member = expand_member_init (mem_initializer_id);
8002 if (member && !DECL_P (member))
8003 in_base_initializer = 1;
8005 expression_list
8006 = cp_parser_parenthesized_expression_list (parser, false,
8007 /*cast_p=*/false,
8008 /*non_constant_p=*/NULL);
8009 if (expression_list == error_mark_node)
8010 return error_mark_node;
8011 if (!expression_list)
8012 expression_list = void_type_node;
8014 in_base_initializer = 0;
8016 return member ? build_tree_list (member, expression_list) : error_mark_node;
8019 /* Parse a mem-initializer-id.
8021 mem-initializer-id:
8022 :: [opt] nested-name-specifier [opt] class-name
8023 identifier
8025 Returns a TYPE indicating the class to be initializer for the first
8026 production. Returns an IDENTIFIER_NODE indicating the data member
8027 to be initialized for the second production. */
8029 static tree
8030 cp_parser_mem_initializer_id (cp_parser* parser)
8032 bool global_scope_p;
8033 bool nested_name_specifier_p;
8034 bool template_p = false;
8035 tree id;
8037 /* `typename' is not allowed in this context ([temp.res]). */
8038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8040 error ("keyword %<typename%> not allowed in this context (a qualified "
8041 "member initializer is implicitly a type)");
8042 cp_lexer_consume_token (parser->lexer);
8044 /* Look for the optional `::' operator. */
8045 global_scope_p
8046 = (cp_parser_global_scope_opt (parser,
8047 /*current_scope_valid_p=*/false)
8048 != NULL_TREE);
8049 /* Look for the optional nested-name-specifier. The simplest way to
8050 implement:
8052 [temp.res]
8054 The keyword `typename' is not permitted in a base-specifier or
8055 mem-initializer; in these contexts a qualified name that
8056 depends on a template-parameter is implicitly assumed to be a
8057 type name.
8059 is to assume that we have seen the `typename' keyword at this
8060 point. */
8061 nested_name_specifier_p
8062 = (cp_parser_nested_name_specifier_opt (parser,
8063 /*typename_keyword_p=*/true,
8064 /*check_dependency_p=*/true,
8065 /*type_p=*/true,
8066 /*is_declaration=*/true)
8067 != NULL_TREE);
8068 if (nested_name_specifier_p)
8069 template_p = cp_parser_optional_template_keyword (parser);
8070 /* If there is a `::' operator or a nested-name-specifier, then we
8071 are definitely looking for a class-name. */
8072 if (global_scope_p || nested_name_specifier_p)
8073 return cp_parser_class_name (parser,
8074 /*typename_keyword_p=*/true,
8075 /*template_keyword_p=*/template_p,
8076 none_type,
8077 /*check_dependency_p=*/true,
8078 /*class_head_p=*/false,
8079 /*is_declaration=*/true);
8080 /* Otherwise, we could also be looking for an ordinary identifier. */
8081 cp_parser_parse_tentatively (parser);
8082 /* Try a class-name. */
8083 id = cp_parser_class_name (parser,
8084 /*typename_keyword_p=*/true,
8085 /*template_keyword_p=*/false,
8086 none_type,
8087 /*check_dependency_p=*/true,
8088 /*class_head_p=*/false,
8089 /*is_declaration=*/true);
8090 /* If we found one, we're done. */
8091 if (cp_parser_parse_definitely (parser))
8092 return id;
8093 /* Otherwise, look for an ordinary identifier. */
8094 return cp_parser_identifier (parser);
8097 /* Overloading [gram.over] */
8099 /* Parse an operator-function-id.
8101 operator-function-id:
8102 operator operator
8104 Returns an IDENTIFIER_NODE for the operator which is a
8105 human-readable spelling of the identifier, e.g., `operator +'. */
8107 static tree
8108 cp_parser_operator_function_id (cp_parser* parser)
8110 /* Look for the `operator' keyword. */
8111 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8112 return error_mark_node;
8113 /* And then the name of the operator itself. */
8114 return cp_parser_operator (parser);
8117 /* Parse an operator.
8119 operator:
8120 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8121 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8122 || ++ -- , ->* -> () []
8124 GNU Extensions:
8126 operator:
8127 <? >? <?= >?=
8129 Returns an IDENTIFIER_NODE for the operator which is a
8130 human-readable spelling of the identifier, e.g., `operator +'. */
8132 static tree
8133 cp_parser_operator (cp_parser* parser)
8135 tree id = NULL_TREE;
8136 cp_token *token;
8138 /* Peek at the next token. */
8139 token = cp_lexer_peek_token (parser->lexer);
8140 /* Figure out which operator we have. */
8141 switch (token->type)
8143 case CPP_KEYWORD:
8145 enum tree_code op;
8147 /* The keyword should be either `new' or `delete'. */
8148 if (token->keyword == RID_NEW)
8149 op = NEW_EXPR;
8150 else if (token->keyword == RID_DELETE)
8151 op = DELETE_EXPR;
8152 else
8153 break;
8155 /* Consume the `new' or `delete' token. */
8156 cp_lexer_consume_token (parser->lexer);
8158 /* Peek at the next token. */
8159 token = cp_lexer_peek_token (parser->lexer);
8160 /* If it's a `[' token then this is the array variant of the
8161 operator. */
8162 if (token->type == CPP_OPEN_SQUARE)
8164 /* Consume the `[' token. */
8165 cp_lexer_consume_token (parser->lexer);
8166 /* Look for the `]' token. */
8167 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8168 id = ansi_opname (op == NEW_EXPR
8169 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8171 /* Otherwise, we have the non-array variant. */
8172 else
8173 id = ansi_opname (op);
8175 return id;
8178 case CPP_PLUS:
8179 id = ansi_opname (PLUS_EXPR);
8180 break;
8182 case CPP_MINUS:
8183 id = ansi_opname (MINUS_EXPR);
8184 break;
8186 case CPP_MULT:
8187 id = ansi_opname (MULT_EXPR);
8188 break;
8190 case CPP_DIV:
8191 id = ansi_opname (TRUNC_DIV_EXPR);
8192 break;
8194 case CPP_MOD:
8195 id = ansi_opname (TRUNC_MOD_EXPR);
8196 break;
8198 case CPP_XOR:
8199 id = ansi_opname (BIT_XOR_EXPR);
8200 break;
8202 case CPP_AND:
8203 id = ansi_opname (BIT_AND_EXPR);
8204 break;
8206 case CPP_OR:
8207 id = ansi_opname (BIT_IOR_EXPR);
8208 break;
8210 case CPP_COMPL:
8211 id = ansi_opname (BIT_NOT_EXPR);
8212 break;
8214 case CPP_NOT:
8215 id = ansi_opname (TRUTH_NOT_EXPR);
8216 break;
8218 case CPP_EQ:
8219 id = ansi_assopname (NOP_EXPR);
8220 break;
8222 case CPP_LESS:
8223 id = ansi_opname (LT_EXPR);
8224 break;
8226 case CPP_GREATER:
8227 id = ansi_opname (GT_EXPR);
8228 break;
8230 case CPP_PLUS_EQ:
8231 id = ansi_assopname (PLUS_EXPR);
8232 break;
8234 case CPP_MINUS_EQ:
8235 id = ansi_assopname (MINUS_EXPR);
8236 break;
8238 case CPP_MULT_EQ:
8239 id = ansi_assopname (MULT_EXPR);
8240 break;
8242 case CPP_DIV_EQ:
8243 id = ansi_assopname (TRUNC_DIV_EXPR);
8244 break;
8246 case CPP_MOD_EQ:
8247 id = ansi_assopname (TRUNC_MOD_EXPR);
8248 break;
8250 case CPP_XOR_EQ:
8251 id = ansi_assopname (BIT_XOR_EXPR);
8252 break;
8254 case CPP_AND_EQ:
8255 id = ansi_assopname (BIT_AND_EXPR);
8256 break;
8258 case CPP_OR_EQ:
8259 id = ansi_assopname (BIT_IOR_EXPR);
8260 break;
8262 case CPP_LSHIFT:
8263 id = ansi_opname (LSHIFT_EXPR);
8264 break;
8266 case CPP_RSHIFT:
8267 id = ansi_opname (RSHIFT_EXPR);
8268 break;
8270 case CPP_LSHIFT_EQ:
8271 id = ansi_assopname (LSHIFT_EXPR);
8272 break;
8274 case CPP_RSHIFT_EQ:
8275 id = ansi_assopname (RSHIFT_EXPR);
8276 break;
8278 case CPP_EQ_EQ:
8279 id = ansi_opname (EQ_EXPR);
8280 break;
8282 case CPP_NOT_EQ:
8283 id = ansi_opname (NE_EXPR);
8284 break;
8286 case CPP_LESS_EQ:
8287 id = ansi_opname (LE_EXPR);
8288 break;
8290 case CPP_GREATER_EQ:
8291 id = ansi_opname (GE_EXPR);
8292 break;
8294 case CPP_AND_AND:
8295 id = ansi_opname (TRUTH_ANDIF_EXPR);
8296 break;
8298 case CPP_OR_OR:
8299 id = ansi_opname (TRUTH_ORIF_EXPR);
8300 break;
8302 case CPP_PLUS_PLUS:
8303 id = ansi_opname (POSTINCREMENT_EXPR);
8304 break;
8306 case CPP_MINUS_MINUS:
8307 id = ansi_opname (PREDECREMENT_EXPR);
8308 break;
8310 case CPP_COMMA:
8311 id = ansi_opname (COMPOUND_EXPR);
8312 break;
8314 case CPP_DEREF_STAR:
8315 id = ansi_opname (MEMBER_REF);
8316 break;
8318 case CPP_DEREF:
8319 id = ansi_opname (COMPONENT_REF);
8320 break;
8322 case CPP_OPEN_PAREN:
8323 /* Consume the `('. */
8324 cp_lexer_consume_token (parser->lexer);
8325 /* Look for the matching `)'. */
8326 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8327 return ansi_opname (CALL_EXPR);
8329 case CPP_OPEN_SQUARE:
8330 /* Consume the `['. */
8331 cp_lexer_consume_token (parser->lexer);
8332 /* Look for the matching `]'. */
8333 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8334 return ansi_opname (ARRAY_REF);
8336 /* Extensions. */
8337 case CPP_MIN:
8338 id = ansi_opname (MIN_EXPR);
8339 cp_parser_warn_min_max ();
8340 break;
8342 case CPP_MAX:
8343 id = ansi_opname (MAX_EXPR);
8344 cp_parser_warn_min_max ();
8345 break;
8347 case CPP_MIN_EQ:
8348 id = ansi_assopname (MIN_EXPR);
8349 cp_parser_warn_min_max ();
8350 break;
8352 case CPP_MAX_EQ:
8353 id = ansi_assopname (MAX_EXPR);
8354 cp_parser_warn_min_max ();
8355 break;
8357 default:
8358 /* Anything else is an error. */
8359 break;
8362 /* If we have selected an identifier, we need to consume the
8363 operator token. */
8364 if (id)
8365 cp_lexer_consume_token (parser->lexer);
8366 /* Otherwise, no valid operator name was present. */
8367 else
8369 cp_parser_error (parser, "expected operator");
8370 id = error_mark_node;
8373 return id;
8376 /* Parse a template-declaration.
8378 template-declaration:
8379 export [opt] template < template-parameter-list > declaration
8381 If MEMBER_P is TRUE, this template-declaration occurs within a
8382 class-specifier.
8384 The grammar rule given by the standard isn't correct. What
8385 is really meant is:
8387 template-declaration:
8388 export [opt] template-parameter-list-seq
8389 decl-specifier-seq [opt] init-declarator [opt] ;
8390 export [opt] template-parameter-list-seq
8391 function-definition
8393 template-parameter-list-seq:
8394 template-parameter-list-seq [opt]
8395 template < template-parameter-list > */
8397 static void
8398 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8400 /* Check for `export'. */
8401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8403 /* Consume the `export' token. */
8404 cp_lexer_consume_token (parser->lexer);
8405 /* Warn that we do not support `export'. */
8406 warning (0, "keyword %<export%> not implemented, and will be ignored");
8409 cp_parser_template_declaration_after_export (parser, member_p);
8412 /* Parse a template-parameter-list.
8414 template-parameter-list:
8415 template-parameter
8416 template-parameter-list , template-parameter
8418 Returns a TREE_LIST. Each node represents a template parameter.
8419 The nodes are connected via their TREE_CHAINs. */
8421 static tree
8422 cp_parser_template_parameter_list (cp_parser* parser)
8424 tree parameter_list = NULL_TREE;
8426 while (true)
8428 tree parameter;
8429 cp_token *token;
8430 bool is_non_type;
8432 /* Parse the template-parameter. */
8433 parameter = cp_parser_template_parameter (parser, &is_non_type);
8434 /* Add it to the list. */
8435 if (parameter != error_mark_node)
8436 parameter_list = process_template_parm (parameter_list,
8437 parameter,
8438 is_non_type);
8439 /* Peek at the next token. */
8440 token = cp_lexer_peek_token (parser->lexer);
8441 /* If it's not a `,', we're done. */
8442 if (token->type != CPP_COMMA)
8443 break;
8444 /* Otherwise, consume the `,' token. */
8445 cp_lexer_consume_token (parser->lexer);
8448 return parameter_list;
8451 /* Parse a template-parameter.
8453 template-parameter:
8454 type-parameter
8455 parameter-declaration
8457 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8458 the parameter. The TREE_PURPOSE is the default value, if any.
8459 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8460 iff this parameter is a non-type parameter. */
8462 static tree
8463 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8465 cp_token *token;
8466 cp_parameter_declarator *parameter_declarator;
8467 tree parm;
8469 /* Assume it is a type parameter or a template parameter. */
8470 *is_non_type = false;
8471 /* Peek at the next token. */
8472 token = cp_lexer_peek_token (parser->lexer);
8473 /* If it is `class' or `template', we have a type-parameter. */
8474 if (token->keyword == RID_TEMPLATE)
8475 return cp_parser_type_parameter (parser);
8476 /* If it is `class' or `typename' we do not know yet whether it is a
8477 type parameter or a non-type parameter. Consider:
8479 template <typename T, typename T::X X> ...
8483 template <class C, class D*> ...
8485 Here, the first parameter is a type parameter, and the second is
8486 a non-type parameter. We can tell by looking at the token after
8487 the identifier -- if it is a `,', `=', or `>' then we have a type
8488 parameter. */
8489 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8491 /* Peek at the token after `class' or `typename'. */
8492 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8493 /* If it's an identifier, skip it. */
8494 if (token->type == CPP_NAME)
8495 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8496 /* Now, see if the token looks like the end of a template
8497 parameter. */
8498 if (token->type == CPP_COMMA
8499 || token->type == CPP_EQ
8500 || token->type == CPP_GREATER)
8501 return cp_parser_type_parameter (parser);
8504 /* Otherwise, it is a non-type parameter.
8506 [temp.param]
8508 When parsing a default template-argument for a non-type
8509 template-parameter, the first non-nested `>' is taken as the end
8510 of the template parameter-list rather than a greater-than
8511 operator. */
8512 *is_non_type = true;
8513 parameter_declarator
8514 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8515 /*parenthesized_p=*/NULL);
8516 parm = grokdeclarator (parameter_declarator->declarator,
8517 &parameter_declarator->decl_specifiers,
8518 PARM, /*initialized=*/0,
8519 /*attrlist=*/NULL);
8520 if (parm == error_mark_node)
8521 return error_mark_node;
8522 return build_tree_list (parameter_declarator->default_argument, parm);
8525 /* Parse a type-parameter.
8527 type-parameter:
8528 class identifier [opt]
8529 class identifier [opt] = type-id
8530 typename identifier [opt]
8531 typename identifier [opt] = type-id
8532 template < template-parameter-list > class identifier [opt]
8533 template < template-parameter-list > class identifier [opt]
8534 = id-expression
8536 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8537 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8538 the declaration of the parameter. */
8540 static tree
8541 cp_parser_type_parameter (cp_parser* parser)
8543 cp_token *token;
8544 tree parameter;
8546 /* Look for a keyword to tell us what kind of parameter this is. */
8547 token = cp_parser_require (parser, CPP_KEYWORD,
8548 "`class', `typename', or `template'");
8549 if (!token)
8550 return error_mark_node;
8552 switch (token->keyword)
8554 case RID_CLASS:
8555 case RID_TYPENAME:
8557 tree identifier;
8558 tree default_argument;
8560 /* If the next token is an identifier, then it names the
8561 parameter. */
8562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8563 identifier = cp_parser_identifier (parser);
8564 else
8565 identifier = NULL_TREE;
8567 /* Create the parameter. */
8568 parameter = finish_template_type_parm (class_type_node, identifier);
8570 /* If the next token is an `=', we have a default argument. */
8571 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8573 /* Consume the `=' token. */
8574 cp_lexer_consume_token (parser->lexer);
8575 /* Parse the default-argument. */
8576 push_deferring_access_checks (dk_no_deferred);
8577 default_argument = cp_parser_type_id (parser);
8578 pop_deferring_access_checks ();
8580 else
8581 default_argument = NULL_TREE;
8583 /* Create the combined representation of the parameter and the
8584 default argument. */
8585 parameter = build_tree_list (default_argument, parameter);
8587 break;
8589 case RID_TEMPLATE:
8591 tree parameter_list;
8592 tree identifier;
8593 tree default_argument;
8595 /* Look for the `<'. */
8596 cp_parser_require (parser, CPP_LESS, "`<'");
8597 /* Parse the template-parameter-list. */
8598 begin_template_parm_list ();
8599 parameter_list
8600 = cp_parser_template_parameter_list (parser);
8601 parameter_list = end_template_parm_list (parameter_list);
8602 /* Look for the `>'. */
8603 cp_parser_require (parser, CPP_GREATER, "`>'");
8604 /* Look for the `class' keyword. */
8605 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8606 /* If the next token is an `=', then there is a
8607 default-argument. If the next token is a `>', we are at
8608 the end of the parameter-list. If the next token is a `,',
8609 then we are at the end of this parameter. */
8610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8611 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8612 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8614 identifier = cp_parser_identifier (parser);
8615 /* Treat invalid names as if the parameter were nameless. */
8616 if (identifier == error_mark_node)
8617 identifier = NULL_TREE;
8619 else
8620 identifier = NULL_TREE;
8622 /* Create the template parameter. */
8623 parameter = finish_template_template_parm (class_type_node,
8624 identifier);
8626 /* If the next token is an `=', then there is a
8627 default-argument. */
8628 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8630 bool is_template;
8632 /* Consume the `='. */
8633 cp_lexer_consume_token (parser->lexer);
8634 /* Parse the id-expression. */
8635 push_deferring_access_checks (dk_no_deferred);
8636 default_argument
8637 = cp_parser_id_expression (parser,
8638 /*template_keyword_p=*/false,
8639 /*check_dependency_p=*/true,
8640 /*template_p=*/&is_template,
8641 /*declarator_p=*/false);
8642 if (TREE_CODE (default_argument) == TYPE_DECL)
8643 /* If the id-expression was a template-id that refers to
8644 a template-class, we already have the declaration here,
8645 so no further lookup is needed. */
8647 else
8648 /* Look up the name. */
8649 default_argument
8650 = cp_parser_lookup_name (parser, default_argument,
8651 none_type,
8652 /*is_template=*/is_template,
8653 /*is_namespace=*/false,
8654 /*check_dependency=*/true,
8655 /*ambiguous_decls=*/NULL);
8656 /* See if the default argument is valid. */
8657 default_argument
8658 = check_template_template_default_arg (default_argument);
8659 pop_deferring_access_checks ();
8661 else
8662 default_argument = NULL_TREE;
8664 /* Create the combined representation of the parameter and the
8665 default argument. */
8666 parameter = build_tree_list (default_argument, parameter);
8668 break;
8670 default:
8671 gcc_unreachable ();
8672 break;
8675 return parameter;
8678 /* Parse a template-id.
8680 template-id:
8681 template-name < template-argument-list [opt] >
8683 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8684 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8685 returned. Otherwise, if the template-name names a function, or set
8686 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8687 names a class, returns a TYPE_DECL for the specialization.
8689 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8690 uninstantiated templates. */
8692 static tree
8693 cp_parser_template_id (cp_parser *parser,
8694 bool template_keyword_p,
8695 bool check_dependency_p,
8696 bool is_declaration)
8698 tree template;
8699 tree arguments;
8700 tree template_id;
8701 cp_token_position start_of_id = 0;
8702 tree access_check = NULL_TREE;
8703 cp_token *next_token, *next_token_2;
8704 bool is_identifier;
8706 /* If the next token corresponds to a template-id, there is no need
8707 to reparse it. */
8708 next_token = cp_lexer_peek_token (parser->lexer);
8709 if (next_token->type == CPP_TEMPLATE_ID)
8711 tree value;
8712 tree check;
8714 /* Get the stored value. */
8715 value = cp_lexer_consume_token (parser->lexer)->value;
8716 /* Perform any access checks that were deferred. */
8717 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8718 perform_or_defer_access_check (TREE_PURPOSE (check),
8719 TREE_VALUE (check));
8720 /* Return the stored value. */
8721 return TREE_VALUE (value);
8724 /* Avoid performing name lookup if there is no possibility of
8725 finding a template-id. */
8726 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8727 || (next_token->type == CPP_NAME
8728 && !cp_parser_nth_token_starts_template_argument_list_p
8729 (parser, 2)))
8731 cp_parser_error (parser, "expected template-id");
8732 return error_mark_node;
8735 /* Remember where the template-id starts. */
8736 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8737 start_of_id = cp_lexer_token_position (parser->lexer, false);
8739 push_deferring_access_checks (dk_deferred);
8741 /* Parse the template-name. */
8742 is_identifier = false;
8743 template = cp_parser_template_name (parser, template_keyword_p,
8744 check_dependency_p,
8745 is_declaration,
8746 &is_identifier);
8747 if (template == error_mark_node || is_identifier)
8749 pop_deferring_access_checks ();
8750 return template;
8753 /* If we find the sequence `[:' after a template-name, it's probably
8754 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8755 parse correctly the argument list. */
8756 next_token = cp_lexer_peek_token (parser->lexer);
8757 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8758 if (next_token->type == CPP_OPEN_SQUARE
8759 && next_token->flags & DIGRAPH
8760 && next_token_2->type == CPP_COLON
8761 && !(next_token_2->flags & PREV_WHITE))
8763 cp_parser_parse_tentatively (parser);
8764 /* Change `:' into `::'. */
8765 next_token_2->type = CPP_SCOPE;
8766 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8767 CPP_LESS. */
8768 cp_lexer_consume_token (parser->lexer);
8769 /* Parse the arguments. */
8770 arguments = cp_parser_enclosed_template_argument_list (parser);
8771 if (!cp_parser_parse_definitely (parser))
8773 /* If we couldn't parse an argument list, then we revert our changes
8774 and return simply an error. Maybe this is not a template-id
8775 after all. */
8776 next_token_2->type = CPP_COLON;
8777 cp_parser_error (parser, "expected %<<%>");
8778 pop_deferring_access_checks ();
8779 return error_mark_node;
8781 /* Otherwise, emit an error about the invalid digraph, but continue
8782 parsing because we got our argument list. */
8783 pedwarn ("%<<::%> cannot begin a template-argument list");
8784 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8785 "between %<<%> and %<::%>");
8786 if (!flag_permissive)
8788 static bool hint;
8789 if (!hint)
8791 inform ("(if you use -fpermissive G++ will accept your code)");
8792 hint = true;
8796 else
8798 /* Look for the `<' that starts the template-argument-list. */
8799 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8801 pop_deferring_access_checks ();
8802 return error_mark_node;
8804 /* Parse the arguments. */
8805 arguments = cp_parser_enclosed_template_argument_list (parser);
8808 /* Build a representation of the specialization. */
8809 if (TREE_CODE (template) == IDENTIFIER_NODE)
8810 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8811 else if (DECL_CLASS_TEMPLATE_P (template)
8812 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8814 bool entering_scope;
8815 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8816 template (rather than some instantiation thereof) only if
8817 is not nested within some other construct. For example, in
8818 "template <typename T> void f(T) { A<T>::", A<T> is just an
8819 instantiation of A. */
8820 entering_scope = (template_parm_scope_p ()
8821 && cp_lexer_next_token_is (parser->lexer,
8822 CPP_SCOPE));
8823 template_id
8824 = finish_template_type (template, arguments, entering_scope);
8826 else
8828 /* If it's not a class-template or a template-template, it should be
8829 a function-template. */
8830 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8831 || TREE_CODE (template) == OVERLOAD
8832 || BASELINK_P (template)));
8834 template_id = lookup_template_function (template, arguments);
8837 /* Retrieve any deferred checks. Do not pop this access checks yet
8838 so the memory will not be reclaimed during token replacing below. */
8839 access_check = get_deferred_access_checks ();
8841 /* If parsing tentatively, replace the sequence of tokens that makes
8842 up the template-id with a CPP_TEMPLATE_ID token. That way,
8843 should we re-parse the token stream, we will not have to repeat
8844 the effort required to do the parse, nor will we issue duplicate
8845 error messages about problems during instantiation of the
8846 template. */
8847 if (start_of_id)
8849 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8851 /* Reset the contents of the START_OF_ID token. */
8852 token->type = CPP_TEMPLATE_ID;
8853 token->value = build_tree_list (access_check, template_id);
8854 token->keyword = RID_MAX;
8856 /* Purge all subsequent tokens. */
8857 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8859 /* ??? Can we actually assume that, if template_id ==
8860 error_mark_node, we will have issued a diagnostic to the
8861 user, as opposed to simply marking the tentative parse as
8862 failed? */
8863 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8864 error ("parse error in template argument list");
8867 pop_deferring_access_checks ();
8868 return template_id;
8871 /* Parse a template-name.
8873 template-name:
8874 identifier
8876 The standard should actually say:
8878 template-name:
8879 identifier
8880 operator-function-id
8882 A defect report has been filed about this issue.
8884 A conversion-function-id cannot be a template name because they cannot
8885 be part of a template-id. In fact, looking at this code:
8887 a.operator K<int>()
8889 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8890 It is impossible to call a templated conversion-function-id with an
8891 explicit argument list, since the only allowed template parameter is
8892 the type to which it is converting.
8894 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8895 `template' keyword, in a construction like:
8897 T::template f<3>()
8899 In that case `f' is taken to be a template-name, even though there
8900 is no way of knowing for sure.
8902 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8903 name refers to a set of overloaded functions, at least one of which
8904 is a template, or an IDENTIFIER_NODE with the name of the template,
8905 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8906 names are looked up inside uninstantiated templates. */
8908 static tree
8909 cp_parser_template_name (cp_parser* parser,
8910 bool template_keyword_p,
8911 bool check_dependency_p,
8912 bool is_declaration,
8913 bool *is_identifier)
8915 tree identifier;
8916 tree decl;
8917 tree fns;
8919 /* If the next token is `operator', then we have either an
8920 operator-function-id or a conversion-function-id. */
8921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8923 /* We don't know whether we're looking at an
8924 operator-function-id or a conversion-function-id. */
8925 cp_parser_parse_tentatively (parser);
8926 /* Try an operator-function-id. */
8927 identifier = cp_parser_operator_function_id (parser);
8928 /* If that didn't work, try a conversion-function-id. */
8929 if (!cp_parser_parse_definitely (parser))
8931 cp_parser_error (parser, "expected template-name");
8932 return error_mark_node;
8935 /* Look for the identifier. */
8936 else
8937 identifier = cp_parser_identifier (parser);
8939 /* If we didn't find an identifier, we don't have a template-id. */
8940 if (identifier == error_mark_node)
8941 return error_mark_node;
8943 /* If the name immediately followed the `template' keyword, then it
8944 is a template-name. However, if the next token is not `<', then
8945 we do not treat it as a template-name, since it is not being used
8946 as part of a template-id. This enables us to handle constructs
8947 like:
8949 template <typename T> struct S { S(); };
8950 template <typename T> S<T>::S();
8952 correctly. We would treat `S' as a template -- if it were `S<T>'
8953 -- but we do not if there is no `<'. */
8955 if (processing_template_decl
8956 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8958 /* In a declaration, in a dependent context, we pretend that the
8959 "template" keyword was present in order to improve error
8960 recovery. For example, given:
8962 template <typename T> void f(T::X<int>);
8964 we want to treat "X<int>" as a template-id. */
8965 if (is_declaration
8966 && !template_keyword_p
8967 && parser->scope && TYPE_P (parser->scope)
8968 && check_dependency_p
8969 && dependent_type_p (parser->scope)
8970 /* Do not do this for dtors (or ctors), since they never
8971 need the template keyword before their name. */
8972 && !constructor_name_p (identifier, parser->scope))
8974 cp_token_position start = 0;
8976 /* Explain what went wrong. */
8977 error ("non-template %qD used as template", identifier);
8978 inform ("use %<%T::template %D%> to indicate that it is a template",
8979 parser->scope, identifier);
8980 /* If parsing tentatively, find the location of the "<" token. */
8981 if (cp_parser_simulate_error (parser))
8982 start = cp_lexer_token_position (parser->lexer, true);
8983 /* Parse the template arguments so that we can issue error
8984 messages about them. */
8985 cp_lexer_consume_token (parser->lexer);
8986 cp_parser_enclosed_template_argument_list (parser);
8987 /* Skip tokens until we find a good place from which to
8988 continue parsing. */
8989 cp_parser_skip_to_closing_parenthesis (parser,
8990 /*recovering=*/true,
8991 /*or_comma=*/true,
8992 /*consume_paren=*/false);
8993 /* If parsing tentatively, permanently remove the
8994 template argument list. That will prevent duplicate
8995 error messages from being issued about the missing
8996 "template" keyword. */
8997 if (start)
8998 cp_lexer_purge_tokens_after (parser->lexer, start);
8999 if (is_identifier)
9000 *is_identifier = true;
9001 return identifier;
9004 /* If the "template" keyword is present, then there is generally
9005 no point in doing name-lookup, so we just return IDENTIFIER.
9006 But, if the qualifying scope is non-dependent then we can
9007 (and must) do name-lookup normally. */
9008 if (template_keyword_p
9009 && (!parser->scope
9010 || (TYPE_P (parser->scope)
9011 && dependent_type_p (parser->scope))))
9012 return identifier;
9015 /* Look up the name. */
9016 decl = cp_parser_lookup_name (parser, identifier,
9017 none_type,
9018 /*is_template=*/false,
9019 /*is_namespace=*/false,
9020 check_dependency_p,
9021 /*ambiguous_decls=*/NULL);
9022 decl = maybe_get_template_decl_from_type_decl (decl);
9024 /* If DECL is a template, then the name was a template-name. */
9025 if (TREE_CODE (decl) == TEMPLATE_DECL)
9027 else
9029 tree fn = NULL_TREE;
9031 /* The standard does not explicitly indicate whether a name that
9032 names a set of overloaded declarations, some of which are
9033 templates, is a template-name. However, such a name should
9034 be a template-name; otherwise, there is no way to form a
9035 template-id for the overloaded templates. */
9036 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9037 if (TREE_CODE (fns) == OVERLOAD)
9038 for (fn = fns; fn; fn = OVL_NEXT (fn))
9039 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9040 break;
9042 if (!fn)
9044 /* The name does not name a template. */
9045 cp_parser_error (parser, "expected template-name");
9046 return error_mark_node;
9050 /* If DECL is dependent, and refers to a function, then just return
9051 its name; we will look it up again during template instantiation. */
9052 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9054 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9055 if (TYPE_P (scope) && dependent_type_p (scope))
9056 return identifier;
9059 return decl;
9062 /* Parse a template-argument-list.
9064 template-argument-list:
9065 template-argument
9066 template-argument-list , template-argument
9068 Returns a TREE_VEC containing the arguments. */
9070 static tree
9071 cp_parser_template_argument_list (cp_parser* parser)
9073 tree fixed_args[10];
9074 unsigned n_args = 0;
9075 unsigned alloced = 10;
9076 tree *arg_ary = fixed_args;
9077 tree vec;
9078 bool saved_in_template_argument_list_p;
9079 bool saved_ice_p;
9080 bool saved_non_ice_p;
9082 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9083 parser->in_template_argument_list_p = true;
9084 /* Even if the template-id appears in an integral
9085 constant-expression, the contents of the argument list do
9086 not. */
9087 saved_ice_p = parser->integral_constant_expression_p;
9088 parser->integral_constant_expression_p = false;
9089 saved_non_ice_p = parser->non_integral_constant_expression_p;
9090 parser->non_integral_constant_expression_p = false;
9091 /* Parse the arguments. */
9094 tree argument;
9096 if (n_args)
9097 /* Consume the comma. */
9098 cp_lexer_consume_token (parser->lexer);
9100 /* Parse the template-argument. */
9101 argument = cp_parser_template_argument (parser);
9102 if (n_args == alloced)
9104 alloced *= 2;
9106 if (arg_ary == fixed_args)
9108 arg_ary = xmalloc (sizeof (tree) * alloced);
9109 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9111 else
9112 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
9114 arg_ary[n_args++] = argument;
9116 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9118 vec = make_tree_vec (n_args);
9120 while (n_args--)
9121 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9123 if (arg_ary != fixed_args)
9124 free (arg_ary);
9125 parser->non_integral_constant_expression_p = saved_non_ice_p;
9126 parser->integral_constant_expression_p = saved_ice_p;
9127 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9128 return vec;
9131 /* Parse a template-argument.
9133 template-argument:
9134 assignment-expression
9135 type-id
9136 id-expression
9138 The representation is that of an assignment-expression, type-id, or
9139 id-expression -- except that the qualified id-expression is
9140 evaluated, so that the value returned is either a DECL or an
9141 OVERLOAD.
9143 Although the standard says "assignment-expression", it forbids
9144 throw-expressions or assignments in the template argument.
9145 Therefore, we use "conditional-expression" instead. */
9147 static tree
9148 cp_parser_template_argument (cp_parser* parser)
9150 tree argument;
9151 bool template_p;
9152 bool address_p;
9153 bool maybe_type_id = false;
9154 cp_token *token;
9155 cp_id_kind idk;
9157 /* There's really no way to know what we're looking at, so we just
9158 try each alternative in order.
9160 [temp.arg]
9162 In a template-argument, an ambiguity between a type-id and an
9163 expression is resolved to a type-id, regardless of the form of
9164 the corresponding template-parameter.
9166 Therefore, we try a type-id first. */
9167 cp_parser_parse_tentatively (parser);
9168 argument = cp_parser_type_id (parser);
9169 /* If there was no error parsing the type-id but the next token is a '>>',
9170 we probably found a typo for '> >'. But there are type-id which are
9171 also valid expressions. For instance:
9173 struct X { int operator >> (int); };
9174 template <int V> struct Foo {};
9175 Foo<X () >> 5> r;
9177 Here 'X()' is a valid type-id of a function type, but the user just
9178 wanted to write the expression "X() >> 5". Thus, we remember that we
9179 found a valid type-id, but we still try to parse the argument as an
9180 expression to see what happens. */
9181 if (!cp_parser_error_occurred (parser)
9182 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9184 maybe_type_id = true;
9185 cp_parser_abort_tentative_parse (parser);
9187 else
9189 /* If the next token isn't a `,' or a `>', then this argument wasn't
9190 really finished. This means that the argument is not a valid
9191 type-id. */
9192 if (!cp_parser_next_token_ends_template_argument_p (parser))
9193 cp_parser_error (parser, "expected template-argument");
9194 /* If that worked, we're done. */
9195 if (cp_parser_parse_definitely (parser))
9196 return argument;
9198 /* We're still not sure what the argument will be. */
9199 cp_parser_parse_tentatively (parser);
9200 /* Try a template. */
9201 argument = cp_parser_id_expression (parser,
9202 /*template_keyword_p=*/false,
9203 /*check_dependency_p=*/true,
9204 &template_p,
9205 /*declarator_p=*/false);
9206 /* If the next token isn't a `,' or a `>', then this argument wasn't
9207 really finished. */
9208 if (!cp_parser_next_token_ends_template_argument_p (parser))
9209 cp_parser_error (parser, "expected template-argument");
9210 if (!cp_parser_error_occurred (parser))
9212 /* Figure out what is being referred to. If the id-expression
9213 was for a class template specialization, then we will have a
9214 TYPE_DECL at this point. There is no need to do name lookup
9215 at this point in that case. */
9216 if (TREE_CODE (argument) != TYPE_DECL)
9217 argument = cp_parser_lookup_name (parser, argument,
9218 none_type,
9219 /*is_template=*/template_p,
9220 /*is_namespace=*/false,
9221 /*check_dependency=*/true,
9222 /*ambiguous_decls=*/NULL);
9223 if (TREE_CODE (argument) != TEMPLATE_DECL
9224 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9225 cp_parser_error (parser, "expected template-name");
9227 if (cp_parser_parse_definitely (parser))
9228 return argument;
9229 /* It must be a non-type argument. There permitted cases are given
9230 in [temp.arg.nontype]:
9232 -- an integral constant-expression of integral or enumeration
9233 type; or
9235 -- the name of a non-type template-parameter; or
9237 -- the name of an object or function with external linkage...
9239 -- the address of an object or function with external linkage...
9241 -- a pointer to member... */
9242 /* Look for a non-type template parameter. */
9243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9245 cp_parser_parse_tentatively (parser);
9246 argument = cp_parser_primary_expression (parser,
9247 /*adress_p=*/false,
9248 /*cast_p=*/false,
9249 /*template_arg_p=*/true,
9250 &idk);
9251 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9252 || !cp_parser_next_token_ends_template_argument_p (parser))
9253 cp_parser_simulate_error (parser);
9254 if (cp_parser_parse_definitely (parser))
9255 return argument;
9258 /* If the next token is "&", the argument must be the address of an
9259 object or function with external linkage. */
9260 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9261 if (address_p)
9262 cp_lexer_consume_token (parser->lexer);
9263 /* See if we might have an id-expression. */
9264 token = cp_lexer_peek_token (parser->lexer);
9265 if (token->type == CPP_NAME
9266 || token->keyword == RID_OPERATOR
9267 || token->type == CPP_SCOPE
9268 || token->type == CPP_TEMPLATE_ID
9269 || token->type == CPP_NESTED_NAME_SPECIFIER)
9271 cp_parser_parse_tentatively (parser);
9272 argument = cp_parser_primary_expression (parser,
9273 address_p,
9274 /*cast_p=*/false,
9275 /*template_arg_p=*/true,
9276 &idk);
9277 if (cp_parser_error_occurred (parser)
9278 || !cp_parser_next_token_ends_template_argument_p (parser))
9279 cp_parser_abort_tentative_parse (parser);
9280 else
9282 if (TREE_CODE (argument) == INDIRECT_REF)
9284 gcc_assert (REFERENCE_REF_P (argument));
9285 argument = TREE_OPERAND (argument, 0);
9288 if (TREE_CODE (argument) == BASELINK)
9289 /* We don't need the information about what class was used
9290 to name the overloaded functions. */
9291 argument = BASELINK_FUNCTIONS (argument);
9293 if (TREE_CODE (argument) == VAR_DECL)
9295 /* A variable without external linkage might still be a
9296 valid constant-expression, so no error is issued here
9297 if the external-linkage check fails. */
9298 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9299 cp_parser_simulate_error (parser);
9301 else if (is_overloaded_fn (argument))
9302 /* All overloaded functions are allowed; if the external
9303 linkage test does not pass, an error will be issued
9304 later. */
9306 else if (address_p
9307 && (TREE_CODE (argument) == OFFSET_REF
9308 || TREE_CODE (argument) == SCOPE_REF))
9309 /* A pointer-to-member. */
9311 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9313 else
9314 cp_parser_simulate_error (parser);
9316 if (cp_parser_parse_definitely (parser))
9318 if (address_p)
9319 argument = build_x_unary_op (ADDR_EXPR, argument);
9320 return argument;
9324 /* If the argument started with "&", there are no other valid
9325 alternatives at this point. */
9326 if (address_p)
9328 cp_parser_error (parser, "invalid non-type template argument");
9329 return error_mark_node;
9332 /* If the argument wasn't successfully parsed as a type-id followed
9333 by '>>', the argument can only be a constant expression now.
9334 Otherwise, we try parsing the constant-expression tentatively,
9335 because the argument could really be a type-id. */
9336 if (maybe_type_id)
9337 cp_parser_parse_tentatively (parser);
9338 argument = cp_parser_constant_expression (parser,
9339 /*allow_non_constant_p=*/false,
9340 /*non_constant_p=*/NULL);
9341 argument = fold_non_dependent_expr (argument);
9342 if (!maybe_type_id)
9343 return argument;
9344 if (!cp_parser_next_token_ends_template_argument_p (parser))
9345 cp_parser_error (parser, "expected template-argument");
9346 if (cp_parser_parse_definitely (parser))
9347 return argument;
9348 /* We did our best to parse the argument as a non type-id, but that
9349 was the only alternative that matched (albeit with a '>' after
9350 it). We can assume it's just a typo from the user, and a
9351 diagnostic will then be issued. */
9352 return cp_parser_type_id (parser);
9355 /* Parse an explicit-instantiation.
9357 explicit-instantiation:
9358 template declaration
9360 Although the standard says `declaration', what it really means is:
9362 explicit-instantiation:
9363 template decl-specifier-seq [opt] declarator [opt] ;
9365 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9366 supposed to be allowed. A defect report has been filed about this
9367 issue.
9369 GNU Extension:
9371 explicit-instantiation:
9372 storage-class-specifier template
9373 decl-specifier-seq [opt] declarator [opt] ;
9374 function-specifier template
9375 decl-specifier-seq [opt] declarator [opt] ; */
9377 static void
9378 cp_parser_explicit_instantiation (cp_parser* parser)
9380 int declares_class_or_enum;
9381 cp_decl_specifier_seq decl_specifiers;
9382 tree extension_specifier = NULL_TREE;
9384 /* Look for an (optional) storage-class-specifier or
9385 function-specifier. */
9386 if (cp_parser_allow_gnu_extensions_p (parser))
9388 extension_specifier
9389 = cp_parser_storage_class_specifier_opt (parser);
9390 if (!extension_specifier)
9391 extension_specifier
9392 = cp_parser_function_specifier_opt (parser,
9393 /*decl_specs=*/NULL);
9396 /* Look for the `template' keyword. */
9397 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9398 /* Let the front end know that we are processing an explicit
9399 instantiation. */
9400 begin_explicit_instantiation ();
9401 /* [temp.explicit] says that we are supposed to ignore access
9402 control while processing explicit instantiation directives. */
9403 push_deferring_access_checks (dk_no_check);
9404 /* Parse a decl-specifier-seq. */
9405 cp_parser_decl_specifier_seq (parser,
9406 CP_PARSER_FLAGS_OPTIONAL,
9407 &decl_specifiers,
9408 &declares_class_or_enum);
9409 /* If there was exactly one decl-specifier, and it declared a class,
9410 and there's no declarator, then we have an explicit type
9411 instantiation. */
9412 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9414 tree type;
9416 type = check_tag_decl (&decl_specifiers);
9417 /* Turn access control back on for names used during
9418 template instantiation. */
9419 pop_deferring_access_checks ();
9420 if (type)
9421 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9423 else
9425 cp_declarator *declarator;
9426 tree decl;
9428 /* Parse the declarator. */
9429 declarator
9430 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9431 /*ctor_dtor_or_conv_p=*/NULL,
9432 /*parenthesized_p=*/NULL,
9433 /*member_p=*/false);
9434 if (declares_class_or_enum & 2)
9435 cp_parser_check_for_definition_in_return_type (declarator,
9436 decl_specifiers.type);
9437 if (declarator != cp_error_declarator)
9439 decl = grokdeclarator (declarator, &decl_specifiers,
9440 NORMAL, 0, NULL);
9441 /* Turn access control back on for names used during
9442 template instantiation. */
9443 pop_deferring_access_checks ();
9444 /* Do the explicit instantiation. */
9445 do_decl_instantiation (decl, extension_specifier);
9447 else
9449 pop_deferring_access_checks ();
9450 /* Skip the body of the explicit instantiation. */
9451 cp_parser_skip_to_end_of_statement (parser);
9454 /* We're done with the instantiation. */
9455 end_explicit_instantiation ();
9457 cp_parser_consume_semicolon_at_end_of_statement (parser);
9460 /* Parse an explicit-specialization.
9462 explicit-specialization:
9463 template < > declaration
9465 Although the standard says `declaration', what it really means is:
9467 explicit-specialization:
9468 template <> decl-specifier [opt] init-declarator [opt] ;
9469 template <> function-definition
9470 template <> explicit-specialization
9471 template <> template-declaration */
9473 static void
9474 cp_parser_explicit_specialization (cp_parser* parser)
9476 bool need_lang_pop;
9477 /* Look for the `template' keyword. */
9478 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9479 /* Look for the `<'. */
9480 cp_parser_require (parser, CPP_LESS, "`<'");
9481 /* Look for the `>'. */
9482 cp_parser_require (parser, CPP_GREATER, "`>'");
9483 /* We have processed another parameter list. */
9484 ++parser->num_template_parameter_lists;
9485 /* [temp]
9487 A template ... explicit specialization ... shall not have C
9488 linkage. */
9489 if (current_lang_name == lang_name_c)
9491 error ("template specialization with C linkage");
9492 /* Give it C++ linkage to avoid confusing other parts of the
9493 front end. */
9494 push_lang_context (lang_name_cplusplus);
9495 need_lang_pop = true;
9497 else
9498 need_lang_pop = false;
9499 /* Let the front end know that we are beginning a specialization. */
9500 begin_specialization ();
9501 /* If the next keyword is `template', we need to figure out whether
9502 or not we're looking a template-declaration. */
9503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9505 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9506 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9507 cp_parser_template_declaration_after_export (parser,
9508 /*member_p=*/false);
9509 else
9510 cp_parser_explicit_specialization (parser);
9512 else
9513 /* Parse the dependent declaration. */
9514 cp_parser_single_declaration (parser,
9515 /*checks=*/NULL_TREE,
9516 /*member_p=*/false,
9517 /*friend_p=*/NULL);
9518 /* We're done with the specialization. */
9519 end_specialization ();
9520 /* For the erroneous case of a template with C linkage, we pushed an
9521 implicit C++ linkage scope; exit that scope now. */
9522 if (need_lang_pop)
9523 pop_lang_context ();
9524 /* We're done with this parameter list. */
9525 --parser->num_template_parameter_lists;
9528 /* Parse a type-specifier.
9530 type-specifier:
9531 simple-type-specifier
9532 class-specifier
9533 enum-specifier
9534 elaborated-type-specifier
9535 cv-qualifier
9537 GNU Extension:
9539 type-specifier:
9540 __complex__
9542 Returns a representation of the type-specifier. For a
9543 class-specifier, enum-specifier, or elaborated-type-specifier, a
9544 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9546 The parser flags FLAGS is used to control type-specifier parsing.
9548 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9549 in a decl-specifier-seq.
9551 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9552 class-specifier, enum-specifier, or elaborated-type-specifier, then
9553 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9554 if a type is declared; 2 if it is defined. Otherwise, it is set to
9555 zero.
9557 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9558 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9559 is set to FALSE. */
9561 static tree
9562 cp_parser_type_specifier (cp_parser* parser,
9563 cp_parser_flags flags,
9564 cp_decl_specifier_seq *decl_specs,
9565 bool is_declaration,
9566 int* declares_class_or_enum,
9567 bool* is_cv_qualifier)
9569 tree type_spec = NULL_TREE;
9570 cp_token *token;
9571 enum rid keyword;
9572 cp_decl_spec ds = ds_last;
9574 /* Assume this type-specifier does not declare a new type. */
9575 if (declares_class_or_enum)
9576 *declares_class_or_enum = 0;
9577 /* And that it does not specify a cv-qualifier. */
9578 if (is_cv_qualifier)
9579 *is_cv_qualifier = false;
9580 /* Peek at the next token. */
9581 token = cp_lexer_peek_token (parser->lexer);
9583 /* If we're looking at a keyword, we can use that to guide the
9584 production we choose. */
9585 keyword = token->keyword;
9586 switch (keyword)
9588 case RID_ENUM:
9589 /* 'enum' [identifier] '{' introduces an enum-specifier;
9590 'enum' <anything else> introduces an elaborated-type-specifier. */
9591 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9592 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9593 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9594 == CPP_OPEN_BRACE))
9596 if (parser->num_template_parameter_lists)
9598 error ("template declaration of %qs", "enum");
9599 cp_parser_skip_to_end_of_block_or_statement (parser);
9600 type_spec = error_mark_node;
9602 else
9603 type_spec = cp_parser_enum_specifier (parser);
9605 if (declares_class_or_enum)
9606 *declares_class_or_enum = 2;
9607 if (decl_specs)
9608 cp_parser_set_decl_spec_type (decl_specs,
9609 type_spec,
9610 /*user_defined_p=*/true);
9611 return type_spec;
9613 else
9614 goto elaborated_type_specifier;
9616 /* Any of these indicate either a class-specifier, or an
9617 elaborated-type-specifier. */
9618 case RID_CLASS:
9619 case RID_STRUCT:
9620 case RID_UNION:
9621 /* Parse tentatively so that we can back up if we don't find a
9622 class-specifier. */
9623 cp_parser_parse_tentatively (parser);
9624 /* Look for the class-specifier. */
9625 type_spec = cp_parser_class_specifier (parser);
9626 /* If that worked, we're done. */
9627 if (cp_parser_parse_definitely (parser))
9629 if (declares_class_or_enum)
9630 *declares_class_or_enum = 2;
9631 if (decl_specs)
9632 cp_parser_set_decl_spec_type (decl_specs,
9633 type_spec,
9634 /*user_defined_p=*/true);
9635 return type_spec;
9638 /* Fall through. */
9639 elaborated_type_specifier:
9640 /* We're declaring (not defining) a class or enum. */
9641 if (declares_class_or_enum)
9642 *declares_class_or_enum = 1;
9644 /* Fall through. */
9645 case RID_TYPENAME:
9646 /* Look for an elaborated-type-specifier. */
9647 type_spec
9648 = (cp_parser_elaborated_type_specifier
9649 (parser,
9650 decl_specs && decl_specs->specs[(int) ds_friend],
9651 is_declaration));
9652 if (decl_specs)
9653 cp_parser_set_decl_spec_type (decl_specs,
9654 type_spec,
9655 /*user_defined_p=*/true);
9656 return type_spec;
9658 case RID_CONST:
9659 ds = ds_const;
9660 if (is_cv_qualifier)
9661 *is_cv_qualifier = true;
9662 break;
9664 case RID_VOLATILE:
9665 ds = ds_volatile;
9666 if (is_cv_qualifier)
9667 *is_cv_qualifier = true;
9668 break;
9670 case RID_RESTRICT:
9671 ds = ds_restrict;
9672 if (is_cv_qualifier)
9673 *is_cv_qualifier = true;
9674 break;
9676 case RID_COMPLEX:
9677 /* The `__complex__' keyword is a GNU extension. */
9678 ds = ds_complex;
9679 break;
9681 default:
9682 break;
9685 /* Handle simple keywords. */
9686 if (ds != ds_last)
9688 if (decl_specs)
9690 ++decl_specs->specs[(int)ds];
9691 decl_specs->any_specifiers_p = true;
9693 return cp_lexer_consume_token (parser->lexer)->value;
9696 /* If we do not already have a type-specifier, assume we are looking
9697 at a simple-type-specifier. */
9698 type_spec = cp_parser_simple_type_specifier (parser,
9699 decl_specs,
9700 flags);
9702 /* If we didn't find a type-specifier, and a type-specifier was not
9703 optional in this context, issue an error message. */
9704 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9706 cp_parser_error (parser, "expected type specifier");
9707 return error_mark_node;
9710 return type_spec;
9713 /* Parse a simple-type-specifier.
9715 simple-type-specifier:
9716 :: [opt] nested-name-specifier [opt] type-name
9717 :: [opt] nested-name-specifier template template-id
9718 char
9719 wchar_t
9720 bool
9721 short
9723 long
9724 signed
9725 unsigned
9726 float
9727 double
9728 void
9730 GNU Extension:
9732 simple-type-specifier:
9733 __typeof__ unary-expression
9734 __typeof__ ( type-id )
9736 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9737 appropriately updated. */
9739 static tree
9740 cp_parser_simple_type_specifier (cp_parser* parser,
9741 cp_decl_specifier_seq *decl_specs,
9742 cp_parser_flags flags)
9744 tree type = NULL_TREE;
9745 cp_token *token;
9747 /* Peek at the next token. */
9748 token = cp_lexer_peek_token (parser->lexer);
9750 /* If we're looking at a keyword, things are easy. */
9751 switch (token->keyword)
9753 case RID_CHAR:
9754 if (decl_specs)
9755 decl_specs->explicit_char_p = true;
9756 type = char_type_node;
9757 break;
9758 case RID_WCHAR:
9759 type = wchar_type_node;
9760 break;
9761 case RID_BOOL:
9762 type = boolean_type_node;
9763 break;
9764 case RID_SHORT:
9765 if (decl_specs)
9766 ++decl_specs->specs[(int) ds_short];
9767 type = short_integer_type_node;
9768 break;
9769 case RID_INT:
9770 if (decl_specs)
9771 decl_specs->explicit_int_p = true;
9772 type = integer_type_node;
9773 break;
9774 case RID_LONG:
9775 if (decl_specs)
9776 ++decl_specs->specs[(int) ds_long];
9777 type = long_integer_type_node;
9778 break;
9779 case RID_SIGNED:
9780 if (decl_specs)
9781 ++decl_specs->specs[(int) ds_signed];
9782 type = integer_type_node;
9783 break;
9784 case RID_UNSIGNED:
9785 if (decl_specs)
9786 ++decl_specs->specs[(int) ds_unsigned];
9787 type = unsigned_type_node;
9788 break;
9789 case RID_FLOAT:
9790 type = float_type_node;
9791 break;
9792 case RID_DOUBLE:
9793 type = double_type_node;
9794 break;
9795 case RID_VOID:
9796 type = void_type_node;
9797 break;
9799 case RID_TYPEOF:
9800 /* Consume the `typeof' token. */
9801 cp_lexer_consume_token (parser->lexer);
9802 /* Parse the operand to `typeof'. */
9803 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9804 /* If it is not already a TYPE, take its type. */
9805 if (!TYPE_P (type))
9806 type = finish_typeof (type);
9808 if (decl_specs)
9809 cp_parser_set_decl_spec_type (decl_specs, type,
9810 /*user_defined_p=*/true);
9812 return type;
9814 default:
9815 break;
9818 /* If the type-specifier was for a built-in type, we're done. */
9819 if (type)
9821 tree id;
9823 /* Record the type. */
9824 if (decl_specs
9825 && (token->keyword != RID_SIGNED
9826 && token->keyword != RID_UNSIGNED
9827 && token->keyword != RID_SHORT
9828 && token->keyword != RID_LONG))
9829 cp_parser_set_decl_spec_type (decl_specs,
9830 type,
9831 /*user_defined=*/false);
9832 if (decl_specs)
9833 decl_specs->any_specifiers_p = true;
9835 /* Consume the token. */
9836 id = cp_lexer_consume_token (parser->lexer)->value;
9838 /* There is no valid C++ program where a non-template type is
9839 followed by a "<". That usually indicates that the user thought
9840 that the type was a template. */
9841 cp_parser_check_for_invalid_template_id (parser, type);
9843 return TYPE_NAME (type);
9846 /* The type-specifier must be a user-defined type. */
9847 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9849 bool qualified_p;
9850 bool global_p;
9852 /* Don't gobble tokens or issue error messages if this is an
9853 optional type-specifier. */
9854 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9855 cp_parser_parse_tentatively (parser);
9857 /* Look for the optional `::' operator. */
9858 global_p
9859 = (cp_parser_global_scope_opt (parser,
9860 /*current_scope_valid_p=*/false)
9861 != NULL_TREE);
9862 /* Look for the nested-name specifier. */
9863 qualified_p
9864 = (cp_parser_nested_name_specifier_opt (parser,
9865 /*typename_keyword_p=*/false,
9866 /*check_dependency_p=*/true,
9867 /*type_p=*/false,
9868 /*is_declaration=*/false)
9869 != NULL_TREE);
9870 /* If we have seen a nested-name-specifier, and the next token
9871 is `template', then we are using the template-id production. */
9872 if (parser->scope
9873 && cp_parser_optional_template_keyword (parser))
9875 /* Look for the template-id. */
9876 type = cp_parser_template_id (parser,
9877 /*template_keyword_p=*/true,
9878 /*check_dependency_p=*/true,
9879 /*is_declaration=*/false);
9880 /* If the template-id did not name a type, we are out of
9881 luck. */
9882 if (TREE_CODE (type) != TYPE_DECL)
9884 cp_parser_error (parser, "expected template-id for type");
9885 type = NULL_TREE;
9888 /* Otherwise, look for a type-name. */
9889 else
9890 type = cp_parser_type_name (parser);
9891 /* Keep track of all name-lookups performed in class scopes. */
9892 if (type
9893 && !global_p
9894 && !qualified_p
9895 && TREE_CODE (type) == TYPE_DECL
9896 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9897 maybe_note_name_used_in_class (DECL_NAME (type), type);
9898 /* If it didn't work out, we don't have a TYPE. */
9899 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9900 && !cp_parser_parse_definitely (parser))
9901 type = NULL_TREE;
9902 if (type && decl_specs)
9903 cp_parser_set_decl_spec_type (decl_specs, type,
9904 /*user_defined=*/true);
9907 /* If we didn't get a type-name, issue an error message. */
9908 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9910 cp_parser_error (parser, "expected type-name");
9911 return error_mark_node;
9914 /* There is no valid C++ program where a non-template type is
9915 followed by a "<". That usually indicates that the user thought
9916 that the type was a template. */
9917 if (type && type != error_mark_node)
9919 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9920 If it is, then the '<'...'>' enclose protocol names rather than
9921 template arguments, and so everything is fine. */
9922 if (c_dialect_objc ()
9923 && (objc_is_id (type) || objc_is_class_name (type)))
9925 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9926 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9928 /* Clobber the "unqualified" type previously entered into
9929 DECL_SPECS with the new, improved protocol-qualified version. */
9930 if (decl_specs)
9931 decl_specs->type = qual_type;
9933 return qual_type;
9936 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9939 return type;
9942 /* Parse a type-name.
9944 type-name:
9945 class-name
9946 enum-name
9947 typedef-name
9949 enum-name:
9950 identifier
9952 typedef-name:
9953 identifier
9955 Returns a TYPE_DECL for the type. */
9957 static tree
9958 cp_parser_type_name (cp_parser* parser)
9960 tree type_decl;
9961 tree identifier;
9963 /* We can't know yet whether it is a class-name or not. */
9964 cp_parser_parse_tentatively (parser);
9965 /* Try a class-name. */
9966 type_decl = cp_parser_class_name (parser,
9967 /*typename_keyword_p=*/false,
9968 /*template_keyword_p=*/false,
9969 none_type,
9970 /*check_dependency_p=*/true,
9971 /*class_head_p=*/false,
9972 /*is_declaration=*/false);
9973 /* If it's not a class-name, keep looking. */
9974 if (!cp_parser_parse_definitely (parser))
9976 /* It must be a typedef-name or an enum-name. */
9977 identifier = cp_parser_identifier (parser);
9978 if (identifier == error_mark_node)
9979 return error_mark_node;
9981 /* Look up the type-name. */
9982 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9984 if (TREE_CODE (type_decl) != TYPE_DECL
9985 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9987 /* See if this is an Objective-C type. */
9988 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9989 tree type = objc_get_protocol_qualified_type (identifier, protos);
9990 if (type)
9991 type_decl = TYPE_NAME (type);
9994 /* Issue an error if we did not find a type-name. */
9995 if (TREE_CODE (type_decl) != TYPE_DECL)
9997 if (!cp_parser_simulate_error (parser))
9998 cp_parser_name_lookup_error (parser, identifier, type_decl,
9999 "is not a type");
10000 type_decl = error_mark_node;
10002 /* Remember that the name was used in the definition of the
10003 current class so that we can check later to see if the
10004 meaning would have been different after the class was
10005 entirely defined. */
10006 else if (type_decl != error_mark_node
10007 && !parser->scope)
10008 maybe_note_name_used_in_class (identifier, type_decl);
10011 return type_decl;
10015 /* Parse an elaborated-type-specifier. Note that the grammar given
10016 here incorporates the resolution to DR68.
10018 elaborated-type-specifier:
10019 class-key :: [opt] nested-name-specifier [opt] identifier
10020 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10021 enum :: [opt] nested-name-specifier [opt] identifier
10022 typename :: [opt] nested-name-specifier identifier
10023 typename :: [opt] nested-name-specifier template [opt]
10024 template-id
10026 GNU extension:
10028 elaborated-type-specifier:
10029 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10030 class-key attributes :: [opt] nested-name-specifier [opt]
10031 template [opt] template-id
10032 enum attributes :: [opt] nested-name-specifier [opt] identifier
10034 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10035 declared `friend'. If IS_DECLARATION is TRUE, then this
10036 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10037 something is being declared.
10039 Returns the TYPE specified. */
10041 static tree
10042 cp_parser_elaborated_type_specifier (cp_parser* parser,
10043 bool is_friend,
10044 bool is_declaration)
10046 enum tag_types tag_type;
10047 tree identifier;
10048 tree type = NULL_TREE;
10049 tree attributes = NULL_TREE;
10051 /* See if we're looking at the `enum' keyword. */
10052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10054 /* Consume the `enum' token. */
10055 cp_lexer_consume_token (parser->lexer);
10056 /* Remember that it's an enumeration type. */
10057 tag_type = enum_type;
10058 /* Parse the attributes. */
10059 attributes = cp_parser_attributes_opt (parser);
10061 /* Or, it might be `typename'. */
10062 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10063 RID_TYPENAME))
10065 /* Consume the `typename' token. */
10066 cp_lexer_consume_token (parser->lexer);
10067 /* Remember that it's a `typename' type. */
10068 tag_type = typename_type;
10069 /* The `typename' keyword is only allowed in templates. */
10070 if (!processing_template_decl)
10071 pedwarn ("using %<typename%> outside of template");
10073 /* Otherwise it must be a class-key. */
10074 else
10076 tag_type = cp_parser_class_key (parser);
10077 if (tag_type == none_type)
10078 return error_mark_node;
10079 /* Parse the attributes. */
10080 attributes = cp_parser_attributes_opt (parser);
10083 /* Look for the `::' operator. */
10084 cp_parser_global_scope_opt (parser,
10085 /*current_scope_valid_p=*/false);
10086 /* Look for the nested-name-specifier. */
10087 if (tag_type == typename_type)
10089 if (!cp_parser_nested_name_specifier (parser,
10090 /*typename_keyword_p=*/true,
10091 /*check_dependency_p=*/true,
10092 /*type_p=*/true,
10093 is_declaration))
10094 return error_mark_node;
10096 else
10097 /* Even though `typename' is not present, the proposed resolution
10098 to Core Issue 180 says that in `class A<T>::B', `B' should be
10099 considered a type-name, even if `A<T>' is dependent. */
10100 cp_parser_nested_name_specifier_opt (parser,
10101 /*typename_keyword_p=*/true,
10102 /*check_dependency_p=*/true,
10103 /*type_p=*/true,
10104 is_declaration);
10105 /* For everything but enumeration types, consider a template-id. */
10106 if (tag_type != enum_type)
10108 bool template_p = false;
10109 tree decl;
10111 /* Allow the `template' keyword. */
10112 template_p = cp_parser_optional_template_keyword (parser);
10113 /* If we didn't see `template', we don't know if there's a
10114 template-id or not. */
10115 if (!template_p)
10116 cp_parser_parse_tentatively (parser);
10117 /* Parse the template-id. */
10118 decl = cp_parser_template_id (parser, template_p,
10119 /*check_dependency_p=*/true,
10120 is_declaration);
10121 /* If we didn't find a template-id, look for an ordinary
10122 identifier. */
10123 if (!template_p && !cp_parser_parse_definitely (parser))
10125 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10126 in effect, then we must assume that, upon instantiation, the
10127 template will correspond to a class. */
10128 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10129 && tag_type == typename_type)
10130 type = make_typename_type (parser->scope, decl,
10131 typename_type,
10132 /*complain=*/1);
10133 else
10134 type = TREE_TYPE (decl);
10137 /* For an enumeration type, consider only a plain identifier. */
10138 if (!type)
10140 identifier = cp_parser_identifier (parser);
10142 if (identifier == error_mark_node)
10144 parser->scope = NULL_TREE;
10145 return error_mark_node;
10148 /* For a `typename', we needn't call xref_tag. */
10149 if (tag_type == typename_type
10150 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10151 return cp_parser_make_typename_type (parser, parser->scope,
10152 identifier);
10153 /* Look up a qualified name in the usual way. */
10154 if (parser->scope)
10156 tree decl;
10158 decl = cp_parser_lookup_name (parser, identifier,
10159 tag_type,
10160 /*is_template=*/false,
10161 /*is_namespace=*/false,
10162 /*check_dependency=*/true,
10163 /*ambiguous_decls=*/NULL);
10165 /* If we are parsing friend declaration, DECL may be a
10166 TEMPLATE_DECL tree node here. However, we need to check
10167 whether this TEMPLATE_DECL results in valid code. Consider
10168 the following example:
10170 namespace N {
10171 template <class T> class C {};
10173 class X {
10174 template <class T> friend class N::C; // #1, valid code
10176 template <class T> class Y {
10177 friend class N::C; // #2, invalid code
10180 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10181 name lookup of `N::C'. We see that friend declaration must
10182 be template for the code to be valid. Note that
10183 processing_template_decl does not work here since it is
10184 always 1 for the above two cases. */
10186 decl = (cp_parser_maybe_treat_template_as_class
10187 (decl, /*tag_name_p=*/is_friend
10188 && parser->num_template_parameter_lists));
10190 if (TREE_CODE (decl) != TYPE_DECL)
10192 cp_parser_diagnose_invalid_type_name (parser,
10193 parser->scope,
10194 identifier);
10195 return error_mark_node;
10198 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10199 check_elaborated_type_specifier
10200 (tag_type, decl,
10201 (parser->num_template_parameter_lists
10202 || DECL_SELF_REFERENCE_P (decl)));
10204 type = TREE_TYPE (decl);
10206 else
10208 /* An elaborated-type-specifier sometimes introduces a new type and
10209 sometimes names an existing type. Normally, the rule is that it
10210 introduces a new type only if there is not an existing type of
10211 the same name already in scope. For example, given:
10213 struct S {};
10214 void f() { struct S s; }
10216 the `struct S' in the body of `f' is the same `struct S' as in
10217 the global scope; the existing definition is used. However, if
10218 there were no global declaration, this would introduce a new
10219 local class named `S'.
10221 An exception to this rule applies to the following code:
10223 namespace N { struct S; }
10225 Here, the elaborated-type-specifier names a new type
10226 unconditionally; even if there is already an `S' in the
10227 containing scope this declaration names a new type.
10228 This exception only applies if the elaborated-type-specifier
10229 forms the complete declaration:
10231 [class.name]
10233 A declaration consisting solely of `class-key identifier ;' is
10234 either a redeclaration of the name in the current scope or a
10235 forward declaration of the identifier as a class name. It
10236 introduces the name into the current scope.
10238 We are in this situation precisely when the next token is a `;'.
10240 An exception to the exception is that a `friend' declaration does
10241 *not* name a new type; i.e., given:
10243 struct S { friend struct T; };
10245 `T' is not a new type in the scope of `S'.
10247 Also, `new struct S' or `sizeof (struct S)' never results in the
10248 definition of a new type; a new type can only be declared in a
10249 declaration context. */
10251 tag_scope ts;
10252 bool template_p;
10254 if (is_friend)
10255 /* Friends have special name lookup rules. */
10256 ts = ts_within_enclosing_non_class;
10257 else if (is_declaration
10258 && cp_lexer_next_token_is (parser->lexer,
10259 CPP_SEMICOLON))
10260 /* This is a `class-key identifier ;' */
10261 ts = ts_current;
10262 else
10263 ts = ts_global;
10265 /* Warn about attributes. They are ignored. */
10266 if (attributes)
10267 warning (OPT_Wattributes,
10268 "type attributes are honored only at type definition");
10270 template_p =
10271 (parser->num_template_parameter_lists
10272 && (cp_parser_next_token_starts_class_definition_p (parser)
10273 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10274 /* An unqualified name was used to reference this type, so
10275 there were no qualifying templates. */
10276 if (!cp_parser_check_template_parameters (parser,
10277 /*num_templates=*/0))
10278 return error_mark_node;
10279 type = xref_tag (tag_type, identifier, ts, template_p);
10282 if (tag_type != enum_type)
10283 cp_parser_check_class_key (tag_type, type);
10285 /* A "<" cannot follow an elaborated type specifier. If that
10286 happens, the user was probably trying to form a template-id. */
10287 cp_parser_check_for_invalid_template_id (parser, type);
10289 return type;
10292 /* Parse an enum-specifier.
10294 enum-specifier:
10295 enum identifier [opt] { enumerator-list [opt] }
10297 GNU Extensions:
10298 enum identifier [opt] { enumerator-list [opt] } attributes
10300 Returns an ENUM_TYPE representing the enumeration. */
10302 static tree
10303 cp_parser_enum_specifier (cp_parser* parser)
10305 tree identifier;
10306 tree type;
10308 /* Caller guarantees that the current token is 'enum', an identifier
10309 possibly follows, and the token after that is an opening brace.
10310 If we don't have an identifier, fabricate an anonymous name for
10311 the enumeration being defined. */
10312 cp_lexer_consume_token (parser->lexer);
10314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10315 identifier = cp_parser_identifier (parser);
10316 else
10317 identifier = make_anon_name ();
10319 /* Issue an error message if type-definitions are forbidden here. */
10320 if (!cp_parser_check_type_definition (parser))
10321 type = error_mark_node;
10322 else
10323 /* Create the new type. We do this before consuming the opening
10324 brace so the enum will be recorded as being on the line of its
10325 tag (or the 'enum' keyword, if there is no tag). */
10326 type = start_enum (identifier);
10328 /* Consume the opening brace. */
10329 cp_lexer_consume_token (parser->lexer);
10331 if (type == error_mark_node)
10333 cp_parser_skip_to_end_of_block_or_statement (parser);
10334 return error_mark_node;
10337 /* If the next token is not '}', then there are some enumerators. */
10338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10339 cp_parser_enumerator_list (parser, type);
10341 /* Consume the final '}'. */
10342 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10344 /* Look for trailing attributes to apply to this enumeration, and
10345 apply them if appropriate. */
10346 if (cp_parser_allow_gnu_extensions_p (parser))
10348 tree trailing_attr = cp_parser_attributes_opt (parser);
10349 cplus_decl_attributes (&type,
10350 trailing_attr,
10351 (int) ATTR_FLAG_TYPE_IN_PLACE);
10354 /* Finish up the enumeration. */
10355 finish_enum (type);
10357 return type;
10360 /* Parse an enumerator-list. The enumerators all have the indicated
10361 TYPE.
10363 enumerator-list:
10364 enumerator-definition
10365 enumerator-list , enumerator-definition */
10367 static void
10368 cp_parser_enumerator_list (cp_parser* parser, tree type)
10370 while (true)
10372 /* Parse an enumerator-definition. */
10373 cp_parser_enumerator_definition (parser, type);
10375 /* If the next token is not a ',', we've reached the end of
10376 the list. */
10377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10378 break;
10379 /* Otherwise, consume the `,' and keep going. */
10380 cp_lexer_consume_token (parser->lexer);
10381 /* If the next token is a `}', there is a trailing comma. */
10382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10384 if (pedantic && !in_system_header)
10385 pedwarn ("comma at end of enumerator list");
10386 break;
10391 /* Parse an enumerator-definition. The enumerator has the indicated
10392 TYPE.
10394 enumerator-definition:
10395 enumerator
10396 enumerator = constant-expression
10398 enumerator:
10399 identifier */
10401 static void
10402 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10404 tree identifier;
10405 tree value;
10407 /* Look for the identifier. */
10408 identifier = cp_parser_identifier (parser);
10409 if (identifier == error_mark_node)
10410 return;
10412 /* If the next token is an '=', then there is an explicit value. */
10413 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10415 /* Consume the `=' token. */
10416 cp_lexer_consume_token (parser->lexer);
10417 /* Parse the value. */
10418 value = cp_parser_constant_expression (parser,
10419 /*allow_non_constant_p=*/false,
10420 NULL);
10422 else
10423 value = NULL_TREE;
10425 /* Create the enumerator. */
10426 build_enumerator (identifier, value, type);
10429 /* Parse a namespace-name.
10431 namespace-name:
10432 original-namespace-name
10433 namespace-alias
10435 Returns the NAMESPACE_DECL for the namespace. */
10437 static tree
10438 cp_parser_namespace_name (cp_parser* parser)
10440 tree identifier;
10441 tree namespace_decl;
10443 /* Get the name of the namespace. */
10444 identifier = cp_parser_identifier (parser);
10445 if (identifier == error_mark_node)
10446 return error_mark_node;
10448 /* Look up the identifier in the currently active scope. Look only
10449 for namespaces, due to:
10451 [basic.lookup.udir]
10453 When looking up a namespace-name in a using-directive or alias
10454 definition, only namespace names are considered.
10456 And:
10458 [basic.lookup.qual]
10460 During the lookup of a name preceding the :: scope resolution
10461 operator, object, function, and enumerator names are ignored.
10463 (Note that cp_parser_class_or_namespace_name only calls this
10464 function if the token after the name is the scope resolution
10465 operator.) */
10466 namespace_decl = cp_parser_lookup_name (parser, identifier,
10467 none_type,
10468 /*is_template=*/false,
10469 /*is_namespace=*/true,
10470 /*check_dependency=*/true,
10471 /*ambiguous_decls=*/NULL);
10472 /* If it's not a namespace, issue an error. */
10473 if (namespace_decl == error_mark_node
10474 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10476 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10477 error ("%qD is not a namespace-name", identifier);
10478 cp_parser_error (parser, "expected namespace-name");
10479 namespace_decl = error_mark_node;
10482 return namespace_decl;
10485 /* Parse a namespace-definition.
10487 namespace-definition:
10488 named-namespace-definition
10489 unnamed-namespace-definition
10491 named-namespace-definition:
10492 original-namespace-definition
10493 extension-namespace-definition
10495 original-namespace-definition:
10496 namespace identifier { namespace-body }
10498 extension-namespace-definition:
10499 namespace original-namespace-name { namespace-body }
10501 unnamed-namespace-definition:
10502 namespace { namespace-body } */
10504 static void
10505 cp_parser_namespace_definition (cp_parser* parser)
10507 tree identifier;
10509 /* Look for the `namespace' keyword. */
10510 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10512 /* Get the name of the namespace. We do not attempt to distinguish
10513 between an original-namespace-definition and an
10514 extension-namespace-definition at this point. The semantic
10515 analysis routines are responsible for that. */
10516 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10517 identifier = cp_parser_identifier (parser);
10518 else
10519 identifier = NULL_TREE;
10521 /* Look for the `{' to start the namespace. */
10522 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10523 /* Start the namespace. */
10524 push_namespace (identifier);
10525 /* Parse the body of the namespace. */
10526 cp_parser_namespace_body (parser);
10527 /* Finish the namespace. */
10528 pop_namespace ();
10529 /* Look for the final `}'. */
10530 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10533 /* Parse a namespace-body.
10535 namespace-body:
10536 declaration-seq [opt] */
10538 static void
10539 cp_parser_namespace_body (cp_parser* parser)
10541 cp_parser_declaration_seq_opt (parser);
10544 /* Parse a namespace-alias-definition.
10546 namespace-alias-definition:
10547 namespace identifier = qualified-namespace-specifier ; */
10549 static void
10550 cp_parser_namespace_alias_definition (cp_parser* parser)
10552 tree identifier;
10553 tree namespace_specifier;
10555 /* Look for the `namespace' keyword. */
10556 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10557 /* Look for the identifier. */
10558 identifier = cp_parser_identifier (parser);
10559 if (identifier == error_mark_node)
10560 return;
10561 /* Look for the `=' token. */
10562 cp_parser_require (parser, CPP_EQ, "`='");
10563 /* Look for the qualified-namespace-specifier. */
10564 namespace_specifier
10565 = cp_parser_qualified_namespace_specifier (parser);
10566 /* Look for the `;' token. */
10567 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10569 /* Register the alias in the symbol table. */
10570 do_namespace_alias (identifier, namespace_specifier);
10573 /* Parse a qualified-namespace-specifier.
10575 qualified-namespace-specifier:
10576 :: [opt] nested-name-specifier [opt] namespace-name
10578 Returns a NAMESPACE_DECL corresponding to the specified
10579 namespace. */
10581 static tree
10582 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10584 /* Look for the optional `::'. */
10585 cp_parser_global_scope_opt (parser,
10586 /*current_scope_valid_p=*/false);
10588 /* Look for the optional nested-name-specifier. */
10589 cp_parser_nested_name_specifier_opt (parser,
10590 /*typename_keyword_p=*/false,
10591 /*check_dependency_p=*/true,
10592 /*type_p=*/false,
10593 /*is_declaration=*/true);
10595 return cp_parser_namespace_name (parser);
10598 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10599 access declaration.
10601 using-declaration:
10602 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10603 using :: unqualified-id ;
10605 access-declaration:
10606 qualified-id ;
10610 static bool
10611 cp_parser_using_declaration (cp_parser* parser,
10612 bool access_declaration_p)
10614 cp_token *token;
10615 bool typename_p = false;
10616 bool global_scope_p;
10617 tree decl;
10618 tree identifier;
10619 tree qscope;
10621 if (access_declaration_p)
10622 cp_parser_parse_tentatively (parser);
10623 else
10625 /* Look for the `using' keyword. */
10626 cp_parser_require_keyword (parser, RID_USING, "`using'");
10628 /* Peek at the next token. */
10629 token = cp_lexer_peek_token (parser->lexer);
10630 /* See if it's `typename'. */
10631 if (token->keyword == RID_TYPENAME)
10633 /* Remember that we've seen it. */
10634 typename_p = true;
10635 /* Consume the `typename' token. */
10636 cp_lexer_consume_token (parser->lexer);
10640 /* Look for the optional global scope qualification. */
10641 global_scope_p
10642 = (cp_parser_global_scope_opt (parser,
10643 /*current_scope_valid_p=*/false)
10644 != NULL_TREE);
10646 /* If we saw `typename', or didn't see `::', then there must be a
10647 nested-name-specifier present. */
10648 if (typename_p || !global_scope_p)
10649 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10650 /*check_dependency_p=*/true,
10651 /*type_p=*/false,
10652 /*is_declaration=*/true);
10653 /* Otherwise, we could be in either of the two productions. In that
10654 case, treat the nested-name-specifier as optional. */
10655 else
10656 qscope = cp_parser_nested_name_specifier_opt (parser,
10657 /*typename_keyword_p=*/false,
10658 /*check_dependency_p=*/true,
10659 /*type_p=*/false,
10660 /*is_declaration=*/true);
10661 if (!qscope)
10662 qscope = global_namespace;
10664 if (access_declaration_p && cp_parser_error_occurred (parser))
10665 /* Something has already gone wrong; there's no need to parse
10666 further. Since an error has occurred, the return value of
10667 cp_parser_parse_definitely will be false, as required. */
10668 return cp_parser_parse_definitely (parser);
10670 /* Parse the unqualified-id. */
10671 identifier = cp_parser_unqualified_id (parser,
10672 /*template_keyword_p=*/false,
10673 /*check_dependency_p=*/true,
10674 /*declarator_p=*/true);
10676 if (access_declaration_p)
10678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10679 cp_parser_simulate_error (parser);
10680 if (!cp_parser_parse_definitely (parser))
10681 return false;
10684 /* The function we call to handle a using-declaration is different
10685 depending on what scope we are in. */
10686 if (qscope == error_mark_node || identifier == error_mark_node)
10688 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10689 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10690 /* [namespace.udecl]
10692 A using declaration shall not name a template-id. */
10693 error ("a template-id may not appear in a using-declaration");
10694 else
10696 if (at_class_scope_p ())
10698 /* Create the USING_DECL. */
10699 decl = do_class_using_decl (parser->scope, identifier);
10700 /* Add it to the list of members in this class. */
10701 finish_member_declaration (decl);
10703 else
10705 decl = cp_parser_lookup_name_simple (parser, identifier);
10706 if (decl == error_mark_node)
10707 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10708 else if (!at_namespace_scope_p ())
10709 do_local_using_decl (decl, qscope, identifier);
10710 else
10711 do_toplevel_using_decl (decl, qscope, identifier);
10715 /* Look for the final `;'. */
10716 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10718 return true;
10721 /* Parse a using-directive.
10723 using-directive:
10724 using namespace :: [opt] nested-name-specifier [opt]
10725 namespace-name ; */
10727 static void
10728 cp_parser_using_directive (cp_parser* parser)
10730 tree namespace_decl;
10731 tree attribs;
10733 /* Look for the `using' keyword. */
10734 cp_parser_require_keyword (parser, RID_USING, "`using'");
10735 /* And the `namespace' keyword. */
10736 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10737 /* Look for the optional `::' operator. */
10738 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10739 /* And the optional nested-name-specifier. */
10740 cp_parser_nested_name_specifier_opt (parser,
10741 /*typename_keyword_p=*/false,
10742 /*check_dependency_p=*/true,
10743 /*type_p=*/false,
10744 /*is_declaration=*/true);
10745 /* Get the namespace being used. */
10746 namespace_decl = cp_parser_namespace_name (parser);
10747 /* And any specified attributes. */
10748 attribs = cp_parser_attributes_opt (parser);
10749 /* Update the symbol table. */
10750 parse_using_directive (namespace_decl, attribs);
10751 /* Look for the final `;'. */
10752 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10755 /* Parse an asm-definition.
10757 asm-definition:
10758 asm ( string-literal ) ;
10760 GNU Extension:
10762 asm-definition:
10763 asm volatile [opt] ( string-literal ) ;
10764 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10765 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10766 : asm-operand-list [opt] ) ;
10767 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10768 : asm-operand-list [opt]
10769 : asm-operand-list [opt] ) ; */
10771 static void
10772 cp_parser_asm_definition (cp_parser* parser)
10774 tree string;
10775 tree outputs = NULL_TREE;
10776 tree inputs = NULL_TREE;
10777 tree clobbers = NULL_TREE;
10778 tree asm_stmt;
10779 bool volatile_p = false;
10780 bool extended_p = false;
10782 /* Look for the `asm' keyword. */
10783 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10784 /* See if the next token is `volatile'. */
10785 if (cp_parser_allow_gnu_extensions_p (parser)
10786 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10788 /* Remember that we saw the `volatile' keyword. */
10789 volatile_p = true;
10790 /* Consume the token. */
10791 cp_lexer_consume_token (parser->lexer);
10793 /* Look for the opening `('. */
10794 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10795 return;
10796 /* Look for the string. */
10797 string = cp_parser_string_literal (parser, false, false);
10798 if (string == error_mark_node)
10800 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10801 /*consume_paren=*/true);
10802 return;
10805 /* If we're allowing GNU extensions, check for the extended assembly
10806 syntax. Unfortunately, the `:' tokens need not be separated by
10807 a space in C, and so, for compatibility, we tolerate that here
10808 too. Doing that means that we have to treat the `::' operator as
10809 two `:' tokens. */
10810 if (cp_parser_allow_gnu_extensions_p (parser)
10811 && parser->in_function_body
10812 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10813 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10815 bool inputs_p = false;
10816 bool clobbers_p = false;
10818 /* The extended syntax was used. */
10819 extended_p = true;
10821 /* Look for outputs. */
10822 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10824 /* Consume the `:'. */
10825 cp_lexer_consume_token (parser->lexer);
10826 /* Parse the output-operands. */
10827 if (cp_lexer_next_token_is_not (parser->lexer,
10828 CPP_COLON)
10829 && cp_lexer_next_token_is_not (parser->lexer,
10830 CPP_SCOPE)
10831 && cp_lexer_next_token_is_not (parser->lexer,
10832 CPP_CLOSE_PAREN))
10833 outputs = cp_parser_asm_operand_list (parser);
10835 /* If the next token is `::', there are no outputs, and the
10836 next token is the beginning of the inputs. */
10837 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10838 /* The inputs are coming next. */
10839 inputs_p = true;
10841 /* Look for inputs. */
10842 if (inputs_p
10843 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10845 /* Consume the `:' or `::'. */
10846 cp_lexer_consume_token (parser->lexer);
10847 /* Parse the output-operands. */
10848 if (cp_lexer_next_token_is_not (parser->lexer,
10849 CPP_COLON)
10850 && cp_lexer_next_token_is_not (parser->lexer,
10851 CPP_CLOSE_PAREN))
10852 inputs = cp_parser_asm_operand_list (parser);
10854 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10855 /* The clobbers are coming next. */
10856 clobbers_p = true;
10858 /* Look for clobbers. */
10859 if (clobbers_p
10860 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10862 /* Consume the `:' or `::'. */
10863 cp_lexer_consume_token (parser->lexer);
10864 /* Parse the clobbers. */
10865 if (cp_lexer_next_token_is_not (parser->lexer,
10866 CPP_CLOSE_PAREN))
10867 clobbers = cp_parser_asm_clobber_list (parser);
10870 /* Look for the closing `)'. */
10871 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10872 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10873 /*consume_paren=*/true);
10874 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10876 /* Create the ASM_EXPR. */
10877 if (parser->in_function_body)
10879 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10880 inputs, clobbers);
10881 /* If the extended syntax was not used, mark the ASM_EXPR. */
10882 if (!extended_p)
10884 tree temp = asm_stmt;
10885 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10886 temp = TREE_OPERAND (temp, 0);
10888 ASM_INPUT_P (temp) = 1;
10891 else
10892 assemble_asm (string);
10895 /* Declarators [gram.dcl.decl] */
10897 /* Parse an init-declarator.
10899 init-declarator:
10900 declarator initializer [opt]
10902 GNU Extension:
10904 init-declarator:
10905 declarator asm-specification [opt] attributes [opt] initializer [opt]
10907 function-definition:
10908 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10909 function-body
10910 decl-specifier-seq [opt] declarator function-try-block
10912 GNU Extension:
10914 function-definition:
10915 __extension__ function-definition
10917 The DECL_SPECIFIERS apply to this declarator. Returns a
10918 representation of the entity declared. If MEMBER_P is TRUE, then
10919 this declarator appears in a class scope. The new DECL created by
10920 this declarator is returned.
10922 The CHECKS are access checks that should be performed once we know
10923 what entity is being declared (and, therefore, what classes have
10924 befriended it).
10926 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10927 for a function-definition here as well. If the declarator is a
10928 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10929 be TRUE upon return. By that point, the function-definition will
10930 have been completely parsed.
10932 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10933 is FALSE. */
10935 static tree
10936 cp_parser_init_declarator (cp_parser* parser,
10937 cp_decl_specifier_seq *decl_specifiers,
10938 tree checks,
10939 bool function_definition_allowed_p,
10940 bool member_p,
10941 int declares_class_or_enum,
10942 bool* function_definition_p)
10944 cp_token *token;
10945 cp_declarator *declarator;
10946 tree prefix_attributes;
10947 tree attributes;
10948 tree asm_specification;
10949 tree initializer;
10950 tree decl = NULL_TREE;
10951 tree scope;
10952 bool is_initialized;
10953 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10954 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10955 "(...)". */
10956 enum cpp_ttype initialization_kind;
10957 bool is_parenthesized_init;
10958 bool is_non_constant_init;
10959 int ctor_dtor_or_conv_p;
10960 bool friend_p;
10961 tree pushed_scope = NULL;
10963 /* Gather the attributes that were provided with the
10964 decl-specifiers. */
10965 prefix_attributes = decl_specifiers->attributes;
10967 /* Assume that this is not the declarator for a function
10968 definition. */
10969 if (function_definition_p)
10970 *function_definition_p = false;
10972 /* Defer access checks while parsing the declarator; we cannot know
10973 what names are accessible until we know what is being
10974 declared. */
10975 resume_deferring_access_checks ();
10977 /* Parse the declarator. */
10978 declarator
10979 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10980 &ctor_dtor_or_conv_p,
10981 /*parenthesized_p=*/NULL,
10982 /*member_p=*/false);
10983 /* Gather up the deferred checks. */
10984 stop_deferring_access_checks ();
10986 /* If the DECLARATOR was erroneous, there's no need to go
10987 further. */
10988 if (declarator == cp_error_declarator)
10989 return error_mark_node;
10991 /* Check that the number of template-parameter-lists is OK. */
10992 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10993 return error_mark_node;
10995 if (declares_class_or_enum & 2)
10996 cp_parser_check_for_definition_in_return_type (declarator,
10997 decl_specifiers->type);
10999 /* Figure out what scope the entity declared by the DECLARATOR is
11000 located in. `grokdeclarator' sometimes changes the scope, so
11001 we compute it now. */
11002 scope = get_scope_of_declarator (declarator);
11004 /* If we're allowing GNU extensions, look for an asm-specification
11005 and attributes. */
11006 if (cp_parser_allow_gnu_extensions_p (parser))
11008 /* Look for an asm-specification. */
11009 asm_specification = cp_parser_asm_specification_opt (parser);
11010 /* And attributes. */
11011 attributes = cp_parser_attributes_opt (parser);
11013 else
11015 asm_specification = NULL_TREE;
11016 attributes = NULL_TREE;
11019 /* Peek at the next token. */
11020 token = cp_lexer_peek_token (parser->lexer);
11021 /* Check to see if the token indicates the start of a
11022 function-definition. */
11023 if (cp_parser_token_starts_function_definition_p (token))
11025 if (!function_definition_allowed_p)
11027 /* If a function-definition should not appear here, issue an
11028 error message. */
11029 cp_parser_error (parser,
11030 "a function-definition is not allowed here");
11031 return error_mark_node;
11033 else
11035 /* Neither attributes nor an asm-specification are allowed
11036 on a function-definition. */
11037 if (asm_specification)
11038 error ("an asm-specification is not allowed on a function-definition");
11039 if (attributes)
11040 error ("attributes are not allowed on a function-definition");
11041 /* This is a function-definition. */
11042 *function_definition_p = true;
11044 /* Parse the function definition. */
11045 if (member_p)
11046 decl = cp_parser_save_member_function_body (parser,
11047 decl_specifiers,
11048 declarator,
11049 prefix_attributes);
11050 else
11051 decl
11052 = (cp_parser_function_definition_from_specifiers_and_declarator
11053 (parser, decl_specifiers, prefix_attributes, declarator));
11055 return decl;
11059 /* [dcl.dcl]
11061 Only in function declarations for constructors, destructors, and
11062 type conversions can the decl-specifier-seq be omitted.
11064 We explicitly postpone this check past the point where we handle
11065 function-definitions because we tolerate function-definitions
11066 that are missing their return types in some modes. */
11067 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11069 cp_parser_error (parser,
11070 "expected constructor, destructor, or type conversion");
11071 return error_mark_node;
11074 /* An `=' or an `(' indicates an initializer. */
11075 if (token->type == CPP_EQ
11076 || token->type == CPP_OPEN_PAREN)
11078 is_initialized = true;
11079 initialization_kind = token->type;
11081 else
11083 /* If the init-declarator isn't initialized and isn't followed by a
11084 `,' or `;', it's not a valid init-declarator. */
11085 if (token->type != CPP_COMMA
11086 && token->type != CPP_SEMICOLON)
11088 cp_parser_error (parser, "expected initializer");
11089 return error_mark_node;
11091 is_initialized = false;
11092 initialization_kind = CPP_EOF;
11095 /* Because start_decl has side-effects, we should only call it if we
11096 know we're going ahead. By this point, we know that we cannot
11097 possibly be looking at any other construct. */
11098 cp_parser_commit_to_tentative_parse (parser);
11100 /* If the decl specifiers were bad, issue an error now that we're
11101 sure this was intended to be a declarator. Then continue
11102 declaring the variable(s), as int, to try to cut down on further
11103 errors. */
11104 if (decl_specifiers->any_specifiers_p
11105 && decl_specifiers->type == error_mark_node)
11107 cp_parser_error (parser, "invalid type in declaration");
11108 decl_specifiers->type = integer_type_node;
11111 /* Check to see whether or not this declaration is a friend. */
11112 friend_p = cp_parser_friend_p (decl_specifiers);
11114 /* Enter the newly declared entry in the symbol table. If we're
11115 processing a declaration in a class-specifier, we wait until
11116 after processing the initializer. */
11117 if (!member_p)
11119 if (parser->in_unbraced_linkage_specification_p)
11120 decl_specifiers->storage_class = sc_extern;
11121 decl = start_decl (declarator, decl_specifiers,
11122 is_initialized, attributes, prefix_attributes,
11123 &pushed_scope);
11125 else if (scope)
11126 /* Enter the SCOPE. That way unqualified names appearing in the
11127 initializer will be looked up in SCOPE. */
11128 pushed_scope = push_scope (scope);
11130 /* Perform deferred access control checks, now that we know in which
11131 SCOPE the declared entity resides. */
11132 if (!member_p && decl)
11134 tree saved_current_function_decl = NULL_TREE;
11136 /* If the entity being declared is a function, pretend that we
11137 are in its scope. If it is a `friend', it may have access to
11138 things that would not otherwise be accessible. */
11139 if (TREE_CODE (decl) == FUNCTION_DECL)
11141 saved_current_function_decl = current_function_decl;
11142 current_function_decl = decl;
11145 /* Perform access checks for template parameters. */
11146 cp_parser_perform_template_parameter_access_checks (checks);
11148 /* Perform the access control checks for the declarator and the
11149 the decl-specifiers. */
11150 perform_deferred_access_checks ();
11152 /* Restore the saved value. */
11153 if (TREE_CODE (decl) == FUNCTION_DECL)
11154 current_function_decl = saved_current_function_decl;
11157 /* Parse the initializer. */
11158 initializer = NULL_TREE;
11159 is_parenthesized_init = false;
11160 is_non_constant_init = true;
11161 if (is_initialized)
11163 if (function_declarator_p (declarator))
11165 if (initialization_kind == CPP_EQ)
11166 initializer = cp_parser_pure_specifier (parser);
11167 else
11169 /* If the declaration was erroneous, we don't really
11170 know what the user intended, so just silently
11171 consume the initializer. */
11172 if (decl != error_mark_node)
11173 error ("initializer provided for function");
11174 cp_parser_skip_to_closing_parenthesis (parser,
11175 /*recovering=*/true,
11176 /*or_comma=*/false,
11177 /*consume_paren=*/true);
11180 else
11181 initializer = cp_parser_initializer (parser,
11182 &is_parenthesized_init,
11183 &is_non_constant_init);
11186 /* The old parser allows attributes to appear after a parenthesized
11187 initializer. Mark Mitchell proposed removing this functionality
11188 on the GCC mailing lists on 2002-08-13. This parser accepts the
11189 attributes -- but ignores them. */
11190 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11191 if (cp_parser_attributes_opt (parser))
11192 warning (OPT_Wattributes,
11193 "attributes after parenthesized initializer ignored");
11195 /* For an in-class declaration, use `grokfield' to create the
11196 declaration. */
11197 if (member_p)
11199 if (pushed_scope)
11201 pop_scope (pushed_scope);
11202 pushed_scope = false;
11204 decl = grokfield (declarator, decl_specifiers,
11205 initializer, !is_non_constant_init,
11206 /*asmspec=*/NULL_TREE,
11207 prefix_attributes);
11208 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11209 cp_parser_save_default_args (parser, decl);
11212 /* Finish processing the declaration. But, skip friend
11213 declarations. */
11214 if (!friend_p && decl && decl != error_mark_node)
11216 cp_finish_decl (decl,
11217 initializer, !is_non_constant_init,
11218 asm_specification,
11219 /* If the initializer is in parentheses, then this is
11220 a direct-initialization, which means that an
11221 `explicit' constructor is OK. Otherwise, an
11222 `explicit' constructor cannot be used. */
11223 ((is_parenthesized_init || !is_initialized)
11224 ? 0 : LOOKUP_ONLYCONVERTING));
11226 if (!friend_p && pushed_scope)
11227 pop_scope (pushed_scope);
11229 return decl;
11232 /* Parse a declarator.
11234 declarator:
11235 direct-declarator
11236 ptr-operator declarator
11238 abstract-declarator:
11239 ptr-operator abstract-declarator [opt]
11240 direct-abstract-declarator
11242 GNU Extensions:
11244 declarator:
11245 attributes [opt] direct-declarator
11246 attributes [opt] ptr-operator declarator
11248 abstract-declarator:
11249 attributes [opt] ptr-operator abstract-declarator [opt]
11250 attributes [opt] direct-abstract-declarator
11252 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11253 detect constructor, destructor or conversion operators. It is set
11254 to -1 if the declarator is a name, and +1 if it is a
11255 function. Otherwise it is set to zero. Usually you just want to
11256 test for >0, but internally the negative value is used.
11258 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11259 a decl-specifier-seq unless it declares a constructor, destructor,
11260 or conversion. It might seem that we could check this condition in
11261 semantic analysis, rather than parsing, but that makes it difficult
11262 to handle something like `f()'. We want to notice that there are
11263 no decl-specifiers, and therefore realize that this is an
11264 expression, not a declaration.)
11266 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11267 the declarator is a direct-declarator of the form "(...)".
11269 MEMBER_P is true iff this declarator is a member-declarator. */
11271 static cp_declarator *
11272 cp_parser_declarator (cp_parser* parser,
11273 cp_parser_declarator_kind dcl_kind,
11274 int* ctor_dtor_or_conv_p,
11275 bool* parenthesized_p,
11276 bool member_p)
11278 cp_token *token;
11279 cp_declarator *declarator;
11280 enum tree_code code;
11281 cp_cv_quals cv_quals;
11282 tree class_type;
11283 tree attributes = NULL_TREE;
11285 /* Assume this is not a constructor, destructor, or type-conversion
11286 operator. */
11287 if (ctor_dtor_or_conv_p)
11288 *ctor_dtor_or_conv_p = 0;
11290 if (cp_parser_allow_gnu_extensions_p (parser))
11291 attributes = cp_parser_attributes_opt (parser);
11293 /* Peek at the next token. */
11294 token = cp_lexer_peek_token (parser->lexer);
11296 /* Check for the ptr-operator production. */
11297 cp_parser_parse_tentatively (parser);
11298 /* Parse the ptr-operator. */
11299 code = cp_parser_ptr_operator (parser,
11300 &class_type,
11301 &cv_quals);
11302 /* If that worked, then we have a ptr-operator. */
11303 if (cp_parser_parse_definitely (parser))
11305 /* If a ptr-operator was found, then this declarator was not
11306 parenthesized. */
11307 if (parenthesized_p)
11308 *parenthesized_p = true;
11309 /* The dependent declarator is optional if we are parsing an
11310 abstract-declarator. */
11311 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11312 cp_parser_parse_tentatively (parser);
11314 /* Parse the dependent declarator. */
11315 declarator = cp_parser_declarator (parser, dcl_kind,
11316 /*ctor_dtor_or_conv_p=*/NULL,
11317 /*parenthesized_p=*/NULL,
11318 /*member_p=*/false);
11320 /* If we are parsing an abstract-declarator, we must handle the
11321 case where the dependent declarator is absent. */
11322 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11323 && !cp_parser_parse_definitely (parser))
11324 declarator = NULL;
11326 /* Build the representation of the ptr-operator. */
11327 if (class_type)
11328 declarator = make_ptrmem_declarator (cv_quals,
11329 class_type,
11330 declarator);
11331 else if (code == INDIRECT_REF)
11332 declarator = make_pointer_declarator (cv_quals, declarator);
11333 else
11334 declarator = make_reference_declarator (cv_quals, declarator);
11336 /* Everything else is a direct-declarator. */
11337 else
11339 if (parenthesized_p)
11340 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11341 CPP_OPEN_PAREN);
11342 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11343 ctor_dtor_or_conv_p,
11344 member_p);
11347 if (attributes && declarator && declarator != cp_error_declarator)
11348 declarator->attributes = attributes;
11350 return declarator;
11353 /* Parse a direct-declarator or direct-abstract-declarator.
11355 direct-declarator:
11356 declarator-id
11357 direct-declarator ( parameter-declaration-clause )
11358 cv-qualifier-seq [opt]
11359 exception-specification [opt]
11360 direct-declarator [ constant-expression [opt] ]
11361 ( declarator )
11363 direct-abstract-declarator:
11364 direct-abstract-declarator [opt]
11365 ( parameter-declaration-clause )
11366 cv-qualifier-seq [opt]
11367 exception-specification [opt]
11368 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11369 ( abstract-declarator )
11371 Returns a representation of the declarator. DCL_KIND is
11372 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11373 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11374 we are parsing a direct-declarator. It is
11375 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11376 of ambiguity we prefer an abstract declarator, as per
11377 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11378 cp_parser_declarator. */
11380 static cp_declarator *
11381 cp_parser_direct_declarator (cp_parser* parser,
11382 cp_parser_declarator_kind dcl_kind,
11383 int* ctor_dtor_or_conv_p,
11384 bool member_p)
11386 cp_token *token;
11387 cp_declarator *declarator = NULL;
11388 tree scope = NULL_TREE;
11389 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11390 bool saved_in_declarator_p = parser->in_declarator_p;
11391 bool first = true;
11392 tree pushed_scope = NULL_TREE;
11394 while (true)
11396 /* Peek at the next token. */
11397 token = cp_lexer_peek_token (parser->lexer);
11398 if (token->type == CPP_OPEN_PAREN)
11400 /* This is either a parameter-declaration-clause, or a
11401 parenthesized declarator. When we know we are parsing a
11402 named declarator, it must be a parenthesized declarator
11403 if FIRST is true. For instance, `(int)' is a
11404 parameter-declaration-clause, with an omitted
11405 direct-abstract-declarator. But `((*))', is a
11406 parenthesized abstract declarator. Finally, when T is a
11407 template parameter `(T)' is a
11408 parameter-declaration-clause, and not a parenthesized
11409 named declarator.
11411 We first try and parse a parameter-declaration-clause,
11412 and then try a nested declarator (if FIRST is true).
11414 It is not an error for it not to be a
11415 parameter-declaration-clause, even when FIRST is
11416 false. Consider,
11418 int i (int);
11419 int i (3);
11421 The first is the declaration of a function while the
11422 second is a the definition of a variable, including its
11423 initializer.
11425 Having seen only the parenthesis, we cannot know which of
11426 these two alternatives should be selected. Even more
11427 complex are examples like:
11429 int i (int (a));
11430 int i (int (3));
11432 The former is a function-declaration; the latter is a
11433 variable initialization.
11435 Thus again, we try a parameter-declaration-clause, and if
11436 that fails, we back out and return. */
11438 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11440 cp_parameter_declarator *params;
11441 unsigned saved_num_template_parameter_lists;
11443 /* In a member-declarator, the only valid interpretation
11444 of a parenthesis is the start of a
11445 parameter-declaration-clause. (It is invalid to
11446 initialize a static data member with a parenthesized
11447 initializer; only the "=" form of initialization is
11448 permitted.) */
11449 if (!member_p)
11450 cp_parser_parse_tentatively (parser);
11452 /* Consume the `('. */
11453 cp_lexer_consume_token (parser->lexer);
11454 if (first)
11456 /* If this is going to be an abstract declarator, we're
11457 in a declarator and we can't have default args. */
11458 parser->default_arg_ok_p = false;
11459 parser->in_declarator_p = true;
11462 /* Inside the function parameter list, surrounding
11463 template-parameter-lists do not apply. */
11464 saved_num_template_parameter_lists
11465 = parser->num_template_parameter_lists;
11466 parser->num_template_parameter_lists = 0;
11468 /* Parse the parameter-declaration-clause. */
11469 params = cp_parser_parameter_declaration_clause (parser);
11471 parser->num_template_parameter_lists
11472 = saved_num_template_parameter_lists;
11474 /* If all went well, parse the cv-qualifier-seq and the
11475 exception-specification. */
11476 if (member_p || cp_parser_parse_definitely (parser))
11478 cp_cv_quals cv_quals;
11479 tree exception_specification;
11481 if (ctor_dtor_or_conv_p)
11482 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11483 first = false;
11484 /* Consume the `)'. */
11485 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11487 /* Parse the cv-qualifier-seq. */
11488 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11489 /* And the exception-specification. */
11490 exception_specification
11491 = cp_parser_exception_specification_opt (parser);
11493 /* Create the function-declarator. */
11494 declarator = make_call_declarator (declarator,
11495 params,
11496 cv_quals,
11497 exception_specification);
11498 /* Any subsequent parameter lists are to do with
11499 return type, so are not those of the declared
11500 function. */
11501 parser->default_arg_ok_p = false;
11503 /* Repeat the main loop. */
11504 continue;
11508 /* If this is the first, we can try a parenthesized
11509 declarator. */
11510 if (first)
11512 bool saved_in_type_id_in_expr_p;
11514 parser->default_arg_ok_p = saved_default_arg_ok_p;
11515 parser->in_declarator_p = saved_in_declarator_p;
11517 /* Consume the `('. */
11518 cp_lexer_consume_token (parser->lexer);
11519 /* Parse the nested declarator. */
11520 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11521 parser->in_type_id_in_expr_p = true;
11522 declarator
11523 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11524 /*parenthesized_p=*/NULL,
11525 member_p);
11526 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11527 first = false;
11528 /* Expect a `)'. */
11529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11530 declarator = cp_error_declarator;
11531 if (declarator == cp_error_declarator)
11532 break;
11534 goto handle_declarator;
11536 /* Otherwise, we must be done. */
11537 else
11538 break;
11540 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11541 && token->type == CPP_OPEN_SQUARE)
11543 /* Parse an array-declarator. */
11544 tree bounds;
11546 if (ctor_dtor_or_conv_p)
11547 *ctor_dtor_or_conv_p = 0;
11549 first = false;
11550 parser->default_arg_ok_p = false;
11551 parser->in_declarator_p = true;
11552 /* Consume the `['. */
11553 cp_lexer_consume_token (parser->lexer);
11554 /* Peek at the next token. */
11555 token = cp_lexer_peek_token (parser->lexer);
11556 /* If the next token is `]', then there is no
11557 constant-expression. */
11558 if (token->type != CPP_CLOSE_SQUARE)
11560 bool non_constant_p;
11562 bounds
11563 = cp_parser_constant_expression (parser,
11564 /*allow_non_constant=*/true,
11565 &non_constant_p);
11566 if (!non_constant_p)
11567 bounds = fold_non_dependent_expr (bounds);
11568 /* Normally, the array bound must be an integral constant
11569 expression. However, as an extension, we allow VLAs
11570 in function scopes. */
11571 else if (!parser->in_function_body)
11573 error ("array bound is not an integer constant");
11574 bounds = error_mark_node;
11577 else
11578 bounds = NULL_TREE;
11579 /* Look for the closing `]'. */
11580 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11582 declarator = cp_error_declarator;
11583 break;
11586 declarator = make_array_declarator (declarator, bounds);
11588 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11590 tree qualifying_scope;
11591 tree unqualified_name;
11592 special_function_kind sfk;
11594 /* Parse a declarator-id */
11595 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11596 cp_parser_parse_tentatively (parser);
11597 unqualified_name = cp_parser_declarator_id (parser);
11598 qualifying_scope = parser->scope;
11599 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11601 if (!cp_parser_parse_definitely (parser))
11602 unqualified_name = error_mark_node;
11603 else if (qualifying_scope
11604 || (TREE_CODE (unqualified_name)
11605 != IDENTIFIER_NODE))
11607 cp_parser_error (parser, "expected unqualified-id");
11608 unqualified_name = error_mark_node;
11612 if (unqualified_name == error_mark_node)
11614 declarator = cp_error_declarator;
11615 break;
11618 if (qualifying_scope && at_namespace_scope_p ()
11619 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11621 /* In the declaration of a member of a template class
11622 outside of the class itself, the SCOPE will sometimes
11623 be a TYPENAME_TYPE. For example, given:
11625 template <typename T>
11626 int S<T>::R::i = 3;
11628 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11629 this context, we must resolve S<T>::R to an ordinary
11630 type, rather than a typename type.
11632 The reason we normally avoid resolving TYPENAME_TYPEs
11633 is that a specialization of `S' might render
11634 `S<T>::R' not a type. However, if `S' is
11635 specialized, then this `i' will not be used, so there
11636 is no harm in resolving the types here. */
11637 tree type;
11639 /* Resolve the TYPENAME_TYPE. */
11640 type = resolve_typename_type (qualifying_scope,
11641 /*only_current_p=*/false);
11642 /* If that failed, the declarator is invalid. */
11643 if (type == error_mark_node)
11644 error ("%<%T::%D%> is not a type",
11645 TYPE_CONTEXT (qualifying_scope),
11646 TYPE_IDENTIFIER (qualifying_scope));
11647 qualifying_scope = type;
11650 sfk = sfk_none;
11651 if (unqualified_name)
11653 tree class_type;
11655 if (qualifying_scope
11656 && CLASS_TYPE_P (qualifying_scope))
11657 class_type = qualifying_scope;
11658 else
11659 class_type = current_class_type;
11661 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11663 tree name_type = TREE_TYPE (unqualified_name);
11664 if (class_type && same_type_p (name_type, class_type))
11666 if (qualifying_scope
11667 && CLASSTYPE_USE_TEMPLATE (name_type))
11669 error ("invalid use of constructor as a template");
11670 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11671 "name the constructor in a qualified name",
11672 class_type,
11673 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11674 class_type, name_type);
11675 declarator = cp_error_declarator;
11676 break;
11678 else
11679 unqualified_name = constructor_name (class_type);
11681 else
11683 /* We do not attempt to print the declarator
11684 here because we do not have enough
11685 information about its original syntactic
11686 form. */
11687 cp_parser_error (parser, "invalid declarator");
11688 declarator = cp_error_declarator;
11689 break;
11693 if (class_type)
11695 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11696 sfk = sfk_destructor;
11697 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11698 sfk = sfk_conversion;
11699 else if (/* There's no way to declare a constructor
11700 for an anonymous type, even if the type
11701 got a name for linkage purposes. */
11702 !TYPE_WAS_ANONYMOUS (class_type)
11703 && constructor_name_p (unqualified_name,
11704 class_type))
11706 unqualified_name = constructor_name (class_type);
11707 sfk = sfk_constructor;
11710 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11711 *ctor_dtor_or_conv_p = -1;
11714 declarator = make_id_declarator (qualifying_scope,
11715 unqualified_name,
11716 sfk);
11717 declarator->id_loc = token->location;
11719 handle_declarator:;
11720 scope = get_scope_of_declarator (declarator);
11721 if (scope)
11722 /* Any names that appear after the declarator-id for a
11723 member are looked up in the containing scope. */
11724 pushed_scope = push_scope (scope);
11725 parser->in_declarator_p = true;
11726 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11727 || (declarator && declarator->kind == cdk_id))
11728 /* Default args are only allowed on function
11729 declarations. */
11730 parser->default_arg_ok_p = saved_default_arg_ok_p;
11731 else
11732 parser->default_arg_ok_p = false;
11734 first = false;
11736 /* We're done. */
11737 else
11738 break;
11741 /* For an abstract declarator, we might wind up with nothing at this
11742 point. That's an error; the declarator is not optional. */
11743 if (!declarator)
11744 cp_parser_error (parser, "expected declarator");
11746 /* If we entered a scope, we must exit it now. */
11747 if (pushed_scope)
11748 pop_scope (pushed_scope);
11750 parser->default_arg_ok_p = saved_default_arg_ok_p;
11751 parser->in_declarator_p = saved_in_declarator_p;
11753 return declarator;
11756 /* Parse a ptr-operator.
11758 ptr-operator:
11759 * cv-qualifier-seq [opt]
11761 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11763 GNU Extension:
11765 ptr-operator:
11766 & cv-qualifier-seq [opt]
11768 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11769 Returns ADDR_EXPR if a reference was used. In the case of a
11770 pointer-to-member, *TYPE is filled in with the TYPE containing the
11771 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11772 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11773 ERROR_MARK if an error occurred. */
11775 static enum tree_code
11776 cp_parser_ptr_operator (cp_parser* parser,
11777 tree* type,
11778 cp_cv_quals *cv_quals)
11780 enum tree_code code = ERROR_MARK;
11781 cp_token *token;
11783 /* Assume that it's not a pointer-to-member. */
11784 *type = NULL_TREE;
11785 /* And that there are no cv-qualifiers. */
11786 *cv_quals = TYPE_UNQUALIFIED;
11788 /* Peek at the next token. */
11789 token = cp_lexer_peek_token (parser->lexer);
11790 /* If it's a `*' or `&' we have a pointer or reference. */
11791 if (token->type == CPP_MULT || token->type == CPP_AND)
11793 /* Remember which ptr-operator we were processing. */
11794 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11796 /* Consume the `*' or `&'. */
11797 cp_lexer_consume_token (parser->lexer);
11799 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11800 `&', if we are allowing GNU extensions. (The only qualifier
11801 that can legally appear after `&' is `restrict', but that is
11802 enforced during semantic analysis. */
11803 if (code == INDIRECT_REF
11804 || cp_parser_allow_gnu_extensions_p (parser))
11805 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11807 else
11809 /* Try the pointer-to-member case. */
11810 cp_parser_parse_tentatively (parser);
11811 /* Look for the optional `::' operator. */
11812 cp_parser_global_scope_opt (parser,
11813 /*current_scope_valid_p=*/false);
11814 /* Look for the nested-name specifier. */
11815 cp_parser_nested_name_specifier (parser,
11816 /*typename_keyword_p=*/false,
11817 /*check_dependency_p=*/true,
11818 /*type_p=*/false,
11819 /*is_declaration=*/false);
11820 /* If we found it, and the next token is a `*', then we are
11821 indeed looking at a pointer-to-member operator. */
11822 if (!cp_parser_error_occurred (parser)
11823 && cp_parser_require (parser, CPP_MULT, "`*'"))
11825 /* Indicate that the `*' operator was used. */
11826 code = INDIRECT_REF;
11828 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11829 error ("%qD is a namespace", parser->scope);
11830 else
11832 /* The type of which the member is a member is given by the
11833 current SCOPE. */
11834 *type = parser->scope;
11835 /* The next name will not be qualified. */
11836 parser->scope = NULL_TREE;
11837 parser->qualifying_scope = NULL_TREE;
11838 parser->object_scope = NULL_TREE;
11839 /* Look for the optional cv-qualifier-seq. */
11840 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11843 /* If that didn't work we don't have a ptr-operator. */
11844 if (!cp_parser_parse_definitely (parser))
11845 cp_parser_error (parser, "expected ptr-operator");
11848 return code;
11851 /* Parse an (optional) cv-qualifier-seq.
11853 cv-qualifier-seq:
11854 cv-qualifier cv-qualifier-seq [opt]
11856 cv-qualifier:
11857 const
11858 volatile
11860 GNU Extension:
11862 cv-qualifier:
11863 __restrict__
11865 Returns a bitmask representing the cv-qualifiers. */
11867 static cp_cv_quals
11868 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11870 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11872 while (true)
11874 cp_token *token;
11875 cp_cv_quals cv_qualifier;
11877 /* Peek at the next token. */
11878 token = cp_lexer_peek_token (parser->lexer);
11879 /* See if it's a cv-qualifier. */
11880 switch (token->keyword)
11882 case RID_CONST:
11883 cv_qualifier = TYPE_QUAL_CONST;
11884 break;
11886 case RID_VOLATILE:
11887 cv_qualifier = TYPE_QUAL_VOLATILE;
11888 break;
11890 case RID_RESTRICT:
11891 cv_qualifier = TYPE_QUAL_RESTRICT;
11892 break;
11894 default:
11895 cv_qualifier = TYPE_UNQUALIFIED;
11896 break;
11899 if (!cv_qualifier)
11900 break;
11902 if (cv_quals & cv_qualifier)
11904 error ("duplicate cv-qualifier");
11905 cp_lexer_purge_token (parser->lexer);
11907 else
11909 cp_lexer_consume_token (parser->lexer);
11910 cv_quals |= cv_qualifier;
11914 return cv_quals;
11917 /* Parse a declarator-id.
11919 declarator-id:
11920 id-expression
11921 :: [opt] nested-name-specifier [opt] type-name
11923 In the `id-expression' case, the value returned is as for
11924 cp_parser_id_expression if the id-expression was an unqualified-id.
11925 If the id-expression was a qualified-id, then a SCOPE_REF is
11926 returned. The first operand is the scope (either a NAMESPACE_DECL
11927 or TREE_TYPE), but the second is still just a representation of an
11928 unqualified-id. */
11930 static tree
11931 cp_parser_declarator_id (cp_parser* parser)
11933 tree id;
11934 /* The expression must be an id-expression. Assume that qualified
11935 names are the names of types so that:
11937 template <class T>
11938 int S<T>::R::i = 3;
11940 will work; we must treat `S<T>::R' as the name of a type.
11941 Similarly, assume that qualified names are templates, where
11942 required, so that:
11944 template <class T>
11945 int S<T>::R<T>::i = 3;
11947 will work, too. */
11948 id = cp_parser_id_expression (parser,
11949 /*template_keyword_p=*/false,
11950 /*check_dependency_p=*/false,
11951 /*template_p=*/NULL,
11952 /*declarator_p=*/true);
11953 if (BASELINK_P (id))
11954 id = BASELINK_FUNCTIONS (id);
11955 return id;
11958 /* Parse a type-id.
11960 type-id:
11961 type-specifier-seq abstract-declarator [opt]
11963 Returns the TYPE specified. */
11965 static tree
11966 cp_parser_type_id (cp_parser* parser)
11968 cp_decl_specifier_seq type_specifier_seq;
11969 cp_declarator *abstract_declarator;
11971 /* Parse the type-specifier-seq. */
11972 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11973 &type_specifier_seq);
11974 if (type_specifier_seq.type == error_mark_node)
11975 return error_mark_node;
11977 /* There might or might not be an abstract declarator. */
11978 cp_parser_parse_tentatively (parser);
11979 /* Look for the declarator. */
11980 abstract_declarator
11981 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11982 /*parenthesized_p=*/NULL,
11983 /*member_p=*/false);
11984 /* Check to see if there really was a declarator. */
11985 if (!cp_parser_parse_definitely (parser))
11986 abstract_declarator = NULL;
11988 return groktypename (&type_specifier_seq, abstract_declarator);
11991 /* Parse a type-specifier-seq.
11993 type-specifier-seq:
11994 type-specifier type-specifier-seq [opt]
11996 GNU extension:
11998 type-specifier-seq:
11999 attributes type-specifier-seq [opt]
12001 If IS_CONDITION is true, we are at the start of a "condition",
12002 e.g., we've just seen "if (".
12004 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12006 static void
12007 cp_parser_type_specifier_seq (cp_parser* parser,
12008 bool is_condition,
12009 cp_decl_specifier_seq *type_specifier_seq)
12011 bool seen_type_specifier = false;
12012 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12014 /* Clear the TYPE_SPECIFIER_SEQ. */
12015 clear_decl_specs (type_specifier_seq);
12017 /* Parse the type-specifiers and attributes. */
12018 while (true)
12020 tree type_specifier;
12021 bool is_cv_qualifier;
12023 /* Check for attributes first. */
12024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12026 type_specifier_seq->attributes =
12027 chainon (type_specifier_seq->attributes,
12028 cp_parser_attributes_opt (parser));
12029 continue;
12032 /* Look for the type-specifier. */
12033 type_specifier = cp_parser_type_specifier (parser,
12034 flags,
12035 type_specifier_seq,
12036 /*is_declaration=*/false,
12037 NULL,
12038 &is_cv_qualifier);
12039 if (!type_specifier)
12041 /* If the first type-specifier could not be found, this is not a
12042 type-specifier-seq at all. */
12043 if (!seen_type_specifier)
12045 cp_parser_error (parser, "expected type-specifier");
12046 type_specifier_seq->type = error_mark_node;
12047 return;
12049 /* If subsequent type-specifiers could not be found, the
12050 type-specifier-seq is complete. */
12051 break;
12054 seen_type_specifier = true;
12055 /* The standard says that a condition can be:
12057 type-specifier-seq declarator = assignment-expression
12059 However, given:
12061 struct S {};
12062 if (int S = ...)
12064 we should treat the "S" as a declarator, not as a
12065 type-specifier. The standard doesn't say that explicitly for
12066 type-specifier-seq, but it does say that for
12067 decl-specifier-seq in an ordinary declaration. Perhaps it
12068 would be clearer just to allow a decl-specifier-seq here, and
12069 then add a semantic restriction that if any decl-specifiers
12070 that are not type-specifiers appear, the program is invalid. */
12071 if (is_condition && !is_cv_qualifier)
12072 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12075 cp_parser_check_decl_spec (type_specifier_seq);
12078 /* Parse a parameter-declaration-clause.
12080 parameter-declaration-clause:
12081 parameter-declaration-list [opt] ... [opt]
12082 parameter-declaration-list , ...
12084 Returns a representation for the parameter declarations. A return
12085 value of NULL indicates a parameter-declaration-clause consisting
12086 only of an ellipsis. */
12088 static cp_parameter_declarator *
12089 cp_parser_parameter_declaration_clause (cp_parser* parser)
12091 cp_parameter_declarator *parameters;
12092 cp_token *token;
12093 bool ellipsis_p;
12094 bool is_error;
12096 /* Peek at the next token. */
12097 token = cp_lexer_peek_token (parser->lexer);
12098 /* Check for trivial parameter-declaration-clauses. */
12099 if (token->type == CPP_ELLIPSIS)
12101 /* Consume the `...' token. */
12102 cp_lexer_consume_token (parser->lexer);
12103 return NULL;
12105 else if (token->type == CPP_CLOSE_PAREN)
12106 /* There are no parameters. */
12108 #ifndef NO_IMPLICIT_EXTERN_C
12109 if (in_system_header && current_class_type == NULL
12110 && current_lang_name == lang_name_c)
12111 return NULL;
12112 else
12113 #endif
12114 return no_parameters;
12116 /* Check for `(void)', too, which is a special case. */
12117 else if (token->keyword == RID_VOID
12118 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12119 == CPP_CLOSE_PAREN))
12121 /* Consume the `void' token. */
12122 cp_lexer_consume_token (parser->lexer);
12123 /* There are no parameters. */
12124 return no_parameters;
12127 /* Parse the parameter-declaration-list. */
12128 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12129 /* If a parse error occurred while parsing the
12130 parameter-declaration-list, then the entire
12131 parameter-declaration-clause is erroneous. */
12132 if (is_error)
12133 return NULL;
12135 /* Peek at the next token. */
12136 token = cp_lexer_peek_token (parser->lexer);
12137 /* If it's a `,', the clause should terminate with an ellipsis. */
12138 if (token->type == CPP_COMMA)
12140 /* Consume the `,'. */
12141 cp_lexer_consume_token (parser->lexer);
12142 /* Expect an ellipsis. */
12143 ellipsis_p
12144 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12146 /* It might also be `...' if the optional trailing `,' was
12147 omitted. */
12148 else if (token->type == CPP_ELLIPSIS)
12150 /* Consume the `...' token. */
12151 cp_lexer_consume_token (parser->lexer);
12152 /* And remember that we saw it. */
12153 ellipsis_p = true;
12155 else
12156 ellipsis_p = false;
12158 /* Finish the parameter list. */
12159 if (parameters && ellipsis_p)
12160 parameters->ellipsis_p = true;
12162 return parameters;
12165 /* Parse a parameter-declaration-list.
12167 parameter-declaration-list:
12168 parameter-declaration
12169 parameter-declaration-list , parameter-declaration
12171 Returns a representation of the parameter-declaration-list, as for
12172 cp_parser_parameter_declaration_clause. However, the
12173 `void_list_node' is never appended to the list. Upon return,
12174 *IS_ERROR will be true iff an error occurred. */
12176 static cp_parameter_declarator *
12177 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12179 cp_parameter_declarator *parameters = NULL;
12180 cp_parameter_declarator **tail = &parameters;
12181 bool saved_in_unbraced_linkage_specification_p;
12183 /* Assume all will go well. */
12184 *is_error = false;
12185 /* The special considerations that apply to a function within an
12186 unbraced linkage specifications do not apply to the parameters
12187 to the function. */
12188 saved_in_unbraced_linkage_specification_p
12189 = parser->in_unbraced_linkage_specification_p;
12190 parser->in_unbraced_linkage_specification_p = false;
12192 /* Look for more parameters. */
12193 while (true)
12195 cp_parameter_declarator *parameter;
12196 bool parenthesized_p;
12197 /* Parse the parameter. */
12198 parameter
12199 = cp_parser_parameter_declaration (parser,
12200 /*template_parm_p=*/false,
12201 &parenthesized_p);
12203 /* If a parse error occurred parsing the parameter declaration,
12204 then the entire parameter-declaration-list is erroneous. */
12205 if (!parameter)
12207 *is_error = true;
12208 parameters = NULL;
12209 break;
12211 /* Add the new parameter to the list. */
12212 *tail = parameter;
12213 tail = &parameter->next;
12215 /* Peek at the next token. */
12216 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12217 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12218 /* These are for Objective-C++ */
12219 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12220 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12221 /* The parameter-declaration-list is complete. */
12222 break;
12223 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12225 cp_token *token;
12227 /* Peek at the next token. */
12228 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12229 /* If it's an ellipsis, then the list is complete. */
12230 if (token->type == CPP_ELLIPSIS)
12231 break;
12232 /* Otherwise, there must be more parameters. Consume the
12233 `,'. */
12234 cp_lexer_consume_token (parser->lexer);
12235 /* When parsing something like:
12237 int i(float f, double d)
12239 we can tell after seeing the declaration for "f" that we
12240 are not looking at an initialization of a variable "i",
12241 but rather at the declaration of a function "i".
12243 Due to the fact that the parsing of template arguments
12244 (as specified to a template-id) requires backtracking we
12245 cannot use this technique when inside a template argument
12246 list. */
12247 if (!parser->in_template_argument_list_p
12248 && !parser->in_type_id_in_expr_p
12249 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12250 /* However, a parameter-declaration of the form
12251 "foat(f)" (which is a valid declaration of a
12252 parameter "f") can also be interpreted as an
12253 expression (the conversion of "f" to "float"). */
12254 && !parenthesized_p)
12255 cp_parser_commit_to_tentative_parse (parser);
12257 else
12259 cp_parser_error (parser, "expected %<,%> or %<...%>");
12260 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12261 cp_parser_skip_to_closing_parenthesis (parser,
12262 /*recovering=*/true,
12263 /*or_comma=*/false,
12264 /*consume_paren=*/false);
12265 break;
12269 parser->in_unbraced_linkage_specification_p
12270 = saved_in_unbraced_linkage_specification_p;
12272 return parameters;
12275 /* Parse a parameter declaration.
12277 parameter-declaration:
12278 decl-specifier-seq declarator
12279 decl-specifier-seq declarator = assignment-expression
12280 decl-specifier-seq abstract-declarator [opt]
12281 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12283 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12284 declares a template parameter. (In that case, a non-nested `>'
12285 token encountered during the parsing of the assignment-expression
12286 is not interpreted as a greater-than operator.)
12288 Returns a representation of the parameter, or NULL if an error
12289 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12290 true iff the declarator is of the form "(p)". */
12292 static cp_parameter_declarator *
12293 cp_parser_parameter_declaration (cp_parser *parser,
12294 bool template_parm_p,
12295 bool *parenthesized_p)
12297 int declares_class_or_enum;
12298 bool greater_than_is_operator_p;
12299 cp_decl_specifier_seq decl_specifiers;
12300 cp_declarator *declarator;
12301 tree default_argument;
12302 cp_token *token;
12303 const char *saved_message;
12305 /* In a template parameter, `>' is not an operator.
12307 [temp.param]
12309 When parsing a default template-argument for a non-type
12310 template-parameter, the first non-nested `>' is taken as the end
12311 of the template parameter-list rather than a greater-than
12312 operator. */
12313 greater_than_is_operator_p = !template_parm_p;
12315 /* Type definitions may not appear in parameter types. */
12316 saved_message = parser->type_definition_forbidden_message;
12317 parser->type_definition_forbidden_message
12318 = "types may not be defined in parameter types";
12320 /* Parse the declaration-specifiers. */
12321 cp_parser_decl_specifier_seq (parser,
12322 CP_PARSER_FLAGS_NONE,
12323 &decl_specifiers,
12324 &declares_class_or_enum);
12325 /* If an error occurred, there's no reason to attempt to parse the
12326 rest of the declaration. */
12327 if (cp_parser_error_occurred (parser))
12329 parser->type_definition_forbidden_message = saved_message;
12330 return NULL;
12333 /* Peek at the next token. */
12334 token = cp_lexer_peek_token (parser->lexer);
12335 /* If the next token is a `)', `,', `=', `>', or `...', then there
12336 is no declarator. */
12337 if (token->type == CPP_CLOSE_PAREN
12338 || token->type == CPP_COMMA
12339 || token->type == CPP_EQ
12340 || token->type == CPP_ELLIPSIS
12341 || token->type == CPP_GREATER)
12343 declarator = NULL;
12344 if (parenthesized_p)
12345 *parenthesized_p = false;
12347 /* Otherwise, there should be a declarator. */
12348 else
12350 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12351 parser->default_arg_ok_p = false;
12353 /* After seeing a decl-specifier-seq, if the next token is not a
12354 "(", there is no possibility that the code is a valid
12355 expression. Therefore, if parsing tentatively, we commit at
12356 this point. */
12357 if (!parser->in_template_argument_list_p
12358 /* In an expression context, having seen:
12360 (int((char ...
12362 we cannot be sure whether we are looking at a
12363 function-type (taking a "char" as a parameter) or a cast
12364 of some object of type "char" to "int". */
12365 && !parser->in_type_id_in_expr_p
12366 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12367 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12368 cp_parser_commit_to_tentative_parse (parser);
12369 /* Parse the declarator. */
12370 declarator = cp_parser_declarator (parser,
12371 CP_PARSER_DECLARATOR_EITHER,
12372 /*ctor_dtor_or_conv_p=*/NULL,
12373 parenthesized_p,
12374 /*member_p=*/false);
12375 parser->default_arg_ok_p = saved_default_arg_ok_p;
12376 /* After the declarator, allow more attributes. */
12377 decl_specifiers.attributes
12378 = chainon (decl_specifiers.attributes,
12379 cp_parser_attributes_opt (parser));
12382 /* The restriction on defining new types applies only to the type
12383 of the parameter, not to the default argument. */
12384 parser->type_definition_forbidden_message = saved_message;
12386 /* If the next token is `=', then process a default argument. */
12387 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12389 bool saved_greater_than_is_operator_p;
12390 /* Consume the `='. */
12391 cp_lexer_consume_token (parser->lexer);
12393 /* If we are defining a class, then the tokens that make up the
12394 default argument must be saved and processed later. */
12395 if (!template_parm_p && at_class_scope_p ()
12396 && TYPE_BEING_DEFINED (current_class_type))
12398 unsigned depth = 0;
12399 cp_token *first_token;
12400 cp_token *token;
12402 /* Add tokens until we have processed the entire default
12403 argument. We add the range [first_token, token). */
12404 first_token = cp_lexer_peek_token (parser->lexer);
12405 while (true)
12407 bool done = false;
12409 /* Peek at the next token. */
12410 token = cp_lexer_peek_token (parser->lexer);
12411 /* What we do depends on what token we have. */
12412 switch (token->type)
12414 /* In valid code, a default argument must be
12415 immediately followed by a `,' `)', or `...'. */
12416 case CPP_COMMA:
12417 case CPP_CLOSE_PAREN:
12418 case CPP_ELLIPSIS:
12419 /* If we run into a non-nested `;', `}', or `]',
12420 then the code is invalid -- but the default
12421 argument is certainly over. */
12422 case CPP_SEMICOLON:
12423 case CPP_CLOSE_BRACE:
12424 case CPP_CLOSE_SQUARE:
12425 if (depth == 0)
12426 done = true;
12427 /* Update DEPTH, if necessary. */
12428 else if (token->type == CPP_CLOSE_PAREN
12429 || token->type == CPP_CLOSE_BRACE
12430 || token->type == CPP_CLOSE_SQUARE)
12431 --depth;
12432 break;
12434 case CPP_OPEN_PAREN:
12435 case CPP_OPEN_SQUARE:
12436 case CPP_OPEN_BRACE:
12437 ++depth;
12438 break;
12440 case CPP_GREATER:
12441 /* If we see a non-nested `>', and `>' is not an
12442 operator, then it marks the end of the default
12443 argument. */
12444 if (!depth && !greater_than_is_operator_p)
12445 done = true;
12446 break;
12448 /* If we run out of tokens, issue an error message. */
12449 case CPP_EOF:
12450 error ("file ends in default argument");
12451 done = true;
12452 break;
12454 case CPP_NAME:
12455 case CPP_SCOPE:
12456 /* In these cases, we should look for template-ids.
12457 For example, if the default argument is
12458 `X<int, double>()', we need to do name lookup to
12459 figure out whether or not `X' is a template; if
12460 so, the `,' does not end the default argument.
12462 That is not yet done. */
12463 break;
12465 default:
12466 break;
12469 /* If we've reached the end, stop. */
12470 if (done)
12471 break;
12473 /* Add the token to the token block. */
12474 token = cp_lexer_consume_token (parser->lexer);
12477 /* Create a DEFAULT_ARG to represented the unparsed default
12478 argument. */
12479 default_argument = make_node (DEFAULT_ARG);
12480 DEFARG_TOKENS (default_argument)
12481 = cp_token_cache_new (first_token, token);
12482 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12484 /* Outside of a class definition, we can just parse the
12485 assignment-expression. */
12486 else
12488 bool saved_local_variables_forbidden_p;
12490 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12491 set correctly. */
12492 saved_greater_than_is_operator_p
12493 = parser->greater_than_is_operator_p;
12494 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12495 /* Local variable names (and the `this' keyword) may not
12496 appear in a default argument. */
12497 saved_local_variables_forbidden_p
12498 = parser->local_variables_forbidden_p;
12499 parser->local_variables_forbidden_p = true;
12500 /* The default argument expression may cause implicitly
12501 defined member functions to be synthesized, which will
12502 result in garbage collection. We must treat this
12503 situation as if we were within the body of function so as
12504 to avoid collecting live data on the stack. */
12505 ++function_depth;
12506 /* Parse the assignment-expression. */
12507 if (template_parm_p)
12508 push_deferring_access_checks (dk_no_deferred);
12509 default_argument
12510 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12511 if (template_parm_p)
12512 pop_deferring_access_checks ();
12513 /* Restore saved state. */
12514 --function_depth;
12515 parser->greater_than_is_operator_p
12516 = saved_greater_than_is_operator_p;
12517 parser->local_variables_forbidden_p
12518 = saved_local_variables_forbidden_p;
12520 if (!parser->default_arg_ok_p)
12522 if (!flag_pedantic_errors)
12523 warning (0, "deprecated use of default argument for parameter of non-function");
12524 else
12526 error ("default arguments are only permitted for function parameters");
12527 default_argument = NULL_TREE;
12531 else
12532 default_argument = NULL_TREE;
12534 return make_parameter_declarator (&decl_specifiers,
12535 declarator,
12536 default_argument);
12539 /* Parse a function-body.
12541 function-body:
12542 compound_statement */
12544 static void
12545 cp_parser_function_body (cp_parser *parser)
12547 cp_parser_compound_statement (parser, NULL, false);
12550 /* Parse a ctor-initializer-opt followed by a function-body. Return
12551 true if a ctor-initializer was present. */
12553 static bool
12554 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12556 tree body;
12557 bool ctor_initializer_p;
12559 /* Begin the function body. */
12560 body = begin_function_body ();
12561 /* Parse the optional ctor-initializer. */
12562 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12563 /* Parse the function-body. */
12564 cp_parser_function_body (parser);
12565 /* Finish the function body. */
12566 finish_function_body (body);
12568 return ctor_initializer_p;
12571 /* Parse an initializer.
12573 initializer:
12574 = initializer-clause
12575 ( expression-list )
12577 Returns an expression representing the initializer. If no
12578 initializer is present, NULL_TREE is returned.
12580 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12581 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12582 set to FALSE if there is no initializer present. If there is an
12583 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12584 is set to true; otherwise it is set to false. */
12586 static tree
12587 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12588 bool* non_constant_p)
12590 cp_token *token;
12591 tree init;
12593 /* Peek at the next token. */
12594 token = cp_lexer_peek_token (parser->lexer);
12596 /* Let our caller know whether or not this initializer was
12597 parenthesized. */
12598 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12599 /* Assume that the initializer is constant. */
12600 *non_constant_p = false;
12602 if (token->type == CPP_EQ)
12604 /* Consume the `='. */
12605 cp_lexer_consume_token (parser->lexer);
12606 /* Parse the initializer-clause. */
12607 init = cp_parser_initializer_clause (parser, non_constant_p);
12609 else if (token->type == CPP_OPEN_PAREN)
12610 init = cp_parser_parenthesized_expression_list (parser, false,
12611 /*cast_p=*/false,
12612 non_constant_p);
12613 else
12615 /* Anything else is an error. */
12616 cp_parser_error (parser, "expected initializer");
12617 init = error_mark_node;
12620 return init;
12623 /* Parse an initializer-clause.
12625 initializer-clause:
12626 assignment-expression
12627 { initializer-list , [opt] }
12630 Returns an expression representing the initializer.
12632 If the `assignment-expression' production is used the value
12633 returned is simply a representation for the expression.
12635 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12636 the elements of the initializer-list (or NULL, if the last
12637 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12638 NULL_TREE. There is no way to detect whether or not the optional
12639 trailing `,' was provided. NON_CONSTANT_P is as for
12640 cp_parser_initializer. */
12642 static tree
12643 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12645 tree initializer;
12647 /* Assume the expression is constant. */
12648 *non_constant_p = false;
12650 /* If it is not a `{', then we are looking at an
12651 assignment-expression. */
12652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12654 initializer
12655 = cp_parser_constant_expression (parser,
12656 /*allow_non_constant_p=*/true,
12657 non_constant_p);
12658 if (!*non_constant_p)
12659 initializer = fold_non_dependent_expr (initializer);
12661 else
12663 /* Consume the `{' token. */
12664 cp_lexer_consume_token (parser->lexer);
12665 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12666 initializer = make_node (CONSTRUCTOR);
12667 /* If it's not a `}', then there is a non-trivial initializer. */
12668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12670 /* Parse the initializer list. */
12671 CONSTRUCTOR_ELTS (initializer)
12672 = cp_parser_initializer_list (parser, non_constant_p);
12673 /* A trailing `,' token is allowed. */
12674 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12675 cp_lexer_consume_token (parser->lexer);
12677 /* Now, there should be a trailing `}'. */
12678 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12681 return initializer;
12684 /* Parse an initializer-list.
12686 initializer-list:
12687 initializer-clause
12688 initializer-list , initializer-clause
12690 GNU Extension:
12692 initializer-list:
12693 identifier : initializer-clause
12694 initializer-list, identifier : initializer-clause
12696 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12697 for the initializer. If the INDEX of the elt is non-NULL, it is the
12698 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12699 as for cp_parser_initializer. */
12701 static VEC(constructor_elt,gc) *
12702 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12704 VEC(constructor_elt,gc) *v = NULL;
12706 /* Assume all of the expressions are constant. */
12707 *non_constant_p = false;
12709 /* Parse the rest of the list. */
12710 while (true)
12712 cp_token *token;
12713 tree identifier;
12714 tree initializer;
12715 bool clause_non_constant_p;
12717 /* If the next token is an identifier and the following one is a
12718 colon, we are looking at the GNU designated-initializer
12719 syntax. */
12720 if (cp_parser_allow_gnu_extensions_p (parser)
12721 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12722 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12724 /* Consume the identifier. */
12725 identifier = cp_lexer_consume_token (parser->lexer)->value;
12726 /* Consume the `:'. */
12727 cp_lexer_consume_token (parser->lexer);
12729 else
12730 identifier = NULL_TREE;
12732 /* Parse the initializer. */
12733 initializer = cp_parser_initializer_clause (parser,
12734 &clause_non_constant_p);
12735 /* If any clause is non-constant, so is the entire initializer. */
12736 if (clause_non_constant_p)
12737 *non_constant_p = true;
12739 /* Add it to the vector. */
12740 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12742 /* If the next token is not a comma, we have reached the end of
12743 the list. */
12744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12745 break;
12747 /* Peek at the next token. */
12748 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12749 /* If the next token is a `}', then we're still done. An
12750 initializer-clause can have a trailing `,' after the
12751 initializer-list and before the closing `}'. */
12752 if (token->type == CPP_CLOSE_BRACE)
12753 break;
12755 /* Consume the `,' token. */
12756 cp_lexer_consume_token (parser->lexer);
12759 return v;
12762 /* Classes [gram.class] */
12764 /* Parse a class-name.
12766 class-name:
12767 identifier
12768 template-id
12770 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12771 to indicate that names looked up in dependent types should be
12772 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12773 keyword has been used to indicate that the name that appears next
12774 is a template. TAG_TYPE indicates the explicit tag given before
12775 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12776 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12777 is the class being defined in a class-head.
12779 Returns the TYPE_DECL representing the class. */
12781 static tree
12782 cp_parser_class_name (cp_parser *parser,
12783 bool typename_keyword_p,
12784 bool template_keyword_p,
12785 enum tag_types tag_type,
12786 bool check_dependency_p,
12787 bool class_head_p,
12788 bool is_declaration)
12790 tree decl;
12791 tree scope;
12792 bool typename_p;
12793 cp_token *token;
12795 /* All class-names start with an identifier. */
12796 token = cp_lexer_peek_token (parser->lexer);
12797 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12799 cp_parser_error (parser, "expected class-name");
12800 return error_mark_node;
12803 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12804 to a template-id, so we save it here. */
12805 scope = parser->scope;
12806 if (scope == error_mark_node)
12807 return error_mark_node;
12809 /* Any name names a type if we're following the `typename' keyword
12810 in a qualified name where the enclosing scope is type-dependent. */
12811 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12812 && dependent_type_p (scope));
12813 /* Handle the common case (an identifier, but not a template-id)
12814 efficiently. */
12815 if (token->type == CPP_NAME
12816 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12818 cp_token *identifier_token;
12819 tree identifier;
12820 bool ambiguous_p;
12822 /* Look for the identifier. */
12823 identifier_token = cp_lexer_peek_token (parser->lexer);
12824 ambiguous_p = identifier_token->ambiguous_p;
12825 identifier = cp_parser_identifier (parser);
12826 /* If the next token isn't an identifier, we are certainly not
12827 looking at a class-name. */
12828 if (identifier == error_mark_node)
12829 decl = error_mark_node;
12830 /* If we know this is a type-name, there's no need to look it
12831 up. */
12832 else if (typename_p)
12833 decl = identifier;
12834 else
12836 tree ambiguous_decls;
12837 /* If we already know that this lookup is ambiguous, then
12838 we've already issued an error message; there's no reason
12839 to check again. */
12840 if (ambiguous_p)
12842 cp_parser_simulate_error (parser);
12843 return error_mark_node;
12845 /* If the next token is a `::', then the name must be a type
12846 name.
12848 [basic.lookup.qual]
12850 During the lookup for a name preceding the :: scope
12851 resolution operator, object, function, and enumerator
12852 names are ignored. */
12853 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12854 tag_type = typename_type;
12855 /* Look up the name. */
12856 decl = cp_parser_lookup_name (parser, identifier,
12857 tag_type,
12858 /*is_template=*/false,
12859 /*is_namespace=*/false,
12860 check_dependency_p,
12861 &ambiguous_decls);
12862 if (ambiguous_decls)
12864 error ("reference to %qD is ambiguous", identifier);
12865 print_candidates (ambiguous_decls);
12866 if (cp_parser_parsing_tentatively (parser))
12868 identifier_token->ambiguous_p = true;
12869 cp_parser_simulate_error (parser);
12871 return error_mark_node;
12875 else
12877 /* Try a template-id. */
12878 decl = cp_parser_template_id (parser, template_keyword_p,
12879 check_dependency_p,
12880 is_declaration);
12881 if (decl == error_mark_node)
12882 return error_mark_node;
12885 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12887 /* If this is a typename, create a TYPENAME_TYPE. */
12888 if (typename_p && decl != error_mark_node)
12890 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12891 if (decl != error_mark_node)
12892 decl = TYPE_NAME (decl);
12895 /* Check to see that it is really the name of a class. */
12896 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12897 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12898 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12899 /* Situations like this:
12901 template <typename T> struct A {
12902 typename T::template X<int>::I i;
12905 are problematic. Is `T::template X<int>' a class-name? The
12906 standard does not seem to be definitive, but there is no other
12907 valid interpretation of the following `::'. Therefore, those
12908 names are considered class-names. */
12910 decl = make_typename_type (scope, decl, tag_type, tf_error);
12911 if (decl != error_mark_node)
12912 decl = TYPE_NAME (decl);
12914 else if (TREE_CODE (decl) != TYPE_DECL
12915 || TREE_TYPE (decl) == error_mark_node
12916 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12917 decl = error_mark_node;
12919 if (decl == error_mark_node)
12920 cp_parser_error (parser, "expected class-name");
12922 return decl;
12925 /* Parse a class-specifier.
12927 class-specifier:
12928 class-head { member-specification [opt] }
12930 Returns the TREE_TYPE representing the class. */
12932 static tree
12933 cp_parser_class_specifier (cp_parser* parser)
12935 cp_token *token;
12936 tree type;
12937 tree attributes = NULL_TREE;
12938 int has_trailing_semicolon;
12939 bool nested_name_specifier_p;
12940 unsigned saved_num_template_parameter_lists;
12941 bool saved_in_function_body;
12942 tree old_scope = NULL_TREE;
12943 tree scope = NULL_TREE;
12944 tree bases = NULL_TREE;
12946 push_deferring_access_checks (dk_no_deferred);
12948 /* Parse the class-head. */
12949 type = cp_parser_class_head (parser,
12950 &nested_name_specifier_p,
12951 &attributes,
12952 &bases);
12953 /* If the class-head was a semantic disaster, skip the entire body
12954 of the class. */
12955 if (!type)
12957 cp_parser_skip_to_end_of_block_or_statement (parser);
12958 pop_deferring_access_checks ();
12959 return error_mark_node;
12962 /* Look for the `{'. */
12963 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12965 pop_deferring_access_checks ();
12966 return error_mark_node;
12969 /* Process the base classes. */
12970 xref_basetypes (type, bases);
12972 /* Issue an error message if type-definitions are forbidden here. */
12973 cp_parser_check_type_definition (parser);
12974 /* Remember that we are defining one more class. */
12975 ++parser->num_classes_being_defined;
12976 /* Inside the class, surrounding template-parameter-lists do not
12977 apply. */
12978 saved_num_template_parameter_lists
12979 = parser->num_template_parameter_lists;
12980 parser->num_template_parameter_lists = 0;
12981 /* We are not in a function body. */
12982 saved_in_function_body = parser->in_function_body;
12983 parser->in_function_body = false;
12985 /* Start the class. */
12986 if (nested_name_specifier_p)
12988 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12989 old_scope = push_inner_scope (scope);
12991 type = begin_class_definition (type);
12993 if (type == error_mark_node)
12994 /* If the type is erroneous, skip the entire body of the class. */
12995 cp_parser_skip_to_closing_brace (parser);
12996 else
12997 /* Parse the member-specification. */
12998 cp_parser_member_specification_opt (parser);
13000 /* Look for the trailing `}'. */
13001 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13002 /* We get better error messages by noticing a common problem: a
13003 missing trailing `;'. */
13004 token = cp_lexer_peek_token (parser->lexer);
13005 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13006 /* Look for trailing attributes to apply to this class. */
13007 if (cp_parser_allow_gnu_extensions_p (parser))
13009 tree sub_attr = cp_parser_attributes_opt (parser);
13010 attributes = chainon (attributes, sub_attr);
13012 if (type != error_mark_node)
13013 type = finish_struct (type, attributes);
13014 if (nested_name_specifier_p)
13015 pop_inner_scope (old_scope, scope);
13016 /* If this class is not itself within the scope of another class,
13017 then we need to parse the bodies of all of the queued function
13018 definitions. Note that the queued functions defined in a class
13019 are not always processed immediately following the
13020 class-specifier for that class. Consider:
13022 struct A {
13023 struct B { void f() { sizeof (A); } };
13026 If `f' were processed before the processing of `A' were
13027 completed, there would be no way to compute the size of `A'.
13028 Note that the nesting we are interested in here is lexical --
13029 not the semantic nesting given by TYPE_CONTEXT. In particular,
13030 for:
13032 struct A { struct B; };
13033 struct A::B { void f() { } };
13035 there is no need to delay the parsing of `A::B::f'. */
13036 if (--parser->num_classes_being_defined == 0)
13038 tree queue_entry;
13039 tree fn;
13040 tree class_type = NULL_TREE;
13041 tree pushed_scope = NULL_TREE;
13043 /* In a first pass, parse default arguments to the functions.
13044 Then, in a second pass, parse the bodies of the functions.
13045 This two-phased approach handles cases like:
13047 struct S {
13048 void f() { g(); }
13049 void g(int i = 3);
13053 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13054 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13055 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13056 TREE_PURPOSE (parser->unparsed_functions_queues)
13057 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13059 fn = TREE_VALUE (queue_entry);
13060 /* If there are default arguments that have not yet been processed,
13061 take care of them now. */
13062 if (class_type != TREE_PURPOSE (queue_entry))
13064 if (pushed_scope)
13065 pop_scope (pushed_scope);
13066 class_type = TREE_PURPOSE (queue_entry);
13067 pushed_scope = push_scope (class_type);
13069 /* Make sure that any template parameters are in scope. */
13070 maybe_begin_member_template_processing (fn);
13071 /* Parse the default argument expressions. */
13072 cp_parser_late_parsing_default_args (parser, fn);
13073 /* Remove any template parameters from the symbol table. */
13074 maybe_end_member_template_processing ();
13076 if (pushed_scope)
13077 pop_scope (pushed_scope);
13078 /* Now parse the body of the functions. */
13079 for (TREE_VALUE (parser->unparsed_functions_queues)
13080 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13081 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13082 TREE_VALUE (parser->unparsed_functions_queues)
13083 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13085 /* Figure out which function we need to process. */
13086 fn = TREE_VALUE (queue_entry);
13087 /* Parse the function. */
13088 cp_parser_late_parsing_for_member (parser, fn);
13092 /* Put back any saved access checks. */
13093 pop_deferring_access_checks ();
13095 /* Restore saved state. */
13096 parser->in_function_body = saved_in_function_body;
13097 parser->num_template_parameter_lists
13098 = saved_num_template_parameter_lists;
13100 return type;
13103 /* Parse a class-head.
13105 class-head:
13106 class-key identifier [opt] base-clause [opt]
13107 class-key nested-name-specifier identifier base-clause [opt]
13108 class-key nested-name-specifier [opt] template-id
13109 base-clause [opt]
13111 GNU Extensions:
13112 class-key attributes identifier [opt] base-clause [opt]
13113 class-key attributes nested-name-specifier identifier base-clause [opt]
13114 class-key attributes nested-name-specifier [opt] template-id
13115 base-clause [opt]
13117 Returns the TYPE of the indicated class. Sets
13118 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13119 involving a nested-name-specifier was used, and FALSE otherwise.
13121 Returns error_mark_node if this is not a class-head.
13123 Returns NULL_TREE if the class-head is syntactically valid, but
13124 semantically invalid in a way that means we should skip the entire
13125 body of the class. */
13127 static tree
13128 cp_parser_class_head (cp_parser* parser,
13129 bool* nested_name_specifier_p,
13130 tree *attributes_p,
13131 tree *bases)
13133 tree nested_name_specifier;
13134 enum tag_types class_key;
13135 tree id = NULL_TREE;
13136 tree type = NULL_TREE;
13137 tree attributes;
13138 bool template_id_p = false;
13139 bool qualified_p = false;
13140 bool invalid_nested_name_p = false;
13141 bool invalid_explicit_specialization_p = false;
13142 tree pushed_scope = NULL_TREE;
13143 unsigned num_templates;
13145 /* Assume no nested-name-specifier will be present. */
13146 *nested_name_specifier_p = false;
13147 /* Assume no template parameter lists will be used in defining the
13148 type. */
13149 num_templates = 0;
13151 /* Look for the class-key. */
13152 class_key = cp_parser_class_key (parser);
13153 if (class_key == none_type)
13154 return error_mark_node;
13156 /* Parse the attributes. */
13157 attributes = cp_parser_attributes_opt (parser);
13159 /* If the next token is `::', that is invalid -- but sometimes
13160 people do try to write:
13162 struct ::S {};
13164 Handle this gracefully by accepting the extra qualifier, and then
13165 issuing an error about it later if this really is a
13166 class-head. If it turns out just to be an elaborated type
13167 specifier, remain silent. */
13168 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13169 qualified_p = true;
13171 push_deferring_access_checks (dk_no_check);
13173 /* Determine the name of the class. Begin by looking for an
13174 optional nested-name-specifier. */
13175 nested_name_specifier
13176 = cp_parser_nested_name_specifier_opt (parser,
13177 /*typename_keyword_p=*/false,
13178 /*check_dependency_p=*/false,
13179 /*type_p=*/false,
13180 /*is_declaration=*/false);
13181 /* If there was a nested-name-specifier, then there *must* be an
13182 identifier. */
13183 if (nested_name_specifier)
13185 /* Although the grammar says `identifier', it really means
13186 `class-name' or `template-name'. You are only allowed to
13187 define a class that has already been declared with this
13188 syntax.
13190 The proposed resolution for Core Issue 180 says that whever
13191 you see `class T::X' you should treat `X' as a type-name.
13193 It is OK to define an inaccessible class; for example:
13195 class A { class B; };
13196 class A::B {};
13198 We do not know if we will see a class-name, or a
13199 template-name. We look for a class-name first, in case the
13200 class-name is a template-id; if we looked for the
13201 template-name first we would stop after the template-name. */
13202 cp_parser_parse_tentatively (parser);
13203 type = cp_parser_class_name (parser,
13204 /*typename_keyword_p=*/false,
13205 /*template_keyword_p=*/false,
13206 class_type,
13207 /*check_dependency_p=*/false,
13208 /*class_head_p=*/true,
13209 /*is_declaration=*/false);
13210 /* If that didn't work, ignore the nested-name-specifier. */
13211 if (!cp_parser_parse_definitely (parser))
13213 invalid_nested_name_p = true;
13214 id = cp_parser_identifier (parser);
13215 if (id == error_mark_node)
13216 id = NULL_TREE;
13218 /* If we could not find a corresponding TYPE, treat this
13219 declaration like an unqualified declaration. */
13220 if (type == error_mark_node)
13221 nested_name_specifier = NULL_TREE;
13222 /* Otherwise, count the number of templates used in TYPE and its
13223 containing scopes. */
13224 else
13226 tree scope;
13228 for (scope = TREE_TYPE (type);
13229 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13230 scope = (TYPE_P (scope)
13231 ? TYPE_CONTEXT (scope)
13232 : DECL_CONTEXT (scope)))
13233 if (TYPE_P (scope)
13234 && CLASS_TYPE_P (scope)
13235 && CLASSTYPE_TEMPLATE_INFO (scope)
13236 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13237 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13238 ++num_templates;
13241 /* Otherwise, the identifier is optional. */
13242 else
13244 /* We don't know whether what comes next is a template-id,
13245 an identifier, or nothing at all. */
13246 cp_parser_parse_tentatively (parser);
13247 /* Check for a template-id. */
13248 id = cp_parser_template_id (parser,
13249 /*template_keyword_p=*/false,
13250 /*check_dependency_p=*/true,
13251 /*is_declaration=*/true);
13252 /* If that didn't work, it could still be an identifier. */
13253 if (!cp_parser_parse_definitely (parser))
13255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13256 id = cp_parser_identifier (parser);
13257 else
13258 id = NULL_TREE;
13260 else
13262 template_id_p = true;
13263 ++num_templates;
13267 pop_deferring_access_checks ();
13269 if (id)
13270 cp_parser_check_for_invalid_template_id (parser, id);
13272 /* If it's not a `:' or a `{' then we can't really be looking at a
13273 class-head, since a class-head only appears as part of a
13274 class-specifier. We have to detect this situation before calling
13275 xref_tag, since that has irreversible side-effects. */
13276 if (!cp_parser_next_token_starts_class_definition_p (parser))
13278 cp_parser_error (parser, "expected %<{%> or %<:%>");
13279 return error_mark_node;
13282 /* At this point, we're going ahead with the class-specifier, even
13283 if some other problem occurs. */
13284 cp_parser_commit_to_tentative_parse (parser);
13285 /* Issue the error about the overly-qualified name now. */
13286 if (qualified_p)
13287 cp_parser_error (parser,
13288 "global qualification of class name is invalid");
13289 else if (invalid_nested_name_p)
13290 cp_parser_error (parser,
13291 "qualified name does not name a class");
13292 else if (nested_name_specifier)
13294 tree scope;
13296 /* Reject typedef-names in class heads. */
13297 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13299 error ("invalid class name in declaration of %qD", type);
13300 type = NULL_TREE;
13301 goto done;
13304 /* Figure out in what scope the declaration is being placed. */
13305 scope = current_scope ();
13306 /* If that scope does not contain the scope in which the
13307 class was originally declared, the program is invalid. */
13308 if (scope && !is_ancestor (scope, nested_name_specifier))
13310 error ("declaration of %qD in %qD which does not enclose %qD",
13311 type, scope, nested_name_specifier);
13312 type = NULL_TREE;
13313 goto done;
13315 /* [dcl.meaning]
13317 A declarator-id shall not be qualified exception of the
13318 definition of a ... nested class outside of its class
13319 ... [or] a the definition or explicit instantiation of a
13320 class member of a namespace outside of its namespace. */
13321 if (scope == nested_name_specifier)
13323 pedwarn ("extra qualification ignored");
13324 nested_name_specifier = NULL_TREE;
13325 num_templates = 0;
13328 /* An explicit-specialization must be preceded by "template <>". If
13329 it is not, try to recover gracefully. */
13330 if (at_namespace_scope_p ()
13331 && parser->num_template_parameter_lists == 0
13332 && template_id_p)
13334 error ("an explicit specialization must be preceded by %<template <>%>");
13335 invalid_explicit_specialization_p = true;
13336 /* Take the same action that would have been taken by
13337 cp_parser_explicit_specialization. */
13338 ++parser->num_template_parameter_lists;
13339 begin_specialization ();
13341 /* There must be no "return" statements between this point and the
13342 end of this function; set "type "to the correct return value and
13343 use "goto done;" to return. */
13344 /* Make sure that the right number of template parameters were
13345 present. */
13346 if (!cp_parser_check_template_parameters (parser, num_templates))
13348 /* If something went wrong, there is no point in even trying to
13349 process the class-definition. */
13350 type = NULL_TREE;
13351 goto done;
13354 /* Look up the type. */
13355 if (template_id_p)
13357 type = TREE_TYPE (id);
13358 type = maybe_process_partial_specialization (type);
13359 if (nested_name_specifier)
13360 pushed_scope = push_scope (nested_name_specifier);
13362 else if (nested_name_specifier)
13364 tree class_type;
13366 /* Given:
13368 template <typename T> struct S { struct T };
13369 template <typename T> struct S<T>::T { };
13371 we will get a TYPENAME_TYPE when processing the definition of
13372 `S::T'. We need to resolve it to the actual type before we
13373 try to define it. */
13374 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13376 class_type = resolve_typename_type (TREE_TYPE (type),
13377 /*only_current_p=*/false);
13378 if (class_type != error_mark_node)
13379 type = TYPE_NAME (class_type);
13380 else
13382 cp_parser_error (parser, "could not resolve typename type");
13383 type = error_mark_node;
13387 maybe_process_partial_specialization (TREE_TYPE (type));
13388 class_type = current_class_type;
13389 /* Enter the scope indicated by the nested-name-specifier. */
13390 pushed_scope = push_scope (nested_name_specifier);
13391 /* Get the canonical version of this type. */
13392 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13393 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13394 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13396 type = push_template_decl (type);
13397 if (type == error_mark_node)
13399 type = NULL_TREE;
13400 goto done;
13404 type = TREE_TYPE (type);
13405 *nested_name_specifier_p = true;
13407 else /* The name is not a nested name. */
13409 /* If the class was unnamed, create a dummy name. */
13410 if (!id)
13411 id = make_anon_name ();
13412 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13413 parser->num_template_parameter_lists);
13416 /* Indicate whether this class was declared as a `class' or as a
13417 `struct'. */
13418 if (TREE_CODE (type) == RECORD_TYPE)
13419 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13420 cp_parser_check_class_key (class_key, type);
13422 /* If this type was already complete, and we see another definition,
13423 that's an error. */
13424 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13426 error ("redefinition of %q#T", type);
13427 error ("previous definition of %q+#T", type);
13428 type = NULL_TREE;
13429 goto done;
13432 /* We will have entered the scope containing the class; the names of
13433 base classes should be looked up in that context. For example:
13435 struct A { struct B {}; struct C; };
13436 struct A::C : B {};
13438 is valid. */
13439 *bases = NULL_TREE;
13441 /* Get the list of base-classes, if there is one. */
13442 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13443 *bases = cp_parser_base_clause (parser);
13445 done:
13446 /* Leave the scope given by the nested-name-specifier. We will
13447 enter the class scope itself while processing the members. */
13448 if (pushed_scope)
13449 pop_scope (pushed_scope);
13451 if (invalid_explicit_specialization_p)
13453 end_specialization ();
13454 --parser->num_template_parameter_lists;
13456 *attributes_p = attributes;
13457 return type;
13460 /* Parse a class-key.
13462 class-key:
13463 class
13464 struct
13465 union
13467 Returns the kind of class-key specified, or none_type to indicate
13468 error. */
13470 static enum tag_types
13471 cp_parser_class_key (cp_parser* parser)
13473 cp_token *token;
13474 enum tag_types tag_type;
13476 /* Look for the class-key. */
13477 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13478 if (!token)
13479 return none_type;
13481 /* Check to see if the TOKEN is a class-key. */
13482 tag_type = cp_parser_token_is_class_key (token);
13483 if (!tag_type)
13484 cp_parser_error (parser, "expected class-key");
13485 return tag_type;
13488 /* Parse an (optional) member-specification.
13490 member-specification:
13491 member-declaration member-specification [opt]
13492 access-specifier : member-specification [opt] */
13494 static void
13495 cp_parser_member_specification_opt (cp_parser* parser)
13497 while (true)
13499 cp_token *token;
13500 enum rid keyword;
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* If it's a `}', or EOF then we've seen all the members. */
13505 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13506 break;
13508 /* See if this token is a keyword. */
13509 keyword = token->keyword;
13510 switch (keyword)
13512 case RID_PUBLIC:
13513 case RID_PROTECTED:
13514 case RID_PRIVATE:
13515 /* Consume the access-specifier. */
13516 cp_lexer_consume_token (parser->lexer);
13517 /* Remember which access-specifier is active. */
13518 current_access_specifier = token->value;
13519 /* Look for the `:'. */
13520 cp_parser_require (parser, CPP_COLON, "`:'");
13521 break;
13523 default:
13524 /* Accept #pragmas at class scope. */
13525 if (token->type == CPP_PRAGMA)
13527 cp_lexer_handle_pragma (parser->lexer);
13528 break;
13531 /* Otherwise, the next construction must be a
13532 member-declaration. */
13533 cp_parser_member_declaration (parser);
13538 /* Parse a member-declaration.
13540 member-declaration:
13541 decl-specifier-seq [opt] member-declarator-list [opt] ;
13542 function-definition ; [opt]
13543 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13544 using-declaration
13545 template-declaration
13547 member-declarator-list:
13548 member-declarator
13549 member-declarator-list , member-declarator
13551 member-declarator:
13552 declarator pure-specifier [opt]
13553 declarator constant-initializer [opt]
13554 identifier [opt] : constant-expression
13556 GNU Extensions:
13558 member-declaration:
13559 __extension__ member-declaration
13561 member-declarator:
13562 declarator attributes [opt] pure-specifier [opt]
13563 declarator attributes [opt] constant-initializer [opt]
13564 identifier [opt] attributes [opt] : constant-expression */
13566 static void
13567 cp_parser_member_declaration (cp_parser* parser)
13569 cp_decl_specifier_seq decl_specifiers;
13570 tree prefix_attributes;
13571 tree decl;
13572 int declares_class_or_enum;
13573 bool friend_p;
13574 cp_token *token;
13575 int saved_pedantic;
13577 /* Check for the `__extension__' keyword. */
13578 if (cp_parser_extension_opt (parser, &saved_pedantic))
13580 /* Recurse. */
13581 cp_parser_member_declaration (parser);
13582 /* Restore the old value of the PEDANTIC flag. */
13583 pedantic = saved_pedantic;
13585 return;
13588 /* Check for a template-declaration. */
13589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13591 /* An explicit specialization here is an error condition, and we
13592 expect the specialization handler to detect and report this. */
13593 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13594 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13595 cp_parser_explicit_specialization (parser);
13596 else
13597 cp_parser_template_declaration (parser, /*member_p=*/true);
13599 return;
13602 /* Check for a using-declaration. */
13603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13605 /* Parse the using-declaration. */
13606 cp_parser_using_declaration (parser,
13607 /*access_declaration_p=*/false);
13608 return;
13611 /* Check for @defs. */
13612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13614 tree ivar, member;
13615 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13616 ivar = ivar_chains;
13617 while (ivar)
13619 member = ivar;
13620 ivar = TREE_CHAIN (member);
13621 TREE_CHAIN (member) = NULL_TREE;
13622 finish_member_declaration (member);
13624 return;
13627 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13628 return;
13630 /* Parse the decl-specifier-seq. */
13631 cp_parser_decl_specifier_seq (parser,
13632 CP_PARSER_FLAGS_OPTIONAL,
13633 &decl_specifiers,
13634 &declares_class_or_enum);
13635 prefix_attributes = decl_specifiers.attributes;
13636 decl_specifiers.attributes = NULL_TREE;
13637 /* Check for an invalid type-name. */
13638 if (!decl_specifiers.type
13639 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13640 return;
13641 /* If there is no declarator, then the decl-specifier-seq should
13642 specify a type. */
13643 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13645 /* If there was no decl-specifier-seq, and the next token is a
13646 `;', then we have something like:
13648 struct S { ; };
13650 [class.mem]
13652 Each member-declaration shall declare at least one member
13653 name of the class. */
13654 if (!decl_specifiers.any_specifiers_p)
13656 cp_token *token = cp_lexer_peek_token (parser->lexer);
13657 if (pedantic && !token->in_system_header)
13658 pedwarn ("%Hextra %<;%>", &token->location);
13660 else
13662 tree type;
13664 /* See if this declaration is a friend. */
13665 friend_p = cp_parser_friend_p (&decl_specifiers);
13666 /* If there were decl-specifiers, check to see if there was
13667 a class-declaration. */
13668 type = check_tag_decl (&decl_specifiers);
13669 /* Nested classes have already been added to the class, but
13670 a `friend' needs to be explicitly registered. */
13671 if (friend_p)
13673 /* If the `friend' keyword was present, the friend must
13674 be introduced with a class-key. */
13675 if (!declares_class_or_enum)
13676 error ("a class-key must be used when declaring a friend");
13677 /* In this case:
13679 template <typename T> struct A {
13680 friend struct A<T>::B;
13683 A<T>::B will be represented by a TYPENAME_TYPE, and
13684 therefore not recognized by check_tag_decl. */
13685 if (!type
13686 && decl_specifiers.type
13687 && TYPE_P (decl_specifiers.type))
13688 type = decl_specifiers.type;
13689 if (!type || !TYPE_P (type))
13690 error ("friend declaration does not name a class or "
13691 "function");
13692 else
13693 make_friend_class (current_class_type, type,
13694 /*complain=*/true);
13696 /* If there is no TYPE, an error message will already have
13697 been issued. */
13698 else if (!type || type == error_mark_node)
13700 /* An anonymous aggregate has to be handled specially; such
13701 a declaration really declares a data member (with a
13702 particular type), as opposed to a nested class. */
13703 else if (ANON_AGGR_TYPE_P (type))
13705 /* Remove constructors and such from TYPE, now that we
13706 know it is an anonymous aggregate. */
13707 fixup_anonymous_aggr (type);
13708 /* And make the corresponding data member. */
13709 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13710 /* Add it to the class. */
13711 finish_member_declaration (decl);
13713 else
13714 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13717 else
13719 /* See if these declarations will be friends. */
13720 friend_p = cp_parser_friend_p (&decl_specifiers);
13722 /* Keep going until we hit the `;' at the end of the
13723 declaration. */
13724 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13726 tree attributes = NULL_TREE;
13727 tree first_attribute;
13729 /* Peek at the next token. */
13730 token = cp_lexer_peek_token (parser->lexer);
13732 /* Check for a bitfield declaration. */
13733 if (token->type == CPP_COLON
13734 || (token->type == CPP_NAME
13735 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13736 == CPP_COLON))
13738 tree identifier;
13739 tree width;
13741 /* Get the name of the bitfield. Note that we cannot just
13742 check TOKEN here because it may have been invalidated by
13743 the call to cp_lexer_peek_nth_token above. */
13744 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13745 identifier = cp_parser_identifier (parser);
13746 else
13747 identifier = NULL_TREE;
13749 /* Consume the `:' token. */
13750 cp_lexer_consume_token (parser->lexer);
13751 /* Get the width of the bitfield. */
13752 width
13753 = cp_parser_constant_expression (parser,
13754 /*allow_non_constant=*/false,
13755 NULL);
13757 /* Look for attributes that apply to the bitfield. */
13758 attributes = cp_parser_attributes_opt (parser);
13759 /* Remember which attributes are prefix attributes and
13760 which are not. */
13761 first_attribute = attributes;
13762 /* Combine the attributes. */
13763 attributes = chainon (prefix_attributes, attributes);
13765 /* Create the bitfield declaration. */
13766 decl = grokbitfield (identifier
13767 ? make_id_declarator (NULL_TREE,
13768 identifier,
13769 sfk_none)
13770 : NULL,
13771 &decl_specifiers,
13772 width);
13773 /* Apply the attributes. */
13774 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13776 else
13778 cp_declarator *declarator;
13779 tree initializer;
13780 tree asm_specification;
13781 int ctor_dtor_or_conv_p;
13783 /* Parse the declarator. */
13784 declarator
13785 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13786 &ctor_dtor_or_conv_p,
13787 /*parenthesized_p=*/NULL,
13788 /*member_p=*/true);
13790 /* If something went wrong parsing the declarator, make sure
13791 that we at least consume some tokens. */
13792 if (declarator == cp_error_declarator)
13794 /* Skip to the end of the statement. */
13795 cp_parser_skip_to_end_of_statement (parser);
13796 /* If the next token is not a semicolon, that is
13797 probably because we just skipped over the body of
13798 a function. So, we consume a semicolon if
13799 present, but do not issue an error message if it
13800 is not present. */
13801 if (cp_lexer_next_token_is (parser->lexer,
13802 CPP_SEMICOLON))
13803 cp_lexer_consume_token (parser->lexer);
13804 return;
13807 if (declares_class_or_enum & 2)
13808 cp_parser_check_for_definition_in_return_type
13809 (declarator, decl_specifiers.type);
13811 /* Look for an asm-specification. */
13812 asm_specification = cp_parser_asm_specification_opt (parser);
13813 /* Look for attributes that apply to the declaration. */
13814 attributes = cp_parser_attributes_opt (parser);
13815 /* Remember which attributes are prefix attributes and
13816 which are not. */
13817 first_attribute = attributes;
13818 /* Combine the attributes. */
13819 attributes = chainon (prefix_attributes, attributes);
13821 /* If it's an `=', then we have a constant-initializer or a
13822 pure-specifier. It is not correct to parse the
13823 initializer before registering the member declaration
13824 since the member declaration should be in scope while
13825 its initializer is processed. However, the rest of the
13826 front end does not yet provide an interface that allows
13827 us to handle this correctly. */
13828 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13830 /* In [class.mem]:
13832 A pure-specifier shall be used only in the declaration of
13833 a virtual function.
13835 A member-declarator can contain a constant-initializer
13836 only if it declares a static member of integral or
13837 enumeration type.
13839 Therefore, if the DECLARATOR is for a function, we look
13840 for a pure-specifier; otherwise, we look for a
13841 constant-initializer. When we call `grokfield', it will
13842 perform more stringent semantics checks. */
13843 if (function_declarator_p (declarator))
13844 initializer = cp_parser_pure_specifier (parser);
13845 else
13846 /* Parse the initializer. */
13847 initializer = cp_parser_constant_initializer (parser);
13849 /* Otherwise, there is no initializer. */
13850 else
13851 initializer = NULL_TREE;
13853 /* See if we are probably looking at a function
13854 definition. We are certainly not looking at a
13855 member-declarator. Calling `grokfield' has
13856 side-effects, so we must not do it unless we are sure
13857 that we are looking at a member-declarator. */
13858 if (cp_parser_token_starts_function_definition_p
13859 (cp_lexer_peek_token (parser->lexer)))
13861 /* The grammar does not allow a pure-specifier to be
13862 used when a member function is defined. (It is
13863 possible that this fact is an oversight in the
13864 standard, since a pure function may be defined
13865 outside of the class-specifier. */
13866 if (initializer)
13867 error ("pure-specifier on function-definition");
13868 decl = cp_parser_save_member_function_body (parser,
13869 &decl_specifiers,
13870 declarator,
13871 attributes);
13872 /* If the member was not a friend, declare it here. */
13873 if (!friend_p)
13874 finish_member_declaration (decl);
13875 /* Peek at the next token. */
13876 token = cp_lexer_peek_token (parser->lexer);
13877 /* If the next token is a semicolon, consume it. */
13878 if (token->type == CPP_SEMICOLON)
13879 cp_lexer_consume_token (parser->lexer);
13880 return;
13882 else
13883 /* Create the declaration. */
13884 decl = grokfield (declarator, &decl_specifiers,
13885 initializer, /*init_const_expr_p=*/true,
13886 asm_specification,
13887 attributes);
13890 /* Reset PREFIX_ATTRIBUTES. */
13891 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13892 attributes = TREE_CHAIN (attributes);
13893 if (attributes)
13894 TREE_CHAIN (attributes) = NULL_TREE;
13896 /* If there is any qualification still in effect, clear it
13897 now; we will be starting fresh with the next declarator. */
13898 parser->scope = NULL_TREE;
13899 parser->qualifying_scope = NULL_TREE;
13900 parser->object_scope = NULL_TREE;
13901 /* If it's a `,', then there are more declarators. */
13902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13903 cp_lexer_consume_token (parser->lexer);
13904 /* If the next token isn't a `;', then we have a parse error. */
13905 else if (cp_lexer_next_token_is_not (parser->lexer,
13906 CPP_SEMICOLON))
13908 cp_parser_error (parser, "expected %<;%>");
13909 /* Skip tokens until we find a `;'. */
13910 cp_parser_skip_to_end_of_statement (parser);
13912 break;
13915 if (decl)
13917 /* Add DECL to the list of members. */
13918 if (!friend_p)
13919 finish_member_declaration (decl);
13921 if (TREE_CODE (decl) == FUNCTION_DECL)
13922 cp_parser_save_default_args (parser, decl);
13927 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13930 /* Parse a pure-specifier.
13932 pure-specifier:
13935 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13936 Otherwise, ERROR_MARK_NODE is returned. */
13938 static tree
13939 cp_parser_pure_specifier (cp_parser* parser)
13941 cp_token *token;
13943 /* Look for the `=' token. */
13944 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13945 return error_mark_node;
13946 /* Look for the `0' token. */
13947 token = cp_lexer_consume_token (parser->lexer);
13948 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13950 cp_parser_error (parser,
13951 "invalid pure specifier (only `= 0' is allowed)");
13952 cp_parser_skip_to_end_of_statement (parser);
13953 return error_mark_node;
13956 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13957 We need to get information from the lexer about how the number
13958 was spelled in order to fix this problem. */
13959 return integer_zero_node;
13962 /* Parse a constant-initializer.
13964 constant-initializer:
13965 = constant-expression
13967 Returns a representation of the constant-expression. */
13969 static tree
13970 cp_parser_constant_initializer (cp_parser* parser)
13972 /* Look for the `=' token. */
13973 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13974 return error_mark_node;
13976 /* It is invalid to write:
13978 struct S { static const int i = { 7 }; };
13981 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13983 cp_parser_error (parser,
13984 "a brace-enclosed initializer is not allowed here");
13985 /* Consume the opening brace. */
13986 cp_lexer_consume_token (parser->lexer);
13987 /* Skip the initializer. */
13988 cp_parser_skip_to_closing_brace (parser);
13989 /* Look for the trailing `}'. */
13990 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13992 return error_mark_node;
13995 return cp_parser_constant_expression (parser,
13996 /*allow_non_constant=*/false,
13997 NULL);
14000 /* Derived classes [gram.class.derived] */
14002 /* Parse a base-clause.
14004 base-clause:
14005 : base-specifier-list
14007 base-specifier-list:
14008 base-specifier
14009 base-specifier-list , base-specifier
14011 Returns a TREE_LIST representing the base-classes, in the order in
14012 which they were declared. The representation of each node is as
14013 described by cp_parser_base_specifier.
14015 In the case that no bases are specified, this function will return
14016 NULL_TREE, not ERROR_MARK_NODE. */
14018 static tree
14019 cp_parser_base_clause (cp_parser* parser)
14021 tree bases = NULL_TREE;
14023 /* Look for the `:' that begins the list. */
14024 cp_parser_require (parser, CPP_COLON, "`:'");
14026 /* Scan the base-specifier-list. */
14027 while (true)
14029 cp_token *token;
14030 tree base;
14032 /* Look for the base-specifier. */
14033 base = cp_parser_base_specifier (parser);
14034 /* Add BASE to the front of the list. */
14035 if (base != error_mark_node)
14037 TREE_CHAIN (base) = bases;
14038 bases = base;
14040 /* Peek at the next token. */
14041 token = cp_lexer_peek_token (parser->lexer);
14042 /* If it's not a comma, then the list is complete. */
14043 if (token->type != CPP_COMMA)
14044 break;
14045 /* Consume the `,'. */
14046 cp_lexer_consume_token (parser->lexer);
14049 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14050 base class had a qualified name. However, the next name that
14051 appears is certainly not qualified. */
14052 parser->scope = NULL_TREE;
14053 parser->qualifying_scope = NULL_TREE;
14054 parser->object_scope = NULL_TREE;
14056 return nreverse (bases);
14059 /* Parse a base-specifier.
14061 base-specifier:
14062 :: [opt] nested-name-specifier [opt] class-name
14063 virtual access-specifier [opt] :: [opt] nested-name-specifier
14064 [opt] class-name
14065 access-specifier virtual [opt] :: [opt] nested-name-specifier
14066 [opt] class-name
14068 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14069 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14070 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14071 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14073 static tree
14074 cp_parser_base_specifier (cp_parser* parser)
14076 cp_token *token;
14077 bool done = false;
14078 bool virtual_p = false;
14079 bool duplicate_virtual_error_issued_p = false;
14080 bool duplicate_access_error_issued_p = false;
14081 bool class_scope_p, template_p;
14082 tree access = access_default_node;
14083 tree type;
14085 /* Process the optional `virtual' and `access-specifier'. */
14086 while (!done)
14088 /* Peek at the next token. */
14089 token = cp_lexer_peek_token (parser->lexer);
14090 /* Process `virtual'. */
14091 switch (token->keyword)
14093 case RID_VIRTUAL:
14094 /* If `virtual' appears more than once, issue an error. */
14095 if (virtual_p && !duplicate_virtual_error_issued_p)
14097 cp_parser_error (parser,
14098 "%<virtual%> specified more than once in base-specified");
14099 duplicate_virtual_error_issued_p = true;
14102 virtual_p = true;
14104 /* Consume the `virtual' token. */
14105 cp_lexer_consume_token (parser->lexer);
14107 break;
14109 case RID_PUBLIC:
14110 case RID_PROTECTED:
14111 case RID_PRIVATE:
14112 /* If more than one access specifier appears, issue an
14113 error. */
14114 if (access != access_default_node
14115 && !duplicate_access_error_issued_p)
14117 cp_parser_error (parser,
14118 "more than one access specifier in base-specified");
14119 duplicate_access_error_issued_p = true;
14122 access = ridpointers[(int) token->keyword];
14124 /* Consume the access-specifier. */
14125 cp_lexer_consume_token (parser->lexer);
14127 break;
14129 default:
14130 done = true;
14131 break;
14134 /* It is not uncommon to see programs mechanically, erroneously, use
14135 the 'typename' keyword to denote (dependent) qualified types
14136 as base classes. */
14137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14139 if (!processing_template_decl)
14140 error ("keyword %<typename%> not allowed outside of templates");
14141 else
14142 error ("keyword %<typename%> not allowed in this context "
14143 "(the base class is implicitly a type)");
14144 cp_lexer_consume_token (parser->lexer);
14147 /* Look for the optional `::' operator. */
14148 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14149 /* Look for the nested-name-specifier. The simplest way to
14150 implement:
14152 [temp.res]
14154 The keyword `typename' is not permitted in a base-specifier or
14155 mem-initializer; in these contexts a qualified name that
14156 depends on a template-parameter is implicitly assumed to be a
14157 type name.
14159 is to pretend that we have seen the `typename' keyword at this
14160 point. */
14161 cp_parser_nested_name_specifier_opt (parser,
14162 /*typename_keyword_p=*/true,
14163 /*check_dependency_p=*/true,
14164 typename_type,
14165 /*is_declaration=*/true);
14166 /* If the base class is given by a qualified name, assume that names
14167 we see are type names or templates, as appropriate. */
14168 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14169 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14171 /* Finally, look for the class-name. */
14172 type = cp_parser_class_name (parser,
14173 class_scope_p,
14174 template_p,
14175 typename_type,
14176 /*check_dependency_p=*/true,
14177 /*class_head_p=*/false,
14178 /*is_declaration=*/true);
14180 if (type == error_mark_node)
14181 return error_mark_node;
14183 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14186 /* Exception handling [gram.exception] */
14188 /* Parse an (optional) exception-specification.
14190 exception-specification:
14191 throw ( type-id-list [opt] )
14193 Returns a TREE_LIST representing the exception-specification. The
14194 TREE_VALUE of each node is a type. */
14196 static tree
14197 cp_parser_exception_specification_opt (cp_parser* parser)
14199 cp_token *token;
14200 tree type_id_list;
14202 /* Peek at the next token. */
14203 token = cp_lexer_peek_token (parser->lexer);
14204 /* If it's not `throw', then there's no exception-specification. */
14205 if (!cp_parser_is_keyword (token, RID_THROW))
14206 return NULL_TREE;
14208 /* Consume the `throw'. */
14209 cp_lexer_consume_token (parser->lexer);
14211 /* Look for the `('. */
14212 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14214 /* Peek at the next token. */
14215 token = cp_lexer_peek_token (parser->lexer);
14216 /* If it's not a `)', then there is a type-id-list. */
14217 if (token->type != CPP_CLOSE_PAREN)
14219 const char *saved_message;
14221 /* Types may not be defined in an exception-specification. */
14222 saved_message = parser->type_definition_forbidden_message;
14223 parser->type_definition_forbidden_message
14224 = "types may not be defined in an exception-specification";
14225 /* Parse the type-id-list. */
14226 type_id_list = cp_parser_type_id_list (parser);
14227 /* Restore the saved message. */
14228 parser->type_definition_forbidden_message = saved_message;
14230 else
14231 type_id_list = empty_except_spec;
14233 /* Look for the `)'. */
14234 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14236 return type_id_list;
14239 /* Parse an (optional) type-id-list.
14241 type-id-list:
14242 type-id
14243 type-id-list , type-id
14245 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14246 in the order that the types were presented. */
14248 static tree
14249 cp_parser_type_id_list (cp_parser* parser)
14251 tree types = NULL_TREE;
14253 while (true)
14255 cp_token *token;
14256 tree type;
14258 /* Get the next type-id. */
14259 type = cp_parser_type_id (parser);
14260 /* Add it to the list. */
14261 types = add_exception_specifier (types, type, /*complain=*/1);
14262 /* Peek at the next token. */
14263 token = cp_lexer_peek_token (parser->lexer);
14264 /* If it is not a `,', we are done. */
14265 if (token->type != CPP_COMMA)
14266 break;
14267 /* Consume the `,'. */
14268 cp_lexer_consume_token (parser->lexer);
14271 return nreverse (types);
14274 /* Parse a try-block.
14276 try-block:
14277 try compound-statement handler-seq */
14279 static tree
14280 cp_parser_try_block (cp_parser* parser)
14282 tree try_block;
14284 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14285 try_block = begin_try_block ();
14286 cp_parser_compound_statement (parser, NULL, true);
14287 finish_try_block (try_block);
14288 cp_parser_handler_seq (parser);
14289 finish_handler_sequence (try_block);
14291 return try_block;
14294 /* Parse a function-try-block.
14296 function-try-block:
14297 try ctor-initializer [opt] function-body handler-seq */
14299 static bool
14300 cp_parser_function_try_block (cp_parser* parser)
14302 tree compound_stmt;
14303 tree try_block;
14304 bool ctor_initializer_p;
14306 /* Look for the `try' keyword. */
14307 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14308 return false;
14309 /* Let the rest of the front-end know where we are. */
14310 try_block = begin_function_try_block (&compound_stmt);
14311 /* Parse the function-body. */
14312 ctor_initializer_p
14313 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14314 /* We're done with the `try' part. */
14315 finish_function_try_block (try_block);
14316 /* Parse the handlers. */
14317 cp_parser_handler_seq (parser);
14318 /* We're done with the handlers. */
14319 finish_function_handler_sequence (try_block, compound_stmt);
14321 return ctor_initializer_p;
14324 /* Parse a handler-seq.
14326 handler-seq:
14327 handler handler-seq [opt] */
14329 static void
14330 cp_parser_handler_seq (cp_parser* parser)
14332 while (true)
14334 cp_token *token;
14336 /* Parse the handler. */
14337 cp_parser_handler (parser);
14338 /* Peek at the next token. */
14339 token = cp_lexer_peek_token (parser->lexer);
14340 /* If it's not `catch' then there are no more handlers. */
14341 if (!cp_parser_is_keyword (token, RID_CATCH))
14342 break;
14346 /* Parse a handler.
14348 handler:
14349 catch ( exception-declaration ) compound-statement */
14351 static void
14352 cp_parser_handler (cp_parser* parser)
14354 tree handler;
14355 tree declaration;
14357 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14358 handler = begin_handler ();
14359 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14360 declaration = cp_parser_exception_declaration (parser);
14361 finish_handler_parms (declaration, handler);
14362 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14363 cp_parser_compound_statement (parser, NULL, false);
14364 finish_handler (handler);
14367 /* Parse an exception-declaration.
14369 exception-declaration:
14370 type-specifier-seq declarator
14371 type-specifier-seq abstract-declarator
14372 type-specifier-seq
14375 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14376 ellipsis variant is used. */
14378 static tree
14379 cp_parser_exception_declaration (cp_parser* parser)
14381 tree decl;
14382 cp_decl_specifier_seq type_specifiers;
14383 cp_declarator *declarator;
14384 const char *saved_message;
14386 /* If it's an ellipsis, it's easy to handle. */
14387 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14389 /* Consume the `...' token. */
14390 cp_lexer_consume_token (parser->lexer);
14391 return NULL_TREE;
14394 /* Types may not be defined in exception-declarations. */
14395 saved_message = parser->type_definition_forbidden_message;
14396 parser->type_definition_forbidden_message
14397 = "types may not be defined in exception-declarations";
14399 /* Parse the type-specifier-seq. */
14400 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14401 &type_specifiers);
14402 /* If it's a `)', then there is no declarator. */
14403 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14404 declarator = NULL;
14405 else
14406 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14407 /*ctor_dtor_or_conv_p=*/NULL,
14408 /*parenthesized_p=*/NULL,
14409 /*member_p=*/false);
14411 /* Restore the saved message. */
14412 parser->type_definition_forbidden_message = saved_message;
14414 if (type_specifiers.any_specifiers_p)
14416 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14417 if (decl == NULL_TREE)
14418 error ("invalid catch parameter");
14420 else
14421 decl = NULL_TREE;
14423 return decl;
14426 /* Parse a throw-expression.
14428 throw-expression:
14429 throw assignment-expression [opt]
14431 Returns a THROW_EXPR representing the throw-expression. */
14433 static tree
14434 cp_parser_throw_expression (cp_parser* parser)
14436 tree expression;
14437 cp_token* token;
14439 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14440 token = cp_lexer_peek_token (parser->lexer);
14441 /* Figure out whether or not there is an assignment-expression
14442 following the "throw" keyword. */
14443 if (token->type == CPP_COMMA
14444 || token->type == CPP_SEMICOLON
14445 || token->type == CPP_CLOSE_PAREN
14446 || token->type == CPP_CLOSE_SQUARE
14447 || token->type == CPP_CLOSE_BRACE
14448 || token->type == CPP_COLON)
14449 expression = NULL_TREE;
14450 else
14451 expression = cp_parser_assignment_expression (parser,
14452 /*cast_p=*/false);
14454 return build_throw (expression);
14457 /* GNU Extensions */
14459 /* Parse an (optional) asm-specification.
14461 asm-specification:
14462 asm ( string-literal )
14464 If the asm-specification is present, returns a STRING_CST
14465 corresponding to the string-literal. Otherwise, returns
14466 NULL_TREE. */
14468 static tree
14469 cp_parser_asm_specification_opt (cp_parser* parser)
14471 cp_token *token;
14472 tree asm_specification;
14474 /* Peek at the next token. */
14475 token = cp_lexer_peek_token (parser->lexer);
14476 /* If the next token isn't the `asm' keyword, then there's no
14477 asm-specification. */
14478 if (!cp_parser_is_keyword (token, RID_ASM))
14479 return NULL_TREE;
14481 /* Consume the `asm' token. */
14482 cp_lexer_consume_token (parser->lexer);
14483 /* Look for the `('. */
14484 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14486 /* Look for the string-literal. */
14487 asm_specification = cp_parser_string_literal (parser, false, false);
14489 /* Look for the `)'. */
14490 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14492 return asm_specification;
14495 /* Parse an asm-operand-list.
14497 asm-operand-list:
14498 asm-operand
14499 asm-operand-list , asm-operand
14501 asm-operand:
14502 string-literal ( expression )
14503 [ string-literal ] string-literal ( expression )
14505 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14506 each node is the expression. The TREE_PURPOSE is itself a
14507 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14508 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14509 is a STRING_CST for the string literal before the parenthesis. */
14511 static tree
14512 cp_parser_asm_operand_list (cp_parser* parser)
14514 tree asm_operands = NULL_TREE;
14516 while (true)
14518 tree string_literal;
14519 tree expression;
14520 tree name;
14522 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14524 /* Consume the `[' token. */
14525 cp_lexer_consume_token (parser->lexer);
14526 /* Read the operand name. */
14527 name = cp_parser_identifier (parser);
14528 if (name != error_mark_node)
14529 name = build_string (IDENTIFIER_LENGTH (name),
14530 IDENTIFIER_POINTER (name));
14531 /* Look for the closing `]'. */
14532 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14534 else
14535 name = NULL_TREE;
14536 /* Look for the string-literal. */
14537 string_literal = cp_parser_string_literal (parser, false, false);
14539 /* Look for the `('. */
14540 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14541 /* Parse the expression. */
14542 expression = cp_parser_expression (parser, /*cast_p=*/false);
14543 /* Look for the `)'. */
14544 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14546 /* Add this operand to the list. */
14547 asm_operands = tree_cons (build_tree_list (name, string_literal),
14548 expression,
14549 asm_operands);
14550 /* If the next token is not a `,', there are no more
14551 operands. */
14552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14553 break;
14554 /* Consume the `,'. */
14555 cp_lexer_consume_token (parser->lexer);
14558 return nreverse (asm_operands);
14561 /* Parse an asm-clobber-list.
14563 asm-clobber-list:
14564 string-literal
14565 asm-clobber-list , string-literal
14567 Returns a TREE_LIST, indicating the clobbers in the order that they
14568 appeared. The TREE_VALUE of each node is a STRING_CST. */
14570 static tree
14571 cp_parser_asm_clobber_list (cp_parser* parser)
14573 tree clobbers = NULL_TREE;
14575 while (true)
14577 tree string_literal;
14579 /* Look for the string literal. */
14580 string_literal = cp_parser_string_literal (parser, false, false);
14581 /* Add it to the list. */
14582 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14583 /* If the next token is not a `,', then the list is
14584 complete. */
14585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14586 break;
14587 /* Consume the `,' token. */
14588 cp_lexer_consume_token (parser->lexer);
14591 return clobbers;
14594 /* Parse an (optional) series of attributes.
14596 attributes:
14597 attributes attribute
14599 attribute:
14600 __attribute__ (( attribute-list [opt] ))
14602 The return value is as for cp_parser_attribute_list. */
14604 static tree
14605 cp_parser_attributes_opt (cp_parser* parser)
14607 tree attributes = NULL_TREE;
14609 while (true)
14611 cp_token *token;
14612 tree attribute_list;
14614 /* Peek at the next token. */
14615 token = cp_lexer_peek_token (parser->lexer);
14616 /* If it's not `__attribute__', then we're done. */
14617 if (token->keyword != RID_ATTRIBUTE)
14618 break;
14620 /* Consume the `__attribute__' keyword. */
14621 cp_lexer_consume_token (parser->lexer);
14622 /* Look for the two `(' tokens. */
14623 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14624 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14626 /* Peek at the next token. */
14627 token = cp_lexer_peek_token (parser->lexer);
14628 if (token->type != CPP_CLOSE_PAREN)
14629 /* Parse the attribute-list. */
14630 attribute_list = cp_parser_attribute_list (parser);
14631 else
14632 /* If the next token is a `)', then there is no attribute
14633 list. */
14634 attribute_list = NULL;
14636 /* Look for the two `)' tokens. */
14637 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14638 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14640 /* Add these new attributes to the list. */
14641 attributes = chainon (attributes, attribute_list);
14644 return attributes;
14647 /* Parse an attribute-list.
14649 attribute-list:
14650 attribute
14651 attribute-list , attribute
14653 attribute:
14654 identifier
14655 identifier ( identifier )
14656 identifier ( identifier , expression-list )
14657 identifier ( expression-list )
14659 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14660 to an attribute. The TREE_PURPOSE of each node is the identifier
14661 indicating which attribute is in use. The TREE_VALUE represents
14662 the arguments, if any. */
14664 static tree
14665 cp_parser_attribute_list (cp_parser* parser)
14667 tree attribute_list = NULL_TREE;
14668 bool save_translate_strings_p = parser->translate_strings_p;
14670 parser->translate_strings_p = false;
14671 while (true)
14673 cp_token *token;
14674 tree identifier;
14675 tree attribute;
14677 /* Look for the identifier. We also allow keywords here; for
14678 example `__attribute__ ((const))' is legal. */
14679 token = cp_lexer_peek_token (parser->lexer);
14680 if (token->type == CPP_NAME
14681 || token->type == CPP_KEYWORD)
14683 tree arguments = NULL_TREE;
14685 /* Consume the token. */
14686 token = cp_lexer_consume_token (parser->lexer);
14688 /* Save away the identifier that indicates which attribute
14689 this is. */
14690 identifier = token->value;
14691 attribute = build_tree_list (identifier, NULL_TREE);
14693 /* Peek at the next token. */
14694 token = cp_lexer_peek_token (parser->lexer);
14695 /* If it's an `(', then parse the attribute arguments. */
14696 if (token->type == CPP_OPEN_PAREN)
14698 arguments = cp_parser_parenthesized_expression_list
14699 (parser, true, /*cast_p=*/false,
14700 /*non_constant_p=*/NULL);
14701 /* Save the arguments away. */
14702 TREE_VALUE (attribute) = arguments;
14705 if (arguments != error_mark_node)
14707 /* Add this attribute to the list. */
14708 TREE_CHAIN (attribute) = attribute_list;
14709 attribute_list = attribute;
14712 token = cp_lexer_peek_token (parser->lexer);
14714 /* Now, look for more attributes. If the next token isn't a
14715 `,', we're done. */
14716 if (token->type != CPP_COMMA)
14717 break;
14719 /* Consume the comma and keep going. */
14720 cp_lexer_consume_token (parser->lexer);
14722 parser->translate_strings_p = save_translate_strings_p;
14724 /* We built up the list in reverse order. */
14725 return nreverse (attribute_list);
14728 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14729 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14730 current value of the PEDANTIC flag, regardless of whether or not
14731 the `__extension__' keyword is present. The caller is responsible
14732 for restoring the value of the PEDANTIC flag. */
14734 static bool
14735 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14737 /* Save the old value of the PEDANTIC flag. */
14738 *saved_pedantic = pedantic;
14740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14742 /* Consume the `__extension__' token. */
14743 cp_lexer_consume_token (parser->lexer);
14744 /* We're not being pedantic while the `__extension__' keyword is
14745 in effect. */
14746 pedantic = 0;
14748 return true;
14751 return false;
14754 /* Parse a label declaration.
14756 label-declaration:
14757 __label__ label-declarator-seq ;
14759 label-declarator-seq:
14760 identifier , label-declarator-seq
14761 identifier */
14763 static void
14764 cp_parser_label_declaration (cp_parser* parser)
14766 /* Look for the `__label__' keyword. */
14767 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14769 while (true)
14771 tree identifier;
14773 /* Look for an identifier. */
14774 identifier = cp_parser_identifier (parser);
14775 /* If we failed, stop. */
14776 if (identifier == error_mark_node)
14777 break;
14778 /* Declare it as a label. */
14779 finish_label_decl (identifier);
14780 /* If the next token is a `;', stop. */
14781 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14782 break;
14783 /* Look for the `,' separating the label declarations. */
14784 cp_parser_require (parser, CPP_COMMA, "`,'");
14787 /* Look for the final `;'. */
14788 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14791 /* Support Functions */
14793 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14794 NAME should have one of the representations used for an
14795 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14796 is returned. If PARSER->SCOPE is a dependent type, then a
14797 SCOPE_REF is returned.
14799 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14800 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14801 was formed. Abstractly, such entities should not be passed to this
14802 function, because they do not need to be looked up, but it is
14803 simpler to check for this special case here, rather than at the
14804 call-sites.
14806 In cases not explicitly covered above, this function returns a
14807 DECL, OVERLOAD, or baselink representing the result of the lookup.
14808 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14809 is returned.
14811 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14812 (e.g., "struct") that was used. In that case bindings that do not
14813 refer to types are ignored.
14815 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14816 ignored.
14818 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14819 are ignored.
14821 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14822 types.
14824 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14825 TREE_LIST of candiates if name-lookup results in an ambiguity, and
14826 NULL_TREE otherwise. */
14828 static tree
14829 cp_parser_lookup_name (cp_parser *parser, tree name,
14830 enum tag_types tag_type,
14831 bool is_template,
14832 bool is_namespace,
14833 bool check_dependency,
14834 tree *ambiguous_decls)
14836 int flags = 0;
14837 tree decl;
14838 tree object_type = parser->context->object_type;
14840 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14841 flags |= LOOKUP_COMPLAIN;
14843 /* Assume that the lookup will be unambiguous. */
14844 if (ambiguous_decls)
14845 *ambiguous_decls = NULL_TREE;
14847 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14848 no longer valid. Note that if we are parsing tentatively, and
14849 the parse fails, OBJECT_TYPE will be automatically restored. */
14850 parser->context->object_type = NULL_TREE;
14852 if (name == error_mark_node)
14853 return error_mark_node;
14855 /* A template-id has already been resolved; there is no lookup to
14856 do. */
14857 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14858 return name;
14859 if (BASELINK_P (name))
14861 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14862 == TEMPLATE_ID_EXPR);
14863 return name;
14866 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14867 it should already have been checked to make sure that the name
14868 used matches the type being destroyed. */
14869 if (TREE_CODE (name) == BIT_NOT_EXPR)
14871 tree type;
14873 /* Figure out to which type this destructor applies. */
14874 if (parser->scope)
14875 type = parser->scope;
14876 else if (object_type)
14877 type = object_type;
14878 else
14879 type = current_class_type;
14880 /* If that's not a class type, there is no destructor. */
14881 if (!type || !CLASS_TYPE_P (type))
14882 return error_mark_node;
14883 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14884 lazily_declare_fn (sfk_destructor, type);
14885 if (!CLASSTYPE_DESTRUCTORS (type))
14886 return error_mark_node;
14887 /* If it was a class type, return the destructor. */
14888 return CLASSTYPE_DESTRUCTORS (type);
14891 /* By this point, the NAME should be an ordinary identifier. If
14892 the id-expression was a qualified name, the qualifying scope is
14893 stored in PARSER->SCOPE at this point. */
14894 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14896 /* Perform the lookup. */
14897 if (parser->scope)
14899 bool dependent_p;
14901 if (parser->scope == error_mark_node)
14902 return error_mark_node;
14904 /* If the SCOPE is dependent, the lookup must be deferred until
14905 the template is instantiated -- unless we are explicitly
14906 looking up names in uninstantiated templates. Even then, we
14907 cannot look up the name if the scope is not a class type; it
14908 might, for example, be a template type parameter. */
14909 dependent_p = (TYPE_P (parser->scope)
14910 && !(parser->in_declarator_p
14911 && currently_open_class (parser->scope))
14912 && dependent_type_p (parser->scope));
14913 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14914 && dependent_p)
14916 if (tag_type)
14918 tree type;
14920 /* The resolution to Core Issue 180 says that `struct
14921 A::B' should be considered a type-name, even if `A'
14922 is dependent. */
14923 type = make_typename_type (parser->scope, name, tag_type,
14924 /*complain=*/1);
14925 decl = TYPE_NAME (type);
14927 else if (is_template
14928 && (cp_parser_next_token_ends_template_argument_p (parser)
14929 || cp_lexer_next_token_is (parser->lexer,
14930 CPP_CLOSE_PAREN)))
14931 decl = make_unbound_class_template (parser->scope,
14932 name, NULL_TREE,
14933 /*complain=*/1);
14934 else
14935 decl = build_qualified_name (/*type=*/NULL_TREE,
14936 parser->scope, name,
14937 is_template);
14939 else
14941 tree pushed_scope = NULL_TREE;
14943 /* If PARSER->SCOPE is a dependent type, then it must be a
14944 class type, and we must not be checking dependencies;
14945 otherwise, we would have processed this lookup above. So
14946 that PARSER->SCOPE is not considered a dependent base by
14947 lookup_member, we must enter the scope here. */
14948 if (dependent_p)
14949 pushed_scope = push_scope (parser->scope);
14950 /* If the PARSER->SCOPE is a template specialization, it
14951 may be instantiated during name lookup. In that case,
14952 errors may be issued. Even if we rollback the current
14953 tentative parse, those errors are valid. */
14954 decl = lookup_qualified_name (parser->scope, name,
14955 tag_type != none_type,
14956 /*complain=*/true);
14957 if (pushed_scope)
14958 pop_scope (pushed_scope);
14960 parser->qualifying_scope = parser->scope;
14961 parser->object_scope = NULL_TREE;
14963 else if (object_type)
14965 tree object_decl = NULL_TREE;
14966 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14967 OBJECT_TYPE is not a class. */
14968 if (CLASS_TYPE_P (object_type))
14969 /* If the OBJECT_TYPE is a template specialization, it may
14970 be instantiated during name lookup. In that case, errors
14971 may be issued. Even if we rollback the current tentative
14972 parse, those errors are valid. */
14973 object_decl = lookup_member (object_type,
14974 name,
14975 /*protect=*/0,
14976 tag_type != none_type);
14977 /* Look it up in the enclosing context, too. */
14978 decl = lookup_name_real (name, tag_type != none_type,
14979 /*nonclass=*/0,
14980 /*block_p=*/true, is_namespace, flags);
14981 parser->object_scope = object_type;
14982 parser->qualifying_scope = NULL_TREE;
14983 if (object_decl)
14984 decl = object_decl;
14986 else
14988 decl = lookup_name_real (name, tag_type != none_type,
14989 /*nonclass=*/0,
14990 /*block_p=*/true, is_namespace, flags);
14991 parser->qualifying_scope = NULL_TREE;
14992 parser->object_scope = NULL_TREE;
14995 /* If the lookup failed, let our caller know. */
14996 if (!decl || decl == error_mark_node)
14997 return error_mark_node;
14999 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15000 if (TREE_CODE (decl) == TREE_LIST)
15002 if (ambiguous_decls)
15003 *ambiguous_decls = decl;
15004 /* The error message we have to print is too complicated for
15005 cp_parser_error, so we incorporate its actions directly. */
15006 if (!cp_parser_simulate_error (parser))
15008 error ("reference to %qD is ambiguous", name);
15009 print_candidates (decl);
15011 return error_mark_node;
15014 gcc_assert (DECL_P (decl)
15015 || TREE_CODE (decl) == OVERLOAD
15016 || TREE_CODE (decl) == SCOPE_REF
15017 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15018 || BASELINK_P (decl));
15020 /* If we have resolved the name of a member declaration, check to
15021 see if the declaration is accessible. When the name resolves to
15022 set of overloaded functions, accessibility is checked when
15023 overload resolution is done.
15025 During an explicit instantiation, access is not checked at all,
15026 as per [temp.explicit]. */
15027 if (DECL_P (decl))
15028 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15030 return decl;
15033 /* Like cp_parser_lookup_name, but for use in the typical case where
15034 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15035 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15037 static tree
15038 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15040 return cp_parser_lookup_name (parser, name,
15041 none_type,
15042 /*is_template=*/false,
15043 /*is_namespace=*/false,
15044 /*check_dependency=*/true,
15045 /*ambiguous_decls=*/NULL);
15048 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15049 the current context, return the TYPE_DECL. If TAG_NAME_P is
15050 true, the DECL indicates the class being defined in a class-head,
15051 or declared in an elaborated-type-specifier.
15053 Otherwise, return DECL. */
15055 static tree
15056 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15058 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15059 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15061 struct A {
15062 template <typename T> struct B;
15065 template <typename T> struct A::B {};
15067 Similarly, in an elaborated-type-specifier:
15069 namespace N { struct X{}; }
15071 struct A {
15072 template <typename T> friend struct N::X;
15075 However, if the DECL refers to a class type, and we are in
15076 the scope of the class, then the name lookup automatically
15077 finds the TYPE_DECL created by build_self_reference rather
15078 than a TEMPLATE_DECL. For example, in:
15080 template <class T> struct S {
15081 S s;
15084 there is no need to handle such case. */
15086 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15087 return DECL_TEMPLATE_RESULT (decl);
15089 return decl;
15092 /* If too many, or too few, template-parameter lists apply to the
15093 declarator, issue an error message. Returns TRUE if all went well,
15094 and FALSE otherwise. */
15096 static bool
15097 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15098 cp_declarator *declarator)
15100 unsigned num_templates;
15102 /* We haven't seen any classes that involve template parameters yet. */
15103 num_templates = 0;
15105 switch (declarator->kind)
15107 case cdk_id:
15108 if (declarator->u.id.qualifying_scope)
15110 tree scope;
15111 tree member;
15113 scope = declarator->u.id.qualifying_scope;
15114 member = declarator->u.id.unqualified_name;
15116 while (scope && CLASS_TYPE_P (scope))
15118 /* You're supposed to have one `template <...>'
15119 for every template class, but you don't need one
15120 for a full specialization. For example:
15122 template <class T> struct S{};
15123 template <> struct S<int> { void f(); };
15124 void S<int>::f () {}
15126 is correct; there shouldn't be a `template <>' for
15127 the definition of `S<int>::f'. */
15128 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15129 /* If SCOPE does not have template information of any
15130 kind, then it is not a template, nor is it nested
15131 within a template. */
15132 break;
15133 if (explicit_class_specialization_p (scope))
15134 break;
15135 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15136 ++num_templates;
15138 scope = TYPE_CONTEXT (scope);
15141 else if (TREE_CODE (declarator->u.id.unqualified_name)
15142 == TEMPLATE_ID_EXPR)
15143 /* If the DECLARATOR has the form `X<y>' then it uses one
15144 additional level of template parameters. */
15145 ++num_templates;
15147 return cp_parser_check_template_parameters (parser,
15148 num_templates);
15150 case cdk_function:
15151 case cdk_array:
15152 case cdk_pointer:
15153 case cdk_reference:
15154 case cdk_ptrmem:
15155 return (cp_parser_check_declarator_template_parameters
15156 (parser, declarator->declarator));
15158 case cdk_error:
15159 return true;
15161 default:
15162 gcc_unreachable ();
15164 return false;
15167 /* NUM_TEMPLATES were used in the current declaration. If that is
15168 invalid, return FALSE and issue an error messages. Otherwise,
15169 return TRUE. */
15171 static bool
15172 cp_parser_check_template_parameters (cp_parser* parser,
15173 unsigned num_templates)
15175 /* If there are more template classes than parameter lists, we have
15176 something like:
15178 template <class T> void S<T>::R<T>::f (); */
15179 if (parser->num_template_parameter_lists < num_templates)
15181 error ("too few template-parameter-lists");
15182 return false;
15184 /* If there are the same number of template classes and parameter
15185 lists, that's OK. */
15186 if (parser->num_template_parameter_lists == num_templates)
15187 return true;
15188 /* If there are more, but only one more, then we are referring to a
15189 member template. That's OK too. */
15190 if (parser->num_template_parameter_lists == num_templates + 1)
15191 return true;
15192 /* Otherwise, there are too many template parameter lists. We have
15193 something like:
15195 template <class T> template <class U> void S::f(); */
15196 error ("too many template-parameter-lists");
15197 return false;
15200 /* Parse an optional `::' token indicating that the following name is
15201 from the global namespace. If so, PARSER->SCOPE is set to the
15202 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15203 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15204 Returns the new value of PARSER->SCOPE, if the `::' token is
15205 present, and NULL_TREE otherwise. */
15207 static tree
15208 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15210 cp_token *token;
15212 /* Peek at the next token. */
15213 token = cp_lexer_peek_token (parser->lexer);
15214 /* If we're looking at a `::' token then we're starting from the
15215 global namespace, not our current location. */
15216 if (token->type == CPP_SCOPE)
15218 /* Consume the `::' token. */
15219 cp_lexer_consume_token (parser->lexer);
15220 /* Set the SCOPE so that we know where to start the lookup. */
15221 parser->scope = global_namespace;
15222 parser->qualifying_scope = global_namespace;
15223 parser->object_scope = NULL_TREE;
15225 return parser->scope;
15227 else if (!current_scope_valid_p)
15229 parser->scope = NULL_TREE;
15230 parser->qualifying_scope = NULL_TREE;
15231 parser->object_scope = NULL_TREE;
15234 return NULL_TREE;
15237 /* Returns TRUE if the upcoming token sequence is the start of a
15238 constructor declarator. If FRIEND_P is true, the declarator is
15239 preceded by the `friend' specifier. */
15241 static bool
15242 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15244 bool constructor_p;
15245 tree type_decl = NULL_TREE;
15246 bool nested_name_p;
15247 cp_token *next_token;
15249 /* The common case is that this is not a constructor declarator, so
15250 try to avoid doing lots of work if at all possible. It's not
15251 valid declare a constructor at function scope. */
15252 if (parser->in_function_body)
15253 return false;
15254 /* And only certain tokens can begin a constructor declarator. */
15255 next_token = cp_lexer_peek_token (parser->lexer);
15256 if (next_token->type != CPP_NAME
15257 && next_token->type != CPP_SCOPE
15258 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15259 && next_token->type != CPP_TEMPLATE_ID)
15260 return false;
15262 /* Parse tentatively; we are going to roll back all of the tokens
15263 consumed here. */
15264 cp_parser_parse_tentatively (parser);
15265 /* Assume that we are looking at a constructor declarator. */
15266 constructor_p = true;
15268 /* Look for the optional `::' operator. */
15269 cp_parser_global_scope_opt (parser,
15270 /*current_scope_valid_p=*/false);
15271 /* Look for the nested-name-specifier. */
15272 nested_name_p
15273 = (cp_parser_nested_name_specifier_opt (parser,
15274 /*typename_keyword_p=*/false,
15275 /*check_dependency_p=*/false,
15276 /*type_p=*/false,
15277 /*is_declaration=*/false)
15278 != NULL_TREE);
15279 /* Outside of a class-specifier, there must be a
15280 nested-name-specifier. */
15281 if (!nested_name_p &&
15282 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15283 || friend_p))
15284 constructor_p = false;
15285 /* If we still think that this might be a constructor-declarator,
15286 look for a class-name. */
15287 if (constructor_p)
15289 /* If we have:
15291 template <typename T> struct S { S(); };
15292 template <typename T> S<T>::S ();
15294 we must recognize that the nested `S' names a class.
15295 Similarly, for:
15297 template <typename T> S<T>::S<T> ();
15299 we must recognize that the nested `S' names a template. */
15300 type_decl = cp_parser_class_name (parser,
15301 /*typename_keyword_p=*/false,
15302 /*template_keyword_p=*/false,
15303 none_type,
15304 /*check_dependency_p=*/false,
15305 /*class_head_p=*/false,
15306 /*is_declaration=*/false);
15307 /* If there was no class-name, then this is not a constructor. */
15308 constructor_p = !cp_parser_error_occurred (parser);
15311 /* If we're still considering a constructor, we have to see a `(',
15312 to begin the parameter-declaration-clause, followed by either a
15313 `)', an `...', or a decl-specifier. We need to check for a
15314 type-specifier to avoid being fooled into thinking that:
15316 S::S (f) (int);
15318 is a constructor. (It is actually a function named `f' that
15319 takes one parameter (of type `int') and returns a value of type
15320 `S::S'. */
15321 if (constructor_p
15322 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15325 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15326 /* A parameter declaration begins with a decl-specifier,
15327 which is either the "attribute" keyword, a storage class
15328 specifier, or (usually) a type-specifier. */
15329 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15331 tree type;
15332 tree pushed_scope = NULL_TREE;
15333 unsigned saved_num_template_parameter_lists;
15335 /* Names appearing in the type-specifier should be looked up
15336 in the scope of the class. */
15337 if (current_class_type)
15338 type = NULL_TREE;
15339 else
15341 type = TREE_TYPE (type_decl);
15342 if (TREE_CODE (type) == TYPENAME_TYPE)
15344 type = resolve_typename_type (type,
15345 /*only_current_p=*/false);
15346 if (type == error_mark_node)
15348 cp_parser_abort_tentative_parse (parser);
15349 return false;
15352 pushed_scope = push_scope (type);
15355 /* Inside the constructor parameter list, surrounding
15356 template-parameter-lists do not apply. */
15357 saved_num_template_parameter_lists
15358 = parser->num_template_parameter_lists;
15359 parser->num_template_parameter_lists = 0;
15361 /* Look for the type-specifier. */
15362 cp_parser_type_specifier (parser,
15363 CP_PARSER_FLAGS_NONE,
15364 /*decl_specs=*/NULL,
15365 /*is_declarator=*/true,
15366 /*declares_class_or_enum=*/NULL,
15367 /*is_cv_qualifier=*/NULL);
15369 parser->num_template_parameter_lists
15370 = saved_num_template_parameter_lists;
15372 /* Leave the scope of the class. */
15373 if (pushed_scope)
15374 pop_scope (pushed_scope);
15376 constructor_p = !cp_parser_error_occurred (parser);
15379 else
15380 constructor_p = false;
15381 /* We did not really want to consume any tokens. */
15382 cp_parser_abort_tentative_parse (parser);
15384 return constructor_p;
15387 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15388 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15389 they must be performed once we are in the scope of the function.
15391 Returns the function defined. */
15393 static tree
15394 cp_parser_function_definition_from_specifiers_and_declarator
15395 (cp_parser* parser,
15396 cp_decl_specifier_seq *decl_specifiers,
15397 tree attributes,
15398 const cp_declarator *declarator)
15400 tree fn;
15401 bool success_p;
15403 /* Begin the function-definition. */
15404 success_p = start_function (decl_specifiers, declarator, attributes);
15406 /* The things we're about to see are not directly qualified by any
15407 template headers we've seen thus far. */
15408 reset_specialization ();
15410 /* If there were names looked up in the decl-specifier-seq that we
15411 did not check, check them now. We must wait until we are in the
15412 scope of the function to perform the checks, since the function
15413 might be a friend. */
15414 perform_deferred_access_checks ();
15416 if (!success_p)
15418 /* Skip the entire function. */
15419 error ("invalid function declaration");
15420 cp_parser_skip_to_end_of_block_or_statement (parser);
15421 fn = error_mark_node;
15423 else
15424 fn = cp_parser_function_definition_after_declarator (parser,
15425 /*inline_p=*/false);
15427 return fn;
15430 /* Parse the part of a function-definition that follows the
15431 declarator. INLINE_P is TRUE iff this function is an inline
15432 function defined with a class-specifier.
15434 Returns the function defined. */
15436 static tree
15437 cp_parser_function_definition_after_declarator (cp_parser* parser,
15438 bool inline_p)
15440 tree fn;
15441 bool ctor_initializer_p = false;
15442 bool saved_in_unbraced_linkage_specification_p;
15443 bool saved_in_function_body;
15444 unsigned saved_num_template_parameter_lists;
15446 saved_in_function_body = parser->in_function_body;
15447 parser->in_function_body = true;
15448 /* If the next token is `return', then the code may be trying to
15449 make use of the "named return value" extension that G++ used to
15450 support. */
15451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15453 /* Consume the `return' keyword. */
15454 cp_lexer_consume_token (parser->lexer);
15455 /* Look for the identifier that indicates what value is to be
15456 returned. */
15457 cp_parser_identifier (parser);
15458 /* Issue an error message. */
15459 error ("named return values are no longer supported");
15460 /* Skip tokens until we reach the start of the function body. */
15461 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15462 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15463 cp_lexer_consume_token (parser->lexer);
15465 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15466 anything declared inside `f'. */
15467 saved_in_unbraced_linkage_specification_p
15468 = parser->in_unbraced_linkage_specification_p;
15469 parser->in_unbraced_linkage_specification_p = false;
15470 /* Inside the function, surrounding template-parameter-lists do not
15471 apply. */
15472 saved_num_template_parameter_lists
15473 = parser->num_template_parameter_lists;
15474 parser->num_template_parameter_lists = 0;
15475 /* If the next token is `try', then we are looking at a
15476 function-try-block. */
15477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15478 ctor_initializer_p = cp_parser_function_try_block (parser);
15479 /* A function-try-block includes the function-body, so we only do
15480 this next part if we're not processing a function-try-block. */
15481 else
15482 ctor_initializer_p
15483 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15485 /* Finish the function. */
15486 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15487 (inline_p ? 2 : 0));
15488 /* Generate code for it, if necessary. */
15489 expand_or_defer_fn (fn);
15490 /* Restore the saved values. */
15491 parser->in_unbraced_linkage_specification_p
15492 = saved_in_unbraced_linkage_specification_p;
15493 parser->num_template_parameter_lists
15494 = saved_num_template_parameter_lists;
15495 parser->in_function_body = saved_in_function_body;
15497 return fn;
15500 /* Parse a template-declaration, assuming that the `export' (and
15501 `extern') keywords, if present, has already been scanned. MEMBER_P
15502 is as for cp_parser_template_declaration. */
15504 static void
15505 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15507 tree decl = NULL_TREE;
15508 tree checks;
15509 tree parameter_list;
15510 bool friend_p = false;
15511 bool need_lang_pop;
15513 /* Look for the `template' keyword. */
15514 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15515 return;
15517 /* And the `<'. */
15518 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15519 return;
15520 if (at_class_scope_p () && current_function_decl)
15522 /* 14.5.2.2 [temp.mem]
15524 A local class shall not have member templates. */
15525 error ("invalid declaration of member template in local class");
15526 cp_parser_skip_to_end_of_block_or_statement (parser);
15527 return;
15529 /* [temp]
15531 A template ... shall not have C linkage. */
15532 if (current_lang_name == lang_name_c)
15534 error ("template with C linkage");
15535 /* Give it C++ linkage to avoid confusing other parts of the
15536 front end. */
15537 push_lang_context (lang_name_cplusplus);
15538 need_lang_pop = true;
15540 else
15541 need_lang_pop = false;
15543 /* We cannot perform access checks on the template parameter
15544 declarations until we know what is being declared, just as we
15545 cannot check the decl-specifier list. */
15546 push_deferring_access_checks (dk_deferred);
15548 /* If the next token is `>', then we have an invalid
15549 specialization. Rather than complain about an invalid template
15550 parameter, issue an error message here. */
15551 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15553 cp_parser_error (parser, "invalid explicit specialization");
15554 begin_specialization ();
15555 parameter_list = NULL_TREE;
15557 else
15559 /* Parse the template parameters. */
15560 begin_template_parm_list ();
15561 parameter_list = cp_parser_template_parameter_list (parser);
15562 parameter_list = end_template_parm_list (parameter_list);
15565 /* Get the deferred access checks from the parameter list. These
15566 will be checked once we know what is being declared, as for a
15567 member template the checks must be performed in the scope of the
15568 class containing the member. */
15569 checks = get_deferred_access_checks ();
15571 /* Look for the `>'. */
15572 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15573 /* We just processed one more parameter list. */
15574 ++parser->num_template_parameter_lists;
15575 /* If the next token is `template', there are more template
15576 parameters. */
15577 if (cp_lexer_next_token_is_keyword (parser->lexer,
15578 RID_TEMPLATE))
15579 cp_parser_template_declaration_after_export (parser, member_p);
15580 else
15582 /* There are no access checks when parsing a template, as we do not
15583 know if a specialization will be a friend. */
15584 push_deferring_access_checks (dk_no_check);
15585 decl = cp_parser_single_declaration (parser,
15586 checks,
15587 member_p,
15588 &friend_p);
15589 pop_deferring_access_checks ();
15591 /* If this is a member template declaration, let the front
15592 end know. */
15593 if (member_p && !friend_p && decl)
15595 if (TREE_CODE (decl) == TYPE_DECL)
15596 cp_parser_check_access_in_redeclaration (decl);
15598 decl = finish_member_template_decl (decl);
15600 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15601 make_friend_class (current_class_type, TREE_TYPE (decl),
15602 /*complain=*/true);
15604 /* We are done with the current parameter list. */
15605 --parser->num_template_parameter_lists;
15607 pop_deferring_access_checks ();
15609 /* Finish up. */
15610 finish_template_decl (parameter_list);
15612 /* Register member declarations. */
15613 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15614 finish_member_declaration (decl);
15615 /* For the erroneous case of a template with C linkage, we pushed an
15616 implicit C++ linkage scope; exit that scope now. */
15617 if (need_lang_pop)
15618 pop_lang_context ();
15619 /* If DECL is a function template, we must return to parse it later.
15620 (Even though there is no definition, there might be default
15621 arguments that need handling.) */
15622 if (member_p && decl
15623 && (TREE_CODE (decl) == FUNCTION_DECL
15624 || DECL_FUNCTION_TEMPLATE_P (decl)))
15625 TREE_VALUE (parser->unparsed_functions_queues)
15626 = tree_cons (NULL_TREE, decl,
15627 TREE_VALUE (parser->unparsed_functions_queues));
15630 /* Perform the deferred access checks from a template-parameter-list.
15631 CHECKS is a TREE_LIST of access checks, as returned by
15632 get_deferred_access_checks. */
15634 static void
15635 cp_parser_perform_template_parameter_access_checks (tree checks)
15637 ++processing_template_parmlist;
15638 perform_access_checks (checks);
15639 --processing_template_parmlist;
15642 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15643 `function-definition' sequence. MEMBER_P is true, this declaration
15644 appears in a class scope.
15646 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15647 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15649 static tree
15650 cp_parser_single_declaration (cp_parser* parser,
15651 tree checks,
15652 bool member_p,
15653 bool* friend_p)
15655 int declares_class_or_enum;
15656 tree decl = NULL_TREE;
15657 cp_decl_specifier_seq decl_specifiers;
15658 bool function_definition_p = false;
15660 /* This function is only used when processing a template
15661 declaration. */
15662 gcc_assert (innermost_scope_kind () == sk_template_parms
15663 || innermost_scope_kind () == sk_template_spec);
15665 /* Defer access checks until we know what is being declared. */
15666 push_deferring_access_checks (dk_deferred);
15668 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15669 alternative. */
15670 cp_parser_decl_specifier_seq (parser,
15671 CP_PARSER_FLAGS_OPTIONAL,
15672 &decl_specifiers,
15673 &declares_class_or_enum);
15674 if (friend_p)
15675 *friend_p = cp_parser_friend_p (&decl_specifiers);
15677 /* There are no template typedefs. */
15678 if (decl_specifiers.specs[(int) ds_typedef])
15680 error ("template declaration of %qs", "typedef");
15681 decl = error_mark_node;
15684 /* Gather up the access checks that occurred the
15685 decl-specifier-seq. */
15686 stop_deferring_access_checks ();
15688 /* Check for the declaration of a template class. */
15689 if (declares_class_or_enum)
15691 if (cp_parser_declares_only_class_p (parser))
15693 decl = shadow_tag (&decl_specifiers);
15695 /* In this case:
15697 struct C {
15698 friend template <typename T> struct A<T>::B;
15701 A<T>::B will be represented by a TYPENAME_TYPE, and
15702 therefore not recognized by shadow_tag. */
15703 if (friend_p && *friend_p
15704 && !decl
15705 && decl_specifiers.type
15706 && TYPE_P (decl_specifiers.type))
15707 decl = decl_specifiers.type;
15709 if (decl && decl != error_mark_node)
15710 decl = TYPE_NAME (decl);
15711 else
15712 decl = error_mark_node;
15714 /* Perform access checks for template parameters. */
15715 cp_parser_perform_template_parameter_access_checks (checks);
15718 /* If it's not a template class, try for a template function. If
15719 the next token is a `;', then this declaration does not declare
15720 anything. But, if there were errors in the decl-specifiers, then
15721 the error might well have come from an attempted class-specifier.
15722 In that case, there's no need to warn about a missing declarator. */
15723 if (!decl
15724 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15725 || decl_specifiers.type != error_mark_node))
15726 decl = cp_parser_init_declarator (parser,
15727 &decl_specifiers,
15728 checks,
15729 /*function_definition_allowed_p=*/true,
15730 member_p,
15731 declares_class_or_enum,
15732 &function_definition_p);
15734 pop_deferring_access_checks ();
15736 /* Clear any current qualification; whatever comes next is the start
15737 of something new. */
15738 parser->scope = NULL_TREE;
15739 parser->qualifying_scope = NULL_TREE;
15740 parser->object_scope = NULL_TREE;
15741 /* Look for a trailing `;' after the declaration. */
15742 if (!function_definition_p
15743 && (decl == error_mark_node
15744 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15745 cp_parser_skip_to_end_of_block_or_statement (parser);
15747 return decl;
15750 /* Parse a cast-expression that is not the operand of a unary "&". */
15752 static tree
15753 cp_parser_simple_cast_expression (cp_parser *parser)
15755 return cp_parser_cast_expression (parser, /*address_p=*/false,
15756 /*cast_p=*/false);
15759 /* Parse a functional cast to TYPE. Returns an expression
15760 representing the cast. */
15762 static tree
15763 cp_parser_functional_cast (cp_parser* parser, tree type)
15765 tree expression_list;
15766 tree cast;
15768 expression_list
15769 = cp_parser_parenthesized_expression_list (parser, false,
15770 /*cast_p=*/true,
15771 /*non_constant_p=*/NULL);
15773 cast = build_functional_cast (type, expression_list);
15774 /* [expr.const]/1: In an integral constant expression "only type
15775 conversions to integral or enumeration type can be used". */
15776 if (TREE_CODE (type) == TYPE_DECL)
15777 type = TREE_TYPE (type);
15778 if (cast != error_mark_node && !dependent_type_p (type)
15779 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15781 if (cp_parser_non_integral_constant_expression
15782 (parser, "a call to a constructor"))
15783 return error_mark_node;
15785 return cast;
15788 /* Save the tokens that make up the body of a member function defined
15789 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15790 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15791 specifiers applied to the declaration. Returns the FUNCTION_DECL
15792 for the member function. */
15794 static tree
15795 cp_parser_save_member_function_body (cp_parser* parser,
15796 cp_decl_specifier_seq *decl_specifiers,
15797 cp_declarator *declarator,
15798 tree attributes)
15800 cp_token *first;
15801 cp_token *last;
15802 tree fn;
15804 /* Create the function-declaration. */
15805 fn = start_method (decl_specifiers, declarator, attributes);
15806 /* If something went badly wrong, bail out now. */
15807 if (fn == error_mark_node)
15809 /* If there's a function-body, skip it. */
15810 if (cp_parser_token_starts_function_definition_p
15811 (cp_lexer_peek_token (parser->lexer)))
15812 cp_parser_skip_to_end_of_block_or_statement (parser);
15813 return error_mark_node;
15816 /* Remember it, if there default args to post process. */
15817 cp_parser_save_default_args (parser, fn);
15819 /* Save away the tokens that make up the body of the
15820 function. */
15821 first = parser->lexer->next_token;
15822 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15823 /* Handle function try blocks. */
15824 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15825 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15826 last = parser->lexer->next_token;
15828 /* Save away the inline definition; we will process it when the
15829 class is complete. */
15830 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15831 DECL_PENDING_INLINE_P (fn) = 1;
15833 /* We need to know that this was defined in the class, so that
15834 friend templates are handled correctly. */
15835 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15837 /* We're done with the inline definition. */
15838 finish_method (fn);
15840 /* Add FN to the queue of functions to be parsed later. */
15841 TREE_VALUE (parser->unparsed_functions_queues)
15842 = tree_cons (NULL_TREE, fn,
15843 TREE_VALUE (parser->unparsed_functions_queues));
15845 return fn;
15848 /* Parse a template-argument-list, as well as the trailing ">" (but
15849 not the opening ">"). See cp_parser_template_argument_list for the
15850 return value. */
15852 static tree
15853 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15855 tree arguments;
15856 tree saved_scope;
15857 tree saved_qualifying_scope;
15858 tree saved_object_scope;
15859 bool saved_greater_than_is_operator_p;
15860 bool saved_skip_evaluation;
15862 /* [temp.names]
15864 When parsing a template-id, the first non-nested `>' is taken as
15865 the end of the template-argument-list rather than a greater-than
15866 operator. */
15867 saved_greater_than_is_operator_p
15868 = parser->greater_than_is_operator_p;
15869 parser->greater_than_is_operator_p = false;
15870 /* Parsing the argument list may modify SCOPE, so we save it
15871 here. */
15872 saved_scope = parser->scope;
15873 saved_qualifying_scope = parser->qualifying_scope;
15874 saved_object_scope = parser->object_scope;
15875 /* We need to evaluate the template arguments, even though this
15876 template-id may be nested within a "sizeof". */
15877 saved_skip_evaluation = skip_evaluation;
15878 skip_evaluation = false;
15879 /* Parse the template-argument-list itself. */
15880 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15881 arguments = NULL_TREE;
15882 else
15883 arguments = cp_parser_template_argument_list (parser);
15884 /* Look for the `>' that ends the template-argument-list. If we find
15885 a '>>' instead, it's probably just a typo. */
15886 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15888 if (!saved_greater_than_is_operator_p)
15890 /* If we're in a nested template argument list, the '>>' has
15891 to be a typo for '> >'. We emit the error message, but we
15892 continue parsing and we push a '>' as next token, so that
15893 the argument list will be parsed correctly. Note that the
15894 global source location is still on the token before the
15895 '>>', so we need to say explicitly where we want it. */
15896 cp_token *token = cp_lexer_peek_token (parser->lexer);
15897 error ("%H%<>>%> should be %<> >%> "
15898 "within a nested template argument list",
15899 &token->location);
15901 /* ??? Proper recovery should terminate two levels of
15902 template argument list here. */
15903 token->type = CPP_GREATER;
15905 else
15907 /* If this is not a nested template argument list, the '>>'
15908 is a typo for '>'. Emit an error message and continue.
15909 Same deal about the token location, but here we can get it
15910 right by consuming the '>>' before issuing the diagnostic. */
15911 cp_lexer_consume_token (parser->lexer);
15912 error ("spurious %<>>%>, use %<>%> to terminate "
15913 "a template argument list");
15916 else
15917 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15918 /* The `>' token might be a greater-than operator again now. */
15919 parser->greater_than_is_operator_p
15920 = saved_greater_than_is_operator_p;
15921 /* Restore the SAVED_SCOPE. */
15922 parser->scope = saved_scope;
15923 parser->qualifying_scope = saved_qualifying_scope;
15924 parser->object_scope = saved_object_scope;
15925 skip_evaluation = saved_skip_evaluation;
15927 return arguments;
15930 /* MEMBER_FUNCTION is a member function, or a friend. If default
15931 arguments, or the body of the function have not yet been parsed,
15932 parse them now. */
15934 static void
15935 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15937 /* If this member is a template, get the underlying
15938 FUNCTION_DECL. */
15939 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15940 member_function = DECL_TEMPLATE_RESULT (member_function);
15942 /* There should not be any class definitions in progress at this
15943 point; the bodies of members are only parsed outside of all class
15944 definitions. */
15945 gcc_assert (parser->num_classes_being_defined == 0);
15946 /* While we're parsing the member functions we might encounter more
15947 classes. We want to handle them right away, but we don't want
15948 them getting mixed up with functions that are currently in the
15949 queue. */
15950 parser->unparsed_functions_queues
15951 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15953 /* Make sure that any template parameters are in scope. */
15954 maybe_begin_member_template_processing (member_function);
15956 /* If the body of the function has not yet been parsed, parse it
15957 now. */
15958 if (DECL_PENDING_INLINE_P (member_function))
15960 tree function_scope;
15961 cp_token_cache *tokens;
15963 /* The function is no longer pending; we are processing it. */
15964 tokens = DECL_PENDING_INLINE_INFO (member_function);
15965 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15966 DECL_PENDING_INLINE_P (member_function) = 0;
15968 /* If this is a local class, enter the scope of the containing
15969 function. */
15970 function_scope = current_function_decl;
15971 if (function_scope)
15972 push_function_context_to (function_scope);
15975 /* Push the body of the function onto the lexer stack. */
15976 cp_parser_push_lexer_for_tokens (parser, tokens);
15978 /* Let the front end know that we going to be defining this
15979 function. */
15980 start_preparsed_function (member_function, NULL_TREE,
15981 SF_PRE_PARSED | SF_INCLASS_INLINE);
15983 /* Don't do access checking if it is a templated function. */
15984 if (processing_template_decl)
15985 push_deferring_access_checks (dk_no_check);
15987 /* Now, parse the body of the function. */
15988 cp_parser_function_definition_after_declarator (parser,
15989 /*inline_p=*/true);
15991 if (processing_template_decl)
15992 pop_deferring_access_checks ();
15994 /* Leave the scope of the containing function. */
15995 if (function_scope)
15996 pop_function_context_from (function_scope);
15997 cp_parser_pop_lexer (parser);
16000 /* Remove any template parameters from the symbol table. */
16001 maybe_end_member_template_processing ();
16003 /* Restore the queue. */
16004 parser->unparsed_functions_queues
16005 = TREE_CHAIN (parser->unparsed_functions_queues);
16008 /* If DECL contains any default args, remember it on the unparsed
16009 functions queue. */
16011 static void
16012 cp_parser_save_default_args (cp_parser* parser, tree decl)
16014 tree probe;
16016 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16017 probe;
16018 probe = TREE_CHAIN (probe))
16019 if (TREE_PURPOSE (probe))
16021 TREE_PURPOSE (parser->unparsed_functions_queues)
16022 = tree_cons (current_class_type, decl,
16023 TREE_PURPOSE (parser->unparsed_functions_queues));
16024 break;
16026 return;
16029 /* FN is a FUNCTION_DECL which may contains a parameter with an
16030 unparsed DEFAULT_ARG. Parse the default args now. This function
16031 assumes that the current scope is the scope in which the default
16032 argument should be processed. */
16034 static void
16035 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16037 bool saved_local_variables_forbidden_p;
16038 tree parm;
16040 /* While we're parsing the default args, we might (due to the
16041 statement expression extension) encounter more classes. We want
16042 to handle them right away, but we don't want them getting mixed
16043 up with default args that are currently in the queue. */
16044 parser->unparsed_functions_queues
16045 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16047 /* Local variable names (and the `this' keyword) may not appear
16048 in a default argument. */
16049 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16050 parser->local_variables_forbidden_p = true;
16052 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16053 parm;
16054 parm = TREE_CHAIN (parm))
16056 cp_token_cache *tokens;
16057 tree default_arg = TREE_PURPOSE (parm);
16058 tree parsed_arg;
16059 VEC(tree,gc) *insts;
16060 tree copy;
16061 unsigned ix;
16063 if (!default_arg)
16064 continue;
16066 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16067 /* This can happen for a friend declaration for a function
16068 already declared with default arguments. */
16069 continue;
16071 /* Push the saved tokens for the default argument onto the parser's
16072 lexer stack. */
16073 tokens = DEFARG_TOKENS (default_arg);
16074 cp_parser_push_lexer_for_tokens (parser, tokens);
16076 /* Parse the assignment-expression. */
16077 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16079 if (!processing_template_decl)
16080 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16082 TREE_PURPOSE (parm) = parsed_arg;
16084 /* Update any instantiations we've already created. */
16085 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16086 VEC_iterate (tree, insts, ix, copy); ix++)
16087 TREE_PURPOSE (copy) = parsed_arg;
16089 /* If the token stream has not been completely used up, then
16090 there was extra junk after the end of the default
16091 argument. */
16092 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16093 cp_parser_error (parser, "expected %<,%>");
16095 /* Revert to the main lexer. */
16096 cp_parser_pop_lexer (parser);
16099 /* Make sure no default arg is missing. */
16100 check_default_args (fn);
16102 /* Restore the state of local_variables_forbidden_p. */
16103 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16105 /* Restore the queue. */
16106 parser->unparsed_functions_queues
16107 = TREE_CHAIN (parser->unparsed_functions_queues);
16110 /* Parse the operand of `sizeof' (or a similar operator). Returns
16111 either a TYPE or an expression, depending on the form of the
16112 input. The KEYWORD indicates which kind of expression we have
16113 encountered. */
16115 static tree
16116 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16118 static const char *format;
16119 tree expr = NULL_TREE;
16120 const char *saved_message;
16121 bool saved_integral_constant_expression_p;
16122 bool saved_non_integral_constant_expression_p;
16124 /* Initialize FORMAT the first time we get here. */
16125 if (!format)
16126 format = "types may not be defined in '%s' expressions";
16128 /* Types cannot be defined in a `sizeof' expression. Save away the
16129 old message. */
16130 saved_message = parser->type_definition_forbidden_message;
16131 /* And create the new one. */
16132 parser->type_definition_forbidden_message
16133 = xmalloc (strlen (format)
16134 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16135 + 1 /* `\0' */);
16136 sprintf ((char *) parser->type_definition_forbidden_message,
16137 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16139 /* The restrictions on constant-expressions do not apply inside
16140 sizeof expressions. */
16141 saved_integral_constant_expression_p
16142 = parser->integral_constant_expression_p;
16143 saved_non_integral_constant_expression_p
16144 = parser->non_integral_constant_expression_p;
16145 parser->integral_constant_expression_p = false;
16147 /* Do not actually evaluate the expression. */
16148 ++skip_evaluation;
16149 /* If it's a `(', then we might be looking at the type-id
16150 construction. */
16151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16153 tree type;
16154 bool saved_in_type_id_in_expr_p;
16156 /* We can't be sure yet whether we're looking at a type-id or an
16157 expression. */
16158 cp_parser_parse_tentatively (parser);
16159 /* Consume the `('. */
16160 cp_lexer_consume_token (parser->lexer);
16161 /* Parse the type-id. */
16162 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16163 parser->in_type_id_in_expr_p = true;
16164 type = cp_parser_type_id (parser);
16165 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16166 /* Now, look for the trailing `)'. */
16167 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16168 /* If all went well, then we're done. */
16169 if (cp_parser_parse_definitely (parser))
16171 cp_decl_specifier_seq decl_specs;
16173 /* Build a trivial decl-specifier-seq. */
16174 clear_decl_specs (&decl_specs);
16175 decl_specs.type = type;
16177 /* Call grokdeclarator to figure out what type this is. */
16178 expr = grokdeclarator (NULL,
16179 &decl_specs,
16180 TYPENAME,
16181 /*initialized=*/0,
16182 /*attrlist=*/NULL);
16186 /* If the type-id production did not work out, then we must be
16187 looking at the unary-expression production. */
16188 if (!expr)
16189 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16190 /*cast_p=*/false);
16191 /* Go back to evaluating expressions. */
16192 --skip_evaluation;
16194 /* Free the message we created. */
16195 free ((char *) parser->type_definition_forbidden_message);
16196 /* And restore the old one. */
16197 parser->type_definition_forbidden_message = saved_message;
16198 parser->integral_constant_expression_p
16199 = saved_integral_constant_expression_p;
16200 parser->non_integral_constant_expression_p
16201 = saved_non_integral_constant_expression_p;
16203 return expr;
16206 /* If the current declaration has no declarator, return true. */
16208 static bool
16209 cp_parser_declares_only_class_p (cp_parser *parser)
16211 /* If the next token is a `;' or a `,' then there is no
16212 declarator. */
16213 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16214 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16217 /* Update the DECL_SPECS to reflect the storage class indicated by
16218 KEYWORD. */
16220 static void
16221 cp_parser_set_storage_class (cp_parser *parser,
16222 cp_decl_specifier_seq *decl_specs,
16223 enum rid keyword)
16225 cp_storage_class storage_class;
16227 if (parser->in_unbraced_linkage_specification_p)
16229 error ("invalid use of %qD in linkage specification",
16230 ridpointers[keyword]);
16231 return;
16233 else if (decl_specs->storage_class != sc_none)
16235 decl_specs->multiple_storage_classes_p = true;
16236 return;
16239 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16240 && decl_specs->specs[(int) ds_thread])
16242 error ("%<__thread%> before %qD", ridpointers[keyword]);
16243 decl_specs->specs[(int) ds_thread] = 0;
16246 switch (keyword)
16248 case RID_AUTO:
16249 storage_class = sc_auto;
16250 break;
16251 case RID_REGISTER:
16252 storage_class = sc_register;
16253 break;
16254 case RID_STATIC:
16255 storage_class = sc_static;
16256 break;
16257 case RID_EXTERN:
16258 storage_class = sc_extern;
16259 break;
16260 case RID_MUTABLE:
16261 storage_class = sc_mutable;
16262 break;
16263 default:
16264 gcc_unreachable ();
16266 decl_specs->storage_class = storage_class;
16269 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16270 is true, the type is a user-defined type; otherwise it is a
16271 built-in type specified by a keyword. */
16273 static void
16274 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16275 tree type_spec,
16276 bool user_defined_p)
16278 decl_specs->any_specifiers_p = true;
16280 /* If the user tries to redeclare bool or wchar_t (with, for
16281 example, in "typedef int wchar_t;") we remember that this is what
16282 happened. In system headers, we ignore these declarations so
16283 that G++ can work with system headers that are not C++-safe. */
16284 if (decl_specs->specs[(int) ds_typedef]
16285 && !user_defined_p
16286 && (type_spec == boolean_type_node
16287 || type_spec == wchar_type_node)
16288 && (decl_specs->type
16289 || decl_specs->specs[(int) ds_long]
16290 || decl_specs->specs[(int) ds_short]
16291 || decl_specs->specs[(int) ds_unsigned]
16292 || decl_specs->specs[(int) ds_signed]))
16294 decl_specs->redefined_builtin_type = type_spec;
16295 if (!decl_specs->type)
16297 decl_specs->type = type_spec;
16298 decl_specs->user_defined_type_p = false;
16301 else if (decl_specs->type)
16302 decl_specs->multiple_types_p = true;
16303 else
16305 decl_specs->type = type_spec;
16306 decl_specs->user_defined_type_p = user_defined_p;
16307 decl_specs->redefined_builtin_type = NULL_TREE;
16311 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16312 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16314 static bool
16315 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16317 return decl_specifiers->specs[(int) ds_friend] != 0;
16320 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16321 issue an error message indicating that TOKEN_DESC was expected.
16323 Returns the token consumed, if the token had the appropriate type.
16324 Otherwise, returns NULL. */
16326 static cp_token *
16327 cp_parser_require (cp_parser* parser,
16328 enum cpp_ttype type,
16329 const char* token_desc)
16331 if (cp_lexer_next_token_is (parser->lexer, type))
16332 return cp_lexer_consume_token (parser->lexer);
16333 else
16335 /* Output the MESSAGE -- unless we're parsing tentatively. */
16336 if (!cp_parser_simulate_error (parser))
16338 char *message = concat ("expected ", token_desc, NULL);
16339 cp_parser_error (parser, message);
16340 free (message);
16342 return NULL;
16346 /* Like cp_parser_require, except that tokens will be skipped until
16347 the desired token is found. An error message is still produced if
16348 the next token is not as expected. */
16350 static void
16351 cp_parser_skip_until_found (cp_parser* parser,
16352 enum cpp_ttype type,
16353 const char* token_desc)
16355 cp_token *token;
16356 unsigned nesting_depth = 0;
16358 if (cp_parser_require (parser, type, token_desc))
16359 return;
16361 /* Skip tokens until the desired token is found. */
16362 while (true)
16364 /* Peek at the next token. */
16365 token = cp_lexer_peek_token (parser->lexer);
16366 /* If we've reached the token we want, consume it and
16367 stop. */
16368 if (token->type == type && !nesting_depth)
16370 cp_lexer_consume_token (parser->lexer);
16371 return;
16373 /* If we've run out of tokens, stop. */
16374 if (token->type == CPP_EOF)
16375 return;
16376 if (token->type == CPP_OPEN_BRACE
16377 || token->type == CPP_OPEN_PAREN
16378 || token->type == CPP_OPEN_SQUARE)
16379 ++nesting_depth;
16380 else if (token->type == CPP_CLOSE_BRACE
16381 || token->type == CPP_CLOSE_PAREN
16382 || token->type == CPP_CLOSE_SQUARE)
16384 if (nesting_depth-- == 0)
16385 return;
16387 /* Consume this token. */
16388 cp_lexer_consume_token (parser->lexer);
16392 /* If the next token is the indicated keyword, consume it. Otherwise,
16393 issue an error message indicating that TOKEN_DESC was expected.
16395 Returns the token consumed, if the token had the appropriate type.
16396 Otherwise, returns NULL. */
16398 static cp_token *
16399 cp_parser_require_keyword (cp_parser* parser,
16400 enum rid keyword,
16401 const char* token_desc)
16403 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16405 if (token && token->keyword != keyword)
16407 dyn_string_t error_msg;
16409 /* Format the error message. */
16410 error_msg = dyn_string_new (0);
16411 dyn_string_append_cstr (error_msg, "expected ");
16412 dyn_string_append_cstr (error_msg, token_desc);
16413 cp_parser_error (parser, error_msg->s);
16414 dyn_string_delete (error_msg);
16415 return NULL;
16418 return token;
16421 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16422 function-definition. */
16424 static bool
16425 cp_parser_token_starts_function_definition_p (cp_token* token)
16427 return (/* An ordinary function-body begins with an `{'. */
16428 token->type == CPP_OPEN_BRACE
16429 /* A ctor-initializer begins with a `:'. */
16430 || token->type == CPP_COLON
16431 /* A function-try-block begins with `try'. */
16432 || token->keyword == RID_TRY
16433 /* The named return value extension begins with `return'. */
16434 || token->keyword == RID_RETURN);
16437 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16438 definition. */
16440 static bool
16441 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16443 cp_token *token;
16445 token = cp_lexer_peek_token (parser->lexer);
16446 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16449 /* Returns TRUE iff the next token is the "," or ">" ending a
16450 template-argument. */
16452 static bool
16453 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16455 cp_token *token;
16457 token = cp_lexer_peek_token (parser->lexer);
16458 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16461 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16462 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16464 static bool
16465 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16466 size_t n)
16468 cp_token *token;
16470 token = cp_lexer_peek_nth_token (parser->lexer, n);
16471 if (token->type == CPP_LESS)
16472 return true;
16473 /* Check for the sequence `<::' in the original code. It would be lexed as
16474 `[:', where `[' is a digraph, and there is no whitespace before
16475 `:'. */
16476 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16478 cp_token *token2;
16479 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16480 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16481 return true;
16483 return false;
16486 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16487 or none_type otherwise. */
16489 static enum tag_types
16490 cp_parser_token_is_class_key (cp_token* token)
16492 switch (token->keyword)
16494 case RID_CLASS:
16495 return class_type;
16496 case RID_STRUCT:
16497 return record_type;
16498 case RID_UNION:
16499 return union_type;
16501 default:
16502 return none_type;
16506 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16508 static void
16509 cp_parser_check_class_key (enum tag_types class_key, tree type)
16511 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16512 pedwarn ("%qs tag used in naming %q#T",
16513 class_key == union_type ? "union"
16514 : class_key == record_type ? "struct" : "class",
16515 type);
16518 /* Issue an error message if DECL is redeclared with different
16519 access than its original declaration [class.access.spec/3].
16520 This applies to nested classes and nested class templates.
16521 [class.mem/1]. */
16523 static void
16524 cp_parser_check_access_in_redeclaration (tree decl)
16526 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16527 return;
16529 if ((TREE_PRIVATE (decl)
16530 != (current_access_specifier == access_private_node))
16531 || (TREE_PROTECTED (decl)
16532 != (current_access_specifier == access_protected_node)))
16533 error ("%qD redeclared with different access", decl);
16536 /* Look for the `template' keyword, as a syntactic disambiguator.
16537 Return TRUE iff it is present, in which case it will be
16538 consumed. */
16540 static bool
16541 cp_parser_optional_template_keyword (cp_parser *parser)
16543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16545 /* The `template' keyword can only be used within templates;
16546 outside templates the parser can always figure out what is a
16547 template and what is not. */
16548 if (!processing_template_decl)
16550 error ("%<template%> (as a disambiguator) is only allowed "
16551 "within templates");
16552 /* If this part of the token stream is rescanned, the same
16553 error message would be generated. So, we purge the token
16554 from the stream. */
16555 cp_lexer_purge_token (parser->lexer);
16556 return false;
16558 else
16560 /* Consume the `template' keyword. */
16561 cp_lexer_consume_token (parser->lexer);
16562 return true;
16566 return false;
16569 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16570 set PARSER->SCOPE, and perform other related actions. */
16572 static void
16573 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16575 tree value;
16576 tree check;
16578 /* Get the stored value. */
16579 value = cp_lexer_consume_token (parser->lexer)->value;
16580 /* Perform any access checks that were deferred. */
16581 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16582 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16583 /* Set the scope from the stored value. */
16584 parser->scope = TREE_VALUE (value);
16585 parser->qualifying_scope = TREE_TYPE (value);
16586 parser->object_scope = NULL_TREE;
16589 /* Consume tokens up through a non-nested END token. */
16591 static void
16592 cp_parser_cache_group (cp_parser *parser,
16593 enum cpp_ttype end,
16594 unsigned depth)
16596 while (true)
16598 cp_token *token;
16600 /* Abort a parenthesized expression if we encounter a brace. */
16601 if ((end == CPP_CLOSE_PAREN || depth == 0)
16602 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16603 return;
16604 /* If we've reached the end of the file, stop. */
16605 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16606 return;
16607 /* Consume the next token. */
16608 token = cp_lexer_consume_token (parser->lexer);
16609 /* See if it starts a new group. */
16610 if (token->type == CPP_OPEN_BRACE)
16612 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16613 if (depth == 0)
16614 return;
16616 else if (token->type == CPP_OPEN_PAREN)
16617 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16618 else if (token->type == end)
16619 return;
16623 /* Begin parsing tentatively. We always save tokens while parsing
16624 tentatively so that if the tentative parsing fails we can restore the
16625 tokens. */
16627 static void
16628 cp_parser_parse_tentatively (cp_parser* parser)
16630 /* Enter a new parsing context. */
16631 parser->context = cp_parser_context_new (parser->context);
16632 /* Begin saving tokens. */
16633 cp_lexer_save_tokens (parser->lexer);
16634 /* In order to avoid repetitive access control error messages,
16635 access checks are queued up until we are no longer parsing
16636 tentatively. */
16637 push_deferring_access_checks (dk_deferred);
16640 /* Commit to the currently active tentative parse. */
16642 static void
16643 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16645 cp_parser_context *context;
16646 cp_lexer *lexer;
16648 /* Mark all of the levels as committed. */
16649 lexer = parser->lexer;
16650 for (context = parser->context; context->next; context = context->next)
16652 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16653 break;
16654 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16655 while (!cp_lexer_saving_tokens (lexer))
16656 lexer = lexer->next;
16657 cp_lexer_commit_tokens (lexer);
16661 /* Abort the currently active tentative parse. All consumed tokens
16662 will be rolled back, and no diagnostics will be issued. */
16664 static void
16665 cp_parser_abort_tentative_parse (cp_parser* parser)
16667 cp_parser_simulate_error (parser);
16668 /* Now, pretend that we want to see if the construct was
16669 successfully parsed. */
16670 cp_parser_parse_definitely (parser);
16673 /* Stop parsing tentatively. If a parse error has occurred, restore the
16674 token stream. Otherwise, commit to the tokens we have consumed.
16675 Returns true if no error occurred; false otherwise. */
16677 static bool
16678 cp_parser_parse_definitely (cp_parser* parser)
16680 bool error_occurred;
16681 cp_parser_context *context;
16683 /* Remember whether or not an error occurred, since we are about to
16684 destroy that information. */
16685 error_occurred = cp_parser_error_occurred (parser);
16686 /* Remove the topmost context from the stack. */
16687 context = parser->context;
16688 parser->context = context->next;
16689 /* If no parse errors occurred, commit to the tentative parse. */
16690 if (!error_occurred)
16692 /* Commit to the tokens read tentatively, unless that was
16693 already done. */
16694 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16695 cp_lexer_commit_tokens (parser->lexer);
16697 pop_to_parent_deferring_access_checks ();
16699 /* Otherwise, if errors occurred, roll back our state so that things
16700 are just as they were before we began the tentative parse. */
16701 else
16703 cp_lexer_rollback_tokens (parser->lexer);
16704 pop_deferring_access_checks ();
16706 /* Add the context to the front of the free list. */
16707 context->next = cp_parser_context_free_list;
16708 cp_parser_context_free_list = context;
16710 return !error_occurred;
16713 /* Returns true if we are parsing tentatively and are not committed to
16714 this tentative parse. */
16716 static bool
16717 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16719 return (cp_parser_parsing_tentatively (parser)
16720 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16723 /* Returns nonzero iff an error has occurred during the most recent
16724 tentative parse. */
16726 static bool
16727 cp_parser_error_occurred (cp_parser* parser)
16729 return (cp_parser_parsing_tentatively (parser)
16730 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16733 /* Returns nonzero if GNU extensions are allowed. */
16735 static bool
16736 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16738 return parser->allow_gnu_extensions_p;
16741 /* Objective-C++ Productions */
16744 /* Parse an Objective-C expression, which feeds into a primary-expression
16745 above.
16747 objc-expression:
16748 objc-message-expression
16749 objc-string-literal
16750 objc-encode-expression
16751 objc-protocol-expression
16752 objc-selector-expression
16754 Returns a tree representation of the expression. */
16756 static tree
16757 cp_parser_objc_expression (cp_parser* parser)
16759 /* Try to figure out what kind of declaration is present. */
16760 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16762 switch (kwd->type)
16764 case CPP_OPEN_SQUARE:
16765 return cp_parser_objc_message_expression (parser);
16767 case CPP_OBJC_STRING:
16768 kwd = cp_lexer_consume_token (parser->lexer);
16769 return objc_build_string_object (kwd->value);
16771 case CPP_KEYWORD:
16772 switch (kwd->keyword)
16774 case RID_AT_ENCODE:
16775 return cp_parser_objc_encode_expression (parser);
16777 case RID_AT_PROTOCOL:
16778 return cp_parser_objc_protocol_expression (parser);
16780 case RID_AT_SELECTOR:
16781 return cp_parser_objc_selector_expression (parser);
16783 default:
16784 break;
16786 default:
16787 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16788 cp_parser_skip_to_end_of_block_or_statement (parser);
16791 return error_mark_node;
16794 /* Parse an Objective-C message expression.
16796 objc-message-expression:
16797 [ objc-message-receiver objc-message-args ]
16799 Returns a representation of an Objective-C message. */
16801 static tree
16802 cp_parser_objc_message_expression (cp_parser* parser)
16804 tree receiver, messageargs;
16806 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16807 receiver = cp_parser_objc_message_receiver (parser);
16808 messageargs = cp_parser_objc_message_args (parser);
16809 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16811 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16814 /* Parse an objc-message-receiver.
16816 objc-message-receiver:
16817 expression
16818 simple-type-specifier
16820 Returns a representation of the type or expression. */
16822 static tree
16823 cp_parser_objc_message_receiver (cp_parser* parser)
16825 tree rcv;
16827 /* An Objective-C message receiver may be either (1) a type
16828 or (2) an expression. */
16829 cp_parser_parse_tentatively (parser);
16830 rcv = cp_parser_expression (parser, false);
16832 if (cp_parser_parse_definitely (parser))
16833 return rcv;
16835 rcv = cp_parser_simple_type_specifier (parser,
16836 /*decl_specs=*/NULL,
16837 CP_PARSER_FLAGS_NONE);
16839 return objc_get_class_reference (rcv);
16842 /* Parse the arguments and selectors comprising an Objective-C message.
16844 objc-message-args:
16845 objc-selector
16846 objc-selector-args
16847 objc-selector-args , objc-comma-args
16849 objc-selector-args:
16850 objc-selector [opt] : assignment-expression
16851 objc-selector-args objc-selector [opt] : assignment-expression
16853 objc-comma-args:
16854 assignment-expression
16855 objc-comma-args , assignment-expression
16857 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16858 selector arguments and TREE_VALUE containing a list of comma
16859 arguments. */
16861 static tree
16862 cp_parser_objc_message_args (cp_parser* parser)
16864 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16865 bool maybe_unary_selector_p = true;
16866 cp_token *token = cp_lexer_peek_token (parser->lexer);
16868 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16870 tree selector = NULL_TREE, arg;
16872 if (token->type != CPP_COLON)
16873 selector = cp_parser_objc_selector (parser);
16875 /* Detect if we have a unary selector. */
16876 if (maybe_unary_selector_p
16877 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16878 return build_tree_list (selector, NULL_TREE);
16880 maybe_unary_selector_p = false;
16881 cp_parser_require (parser, CPP_COLON, "`:'");
16882 arg = cp_parser_assignment_expression (parser, false);
16884 sel_args
16885 = chainon (sel_args,
16886 build_tree_list (selector, arg));
16888 token = cp_lexer_peek_token (parser->lexer);
16891 /* Handle non-selector arguments, if any. */
16892 while (token->type == CPP_COMMA)
16894 tree arg;
16896 cp_lexer_consume_token (parser->lexer);
16897 arg = cp_parser_assignment_expression (parser, false);
16899 addl_args
16900 = chainon (addl_args,
16901 build_tree_list (NULL_TREE, arg));
16903 token = cp_lexer_peek_token (parser->lexer);
16906 return build_tree_list (sel_args, addl_args);
16909 /* Parse an Objective-C encode expression.
16911 objc-encode-expression:
16912 @encode objc-typename
16914 Returns an encoded representation of the type argument. */
16916 static tree
16917 cp_parser_objc_encode_expression (cp_parser* parser)
16919 tree type;
16921 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16923 type = complete_type (cp_parser_type_id (parser));
16924 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16926 if (!type)
16928 error ("%<@encode%> must specify a type as an argument");
16929 return error_mark_node;
16932 return objc_build_encode_expr (type);
16935 /* Parse an Objective-C @defs expression. */
16937 static tree
16938 cp_parser_objc_defs_expression (cp_parser *parser)
16940 tree name;
16942 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16943 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16944 name = cp_parser_identifier (parser);
16945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16947 return objc_get_class_ivars (name);
16950 /* Parse an Objective-C protocol expression.
16952 objc-protocol-expression:
16953 @protocol ( identifier )
16955 Returns a representation of the protocol expression. */
16957 static tree
16958 cp_parser_objc_protocol_expression (cp_parser* parser)
16960 tree proto;
16962 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16963 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16964 proto = cp_parser_identifier (parser);
16965 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16967 return objc_build_protocol_expr (proto);
16970 /* Parse an Objective-C selector expression.
16972 objc-selector-expression:
16973 @selector ( objc-method-signature )
16975 objc-method-signature:
16976 objc-selector
16977 objc-selector-seq
16979 objc-selector-seq:
16980 objc-selector :
16981 objc-selector-seq objc-selector :
16983 Returns a representation of the method selector. */
16985 static tree
16986 cp_parser_objc_selector_expression (cp_parser* parser)
16988 tree sel_seq = NULL_TREE;
16989 bool maybe_unary_selector_p = true;
16990 cp_token *token;
16992 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16993 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16994 token = cp_lexer_peek_token (parser->lexer);
16996 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16997 || token->type == CPP_SCOPE)
16999 tree selector = NULL_TREE;
17001 if (token->type != CPP_COLON
17002 || token->type == CPP_SCOPE)
17003 selector = cp_parser_objc_selector (parser);
17005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17006 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17008 /* Detect if we have a unary selector. */
17009 if (maybe_unary_selector_p)
17011 sel_seq = selector;
17012 goto finish_selector;
17014 else
17016 cp_parser_error (parser, "expected %<:%>");
17019 maybe_unary_selector_p = false;
17020 token = cp_lexer_consume_token (parser->lexer);
17022 if (token->type == CPP_SCOPE)
17024 sel_seq
17025 = chainon (sel_seq,
17026 build_tree_list (selector, NULL_TREE));
17027 sel_seq
17028 = chainon (sel_seq,
17029 build_tree_list (NULL_TREE, NULL_TREE));
17031 else
17032 sel_seq
17033 = chainon (sel_seq,
17034 build_tree_list (selector, NULL_TREE));
17036 token = cp_lexer_peek_token (parser->lexer);
17039 finish_selector:
17040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17042 return objc_build_selector_expr (sel_seq);
17045 /* Parse a list of identifiers.
17047 objc-identifier-list:
17048 identifier
17049 objc-identifier-list , identifier
17051 Returns a TREE_LIST of identifier nodes. */
17053 static tree
17054 cp_parser_objc_identifier_list (cp_parser* parser)
17056 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17057 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17059 while (sep->type == CPP_COMMA)
17061 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17062 list = chainon (list,
17063 build_tree_list (NULL_TREE,
17064 cp_parser_identifier (parser)));
17065 sep = cp_lexer_peek_token (parser->lexer);
17068 return list;
17071 /* Parse an Objective-C alias declaration.
17073 objc-alias-declaration:
17074 @compatibility_alias identifier identifier ;
17076 This function registers the alias mapping with the Objective-C front-end.
17077 It returns nothing. */
17079 static void
17080 cp_parser_objc_alias_declaration (cp_parser* parser)
17082 tree alias, orig;
17084 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17085 alias = cp_parser_identifier (parser);
17086 orig = cp_parser_identifier (parser);
17087 objc_declare_alias (alias, orig);
17088 cp_parser_consume_semicolon_at_end_of_statement (parser);
17091 /* Parse an Objective-C class forward-declaration.
17093 objc-class-declaration:
17094 @class objc-identifier-list ;
17096 The function registers the forward declarations with the Objective-C
17097 front-end. It returns nothing. */
17099 static void
17100 cp_parser_objc_class_declaration (cp_parser* parser)
17102 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17103 objc_declare_class (cp_parser_objc_identifier_list (parser));
17104 cp_parser_consume_semicolon_at_end_of_statement (parser);
17107 /* Parse a list of Objective-C protocol references.
17109 objc-protocol-refs-opt:
17110 objc-protocol-refs [opt]
17112 objc-protocol-refs:
17113 < objc-identifier-list >
17115 Returns a TREE_LIST of identifiers, if any. */
17117 static tree
17118 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17120 tree protorefs = NULL_TREE;
17122 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17124 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17125 protorefs = cp_parser_objc_identifier_list (parser);
17126 cp_parser_require (parser, CPP_GREATER, "`>'");
17129 return protorefs;
17132 /* Parse a Objective-C visibility specification. */
17134 static void
17135 cp_parser_objc_visibility_spec (cp_parser* parser)
17137 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17139 switch (vis->keyword)
17141 case RID_AT_PRIVATE:
17142 objc_set_visibility (2);
17143 break;
17144 case RID_AT_PROTECTED:
17145 objc_set_visibility (0);
17146 break;
17147 case RID_AT_PUBLIC:
17148 objc_set_visibility (1);
17149 break;
17150 default:
17151 return;
17154 /* Eat '@private'/'@protected'/'@public'. */
17155 cp_lexer_consume_token (parser->lexer);
17158 /* Parse an Objective-C method type. */
17160 static void
17161 cp_parser_objc_method_type (cp_parser* parser)
17163 objc_set_method_type
17164 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17165 ? PLUS_EXPR
17166 : MINUS_EXPR);
17169 /* Parse an Objective-C protocol qualifier. */
17171 static tree
17172 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17174 tree quals = NULL_TREE, node;
17175 cp_token *token = cp_lexer_peek_token (parser->lexer);
17177 node = token->value;
17179 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17180 && (node == ridpointers [(int) RID_IN]
17181 || node == ridpointers [(int) RID_OUT]
17182 || node == ridpointers [(int) RID_INOUT]
17183 || node == ridpointers [(int) RID_BYCOPY]
17184 || node == ridpointers [(int) RID_BYREF]
17185 || node == ridpointers [(int) RID_ONEWAY]))
17187 quals = tree_cons (NULL_TREE, node, quals);
17188 cp_lexer_consume_token (parser->lexer);
17189 token = cp_lexer_peek_token (parser->lexer);
17190 node = token->value;
17193 return quals;
17196 /* Parse an Objective-C typename. */
17198 static tree
17199 cp_parser_objc_typename (cp_parser* parser)
17201 tree typename = NULL_TREE;
17203 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17205 tree proto_quals, cp_type = NULL_TREE;
17207 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17208 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17210 /* An ObjC type name may consist of just protocol qualifiers, in which
17211 case the type shall default to 'id'. */
17212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17213 cp_type = cp_parser_type_id (parser);
17215 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17216 typename = build_tree_list (proto_quals, cp_type);
17219 return typename;
17222 /* Check to see if TYPE refers to an Objective-C selector name. */
17224 static bool
17225 cp_parser_objc_selector_p (enum cpp_ttype type)
17227 return (type == CPP_NAME || type == CPP_KEYWORD
17228 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17229 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17230 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17231 || type == CPP_XOR || type == CPP_XOR_EQ);
17234 /* Parse an Objective-C selector. */
17236 static tree
17237 cp_parser_objc_selector (cp_parser* parser)
17239 cp_token *token = cp_lexer_consume_token (parser->lexer);
17241 if (!cp_parser_objc_selector_p (token->type))
17243 error ("invalid Objective-C++ selector name");
17244 return error_mark_node;
17247 /* C++ operator names are allowed to appear in ObjC selectors. */
17248 switch (token->type)
17250 case CPP_AND_AND: return get_identifier ("and");
17251 case CPP_AND_EQ: return get_identifier ("and_eq");
17252 case CPP_AND: return get_identifier ("bitand");
17253 case CPP_OR: return get_identifier ("bitor");
17254 case CPP_COMPL: return get_identifier ("compl");
17255 case CPP_NOT: return get_identifier ("not");
17256 case CPP_NOT_EQ: return get_identifier ("not_eq");
17257 case CPP_OR_OR: return get_identifier ("or");
17258 case CPP_OR_EQ: return get_identifier ("or_eq");
17259 case CPP_XOR: return get_identifier ("xor");
17260 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17261 default: return token->value;
17265 /* Parse an Objective-C params list. */
17267 static tree
17268 cp_parser_objc_method_keyword_params (cp_parser* parser)
17270 tree params = NULL_TREE;
17271 bool maybe_unary_selector_p = true;
17272 cp_token *token = cp_lexer_peek_token (parser->lexer);
17274 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17276 tree selector = NULL_TREE, typename, identifier;
17278 if (token->type != CPP_COLON)
17279 selector = cp_parser_objc_selector (parser);
17281 /* Detect if we have a unary selector. */
17282 if (maybe_unary_selector_p
17283 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17284 return selector;
17286 maybe_unary_selector_p = false;
17287 cp_parser_require (parser, CPP_COLON, "`:'");
17288 typename = cp_parser_objc_typename (parser);
17289 identifier = cp_parser_identifier (parser);
17291 params
17292 = chainon (params,
17293 objc_build_keyword_decl (selector,
17294 typename,
17295 identifier));
17297 token = cp_lexer_peek_token (parser->lexer);
17300 return params;
17303 /* Parse the non-keyword Objective-C params. */
17305 static tree
17306 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17308 tree params = make_node (TREE_LIST);
17309 cp_token *token = cp_lexer_peek_token (parser->lexer);
17310 *ellipsisp = false; /* Initially, assume no ellipsis. */
17312 while (token->type == CPP_COMMA)
17314 cp_parameter_declarator *parmdecl;
17315 tree parm;
17317 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17318 token = cp_lexer_peek_token (parser->lexer);
17320 if (token->type == CPP_ELLIPSIS)
17322 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17323 *ellipsisp = true;
17324 break;
17327 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17328 parm = grokdeclarator (parmdecl->declarator,
17329 &parmdecl->decl_specifiers,
17330 PARM, /*initialized=*/0,
17331 /*attrlist=*/NULL);
17333 chainon (params, build_tree_list (NULL_TREE, parm));
17334 token = cp_lexer_peek_token (parser->lexer);
17337 return params;
17340 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17342 static void
17343 cp_parser_objc_interstitial_code (cp_parser* parser)
17345 cp_token *token = cp_lexer_peek_token (parser->lexer);
17347 /* If the next token is `extern' and the following token is a string
17348 literal, then we have a linkage specification. */
17349 if (token->keyword == RID_EXTERN
17350 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17351 cp_parser_linkage_specification (parser);
17352 /* Handle #pragma, if any. */
17353 else if (token->type == CPP_PRAGMA)
17354 cp_lexer_handle_pragma (parser->lexer);
17355 /* Allow stray semicolons. */
17356 else if (token->type == CPP_SEMICOLON)
17357 cp_lexer_consume_token (parser->lexer);
17358 /* Finally, try to parse a block-declaration, or a function-definition. */
17359 else
17360 cp_parser_block_declaration (parser, /*statement_p=*/false);
17363 /* Parse a method signature. */
17365 static tree
17366 cp_parser_objc_method_signature (cp_parser* parser)
17368 tree rettype, kwdparms, optparms;
17369 bool ellipsis = false;
17371 cp_parser_objc_method_type (parser);
17372 rettype = cp_parser_objc_typename (parser);
17373 kwdparms = cp_parser_objc_method_keyword_params (parser);
17374 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17376 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17379 /* Pars an Objective-C method prototype list. */
17381 static void
17382 cp_parser_objc_method_prototype_list (cp_parser* parser)
17384 cp_token *token = cp_lexer_peek_token (parser->lexer);
17386 while (token->keyword != RID_AT_END)
17388 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17390 objc_add_method_declaration
17391 (cp_parser_objc_method_signature (parser));
17392 cp_parser_consume_semicolon_at_end_of_statement (parser);
17394 else
17395 /* Allow for interspersed non-ObjC++ code. */
17396 cp_parser_objc_interstitial_code (parser);
17398 token = cp_lexer_peek_token (parser->lexer);
17401 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17402 objc_finish_interface ();
17405 /* Parse an Objective-C method definition list. */
17407 static void
17408 cp_parser_objc_method_definition_list (cp_parser* parser)
17410 cp_token *token = cp_lexer_peek_token (parser->lexer);
17412 while (token->keyword != RID_AT_END)
17414 tree meth;
17416 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17418 push_deferring_access_checks (dk_deferred);
17419 objc_start_method_definition
17420 (cp_parser_objc_method_signature (parser));
17422 /* For historical reasons, we accept an optional semicolon. */
17423 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17424 cp_lexer_consume_token (parser->lexer);
17426 perform_deferred_access_checks ();
17427 stop_deferring_access_checks ();
17428 meth = cp_parser_function_definition_after_declarator (parser,
17429 false);
17430 pop_deferring_access_checks ();
17431 objc_finish_method_definition (meth);
17433 else
17434 /* Allow for interspersed non-ObjC++ code. */
17435 cp_parser_objc_interstitial_code (parser);
17437 token = cp_lexer_peek_token (parser->lexer);
17440 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17441 objc_finish_implementation ();
17444 /* Parse Objective-C ivars. */
17446 static void
17447 cp_parser_objc_class_ivars (cp_parser* parser)
17449 cp_token *token = cp_lexer_peek_token (parser->lexer);
17451 if (token->type != CPP_OPEN_BRACE)
17452 return; /* No ivars specified. */
17454 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17455 token = cp_lexer_peek_token (parser->lexer);
17457 while (token->type != CPP_CLOSE_BRACE)
17459 cp_decl_specifier_seq declspecs;
17460 int decl_class_or_enum_p;
17461 tree prefix_attributes;
17463 cp_parser_objc_visibility_spec (parser);
17465 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17466 break;
17468 cp_parser_decl_specifier_seq (parser,
17469 CP_PARSER_FLAGS_OPTIONAL,
17470 &declspecs,
17471 &decl_class_or_enum_p);
17472 prefix_attributes = declspecs.attributes;
17473 declspecs.attributes = NULL_TREE;
17475 /* Keep going until we hit the `;' at the end of the
17476 declaration. */
17477 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17479 tree width = NULL_TREE, attributes, first_attribute, decl;
17480 cp_declarator *declarator = NULL;
17481 int ctor_dtor_or_conv_p;
17483 /* Check for a (possibly unnamed) bitfield declaration. */
17484 token = cp_lexer_peek_token (parser->lexer);
17485 if (token->type == CPP_COLON)
17486 goto eat_colon;
17488 if (token->type == CPP_NAME
17489 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17490 == CPP_COLON))
17492 /* Get the name of the bitfield. */
17493 declarator = make_id_declarator (NULL_TREE,
17494 cp_parser_identifier (parser),
17495 sfk_none);
17497 eat_colon:
17498 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17499 /* Get the width of the bitfield. */
17500 width
17501 = cp_parser_constant_expression (parser,
17502 /*allow_non_constant=*/false,
17503 NULL);
17505 else
17507 /* Parse the declarator. */
17508 declarator
17509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17510 &ctor_dtor_or_conv_p,
17511 /*parenthesized_p=*/NULL,
17512 /*member_p=*/false);
17515 /* Look for attributes that apply to the ivar. */
17516 attributes = cp_parser_attributes_opt (parser);
17517 /* Remember which attributes are prefix attributes and
17518 which are not. */
17519 first_attribute = attributes;
17520 /* Combine the attributes. */
17521 attributes = chainon (prefix_attributes, attributes);
17523 if (width)
17525 /* Create the bitfield declaration. */
17526 decl = grokbitfield (declarator, &declspecs, width);
17527 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17529 else
17530 decl = grokfield (declarator, &declspecs,
17531 NULL_TREE, /*init_const_expr_p=*/false,
17532 NULL_TREE, attributes);
17534 /* Add the instance variable. */
17535 objc_add_instance_variable (decl);
17537 /* Reset PREFIX_ATTRIBUTES. */
17538 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17539 attributes = TREE_CHAIN (attributes);
17540 if (attributes)
17541 TREE_CHAIN (attributes) = NULL_TREE;
17543 token = cp_lexer_peek_token (parser->lexer);
17545 if (token->type == CPP_COMMA)
17547 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17548 continue;
17550 break;
17553 cp_parser_consume_semicolon_at_end_of_statement (parser);
17554 token = cp_lexer_peek_token (parser->lexer);
17557 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17558 /* For historical reasons, we accept an optional semicolon. */
17559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17560 cp_lexer_consume_token (parser->lexer);
17563 /* Parse an Objective-C protocol declaration. */
17565 static void
17566 cp_parser_objc_protocol_declaration (cp_parser* parser)
17568 tree proto, protorefs;
17569 cp_token *tok;
17571 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17574 error ("identifier expected after %<@protocol%>");
17575 goto finish;
17578 /* See if we have a forward declaration or a definition. */
17579 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17581 /* Try a forward declaration first. */
17582 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17584 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17585 finish:
17586 cp_parser_consume_semicolon_at_end_of_statement (parser);
17589 /* Ok, we got a full-fledged definition (or at least should). */
17590 else
17592 proto = cp_parser_identifier (parser);
17593 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17594 objc_start_protocol (proto, protorefs);
17595 cp_parser_objc_method_prototype_list (parser);
17599 /* Parse an Objective-C superclass or category. */
17601 static void
17602 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17603 tree *categ)
17605 cp_token *next = cp_lexer_peek_token (parser->lexer);
17607 *super = *categ = NULL_TREE;
17608 if (next->type == CPP_COLON)
17610 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17611 *super = cp_parser_identifier (parser);
17613 else if (next->type == CPP_OPEN_PAREN)
17615 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17616 *categ = cp_parser_identifier (parser);
17617 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17621 /* Parse an Objective-C class interface. */
17623 static void
17624 cp_parser_objc_class_interface (cp_parser* parser)
17626 tree name, super, categ, protos;
17628 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17629 name = cp_parser_identifier (parser);
17630 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17631 protos = cp_parser_objc_protocol_refs_opt (parser);
17633 /* We have either a class or a category on our hands. */
17634 if (categ)
17635 objc_start_category_interface (name, categ, protos);
17636 else
17638 objc_start_class_interface (name, super, protos);
17639 /* Handle instance variable declarations, if any. */
17640 cp_parser_objc_class_ivars (parser);
17641 objc_continue_interface ();
17644 cp_parser_objc_method_prototype_list (parser);
17647 /* Parse an Objective-C class implementation. */
17649 static void
17650 cp_parser_objc_class_implementation (cp_parser* parser)
17652 tree name, super, categ;
17654 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17655 name = cp_parser_identifier (parser);
17656 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17658 /* We have either a class or a category on our hands. */
17659 if (categ)
17660 objc_start_category_implementation (name, categ);
17661 else
17663 objc_start_class_implementation (name, super);
17664 /* Handle instance variable declarations, if any. */
17665 cp_parser_objc_class_ivars (parser);
17666 objc_continue_implementation ();
17669 cp_parser_objc_method_definition_list (parser);
17672 /* Consume the @end token and finish off the implementation. */
17674 static void
17675 cp_parser_objc_end_implementation (cp_parser* parser)
17677 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17678 objc_finish_implementation ();
17681 /* Parse an Objective-C declaration. */
17683 static void
17684 cp_parser_objc_declaration (cp_parser* parser)
17686 /* Try to figure out what kind of declaration is present. */
17687 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17689 switch (kwd->keyword)
17691 case RID_AT_ALIAS:
17692 cp_parser_objc_alias_declaration (parser);
17693 break;
17694 case RID_AT_CLASS:
17695 cp_parser_objc_class_declaration (parser);
17696 break;
17697 case RID_AT_PROTOCOL:
17698 cp_parser_objc_protocol_declaration (parser);
17699 break;
17700 case RID_AT_INTERFACE:
17701 cp_parser_objc_class_interface (parser);
17702 break;
17703 case RID_AT_IMPLEMENTATION:
17704 cp_parser_objc_class_implementation (parser);
17705 break;
17706 case RID_AT_END:
17707 cp_parser_objc_end_implementation (parser);
17708 break;
17709 default:
17710 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17711 cp_parser_skip_to_end_of_block_or_statement (parser);
17715 /* Parse an Objective-C try-catch-finally statement.
17717 objc-try-catch-finally-stmt:
17718 @try compound-statement objc-catch-clause-seq [opt]
17719 objc-finally-clause [opt]
17721 objc-catch-clause-seq:
17722 objc-catch-clause objc-catch-clause-seq [opt]
17724 objc-catch-clause:
17725 @catch ( exception-declaration ) compound-statement
17727 objc-finally-clause
17728 @finally compound-statement
17730 Returns NULL_TREE. */
17732 static tree
17733 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17734 location_t location;
17735 tree stmt;
17737 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17738 location = cp_lexer_peek_token (parser->lexer)->location;
17739 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17740 node, lest it get absorbed into the surrounding block. */
17741 stmt = push_stmt_list ();
17742 cp_parser_compound_statement (parser, NULL, false);
17743 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17745 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17747 cp_parameter_declarator *parmdecl;
17748 tree parm;
17750 cp_lexer_consume_token (parser->lexer);
17751 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17752 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17753 parm = grokdeclarator (parmdecl->declarator,
17754 &parmdecl->decl_specifiers,
17755 PARM, /*initialized=*/0,
17756 /*attrlist=*/NULL);
17757 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17758 objc_begin_catch_clause (parm);
17759 cp_parser_compound_statement (parser, NULL, false);
17760 objc_finish_catch_clause ();
17763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17765 cp_lexer_consume_token (parser->lexer);
17766 location = cp_lexer_peek_token (parser->lexer)->location;
17767 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17768 node, lest it get absorbed into the surrounding block. */
17769 stmt = push_stmt_list ();
17770 cp_parser_compound_statement (parser, NULL, false);
17771 objc_build_finally_clause (location, pop_stmt_list (stmt));
17774 return objc_finish_try_stmt ();
17777 /* Parse an Objective-C synchronized statement.
17779 objc-synchronized-stmt:
17780 @synchronized ( expression ) compound-statement
17782 Returns NULL_TREE. */
17784 static tree
17785 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17786 location_t location;
17787 tree lock, stmt;
17789 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17791 location = cp_lexer_peek_token (parser->lexer)->location;
17792 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17793 lock = cp_parser_expression (parser, false);
17794 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17796 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17797 node, lest it get absorbed into the surrounding block. */
17798 stmt = push_stmt_list ();
17799 cp_parser_compound_statement (parser, NULL, false);
17801 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17804 /* Parse an Objective-C throw statement.
17806 objc-throw-stmt:
17807 @throw assignment-expression [opt] ;
17809 Returns a constructed '@throw' statement. */
17811 static tree
17812 cp_parser_objc_throw_statement (cp_parser *parser) {
17813 tree expr = NULL_TREE;
17815 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17818 expr = cp_parser_assignment_expression (parser, false);
17820 cp_parser_consume_semicolon_at_end_of_statement (parser);
17822 return objc_build_throw_stmt (expr);
17825 /* Parse an Objective-C statement. */
17827 static tree
17828 cp_parser_objc_statement (cp_parser * parser) {
17829 /* Try to figure out what kind of declaration is present. */
17830 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17832 switch (kwd->keyword)
17834 case RID_AT_TRY:
17835 return cp_parser_objc_try_catch_finally_statement (parser);
17836 case RID_AT_SYNCHRONIZED:
17837 return cp_parser_objc_synchronized_statement (parser);
17838 case RID_AT_THROW:
17839 return cp_parser_objc_throw_statement (parser);
17840 default:
17841 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17842 cp_parser_skip_to_end_of_block_or_statement (parser);
17845 return error_mark_node;
17848 /* The parser. */
17850 static GTY (()) cp_parser *the_parser;
17852 /* External interface. */
17854 /* Parse one entire translation unit. */
17856 void
17857 c_parse_file (void)
17859 bool error_occurred;
17860 static bool already_called = false;
17862 if (already_called)
17864 sorry ("inter-module optimizations not implemented for C++");
17865 return;
17867 already_called = true;
17869 the_parser = cp_parser_new ();
17870 push_deferring_access_checks (flag_access_control
17871 ? dk_no_deferred : dk_no_check);
17872 error_occurred = cp_parser_translation_unit (the_parser);
17873 the_parser = NULL;
17876 /* This variable must be provided by every front end. */
17878 int yydebug;
17880 #include "gt-cp-parser.h"