* name-lookup.c (set_decl_namespace): Use CP_DECL_CONTEXT.
[official-gcc.git] / gcc / cp / parser.c
blob637e183946a5af2a2bfa1e1cf62998628656043c
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 /* Identifier for the pragma. */
59 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
60 /* True if this token is from a system header. */
61 BOOL_BITFIELD in_system_header : 1;
62 /* True if this token is from a context where it is implicitly extern "C" */
63 BOOL_BITFIELD implicit_extern_c : 1;
64 /* True for a CPP_NAME token that is not a keyword (i.e., for which
65 KEYWORD is RID_MAX) iff this name was looked up and found to be
66 ambiguous. An error has already been reported. */
67 BOOL_BITFIELD ambiguous_p : 1;
68 /* The value associated with this token, if any. */
69 tree value;
70 /* The location at which this token was found. */
71 location_t location;
72 } cp_token;
74 /* We use a stack of token pointer for saving token sets. */
75 typedef struct cp_token *cp_token_position;
76 DEF_VEC_P (cp_token_position);
77 DEF_VEC_ALLOC_P (cp_token_position,heap);
79 static const cp_token eof_token =
81 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
82 #if USE_MAPPED_LOCATION
84 #else
85 {0, 0}
86 #endif
89 /* The cp_lexer structure represents the C++ lexer. It is responsible
90 for managing the token stream from the preprocessor and supplying
91 it to the parser. Tokens are never added to the cp_lexer after
92 it is created. */
94 typedef struct cp_lexer GTY (())
96 /* The memory allocated for the buffer. NULL if this lexer does not
97 own the token buffer. */
98 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
99 /* If the lexer owns the buffer, this is the number of tokens in the
100 buffer. */
101 size_t buffer_length;
103 /* A pointer just past the last available token. The tokens
104 in this lexer are [buffer, last_token). */
105 cp_token_position GTY ((skip)) last_token;
107 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
108 no more available tokens. */
109 cp_token_position GTY ((skip)) next_token;
111 /* A stack indicating positions at which cp_lexer_save_tokens was
112 called. The top entry is the most recent position at which we
113 began saving tokens. If the stack is non-empty, we are saving
114 tokens. */
115 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117 /* The next lexer in a linked list of lexers. */
118 struct cp_lexer *next;
120 /* True if we should output debugging information. */
121 bool debugging_p;
123 /* True if we're in the context of parsing a pragma, and should not
124 increment past the end-of-line marker. */
125 bool in_pragma;
126 } cp_lexer;
128 /* cp_token_cache is a range of tokens. There is no need to represent
129 allocate heap memory for it, since tokens are never removed from the
130 lexer's array. There is also no need for the GC to walk through
131 a cp_token_cache, since everything in here is referenced through
132 a lexer. */
134 typedef struct cp_token_cache GTY(())
136 /* The beginning of the token range. */
137 cp_token * GTY((skip)) first;
139 /* Points immediately after the last token in the range. */
140 cp_token * GTY ((skip)) last;
141 } cp_token_cache;
143 /* Prototypes. */
145 static cp_lexer *cp_lexer_new_main
146 (void);
147 static cp_lexer *cp_lexer_new_from_tokens
148 (cp_token_cache *tokens);
149 static void cp_lexer_destroy
150 (cp_lexer *);
151 static int cp_lexer_saving_tokens
152 (const cp_lexer *);
153 static cp_token_position cp_lexer_token_position
154 (cp_lexer *, bool);
155 static cp_token *cp_lexer_token_at
156 (cp_lexer *, cp_token_position);
157 static void cp_lexer_get_preprocessor_token
158 (cp_lexer *, cp_token *);
159 static inline cp_token *cp_lexer_peek_token
160 (cp_lexer *);
161 static cp_token *cp_lexer_peek_nth_token
162 (cp_lexer *, size_t);
163 static inline bool cp_lexer_next_token_is
164 (cp_lexer *, enum cpp_ttype);
165 static bool cp_lexer_next_token_is_not
166 (cp_lexer *, enum cpp_ttype);
167 static bool cp_lexer_next_token_is_keyword
168 (cp_lexer *, enum rid);
169 static cp_token *cp_lexer_consume_token
170 (cp_lexer *);
171 static void cp_lexer_purge_token
172 (cp_lexer *);
173 static void cp_lexer_purge_tokens_after
174 (cp_lexer *, cp_token_position);
175 static void cp_lexer_save_tokens
176 (cp_lexer *);
177 static void cp_lexer_commit_tokens
178 (cp_lexer *);
179 static void cp_lexer_rollback_tokens
180 (cp_lexer *);
181 #ifdef ENABLE_CHECKING
182 static void cp_lexer_print_token
183 (FILE *, cp_token *);
184 static inline bool cp_lexer_debugging_p
185 (cp_lexer *);
186 static void cp_lexer_start_debugging
187 (cp_lexer *) ATTRIBUTE_UNUSED;
188 static void cp_lexer_stop_debugging
189 (cp_lexer *) ATTRIBUTE_UNUSED;
190 #else
191 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
192 about passing NULL to functions that require non-NULL arguments
193 (fputs, fprintf). It will never be used, so all we need is a value
194 of the right type that's guaranteed not to be NULL. */
195 #define cp_lexer_debug_stream stdout
196 #define cp_lexer_print_token(str, tok) (void) 0
197 #define cp_lexer_debugging_p(lexer) 0
198 #endif /* ENABLE_CHECKING */
200 static cp_token_cache *cp_token_cache_new
201 (cp_token *, cp_token *);
203 static void cp_parser_initial_pragma
204 (cp_token *);
206 /* Manifest constants. */
207 #define CP_LEXER_BUFFER_SIZE 10000
208 #define CP_SAVED_TOKEN_STACK 5
210 /* A token type for keywords, as opposed to ordinary identifiers. */
211 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213 /* A token type for template-ids. If a template-id is processed while
214 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
215 the value of the CPP_TEMPLATE_ID is whatever was returned by
216 cp_parser_template_id. */
217 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219 /* A token type for nested-name-specifiers. If a
220 nested-name-specifier is processed while parsing tentatively, it is
221 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
222 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
223 cp_parser_nested_name_specifier_opt. */
224 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226 /* A token type for tokens that are not tokens at all; these are used
227 to represent slots in the array where there used to be a token
228 that has now been deleted. */
229 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231 /* The number of token types, including C++-specific ones. */
232 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
234 /* Variables. */
236 #ifdef ENABLE_CHECKING
237 /* The stream to which debugging output should be written. */
238 static FILE *cp_lexer_debug_stream;
239 #endif /* ENABLE_CHECKING */
241 /* Create a new main C++ lexer, the lexer that gets tokens from the
242 preprocessor. */
244 static cp_lexer *
245 cp_lexer_new_main (void)
247 cp_token first_token;
248 cp_lexer *lexer;
249 cp_token *pos;
250 size_t alloc;
251 size_t space;
252 cp_token *buffer;
254 /* It's possible that parsing the first pragma will load a PCH file,
255 which is a GC collection point. So we have to do that before
256 allocating any memory. */
257 cp_parser_initial_pragma (&first_token);
259 /* Tell c_lex_with_flags not to merge string constants. */
260 c_lex_return_raw_strings = true;
262 c_common_no_more_pch ();
264 /* Allocate the memory. */
265 lexer = GGC_CNEW (cp_lexer);
267 #ifdef ENABLE_CHECKING
268 /* Initially we are not debugging. */
269 lexer->debugging_p = false;
270 #endif /* ENABLE_CHECKING */
271 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
272 CP_SAVED_TOKEN_STACK);
274 /* Create the buffer. */
275 alloc = CP_LEXER_BUFFER_SIZE;
276 buffer = GGC_NEWVEC (cp_token, alloc);
278 /* Put the first token in the buffer. */
279 space = alloc;
280 pos = buffer;
281 *pos = first_token;
283 /* Get the remaining tokens from the preprocessor. */
284 while (pos->type != CPP_EOF)
286 pos++;
287 if (!--space)
289 space = alloc;
290 alloc *= 2;
291 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
292 pos = buffer + space;
294 cp_lexer_get_preprocessor_token (lexer, pos);
296 lexer->buffer = buffer;
297 lexer->buffer_length = alloc - space;
298 lexer->last_token = pos;
299 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301 /* Subsequent preprocessor diagnostics should use compiler
302 diagnostic functions to get the compiler source location. */
303 cpp_get_options (parse_in)->client_diagnostic = true;
304 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306 gcc_assert (lexer->next_token->type != CPP_PURGED);
307 return lexer;
310 /* Create a new lexer whose token stream is primed with the tokens in
311 CACHE. When these tokens are exhausted, no new tokens will be read. */
313 static cp_lexer *
314 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 cp_token *first = cache->first;
317 cp_token *last = cache->last;
318 cp_lexer *lexer = GGC_CNEW (cp_lexer);
320 /* We do not own the buffer. */
321 lexer->buffer = NULL;
322 lexer->buffer_length = 0;
323 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
324 lexer->last_token = last;
326 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
327 CP_SAVED_TOKEN_STACK);
329 #ifdef ENABLE_CHECKING
330 /* Initially we are not debugging. */
331 lexer->debugging_p = false;
332 #endif
334 gcc_assert (lexer->next_token->type != CPP_PURGED);
335 return lexer;
338 /* Frees all resources associated with LEXER. */
340 static void
341 cp_lexer_destroy (cp_lexer *lexer)
343 if (lexer->buffer)
344 ggc_free (lexer->buffer);
345 VEC_free (cp_token_position, heap, lexer->saved_tokens);
346 ggc_free (lexer);
349 /* Returns nonzero if debugging information should be output. */
351 #ifdef ENABLE_CHECKING
353 static inline bool
354 cp_lexer_debugging_p (cp_lexer *lexer)
356 return lexer->debugging_p;
359 #endif /* ENABLE_CHECKING */
361 static inline cp_token_position
362 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 gcc_assert (!previous_p || lexer->next_token != &eof_token);
366 return lexer->next_token - previous_p;
369 static inline cp_token *
370 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
372 return pos;
375 /* nonzero if we are presently saving tokens. */
377 static inline int
378 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
383 /* Store the next token from the preprocessor in *TOKEN. Return true
384 if we reach EOF. */
386 static void
387 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
388 cp_token *token)
390 static int is_extern_c = 0;
392 /* Get a new token from the preprocessor. */
393 token->type
394 = c_lex_with_flags (&token->value, &token->location, &token->flags);
395 token->keyword = RID_MAX;
396 token->pragma_kind = PRAGMA_NONE;
397 token->in_system_header = in_system_header;
399 /* On some systems, some header files are surrounded by an
400 implicit extern "C" block. Set a flag in the token if it
401 comes from such a header. */
402 is_extern_c += pending_lang_change;
403 pending_lang_change = 0;
404 token->implicit_extern_c = is_extern_c > 0;
406 /* Check to see if this token is a keyword. */
407 if (token->type == CPP_NAME)
409 if (C_IS_RESERVED_WORD (token->value))
411 /* Mark this token as a keyword. */
412 token->type = CPP_KEYWORD;
413 /* Record which keyword. */
414 token->keyword = C_RID_CODE (token->value);
415 /* Update the value. Some keywords are mapped to particular
416 entities, rather than simply having the value of the
417 corresponding IDENTIFIER_NODE. For example, `__const' is
418 mapped to `const'. */
419 token->value = ridpointers[token->keyword];
421 else
423 token->ambiguous_p = false;
424 token->keyword = RID_MAX;
427 /* Handle Objective-C++ keywords. */
428 else if (token->type == CPP_AT_NAME)
430 token->type = CPP_KEYWORD;
431 switch (C_RID_CODE (token->value))
433 /* Map 'class' to '@class', 'private' to '@private', etc. */
434 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438 case RID_THROW: token->keyword = RID_AT_THROW; break;
439 case RID_TRY: token->keyword = RID_AT_TRY; break;
440 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441 default: token->keyword = C_RID_CODE (token->value);
444 else if (token->type == CPP_PRAGMA)
446 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
447 token->pragma_kind = TREE_INT_CST_LOW (token->value);
448 token->value = NULL;
452 /* Update the globals input_location and in_system_header from TOKEN. */
453 static inline void
454 cp_lexer_set_source_position_from_token (cp_token *token)
456 if (token->type != CPP_EOF)
458 input_location = token->location;
459 in_system_header = token->in_system_header;
463 /* Return a pointer to the next token in the token stream, but do not
464 consume it. */
466 static inline cp_token *
467 cp_lexer_peek_token (cp_lexer *lexer)
469 if (cp_lexer_debugging_p (lexer))
471 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
472 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
473 putc ('\n', cp_lexer_debug_stream);
475 return lexer->next_token;
478 /* Return true if the next token has the indicated TYPE. */
480 static inline bool
481 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 return cp_lexer_peek_token (lexer)->type == type;
486 /* Return true if the next token does not have the indicated TYPE. */
488 static inline bool
489 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 return !cp_lexer_next_token_is (lexer, type);
494 /* Return true if the next token is the indicated KEYWORD. */
496 static inline bool
497 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
499 cp_token *token;
501 /* Peek at the next token. */
502 token = cp_lexer_peek_token (lexer);
503 /* Check to see if it is the indicated keyword. */
504 return token->keyword == keyword;
507 /* Return a pointer to the Nth token in the token stream. If N is 1,
508 then this is precisely equivalent to cp_lexer_peek_token (except
509 that it is not inline). One would like to disallow that case, but
510 there is one case (cp_parser_nth_token_starts_template_id) where
511 the caller passes a variable for N and it might be 1. */
513 static cp_token *
514 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
516 cp_token *token;
518 /* N is 1-based, not zero-based. */
519 gcc_assert (n > 0);
521 if (cp_lexer_debugging_p (lexer))
522 fprintf (cp_lexer_debug_stream,
523 "cp_lexer: peeking ahead %ld at token: ", (long)n);
525 --n;
526 token = lexer->next_token;
527 gcc_assert (!n || token != &eof_token);
528 while (n != 0)
530 ++token;
531 if (token == lexer->last_token)
533 token = (cp_token *)&eof_token;
534 break;
537 if (token->type != CPP_PURGED)
538 --n;
541 if (cp_lexer_debugging_p (lexer))
543 cp_lexer_print_token (cp_lexer_debug_stream, token);
544 putc ('\n', cp_lexer_debug_stream);
547 return token;
550 /* Return the next token, and advance the lexer's next_token pointer
551 to point to the next non-purged token. */
553 static cp_token *
554 cp_lexer_consume_token (cp_lexer* lexer)
556 cp_token *token = lexer->next_token;
558 gcc_assert (token != &eof_token);
559 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
563 lexer->next_token++;
564 if (lexer->next_token == lexer->last_token)
566 lexer->next_token = (cp_token *)&eof_token;
567 break;
571 while (lexer->next_token->type == CPP_PURGED);
573 cp_lexer_set_source_position_from_token (token);
575 /* Provide debugging output. */
576 if (cp_lexer_debugging_p (lexer))
578 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
579 cp_lexer_print_token (cp_lexer_debug_stream, token);
580 putc ('\n', cp_lexer_debug_stream);
583 return token;
586 /* Permanently remove the next token from the token stream, and
587 advance the next_token pointer to refer to the next non-purged
588 token. */
590 static void
591 cp_lexer_purge_token (cp_lexer *lexer)
593 cp_token *tok = lexer->next_token;
595 gcc_assert (tok != &eof_token);
596 tok->type = CPP_PURGED;
597 tok->location = UNKNOWN_LOCATION;
598 tok->value = NULL_TREE;
599 tok->keyword = RID_MAX;
603 tok++;
604 if (tok == lexer->last_token)
606 tok = (cp_token *)&eof_token;
607 break;
610 while (tok->type == CPP_PURGED);
611 lexer->next_token = tok;
614 /* Permanently remove all tokens after TOK, up to, but not
615 including, the token that will be returned next by
616 cp_lexer_peek_token. */
618 static void
619 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 cp_token *peek = lexer->next_token;
623 if (peek == &eof_token)
624 peek = lexer->last_token;
626 gcc_assert (tok < peek);
628 for ( tok += 1; tok != peek; tok += 1)
630 tok->type = CPP_PURGED;
631 tok->location = UNKNOWN_LOCATION;
632 tok->value = NULL_TREE;
633 tok->keyword = RID_MAX;
637 /* Begin saving tokens. All tokens consumed after this point will be
638 preserved. */
640 static void
641 cp_lexer_save_tokens (cp_lexer* lexer)
643 /* Provide debugging output. */
644 if (cp_lexer_debugging_p (lexer))
645 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647 VEC_safe_push (cp_token_position, heap,
648 lexer->saved_tokens, lexer->next_token);
651 /* Commit to the portion of the token stream most recently saved. */
653 static void
654 cp_lexer_commit_tokens (cp_lexer* lexer)
656 /* Provide debugging output. */
657 if (cp_lexer_debugging_p (lexer))
658 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660 VEC_pop (cp_token_position, lexer->saved_tokens);
663 /* Return all tokens saved since the last call to cp_lexer_save_tokens
664 to the token stream. Stop saving tokens. */
666 static void
667 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 /* Provide debugging output. */
670 if (cp_lexer_debugging_p (lexer))
671 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
676 /* Print a representation of the TOKEN on the STREAM. */
678 #ifdef ENABLE_CHECKING
680 static void
681 cp_lexer_print_token (FILE * stream, cp_token *token)
683 /* We don't use cpp_type2name here because the parser defines
684 a few tokens of its own. */
685 static const char *const token_names[] = {
686 /* cpplib-defined token types */
687 #define OP(e, s) #e,
688 #define TK(e, s) #e,
689 TTYPE_TABLE
690 #undef OP
691 #undef TK
692 /* C++ parser token types - see "Manifest constants", above. */
693 "KEYWORD",
694 "TEMPLATE_ID",
695 "NESTED_NAME_SPECIFIER",
696 "PURGED"
699 /* If we have a name for the token, print it out. Otherwise, we
700 simply give the numeric code. */
701 gcc_assert (token->type < ARRAY_SIZE(token_names));
702 fputs (token_names[token->type], stream);
704 /* For some tokens, print the associated data. */
705 switch (token->type)
707 case CPP_KEYWORD:
708 /* Some keywords have a value that is not an IDENTIFIER_NODE.
709 For example, `struct' is mapped to an INTEGER_CST. */
710 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
711 break;
712 /* else fall through */
713 case CPP_NAME:
714 fputs (IDENTIFIER_POINTER (token->value), stream);
715 break;
717 case CPP_STRING:
718 case CPP_WSTRING:
719 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720 break;
722 default:
723 break;
727 /* Start emitting debugging information. */
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
732 lexer->debugging_p = true;
735 /* Stop emitting debugging information. */
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
740 lexer->debugging_p = false;
743 #endif /* ENABLE_CHECKING */
745 /* Create a new cp_token_cache, representing a range of tokens. */
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
750 cp_token_cache *cache = GGC_NEW (cp_token_cache);
751 cache->first = first;
752 cache->last = last;
753 return cache;
757 /* Decl-specifiers. */
759 static void clear_decl_specs
760 (cp_decl_specifier_seq *);
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
767 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
770 /* Declarators. */
772 /* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
777 static cp_declarator *make_call_declarator
778 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780 (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784 (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786 (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788 (cp_cv_quals, tree, cp_declarator *);
790 cp_declarator *cp_error_declarator;
792 /* The obstack on which declarators and related data structures are
793 allocated. */
794 static struct obstack declarator_obstack;
796 /* Alloc BYTES from the declarator memory pool. */
798 static inline void *
799 alloc_declarator (size_t bytes)
801 return obstack_alloc (&declarator_obstack, bytes);
804 /* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
810 cp_declarator *declarator;
812 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813 declarator->kind = kind;
814 declarator->attributes = NULL_TREE;
815 declarator->declarator = NULL;
817 return declarator;
820 /* Make a declarator for a generalized identifier. If
821 QUALIFYING_SCOPE is non-NULL, the identifier is
822 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
823 UNQUALIFIED_NAME. SFK indicates the kind of special function this
824 is, if any. */
826 static cp_declarator *
827 make_id_declarator (tree qualifying_scope, tree unqualified_name,
828 special_function_kind sfk)
830 cp_declarator *declarator;
832 /* It is valid to write:
834 class C { void f(); };
835 typedef C D;
836 void D::f();
838 The standard is not clear about whether `typedef const C D' is
839 legal; as of 2002-09-15 the committee is considering that
840 question. EDG 3.0 allows that syntax. Therefore, we do as
841 well. */
842 if (qualifying_scope && TYPE_P (qualifying_scope))
843 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
845 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
846 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
847 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
849 declarator = make_declarator (cdk_id);
850 declarator->u.id.qualifying_scope = qualifying_scope;
851 declarator->u.id.unqualified_name = unqualified_name;
852 declarator->u.id.sfk = sfk;
854 return declarator;
857 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
858 of modifiers such as const or volatile to apply to the pointer
859 type, represented as identifiers. */
861 cp_declarator *
862 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
864 cp_declarator *declarator;
866 declarator = make_declarator (cdk_pointer);
867 declarator->declarator = target;
868 declarator->u.pointer.qualifiers = cv_qualifiers;
869 declarator->u.pointer.class_type = NULL_TREE;
871 return declarator;
874 /* Like make_pointer_declarator -- but for references. */
876 cp_declarator *
877 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
879 cp_declarator *declarator;
881 declarator = make_declarator (cdk_reference);
882 declarator->declarator = target;
883 declarator->u.pointer.qualifiers = cv_qualifiers;
884 declarator->u.pointer.class_type = NULL_TREE;
886 return declarator;
889 /* Like make_pointer_declarator -- but for a pointer to a non-static
890 member of CLASS_TYPE. */
892 cp_declarator *
893 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
894 cp_declarator *pointee)
896 cp_declarator *declarator;
898 declarator = make_declarator (cdk_ptrmem);
899 declarator->declarator = pointee;
900 declarator->u.pointer.qualifiers = cv_qualifiers;
901 declarator->u.pointer.class_type = class_type;
903 return declarator;
906 /* Make a declarator for the function given by TARGET, with the
907 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
908 "const"-qualified member function. The EXCEPTION_SPECIFICATION
909 indicates what exceptions can be thrown. */
911 cp_declarator *
912 make_call_declarator (cp_declarator *target,
913 cp_parameter_declarator *parms,
914 cp_cv_quals cv_qualifiers,
915 tree exception_specification)
917 cp_declarator *declarator;
919 declarator = make_declarator (cdk_function);
920 declarator->declarator = target;
921 declarator->u.function.parameters = parms;
922 declarator->u.function.qualifiers = cv_qualifiers;
923 declarator->u.function.exception_specification = exception_specification;
925 return declarator;
928 /* Make a declarator for an array of BOUNDS elements, each of which is
929 defined by ELEMENT. */
931 cp_declarator *
932 make_array_declarator (cp_declarator *element, tree bounds)
934 cp_declarator *declarator;
936 declarator = make_declarator (cdk_array);
937 declarator->declarator = element;
938 declarator->u.array.bounds = bounds;
940 return declarator;
943 cp_parameter_declarator *no_parameters;
945 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
946 DECLARATOR and DEFAULT_ARGUMENT. */
948 cp_parameter_declarator *
949 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
950 cp_declarator *declarator,
951 tree default_argument)
953 cp_parameter_declarator *parameter;
955 parameter = ((cp_parameter_declarator *)
956 alloc_declarator (sizeof (cp_parameter_declarator)));
957 parameter->next = NULL;
958 if (decl_specifiers)
959 parameter->decl_specifiers = *decl_specifiers;
960 else
961 clear_decl_specs (&parameter->decl_specifiers);
962 parameter->declarator = declarator;
963 parameter->default_argument = default_argument;
964 parameter->ellipsis_p = false;
966 return parameter;
969 /* The parser. */
971 /* Overview
972 --------
974 A cp_parser parses the token stream as specified by the C++
975 grammar. Its job is purely parsing, not semantic analysis. For
976 example, the parser breaks the token stream into declarators,
977 expressions, statements, and other similar syntactic constructs.
978 It does not check that the types of the expressions on either side
979 of an assignment-statement are compatible, or that a function is
980 not declared with a parameter of type `void'.
982 The parser invokes routines elsewhere in the compiler to perform
983 semantic analysis and to build up the abstract syntax tree for the
984 code processed.
986 The parser (and the template instantiation code, which is, in a
987 way, a close relative of parsing) are the only parts of the
988 compiler that should be calling push_scope and pop_scope, or
989 related functions. The parser (and template instantiation code)
990 keeps track of what scope is presently active; everything else
991 should simply honor that. (The code that generates static
992 initializers may also need to set the scope, in order to check
993 access control correctly when emitting the initializers.)
995 Methodology
996 -----------
998 The parser is of the standard recursive-descent variety. Upcoming
999 tokens in the token stream are examined in order to determine which
1000 production to use when parsing a non-terminal. Some C++ constructs
1001 require arbitrary look ahead to disambiguate. For example, it is
1002 impossible, in the general case, to tell whether a statement is an
1003 expression or declaration without scanning the entire statement.
1004 Therefore, the parser is capable of "parsing tentatively." When the
1005 parser is not sure what construct comes next, it enters this mode.
1006 Then, while we attempt to parse the construct, the parser queues up
1007 error messages, rather than issuing them immediately, and saves the
1008 tokens it consumes. If the construct is parsed successfully, the
1009 parser "commits", i.e., it issues any queued error messages and
1010 the tokens that were being preserved are permanently discarded.
1011 If, however, the construct is not parsed successfully, the parser
1012 rolls back its state completely so that it can resume parsing using
1013 a different alternative.
1015 Future Improvements
1016 -------------------
1018 The performance of the parser could probably be improved substantially.
1019 We could often eliminate the need to parse tentatively by looking ahead
1020 a little bit. In some places, this approach might not entirely eliminate
1021 the need to parse tentatively, but it might still speed up the average
1022 case. */
1024 /* Flags that are passed to some parsing functions. These values can
1025 be bitwise-ored together. */
1027 typedef enum cp_parser_flags
1029 /* No flags. */
1030 CP_PARSER_FLAGS_NONE = 0x0,
1031 /* The construct is optional. If it is not present, then no error
1032 should be issued. */
1033 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1034 /* When parsing a type-specifier, do not allow user-defined types. */
1035 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1036 } cp_parser_flags;
1038 /* The different kinds of declarators we want to parse. */
1040 typedef enum cp_parser_declarator_kind
1042 /* We want an abstract declarator. */
1043 CP_PARSER_DECLARATOR_ABSTRACT,
1044 /* We want a named declarator. */
1045 CP_PARSER_DECLARATOR_NAMED,
1046 /* We don't mind, but the name must be an unqualified-id. */
1047 CP_PARSER_DECLARATOR_EITHER
1048 } cp_parser_declarator_kind;
1050 /* The precedence values used to parse binary expressions. The minimum value
1051 of PREC must be 1, because zero is reserved to quickly discriminate
1052 binary operators from other tokens. */
1054 enum cp_parser_prec
1056 PREC_NOT_OPERATOR,
1057 PREC_LOGICAL_OR_EXPRESSION,
1058 PREC_LOGICAL_AND_EXPRESSION,
1059 PREC_INCLUSIVE_OR_EXPRESSION,
1060 PREC_EXCLUSIVE_OR_EXPRESSION,
1061 PREC_AND_EXPRESSION,
1062 PREC_EQUALITY_EXPRESSION,
1063 PREC_RELATIONAL_EXPRESSION,
1064 PREC_SHIFT_EXPRESSION,
1065 PREC_ADDITIVE_EXPRESSION,
1066 PREC_MULTIPLICATIVE_EXPRESSION,
1067 PREC_PM_EXPRESSION,
1068 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1071 /* A mapping from a token type to a corresponding tree node type, with a
1072 precedence value. */
1074 typedef struct cp_parser_binary_operations_map_node
1076 /* The token type. */
1077 enum cpp_ttype token_type;
1078 /* The corresponding tree code. */
1079 enum tree_code tree_type;
1080 /* The precedence of this operator. */
1081 enum cp_parser_prec prec;
1082 } cp_parser_binary_operations_map_node;
1084 /* The status of a tentative parse. */
1086 typedef enum cp_parser_status_kind
1088 /* No errors have occurred. */
1089 CP_PARSER_STATUS_KIND_NO_ERROR,
1090 /* An error has occurred. */
1091 CP_PARSER_STATUS_KIND_ERROR,
1092 /* We are committed to this tentative parse, whether or not an error
1093 has occurred. */
1094 CP_PARSER_STATUS_KIND_COMMITTED
1095 } cp_parser_status_kind;
1097 typedef struct cp_parser_expression_stack_entry
1099 tree lhs;
1100 enum tree_code tree_type;
1101 int prec;
1102 } cp_parser_expression_stack_entry;
1104 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1105 entries because precedence levels on the stack are monotonically
1106 increasing. */
1107 typedef struct cp_parser_expression_stack_entry
1108 cp_parser_expression_stack[NUM_PREC_VALUES];
1110 /* Context that is saved and restored when parsing tentatively. */
1111 typedef struct cp_parser_context GTY (())
1113 /* If this is a tentative parsing context, the status of the
1114 tentative parse. */
1115 enum cp_parser_status_kind status;
1116 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1117 that are looked up in this context must be looked up both in the
1118 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1119 the context of the containing expression. */
1120 tree object_type;
1122 /* The next parsing context in the stack. */
1123 struct cp_parser_context *next;
1124 } cp_parser_context;
1126 /* Prototypes. */
1128 /* Constructors and destructors. */
1130 static cp_parser_context *cp_parser_context_new
1131 (cp_parser_context *);
1133 /* Class variables. */
1135 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1137 /* The operator-precedence table used by cp_parser_binary_expression.
1138 Transformed into an associative array (binops_by_token) by
1139 cp_parser_new. */
1141 static const cp_parser_binary_operations_map_node binops[] = {
1142 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1143 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1145 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1149 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1152 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1155 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1157 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1160 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1162 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1163 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1165 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1167 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1169 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1171 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1173 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1176 /* The same as binops, but initialized by cp_parser_new so that
1177 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1178 for speed. */
1179 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1181 /* Constructors and destructors. */
1183 /* Construct a new context. The context below this one on the stack
1184 is given by NEXT. */
1186 static cp_parser_context *
1187 cp_parser_context_new (cp_parser_context* next)
1189 cp_parser_context *context;
1191 /* Allocate the storage. */
1192 if (cp_parser_context_free_list != NULL)
1194 /* Pull the first entry from the free list. */
1195 context = cp_parser_context_free_list;
1196 cp_parser_context_free_list = context->next;
1197 memset (context, 0, sizeof (*context));
1199 else
1200 context = GGC_CNEW (cp_parser_context);
1202 /* No errors have occurred yet in this context. */
1203 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1204 /* If this is not the bottomost context, copy information that we
1205 need from the previous context. */
1206 if (next)
1208 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1209 expression, then we are parsing one in this context, too. */
1210 context->object_type = next->object_type;
1211 /* Thread the stack. */
1212 context->next = next;
1215 return context;
1218 /* The cp_parser structure represents the C++ parser. */
1220 typedef struct cp_parser GTY(())
1222 /* The lexer from which we are obtaining tokens. */
1223 cp_lexer *lexer;
1225 /* The scope in which names should be looked up. If NULL_TREE, then
1226 we look up names in the scope that is currently open in the
1227 source program. If non-NULL, this is either a TYPE or
1228 NAMESPACE_DECL for the scope in which we should look. It can
1229 also be ERROR_MARK, when we've parsed a bogus scope.
1231 This value is not cleared automatically after a name is looked
1232 up, so we must be careful to clear it before starting a new look
1233 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1234 will look up `Z' in the scope of `X', rather than the current
1235 scope.) Unfortunately, it is difficult to tell when name lookup
1236 is complete, because we sometimes peek at a token, look it up,
1237 and then decide not to consume it. */
1238 tree scope;
1240 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1241 last lookup took place. OBJECT_SCOPE is used if an expression
1242 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1243 respectively. QUALIFYING_SCOPE is used for an expression of the
1244 form "X::Y"; it refers to X. */
1245 tree object_scope;
1246 tree qualifying_scope;
1248 /* A stack of parsing contexts. All but the bottom entry on the
1249 stack will be tentative contexts.
1251 We parse tentatively in order to determine which construct is in
1252 use in some situations. For example, in order to determine
1253 whether a statement is an expression-statement or a
1254 declaration-statement we parse it tentatively as a
1255 declaration-statement. If that fails, we then reparse the same
1256 token stream as an expression-statement. */
1257 cp_parser_context *context;
1259 /* True if we are parsing GNU C++. If this flag is not set, then
1260 GNU extensions are not recognized. */
1261 bool allow_gnu_extensions_p;
1263 /* TRUE if the `>' token should be interpreted as the greater-than
1264 operator. FALSE if it is the end of a template-id or
1265 template-parameter-list. */
1266 bool greater_than_is_operator_p;
1268 /* TRUE if default arguments are allowed within a parameter list
1269 that starts at this point. FALSE if only a gnu extension makes
1270 them permissible. */
1271 bool default_arg_ok_p;
1273 /* TRUE if we are parsing an integral constant-expression. See
1274 [expr.const] for a precise definition. */
1275 bool integral_constant_expression_p;
1277 /* TRUE if we are parsing an integral constant-expression -- but a
1278 non-constant expression should be permitted as well. This flag
1279 is used when parsing an array bound so that GNU variable-length
1280 arrays are tolerated. */
1281 bool allow_non_integral_constant_expression_p;
1283 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1284 been seen that makes the expression non-constant. */
1285 bool non_integral_constant_expression_p;
1287 /* TRUE if local variable names and `this' are forbidden in the
1288 current context. */
1289 bool local_variables_forbidden_p;
1291 /* TRUE if the declaration we are parsing is part of a
1292 linkage-specification of the form `extern string-literal
1293 declaration'. */
1294 bool in_unbraced_linkage_specification_p;
1296 /* TRUE if we are presently parsing a declarator, after the
1297 direct-declarator. */
1298 bool in_declarator_p;
1300 /* TRUE if we are presently parsing a template-argument-list. */
1301 bool in_template_argument_list_p;
1303 /* TRUE if we are presently parsing the body of an
1304 iteration-statement. */
1305 bool in_iteration_statement_p;
1307 /* TRUE if we are presently parsing the body of a switch
1308 statement. */
1309 bool in_switch_statement_p;
1311 /* TRUE if we are parsing a type-id in an expression context. In
1312 such a situation, both "type (expr)" and "type (type)" are valid
1313 alternatives. */
1314 bool in_type_id_in_expr_p;
1316 /* TRUE if we are currently in a header file where declarations are
1317 implicitly extern "C". */
1318 bool implicit_extern_c;
1320 /* TRUE if strings in expressions should be translated to the execution
1321 character set. */
1322 bool translate_strings_p;
1324 /* If non-NULL, then we are parsing a construct where new type
1325 definitions are not permitted. The string stored here will be
1326 issued as an error message if a type is defined. */
1327 const char *type_definition_forbidden_message;
1329 /* A list of lists. The outer list is a stack, used for member
1330 functions of local classes. At each level there are two sub-list,
1331 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1332 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1333 TREE_VALUE's. The functions are chained in reverse declaration
1334 order.
1336 The TREE_PURPOSE sublist contains those functions with default
1337 arguments that need post processing, and the TREE_VALUE sublist
1338 contains those functions with definitions that need post
1339 processing.
1341 These lists can only be processed once the outermost class being
1342 defined is complete. */
1343 tree unparsed_functions_queues;
1345 /* The number of classes whose definitions are currently in
1346 progress. */
1347 unsigned num_classes_being_defined;
1349 /* The number of template parameter lists that apply directly to the
1350 current declaration. */
1351 unsigned num_template_parameter_lists;
1352 } cp_parser;
1354 /* The type of a function that parses some kind of expression. */
1355 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1357 /* Prototypes. */
1359 /* Constructors and destructors. */
1361 static cp_parser *cp_parser_new
1362 (void);
1364 /* Routines to parse various constructs.
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
1369 attempted, even though a parse error occurred. So, to check
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1376 /* Lexical conventions [gram.lex] */
1378 static tree cp_parser_identifier
1379 (cp_parser *);
1380 static tree cp_parser_string_literal
1381 (cp_parser *, bool, bool);
1383 /* Basic concepts [gram.basic] */
1385 static bool cp_parser_translation_unit
1386 (cp_parser *);
1388 /* Expressions [gram.expr] */
1390 static tree cp_parser_primary_expression
1391 (cp_parser *, bool, bool, bool, cp_id_kind *);
1392 static tree cp_parser_id_expression
1393 (cp_parser *, bool, bool, bool *, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397 (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399 (cp_parser *, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401 (cp_parser *, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403 (cp_parser *, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405 (cp_parser *, tree, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1408 static tree cp_parser_parenthesized_expression_list
1409 (cp_parser *, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411 (cp_parser *, tree *, tree *);
1412 static tree cp_parser_unary_expression
1413 (cp_parser *, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1415 (cp_token *);
1416 static tree cp_parser_new_expression
1417 (cp_parser *);
1418 static tree cp_parser_new_placement
1419 (cp_parser *);
1420 static tree cp_parser_new_type_id
1421 (cp_parser *, tree *);
1422 static cp_declarator *cp_parser_new_declarator_opt
1423 (cp_parser *);
1424 static cp_declarator *cp_parser_direct_new_declarator
1425 (cp_parser *);
1426 static tree cp_parser_new_initializer
1427 (cp_parser *);
1428 static tree cp_parser_delete_expression
1429 (cp_parser *);
1430 static tree cp_parser_cast_expression
1431 (cp_parser *, bool, bool);
1432 static tree cp_parser_binary_expression
1433 (cp_parser *, bool);
1434 static tree cp_parser_question_colon_clause
1435 (cp_parser *, tree);
1436 static tree cp_parser_assignment_expression
1437 (cp_parser *, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1439 (cp_parser *);
1440 static tree cp_parser_expression
1441 (cp_parser *, bool);
1442 static tree cp_parser_constant_expression
1443 (cp_parser *, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1445 (cp_parser *);
1447 /* Statements [gram.stmt.stmt] */
1449 static void cp_parser_statement
1450 (cp_parser *, tree, bool);
1451 static tree cp_parser_labeled_statement
1452 (cp_parser *, tree, bool);
1453 static tree cp_parser_expression_statement
1454 (cp_parser *, tree);
1455 static tree cp_parser_compound_statement
1456 (cp_parser *, tree, bool);
1457 static void cp_parser_statement_seq_opt
1458 (cp_parser *, tree);
1459 static tree cp_parser_selection_statement
1460 (cp_parser *);
1461 static tree cp_parser_condition
1462 (cp_parser *);
1463 static tree cp_parser_iteration_statement
1464 (cp_parser *);
1465 static void cp_parser_for_init_statement
1466 (cp_parser *);
1467 static tree cp_parser_jump_statement
1468 (cp_parser *);
1469 static void cp_parser_declaration_statement
1470 (cp_parser *);
1472 static tree cp_parser_implicitly_scoped_statement
1473 (cp_parser *);
1474 static void cp_parser_already_scoped_statement
1475 (cp_parser *);
1477 /* Declarations [gram.dcl.dcl] */
1479 static void cp_parser_declaration_seq_opt
1480 (cp_parser *);
1481 static void cp_parser_declaration
1482 (cp_parser *);
1483 static void cp_parser_block_declaration
1484 (cp_parser *, bool);
1485 static void cp_parser_simple_declaration
1486 (cp_parser *, bool);
1487 static void cp_parser_decl_specifier_seq
1488 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1490 (cp_parser *);
1491 static tree cp_parser_function_specifier_opt
1492 (cp_parser *, cp_decl_specifier_seq *);
1493 static tree cp_parser_type_specifier
1494 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1495 int *, bool *);
1496 static tree cp_parser_simple_type_specifier
1497 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1498 static tree cp_parser_type_name
1499 (cp_parser *);
1500 static tree cp_parser_elaborated_type_specifier
1501 (cp_parser *, bool, bool);
1502 static tree cp_parser_enum_specifier
1503 (cp_parser *);
1504 static void cp_parser_enumerator_list
1505 (cp_parser *, tree);
1506 static void cp_parser_enumerator_definition
1507 (cp_parser *, tree);
1508 static tree cp_parser_namespace_name
1509 (cp_parser *);
1510 static void cp_parser_namespace_definition
1511 (cp_parser *);
1512 static void cp_parser_namespace_body
1513 (cp_parser *);
1514 static tree cp_parser_qualified_namespace_specifier
1515 (cp_parser *);
1516 static void cp_parser_namespace_alias_definition
1517 (cp_parser *);
1518 static void cp_parser_using_declaration
1519 (cp_parser *);
1520 static void cp_parser_using_directive
1521 (cp_parser *);
1522 static void cp_parser_asm_definition
1523 (cp_parser *);
1524 static void cp_parser_linkage_specification
1525 (cp_parser *);
1527 /* Declarators [gram.dcl.decl] */
1529 static tree cp_parser_init_declarator
1530 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1531 static cp_declarator *cp_parser_declarator
1532 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1533 static cp_declarator *cp_parser_direct_declarator
1534 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536 (cp_parser *, tree *, cp_cv_quals *);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1538 (cp_parser *);
1539 static tree cp_parser_declarator_id
1540 (cp_parser *);
1541 static tree cp_parser_type_id
1542 (cp_parser *);
1543 static void cp_parser_type_specifier_seq
1544 (cp_parser *, bool, cp_decl_specifier_seq *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1546 (cp_parser *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548 (cp_parser *, bool *);
1549 static cp_parameter_declarator *cp_parser_parameter_declaration
1550 (cp_parser *, bool, bool *);
1551 static void cp_parser_function_body
1552 (cp_parser *);
1553 static tree cp_parser_initializer
1554 (cp_parser *, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556 (cp_parser *, bool *);
1557 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1558 (cp_parser *, bool *);
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1561 (cp_parser *);
1563 /* Classes [gram.class] */
1565 static tree cp_parser_class_name
1566 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568 (cp_parser *);
1569 static tree cp_parser_class_head
1570 (cp_parser *, bool *, tree *);
1571 static enum tag_types cp_parser_class_key
1572 (cp_parser *);
1573 static void cp_parser_member_specification_opt
1574 (cp_parser *);
1575 static void cp_parser_member_declaration
1576 (cp_parser *);
1577 static tree cp_parser_pure_specifier
1578 (cp_parser *);
1579 static tree cp_parser_constant_initializer
1580 (cp_parser *);
1582 /* Derived classes [gram.class.derived] */
1584 static tree cp_parser_base_clause
1585 (cp_parser *);
1586 static tree cp_parser_base_specifier
1587 (cp_parser *);
1589 /* Special member functions [gram.special] */
1591 static tree cp_parser_conversion_function_id
1592 (cp_parser *);
1593 static tree cp_parser_conversion_type_id
1594 (cp_parser *);
1595 static cp_declarator *cp_parser_conversion_declarator_opt
1596 (cp_parser *);
1597 static bool cp_parser_ctor_initializer_opt
1598 (cp_parser *);
1599 static void cp_parser_mem_initializer_list
1600 (cp_parser *);
1601 static tree cp_parser_mem_initializer
1602 (cp_parser *);
1603 static tree cp_parser_mem_initializer_id
1604 (cp_parser *);
1606 /* Overloading [gram.over] */
1608 static tree cp_parser_operator_function_id
1609 (cp_parser *);
1610 static tree cp_parser_operator
1611 (cp_parser *);
1613 /* Templates [gram.temp] */
1615 static void cp_parser_template_declaration
1616 (cp_parser *, bool);
1617 static tree cp_parser_template_parameter_list
1618 (cp_parser *);
1619 static tree cp_parser_template_parameter
1620 (cp_parser *, bool *);
1621 static tree cp_parser_type_parameter
1622 (cp_parser *);
1623 static tree cp_parser_template_id
1624 (cp_parser *, bool, bool, bool);
1625 static tree cp_parser_template_name
1626 (cp_parser *, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1628 (cp_parser *);
1629 static tree cp_parser_template_argument
1630 (cp_parser *);
1631 static void cp_parser_explicit_instantiation
1632 (cp_parser *);
1633 static void cp_parser_explicit_specialization
1634 (cp_parser *);
1636 /* Exception handling [gram.exception] */
1638 static tree cp_parser_try_block
1639 (cp_parser *);
1640 static bool cp_parser_function_try_block
1641 (cp_parser *);
1642 static void cp_parser_handler_seq
1643 (cp_parser *);
1644 static void cp_parser_handler
1645 (cp_parser *);
1646 static tree cp_parser_exception_declaration
1647 (cp_parser *);
1648 static tree cp_parser_throw_expression
1649 (cp_parser *);
1650 static tree cp_parser_exception_specification_opt
1651 (cp_parser *);
1652 static tree cp_parser_type_id_list
1653 (cp_parser *);
1655 /* GNU Extensions */
1657 static tree cp_parser_asm_specification_opt
1658 (cp_parser *);
1659 static tree cp_parser_asm_operand_list
1660 (cp_parser *);
1661 static tree cp_parser_asm_clobber_list
1662 (cp_parser *);
1663 static tree cp_parser_attributes_opt
1664 (cp_parser *);
1665 static tree cp_parser_attribute_list
1666 (cp_parser *);
1667 static bool cp_parser_extension_opt
1668 (cp_parser *, int *);
1669 static void cp_parser_label_declaration
1670 (cp_parser *);
1672 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673 static bool cp_parser_pragma
1674 (cp_parser *, enum pragma_context);
1676 /* Objective-C++ Productions */
1678 static tree cp_parser_objc_message_receiver
1679 (cp_parser *);
1680 static tree cp_parser_objc_message_args
1681 (cp_parser *);
1682 static tree cp_parser_objc_message_expression
1683 (cp_parser *);
1684 static tree cp_parser_objc_encode_expression
1685 (cp_parser *);
1686 static tree cp_parser_objc_defs_expression
1687 (cp_parser *);
1688 static tree cp_parser_objc_protocol_expression
1689 (cp_parser *);
1690 static tree cp_parser_objc_selector_expression
1691 (cp_parser *);
1692 static tree cp_parser_objc_expression
1693 (cp_parser *);
1694 static bool cp_parser_objc_selector_p
1695 (enum cpp_ttype);
1696 static tree cp_parser_objc_selector
1697 (cp_parser *);
1698 static tree cp_parser_objc_protocol_refs_opt
1699 (cp_parser *);
1700 static void cp_parser_objc_declaration
1701 (cp_parser *);
1702 static tree cp_parser_objc_statement
1703 (cp_parser *);
1705 /* Utility Routines */
1707 static tree cp_parser_lookup_name
1708 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1709 static tree cp_parser_lookup_name_simple
1710 (cp_parser *, tree);
1711 static tree cp_parser_maybe_treat_template_as_class
1712 (tree, bool);
1713 static bool cp_parser_check_declarator_template_parameters
1714 (cp_parser *, cp_declarator *);
1715 static bool cp_parser_check_template_parameters
1716 (cp_parser *, unsigned);
1717 static tree cp_parser_simple_cast_expression
1718 (cp_parser *);
1719 static tree cp_parser_global_scope_opt
1720 (cp_parser *, bool);
1721 static bool cp_parser_constructor_declarator_p
1722 (cp_parser *, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1725 static tree cp_parser_function_definition_after_declarator
1726 (cp_parser *, bool);
1727 static void cp_parser_template_declaration_after_export
1728 (cp_parser *, bool);
1729 static tree cp_parser_single_declaration
1730 (cp_parser *, bool, bool *);
1731 static tree cp_parser_functional_cast
1732 (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1736 (cp_parser *);
1737 static void cp_parser_save_default_args
1738 (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740 (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742 (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744 (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1746 (cp_parser *);
1747 static void cp_parser_set_storage_class
1748 (cp_decl_specifier_seq *, cp_storage_class);
1749 static void cp_parser_set_decl_spec_type
1750 (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752 (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756 (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1758 (cp_token *);
1759 static bool cp_parser_next_token_starts_class_definition_p
1760 (cp_parser *);
1761 static bool cp_parser_next_token_ends_template_argument_p
1762 (cp_parser *);
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764 (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1766 (cp_token *);
1767 static void cp_parser_check_class_key
1768 (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1770 (tree type);
1771 static bool cp_parser_optional_template_keyword
1772 (cp_parser *);
1773 static void cp_parser_pre_parsed_nested_name_specifier
1774 (cp_parser *);
1775 static void cp_parser_cache_group
1776 (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1778 (cp_parser *);
1779 static void cp_parser_commit_to_tentative_parse
1780 (cp_parser *);
1781 static void cp_parser_abort_tentative_parse
1782 (cp_parser *);
1783 static bool cp_parser_parse_definitely
1784 (cp_parser *);
1785 static inline bool cp_parser_parsing_tentatively
1786 (cp_parser *);
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1788 (cp_parser *);
1789 static void cp_parser_error
1790 (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792 (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1794 (cp_parser *);
1795 static void cp_parser_check_type_definition
1796 (cp_parser *);
1797 static void cp_parser_check_for_definition_in_return_type
1798 (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800 (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802 (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804 (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1806 (cp_parser *);
1807 static int cp_parser_skip_to_closing_parenthesis
1808 (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1810 (cp_parser *);
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1812 (cp_parser *);
1813 static void cp_parser_skip_to_end_of_block_or_statement
1814 (cp_parser *);
1815 static void cp_parser_skip_to_closing_brace
1816 (cp_parser *);
1817 static void cp_parser_skip_until_found
1818 (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820 (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1822 (cp_parser *);
1823 static bool cp_parser_allow_gnu_extensions_p
1824 (cp_parser *);
1825 static bool cp_parser_is_string_literal
1826 (cp_token *);
1827 static bool cp_parser_is_keyword
1828 (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830 (cp_parser *, tree, tree);
1832 /* Returns nonzero if we are parsing tentatively. */
1834 static inline bool
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1837 return parser->context->next != NULL;
1840 /* Returns nonzero if TOKEN is a string literal. */
1842 static bool
1843 cp_parser_is_string_literal (cp_token* token)
1845 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1850 static bool
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1853 return token->keyword == keyword;
1856 /* A minimum or maximum operator has been seen. As these are
1857 deprecated, issue a warning. */
1859 static inline void
1860 cp_parser_warn_min_max (void)
1862 if (warn_deprecated && !in_system_header)
1863 warning (0, "minimum/maximum operators are deprecated");
1866 /* If not parsing tentatively, issue a diagnostic of the form
1867 FILE:LINE: MESSAGE before TOKEN
1868 where TOKEN is the next token in the input stream. MESSAGE
1869 (specified by the caller) is usually of the form "expected
1870 OTHER-TOKEN". */
1872 static void
1873 cp_parser_error (cp_parser* parser, const char* message)
1875 if (!cp_parser_simulate_error (parser))
1877 cp_token *token = cp_lexer_peek_token (parser->lexer);
1878 /* This diagnostic makes more sense if it is tagged to the line
1879 of the token we just peeked at. */
1880 cp_lexer_set_source_position_from_token (token);
1882 if (token->type == CPP_PRAGMA)
1884 error ("%<#pragma%> is not allowed here");
1885 cp_parser_skip_to_pragma_eol (parser, token);
1886 return;
1889 c_parse_error (message,
1890 /* Because c_parser_error does not understand
1891 CPP_KEYWORD, keywords are treated like
1892 identifiers. */
1893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1894 token->value);
1898 /* Issue an error about name-lookup failing. NAME is the
1899 IDENTIFIER_NODE DECL is the result of
1900 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1901 the thing that we hoped to find. */
1903 static void
1904 cp_parser_name_lookup_error (cp_parser* parser,
1905 tree name,
1906 tree decl,
1907 const char* desired)
1909 /* If name lookup completely failed, tell the user that NAME was not
1910 declared. */
1911 if (decl == error_mark_node)
1913 if (parser->scope && parser->scope != global_namespace)
1914 error ("%<%D::%D%> has not been declared",
1915 parser->scope, name);
1916 else if (parser->scope == global_namespace)
1917 error ("%<::%D%> has not been declared", name);
1918 else if (parser->object_scope
1919 && !CLASS_TYPE_P (parser->object_scope))
1920 error ("request for member %qD in non-class type %qT",
1921 name, parser->object_scope);
1922 else if (parser->object_scope)
1923 error ("%<%T::%D%> has not been declared",
1924 parser->object_scope, name);
1925 else
1926 error ("%qD has not been declared", name);
1928 else if (parser->scope && parser->scope != global_namespace)
1929 error ("%<%D::%D%> %s", parser->scope, name, desired);
1930 else if (parser->scope == global_namespace)
1931 error ("%<::%D%> %s", name, desired);
1932 else
1933 error ("%qD %s", name, desired);
1936 /* If we are parsing tentatively, remember that an error has occurred
1937 during this tentative parse. Returns true if the error was
1938 simulated; false if a message should be issued by the caller. */
1940 static bool
1941 cp_parser_simulate_error (cp_parser* parser)
1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1945 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1946 return true;
1948 return false;
1951 /* This function is called when a type is defined. If type
1952 definitions are forbidden at this point, an error message is
1953 issued. */
1955 static void
1956 cp_parser_check_type_definition (cp_parser* parser)
1958 /* If types are forbidden here, issue a message. */
1959 if (parser->type_definition_forbidden_message)
1960 /* Use `%s' to print the string in case there are any escape
1961 characters in the message. */
1962 error ("%s", parser->type_definition_forbidden_message);
1965 /* This function is called when the DECLARATOR is processed. The TYPE
1966 was a type defined in the decl-specifiers. If it is invalid to
1967 define a type in the decl-specifiers for DECLARATOR, an error is
1968 issued. */
1970 static void
1971 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1972 tree type)
1974 /* [dcl.fct] forbids type definitions in return types.
1975 Unfortunately, it's not easy to know whether or not we are
1976 processing a return type until after the fact. */
1977 while (declarator
1978 && (declarator->kind == cdk_pointer
1979 || declarator->kind == cdk_reference
1980 || declarator->kind == cdk_ptrmem))
1981 declarator = declarator->declarator;
1982 if (declarator
1983 && declarator->kind == cdk_function)
1985 error ("new types may not be defined in a return type");
1986 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1987 type);
1991 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1992 "<" in any valid C++ program. If the next token is indeed "<",
1993 issue a message warning the user about what appears to be an
1994 invalid attempt to form a template-id. */
1996 static void
1997 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1998 tree type)
2000 cp_token_position start = 0;
2002 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2004 if (TYPE_P (type))
2005 error ("%qT is not a template", type);
2006 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2007 error ("%qE is not a template", type);
2008 else
2009 error ("invalid template-id");
2010 /* Remember the location of the invalid "<". */
2011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2012 start = cp_lexer_token_position (parser->lexer, true);
2013 /* Consume the "<". */
2014 cp_lexer_consume_token (parser->lexer);
2015 /* Parse the template arguments. */
2016 cp_parser_enclosed_template_argument_list (parser);
2017 /* Permanently remove the invalid template arguments so that
2018 this error message is not issued again. */
2019 if (start)
2020 cp_lexer_purge_tokens_after (parser->lexer, start);
2024 /* If parsing an integral constant-expression, issue an error message
2025 about the fact that THING appeared and return true. Otherwise,
2026 return false. In either case, set
2027 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2029 static bool
2030 cp_parser_non_integral_constant_expression (cp_parser *parser,
2031 const char *thing)
2033 parser->non_integral_constant_expression_p = true;
2034 if (parser->integral_constant_expression_p)
2036 if (!parser->allow_non_integral_constant_expression_p)
2038 error ("%s cannot appear in a constant-expression", thing);
2039 return true;
2042 return false;
2045 /* Emit a diagnostic for an invalid type name. SCOPE is the
2046 qualifying scope (or NULL, if none) for ID. This function commits
2047 to the current active tentative parse, if any. (Otherwise, the
2048 problematic construct might be encountered again later, resulting
2049 in duplicate error messages.) */
2051 static void
2052 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2054 tree decl, old_scope;
2055 /* Try to lookup the identifier. */
2056 old_scope = parser->scope;
2057 parser->scope = scope;
2058 decl = cp_parser_lookup_name_simple (parser, id);
2059 parser->scope = old_scope;
2060 /* If the lookup found a template-name, it means that the user forgot
2061 to specify an argument list. Emit a useful error message. */
2062 if (TREE_CODE (decl) == TEMPLATE_DECL)
2063 error ("invalid use of template-name %qE without an argument list",
2064 decl);
2065 else if (!parser->scope)
2067 /* Issue an error message. */
2068 error ("%qE does not name a type", id);
2069 /* If we're in a template class, it's possible that the user was
2070 referring to a type from a base class. For example:
2072 template <typename T> struct A { typedef T X; };
2073 template <typename T> struct B : public A<T> { X x; };
2075 The user should have said "typename A<T>::X". */
2076 if (processing_template_decl && current_class_type
2077 && TYPE_BINFO (current_class_type))
2079 tree b;
2081 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2083 b = TREE_CHAIN (b))
2085 tree base_type = BINFO_TYPE (b);
2086 if (CLASS_TYPE_P (base_type)
2087 && dependent_type_p (base_type))
2089 tree field;
2090 /* Go from a particular instantiation of the
2091 template (which will have an empty TYPE_FIELDs),
2092 to the main version. */
2093 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2094 for (field = TYPE_FIELDS (base_type);
2095 field;
2096 field = TREE_CHAIN (field))
2097 if (TREE_CODE (field) == TYPE_DECL
2098 && DECL_NAME (field) == id)
2100 inform ("(perhaps %<typename %T::%E%> was intended)",
2101 BINFO_TYPE (b), id);
2102 break;
2104 if (field)
2105 break;
2110 /* Here we diagnose qualified-ids where the scope is actually correct,
2111 but the identifier does not resolve to a valid type name. */
2112 else if (parser->scope != error_mark_node)
2114 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2115 error ("%qE in namespace %qE does not name a type",
2116 id, parser->scope);
2117 else if (TYPE_P (parser->scope))
2118 error ("%qE in class %qT does not name a type", id, parser->scope);
2119 else
2120 gcc_unreachable ();
2122 cp_parser_commit_to_tentative_parse (parser);
2125 /* Check for a common situation where a type-name should be present,
2126 but is not, and issue a sensible error message. Returns true if an
2127 invalid type-name was detected.
2129 The situation handled by this function are variable declarations of the
2130 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2131 Usually, `ID' should name a type, but if we got here it means that it
2132 does not. We try to emit the best possible error message depending on
2133 how exactly the id-expression looks like.
2136 static bool
2137 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2139 tree id;
2141 cp_parser_parse_tentatively (parser);
2142 id = cp_parser_id_expression (parser,
2143 /*template_keyword_p=*/false,
2144 /*check_dependency_p=*/true,
2145 /*template_p=*/NULL,
2146 /*declarator_p=*/true);
2147 /* After the id-expression, there should be a plain identifier,
2148 otherwise this is not a simple variable declaration. Also, if
2149 the scope is dependent, we cannot do much. */
2150 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2151 || (parser->scope && TYPE_P (parser->scope)
2152 && dependent_type_p (parser->scope)))
2154 cp_parser_abort_tentative_parse (parser);
2155 return false;
2157 if (!cp_parser_parse_definitely (parser)
2158 || TREE_CODE (id) != IDENTIFIER_NODE)
2159 return false;
2161 /* Emit a diagnostic for the invalid type. */
2162 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2163 /* Skip to the end of the declaration; there's no point in
2164 trying to process it. */
2165 cp_parser_skip_to_end_of_block_or_statement (parser);
2166 return true;
2169 /* Consume tokens up to, and including, the next non-nested closing `)'.
2170 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2171 are doing error recovery. Returns -1 if OR_COMMA is true and we
2172 found an unnested comma. */
2174 static int
2175 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2176 bool recovering,
2177 bool or_comma,
2178 bool consume_paren)
2180 unsigned paren_depth = 0;
2181 unsigned brace_depth = 0;
2183 if (recovering && !or_comma
2184 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2185 return 0;
2187 while (true)
2189 cp_token * token = cp_lexer_peek_token (parser->lexer);
2191 switch (token->type)
2193 case CPP_EOF:
2194 case CPP_PRAGMA_EOL:
2195 /* If we've run out of tokens, then there is no closing `)'. */
2196 return 0;
2198 case CPP_SEMICOLON:
2199 /* This matches the processing in skip_to_end_of_statement. */
2200 if (!brace_depth)
2201 return 0;
2202 break;
2204 case CPP_OPEN_BRACE:
2205 ++brace_depth;
2206 break;
2207 case CPP_CLOSE_BRACE:
2208 if (!brace_depth--)
2209 return 0;
2210 break;
2212 case CPP_COMMA:
2213 if (recovering && or_comma && !brace_depth && !paren_depth)
2214 return -1;
2215 break;
2217 case CPP_OPEN_PAREN:
2218 if (!brace_depth)
2219 ++paren_depth;
2220 break;
2222 case CPP_CLOSE_PAREN:
2223 if (!brace_depth && !paren_depth--)
2225 if (consume_paren)
2226 cp_lexer_consume_token (parser->lexer);
2227 return 1;
2229 break;
2231 default:
2232 break;
2235 /* Consume the token. */
2236 cp_lexer_consume_token (parser->lexer);
2240 /* Consume tokens until we reach the end of the current statement.
2241 Normally, that will be just before consuming a `;'. However, if a
2242 non-nested `}' comes first, then we stop before consuming that. */
2244 static void
2245 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2247 unsigned nesting_depth = 0;
2249 while (true)
2251 cp_token *token = cp_lexer_peek_token (parser->lexer);
2253 switch (token->type)
2255 case CPP_EOF:
2256 case CPP_PRAGMA_EOL:
2257 /* If we've run out of tokens, stop. */
2258 return;
2260 case CPP_SEMICOLON:
2261 /* If the next token is a `;', we have reached the end of the
2262 statement. */
2263 if (!nesting_depth)
2264 return;
2265 break;
2267 case CPP_CLOSE_BRACE:
2268 /* If this is a non-nested '}', stop before consuming it.
2269 That way, when confronted with something like:
2271 { 3 + }
2273 we stop before consuming the closing '}', even though we
2274 have not yet reached a `;'. */
2275 if (nesting_depth == 0)
2276 return;
2278 /* If it is the closing '}' for a block that we have
2279 scanned, stop -- but only after consuming the token.
2280 That way given:
2282 void f g () { ... }
2283 typedef int I;
2285 we will stop after the body of the erroneously declared
2286 function, but before consuming the following `typedef'
2287 declaration. */
2288 if (--nesting_depth == 0)
2290 cp_lexer_consume_token (parser->lexer);
2291 return;
2294 case CPP_OPEN_BRACE:
2295 ++nesting_depth;
2296 break;
2298 default:
2299 break;
2302 /* Consume the token. */
2303 cp_lexer_consume_token (parser->lexer);
2307 /* This function is called at the end of a statement or declaration.
2308 If the next token is a semicolon, it is consumed; otherwise, error
2309 recovery is attempted. */
2311 static void
2312 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2314 /* Look for the trailing `;'. */
2315 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2317 /* If there is additional (erroneous) input, skip to the end of
2318 the statement. */
2319 cp_parser_skip_to_end_of_statement (parser);
2320 /* If the next token is now a `;', consume it. */
2321 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2322 cp_lexer_consume_token (parser->lexer);
2326 /* Skip tokens until we have consumed an entire block, or until we
2327 have consumed a non-nested `;'. */
2329 static void
2330 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2332 int nesting_depth = 0;
2334 while (nesting_depth >= 0)
2336 cp_token *token = cp_lexer_peek_token (parser->lexer);
2338 switch (token->type)
2340 case CPP_EOF:
2341 case CPP_PRAGMA_EOL:
2342 /* If we've run out of tokens, stop. */
2343 return;
2345 case CPP_SEMICOLON:
2346 /* Stop if this is an unnested ';'. */
2347 if (!nesting_depth)
2348 nesting_depth = -1;
2349 break;
2351 case CPP_CLOSE_BRACE:
2352 /* Stop if this is an unnested '}', or closes the outermost
2353 nesting level. */
2354 nesting_depth--;
2355 if (!nesting_depth)
2356 nesting_depth = -1;
2357 break;
2359 case CPP_OPEN_BRACE:
2360 /* Nest. */
2361 nesting_depth++;
2362 break;
2364 default:
2365 break;
2368 /* Consume the token. */
2369 cp_lexer_consume_token (parser->lexer);
2373 /* Skip tokens until a non-nested closing curly brace is the next
2374 token. */
2376 static void
2377 cp_parser_skip_to_closing_brace (cp_parser *parser)
2379 unsigned nesting_depth = 0;
2381 while (true)
2383 cp_token *token = cp_lexer_peek_token (parser->lexer);
2385 switch (token->type)
2387 case CPP_EOF:
2388 case CPP_PRAGMA_EOL:
2389 /* If we've run out of tokens, stop. */
2390 return;
2392 case CPP_CLOSE_BRACE:
2393 /* If the next token is a non-nested `}', then we have reached
2394 the end of the current block. */
2395 if (nesting_depth-- == 0)
2396 return;
2397 break;
2399 case CPP_OPEN_BRACE:
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2402 ++nesting_depth;
2403 break;
2405 default:
2406 break;
2409 /* Consume the token. */
2410 cp_lexer_consume_token (parser->lexer);
2414 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2415 parameter is the PRAGMA token, allowing us to purge the entire pragma
2416 sequence. */
2418 static void
2419 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2421 cp_token *token;
2423 parser->lexer->in_pragma = false;
2426 token = cp_lexer_consume_token (parser->lexer);
2427 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2429 /* Ensure that the pragma is not parsed again. */
2430 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2433 /* This is a simple wrapper around make_typename_type. When the id is
2434 an unresolved identifier node, we can provide a superior diagnostic
2435 using cp_parser_diagnose_invalid_type_name. */
2437 static tree
2438 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2440 tree result;
2441 if (TREE_CODE (id) == IDENTIFIER_NODE)
2443 result = make_typename_type (scope, id, typename_type,
2444 /*complain=*/tf_none);
2445 if (result == error_mark_node)
2446 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2447 return result;
2449 return make_typename_type (scope, id, typename_type, tf_error);
2453 /* Create a new C++ parser. */
2455 static cp_parser *
2456 cp_parser_new (void)
2458 cp_parser *parser;
2459 cp_lexer *lexer;
2460 unsigned i;
2462 /* cp_lexer_new_main is called before calling ggc_alloc because
2463 cp_lexer_new_main might load a PCH file. */
2464 lexer = cp_lexer_new_main ();
2466 /* Initialize the binops_by_token so that we can get the tree
2467 directly from the token. */
2468 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2469 binops_by_token[binops[i].token_type] = binops[i];
2471 parser = GGC_CNEW (cp_parser);
2472 parser->lexer = lexer;
2473 parser->context = cp_parser_context_new (NULL);
2475 /* For now, we always accept GNU extensions. */
2476 parser->allow_gnu_extensions_p = 1;
2478 /* The `>' token is a greater-than operator, not the end of a
2479 template-id. */
2480 parser->greater_than_is_operator_p = true;
2482 parser->default_arg_ok_p = true;
2484 /* We are not parsing a constant-expression. */
2485 parser->integral_constant_expression_p = false;
2486 parser->allow_non_integral_constant_expression_p = false;
2487 parser->non_integral_constant_expression_p = false;
2489 /* Local variable names are not forbidden. */
2490 parser->local_variables_forbidden_p = false;
2492 /* We are not processing an `extern "C"' declaration. */
2493 parser->in_unbraced_linkage_specification_p = false;
2495 /* We are not processing a declarator. */
2496 parser->in_declarator_p = false;
2498 /* We are not processing a template-argument-list. */
2499 parser->in_template_argument_list_p = false;
2501 /* We are not in an iteration statement. */
2502 parser->in_iteration_statement_p = false;
2504 /* We are not in a switch statement. */
2505 parser->in_switch_statement_p = false;
2507 /* We are not parsing a type-id inside an expression. */
2508 parser->in_type_id_in_expr_p = false;
2510 /* Declarations aren't implicitly extern "C". */
2511 parser->implicit_extern_c = false;
2513 /* String literals should be translated to the execution character set. */
2514 parser->translate_strings_p = true;
2516 /* The unparsed function queue is empty. */
2517 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2519 /* There are no classes being defined. */
2520 parser->num_classes_being_defined = 0;
2522 /* No template parameters apply. */
2523 parser->num_template_parameter_lists = 0;
2525 return parser;
2528 /* Create a cp_lexer structure which will emit the tokens in CACHE
2529 and push it onto the parser's lexer stack. This is used for delayed
2530 parsing of in-class method bodies and default arguments, and should
2531 not be confused with tentative parsing. */
2532 static void
2533 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2535 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2536 lexer->next = parser->lexer;
2537 parser->lexer = lexer;
2539 /* Move the current source position to that of the first token in the
2540 new lexer. */
2541 cp_lexer_set_source_position_from_token (lexer->next_token);
2544 /* Pop the top lexer off the parser stack. This is never used for the
2545 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2546 static void
2547 cp_parser_pop_lexer (cp_parser *parser)
2549 cp_lexer *lexer = parser->lexer;
2550 parser->lexer = lexer->next;
2551 cp_lexer_destroy (lexer);
2553 /* Put the current source position back where it was before this
2554 lexer was pushed. */
2555 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2558 /* Lexical conventions [gram.lex] */
2560 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2561 identifier. */
2563 static tree
2564 cp_parser_identifier (cp_parser* parser)
2566 cp_token *token;
2568 /* Look for the identifier. */
2569 token = cp_parser_require (parser, CPP_NAME, "identifier");
2570 /* Return the value. */
2571 return token ? token->value : error_mark_node;
2574 /* Parse a sequence of adjacent string constants. Returns a
2575 TREE_STRING representing the combined, nul-terminated string
2576 constant. If TRANSLATE is true, translate the string to the
2577 execution character set. If WIDE_OK is true, a wide string is
2578 invalid here.
2580 C++98 [lex.string] says that if a narrow string literal token is
2581 adjacent to a wide string literal token, the behavior is undefined.
2582 However, C99 6.4.5p4 says that this results in a wide string literal.
2583 We follow C99 here, for consistency with the C front end.
2585 This code is largely lifted from lex_string() in c-lex.c.
2587 FUTURE: ObjC++ will need to handle @-strings here. */
2588 static tree
2589 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2591 tree value;
2592 bool wide = false;
2593 size_t count;
2594 struct obstack str_ob;
2595 cpp_string str, istr, *strs;
2596 cp_token *tok;
2598 tok = cp_lexer_peek_token (parser->lexer);
2599 if (!cp_parser_is_string_literal (tok))
2601 cp_parser_error (parser, "expected string-literal");
2602 return error_mark_node;
2605 /* Try to avoid the overhead of creating and destroying an obstack
2606 for the common case of just one string. */
2607 if (!cp_parser_is_string_literal
2608 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2610 cp_lexer_consume_token (parser->lexer);
2612 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2613 str.len = TREE_STRING_LENGTH (tok->value);
2614 count = 1;
2615 if (tok->type == CPP_WSTRING)
2616 wide = true;
2618 strs = &str;
2620 else
2622 gcc_obstack_init (&str_ob);
2623 count = 0;
2627 cp_lexer_consume_token (parser->lexer);
2628 count++;
2629 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2630 str.len = TREE_STRING_LENGTH (tok->value);
2631 if (tok->type == CPP_WSTRING)
2632 wide = true;
2634 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2636 tok = cp_lexer_peek_token (parser->lexer);
2638 while (cp_parser_is_string_literal (tok));
2640 strs = (cpp_string *) obstack_finish (&str_ob);
2643 if (wide && !wide_ok)
2645 cp_parser_error (parser, "a wide string is invalid in this context");
2646 wide = false;
2649 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2650 (parse_in, strs, count, &istr, wide))
2652 value = build_string (istr.len, (char *)istr.text);
2653 free ((void *)istr.text);
2655 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2656 value = fix_string_type (value);
2658 else
2659 /* cpp_interpret_string has issued an error. */
2660 value = error_mark_node;
2662 if (count > 1)
2663 obstack_free (&str_ob, 0);
2665 return value;
2669 /* Basic concepts [gram.basic] */
2671 /* Parse a translation-unit.
2673 translation-unit:
2674 declaration-seq [opt]
2676 Returns TRUE if all went well. */
2678 static bool
2679 cp_parser_translation_unit (cp_parser* parser)
2681 /* The address of the first non-permanent object on the declarator
2682 obstack. */
2683 static void *declarator_obstack_base;
2685 bool success;
2687 /* Create the declarator obstack, if necessary. */
2688 if (!cp_error_declarator)
2690 gcc_obstack_init (&declarator_obstack);
2691 /* Create the error declarator. */
2692 cp_error_declarator = make_declarator (cdk_error);
2693 /* Create the empty parameter list. */
2694 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2695 /* Remember where the base of the declarator obstack lies. */
2696 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2699 cp_parser_declaration_seq_opt (parser);
2701 /* If there are no tokens left then all went well. */
2702 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2704 /* Get rid of the token array; we don't need it any more. */
2705 cp_lexer_destroy (parser->lexer);
2706 parser->lexer = NULL;
2708 /* This file might have been a context that's implicitly extern
2709 "C". If so, pop the lang context. (Only relevant for PCH.) */
2710 if (parser->implicit_extern_c)
2712 pop_lang_context ();
2713 parser->implicit_extern_c = false;
2716 /* Finish up. */
2717 finish_translation_unit ();
2719 success = true;
2721 else
2723 cp_parser_error (parser, "expected declaration");
2724 success = false;
2727 /* Make sure the declarator obstack was fully cleaned up. */
2728 gcc_assert (obstack_next_free (&declarator_obstack)
2729 == declarator_obstack_base);
2731 /* All went well. */
2732 return success;
2735 /* Expressions [gram.expr] */
2737 /* Parse a primary-expression.
2739 primary-expression:
2740 literal
2741 this
2742 ( expression )
2743 id-expression
2745 GNU Extensions:
2747 primary-expression:
2748 ( compound-statement )
2749 __builtin_va_arg ( assignment-expression , type-id )
2750 __builtin_offsetof ( type-id , offsetof-expression )
2752 Objective-C++ Extension:
2754 primary-expression:
2755 objc-expression
2757 literal:
2758 __null
2760 ADDRESS_P is true iff this expression was immediately preceded by
2761 "&" and therefore might denote a pointer-to-member. CAST_P is true
2762 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2763 true iff this expression is a template argument.
2765 Returns a representation of the expression. Upon return, *IDK
2766 indicates what kind of id-expression (if any) was present. */
2768 static tree
2769 cp_parser_primary_expression (cp_parser *parser,
2770 bool address_p,
2771 bool cast_p,
2772 bool template_arg_p,
2773 cp_id_kind *idk)
2775 cp_token *token;
2777 /* Assume the primary expression is not an id-expression. */
2778 *idk = CP_ID_KIND_NONE;
2780 /* Peek at the next token. */
2781 token = cp_lexer_peek_token (parser->lexer);
2782 switch (token->type)
2784 /* literal:
2785 integer-literal
2786 character-literal
2787 floating-literal
2788 string-literal
2789 boolean-literal */
2790 case CPP_CHAR:
2791 case CPP_WCHAR:
2792 case CPP_NUMBER:
2793 token = cp_lexer_consume_token (parser->lexer);
2794 /* Floating-point literals are only allowed in an integral
2795 constant expression if they are cast to an integral or
2796 enumeration type. */
2797 if (TREE_CODE (token->value) == REAL_CST
2798 && parser->integral_constant_expression_p
2799 && pedantic)
2801 /* CAST_P will be set even in invalid code like "int(2.7 +
2802 ...)". Therefore, we have to check that the next token
2803 is sure to end the cast. */
2804 if (cast_p)
2806 cp_token *next_token;
2808 next_token = cp_lexer_peek_token (parser->lexer);
2809 if (/* The comma at the end of an
2810 enumerator-definition. */
2811 next_token->type != CPP_COMMA
2812 /* The curly brace at the end of an enum-specifier. */
2813 && next_token->type != CPP_CLOSE_BRACE
2814 /* The end of a statement. */
2815 && next_token->type != CPP_SEMICOLON
2816 /* The end of the cast-expression. */
2817 && next_token->type != CPP_CLOSE_PAREN
2818 /* The end of an array bound. */
2819 && next_token->type != CPP_CLOSE_SQUARE
2820 /* The closing ">" in a template-argument-list. */
2821 && (next_token->type != CPP_GREATER
2822 || parser->greater_than_is_operator_p))
2823 cast_p = false;
2826 /* If we are within a cast, then the constraint that the
2827 cast is to an integral or enumeration type will be
2828 checked at that point. If we are not within a cast, then
2829 this code is invalid. */
2830 if (!cast_p)
2831 cp_parser_non_integral_constant_expression
2832 (parser, "floating-point literal");
2834 return token->value;
2836 case CPP_STRING:
2837 case CPP_WSTRING:
2838 /* ??? Should wide strings be allowed when parser->translate_strings_p
2839 is false (i.e. in attributes)? If not, we can kill the third
2840 argument to cp_parser_string_literal. */
2841 return cp_parser_string_literal (parser,
2842 parser->translate_strings_p,
2843 true);
2845 case CPP_OPEN_PAREN:
2847 tree expr;
2848 bool saved_greater_than_is_operator_p;
2850 /* Consume the `('. */
2851 cp_lexer_consume_token (parser->lexer);
2852 /* Within a parenthesized expression, a `>' token is always
2853 the greater-than operator. */
2854 saved_greater_than_is_operator_p
2855 = parser->greater_than_is_operator_p;
2856 parser->greater_than_is_operator_p = true;
2857 /* If we see `( { ' then we are looking at the beginning of
2858 a GNU statement-expression. */
2859 if (cp_parser_allow_gnu_extensions_p (parser)
2860 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2862 /* Statement-expressions are not allowed by the standard. */
2863 if (pedantic)
2864 pedwarn ("ISO C++ forbids braced-groups within expressions");
2866 /* And they're not allowed outside of a function-body; you
2867 cannot, for example, write:
2869 int i = ({ int j = 3; j + 1; });
2871 at class or namespace scope. */
2872 if (!at_function_scope_p ())
2873 error ("statement-expressions are allowed only inside functions");
2874 /* Start the statement-expression. */
2875 expr = begin_stmt_expr ();
2876 /* Parse the compound-statement. */
2877 cp_parser_compound_statement (parser, expr, false);
2878 /* Finish up. */
2879 expr = finish_stmt_expr (expr, false);
2881 else
2883 /* Parse the parenthesized expression. */
2884 expr = cp_parser_expression (parser, cast_p);
2885 /* Let the front end know that this expression was
2886 enclosed in parentheses. This matters in case, for
2887 example, the expression is of the form `A::B', since
2888 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2889 not. */
2890 finish_parenthesized_expr (expr);
2892 /* The `>' token might be the end of a template-id or
2893 template-parameter-list now. */
2894 parser->greater_than_is_operator_p
2895 = saved_greater_than_is_operator_p;
2896 /* Consume the `)'. */
2897 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2898 cp_parser_skip_to_end_of_statement (parser);
2900 return expr;
2903 case CPP_KEYWORD:
2904 switch (token->keyword)
2906 /* These two are the boolean literals. */
2907 case RID_TRUE:
2908 cp_lexer_consume_token (parser->lexer);
2909 return boolean_true_node;
2910 case RID_FALSE:
2911 cp_lexer_consume_token (parser->lexer);
2912 return boolean_false_node;
2914 /* The `__null' literal. */
2915 case RID_NULL:
2916 cp_lexer_consume_token (parser->lexer);
2917 return null_node;
2919 /* Recognize the `this' keyword. */
2920 case RID_THIS:
2921 cp_lexer_consume_token (parser->lexer);
2922 if (parser->local_variables_forbidden_p)
2924 error ("%<this%> may not be used in this context");
2925 return error_mark_node;
2927 /* Pointers cannot appear in constant-expressions. */
2928 if (cp_parser_non_integral_constant_expression (parser,
2929 "`this'"))
2930 return error_mark_node;
2931 return finish_this_expr ();
2933 /* The `operator' keyword can be the beginning of an
2934 id-expression. */
2935 case RID_OPERATOR:
2936 goto id_expression;
2938 case RID_FUNCTION_NAME:
2939 case RID_PRETTY_FUNCTION_NAME:
2940 case RID_C99_FUNCTION_NAME:
2941 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2942 __func__ are the names of variables -- but they are
2943 treated specially. Therefore, they are handled here,
2944 rather than relying on the generic id-expression logic
2945 below. Grammatically, these names are id-expressions.
2947 Consume the token. */
2948 token = cp_lexer_consume_token (parser->lexer);
2949 /* Look up the name. */
2950 return finish_fname (token->value);
2952 case RID_VA_ARG:
2954 tree expression;
2955 tree type;
2957 /* The `__builtin_va_arg' construct is used to handle
2958 `va_arg'. Consume the `__builtin_va_arg' token. */
2959 cp_lexer_consume_token (parser->lexer);
2960 /* Look for the opening `('. */
2961 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2962 /* Now, parse the assignment-expression. */
2963 expression = cp_parser_assignment_expression (parser,
2964 /*cast_p=*/false);
2965 /* Look for the `,'. */
2966 cp_parser_require (parser, CPP_COMMA, "`,'");
2967 /* Parse the type-id. */
2968 type = cp_parser_type_id (parser);
2969 /* Look for the closing `)'. */
2970 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2971 /* Using `va_arg' in a constant-expression is not
2972 allowed. */
2973 if (cp_parser_non_integral_constant_expression (parser,
2974 "`va_arg'"))
2975 return error_mark_node;
2976 return build_x_va_arg (expression, type);
2979 case RID_OFFSETOF:
2980 return cp_parser_builtin_offsetof (parser);
2982 /* Objective-C++ expressions. */
2983 case RID_AT_ENCODE:
2984 case RID_AT_PROTOCOL:
2985 case RID_AT_SELECTOR:
2986 return cp_parser_objc_expression (parser);
2988 default:
2989 cp_parser_error (parser, "expected primary-expression");
2990 return error_mark_node;
2993 /* An id-expression can start with either an identifier, a
2994 `::' as the beginning of a qualified-id, or the "operator"
2995 keyword. */
2996 case CPP_NAME:
2997 case CPP_SCOPE:
2998 case CPP_TEMPLATE_ID:
2999 case CPP_NESTED_NAME_SPECIFIER:
3001 tree id_expression;
3002 tree decl;
3003 const char *error_msg;
3004 bool template_p;
3005 bool done;
3007 id_expression:
3008 /* Parse the id-expression. */
3009 id_expression
3010 = cp_parser_id_expression (parser,
3011 /*template_keyword_p=*/false,
3012 /*check_dependency_p=*/true,
3013 &template_p,
3014 /*declarator_p=*/false);
3015 if (id_expression == error_mark_node)
3016 return error_mark_node;
3017 token = cp_lexer_peek_token (parser->lexer);
3018 done = (token->type != CPP_OPEN_SQUARE
3019 && token->type != CPP_OPEN_PAREN
3020 && token->type != CPP_DOT
3021 && token->type != CPP_DEREF
3022 && token->type != CPP_PLUS_PLUS
3023 && token->type != CPP_MINUS_MINUS);
3024 /* If we have a template-id, then no further lookup is
3025 required. If the template-id was for a template-class, we
3026 will sometimes have a TYPE_DECL at this point. */
3027 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3028 || TREE_CODE (id_expression) == TYPE_DECL)
3029 decl = id_expression;
3030 /* Look up the name. */
3031 else
3033 tree ambiguous_decls;
3035 decl = cp_parser_lookup_name (parser, id_expression,
3036 none_type,
3037 template_p,
3038 /*is_namespace=*/false,
3039 /*check_dependency=*/true,
3040 &ambiguous_decls);
3041 /* If the lookup was ambiguous, an error will already have
3042 been issued. */
3043 if (ambiguous_decls)
3044 return error_mark_node;
3046 /* In Objective-C++, an instance variable (ivar) may be preferred
3047 to whatever cp_parser_lookup_name() found. */
3048 decl = objc_lookup_ivar (decl, id_expression);
3050 /* If name lookup gives us a SCOPE_REF, then the
3051 qualifying scope was dependent. */
3052 if (TREE_CODE (decl) == SCOPE_REF)
3053 return decl;
3054 /* Check to see if DECL is a local variable in a context
3055 where that is forbidden. */
3056 if (parser->local_variables_forbidden_p
3057 && local_variable_p (decl))
3059 /* It might be that we only found DECL because we are
3060 trying to be generous with pre-ISO scoping rules.
3061 For example, consider:
3063 int i;
3064 void g() {
3065 for (int i = 0; i < 10; ++i) {}
3066 extern void f(int j = i);
3069 Here, name look up will originally find the out
3070 of scope `i'. We need to issue a warning message,
3071 but then use the global `i'. */
3072 decl = check_for_out_of_scope_variable (decl);
3073 if (local_variable_p (decl))
3075 error ("local variable %qD may not appear in this context",
3076 decl);
3077 return error_mark_node;
3082 decl = (finish_id_expression
3083 (id_expression, decl, parser->scope,
3084 idk,
3085 parser->integral_constant_expression_p,
3086 parser->allow_non_integral_constant_expression_p,
3087 &parser->non_integral_constant_expression_p,
3088 template_p, done, address_p,
3089 template_arg_p,
3090 &error_msg));
3091 if (error_msg)
3092 cp_parser_error (parser, error_msg);
3093 return decl;
3096 /* Anything else is an error. */
3097 default:
3098 /* ...unless we have an Objective-C++ message or string literal, that is. */
3099 if (c_dialect_objc ()
3100 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3101 return cp_parser_objc_expression (parser);
3103 cp_parser_error (parser, "expected primary-expression");
3104 return error_mark_node;
3108 /* Parse an id-expression.
3110 id-expression:
3111 unqualified-id
3112 qualified-id
3114 qualified-id:
3115 :: [opt] nested-name-specifier template [opt] unqualified-id
3116 :: identifier
3117 :: operator-function-id
3118 :: template-id
3120 Return a representation of the unqualified portion of the
3121 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3122 a `::' or nested-name-specifier.
3124 Often, if the id-expression was a qualified-id, the caller will
3125 want to make a SCOPE_REF to represent the qualified-id. This
3126 function does not do this in order to avoid wastefully creating
3127 SCOPE_REFs when they are not required.
3129 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3130 `template' keyword.
3132 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3133 uninstantiated templates.
3135 If *TEMPLATE_P is non-NULL, it is set to true iff the
3136 `template' keyword is used to explicitly indicate that the entity
3137 named is a template.
3139 If DECLARATOR_P is true, the id-expression is appearing as part of
3140 a declarator, rather than as part of an expression. */
3142 static tree
3143 cp_parser_id_expression (cp_parser *parser,
3144 bool template_keyword_p,
3145 bool check_dependency_p,
3146 bool *template_p,
3147 bool declarator_p)
3149 bool global_scope_p;
3150 bool nested_name_specifier_p;
3152 /* Assume the `template' keyword was not used. */
3153 if (template_p)
3154 *template_p = template_keyword_p;
3156 /* Look for the optional `::' operator. */
3157 global_scope_p
3158 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3159 != NULL_TREE);
3160 /* Look for the optional nested-name-specifier. */
3161 nested_name_specifier_p
3162 = (cp_parser_nested_name_specifier_opt (parser,
3163 /*typename_keyword_p=*/false,
3164 check_dependency_p,
3165 /*type_p=*/false,
3166 declarator_p)
3167 != NULL_TREE);
3168 /* If there is a nested-name-specifier, then we are looking at
3169 the first qualified-id production. */
3170 if (nested_name_specifier_p)
3172 tree saved_scope;
3173 tree saved_object_scope;
3174 tree saved_qualifying_scope;
3175 tree unqualified_id;
3176 bool is_template;
3178 /* See if the next token is the `template' keyword. */
3179 if (!template_p)
3180 template_p = &is_template;
3181 *template_p = cp_parser_optional_template_keyword (parser);
3182 /* Name lookup we do during the processing of the
3183 unqualified-id might obliterate SCOPE. */
3184 saved_scope = parser->scope;
3185 saved_object_scope = parser->object_scope;
3186 saved_qualifying_scope = parser->qualifying_scope;
3187 /* Process the final unqualified-id. */
3188 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3189 check_dependency_p,
3190 declarator_p);
3191 /* Restore the SAVED_SCOPE for our caller. */
3192 parser->scope = saved_scope;
3193 parser->object_scope = saved_object_scope;
3194 parser->qualifying_scope = saved_qualifying_scope;
3196 return unqualified_id;
3198 /* Otherwise, if we are in global scope, then we are looking at one
3199 of the other qualified-id productions. */
3200 else if (global_scope_p)
3202 cp_token *token;
3203 tree id;
3205 /* Peek at the next token. */
3206 token = cp_lexer_peek_token (parser->lexer);
3208 /* If it's an identifier, and the next token is not a "<", then
3209 we can avoid the template-id case. This is an optimization
3210 for this common case. */
3211 if (token->type == CPP_NAME
3212 && !cp_parser_nth_token_starts_template_argument_list_p
3213 (parser, 2))
3214 return cp_parser_identifier (parser);
3216 cp_parser_parse_tentatively (parser);
3217 /* Try a template-id. */
3218 id = cp_parser_template_id (parser,
3219 /*template_keyword_p=*/false,
3220 /*check_dependency_p=*/true,
3221 declarator_p);
3222 /* If that worked, we're done. */
3223 if (cp_parser_parse_definitely (parser))
3224 return id;
3226 /* Peek at the next token. (Changes in the token buffer may
3227 have invalidated the pointer obtained above.) */
3228 token = cp_lexer_peek_token (parser->lexer);
3230 switch (token->type)
3232 case CPP_NAME:
3233 return cp_parser_identifier (parser);
3235 case CPP_KEYWORD:
3236 if (token->keyword == RID_OPERATOR)
3237 return cp_parser_operator_function_id (parser);
3238 /* Fall through. */
3240 default:
3241 cp_parser_error (parser, "expected id-expression");
3242 return error_mark_node;
3245 else
3246 return cp_parser_unqualified_id (parser, template_keyword_p,
3247 /*check_dependency_p=*/true,
3248 declarator_p);
3251 /* Parse an unqualified-id.
3253 unqualified-id:
3254 identifier
3255 operator-function-id
3256 conversion-function-id
3257 ~ class-name
3258 template-id
3260 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3261 keyword, in a construct like `A::template ...'.
3263 Returns a representation of unqualified-id. For the `identifier'
3264 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3265 production a BIT_NOT_EXPR is returned; the operand of the
3266 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3267 other productions, see the documentation accompanying the
3268 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3269 names are looked up in uninstantiated templates. If DECLARATOR_P
3270 is true, the unqualified-id is appearing as part of a declarator,
3271 rather than as part of an expression. */
3273 static tree
3274 cp_parser_unqualified_id (cp_parser* parser,
3275 bool template_keyword_p,
3276 bool check_dependency_p,
3277 bool declarator_p)
3279 cp_token *token;
3281 /* Peek at the next token. */
3282 token = cp_lexer_peek_token (parser->lexer);
3284 switch (token->type)
3286 case CPP_NAME:
3288 tree id;
3290 /* We don't know yet whether or not this will be a
3291 template-id. */
3292 cp_parser_parse_tentatively (parser);
3293 /* Try a template-id. */
3294 id = cp_parser_template_id (parser, template_keyword_p,
3295 check_dependency_p,
3296 declarator_p);
3297 /* If it worked, we're done. */
3298 if (cp_parser_parse_definitely (parser))
3299 return id;
3300 /* Otherwise, it's an ordinary identifier. */
3301 return cp_parser_identifier (parser);
3304 case CPP_TEMPLATE_ID:
3305 return cp_parser_template_id (parser, template_keyword_p,
3306 check_dependency_p,
3307 declarator_p);
3309 case CPP_COMPL:
3311 tree type_decl;
3312 tree qualifying_scope;
3313 tree object_scope;
3314 tree scope;
3315 bool done;
3317 /* Consume the `~' token. */
3318 cp_lexer_consume_token (parser->lexer);
3319 /* Parse the class-name. The standard, as written, seems to
3320 say that:
3322 template <typename T> struct S { ~S (); };
3323 template <typename T> S<T>::~S() {}
3325 is invalid, since `~' must be followed by a class-name, but
3326 `S<T>' is dependent, and so not known to be a class.
3327 That's not right; we need to look in uninstantiated
3328 templates. A further complication arises from:
3330 template <typename T> void f(T t) {
3331 t.T::~T();
3334 Here, it is not possible to look up `T' in the scope of `T'
3335 itself. We must look in both the current scope, and the
3336 scope of the containing complete expression.
3338 Yet another issue is:
3340 struct S {
3341 int S;
3342 ~S();
3345 S::~S() {}
3347 The standard does not seem to say that the `S' in `~S'
3348 should refer to the type `S' and not the data member
3349 `S::S'. */
3351 /* DR 244 says that we look up the name after the "~" in the
3352 same scope as we looked up the qualifying name. That idea
3353 isn't fully worked out; it's more complicated than that. */
3354 scope = parser->scope;
3355 object_scope = parser->object_scope;
3356 qualifying_scope = parser->qualifying_scope;
3358 /* If the name is of the form "X::~X" it's OK. */
3359 if (scope && TYPE_P (scope)
3360 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3361 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3362 == CPP_OPEN_PAREN)
3363 && (cp_lexer_peek_token (parser->lexer)->value
3364 == TYPE_IDENTIFIER (scope)))
3366 cp_lexer_consume_token (parser->lexer);
3367 return build_nt (BIT_NOT_EXPR, scope);
3370 /* If there was an explicit qualification (S::~T), first look
3371 in the scope given by the qualification (i.e., S). */
3372 done = false;
3373 type_decl = NULL_TREE;
3374 if (scope)
3376 cp_parser_parse_tentatively (parser);
3377 type_decl = cp_parser_class_name (parser,
3378 /*typename_keyword_p=*/false,
3379 /*template_keyword_p=*/false,
3380 none_type,
3381 /*check_dependency=*/false,
3382 /*class_head_p=*/false,
3383 declarator_p);
3384 if (cp_parser_parse_definitely (parser))
3385 done = true;
3387 /* In "N::S::~S", look in "N" as well. */
3388 if (!done && scope && qualifying_scope)
3390 cp_parser_parse_tentatively (parser);
3391 parser->scope = qualifying_scope;
3392 parser->object_scope = NULL_TREE;
3393 parser->qualifying_scope = NULL_TREE;
3394 type_decl
3395 = cp_parser_class_name (parser,
3396 /*typename_keyword_p=*/false,
3397 /*template_keyword_p=*/false,
3398 none_type,
3399 /*check_dependency=*/false,
3400 /*class_head_p=*/false,
3401 declarator_p);
3402 if (cp_parser_parse_definitely (parser))
3403 done = true;
3405 /* In "p->S::~T", look in the scope given by "*p" as well. */
3406 else if (!done && object_scope)
3408 cp_parser_parse_tentatively (parser);
3409 parser->scope = object_scope;
3410 parser->object_scope = NULL_TREE;
3411 parser->qualifying_scope = NULL_TREE;
3412 type_decl
3413 = cp_parser_class_name (parser,
3414 /*typename_keyword_p=*/false,
3415 /*template_keyword_p=*/false,
3416 none_type,
3417 /*check_dependency=*/false,
3418 /*class_head_p=*/false,
3419 declarator_p);
3420 if (cp_parser_parse_definitely (parser))
3421 done = true;
3423 /* Look in the surrounding context. */
3424 if (!done)
3426 parser->scope = NULL_TREE;
3427 parser->object_scope = NULL_TREE;
3428 parser->qualifying_scope = NULL_TREE;
3429 type_decl
3430 = cp_parser_class_name (parser,
3431 /*typename_keyword_p=*/false,
3432 /*template_keyword_p=*/false,
3433 none_type,
3434 /*check_dependency=*/false,
3435 /*class_head_p=*/false,
3436 declarator_p);
3438 /* If an error occurred, assume that the name of the
3439 destructor is the same as the name of the qualifying
3440 class. That allows us to keep parsing after running
3441 into ill-formed destructor names. */
3442 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3443 return build_nt (BIT_NOT_EXPR, scope);
3444 else if (type_decl == error_mark_node)
3445 return error_mark_node;
3447 /* [class.dtor]
3449 A typedef-name that names a class shall not be used as the
3450 identifier in the declarator for a destructor declaration. */
3451 if (declarator_p
3452 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3453 && !DECL_SELF_REFERENCE_P (type_decl)
3454 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3455 error ("typedef-name %qD used as destructor declarator",
3456 type_decl);
3458 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3461 case CPP_KEYWORD:
3462 if (token->keyword == RID_OPERATOR)
3464 tree id;
3466 /* This could be a template-id, so we try that first. */
3467 cp_parser_parse_tentatively (parser);
3468 /* Try a template-id. */
3469 id = cp_parser_template_id (parser, template_keyword_p,
3470 /*check_dependency_p=*/true,
3471 declarator_p);
3472 /* If that worked, we're done. */
3473 if (cp_parser_parse_definitely (parser))
3474 return id;
3475 /* We still don't know whether we're looking at an
3476 operator-function-id or a conversion-function-id. */
3477 cp_parser_parse_tentatively (parser);
3478 /* Try an operator-function-id. */
3479 id = cp_parser_operator_function_id (parser);
3480 /* If that didn't work, try a conversion-function-id. */
3481 if (!cp_parser_parse_definitely (parser))
3482 id = cp_parser_conversion_function_id (parser);
3484 return id;
3486 /* Fall through. */
3488 default:
3489 cp_parser_error (parser, "expected unqualified-id");
3490 return error_mark_node;
3494 /* Parse an (optional) nested-name-specifier.
3496 nested-name-specifier:
3497 class-or-namespace-name :: nested-name-specifier [opt]
3498 class-or-namespace-name :: template nested-name-specifier [opt]
3500 PARSER->SCOPE should be set appropriately before this function is
3501 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3502 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3503 in name lookups.
3505 Sets PARSER->SCOPE to the class (TYPE) or namespace
3506 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3507 it unchanged if there is no nested-name-specifier. Returns the new
3508 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3510 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3511 part of a declaration and/or decl-specifier. */
3513 static tree
3514 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3515 bool typename_keyword_p,
3516 bool check_dependency_p,
3517 bool type_p,
3518 bool is_declaration)
3520 bool success = false;
3521 cp_token_position start = 0;
3522 cp_token *token;
3524 /* If the next token corresponds to a nested name specifier, there
3525 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3526 false, it may have been true before, in which case something
3527 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3528 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3529 CHECK_DEPENDENCY_P is false, we have to fall through into the
3530 main loop. */
3531 if (check_dependency_p
3532 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3534 cp_parser_pre_parsed_nested_name_specifier (parser);
3535 return parser->scope;
3538 /* Remember where the nested-name-specifier starts. */
3539 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3541 start = cp_lexer_token_position (parser->lexer, false);
3542 push_deferring_access_checks (dk_deferred);
3545 while (true)
3547 tree new_scope;
3548 tree old_scope;
3549 tree saved_qualifying_scope;
3550 bool template_keyword_p;
3552 /* Spot cases that cannot be the beginning of a
3553 nested-name-specifier. */
3554 token = cp_lexer_peek_token (parser->lexer);
3556 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3557 the already parsed nested-name-specifier. */
3558 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3560 /* Grab the nested-name-specifier and continue the loop. */
3561 cp_parser_pre_parsed_nested_name_specifier (parser);
3562 success = true;
3563 continue;
3566 /* Spot cases that cannot be the beginning of a
3567 nested-name-specifier. On the second and subsequent times
3568 through the loop, we look for the `template' keyword. */
3569 if (success && token->keyword == RID_TEMPLATE)
3571 /* A template-id can start a nested-name-specifier. */
3572 else if (token->type == CPP_TEMPLATE_ID)
3574 else
3576 /* If the next token is not an identifier, then it is
3577 definitely not a class-or-namespace-name. */
3578 if (token->type != CPP_NAME)
3579 break;
3580 /* If the following token is neither a `<' (to begin a
3581 template-id), nor a `::', then we are not looking at a
3582 nested-name-specifier. */
3583 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3584 if (token->type != CPP_SCOPE
3585 && !cp_parser_nth_token_starts_template_argument_list_p
3586 (parser, 2))
3587 break;
3590 /* The nested-name-specifier is optional, so we parse
3591 tentatively. */
3592 cp_parser_parse_tentatively (parser);
3594 /* Look for the optional `template' keyword, if this isn't the
3595 first time through the loop. */
3596 if (success)
3597 template_keyword_p = cp_parser_optional_template_keyword (parser);
3598 else
3599 template_keyword_p = false;
3601 /* Save the old scope since the name lookup we are about to do
3602 might destroy it. */
3603 old_scope = parser->scope;
3604 saved_qualifying_scope = parser->qualifying_scope;
3605 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3606 look up names in "X<T>::I" in order to determine that "Y" is
3607 a template. So, if we have a typename at this point, we make
3608 an effort to look through it. */
3609 if (is_declaration
3610 && !typename_keyword_p
3611 && parser->scope
3612 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3613 parser->scope = resolve_typename_type (parser->scope,
3614 /*only_current_p=*/false);
3615 /* Parse the qualifying entity. */
3616 new_scope
3617 = cp_parser_class_or_namespace_name (parser,
3618 typename_keyword_p,
3619 template_keyword_p,
3620 check_dependency_p,
3621 type_p,
3622 is_declaration);
3623 /* Look for the `::' token. */
3624 cp_parser_require (parser, CPP_SCOPE, "`::'");
3626 /* If we found what we wanted, we keep going; otherwise, we're
3627 done. */
3628 if (!cp_parser_parse_definitely (parser))
3630 bool error_p = false;
3632 /* Restore the OLD_SCOPE since it was valid before the
3633 failed attempt at finding the last
3634 class-or-namespace-name. */
3635 parser->scope = old_scope;
3636 parser->qualifying_scope = saved_qualifying_scope;
3637 /* If the next token is an identifier, and the one after
3638 that is a `::', then any valid interpretation would have
3639 found a class-or-namespace-name. */
3640 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3641 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3642 == CPP_SCOPE)
3643 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3644 != CPP_COMPL))
3646 token = cp_lexer_consume_token (parser->lexer);
3647 if (!error_p)
3649 if (!token->ambiguous_p)
3651 tree decl;
3652 tree ambiguous_decls;
3654 decl = cp_parser_lookup_name (parser, token->value,
3655 none_type,
3656 /*is_template=*/false,
3657 /*is_namespace=*/false,
3658 /*check_dependency=*/true,
3659 &ambiguous_decls);
3660 if (TREE_CODE (decl) == TEMPLATE_DECL)
3661 error ("%qD used without template parameters", decl);
3662 else if (ambiguous_decls)
3664 error ("reference to %qD is ambiguous",
3665 token->value);
3666 print_candidates (ambiguous_decls);
3667 decl = error_mark_node;
3669 else
3670 cp_parser_name_lookup_error
3671 (parser, token->value, decl,
3672 "is not a class or namespace");
3674 parser->scope = error_mark_node;
3675 error_p = true;
3676 /* Treat this as a successful nested-name-specifier
3677 due to:
3679 [basic.lookup.qual]
3681 If the name found is not a class-name (clause
3682 _class_) or namespace-name (_namespace.def_), the
3683 program is ill-formed. */
3684 success = true;
3686 cp_lexer_consume_token (parser->lexer);
3688 break;
3690 /* We've found one valid nested-name-specifier. */
3691 success = true;
3692 /* Name lookup always gives us a DECL. */
3693 if (TREE_CODE (new_scope) == TYPE_DECL)
3694 new_scope = TREE_TYPE (new_scope);
3695 /* Uses of "template" must be followed by actual templates. */
3696 if (template_keyword_p
3697 && !(CLASS_TYPE_P (new_scope)
3698 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3699 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3700 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3701 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3702 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3703 == TEMPLATE_ID_EXPR)))
3704 pedwarn (TYPE_P (new_scope)
3705 ? "%qT is not a template"
3706 : "%qD is not a template",
3707 new_scope);
3708 /* If it is a class scope, try to complete it; we are about to
3709 be looking up names inside the class. */
3710 if (TYPE_P (new_scope)
3711 /* Since checking types for dependency can be expensive,
3712 avoid doing it if the type is already complete. */
3713 && !COMPLETE_TYPE_P (new_scope)
3714 /* Do not try to complete dependent types. */
3715 && !dependent_type_p (new_scope))
3716 new_scope = complete_type (new_scope);
3717 /* Make sure we look in the right scope the next time through
3718 the loop. */
3719 parser->scope = new_scope;
3722 /* If parsing tentatively, replace the sequence of tokens that makes
3723 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3724 token. That way, should we re-parse the token stream, we will
3725 not have to repeat the effort required to do the parse, nor will
3726 we issue duplicate error messages. */
3727 if (success && start)
3729 cp_token *token;
3730 tree access_checks;
3732 token = cp_lexer_token_at (parser->lexer, start);
3733 /* Reset the contents of the START token. */
3734 token->type = CPP_NESTED_NAME_SPECIFIER;
3735 /* Retrieve any deferred checks. Do not pop this access checks yet
3736 so the memory will not be reclaimed during token replacing below. */
3737 access_checks = get_deferred_access_checks ();
3738 token->value = build_tree_list (copy_list (access_checks),
3739 parser->scope);
3740 TREE_TYPE (token->value) = parser->qualifying_scope;
3741 token->keyword = RID_MAX;
3743 /* Purge all subsequent tokens. */
3744 cp_lexer_purge_tokens_after (parser->lexer, start);
3747 if (start)
3748 pop_to_parent_deferring_access_checks ();
3750 return success ? parser->scope : NULL_TREE;
3753 /* Parse a nested-name-specifier. See
3754 cp_parser_nested_name_specifier_opt for details. This function
3755 behaves identically, except that it will an issue an error if no
3756 nested-name-specifier is present. */
3758 static tree
3759 cp_parser_nested_name_specifier (cp_parser *parser,
3760 bool typename_keyword_p,
3761 bool check_dependency_p,
3762 bool type_p,
3763 bool is_declaration)
3765 tree scope;
3767 /* Look for the nested-name-specifier. */
3768 scope = cp_parser_nested_name_specifier_opt (parser,
3769 typename_keyword_p,
3770 check_dependency_p,
3771 type_p,
3772 is_declaration);
3773 /* If it was not present, issue an error message. */
3774 if (!scope)
3776 cp_parser_error (parser, "expected nested-name-specifier");
3777 parser->scope = NULL_TREE;
3780 return scope;
3783 /* Parse a class-or-namespace-name.
3785 class-or-namespace-name:
3786 class-name
3787 namespace-name
3789 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3790 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3791 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3792 TYPE_P is TRUE iff the next name should be taken as a class-name,
3793 even the same name is declared to be another entity in the same
3794 scope.
3796 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3797 specified by the class-or-namespace-name. If neither is found the
3798 ERROR_MARK_NODE is returned. */
3800 static tree
3801 cp_parser_class_or_namespace_name (cp_parser *parser,
3802 bool typename_keyword_p,
3803 bool template_keyword_p,
3804 bool check_dependency_p,
3805 bool type_p,
3806 bool is_declaration)
3808 tree saved_scope;
3809 tree saved_qualifying_scope;
3810 tree saved_object_scope;
3811 tree scope;
3812 bool only_class_p;
3814 /* Before we try to parse the class-name, we must save away the
3815 current PARSER->SCOPE since cp_parser_class_name will destroy
3816 it. */
3817 saved_scope = parser->scope;
3818 saved_qualifying_scope = parser->qualifying_scope;
3819 saved_object_scope = parser->object_scope;
3820 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3821 there is no need to look for a namespace-name. */
3822 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3823 if (!only_class_p)
3824 cp_parser_parse_tentatively (parser);
3825 scope = cp_parser_class_name (parser,
3826 typename_keyword_p,
3827 template_keyword_p,
3828 type_p ? class_type : none_type,
3829 check_dependency_p,
3830 /*class_head_p=*/false,
3831 is_declaration);
3832 /* If that didn't work, try for a namespace-name. */
3833 if (!only_class_p && !cp_parser_parse_definitely (parser))
3835 /* Restore the saved scope. */
3836 parser->scope = saved_scope;
3837 parser->qualifying_scope = saved_qualifying_scope;
3838 parser->object_scope = saved_object_scope;
3839 /* If we are not looking at an identifier followed by the scope
3840 resolution operator, then this is not part of a
3841 nested-name-specifier. (Note that this function is only used
3842 to parse the components of a nested-name-specifier.) */
3843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3844 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3845 return error_mark_node;
3846 scope = cp_parser_namespace_name (parser);
3849 return scope;
3852 /* Parse a postfix-expression.
3854 postfix-expression:
3855 primary-expression
3856 postfix-expression [ expression ]
3857 postfix-expression ( expression-list [opt] )
3858 simple-type-specifier ( expression-list [opt] )
3859 typename :: [opt] nested-name-specifier identifier
3860 ( expression-list [opt] )
3861 typename :: [opt] nested-name-specifier template [opt] template-id
3862 ( expression-list [opt] )
3863 postfix-expression . template [opt] id-expression
3864 postfix-expression -> template [opt] id-expression
3865 postfix-expression . pseudo-destructor-name
3866 postfix-expression -> pseudo-destructor-name
3867 postfix-expression ++
3868 postfix-expression --
3869 dynamic_cast < type-id > ( expression )
3870 static_cast < type-id > ( expression )
3871 reinterpret_cast < type-id > ( expression )
3872 const_cast < type-id > ( expression )
3873 typeid ( expression )
3874 typeid ( type-id )
3876 GNU Extension:
3878 postfix-expression:
3879 ( type-id ) { initializer-list , [opt] }
3881 This extension is a GNU version of the C99 compound-literal
3882 construct. (The C99 grammar uses `type-name' instead of `type-id',
3883 but they are essentially the same concept.)
3885 If ADDRESS_P is true, the postfix expression is the operand of the
3886 `&' operator. CAST_P is true if this expression is the target of a
3887 cast.
3889 Returns a representation of the expression. */
3891 static tree
3892 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3894 cp_token *token;
3895 enum rid keyword;
3896 cp_id_kind idk = CP_ID_KIND_NONE;
3897 tree postfix_expression = NULL_TREE;
3899 /* Peek at the next token. */
3900 token = cp_lexer_peek_token (parser->lexer);
3901 /* Some of the productions are determined by keywords. */
3902 keyword = token->keyword;
3903 switch (keyword)
3905 case RID_DYNCAST:
3906 case RID_STATCAST:
3907 case RID_REINTCAST:
3908 case RID_CONSTCAST:
3910 tree type;
3911 tree expression;
3912 const char *saved_message;
3914 /* All of these can be handled in the same way from the point
3915 of view of parsing. Begin by consuming the token
3916 identifying the cast. */
3917 cp_lexer_consume_token (parser->lexer);
3919 /* New types cannot be defined in the cast. */
3920 saved_message = parser->type_definition_forbidden_message;
3921 parser->type_definition_forbidden_message
3922 = "types may not be defined in casts";
3924 /* Look for the opening `<'. */
3925 cp_parser_require (parser, CPP_LESS, "`<'");
3926 /* Parse the type to which we are casting. */
3927 type = cp_parser_type_id (parser);
3928 /* Look for the closing `>'. */
3929 cp_parser_require (parser, CPP_GREATER, "`>'");
3930 /* Restore the old message. */
3931 parser->type_definition_forbidden_message = saved_message;
3933 /* And the expression which is being cast. */
3934 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3935 expression = cp_parser_expression (parser, /*cast_p=*/true);
3936 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3938 /* Only type conversions to integral or enumeration types
3939 can be used in constant-expressions. */
3940 if (parser->integral_constant_expression_p
3941 && !dependent_type_p (type)
3942 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3943 && (cp_parser_non_integral_constant_expression
3944 (parser,
3945 "a cast to a type other than an integral or "
3946 "enumeration type")))
3947 return error_mark_node;
3949 switch (keyword)
3951 case RID_DYNCAST:
3952 postfix_expression
3953 = build_dynamic_cast (type, expression);
3954 break;
3955 case RID_STATCAST:
3956 postfix_expression
3957 = build_static_cast (type, expression);
3958 break;
3959 case RID_REINTCAST:
3960 postfix_expression
3961 = build_reinterpret_cast (type, expression);
3962 break;
3963 case RID_CONSTCAST:
3964 postfix_expression
3965 = build_const_cast (type, expression);
3966 break;
3967 default:
3968 gcc_unreachable ();
3971 break;
3973 case RID_TYPEID:
3975 tree type;
3976 const char *saved_message;
3977 bool saved_in_type_id_in_expr_p;
3979 /* Consume the `typeid' token. */
3980 cp_lexer_consume_token (parser->lexer);
3981 /* Look for the `(' token. */
3982 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3983 /* Types cannot be defined in a `typeid' expression. */
3984 saved_message = parser->type_definition_forbidden_message;
3985 parser->type_definition_forbidden_message
3986 = "types may not be defined in a `typeid\' expression";
3987 /* We can't be sure yet whether we're looking at a type-id or an
3988 expression. */
3989 cp_parser_parse_tentatively (parser);
3990 /* Try a type-id first. */
3991 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3992 parser->in_type_id_in_expr_p = true;
3993 type = cp_parser_type_id (parser);
3994 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3995 /* Look for the `)' token. Otherwise, we can't be sure that
3996 we're not looking at an expression: consider `typeid (int
3997 (3))', for example. */
3998 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3999 /* If all went well, simply lookup the type-id. */
4000 if (cp_parser_parse_definitely (parser))
4001 postfix_expression = get_typeid (type);
4002 /* Otherwise, fall back to the expression variant. */
4003 else
4005 tree expression;
4007 /* Look for an expression. */
4008 expression = cp_parser_expression (parser, /*cast_p=*/false);
4009 /* Compute its typeid. */
4010 postfix_expression = build_typeid (expression);
4011 /* Look for the `)' token. */
4012 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4014 /* `typeid' may not appear in an integral constant expression. */
4015 if (cp_parser_non_integral_constant_expression(parser,
4016 "`typeid' operator"))
4017 return error_mark_node;
4018 /* Restore the saved message. */
4019 parser->type_definition_forbidden_message = saved_message;
4021 break;
4023 case RID_TYPENAME:
4025 tree type;
4026 /* The syntax permitted here is the same permitted for an
4027 elaborated-type-specifier. */
4028 type = cp_parser_elaborated_type_specifier (parser,
4029 /*is_friend=*/false,
4030 /*is_declaration=*/false);
4031 postfix_expression = cp_parser_functional_cast (parser, type);
4033 break;
4035 default:
4037 tree type;
4039 /* If the next thing is a simple-type-specifier, we may be
4040 looking at a functional cast. We could also be looking at
4041 an id-expression. So, we try the functional cast, and if
4042 that doesn't work we fall back to the primary-expression. */
4043 cp_parser_parse_tentatively (parser);
4044 /* Look for the simple-type-specifier. */
4045 type = cp_parser_simple_type_specifier (parser,
4046 /*decl_specs=*/NULL,
4047 CP_PARSER_FLAGS_NONE);
4048 /* Parse the cast itself. */
4049 if (!cp_parser_error_occurred (parser))
4050 postfix_expression
4051 = cp_parser_functional_cast (parser, type);
4052 /* If that worked, we're done. */
4053 if (cp_parser_parse_definitely (parser))
4054 break;
4056 /* If the functional-cast didn't work out, try a
4057 compound-literal. */
4058 if (cp_parser_allow_gnu_extensions_p (parser)
4059 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4061 VEC(constructor_elt,gc) *initializer_list = NULL;
4062 bool saved_in_type_id_in_expr_p;
4064 cp_parser_parse_tentatively (parser);
4065 /* Consume the `('. */
4066 cp_lexer_consume_token (parser->lexer);
4067 /* Parse the type. */
4068 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4069 parser->in_type_id_in_expr_p = true;
4070 type = cp_parser_type_id (parser);
4071 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4072 /* Look for the `)'. */
4073 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4074 /* Look for the `{'. */
4075 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4076 /* If things aren't going well, there's no need to
4077 keep going. */
4078 if (!cp_parser_error_occurred (parser))
4080 bool non_constant_p;
4081 /* Parse the initializer-list. */
4082 initializer_list
4083 = cp_parser_initializer_list (parser, &non_constant_p);
4084 /* Allow a trailing `,'. */
4085 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4086 cp_lexer_consume_token (parser->lexer);
4087 /* Look for the final `}'. */
4088 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4090 /* If that worked, we're definitely looking at a
4091 compound-literal expression. */
4092 if (cp_parser_parse_definitely (parser))
4094 /* Warn the user that a compound literal is not
4095 allowed in standard C++. */
4096 if (pedantic)
4097 pedwarn ("ISO C++ forbids compound-literals");
4098 /* Form the representation of the compound-literal. */
4099 postfix_expression
4100 = finish_compound_literal (type, initializer_list);
4101 break;
4105 /* It must be a primary-expression. */
4106 postfix_expression
4107 = cp_parser_primary_expression (parser, address_p, cast_p,
4108 /*template_arg_p=*/false,
4109 &idk);
4111 break;
4114 /* Keep looping until the postfix-expression is complete. */
4115 while (true)
4117 if (idk == CP_ID_KIND_UNQUALIFIED
4118 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4119 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4120 /* It is not a Koenig lookup function call. */
4121 postfix_expression
4122 = unqualified_name_lookup_error (postfix_expression);
4124 /* Peek at the next token. */
4125 token = cp_lexer_peek_token (parser->lexer);
4127 switch (token->type)
4129 case CPP_OPEN_SQUARE:
4130 postfix_expression
4131 = cp_parser_postfix_open_square_expression (parser,
4132 postfix_expression,
4133 false);
4134 idk = CP_ID_KIND_NONE;
4135 break;
4137 case CPP_OPEN_PAREN:
4138 /* postfix-expression ( expression-list [opt] ) */
4140 bool koenig_p;
4141 bool is_builtin_constant_p;
4142 bool saved_integral_constant_expression_p = false;
4143 bool saved_non_integral_constant_expression_p = false;
4144 tree args;
4146 is_builtin_constant_p
4147 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4148 if (is_builtin_constant_p)
4150 /* The whole point of __builtin_constant_p is to allow
4151 non-constant expressions to appear as arguments. */
4152 saved_integral_constant_expression_p
4153 = parser->integral_constant_expression_p;
4154 saved_non_integral_constant_expression_p
4155 = parser->non_integral_constant_expression_p;
4156 parser->integral_constant_expression_p = false;
4158 args = (cp_parser_parenthesized_expression_list
4159 (parser, /*is_attribute_list=*/false,
4160 /*cast_p=*/false,
4161 /*non_constant_p=*/NULL));
4162 if (is_builtin_constant_p)
4164 parser->integral_constant_expression_p
4165 = saved_integral_constant_expression_p;
4166 parser->non_integral_constant_expression_p
4167 = saved_non_integral_constant_expression_p;
4170 if (args == error_mark_node)
4172 postfix_expression = error_mark_node;
4173 break;
4176 /* Function calls are not permitted in
4177 constant-expressions. */
4178 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4179 && cp_parser_non_integral_constant_expression (parser,
4180 "a function call"))
4182 postfix_expression = error_mark_node;
4183 break;
4186 koenig_p = false;
4187 if (idk == CP_ID_KIND_UNQUALIFIED)
4189 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4191 if (args)
4193 koenig_p = true;
4194 postfix_expression
4195 = perform_koenig_lookup (postfix_expression, args);
4197 else
4198 postfix_expression
4199 = unqualified_fn_lookup_error (postfix_expression);
4201 /* We do not perform argument-dependent lookup if
4202 normal lookup finds a non-function, in accordance
4203 with the expected resolution of DR 218. */
4204 else if (args && is_overloaded_fn (postfix_expression))
4206 tree fn = get_first_fn (postfix_expression);
4208 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4209 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4211 /* Only do argument dependent lookup if regular
4212 lookup does not find a set of member functions.
4213 [basic.lookup.koenig]/2a */
4214 if (!DECL_FUNCTION_MEMBER_P (fn))
4216 koenig_p = true;
4217 postfix_expression
4218 = perform_koenig_lookup (postfix_expression, args);
4223 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4225 tree instance = TREE_OPERAND (postfix_expression, 0);
4226 tree fn = TREE_OPERAND (postfix_expression, 1);
4228 if (processing_template_decl
4229 && (type_dependent_expression_p (instance)
4230 || (!BASELINK_P (fn)
4231 && TREE_CODE (fn) != FIELD_DECL)
4232 || type_dependent_expression_p (fn)
4233 || any_type_dependent_arguments_p (args)))
4235 postfix_expression
4236 = build_min_nt (CALL_EXPR, postfix_expression,
4237 args, NULL_TREE);
4238 break;
4241 if (BASELINK_P (fn))
4242 postfix_expression
4243 = (build_new_method_call
4244 (instance, fn, args, NULL_TREE,
4245 (idk == CP_ID_KIND_QUALIFIED
4246 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4247 else
4248 postfix_expression
4249 = finish_call_expr (postfix_expression, args,
4250 /*disallow_virtual=*/false,
4251 /*koenig_p=*/false);
4253 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4254 || TREE_CODE (postfix_expression) == MEMBER_REF
4255 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4256 postfix_expression = (build_offset_ref_call_from_tree
4257 (postfix_expression, args));
4258 else if (idk == CP_ID_KIND_QUALIFIED)
4259 /* A call to a static class member, or a namespace-scope
4260 function. */
4261 postfix_expression
4262 = finish_call_expr (postfix_expression, args,
4263 /*disallow_virtual=*/true,
4264 koenig_p);
4265 else
4266 /* All other function calls. */
4267 postfix_expression
4268 = finish_call_expr (postfix_expression, args,
4269 /*disallow_virtual=*/false,
4270 koenig_p);
4272 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4273 idk = CP_ID_KIND_NONE;
4275 break;
4277 case CPP_DOT:
4278 case CPP_DEREF:
4279 /* postfix-expression . template [opt] id-expression
4280 postfix-expression . pseudo-destructor-name
4281 postfix-expression -> template [opt] id-expression
4282 postfix-expression -> pseudo-destructor-name */
4284 /* Consume the `.' or `->' operator. */
4285 cp_lexer_consume_token (parser->lexer);
4287 postfix_expression
4288 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4289 postfix_expression,
4290 false, &idk);
4291 break;
4293 case CPP_PLUS_PLUS:
4294 /* postfix-expression ++ */
4295 /* Consume the `++' token. */
4296 cp_lexer_consume_token (parser->lexer);
4297 /* Generate a representation for the complete expression. */
4298 postfix_expression
4299 = finish_increment_expr (postfix_expression,
4300 POSTINCREMENT_EXPR);
4301 /* Increments may not appear in constant-expressions. */
4302 if (cp_parser_non_integral_constant_expression (parser,
4303 "an increment"))
4304 postfix_expression = error_mark_node;
4305 idk = CP_ID_KIND_NONE;
4306 break;
4308 case CPP_MINUS_MINUS:
4309 /* postfix-expression -- */
4310 /* Consume the `--' token. */
4311 cp_lexer_consume_token (parser->lexer);
4312 /* Generate a representation for the complete expression. */
4313 postfix_expression
4314 = finish_increment_expr (postfix_expression,
4315 POSTDECREMENT_EXPR);
4316 /* Decrements may not appear in constant-expressions. */
4317 if (cp_parser_non_integral_constant_expression (parser,
4318 "a decrement"))
4319 postfix_expression = error_mark_node;
4320 idk = CP_ID_KIND_NONE;
4321 break;
4323 default:
4324 return postfix_expression;
4328 /* We should never get here. */
4329 gcc_unreachable ();
4330 return error_mark_node;
4333 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4334 by cp_parser_builtin_offsetof. We're looking for
4336 postfix-expression [ expression ]
4338 FOR_OFFSETOF is set if we're being called in that context, which
4339 changes how we deal with integer constant expressions. */
4341 static tree
4342 cp_parser_postfix_open_square_expression (cp_parser *parser,
4343 tree postfix_expression,
4344 bool for_offsetof)
4346 tree index;
4348 /* Consume the `[' token. */
4349 cp_lexer_consume_token (parser->lexer);
4351 /* Parse the index expression. */
4352 /* ??? For offsetof, there is a question of what to allow here. If
4353 offsetof is not being used in an integral constant expression context,
4354 then we *could* get the right answer by computing the value at runtime.
4355 If we are in an integral constant expression context, then we might
4356 could accept any constant expression; hard to say without analysis.
4357 Rather than open the barn door too wide right away, allow only integer
4358 constant expressions here. */
4359 if (for_offsetof)
4360 index = cp_parser_constant_expression (parser, false, NULL);
4361 else
4362 index = cp_parser_expression (parser, /*cast_p=*/false);
4364 /* Look for the closing `]'. */
4365 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4367 /* Build the ARRAY_REF. */
4368 postfix_expression = grok_array_decl (postfix_expression, index);
4370 /* When not doing offsetof, array references are not permitted in
4371 constant-expressions. */
4372 if (!for_offsetof
4373 && (cp_parser_non_integral_constant_expression
4374 (parser, "an array reference")))
4375 postfix_expression = error_mark_node;
4377 return postfix_expression;
4380 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4381 by cp_parser_builtin_offsetof. We're looking for
4383 postfix-expression . template [opt] id-expression
4384 postfix-expression . pseudo-destructor-name
4385 postfix-expression -> template [opt] id-expression
4386 postfix-expression -> pseudo-destructor-name
4388 FOR_OFFSETOF is set if we're being called in that context. That sorta
4389 limits what of the above we'll actually accept, but nevermind.
4390 TOKEN_TYPE is the "." or "->" token, which will already have been
4391 removed from the stream. */
4393 static tree
4394 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4395 enum cpp_ttype token_type,
4396 tree postfix_expression,
4397 bool for_offsetof, cp_id_kind *idk)
4399 tree name;
4400 bool dependent_p;
4401 bool pseudo_destructor_p;
4402 tree scope = NULL_TREE;
4404 /* If this is a `->' operator, dereference the pointer. */
4405 if (token_type == CPP_DEREF)
4406 postfix_expression = build_x_arrow (postfix_expression);
4407 /* Check to see whether or not the expression is type-dependent. */
4408 dependent_p = type_dependent_expression_p (postfix_expression);
4409 /* The identifier following the `->' or `.' is not qualified. */
4410 parser->scope = NULL_TREE;
4411 parser->qualifying_scope = NULL_TREE;
4412 parser->object_scope = NULL_TREE;
4413 *idk = CP_ID_KIND_NONE;
4414 /* Enter the scope corresponding to the type of the object
4415 given by the POSTFIX_EXPRESSION. */
4416 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4418 scope = TREE_TYPE (postfix_expression);
4419 /* According to the standard, no expression should ever have
4420 reference type. Unfortunately, we do not currently match
4421 the standard in this respect in that our internal representation
4422 of an expression may have reference type even when the standard
4423 says it does not. Therefore, we have to manually obtain the
4424 underlying type here. */
4425 scope = non_reference (scope);
4426 /* The type of the POSTFIX_EXPRESSION must be complete. */
4427 if (scope == unknown_type_node)
4429 error ("%qE does not have class type", postfix_expression);
4430 scope = NULL_TREE;
4432 else
4433 scope = complete_type_or_else (scope, NULL_TREE);
4434 /* Let the name lookup machinery know that we are processing a
4435 class member access expression. */
4436 parser->context->object_type = scope;
4437 /* If something went wrong, we want to be able to discern that case,
4438 as opposed to the case where there was no SCOPE due to the type
4439 of expression being dependent. */
4440 if (!scope)
4441 scope = error_mark_node;
4442 /* If the SCOPE was erroneous, make the various semantic analysis
4443 functions exit quickly -- and without issuing additional error
4444 messages. */
4445 if (scope == error_mark_node)
4446 postfix_expression = error_mark_node;
4449 /* Assume this expression is not a pseudo-destructor access. */
4450 pseudo_destructor_p = false;
4452 /* If the SCOPE is a scalar type, then, if this is a valid program,
4453 we must be looking at a pseudo-destructor-name. */
4454 if (scope && SCALAR_TYPE_P (scope))
4456 tree s;
4457 tree type;
4459 cp_parser_parse_tentatively (parser);
4460 /* Parse the pseudo-destructor-name. */
4461 s = NULL_TREE;
4462 cp_parser_pseudo_destructor_name (parser, &s, &type);
4463 if (cp_parser_parse_definitely (parser))
4465 pseudo_destructor_p = true;
4466 postfix_expression
4467 = finish_pseudo_destructor_expr (postfix_expression,
4468 s, TREE_TYPE (type));
4472 if (!pseudo_destructor_p)
4474 /* If the SCOPE is not a scalar type, we are looking at an
4475 ordinary class member access expression, rather than a
4476 pseudo-destructor-name. */
4477 bool template_p;
4478 /* Parse the id-expression. */
4479 name = (cp_parser_id_expression
4480 (parser,
4481 cp_parser_optional_template_keyword (parser),
4482 /*check_dependency_p=*/true,
4483 &template_p,
4484 /*declarator_p=*/false));
4485 /* In general, build a SCOPE_REF if the member name is qualified.
4486 However, if the name was not dependent and has already been
4487 resolved; there is no need to build the SCOPE_REF. For example;
4489 struct X { void f(); };
4490 template <typename T> void f(T* t) { t->X::f(); }
4492 Even though "t" is dependent, "X::f" is not and has been resolved
4493 to a BASELINK; there is no need to include scope information. */
4495 /* But we do need to remember that there was an explicit scope for
4496 virtual function calls. */
4497 if (parser->scope)
4498 *idk = CP_ID_KIND_QUALIFIED;
4500 /* If the name is a template-id that names a type, we will get a
4501 TYPE_DECL here. That is invalid code. */
4502 if (TREE_CODE (name) == TYPE_DECL)
4504 error ("invalid use of %qD", name);
4505 postfix_expression = error_mark_node;
4507 else
4509 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4511 name = build_qualified_name (/*type=*/NULL_TREE,
4512 parser->scope,
4513 name,
4514 template_p);
4515 parser->scope = NULL_TREE;
4516 parser->qualifying_scope = NULL_TREE;
4517 parser->object_scope = NULL_TREE;
4519 if (scope && name && BASELINK_P (name))
4520 adjust_result_of_qualified_name_lookup
4521 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4522 postfix_expression
4523 = finish_class_member_access_expr (postfix_expression, name,
4524 template_p);
4528 /* We no longer need to look up names in the scope of the object on
4529 the left-hand side of the `.' or `->' operator. */
4530 parser->context->object_type = NULL_TREE;
4532 /* Outside of offsetof, these operators may not appear in
4533 constant-expressions. */
4534 if (!for_offsetof
4535 && (cp_parser_non_integral_constant_expression
4536 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4537 postfix_expression = error_mark_node;
4539 return postfix_expression;
4542 /* Parse a parenthesized expression-list.
4544 expression-list:
4545 assignment-expression
4546 expression-list, assignment-expression
4548 attribute-list:
4549 expression-list
4550 identifier
4551 identifier, expression-list
4553 CAST_P is true if this expression is the target of a cast.
4555 Returns a TREE_LIST. The TREE_VALUE of each node is a
4556 representation of an assignment-expression. Note that a TREE_LIST
4557 is returned even if there is only a single expression in the list.
4558 error_mark_node is returned if the ( and or ) are
4559 missing. NULL_TREE is returned on no expressions. The parentheses
4560 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4561 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4562 indicates whether or not all of the expressions in the list were
4563 constant. */
4565 static tree
4566 cp_parser_parenthesized_expression_list (cp_parser* parser,
4567 bool is_attribute_list,
4568 bool cast_p,
4569 bool *non_constant_p)
4571 tree expression_list = NULL_TREE;
4572 bool fold_expr_p = is_attribute_list;
4573 tree identifier = NULL_TREE;
4575 /* Assume all the expressions will be constant. */
4576 if (non_constant_p)
4577 *non_constant_p = false;
4579 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4580 return error_mark_node;
4582 /* Consume expressions until there are no more. */
4583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4584 while (true)
4586 tree expr;
4588 /* At the beginning of attribute lists, check to see if the
4589 next token is an identifier. */
4590 if (is_attribute_list
4591 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4593 cp_token *token;
4595 /* Consume the identifier. */
4596 token = cp_lexer_consume_token (parser->lexer);
4597 /* Save the identifier. */
4598 identifier = token->value;
4600 else
4602 /* Parse the next assignment-expression. */
4603 if (non_constant_p)
4605 bool expr_non_constant_p;
4606 expr = (cp_parser_constant_expression
4607 (parser, /*allow_non_constant_p=*/true,
4608 &expr_non_constant_p));
4609 if (expr_non_constant_p)
4610 *non_constant_p = true;
4612 else
4613 expr = cp_parser_assignment_expression (parser, cast_p);
4615 if (fold_expr_p)
4616 expr = fold_non_dependent_expr (expr);
4618 /* Add it to the list. We add error_mark_node
4619 expressions to the list, so that we can still tell if
4620 the correct form for a parenthesized expression-list
4621 is found. That gives better errors. */
4622 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4624 if (expr == error_mark_node)
4625 goto skip_comma;
4628 /* After the first item, attribute lists look the same as
4629 expression lists. */
4630 is_attribute_list = false;
4632 get_comma:;
4633 /* If the next token isn't a `,', then we are done. */
4634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4635 break;
4637 /* Otherwise, consume the `,' and keep going. */
4638 cp_lexer_consume_token (parser->lexer);
4641 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4643 int ending;
4645 skip_comma:;
4646 /* We try and resync to an unnested comma, as that will give the
4647 user better diagnostics. */
4648 ending = cp_parser_skip_to_closing_parenthesis (parser,
4649 /*recovering=*/true,
4650 /*or_comma=*/true,
4651 /*consume_paren=*/true);
4652 if (ending < 0)
4653 goto get_comma;
4654 if (!ending)
4655 return error_mark_node;
4658 /* We built up the list in reverse order so we must reverse it now. */
4659 expression_list = nreverse (expression_list);
4660 if (identifier)
4661 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4663 return expression_list;
4666 /* Parse a pseudo-destructor-name.
4668 pseudo-destructor-name:
4669 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4670 :: [opt] nested-name-specifier template template-id :: ~ type-name
4671 :: [opt] nested-name-specifier [opt] ~ type-name
4673 If either of the first two productions is used, sets *SCOPE to the
4674 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4675 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4676 or ERROR_MARK_NODE if the parse fails. */
4678 static void
4679 cp_parser_pseudo_destructor_name (cp_parser* parser,
4680 tree* scope,
4681 tree* type)
4683 bool nested_name_specifier_p;
4685 /* Assume that things will not work out. */
4686 *type = error_mark_node;
4688 /* Look for the optional `::' operator. */
4689 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4690 /* Look for the optional nested-name-specifier. */
4691 nested_name_specifier_p
4692 = (cp_parser_nested_name_specifier_opt (parser,
4693 /*typename_keyword_p=*/false,
4694 /*check_dependency_p=*/true,
4695 /*type_p=*/false,
4696 /*is_declaration=*/true)
4697 != NULL_TREE);
4698 /* Now, if we saw a nested-name-specifier, we might be doing the
4699 second production. */
4700 if (nested_name_specifier_p
4701 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4703 /* Consume the `template' keyword. */
4704 cp_lexer_consume_token (parser->lexer);
4705 /* Parse the template-id. */
4706 cp_parser_template_id (parser,
4707 /*template_keyword_p=*/true,
4708 /*check_dependency_p=*/false,
4709 /*is_declaration=*/true);
4710 /* Look for the `::' token. */
4711 cp_parser_require (parser, CPP_SCOPE, "`::'");
4713 /* If the next token is not a `~', then there might be some
4714 additional qualification. */
4715 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4717 /* Look for the type-name. */
4718 *scope = TREE_TYPE (cp_parser_type_name (parser));
4720 if (*scope == error_mark_node)
4721 return;
4723 /* If we don't have ::~, then something has gone wrong. Since
4724 the only caller of this function is looking for something
4725 after `.' or `->' after a scalar type, most likely the
4726 program is trying to get a member of a non-aggregate
4727 type. */
4728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4729 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4731 cp_parser_error (parser, "request for member of non-aggregate type");
4732 return;
4735 /* Look for the `::' token. */
4736 cp_parser_require (parser, CPP_SCOPE, "`::'");
4738 else
4739 *scope = NULL_TREE;
4741 /* Look for the `~'. */
4742 cp_parser_require (parser, CPP_COMPL, "`~'");
4743 /* Look for the type-name again. We are not responsible for
4744 checking that it matches the first type-name. */
4745 *type = cp_parser_type_name (parser);
4748 /* Parse a unary-expression.
4750 unary-expression:
4751 postfix-expression
4752 ++ cast-expression
4753 -- cast-expression
4754 unary-operator cast-expression
4755 sizeof unary-expression
4756 sizeof ( type-id )
4757 new-expression
4758 delete-expression
4760 GNU Extensions:
4762 unary-expression:
4763 __extension__ cast-expression
4764 __alignof__ unary-expression
4765 __alignof__ ( type-id )
4766 __real__ cast-expression
4767 __imag__ cast-expression
4768 && identifier
4770 ADDRESS_P is true iff the unary-expression is appearing as the
4771 operand of the `&' operator. CAST_P is true if this expression is
4772 the target of a cast.
4774 Returns a representation of the expression. */
4776 static tree
4777 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4779 cp_token *token;
4780 enum tree_code unary_operator;
4782 /* Peek at the next token. */
4783 token = cp_lexer_peek_token (parser->lexer);
4784 /* Some keywords give away the kind of expression. */
4785 if (token->type == CPP_KEYWORD)
4787 enum rid keyword = token->keyword;
4789 switch (keyword)
4791 case RID_ALIGNOF:
4792 case RID_SIZEOF:
4794 tree operand;
4795 enum tree_code op;
4797 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4798 /* Consume the token. */
4799 cp_lexer_consume_token (parser->lexer);
4800 /* Parse the operand. */
4801 operand = cp_parser_sizeof_operand (parser, keyword);
4803 if (TYPE_P (operand))
4804 return cxx_sizeof_or_alignof_type (operand, op, true);
4805 else
4806 return cxx_sizeof_or_alignof_expr (operand, op);
4809 case RID_NEW:
4810 return cp_parser_new_expression (parser);
4812 case RID_DELETE:
4813 return cp_parser_delete_expression (parser);
4815 case RID_EXTENSION:
4817 /* The saved value of the PEDANTIC flag. */
4818 int saved_pedantic;
4819 tree expr;
4821 /* Save away the PEDANTIC flag. */
4822 cp_parser_extension_opt (parser, &saved_pedantic);
4823 /* Parse the cast-expression. */
4824 expr = cp_parser_simple_cast_expression (parser);
4825 /* Restore the PEDANTIC flag. */
4826 pedantic = saved_pedantic;
4828 return expr;
4831 case RID_REALPART:
4832 case RID_IMAGPART:
4834 tree expression;
4836 /* Consume the `__real__' or `__imag__' token. */
4837 cp_lexer_consume_token (parser->lexer);
4838 /* Parse the cast-expression. */
4839 expression = cp_parser_simple_cast_expression (parser);
4840 /* Create the complete representation. */
4841 return build_x_unary_op ((keyword == RID_REALPART
4842 ? REALPART_EXPR : IMAGPART_EXPR),
4843 expression);
4845 break;
4847 default:
4848 break;
4852 /* Look for the `:: new' and `:: delete', which also signal the
4853 beginning of a new-expression, or delete-expression,
4854 respectively. If the next token is `::', then it might be one of
4855 these. */
4856 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4858 enum rid keyword;
4860 /* See if the token after the `::' is one of the keywords in
4861 which we're interested. */
4862 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4863 /* If it's `new', we have a new-expression. */
4864 if (keyword == RID_NEW)
4865 return cp_parser_new_expression (parser);
4866 /* Similarly, for `delete'. */
4867 else if (keyword == RID_DELETE)
4868 return cp_parser_delete_expression (parser);
4871 /* Look for a unary operator. */
4872 unary_operator = cp_parser_unary_operator (token);
4873 /* The `++' and `--' operators can be handled similarly, even though
4874 they are not technically unary-operators in the grammar. */
4875 if (unary_operator == ERROR_MARK)
4877 if (token->type == CPP_PLUS_PLUS)
4878 unary_operator = PREINCREMENT_EXPR;
4879 else if (token->type == CPP_MINUS_MINUS)
4880 unary_operator = PREDECREMENT_EXPR;
4881 /* Handle the GNU address-of-label extension. */
4882 else if (cp_parser_allow_gnu_extensions_p (parser)
4883 && token->type == CPP_AND_AND)
4885 tree identifier;
4887 /* Consume the '&&' token. */
4888 cp_lexer_consume_token (parser->lexer);
4889 /* Look for the identifier. */
4890 identifier = cp_parser_identifier (parser);
4891 /* Create an expression representing the address. */
4892 return finish_label_address_expr (identifier);
4895 if (unary_operator != ERROR_MARK)
4897 tree cast_expression;
4898 tree expression = error_mark_node;
4899 const char *non_constant_p = NULL;
4901 /* Consume the operator token. */
4902 token = cp_lexer_consume_token (parser->lexer);
4903 /* Parse the cast-expression. */
4904 cast_expression
4905 = cp_parser_cast_expression (parser,
4906 unary_operator == ADDR_EXPR,
4907 /*cast_p=*/false);
4908 /* Now, build an appropriate representation. */
4909 switch (unary_operator)
4911 case INDIRECT_REF:
4912 non_constant_p = "`*'";
4913 expression = build_x_indirect_ref (cast_expression, "unary *");
4914 break;
4916 case ADDR_EXPR:
4917 non_constant_p = "`&'";
4918 /* Fall through. */
4919 case BIT_NOT_EXPR:
4920 expression = build_x_unary_op (unary_operator, cast_expression);
4921 break;
4923 case PREINCREMENT_EXPR:
4924 case PREDECREMENT_EXPR:
4925 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4926 ? "`++'" : "`--'");
4927 /* Fall through. */
4928 case UNARY_PLUS_EXPR:
4929 case NEGATE_EXPR:
4930 case TRUTH_NOT_EXPR:
4931 expression = finish_unary_op_expr (unary_operator, cast_expression);
4932 break;
4934 default:
4935 gcc_unreachable ();
4938 if (non_constant_p
4939 && cp_parser_non_integral_constant_expression (parser,
4940 non_constant_p))
4941 expression = error_mark_node;
4943 return expression;
4946 return cp_parser_postfix_expression (parser, address_p, cast_p);
4949 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4950 unary-operator, the corresponding tree code is returned. */
4952 static enum tree_code
4953 cp_parser_unary_operator (cp_token* token)
4955 switch (token->type)
4957 case CPP_MULT:
4958 return INDIRECT_REF;
4960 case CPP_AND:
4961 return ADDR_EXPR;
4963 case CPP_PLUS:
4964 return UNARY_PLUS_EXPR;
4966 case CPP_MINUS:
4967 return NEGATE_EXPR;
4969 case CPP_NOT:
4970 return TRUTH_NOT_EXPR;
4972 case CPP_COMPL:
4973 return BIT_NOT_EXPR;
4975 default:
4976 return ERROR_MARK;
4980 /* Parse a new-expression.
4982 new-expression:
4983 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4984 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4986 Returns a representation of the expression. */
4988 static tree
4989 cp_parser_new_expression (cp_parser* parser)
4991 bool global_scope_p;
4992 tree placement;
4993 tree type;
4994 tree initializer;
4995 tree nelts;
4997 /* Look for the optional `::' operator. */
4998 global_scope_p
4999 = (cp_parser_global_scope_opt (parser,
5000 /*current_scope_valid_p=*/false)
5001 != NULL_TREE);
5002 /* Look for the `new' operator. */
5003 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5004 /* There's no easy way to tell a new-placement from the
5005 `( type-id )' construct. */
5006 cp_parser_parse_tentatively (parser);
5007 /* Look for a new-placement. */
5008 placement = cp_parser_new_placement (parser);
5009 /* If that didn't work out, there's no new-placement. */
5010 if (!cp_parser_parse_definitely (parser))
5011 placement = NULL_TREE;
5013 /* If the next token is a `(', then we have a parenthesized
5014 type-id. */
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5017 /* Consume the `('. */
5018 cp_lexer_consume_token (parser->lexer);
5019 /* Parse the type-id. */
5020 type = cp_parser_type_id (parser);
5021 /* Look for the closing `)'. */
5022 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5023 /* There should not be a direct-new-declarator in this production,
5024 but GCC used to allowed this, so we check and emit a sensible error
5025 message for this case. */
5026 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5028 error ("array bound forbidden after parenthesized type-id");
5029 inform ("try removing the parentheses around the type-id");
5030 cp_parser_direct_new_declarator (parser);
5032 nelts = NULL_TREE;
5034 /* Otherwise, there must be a new-type-id. */
5035 else
5036 type = cp_parser_new_type_id (parser, &nelts);
5038 /* If the next token is a `(', then we have a new-initializer. */
5039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5040 initializer = cp_parser_new_initializer (parser);
5041 else
5042 initializer = NULL_TREE;
5044 /* A new-expression may not appear in an integral constant
5045 expression. */
5046 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5047 return error_mark_node;
5049 /* Create a representation of the new-expression. */
5050 return build_new (placement, type, nelts, initializer, global_scope_p);
5053 /* Parse a new-placement.
5055 new-placement:
5056 ( expression-list )
5058 Returns the same representation as for an expression-list. */
5060 static tree
5061 cp_parser_new_placement (cp_parser* parser)
5063 tree expression_list;
5065 /* Parse the expression-list. */
5066 expression_list = (cp_parser_parenthesized_expression_list
5067 (parser, false, /*cast_p=*/false,
5068 /*non_constant_p=*/NULL));
5070 return expression_list;
5073 /* Parse a new-type-id.
5075 new-type-id:
5076 type-specifier-seq new-declarator [opt]
5078 Returns the TYPE allocated. If the new-type-id indicates an array
5079 type, *NELTS is set to the number of elements in the last array
5080 bound; the TYPE will not include the last array bound. */
5082 static tree
5083 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5085 cp_decl_specifier_seq type_specifier_seq;
5086 cp_declarator *new_declarator;
5087 cp_declarator *declarator;
5088 cp_declarator *outer_declarator;
5089 const char *saved_message;
5090 tree type;
5092 /* The type-specifier sequence must not contain type definitions.
5093 (It cannot contain declarations of new types either, but if they
5094 are not definitions we will catch that because they are not
5095 complete.) */
5096 saved_message = parser->type_definition_forbidden_message;
5097 parser->type_definition_forbidden_message
5098 = "types may not be defined in a new-type-id";
5099 /* Parse the type-specifier-seq. */
5100 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5101 &type_specifier_seq);
5102 /* Restore the old message. */
5103 parser->type_definition_forbidden_message = saved_message;
5104 /* Parse the new-declarator. */
5105 new_declarator = cp_parser_new_declarator_opt (parser);
5107 /* Determine the number of elements in the last array dimension, if
5108 any. */
5109 *nelts = NULL_TREE;
5110 /* Skip down to the last array dimension. */
5111 declarator = new_declarator;
5112 outer_declarator = NULL;
5113 while (declarator && (declarator->kind == cdk_pointer
5114 || declarator->kind == cdk_ptrmem))
5116 outer_declarator = declarator;
5117 declarator = declarator->declarator;
5119 while (declarator
5120 && declarator->kind == cdk_array
5121 && declarator->declarator
5122 && declarator->declarator->kind == cdk_array)
5124 outer_declarator = declarator;
5125 declarator = declarator->declarator;
5128 if (declarator && declarator->kind == cdk_array)
5130 *nelts = declarator->u.array.bounds;
5131 if (*nelts == error_mark_node)
5132 *nelts = integer_one_node;
5134 if (outer_declarator)
5135 outer_declarator->declarator = declarator->declarator;
5136 else
5137 new_declarator = NULL;
5140 type = groktypename (&type_specifier_seq, new_declarator);
5141 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5143 *nelts = array_type_nelts_top (type);
5144 type = TREE_TYPE (type);
5146 return type;
5149 /* Parse an (optional) new-declarator.
5151 new-declarator:
5152 ptr-operator new-declarator [opt]
5153 direct-new-declarator
5155 Returns the declarator. */
5157 static cp_declarator *
5158 cp_parser_new_declarator_opt (cp_parser* parser)
5160 enum tree_code code;
5161 tree type;
5162 cp_cv_quals cv_quals;
5164 /* We don't know if there's a ptr-operator next, or not. */
5165 cp_parser_parse_tentatively (parser);
5166 /* Look for a ptr-operator. */
5167 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5168 /* If that worked, look for more new-declarators. */
5169 if (cp_parser_parse_definitely (parser))
5171 cp_declarator *declarator;
5173 /* Parse another optional declarator. */
5174 declarator = cp_parser_new_declarator_opt (parser);
5176 /* Create the representation of the declarator. */
5177 if (type)
5178 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5179 else if (code == INDIRECT_REF)
5180 declarator = make_pointer_declarator (cv_quals, declarator);
5181 else
5182 declarator = make_reference_declarator (cv_quals, declarator);
5184 return declarator;
5187 /* If the next token is a `[', there is a direct-new-declarator. */
5188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5189 return cp_parser_direct_new_declarator (parser);
5191 return NULL;
5194 /* Parse a direct-new-declarator.
5196 direct-new-declarator:
5197 [ expression ]
5198 direct-new-declarator [constant-expression]
5202 static cp_declarator *
5203 cp_parser_direct_new_declarator (cp_parser* parser)
5205 cp_declarator *declarator = NULL;
5207 while (true)
5209 tree expression;
5211 /* Look for the opening `['. */
5212 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5213 /* The first expression is not required to be constant. */
5214 if (!declarator)
5216 expression = cp_parser_expression (parser, /*cast_p=*/false);
5217 /* The standard requires that the expression have integral
5218 type. DR 74 adds enumeration types. We believe that the
5219 real intent is that these expressions be handled like the
5220 expression in a `switch' condition, which also allows
5221 classes with a single conversion to integral or
5222 enumeration type. */
5223 if (!processing_template_decl)
5225 expression
5226 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5227 expression,
5228 /*complain=*/true);
5229 if (!expression)
5231 error ("expression in new-declarator must have integral "
5232 "or enumeration type");
5233 expression = error_mark_node;
5237 /* But all the other expressions must be. */
5238 else
5239 expression
5240 = cp_parser_constant_expression (parser,
5241 /*allow_non_constant=*/false,
5242 NULL);
5243 /* Look for the closing `]'. */
5244 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5246 /* Add this bound to the declarator. */
5247 declarator = make_array_declarator (declarator, expression);
5249 /* If the next token is not a `[', then there are no more
5250 bounds. */
5251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5252 break;
5255 return declarator;
5258 /* Parse a new-initializer.
5260 new-initializer:
5261 ( expression-list [opt] )
5263 Returns a representation of the expression-list. If there is no
5264 expression-list, VOID_ZERO_NODE is returned. */
5266 static tree
5267 cp_parser_new_initializer (cp_parser* parser)
5269 tree expression_list;
5271 expression_list = (cp_parser_parenthesized_expression_list
5272 (parser, false, /*cast_p=*/false,
5273 /*non_constant_p=*/NULL));
5274 if (!expression_list)
5275 expression_list = void_zero_node;
5277 return expression_list;
5280 /* Parse a delete-expression.
5282 delete-expression:
5283 :: [opt] delete cast-expression
5284 :: [opt] delete [ ] cast-expression
5286 Returns a representation of the expression. */
5288 static tree
5289 cp_parser_delete_expression (cp_parser* parser)
5291 bool global_scope_p;
5292 bool array_p;
5293 tree expression;
5295 /* Look for the optional `::' operator. */
5296 global_scope_p
5297 = (cp_parser_global_scope_opt (parser,
5298 /*current_scope_valid_p=*/false)
5299 != NULL_TREE);
5300 /* Look for the `delete' keyword. */
5301 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5302 /* See if the array syntax is in use. */
5303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5305 /* Consume the `[' token. */
5306 cp_lexer_consume_token (parser->lexer);
5307 /* Look for the `]' token. */
5308 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5309 /* Remember that this is the `[]' construct. */
5310 array_p = true;
5312 else
5313 array_p = false;
5315 /* Parse the cast-expression. */
5316 expression = cp_parser_simple_cast_expression (parser);
5318 /* A delete-expression may not appear in an integral constant
5319 expression. */
5320 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5321 return error_mark_node;
5323 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5326 /* Parse a cast-expression.
5328 cast-expression:
5329 unary-expression
5330 ( type-id ) cast-expression
5332 ADDRESS_P is true iff the unary-expression is appearing as the
5333 operand of the `&' operator. CAST_P is true if this expression is
5334 the target of a cast.
5336 Returns a representation of the expression. */
5338 static tree
5339 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5341 /* If it's a `(', then we might be looking at a cast. */
5342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5344 tree type = NULL_TREE;
5345 tree expr = NULL_TREE;
5346 bool compound_literal_p;
5347 const char *saved_message;
5349 /* There's no way to know yet whether or not this is a cast.
5350 For example, `(int (3))' is a unary-expression, while `(int)
5351 3' is a cast. So, we resort to parsing tentatively. */
5352 cp_parser_parse_tentatively (parser);
5353 /* Types may not be defined in a cast. */
5354 saved_message = parser->type_definition_forbidden_message;
5355 parser->type_definition_forbidden_message
5356 = "types may not be defined in casts";
5357 /* Consume the `('. */
5358 cp_lexer_consume_token (parser->lexer);
5359 /* A very tricky bit is that `(struct S) { 3 }' is a
5360 compound-literal (which we permit in C++ as an extension).
5361 But, that construct is not a cast-expression -- it is a
5362 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5363 is legal; if the compound-literal were a cast-expression,
5364 you'd need an extra set of parentheses.) But, if we parse
5365 the type-id, and it happens to be a class-specifier, then we
5366 will commit to the parse at that point, because we cannot
5367 undo the action that is done when creating a new class. So,
5368 then we cannot back up and do a postfix-expression.
5370 Therefore, we scan ahead to the closing `)', and check to see
5371 if the token after the `)' is a `{'. If so, we are not
5372 looking at a cast-expression.
5374 Save tokens so that we can put them back. */
5375 cp_lexer_save_tokens (parser->lexer);
5376 /* Skip tokens until the next token is a closing parenthesis.
5377 If we find the closing `)', and the next token is a `{', then
5378 we are looking at a compound-literal. */
5379 compound_literal_p
5380 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5381 /*consume_paren=*/true)
5382 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5383 /* Roll back the tokens we skipped. */
5384 cp_lexer_rollback_tokens (parser->lexer);
5385 /* If we were looking at a compound-literal, simulate an error
5386 so that the call to cp_parser_parse_definitely below will
5387 fail. */
5388 if (compound_literal_p)
5389 cp_parser_simulate_error (parser);
5390 else
5392 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5393 parser->in_type_id_in_expr_p = true;
5394 /* Look for the type-id. */
5395 type = cp_parser_type_id (parser);
5396 /* Look for the closing `)'. */
5397 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5398 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5401 /* Restore the saved message. */
5402 parser->type_definition_forbidden_message = saved_message;
5404 /* If ok so far, parse the dependent expression. We cannot be
5405 sure it is a cast. Consider `(T ())'. It is a parenthesized
5406 ctor of T, but looks like a cast to function returning T
5407 without a dependent expression. */
5408 if (!cp_parser_error_occurred (parser))
5409 expr = cp_parser_cast_expression (parser,
5410 /*address_p=*/false,
5411 /*cast_p=*/true);
5413 if (cp_parser_parse_definitely (parser))
5415 /* Warn about old-style casts, if so requested. */
5416 if (warn_old_style_cast
5417 && !in_system_header
5418 && !VOID_TYPE_P (type)
5419 && current_lang_name != lang_name_c)
5420 warning (0, "use of old-style cast");
5422 /* Only type conversions to integral or enumeration types
5423 can be used in constant-expressions. */
5424 if (parser->integral_constant_expression_p
5425 && !dependent_type_p (type)
5426 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5427 && (cp_parser_non_integral_constant_expression
5428 (parser,
5429 "a cast to a type other than an integral or "
5430 "enumeration type")))
5431 return error_mark_node;
5433 /* Perform the cast. */
5434 expr = build_c_cast (type, expr);
5435 return expr;
5439 /* If we get here, then it's not a cast, so it must be a
5440 unary-expression. */
5441 return cp_parser_unary_expression (parser, address_p, cast_p);
5444 /* Parse a binary expression of the general form:
5446 pm-expression:
5447 cast-expression
5448 pm-expression .* cast-expression
5449 pm-expression ->* cast-expression
5451 multiplicative-expression:
5452 pm-expression
5453 multiplicative-expression * pm-expression
5454 multiplicative-expression / pm-expression
5455 multiplicative-expression % pm-expression
5457 additive-expression:
5458 multiplicative-expression
5459 additive-expression + multiplicative-expression
5460 additive-expression - multiplicative-expression
5462 shift-expression:
5463 additive-expression
5464 shift-expression << additive-expression
5465 shift-expression >> additive-expression
5467 relational-expression:
5468 shift-expression
5469 relational-expression < shift-expression
5470 relational-expression > shift-expression
5471 relational-expression <= shift-expression
5472 relational-expression >= shift-expression
5474 GNU Extension:
5476 relational-expression:
5477 relational-expression <? shift-expression
5478 relational-expression >? shift-expression
5480 equality-expression:
5481 relational-expression
5482 equality-expression == relational-expression
5483 equality-expression != relational-expression
5485 and-expression:
5486 equality-expression
5487 and-expression & equality-expression
5489 exclusive-or-expression:
5490 and-expression
5491 exclusive-or-expression ^ and-expression
5493 inclusive-or-expression:
5494 exclusive-or-expression
5495 inclusive-or-expression | exclusive-or-expression
5497 logical-and-expression:
5498 inclusive-or-expression
5499 logical-and-expression && inclusive-or-expression
5501 logical-or-expression:
5502 logical-and-expression
5503 logical-or-expression || logical-and-expression
5505 All these are implemented with a single function like:
5507 binary-expression:
5508 simple-cast-expression
5509 binary-expression <token> binary-expression
5511 CAST_P is true if this expression is the target of a cast.
5513 The binops_by_token map is used to get the tree codes for each <token> type.
5514 binary-expressions are associated according to a precedence table. */
5516 #define TOKEN_PRECEDENCE(token) \
5517 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5518 ? PREC_NOT_OPERATOR \
5519 : binops_by_token[token->type].prec)
5521 static tree
5522 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5524 cp_parser_expression_stack stack;
5525 cp_parser_expression_stack_entry *sp = &stack[0];
5526 tree lhs, rhs;
5527 cp_token *token;
5528 enum tree_code tree_type;
5529 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5530 bool overloaded_p;
5532 /* Parse the first expression. */
5533 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5535 for (;;)
5537 /* Get an operator token. */
5538 token = cp_lexer_peek_token (parser->lexer);
5539 if (token->type == CPP_MIN || token->type == CPP_MAX)
5540 cp_parser_warn_min_max ();
5542 new_prec = TOKEN_PRECEDENCE (token);
5544 /* Popping an entry off the stack means we completed a subexpression:
5545 - either we found a token which is not an operator (`>' where it is not
5546 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5547 will happen repeatedly;
5548 - or, we found an operator which has lower priority. This is the case
5549 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5550 parsing `3 * 4'. */
5551 if (new_prec <= prec)
5553 if (sp == stack)
5554 break;
5555 else
5556 goto pop;
5559 get_rhs:
5560 tree_type = binops_by_token[token->type].tree_type;
5562 /* We used the operator token. */
5563 cp_lexer_consume_token (parser->lexer);
5565 /* Extract another operand. It may be the RHS of this expression
5566 or the LHS of a new, higher priority expression. */
5567 rhs = cp_parser_simple_cast_expression (parser);
5569 /* Get another operator token. Look up its precedence to avoid
5570 building a useless (immediately popped) stack entry for common
5571 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5572 token = cp_lexer_peek_token (parser->lexer);
5573 lookahead_prec = TOKEN_PRECEDENCE (token);
5574 if (lookahead_prec > new_prec)
5576 /* ... and prepare to parse the RHS of the new, higher priority
5577 expression. Since precedence levels on the stack are
5578 monotonically increasing, we do not have to care about
5579 stack overflows. */
5580 sp->prec = prec;
5581 sp->tree_type = tree_type;
5582 sp->lhs = lhs;
5583 sp++;
5584 lhs = rhs;
5585 prec = new_prec;
5586 new_prec = lookahead_prec;
5587 goto get_rhs;
5589 pop:
5590 /* If the stack is not empty, we have parsed into LHS the right side
5591 (`4' in the example above) of an expression we had suspended.
5592 We can use the information on the stack to recover the LHS (`3')
5593 from the stack together with the tree code (`MULT_EXPR'), and
5594 the precedence of the higher level subexpression
5595 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5596 which will be used to actually build the additive expression. */
5597 --sp;
5598 prec = sp->prec;
5599 tree_type = sp->tree_type;
5600 rhs = lhs;
5601 lhs = sp->lhs;
5604 overloaded_p = false;
5605 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5607 /* If the binary operator required the use of an overloaded operator,
5608 then this expression cannot be an integral constant-expression.
5609 An overloaded operator can be used even if both operands are
5610 otherwise permissible in an integral constant-expression if at
5611 least one of the operands is of enumeration type. */
5613 if (overloaded_p
5614 && (cp_parser_non_integral_constant_expression
5615 (parser, "calls to overloaded operators")))
5616 return error_mark_node;
5619 return lhs;
5623 /* Parse the `? expression : assignment-expression' part of a
5624 conditional-expression. The LOGICAL_OR_EXPR is the
5625 logical-or-expression that started the conditional-expression.
5626 Returns a representation of the entire conditional-expression.
5628 This routine is used by cp_parser_assignment_expression.
5630 ? expression : assignment-expression
5632 GNU Extensions:
5634 ? : assignment-expression */
5636 static tree
5637 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5639 tree expr;
5640 tree assignment_expr;
5642 /* Consume the `?' token. */
5643 cp_lexer_consume_token (parser->lexer);
5644 if (cp_parser_allow_gnu_extensions_p (parser)
5645 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5646 /* Implicit true clause. */
5647 expr = NULL_TREE;
5648 else
5649 /* Parse the expression. */
5650 expr = cp_parser_expression (parser, /*cast_p=*/false);
5652 /* The next token should be a `:'. */
5653 cp_parser_require (parser, CPP_COLON, "`:'");
5654 /* Parse the assignment-expression. */
5655 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5657 /* Build the conditional-expression. */
5658 return build_x_conditional_expr (logical_or_expr,
5659 expr,
5660 assignment_expr);
5663 /* Parse an assignment-expression.
5665 assignment-expression:
5666 conditional-expression
5667 logical-or-expression assignment-operator assignment_expression
5668 throw-expression
5670 CAST_P is true if this expression is the target of a cast.
5672 Returns a representation for the expression. */
5674 static tree
5675 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5677 tree expr;
5679 /* If the next token is the `throw' keyword, then we're looking at
5680 a throw-expression. */
5681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5682 expr = cp_parser_throw_expression (parser);
5683 /* Otherwise, it must be that we are looking at a
5684 logical-or-expression. */
5685 else
5687 /* Parse the binary expressions (logical-or-expression). */
5688 expr = cp_parser_binary_expression (parser, cast_p);
5689 /* If the next token is a `?' then we're actually looking at a
5690 conditional-expression. */
5691 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5692 return cp_parser_question_colon_clause (parser, expr);
5693 else
5695 enum tree_code assignment_operator;
5697 /* If it's an assignment-operator, we're using the second
5698 production. */
5699 assignment_operator
5700 = cp_parser_assignment_operator_opt (parser);
5701 if (assignment_operator != ERROR_MARK)
5703 tree rhs;
5705 /* Parse the right-hand side of the assignment. */
5706 rhs = cp_parser_assignment_expression (parser, cast_p);
5707 /* An assignment may not appear in a
5708 constant-expression. */
5709 if (cp_parser_non_integral_constant_expression (parser,
5710 "an assignment"))
5711 return error_mark_node;
5712 /* Build the assignment expression. */
5713 expr = build_x_modify_expr (expr,
5714 assignment_operator,
5715 rhs);
5720 return expr;
5723 /* Parse an (optional) assignment-operator.
5725 assignment-operator: one of
5726 = *= /= %= += -= >>= <<= &= ^= |=
5728 GNU Extension:
5730 assignment-operator: one of
5731 <?= >?=
5733 If the next token is an assignment operator, the corresponding tree
5734 code is returned, and the token is consumed. For example, for
5735 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5736 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5737 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5738 operator, ERROR_MARK is returned. */
5740 static enum tree_code
5741 cp_parser_assignment_operator_opt (cp_parser* parser)
5743 enum tree_code op;
5744 cp_token *token;
5746 /* Peek at the next toen. */
5747 token = cp_lexer_peek_token (parser->lexer);
5749 switch (token->type)
5751 case CPP_EQ:
5752 op = NOP_EXPR;
5753 break;
5755 case CPP_MULT_EQ:
5756 op = MULT_EXPR;
5757 break;
5759 case CPP_DIV_EQ:
5760 op = TRUNC_DIV_EXPR;
5761 break;
5763 case CPP_MOD_EQ:
5764 op = TRUNC_MOD_EXPR;
5765 break;
5767 case CPP_PLUS_EQ:
5768 op = PLUS_EXPR;
5769 break;
5771 case CPP_MINUS_EQ:
5772 op = MINUS_EXPR;
5773 break;
5775 case CPP_RSHIFT_EQ:
5776 op = RSHIFT_EXPR;
5777 break;
5779 case CPP_LSHIFT_EQ:
5780 op = LSHIFT_EXPR;
5781 break;
5783 case CPP_AND_EQ:
5784 op = BIT_AND_EXPR;
5785 break;
5787 case CPP_XOR_EQ:
5788 op = BIT_XOR_EXPR;
5789 break;
5791 case CPP_OR_EQ:
5792 op = BIT_IOR_EXPR;
5793 break;
5795 case CPP_MIN_EQ:
5796 op = MIN_EXPR;
5797 cp_parser_warn_min_max ();
5798 break;
5800 case CPP_MAX_EQ:
5801 op = MAX_EXPR;
5802 cp_parser_warn_min_max ();
5803 break;
5805 default:
5806 /* Nothing else is an assignment operator. */
5807 op = ERROR_MARK;
5810 /* If it was an assignment operator, consume it. */
5811 if (op != ERROR_MARK)
5812 cp_lexer_consume_token (parser->lexer);
5814 return op;
5817 /* Parse an expression.
5819 expression:
5820 assignment-expression
5821 expression , assignment-expression
5823 CAST_P is true if this expression is the target of a cast.
5825 Returns a representation of the expression. */
5827 static tree
5828 cp_parser_expression (cp_parser* parser, bool cast_p)
5830 tree expression = NULL_TREE;
5832 while (true)
5834 tree assignment_expression;
5836 /* Parse the next assignment-expression. */
5837 assignment_expression
5838 = cp_parser_assignment_expression (parser, cast_p);
5839 /* If this is the first assignment-expression, we can just
5840 save it away. */
5841 if (!expression)
5842 expression = assignment_expression;
5843 else
5844 expression = build_x_compound_expr (expression,
5845 assignment_expression);
5846 /* If the next token is not a comma, then we are done with the
5847 expression. */
5848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5849 break;
5850 /* Consume the `,'. */
5851 cp_lexer_consume_token (parser->lexer);
5852 /* A comma operator cannot appear in a constant-expression. */
5853 if (cp_parser_non_integral_constant_expression (parser,
5854 "a comma operator"))
5855 expression = error_mark_node;
5858 return expression;
5861 /* Parse a constant-expression.
5863 constant-expression:
5864 conditional-expression
5866 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5867 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5868 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5869 is false, NON_CONSTANT_P should be NULL. */
5871 static tree
5872 cp_parser_constant_expression (cp_parser* parser,
5873 bool allow_non_constant_p,
5874 bool *non_constant_p)
5876 bool saved_integral_constant_expression_p;
5877 bool saved_allow_non_integral_constant_expression_p;
5878 bool saved_non_integral_constant_expression_p;
5879 tree expression;
5881 /* It might seem that we could simply parse the
5882 conditional-expression, and then check to see if it were
5883 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5884 one that the compiler can figure out is constant, possibly after
5885 doing some simplifications or optimizations. The standard has a
5886 precise definition of constant-expression, and we must honor
5887 that, even though it is somewhat more restrictive.
5889 For example:
5891 int i[(2, 3)];
5893 is not a legal declaration, because `(2, 3)' is not a
5894 constant-expression. The `,' operator is forbidden in a
5895 constant-expression. However, GCC's constant-folding machinery
5896 will fold this operation to an INTEGER_CST for `3'. */
5898 /* Save the old settings. */
5899 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5900 saved_allow_non_integral_constant_expression_p
5901 = parser->allow_non_integral_constant_expression_p;
5902 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5903 /* We are now parsing a constant-expression. */
5904 parser->integral_constant_expression_p = true;
5905 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5906 parser->non_integral_constant_expression_p = false;
5907 /* Although the grammar says "conditional-expression", we parse an
5908 "assignment-expression", which also permits "throw-expression"
5909 and the use of assignment operators. In the case that
5910 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5911 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5912 actually essential that we look for an assignment-expression.
5913 For example, cp_parser_initializer_clauses uses this function to
5914 determine whether a particular assignment-expression is in fact
5915 constant. */
5916 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5917 /* Restore the old settings. */
5918 parser->integral_constant_expression_p
5919 = saved_integral_constant_expression_p;
5920 parser->allow_non_integral_constant_expression_p
5921 = saved_allow_non_integral_constant_expression_p;
5922 if (allow_non_constant_p)
5923 *non_constant_p = parser->non_integral_constant_expression_p;
5924 else if (parser->non_integral_constant_expression_p)
5925 expression = error_mark_node;
5926 parser->non_integral_constant_expression_p
5927 = saved_non_integral_constant_expression_p;
5929 return expression;
5932 /* Parse __builtin_offsetof.
5934 offsetof-expression:
5935 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5937 offsetof-member-designator:
5938 id-expression
5939 | offsetof-member-designator "." id-expression
5940 | offsetof-member-designator "[" expression "]"
5943 static tree
5944 cp_parser_builtin_offsetof (cp_parser *parser)
5946 int save_ice_p, save_non_ice_p;
5947 tree type, expr;
5948 cp_id_kind dummy;
5950 /* We're about to accept non-integral-constant things, but will
5951 definitely yield an integral constant expression. Save and
5952 restore these values around our local parsing. */
5953 save_ice_p = parser->integral_constant_expression_p;
5954 save_non_ice_p = parser->non_integral_constant_expression_p;
5956 /* Consume the "__builtin_offsetof" token. */
5957 cp_lexer_consume_token (parser->lexer);
5958 /* Consume the opening `('. */
5959 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5960 /* Parse the type-id. */
5961 type = cp_parser_type_id (parser);
5962 /* Look for the `,'. */
5963 cp_parser_require (parser, CPP_COMMA, "`,'");
5965 /* Build the (type *)null that begins the traditional offsetof macro. */
5966 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5968 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5969 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5970 true, &dummy);
5971 while (true)
5973 cp_token *token = cp_lexer_peek_token (parser->lexer);
5974 switch (token->type)
5976 case CPP_OPEN_SQUARE:
5977 /* offsetof-member-designator "[" expression "]" */
5978 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5979 break;
5981 case CPP_DOT:
5982 /* offsetof-member-designator "." identifier */
5983 cp_lexer_consume_token (parser->lexer);
5984 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5985 true, &dummy);
5986 break;
5988 case CPP_CLOSE_PAREN:
5989 /* Consume the ")" token. */
5990 cp_lexer_consume_token (parser->lexer);
5991 goto success;
5993 default:
5994 /* Error. We know the following require will fail, but
5995 that gives the proper error message. */
5996 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5997 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5998 expr = error_mark_node;
5999 goto failure;
6003 success:
6004 /* If we're processing a template, we can't finish the semantics yet.
6005 Otherwise we can fold the entire expression now. */
6006 if (processing_template_decl)
6007 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6008 else
6009 expr = fold_offsetof (expr);
6011 failure:
6012 parser->integral_constant_expression_p = save_ice_p;
6013 parser->non_integral_constant_expression_p = save_non_ice_p;
6015 return expr;
6018 /* Statements [gram.stmt.stmt] */
6020 /* Parse a statement.
6022 statement:
6023 labeled-statement
6024 expression-statement
6025 compound-statement
6026 selection-statement
6027 iteration-statement
6028 jump-statement
6029 declaration-statement
6030 try-block
6032 IN_COMPOUND is true when the statement is nested inside a
6033 cp_parser_compound_statement; this matters for certain pragmas. */
6035 static void
6036 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6037 bool in_compound)
6039 tree statement;
6040 cp_token *token;
6041 location_t statement_location;
6043 restart:
6044 /* There is no statement yet. */
6045 statement = NULL_TREE;
6046 /* Peek at the next token. */
6047 token = cp_lexer_peek_token (parser->lexer);
6048 /* Remember the location of the first token in the statement. */
6049 statement_location = token->location;
6050 /* If this is a keyword, then that will often determine what kind of
6051 statement we have. */
6052 if (token->type == CPP_KEYWORD)
6054 enum rid keyword = token->keyword;
6056 switch (keyword)
6058 case RID_CASE:
6059 case RID_DEFAULT:
6060 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6061 in_compound);
6062 break;
6064 case RID_IF:
6065 case RID_SWITCH:
6066 statement = cp_parser_selection_statement (parser);
6067 break;
6069 case RID_WHILE:
6070 case RID_DO:
6071 case RID_FOR:
6072 statement = cp_parser_iteration_statement (parser);
6073 break;
6075 case RID_BREAK:
6076 case RID_CONTINUE:
6077 case RID_RETURN:
6078 case RID_GOTO:
6079 statement = cp_parser_jump_statement (parser);
6080 break;
6082 /* Objective-C++ exception-handling constructs. */
6083 case RID_AT_TRY:
6084 case RID_AT_CATCH:
6085 case RID_AT_FINALLY:
6086 case RID_AT_SYNCHRONIZED:
6087 case RID_AT_THROW:
6088 statement = cp_parser_objc_statement (parser);
6089 break;
6091 case RID_TRY:
6092 statement = cp_parser_try_block (parser);
6093 break;
6095 default:
6096 /* It might be a keyword like `int' that can start a
6097 declaration-statement. */
6098 break;
6101 else if (token->type == CPP_NAME)
6103 /* If the next token is a `:', then we are looking at a
6104 labeled-statement. */
6105 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6106 if (token->type == CPP_COLON)
6107 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6108 in_compound);
6110 /* Anything that starts with a `{' must be a compound-statement. */
6111 else if (token->type == CPP_OPEN_BRACE)
6112 statement = cp_parser_compound_statement (parser, NULL, false);
6113 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6114 a statement all its own. */
6115 else if (token->type == CPP_PRAGMA)
6117 /* Only certain OpenMP pragmas are attached to statements, and thus
6118 are considered statements themselves. All others are not. In
6119 the context of a compound, accept the pragma as a "statement" and
6120 return so that we can check for a close brace. Otherwise we
6121 require a real statement and must go back and read one. */
6122 if (in_compound)
6123 cp_parser_pragma (parser, pragma_compound);
6124 else if (!cp_parser_pragma (parser, pragma_stmt))
6125 goto restart;
6126 return;
6128 else if (token->type == CPP_EOF)
6130 cp_parser_error (parser, "expected statement");
6131 return;
6134 /* Everything else must be a declaration-statement or an
6135 expression-statement. Try for the declaration-statement
6136 first, unless we are looking at a `;', in which case we know that
6137 we have an expression-statement. */
6138 if (!statement)
6140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6142 cp_parser_parse_tentatively (parser);
6143 /* Try to parse the declaration-statement. */
6144 cp_parser_declaration_statement (parser);
6145 /* If that worked, we're done. */
6146 if (cp_parser_parse_definitely (parser))
6147 return;
6149 /* Look for an expression-statement instead. */
6150 statement = cp_parser_expression_statement (parser, in_statement_expr);
6153 /* Set the line number for the statement. */
6154 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6155 SET_EXPR_LOCATION (statement, statement_location);
6158 /* Parse a labeled-statement.
6160 labeled-statement:
6161 identifier : statement
6162 case constant-expression : statement
6163 default : statement
6165 GNU Extension:
6167 labeled-statement:
6168 case constant-expression ... constant-expression : statement
6170 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6171 For an ordinary label, returns a LABEL_EXPR.
6173 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6174 inside a compound. */
6176 static tree
6177 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6178 bool in_compound)
6180 cp_token *token;
6181 tree statement = error_mark_node;
6183 /* The next token should be an identifier. */
6184 token = cp_lexer_peek_token (parser->lexer);
6185 if (token->type != CPP_NAME
6186 && token->type != CPP_KEYWORD)
6188 cp_parser_error (parser, "expected labeled-statement");
6189 return error_mark_node;
6192 switch (token->keyword)
6194 case RID_CASE:
6196 tree expr, expr_hi;
6197 cp_token *ellipsis;
6199 /* Consume the `case' token. */
6200 cp_lexer_consume_token (parser->lexer);
6201 /* Parse the constant-expression. */
6202 expr = cp_parser_constant_expression (parser,
6203 /*allow_non_constant_p=*/false,
6204 NULL);
6206 ellipsis = cp_lexer_peek_token (parser->lexer);
6207 if (ellipsis->type == CPP_ELLIPSIS)
6209 /* Consume the `...' token. */
6210 cp_lexer_consume_token (parser->lexer);
6211 expr_hi =
6212 cp_parser_constant_expression (parser,
6213 /*allow_non_constant_p=*/false,
6214 NULL);
6215 /* We don't need to emit warnings here, as the common code
6216 will do this for us. */
6218 else
6219 expr_hi = NULL_TREE;
6221 if (parser->in_switch_statement_p)
6222 statement = finish_case_label (expr, expr_hi);
6223 else
6224 error ("case label %qE not within a switch statement", expr);
6226 break;
6228 case RID_DEFAULT:
6229 /* Consume the `default' token. */
6230 cp_lexer_consume_token (parser->lexer);
6232 if (parser->in_switch_statement_p)
6233 statement = finish_case_label (NULL_TREE, NULL_TREE);
6234 else
6235 error ("case label not within a switch statement");
6236 break;
6238 default:
6239 /* Anything else must be an ordinary label. */
6240 statement = finish_label_stmt (cp_parser_identifier (parser));
6241 break;
6244 /* Require the `:' token. */
6245 cp_parser_require (parser, CPP_COLON, "`:'");
6246 /* Parse the labeled statement. */
6247 cp_parser_statement (parser, in_statement_expr, in_compound);
6249 /* Return the label, in the case of a `case' or `default' label. */
6250 return statement;
6253 /* Parse an expression-statement.
6255 expression-statement:
6256 expression [opt] ;
6258 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6259 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6260 indicates whether this expression-statement is part of an
6261 expression statement. */
6263 static tree
6264 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6266 tree statement = NULL_TREE;
6268 /* If the next token is a ';', then there is no expression
6269 statement. */
6270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6271 statement = cp_parser_expression (parser, /*cast_p=*/false);
6273 /* Consume the final `;'. */
6274 cp_parser_consume_semicolon_at_end_of_statement (parser);
6276 if (in_statement_expr
6277 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6278 /* This is the final expression statement of a statement
6279 expression. */
6280 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6281 else if (statement)
6282 statement = finish_expr_stmt (statement);
6283 else
6284 finish_stmt ();
6286 return statement;
6289 /* Parse a compound-statement.
6291 compound-statement:
6292 { statement-seq [opt] }
6294 Returns a tree representing the statement. */
6296 static tree
6297 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6298 bool in_try)
6300 tree compound_stmt;
6302 /* Consume the `{'. */
6303 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6304 return error_mark_node;
6305 /* Begin the compound-statement. */
6306 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6307 /* Parse an (optional) statement-seq. */
6308 cp_parser_statement_seq_opt (parser, in_statement_expr);
6309 /* Finish the compound-statement. */
6310 finish_compound_stmt (compound_stmt);
6311 /* Consume the `}'. */
6312 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6314 return compound_stmt;
6317 /* Parse an (optional) statement-seq.
6319 statement-seq:
6320 statement
6321 statement-seq [opt] statement */
6323 static void
6324 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6326 /* Scan statements until there aren't any more. */
6327 while (true)
6329 cp_token *token = cp_lexer_peek_token (parser->lexer);
6331 /* If we're looking at a `}', then we've run out of statements. */
6332 if (token->type == CPP_CLOSE_BRACE
6333 || token->type == CPP_EOF
6334 || token->type == CPP_PRAGMA_EOL)
6335 break;
6337 /* Parse the statement. */
6338 cp_parser_statement (parser, in_statement_expr, true);
6342 /* Parse a selection-statement.
6344 selection-statement:
6345 if ( condition ) statement
6346 if ( condition ) statement else statement
6347 switch ( condition ) statement
6349 Returns the new IF_STMT or SWITCH_STMT. */
6351 static tree
6352 cp_parser_selection_statement (cp_parser* parser)
6354 cp_token *token;
6355 enum rid keyword;
6357 /* Peek at the next token. */
6358 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6360 /* See what kind of keyword it is. */
6361 keyword = token->keyword;
6362 switch (keyword)
6364 case RID_IF:
6365 case RID_SWITCH:
6367 tree statement;
6368 tree condition;
6370 /* Look for the `('. */
6371 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6373 cp_parser_skip_to_end_of_statement (parser);
6374 return error_mark_node;
6377 /* Begin the selection-statement. */
6378 if (keyword == RID_IF)
6379 statement = begin_if_stmt ();
6380 else
6381 statement = begin_switch_stmt ();
6383 /* Parse the condition. */
6384 condition = cp_parser_condition (parser);
6385 /* Look for the `)'. */
6386 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6387 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6388 /*consume_paren=*/true);
6390 if (keyword == RID_IF)
6392 /* Add the condition. */
6393 finish_if_stmt_cond (condition, statement);
6395 /* Parse the then-clause. */
6396 cp_parser_implicitly_scoped_statement (parser);
6397 finish_then_clause (statement);
6399 /* If the next token is `else', parse the else-clause. */
6400 if (cp_lexer_next_token_is_keyword (parser->lexer,
6401 RID_ELSE))
6403 /* Consume the `else' keyword. */
6404 cp_lexer_consume_token (parser->lexer);
6405 begin_else_clause (statement);
6406 /* Parse the else-clause. */
6407 cp_parser_implicitly_scoped_statement (parser);
6408 finish_else_clause (statement);
6411 /* Now we're all done with the if-statement. */
6412 finish_if_stmt (statement);
6414 else
6416 bool in_switch_statement_p;
6418 /* Add the condition. */
6419 finish_switch_cond (condition, statement);
6421 /* Parse the body of the switch-statement. */
6422 in_switch_statement_p = parser->in_switch_statement_p;
6423 parser->in_switch_statement_p = true;
6424 cp_parser_implicitly_scoped_statement (parser);
6425 parser->in_switch_statement_p = in_switch_statement_p;
6427 /* Now we're all done with the switch-statement. */
6428 finish_switch_stmt (statement);
6431 return statement;
6433 break;
6435 default:
6436 cp_parser_error (parser, "expected selection-statement");
6437 return error_mark_node;
6441 /* Parse a condition.
6443 condition:
6444 expression
6445 type-specifier-seq declarator = assignment-expression
6447 GNU Extension:
6449 condition:
6450 type-specifier-seq declarator asm-specification [opt]
6451 attributes [opt] = assignment-expression
6453 Returns the expression that should be tested. */
6455 static tree
6456 cp_parser_condition (cp_parser* parser)
6458 cp_decl_specifier_seq type_specifiers;
6459 const char *saved_message;
6461 /* Try the declaration first. */
6462 cp_parser_parse_tentatively (parser);
6463 /* New types are not allowed in the type-specifier-seq for a
6464 condition. */
6465 saved_message = parser->type_definition_forbidden_message;
6466 parser->type_definition_forbidden_message
6467 = "types may not be defined in conditions";
6468 /* Parse the type-specifier-seq. */
6469 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6470 &type_specifiers);
6471 /* Restore the saved message. */
6472 parser->type_definition_forbidden_message = saved_message;
6473 /* If all is well, we might be looking at a declaration. */
6474 if (!cp_parser_error_occurred (parser))
6476 tree decl;
6477 tree asm_specification;
6478 tree attributes;
6479 cp_declarator *declarator;
6480 tree initializer = NULL_TREE;
6482 /* Parse the declarator. */
6483 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6484 /*ctor_dtor_or_conv_p=*/NULL,
6485 /*parenthesized_p=*/NULL,
6486 /*member_p=*/false);
6487 /* Parse the attributes. */
6488 attributes = cp_parser_attributes_opt (parser);
6489 /* Parse the asm-specification. */
6490 asm_specification = cp_parser_asm_specification_opt (parser);
6491 /* If the next token is not an `=', then we might still be
6492 looking at an expression. For example:
6494 if (A(a).x)
6496 looks like a decl-specifier-seq and a declarator -- but then
6497 there is no `=', so this is an expression. */
6498 cp_parser_require (parser, CPP_EQ, "`='");
6499 /* If we did see an `=', then we are looking at a declaration
6500 for sure. */
6501 if (cp_parser_parse_definitely (parser))
6503 tree pushed_scope;
6505 /* Create the declaration. */
6506 decl = start_decl (declarator, &type_specifiers,
6507 /*initialized_p=*/true,
6508 attributes, /*prefix_attributes=*/NULL_TREE,
6509 &pushed_scope);
6510 /* Parse the assignment-expression. */
6511 initializer = cp_parser_assignment_expression (parser,
6512 /*cast_p=*/false);
6514 /* Process the initializer. */
6515 cp_finish_decl (decl,
6516 initializer,
6517 asm_specification,
6518 LOOKUP_ONLYCONVERTING);
6520 if (pushed_scope)
6521 pop_scope (pushed_scope);
6523 return convert_from_reference (decl);
6526 /* If we didn't even get past the declarator successfully, we are
6527 definitely not looking at a declaration. */
6528 else
6529 cp_parser_abort_tentative_parse (parser);
6531 /* Otherwise, we are looking at an expression. */
6532 return cp_parser_expression (parser, /*cast_p=*/false);
6535 /* Parse an iteration-statement.
6537 iteration-statement:
6538 while ( condition ) statement
6539 do statement while ( expression ) ;
6540 for ( for-init-statement condition [opt] ; expression [opt] )
6541 statement
6543 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6545 static tree
6546 cp_parser_iteration_statement (cp_parser* parser)
6548 cp_token *token;
6549 enum rid keyword;
6550 tree statement;
6551 bool in_iteration_statement_p;
6554 /* Peek at the next token. */
6555 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6556 if (!token)
6557 return error_mark_node;
6559 /* Remember whether or not we are already within an iteration
6560 statement. */
6561 in_iteration_statement_p = parser->in_iteration_statement_p;
6563 /* See what kind of keyword it is. */
6564 keyword = token->keyword;
6565 switch (keyword)
6567 case RID_WHILE:
6569 tree condition;
6571 /* Begin the while-statement. */
6572 statement = begin_while_stmt ();
6573 /* Look for the `('. */
6574 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6575 /* Parse the condition. */
6576 condition = cp_parser_condition (parser);
6577 finish_while_stmt_cond (condition, statement);
6578 /* Look for the `)'. */
6579 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6580 /* Parse the dependent statement. */
6581 parser->in_iteration_statement_p = true;
6582 cp_parser_already_scoped_statement (parser);
6583 parser->in_iteration_statement_p = in_iteration_statement_p;
6584 /* We're done with the while-statement. */
6585 finish_while_stmt (statement);
6587 break;
6589 case RID_DO:
6591 tree expression;
6593 /* Begin the do-statement. */
6594 statement = begin_do_stmt ();
6595 /* Parse the body of the do-statement. */
6596 parser->in_iteration_statement_p = true;
6597 cp_parser_implicitly_scoped_statement (parser);
6598 parser->in_iteration_statement_p = in_iteration_statement_p;
6599 finish_do_body (statement);
6600 /* Look for the `while' keyword. */
6601 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6602 /* Look for the `('. */
6603 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6604 /* Parse the expression. */
6605 expression = cp_parser_expression (parser, /*cast_p=*/false);
6606 /* We're done with the do-statement. */
6607 finish_do_stmt (expression, statement);
6608 /* Look for the `)'. */
6609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6610 /* Look for the `;'. */
6611 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6613 break;
6615 case RID_FOR:
6617 tree condition = NULL_TREE;
6618 tree expression = NULL_TREE;
6620 /* Begin the for-statement. */
6621 statement = begin_for_stmt ();
6622 /* Look for the `('. */
6623 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6624 /* Parse the initialization. */
6625 cp_parser_for_init_statement (parser);
6626 finish_for_init_stmt (statement);
6628 /* If there's a condition, process it. */
6629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6630 condition = cp_parser_condition (parser);
6631 finish_for_cond (condition, statement);
6632 /* Look for the `;'. */
6633 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6635 /* If there's an expression, process it. */
6636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6637 expression = cp_parser_expression (parser, /*cast_p=*/false);
6638 finish_for_expr (expression, statement);
6639 /* Look for the `)'. */
6640 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6642 /* Parse the body of the for-statement. */
6643 parser->in_iteration_statement_p = true;
6644 cp_parser_already_scoped_statement (parser);
6645 parser->in_iteration_statement_p = in_iteration_statement_p;
6647 /* We're done with the for-statement. */
6648 finish_for_stmt (statement);
6650 break;
6652 default:
6653 cp_parser_error (parser, "expected iteration-statement");
6654 statement = error_mark_node;
6655 break;
6658 return statement;
6661 /* Parse a for-init-statement.
6663 for-init-statement:
6664 expression-statement
6665 simple-declaration */
6667 static void
6668 cp_parser_for_init_statement (cp_parser* parser)
6670 /* If the next token is a `;', then we have an empty
6671 expression-statement. Grammatically, this is also a
6672 simple-declaration, but an invalid one, because it does not
6673 declare anything. Therefore, if we did not handle this case
6674 specially, we would issue an error message about an invalid
6675 declaration. */
6676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6678 /* We're going to speculatively look for a declaration, falling back
6679 to an expression, if necessary. */
6680 cp_parser_parse_tentatively (parser);
6681 /* Parse the declaration. */
6682 cp_parser_simple_declaration (parser,
6683 /*function_definition_allowed_p=*/false);
6684 /* If the tentative parse failed, then we shall need to look for an
6685 expression-statement. */
6686 if (cp_parser_parse_definitely (parser))
6687 return;
6690 cp_parser_expression_statement (parser, false);
6693 /* Parse a jump-statement.
6695 jump-statement:
6696 break ;
6697 continue ;
6698 return expression [opt] ;
6699 goto identifier ;
6701 GNU extension:
6703 jump-statement:
6704 goto * expression ;
6706 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6708 static tree
6709 cp_parser_jump_statement (cp_parser* parser)
6711 tree statement = error_mark_node;
6712 cp_token *token;
6713 enum rid keyword;
6715 /* Peek at the next token. */
6716 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6717 if (!token)
6718 return error_mark_node;
6720 /* See what kind of keyword it is. */
6721 keyword = token->keyword;
6722 switch (keyword)
6724 case RID_BREAK:
6725 if (!parser->in_switch_statement_p
6726 && !parser->in_iteration_statement_p)
6728 error ("break statement not within loop or switch");
6729 statement = error_mark_node;
6731 else
6732 statement = finish_break_stmt ();
6733 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6734 break;
6736 case RID_CONTINUE:
6737 if (!parser->in_iteration_statement_p)
6739 error ("continue statement not within a loop");
6740 statement = error_mark_node;
6742 else
6743 statement = finish_continue_stmt ();
6744 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6745 break;
6747 case RID_RETURN:
6749 tree expr;
6751 /* If the next token is a `;', then there is no
6752 expression. */
6753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6754 expr = cp_parser_expression (parser, /*cast_p=*/false);
6755 else
6756 expr = NULL_TREE;
6757 /* Build the return-statement. */
6758 statement = finish_return_stmt (expr);
6759 /* Look for the final `;'. */
6760 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6762 break;
6764 case RID_GOTO:
6765 /* Create the goto-statement. */
6766 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6768 /* Issue a warning about this use of a GNU extension. */
6769 if (pedantic)
6770 pedwarn ("ISO C++ forbids computed gotos");
6771 /* Consume the '*' token. */
6772 cp_lexer_consume_token (parser->lexer);
6773 /* Parse the dependent expression. */
6774 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6776 else
6777 finish_goto_stmt (cp_parser_identifier (parser));
6778 /* Look for the final `;'. */
6779 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6780 break;
6782 default:
6783 cp_parser_error (parser, "expected jump-statement");
6784 break;
6787 return statement;
6790 /* Parse a declaration-statement.
6792 declaration-statement:
6793 block-declaration */
6795 static void
6796 cp_parser_declaration_statement (cp_parser* parser)
6798 void *p;
6800 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6801 p = obstack_alloc (&declarator_obstack, 0);
6803 /* Parse the block-declaration. */
6804 cp_parser_block_declaration (parser, /*statement_p=*/true);
6806 /* Free any declarators allocated. */
6807 obstack_free (&declarator_obstack, p);
6809 /* Finish off the statement. */
6810 finish_stmt ();
6813 /* Some dependent statements (like `if (cond) statement'), are
6814 implicitly in their own scope. In other words, if the statement is
6815 a single statement (as opposed to a compound-statement), it is
6816 none-the-less treated as if it were enclosed in braces. Any
6817 declarations appearing in the dependent statement are out of scope
6818 after control passes that point. This function parses a statement,
6819 but ensures that is in its own scope, even if it is not a
6820 compound-statement.
6822 Returns the new statement. */
6824 static tree
6825 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6827 tree statement;
6829 /* If the token is not a `{', then we must take special action. */
6830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6832 /* Create a compound-statement. */
6833 statement = begin_compound_stmt (0);
6834 /* Parse the dependent-statement. */
6835 cp_parser_statement (parser, NULL_TREE, false);
6836 /* Finish the dummy compound-statement. */
6837 finish_compound_stmt (statement);
6839 /* Otherwise, we simply parse the statement directly. */
6840 else
6841 statement = cp_parser_compound_statement (parser, NULL, false);
6843 /* Return the statement. */
6844 return statement;
6847 /* For some dependent statements (like `while (cond) statement'), we
6848 have already created a scope. Therefore, even if the dependent
6849 statement is a compound-statement, we do not want to create another
6850 scope. */
6852 static void
6853 cp_parser_already_scoped_statement (cp_parser* parser)
6855 /* If the token is a `{', then we must take special action. */
6856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6857 cp_parser_statement (parser, NULL_TREE, false);
6858 else
6860 /* Avoid calling cp_parser_compound_statement, so that we
6861 don't create a new scope. Do everything else by hand. */
6862 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6863 cp_parser_statement_seq_opt (parser, NULL_TREE);
6864 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6868 /* Declarations [gram.dcl.dcl] */
6870 /* Parse an optional declaration-sequence.
6872 declaration-seq:
6873 declaration
6874 declaration-seq declaration */
6876 static void
6877 cp_parser_declaration_seq_opt (cp_parser* parser)
6879 while (true)
6881 cp_token *token;
6883 token = cp_lexer_peek_token (parser->lexer);
6885 if (token->type == CPP_CLOSE_BRACE
6886 || token->type == CPP_EOF
6887 || token->type == CPP_PRAGMA_EOL)
6888 break;
6890 if (token->type == CPP_SEMICOLON)
6892 /* A declaration consisting of a single semicolon is
6893 invalid. Allow it unless we're being pedantic. */
6894 cp_lexer_consume_token (parser->lexer);
6895 if (pedantic && !in_system_header)
6896 pedwarn ("extra %<;%>");
6897 continue;
6900 /* If we're entering or exiting a region that's implicitly
6901 extern "C", modify the lang context appropriately. */
6902 if (!parser->implicit_extern_c && token->implicit_extern_c)
6904 push_lang_context (lang_name_c);
6905 parser->implicit_extern_c = true;
6907 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6909 pop_lang_context ();
6910 parser->implicit_extern_c = false;
6913 if (token->type == CPP_PRAGMA)
6915 /* A top-level declaration can consist solely of a #pragma.
6916 A nested declaration cannot, so this is done here and not
6917 in cp_parser_declaration. (A #pragma at block scope is
6918 handled in cp_parser_statement.) */
6919 cp_parser_pragma (parser, pragma_external);
6920 continue;
6923 /* Parse the declaration itself. */
6924 cp_parser_declaration (parser);
6928 /* Parse a declaration.
6930 declaration:
6931 block-declaration
6932 function-definition
6933 template-declaration
6934 explicit-instantiation
6935 explicit-specialization
6936 linkage-specification
6937 namespace-definition
6939 GNU extension:
6941 declaration:
6942 __extension__ declaration */
6944 static void
6945 cp_parser_declaration (cp_parser* parser)
6947 cp_token token1;
6948 cp_token token2;
6949 int saved_pedantic;
6950 void *p;
6952 /* Check for the `__extension__' keyword. */
6953 if (cp_parser_extension_opt (parser, &saved_pedantic))
6955 /* Parse the qualified declaration. */
6956 cp_parser_declaration (parser);
6957 /* Restore the PEDANTIC flag. */
6958 pedantic = saved_pedantic;
6960 return;
6963 /* Try to figure out what kind of declaration is present. */
6964 token1 = *cp_lexer_peek_token (parser->lexer);
6966 if (token1.type != CPP_EOF)
6967 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6968 else
6970 token2.type = CPP_EOF;
6971 token2.keyword = RID_MAX;
6974 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6975 p = obstack_alloc (&declarator_obstack, 0);
6977 /* If the next token is `extern' and the following token is a string
6978 literal, then we have a linkage specification. */
6979 if (token1.keyword == RID_EXTERN
6980 && cp_parser_is_string_literal (&token2))
6981 cp_parser_linkage_specification (parser);
6982 /* If the next token is `template', then we have either a template
6983 declaration, an explicit instantiation, or an explicit
6984 specialization. */
6985 else if (token1.keyword == RID_TEMPLATE)
6987 /* `template <>' indicates a template specialization. */
6988 if (token2.type == CPP_LESS
6989 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6990 cp_parser_explicit_specialization (parser);
6991 /* `template <' indicates a template declaration. */
6992 else if (token2.type == CPP_LESS)
6993 cp_parser_template_declaration (parser, /*member_p=*/false);
6994 /* Anything else must be an explicit instantiation. */
6995 else
6996 cp_parser_explicit_instantiation (parser);
6998 /* If the next token is `export', then we have a template
6999 declaration. */
7000 else if (token1.keyword == RID_EXPORT)
7001 cp_parser_template_declaration (parser, /*member_p=*/false);
7002 /* If the next token is `extern', 'static' or 'inline' and the one
7003 after that is `template', we have a GNU extended explicit
7004 instantiation directive. */
7005 else if (cp_parser_allow_gnu_extensions_p (parser)
7006 && (token1.keyword == RID_EXTERN
7007 || token1.keyword == RID_STATIC
7008 || token1.keyword == RID_INLINE)
7009 && token2.keyword == RID_TEMPLATE)
7010 cp_parser_explicit_instantiation (parser);
7011 /* If the next token is `namespace', check for a named or unnamed
7012 namespace definition. */
7013 else if (token1.keyword == RID_NAMESPACE
7014 && (/* A named namespace definition. */
7015 (token2.type == CPP_NAME
7016 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7017 == CPP_OPEN_BRACE))
7018 /* An unnamed namespace definition. */
7019 || token2.type == CPP_OPEN_BRACE))
7020 cp_parser_namespace_definition (parser);
7021 /* Objective-C++ declaration/definition. */
7022 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7023 cp_parser_objc_declaration (parser);
7024 /* We must have either a block declaration or a function
7025 definition. */
7026 else
7027 /* Try to parse a block-declaration, or a function-definition. */
7028 cp_parser_block_declaration (parser, /*statement_p=*/false);
7030 /* Free any declarators allocated. */
7031 obstack_free (&declarator_obstack, p);
7034 /* Parse a block-declaration.
7036 block-declaration:
7037 simple-declaration
7038 asm-definition
7039 namespace-alias-definition
7040 using-declaration
7041 using-directive
7043 GNU Extension:
7045 block-declaration:
7046 __extension__ block-declaration
7047 label-declaration
7049 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7050 part of a declaration-statement. */
7052 static void
7053 cp_parser_block_declaration (cp_parser *parser,
7054 bool statement_p)
7056 cp_token *token1;
7057 int saved_pedantic;
7059 /* Check for the `__extension__' keyword. */
7060 if (cp_parser_extension_opt (parser, &saved_pedantic))
7062 /* Parse the qualified declaration. */
7063 cp_parser_block_declaration (parser, statement_p);
7064 /* Restore the PEDANTIC flag. */
7065 pedantic = saved_pedantic;
7067 return;
7070 /* Peek at the next token to figure out which kind of declaration is
7071 present. */
7072 token1 = cp_lexer_peek_token (parser->lexer);
7074 /* If the next keyword is `asm', we have an asm-definition. */
7075 if (token1->keyword == RID_ASM)
7077 if (statement_p)
7078 cp_parser_commit_to_tentative_parse (parser);
7079 cp_parser_asm_definition (parser);
7081 /* If the next keyword is `namespace', we have a
7082 namespace-alias-definition. */
7083 else if (token1->keyword == RID_NAMESPACE)
7084 cp_parser_namespace_alias_definition (parser);
7085 /* If the next keyword is `using', we have either a
7086 using-declaration or a using-directive. */
7087 else if (token1->keyword == RID_USING)
7089 cp_token *token2;
7091 if (statement_p)
7092 cp_parser_commit_to_tentative_parse (parser);
7093 /* If the token after `using' is `namespace', then we have a
7094 using-directive. */
7095 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7096 if (token2->keyword == RID_NAMESPACE)
7097 cp_parser_using_directive (parser);
7098 /* Otherwise, it's a using-declaration. */
7099 else
7100 cp_parser_using_declaration (parser);
7102 /* If the next keyword is `__label__' we have a label declaration. */
7103 else if (token1->keyword == RID_LABEL)
7105 if (statement_p)
7106 cp_parser_commit_to_tentative_parse (parser);
7107 cp_parser_label_declaration (parser);
7109 /* Anything else must be a simple-declaration. */
7110 else
7111 cp_parser_simple_declaration (parser, !statement_p);
7114 /* Parse a simple-declaration.
7116 simple-declaration:
7117 decl-specifier-seq [opt] init-declarator-list [opt] ;
7119 init-declarator-list:
7120 init-declarator
7121 init-declarator-list , init-declarator
7123 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7124 function-definition as a simple-declaration. */
7126 static void
7127 cp_parser_simple_declaration (cp_parser* parser,
7128 bool function_definition_allowed_p)
7130 cp_decl_specifier_seq decl_specifiers;
7131 int declares_class_or_enum;
7132 bool saw_declarator;
7134 /* Defer access checks until we know what is being declared; the
7135 checks for names appearing in the decl-specifier-seq should be
7136 done as if we were in the scope of the thing being declared. */
7137 push_deferring_access_checks (dk_deferred);
7139 /* Parse the decl-specifier-seq. We have to keep track of whether
7140 or not the decl-specifier-seq declares a named class or
7141 enumeration type, since that is the only case in which the
7142 init-declarator-list is allowed to be empty.
7144 [dcl.dcl]
7146 In a simple-declaration, the optional init-declarator-list can be
7147 omitted only when declaring a class or enumeration, that is when
7148 the decl-specifier-seq contains either a class-specifier, an
7149 elaborated-type-specifier, or an enum-specifier. */
7150 cp_parser_decl_specifier_seq (parser,
7151 CP_PARSER_FLAGS_OPTIONAL,
7152 &decl_specifiers,
7153 &declares_class_or_enum);
7154 /* We no longer need to defer access checks. */
7155 stop_deferring_access_checks ();
7157 /* In a block scope, a valid declaration must always have a
7158 decl-specifier-seq. By not trying to parse declarators, we can
7159 resolve the declaration/expression ambiguity more quickly. */
7160 if (!function_definition_allowed_p
7161 && !decl_specifiers.any_specifiers_p)
7163 cp_parser_error (parser, "expected declaration");
7164 goto done;
7167 /* If the next two tokens are both identifiers, the code is
7168 erroneous. The usual cause of this situation is code like:
7170 T t;
7172 where "T" should name a type -- but does not. */
7173 if (!decl_specifiers.type
7174 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7176 /* If parsing tentatively, we should commit; we really are
7177 looking at a declaration. */
7178 cp_parser_commit_to_tentative_parse (parser);
7179 /* Give up. */
7180 goto done;
7183 /* If we have seen at least one decl-specifier, and the next token
7184 is not a parenthesis, then we must be looking at a declaration.
7185 (After "int (" we might be looking at a functional cast.) */
7186 if (decl_specifiers.any_specifiers_p
7187 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7188 cp_parser_commit_to_tentative_parse (parser);
7190 /* Keep going until we hit the `;' at the end of the simple
7191 declaration. */
7192 saw_declarator = false;
7193 while (cp_lexer_next_token_is_not (parser->lexer,
7194 CPP_SEMICOLON))
7196 cp_token *token;
7197 bool function_definition_p;
7198 tree decl;
7200 if (saw_declarator)
7202 /* If we are processing next declarator, coma is expected */
7203 token = cp_lexer_peek_token (parser->lexer);
7204 gcc_assert (token->type == CPP_COMMA);
7205 cp_lexer_consume_token (parser->lexer);
7207 else
7208 saw_declarator = true;
7210 /* Parse the init-declarator. */
7211 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7212 function_definition_allowed_p,
7213 /*member_p=*/false,
7214 declares_class_or_enum,
7215 &function_definition_p);
7216 /* If an error occurred while parsing tentatively, exit quickly.
7217 (That usually happens when in the body of a function; each
7218 statement is treated as a declaration-statement until proven
7219 otherwise.) */
7220 if (cp_parser_error_occurred (parser))
7221 goto done;
7222 /* Handle function definitions specially. */
7223 if (function_definition_p)
7225 /* If the next token is a `,', then we are probably
7226 processing something like:
7228 void f() {}, *p;
7230 which is erroneous. */
7231 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7232 error ("mixing declarations and function-definitions is forbidden");
7233 /* Otherwise, we're done with the list of declarators. */
7234 else
7236 pop_deferring_access_checks ();
7237 return;
7240 /* The next token should be either a `,' or a `;'. */
7241 token = cp_lexer_peek_token (parser->lexer);
7242 /* If it's a `,', there are more declarators to come. */
7243 if (token->type == CPP_COMMA)
7244 /* will be consumed next time around */;
7245 /* If it's a `;', we are done. */
7246 else if (token->type == CPP_SEMICOLON)
7247 break;
7248 /* Anything else is an error. */
7249 else
7251 /* If we have already issued an error message we don't need
7252 to issue another one. */
7253 if (decl != error_mark_node
7254 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7255 cp_parser_error (parser, "expected %<,%> or %<;%>");
7256 /* Skip tokens until we reach the end of the statement. */
7257 cp_parser_skip_to_end_of_statement (parser);
7258 /* If the next token is now a `;', consume it. */
7259 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7260 cp_lexer_consume_token (parser->lexer);
7261 goto done;
7263 /* After the first time around, a function-definition is not
7264 allowed -- even if it was OK at first. For example:
7266 int i, f() {}
7268 is not valid. */
7269 function_definition_allowed_p = false;
7272 /* Issue an error message if no declarators are present, and the
7273 decl-specifier-seq does not itself declare a class or
7274 enumeration. */
7275 if (!saw_declarator)
7277 if (cp_parser_declares_only_class_p (parser))
7278 shadow_tag (&decl_specifiers);
7279 /* Perform any deferred access checks. */
7280 perform_deferred_access_checks ();
7283 /* Consume the `;'. */
7284 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7286 done:
7287 pop_deferring_access_checks ();
7290 /* Parse a decl-specifier-seq.
7292 decl-specifier-seq:
7293 decl-specifier-seq [opt] decl-specifier
7295 decl-specifier:
7296 storage-class-specifier
7297 type-specifier
7298 function-specifier
7299 friend
7300 typedef
7302 GNU Extension:
7304 decl-specifier:
7305 attributes
7307 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7309 The parser flags FLAGS is used to control type-specifier parsing.
7311 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7312 flags:
7314 1: one of the decl-specifiers is an elaborated-type-specifier
7315 (i.e., a type declaration)
7316 2: one of the decl-specifiers is an enum-specifier or a
7317 class-specifier (i.e., a type definition)
7321 static void
7322 cp_parser_decl_specifier_seq (cp_parser* parser,
7323 cp_parser_flags flags,
7324 cp_decl_specifier_seq *decl_specs,
7325 int* declares_class_or_enum)
7327 bool constructor_possible_p = !parser->in_declarator_p;
7329 /* Clear DECL_SPECS. */
7330 clear_decl_specs (decl_specs);
7332 /* Assume no class or enumeration type is declared. */
7333 *declares_class_or_enum = 0;
7335 /* Keep reading specifiers until there are no more to read. */
7336 while (true)
7338 bool constructor_p;
7339 bool found_decl_spec;
7340 cp_token *token;
7342 /* Peek at the next token. */
7343 token = cp_lexer_peek_token (parser->lexer);
7344 /* Handle attributes. */
7345 if (token->keyword == RID_ATTRIBUTE)
7347 /* Parse the attributes. */
7348 decl_specs->attributes
7349 = chainon (decl_specs->attributes,
7350 cp_parser_attributes_opt (parser));
7351 continue;
7353 /* Assume we will find a decl-specifier keyword. */
7354 found_decl_spec = true;
7355 /* If the next token is an appropriate keyword, we can simply
7356 add it to the list. */
7357 switch (token->keyword)
7359 /* decl-specifier:
7360 friend */
7361 case RID_FRIEND:
7362 if (decl_specs->specs[(int) ds_friend]++)
7363 error ("duplicate %<friend%>");
7364 /* Consume the token. */
7365 cp_lexer_consume_token (parser->lexer);
7366 break;
7368 /* function-specifier:
7369 inline
7370 virtual
7371 explicit */
7372 case RID_INLINE:
7373 case RID_VIRTUAL:
7374 case RID_EXPLICIT:
7375 cp_parser_function_specifier_opt (parser, decl_specs);
7376 break;
7378 /* decl-specifier:
7379 typedef */
7380 case RID_TYPEDEF:
7381 ++decl_specs->specs[(int) ds_typedef];
7382 /* Consume the token. */
7383 cp_lexer_consume_token (parser->lexer);
7384 /* A constructor declarator cannot appear in a typedef. */
7385 constructor_possible_p = false;
7386 /* The "typedef" keyword can only occur in a declaration; we
7387 may as well commit at this point. */
7388 cp_parser_commit_to_tentative_parse (parser);
7389 break;
7391 /* storage-class-specifier:
7392 auto
7393 register
7394 static
7395 extern
7396 mutable
7398 GNU Extension:
7399 thread */
7400 case RID_AUTO:
7401 /* Consume the token. */
7402 cp_lexer_consume_token (parser->lexer);
7403 cp_parser_set_storage_class (decl_specs, sc_auto);
7404 break;
7405 case RID_REGISTER:
7406 /* Consume the token. */
7407 cp_lexer_consume_token (parser->lexer);
7408 cp_parser_set_storage_class (decl_specs, sc_register);
7409 break;
7410 case RID_STATIC:
7411 /* Consume the token. */
7412 cp_lexer_consume_token (parser->lexer);
7413 if (decl_specs->specs[(int) ds_thread])
7415 error ("%<__thread%> before %<static%>");
7416 decl_specs->specs[(int) ds_thread] = 0;
7418 cp_parser_set_storage_class (decl_specs, sc_static);
7419 break;
7420 case RID_EXTERN:
7421 /* Consume the token. */
7422 cp_lexer_consume_token (parser->lexer);
7423 if (decl_specs->specs[(int) ds_thread])
7425 error ("%<__thread%> before %<extern%>");
7426 decl_specs->specs[(int) ds_thread] = 0;
7428 cp_parser_set_storage_class (decl_specs, sc_extern);
7429 break;
7430 case RID_MUTABLE:
7431 /* Consume the token. */
7432 cp_lexer_consume_token (parser->lexer);
7433 cp_parser_set_storage_class (decl_specs, sc_mutable);
7434 break;
7435 case RID_THREAD:
7436 /* Consume the token. */
7437 cp_lexer_consume_token (parser->lexer);
7438 ++decl_specs->specs[(int) ds_thread];
7439 break;
7441 default:
7442 /* We did not yet find a decl-specifier yet. */
7443 found_decl_spec = false;
7444 break;
7447 /* Constructors are a special case. The `S' in `S()' is not a
7448 decl-specifier; it is the beginning of the declarator. */
7449 constructor_p
7450 = (!found_decl_spec
7451 && constructor_possible_p
7452 && (cp_parser_constructor_declarator_p
7453 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7455 /* If we don't have a DECL_SPEC yet, then we must be looking at
7456 a type-specifier. */
7457 if (!found_decl_spec && !constructor_p)
7459 int decl_spec_declares_class_or_enum;
7460 bool is_cv_qualifier;
7461 tree type_spec;
7463 type_spec
7464 = cp_parser_type_specifier (parser, flags,
7465 decl_specs,
7466 /*is_declaration=*/true,
7467 &decl_spec_declares_class_or_enum,
7468 &is_cv_qualifier);
7470 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7472 /* If this type-specifier referenced a user-defined type
7473 (a typedef, class-name, etc.), then we can't allow any
7474 more such type-specifiers henceforth.
7476 [dcl.spec]
7478 The longest sequence of decl-specifiers that could
7479 possibly be a type name is taken as the
7480 decl-specifier-seq of a declaration. The sequence shall
7481 be self-consistent as described below.
7483 [dcl.type]
7485 As a general rule, at most one type-specifier is allowed
7486 in the complete decl-specifier-seq of a declaration. The
7487 only exceptions are the following:
7489 -- const or volatile can be combined with any other
7490 type-specifier.
7492 -- signed or unsigned can be combined with char, long,
7493 short, or int.
7495 -- ..
7497 Example:
7499 typedef char* Pc;
7500 void g (const int Pc);
7502 Here, Pc is *not* part of the decl-specifier seq; it's
7503 the declarator. Therefore, once we see a type-specifier
7504 (other than a cv-qualifier), we forbid any additional
7505 user-defined types. We *do* still allow things like `int
7506 int' to be considered a decl-specifier-seq, and issue the
7507 error message later. */
7508 if (type_spec && !is_cv_qualifier)
7509 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7510 /* A constructor declarator cannot follow a type-specifier. */
7511 if (type_spec)
7513 constructor_possible_p = false;
7514 found_decl_spec = true;
7518 /* If we still do not have a DECL_SPEC, then there are no more
7519 decl-specifiers. */
7520 if (!found_decl_spec)
7521 break;
7523 decl_specs->any_specifiers_p = true;
7524 /* After we see one decl-specifier, further decl-specifiers are
7525 always optional. */
7526 flags |= CP_PARSER_FLAGS_OPTIONAL;
7529 /* Don't allow a friend specifier with a class definition. */
7530 if (decl_specs->specs[(int) ds_friend] != 0
7531 && (*declares_class_or_enum & 2))
7532 error ("class definition may not be declared a friend");
7535 /* Parse an (optional) storage-class-specifier.
7537 storage-class-specifier:
7538 auto
7539 register
7540 static
7541 extern
7542 mutable
7544 GNU Extension:
7546 storage-class-specifier:
7547 thread
7549 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7551 static tree
7552 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7554 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7556 case RID_AUTO:
7557 case RID_REGISTER:
7558 case RID_STATIC:
7559 case RID_EXTERN:
7560 case RID_MUTABLE:
7561 case RID_THREAD:
7562 /* Consume the token. */
7563 return cp_lexer_consume_token (parser->lexer)->value;
7565 default:
7566 return NULL_TREE;
7570 /* Parse an (optional) function-specifier.
7572 function-specifier:
7573 inline
7574 virtual
7575 explicit
7577 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7578 Updates DECL_SPECS, if it is non-NULL. */
7580 static tree
7581 cp_parser_function_specifier_opt (cp_parser* parser,
7582 cp_decl_specifier_seq *decl_specs)
7584 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7586 case RID_INLINE:
7587 if (decl_specs)
7588 ++decl_specs->specs[(int) ds_inline];
7589 break;
7591 case RID_VIRTUAL:
7592 if (decl_specs)
7593 ++decl_specs->specs[(int) ds_virtual];
7594 break;
7596 case RID_EXPLICIT:
7597 if (decl_specs)
7598 ++decl_specs->specs[(int) ds_explicit];
7599 break;
7601 default:
7602 return NULL_TREE;
7605 /* Consume the token. */
7606 return cp_lexer_consume_token (parser->lexer)->value;
7609 /* Parse a linkage-specification.
7611 linkage-specification:
7612 extern string-literal { declaration-seq [opt] }
7613 extern string-literal declaration */
7615 static void
7616 cp_parser_linkage_specification (cp_parser* parser)
7618 tree linkage;
7620 /* Look for the `extern' keyword. */
7621 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7623 /* Look for the string-literal. */
7624 linkage = cp_parser_string_literal (parser, false, false);
7626 /* Transform the literal into an identifier. If the literal is a
7627 wide-character string, or contains embedded NULs, then we can't
7628 handle it as the user wants. */
7629 if (strlen (TREE_STRING_POINTER (linkage))
7630 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7632 cp_parser_error (parser, "invalid linkage-specification");
7633 /* Assume C++ linkage. */
7634 linkage = lang_name_cplusplus;
7636 else
7637 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7639 /* We're now using the new linkage. */
7640 push_lang_context (linkage);
7642 /* If the next token is a `{', then we're using the first
7643 production. */
7644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7646 /* Consume the `{' token. */
7647 cp_lexer_consume_token (parser->lexer);
7648 /* Parse the declarations. */
7649 cp_parser_declaration_seq_opt (parser);
7650 /* Look for the closing `}'. */
7651 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7653 /* Otherwise, there's just one declaration. */
7654 else
7656 bool saved_in_unbraced_linkage_specification_p;
7658 saved_in_unbraced_linkage_specification_p
7659 = parser->in_unbraced_linkage_specification_p;
7660 parser->in_unbraced_linkage_specification_p = true;
7661 have_extern_spec = true;
7662 cp_parser_declaration (parser);
7663 have_extern_spec = false;
7664 parser->in_unbraced_linkage_specification_p
7665 = saved_in_unbraced_linkage_specification_p;
7668 /* We're done with the linkage-specification. */
7669 pop_lang_context ();
7672 /* Special member functions [gram.special] */
7674 /* Parse a conversion-function-id.
7676 conversion-function-id:
7677 operator conversion-type-id
7679 Returns an IDENTIFIER_NODE representing the operator. */
7681 static tree
7682 cp_parser_conversion_function_id (cp_parser* parser)
7684 tree type;
7685 tree saved_scope;
7686 tree saved_qualifying_scope;
7687 tree saved_object_scope;
7688 tree pushed_scope = NULL_TREE;
7690 /* Look for the `operator' token. */
7691 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7692 return error_mark_node;
7693 /* When we parse the conversion-type-id, the current scope will be
7694 reset. However, we need that information in able to look up the
7695 conversion function later, so we save it here. */
7696 saved_scope = parser->scope;
7697 saved_qualifying_scope = parser->qualifying_scope;
7698 saved_object_scope = parser->object_scope;
7699 /* We must enter the scope of the class so that the names of
7700 entities declared within the class are available in the
7701 conversion-type-id. For example, consider:
7703 struct S {
7704 typedef int I;
7705 operator I();
7708 S::operator I() { ... }
7710 In order to see that `I' is a type-name in the definition, we
7711 must be in the scope of `S'. */
7712 if (saved_scope)
7713 pushed_scope = push_scope (saved_scope);
7714 /* Parse the conversion-type-id. */
7715 type = cp_parser_conversion_type_id (parser);
7716 /* Leave the scope of the class, if any. */
7717 if (pushed_scope)
7718 pop_scope (pushed_scope);
7719 /* Restore the saved scope. */
7720 parser->scope = saved_scope;
7721 parser->qualifying_scope = saved_qualifying_scope;
7722 parser->object_scope = saved_object_scope;
7723 /* If the TYPE is invalid, indicate failure. */
7724 if (type == error_mark_node)
7725 return error_mark_node;
7726 return mangle_conv_op_name_for_type (type);
7729 /* Parse a conversion-type-id:
7731 conversion-type-id:
7732 type-specifier-seq conversion-declarator [opt]
7734 Returns the TYPE specified. */
7736 static tree
7737 cp_parser_conversion_type_id (cp_parser* parser)
7739 tree attributes;
7740 cp_decl_specifier_seq type_specifiers;
7741 cp_declarator *declarator;
7742 tree type_specified;
7744 /* Parse the attributes. */
7745 attributes = cp_parser_attributes_opt (parser);
7746 /* Parse the type-specifiers. */
7747 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7748 &type_specifiers);
7749 /* If that didn't work, stop. */
7750 if (type_specifiers.type == error_mark_node)
7751 return error_mark_node;
7752 /* Parse the conversion-declarator. */
7753 declarator = cp_parser_conversion_declarator_opt (parser);
7755 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7756 /*initialized=*/0, &attributes);
7757 if (attributes)
7758 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7759 return type_specified;
7762 /* Parse an (optional) conversion-declarator.
7764 conversion-declarator:
7765 ptr-operator conversion-declarator [opt]
7769 static cp_declarator *
7770 cp_parser_conversion_declarator_opt (cp_parser* parser)
7772 enum tree_code code;
7773 tree class_type;
7774 cp_cv_quals cv_quals;
7776 /* We don't know if there's a ptr-operator next, or not. */
7777 cp_parser_parse_tentatively (parser);
7778 /* Try the ptr-operator. */
7779 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7780 /* If it worked, look for more conversion-declarators. */
7781 if (cp_parser_parse_definitely (parser))
7783 cp_declarator *declarator;
7785 /* Parse another optional declarator. */
7786 declarator = cp_parser_conversion_declarator_opt (parser);
7788 /* Create the representation of the declarator. */
7789 if (class_type)
7790 declarator = make_ptrmem_declarator (cv_quals, class_type,
7791 declarator);
7792 else if (code == INDIRECT_REF)
7793 declarator = make_pointer_declarator (cv_quals, declarator);
7794 else
7795 declarator = make_reference_declarator (cv_quals, declarator);
7797 return declarator;
7800 return NULL;
7803 /* Parse an (optional) ctor-initializer.
7805 ctor-initializer:
7806 : mem-initializer-list
7808 Returns TRUE iff the ctor-initializer was actually present. */
7810 static bool
7811 cp_parser_ctor_initializer_opt (cp_parser* parser)
7813 /* If the next token is not a `:', then there is no
7814 ctor-initializer. */
7815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7817 /* Do default initialization of any bases and members. */
7818 if (DECL_CONSTRUCTOR_P (current_function_decl))
7819 finish_mem_initializers (NULL_TREE);
7821 return false;
7824 /* Consume the `:' token. */
7825 cp_lexer_consume_token (parser->lexer);
7826 /* And the mem-initializer-list. */
7827 cp_parser_mem_initializer_list (parser);
7829 return true;
7832 /* Parse a mem-initializer-list.
7834 mem-initializer-list:
7835 mem-initializer
7836 mem-initializer , mem-initializer-list */
7838 static void
7839 cp_parser_mem_initializer_list (cp_parser* parser)
7841 tree mem_initializer_list = NULL_TREE;
7843 /* Let the semantic analysis code know that we are starting the
7844 mem-initializer-list. */
7845 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7846 error ("only constructors take base initializers");
7848 /* Loop through the list. */
7849 while (true)
7851 tree mem_initializer;
7853 /* Parse the mem-initializer. */
7854 mem_initializer = cp_parser_mem_initializer (parser);
7855 /* Add it to the list, unless it was erroneous. */
7856 if (mem_initializer != error_mark_node)
7858 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7859 mem_initializer_list = mem_initializer;
7861 /* If the next token is not a `,', we're done. */
7862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7863 break;
7864 /* Consume the `,' token. */
7865 cp_lexer_consume_token (parser->lexer);
7868 /* Perform semantic analysis. */
7869 if (DECL_CONSTRUCTOR_P (current_function_decl))
7870 finish_mem_initializers (mem_initializer_list);
7873 /* Parse a mem-initializer.
7875 mem-initializer:
7876 mem-initializer-id ( expression-list [opt] )
7878 GNU extension:
7880 mem-initializer:
7881 ( expression-list [opt] )
7883 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7884 class) or FIELD_DECL (for a non-static data member) to initialize;
7885 the TREE_VALUE is the expression-list. An empty initialization
7886 list is represented by void_list_node. */
7888 static tree
7889 cp_parser_mem_initializer (cp_parser* parser)
7891 tree mem_initializer_id;
7892 tree expression_list;
7893 tree member;
7895 /* Find out what is being initialized. */
7896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7898 pedwarn ("anachronistic old-style base class initializer");
7899 mem_initializer_id = NULL_TREE;
7901 else
7902 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7903 member = expand_member_init (mem_initializer_id);
7904 if (member && !DECL_P (member))
7905 in_base_initializer = 1;
7907 expression_list
7908 = cp_parser_parenthesized_expression_list (parser, false,
7909 /*cast_p=*/false,
7910 /*non_constant_p=*/NULL);
7911 if (expression_list == error_mark_node)
7912 return error_mark_node;
7913 if (!expression_list)
7914 expression_list = void_type_node;
7916 in_base_initializer = 0;
7918 return member ? build_tree_list (member, expression_list) : error_mark_node;
7921 /* Parse a mem-initializer-id.
7923 mem-initializer-id:
7924 :: [opt] nested-name-specifier [opt] class-name
7925 identifier
7927 Returns a TYPE indicating the class to be initializer for the first
7928 production. Returns an IDENTIFIER_NODE indicating the data member
7929 to be initialized for the second production. */
7931 static tree
7932 cp_parser_mem_initializer_id (cp_parser* parser)
7934 bool global_scope_p;
7935 bool nested_name_specifier_p;
7936 bool template_p = false;
7937 tree id;
7939 /* `typename' is not allowed in this context ([temp.res]). */
7940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7942 error ("keyword %<typename%> not allowed in this context (a qualified "
7943 "member initializer is implicitly a type)");
7944 cp_lexer_consume_token (parser->lexer);
7946 /* Look for the optional `::' operator. */
7947 global_scope_p
7948 = (cp_parser_global_scope_opt (parser,
7949 /*current_scope_valid_p=*/false)
7950 != NULL_TREE);
7951 /* Look for the optional nested-name-specifier. The simplest way to
7952 implement:
7954 [temp.res]
7956 The keyword `typename' is not permitted in a base-specifier or
7957 mem-initializer; in these contexts a qualified name that
7958 depends on a template-parameter is implicitly assumed to be a
7959 type name.
7961 is to assume that we have seen the `typename' keyword at this
7962 point. */
7963 nested_name_specifier_p
7964 = (cp_parser_nested_name_specifier_opt (parser,
7965 /*typename_keyword_p=*/true,
7966 /*check_dependency_p=*/true,
7967 /*type_p=*/true,
7968 /*is_declaration=*/true)
7969 != NULL_TREE);
7970 if (nested_name_specifier_p)
7971 template_p = cp_parser_optional_template_keyword (parser);
7972 /* If there is a `::' operator or a nested-name-specifier, then we
7973 are definitely looking for a class-name. */
7974 if (global_scope_p || nested_name_specifier_p)
7975 return cp_parser_class_name (parser,
7976 /*typename_keyword_p=*/true,
7977 /*template_keyword_p=*/template_p,
7978 none_type,
7979 /*check_dependency_p=*/true,
7980 /*class_head_p=*/false,
7981 /*is_declaration=*/true);
7982 /* Otherwise, we could also be looking for an ordinary identifier. */
7983 cp_parser_parse_tentatively (parser);
7984 /* Try a class-name. */
7985 id = cp_parser_class_name (parser,
7986 /*typename_keyword_p=*/true,
7987 /*template_keyword_p=*/false,
7988 none_type,
7989 /*check_dependency_p=*/true,
7990 /*class_head_p=*/false,
7991 /*is_declaration=*/true);
7992 /* If we found one, we're done. */
7993 if (cp_parser_parse_definitely (parser))
7994 return id;
7995 /* Otherwise, look for an ordinary identifier. */
7996 return cp_parser_identifier (parser);
7999 /* Overloading [gram.over] */
8001 /* Parse an operator-function-id.
8003 operator-function-id:
8004 operator operator
8006 Returns an IDENTIFIER_NODE for the operator which is a
8007 human-readable spelling of the identifier, e.g., `operator +'. */
8009 static tree
8010 cp_parser_operator_function_id (cp_parser* parser)
8012 /* Look for the `operator' keyword. */
8013 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8014 return error_mark_node;
8015 /* And then the name of the operator itself. */
8016 return cp_parser_operator (parser);
8019 /* Parse an operator.
8021 operator:
8022 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8023 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8024 || ++ -- , ->* -> () []
8026 GNU Extensions:
8028 operator:
8029 <? >? <?= >?=
8031 Returns an IDENTIFIER_NODE for the operator which is a
8032 human-readable spelling of the identifier, e.g., `operator +'. */
8034 static tree
8035 cp_parser_operator (cp_parser* parser)
8037 tree id = NULL_TREE;
8038 cp_token *token;
8040 /* Peek at the next token. */
8041 token = cp_lexer_peek_token (parser->lexer);
8042 /* Figure out which operator we have. */
8043 switch (token->type)
8045 case CPP_KEYWORD:
8047 enum tree_code op;
8049 /* The keyword should be either `new' or `delete'. */
8050 if (token->keyword == RID_NEW)
8051 op = NEW_EXPR;
8052 else if (token->keyword == RID_DELETE)
8053 op = DELETE_EXPR;
8054 else
8055 break;
8057 /* Consume the `new' or `delete' token. */
8058 cp_lexer_consume_token (parser->lexer);
8060 /* Peek at the next token. */
8061 token = cp_lexer_peek_token (parser->lexer);
8062 /* If it's a `[' token then this is the array variant of the
8063 operator. */
8064 if (token->type == CPP_OPEN_SQUARE)
8066 /* Consume the `[' token. */
8067 cp_lexer_consume_token (parser->lexer);
8068 /* Look for the `]' token. */
8069 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8070 id = ansi_opname (op == NEW_EXPR
8071 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8073 /* Otherwise, we have the non-array variant. */
8074 else
8075 id = ansi_opname (op);
8077 return id;
8080 case CPP_PLUS:
8081 id = ansi_opname (PLUS_EXPR);
8082 break;
8084 case CPP_MINUS:
8085 id = ansi_opname (MINUS_EXPR);
8086 break;
8088 case CPP_MULT:
8089 id = ansi_opname (MULT_EXPR);
8090 break;
8092 case CPP_DIV:
8093 id = ansi_opname (TRUNC_DIV_EXPR);
8094 break;
8096 case CPP_MOD:
8097 id = ansi_opname (TRUNC_MOD_EXPR);
8098 break;
8100 case CPP_XOR:
8101 id = ansi_opname (BIT_XOR_EXPR);
8102 break;
8104 case CPP_AND:
8105 id = ansi_opname (BIT_AND_EXPR);
8106 break;
8108 case CPP_OR:
8109 id = ansi_opname (BIT_IOR_EXPR);
8110 break;
8112 case CPP_COMPL:
8113 id = ansi_opname (BIT_NOT_EXPR);
8114 break;
8116 case CPP_NOT:
8117 id = ansi_opname (TRUTH_NOT_EXPR);
8118 break;
8120 case CPP_EQ:
8121 id = ansi_assopname (NOP_EXPR);
8122 break;
8124 case CPP_LESS:
8125 id = ansi_opname (LT_EXPR);
8126 break;
8128 case CPP_GREATER:
8129 id = ansi_opname (GT_EXPR);
8130 break;
8132 case CPP_PLUS_EQ:
8133 id = ansi_assopname (PLUS_EXPR);
8134 break;
8136 case CPP_MINUS_EQ:
8137 id = ansi_assopname (MINUS_EXPR);
8138 break;
8140 case CPP_MULT_EQ:
8141 id = ansi_assopname (MULT_EXPR);
8142 break;
8144 case CPP_DIV_EQ:
8145 id = ansi_assopname (TRUNC_DIV_EXPR);
8146 break;
8148 case CPP_MOD_EQ:
8149 id = ansi_assopname (TRUNC_MOD_EXPR);
8150 break;
8152 case CPP_XOR_EQ:
8153 id = ansi_assopname (BIT_XOR_EXPR);
8154 break;
8156 case CPP_AND_EQ:
8157 id = ansi_assopname (BIT_AND_EXPR);
8158 break;
8160 case CPP_OR_EQ:
8161 id = ansi_assopname (BIT_IOR_EXPR);
8162 break;
8164 case CPP_LSHIFT:
8165 id = ansi_opname (LSHIFT_EXPR);
8166 break;
8168 case CPP_RSHIFT:
8169 id = ansi_opname (RSHIFT_EXPR);
8170 break;
8172 case CPP_LSHIFT_EQ:
8173 id = ansi_assopname (LSHIFT_EXPR);
8174 break;
8176 case CPP_RSHIFT_EQ:
8177 id = ansi_assopname (RSHIFT_EXPR);
8178 break;
8180 case CPP_EQ_EQ:
8181 id = ansi_opname (EQ_EXPR);
8182 break;
8184 case CPP_NOT_EQ:
8185 id = ansi_opname (NE_EXPR);
8186 break;
8188 case CPP_LESS_EQ:
8189 id = ansi_opname (LE_EXPR);
8190 break;
8192 case CPP_GREATER_EQ:
8193 id = ansi_opname (GE_EXPR);
8194 break;
8196 case CPP_AND_AND:
8197 id = ansi_opname (TRUTH_ANDIF_EXPR);
8198 break;
8200 case CPP_OR_OR:
8201 id = ansi_opname (TRUTH_ORIF_EXPR);
8202 break;
8204 case CPP_PLUS_PLUS:
8205 id = ansi_opname (POSTINCREMENT_EXPR);
8206 break;
8208 case CPP_MINUS_MINUS:
8209 id = ansi_opname (PREDECREMENT_EXPR);
8210 break;
8212 case CPP_COMMA:
8213 id = ansi_opname (COMPOUND_EXPR);
8214 break;
8216 case CPP_DEREF_STAR:
8217 id = ansi_opname (MEMBER_REF);
8218 break;
8220 case CPP_DEREF:
8221 id = ansi_opname (COMPONENT_REF);
8222 break;
8224 case CPP_OPEN_PAREN:
8225 /* Consume the `('. */
8226 cp_lexer_consume_token (parser->lexer);
8227 /* Look for the matching `)'. */
8228 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8229 return ansi_opname (CALL_EXPR);
8231 case CPP_OPEN_SQUARE:
8232 /* Consume the `['. */
8233 cp_lexer_consume_token (parser->lexer);
8234 /* Look for the matching `]'. */
8235 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8236 return ansi_opname (ARRAY_REF);
8238 /* Extensions. */
8239 case CPP_MIN:
8240 id = ansi_opname (MIN_EXPR);
8241 cp_parser_warn_min_max ();
8242 break;
8244 case CPP_MAX:
8245 id = ansi_opname (MAX_EXPR);
8246 cp_parser_warn_min_max ();
8247 break;
8249 case CPP_MIN_EQ:
8250 id = ansi_assopname (MIN_EXPR);
8251 cp_parser_warn_min_max ();
8252 break;
8254 case CPP_MAX_EQ:
8255 id = ansi_assopname (MAX_EXPR);
8256 cp_parser_warn_min_max ();
8257 break;
8259 default:
8260 /* Anything else is an error. */
8261 break;
8264 /* If we have selected an identifier, we need to consume the
8265 operator token. */
8266 if (id)
8267 cp_lexer_consume_token (parser->lexer);
8268 /* Otherwise, no valid operator name was present. */
8269 else
8271 cp_parser_error (parser, "expected operator");
8272 id = error_mark_node;
8275 return id;
8278 /* Parse a template-declaration.
8280 template-declaration:
8281 export [opt] template < template-parameter-list > declaration
8283 If MEMBER_P is TRUE, this template-declaration occurs within a
8284 class-specifier.
8286 The grammar rule given by the standard isn't correct. What
8287 is really meant is:
8289 template-declaration:
8290 export [opt] template-parameter-list-seq
8291 decl-specifier-seq [opt] init-declarator [opt] ;
8292 export [opt] template-parameter-list-seq
8293 function-definition
8295 template-parameter-list-seq:
8296 template-parameter-list-seq [opt]
8297 template < template-parameter-list > */
8299 static void
8300 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8302 /* Check for `export'. */
8303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8305 /* Consume the `export' token. */
8306 cp_lexer_consume_token (parser->lexer);
8307 /* Warn that we do not support `export'. */
8308 warning (0, "keyword %<export%> not implemented, and will be ignored");
8311 cp_parser_template_declaration_after_export (parser, member_p);
8314 /* Parse a template-parameter-list.
8316 template-parameter-list:
8317 template-parameter
8318 template-parameter-list , template-parameter
8320 Returns a TREE_LIST. Each node represents a template parameter.
8321 The nodes are connected via their TREE_CHAINs. */
8323 static tree
8324 cp_parser_template_parameter_list (cp_parser* parser)
8326 tree parameter_list = NULL_TREE;
8328 begin_template_parm_list ();
8329 while (true)
8331 tree parameter;
8332 cp_token *token;
8333 bool is_non_type;
8335 /* Parse the template-parameter. */
8336 parameter = cp_parser_template_parameter (parser, &is_non_type);
8337 /* Add it to the list. */
8338 if (parameter != error_mark_node)
8339 parameter_list = process_template_parm (parameter_list,
8340 parameter,
8341 is_non_type);
8342 /* Peek at the next token. */
8343 token = cp_lexer_peek_token (parser->lexer);
8344 /* If it's not a `,', we're done. */
8345 if (token->type != CPP_COMMA)
8346 break;
8347 /* Otherwise, consume the `,' token. */
8348 cp_lexer_consume_token (parser->lexer);
8351 return end_template_parm_list (parameter_list);
8354 /* Parse a template-parameter.
8356 template-parameter:
8357 type-parameter
8358 parameter-declaration
8360 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8361 the parameter. The TREE_PURPOSE is the default value, if any.
8362 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8363 iff this parameter is a non-type parameter. */
8365 static tree
8366 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8368 cp_token *token;
8369 cp_parameter_declarator *parameter_declarator;
8370 tree parm;
8372 /* Assume it is a type parameter or a template parameter. */
8373 *is_non_type = false;
8374 /* Peek at the next token. */
8375 token = cp_lexer_peek_token (parser->lexer);
8376 /* If it is `class' or `template', we have a type-parameter. */
8377 if (token->keyword == RID_TEMPLATE)
8378 return cp_parser_type_parameter (parser);
8379 /* If it is `class' or `typename' we do not know yet whether it is a
8380 type parameter or a non-type parameter. Consider:
8382 template <typename T, typename T::X X> ...
8386 template <class C, class D*> ...
8388 Here, the first parameter is a type parameter, and the second is
8389 a non-type parameter. We can tell by looking at the token after
8390 the identifier -- if it is a `,', `=', or `>' then we have a type
8391 parameter. */
8392 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8394 /* Peek at the token after `class' or `typename'. */
8395 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8396 /* If it's an identifier, skip it. */
8397 if (token->type == CPP_NAME)
8398 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8399 /* Now, see if the token looks like the end of a template
8400 parameter. */
8401 if (token->type == CPP_COMMA
8402 || token->type == CPP_EQ
8403 || token->type == CPP_GREATER)
8404 return cp_parser_type_parameter (parser);
8407 /* Otherwise, it is a non-type parameter.
8409 [temp.param]
8411 When parsing a default template-argument for a non-type
8412 template-parameter, the first non-nested `>' is taken as the end
8413 of the template parameter-list rather than a greater-than
8414 operator. */
8415 *is_non_type = true;
8416 parameter_declarator
8417 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8418 /*parenthesized_p=*/NULL);
8419 parm = grokdeclarator (parameter_declarator->declarator,
8420 &parameter_declarator->decl_specifiers,
8421 PARM, /*initialized=*/0,
8422 /*attrlist=*/NULL);
8423 if (parm == error_mark_node)
8424 return error_mark_node;
8425 return build_tree_list (parameter_declarator->default_argument, parm);
8428 /* Parse a type-parameter.
8430 type-parameter:
8431 class identifier [opt]
8432 class identifier [opt] = type-id
8433 typename identifier [opt]
8434 typename identifier [opt] = type-id
8435 template < template-parameter-list > class identifier [opt]
8436 template < template-parameter-list > class identifier [opt]
8437 = id-expression
8439 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8440 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8441 the declaration of the parameter. */
8443 static tree
8444 cp_parser_type_parameter (cp_parser* parser)
8446 cp_token *token;
8447 tree parameter;
8449 /* Look for a keyword to tell us what kind of parameter this is. */
8450 token = cp_parser_require (parser, CPP_KEYWORD,
8451 "`class', `typename', or `template'");
8452 if (!token)
8453 return error_mark_node;
8455 switch (token->keyword)
8457 case RID_CLASS:
8458 case RID_TYPENAME:
8460 tree identifier;
8461 tree default_argument;
8463 /* If the next token is an identifier, then it names the
8464 parameter. */
8465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8466 identifier = cp_parser_identifier (parser);
8467 else
8468 identifier = NULL_TREE;
8470 /* Create the parameter. */
8471 parameter = finish_template_type_parm (class_type_node, identifier);
8473 /* If the next token is an `=', we have a default argument. */
8474 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8476 /* Consume the `=' token. */
8477 cp_lexer_consume_token (parser->lexer);
8478 /* Parse the default-argument. */
8479 default_argument = cp_parser_type_id (parser);
8481 else
8482 default_argument = NULL_TREE;
8484 /* Create the combined representation of the parameter and the
8485 default argument. */
8486 parameter = build_tree_list (default_argument, parameter);
8488 break;
8490 case RID_TEMPLATE:
8492 tree parameter_list;
8493 tree identifier;
8494 tree default_argument;
8496 /* Look for the `<'. */
8497 cp_parser_require (parser, CPP_LESS, "`<'");
8498 /* Parse the template-parameter-list. */
8499 parameter_list = cp_parser_template_parameter_list (parser);
8500 /* Look for the `>'. */
8501 cp_parser_require (parser, CPP_GREATER, "`>'");
8502 /* Look for the `class' keyword. */
8503 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8504 /* If the next token is an `=', then there is a
8505 default-argument. If the next token is a `>', we are at
8506 the end of the parameter-list. If the next token is a `,',
8507 then we are at the end of this parameter. */
8508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8509 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8510 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8512 identifier = cp_parser_identifier (parser);
8513 /* Treat invalid names as if the parameter were nameless. */
8514 if (identifier == error_mark_node)
8515 identifier = NULL_TREE;
8517 else
8518 identifier = NULL_TREE;
8520 /* Create the template parameter. */
8521 parameter = finish_template_template_parm (class_type_node,
8522 identifier);
8524 /* If the next token is an `=', then there is a
8525 default-argument. */
8526 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8528 bool is_template;
8530 /* Consume the `='. */
8531 cp_lexer_consume_token (parser->lexer);
8532 /* Parse the id-expression. */
8533 default_argument
8534 = cp_parser_id_expression (parser,
8535 /*template_keyword_p=*/false,
8536 /*check_dependency_p=*/true,
8537 /*template_p=*/&is_template,
8538 /*declarator_p=*/false);
8539 if (TREE_CODE (default_argument) == TYPE_DECL)
8540 /* If the id-expression was a template-id that refers to
8541 a template-class, we already have the declaration here,
8542 so no further lookup is needed. */
8544 else
8545 /* Look up the name. */
8546 default_argument
8547 = cp_parser_lookup_name (parser, default_argument,
8548 none_type,
8549 /*is_template=*/is_template,
8550 /*is_namespace=*/false,
8551 /*check_dependency=*/true,
8552 /*ambiguous_decls=*/NULL);
8553 /* See if the default argument is valid. */
8554 default_argument
8555 = check_template_template_default_arg (default_argument);
8557 else
8558 default_argument = NULL_TREE;
8560 /* Create the combined representation of the parameter and the
8561 default argument. */
8562 parameter = build_tree_list (default_argument, parameter);
8564 break;
8566 default:
8567 gcc_unreachable ();
8568 break;
8571 return parameter;
8574 /* Parse a template-id.
8576 template-id:
8577 template-name < template-argument-list [opt] >
8579 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8580 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8581 returned. Otherwise, if the template-name names a function, or set
8582 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8583 names a class, returns a TYPE_DECL for the specialization.
8585 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8586 uninstantiated templates. */
8588 static tree
8589 cp_parser_template_id (cp_parser *parser,
8590 bool template_keyword_p,
8591 bool check_dependency_p,
8592 bool is_declaration)
8594 tree template;
8595 tree arguments;
8596 tree template_id;
8597 cp_token_position start_of_id = 0;
8598 tree access_check = NULL_TREE;
8599 cp_token *next_token, *next_token_2;
8600 bool is_identifier;
8602 /* If the next token corresponds to a template-id, there is no need
8603 to reparse it. */
8604 next_token = cp_lexer_peek_token (parser->lexer);
8605 if (next_token->type == CPP_TEMPLATE_ID)
8607 tree value;
8608 tree check;
8610 /* Get the stored value. */
8611 value = cp_lexer_consume_token (parser->lexer)->value;
8612 /* Perform any access checks that were deferred. */
8613 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8614 perform_or_defer_access_check (TREE_PURPOSE (check),
8615 TREE_VALUE (check));
8616 /* Return the stored value. */
8617 return TREE_VALUE (value);
8620 /* Avoid performing name lookup if there is no possibility of
8621 finding a template-id. */
8622 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8623 || (next_token->type == CPP_NAME
8624 && !cp_parser_nth_token_starts_template_argument_list_p
8625 (parser, 2)))
8627 cp_parser_error (parser, "expected template-id");
8628 return error_mark_node;
8631 /* Remember where the template-id starts. */
8632 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8633 start_of_id = cp_lexer_token_position (parser->lexer, false);
8635 push_deferring_access_checks (dk_deferred);
8637 /* Parse the template-name. */
8638 is_identifier = false;
8639 template = cp_parser_template_name (parser, template_keyword_p,
8640 check_dependency_p,
8641 is_declaration,
8642 &is_identifier);
8643 if (template == error_mark_node || is_identifier)
8645 pop_deferring_access_checks ();
8646 return template;
8649 /* If we find the sequence `[:' after a template-name, it's probably
8650 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8651 parse correctly the argument list. */
8652 next_token = cp_lexer_peek_token (parser->lexer);
8653 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8654 if (next_token->type == CPP_OPEN_SQUARE
8655 && next_token->flags & DIGRAPH
8656 && next_token_2->type == CPP_COLON
8657 && !(next_token_2->flags & PREV_WHITE))
8659 cp_parser_parse_tentatively (parser);
8660 /* Change `:' into `::'. */
8661 next_token_2->type = CPP_SCOPE;
8662 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8663 CPP_LESS. */
8664 cp_lexer_consume_token (parser->lexer);
8665 /* Parse the arguments. */
8666 arguments = cp_parser_enclosed_template_argument_list (parser);
8667 if (!cp_parser_parse_definitely (parser))
8669 /* If we couldn't parse an argument list, then we revert our changes
8670 and return simply an error. Maybe this is not a template-id
8671 after all. */
8672 next_token_2->type = CPP_COLON;
8673 cp_parser_error (parser, "expected %<<%>");
8674 pop_deferring_access_checks ();
8675 return error_mark_node;
8677 /* Otherwise, emit an error about the invalid digraph, but continue
8678 parsing because we got our argument list. */
8679 pedwarn ("%<<::%> cannot begin a template-argument list");
8680 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8681 "between %<<%> and %<::%>");
8682 if (!flag_permissive)
8684 static bool hint;
8685 if (!hint)
8687 inform ("(if you use -fpermissive G++ will accept your code)");
8688 hint = true;
8692 else
8694 /* Look for the `<' that starts the template-argument-list. */
8695 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8697 pop_deferring_access_checks ();
8698 return error_mark_node;
8700 /* Parse the arguments. */
8701 arguments = cp_parser_enclosed_template_argument_list (parser);
8704 /* Build a representation of the specialization. */
8705 if (TREE_CODE (template) == IDENTIFIER_NODE)
8706 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8707 else if (DECL_CLASS_TEMPLATE_P (template)
8708 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8709 template_id
8710 = finish_template_type (template, arguments,
8711 cp_lexer_next_token_is (parser->lexer,
8712 CPP_SCOPE));
8713 else
8715 /* If it's not a class-template or a template-template, it should be
8716 a function-template. */
8717 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8718 || TREE_CODE (template) == OVERLOAD
8719 || BASELINK_P (template)));
8721 template_id = lookup_template_function (template, arguments);
8724 /* Retrieve any deferred checks. Do not pop this access checks yet
8725 so the memory will not be reclaimed during token replacing below. */
8726 access_check = get_deferred_access_checks ();
8728 /* If parsing tentatively, replace the sequence of tokens that makes
8729 up the template-id with a CPP_TEMPLATE_ID token. That way,
8730 should we re-parse the token stream, we will not have to repeat
8731 the effort required to do the parse, nor will we issue duplicate
8732 error messages about problems during instantiation of the
8733 template. */
8734 if (start_of_id)
8736 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8738 /* Reset the contents of the START_OF_ID token. */
8739 token->type = CPP_TEMPLATE_ID;
8740 token->value = build_tree_list (access_check, template_id);
8741 token->keyword = RID_MAX;
8743 /* Purge all subsequent tokens. */
8744 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8746 /* ??? Can we actually assume that, if template_id ==
8747 error_mark_node, we will have issued a diagnostic to the
8748 user, as opposed to simply marking the tentative parse as
8749 failed? */
8750 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8751 error ("parse error in template argument list");
8754 pop_deferring_access_checks ();
8755 return template_id;
8758 /* Parse a template-name.
8760 template-name:
8761 identifier
8763 The standard should actually say:
8765 template-name:
8766 identifier
8767 operator-function-id
8769 A defect report has been filed about this issue.
8771 A conversion-function-id cannot be a template name because they cannot
8772 be part of a template-id. In fact, looking at this code:
8774 a.operator K<int>()
8776 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8777 It is impossible to call a templated conversion-function-id with an
8778 explicit argument list, since the only allowed template parameter is
8779 the type to which it is converting.
8781 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8782 `template' keyword, in a construction like:
8784 T::template f<3>()
8786 In that case `f' is taken to be a template-name, even though there
8787 is no way of knowing for sure.
8789 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8790 name refers to a set of overloaded functions, at least one of which
8791 is a template, or an IDENTIFIER_NODE with the name of the template,
8792 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8793 names are looked up inside uninstantiated templates. */
8795 static tree
8796 cp_parser_template_name (cp_parser* parser,
8797 bool template_keyword_p,
8798 bool check_dependency_p,
8799 bool is_declaration,
8800 bool *is_identifier)
8802 tree identifier;
8803 tree decl;
8804 tree fns;
8806 /* If the next token is `operator', then we have either an
8807 operator-function-id or a conversion-function-id. */
8808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8810 /* We don't know whether we're looking at an
8811 operator-function-id or a conversion-function-id. */
8812 cp_parser_parse_tentatively (parser);
8813 /* Try an operator-function-id. */
8814 identifier = cp_parser_operator_function_id (parser);
8815 /* If that didn't work, try a conversion-function-id. */
8816 if (!cp_parser_parse_definitely (parser))
8818 cp_parser_error (parser, "expected template-name");
8819 return error_mark_node;
8822 /* Look for the identifier. */
8823 else
8824 identifier = cp_parser_identifier (parser);
8826 /* If we didn't find an identifier, we don't have a template-id. */
8827 if (identifier == error_mark_node)
8828 return error_mark_node;
8830 /* If the name immediately followed the `template' keyword, then it
8831 is a template-name. However, if the next token is not `<', then
8832 we do not treat it as a template-name, since it is not being used
8833 as part of a template-id. This enables us to handle constructs
8834 like:
8836 template <typename T> struct S { S(); };
8837 template <typename T> S<T>::S();
8839 correctly. We would treat `S' as a template -- if it were `S<T>'
8840 -- but we do not if there is no `<'. */
8842 if (processing_template_decl
8843 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8845 /* In a declaration, in a dependent context, we pretend that the
8846 "template" keyword was present in order to improve error
8847 recovery. For example, given:
8849 template <typename T> void f(T::X<int>);
8851 we want to treat "X<int>" as a template-id. */
8852 if (is_declaration
8853 && !template_keyword_p
8854 && parser->scope && TYPE_P (parser->scope)
8855 && check_dependency_p
8856 && dependent_type_p (parser->scope)
8857 /* Do not do this for dtors (or ctors), since they never
8858 need the template keyword before their name. */
8859 && !constructor_name_p (identifier, parser->scope))
8861 cp_token_position start = 0;
8863 /* Explain what went wrong. */
8864 error ("non-template %qD used as template", identifier);
8865 inform ("use %<%T::template %D%> to indicate that it is a template",
8866 parser->scope, identifier);
8867 /* If parsing tentatively, find the location of the "<" token. */
8868 if (cp_parser_simulate_error (parser))
8869 start = cp_lexer_token_position (parser->lexer, true);
8870 /* Parse the template arguments so that we can issue error
8871 messages about them. */
8872 cp_lexer_consume_token (parser->lexer);
8873 cp_parser_enclosed_template_argument_list (parser);
8874 /* Skip tokens until we find a good place from which to
8875 continue parsing. */
8876 cp_parser_skip_to_closing_parenthesis (parser,
8877 /*recovering=*/true,
8878 /*or_comma=*/true,
8879 /*consume_paren=*/false);
8880 /* If parsing tentatively, permanently remove the
8881 template argument list. That will prevent duplicate
8882 error messages from being issued about the missing
8883 "template" keyword. */
8884 if (start)
8885 cp_lexer_purge_tokens_after (parser->lexer, start);
8886 if (is_identifier)
8887 *is_identifier = true;
8888 return identifier;
8891 /* If the "template" keyword is present, then there is generally
8892 no point in doing name-lookup, so we just return IDENTIFIER.
8893 But, if the qualifying scope is non-dependent then we can
8894 (and must) do name-lookup normally. */
8895 if (template_keyword_p
8896 && (!parser->scope
8897 || (TYPE_P (parser->scope)
8898 && dependent_type_p (parser->scope))))
8899 return identifier;
8902 /* Look up the name. */
8903 decl = cp_parser_lookup_name (parser, identifier,
8904 none_type,
8905 /*is_template=*/false,
8906 /*is_namespace=*/false,
8907 check_dependency_p,
8908 /*ambiguous_decls=*/NULL);
8909 decl = maybe_get_template_decl_from_type_decl (decl);
8911 /* If DECL is a template, then the name was a template-name. */
8912 if (TREE_CODE (decl) == TEMPLATE_DECL)
8914 else
8916 tree fn = NULL_TREE;
8918 /* The standard does not explicitly indicate whether a name that
8919 names a set of overloaded declarations, some of which are
8920 templates, is a template-name. However, such a name should
8921 be a template-name; otherwise, there is no way to form a
8922 template-id for the overloaded templates. */
8923 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8924 if (TREE_CODE (fns) == OVERLOAD)
8925 for (fn = fns; fn; fn = OVL_NEXT (fn))
8926 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8927 break;
8929 if (!fn)
8931 /* The name does not name a template. */
8932 cp_parser_error (parser, "expected template-name");
8933 return error_mark_node;
8937 /* If DECL is dependent, and refers to a function, then just return
8938 its name; we will look it up again during template instantiation. */
8939 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8941 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8942 if (TYPE_P (scope) && dependent_type_p (scope))
8943 return identifier;
8946 return decl;
8949 /* Parse a template-argument-list.
8951 template-argument-list:
8952 template-argument
8953 template-argument-list , template-argument
8955 Returns a TREE_VEC containing the arguments. */
8957 static tree
8958 cp_parser_template_argument_list (cp_parser* parser)
8960 tree fixed_args[10];
8961 unsigned n_args = 0;
8962 unsigned alloced = 10;
8963 tree *arg_ary = fixed_args;
8964 tree vec;
8965 bool saved_in_template_argument_list_p;
8966 bool saved_ice_p;
8967 bool saved_non_ice_p;
8969 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8970 parser->in_template_argument_list_p = true;
8971 /* Even if the template-id appears in an integral
8972 constant-expression, the contents of the argument list do
8973 not. */
8974 saved_ice_p = parser->integral_constant_expression_p;
8975 parser->integral_constant_expression_p = false;
8976 saved_non_ice_p = parser->non_integral_constant_expression_p;
8977 parser->non_integral_constant_expression_p = false;
8978 /* Parse the arguments. */
8981 tree argument;
8983 if (n_args)
8984 /* Consume the comma. */
8985 cp_lexer_consume_token (parser->lexer);
8987 /* Parse the template-argument. */
8988 argument = cp_parser_template_argument (parser);
8989 if (n_args == alloced)
8991 alloced *= 2;
8993 if (arg_ary == fixed_args)
8995 arg_ary = XNEWVEC (tree, alloced);
8996 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8998 else
8999 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9001 arg_ary[n_args++] = argument;
9003 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9005 vec = make_tree_vec (n_args);
9007 while (n_args--)
9008 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9010 if (arg_ary != fixed_args)
9011 free (arg_ary);
9012 parser->non_integral_constant_expression_p = saved_non_ice_p;
9013 parser->integral_constant_expression_p = saved_ice_p;
9014 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9015 return vec;
9018 /* Parse a template-argument.
9020 template-argument:
9021 assignment-expression
9022 type-id
9023 id-expression
9025 The representation is that of an assignment-expression, type-id, or
9026 id-expression -- except that the qualified id-expression is
9027 evaluated, so that the value returned is either a DECL or an
9028 OVERLOAD.
9030 Although the standard says "assignment-expression", it forbids
9031 throw-expressions or assignments in the template argument.
9032 Therefore, we use "conditional-expression" instead. */
9034 static tree
9035 cp_parser_template_argument (cp_parser* parser)
9037 tree argument;
9038 bool template_p;
9039 bool address_p;
9040 bool maybe_type_id = false;
9041 cp_token *token;
9042 cp_id_kind idk;
9044 /* There's really no way to know what we're looking at, so we just
9045 try each alternative in order.
9047 [temp.arg]
9049 In a template-argument, an ambiguity between a type-id and an
9050 expression is resolved to a type-id, regardless of the form of
9051 the corresponding template-parameter.
9053 Therefore, we try a type-id first. */
9054 cp_parser_parse_tentatively (parser);
9055 argument = cp_parser_type_id (parser);
9056 /* If there was no error parsing the type-id but the next token is a '>>',
9057 we probably found a typo for '> >'. But there are type-id which are
9058 also valid expressions. For instance:
9060 struct X { int operator >> (int); };
9061 template <int V> struct Foo {};
9062 Foo<X () >> 5> r;
9064 Here 'X()' is a valid type-id of a function type, but the user just
9065 wanted to write the expression "X() >> 5". Thus, we remember that we
9066 found a valid type-id, but we still try to parse the argument as an
9067 expression to see what happens. */
9068 if (!cp_parser_error_occurred (parser)
9069 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9071 maybe_type_id = true;
9072 cp_parser_abort_tentative_parse (parser);
9074 else
9076 /* If the next token isn't a `,' or a `>', then this argument wasn't
9077 really finished. This means that the argument is not a valid
9078 type-id. */
9079 if (!cp_parser_next_token_ends_template_argument_p (parser))
9080 cp_parser_error (parser, "expected template-argument");
9081 /* If that worked, we're done. */
9082 if (cp_parser_parse_definitely (parser))
9083 return argument;
9085 /* We're still not sure what the argument will be. */
9086 cp_parser_parse_tentatively (parser);
9087 /* Try a template. */
9088 argument = cp_parser_id_expression (parser,
9089 /*template_keyword_p=*/false,
9090 /*check_dependency_p=*/true,
9091 &template_p,
9092 /*declarator_p=*/false);
9093 /* If the next token isn't a `,' or a `>', then this argument wasn't
9094 really finished. */
9095 if (!cp_parser_next_token_ends_template_argument_p (parser))
9096 cp_parser_error (parser, "expected template-argument");
9097 if (!cp_parser_error_occurred (parser))
9099 /* Figure out what is being referred to. If the id-expression
9100 was for a class template specialization, then we will have a
9101 TYPE_DECL at this point. There is no need to do name lookup
9102 at this point in that case. */
9103 if (TREE_CODE (argument) != TYPE_DECL)
9104 argument = cp_parser_lookup_name (parser, argument,
9105 none_type,
9106 /*is_template=*/template_p,
9107 /*is_namespace=*/false,
9108 /*check_dependency=*/true,
9109 /*ambiguous_decls=*/NULL);
9110 if (TREE_CODE (argument) != TEMPLATE_DECL
9111 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9112 cp_parser_error (parser, "expected template-name");
9114 if (cp_parser_parse_definitely (parser))
9115 return argument;
9116 /* It must be a non-type argument. There permitted cases are given
9117 in [temp.arg.nontype]:
9119 -- an integral constant-expression of integral or enumeration
9120 type; or
9122 -- the name of a non-type template-parameter; or
9124 -- the name of an object or function with external linkage...
9126 -- the address of an object or function with external linkage...
9128 -- a pointer to member... */
9129 /* Look for a non-type template parameter. */
9130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9132 cp_parser_parse_tentatively (parser);
9133 argument = cp_parser_primary_expression (parser,
9134 /*adress_p=*/false,
9135 /*cast_p=*/false,
9136 /*template_arg_p=*/true,
9137 &idk);
9138 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9139 || !cp_parser_next_token_ends_template_argument_p (parser))
9140 cp_parser_simulate_error (parser);
9141 if (cp_parser_parse_definitely (parser))
9142 return argument;
9145 /* If the next token is "&", the argument must be the address of an
9146 object or function with external linkage. */
9147 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9148 if (address_p)
9149 cp_lexer_consume_token (parser->lexer);
9150 /* See if we might have an id-expression. */
9151 token = cp_lexer_peek_token (parser->lexer);
9152 if (token->type == CPP_NAME
9153 || token->keyword == RID_OPERATOR
9154 || token->type == CPP_SCOPE
9155 || token->type == CPP_TEMPLATE_ID
9156 || token->type == CPP_NESTED_NAME_SPECIFIER)
9158 cp_parser_parse_tentatively (parser);
9159 argument = cp_parser_primary_expression (parser,
9160 address_p,
9161 /*cast_p=*/false,
9162 /*template_arg_p=*/true,
9163 &idk);
9164 if (cp_parser_error_occurred (parser)
9165 || !cp_parser_next_token_ends_template_argument_p (parser))
9166 cp_parser_abort_tentative_parse (parser);
9167 else
9169 if (TREE_CODE (argument) == INDIRECT_REF)
9171 gcc_assert (REFERENCE_REF_P (argument));
9172 argument = TREE_OPERAND (argument, 0);
9175 if (TREE_CODE (argument) == BASELINK)
9176 /* We don't need the information about what class was used
9177 to name the overloaded functions. */
9178 argument = BASELINK_FUNCTIONS (argument);
9180 if (TREE_CODE (argument) == VAR_DECL)
9182 /* A variable without external linkage might still be a
9183 valid constant-expression, so no error is issued here
9184 if the external-linkage check fails. */
9185 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9186 cp_parser_simulate_error (parser);
9188 else if (is_overloaded_fn (argument))
9189 /* All overloaded functions are allowed; if the external
9190 linkage test does not pass, an error will be issued
9191 later. */
9193 else if (address_p
9194 && (TREE_CODE (argument) == OFFSET_REF
9195 || TREE_CODE (argument) == SCOPE_REF))
9196 /* A pointer-to-member. */
9198 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9200 else
9201 cp_parser_simulate_error (parser);
9203 if (cp_parser_parse_definitely (parser))
9205 if (address_p)
9206 argument = build_x_unary_op (ADDR_EXPR, argument);
9207 return argument;
9211 /* If the argument started with "&", there are no other valid
9212 alternatives at this point. */
9213 if (address_p)
9215 cp_parser_error (parser, "invalid non-type template argument");
9216 return error_mark_node;
9219 /* If the argument wasn't successfully parsed as a type-id followed
9220 by '>>', the argument can only be a constant expression now.
9221 Otherwise, we try parsing the constant-expression tentatively,
9222 because the argument could really be a type-id. */
9223 if (maybe_type_id)
9224 cp_parser_parse_tentatively (parser);
9225 argument = cp_parser_constant_expression (parser,
9226 /*allow_non_constant_p=*/false,
9227 /*non_constant_p=*/NULL);
9228 argument = fold_non_dependent_expr (argument);
9229 if (!maybe_type_id)
9230 return argument;
9231 if (!cp_parser_next_token_ends_template_argument_p (parser))
9232 cp_parser_error (parser, "expected template-argument");
9233 if (cp_parser_parse_definitely (parser))
9234 return argument;
9235 /* We did our best to parse the argument as a non type-id, but that
9236 was the only alternative that matched (albeit with a '>' after
9237 it). We can assume it's just a typo from the user, and a
9238 diagnostic will then be issued. */
9239 return cp_parser_type_id (parser);
9242 /* Parse an explicit-instantiation.
9244 explicit-instantiation:
9245 template declaration
9247 Although the standard says `declaration', what it really means is:
9249 explicit-instantiation:
9250 template decl-specifier-seq [opt] declarator [opt] ;
9252 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9253 supposed to be allowed. A defect report has been filed about this
9254 issue.
9256 GNU Extension:
9258 explicit-instantiation:
9259 storage-class-specifier template
9260 decl-specifier-seq [opt] declarator [opt] ;
9261 function-specifier template
9262 decl-specifier-seq [opt] declarator [opt] ; */
9264 static void
9265 cp_parser_explicit_instantiation (cp_parser* parser)
9267 int declares_class_or_enum;
9268 cp_decl_specifier_seq decl_specifiers;
9269 tree extension_specifier = NULL_TREE;
9271 /* Look for an (optional) storage-class-specifier or
9272 function-specifier. */
9273 if (cp_parser_allow_gnu_extensions_p (parser))
9275 extension_specifier
9276 = cp_parser_storage_class_specifier_opt (parser);
9277 if (!extension_specifier)
9278 extension_specifier
9279 = cp_parser_function_specifier_opt (parser,
9280 /*decl_specs=*/NULL);
9283 /* Look for the `template' keyword. */
9284 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9285 /* Let the front end know that we are processing an explicit
9286 instantiation. */
9287 begin_explicit_instantiation ();
9288 /* [temp.explicit] says that we are supposed to ignore access
9289 control while processing explicit instantiation directives. */
9290 push_deferring_access_checks (dk_no_check);
9291 /* Parse a decl-specifier-seq. */
9292 cp_parser_decl_specifier_seq (parser,
9293 CP_PARSER_FLAGS_OPTIONAL,
9294 &decl_specifiers,
9295 &declares_class_or_enum);
9296 /* If there was exactly one decl-specifier, and it declared a class,
9297 and there's no declarator, then we have an explicit type
9298 instantiation. */
9299 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9301 tree type;
9303 type = check_tag_decl (&decl_specifiers);
9304 /* Turn access control back on for names used during
9305 template instantiation. */
9306 pop_deferring_access_checks ();
9307 if (type)
9308 do_type_instantiation (type, extension_specifier,
9309 /*complain=*/tf_error);
9311 else
9313 cp_declarator *declarator;
9314 tree decl;
9316 /* Parse the declarator. */
9317 declarator
9318 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9319 /*ctor_dtor_or_conv_p=*/NULL,
9320 /*parenthesized_p=*/NULL,
9321 /*member_p=*/false);
9322 if (declares_class_or_enum & 2)
9323 cp_parser_check_for_definition_in_return_type (declarator,
9324 decl_specifiers.type);
9325 if (declarator != cp_error_declarator)
9327 decl = grokdeclarator (declarator, &decl_specifiers,
9328 NORMAL, 0, NULL);
9329 /* Turn access control back on for names used during
9330 template instantiation. */
9331 pop_deferring_access_checks ();
9332 /* Do the explicit instantiation. */
9333 do_decl_instantiation (decl, extension_specifier);
9335 else
9337 pop_deferring_access_checks ();
9338 /* Skip the body of the explicit instantiation. */
9339 cp_parser_skip_to_end_of_statement (parser);
9342 /* We're done with the instantiation. */
9343 end_explicit_instantiation ();
9345 cp_parser_consume_semicolon_at_end_of_statement (parser);
9348 /* Parse an explicit-specialization.
9350 explicit-specialization:
9351 template < > declaration
9353 Although the standard says `declaration', what it really means is:
9355 explicit-specialization:
9356 template <> decl-specifier [opt] init-declarator [opt] ;
9357 template <> function-definition
9358 template <> explicit-specialization
9359 template <> template-declaration */
9361 static void
9362 cp_parser_explicit_specialization (cp_parser* parser)
9364 bool need_lang_pop;
9365 /* Look for the `template' keyword. */
9366 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9367 /* Look for the `<'. */
9368 cp_parser_require (parser, CPP_LESS, "`<'");
9369 /* Look for the `>'. */
9370 cp_parser_require (parser, CPP_GREATER, "`>'");
9371 /* We have processed another parameter list. */
9372 ++parser->num_template_parameter_lists;
9373 /* [temp]
9375 A template ... explicit specialization ... shall not have C
9376 linkage. */
9377 if (current_lang_name == lang_name_c)
9379 error ("template specialization with C linkage");
9380 /* Give it C++ linkage to avoid confusing other parts of the
9381 front end. */
9382 push_lang_context (lang_name_cplusplus);
9383 need_lang_pop = true;
9385 else
9386 need_lang_pop = false;
9387 /* Let the front end know that we are beginning a specialization. */
9388 begin_specialization ();
9389 /* If the next keyword is `template', we need to figure out whether
9390 or not we're looking a template-declaration. */
9391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9393 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9394 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9395 cp_parser_template_declaration_after_export (parser,
9396 /*member_p=*/false);
9397 else
9398 cp_parser_explicit_specialization (parser);
9400 else
9401 /* Parse the dependent declaration. */
9402 cp_parser_single_declaration (parser,
9403 /*member_p=*/false,
9404 /*friend_p=*/NULL);
9405 /* We're done with the specialization. */
9406 end_specialization ();
9407 /* For the erroneous case of a template with C linkage, we pushed an
9408 implicit C++ linkage scope; exit that scope now. */
9409 if (need_lang_pop)
9410 pop_lang_context ();
9411 /* We're done with this parameter list. */
9412 --parser->num_template_parameter_lists;
9415 /* Parse a type-specifier.
9417 type-specifier:
9418 simple-type-specifier
9419 class-specifier
9420 enum-specifier
9421 elaborated-type-specifier
9422 cv-qualifier
9424 GNU Extension:
9426 type-specifier:
9427 __complex__
9429 Returns a representation of the type-specifier. For a
9430 class-specifier, enum-specifier, or elaborated-type-specifier, a
9431 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9433 The parser flags FLAGS is used to control type-specifier parsing.
9435 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9436 in a decl-specifier-seq.
9438 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9439 class-specifier, enum-specifier, or elaborated-type-specifier, then
9440 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9441 if a type is declared; 2 if it is defined. Otherwise, it is set to
9442 zero.
9444 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9445 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9446 is set to FALSE. */
9448 static tree
9449 cp_parser_type_specifier (cp_parser* parser,
9450 cp_parser_flags flags,
9451 cp_decl_specifier_seq *decl_specs,
9452 bool is_declaration,
9453 int* declares_class_or_enum,
9454 bool* is_cv_qualifier)
9456 tree type_spec = NULL_TREE;
9457 cp_token *token;
9458 enum rid keyword;
9459 cp_decl_spec ds = ds_last;
9461 /* Assume this type-specifier does not declare a new type. */
9462 if (declares_class_or_enum)
9463 *declares_class_or_enum = 0;
9464 /* And that it does not specify a cv-qualifier. */
9465 if (is_cv_qualifier)
9466 *is_cv_qualifier = false;
9467 /* Peek at the next token. */
9468 token = cp_lexer_peek_token (parser->lexer);
9470 /* If we're looking at a keyword, we can use that to guide the
9471 production we choose. */
9472 keyword = token->keyword;
9473 switch (keyword)
9475 case RID_ENUM:
9476 /* 'enum' [identifier] '{' introduces an enum-specifier;
9477 'enum' <anything else> introduces an elaborated-type-specifier. */
9478 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9479 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9480 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9481 == CPP_OPEN_BRACE))
9483 if (parser->num_template_parameter_lists)
9485 error ("template declaration of %qs", "enum");
9486 cp_parser_skip_to_end_of_block_or_statement (parser);
9487 type_spec = error_mark_node;
9489 else
9490 type_spec = cp_parser_enum_specifier (parser);
9492 if (declares_class_or_enum)
9493 *declares_class_or_enum = 2;
9494 if (decl_specs)
9495 cp_parser_set_decl_spec_type (decl_specs,
9496 type_spec,
9497 /*user_defined_p=*/true);
9498 return type_spec;
9500 else
9501 goto elaborated_type_specifier;
9503 /* Any of these indicate either a class-specifier, or an
9504 elaborated-type-specifier. */
9505 case RID_CLASS:
9506 case RID_STRUCT:
9507 case RID_UNION:
9508 /* Parse tentatively so that we can back up if we don't find a
9509 class-specifier. */
9510 cp_parser_parse_tentatively (parser);
9511 /* Look for the class-specifier. */
9512 type_spec = cp_parser_class_specifier (parser);
9513 /* If that worked, we're done. */
9514 if (cp_parser_parse_definitely (parser))
9516 if (declares_class_or_enum)
9517 *declares_class_or_enum = 2;
9518 if (decl_specs)
9519 cp_parser_set_decl_spec_type (decl_specs,
9520 type_spec,
9521 /*user_defined_p=*/true);
9522 return type_spec;
9525 /* Fall through. */
9526 elaborated_type_specifier:
9527 /* We're declaring (not defining) a class or enum. */
9528 if (declares_class_or_enum)
9529 *declares_class_or_enum = 1;
9531 /* Fall through. */
9532 case RID_TYPENAME:
9533 /* Look for an elaborated-type-specifier. */
9534 type_spec
9535 = (cp_parser_elaborated_type_specifier
9536 (parser,
9537 decl_specs && decl_specs->specs[(int) ds_friend],
9538 is_declaration));
9539 if (decl_specs)
9540 cp_parser_set_decl_spec_type (decl_specs,
9541 type_spec,
9542 /*user_defined_p=*/true);
9543 return type_spec;
9545 case RID_CONST:
9546 ds = ds_const;
9547 if (is_cv_qualifier)
9548 *is_cv_qualifier = true;
9549 break;
9551 case RID_VOLATILE:
9552 ds = ds_volatile;
9553 if (is_cv_qualifier)
9554 *is_cv_qualifier = true;
9555 break;
9557 case RID_RESTRICT:
9558 ds = ds_restrict;
9559 if (is_cv_qualifier)
9560 *is_cv_qualifier = true;
9561 break;
9563 case RID_COMPLEX:
9564 /* The `__complex__' keyword is a GNU extension. */
9565 ds = ds_complex;
9566 break;
9568 default:
9569 break;
9572 /* Handle simple keywords. */
9573 if (ds != ds_last)
9575 if (decl_specs)
9577 ++decl_specs->specs[(int)ds];
9578 decl_specs->any_specifiers_p = true;
9580 return cp_lexer_consume_token (parser->lexer)->value;
9583 /* If we do not already have a type-specifier, assume we are looking
9584 at a simple-type-specifier. */
9585 type_spec = cp_parser_simple_type_specifier (parser,
9586 decl_specs,
9587 flags);
9589 /* If we didn't find a type-specifier, and a type-specifier was not
9590 optional in this context, issue an error message. */
9591 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9593 cp_parser_error (parser, "expected type specifier");
9594 return error_mark_node;
9597 return type_spec;
9600 /* Parse a simple-type-specifier.
9602 simple-type-specifier:
9603 :: [opt] nested-name-specifier [opt] type-name
9604 :: [opt] nested-name-specifier template template-id
9605 char
9606 wchar_t
9607 bool
9608 short
9610 long
9611 signed
9612 unsigned
9613 float
9614 double
9615 void
9617 GNU Extension:
9619 simple-type-specifier:
9620 __typeof__ unary-expression
9621 __typeof__ ( type-id )
9623 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9624 appropriately updated. */
9626 static tree
9627 cp_parser_simple_type_specifier (cp_parser* parser,
9628 cp_decl_specifier_seq *decl_specs,
9629 cp_parser_flags flags)
9631 tree type = NULL_TREE;
9632 cp_token *token;
9634 /* Peek at the next token. */
9635 token = cp_lexer_peek_token (parser->lexer);
9637 /* If we're looking at a keyword, things are easy. */
9638 switch (token->keyword)
9640 case RID_CHAR:
9641 if (decl_specs)
9642 decl_specs->explicit_char_p = true;
9643 type = char_type_node;
9644 break;
9645 case RID_WCHAR:
9646 type = wchar_type_node;
9647 break;
9648 case RID_BOOL:
9649 type = boolean_type_node;
9650 break;
9651 case RID_SHORT:
9652 if (decl_specs)
9653 ++decl_specs->specs[(int) ds_short];
9654 type = short_integer_type_node;
9655 break;
9656 case RID_INT:
9657 if (decl_specs)
9658 decl_specs->explicit_int_p = true;
9659 type = integer_type_node;
9660 break;
9661 case RID_LONG:
9662 if (decl_specs)
9663 ++decl_specs->specs[(int) ds_long];
9664 type = long_integer_type_node;
9665 break;
9666 case RID_SIGNED:
9667 if (decl_specs)
9668 ++decl_specs->specs[(int) ds_signed];
9669 type = integer_type_node;
9670 break;
9671 case RID_UNSIGNED:
9672 if (decl_specs)
9673 ++decl_specs->specs[(int) ds_unsigned];
9674 type = unsigned_type_node;
9675 break;
9676 case RID_FLOAT:
9677 type = float_type_node;
9678 break;
9679 case RID_DOUBLE:
9680 type = double_type_node;
9681 break;
9682 case RID_VOID:
9683 type = void_type_node;
9684 break;
9686 case RID_TYPEOF:
9687 /* Consume the `typeof' token. */
9688 cp_lexer_consume_token (parser->lexer);
9689 /* Parse the operand to `typeof'. */
9690 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9691 /* If it is not already a TYPE, take its type. */
9692 if (!TYPE_P (type))
9693 type = finish_typeof (type);
9695 if (decl_specs)
9696 cp_parser_set_decl_spec_type (decl_specs, type,
9697 /*user_defined_p=*/true);
9699 return type;
9701 default:
9702 break;
9705 /* If the type-specifier was for a built-in type, we're done. */
9706 if (type)
9708 tree id;
9710 /* Record the type. */
9711 if (decl_specs
9712 && (token->keyword != RID_SIGNED
9713 && token->keyword != RID_UNSIGNED
9714 && token->keyword != RID_SHORT
9715 && token->keyword != RID_LONG))
9716 cp_parser_set_decl_spec_type (decl_specs,
9717 type,
9718 /*user_defined=*/false);
9719 if (decl_specs)
9720 decl_specs->any_specifiers_p = true;
9722 /* Consume the token. */
9723 id = cp_lexer_consume_token (parser->lexer)->value;
9725 /* There is no valid C++ program where a non-template type is
9726 followed by a "<". That usually indicates that the user thought
9727 that the type was a template. */
9728 cp_parser_check_for_invalid_template_id (parser, type);
9730 return TYPE_NAME (type);
9733 /* The type-specifier must be a user-defined type. */
9734 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9736 bool qualified_p;
9737 bool global_p;
9739 /* Don't gobble tokens or issue error messages if this is an
9740 optional type-specifier. */
9741 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9742 cp_parser_parse_tentatively (parser);
9744 /* Look for the optional `::' operator. */
9745 global_p
9746 = (cp_parser_global_scope_opt (parser,
9747 /*current_scope_valid_p=*/false)
9748 != NULL_TREE);
9749 /* Look for the nested-name specifier. */
9750 qualified_p
9751 = (cp_parser_nested_name_specifier_opt (parser,
9752 /*typename_keyword_p=*/false,
9753 /*check_dependency_p=*/true,
9754 /*type_p=*/false,
9755 /*is_declaration=*/false)
9756 != NULL_TREE);
9757 /* If we have seen a nested-name-specifier, and the next token
9758 is `template', then we are using the template-id production. */
9759 if (parser->scope
9760 && cp_parser_optional_template_keyword (parser))
9762 /* Look for the template-id. */
9763 type = cp_parser_template_id (parser,
9764 /*template_keyword_p=*/true,
9765 /*check_dependency_p=*/true,
9766 /*is_declaration=*/false);
9767 /* If the template-id did not name a type, we are out of
9768 luck. */
9769 if (TREE_CODE (type) != TYPE_DECL)
9771 cp_parser_error (parser, "expected template-id for type");
9772 type = NULL_TREE;
9775 /* Otherwise, look for a type-name. */
9776 else
9777 type = cp_parser_type_name (parser);
9778 /* Keep track of all name-lookups performed in class scopes. */
9779 if (type
9780 && !global_p
9781 && !qualified_p
9782 && TREE_CODE (type) == TYPE_DECL
9783 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9784 maybe_note_name_used_in_class (DECL_NAME (type), type);
9785 /* If it didn't work out, we don't have a TYPE. */
9786 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9787 && !cp_parser_parse_definitely (parser))
9788 type = NULL_TREE;
9789 if (type && decl_specs)
9790 cp_parser_set_decl_spec_type (decl_specs, type,
9791 /*user_defined=*/true);
9794 /* If we didn't get a type-name, issue an error message. */
9795 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9797 cp_parser_error (parser, "expected type-name");
9798 return error_mark_node;
9801 /* There is no valid C++ program where a non-template type is
9802 followed by a "<". That usually indicates that the user thought
9803 that the type was a template. */
9804 if (type && type != error_mark_node)
9806 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9807 If it is, then the '<'...'>' enclose protocol names rather than
9808 template arguments, and so everything is fine. */
9809 if (c_dialect_objc ()
9810 && (objc_is_id (type) || objc_is_class_name (type)))
9812 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9813 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9815 /* Clobber the "unqualified" type previously entered into
9816 DECL_SPECS with the new, improved protocol-qualified version. */
9817 if (decl_specs)
9818 decl_specs->type = qual_type;
9820 return qual_type;
9823 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9826 return type;
9829 /* Parse a type-name.
9831 type-name:
9832 class-name
9833 enum-name
9834 typedef-name
9836 enum-name:
9837 identifier
9839 typedef-name:
9840 identifier
9842 Returns a TYPE_DECL for the type. */
9844 static tree
9845 cp_parser_type_name (cp_parser* parser)
9847 tree type_decl;
9848 tree identifier;
9850 /* We can't know yet whether it is a class-name or not. */
9851 cp_parser_parse_tentatively (parser);
9852 /* Try a class-name. */
9853 type_decl = cp_parser_class_name (parser,
9854 /*typename_keyword_p=*/false,
9855 /*template_keyword_p=*/false,
9856 none_type,
9857 /*check_dependency_p=*/true,
9858 /*class_head_p=*/false,
9859 /*is_declaration=*/false);
9860 /* If it's not a class-name, keep looking. */
9861 if (!cp_parser_parse_definitely (parser))
9863 /* It must be a typedef-name or an enum-name. */
9864 identifier = cp_parser_identifier (parser);
9865 if (identifier == error_mark_node)
9866 return error_mark_node;
9868 /* Look up the type-name. */
9869 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9871 if (TREE_CODE (type_decl) != TYPE_DECL
9872 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9874 /* See if this is an Objective-C type. */
9875 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9876 tree type = objc_get_protocol_qualified_type (identifier, protos);
9877 if (type)
9878 type_decl = TYPE_NAME (type);
9881 /* Issue an error if we did not find a type-name. */
9882 if (TREE_CODE (type_decl) != TYPE_DECL)
9884 if (!cp_parser_simulate_error (parser))
9885 cp_parser_name_lookup_error (parser, identifier, type_decl,
9886 "is not a type");
9887 type_decl = error_mark_node;
9889 /* Remember that the name was used in the definition of the
9890 current class so that we can check later to see if the
9891 meaning would have been different after the class was
9892 entirely defined. */
9893 else if (type_decl != error_mark_node
9894 && !parser->scope)
9895 maybe_note_name_used_in_class (identifier, type_decl);
9898 return type_decl;
9902 /* Parse an elaborated-type-specifier. Note that the grammar given
9903 here incorporates the resolution to DR68.
9905 elaborated-type-specifier:
9906 class-key :: [opt] nested-name-specifier [opt] identifier
9907 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9908 enum :: [opt] nested-name-specifier [opt] identifier
9909 typename :: [opt] nested-name-specifier identifier
9910 typename :: [opt] nested-name-specifier template [opt]
9911 template-id
9913 GNU extension:
9915 elaborated-type-specifier:
9916 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9917 class-key attributes :: [opt] nested-name-specifier [opt]
9918 template [opt] template-id
9919 enum attributes :: [opt] nested-name-specifier [opt] identifier
9921 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9922 declared `friend'. If IS_DECLARATION is TRUE, then this
9923 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9924 something is being declared.
9926 Returns the TYPE specified. */
9928 static tree
9929 cp_parser_elaborated_type_specifier (cp_parser* parser,
9930 bool is_friend,
9931 bool is_declaration)
9933 enum tag_types tag_type;
9934 tree identifier;
9935 tree type = NULL_TREE;
9936 tree attributes = NULL_TREE;
9938 /* See if we're looking at the `enum' keyword. */
9939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9941 /* Consume the `enum' token. */
9942 cp_lexer_consume_token (parser->lexer);
9943 /* Remember that it's an enumeration type. */
9944 tag_type = enum_type;
9945 /* Parse the attributes. */
9946 attributes = cp_parser_attributes_opt (parser);
9948 /* Or, it might be `typename'. */
9949 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9950 RID_TYPENAME))
9952 /* Consume the `typename' token. */
9953 cp_lexer_consume_token (parser->lexer);
9954 /* Remember that it's a `typename' type. */
9955 tag_type = typename_type;
9956 /* The `typename' keyword is only allowed in templates. */
9957 if (!processing_template_decl)
9958 pedwarn ("using %<typename%> outside of template");
9960 /* Otherwise it must be a class-key. */
9961 else
9963 tag_type = cp_parser_class_key (parser);
9964 if (tag_type == none_type)
9965 return error_mark_node;
9966 /* Parse the attributes. */
9967 attributes = cp_parser_attributes_opt (parser);
9970 /* Look for the `::' operator. */
9971 cp_parser_global_scope_opt (parser,
9972 /*current_scope_valid_p=*/false);
9973 /* Look for the nested-name-specifier. */
9974 if (tag_type == typename_type)
9976 if (!cp_parser_nested_name_specifier (parser,
9977 /*typename_keyword_p=*/true,
9978 /*check_dependency_p=*/true,
9979 /*type_p=*/true,
9980 is_declaration))
9981 return error_mark_node;
9983 else
9984 /* Even though `typename' is not present, the proposed resolution
9985 to Core Issue 180 says that in `class A<T>::B', `B' should be
9986 considered a type-name, even if `A<T>' is dependent. */
9987 cp_parser_nested_name_specifier_opt (parser,
9988 /*typename_keyword_p=*/true,
9989 /*check_dependency_p=*/true,
9990 /*type_p=*/true,
9991 is_declaration);
9992 /* For everything but enumeration types, consider a template-id. */
9993 if (tag_type != enum_type)
9995 bool template_p = false;
9996 tree decl;
9998 /* Allow the `template' keyword. */
9999 template_p = cp_parser_optional_template_keyword (parser);
10000 /* If we didn't see `template', we don't know if there's a
10001 template-id or not. */
10002 if (!template_p)
10003 cp_parser_parse_tentatively (parser);
10004 /* Parse the template-id. */
10005 decl = cp_parser_template_id (parser, template_p,
10006 /*check_dependency_p=*/true,
10007 is_declaration);
10008 /* If we didn't find a template-id, look for an ordinary
10009 identifier. */
10010 if (!template_p && !cp_parser_parse_definitely (parser))
10012 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10013 in effect, then we must assume that, upon instantiation, the
10014 template will correspond to a class. */
10015 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10016 && tag_type == typename_type)
10017 type = make_typename_type (parser->scope, decl,
10018 typename_type,
10019 /*complain=*/tf_error);
10020 else
10021 type = TREE_TYPE (decl);
10024 /* For an enumeration type, consider only a plain identifier. */
10025 if (!type)
10027 identifier = cp_parser_identifier (parser);
10029 if (identifier == error_mark_node)
10031 parser->scope = NULL_TREE;
10032 return error_mark_node;
10035 /* For a `typename', we needn't call xref_tag. */
10036 if (tag_type == typename_type
10037 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10038 return cp_parser_make_typename_type (parser, parser->scope,
10039 identifier);
10040 /* Look up a qualified name in the usual way. */
10041 if (parser->scope)
10043 tree decl;
10045 decl = cp_parser_lookup_name (parser, identifier,
10046 tag_type,
10047 /*is_template=*/false,
10048 /*is_namespace=*/false,
10049 /*check_dependency=*/true,
10050 /*ambiguous_decls=*/NULL);
10052 /* If we are parsing friend declaration, DECL may be a
10053 TEMPLATE_DECL tree node here. However, we need to check
10054 whether this TEMPLATE_DECL results in valid code. Consider
10055 the following example:
10057 namespace N {
10058 template <class T> class C {};
10060 class X {
10061 template <class T> friend class N::C; // #1, valid code
10063 template <class T> class Y {
10064 friend class N::C; // #2, invalid code
10067 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10068 name lookup of `N::C'. We see that friend declaration must
10069 be template for the code to be valid. Note that
10070 processing_template_decl does not work here since it is
10071 always 1 for the above two cases. */
10073 decl = (cp_parser_maybe_treat_template_as_class
10074 (decl, /*tag_name_p=*/is_friend
10075 && parser->num_template_parameter_lists));
10077 if (TREE_CODE (decl) != TYPE_DECL)
10079 cp_parser_diagnose_invalid_type_name (parser,
10080 parser->scope,
10081 identifier);
10082 return error_mark_node;
10085 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10086 check_elaborated_type_specifier
10087 (tag_type, decl,
10088 (parser->num_template_parameter_lists
10089 || DECL_SELF_REFERENCE_P (decl)));
10091 type = TREE_TYPE (decl);
10093 else
10095 /* An elaborated-type-specifier sometimes introduces a new type and
10096 sometimes names an existing type. Normally, the rule is that it
10097 introduces a new type only if there is not an existing type of
10098 the same name already in scope. For example, given:
10100 struct S {};
10101 void f() { struct S s; }
10103 the `struct S' in the body of `f' is the same `struct S' as in
10104 the global scope; the existing definition is used. However, if
10105 there were no global declaration, this would introduce a new
10106 local class named `S'.
10108 An exception to this rule applies to the following code:
10110 namespace N { struct S; }
10112 Here, the elaborated-type-specifier names a new type
10113 unconditionally; even if there is already an `S' in the
10114 containing scope this declaration names a new type.
10115 This exception only applies if the elaborated-type-specifier
10116 forms the complete declaration:
10118 [class.name]
10120 A declaration consisting solely of `class-key identifier ;' is
10121 either a redeclaration of the name in the current scope or a
10122 forward declaration of the identifier as a class name. It
10123 introduces the name into the current scope.
10125 We are in this situation precisely when the next token is a `;'.
10127 An exception to the exception is that a `friend' declaration does
10128 *not* name a new type; i.e., given:
10130 struct S { friend struct T; };
10132 `T' is not a new type in the scope of `S'.
10134 Also, `new struct S' or `sizeof (struct S)' never results in the
10135 definition of a new type; a new type can only be declared in a
10136 declaration context. */
10138 tag_scope ts;
10139 bool template_p;
10141 if (is_friend)
10142 /* Friends have special name lookup rules. */
10143 ts = ts_within_enclosing_non_class;
10144 else if (is_declaration
10145 && cp_lexer_next_token_is (parser->lexer,
10146 CPP_SEMICOLON))
10147 /* This is a `class-key identifier ;' */
10148 ts = ts_current;
10149 else
10150 ts = ts_global;
10152 /* Warn about attributes. They are ignored. */
10153 if (attributes)
10154 warning (OPT_Wattributes,
10155 "type attributes are honored only at type definition");
10157 template_p =
10158 (parser->num_template_parameter_lists
10159 && (cp_parser_next_token_starts_class_definition_p (parser)
10160 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10161 /* An unqualified name was used to reference this type, so
10162 there were no qualifying templates. */
10163 if (!cp_parser_check_template_parameters (parser,
10164 /*num_templates=*/0))
10165 return error_mark_node;
10166 type = xref_tag (tag_type, identifier, ts, template_p);
10169 if (tag_type != enum_type)
10170 cp_parser_check_class_key (tag_type, type);
10172 /* A "<" cannot follow an elaborated type specifier. If that
10173 happens, the user was probably trying to form a template-id. */
10174 cp_parser_check_for_invalid_template_id (parser, type);
10176 return type;
10179 /* Parse an enum-specifier.
10181 enum-specifier:
10182 enum identifier [opt] { enumerator-list [opt] }
10184 GNU Extensions:
10185 enum identifier [opt] { enumerator-list [opt] } attributes
10187 Returns an ENUM_TYPE representing the enumeration. */
10189 static tree
10190 cp_parser_enum_specifier (cp_parser* parser)
10192 tree identifier;
10193 tree type;
10195 /* Caller guarantees that the current token is 'enum', an identifier
10196 possibly follows, and the token after that is an opening brace.
10197 If we don't have an identifier, fabricate an anonymous name for
10198 the enumeration being defined. */
10199 cp_lexer_consume_token (parser->lexer);
10201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10202 identifier = cp_parser_identifier (parser);
10203 else
10204 identifier = make_anon_name ();
10206 /* Issue an error message if type-definitions are forbidden here. */
10207 cp_parser_check_type_definition (parser);
10209 /* Create the new type. We do this before consuming the opening brace
10210 so the enum will be recorded as being on the line of its tag (or the
10211 'enum' keyword, if there is no tag). */
10212 type = start_enum (identifier);
10214 /* Consume the opening brace. */
10215 cp_lexer_consume_token (parser->lexer);
10217 /* If the next token is not '}', then there are some enumerators. */
10218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10219 cp_parser_enumerator_list (parser, type);
10221 /* Consume the final '}'. */
10222 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10224 /* Look for trailing attributes to apply to this enumeration, and
10225 apply them if appropriate. */
10226 if (cp_parser_allow_gnu_extensions_p (parser))
10228 tree trailing_attr = cp_parser_attributes_opt (parser);
10229 cplus_decl_attributes (&type,
10230 trailing_attr,
10231 (int) ATTR_FLAG_TYPE_IN_PLACE);
10234 /* Finish up the enumeration. */
10235 finish_enum (type);
10237 return type;
10240 /* Parse an enumerator-list. The enumerators all have the indicated
10241 TYPE.
10243 enumerator-list:
10244 enumerator-definition
10245 enumerator-list , enumerator-definition */
10247 static void
10248 cp_parser_enumerator_list (cp_parser* parser, tree type)
10250 while (true)
10252 /* Parse an enumerator-definition. */
10253 cp_parser_enumerator_definition (parser, type);
10255 /* If the next token is not a ',', we've reached the end of
10256 the list. */
10257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10258 break;
10259 /* Otherwise, consume the `,' and keep going. */
10260 cp_lexer_consume_token (parser->lexer);
10261 /* If the next token is a `}', there is a trailing comma. */
10262 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10264 if (pedantic && !in_system_header)
10265 pedwarn ("comma at end of enumerator list");
10266 break;
10271 /* Parse an enumerator-definition. The enumerator has the indicated
10272 TYPE.
10274 enumerator-definition:
10275 enumerator
10276 enumerator = constant-expression
10278 enumerator:
10279 identifier */
10281 static void
10282 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10284 tree identifier;
10285 tree value;
10287 /* Look for the identifier. */
10288 identifier = cp_parser_identifier (parser);
10289 if (identifier == error_mark_node)
10290 return;
10292 /* If the next token is an '=', then there is an explicit value. */
10293 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10295 /* Consume the `=' token. */
10296 cp_lexer_consume_token (parser->lexer);
10297 /* Parse the value. */
10298 value = cp_parser_constant_expression (parser,
10299 /*allow_non_constant_p=*/false,
10300 NULL);
10302 else
10303 value = NULL_TREE;
10305 /* Create the enumerator. */
10306 build_enumerator (identifier, value, type);
10309 /* Parse a namespace-name.
10311 namespace-name:
10312 original-namespace-name
10313 namespace-alias
10315 Returns the NAMESPACE_DECL for the namespace. */
10317 static tree
10318 cp_parser_namespace_name (cp_parser* parser)
10320 tree identifier;
10321 tree namespace_decl;
10323 /* Get the name of the namespace. */
10324 identifier = cp_parser_identifier (parser);
10325 if (identifier == error_mark_node)
10326 return error_mark_node;
10328 /* Look up the identifier in the currently active scope. Look only
10329 for namespaces, due to:
10331 [basic.lookup.udir]
10333 When looking up a namespace-name in a using-directive or alias
10334 definition, only namespace names are considered.
10336 And:
10338 [basic.lookup.qual]
10340 During the lookup of a name preceding the :: scope resolution
10341 operator, object, function, and enumerator names are ignored.
10343 (Note that cp_parser_class_or_namespace_name only calls this
10344 function if the token after the name is the scope resolution
10345 operator.) */
10346 namespace_decl = cp_parser_lookup_name (parser, identifier,
10347 none_type,
10348 /*is_template=*/false,
10349 /*is_namespace=*/true,
10350 /*check_dependency=*/true,
10351 /*ambiguous_decls=*/NULL);
10352 /* If it's not a namespace, issue an error. */
10353 if (namespace_decl == error_mark_node
10354 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10356 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10357 error ("%qD is not a namespace-name", identifier);
10358 cp_parser_error (parser, "expected namespace-name");
10359 namespace_decl = error_mark_node;
10362 return namespace_decl;
10365 /* Parse a namespace-definition.
10367 namespace-definition:
10368 named-namespace-definition
10369 unnamed-namespace-definition
10371 named-namespace-definition:
10372 original-namespace-definition
10373 extension-namespace-definition
10375 original-namespace-definition:
10376 namespace identifier { namespace-body }
10378 extension-namespace-definition:
10379 namespace original-namespace-name { namespace-body }
10381 unnamed-namespace-definition:
10382 namespace { namespace-body } */
10384 static void
10385 cp_parser_namespace_definition (cp_parser* parser)
10387 tree identifier;
10389 /* Look for the `namespace' keyword. */
10390 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10392 /* Get the name of the namespace. We do not attempt to distinguish
10393 between an original-namespace-definition and an
10394 extension-namespace-definition at this point. The semantic
10395 analysis routines are responsible for that. */
10396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10397 identifier = cp_parser_identifier (parser);
10398 else
10399 identifier = NULL_TREE;
10401 /* Look for the `{' to start the namespace. */
10402 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10403 /* Start the namespace. */
10404 push_namespace (identifier);
10405 /* Parse the body of the namespace. */
10406 cp_parser_namespace_body (parser);
10407 /* Finish the namespace. */
10408 pop_namespace ();
10409 /* Look for the final `}'. */
10410 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10413 /* Parse a namespace-body.
10415 namespace-body:
10416 declaration-seq [opt] */
10418 static void
10419 cp_parser_namespace_body (cp_parser* parser)
10421 cp_parser_declaration_seq_opt (parser);
10424 /* Parse a namespace-alias-definition.
10426 namespace-alias-definition:
10427 namespace identifier = qualified-namespace-specifier ; */
10429 static void
10430 cp_parser_namespace_alias_definition (cp_parser* parser)
10432 tree identifier;
10433 tree namespace_specifier;
10435 /* Look for the `namespace' keyword. */
10436 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10437 /* Look for the identifier. */
10438 identifier = cp_parser_identifier (parser);
10439 if (identifier == error_mark_node)
10440 return;
10441 /* Look for the `=' token. */
10442 cp_parser_require (parser, CPP_EQ, "`='");
10443 /* Look for the qualified-namespace-specifier. */
10444 namespace_specifier
10445 = cp_parser_qualified_namespace_specifier (parser);
10446 /* Look for the `;' token. */
10447 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10449 /* Register the alias in the symbol table. */
10450 do_namespace_alias (identifier, namespace_specifier);
10453 /* Parse a qualified-namespace-specifier.
10455 qualified-namespace-specifier:
10456 :: [opt] nested-name-specifier [opt] namespace-name
10458 Returns a NAMESPACE_DECL corresponding to the specified
10459 namespace. */
10461 static tree
10462 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10464 /* Look for the optional `::'. */
10465 cp_parser_global_scope_opt (parser,
10466 /*current_scope_valid_p=*/false);
10468 /* Look for the optional nested-name-specifier. */
10469 cp_parser_nested_name_specifier_opt (parser,
10470 /*typename_keyword_p=*/false,
10471 /*check_dependency_p=*/true,
10472 /*type_p=*/false,
10473 /*is_declaration=*/true);
10475 return cp_parser_namespace_name (parser);
10478 /* Parse a using-declaration.
10480 using-declaration:
10481 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10482 using :: unqualified-id ; */
10484 static void
10485 cp_parser_using_declaration (cp_parser* parser)
10487 cp_token *token;
10488 bool typename_p = false;
10489 bool global_scope_p;
10490 tree decl;
10491 tree identifier;
10492 tree qscope;
10494 /* Look for the `using' keyword. */
10495 cp_parser_require_keyword (parser, RID_USING, "`using'");
10497 /* Peek at the next token. */
10498 token = cp_lexer_peek_token (parser->lexer);
10499 /* See if it's `typename'. */
10500 if (token->keyword == RID_TYPENAME)
10502 /* Remember that we've seen it. */
10503 typename_p = true;
10504 /* Consume the `typename' token. */
10505 cp_lexer_consume_token (parser->lexer);
10508 /* Look for the optional global scope qualification. */
10509 global_scope_p
10510 = (cp_parser_global_scope_opt (parser,
10511 /*current_scope_valid_p=*/false)
10512 != NULL_TREE);
10514 /* If we saw `typename', or didn't see `::', then there must be a
10515 nested-name-specifier present. */
10516 if (typename_p || !global_scope_p)
10517 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10518 /*check_dependency_p=*/true,
10519 /*type_p=*/false,
10520 /*is_declaration=*/true);
10521 /* Otherwise, we could be in either of the two productions. In that
10522 case, treat the nested-name-specifier as optional. */
10523 else
10524 qscope = cp_parser_nested_name_specifier_opt (parser,
10525 /*typename_keyword_p=*/false,
10526 /*check_dependency_p=*/true,
10527 /*type_p=*/false,
10528 /*is_declaration=*/true);
10529 if (!qscope)
10530 qscope = global_namespace;
10532 /* Parse the unqualified-id. */
10533 identifier = cp_parser_unqualified_id (parser,
10534 /*template_keyword_p=*/false,
10535 /*check_dependency_p=*/true,
10536 /*declarator_p=*/true);
10538 /* The function we call to handle a using-declaration is different
10539 depending on what scope we are in. */
10540 if (qscope == error_mark_node || identifier == error_mark_node)
10542 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10543 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10544 /* [namespace.udecl]
10546 A using declaration shall not name a template-id. */
10547 error ("a template-id may not appear in a using-declaration");
10548 else
10550 if (at_class_scope_p ())
10552 /* Create the USING_DECL. */
10553 decl = do_class_using_decl (parser->scope, identifier);
10554 /* Add it to the list of members in this class. */
10555 finish_member_declaration (decl);
10557 else
10559 decl = cp_parser_lookup_name_simple (parser, identifier);
10560 if (decl == error_mark_node)
10561 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10562 else if (!at_namespace_scope_p ())
10563 do_local_using_decl (decl, qscope, identifier);
10564 else
10565 do_toplevel_using_decl (decl, qscope, identifier);
10569 /* Look for the final `;'. */
10570 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10573 /* Parse a using-directive.
10575 using-directive:
10576 using namespace :: [opt] nested-name-specifier [opt]
10577 namespace-name ; */
10579 static void
10580 cp_parser_using_directive (cp_parser* parser)
10582 tree namespace_decl;
10583 tree attribs;
10585 /* Look for the `using' keyword. */
10586 cp_parser_require_keyword (parser, RID_USING, "`using'");
10587 /* And the `namespace' keyword. */
10588 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10589 /* Look for the optional `::' operator. */
10590 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10591 /* And the optional nested-name-specifier. */
10592 cp_parser_nested_name_specifier_opt (parser,
10593 /*typename_keyword_p=*/false,
10594 /*check_dependency_p=*/true,
10595 /*type_p=*/false,
10596 /*is_declaration=*/true);
10597 /* Get the namespace being used. */
10598 namespace_decl = cp_parser_namespace_name (parser);
10599 /* And any specified attributes. */
10600 attribs = cp_parser_attributes_opt (parser);
10601 /* Update the symbol table. */
10602 parse_using_directive (namespace_decl, attribs);
10603 /* Look for the final `;'. */
10604 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10607 /* Parse an asm-definition.
10609 asm-definition:
10610 asm ( string-literal ) ;
10612 GNU Extension:
10614 asm-definition:
10615 asm volatile [opt] ( string-literal ) ;
10616 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10617 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10618 : asm-operand-list [opt] ) ;
10619 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10620 : asm-operand-list [opt]
10621 : asm-operand-list [opt] ) ; */
10623 static void
10624 cp_parser_asm_definition (cp_parser* parser)
10626 tree string;
10627 tree outputs = NULL_TREE;
10628 tree inputs = NULL_TREE;
10629 tree clobbers = NULL_TREE;
10630 tree asm_stmt;
10631 bool volatile_p = false;
10632 bool extended_p = false;
10634 /* Look for the `asm' keyword. */
10635 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10636 /* See if the next token is `volatile'. */
10637 if (cp_parser_allow_gnu_extensions_p (parser)
10638 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10640 /* Remember that we saw the `volatile' keyword. */
10641 volatile_p = true;
10642 /* Consume the token. */
10643 cp_lexer_consume_token (parser->lexer);
10645 /* Look for the opening `('. */
10646 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10647 return;
10648 /* Look for the string. */
10649 string = cp_parser_string_literal (parser, false, false);
10650 if (string == error_mark_node)
10652 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10653 /*consume_paren=*/true);
10654 return;
10657 /* If we're allowing GNU extensions, check for the extended assembly
10658 syntax. Unfortunately, the `:' tokens need not be separated by
10659 a space in C, and so, for compatibility, we tolerate that here
10660 too. Doing that means that we have to treat the `::' operator as
10661 two `:' tokens. */
10662 if (cp_parser_allow_gnu_extensions_p (parser)
10663 && at_function_scope_p ()
10664 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10665 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10667 bool inputs_p = false;
10668 bool clobbers_p = false;
10670 /* The extended syntax was used. */
10671 extended_p = true;
10673 /* Look for outputs. */
10674 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10676 /* Consume the `:'. */
10677 cp_lexer_consume_token (parser->lexer);
10678 /* Parse the output-operands. */
10679 if (cp_lexer_next_token_is_not (parser->lexer,
10680 CPP_COLON)
10681 && cp_lexer_next_token_is_not (parser->lexer,
10682 CPP_SCOPE)
10683 && cp_lexer_next_token_is_not (parser->lexer,
10684 CPP_CLOSE_PAREN))
10685 outputs = cp_parser_asm_operand_list (parser);
10687 /* If the next token is `::', there are no outputs, and the
10688 next token is the beginning of the inputs. */
10689 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10690 /* The inputs are coming next. */
10691 inputs_p = true;
10693 /* Look for inputs. */
10694 if (inputs_p
10695 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10697 /* Consume the `:' or `::'. */
10698 cp_lexer_consume_token (parser->lexer);
10699 /* Parse the output-operands. */
10700 if (cp_lexer_next_token_is_not (parser->lexer,
10701 CPP_COLON)
10702 && cp_lexer_next_token_is_not (parser->lexer,
10703 CPP_CLOSE_PAREN))
10704 inputs = cp_parser_asm_operand_list (parser);
10706 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10707 /* The clobbers are coming next. */
10708 clobbers_p = true;
10710 /* Look for clobbers. */
10711 if (clobbers_p
10712 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10714 /* Consume the `:' or `::'. */
10715 cp_lexer_consume_token (parser->lexer);
10716 /* Parse the clobbers. */
10717 if (cp_lexer_next_token_is_not (parser->lexer,
10718 CPP_CLOSE_PAREN))
10719 clobbers = cp_parser_asm_clobber_list (parser);
10722 /* Look for the closing `)'. */
10723 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10724 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10725 /*consume_paren=*/true);
10726 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10728 /* Create the ASM_EXPR. */
10729 if (at_function_scope_p ())
10731 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10732 inputs, clobbers);
10733 /* If the extended syntax was not used, mark the ASM_EXPR. */
10734 if (!extended_p)
10736 tree temp = asm_stmt;
10737 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10738 temp = TREE_OPERAND (temp, 0);
10740 ASM_INPUT_P (temp) = 1;
10743 else
10744 assemble_asm (string);
10747 /* Declarators [gram.dcl.decl] */
10749 /* Parse an init-declarator.
10751 init-declarator:
10752 declarator initializer [opt]
10754 GNU Extension:
10756 init-declarator:
10757 declarator asm-specification [opt] attributes [opt] initializer [opt]
10759 function-definition:
10760 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10761 function-body
10762 decl-specifier-seq [opt] declarator function-try-block
10764 GNU Extension:
10766 function-definition:
10767 __extension__ function-definition
10769 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10770 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10771 then this declarator appears in a class scope. The new DECL created
10772 by this declarator is returned.
10774 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10775 for a function-definition here as well. If the declarator is a
10776 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10777 be TRUE upon return. By that point, the function-definition will
10778 have been completely parsed.
10780 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10781 is FALSE. */
10783 static tree
10784 cp_parser_init_declarator (cp_parser* parser,
10785 cp_decl_specifier_seq *decl_specifiers,
10786 bool function_definition_allowed_p,
10787 bool member_p,
10788 int declares_class_or_enum,
10789 bool* function_definition_p)
10791 cp_token *token;
10792 cp_declarator *declarator;
10793 tree prefix_attributes;
10794 tree attributes;
10795 tree asm_specification;
10796 tree initializer;
10797 tree decl = NULL_TREE;
10798 tree scope;
10799 bool is_initialized;
10800 bool is_parenthesized_init;
10801 bool is_non_constant_init;
10802 int ctor_dtor_or_conv_p;
10803 bool friend_p;
10804 tree pushed_scope = NULL;
10806 /* Gather the attributes that were provided with the
10807 decl-specifiers. */
10808 prefix_attributes = decl_specifiers->attributes;
10810 /* Assume that this is not the declarator for a function
10811 definition. */
10812 if (function_definition_p)
10813 *function_definition_p = false;
10815 /* Defer access checks while parsing the declarator; we cannot know
10816 what names are accessible until we know what is being
10817 declared. */
10818 resume_deferring_access_checks ();
10820 /* Parse the declarator. */
10821 declarator
10822 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10823 &ctor_dtor_or_conv_p,
10824 /*parenthesized_p=*/NULL,
10825 /*member_p=*/false);
10826 /* Gather up the deferred checks. */
10827 stop_deferring_access_checks ();
10829 /* If the DECLARATOR was erroneous, there's no need to go
10830 further. */
10831 if (declarator == cp_error_declarator)
10832 return error_mark_node;
10834 if (declares_class_or_enum & 2)
10835 cp_parser_check_for_definition_in_return_type (declarator,
10836 decl_specifiers->type);
10838 /* Figure out what scope the entity declared by the DECLARATOR is
10839 located in. `grokdeclarator' sometimes changes the scope, so
10840 we compute it now. */
10841 scope = get_scope_of_declarator (declarator);
10843 /* If we're allowing GNU extensions, look for an asm-specification
10844 and attributes. */
10845 if (cp_parser_allow_gnu_extensions_p (parser))
10847 /* Look for an asm-specification. */
10848 asm_specification = cp_parser_asm_specification_opt (parser);
10849 /* And attributes. */
10850 attributes = cp_parser_attributes_opt (parser);
10852 else
10854 asm_specification = NULL_TREE;
10855 attributes = NULL_TREE;
10858 /* Peek at the next token. */
10859 token = cp_lexer_peek_token (parser->lexer);
10860 /* Check to see if the token indicates the start of a
10861 function-definition. */
10862 if (cp_parser_token_starts_function_definition_p (token))
10864 if (!function_definition_allowed_p)
10866 /* If a function-definition should not appear here, issue an
10867 error message. */
10868 cp_parser_error (parser,
10869 "a function-definition is not allowed here");
10870 return error_mark_node;
10872 else
10874 /* Neither attributes nor an asm-specification are allowed
10875 on a function-definition. */
10876 if (asm_specification)
10877 error ("an asm-specification is not allowed on a function-definition");
10878 if (attributes)
10879 error ("attributes are not allowed on a function-definition");
10880 /* This is a function-definition. */
10881 *function_definition_p = true;
10883 /* Parse the function definition. */
10884 if (member_p)
10885 decl = cp_parser_save_member_function_body (parser,
10886 decl_specifiers,
10887 declarator,
10888 prefix_attributes);
10889 else
10890 decl
10891 = (cp_parser_function_definition_from_specifiers_and_declarator
10892 (parser, decl_specifiers, prefix_attributes, declarator));
10894 return decl;
10898 /* [dcl.dcl]
10900 Only in function declarations for constructors, destructors, and
10901 type conversions can the decl-specifier-seq be omitted.
10903 We explicitly postpone this check past the point where we handle
10904 function-definitions because we tolerate function-definitions
10905 that are missing their return types in some modes. */
10906 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10908 cp_parser_error (parser,
10909 "expected constructor, destructor, or type conversion");
10910 return error_mark_node;
10913 /* An `=' or an `(' indicates an initializer. */
10914 is_initialized = (token->type == CPP_EQ
10915 || token->type == CPP_OPEN_PAREN);
10916 /* If the init-declarator isn't initialized and isn't followed by a
10917 `,' or `;', it's not a valid init-declarator. */
10918 if (!is_initialized
10919 && token->type != CPP_COMMA
10920 && token->type != CPP_SEMICOLON)
10922 cp_parser_error (parser, "expected initializer");
10923 return error_mark_node;
10926 /* Because start_decl has side-effects, we should only call it if we
10927 know we're going ahead. By this point, we know that we cannot
10928 possibly be looking at any other construct. */
10929 cp_parser_commit_to_tentative_parse (parser);
10931 /* If the decl specifiers were bad, issue an error now that we're
10932 sure this was intended to be a declarator. Then continue
10933 declaring the variable(s), as int, to try to cut down on further
10934 errors. */
10935 if (decl_specifiers->any_specifiers_p
10936 && decl_specifiers->type == error_mark_node)
10938 cp_parser_error (parser, "invalid type in declaration");
10939 decl_specifiers->type = integer_type_node;
10942 /* Check to see whether or not this declaration is a friend. */
10943 friend_p = cp_parser_friend_p (decl_specifiers);
10945 /* Check that the number of template-parameter-lists is OK. */
10946 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10947 return error_mark_node;
10949 /* Enter the newly declared entry in the symbol table. If we're
10950 processing a declaration in a class-specifier, we wait until
10951 after processing the initializer. */
10952 if (!member_p)
10954 if (parser->in_unbraced_linkage_specification_p)
10956 decl_specifiers->storage_class = sc_extern;
10957 have_extern_spec = false;
10959 decl = start_decl (declarator, decl_specifiers,
10960 is_initialized, attributes, prefix_attributes,
10961 &pushed_scope);
10963 else if (scope)
10964 /* Enter the SCOPE. That way unqualified names appearing in the
10965 initializer will be looked up in SCOPE. */
10966 pushed_scope = push_scope (scope);
10968 /* Perform deferred access control checks, now that we know in which
10969 SCOPE the declared entity resides. */
10970 if (!member_p && decl)
10972 tree saved_current_function_decl = NULL_TREE;
10974 /* If the entity being declared is a function, pretend that we
10975 are in its scope. If it is a `friend', it may have access to
10976 things that would not otherwise be accessible. */
10977 if (TREE_CODE (decl) == FUNCTION_DECL)
10979 saved_current_function_decl = current_function_decl;
10980 current_function_decl = decl;
10983 /* Perform the access control checks for the declarator and the
10984 the decl-specifiers. */
10985 perform_deferred_access_checks ();
10987 /* Restore the saved value. */
10988 if (TREE_CODE (decl) == FUNCTION_DECL)
10989 current_function_decl = saved_current_function_decl;
10992 /* Parse the initializer. */
10993 if (is_initialized)
10994 initializer = cp_parser_initializer (parser,
10995 &is_parenthesized_init,
10996 &is_non_constant_init);
10997 else
10999 initializer = NULL_TREE;
11000 is_parenthesized_init = false;
11001 is_non_constant_init = true;
11004 /* The old parser allows attributes to appear after a parenthesized
11005 initializer. Mark Mitchell proposed removing this functionality
11006 on the GCC mailing lists on 2002-08-13. This parser accepts the
11007 attributes -- but ignores them. */
11008 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11009 if (cp_parser_attributes_opt (parser))
11010 warning (OPT_Wattributes,
11011 "attributes after parenthesized initializer ignored");
11013 /* For an in-class declaration, use `grokfield' to create the
11014 declaration. */
11015 if (member_p)
11017 if (pushed_scope)
11019 pop_scope (pushed_scope);
11020 pushed_scope = false;
11022 decl = grokfield (declarator, decl_specifiers,
11023 initializer, /*asmspec=*/NULL_TREE,
11024 prefix_attributes);
11025 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11026 cp_parser_save_default_args (parser, decl);
11029 /* Finish processing the declaration. But, skip friend
11030 declarations. */
11031 if (!friend_p && decl && decl != error_mark_node)
11033 cp_finish_decl (decl,
11034 initializer,
11035 asm_specification,
11036 /* If the initializer is in parentheses, then this is
11037 a direct-initialization, which means that an
11038 `explicit' constructor is OK. Otherwise, an
11039 `explicit' constructor cannot be used. */
11040 ((is_parenthesized_init || !is_initialized)
11041 ? 0 : LOOKUP_ONLYCONVERTING));
11043 if (!friend_p && pushed_scope)
11044 pop_scope (pushed_scope);
11046 /* Remember whether or not variables were initialized by
11047 constant-expressions. */
11048 if (decl && TREE_CODE (decl) == VAR_DECL
11049 && is_initialized && !is_non_constant_init)
11050 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11052 return decl;
11055 /* Parse a declarator.
11057 declarator:
11058 direct-declarator
11059 ptr-operator declarator
11061 abstract-declarator:
11062 ptr-operator abstract-declarator [opt]
11063 direct-abstract-declarator
11065 GNU Extensions:
11067 declarator:
11068 attributes [opt] direct-declarator
11069 attributes [opt] ptr-operator declarator
11071 abstract-declarator:
11072 attributes [opt] ptr-operator abstract-declarator [opt]
11073 attributes [opt] direct-abstract-declarator
11075 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11076 detect constructor, destructor or conversion operators. It is set
11077 to -1 if the declarator is a name, and +1 if it is a
11078 function. Otherwise it is set to zero. Usually you just want to
11079 test for >0, but internally the negative value is used.
11081 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11082 a decl-specifier-seq unless it declares a constructor, destructor,
11083 or conversion. It might seem that we could check this condition in
11084 semantic analysis, rather than parsing, but that makes it difficult
11085 to handle something like `f()'. We want to notice that there are
11086 no decl-specifiers, and therefore realize that this is an
11087 expression, not a declaration.)
11089 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11090 the declarator is a direct-declarator of the form "(...)".
11092 MEMBER_P is true iff this declarator is a member-declarator. */
11094 static cp_declarator *
11095 cp_parser_declarator (cp_parser* parser,
11096 cp_parser_declarator_kind dcl_kind,
11097 int* ctor_dtor_or_conv_p,
11098 bool* parenthesized_p,
11099 bool member_p)
11101 cp_token *token;
11102 cp_declarator *declarator;
11103 enum tree_code code;
11104 cp_cv_quals cv_quals;
11105 tree class_type;
11106 tree attributes = NULL_TREE;
11108 /* Assume this is not a constructor, destructor, or type-conversion
11109 operator. */
11110 if (ctor_dtor_or_conv_p)
11111 *ctor_dtor_or_conv_p = 0;
11113 if (cp_parser_allow_gnu_extensions_p (parser))
11114 attributes = cp_parser_attributes_opt (parser);
11116 /* Peek at the next token. */
11117 token = cp_lexer_peek_token (parser->lexer);
11119 /* Check for the ptr-operator production. */
11120 cp_parser_parse_tentatively (parser);
11121 /* Parse the ptr-operator. */
11122 code = cp_parser_ptr_operator (parser,
11123 &class_type,
11124 &cv_quals);
11125 /* If that worked, then we have a ptr-operator. */
11126 if (cp_parser_parse_definitely (parser))
11128 /* If a ptr-operator was found, then this declarator was not
11129 parenthesized. */
11130 if (parenthesized_p)
11131 *parenthesized_p = true;
11132 /* The dependent declarator is optional if we are parsing an
11133 abstract-declarator. */
11134 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11135 cp_parser_parse_tentatively (parser);
11137 /* Parse the dependent declarator. */
11138 declarator = cp_parser_declarator (parser, dcl_kind,
11139 /*ctor_dtor_or_conv_p=*/NULL,
11140 /*parenthesized_p=*/NULL,
11141 /*member_p=*/false);
11143 /* If we are parsing an abstract-declarator, we must handle the
11144 case where the dependent declarator is absent. */
11145 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11146 && !cp_parser_parse_definitely (parser))
11147 declarator = NULL;
11149 /* Build the representation of the ptr-operator. */
11150 if (class_type)
11151 declarator = make_ptrmem_declarator (cv_quals,
11152 class_type,
11153 declarator);
11154 else if (code == INDIRECT_REF)
11155 declarator = make_pointer_declarator (cv_quals, declarator);
11156 else
11157 declarator = make_reference_declarator (cv_quals, declarator);
11159 /* Everything else is a direct-declarator. */
11160 else
11162 if (parenthesized_p)
11163 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11164 CPP_OPEN_PAREN);
11165 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11166 ctor_dtor_or_conv_p,
11167 member_p);
11170 if (attributes && declarator != cp_error_declarator)
11171 declarator->attributes = attributes;
11173 return declarator;
11176 /* Parse a direct-declarator or direct-abstract-declarator.
11178 direct-declarator:
11179 declarator-id
11180 direct-declarator ( parameter-declaration-clause )
11181 cv-qualifier-seq [opt]
11182 exception-specification [opt]
11183 direct-declarator [ constant-expression [opt] ]
11184 ( declarator )
11186 direct-abstract-declarator:
11187 direct-abstract-declarator [opt]
11188 ( parameter-declaration-clause )
11189 cv-qualifier-seq [opt]
11190 exception-specification [opt]
11191 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11192 ( abstract-declarator )
11194 Returns a representation of the declarator. DCL_KIND is
11195 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11196 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11197 we are parsing a direct-declarator. It is
11198 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11199 of ambiguity we prefer an abstract declarator, as per
11200 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11201 cp_parser_declarator. */
11203 static cp_declarator *
11204 cp_parser_direct_declarator (cp_parser* parser,
11205 cp_parser_declarator_kind dcl_kind,
11206 int* ctor_dtor_or_conv_p,
11207 bool member_p)
11209 cp_token *token;
11210 cp_declarator *declarator = NULL;
11211 tree scope = NULL_TREE;
11212 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11213 bool saved_in_declarator_p = parser->in_declarator_p;
11214 bool first = true;
11215 tree pushed_scope = NULL_TREE;
11217 while (true)
11219 /* Peek at the next token. */
11220 token = cp_lexer_peek_token (parser->lexer);
11221 if (token->type == CPP_OPEN_PAREN)
11223 /* This is either a parameter-declaration-clause, or a
11224 parenthesized declarator. When we know we are parsing a
11225 named declarator, it must be a parenthesized declarator
11226 if FIRST is true. For instance, `(int)' is a
11227 parameter-declaration-clause, with an omitted
11228 direct-abstract-declarator. But `((*))', is a
11229 parenthesized abstract declarator. Finally, when T is a
11230 template parameter `(T)' is a
11231 parameter-declaration-clause, and not a parenthesized
11232 named declarator.
11234 We first try and parse a parameter-declaration-clause,
11235 and then try a nested declarator (if FIRST is true).
11237 It is not an error for it not to be a
11238 parameter-declaration-clause, even when FIRST is
11239 false. Consider,
11241 int i (int);
11242 int i (3);
11244 The first is the declaration of a function while the
11245 second is a the definition of a variable, including its
11246 initializer.
11248 Having seen only the parenthesis, we cannot know which of
11249 these two alternatives should be selected. Even more
11250 complex are examples like:
11252 int i (int (a));
11253 int i (int (3));
11255 The former is a function-declaration; the latter is a
11256 variable initialization.
11258 Thus again, we try a parameter-declaration-clause, and if
11259 that fails, we back out and return. */
11261 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11263 cp_parameter_declarator *params;
11264 unsigned saved_num_template_parameter_lists;
11266 /* In a member-declarator, the only valid interpretation
11267 of a parenthesis is the start of a
11268 parameter-declaration-clause. (It is invalid to
11269 initialize a static data member with a parenthesized
11270 initializer; only the "=" form of initialization is
11271 permitted.) */
11272 if (!member_p)
11273 cp_parser_parse_tentatively (parser);
11275 /* Consume the `('. */
11276 cp_lexer_consume_token (parser->lexer);
11277 if (first)
11279 /* If this is going to be an abstract declarator, we're
11280 in a declarator and we can't have default args. */
11281 parser->default_arg_ok_p = false;
11282 parser->in_declarator_p = true;
11285 /* Inside the function parameter list, surrounding
11286 template-parameter-lists do not apply. */
11287 saved_num_template_parameter_lists
11288 = parser->num_template_parameter_lists;
11289 parser->num_template_parameter_lists = 0;
11291 /* Parse the parameter-declaration-clause. */
11292 params = cp_parser_parameter_declaration_clause (parser);
11294 parser->num_template_parameter_lists
11295 = saved_num_template_parameter_lists;
11297 /* If all went well, parse the cv-qualifier-seq and the
11298 exception-specification. */
11299 if (member_p || cp_parser_parse_definitely (parser))
11301 cp_cv_quals cv_quals;
11302 tree exception_specification;
11304 if (ctor_dtor_or_conv_p)
11305 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11306 first = false;
11307 /* Consume the `)'. */
11308 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11310 /* Parse the cv-qualifier-seq. */
11311 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11312 /* And the exception-specification. */
11313 exception_specification
11314 = cp_parser_exception_specification_opt (parser);
11316 /* Create the function-declarator. */
11317 declarator = make_call_declarator (declarator,
11318 params,
11319 cv_quals,
11320 exception_specification);
11321 /* Any subsequent parameter lists are to do with
11322 return type, so are not those of the declared
11323 function. */
11324 parser->default_arg_ok_p = false;
11326 /* Repeat the main loop. */
11327 continue;
11331 /* If this is the first, we can try a parenthesized
11332 declarator. */
11333 if (first)
11335 bool saved_in_type_id_in_expr_p;
11337 parser->default_arg_ok_p = saved_default_arg_ok_p;
11338 parser->in_declarator_p = saved_in_declarator_p;
11340 /* Consume the `('. */
11341 cp_lexer_consume_token (parser->lexer);
11342 /* Parse the nested declarator. */
11343 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11344 parser->in_type_id_in_expr_p = true;
11345 declarator
11346 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11347 /*parenthesized_p=*/NULL,
11348 member_p);
11349 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11350 first = false;
11351 /* Expect a `)'. */
11352 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11353 declarator = cp_error_declarator;
11354 if (declarator == cp_error_declarator)
11355 break;
11357 goto handle_declarator;
11359 /* Otherwise, we must be done. */
11360 else
11361 break;
11363 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11364 && token->type == CPP_OPEN_SQUARE)
11366 /* Parse an array-declarator. */
11367 tree bounds;
11369 if (ctor_dtor_or_conv_p)
11370 *ctor_dtor_or_conv_p = 0;
11372 first = false;
11373 parser->default_arg_ok_p = false;
11374 parser->in_declarator_p = true;
11375 /* Consume the `['. */
11376 cp_lexer_consume_token (parser->lexer);
11377 /* Peek at the next token. */
11378 token = cp_lexer_peek_token (parser->lexer);
11379 /* If the next token is `]', then there is no
11380 constant-expression. */
11381 if (token->type != CPP_CLOSE_SQUARE)
11383 bool non_constant_p;
11385 bounds
11386 = cp_parser_constant_expression (parser,
11387 /*allow_non_constant=*/true,
11388 &non_constant_p);
11389 if (!non_constant_p)
11390 bounds = fold_non_dependent_expr (bounds);
11391 /* Normally, the array bound must be an integral constant
11392 expression. However, as an extension, we allow VLAs
11393 in function scopes. */
11394 else if (!at_function_scope_p ())
11396 error ("array bound is not an integer constant");
11397 bounds = error_mark_node;
11400 else
11401 bounds = NULL_TREE;
11402 /* Look for the closing `]'. */
11403 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11405 declarator = cp_error_declarator;
11406 break;
11409 declarator = make_array_declarator (declarator, bounds);
11411 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11413 tree qualifying_scope;
11414 tree unqualified_name;
11415 special_function_kind sfk;
11417 /* Parse a declarator-id */
11418 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11419 cp_parser_parse_tentatively (parser);
11420 unqualified_name = cp_parser_declarator_id (parser);
11421 qualifying_scope = parser->scope;
11422 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11424 if (!cp_parser_parse_definitely (parser))
11425 unqualified_name = error_mark_node;
11426 else if (qualifying_scope
11427 || (TREE_CODE (unqualified_name)
11428 != IDENTIFIER_NODE))
11430 cp_parser_error (parser, "expected unqualified-id");
11431 unqualified_name = error_mark_node;
11435 if (unqualified_name == error_mark_node)
11437 declarator = cp_error_declarator;
11438 break;
11441 if (qualifying_scope && at_namespace_scope_p ()
11442 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11444 /* In the declaration of a member of a template class
11445 outside of the class itself, the SCOPE will sometimes
11446 be a TYPENAME_TYPE. For example, given:
11448 template <typename T>
11449 int S<T>::R::i = 3;
11451 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11452 this context, we must resolve S<T>::R to an ordinary
11453 type, rather than a typename type.
11455 The reason we normally avoid resolving TYPENAME_TYPEs
11456 is that a specialization of `S' might render
11457 `S<T>::R' not a type. However, if `S' is
11458 specialized, then this `i' will not be used, so there
11459 is no harm in resolving the types here. */
11460 tree type;
11462 /* Resolve the TYPENAME_TYPE. */
11463 type = resolve_typename_type (qualifying_scope,
11464 /*only_current_p=*/false);
11465 /* If that failed, the declarator is invalid. */
11466 if (type == error_mark_node)
11467 error ("%<%T::%D%> is not a type",
11468 TYPE_CONTEXT (qualifying_scope),
11469 TYPE_IDENTIFIER (qualifying_scope));
11470 qualifying_scope = type;
11473 sfk = sfk_none;
11474 if (unqualified_name)
11476 tree class_type;
11478 if (qualifying_scope
11479 && CLASS_TYPE_P (qualifying_scope))
11480 class_type = qualifying_scope;
11481 else
11482 class_type = current_class_type;
11484 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11486 if (qualifying_scope
11487 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11489 error ("invalid use of constructor as a template");
11490 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11491 "the constructor in a qualified name",
11492 class_type,
11493 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11494 class_type, class_type);
11495 declarator = cp_error_declarator;
11496 break;
11498 else if (class_type
11499 && same_type_p (TREE_TYPE (unqualified_name),
11500 class_type))
11501 unqualified_name = constructor_name (class_type);
11502 else
11504 /* We do not attempt to print the declarator
11505 here because we do not have enough
11506 information about its original syntactic
11507 form. */
11508 error ("invalid declarator");
11509 declarator = cp_error_declarator;
11510 break;
11514 if (class_type)
11516 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11517 sfk = sfk_destructor;
11518 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11519 sfk = sfk_conversion;
11520 else if (/* There's no way to declare a constructor
11521 for an anonymous type, even if the type
11522 got a name for linkage purposes. */
11523 !TYPE_WAS_ANONYMOUS (class_type)
11524 && constructor_name_p (unqualified_name,
11525 class_type))
11527 unqualified_name = constructor_name (class_type);
11528 sfk = sfk_constructor;
11531 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11532 *ctor_dtor_or_conv_p = -1;
11535 declarator = make_id_declarator (qualifying_scope,
11536 unqualified_name,
11537 sfk);
11538 declarator->id_loc = token->location;
11540 handle_declarator:;
11541 scope = get_scope_of_declarator (declarator);
11542 if (scope)
11543 /* Any names that appear after the declarator-id for a
11544 member are looked up in the containing scope. */
11545 pushed_scope = push_scope (scope);
11546 parser->in_declarator_p = true;
11547 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11548 || (declarator && declarator->kind == cdk_id))
11549 /* Default args are only allowed on function
11550 declarations. */
11551 parser->default_arg_ok_p = saved_default_arg_ok_p;
11552 else
11553 parser->default_arg_ok_p = false;
11555 first = false;
11557 /* We're done. */
11558 else
11559 break;
11562 /* For an abstract declarator, we might wind up with nothing at this
11563 point. That's an error; the declarator is not optional. */
11564 if (!declarator)
11565 cp_parser_error (parser, "expected declarator");
11567 /* If we entered a scope, we must exit it now. */
11568 if (pushed_scope)
11569 pop_scope (pushed_scope);
11571 parser->default_arg_ok_p = saved_default_arg_ok_p;
11572 parser->in_declarator_p = saved_in_declarator_p;
11574 return declarator;
11577 /* Parse a ptr-operator.
11579 ptr-operator:
11580 * cv-qualifier-seq [opt]
11582 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11584 GNU Extension:
11586 ptr-operator:
11587 & cv-qualifier-seq [opt]
11589 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11590 Returns ADDR_EXPR if a reference was used. In the case of a
11591 pointer-to-member, *TYPE is filled in with the TYPE containing the
11592 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11593 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11594 ERROR_MARK if an error occurred. */
11596 static enum tree_code
11597 cp_parser_ptr_operator (cp_parser* parser,
11598 tree* type,
11599 cp_cv_quals *cv_quals)
11601 enum tree_code code = ERROR_MARK;
11602 cp_token *token;
11604 /* Assume that it's not a pointer-to-member. */
11605 *type = NULL_TREE;
11606 /* And that there are no cv-qualifiers. */
11607 *cv_quals = TYPE_UNQUALIFIED;
11609 /* Peek at the next token. */
11610 token = cp_lexer_peek_token (parser->lexer);
11611 /* If it's a `*' or `&' we have a pointer or reference. */
11612 if (token->type == CPP_MULT || token->type == CPP_AND)
11614 /* Remember which ptr-operator we were processing. */
11615 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11617 /* Consume the `*' or `&'. */
11618 cp_lexer_consume_token (parser->lexer);
11620 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11621 `&', if we are allowing GNU extensions. (The only qualifier
11622 that can legally appear after `&' is `restrict', but that is
11623 enforced during semantic analysis. */
11624 if (code == INDIRECT_REF
11625 || cp_parser_allow_gnu_extensions_p (parser))
11626 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11628 else
11630 /* Try the pointer-to-member case. */
11631 cp_parser_parse_tentatively (parser);
11632 /* Look for the optional `::' operator. */
11633 cp_parser_global_scope_opt (parser,
11634 /*current_scope_valid_p=*/false);
11635 /* Look for the nested-name specifier. */
11636 cp_parser_nested_name_specifier (parser,
11637 /*typename_keyword_p=*/false,
11638 /*check_dependency_p=*/true,
11639 /*type_p=*/false,
11640 /*is_declaration=*/false);
11641 /* If we found it, and the next token is a `*', then we are
11642 indeed looking at a pointer-to-member operator. */
11643 if (!cp_parser_error_occurred (parser)
11644 && cp_parser_require (parser, CPP_MULT, "`*'"))
11646 /* The type of which the member is a member is given by the
11647 current SCOPE. */
11648 *type = parser->scope;
11649 /* The next name will not be qualified. */
11650 parser->scope = NULL_TREE;
11651 parser->qualifying_scope = NULL_TREE;
11652 parser->object_scope = NULL_TREE;
11653 /* Indicate that the `*' operator was used. */
11654 code = INDIRECT_REF;
11655 /* Look for the optional cv-qualifier-seq. */
11656 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11658 /* If that didn't work we don't have a ptr-operator. */
11659 if (!cp_parser_parse_definitely (parser))
11660 cp_parser_error (parser, "expected ptr-operator");
11663 return code;
11666 /* Parse an (optional) cv-qualifier-seq.
11668 cv-qualifier-seq:
11669 cv-qualifier cv-qualifier-seq [opt]
11671 cv-qualifier:
11672 const
11673 volatile
11675 GNU Extension:
11677 cv-qualifier:
11678 __restrict__
11680 Returns a bitmask representing the cv-qualifiers. */
11682 static cp_cv_quals
11683 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11685 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11687 while (true)
11689 cp_token *token;
11690 cp_cv_quals cv_qualifier;
11692 /* Peek at the next token. */
11693 token = cp_lexer_peek_token (parser->lexer);
11694 /* See if it's a cv-qualifier. */
11695 switch (token->keyword)
11697 case RID_CONST:
11698 cv_qualifier = TYPE_QUAL_CONST;
11699 break;
11701 case RID_VOLATILE:
11702 cv_qualifier = TYPE_QUAL_VOLATILE;
11703 break;
11705 case RID_RESTRICT:
11706 cv_qualifier = TYPE_QUAL_RESTRICT;
11707 break;
11709 default:
11710 cv_qualifier = TYPE_UNQUALIFIED;
11711 break;
11714 if (!cv_qualifier)
11715 break;
11717 if (cv_quals & cv_qualifier)
11719 error ("duplicate cv-qualifier");
11720 cp_lexer_purge_token (parser->lexer);
11722 else
11724 cp_lexer_consume_token (parser->lexer);
11725 cv_quals |= cv_qualifier;
11729 return cv_quals;
11732 /* Parse a declarator-id.
11734 declarator-id:
11735 id-expression
11736 :: [opt] nested-name-specifier [opt] type-name
11738 In the `id-expression' case, the value returned is as for
11739 cp_parser_id_expression if the id-expression was an unqualified-id.
11740 If the id-expression was a qualified-id, then a SCOPE_REF is
11741 returned. The first operand is the scope (either a NAMESPACE_DECL
11742 or TREE_TYPE), but the second is still just a representation of an
11743 unqualified-id. */
11745 static tree
11746 cp_parser_declarator_id (cp_parser* parser)
11748 tree id;
11749 /* The expression must be an id-expression. Assume that qualified
11750 names are the names of types so that:
11752 template <class T>
11753 int S<T>::R::i = 3;
11755 will work; we must treat `S<T>::R' as the name of a type.
11756 Similarly, assume that qualified names are templates, where
11757 required, so that:
11759 template <class T>
11760 int S<T>::R<T>::i = 3;
11762 will work, too. */
11763 id = cp_parser_id_expression (parser,
11764 /*template_keyword_p=*/false,
11765 /*check_dependency_p=*/false,
11766 /*template_p=*/NULL,
11767 /*declarator_p=*/true);
11768 if (BASELINK_P (id))
11769 id = BASELINK_FUNCTIONS (id);
11770 return id;
11773 /* Parse a type-id.
11775 type-id:
11776 type-specifier-seq abstract-declarator [opt]
11778 Returns the TYPE specified. */
11780 static tree
11781 cp_parser_type_id (cp_parser* parser)
11783 cp_decl_specifier_seq type_specifier_seq;
11784 cp_declarator *abstract_declarator;
11786 /* Parse the type-specifier-seq. */
11787 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11788 &type_specifier_seq);
11789 if (type_specifier_seq.type == error_mark_node)
11790 return error_mark_node;
11792 /* There might or might not be an abstract declarator. */
11793 cp_parser_parse_tentatively (parser);
11794 /* Look for the declarator. */
11795 abstract_declarator
11796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11797 /*parenthesized_p=*/NULL,
11798 /*member_p=*/false);
11799 /* Check to see if there really was a declarator. */
11800 if (!cp_parser_parse_definitely (parser))
11801 abstract_declarator = NULL;
11803 return groktypename (&type_specifier_seq, abstract_declarator);
11806 /* Parse a type-specifier-seq.
11808 type-specifier-seq:
11809 type-specifier type-specifier-seq [opt]
11811 GNU extension:
11813 type-specifier-seq:
11814 attributes type-specifier-seq [opt]
11816 If IS_CONDITION is true, we are at the start of a "condition",
11817 e.g., we've just seen "if (".
11819 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11821 static void
11822 cp_parser_type_specifier_seq (cp_parser* parser,
11823 bool is_condition,
11824 cp_decl_specifier_seq *type_specifier_seq)
11826 bool seen_type_specifier = false;
11827 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11829 /* Clear the TYPE_SPECIFIER_SEQ. */
11830 clear_decl_specs (type_specifier_seq);
11832 /* Parse the type-specifiers and attributes. */
11833 while (true)
11835 tree type_specifier;
11836 bool is_cv_qualifier;
11838 /* Check for attributes first. */
11839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11841 type_specifier_seq->attributes =
11842 chainon (type_specifier_seq->attributes,
11843 cp_parser_attributes_opt (parser));
11844 continue;
11847 /* Look for the type-specifier. */
11848 type_specifier = cp_parser_type_specifier (parser,
11849 flags,
11850 type_specifier_seq,
11851 /*is_declaration=*/false,
11852 NULL,
11853 &is_cv_qualifier);
11854 if (!type_specifier)
11856 /* If the first type-specifier could not be found, this is not a
11857 type-specifier-seq at all. */
11858 if (!seen_type_specifier)
11860 cp_parser_error (parser, "expected type-specifier");
11861 type_specifier_seq->type = error_mark_node;
11862 return;
11864 /* If subsequent type-specifiers could not be found, the
11865 type-specifier-seq is complete. */
11866 break;
11869 seen_type_specifier = true;
11870 /* The standard says that a condition can be:
11872 type-specifier-seq declarator = assignment-expression
11874 However, given:
11876 struct S {};
11877 if (int S = ...)
11879 we should treat the "S" as a declarator, not as a
11880 type-specifier. The standard doesn't say that explicitly for
11881 type-specifier-seq, but it does say that for
11882 decl-specifier-seq in an ordinary declaration. Perhaps it
11883 would be clearer just to allow a decl-specifier-seq here, and
11884 then add a semantic restriction that if any decl-specifiers
11885 that are not type-specifiers appear, the program is invalid. */
11886 if (is_condition && !is_cv_qualifier)
11887 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11890 return;
11893 /* Parse a parameter-declaration-clause.
11895 parameter-declaration-clause:
11896 parameter-declaration-list [opt] ... [opt]
11897 parameter-declaration-list , ...
11899 Returns a representation for the parameter declarations. A return
11900 value of NULL indicates a parameter-declaration-clause consisting
11901 only of an ellipsis. */
11903 static cp_parameter_declarator *
11904 cp_parser_parameter_declaration_clause (cp_parser* parser)
11906 cp_parameter_declarator *parameters;
11907 cp_token *token;
11908 bool ellipsis_p;
11909 bool is_error;
11911 /* Peek at the next token. */
11912 token = cp_lexer_peek_token (parser->lexer);
11913 /* Check for trivial parameter-declaration-clauses. */
11914 if (token->type == CPP_ELLIPSIS)
11916 /* Consume the `...' token. */
11917 cp_lexer_consume_token (parser->lexer);
11918 return NULL;
11920 else if (token->type == CPP_CLOSE_PAREN)
11921 /* There are no parameters. */
11923 #ifndef NO_IMPLICIT_EXTERN_C
11924 if (in_system_header && current_class_type == NULL
11925 && current_lang_name == lang_name_c)
11926 return NULL;
11927 else
11928 #endif
11929 return no_parameters;
11931 /* Check for `(void)', too, which is a special case. */
11932 else if (token->keyword == RID_VOID
11933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11934 == CPP_CLOSE_PAREN))
11936 /* Consume the `void' token. */
11937 cp_lexer_consume_token (parser->lexer);
11938 /* There are no parameters. */
11939 return no_parameters;
11942 /* Parse the parameter-declaration-list. */
11943 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11944 /* If a parse error occurred while parsing the
11945 parameter-declaration-list, then the entire
11946 parameter-declaration-clause is erroneous. */
11947 if (is_error)
11948 return NULL;
11950 /* Peek at the next token. */
11951 token = cp_lexer_peek_token (parser->lexer);
11952 /* If it's a `,', the clause should terminate with an ellipsis. */
11953 if (token->type == CPP_COMMA)
11955 /* Consume the `,'. */
11956 cp_lexer_consume_token (parser->lexer);
11957 /* Expect an ellipsis. */
11958 ellipsis_p
11959 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11961 /* It might also be `...' if the optional trailing `,' was
11962 omitted. */
11963 else if (token->type == CPP_ELLIPSIS)
11965 /* Consume the `...' token. */
11966 cp_lexer_consume_token (parser->lexer);
11967 /* And remember that we saw it. */
11968 ellipsis_p = true;
11970 else
11971 ellipsis_p = false;
11973 /* Finish the parameter list. */
11974 if (parameters && ellipsis_p)
11975 parameters->ellipsis_p = true;
11977 return parameters;
11980 /* Parse a parameter-declaration-list.
11982 parameter-declaration-list:
11983 parameter-declaration
11984 parameter-declaration-list , parameter-declaration
11986 Returns a representation of the parameter-declaration-list, as for
11987 cp_parser_parameter_declaration_clause. However, the
11988 `void_list_node' is never appended to the list. Upon return,
11989 *IS_ERROR will be true iff an error occurred. */
11991 static cp_parameter_declarator *
11992 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11994 cp_parameter_declarator *parameters = NULL;
11995 cp_parameter_declarator **tail = &parameters;
11997 /* Assume all will go well. */
11998 *is_error = false;
12000 /* Look for more parameters. */
12001 while (true)
12003 cp_parameter_declarator *parameter;
12004 bool parenthesized_p;
12005 /* Parse the parameter. */
12006 parameter
12007 = cp_parser_parameter_declaration (parser,
12008 /*template_parm_p=*/false,
12009 &parenthesized_p);
12011 /* If a parse error occurred parsing the parameter declaration,
12012 then the entire parameter-declaration-list is erroneous. */
12013 if (!parameter)
12015 *is_error = true;
12016 parameters = NULL;
12017 break;
12019 /* Add the new parameter to the list. */
12020 *tail = parameter;
12021 tail = &parameter->next;
12023 /* Peek at the next token. */
12024 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12025 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12026 /* These are for Objective-C++ */
12027 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12028 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12029 /* The parameter-declaration-list is complete. */
12030 break;
12031 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12033 cp_token *token;
12035 /* Peek at the next token. */
12036 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12037 /* If it's an ellipsis, then the list is complete. */
12038 if (token->type == CPP_ELLIPSIS)
12039 break;
12040 /* Otherwise, there must be more parameters. Consume the
12041 `,'. */
12042 cp_lexer_consume_token (parser->lexer);
12043 /* When parsing something like:
12045 int i(float f, double d)
12047 we can tell after seeing the declaration for "f" that we
12048 are not looking at an initialization of a variable "i",
12049 but rather at the declaration of a function "i".
12051 Due to the fact that the parsing of template arguments
12052 (as specified to a template-id) requires backtracking we
12053 cannot use this technique when inside a template argument
12054 list. */
12055 if (!parser->in_template_argument_list_p
12056 && !parser->in_type_id_in_expr_p
12057 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12058 /* However, a parameter-declaration of the form
12059 "foat(f)" (which is a valid declaration of a
12060 parameter "f") can also be interpreted as an
12061 expression (the conversion of "f" to "float"). */
12062 && !parenthesized_p)
12063 cp_parser_commit_to_tentative_parse (parser);
12065 else
12067 cp_parser_error (parser, "expected %<,%> or %<...%>");
12068 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12069 cp_parser_skip_to_closing_parenthesis (parser,
12070 /*recovering=*/true,
12071 /*or_comma=*/false,
12072 /*consume_paren=*/false);
12073 break;
12077 return parameters;
12080 /* Parse a parameter declaration.
12082 parameter-declaration:
12083 decl-specifier-seq declarator
12084 decl-specifier-seq declarator = assignment-expression
12085 decl-specifier-seq abstract-declarator [opt]
12086 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12088 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12089 declares a template parameter. (In that case, a non-nested `>'
12090 token encountered during the parsing of the assignment-expression
12091 is not interpreted as a greater-than operator.)
12093 Returns a representation of the parameter, or NULL if an error
12094 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12095 true iff the declarator is of the form "(p)". */
12097 static cp_parameter_declarator *
12098 cp_parser_parameter_declaration (cp_parser *parser,
12099 bool template_parm_p,
12100 bool *parenthesized_p)
12102 int declares_class_or_enum;
12103 bool greater_than_is_operator_p;
12104 cp_decl_specifier_seq decl_specifiers;
12105 cp_declarator *declarator;
12106 tree default_argument;
12107 cp_token *token;
12108 const char *saved_message;
12110 /* In a template parameter, `>' is not an operator.
12112 [temp.param]
12114 When parsing a default template-argument for a non-type
12115 template-parameter, the first non-nested `>' is taken as the end
12116 of the template parameter-list rather than a greater-than
12117 operator. */
12118 greater_than_is_operator_p = !template_parm_p;
12120 /* Type definitions may not appear in parameter types. */
12121 saved_message = parser->type_definition_forbidden_message;
12122 parser->type_definition_forbidden_message
12123 = "types may not be defined in parameter types";
12125 /* Parse the declaration-specifiers. */
12126 cp_parser_decl_specifier_seq (parser,
12127 CP_PARSER_FLAGS_NONE,
12128 &decl_specifiers,
12129 &declares_class_or_enum);
12130 /* If an error occurred, there's no reason to attempt to parse the
12131 rest of the declaration. */
12132 if (cp_parser_error_occurred (parser))
12134 parser->type_definition_forbidden_message = saved_message;
12135 return NULL;
12138 /* Peek at the next token. */
12139 token = cp_lexer_peek_token (parser->lexer);
12140 /* If the next token is a `)', `,', `=', `>', or `...', then there
12141 is no declarator. */
12142 if (token->type == CPP_CLOSE_PAREN
12143 || token->type == CPP_COMMA
12144 || token->type == CPP_EQ
12145 || token->type == CPP_ELLIPSIS
12146 || token->type == CPP_GREATER)
12148 declarator = NULL;
12149 if (parenthesized_p)
12150 *parenthesized_p = false;
12152 /* Otherwise, there should be a declarator. */
12153 else
12155 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12156 parser->default_arg_ok_p = false;
12158 /* After seeing a decl-specifier-seq, if the next token is not a
12159 "(", there is no possibility that the code is a valid
12160 expression. Therefore, if parsing tentatively, we commit at
12161 this point. */
12162 if (!parser->in_template_argument_list_p
12163 /* In an expression context, having seen:
12165 (int((char ...
12167 we cannot be sure whether we are looking at a
12168 function-type (taking a "char" as a parameter) or a cast
12169 of some object of type "char" to "int". */
12170 && !parser->in_type_id_in_expr_p
12171 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12172 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12173 cp_parser_commit_to_tentative_parse (parser);
12174 /* Parse the declarator. */
12175 declarator = cp_parser_declarator (parser,
12176 CP_PARSER_DECLARATOR_EITHER,
12177 /*ctor_dtor_or_conv_p=*/NULL,
12178 parenthesized_p,
12179 /*member_p=*/false);
12180 parser->default_arg_ok_p = saved_default_arg_ok_p;
12181 /* After the declarator, allow more attributes. */
12182 decl_specifiers.attributes
12183 = chainon (decl_specifiers.attributes,
12184 cp_parser_attributes_opt (parser));
12187 /* The restriction on defining new types applies only to the type
12188 of the parameter, not to the default argument. */
12189 parser->type_definition_forbidden_message = saved_message;
12191 /* If the next token is `=', then process a default argument. */
12192 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12194 bool saved_greater_than_is_operator_p;
12195 /* Consume the `='. */
12196 cp_lexer_consume_token (parser->lexer);
12198 /* If we are defining a class, then the tokens that make up the
12199 default argument must be saved and processed later. */
12200 if (!template_parm_p && at_class_scope_p ()
12201 && TYPE_BEING_DEFINED (current_class_type))
12203 unsigned depth = 0;
12204 cp_token *first_token;
12205 cp_token *token;
12207 /* Add tokens until we have processed the entire default
12208 argument. We add the range [first_token, token). */
12209 first_token = cp_lexer_peek_token (parser->lexer);
12210 while (true)
12212 bool done = false;
12214 /* Peek at the next token. */
12215 token = cp_lexer_peek_token (parser->lexer);
12216 /* What we do depends on what token we have. */
12217 switch (token->type)
12219 /* In valid code, a default argument must be
12220 immediately followed by a `,' `)', or `...'. */
12221 case CPP_COMMA:
12222 case CPP_CLOSE_PAREN:
12223 case CPP_ELLIPSIS:
12224 /* If we run into a non-nested `;', `}', or `]',
12225 then the code is invalid -- but the default
12226 argument is certainly over. */
12227 case CPP_SEMICOLON:
12228 case CPP_CLOSE_BRACE:
12229 case CPP_CLOSE_SQUARE:
12230 if (depth == 0)
12231 done = true;
12232 /* Update DEPTH, if necessary. */
12233 else if (token->type == CPP_CLOSE_PAREN
12234 || token->type == CPP_CLOSE_BRACE
12235 || token->type == CPP_CLOSE_SQUARE)
12236 --depth;
12237 break;
12239 case CPP_OPEN_PAREN:
12240 case CPP_OPEN_SQUARE:
12241 case CPP_OPEN_BRACE:
12242 ++depth;
12243 break;
12245 case CPP_GREATER:
12246 /* If we see a non-nested `>', and `>' is not an
12247 operator, then it marks the end of the default
12248 argument. */
12249 if (!depth && !greater_than_is_operator_p)
12250 done = true;
12251 break;
12253 /* If we run out of tokens, issue an error message. */
12254 case CPP_EOF:
12255 case CPP_PRAGMA_EOL:
12256 error ("file ends in default argument");
12257 done = true;
12258 break;
12260 case CPP_NAME:
12261 case CPP_SCOPE:
12262 /* In these cases, we should look for template-ids.
12263 For example, if the default argument is
12264 `X<int, double>()', we need to do name lookup to
12265 figure out whether or not `X' is a template; if
12266 so, the `,' does not end the default argument.
12268 That is not yet done. */
12269 break;
12271 default:
12272 break;
12275 /* If we've reached the end, stop. */
12276 if (done)
12277 break;
12279 /* Add the token to the token block. */
12280 token = cp_lexer_consume_token (parser->lexer);
12283 /* Create a DEFAULT_ARG to represented the unparsed default
12284 argument. */
12285 default_argument = make_node (DEFAULT_ARG);
12286 DEFARG_TOKENS (default_argument)
12287 = cp_token_cache_new (first_token, token);
12288 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12290 /* Outside of a class definition, we can just parse the
12291 assignment-expression. */
12292 else
12294 bool saved_local_variables_forbidden_p;
12296 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12297 set correctly. */
12298 saved_greater_than_is_operator_p
12299 = parser->greater_than_is_operator_p;
12300 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12301 /* Local variable names (and the `this' keyword) may not
12302 appear in a default argument. */
12303 saved_local_variables_forbidden_p
12304 = parser->local_variables_forbidden_p;
12305 parser->local_variables_forbidden_p = true;
12306 /* Parse the assignment-expression. */
12307 default_argument
12308 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12309 /* Restore saved state. */
12310 parser->greater_than_is_operator_p
12311 = saved_greater_than_is_operator_p;
12312 parser->local_variables_forbidden_p
12313 = saved_local_variables_forbidden_p;
12315 if (!parser->default_arg_ok_p)
12317 if (!flag_pedantic_errors)
12318 warning (0, "deprecated use of default argument for parameter of non-function");
12319 else
12321 error ("default arguments are only permitted for function parameters");
12322 default_argument = NULL_TREE;
12326 else
12327 default_argument = NULL_TREE;
12329 return make_parameter_declarator (&decl_specifiers,
12330 declarator,
12331 default_argument);
12334 /* Parse a function-body.
12336 function-body:
12337 compound_statement */
12339 static void
12340 cp_parser_function_body (cp_parser *parser)
12342 cp_parser_compound_statement (parser, NULL, false);
12345 /* Parse a ctor-initializer-opt followed by a function-body. Return
12346 true if a ctor-initializer was present. */
12348 static bool
12349 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12351 tree body;
12352 bool ctor_initializer_p;
12354 /* Begin the function body. */
12355 body = begin_function_body ();
12356 /* Parse the optional ctor-initializer. */
12357 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12358 /* Parse the function-body. */
12359 cp_parser_function_body (parser);
12360 /* Finish the function body. */
12361 finish_function_body (body);
12363 return ctor_initializer_p;
12366 /* Parse an initializer.
12368 initializer:
12369 = initializer-clause
12370 ( expression-list )
12372 Returns an expression representing the initializer. If no
12373 initializer is present, NULL_TREE is returned.
12375 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12376 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12377 set to FALSE if there is no initializer present. If there is an
12378 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12379 is set to true; otherwise it is set to false. */
12381 static tree
12382 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12383 bool* non_constant_p)
12385 cp_token *token;
12386 tree init;
12388 /* Peek at the next token. */
12389 token = cp_lexer_peek_token (parser->lexer);
12391 /* Let our caller know whether or not this initializer was
12392 parenthesized. */
12393 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12394 /* Assume that the initializer is constant. */
12395 *non_constant_p = false;
12397 if (token->type == CPP_EQ)
12399 /* Consume the `='. */
12400 cp_lexer_consume_token (parser->lexer);
12401 /* Parse the initializer-clause. */
12402 init = cp_parser_initializer_clause (parser, non_constant_p);
12404 else if (token->type == CPP_OPEN_PAREN)
12405 init = cp_parser_parenthesized_expression_list (parser, false,
12406 /*cast_p=*/false,
12407 non_constant_p);
12408 else
12410 /* Anything else is an error. */
12411 cp_parser_error (parser, "expected initializer");
12412 init = error_mark_node;
12415 return init;
12418 /* Parse an initializer-clause.
12420 initializer-clause:
12421 assignment-expression
12422 { initializer-list , [opt] }
12425 Returns an expression representing the initializer.
12427 If the `assignment-expression' production is used the value
12428 returned is simply a representation for the expression.
12430 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12431 the elements of the initializer-list (or NULL, if the last
12432 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12433 NULL_TREE. There is no way to detect whether or not the optional
12434 trailing `,' was provided. NON_CONSTANT_P is as for
12435 cp_parser_initializer. */
12437 static tree
12438 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12440 tree initializer;
12442 /* Assume the expression is constant. */
12443 *non_constant_p = false;
12445 /* If it is not a `{', then we are looking at an
12446 assignment-expression. */
12447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12449 initializer
12450 = cp_parser_constant_expression (parser,
12451 /*allow_non_constant_p=*/true,
12452 non_constant_p);
12453 if (!*non_constant_p)
12454 initializer = fold_non_dependent_expr (initializer);
12456 else
12458 /* Consume the `{' token. */
12459 cp_lexer_consume_token (parser->lexer);
12460 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12461 initializer = make_node (CONSTRUCTOR);
12462 /* If it's not a `}', then there is a non-trivial initializer. */
12463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12465 /* Parse the initializer list. */
12466 CONSTRUCTOR_ELTS (initializer)
12467 = cp_parser_initializer_list (parser, non_constant_p);
12468 /* A trailing `,' token is allowed. */
12469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12470 cp_lexer_consume_token (parser->lexer);
12472 /* Now, there should be a trailing `}'. */
12473 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12476 return initializer;
12479 /* Parse an initializer-list.
12481 initializer-list:
12482 initializer-clause
12483 initializer-list , initializer-clause
12485 GNU Extension:
12487 initializer-list:
12488 identifier : initializer-clause
12489 initializer-list, identifier : initializer-clause
12491 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12492 for the initializer. If the INDEX of the elt is non-NULL, it is the
12493 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12494 as for cp_parser_initializer. */
12496 static VEC(constructor_elt,gc) *
12497 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12499 VEC(constructor_elt,gc) *v = NULL;
12501 /* Assume all of the expressions are constant. */
12502 *non_constant_p = false;
12504 /* Parse the rest of the list. */
12505 while (true)
12507 cp_token *token;
12508 tree identifier;
12509 tree initializer;
12510 bool clause_non_constant_p;
12512 /* If the next token is an identifier and the following one is a
12513 colon, we are looking at the GNU designated-initializer
12514 syntax. */
12515 if (cp_parser_allow_gnu_extensions_p (parser)
12516 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12517 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12519 /* Consume the identifier. */
12520 identifier = cp_lexer_consume_token (parser->lexer)->value;
12521 /* Consume the `:'. */
12522 cp_lexer_consume_token (parser->lexer);
12524 else
12525 identifier = NULL_TREE;
12527 /* Parse the initializer. */
12528 initializer = cp_parser_initializer_clause (parser,
12529 &clause_non_constant_p);
12530 /* If any clause is non-constant, so is the entire initializer. */
12531 if (clause_non_constant_p)
12532 *non_constant_p = true;
12534 /* Add it to the vector. */
12535 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12537 /* If the next token is not a comma, we have reached the end of
12538 the list. */
12539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12540 break;
12542 /* Peek at the next token. */
12543 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12544 /* If the next token is a `}', then we're still done. An
12545 initializer-clause can have a trailing `,' after the
12546 initializer-list and before the closing `}'. */
12547 if (token->type == CPP_CLOSE_BRACE)
12548 break;
12550 /* Consume the `,' token. */
12551 cp_lexer_consume_token (parser->lexer);
12554 return v;
12557 /* Classes [gram.class] */
12559 /* Parse a class-name.
12561 class-name:
12562 identifier
12563 template-id
12565 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12566 to indicate that names looked up in dependent types should be
12567 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12568 keyword has been used to indicate that the name that appears next
12569 is a template. TAG_TYPE indicates the explicit tag given before
12570 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12571 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12572 is the class being defined in a class-head.
12574 Returns the TYPE_DECL representing the class. */
12576 static tree
12577 cp_parser_class_name (cp_parser *parser,
12578 bool typename_keyword_p,
12579 bool template_keyword_p,
12580 enum tag_types tag_type,
12581 bool check_dependency_p,
12582 bool class_head_p,
12583 bool is_declaration)
12585 tree decl;
12586 tree scope;
12587 bool typename_p;
12588 cp_token *token;
12590 /* All class-names start with an identifier. */
12591 token = cp_lexer_peek_token (parser->lexer);
12592 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12594 cp_parser_error (parser, "expected class-name");
12595 return error_mark_node;
12598 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12599 to a template-id, so we save it here. */
12600 scope = parser->scope;
12601 if (scope == error_mark_node)
12602 return error_mark_node;
12604 /* Any name names a type if we're following the `typename' keyword
12605 in a qualified name where the enclosing scope is type-dependent. */
12606 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12607 && dependent_type_p (scope));
12608 /* Handle the common case (an identifier, but not a template-id)
12609 efficiently. */
12610 if (token->type == CPP_NAME
12611 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12613 cp_token *identifier_token;
12614 tree identifier;
12615 bool ambiguous_p;
12617 /* Look for the identifier. */
12618 identifier_token = cp_lexer_peek_token (parser->lexer);
12619 ambiguous_p = identifier_token->ambiguous_p;
12620 identifier = cp_parser_identifier (parser);
12621 /* If the next token isn't an identifier, we are certainly not
12622 looking at a class-name. */
12623 if (identifier == error_mark_node)
12624 decl = error_mark_node;
12625 /* If we know this is a type-name, there's no need to look it
12626 up. */
12627 else if (typename_p)
12628 decl = identifier;
12629 else
12631 tree ambiguous_decls;
12632 /* If we already know that this lookup is ambiguous, then
12633 we've already issued an error message; there's no reason
12634 to check again. */
12635 if (ambiguous_p)
12637 cp_parser_simulate_error (parser);
12638 return error_mark_node;
12640 /* If the next token is a `::', then the name must be a type
12641 name.
12643 [basic.lookup.qual]
12645 During the lookup for a name preceding the :: scope
12646 resolution operator, object, function, and enumerator
12647 names are ignored. */
12648 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12649 tag_type = typename_type;
12650 /* Look up the name. */
12651 decl = cp_parser_lookup_name (parser, identifier,
12652 tag_type,
12653 /*is_template=*/false,
12654 /*is_namespace=*/false,
12655 check_dependency_p,
12656 &ambiguous_decls);
12657 if (ambiguous_decls)
12659 error ("reference to %qD is ambiguous", identifier);
12660 print_candidates (ambiguous_decls);
12661 if (cp_parser_parsing_tentatively (parser))
12663 identifier_token->ambiguous_p = true;
12664 cp_parser_simulate_error (parser);
12666 return error_mark_node;
12670 else
12672 /* Try a template-id. */
12673 decl = cp_parser_template_id (parser, template_keyword_p,
12674 check_dependency_p,
12675 is_declaration);
12676 if (decl == error_mark_node)
12677 return error_mark_node;
12680 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12682 /* If this is a typename, create a TYPENAME_TYPE. */
12683 if (typename_p && decl != error_mark_node)
12685 decl = make_typename_type (scope, decl, typename_type,
12686 /*complain=*/tf_error);
12687 if (decl != error_mark_node)
12688 decl = TYPE_NAME (decl);
12691 /* Check to see that it is really the name of a class. */
12692 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12693 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12694 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12695 /* Situations like this:
12697 template <typename T> struct A {
12698 typename T::template X<int>::I i;
12701 are problematic. Is `T::template X<int>' a class-name? The
12702 standard does not seem to be definitive, but there is no other
12703 valid interpretation of the following `::'. Therefore, those
12704 names are considered class-names. */
12705 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12706 else if (decl == error_mark_node
12707 || TREE_CODE (decl) != TYPE_DECL
12708 || TREE_TYPE (decl) == error_mark_node
12709 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12711 cp_parser_error (parser, "expected class-name");
12712 return error_mark_node;
12715 return decl;
12718 /* Parse a class-specifier.
12720 class-specifier:
12721 class-head { member-specification [opt] }
12723 Returns the TREE_TYPE representing the class. */
12725 static tree
12726 cp_parser_class_specifier (cp_parser* parser)
12728 cp_token *token;
12729 tree type;
12730 tree attributes = NULL_TREE;
12731 int has_trailing_semicolon;
12732 bool nested_name_specifier_p;
12733 unsigned saved_num_template_parameter_lists;
12734 tree old_scope = NULL_TREE;
12735 tree scope = NULL_TREE;
12737 push_deferring_access_checks (dk_no_deferred);
12739 /* Parse the class-head. */
12740 type = cp_parser_class_head (parser,
12741 &nested_name_specifier_p,
12742 &attributes);
12743 /* If the class-head was a semantic disaster, skip the entire body
12744 of the class. */
12745 if (!type)
12747 cp_parser_skip_to_end_of_block_or_statement (parser);
12748 pop_deferring_access_checks ();
12749 return error_mark_node;
12752 /* Look for the `{'. */
12753 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12755 pop_deferring_access_checks ();
12756 return error_mark_node;
12759 /* Issue an error message if type-definitions are forbidden here. */
12760 cp_parser_check_type_definition (parser);
12761 /* Remember that we are defining one more class. */
12762 ++parser->num_classes_being_defined;
12763 /* Inside the class, surrounding template-parameter-lists do not
12764 apply. */
12765 saved_num_template_parameter_lists
12766 = parser->num_template_parameter_lists;
12767 parser->num_template_parameter_lists = 0;
12769 /* Start the class. */
12770 if (nested_name_specifier_p)
12772 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12773 old_scope = push_inner_scope (scope);
12775 type = begin_class_definition (type);
12777 if (type == error_mark_node)
12778 /* If the type is erroneous, skip the entire body of the class. */
12779 cp_parser_skip_to_closing_brace (parser);
12780 else
12781 /* Parse the member-specification. */
12782 cp_parser_member_specification_opt (parser);
12784 /* Look for the trailing `}'. */
12785 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12786 /* We get better error messages by noticing a common problem: a
12787 missing trailing `;'. */
12788 token = cp_lexer_peek_token (parser->lexer);
12789 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12790 /* Look for trailing attributes to apply to this class. */
12791 if (cp_parser_allow_gnu_extensions_p (parser))
12793 tree sub_attr = cp_parser_attributes_opt (parser);
12794 attributes = chainon (attributes, sub_attr);
12796 if (type != error_mark_node)
12797 type = finish_struct (type, attributes);
12798 if (nested_name_specifier_p)
12799 pop_inner_scope (old_scope, scope);
12800 /* If this class is not itself within the scope of another class,
12801 then we need to parse the bodies of all of the queued function
12802 definitions. Note that the queued functions defined in a class
12803 are not always processed immediately following the
12804 class-specifier for that class. Consider:
12806 struct A {
12807 struct B { void f() { sizeof (A); } };
12810 If `f' were processed before the processing of `A' were
12811 completed, there would be no way to compute the size of `A'.
12812 Note that the nesting we are interested in here is lexical --
12813 not the semantic nesting given by TYPE_CONTEXT. In particular,
12814 for:
12816 struct A { struct B; };
12817 struct A::B { void f() { } };
12819 there is no need to delay the parsing of `A::B::f'. */
12820 if (--parser->num_classes_being_defined == 0)
12822 tree queue_entry;
12823 tree fn;
12824 tree class_type = NULL_TREE;
12825 tree pushed_scope = NULL_TREE;
12827 /* In a first pass, parse default arguments to the functions.
12828 Then, in a second pass, parse the bodies of the functions.
12829 This two-phased approach handles cases like:
12831 struct S {
12832 void f() { g(); }
12833 void g(int i = 3);
12837 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12838 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12839 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12840 TREE_PURPOSE (parser->unparsed_functions_queues)
12841 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12843 fn = TREE_VALUE (queue_entry);
12844 /* If there are default arguments that have not yet been processed,
12845 take care of them now. */
12846 if (class_type != TREE_PURPOSE (queue_entry))
12848 if (pushed_scope)
12849 pop_scope (pushed_scope);
12850 class_type = TREE_PURPOSE (queue_entry);
12851 pushed_scope = push_scope (class_type);
12853 /* Make sure that any template parameters are in scope. */
12854 maybe_begin_member_template_processing (fn);
12855 /* Parse the default argument expressions. */
12856 cp_parser_late_parsing_default_args (parser, fn);
12857 /* Remove any template parameters from the symbol table. */
12858 maybe_end_member_template_processing ();
12860 if (pushed_scope)
12861 pop_scope (pushed_scope);
12862 /* Now parse the body of the functions. */
12863 for (TREE_VALUE (parser->unparsed_functions_queues)
12864 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12865 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12866 TREE_VALUE (parser->unparsed_functions_queues)
12867 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12869 /* Figure out which function we need to process. */
12870 fn = TREE_VALUE (queue_entry);
12871 /* Parse the function. */
12872 cp_parser_late_parsing_for_member (parser, fn);
12876 /* Put back any saved access checks. */
12877 pop_deferring_access_checks ();
12879 /* Restore the count of active template-parameter-lists. */
12880 parser->num_template_parameter_lists
12881 = saved_num_template_parameter_lists;
12883 return type;
12886 /* Parse a class-head.
12888 class-head:
12889 class-key identifier [opt] base-clause [opt]
12890 class-key nested-name-specifier identifier base-clause [opt]
12891 class-key nested-name-specifier [opt] template-id
12892 base-clause [opt]
12894 GNU Extensions:
12895 class-key attributes identifier [opt] base-clause [opt]
12896 class-key attributes nested-name-specifier identifier base-clause [opt]
12897 class-key attributes nested-name-specifier [opt] template-id
12898 base-clause [opt]
12900 Returns the TYPE of the indicated class. Sets
12901 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12902 involving a nested-name-specifier was used, and FALSE otherwise.
12904 Returns error_mark_node if this is not a class-head.
12906 Returns NULL_TREE if the class-head is syntactically valid, but
12907 semantically invalid in a way that means we should skip the entire
12908 body of the class. */
12910 static tree
12911 cp_parser_class_head (cp_parser* parser,
12912 bool* nested_name_specifier_p,
12913 tree *attributes_p)
12915 tree nested_name_specifier;
12916 enum tag_types class_key;
12917 tree id = NULL_TREE;
12918 tree type = NULL_TREE;
12919 tree attributes;
12920 bool template_id_p = false;
12921 bool qualified_p = false;
12922 bool invalid_nested_name_p = false;
12923 bool invalid_explicit_specialization_p = false;
12924 tree pushed_scope = NULL_TREE;
12925 unsigned num_templates;
12926 tree bases;
12928 /* Assume no nested-name-specifier will be present. */
12929 *nested_name_specifier_p = false;
12930 /* Assume no template parameter lists will be used in defining the
12931 type. */
12932 num_templates = 0;
12934 /* Look for the class-key. */
12935 class_key = cp_parser_class_key (parser);
12936 if (class_key == none_type)
12937 return error_mark_node;
12939 /* Parse the attributes. */
12940 attributes = cp_parser_attributes_opt (parser);
12942 /* If the next token is `::', that is invalid -- but sometimes
12943 people do try to write:
12945 struct ::S {};
12947 Handle this gracefully by accepting the extra qualifier, and then
12948 issuing an error about it later if this really is a
12949 class-head. If it turns out just to be an elaborated type
12950 specifier, remain silent. */
12951 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12952 qualified_p = true;
12954 push_deferring_access_checks (dk_no_check);
12956 /* Determine the name of the class. Begin by looking for an
12957 optional nested-name-specifier. */
12958 nested_name_specifier
12959 = cp_parser_nested_name_specifier_opt (parser,
12960 /*typename_keyword_p=*/false,
12961 /*check_dependency_p=*/false,
12962 /*type_p=*/false,
12963 /*is_declaration=*/false);
12964 /* If there was a nested-name-specifier, then there *must* be an
12965 identifier. */
12966 if (nested_name_specifier)
12968 /* Although the grammar says `identifier', it really means
12969 `class-name' or `template-name'. You are only allowed to
12970 define a class that has already been declared with this
12971 syntax.
12973 The proposed resolution for Core Issue 180 says that whever
12974 you see `class T::X' you should treat `X' as a type-name.
12976 It is OK to define an inaccessible class; for example:
12978 class A { class B; };
12979 class A::B {};
12981 We do not know if we will see a class-name, or a
12982 template-name. We look for a class-name first, in case the
12983 class-name is a template-id; if we looked for the
12984 template-name first we would stop after the template-name. */
12985 cp_parser_parse_tentatively (parser);
12986 type = cp_parser_class_name (parser,
12987 /*typename_keyword_p=*/false,
12988 /*template_keyword_p=*/false,
12989 class_type,
12990 /*check_dependency_p=*/false,
12991 /*class_head_p=*/true,
12992 /*is_declaration=*/false);
12993 /* If that didn't work, ignore the nested-name-specifier. */
12994 if (!cp_parser_parse_definitely (parser))
12996 invalid_nested_name_p = true;
12997 id = cp_parser_identifier (parser);
12998 if (id == error_mark_node)
12999 id = NULL_TREE;
13001 /* If we could not find a corresponding TYPE, treat this
13002 declaration like an unqualified declaration. */
13003 if (type == error_mark_node)
13004 nested_name_specifier = NULL_TREE;
13005 /* Otherwise, count the number of templates used in TYPE and its
13006 containing scopes. */
13007 else
13009 tree scope;
13011 for (scope = TREE_TYPE (type);
13012 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13013 scope = (TYPE_P (scope)
13014 ? TYPE_CONTEXT (scope)
13015 : DECL_CONTEXT (scope)))
13016 if (TYPE_P (scope)
13017 && CLASS_TYPE_P (scope)
13018 && CLASSTYPE_TEMPLATE_INFO (scope)
13019 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13020 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13021 ++num_templates;
13024 /* Otherwise, the identifier is optional. */
13025 else
13027 /* We don't know whether what comes next is a template-id,
13028 an identifier, or nothing at all. */
13029 cp_parser_parse_tentatively (parser);
13030 /* Check for a template-id. */
13031 id = cp_parser_template_id (parser,
13032 /*template_keyword_p=*/false,
13033 /*check_dependency_p=*/true,
13034 /*is_declaration=*/true);
13035 /* If that didn't work, it could still be an identifier. */
13036 if (!cp_parser_parse_definitely (parser))
13038 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13039 id = cp_parser_identifier (parser);
13040 else
13041 id = NULL_TREE;
13043 else
13045 template_id_p = true;
13046 ++num_templates;
13050 pop_deferring_access_checks ();
13052 if (id)
13053 cp_parser_check_for_invalid_template_id (parser, id);
13055 /* If it's not a `:' or a `{' then we can't really be looking at a
13056 class-head, since a class-head only appears as part of a
13057 class-specifier. We have to detect this situation before calling
13058 xref_tag, since that has irreversible side-effects. */
13059 if (!cp_parser_next_token_starts_class_definition_p (parser))
13061 cp_parser_error (parser, "expected %<{%> or %<:%>");
13062 return error_mark_node;
13065 /* At this point, we're going ahead with the class-specifier, even
13066 if some other problem occurs. */
13067 cp_parser_commit_to_tentative_parse (parser);
13068 /* Issue the error about the overly-qualified name now. */
13069 if (qualified_p)
13070 cp_parser_error (parser,
13071 "global qualification of class name is invalid");
13072 else if (invalid_nested_name_p)
13073 cp_parser_error (parser,
13074 "qualified name does not name a class");
13075 else if (nested_name_specifier)
13077 tree scope;
13079 /* Reject typedef-names in class heads. */
13080 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13082 error ("invalid class name in declaration of %qD", type);
13083 type = NULL_TREE;
13084 goto done;
13087 /* Figure out in what scope the declaration is being placed. */
13088 scope = current_scope ();
13089 /* If that scope does not contain the scope in which the
13090 class was originally declared, the program is invalid. */
13091 if (scope && !is_ancestor (scope, nested_name_specifier))
13093 error ("declaration of %qD in %qD which does not enclose %qD",
13094 type, scope, nested_name_specifier);
13095 type = NULL_TREE;
13096 goto done;
13098 /* [dcl.meaning]
13100 A declarator-id shall not be qualified exception of the
13101 definition of a ... nested class outside of its class
13102 ... [or] a the definition or explicit instantiation of a
13103 class member of a namespace outside of its namespace. */
13104 if (scope == nested_name_specifier)
13106 pedwarn ("extra qualification ignored");
13107 nested_name_specifier = NULL_TREE;
13108 num_templates = 0;
13111 /* An explicit-specialization must be preceded by "template <>". If
13112 it is not, try to recover gracefully. */
13113 if (at_namespace_scope_p ()
13114 && parser->num_template_parameter_lists == 0
13115 && template_id_p)
13117 error ("an explicit specialization must be preceded by %<template <>%>");
13118 invalid_explicit_specialization_p = true;
13119 /* Take the same action that would have been taken by
13120 cp_parser_explicit_specialization. */
13121 ++parser->num_template_parameter_lists;
13122 begin_specialization ();
13124 /* There must be no "return" statements between this point and the
13125 end of this function; set "type "to the correct return value and
13126 use "goto done;" to return. */
13127 /* Make sure that the right number of template parameters were
13128 present. */
13129 if (!cp_parser_check_template_parameters (parser, num_templates))
13131 /* If something went wrong, there is no point in even trying to
13132 process the class-definition. */
13133 type = NULL_TREE;
13134 goto done;
13137 /* Look up the type. */
13138 if (template_id_p)
13140 type = TREE_TYPE (id);
13141 maybe_process_partial_specialization (type);
13142 if (nested_name_specifier)
13143 pushed_scope = push_scope (nested_name_specifier);
13145 else if (nested_name_specifier)
13147 tree class_type;
13149 /* Given:
13151 template <typename T> struct S { struct T };
13152 template <typename T> struct S<T>::T { };
13154 we will get a TYPENAME_TYPE when processing the definition of
13155 `S::T'. We need to resolve it to the actual type before we
13156 try to define it. */
13157 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13159 class_type = resolve_typename_type (TREE_TYPE (type),
13160 /*only_current_p=*/false);
13161 if (class_type != error_mark_node)
13162 type = TYPE_NAME (class_type);
13163 else
13165 cp_parser_error (parser, "could not resolve typename type");
13166 type = error_mark_node;
13170 maybe_process_partial_specialization (TREE_TYPE (type));
13171 class_type = current_class_type;
13172 /* Enter the scope indicated by the nested-name-specifier. */
13173 pushed_scope = push_scope (nested_name_specifier);
13174 /* Get the canonical version of this type. */
13175 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13176 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13177 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13179 type = push_template_decl (type);
13180 if (type == error_mark_node)
13182 type = NULL_TREE;
13183 goto done;
13187 type = TREE_TYPE (type);
13188 *nested_name_specifier_p = true;
13190 else /* The name is not a nested name. */
13192 /* If the class was unnamed, create a dummy name. */
13193 if (!id)
13194 id = make_anon_name ();
13195 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13196 parser->num_template_parameter_lists);
13199 /* Indicate whether this class was declared as a `class' or as a
13200 `struct'. */
13201 if (TREE_CODE (type) == RECORD_TYPE)
13202 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13203 cp_parser_check_class_key (class_key, type);
13205 /* If this type was already complete, and we see another definition,
13206 that's an error. */
13207 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13209 error ("redefinition of %q#T", type);
13210 error ("previous definition of %q+#T", type);
13211 type = NULL_TREE;
13212 goto done;
13215 /* We will have entered the scope containing the class; the names of
13216 base classes should be looked up in that context. For example:
13218 struct A { struct B {}; struct C; };
13219 struct A::C : B {};
13221 is valid. */
13222 bases = NULL_TREE;
13224 /* Get the list of base-classes, if there is one. */
13225 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13226 bases = cp_parser_base_clause (parser);
13228 /* Process the base classes. */
13229 xref_basetypes (type, bases);
13231 done:
13232 /* Leave the scope given by the nested-name-specifier. We will
13233 enter the class scope itself while processing the members. */
13234 if (pushed_scope)
13235 pop_scope (pushed_scope);
13237 if (invalid_explicit_specialization_p)
13239 end_specialization ();
13240 --parser->num_template_parameter_lists;
13242 *attributes_p = attributes;
13243 return type;
13246 /* Parse a class-key.
13248 class-key:
13249 class
13250 struct
13251 union
13253 Returns the kind of class-key specified, or none_type to indicate
13254 error. */
13256 static enum tag_types
13257 cp_parser_class_key (cp_parser* parser)
13259 cp_token *token;
13260 enum tag_types tag_type;
13262 /* Look for the class-key. */
13263 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13264 if (!token)
13265 return none_type;
13267 /* Check to see if the TOKEN is a class-key. */
13268 tag_type = cp_parser_token_is_class_key (token);
13269 if (!tag_type)
13270 cp_parser_error (parser, "expected class-key");
13271 return tag_type;
13274 /* Parse an (optional) member-specification.
13276 member-specification:
13277 member-declaration member-specification [opt]
13278 access-specifier : member-specification [opt] */
13280 static void
13281 cp_parser_member_specification_opt (cp_parser* parser)
13283 while (true)
13285 cp_token *token;
13286 enum rid keyword;
13288 /* Peek at the next token. */
13289 token = cp_lexer_peek_token (parser->lexer);
13290 /* If it's a `}', or EOF then we've seen all the members. */
13291 if (token->type == CPP_CLOSE_BRACE
13292 || token->type == CPP_EOF
13293 || token->type == CPP_PRAGMA_EOL)
13294 break;
13296 /* See if this token is a keyword. */
13297 keyword = token->keyword;
13298 switch (keyword)
13300 case RID_PUBLIC:
13301 case RID_PROTECTED:
13302 case RID_PRIVATE:
13303 /* Consume the access-specifier. */
13304 cp_lexer_consume_token (parser->lexer);
13305 /* Remember which access-specifier is active. */
13306 current_access_specifier = token->value;
13307 /* Look for the `:'. */
13308 cp_parser_require (parser, CPP_COLON, "`:'");
13309 break;
13311 default:
13312 /* Accept #pragmas at class scope. */
13313 if (token->type == CPP_PRAGMA)
13315 cp_parser_pragma (parser, pragma_external);
13316 break;
13319 /* Otherwise, the next construction must be a
13320 member-declaration. */
13321 cp_parser_member_declaration (parser);
13326 /* Parse a member-declaration.
13328 member-declaration:
13329 decl-specifier-seq [opt] member-declarator-list [opt] ;
13330 function-definition ; [opt]
13331 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13332 using-declaration
13333 template-declaration
13335 member-declarator-list:
13336 member-declarator
13337 member-declarator-list , member-declarator
13339 member-declarator:
13340 declarator pure-specifier [opt]
13341 declarator constant-initializer [opt]
13342 identifier [opt] : constant-expression
13344 GNU Extensions:
13346 member-declaration:
13347 __extension__ member-declaration
13349 member-declarator:
13350 declarator attributes [opt] pure-specifier [opt]
13351 declarator attributes [opt] constant-initializer [opt]
13352 identifier [opt] attributes [opt] : constant-expression */
13354 static void
13355 cp_parser_member_declaration (cp_parser* parser)
13357 cp_decl_specifier_seq decl_specifiers;
13358 tree prefix_attributes;
13359 tree decl;
13360 int declares_class_or_enum;
13361 bool friend_p;
13362 cp_token *token;
13363 int saved_pedantic;
13365 /* Check for the `__extension__' keyword. */
13366 if (cp_parser_extension_opt (parser, &saved_pedantic))
13368 /* Recurse. */
13369 cp_parser_member_declaration (parser);
13370 /* Restore the old value of the PEDANTIC flag. */
13371 pedantic = saved_pedantic;
13373 return;
13376 /* Check for a template-declaration. */
13377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13379 /* An explicit specialization here is an error condition, and we
13380 expect the specialization handler to detect and report this. */
13381 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13382 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13383 cp_parser_explicit_specialization (parser);
13384 else
13385 cp_parser_template_declaration (parser, /*member_p=*/true);
13387 return;
13390 /* Check for a using-declaration. */
13391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13393 /* Parse the using-declaration. */
13394 cp_parser_using_declaration (parser);
13396 return;
13399 /* Check for @defs. */
13400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13402 tree ivar, member;
13403 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13404 ivar = ivar_chains;
13405 while (ivar)
13407 member = ivar;
13408 ivar = TREE_CHAIN (member);
13409 TREE_CHAIN (member) = NULL_TREE;
13410 finish_member_declaration (member);
13412 return;
13415 /* Parse the decl-specifier-seq. */
13416 cp_parser_decl_specifier_seq (parser,
13417 CP_PARSER_FLAGS_OPTIONAL,
13418 &decl_specifiers,
13419 &declares_class_or_enum);
13420 prefix_attributes = decl_specifiers.attributes;
13421 decl_specifiers.attributes = NULL_TREE;
13422 /* Check for an invalid type-name. */
13423 if (!decl_specifiers.type
13424 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13425 return;
13426 /* If there is no declarator, then the decl-specifier-seq should
13427 specify a type. */
13428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13430 /* If there was no decl-specifier-seq, and the next token is a
13431 `;', then we have something like:
13433 struct S { ; };
13435 [class.mem]
13437 Each member-declaration shall declare at least one member
13438 name of the class. */
13439 if (!decl_specifiers.any_specifiers_p)
13441 cp_token *token = cp_lexer_peek_token (parser->lexer);
13442 if (pedantic && !token->in_system_header)
13443 pedwarn ("%Hextra %<;%>", &token->location);
13445 else
13447 tree type;
13449 /* See if this declaration is a friend. */
13450 friend_p = cp_parser_friend_p (&decl_specifiers);
13451 /* If there were decl-specifiers, check to see if there was
13452 a class-declaration. */
13453 type = check_tag_decl (&decl_specifiers);
13454 /* Nested classes have already been added to the class, but
13455 a `friend' needs to be explicitly registered. */
13456 if (friend_p)
13458 /* If the `friend' keyword was present, the friend must
13459 be introduced with a class-key. */
13460 if (!declares_class_or_enum)
13461 error ("a class-key must be used when declaring a friend");
13462 /* In this case:
13464 template <typename T> struct A {
13465 friend struct A<T>::B;
13468 A<T>::B will be represented by a TYPENAME_TYPE, and
13469 therefore not recognized by check_tag_decl. */
13470 if (!type
13471 && decl_specifiers.type
13472 && TYPE_P (decl_specifiers.type))
13473 type = decl_specifiers.type;
13474 if (!type || !TYPE_P (type))
13475 error ("friend declaration does not name a class or "
13476 "function");
13477 else
13478 make_friend_class (current_class_type, type,
13479 /*complain=*/true);
13481 /* If there is no TYPE, an error message will already have
13482 been issued. */
13483 else if (!type || type == error_mark_node)
13485 /* An anonymous aggregate has to be handled specially; such
13486 a declaration really declares a data member (with a
13487 particular type), as opposed to a nested class. */
13488 else if (ANON_AGGR_TYPE_P (type))
13490 /* Remove constructors and such from TYPE, now that we
13491 know it is an anonymous aggregate. */
13492 fixup_anonymous_aggr (type);
13493 /* And make the corresponding data member. */
13494 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13495 /* Add it to the class. */
13496 finish_member_declaration (decl);
13498 else
13499 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13502 else
13504 /* See if these declarations will be friends. */
13505 friend_p = cp_parser_friend_p (&decl_specifiers);
13507 /* Keep going until we hit the `;' at the end of the
13508 declaration. */
13509 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13511 tree attributes = NULL_TREE;
13512 tree first_attribute;
13514 /* Peek at the next token. */
13515 token = cp_lexer_peek_token (parser->lexer);
13517 /* Check for a bitfield declaration. */
13518 if (token->type == CPP_COLON
13519 || (token->type == CPP_NAME
13520 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13521 == CPP_COLON))
13523 tree identifier;
13524 tree width;
13526 /* Get the name of the bitfield. Note that we cannot just
13527 check TOKEN here because it may have been invalidated by
13528 the call to cp_lexer_peek_nth_token above. */
13529 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13530 identifier = cp_parser_identifier (parser);
13531 else
13532 identifier = NULL_TREE;
13534 /* Consume the `:' token. */
13535 cp_lexer_consume_token (parser->lexer);
13536 /* Get the width of the bitfield. */
13537 width
13538 = cp_parser_constant_expression (parser,
13539 /*allow_non_constant=*/false,
13540 NULL);
13542 /* Look for attributes that apply to the bitfield. */
13543 attributes = cp_parser_attributes_opt (parser);
13544 /* Remember which attributes are prefix attributes and
13545 which are not. */
13546 first_attribute = attributes;
13547 /* Combine the attributes. */
13548 attributes = chainon (prefix_attributes, attributes);
13550 /* Create the bitfield declaration. */
13551 decl = grokbitfield (identifier
13552 ? make_id_declarator (NULL_TREE,
13553 identifier,
13554 sfk_none)
13555 : NULL,
13556 &decl_specifiers,
13557 width);
13558 /* Apply the attributes. */
13559 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13561 else
13563 cp_declarator *declarator;
13564 tree initializer;
13565 tree asm_specification;
13566 int ctor_dtor_or_conv_p;
13568 /* Parse the declarator. */
13569 declarator
13570 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13571 &ctor_dtor_or_conv_p,
13572 /*parenthesized_p=*/NULL,
13573 /*member_p=*/true);
13575 /* If something went wrong parsing the declarator, make sure
13576 that we at least consume some tokens. */
13577 if (declarator == cp_error_declarator)
13579 /* Skip to the end of the statement. */
13580 cp_parser_skip_to_end_of_statement (parser);
13581 /* If the next token is not a semicolon, that is
13582 probably because we just skipped over the body of
13583 a function. So, we consume a semicolon if
13584 present, but do not issue an error message if it
13585 is not present. */
13586 if (cp_lexer_next_token_is (parser->lexer,
13587 CPP_SEMICOLON))
13588 cp_lexer_consume_token (parser->lexer);
13589 return;
13592 if (declares_class_or_enum & 2)
13593 cp_parser_check_for_definition_in_return_type
13594 (declarator, decl_specifiers.type);
13596 /* Look for an asm-specification. */
13597 asm_specification = cp_parser_asm_specification_opt (parser);
13598 /* Look for attributes that apply to the declaration. */
13599 attributes = cp_parser_attributes_opt (parser);
13600 /* Remember which attributes are prefix attributes and
13601 which are not. */
13602 first_attribute = attributes;
13603 /* Combine the attributes. */
13604 attributes = chainon (prefix_attributes, attributes);
13606 /* If it's an `=', then we have a constant-initializer or a
13607 pure-specifier. It is not correct to parse the
13608 initializer before registering the member declaration
13609 since the member declaration should be in scope while
13610 its initializer is processed. However, the rest of the
13611 front end does not yet provide an interface that allows
13612 us to handle this correctly. */
13613 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13615 /* In [class.mem]:
13617 A pure-specifier shall be used only in the declaration of
13618 a virtual function.
13620 A member-declarator can contain a constant-initializer
13621 only if it declares a static member of integral or
13622 enumeration type.
13624 Therefore, if the DECLARATOR is for a function, we look
13625 for a pure-specifier; otherwise, we look for a
13626 constant-initializer. When we call `grokfield', it will
13627 perform more stringent semantics checks. */
13628 if (declarator->kind == cdk_function)
13629 initializer = cp_parser_pure_specifier (parser);
13630 else
13631 /* Parse the initializer. */
13632 initializer = cp_parser_constant_initializer (parser);
13634 /* Otherwise, there is no initializer. */
13635 else
13636 initializer = NULL_TREE;
13638 /* See if we are probably looking at a function
13639 definition. We are certainly not looking at a
13640 member-declarator. Calling `grokfield' has
13641 side-effects, so we must not do it unless we are sure
13642 that we are looking at a member-declarator. */
13643 if (cp_parser_token_starts_function_definition_p
13644 (cp_lexer_peek_token (parser->lexer)))
13646 /* The grammar does not allow a pure-specifier to be
13647 used when a member function is defined. (It is
13648 possible that this fact is an oversight in the
13649 standard, since a pure function may be defined
13650 outside of the class-specifier. */
13651 if (initializer)
13652 error ("pure-specifier on function-definition");
13653 decl = cp_parser_save_member_function_body (parser,
13654 &decl_specifiers,
13655 declarator,
13656 attributes);
13657 /* If the member was not a friend, declare it here. */
13658 if (!friend_p)
13659 finish_member_declaration (decl);
13660 /* Peek at the next token. */
13661 token = cp_lexer_peek_token (parser->lexer);
13662 /* If the next token is a semicolon, consume it. */
13663 if (token->type == CPP_SEMICOLON)
13664 cp_lexer_consume_token (parser->lexer);
13665 return;
13667 else
13669 /* Create the declaration. */
13670 decl = grokfield (declarator, &decl_specifiers,
13671 initializer, asm_specification,
13672 attributes);
13673 /* Any initialization must have been from a
13674 constant-expression. */
13675 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13676 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13680 /* Reset PREFIX_ATTRIBUTES. */
13681 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13682 attributes = TREE_CHAIN (attributes);
13683 if (attributes)
13684 TREE_CHAIN (attributes) = NULL_TREE;
13686 /* If there is any qualification still in effect, clear it
13687 now; we will be starting fresh with the next declarator. */
13688 parser->scope = NULL_TREE;
13689 parser->qualifying_scope = NULL_TREE;
13690 parser->object_scope = NULL_TREE;
13691 /* If it's a `,', then there are more declarators. */
13692 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13693 cp_lexer_consume_token (parser->lexer);
13694 /* If the next token isn't a `;', then we have a parse error. */
13695 else if (cp_lexer_next_token_is_not (parser->lexer,
13696 CPP_SEMICOLON))
13698 cp_parser_error (parser, "expected %<;%>");
13699 /* Skip tokens until we find a `;'. */
13700 cp_parser_skip_to_end_of_statement (parser);
13702 break;
13705 if (decl)
13707 /* Add DECL to the list of members. */
13708 if (!friend_p)
13709 finish_member_declaration (decl);
13711 if (TREE_CODE (decl) == FUNCTION_DECL)
13712 cp_parser_save_default_args (parser, decl);
13717 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13720 /* Parse a pure-specifier.
13722 pure-specifier:
13725 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13726 Otherwise, ERROR_MARK_NODE is returned. */
13728 static tree
13729 cp_parser_pure_specifier (cp_parser* parser)
13731 cp_token *token;
13733 /* Look for the `=' token. */
13734 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13735 return error_mark_node;
13736 /* Look for the `0' token. */
13737 token = cp_lexer_consume_token (parser->lexer);
13738 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13739 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13740 return integer_zero_node;
13742 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13743 cp_parser_skip_to_end_of_statement (parser);
13744 return error_mark_node;
13747 /* Parse a constant-initializer.
13749 constant-initializer:
13750 = constant-expression
13752 Returns a representation of the constant-expression. */
13754 static tree
13755 cp_parser_constant_initializer (cp_parser* parser)
13757 /* Look for the `=' token. */
13758 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13759 return error_mark_node;
13761 /* It is invalid to write:
13763 struct S { static const int i = { 7 }; };
13766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13768 cp_parser_error (parser,
13769 "a brace-enclosed initializer is not allowed here");
13770 /* Consume the opening brace. */
13771 cp_lexer_consume_token (parser->lexer);
13772 /* Skip the initializer. */
13773 cp_parser_skip_to_closing_brace (parser);
13774 /* Look for the trailing `}'. */
13775 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13777 return error_mark_node;
13780 return cp_parser_constant_expression (parser,
13781 /*allow_non_constant=*/false,
13782 NULL);
13785 /* Derived classes [gram.class.derived] */
13787 /* Parse a base-clause.
13789 base-clause:
13790 : base-specifier-list
13792 base-specifier-list:
13793 base-specifier
13794 base-specifier-list , base-specifier
13796 Returns a TREE_LIST representing the base-classes, in the order in
13797 which they were declared. The representation of each node is as
13798 described by cp_parser_base_specifier.
13800 In the case that no bases are specified, this function will return
13801 NULL_TREE, not ERROR_MARK_NODE. */
13803 static tree
13804 cp_parser_base_clause (cp_parser* parser)
13806 tree bases = NULL_TREE;
13808 /* Look for the `:' that begins the list. */
13809 cp_parser_require (parser, CPP_COLON, "`:'");
13811 /* Scan the base-specifier-list. */
13812 while (true)
13814 cp_token *token;
13815 tree base;
13817 /* Look for the base-specifier. */
13818 base = cp_parser_base_specifier (parser);
13819 /* Add BASE to the front of the list. */
13820 if (base != error_mark_node)
13822 TREE_CHAIN (base) = bases;
13823 bases = base;
13825 /* Peek at the next token. */
13826 token = cp_lexer_peek_token (parser->lexer);
13827 /* If it's not a comma, then the list is complete. */
13828 if (token->type != CPP_COMMA)
13829 break;
13830 /* Consume the `,'. */
13831 cp_lexer_consume_token (parser->lexer);
13834 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13835 base class had a qualified name. However, the next name that
13836 appears is certainly not qualified. */
13837 parser->scope = NULL_TREE;
13838 parser->qualifying_scope = NULL_TREE;
13839 parser->object_scope = NULL_TREE;
13841 return nreverse (bases);
13844 /* Parse a base-specifier.
13846 base-specifier:
13847 :: [opt] nested-name-specifier [opt] class-name
13848 virtual access-specifier [opt] :: [opt] nested-name-specifier
13849 [opt] class-name
13850 access-specifier virtual [opt] :: [opt] nested-name-specifier
13851 [opt] class-name
13853 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13854 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13855 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13856 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13858 static tree
13859 cp_parser_base_specifier (cp_parser* parser)
13861 cp_token *token;
13862 bool done = false;
13863 bool virtual_p = false;
13864 bool duplicate_virtual_error_issued_p = false;
13865 bool duplicate_access_error_issued_p = false;
13866 bool class_scope_p, template_p;
13867 tree access = access_default_node;
13868 tree type;
13870 /* Process the optional `virtual' and `access-specifier'. */
13871 while (!done)
13873 /* Peek at the next token. */
13874 token = cp_lexer_peek_token (parser->lexer);
13875 /* Process `virtual'. */
13876 switch (token->keyword)
13878 case RID_VIRTUAL:
13879 /* If `virtual' appears more than once, issue an error. */
13880 if (virtual_p && !duplicate_virtual_error_issued_p)
13882 cp_parser_error (parser,
13883 "%<virtual%> specified more than once in base-specified");
13884 duplicate_virtual_error_issued_p = true;
13887 virtual_p = true;
13889 /* Consume the `virtual' token. */
13890 cp_lexer_consume_token (parser->lexer);
13892 break;
13894 case RID_PUBLIC:
13895 case RID_PROTECTED:
13896 case RID_PRIVATE:
13897 /* If more than one access specifier appears, issue an
13898 error. */
13899 if (access != access_default_node
13900 && !duplicate_access_error_issued_p)
13902 cp_parser_error (parser,
13903 "more than one access specifier in base-specified");
13904 duplicate_access_error_issued_p = true;
13907 access = ridpointers[(int) token->keyword];
13909 /* Consume the access-specifier. */
13910 cp_lexer_consume_token (parser->lexer);
13912 break;
13914 default:
13915 done = true;
13916 break;
13919 /* It is not uncommon to see programs mechanically, erroneously, use
13920 the 'typename' keyword to denote (dependent) qualified types
13921 as base classes. */
13922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13924 if (!processing_template_decl)
13925 error ("keyword %<typename%> not allowed outside of templates");
13926 else
13927 error ("keyword %<typename%> not allowed in this context "
13928 "(the base class is implicitly a type)");
13929 cp_lexer_consume_token (parser->lexer);
13932 /* Look for the optional `::' operator. */
13933 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13934 /* Look for the nested-name-specifier. The simplest way to
13935 implement:
13937 [temp.res]
13939 The keyword `typename' is not permitted in a base-specifier or
13940 mem-initializer; in these contexts a qualified name that
13941 depends on a template-parameter is implicitly assumed to be a
13942 type name.
13944 is to pretend that we have seen the `typename' keyword at this
13945 point. */
13946 cp_parser_nested_name_specifier_opt (parser,
13947 /*typename_keyword_p=*/true,
13948 /*check_dependency_p=*/true,
13949 typename_type,
13950 /*is_declaration=*/true);
13951 /* If the base class is given by a qualified name, assume that names
13952 we see are type names or templates, as appropriate. */
13953 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13954 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13956 /* Finally, look for the class-name. */
13957 type = cp_parser_class_name (parser,
13958 class_scope_p,
13959 template_p,
13960 typename_type,
13961 /*check_dependency_p=*/true,
13962 /*class_head_p=*/false,
13963 /*is_declaration=*/true);
13965 if (type == error_mark_node)
13966 return error_mark_node;
13968 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13971 /* Exception handling [gram.exception] */
13973 /* Parse an (optional) exception-specification.
13975 exception-specification:
13976 throw ( type-id-list [opt] )
13978 Returns a TREE_LIST representing the exception-specification. The
13979 TREE_VALUE of each node is a type. */
13981 static tree
13982 cp_parser_exception_specification_opt (cp_parser* parser)
13984 cp_token *token;
13985 tree type_id_list;
13987 /* Peek at the next token. */
13988 token = cp_lexer_peek_token (parser->lexer);
13989 /* If it's not `throw', then there's no exception-specification. */
13990 if (!cp_parser_is_keyword (token, RID_THROW))
13991 return NULL_TREE;
13993 /* Consume the `throw'. */
13994 cp_lexer_consume_token (parser->lexer);
13996 /* Look for the `('. */
13997 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13999 /* Peek at the next token. */
14000 token = cp_lexer_peek_token (parser->lexer);
14001 /* If it's not a `)', then there is a type-id-list. */
14002 if (token->type != CPP_CLOSE_PAREN)
14004 const char *saved_message;
14006 /* Types may not be defined in an exception-specification. */
14007 saved_message = parser->type_definition_forbidden_message;
14008 parser->type_definition_forbidden_message
14009 = "types may not be defined in an exception-specification";
14010 /* Parse the type-id-list. */
14011 type_id_list = cp_parser_type_id_list (parser);
14012 /* Restore the saved message. */
14013 parser->type_definition_forbidden_message = saved_message;
14015 else
14016 type_id_list = empty_except_spec;
14018 /* Look for the `)'. */
14019 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14021 return type_id_list;
14024 /* Parse an (optional) type-id-list.
14026 type-id-list:
14027 type-id
14028 type-id-list , type-id
14030 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14031 in the order that the types were presented. */
14033 static tree
14034 cp_parser_type_id_list (cp_parser* parser)
14036 tree types = NULL_TREE;
14038 while (true)
14040 cp_token *token;
14041 tree type;
14043 /* Get the next type-id. */
14044 type = cp_parser_type_id (parser);
14045 /* Add it to the list. */
14046 types = add_exception_specifier (types, type, /*complain=*/1);
14047 /* Peek at the next token. */
14048 token = cp_lexer_peek_token (parser->lexer);
14049 /* If it is not a `,', we are done. */
14050 if (token->type != CPP_COMMA)
14051 break;
14052 /* Consume the `,'. */
14053 cp_lexer_consume_token (parser->lexer);
14056 return nreverse (types);
14059 /* Parse a try-block.
14061 try-block:
14062 try compound-statement handler-seq */
14064 static tree
14065 cp_parser_try_block (cp_parser* parser)
14067 tree try_block;
14069 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14070 try_block = begin_try_block ();
14071 cp_parser_compound_statement (parser, NULL, true);
14072 finish_try_block (try_block);
14073 cp_parser_handler_seq (parser);
14074 finish_handler_sequence (try_block);
14076 return try_block;
14079 /* Parse a function-try-block.
14081 function-try-block:
14082 try ctor-initializer [opt] function-body handler-seq */
14084 static bool
14085 cp_parser_function_try_block (cp_parser* parser)
14087 tree try_block;
14088 bool ctor_initializer_p;
14090 /* Look for the `try' keyword. */
14091 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14092 return false;
14093 /* Let the rest of the front-end know where we are. */
14094 try_block = begin_function_try_block ();
14095 /* Parse the function-body. */
14096 ctor_initializer_p
14097 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14098 /* We're done with the `try' part. */
14099 finish_function_try_block (try_block);
14100 /* Parse the handlers. */
14101 cp_parser_handler_seq (parser);
14102 /* We're done with the handlers. */
14103 finish_function_handler_sequence (try_block);
14105 return ctor_initializer_p;
14108 /* Parse a handler-seq.
14110 handler-seq:
14111 handler handler-seq [opt] */
14113 static void
14114 cp_parser_handler_seq (cp_parser* parser)
14116 while (true)
14118 cp_token *token;
14120 /* Parse the handler. */
14121 cp_parser_handler (parser);
14122 /* Peek at the next token. */
14123 token = cp_lexer_peek_token (parser->lexer);
14124 /* If it's not `catch' then there are no more handlers. */
14125 if (!cp_parser_is_keyword (token, RID_CATCH))
14126 break;
14130 /* Parse a handler.
14132 handler:
14133 catch ( exception-declaration ) compound-statement */
14135 static void
14136 cp_parser_handler (cp_parser* parser)
14138 tree handler;
14139 tree declaration;
14141 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14142 handler = begin_handler ();
14143 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14144 declaration = cp_parser_exception_declaration (parser);
14145 finish_handler_parms (declaration, handler);
14146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14147 cp_parser_compound_statement (parser, NULL, false);
14148 finish_handler (handler);
14151 /* Parse an exception-declaration.
14153 exception-declaration:
14154 type-specifier-seq declarator
14155 type-specifier-seq abstract-declarator
14156 type-specifier-seq
14159 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14160 ellipsis variant is used. */
14162 static tree
14163 cp_parser_exception_declaration (cp_parser* parser)
14165 tree decl;
14166 cp_decl_specifier_seq type_specifiers;
14167 cp_declarator *declarator;
14168 const char *saved_message;
14170 /* If it's an ellipsis, it's easy to handle. */
14171 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14173 /* Consume the `...' token. */
14174 cp_lexer_consume_token (parser->lexer);
14175 return NULL_TREE;
14178 /* Types may not be defined in exception-declarations. */
14179 saved_message = parser->type_definition_forbidden_message;
14180 parser->type_definition_forbidden_message
14181 = "types may not be defined in exception-declarations";
14183 /* Parse the type-specifier-seq. */
14184 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14185 &type_specifiers);
14186 /* If it's a `)', then there is no declarator. */
14187 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14188 declarator = NULL;
14189 else
14190 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14191 /*ctor_dtor_or_conv_p=*/NULL,
14192 /*parenthesized_p=*/NULL,
14193 /*member_p=*/false);
14195 /* Restore the saved message. */
14196 parser->type_definition_forbidden_message = saved_message;
14198 if (type_specifiers.any_specifiers_p)
14200 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14201 if (decl == NULL_TREE)
14202 error ("invalid catch parameter");
14204 else
14205 decl = NULL_TREE;
14207 return decl;
14210 /* Parse a throw-expression.
14212 throw-expression:
14213 throw assignment-expression [opt]
14215 Returns a THROW_EXPR representing the throw-expression. */
14217 static tree
14218 cp_parser_throw_expression (cp_parser* parser)
14220 tree expression;
14221 cp_token* token;
14223 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14224 token = cp_lexer_peek_token (parser->lexer);
14225 /* Figure out whether or not there is an assignment-expression
14226 following the "throw" keyword. */
14227 if (token->type == CPP_COMMA
14228 || token->type == CPP_SEMICOLON
14229 || token->type == CPP_CLOSE_PAREN
14230 || token->type == CPP_CLOSE_SQUARE
14231 || token->type == CPP_CLOSE_BRACE
14232 || token->type == CPP_COLON)
14233 expression = NULL_TREE;
14234 else
14235 expression = cp_parser_assignment_expression (parser,
14236 /*cast_p=*/false);
14238 return build_throw (expression);
14241 /* GNU Extensions */
14243 /* Parse an (optional) asm-specification.
14245 asm-specification:
14246 asm ( string-literal )
14248 If the asm-specification is present, returns a STRING_CST
14249 corresponding to the string-literal. Otherwise, returns
14250 NULL_TREE. */
14252 static tree
14253 cp_parser_asm_specification_opt (cp_parser* parser)
14255 cp_token *token;
14256 tree asm_specification;
14258 /* Peek at the next token. */
14259 token = cp_lexer_peek_token (parser->lexer);
14260 /* If the next token isn't the `asm' keyword, then there's no
14261 asm-specification. */
14262 if (!cp_parser_is_keyword (token, RID_ASM))
14263 return NULL_TREE;
14265 /* Consume the `asm' token. */
14266 cp_lexer_consume_token (parser->lexer);
14267 /* Look for the `('. */
14268 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14270 /* Look for the string-literal. */
14271 asm_specification = cp_parser_string_literal (parser, false, false);
14273 /* Look for the `)'. */
14274 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14276 return asm_specification;
14279 /* Parse an asm-operand-list.
14281 asm-operand-list:
14282 asm-operand
14283 asm-operand-list , asm-operand
14285 asm-operand:
14286 string-literal ( expression )
14287 [ string-literal ] string-literal ( expression )
14289 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14290 each node is the expression. The TREE_PURPOSE is itself a
14291 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14292 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14293 is a STRING_CST for the string literal before the parenthesis. */
14295 static tree
14296 cp_parser_asm_operand_list (cp_parser* parser)
14298 tree asm_operands = NULL_TREE;
14300 while (true)
14302 tree string_literal;
14303 tree expression;
14304 tree name;
14306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14308 /* Consume the `[' token. */
14309 cp_lexer_consume_token (parser->lexer);
14310 /* Read the operand name. */
14311 name = cp_parser_identifier (parser);
14312 if (name != error_mark_node)
14313 name = build_string (IDENTIFIER_LENGTH (name),
14314 IDENTIFIER_POINTER (name));
14315 /* Look for the closing `]'. */
14316 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14318 else
14319 name = NULL_TREE;
14320 /* Look for the string-literal. */
14321 string_literal = cp_parser_string_literal (parser, false, false);
14323 /* Look for the `('. */
14324 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14325 /* Parse the expression. */
14326 expression = cp_parser_expression (parser, /*cast_p=*/false);
14327 /* Look for the `)'. */
14328 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14330 /* Add this operand to the list. */
14331 asm_operands = tree_cons (build_tree_list (name, string_literal),
14332 expression,
14333 asm_operands);
14334 /* If the next token is not a `,', there are no more
14335 operands. */
14336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14337 break;
14338 /* Consume the `,'. */
14339 cp_lexer_consume_token (parser->lexer);
14342 return nreverse (asm_operands);
14345 /* Parse an asm-clobber-list.
14347 asm-clobber-list:
14348 string-literal
14349 asm-clobber-list , string-literal
14351 Returns a TREE_LIST, indicating the clobbers in the order that they
14352 appeared. The TREE_VALUE of each node is a STRING_CST. */
14354 static tree
14355 cp_parser_asm_clobber_list (cp_parser* parser)
14357 tree clobbers = NULL_TREE;
14359 while (true)
14361 tree string_literal;
14363 /* Look for the string literal. */
14364 string_literal = cp_parser_string_literal (parser, false, false);
14365 /* Add it to the list. */
14366 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14367 /* If the next token is not a `,', then the list is
14368 complete. */
14369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14370 break;
14371 /* Consume the `,' token. */
14372 cp_lexer_consume_token (parser->lexer);
14375 return clobbers;
14378 /* Parse an (optional) series of attributes.
14380 attributes:
14381 attributes attribute
14383 attribute:
14384 __attribute__ (( attribute-list [opt] ))
14386 The return value is as for cp_parser_attribute_list. */
14388 static tree
14389 cp_parser_attributes_opt (cp_parser* parser)
14391 tree attributes = NULL_TREE;
14393 while (true)
14395 cp_token *token;
14396 tree attribute_list;
14398 /* Peek at the next token. */
14399 token = cp_lexer_peek_token (parser->lexer);
14400 /* If it's not `__attribute__', then we're done. */
14401 if (token->keyword != RID_ATTRIBUTE)
14402 break;
14404 /* Consume the `__attribute__' keyword. */
14405 cp_lexer_consume_token (parser->lexer);
14406 /* Look for the two `(' tokens. */
14407 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14408 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14410 /* Peek at the next token. */
14411 token = cp_lexer_peek_token (parser->lexer);
14412 if (token->type != CPP_CLOSE_PAREN)
14413 /* Parse the attribute-list. */
14414 attribute_list = cp_parser_attribute_list (parser);
14415 else
14416 /* If the next token is a `)', then there is no attribute
14417 list. */
14418 attribute_list = NULL;
14420 /* Look for the two `)' tokens. */
14421 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14422 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14424 /* Add these new attributes to the list. */
14425 attributes = chainon (attributes, attribute_list);
14428 return attributes;
14431 /* Parse an attribute-list.
14433 attribute-list:
14434 attribute
14435 attribute-list , attribute
14437 attribute:
14438 identifier
14439 identifier ( identifier )
14440 identifier ( identifier , expression-list )
14441 identifier ( expression-list )
14443 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14444 to an attribute. The TREE_PURPOSE of each node is the identifier
14445 indicating which attribute is in use. The TREE_VALUE represents
14446 the arguments, if any. */
14448 static tree
14449 cp_parser_attribute_list (cp_parser* parser)
14451 tree attribute_list = NULL_TREE;
14452 bool save_translate_strings_p = parser->translate_strings_p;
14454 parser->translate_strings_p = false;
14455 while (true)
14457 cp_token *token;
14458 tree identifier;
14459 tree attribute;
14461 /* Look for the identifier. We also allow keywords here; for
14462 example `__attribute__ ((const))' is legal. */
14463 token = cp_lexer_peek_token (parser->lexer);
14464 if (token->type == CPP_NAME
14465 || token->type == CPP_KEYWORD)
14467 /* Consume the token. */
14468 token = cp_lexer_consume_token (parser->lexer);
14470 /* Save away the identifier that indicates which attribute
14471 this is. */
14472 identifier = token->value;
14473 attribute = build_tree_list (identifier, NULL_TREE);
14475 /* Peek at the next token. */
14476 token = cp_lexer_peek_token (parser->lexer);
14477 /* If it's an `(', then parse the attribute arguments. */
14478 if (token->type == CPP_OPEN_PAREN)
14480 tree arguments;
14482 arguments = (cp_parser_parenthesized_expression_list
14483 (parser, true, /*cast_p=*/false,
14484 /*non_constant_p=*/NULL));
14485 /* Save the identifier and arguments away. */
14486 TREE_VALUE (attribute) = arguments;
14489 /* Add this attribute to the list. */
14490 TREE_CHAIN (attribute) = attribute_list;
14491 attribute_list = attribute;
14493 token = cp_lexer_peek_token (parser->lexer);
14495 /* Now, look for more attributes. If the next token isn't a
14496 `,', we're done. */
14497 if (token->type != CPP_COMMA)
14498 break;
14500 /* Consume the comma and keep going. */
14501 cp_lexer_consume_token (parser->lexer);
14503 parser->translate_strings_p = save_translate_strings_p;
14505 /* We built up the list in reverse order. */
14506 return nreverse (attribute_list);
14509 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14510 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14511 current value of the PEDANTIC flag, regardless of whether or not
14512 the `__extension__' keyword is present. The caller is responsible
14513 for restoring the value of the PEDANTIC flag. */
14515 static bool
14516 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14518 /* Save the old value of the PEDANTIC flag. */
14519 *saved_pedantic = pedantic;
14521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14523 /* Consume the `__extension__' token. */
14524 cp_lexer_consume_token (parser->lexer);
14525 /* We're not being pedantic while the `__extension__' keyword is
14526 in effect. */
14527 pedantic = 0;
14529 return true;
14532 return false;
14535 /* Parse a label declaration.
14537 label-declaration:
14538 __label__ label-declarator-seq ;
14540 label-declarator-seq:
14541 identifier , label-declarator-seq
14542 identifier */
14544 static void
14545 cp_parser_label_declaration (cp_parser* parser)
14547 /* Look for the `__label__' keyword. */
14548 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14550 while (true)
14552 tree identifier;
14554 /* Look for an identifier. */
14555 identifier = cp_parser_identifier (parser);
14556 /* If we failed, stop. */
14557 if (identifier == error_mark_node)
14558 break;
14559 /* Declare it as a label. */
14560 finish_label_decl (identifier);
14561 /* If the next token is a `;', stop. */
14562 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14563 break;
14564 /* Look for the `,' separating the label declarations. */
14565 cp_parser_require (parser, CPP_COMMA, "`,'");
14568 /* Look for the final `;'. */
14569 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14572 /* Support Functions */
14574 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14575 NAME should have one of the representations used for an
14576 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14577 is returned. If PARSER->SCOPE is a dependent type, then a
14578 SCOPE_REF is returned.
14580 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14581 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14582 was formed. Abstractly, such entities should not be passed to this
14583 function, because they do not need to be looked up, but it is
14584 simpler to check for this special case here, rather than at the
14585 call-sites.
14587 In cases not explicitly covered above, this function returns a
14588 DECL, OVERLOAD, or baselink representing the result of the lookup.
14589 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14590 is returned.
14592 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14593 (e.g., "struct") that was used. In that case bindings that do not
14594 refer to types are ignored.
14596 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14597 ignored.
14599 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14600 are ignored.
14602 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14603 types.
14605 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14606 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14607 NULL_TREE otherwise. */
14609 static tree
14610 cp_parser_lookup_name (cp_parser *parser, tree name,
14611 enum tag_types tag_type,
14612 bool is_template,
14613 bool is_namespace,
14614 bool check_dependency,
14615 tree *ambiguous_decls)
14617 int flags = 0;
14618 tree decl;
14619 tree object_type = parser->context->object_type;
14621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14622 flags |= LOOKUP_COMPLAIN;
14624 /* Assume that the lookup will be unambiguous. */
14625 if (ambiguous_decls)
14626 *ambiguous_decls = NULL_TREE;
14628 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14629 no longer valid. Note that if we are parsing tentatively, and
14630 the parse fails, OBJECT_TYPE will be automatically restored. */
14631 parser->context->object_type = NULL_TREE;
14633 if (name == error_mark_node)
14634 return error_mark_node;
14636 /* A template-id has already been resolved; there is no lookup to
14637 do. */
14638 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14639 return name;
14640 if (BASELINK_P (name))
14642 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14643 == TEMPLATE_ID_EXPR);
14644 return name;
14647 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14648 it should already have been checked to make sure that the name
14649 used matches the type being destroyed. */
14650 if (TREE_CODE (name) == BIT_NOT_EXPR)
14652 tree type;
14654 /* Figure out to which type this destructor applies. */
14655 if (parser->scope)
14656 type = parser->scope;
14657 else if (object_type)
14658 type = object_type;
14659 else
14660 type = current_class_type;
14661 /* If that's not a class type, there is no destructor. */
14662 if (!type || !CLASS_TYPE_P (type))
14663 return error_mark_node;
14664 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14665 lazily_declare_fn (sfk_destructor, type);
14666 if (!CLASSTYPE_DESTRUCTORS (type))
14667 return error_mark_node;
14668 /* If it was a class type, return the destructor. */
14669 return CLASSTYPE_DESTRUCTORS (type);
14672 /* By this point, the NAME should be an ordinary identifier. If
14673 the id-expression was a qualified name, the qualifying scope is
14674 stored in PARSER->SCOPE at this point. */
14675 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14677 /* Perform the lookup. */
14678 if (parser->scope)
14680 bool dependent_p;
14682 if (parser->scope == error_mark_node)
14683 return error_mark_node;
14685 /* If the SCOPE is dependent, the lookup must be deferred until
14686 the template is instantiated -- unless we are explicitly
14687 looking up names in uninstantiated templates. Even then, we
14688 cannot look up the name if the scope is not a class type; it
14689 might, for example, be a template type parameter. */
14690 dependent_p = (TYPE_P (parser->scope)
14691 && !(parser->in_declarator_p
14692 && currently_open_class (parser->scope))
14693 && dependent_type_p (parser->scope));
14694 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14695 && dependent_p)
14697 if (tag_type)
14699 tree type;
14701 /* The resolution to Core Issue 180 says that `struct
14702 A::B' should be considered a type-name, even if `A'
14703 is dependent. */
14704 type = make_typename_type (parser->scope, name, tag_type,
14705 /*complain=*/tf_error);
14706 decl = TYPE_NAME (type);
14708 else if (is_template
14709 && (cp_parser_next_token_ends_template_argument_p (parser)
14710 || cp_lexer_next_token_is (parser->lexer,
14711 CPP_CLOSE_PAREN)))
14712 decl = make_unbound_class_template (parser->scope,
14713 name, NULL_TREE,
14714 /*complain=*/tf_error);
14715 else
14716 decl = build_qualified_name (/*type=*/NULL_TREE,
14717 parser->scope, name,
14718 is_template);
14720 else
14722 tree pushed_scope = NULL_TREE;
14724 /* If PARSER->SCOPE is a dependent type, then it must be a
14725 class type, and we must not be checking dependencies;
14726 otherwise, we would have processed this lookup above. So
14727 that PARSER->SCOPE is not considered a dependent base by
14728 lookup_member, we must enter the scope here. */
14729 if (dependent_p)
14730 pushed_scope = push_scope (parser->scope);
14731 /* If the PARSER->SCOPE is a template specialization, it
14732 may be instantiated during name lookup. In that case,
14733 errors may be issued. Even if we rollback the current
14734 tentative parse, those errors are valid. */
14735 decl = lookup_qualified_name (parser->scope, name,
14736 tag_type != none_type,
14737 /*complain=*/true);
14738 if (pushed_scope)
14739 pop_scope (pushed_scope);
14741 parser->qualifying_scope = parser->scope;
14742 parser->object_scope = NULL_TREE;
14744 else if (object_type)
14746 tree object_decl = NULL_TREE;
14747 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14748 OBJECT_TYPE is not a class. */
14749 if (CLASS_TYPE_P (object_type))
14750 /* If the OBJECT_TYPE is a template specialization, it may
14751 be instantiated during name lookup. In that case, errors
14752 may be issued. Even if we rollback the current tentative
14753 parse, those errors are valid. */
14754 object_decl = lookup_member (object_type,
14755 name,
14756 /*protect=*/0,
14757 tag_type != none_type);
14758 /* Look it up in the enclosing context, too. */
14759 decl = lookup_name_real (name, tag_type != none_type,
14760 /*nonclass=*/0,
14761 /*block_p=*/true, is_namespace, flags);
14762 parser->object_scope = object_type;
14763 parser->qualifying_scope = NULL_TREE;
14764 if (object_decl)
14765 decl = object_decl;
14767 else
14769 decl = lookup_name_real (name, tag_type != none_type,
14770 /*nonclass=*/0,
14771 /*block_p=*/true, is_namespace, flags);
14772 parser->qualifying_scope = NULL_TREE;
14773 parser->object_scope = NULL_TREE;
14776 /* If the lookup failed, let our caller know. */
14777 if (!decl || decl == error_mark_node)
14778 return error_mark_node;
14780 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14781 if (TREE_CODE (decl) == TREE_LIST)
14783 if (ambiguous_decls)
14784 *ambiguous_decls = decl;
14785 /* The error message we have to print is too complicated for
14786 cp_parser_error, so we incorporate its actions directly. */
14787 if (!cp_parser_simulate_error (parser))
14789 error ("reference to %qD is ambiguous", name);
14790 print_candidates (decl);
14792 return error_mark_node;
14795 gcc_assert (DECL_P (decl)
14796 || TREE_CODE (decl) == OVERLOAD
14797 || TREE_CODE (decl) == SCOPE_REF
14798 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14799 || BASELINK_P (decl));
14801 /* If we have resolved the name of a member declaration, check to
14802 see if the declaration is accessible. When the name resolves to
14803 set of overloaded functions, accessibility is checked when
14804 overload resolution is done.
14806 During an explicit instantiation, access is not checked at all,
14807 as per [temp.explicit]. */
14808 if (DECL_P (decl))
14809 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14811 return decl;
14814 /* Like cp_parser_lookup_name, but for use in the typical case where
14815 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14816 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14818 static tree
14819 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14821 return cp_parser_lookup_name (parser, name,
14822 none_type,
14823 /*is_template=*/false,
14824 /*is_namespace=*/false,
14825 /*check_dependency=*/true,
14826 /*ambiguous_decls=*/NULL);
14829 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14830 the current context, return the TYPE_DECL. If TAG_NAME_P is
14831 true, the DECL indicates the class being defined in a class-head,
14832 or declared in an elaborated-type-specifier.
14834 Otherwise, return DECL. */
14836 static tree
14837 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14839 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14840 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14842 struct A {
14843 template <typename T> struct B;
14846 template <typename T> struct A::B {};
14848 Similarly, in an elaborated-type-specifier:
14850 namespace N { struct X{}; }
14852 struct A {
14853 template <typename T> friend struct N::X;
14856 However, if the DECL refers to a class type, and we are in
14857 the scope of the class, then the name lookup automatically
14858 finds the TYPE_DECL created by build_self_reference rather
14859 than a TEMPLATE_DECL. For example, in:
14861 template <class T> struct S {
14862 S s;
14865 there is no need to handle such case. */
14867 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14868 return DECL_TEMPLATE_RESULT (decl);
14870 return decl;
14873 /* If too many, or too few, template-parameter lists apply to the
14874 declarator, issue an error message. Returns TRUE if all went well,
14875 and FALSE otherwise. */
14877 static bool
14878 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14879 cp_declarator *declarator)
14881 unsigned num_templates;
14883 /* We haven't seen any classes that involve template parameters yet. */
14884 num_templates = 0;
14886 switch (declarator->kind)
14888 case cdk_id:
14889 if (declarator->u.id.qualifying_scope)
14891 tree scope;
14892 tree member;
14894 scope = declarator->u.id.qualifying_scope;
14895 member = declarator->u.id.unqualified_name;
14897 while (scope && CLASS_TYPE_P (scope))
14899 /* You're supposed to have one `template <...>'
14900 for every template class, but you don't need one
14901 for a full specialization. For example:
14903 template <class T> struct S{};
14904 template <> struct S<int> { void f(); };
14905 void S<int>::f () {}
14907 is correct; there shouldn't be a `template <>' for
14908 the definition of `S<int>::f'. */
14909 if (CLASSTYPE_TEMPLATE_INFO (scope)
14910 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14911 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14912 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14913 ++num_templates;
14915 scope = TYPE_CONTEXT (scope);
14918 else if (TREE_CODE (declarator->u.id.unqualified_name)
14919 == TEMPLATE_ID_EXPR)
14920 /* If the DECLARATOR has the form `X<y>' then it uses one
14921 additional level of template parameters. */
14922 ++num_templates;
14924 return cp_parser_check_template_parameters (parser,
14925 num_templates);
14927 case cdk_function:
14928 case cdk_array:
14929 case cdk_pointer:
14930 case cdk_reference:
14931 case cdk_ptrmem:
14932 return (cp_parser_check_declarator_template_parameters
14933 (parser, declarator->declarator));
14935 case cdk_error:
14936 return true;
14938 default:
14939 gcc_unreachable ();
14941 return false;
14944 /* NUM_TEMPLATES were used in the current declaration. If that is
14945 invalid, return FALSE and issue an error messages. Otherwise,
14946 return TRUE. */
14948 static bool
14949 cp_parser_check_template_parameters (cp_parser* parser,
14950 unsigned num_templates)
14952 /* If there are more template classes than parameter lists, we have
14953 something like:
14955 template <class T> void S<T>::R<T>::f (); */
14956 if (parser->num_template_parameter_lists < num_templates)
14958 error ("too few template-parameter-lists");
14959 return false;
14961 /* If there are the same number of template classes and parameter
14962 lists, that's OK. */
14963 if (parser->num_template_parameter_lists == num_templates)
14964 return true;
14965 /* If there are more, but only one more, then we are referring to a
14966 member template. That's OK too. */
14967 if (parser->num_template_parameter_lists == num_templates + 1)
14968 return true;
14969 /* Otherwise, there are too many template parameter lists. We have
14970 something like:
14972 template <class T> template <class U> void S::f(); */
14973 error ("too many template-parameter-lists");
14974 return false;
14977 /* Parse an optional `::' token indicating that the following name is
14978 from the global namespace. If so, PARSER->SCOPE is set to the
14979 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14980 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14981 Returns the new value of PARSER->SCOPE, if the `::' token is
14982 present, and NULL_TREE otherwise. */
14984 static tree
14985 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14987 cp_token *token;
14989 /* Peek at the next token. */
14990 token = cp_lexer_peek_token (parser->lexer);
14991 /* If we're looking at a `::' token then we're starting from the
14992 global namespace, not our current location. */
14993 if (token->type == CPP_SCOPE)
14995 /* Consume the `::' token. */
14996 cp_lexer_consume_token (parser->lexer);
14997 /* Set the SCOPE so that we know where to start the lookup. */
14998 parser->scope = global_namespace;
14999 parser->qualifying_scope = global_namespace;
15000 parser->object_scope = NULL_TREE;
15002 return parser->scope;
15004 else if (!current_scope_valid_p)
15006 parser->scope = NULL_TREE;
15007 parser->qualifying_scope = NULL_TREE;
15008 parser->object_scope = NULL_TREE;
15011 return NULL_TREE;
15014 /* Returns TRUE if the upcoming token sequence is the start of a
15015 constructor declarator. If FRIEND_P is true, the declarator is
15016 preceded by the `friend' specifier. */
15018 static bool
15019 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15021 bool constructor_p;
15022 tree type_decl = NULL_TREE;
15023 bool nested_name_p;
15024 cp_token *next_token;
15026 /* The common case is that this is not a constructor declarator, so
15027 try to avoid doing lots of work if at all possible. It's not
15028 valid declare a constructor at function scope. */
15029 if (at_function_scope_p ())
15030 return false;
15031 /* And only certain tokens can begin a constructor declarator. */
15032 next_token = cp_lexer_peek_token (parser->lexer);
15033 if (next_token->type != CPP_NAME
15034 && next_token->type != CPP_SCOPE
15035 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15036 && next_token->type != CPP_TEMPLATE_ID)
15037 return false;
15039 /* Parse tentatively; we are going to roll back all of the tokens
15040 consumed here. */
15041 cp_parser_parse_tentatively (parser);
15042 /* Assume that we are looking at a constructor declarator. */
15043 constructor_p = true;
15045 /* Look for the optional `::' operator. */
15046 cp_parser_global_scope_opt (parser,
15047 /*current_scope_valid_p=*/false);
15048 /* Look for the nested-name-specifier. */
15049 nested_name_p
15050 = (cp_parser_nested_name_specifier_opt (parser,
15051 /*typename_keyword_p=*/false,
15052 /*check_dependency_p=*/false,
15053 /*type_p=*/false,
15054 /*is_declaration=*/false)
15055 != NULL_TREE);
15056 /* Outside of a class-specifier, there must be a
15057 nested-name-specifier. */
15058 if (!nested_name_p &&
15059 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15060 || friend_p))
15061 constructor_p = false;
15062 /* If we still think that this might be a constructor-declarator,
15063 look for a class-name. */
15064 if (constructor_p)
15066 /* If we have:
15068 template <typename T> struct S { S(); };
15069 template <typename T> S<T>::S ();
15071 we must recognize that the nested `S' names a class.
15072 Similarly, for:
15074 template <typename T> S<T>::S<T> ();
15076 we must recognize that the nested `S' names a template. */
15077 type_decl = cp_parser_class_name (parser,
15078 /*typename_keyword_p=*/false,
15079 /*template_keyword_p=*/false,
15080 none_type,
15081 /*check_dependency_p=*/false,
15082 /*class_head_p=*/false,
15083 /*is_declaration=*/false);
15084 /* If there was no class-name, then this is not a constructor. */
15085 constructor_p = !cp_parser_error_occurred (parser);
15088 /* If we're still considering a constructor, we have to see a `(',
15089 to begin the parameter-declaration-clause, followed by either a
15090 `)', an `...', or a decl-specifier. We need to check for a
15091 type-specifier to avoid being fooled into thinking that:
15093 S::S (f) (int);
15095 is a constructor. (It is actually a function named `f' that
15096 takes one parameter (of type `int') and returns a value of type
15097 `S::S'. */
15098 if (constructor_p
15099 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15102 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15103 /* A parameter declaration begins with a decl-specifier,
15104 which is either the "attribute" keyword, a storage class
15105 specifier, or (usually) a type-specifier. */
15106 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15107 && !cp_parser_storage_class_specifier_opt (parser))
15109 tree type;
15110 tree pushed_scope = NULL_TREE;
15111 unsigned saved_num_template_parameter_lists;
15113 /* Names appearing in the type-specifier should be looked up
15114 in the scope of the class. */
15115 if (current_class_type)
15116 type = NULL_TREE;
15117 else
15119 type = TREE_TYPE (type_decl);
15120 if (TREE_CODE (type) == TYPENAME_TYPE)
15122 type = resolve_typename_type (type,
15123 /*only_current_p=*/false);
15124 if (type == error_mark_node)
15126 cp_parser_abort_tentative_parse (parser);
15127 return false;
15130 pushed_scope = push_scope (type);
15133 /* Inside the constructor parameter list, surrounding
15134 template-parameter-lists do not apply. */
15135 saved_num_template_parameter_lists
15136 = parser->num_template_parameter_lists;
15137 parser->num_template_parameter_lists = 0;
15139 /* Look for the type-specifier. */
15140 cp_parser_type_specifier (parser,
15141 CP_PARSER_FLAGS_NONE,
15142 /*decl_specs=*/NULL,
15143 /*is_declarator=*/true,
15144 /*declares_class_or_enum=*/NULL,
15145 /*is_cv_qualifier=*/NULL);
15147 parser->num_template_parameter_lists
15148 = saved_num_template_parameter_lists;
15150 /* Leave the scope of the class. */
15151 if (pushed_scope)
15152 pop_scope (pushed_scope);
15154 constructor_p = !cp_parser_error_occurred (parser);
15157 else
15158 constructor_p = false;
15159 /* We did not really want to consume any tokens. */
15160 cp_parser_abort_tentative_parse (parser);
15162 return constructor_p;
15165 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15166 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15167 they must be performed once we are in the scope of the function.
15169 Returns the function defined. */
15171 static tree
15172 cp_parser_function_definition_from_specifiers_and_declarator
15173 (cp_parser* parser,
15174 cp_decl_specifier_seq *decl_specifiers,
15175 tree attributes,
15176 const cp_declarator *declarator)
15178 tree fn;
15179 bool success_p;
15181 /* Begin the function-definition. */
15182 success_p = start_function (decl_specifiers, declarator, attributes);
15184 /* The things we're about to see are not directly qualified by any
15185 template headers we've seen thus far. */
15186 reset_specialization ();
15188 /* If there were names looked up in the decl-specifier-seq that we
15189 did not check, check them now. We must wait until we are in the
15190 scope of the function to perform the checks, since the function
15191 might be a friend. */
15192 perform_deferred_access_checks ();
15194 if (!success_p)
15196 /* Skip the entire function. */
15197 error ("invalid function declaration");
15198 cp_parser_skip_to_end_of_block_or_statement (parser);
15199 fn = error_mark_node;
15201 else
15202 fn = cp_parser_function_definition_after_declarator (parser,
15203 /*inline_p=*/false);
15205 return fn;
15208 /* Parse the part of a function-definition that follows the
15209 declarator. INLINE_P is TRUE iff this function is an inline
15210 function defined with a class-specifier.
15212 Returns the function defined. */
15214 static tree
15215 cp_parser_function_definition_after_declarator (cp_parser* parser,
15216 bool inline_p)
15218 tree fn;
15219 bool ctor_initializer_p = false;
15220 bool saved_in_unbraced_linkage_specification_p;
15221 unsigned saved_num_template_parameter_lists;
15223 /* If the next token is `return', then the code may be trying to
15224 make use of the "named return value" extension that G++ used to
15225 support. */
15226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15228 /* Consume the `return' keyword. */
15229 cp_lexer_consume_token (parser->lexer);
15230 /* Look for the identifier that indicates what value is to be
15231 returned. */
15232 cp_parser_identifier (parser);
15233 /* Issue an error message. */
15234 error ("named return values are no longer supported");
15235 /* Skip tokens until we reach the start of the function body. */
15236 while (true)
15238 cp_token *token = cp_lexer_peek_token (parser->lexer);
15239 if (token->type == CPP_OPEN_BRACE
15240 || token->type == CPP_EOF
15241 || token->type == CPP_PRAGMA_EOL)
15242 break;
15243 cp_lexer_consume_token (parser->lexer);
15246 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15247 anything declared inside `f'. */
15248 saved_in_unbraced_linkage_specification_p
15249 = parser->in_unbraced_linkage_specification_p;
15250 parser->in_unbraced_linkage_specification_p = false;
15251 /* Inside the function, surrounding template-parameter-lists do not
15252 apply. */
15253 saved_num_template_parameter_lists
15254 = parser->num_template_parameter_lists;
15255 parser->num_template_parameter_lists = 0;
15256 /* If the next token is `try', then we are looking at a
15257 function-try-block. */
15258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15259 ctor_initializer_p = cp_parser_function_try_block (parser);
15260 /* A function-try-block includes the function-body, so we only do
15261 this next part if we're not processing a function-try-block. */
15262 else
15263 ctor_initializer_p
15264 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15266 /* Finish the function. */
15267 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15268 (inline_p ? 2 : 0));
15269 /* Generate code for it, if necessary. */
15270 expand_or_defer_fn (fn);
15271 /* Restore the saved values. */
15272 parser->in_unbraced_linkage_specification_p
15273 = saved_in_unbraced_linkage_specification_p;
15274 parser->num_template_parameter_lists
15275 = saved_num_template_parameter_lists;
15277 return fn;
15280 /* Parse a template-declaration, assuming that the `export' (and
15281 `extern') keywords, if present, has already been scanned. MEMBER_P
15282 is as for cp_parser_template_declaration. */
15284 static void
15285 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15287 tree decl = NULL_TREE;
15288 tree parameter_list;
15289 bool friend_p = false;
15290 bool need_lang_pop;
15292 /* Look for the `template' keyword. */
15293 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15294 return;
15296 /* And the `<'. */
15297 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15298 return;
15299 /* [temp]
15301 A template ... shall not have C linkage. */
15302 if (current_lang_name == lang_name_c)
15304 error ("template with C linkage");
15305 /* Give it C++ linkage to avoid confusing other parts of the
15306 front end. */
15307 push_lang_context (lang_name_cplusplus);
15308 need_lang_pop = true;
15310 else
15311 need_lang_pop = false;
15312 /* If the next token is `>', then we have an invalid
15313 specialization. Rather than complain about an invalid template
15314 parameter, issue an error message here. */
15315 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15317 cp_parser_error (parser, "invalid explicit specialization");
15318 begin_specialization ();
15319 parameter_list = NULL_TREE;
15321 else
15322 /* Parse the template parameters. */
15323 parameter_list = cp_parser_template_parameter_list (parser);
15325 /* Look for the `>'. */
15326 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15327 /* We just processed one more parameter list. */
15328 ++parser->num_template_parameter_lists;
15329 /* If the next token is `template', there are more template
15330 parameters. */
15331 if (cp_lexer_next_token_is_keyword (parser->lexer,
15332 RID_TEMPLATE))
15333 cp_parser_template_declaration_after_export (parser, member_p);
15334 else
15336 /* There are no access checks when parsing a template, as we do not
15337 know if a specialization will be a friend. */
15338 push_deferring_access_checks (dk_no_check);
15340 decl = cp_parser_single_declaration (parser,
15341 member_p,
15342 &friend_p);
15344 pop_deferring_access_checks ();
15346 /* If this is a member template declaration, let the front
15347 end know. */
15348 if (member_p && !friend_p && decl)
15350 if (TREE_CODE (decl) == TYPE_DECL)
15351 cp_parser_check_access_in_redeclaration (decl);
15353 decl = finish_member_template_decl (decl);
15355 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15356 make_friend_class (current_class_type, TREE_TYPE (decl),
15357 /*complain=*/true);
15359 /* We are done with the current parameter list. */
15360 --parser->num_template_parameter_lists;
15362 /* Finish up. */
15363 finish_template_decl (parameter_list);
15365 /* Register member declarations. */
15366 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15367 finish_member_declaration (decl);
15368 /* For the erroneous case of a template with C linkage, we pushed an
15369 implicit C++ linkage scope; exit that scope now. */
15370 if (need_lang_pop)
15371 pop_lang_context ();
15372 /* If DECL is a function template, we must return to parse it later.
15373 (Even though there is no definition, there might be default
15374 arguments that need handling.) */
15375 if (member_p && decl
15376 && (TREE_CODE (decl) == FUNCTION_DECL
15377 || DECL_FUNCTION_TEMPLATE_P (decl)))
15378 TREE_VALUE (parser->unparsed_functions_queues)
15379 = tree_cons (NULL_TREE, decl,
15380 TREE_VALUE (parser->unparsed_functions_queues));
15383 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15384 `function-definition' sequence. MEMBER_P is true, this declaration
15385 appears in a class scope.
15387 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15388 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15390 static tree
15391 cp_parser_single_declaration (cp_parser* parser,
15392 bool member_p,
15393 bool* friend_p)
15395 int declares_class_or_enum;
15396 tree decl = NULL_TREE;
15397 cp_decl_specifier_seq decl_specifiers;
15398 bool function_definition_p = false;
15400 /* This function is only used when processing a template
15401 declaration. */
15402 gcc_assert (innermost_scope_kind () == sk_template_parms
15403 || innermost_scope_kind () == sk_template_spec);
15405 /* Defer access checks until we know what is being declared. */
15406 push_deferring_access_checks (dk_deferred);
15408 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15409 alternative. */
15410 cp_parser_decl_specifier_seq (parser,
15411 CP_PARSER_FLAGS_OPTIONAL,
15412 &decl_specifiers,
15413 &declares_class_or_enum);
15414 if (friend_p)
15415 *friend_p = cp_parser_friend_p (&decl_specifiers);
15417 /* There are no template typedefs. */
15418 if (decl_specifiers.specs[(int) ds_typedef])
15420 error ("template declaration of %qs", "typedef");
15421 decl = error_mark_node;
15424 /* Gather up the access checks that occurred the
15425 decl-specifier-seq. */
15426 stop_deferring_access_checks ();
15428 /* Check for the declaration of a template class. */
15429 if (declares_class_or_enum)
15431 if (cp_parser_declares_only_class_p (parser))
15433 decl = shadow_tag (&decl_specifiers);
15435 /* In this case:
15437 struct C {
15438 friend template <typename T> struct A<T>::B;
15441 A<T>::B will be represented by a TYPENAME_TYPE, and
15442 therefore not recognized by shadow_tag. */
15443 if (friend_p && *friend_p
15444 && !decl
15445 && decl_specifiers.type
15446 && TYPE_P (decl_specifiers.type))
15447 decl = decl_specifiers.type;
15449 if (decl && decl != error_mark_node)
15450 decl = TYPE_NAME (decl);
15451 else
15452 decl = error_mark_node;
15455 /* If it's not a template class, try for a template function. If
15456 the next token is a `;', then this declaration does not declare
15457 anything. But, if there were errors in the decl-specifiers, then
15458 the error might well have come from an attempted class-specifier.
15459 In that case, there's no need to warn about a missing declarator. */
15460 if (!decl
15461 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15462 || decl_specifiers.type != error_mark_node))
15463 decl = cp_parser_init_declarator (parser,
15464 &decl_specifiers,
15465 /*function_definition_allowed_p=*/true,
15466 member_p,
15467 declares_class_or_enum,
15468 &function_definition_p);
15470 pop_deferring_access_checks ();
15472 /* Clear any current qualification; whatever comes next is the start
15473 of something new. */
15474 parser->scope = NULL_TREE;
15475 parser->qualifying_scope = NULL_TREE;
15476 parser->object_scope = NULL_TREE;
15477 /* Look for a trailing `;' after the declaration. */
15478 if (!function_definition_p
15479 && (decl == error_mark_node
15480 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15481 cp_parser_skip_to_end_of_block_or_statement (parser);
15483 return decl;
15486 /* Parse a cast-expression that is not the operand of a unary "&". */
15488 static tree
15489 cp_parser_simple_cast_expression (cp_parser *parser)
15491 return cp_parser_cast_expression (parser, /*address_p=*/false,
15492 /*cast_p=*/false);
15495 /* Parse a functional cast to TYPE. Returns an expression
15496 representing the cast. */
15498 static tree
15499 cp_parser_functional_cast (cp_parser* parser, tree type)
15501 tree expression_list;
15502 tree cast;
15504 expression_list
15505 = cp_parser_parenthesized_expression_list (parser, false,
15506 /*cast_p=*/true,
15507 /*non_constant_p=*/NULL);
15509 cast = build_functional_cast (type, expression_list);
15510 /* [expr.const]/1: In an integral constant expression "only type
15511 conversions to integral or enumeration type can be used". */
15512 if (TREE_CODE (type) == TYPE_DECL)
15513 type = TREE_TYPE (type);
15514 if (cast != error_mark_node && !dependent_type_p (type)
15515 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15517 if (cp_parser_non_integral_constant_expression
15518 (parser, "a call to a constructor"))
15519 return error_mark_node;
15521 return cast;
15524 /* Save the tokens that make up the body of a member function defined
15525 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15526 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15527 specifiers applied to the declaration. Returns the FUNCTION_DECL
15528 for the member function. */
15530 static tree
15531 cp_parser_save_member_function_body (cp_parser* parser,
15532 cp_decl_specifier_seq *decl_specifiers,
15533 cp_declarator *declarator,
15534 tree attributes)
15536 cp_token *first;
15537 cp_token *last;
15538 tree fn;
15540 /* Create the function-declaration. */
15541 fn = start_method (decl_specifiers, declarator, attributes);
15542 /* If something went badly wrong, bail out now. */
15543 if (fn == error_mark_node)
15545 /* If there's a function-body, skip it. */
15546 if (cp_parser_token_starts_function_definition_p
15547 (cp_lexer_peek_token (parser->lexer)))
15548 cp_parser_skip_to_end_of_block_or_statement (parser);
15549 return error_mark_node;
15552 /* Remember it, if there default args to post process. */
15553 cp_parser_save_default_args (parser, fn);
15555 /* Save away the tokens that make up the body of the
15556 function. */
15557 first = parser->lexer->next_token;
15558 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15559 /* Handle function try blocks. */
15560 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15561 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15562 last = parser->lexer->next_token;
15564 /* Save away the inline definition; we will process it when the
15565 class is complete. */
15566 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15567 DECL_PENDING_INLINE_P (fn) = 1;
15569 /* We need to know that this was defined in the class, so that
15570 friend templates are handled correctly. */
15571 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15573 /* We're done with the inline definition. */
15574 finish_method (fn);
15576 /* Add FN to the queue of functions to be parsed later. */
15577 TREE_VALUE (parser->unparsed_functions_queues)
15578 = tree_cons (NULL_TREE, fn,
15579 TREE_VALUE (parser->unparsed_functions_queues));
15581 return fn;
15584 /* Parse a template-argument-list, as well as the trailing ">" (but
15585 not the opening ">"). See cp_parser_template_argument_list for the
15586 return value. */
15588 static tree
15589 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15591 tree arguments;
15592 tree saved_scope;
15593 tree saved_qualifying_scope;
15594 tree saved_object_scope;
15595 bool saved_greater_than_is_operator_p;
15596 bool saved_skip_evaluation;
15598 /* [temp.names]
15600 When parsing a template-id, the first non-nested `>' is taken as
15601 the end of the template-argument-list rather than a greater-than
15602 operator. */
15603 saved_greater_than_is_operator_p
15604 = parser->greater_than_is_operator_p;
15605 parser->greater_than_is_operator_p = false;
15606 /* Parsing the argument list may modify SCOPE, so we save it
15607 here. */
15608 saved_scope = parser->scope;
15609 saved_qualifying_scope = parser->qualifying_scope;
15610 saved_object_scope = parser->object_scope;
15611 /* We need to evaluate the template arguments, even though this
15612 template-id may be nested within a "sizeof". */
15613 saved_skip_evaluation = skip_evaluation;
15614 skip_evaluation = false;
15615 /* Parse the template-argument-list itself. */
15616 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15617 arguments = NULL_TREE;
15618 else
15619 arguments = cp_parser_template_argument_list (parser);
15620 /* Look for the `>' that ends the template-argument-list. If we find
15621 a '>>' instead, it's probably just a typo. */
15622 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15624 if (!saved_greater_than_is_operator_p)
15626 /* If we're in a nested template argument list, the '>>' has
15627 to be a typo for '> >'. We emit the error message, but we
15628 continue parsing and we push a '>' as next token, so that
15629 the argument list will be parsed correctly. Note that the
15630 global source location is still on the token before the
15631 '>>', so we need to say explicitly where we want it. */
15632 cp_token *token = cp_lexer_peek_token (parser->lexer);
15633 error ("%H%<>>%> should be %<> >%> "
15634 "within a nested template argument list",
15635 &token->location);
15637 /* ??? Proper recovery should terminate two levels of
15638 template argument list here. */
15639 token->type = CPP_GREATER;
15641 else
15643 /* If this is not a nested template argument list, the '>>'
15644 is a typo for '>'. Emit an error message and continue.
15645 Same deal about the token location, but here we can get it
15646 right by consuming the '>>' before issuing the diagnostic. */
15647 cp_lexer_consume_token (parser->lexer);
15648 error ("spurious %<>>%>, use %<>%> to terminate "
15649 "a template argument list");
15652 else
15653 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15654 /* The `>' token might be a greater-than operator again now. */
15655 parser->greater_than_is_operator_p
15656 = saved_greater_than_is_operator_p;
15657 /* Restore the SAVED_SCOPE. */
15658 parser->scope = saved_scope;
15659 parser->qualifying_scope = saved_qualifying_scope;
15660 parser->object_scope = saved_object_scope;
15661 skip_evaluation = saved_skip_evaluation;
15663 return arguments;
15666 /* MEMBER_FUNCTION is a member function, or a friend. If default
15667 arguments, or the body of the function have not yet been parsed,
15668 parse them now. */
15670 static void
15671 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15673 /* If this member is a template, get the underlying
15674 FUNCTION_DECL. */
15675 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15676 member_function = DECL_TEMPLATE_RESULT (member_function);
15678 /* There should not be any class definitions in progress at this
15679 point; the bodies of members are only parsed outside of all class
15680 definitions. */
15681 gcc_assert (parser->num_classes_being_defined == 0);
15682 /* While we're parsing the member functions we might encounter more
15683 classes. We want to handle them right away, but we don't want
15684 them getting mixed up with functions that are currently in the
15685 queue. */
15686 parser->unparsed_functions_queues
15687 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15689 /* Make sure that any template parameters are in scope. */
15690 maybe_begin_member_template_processing (member_function);
15692 /* If the body of the function has not yet been parsed, parse it
15693 now. */
15694 if (DECL_PENDING_INLINE_P (member_function))
15696 tree function_scope;
15697 cp_token_cache *tokens;
15699 /* The function is no longer pending; we are processing it. */
15700 tokens = DECL_PENDING_INLINE_INFO (member_function);
15701 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15702 DECL_PENDING_INLINE_P (member_function) = 0;
15704 /* If this is a local class, enter the scope of the containing
15705 function. */
15706 function_scope = current_function_decl;
15707 if (function_scope)
15708 push_function_context_to (function_scope);
15711 /* Push the body of the function onto the lexer stack. */
15712 cp_parser_push_lexer_for_tokens (parser, tokens);
15714 /* Let the front end know that we going to be defining this
15715 function. */
15716 start_preparsed_function (member_function, NULL_TREE,
15717 SF_PRE_PARSED | SF_INCLASS_INLINE);
15719 /* Don't do access checking if it is a templated function. */
15720 if (processing_template_decl)
15721 push_deferring_access_checks (dk_no_check);
15723 /* Now, parse the body of the function. */
15724 cp_parser_function_definition_after_declarator (parser,
15725 /*inline_p=*/true);
15727 if (processing_template_decl)
15728 pop_deferring_access_checks ();
15730 /* Leave the scope of the containing function. */
15731 if (function_scope)
15732 pop_function_context_from (function_scope);
15733 cp_parser_pop_lexer (parser);
15736 /* Remove any template parameters from the symbol table. */
15737 maybe_end_member_template_processing ();
15739 /* Restore the queue. */
15740 parser->unparsed_functions_queues
15741 = TREE_CHAIN (parser->unparsed_functions_queues);
15744 /* If DECL contains any default args, remember it on the unparsed
15745 functions queue. */
15747 static void
15748 cp_parser_save_default_args (cp_parser* parser, tree decl)
15750 tree probe;
15752 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15753 probe;
15754 probe = TREE_CHAIN (probe))
15755 if (TREE_PURPOSE (probe))
15757 TREE_PURPOSE (parser->unparsed_functions_queues)
15758 = tree_cons (current_class_type, decl,
15759 TREE_PURPOSE (parser->unparsed_functions_queues));
15760 break;
15762 return;
15765 /* FN is a FUNCTION_DECL which may contains a parameter with an
15766 unparsed DEFAULT_ARG. Parse the default args now. This function
15767 assumes that the current scope is the scope in which the default
15768 argument should be processed. */
15770 static void
15771 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15773 bool saved_local_variables_forbidden_p;
15774 tree parm;
15776 /* While we're parsing the default args, we might (due to the
15777 statement expression extension) encounter more classes. We want
15778 to handle them right away, but we don't want them getting mixed
15779 up with default args that are currently in the queue. */
15780 parser->unparsed_functions_queues
15781 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15783 /* Local variable names (and the `this' keyword) may not appear
15784 in a default argument. */
15785 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15786 parser->local_variables_forbidden_p = true;
15788 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15789 parm;
15790 parm = TREE_CHAIN (parm))
15792 cp_token_cache *tokens;
15793 tree default_arg = TREE_PURPOSE (parm);
15794 tree parsed_arg;
15795 VEC(tree,gc) *insts;
15796 tree copy;
15797 unsigned ix;
15799 if (!default_arg)
15800 continue;
15802 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15803 /* This can happen for a friend declaration for a function
15804 already declared with default arguments. */
15805 continue;
15807 /* Push the saved tokens for the default argument onto the parser's
15808 lexer stack. */
15809 tokens = DEFARG_TOKENS (default_arg);
15810 cp_parser_push_lexer_for_tokens (parser, tokens);
15812 /* Parse the assignment-expression. */
15813 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15815 if (!processing_template_decl)
15816 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15818 TREE_PURPOSE (parm) = parsed_arg;
15820 /* Update any instantiations we've already created. */
15821 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15822 VEC_iterate (tree, insts, ix, copy); ix++)
15823 TREE_PURPOSE (copy) = parsed_arg;
15825 /* If the token stream has not been completely used up, then
15826 there was extra junk after the end of the default
15827 argument. */
15828 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15829 cp_parser_error (parser, "expected %<,%>");
15831 /* Revert to the main lexer. */
15832 cp_parser_pop_lexer (parser);
15835 /* Restore the state of local_variables_forbidden_p. */
15836 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15838 /* Restore the queue. */
15839 parser->unparsed_functions_queues
15840 = TREE_CHAIN (parser->unparsed_functions_queues);
15843 /* Parse the operand of `sizeof' (or a similar operator). Returns
15844 either a TYPE or an expression, depending on the form of the
15845 input. The KEYWORD indicates which kind of expression we have
15846 encountered. */
15848 static tree
15849 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15851 static const char *format;
15852 tree expr = NULL_TREE;
15853 const char *saved_message;
15854 bool saved_integral_constant_expression_p;
15855 bool saved_non_integral_constant_expression_p;
15857 /* Initialize FORMAT the first time we get here. */
15858 if (!format)
15859 format = "types may not be defined in '%s' expressions";
15861 /* Types cannot be defined in a `sizeof' expression. Save away the
15862 old message. */
15863 saved_message = parser->type_definition_forbidden_message;
15864 /* And create the new one. */
15865 parser->type_definition_forbidden_message
15866 = XNEWVEC (const char, strlen (format)
15867 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15868 + 1 /* `\0' */);
15869 sprintf ((char *) parser->type_definition_forbidden_message,
15870 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15872 /* The restrictions on constant-expressions do not apply inside
15873 sizeof expressions. */
15874 saved_integral_constant_expression_p
15875 = parser->integral_constant_expression_p;
15876 saved_non_integral_constant_expression_p
15877 = parser->non_integral_constant_expression_p;
15878 parser->integral_constant_expression_p = false;
15880 /* Do not actually evaluate the expression. */
15881 ++skip_evaluation;
15882 /* If it's a `(', then we might be looking at the type-id
15883 construction. */
15884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15886 tree type;
15887 bool saved_in_type_id_in_expr_p;
15889 /* We can't be sure yet whether we're looking at a type-id or an
15890 expression. */
15891 cp_parser_parse_tentatively (parser);
15892 /* Consume the `('. */
15893 cp_lexer_consume_token (parser->lexer);
15894 /* Parse the type-id. */
15895 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15896 parser->in_type_id_in_expr_p = true;
15897 type = cp_parser_type_id (parser);
15898 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15899 /* Now, look for the trailing `)'. */
15900 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15901 /* If all went well, then we're done. */
15902 if (cp_parser_parse_definitely (parser))
15904 cp_decl_specifier_seq decl_specs;
15906 /* Build a trivial decl-specifier-seq. */
15907 clear_decl_specs (&decl_specs);
15908 decl_specs.type = type;
15910 /* Call grokdeclarator to figure out what type this is. */
15911 expr = grokdeclarator (NULL,
15912 &decl_specs,
15913 TYPENAME,
15914 /*initialized=*/0,
15915 /*attrlist=*/NULL);
15919 /* If the type-id production did not work out, then we must be
15920 looking at the unary-expression production. */
15921 if (!expr)
15922 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15923 /*cast_p=*/false);
15924 /* Go back to evaluating expressions. */
15925 --skip_evaluation;
15927 /* Free the message we created. */
15928 free ((char *) parser->type_definition_forbidden_message);
15929 /* And restore the old one. */
15930 parser->type_definition_forbidden_message = saved_message;
15931 parser->integral_constant_expression_p
15932 = saved_integral_constant_expression_p;
15933 parser->non_integral_constant_expression_p
15934 = saved_non_integral_constant_expression_p;
15936 return expr;
15939 /* If the current declaration has no declarator, return true. */
15941 static bool
15942 cp_parser_declares_only_class_p (cp_parser *parser)
15944 /* If the next token is a `;' or a `,' then there is no
15945 declarator. */
15946 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15947 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15950 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15952 static void
15953 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15954 cp_storage_class storage_class)
15956 if (decl_specs->storage_class != sc_none)
15957 decl_specs->multiple_storage_classes_p = true;
15958 else
15959 decl_specs->storage_class = storage_class;
15962 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15963 is true, the type is a user-defined type; otherwise it is a
15964 built-in type specified by a keyword. */
15966 static void
15967 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15968 tree type_spec,
15969 bool user_defined_p)
15971 decl_specs->any_specifiers_p = true;
15973 /* If the user tries to redeclare bool or wchar_t (with, for
15974 example, in "typedef int wchar_t;") we remember that this is what
15975 happened. In system headers, we ignore these declarations so
15976 that G++ can work with system headers that are not C++-safe. */
15977 if (decl_specs->specs[(int) ds_typedef]
15978 && !user_defined_p
15979 && (type_spec == boolean_type_node
15980 || type_spec == wchar_type_node)
15981 && (decl_specs->type
15982 || decl_specs->specs[(int) ds_long]
15983 || decl_specs->specs[(int) ds_short]
15984 || decl_specs->specs[(int) ds_unsigned]
15985 || decl_specs->specs[(int) ds_signed]))
15987 decl_specs->redefined_builtin_type = type_spec;
15988 if (!decl_specs->type)
15990 decl_specs->type = type_spec;
15991 decl_specs->user_defined_type_p = false;
15994 else if (decl_specs->type)
15995 decl_specs->multiple_types_p = true;
15996 else
15998 decl_specs->type = type_spec;
15999 decl_specs->user_defined_type_p = user_defined_p;
16000 decl_specs->redefined_builtin_type = NULL_TREE;
16004 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16005 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16007 static bool
16008 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16010 return decl_specifiers->specs[(int) ds_friend] != 0;
16013 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16014 issue an error message indicating that TOKEN_DESC was expected.
16016 Returns the token consumed, if the token had the appropriate type.
16017 Otherwise, returns NULL. */
16019 static cp_token *
16020 cp_parser_require (cp_parser* parser,
16021 enum cpp_ttype type,
16022 const char* token_desc)
16024 if (cp_lexer_next_token_is (parser->lexer, type))
16025 return cp_lexer_consume_token (parser->lexer);
16026 else
16028 /* Output the MESSAGE -- unless we're parsing tentatively. */
16029 if (!cp_parser_simulate_error (parser))
16031 char *message = concat ("expected ", token_desc, NULL);
16032 cp_parser_error (parser, message);
16033 free (message);
16035 return NULL;
16039 /* Like cp_parser_require, except that tokens will be skipped until
16040 the desired token is found. An error message is still produced if
16041 the next token is not as expected. */
16043 static void
16044 cp_parser_skip_until_found (cp_parser* parser,
16045 enum cpp_ttype type,
16046 const char* token_desc)
16048 cp_token *token;
16049 unsigned nesting_depth = 0;
16051 if (cp_parser_require (parser, type, token_desc))
16052 return;
16054 /* Skip tokens until the desired token is found. */
16055 while (true)
16057 /* Peek at the next token. */
16058 token = cp_lexer_peek_token (parser->lexer);
16060 /* If we've reached the token we want, consume it and stop. */
16061 if (token->type == type && !nesting_depth)
16063 cp_lexer_consume_token (parser->lexer);
16064 return;
16067 switch (token->type)
16069 case CPP_EOF:
16070 case CPP_PRAGMA_EOL:
16071 /* If we've run out of tokens, stop. */
16072 return;
16074 case CPP_OPEN_BRACE:
16075 case CPP_OPEN_PAREN:
16076 case CPP_OPEN_SQUARE:
16077 ++nesting_depth;
16078 break;
16080 case CPP_CLOSE_BRACE:
16081 case CPP_CLOSE_PAREN:
16082 case CPP_CLOSE_SQUARE:
16083 if (nesting_depth-- == 0)
16084 return;
16085 break;
16087 default:
16088 break;
16091 /* Consume this token. */
16092 cp_lexer_consume_token (parser->lexer);
16096 /* If the next token is the indicated keyword, consume it. Otherwise,
16097 issue an error message indicating that TOKEN_DESC was expected.
16099 Returns the token consumed, if the token had the appropriate type.
16100 Otherwise, returns NULL. */
16102 static cp_token *
16103 cp_parser_require_keyword (cp_parser* parser,
16104 enum rid keyword,
16105 const char* token_desc)
16107 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16109 if (token && token->keyword != keyword)
16111 dyn_string_t error_msg;
16113 /* Format the error message. */
16114 error_msg = dyn_string_new (0);
16115 dyn_string_append_cstr (error_msg, "expected ");
16116 dyn_string_append_cstr (error_msg, token_desc);
16117 cp_parser_error (parser, error_msg->s);
16118 dyn_string_delete (error_msg);
16119 return NULL;
16122 return token;
16125 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16126 function-definition. */
16128 static bool
16129 cp_parser_token_starts_function_definition_p (cp_token* token)
16131 return (/* An ordinary function-body begins with an `{'. */
16132 token->type == CPP_OPEN_BRACE
16133 /* A ctor-initializer begins with a `:'. */
16134 || token->type == CPP_COLON
16135 /* A function-try-block begins with `try'. */
16136 || token->keyword == RID_TRY
16137 /* The named return value extension begins with `return'. */
16138 || token->keyword == RID_RETURN);
16141 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16142 definition. */
16144 static bool
16145 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16147 cp_token *token;
16149 token = cp_lexer_peek_token (parser->lexer);
16150 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16153 /* Returns TRUE iff the next token is the "," or ">" ending a
16154 template-argument. */
16156 static bool
16157 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16159 cp_token *token;
16161 token = cp_lexer_peek_token (parser->lexer);
16162 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16165 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16166 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16168 static bool
16169 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16170 size_t n)
16172 cp_token *token;
16174 token = cp_lexer_peek_nth_token (parser->lexer, n);
16175 if (token->type == CPP_LESS)
16176 return true;
16177 /* Check for the sequence `<::' in the original code. It would be lexed as
16178 `[:', where `[' is a digraph, and there is no whitespace before
16179 `:'. */
16180 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16182 cp_token *token2;
16183 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16184 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16185 return true;
16187 return false;
16190 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16191 or none_type otherwise. */
16193 static enum tag_types
16194 cp_parser_token_is_class_key (cp_token* token)
16196 switch (token->keyword)
16198 case RID_CLASS:
16199 return class_type;
16200 case RID_STRUCT:
16201 return record_type;
16202 case RID_UNION:
16203 return union_type;
16205 default:
16206 return none_type;
16210 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16212 static void
16213 cp_parser_check_class_key (enum tag_types class_key, tree type)
16215 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16216 pedwarn ("%qs tag used in naming %q#T",
16217 class_key == union_type ? "union"
16218 : class_key == record_type ? "struct" : "class",
16219 type);
16222 /* Issue an error message if DECL is redeclared with different
16223 access than its original declaration [class.access.spec/3].
16224 This applies to nested classes and nested class templates.
16225 [class.mem/1]. */
16227 static void
16228 cp_parser_check_access_in_redeclaration (tree decl)
16230 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16231 return;
16233 if ((TREE_PRIVATE (decl)
16234 != (current_access_specifier == access_private_node))
16235 || (TREE_PROTECTED (decl)
16236 != (current_access_specifier == access_protected_node)))
16237 error ("%qD redeclared with different access", decl);
16240 /* Look for the `template' keyword, as a syntactic disambiguator.
16241 Return TRUE iff it is present, in which case it will be
16242 consumed. */
16244 static bool
16245 cp_parser_optional_template_keyword (cp_parser *parser)
16247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16249 /* The `template' keyword can only be used within templates;
16250 outside templates the parser can always figure out what is a
16251 template and what is not. */
16252 if (!processing_template_decl)
16254 error ("%<template%> (as a disambiguator) is only allowed "
16255 "within templates");
16256 /* If this part of the token stream is rescanned, the same
16257 error message would be generated. So, we purge the token
16258 from the stream. */
16259 cp_lexer_purge_token (parser->lexer);
16260 return false;
16262 else
16264 /* Consume the `template' keyword. */
16265 cp_lexer_consume_token (parser->lexer);
16266 return true;
16270 return false;
16273 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16274 set PARSER->SCOPE, and perform other related actions. */
16276 static void
16277 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16279 tree value;
16280 tree check;
16282 /* Get the stored value. */
16283 value = cp_lexer_consume_token (parser->lexer)->value;
16284 /* Perform any access checks that were deferred. */
16285 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16286 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16287 /* Set the scope from the stored value. */
16288 parser->scope = TREE_VALUE (value);
16289 parser->qualifying_scope = TREE_TYPE (value);
16290 parser->object_scope = NULL_TREE;
16293 /* Consume tokens up through a non-nested END token. */
16295 static void
16296 cp_parser_cache_group (cp_parser *parser,
16297 enum cpp_ttype end,
16298 unsigned depth)
16300 while (true)
16302 cp_token *token;
16304 /* Abort a parenthesized expression if we encounter a brace. */
16305 if ((end == CPP_CLOSE_PAREN || depth == 0)
16306 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16307 return;
16308 /* If we've reached the end of the file, stop. */
16309 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16310 || (end != CPP_PRAGMA_EOL
16311 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16312 return;
16313 /* Consume the next token. */
16314 token = cp_lexer_consume_token (parser->lexer);
16315 /* See if it starts a new group. */
16316 if (token->type == CPP_OPEN_BRACE)
16318 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16319 if (depth == 0)
16320 return;
16322 else if (token->type == CPP_OPEN_PAREN)
16323 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16324 else if (token->type == CPP_PRAGMA)
16325 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16326 else if (token->type == end)
16327 return;
16331 /* Begin parsing tentatively. We always save tokens while parsing
16332 tentatively so that if the tentative parsing fails we can restore the
16333 tokens. */
16335 static void
16336 cp_parser_parse_tentatively (cp_parser* parser)
16338 /* Enter a new parsing context. */
16339 parser->context = cp_parser_context_new (parser->context);
16340 /* Begin saving tokens. */
16341 cp_lexer_save_tokens (parser->lexer);
16342 /* In order to avoid repetitive access control error messages,
16343 access checks are queued up until we are no longer parsing
16344 tentatively. */
16345 push_deferring_access_checks (dk_deferred);
16348 /* Commit to the currently active tentative parse. */
16350 static void
16351 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16353 cp_parser_context *context;
16354 cp_lexer *lexer;
16356 /* Mark all of the levels as committed. */
16357 lexer = parser->lexer;
16358 for (context = parser->context; context->next; context = context->next)
16360 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16361 break;
16362 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16363 while (!cp_lexer_saving_tokens (lexer))
16364 lexer = lexer->next;
16365 cp_lexer_commit_tokens (lexer);
16369 /* Abort the currently active tentative parse. All consumed tokens
16370 will be rolled back, and no diagnostics will be issued. */
16372 static void
16373 cp_parser_abort_tentative_parse (cp_parser* parser)
16375 cp_parser_simulate_error (parser);
16376 /* Now, pretend that we want to see if the construct was
16377 successfully parsed. */
16378 cp_parser_parse_definitely (parser);
16381 /* Stop parsing tentatively. If a parse error has occurred, restore the
16382 token stream. Otherwise, commit to the tokens we have consumed.
16383 Returns true if no error occurred; false otherwise. */
16385 static bool
16386 cp_parser_parse_definitely (cp_parser* parser)
16388 bool error_occurred;
16389 cp_parser_context *context;
16391 /* Remember whether or not an error occurred, since we are about to
16392 destroy that information. */
16393 error_occurred = cp_parser_error_occurred (parser);
16394 /* Remove the topmost context from the stack. */
16395 context = parser->context;
16396 parser->context = context->next;
16397 /* If no parse errors occurred, commit to the tentative parse. */
16398 if (!error_occurred)
16400 /* Commit to the tokens read tentatively, unless that was
16401 already done. */
16402 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16403 cp_lexer_commit_tokens (parser->lexer);
16405 pop_to_parent_deferring_access_checks ();
16407 /* Otherwise, if errors occurred, roll back our state so that things
16408 are just as they were before we began the tentative parse. */
16409 else
16411 cp_lexer_rollback_tokens (parser->lexer);
16412 pop_deferring_access_checks ();
16414 /* Add the context to the front of the free list. */
16415 context->next = cp_parser_context_free_list;
16416 cp_parser_context_free_list = context;
16418 return !error_occurred;
16421 /* Returns true if we are parsing tentatively and are not committed to
16422 this tentative parse. */
16424 static bool
16425 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16427 return (cp_parser_parsing_tentatively (parser)
16428 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16431 /* Returns nonzero iff an error has occurred during the most recent
16432 tentative parse. */
16434 static bool
16435 cp_parser_error_occurred (cp_parser* parser)
16437 return (cp_parser_parsing_tentatively (parser)
16438 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16441 /* Returns nonzero if GNU extensions are allowed. */
16443 static bool
16444 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16446 return parser->allow_gnu_extensions_p;
16449 /* Objective-C++ Productions */
16452 /* Parse an Objective-C expression, which feeds into a primary-expression
16453 above.
16455 objc-expression:
16456 objc-message-expression
16457 objc-string-literal
16458 objc-encode-expression
16459 objc-protocol-expression
16460 objc-selector-expression
16462 Returns a tree representation of the expression. */
16464 static tree
16465 cp_parser_objc_expression (cp_parser* parser)
16467 /* Try to figure out what kind of declaration is present. */
16468 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16470 switch (kwd->type)
16472 case CPP_OPEN_SQUARE:
16473 return cp_parser_objc_message_expression (parser);
16475 case CPP_OBJC_STRING:
16476 kwd = cp_lexer_consume_token (parser->lexer);
16477 return objc_build_string_object (kwd->value);
16479 case CPP_KEYWORD:
16480 switch (kwd->keyword)
16482 case RID_AT_ENCODE:
16483 return cp_parser_objc_encode_expression (parser);
16485 case RID_AT_PROTOCOL:
16486 return cp_parser_objc_protocol_expression (parser);
16488 case RID_AT_SELECTOR:
16489 return cp_parser_objc_selector_expression (parser);
16491 default:
16492 break;
16494 default:
16495 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16496 cp_parser_skip_to_end_of_block_or_statement (parser);
16499 return error_mark_node;
16502 /* Parse an Objective-C message expression.
16504 objc-message-expression:
16505 [ objc-message-receiver objc-message-args ]
16507 Returns a representation of an Objective-C message. */
16509 static tree
16510 cp_parser_objc_message_expression (cp_parser* parser)
16512 tree receiver, messageargs;
16514 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16515 receiver = cp_parser_objc_message_receiver (parser);
16516 messageargs = cp_parser_objc_message_args (parser);
16517 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16519 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16522 /* Parse an objc-message-receiver.
16524 objc-message-receiver:
16525 expression
16526 simple-type-specifier
16528 Returns a representation of the type or expression. */
16530 static tree
16531 cp_parser_objc_message_receiver (cp_parser* parser)
16533 tree rcv;
16535 /* An Objective-C message receiver may be either (1) a type
16536 or (2) an expression. */
16537 cp_parser_parse_tentatively (parser);
16538 rcv = cp_parser_expression (parser, false);
16540 if (cp_parser_parse_definitely (parser))
16541 return rcv;
16543 rcv = cp_parser_simple_type_specifier (parser,
16544 /*decl_specs=*/NULL,
16545 CP_PARSER_FLAGS_NONE);
16547 return objc_get_class_reference (rcv);
16550 /* Parse the arguments and selectors comprising an Objective-C message.
16552 objc-message-args:
16553 objc-selector
16554 objc-selector-args
16555 objc-selector-args , objc-comma-args
16557 objc-selector-args:
16558 objc-selector [opt] : assignment-expression
16559 objc-selector-args objc-selector [opt] : assignment-expression
16561 objc-comma-args:
16562 assignment-expression
16563 objc-comma-args , assignment-expression
16565 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16566 selector arguments and TREE_VALUE containing a list of comma
16567 arguments. */
16569 static tree
16570 cp_parser_objc_message_args (cp_parser* parser)
16572 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16573 bool maybe_unary_selector_p = true;
16574 cp_token *token = cp_lexer_peek_token (parser->lexer);
16576 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16578 tree selector = NULL_TREE, arg;
16580 if (token->type != CPP_COLON)
16581 selector = cp_parser_objc_selector (parser);
16583 /* Detect if we have a unary selector. */
16584 if (maybe_unary_selector_p
16585 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16586 return build_tree_list (selector, NULL_TREE);
16588 maybe_unary_selector_p = false;
16589 cp_parser_require (parser, CPP_COLON, "`:'");
16590 arg = cp_parser_assignment_expression (parser, false);
16592 sel_args
16593 = chainon (sel_args,
16594 build_tree_list (selector, arg));
16596 token = cp_lexer_peek_token (parser->lexer);
16599 /* Handle non-selector arguments, if any. */
16600 while (token->type == CPP_COMMA)
16602 tree arg;
16604 cp_lexer_consume_token (parser->lexer);
16605 arg = cp_parser_assignment_expression (parser, false);
16607 addl_args
16608 = chainon (addl_args,
16609 build_tree_list (NULL_TREE, arg));
16611 token = cp_lexer_peek_token (parser->lexer);
16614 return build_tree_list (sel_args, addl_args);
16617 /* Parse an Objective-C encode expression.
16619 objc-encode-expression:
16620 @encode objc-typename
16622 Returns an encoded representation of the type argument. */
16624 static tree
16625 cp_parser_objc_encode_expression (cp_parser* parser)
16627 tree type;
16629 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16630 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16631 type = complete_type (cp_parser_type_id (parser));
16632 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16634 if (!type)
16636 error ("%<@encode%> must specify a type as an argument");
16637 return error_mark_node;
16640 return objc_build_encode_expr (type);
16643 /* Parse an Objective-C @defs expression. */
16645 static tree
16646 cp_parser_objc_defs_expression (cp_parser *parser)
16648 tree name;
16650 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16651 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16652 name = cp_parser_identifier (parser);
16653 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16655 return objc_get_class_ivars (name);
16658 /* Parse an Objective-C protocol expression.
16660 objc-protocol-expression:
16661 @protocol ( identifier )
16663 Returns a representation of the protocol expression. */
16665 static tree
16666 cp_parser_objc_protocol_expression (cp_parser* parser)
16668 tree proto;
16670 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16671 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16672 proto = cp_parser_identifier (parser);
16673 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16675 return objc_build_protocol_expr (proto);
16678 /* Parse an Objective-C selector expression.
16680 objc-selector-expression:
16681 @selector ( objc-method-signature )
16683 objc-method-signature:
16684 objc-selector
16685 objc-selector-seq
16687 objc-selector-seq:
16688 objc-selector :
16689 objc-selector-seq objc-selector :
16691 Returns a representation of the method selector. */
16693 static tree
16694 cp_parser_objc_selector_expression (cp_parser* parser)
16696 tree sel_seq = NULL_TREE;
16697 bool maybe_unary_selector_p = true;
16698 cp_token *token;
16700 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16701 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16702 token = cp_lexer_peek_token (parser->lexer);
16704 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16705 || token->type == CPP_SCOPE)
16707 tree selector = NULL_TREE;
16709 if (token->type != CPP_COLON
16710 || token->type == CPP_SCOPE)
16711 selector = cp_parser_objc_selector (parser);
16713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16714 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16716 /* Detect if we have a unary selector. */
16717 if (maybe_unary_selector_p)
16719 sel_seq = selector;
16720 goto finish_selector;
16722 else
16724 cp_parser_error (parser, "expected %<:%>");
16727 maybe_unary_selector_p = false;
16728 token = cp_lexer_consume_token (parser->lexer);
16730 if (token->type == CPP_SCOPE)
16732 sel_seq
16733 = chainon (sel_seq,
16734 build_tree_list (selector, NULL_TREE));
16735 sel_seq
16736 = chainon (sel_seq,
16737 build_tree_list (NULL_TREE, NULL_TREE));
16739 else
16740 sel_seq
16741 = chainon (sel_seq,
16742 build_tree_list (selector, NULL_TREE));
16744 token = cp_lexer_peek_token (parser->lexer);
16747 finish_selector:
16748 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16750 return objc_build_selector_expr (sel_seq);
16753 /* Parse a list of identifiers.
16755 objc-identifier-list:
16756 identifier
16757 objc-identifier-list , identifier
16759 Returns a TREE_LIST of identifier nodes. */
16761 static tree
16762 cp_parser_objc_identifier_list (cp_parser* parser)
16764 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16765 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16767 while (sep->type == CPP_COMMA)
16769 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16770 list = chainon (list,
16771 build_tree_list (NULL_TREE,
16772 cp_parser_identifier (parser)));
16773 sep = cp_lexer_peek_token (parser->lexer);
16776 return list;
16779 /* Parse an Objective-C alias declaration.
16781 objc-alias-declaration:
16782 @compatibility_alias identifier identifier ;
16784 This function registers the alias mapping with the Objective-C front-end.
16785 It returns nothing. */
16787 static void
16788 cp_parser_objc_alias_declaration (cp_parser* parser)
16790 tree alias, orig;
16792 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16793 alias = cp_parser_identifier (parser);
16794 orig = cp_parser_identifier (parser);
16795 objc_declare_alias (alias, orig);
16796 cp_parser_consume_semicolon_at_end_of_statement (parser);
16799 /* Parse an Objective-C class forward-declaration.
16801 objc-class-declaration:
16802 @class objc-identifier-list ;
16804 The function registers the forward declarations with the Objective-C
16805 front-end. It returns nothing. */
16807 static void
16808 cp_parser_objc_class_declaration (cp_parser* parser)
16810 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16811 objc_declare_class (cp_parser_objc_identifier_list (parser));
16812 cp_parser_consume_semicolon_at_end_of_statement (parser);
16815 /* Parse a list of Objective-C protocol references.
16817 objc-protocol-refs-opt:
16818 objc-protocol-refs [opt]
16820 objc-protocol-refs:
16821 < objc-identifier-list >
16823 Returns a TREE_LIST of identifiers, if any. */
16825 static tree
16826 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16828 tree protorefs = NULL_TREE;
16830 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16832 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16833 protorefs = cp_parser_objc_identifier_list (parser);
16834 cp_parser_require (parser, CPP_GREATER, "`>'");
16837 return protorefs;
16840 /* Parse a Objective-C visibility specification. */
16842 static void
16843 cp_parser_objc_visibility_spec (cp_parser* parser)
16845 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16847 switch (vis->keyword)
16849 case RID_AT_PRIVATE:
16850 objc_set_visibility (2);
16851 break;
16852 case RID_AT_PROTECTED:
16853 objc_set_visibility (0);
16854 break;
16855 case RID_AT_PUBLIC:
16856 objc_set_visibility (1);
16857 break;
16858 default:
16859 return;
16862 /* Eat '@private'/'@protected'/'@public'. */
16863 cp_lexer_consume_token (parser->lexer);
16866 /* Parse an Objective-C method type. */
16868 static void
16869 cp_parser_objc_method_type (cp_parser* parser)
16871 objc_set_method_type
16872 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16873 ? PLUS_EXPR
16874 : MINUS_EXPR);
16877 /* Parse an Objective-C protocol qualifier. */
16879 static tree
16880 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16882 tree quals = NULL_TREE, node;
16883 cp_token *token = cp_lexer_peek_token (parser->lexer);
16885 node = token->value;
16887 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16888 && (node == ridpointers [(int) RID_IN]
16889 || node == ridpointers [(int) RID_OUT]
16890 || node == ridpointers [(int) RID_INOUT]
16891 || node == ridpointers [(int) RID_BYCOPY]
16892 || node == ridpointers [(int) RID_BYREF]
16893 || node == ridpointers [(int) RID_ONEWAY]))
16895 quals = tree_cons (NULL_TREE, node, quals);
16896 cp_lexer_consume_token (parser->lexer);
16897 token = cp_lexer_peek_token (parser->lexer);
16898 node = token->value;
16901 return quals;
16904 /* Parse an Objective-C typename. */
16906 static tree
16907 cp_parser_objc_typename (cp_parser* parser)
16909 tree typename = NULL_TREE;
16911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16913 tree proto_quals, cp_type = NULL_TREE;
16915 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16916 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16918 /* An ObjC type name may consist of just protocol qualifiers, in which
16919 case the type shall default to 'id'. */
16920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16921 cp_type = cp_parser_type_id (parser);
16923 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16924 typename = build_tree_list (proto_quals, cp_type);
16927 return typename;
16930 /* Check to see if TYPE refers to an Objective-C selector name. */
16932 static bool
16933 cp_parser_objc_selector_p (enum cpp_ttype type)
16935 return (type == CPP_NAME || type == CPP_KEYWORD
16936 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16937 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16938 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16939 || type == CPP_XOR || type == CPP_XOR_EQ);
16942 /* Parse an Objective-C selector. */
16944 static tree
16945 cp_parser_objc_selector (cp_parser* parser)
16947 cp_token *token = cp_lexer_consume_token (parser->lexer);
16949 if (!cp_parser_objc_selector_p (token->type))
16951 error ("invalid Objective-C++ selector name");
16952 return error_mark_node;
16955 /* C++ operator names are allowed to appear in ObjC selectors. */
16956 switch (token->type)
16958 case CPP_AND_AND: return get_identifier ("and");
16959 case CPP_AND_EQ: return get_identifier ("and_eq");
16960 case CPP_AND: return get_identifier ("bitand");
16961 case CPP_OR: return get_identifier ("bitor");
16962 case CPP_COMPL: return get_identifier ("compl");
16963 case CPP_NOT: return get_identifier ("not");
16964 case CPP_NOT_EQ: return get_identifier ("not_eq");
16965 case CPP_OR_OR: return get_identifier ("or");
16966 case CPP_OR_EQ: return get_identifier ("or_eq");
16967 case CPP_XOR: return get_identifier ("xor");
16968 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16969 default: return token->value;
16973 /* Parse an Objective-C params list. */
16975 static tree
16976 cp_parser_objc_method_keyword_params (cp_parser* parser)
16978 tree params = NULL_TREE;
16979 bool maybe_unary_selector_p = true;
16980 cp_token *token = cp_lexer_peek_token (parser->lexer);
16982 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16984 tree selector = NULL_TREE, typename, identifier;
16986 if (token->type != CPP_COLON)
16987 selector = cp_parser_objc_selector (parser);
16989 /* Detect if we have a unary selector. */
16990 if (maybe_unary_selector_p
16991 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16992 return selector;
16994 maybe_unary_selector_p = false;
16995 cp_parser_require (parser, CPP_COLON, "`:'");
16996 typename = cp_parser_objc_typename (parser);
16997 identifier = cp_parser_identifier (parser);
16999 params
17000 = chainon (params,
17001 objc_build_keyword_decl (selector,
17002 typename,
17003 identifier));
17005 token = cp_lexer_peek_token (parser->lexer);
17008 return params;
17011 /* Parse the non-keyword Objective-C params. */
17013 static tree
17014 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17016 tree params = make_node (TREE_LIST);
17017 cp_token *token = cp_lexer_peek_token (parser->lexer);
17018 *ellipsisp = false; /* Initially, assume no ellipsis. */
17020 while (token->type == CPP_COMMA)
17022 cp_parameter_declarator *parmdecl;
17023 tree parm;
17025 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17026 token = cp_lexer_peek_token (parser->lexer);
17028 if (token->type == CPP_ELLIPSIS)
17030 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17031 *ellipsisp = true;
17032 break;
17035 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17036 parm = grokdeclarator (parmdecl->declarator,
17037 &parmdecl->decl_specifiers,
17038 PARM, /*initialized=*/0,
17039 /*attrlist=*/NULL);
17041 chainon (params, build_tree_list (NULL_TREE, parm));
17042 token = cp_lexer_peek_token (parser->lexer);
17045 return params;
17048 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17050 static void
17051 cp_parser_objc_interstitial_code (cp_parser* parser)
17053 cp_token *token = cp_lexer_peek_token (parser->lexer);
17055 /* If the next token is `extern' and the following token is a string
17056 literal, then we have a linkage specification. */
17057 if (token->keyword == RID_EXTERN
17058 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17059 cp_parser_linkage_specification (parser);
17060 /* Handle #pragma, if any. */
17061 else if (token->type == CPP_PRAGMA)
17062 cp_parser_pragma (parser, pragma_external);
17063 /* Allow stray semicolons. */
17064 else if (token->type == CPP_SEMICOLON)
17065 cp_lexer_consume_token (parser->lexer);
17066 /* Finally, try to parse a block-declaration, or a function-definition. */
17067 else
17068 cp_parser_block_declaration (parser, /*statement_p=*/false);
17071 /* Parse a method signature. */
17073 static tree
17074 cp_parser_objc_method_signature (cp_parser* parser)
17076 tree rettype, kwdparms, optparms;
17077 bool ellipsis = false;
17079 cp_parser_objc_method_type (parser);
17080 rettype = cp_parser_objc_typename (parser);
17081 kwdparms = cp_parser_objc_method_keyword_params (parser);
17082 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17084 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17087 /* Pars an Objective-C method prototype list. */
17089 static void
17090 cp_parser_objc_method_prototype_list (cp_parser* parser)
17092 cp_token *token = cp_lexer_peek_token (parser->lexer);
17094 while (token->keyword != RID_AT_END)
17096 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17098 objc_add_method_declaration
17099 (cp_parser_objc_method_signature (parser));
17100 cp_parser_consume_semicolon_at_end_of_statement (parser);
17102 else
17103 /* Allow for interspersed non-ObjC++ code. */
17104 cp_parser_objc_interstitial_code (parser);
17106 token = cp_lexer_peek_token (parser->lexer);
17109 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17110 objc_finish_interface ();
17113 /* Parse an Objective-C method definition list. */
17115 static void
17116 cp_parser_objc_method_definition_list (cp_parser* parser)
17118 cp_token *token = cp_lexer_peek_token (parser->lexer);
17120 while (token->keyword != RID_AT_END)
17122 tree meth;
17124 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17126 push_deferring_access_checks (dk_deferred);
17127 objc_start_method_definition
17128 (cp_parser_objc_method_signature (parser));
17130 /* For historical reasons, we accept an optional semicolon. */
17131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17132 cp_lexer_consume_token (parser->lexer);
17134 perform_deferred_access_checks ();
17135 stop_deferring_access_checks ();
17136 meth = cp_parser_function_definition_after_declarator (parser,
17137 false);
17138 pop_deferring_access_checks ();
17139 objc_finish_method_definition (meth);
17141 else
17142 /* Allow for interspersed non-ObjC++ code. */
17143 cp_parser_objc_interstitial_code (parser);
17145 token = cp_lexer_peek_token (parser->lexer);
17148 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17149 objc_finish_implementation ();
17152 /* Parse Objective-C ivars. */
17154 static void
17155 cp_parser_objc_class_ivars (cp_parser* parser)
17157 cp_token *token = cp_lexer_peek_token (parser->lexer);
17159 if (token->type != CPP_OPEN_BRACE)
17160 return; /* No ivars specified. */
17162 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17163 token = cp_lexer_peek_token (parser->lexer);
17165 while (token->type != CPP_CLOSE_BRACE)
17167 cp_decl_specifier_seq declspecs;
17168 int decl_class_or_enum_p;
17169 tree prefix_attributes;
17171 cp_parser_objc_visibility_spec (parser);
17173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17174 break;
17176 cp_parser_decl_specifier_seq (parser,
17177 CP_PARSER_FLAGS_OPTIONAL,
17178 &declspecs,
17179 &decl_class_or_enum_p);
17180 prefix_attributes = declspecs.attributes;
17181 declspecs.attributes = NULL_TREE;
17183 /* Keep going until we hit the `;' at the end of the
17184 declaration. */
17185 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17187 tree width = NULL_TREE, attributes, first_attribute, decl;
17188 cp_declarator *declarator = NULL;
17189 int ctor_dtor_or_conv_p;
17191 /* Check for a (possibly unnamed) bitfield declaration. */
17192 token = cp_lexer_peek_token (parser->lexer);
17193 if (token->type == CPP_COLON)
17194 goto eat_colon;
17196 if (token->type == CPP_NAME
17197 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17198 == CPP_COLON))
17200 /* Get the name of the bitfield. */
17201 declarator = make_id_declarator (NULL_TREE,
17202 cp_parser_identifier (parser),
17203 sfk_none);
17205 eat_colon:
17206 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17207 /* Get the width of the bitfield. */
17208 width
17209 = cp_parser_constant_expression (parser,
17210 /*allow_non_constant=*/false,
17211 NULL);
17213 else
17215 /* Parse the declarator. */
17216 declarator
17217 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17218 &ctor_dtor_or_conv_p,
17219 /*parenthesized_p=*/NULL,
17220 /*member_p=*/false);
17223 /* Look for attributes that apply to the ivar. */
17224 attributes = cp_parser_attributes_opt (parser);
17225 /* Remember which attributes are prefix attributes and
17226 which are not. */
17227 first_attribute = attributes;
17228 /* Combine the attributes. */
17229 attributes = chainon (prefix_attributes, attributes);
17231 if (width)
17233 /* Create the bitfield declaration. */
17234 decl = grokbitfield (declarator, &declspecs, width);
17235 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17237 else
17238 decl = grokfield (declarator, &declspecs, NULL_TREE,
17239 NULL_TREE, attributes);
17241 /* Add the instance variable. */
17242 objc_add_instance_variable (decl);
17244 /* Reset PREFIX_ATTRIBUTES. */
17245 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17246 attributes = TREE_CHAIN (attributes);
17247 if (attributes)
17248 TREE_CHAIN (attributes) = NULL_TREE;
17250 token = cp_lexer_peek_token (parser->lexer);
17252 if (token->type == CPP_COMMA)
17254 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17255 continue;
17257 break;
17260 cp_parser_consume_semicolon_at_end_of_statement (parser);
17261 token = cp_lexer_peek_token (parser->lexer);
17264 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17265 /* For historical reasons, we accept an optional semicolon. */
17266 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17267 cp_lexer_consume_token (parser->lexer);
17270 /* Parse an Objective-C protocol declaration. */
17272 static void
17273 cp_parser_objc_protocol_declaration (cp_parser* parser)
17275 tree proto, protorefs;
17276 cp_token *tok;
17278 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17281 error ("identifier expected after %<@protocol%>");
17282 goto finish;
17285 /* See if we have a forward declaration or a definition. */
17286 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17288 /* Try a forward declaration first. */
17289 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17291 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17292 finish:
17293 cp_parser_consume_semicolon_at_end_of_statement (parser);
17296 /* Ok, we got a full-fledged definition (or at least should). */
17297 else
17299 proto = cp_parser_identifier (parser);
17300 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17301 objc_start_protocol (proto, protorefs);
17302 cp_parser_objc_method_prototype_list (parser);
17306 /* Parse an Objective-C superclass or category. */
17308 static void
17309 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17310 tree *categ)
17312 cp_token *next = cp_lexer_peek_token (parser->lexer);
17314 *super = *categ = NULL_TREE;
17315 if (next->type == CPP_COLON)
17317 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17318 *super = cp_parser_identifier (parser);
17320 else if (next->type == CPP_OPEN_PAREN)
17322 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17323 *categ = cp_parser_identifier (parser);
17324 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17328 /* Parse an Objective-C class interface. */
17330 static void
17331 cp_parser_objc_class_interface (cp_parser* parser)
17333 tree name, super, categ, protos;
17335 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17336 name = cp_parser_identifier (parser);
17337 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17338 protos = cp_parser_objc_protocol_refs_opt (parser);
17340 /* We have either a class or a category on our hands. */
17341 if (categ)
17342 objc_start_category_interface (name, categ, protos);
17343 else
17345 objc_start_class_interface (name, super, protos);
17346 /* Handle instance variable declarations, if any. */
17347 cp_parser_objc_class_ivars (parser);
17348 objc_continue_interface ();
17351 cp_parser_objc_method_prototype_list (parser);
17354 /* Parse an Objective-C class implementation. */
17356 static void
17357 cp_parser_objc_class_implementation (cp_parser* parser)
17359 tree name, super, categ;
17361 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17362 name = cp_parser_identifier (parser);
17363 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17365 /* We have either a class or a category on our hands. */
17366 if (categ)
17367 objc_start_category_implementation (name, categ);
17368 else
17370 objc_start_class_implementation (name, super);
17371 /* Handle instance variable declarations, if any. */
17372 cp_parser_objc_class_ivars (parser);
17373 objc_continue_implementation ();
17376 cp_parser_objc_method_definition_list (parser);
17379 /* Consume the @end token and finish off the implementation. */
17381 static void
17382 cp_parser_objc_end_implementation (cp_parser* parser)
17384 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17385 objc_finish_implementation ();
17388 /* Parse an Objective-C declaration. */
17390 static void
17391 cp_parser_objc_declaration (cp_parser* parser)
17393 /* Try to figure out what kind of declaration is present. */
17394 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17396 switch (kwd->keyword)
17398 case RID_AT_ALIAS:
17399 cp_parser_objc_alias_declaration (parser);
17400 break;
17401 case RID_AT_CLASS:
17402 cp_parser_objc_class_declaration (parser);
17403 break;
17404 case RID_AT_PROTOCOL:
17405 cp_parser_objc_protocol_declaration (parser);
17406 break;
17407 case RID_AT_INTERFACE:
17408 cp_parser_objc_class_interface (parser);
17409 break;
17410 case RID_AT_IMPLEMENTATION:
17411 cp_parser_objc_class_implementation (parser);
17412 break;
17413 case RID_AT_END:
17414 cp_parser_objc_end_implementation (parser);
17415 break;
17416 default:
17417 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17418 cp_parser_skip_to_end_of_block_or_statement (parser);
17422 /* Parse an Objective-C try-catch-finally statement.
17424 objc-try-catch-finally-stmt:
17425 @try compound-statement objc-catch-clause-seq [opt]
17426 objc-finally-clause [opt]
17428 objc-catch-clause-seq:
17429 objc-catch-clause objc-catch-clause-seq [opt]
17431 objc-catch-clause:
17432 @catch ( exception-declaration ) compound-statement
17434 objc-finally-clause
17435 @finally compound-statement
17437 Returns NULL_TREE. */
17439 static tree
17440 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17441 location_t location;
17442 tree stmt;
17444 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17445 location = cp_lexer_peek_token (parser->lexer)->location;
17446 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17447 node, lest it get absorbed into the surrounding block. */
17448 stmt = push_stmt_list ();
17449 cp_parser_compound_statement (parser, NULL, false);
17450 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17452 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17454 cp_parameter_declarator *parmdecl;
17455 tree parm;
17457 cp_lexer_consume_token (parser->lexer);
17458 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17459 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17460 parm = grokdeclarator (parmdecl->declarator,
17461 &parmdecl->decl_specifiers,
17462 PARM, /*initialized=*/0,
17463 /*attrlist=*/NULL);
17464 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17465 objc_begin_catch_clause (parm);
17466 cp_parser_compound_statement (parser, NULL, false);
17467 objc_finish_catch_clause ();
17470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17472 cp_lexer_consume_token (parser->lexer);
17473 location = cp_lexer_peek_token (parser->lexer)->location;
17474 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17475 node, lest it get absorbed into the surrounding block. */
17476 stmt = push_stmt_list ();
17477 cp_parser_compound_statement (parser, NULL, false);
17478 objc_build_finally_clause (location, pop_stmt_list (stmt));
17481 return objc_finish_try_stmt ();
17484 /* Parse an Objective-C synchronized statement.
17486 objc-synchronized-stmt:
17487 @synchronized ( expression ) compound-statement
17489 Returns NULL_TREE. */
17491 static tree
17492 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17493 location_t location;
17494 tree lock, stmt;
17496 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17498 location = cp_lexer_peek_token (parser->lexer)->location;
17499 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17500 lock = cp_parser_expression (parser, false);
17501 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17503 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17504 node, lest it get absorbed into the surrounding block. */
17505 stmt = push_stmt_list ();
17506 cp_parser_compound_statement (parser, NULL, false);
17508 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17511 /* Parse an Objective-C throw statement.
17513 objc-throw-stmt:
17514 @throw assignment-expression [opt] ;
17516 Returns a constructed '@throw' statement. */
17518 static tree
17519 cp_parser_objc_throw_statement (cp_parser *parser) {
17520 tree expr = NULL_TREE;
17522 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17525 expr = cp_parser_assignment_expression (parser, false);
17527 cp_parser_consume_semicolon_at_end_of_statement (parser);
17529 return objc_build_throw_stmt (expr);
17532 /* Parse an Objective-C statement. */
17534 static tree
17535 cp_parser_objc_statement (cp_parser * parser) {
17536 /* Try to figure out what kind of declaration is present. */
17537 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17539 switch (kwd->keyword)
17541 case RID_AT_TRY:
17542 return cp_parser_objc_try_catch_finally_statement (parser);
17543 case RID_AT_SYNCHRONIZED:
17544 return cp_parser_objc_synchronized_statement (parser);
17545 case RID_AT_THROW:
17546 return cp_parser_objc_throw_statement (parser);
17547 default:
17548 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17549 cp_parser_skip_to_end_of_block_or_statement (parser);
17552 return error_mark_node;
17554 /* The parser. */
17556 static GTY (()) cp_parser *the_parser;
17559 /* Special handling for the first token or line in the file. The first
17560 thing in the file might be #pragma GCC pch_preprocess, which loads a
17561 PCH file, which is a GC collection point. So we need to handle this
17562 first pragma without benefit of an existing lexer structure.
17564 Always returns one token to the caller in *FIRST_TOKEN. This is
17565 either the true first token of the file, or the first token after
17566 the initial pragma. */
17568 static void
17569 cp_parser_initial_pragma (cp_token *first_token)
17571 tree name = NULL;
17573 cp_lexer_get_preprocessor_token (NULL, first_token);
17574 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17575 return;
17577 cp_lexer_get_preprocessor_token (NULL, first_token);
17578 if (first_token->type == CPP_STRING)
17580 name = first_token->value;
17582 cp_lexer_get_preprocessor_token (NULL, first_token);
17583 if (first_token->type != CPP_PRAGMA_EOL)
17584 error ("junk at end of %<#pragma GCC pch_preprocess%>");
17586 else
17587 error ("expected string literal");
17589 /* Skip to the end of the pragma. */
17590 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17591 cp_lexer_get_preprocessor_token (NULL, first_token);
17593 /* Read one more token to return to our caller. */
17594 cp_lexer_get_preprocessor_token (NULL, first_token);
17596 /* Now actually load the PCH file. */
17597 if (name)
17598 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17601 /* Normal parsing of a pragma token. Here we can (and must) use the
17602 regular lexer. */
17604 static bool
17605 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17607 cp_token *pragma_tok;
17608 unsigned int id;
17610 pragma_tok = cp_lexer_consume_token (parser->lexer);
17611 gcc_assert (pragma_tok->type == CPP_PRAGMA);
17612 parser->lexer->in_pragma = true;
17614 id = pragma_tok->pragma_kind;
17615 switch (id)
17617 case PRAGMA_GCC_PCH_PREPROCESS:
17618 error ("%<#pragma GCC pch_preprocess%> must be first");
17619 break;
17621 default:
17622 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17623 c_invoke_pragma_handler (id);
17624 break;
17627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17628 return false;
17631 /* The interface the pragma parsers have to the lexer. */
17633 enum cpp_ttype
17634 pragma_lex (tree *value)
17636 cp_token *tok;
17637 enum cpp_ttype ret;
17639 tok = cp_lexer_peek_token (the_parser->lexer);
17641 ret = tok->type;
17642 *value = tok->value;
17644 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17645 ret = CPP_EOF;
17646 else if (ret == CPP_STRING)
17647 *value = cp_parser_string_literal (the_parser, false, false);
17648 else
17650 cp_lexer_consume_token (the_parser->lexer);
17651 if (ret == CPP_KEYWORD)
17652 ret = CPP_NAME;
17655 return ret;
17659 /* External interface. */
17661 /* Parse one entire translation unit. */
17663 void
17664 c_parse_file (void)
17666 bool error_occurred;
17667 static bool already_called = false;
17669 if (already_called)
17671 sorry ("inter-module optimizations not implemented for C++");
17672 return;
17674 already_called = true;
17676 the_parser = cp_parser_new ();
17677 push_deferring_access_checks (flag_access_control
17678 ? dk_no_deferred : dk_no_check);
17679 error_occurred = cp_parser_translation_unit (the_parser);
17680 the_parser = NULL;
17683 /* This variable must be provided by every front end. */
17685 int yydebug;
17687 #include "gt-cp-parser.h"