* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / gcc / cp / parser.c
blob20941cb2b22da66f3fee193f34f0d97b7f23d5a1
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 "cgraph.h"
40 #include "c-common.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A C++ token. */
50 typedef struct cp_token GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The value associated with this token, if any. */
70 tree value;
71 /* The location at which this token was found. */
72 location_t location;
73 } cp_token;
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
80 static const cp_token eof_token =
82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
85 #else
86 {0, 0}
87 #endif
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
93 it is created. */
95 typedef struct cp_lexer GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
101 buffer. */
102 size_t buffer_length;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position GTY ((skip)) last_token;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position GTY ((skip)) next_token;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
115 tokens. */
116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
121 /* True if we should output debugging information. */
122 bool debugging_p;
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
126 bool in_pragma;
127 } cp_lexer;
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
133 a lexer. */
135 typedef struct cp_token_cache GTY(())
137 /* The beginning of the token range. */
138 cp_token * GTY((skip)) first;
140 /* Points immediately after the last token in the range. */
141 cp_token * GTY ((skip)) last;
142 } cp_token_cache;
144 /* Prototypes. */
146 static cp_lexer *cp_lexer_new_main
147 (void);
148 static cp_lexer *cp_lexer_new_from_tokens
149 (cp_token_cache *tokens);
150 static void cp_lexer_destroy
151 (cp_lexer *);
152 static int cp_lexer_saving_tokens
153 (const cp_lexer *);
154 static cp_token_position cp_lexer_token_position
155 (cp_lexer *, bool);
156 static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
161 (cp_lexer *);
162 static cp_token *cp_lexer_peek_nth_token
163 (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
171 (cp_lexer *);
172 static void cp_lexer_purge_token
173 (cp_lexer *);
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
177 (cp_lexer *);
178 static void cp_lexer_commit_tokens
179 (cp_lexer *);
180 static void cp_lexer_rollback_tokens
181 (cp_lexer *);
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
186 (cp_lexer *);
187 static void cp_lexer_start_debugging
188 (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 #else
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
204 static void cp_parser_initial_pragma
205 (cp_token *);
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
235 /* Variables. */
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
243 preprocessor. */
245 static cp_lexer *
246 cp_lexer_new_main (void)
248 cp_token first_token;
249 cp_lexer *lexer;
250 cp_token *pos;
251 size_t alloc;
252 size_t space;
253 cp_token *buffer;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings = true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer = GGC_CNEW (cp_lexer);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
277 buffer = GGC_NEWVEC (cp_token, alloc);
279 /* Put the first token in the buffer. */
280 space = alloc;
281 pos = buffer;
282 *pos = first_token;
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos->type != CPP_EOF)
287 pos++;
288 if (!--space)
290 space = alloc;
291 alloc *= 2;
292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293 pos = buffer + space;
295 cp_lexer_get_preprocessor_token (lexer, pos);
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
307 gcc_assert (lexer->next_token->type != CPP_PURGED);
308 return lexer;
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
314 static cp_lexer *
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
321 /* We do not own the buffer. */
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325 lexer->last_token = last;
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer->debugging_p = false;
333 #endif
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
336 return lexer;
339 /* Frees all resources associated with LEXER. */
341 static void
342 cp_lexer_destroy (cp_lexer *lexer)
344 if (lexer->buffer)
345 ggc_free (lexer->buffer);
346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
347 ggc_free (lexer);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
354 static inline bool
355 cp_lexer_debugging_p (cp_lexer *lexer)
357 return lexer->debugging_p;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
367 return lexer->next_token - previous_p;
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
373 return pos;
376 /* nonzero if we are presently saving tokens. */
378 static inline int
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
385 if we reach EOF. */
387 static void
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389 cp_token *token)
391 static int is_extern_c = 0;
393 /* Get a new token from the preprocessor. */
394 token->type
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
422 else
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else if (token->type == CPP_PRAGMA)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
449 token->value = NULL;
453 /* Update the globals input_location and in_system_header from TOKEN. */
454 static inline void
455 cp_lexer_set_source_position_from_token (cp_token *token)
457 if (token->type != CPP_EOF)
459 input_location = token->location;
460 in_system_header = token->in_system_header;
464 /* Return a pointer to the next token in the token stream, but do not
465 consume it. */
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
470 if (cp_lexer_debugging_p (lexer))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
476 return lexer->next_token;
479 /* Return true if the next token has the indicated TYPE. */
481 static inline bool
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
484 return cp_lexer_peek_token (lexer)->type == type;
487 /* Return true if the next token does not have the indicated TYPE. */
489 static inline bool
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
492 return !cp_lexer_next_token_is (lexer, type);
495 /* Return true if the next token is the indicated KEYWORD. */
497 static inline bool
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
500 return cp_lexer_peek_token (lexer)->keyword == keyword;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
509 static cp_token *
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
512 cp_token *token;
514 /* N is 1-based, not zero-based. */
515 gcc_assert (n > 0);
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
521 --n;
522 token = lexer->next_token;
523 gcc_assert (!n || token != &eof_token);
524 while (n != 0)
526 ++token;
527 if (token == lexer->last_token)
529 token = (cp_token *)&eof_token;
530 break;
533 if (token->type != CPP_PURGED)
534 --n;
537 if (cp_lexer_debugging_p (lexer))
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
543 return token;
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
549 static cp_token *
550 cp_lexer_consume_token (cp_lexer* lexer)
552 cp_token *token = lexer->next_token;
554 gcc_assert (token != &eof_token);
555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
559 lexer->next_token++;
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
563 break;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
579 return token;
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
584 token. */
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
599 tok++;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
603 break;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Begin saving tokens. All tokens consumed after this point will be
634 preserved. */
636 static void
637 cp_lexer_save_tokens (cp_lexer* lexer)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
647 /* Commit to the portion of the token stream most recently saved. */
649 static void
650 cp_lexer_commit_tokens (cp_lexer* lexer)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position, lexer->saved_tokens);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
662 static void
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
676 static void
677 cp_lexer_print_token (FILE * stream, cp_token *token)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
683 #define OP(e, s) #e,
684 #define TK(e, s) #e,
685 TTYPE_TABLE
686 #undef OP
687 #undef TK
688 /* C++ parser token types - see "Manifest constants", above. */
689 "KEYWORD",
690 "TEMPLATE_ID",
691 "NESTED_NAME_SPECIFIER",
692 "PURGED"
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
700 /* For some tokens, print the associated data. */
701 switch (token->type)
703 case CPP_KEYWORD:
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707 break;
708 /* else fall through */
709 case CPP_NAME:
710 fputs (IDENTIFIER_POINTER (token->value), stream);
711 break;
713 case CPP_STRING:
714 case CPP_WSTRING:
715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
716 break;
718 default:
719 break;
723 /* Start emitting debugging information. */
725 static void
726 cp_lexer_start_debugging (cp_lexer* lexer)
728 lexer->debugging_p = true;
731 /* Stop emitting debugging information. */
733 static void
734 cp_lexer_stop_debugging (cp_lexer* lexer)
736 lexer->debugging_p = false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
748 cache->last = last;
749 return cache;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
757 static void
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
763 /* Declarators. */
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator *make_call_declarator
771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773 (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775 (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777 (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779 (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781 (cp_cv_quals, tree, cp_declarator *);
783 cp_declarator *cp_error_declarator;
785 /* The obstack on which declarators and related data structures are
786 allocated. */
787 static struct obstack declarator_obstack;
789 /* Alloc BYTES from the declarator memory pool. */
791 static inline void *
792 alloc_declarator (size_t bytes)
794 return obstack_alloc (&declarator_obstack, bytes);
797 /* Allocate a declarator of the indicated KIND. Clear fields that are
798 common to all declarators. */
800 static cp_declarator *
801 make_declarator (cp_declarator_kind kind)
803 cp_declarator *declarator;
805 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
806 declarator->kind = kind;
807 declarator->attributes = NULL_TREE;
808 declarator->declarator = NULL;
810 return declarator;
813 /* Make a declarator for a generalized identifier. If
814 QUALIFYING_SCOPE is non-NULL, the identifier is
815 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
816 UNQUALIFIED_NAME. SFK indicates the kind of special function this
817 is, if any. */
819 static cp_declarator *
820 make_id_declarator (tree qualifying_scope, tree unqualified_name,
821 special_function_kind sfk)
823 cp_declarator *declarator;
825 /* It is valid to write:
827 class C { void f(); };
828 typedef C D;
829 void D::f();
831 The standard is not clear about whether `typedef const C D' is
832 legal; as of 2002-09-15 the committee is considering that
833 question. EDG 3.0 allows that syntax. Therefore, we do as
834 well. */
835 if (qualifying_scope && TYPE_P (qualifying_scope))
836 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
839 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
840 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842 declarator = make_declarator (cdk_id);
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
845 declarator->u.id.sfk = sfk;
847 return declarator;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
854 cp_declarator *
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 cp_declarator *declarator;
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
864 return declarator;
867 /* Like make_pointer_declarator -- but for references. */
869 cp_declarator *
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 cp_declarator *declarator;
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
879 return declarator;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
885 cp_declarator *
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887 cp_declarator *pointee)
889 cp_declarator *declarator;
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
896 return declarator;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
904 cp_declarator *
905 make_call_declarator (cp_declarator *target,
906 cp_parameter_declarator *parms,
907 cp_cv_quals cv_qualifiers,
908 tree exception_specification)
910 cp_declarator *declarator;
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
918 return declarator;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
924 cp_declarator *
925 make_array_declarator (cp_declarator *element, tree bounds)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
933 return declarator;
936 cp_parameter_declarator *no_parameters;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943 cp_declarator *declarator,
944 tree default_argument)
946 cp_parameter_declarator *parameter;
948 parameter = ((cp_parameter_declarator *)
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
951 if (decl_specifiers)
952 parameter->decl_specifiers = *decl_specifiers;
953 else
954 clear_decl_specs (&parameter->decl_specifiers);
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
959 return parameter;
962 /* The parser. */
964 /* Overview
965 --------
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
977 code processed.
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
988 Methodology
989 -----------
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1008 Future Improvements
1009 -------------------
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1015 case. */
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1022 /* No flags. */
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029 } cp_parser_flags;
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1047 enum cp_parser_prec
1049 PREC_NOT_OPERATOR,
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
1055 PREC_EQUALITY_EXPRESSION,
1056 PREC_RELATIONAL_EXPRESSION,
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1060 PREC_PM_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1086 has occurred. */
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1090 typedef struct cp_parser_expression_stack_entry
1092 tree lhs;
1093 enum tree_code tree_type;
1094 int prec;
1095 } cp_parser_expression_stack_entry;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1099 increasing. */
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117 } cp_parser_context;
1119 /* Prototypes. */
1121 /* Constructors and destructors. */
1123 static cp_parser_context *cp_parser_context_new
1124 (cp_parser_context *);
1126 /* Class variables. */
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1132 cp_parser_new. */
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 for speed. */
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1182 cp_parser_context *context;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list != NULL)
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
1190 memset (context, 0, sizeof (*context));
1192 else
1193 context = GGC_CNEW (cp_parser_context);
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1199 if (next)
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
1204 /* Thread the stack. */
1205 context->next = next;
1208 return context;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1216 cp_lexer *lexer;
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1231 tree scope;
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1238 tree object_scope;
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1281 current context. */
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1286 declaration'. */
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1296 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1297 to IN_OMP_BLOCK if parsing OpenMP structured block and
1298 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1299 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1300 iteration-statement, OpenMP block or loop within that switch. */
1301 #define IN_SWITCH_STMT 1
1302 #define IN_ITERATION_STMT 2
1303 #define IN_OMP_BLOCK 4
1304 #define IN_OMP_FOR 8
1305 unsigned char in_statement;
1307 /* TRUE if we are presently parsing the body of a switch statement. */
1308 bool in_switch_statement_p;
1310 /* TRUE if we are parsing a type-id in an expression context. In
1311 such a situation, both "type (expr)" and "type (type)" are valid
1312 alternatives. */
1313 bool in_type_id_in_expr_p;
1315 /* TRUE if we are currently in a header file where declarations are
1316 implicitly extern "C". */
1317 bool implicit_extern_c;
1319 /* TRUE if strings in expressions should be translated to the execution
1320 character set. */
1321 bool translate_strings_p;
1323 /* If non-NULL, then we are parsing a construct where new type
1324 definitions are not permitted. The string stored here will be
1325 issued as an error message if a type is defined. */
1326 const char *type_definition_forbidden_message;
1328 /* A list of lists. The outer list is a stack, used for member
1329 functions of local classes. At each level there are two sub-list,
1330 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1331 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1332 TREE_VALUE's. The functions are chained in reverse declaration
1333 order.
1335 The TREE_PURPOSE sublist contains those functions with default
1336 arguments that need post processing, and the TREE_VALUE sublist
1337 contains those functions with definitions that need post
1338 processing.
1340 These lists can only be processed once the outermost class being
1341 defined is complete. */
1342 tree unparsed_functions_queues;
1344 /* The number of classes whose definitions are currently in
1345 progress. */
1346 unsigned num_classes_being_defined;
1348 /* The number of template parameter lists that apply directly to the
1349 current declaration. */
1350 unsigned num_template_parameter_lists;
1351 } cp_parser;
1353 /* Prototypes. */
1355 /* Constructors and destructors. */
1357 static cp_parser *cp_parser_new
1358 (void);
1360 /* Routines to parse various constructs.
1362 Those that return `tree' will return the error_mark_node (rather
1363 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1364 Sometimes, they will return an ordinary node if error-recovery was
1365 attempted, even though a parse error occurred. So, to check
1366 whether or not a parse error occurred, you should always use
1367 cp_parser_error_occurred. If the construct is optional (indicated
1368 either by an `_opt' in the name of the function that does the
1369 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1370 the construct is not present. */
1372 /* Lexical conventions [gram.lex] */
1374 static tree cp_parser_identifier
1375 (cp_parser *);
1376 static tree cp_parser_string_literal
1377 (cp_parser *, bool, bool);
1379 /* Basic concepts [gram.basic] */
1381 static bool cp_parser_translation_unit
1382 (cp_parser *);
1384 /* Expressions [gram.expr] */
1386 static tree cp_parser_primary_expression
1387 (cp_parser *, bool, bool, bool, cp_id_kind *);
1388 static tree cp_parser_id_expression
1389 (cp_parser *, bool, bool, bool *, bool);
1390 static tree cp_parser_unqualified_id
1391 (cp_parser *, bool, bool, bool);
1392 static tree cp_parser_nested_name_specifier_opt
1393 (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier
1395 (cp_parser *, bool, bool, bool, bool);
1396 static tree cp_parser_class_or_namespace_name
1397 (cp_parser *, bool, bool, bool, bool, bool);
1398 static tree cp_parser_postfix_expression
1399 (cp_parser *, bool, bool);
1400 static tree cp_parser_postfix_open_square_expression
1401 (cp_parser *, tree, bool);
1402 static tree cp_parser_postfix_dot_deref_expression
1403 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1404 static tree cp_parser_parenthesized_expression_list
1405 (cp_parser *, bool, bool, bool *);
1406 static void cp_parser_pseudo_destructor_name
1407 (cp_parser *, tree *, tree *);
1408 static tree cp_parser_unary_expression
1409 (cp_parser *, bool, bool);
1410 static enum tree_code cp_parser_unary_operator
1411 (cp_token *);
1412 static tree cp_parser_new_expression
1413 (cp_parser *);
1414 static tree cp_parser_new_placement
1415 (cp_parser *);
1416 static tree cp_parser_new_type_id
1417 (cp_parser *, tree *);
1418 static cp_declarator *cp_parser_new_declarator_opt
1419 (cp_parser *);
1420 static cp_declarator *cp_parser_direct_new_declarator
1421 (cp_parser *);
1422 static tree cp_parser_new_initializer
1423 (cp_parser *);
1424 static tree cp_parser_delete_expression
1425 (cp_parser *);
1426 static tree cp_parser_cast_expression
1427 (cp_parser *, bool, bool);
1428 static tree cp_parser_binary_expression
1429 (cp_parser *, bool);
1430 static tree cp_parser_question_colon_clause
1431 (cp_parser *, tree);
1432 static tree cp_parser_assignment_expression
1433 (cp_parser *, bool);
1434 static enum tree_code cp_parser_assignment_operator_opt
1435 (cp_parser *);
1436 static tree cp_parser_expression
1437 (cp_parser *, bool);
1438 static tree cp_parser_constant_expression
1439 (cp_parser *, bool, bool *);
1440 static tree cp_parser_builtin_offsetof
1441 (cp_parser *);
1443 /* Statements [gram.stmt.stmt] */
1445 static void cp_parser_statement
1446 (cp_parser *, tree, bool);
1447 static tree cp_parser_labeled_statement
1448 (cp_parser *, tree, bool);
1449 static tree cp_parser_expression_statement
1450 (cp_parser *, tree);
1451 static tree cp_parser_compound_statement
1452 (cp_parser *, tree, bool);
1453 static void cp_parser_statement_seq_opt
1454 (cp_parser *, tree);
1455 static tree cp_parser_selection_statement
1456 (cp_parser *);
1457 static tree cp_parser_condition
1458 (cp_parser *);
1459 static tree cp_parser_iteration_statement
1460 (cp_parser *);
1461 static void cp_parser_for_init_statement
1462 (cp_parser *);
1463 static tree cp_parser_jump_statement
1464 (cp_parser *);
1465 static void cp_parser_declaration_statement
1466 (cp_parser *);
1468 static tree cp_parser_implicitly_scoped_statement
1469 (cp_parser *);
1470 static void cp_parser_already_scoped_statement
1471 (cp_parser *);
1473 /* Declarations [gram.dcl.dcl] */
1475 static void cp_parser_declaration_seq_opt
1476 (cp_parser *);
1477 static void cp_parser_declaration
1478 (cp_parser *);
1479 static void cp_parser_block_declaration
1480 (cp_parser *, bool);
1481 static void cp_parser_simple_declaration
1482 (cp_parser *, bool);
1483 static void cp_parser_decl_specifier_seq
1484 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1485 static tree cp_parser_storage_class_specifier_opt
1486 (cp_parser *);
1487 static tree cp_parser_function_specifier_opt
1488 (cp_parser *, cp_decl_specifier_seq *);
1489 static tree cp_parser_type_specifier
1490 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1491 int *, bool *);
1492 static tree cp_parser_simple_type_specifier
1493 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1494 static tree cp_parser_type_name
1495 (cp_parser *);
1496 static tree cp_parser_elaborated_type_specifier
1497 (cp_parser *, bool, bool);
1498 static tree cp_parser_enum_specifier
1499 (cp_parser *);
1500 static void cp_parser_enumerator_list
1501 (cp_parser *, tree);
1502 static void cp_parser_enumerator_definition
1503 (cp_parser *, tree);
1504 static tree cp_parser_namespace_name
1505 (cp_parser *);
1506 static void cp_parser_namespace_definition
1507 (cp_parser *);
1508 static void cp_parser_namespace_body
1509 (cp_parser *);
1510 static tree cp_parser_qualified_namespace_specifier
1511 (cp_parser *);
1512 static void cp_parser_namespace_alias_definition
1513 (cp_parser *);
1514 static void cp_parser_using_declaration
1515 (cp_parser *);
1516 static void cp_parser_using_directive
1517 (cp_parser *);
1518 static void cp_parser_asm_definition
1519 (cp_parser *);
1520 static void cp_parser_linkage_specification
1521 (cp_parser *);
1523 /* Declarators [gram.dcl.decl] */
1525 static tree cp_parser_init_declarator
1526 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1527 static cp_declarator *cp_parser_declarator
1528 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1529 static cp_declarator *cp_parser_direct_declarator
1530 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1531 static enum tree_code cp_parser_ptr_operator
1532 (cp_parser *, tree *, cp_cv_quals *);
1533 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1534 (cp_parser *);
1535 static tree cp_parser_declarator_id
1536 (cp_parser *);
1537 static tree cp_parser_type_id
1538 (cp_parser *);
1539 static void cp_parser_type_specifier_seq
1540 (cp_parser *, bool, cp_decl_specifier_seq *);
1541 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1542 (cp_parser *);
1543 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1544 (cp_parser *, bool *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration
1546 (cp_parser *, bool, bool *);
1547 static void cp_parser_function_body
1548 (cp_parser *);
1549 static tree cp_parser_initializer
1550 (cp_parser *, bool *, bool *);
1551 static tree cp_parser_initializer_clause
1552 (cp_parser *, bool *);
1553 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1554 (cp_parser *, bool *);
1556 static bool cp_parser_ctor_initializer_opt_and_function_body
1557 (cp_parser *);
1559 /* Classes [gram.class] */
1561 static tree cp_parser_class_name
1562 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1563 static tree cp_parser_class_specifier
1564 (cp_parser *);
1565 static tree cp_parser_class_head
1566 (cp_parser *, bool *, tree *);
1567 static enum tag_types cp_parser_class_key
1568 (cp_parser *);
1569 static void cp_parser_member_specification_opt
1570 (cp_parser *);
1571 static void cp_parser_member_declaration
1572 (cp_parser *);
1573 static tree cp_parser_pure_specifier
1574 (cp_parser *);
1575 static tree cp_parser_constant_initializer
1576 (cp_parser *);
1578 /* Derived classes [gram.class.derived] */
1580 static tree cp_parser_base_clause
1581 (cp_parser *);
1582 static tree cp_parser_base_specifier
1583 (cp_parser *);
1585 /* Special member functions [gram.special] */
1587 static tree cp_parser_conversion_function_id
1588 (cp_parser *);
1589 static tree cp_parser_conversion_type_id
1590 (cp_parser *);
1591 static cp_declarator *cp_parser_conversion_declarator_opt
1592 (cp_parser *);
1593 static bool cp_parser_ctor_initializer_opt
1594 (cp_parser *);
1595 static void cp_parser_mem_initializer_list
1596 (cp_parser *);
1597 static tree cp_parser_mem_initializer
1598 (cp_parser *);
1599 static tree cp_parser_mem_initializer_id
1600 (cp_parser *);
1602 /* Overloading [gram.over] */
1604 static tree cp_parser_operator_function_id
1605 (cp_parser *);
1606 static tree cp_parser_operator
1607 (cp_parser *);
1609 /* Templates [gram.temp] */
1611 static void cp_parser_template_declaration
1612 (cp_parser *, bool);
1613 static tree cp_parser_template_parameter_list
1614 (cp_parser *);
1615 static tree cp_parser_template_parameter
1616 (cp_parser *, bool *);
1617 static tree cp_parser_type_parameter
1618 (cp_parser *);
1619 static tree cp_parser_template_id
1620 (cp_parser *, bool, bool, bool);
1621 static tree cp_parser_template_name
1622 (cp_parser *, bool, bool, bool, bool *);
1623 static tree cp_parser_template_argument_list
1624 (cp_parser *);
1625 static tree cp_parser_template_argument
1626 (cp_parser *);
1627 static void cp_parser_explicit_instantiation
1628 (cp_parser *);
1629 static void cp_parser_explicit_specialization
1630 (cp_parser *);
1632 /* Exception handling [gram.exception] */
1634 static tree cp_parser_try_block
1635 (cp_parser *);
1636 static bool cp_parser_function_try_block
1637 (cp_parser *);
1638 static void cp_parser_handler_seq
1639 (cp_parser *);
1640 static void cp_parser_handler
1641 (cp_parser *);
1642 static tree cp_parser_exception_declaration
1643 (cp_parser *);
1644 static tree cp_parser_throw_expression
1645 (cp_parser *);
1646 static tree cp_parser_exception_specification_opt
1647 (cp_parser *);
1648 static tree cp_parser_type_id_list
1649 (cp_parser *);
1651 /* GNU Extensions */
1653 static tree cp_parser_asm_specification_opt
1654 (cp_parser *);
1655 static tree cp_parser_asm_operand_list
1656 (cp_parser *);
1657 static tree cp_parser_asm_clobber_list
1658 (cp_parser *);
1659 static tree cp_parser_attributes_opt
1660 (cp_parser *);
1661 static tree cp_parser_attribute_list
1662 (cp_parser *);
1663 static bool cp_parser_extension_opt
1664 (cp_parser *, int *);
1665 static void cp_parser_label_declaration
1666 (cp_parser *);
1668 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1669 static bool cp_parser_pragma
1670 (cp_parser *, enum pragma_context);
1672 /* Objective-C++ Productions */
1674 static tree cp_parser_objc_message_receiver
1675 (cp_parser *);
1676 static tree cp_parser_objc_message_args
1677 (cp_parser *);
1678 static tree cp_parser_objc_message_expression
1679 (cp_parser *);
1680 static tree cp_parser_objc_encode_expression
1681 (cp_parser *);
1682 static tree cp_parser_objc_defs_expression
1683 (cp_parser *);
1684 static tree cp_parser_objc_protocol_expression
1685 (cp_parser *);
1686 static tree cp_parser_objc_selector_expression
1687 (cp_parser *);
1688 static tree cp_parser_objc_expression
1689 (cp_parser *);
1690 static bool cp_parser_objc_selector_p
1691 (enum cpp_ttype);
1692 static tree cp_parser_objc_selector
1693 (cp_parser *);
1694 static tree cp_parser_objc_protocol_refs_opt
1695 (cp_parser *);
1696 static void cp_parser_objc_declaration
1697 (cp_parser *);
1698 static tree cp_parser_objc_statement
1699 (cp_parser *);
1701 /* Utility Routines */
1703 static tree cp_parser_lookup_name
1704 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1705 static tree cp_parser_lookup_name_simple
1706 (cp_parser *, tree);
1707 static tree cp_parser_maybe_treat_template_as_class
1708 (tree, bool);
1709 static bool cp_parser_check_declarator_template_parameters
1710 (cp_parser *, cp_declarator *);
1711 static bool cp_parser_check_template_parameters
1712 (cp_parser *, unsigned);
1713 static tree cp_parser_simple_cast_expression
1714 (cp_parser *);
1715 static tree cp_parser_global_scope_opt
1716 (cp_parser *, bool);
1717 static bool cp_parser_constructor_declarator_p
1718 (cp_parser *, bool);
1719 static tree cp_parser_function_definition_from_specifiers_and_declarator
1720 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1721 static tree cp_parser_function_definition_after_declarator
1722 (cp_parser *, bool);
1723 static void cp_parser_template_declaration_after_export
1724 (cp_parser *, bool);
1725 static tree cp_parser_single_declaration
1726 (cp_parser *, bool, bool *);
1727 static tree cp_parser_functional_cast
1728 (cp_parser *, tree);
1729 static tree cp_parser_save_member_function_body
1730 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1731 static tree cp_parser_enclosed_template_argument_list
1732 (cp_parser *);
1733 static void cp_parser_save_default_args
1734 (cp_parser *, tree);
1735 static void cp_parser_late_parsing_for_member
1736 (cp_parser *, tree);
1737 static void cp_parser_late_parsing_default_args
1738 (cp_parser *, tree);
1739 static tree cp_parser_sizeof_operand
1740 (cp_parser *, enum rid);
1741 static bool cp_parser_declares_only_class_p
1742 (cp_parser *);
1743 static void cp_parser_set_storage_class
1744 (cp_decl_specifier_seq *, cp_storage_class);
1745 static void cp_parser_set_decl_spec_type
1746 (cp_decl_specifier_seq *, tree, bool);
1747 static bool cp_parser_friend_p
1748 (const cp_decl_specifier_seq *);
1749 static cp_token *cp_parser_require
1750 (cp_parser *, enum cpp_ttype, const char *);
1751 static cp_token *cp_parser_require_keyword
1752 (cp_parser *, enum rid, const char *);
1753 static bool cp_parser_token_starts_function_definition_p
1754 (cp_token *);
1755 static bool cp_parser_next_token_starts_class_definition_p
1756 (cp_parser *);
1757 static bool cp_parser_next_token_ends_template_argument_p
1758 (cp_parser *);
1759 static bool cp_parser_nth_token_starts_template_argument_list_p
1760 (cp_parser *, size_t);
1761 static enum tag_types cp_parser_token_is_class_key
1762 (cp_token *);
1763 static void cp_parser_check_class_key
1764 (enum tag_types, tree type);
1765 static void cp_parser_check_access_in_redeclaration
1766 (tree type);
1767 static bool cp_parser_optional_template_keyword
1768 (cp_parser *);
1769 static void cp_parser_pre_parsed_nested_name_specifier
1770 (cp_parser *);
1771 static void cp_parser_cache_group
1772 (cp_parser *, enum cpp_ttype, unsigned);
1773 static void cp_parser_parse_tentatively
1774 (cp_parser *);
1775 static void cp_parser_commit_to_tentative_parse
1776 (cp_parser *);
1777 static void cp_parser_abort_tentative_parse
1778 (cp_parser *);
1779 static bool cp_parser_parse_definitely
1780 (cp_parser *);
1781 static inline bool cp_parser_parsing_tentatively
1782 (cp_parser *);
1783 static bool cp_parser_uncommitted_to_tentative_parse_p
1784 (cp_parser *);
1785 static void cp_parser_error
1786 (cp_parser *, const char *);
1787 static void cp_parser_name_lookup_error
1788 (cp_parser *, tree, tree, const char *);
1789 static bool cp_parser_simulate_error
1790 (cp_parser *);
1791 static void cp_parser_check_type_definition
1792 (cp_parser *);
1793 static void cp_parser_check_for_definition_in_return_type
1794 (cp_declarator *, tree);
1795 static void cp_parser_check_for_invalid_template_id
1796 (cp_parser *, tree);
1797 static bool cp_parser_non_integral_constant_expression
1798 (cp_parser *, const char *);
1799 static void cp_parser_diagnose_invalid_type_name
1800 (cp_parser *, tree, tree);
1801 static bool cp_parser_parse_and_diagnose_invalid_type_name
1802 (cp_parser *);
1803 static int cp_parser_skip_to_closing_parenthesis
1804 (cp_parser *, bool, bool, bool);
1805 static void cp_parser_skip_to_end_of_statement
1806 (cp_parser *);
1807 static void cp_parser_consume_semicolon_at_end_of_statement
1808 (cp_parser *);
1809 static void cp_parser_skip_to_end_of_block_or_statement
1810 (cp_parser *);
1811 static void cp_parser_skip_to_closing_brace
1812 (cp_parser *);
1813 static void cp_parser_skip_until_found
1814 (cp_parser *, enum cpp_ttype, const char *);
1815 static void cp_parser_skip_to_pragma_eol
1816 (cp_parser*, cp_token *);
1817 static bool cp_parser_error_occurred
1818 (cp_parser *);
1819 static bool cp_parser_allow_gnu_extensions_p
1820 (cp_parser *);
1821 static bool cp_parser_is_string_literal
1822 (cp_token *);
1823 static bool cp_parser_is_keyword
1824 (cp_token *, enum rid);
1825 static tree cp_parser_make_typename_type
1826 (cp_parser *, tree, tree);
1828 /* Returns nonzero if we are parsing tentatively. */
1830 static inline bool
1831 cp_parser_parsing_tentatively (cp_parser* parser)
1833 return parser->context->next != NULL;
1836 /* Returns nonzero if TOKEN is a string literal. */
1838 static bool
1839 cp_parser_is_string_literal (cp_token* token)
1841 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1844 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1846 static bool
1847 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1849 return token->keyword == keyword;
1852 /* A minimum or maximum operator has been seen. As these are
1853 deprecated, issue a warning. */
1855 static inline void
1856 cp_parser_warn_min_max (void)
1858 if (warn_deprecated && !in_system_header)
1859 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1862 /* If not parsing tentatively, issue a diagnostic of the form
1863 FILE:LINE: MESSAGE before TOKEN
1864 where TOKEN is the next token in the input stream. MESSAGE
1865 (specified by the caller) is usually of the form "expected
1866 OTHER-TOKEN". */
1868 static void
1869 cp_parser_error (cp_parser* parser, const char* message)
1871 if (!cp_parser_simulate_error (parser))
1873 cp_token *token = cp_lexer_peek_token (parser->lexer);
1874 /* This diagnostic makes more sense if it is tagged to the line
1875 of the token we just peeked at. */
1876 cp_lexer_set_source_position_from_token (token);
1878 if (token->type == CPP_PRAGMA)
1880 error ("%<#pragma%> is not allowed here");
1881 cp_parser_skip_to_pragma_eol (parser, token);
1882 return;
1885 c_parse_error (message,
1886 /* Because c_parser_error does not understand
1887 CPP_KEYWORD, keywords are treated like
1888 identifiers. */
1889 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1890 token->value);
1894 /* Issue an error about name-lookup failing. NAME is the
1895 IDENTIFIER_NODE DECL is the result of
1896 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1897 the thing that we hoped to find. */
1899 static void
1900 cp_parser_name_lookup_error (cp_parser* parser,
1901 tree name,
1902 tree decl,
1903 const char* desired)
1905 /* If name lookup completely failed, tell the user that NAME was not
1906 declared. */
1907 if (decl == error_mark_node)
1909 if (parser->scope && parser->scope != global_namespace)
1910 error ("%<%D::%D%> has not been declared",
1911 parser->scope, name);
1912 else if (parser->scope == global_namespace)
1913 error ("%<::%D%> has not been declared", name);
1914 else if (parser->object_scope
1915 && !CLASS_TYPE_P (parser->object_scope))
1916 error ("request for member %qD in non-class type %qT",
1917 name, parser->object_scope);
1918 else if (parser->object_scope)
1919 error ("%<%T::%D%> has not been declared",
1920 parser->object_scope, name);
1921 else
1922 error ("%qD has not been declared", name);
1924 else if (parser->scope && parser->scope != global_namespace)
1925 error ("%<%D::%D%> %s", parser->scope, name, desired);
1926 else if (parser->scope == global_namespace)
1927 error ("%<::%D%> %s", name, desired);
1928 else
1929 error ("%qD %s", name, desired);
1932 /* If we are parsing tentatively, remember that an error has occurred
1933 during this tentative parse. Returns true if the error was
1934 simulated; false if a message should be issued by the caller. */
1936 static bool
1937 cp_parser_simulate_error (cp_parser* parser)
1939 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1941 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1942 return true;
1944 return false;
1947 /* This function is called when a type is defined. If type
1948 definitions are forbidden at this point, an error message is
1949 issued. */
1951 static void
1952 cp_parser_check_type_definition (cp_parser* parser)
1954 /* If types are forbidden here, issue a message. */
1955 if (parser->type_definition_forbidden_message)
1956 /* Use `%s' to print the string in case there are any escape
1957 characters in the message. */
1958 error ("%s", parser->type_definition_forbidden_message);
1961 /* This function is called when the DECLARATOR is processed. The TYPE
1962 was a type defined in the decl-specifiers. If it is invalid to
1963 define a type in the decl-specifiers for DECLARATOR, an error is
1964 issued. */
1966 static void
1967 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1968 tree type)
1970 /* [dcl.fct] forbids type definitions in return types.
1971 Unfortunately, it's not easy to know whether or not we are
1972 processing a return type until after the fact. */
1973 while (declarator
1974 && (declarator->kind == cdk_pointer
1975 || declarator->kind == cdk_reference
1976 || declarator->kind == cdk_ptrmem))
1977 declarator = declarator->declarator;
1978 if (declarator
1979 && declarator->kind == cdk_function)
1981 error ("new types may not be defined in a return type");
1982 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1983 type);
1987 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1988 "<" in any valid C++ program. If the next token is indeed "<",
1989 issue a message warning the user about what appears to be an
1990 invalid attempt to form a template-id. */
1992 static void
1993 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1994 tree type)
1996 cp_token_position start = 0;
1998 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2000 if (TYPE_P (type))
2001 error ("%qT is not a template", type);
2002 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2003 error ("%qE is not a template", type);
2004 else
2005 error ("invalid template-id");
2006 /* Remember the location of the invalid "<". */
2007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2008 start = cp_lexer_token_position (parser->lexer, true);
2009 /* Consume the "<". */
2010 cp_lexer_consume_token (parser->lexer);
2011 /* Parse the template arguments. */
2012 cp_parser_enclosed_template_argument_list (parser);
2013 /* Permanently remove the invalid template arguments so that
2014 this error message is not issued again. */
2015 if (start)
2016 cp_lexer_purge_tokens_after (parser->lexer, start);
2020 /* If parsing an integral constant-expression, issue an error message
2021 about the fact that THING appeared and return true. Otherwise,
2022 return false. In either case, set
2023 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2025 static bool
2026 cp_parser_non_integral_constant_expression (cp_parser *parser,
2027 const char *thing)
2029 parser->non_integral_constant_expression_p = true;
2030 if (parser->integral_constant_expression_p)
2032 if (!parser->allow_non_integral_constant_expression_p)
2034 error ("%s cannot appear in a constant-expression", thing);
2035 return true;
2038 return false;
2041 /* Emit a diagnostic for an invalid type name. SCOPE is the
2042 qualifying scope (or NULL, if none) for ID. This function commits
2043 to the current active tentative parse, if any. (Otherwise, the
2044 problematic construct might be encountered again later, resulting
2045 in duplicate error messages.) */
2047 static void
2048 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2050 tree decl, old_scope;
2051 /* Try to lookup the identifier. */
2052 old_scope = parser->scope;
2053 parser->scope = scope;
2054 decl = cp_parser_lookup_name_simple (parser, id);
2055 parser->scope = old_scope;
2056 /* If the lookup found a template-name, it means that the user forgot
2057 to specify an argument list. Emit a useful error message. */
2058 if (TREE_CODE (decl) == TEMPLATE_DECL)
2059 error ("invalid use of template-name %qE without an argument list",
2060 decl);
2061 else if (!parser->scope)
2063 /* Issue an error message. */
2064 error ("%qE does not name a type", id);
2065 /* If we're in a template class, it's possible that the user was
2066 referring to a type from a base class. For example:
2068 template <typename T> struct A { typedef T X; };
2069 template <typename T> struct B : public A<T> { X x; };
2071 The user should have said "typename A<T>::X". */
2072 if (processing_template_decl && current_class_type
2073 && TYPE_BINFO (current_class_type))
2075 tree b;
2077 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2079 b = TREE_CHAIN (b))
2081 tree base_type = BINFO_TYPE (b);
2082 if (CLASS_TYPE_P (base_type)
2083 && dependent_type_p (base_type))
2085 tree field;
2086 /* Go from a particular instantiation of the
2087 template (which will have an empty TYPE_FIELDs),
2088 to the main version. */
2089 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2090 for (field = TYPE_FIELDS (base_type);
2091 field;
2092 field = TREE_CHAIN (field))
2093 if (TREE_CODE (field) == TYPE_DECL
2094 && DECL_NAME (field) == id)
2096 inform ("(perhaps %<typename %T::%E%> was intended)",
2097 BINFO_TYPE (b), id);
2098 break;
2100 if (field)
2101 break;
2106 /* Here we diagnose qualified-ids where the scope is actually correct,
2107 but the identifier does not resolve to a valid type name. */
2108 else if (parser->scope != error_mark_node)
2110 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2111 error ("%qE in namespace %qE does not name a type",
2112 id, parser->scope);
2113 else if (TYPE_P (parser->scope))
2114 error ("%qE in class %qT does not name a type", id, parser->scope);
2115 else
2116 gcc_unreachable ();
2118 cp_parser_commit_to_tentative_parse (parser);
2121 /* Check for a common situation where a type-name should be present,
2122 but is not, and issue a sensible error message. Returns true if an
2123 invalid type-name was detected.
2125 The situation handled by this function are variable declarations of the
2126 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2127 Usually, `ID' should name a type, but if we got here it means that it
2128 does not. We try to emit the best possible error message depending on
2129 how exactly the id-expression looks like.
2132 static bool
2133 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2135 tree id;
2137 cp_parser_parse_tentatively (parser);
2138 id = cp_parser_id_expression (parser,
2139 /*template_keyword_p=*/false,
2140 /*check_dependency_p=*/true,
2141 /*template_p=*/NULL,
2142 /*declarator_p=*/true);
2143 /* After the id-expression, there should be a plain identifier,
2144 otherwise this is not a simple variable declaration. Also, if
2145 the scope is dependent, we cannot do much. */
2146 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2147 || (parser->scope && TYPE_P (parser->scope)
2148 && dependent_type_p (parser->scope)))
2150 cp_parser_abort_tentative_parse (parser);
2151 return false;
2153 if (!cp_parser_parse_definitely (parser)
2154 || TREE_CODE (id) != IDENTIFIER_NODE)
2155 return false;
2157 /* Emit a diagnostic for the invalid type. */
2158 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2159 /* Skip to the end of the declaration; there's no point in
2160 trying to process it. */
2161 cp_parser_skip_to_end_of_block_or_statement (parser);
2162 return true;
2165 /* Consume tokens up to, and including, the next non-nested closing `)'.
2166 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2167 are doing error recovery. Returns -1 if OR_COMMA is true and we
2168 found an unnested comma. */
2170 static int
2171 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2172 bool recovering,
2173 bool or_comma,
2174 bool consume_paren)
2176 unsigned paren_depth = 0;
2177 unsigned brace_depth = 0;
2179 if (recovering && !or_comma
2180 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2181 return 0;
2183 while (true)
2185 cp_token * token = cp_lexer_peek_token (parser->lexer);
2187 switch (token->type)
2189 case CPP_EOF:
2190 case CPP_PRAGMA_EOL:
2191 /* If we've run out of tokens, then there is no closing `)'. */
2192 return 0;
2194 case CPP_SEMICOLON:
2195 /* This matches the processing in skip_to_end_of_statement. */
2196 if (!brace_depth)
2197 return 0;
2198 break;
2200 case CPP_OPEN_BRACE:
2201 ++brace_depth;
2202 break;
2203 case CPP_CLOSE_BRACE:
2204 if (!brace_depth--)
2205 return 0;
2206 break;
2208 case CPP_COMMA:
2209 if (recovering && or_comma && !brace_depth && !paren_depth)
2210 return -1;
2211 break;
2213 case CPP_OPEN_PAREN:
2214 if (!brace_depth)
2215 ++paren_depth;
2216 break;
2218 case CPP_CLOSE_PAREN:
2219 if (!brace_depth && !paren_depth--)
2221 if (consume_paren)
2222 cp_lexer_consume_token (parser->lexer);
2223 return 1;
2225 break;
2227 default:
2228 break;
2231 /* Consume the token. */
2232 cp_lexer_consume_token (parser->lexer);
2236 /* Consume tokens until we reach the end of the current statement.
2237 Normally, that will be just before consuming a `;'. However, if a
2238 non-nested `}' comes first, then we stop before consuming that. */
2240 static void
2241 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2243 unsigned nesting_depth = 0;
2245 while (true)
2247 cp_token *token = cp_lexer_peek_token (parser->lexer);
2249 switch (token->type)
2251 case CPP_EOF:
2252 case CPP_PRAGMA_EOL:
2253 /* If we've run out of tokens, stop. */
2254 return;
2256 case CPP_SEMICOLON:
2257 /* If the next token is a `;', we have reached the end of the
2258 statement. */
2259 if (!nesting_depth)
2260 return;
2261 break;
2263 case CPP_CLOSE_BRACE:
2264 /* If this is a non-nested '}', stop before consuming it.
2265 That way, when confronted with something like:
2267 { 3 + }
2269 we stop before consuming the closing '}', even though we
2270 have not yet reached a `;'. */
2271 if (nesting_depth == 0)
2272 return;
2274 /* If it is the closing '}' for a block that we have
2275 scanned, stop -- but only after consuming the token.
2276 That way given:
2278 void f g () { ... }
2279 typedef int I;
2281 we will stop after the body of the erroneously declared
2282 function, but before consuming the following `typedef'
2283 declaration. */
2284 if (--nesting_depth == 0)
2286 cp_lexer_consume_token (parser->lexer);
2287 return;
2290 case CPP_OPEN_BRACE:
2291 ++nesting_depth;
2292 break;
2294 default:
2295 break;
2298 /* Consume the token. */
2299 cp_lexer_consume_token (parser->lexer);
2303 /* This function is called at the end of a statement or declaration.
2304 If the next token is a semicolon, it is consumed; otherwise, error
2305 recovery is attempted. */
2307 static void
2308 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2310 /* Look for the trailing `;'. */
2311 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2313 /* If there is additional (erroneous) input, skip to the end of
2314 the statement. */
2315 cp_parser_skip_to_end_of_statement (parser);
2316 /* If the next token is now a `;', consume it. */
2317 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2318 cp_lexer_consume_token (parser->lexer);
2322 /* Skip tokens until we have consumed an entire block, or until we
2323 have consumed a non-nested `;'. */
2325 static void
2326 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2328 int nesting_depth = 0;
2330 while (nesting_depth >= 0)
2332 cp_token *token = cp_lexer_peek_token (parser->lexer);
2334 switch (token->type)
2336 case CPP_EOF:
2337 case CPP_PRAGMA_EOL:
2338 /* If we've run out of tokens, stop. */
2339 return;
2341 case CPP_SEMICOLON:
2342 /* Stop if this is an unnested ';'. */
2343 if (!nesting_depth)
2344 nesting_depth = -1;
2345 break;
2347 case CPP_CLOSE_BRACE:
2348 /* Stop if this is an unnested '}', or closes the outermost
2349 nesting level. */
2350 nesting_depth--;
2351 if (!nesting_depth)
2352 nesting_depth = -1;
2353 break;
2355 case CPP_OPEN_BRACE:
2356 /* Nest. */
2357 nesting_depth++;
2358 break;
2360 default:
2361 break;
2364 /* Consume the token. */
2365 cp_lexer_consume_token (parser->lexer);
2369 /* Skip tokens until a non-nested closing curly brace is the next
2370 token. */
2372 static void
2373 cp_parser_skip_to_closing_brace (cp_parser *parser)
2375 unsigned nesting_depth = 0;
2377 while (true)
2379 cp_token *token = cp_lexer_peek_token (parser->lexer);
2381 switch (token->type)
2383 case CPP_EOF:
2384 case CPP_PRAGMA_EOL:
2385 /* If we've run out of tokens, stop. */
2386 return;
2388 case CPP_CLOSE_BRACE:
2389 /* If the next token is a non-nested `}', then we have reached
2390 the end of the current block. */
2391 if (nesting_depth-- == 0)
2392 return;
2393 break;
2395 case CPP_OPEN_BRACE:
2396 /* If it the next token is a `{', then we are entering a new
2397 block. Consume the entire block. */
2398 ++nesting_depth;
2399 break;
2401 default:
2402 break;
2405 /* Consume the token. */
2406 cp_lexer_consume_token (parser->lexer);
2410 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2411 parameter is the PRAGMA token, allowing us to purge the entire pragma
2412 sequence. */
2414 static void
2415 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2417 cp_token *token;
2419 parser->lexer->in_pragma = false;
2422 token = cp_lexer_consume_token (parser->lexer);
2423 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2425 /* Ensure that the pragma is not parsed again. */
2426 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2429 /* Require pragma end of line, resyncing with it as necessary. The
2430 arguments are as for cp_parser_skip_to_pragma_eol. */
2432 static void
2433 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2435 parser->lexer->in_pragma = false;
2436 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2437 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2440 /* This is a simple wrapper around make_typename_type. When the id is
2441 an unresolved identifier node, we can provide a superior diagnostic
2442 using cp_parser_diagnose_invalid_type_name. */
2444 static tree
2445 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2447 tree result;
2448 if (TREE_CODE (id) == IDENTIFIER_NODE)
2450 result = make_typename_type (scope, id, typename_type,
2451 /*complain=*/tf_none);
2452 if (result == error_mark_node)
2453 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2454 return result;
2456 return make_typename_type (scope, id, typename_type, tf_error);
2460 /* Create a new C++ parser. */
2462 static cp_parser *
2463 cp_parser_new (void)
2465 cp_parser *parser;
2466 cp_lexer *lexer;
2467 unsigned i;
2469 /* cp_lexer_new_main is called before calling ggc_alloc because
2470 cp_lexer_new_main might load a PCH file. */
2471 lexer = cp_lexer_new_main ();
2473 /* Initialize the binops_by_token so that we can get the tree
2474 directly from the token. */
2475 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2476 binops_by_token[binops[i].token_type] = binops[i];
2478 parser = GGC_CNEW (cp_parser);
2479 parser->lexer = lexer;
2480 parser->context = cp_parser_context_new (NULL);
2482 /* For now, we always accept GNU extensions. */
2483 parser->allow_gnu_extensions_p = 1;
2485 /* The `>' token is a greater-than operator, not the end of a
2486 template-id. */
2487 parser->greater_than_is_operator_p = true;
2489 parser->default_arg_ok_p = true;
2491 /* We are not parsing a constant-expression. */
2492 parser->integral_constant_expression_p = false;
2493 parser->allow_non_integral_constant_expression_p = false;
2494 parser->non_integral_constant_expression_p = false;
2496 /* Local variable names are not forbidden. */
2497 parser->local_variables_forbidden_p = false;
2499 /* We are not processing an `extern "C"' declaration. */
2500 parser->in_unbraced_linkage_specification_p = false;
2502 /* We are not processing a declarator. */
2503 parser->in_declarator_p = false;
2505 /* We are not processing a template-argument-list. */
2506 parser->in_template_argument_list_p = false;
2508 /* We are not in an iteration statement. */
2509 parser->in_statement = 0;
2511 /* We are not in a switch statement. */
2512 parser->in_switch_statement_p = false;
2514 /* We are not parsing a type-id inside an expression. */
2515 parser->in_type_id_in_expr_p = false;
2517 /* Declarations aren't implicitly extern "C". */
2518 parser->implicit_extern_c = false;
2520 /* String literals should be translated to the execution character set. */
2521 parser->translate_strings_p = true;
2523 /* The unparsed function queue is empty. */
2524 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2526 /* There are no classes being defined. */
2527 parser->num_classes_being_defined = 0;
2529 /* No template parameters apply. */
2530 parser->num_template_parameter_lists = 0;
2532 return parser;
2535 /* Create a cp_lexer structure which will emit the tokens in CACHE
2536 and push it onto the parser's lexer stack. This is used for delayed
2537 parsing of in-class method bodies and default arguments, and should
2538 not be confused with tentative parsing. */
2539 static void
2540 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2542 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2543 lexer->next = parser->lexer;
2544 parser->lexer = lexer;
2546 /* Move the current source position to that of the first token in the
2547 new lexer. */
2548 cp_lexer_set_source_position_from_token (lexer->next_token);
2551 /* Pop the top lexer off the parser stack. This is never used for the
2552 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2553 static void
2554 cp_parser_pop_lexer (cp_parser *parser)
2556 cp_lexer *lexer = parser->lexer;
2557 parser->lexer = lexer->next;
2558 cp_lexer_destroy (lexer);
2560 /* Put the current source position back where it was before this
2561 lexer was pushed. */
2562 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2565 /* Lexical conventions [gram.lex] */
2567 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2568 identifier. */
2570 static tree
2571 cp_parser_identifier (cp_parser* parser)
2573 cp_token *token;
2575 /* Look for the identifier. */
2576 token = cp_parser_require (parser, CPP_NAME, "identifier");
2577 /* Return the value. */
2578 return token ? token->value : error_mark_node;
2581 /* Parse a sequence of adjacent string constants. Returns a
2582 TREE_STRING representing the combined, nul-terminated string
2583 constant. If TRANSLATE is true, translate the string to the
2584 execution character set. If WIDE_OK is true, a wide string is
2585 invalid here.
2587 C++98 [lex.string] says that if a narrow string literal token is
2588 adjacent to a wide string literal token, the behavior is undefined.
2589 However, C99 6.4.5p4 says that this results in a wide string literal.
2590 We follow C99 here, for consistency with the C front end.
2592 This code is largely lifted from lex_string() in c-lex.c.
2594 FUTURE: ObjC++ will need to handle @-strings here. */
2595 static tree
2596 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2598 tree value;
2599 bool wide = false;
2600 size_t count;
2601 struct obstack str_ob;
2602 cpp_string str, istr, *strs;
2603 cp_token *tok;
2605 tok = cp_lexer_peek_token (parser->lexer);
2606 if (!cp_parser_is_string_literal (tok))
2608 cp_parser_error (parser, "expected string-literal");
2609 return error_mark_node;
2612 /* Try to avoid the overhead of creating and destroying an obstack
2613 for the common case of just one string. */
2614 if (!cp_parser_is_string_literal
2615 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2617 cp_lexer_consume_token (parser->lexer);
2619 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2620 str.len = TREE_STRING_LENGTH (tok->value);
2621 count = 1;
2622 if (tok->type == CPP_WSTRING)
2623 wide = true;
2625 strs = &str;
2627 else
2629 gcc_obstack_init (&str_ob);
2630 count = 0;
2634 cp_lexer_consume_token (parser->lexer);
2635 count++;
2636 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2637 str.len = TREE_STRING_LENGTH (tok->value);
2638 if (tok->type == CPP_WSTRING)
2639 wide = true;
2641 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2643 tok = cp_lexer_peek_token (parser->lexer);
2645 while (cp_parser_is_string_literal (tok));
2647 strs = (cpp_string *) obstack_finish (&str_ob);
2650 if (wide && !wide_ok)
2652 cp_parser_error (parser, "a wide string is invalid in this context");
2653 wide = false;
2656 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2657 (parse_in, strs, count, &istr, wide))
2659 value = build_string (istr.len, (char *)istr.text);
2660 free ((void *)istr.text);
2662 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2663 value = fix_string_type (value);
2665 else
2666 /* cpp_interpret_string has issued an error. */
2667 value = error_mark_node;
2669 if (count > 1)
2670 obstack_free (&str_ob, 0);
2672 return value;
2676 /* Basic concepts [gram.basic] */
2678 /* Parse a translation-unit.
2680 translation-unit:
2681 declaration-seq [opt]
2683 Returns TRUE if all went well. */
2685 static bool
2686 cp_parser_translation_unit (cp_parser* parser)
2688 /* The address of the first non-permanent object on the declarator
2689 obstack. */
2690 static void *declarator_obstack_base;
2692 bool success;
2694 /* Create the declarator obstack, if necessary. */
2695 if (!cp_error_declarator)
2697 gcc_obstack_init (&declarator_obstack);
2698 /* Create the error declarator. */
2699 cp_error_declarator = make_declarator (cdk_error);
2700 /* Create the empty parameter list. */
2701 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2702 /* Remember where the base of the declarator obstack lies. */
2703 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2706 cp_parser_declaration_seq_opt (parser);
2708 /* If there are no tokens left then all went well. */
2709 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2711 /* Get rid of the token array; we don't need it any more. */
2712 cp_lexer_destroy (parser->lexer);
2713 parser->lexer = NULL;
2715 /* This file might have been a context that's implicitly extern
2716 "C". If so, pop the lang context. (Only relevant for PCH.) */
2717 if (parser->implicit_extern_c)
2719 pop_lang_context ();
2720 parser->implicit_extern_c = false;
2723 /* Finish up. */
2724 finish_translation_unit ();
2726 success = true;
2728 else
2730 cp_parser_error (parser, "expected declaration");
2731 success = false;
2734 /* Make sure the declarator obstack was fully cleaned up. */
2735 gcc_assert (obstack_next_free (&declarator_obstack)
2736 == declarator_obstack_base);
2738 /* All went well. */
2739 return success;
2742 /* Expressions [gram.expr] */
2744 /* Parse a primary-expression.
2746 primary-expression:
2747 literal
2748 this
2749 ( expression )
2750 id-expression
2752 GNU Extensions:
2754 primary-expression:
2755 ( compound-statement )
2756 __builtin_va_arg ( assignment-expression , type-id )
2757 __builtin_offsetof ( type-id , offsetof-expression )
2759 Objective-C++ Extension:
2761 primary-expression:
2762 objc-expression
2764 literal:
2765 __null
2767 ADDRESS_P is true iff this expression was immediately preceded by
2768 "&" and therefore might denote a pointer-to-member. CAST_P is true
2769 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2770 true iff this expression is a template argument.
2772 Returns a representation of the expression. Upon return, *IDK
2773 indicates what kind of id-expression (if any) was present. */
2775 static tree
2776 cp_parser_primary_expression (cp_parser *parser,
2777 bool address_p,
2778 bool cast_p,
2779 bool template_arg_p,
2780 cp_id_kind *idk)
2782 cp_token *token;
2784 /* Assume the primary expression is not an id-expression. */
2785 *idk = CP_ID_KIND_NONE;
2787 /* Peek at the next token. */
2788 token = cp_lexer_peek_token (parser->lexer);
2789 switch (token->type)
2791 /* literal:
2792 integer-literal
2793 character-literal
2794 floating-literal
2795 string-literal
2796 boolean-literal */
2797 case CPP_CHAR:
2798 case CPP_WCHAR:
2799 case CPP_NUMBER:
2800 token = cp_lexer_consume_token (parser->lexer);
2801 /* Floating-point literals are only allowed in an integral
2802 constant expression if they are cast to an integral or
2803 enumeration type. */
2804 if (TREE_CODE (token->value) == REAL_CST
2805 && parser->integral_constant_expression_p
2806 && pedantic)
2808 /* CAST_P will be set even in invalid code like "int(2.7 +
2809 ...)". Therefore, we have to check that the next token
2810 is sure to end the cast. */
2811 if (cast_p)
2813 cp_token *next_token;
2815 next_token = cp_lexer_peek_token (parser->lexer);
2816 if (/* The comma at the end of an
2817 enumerator-definition. */
2818 next_token->type != CPP_COMMA
2819 /* The curly brace at the end of an enum-specifier. */
2820 && next_token->type != CPP_CLOSE_BRACE
2821 /* The end of a statement. */
2822 && next_token->type != CPP_SEMICOLON
2823 /* The end of the cast-expression. */
2824 && next_token->type != CPP_CLOSE_PAREN
2825 /* The end of an array bound. */
2826 && next_token->type != CPP_CLOSE_SQUARE
2827 /* The closing ">" in a template-argument-list. */
2828 && (next_token->type != CPP_GREATER
2829 || parser->greater_than_is_operator_p))
2830 cast_p = false;
2833 /* If we are within a cast, then the constraint that the
2834 cast is to an integral or enumeration type will be
2835 checked at that point. If we are not within a cast, then
2836 this code is invalid. */
2837 if (!cast_p)
2838 cp_parser_non_integral_constant_expression
2839 (parser, "floating-point literal");
2841 return token->value;
2843 case CPP_STRING:
2844 case CPP_WSTRING:
2845 /* ??? Should wide strings be allowed when parser->translate_strings_p
2846 is false (i.e. in attributes)? If not, we can kill the third
2847 argument to cp_parser_string_literal. */
2848 return cp_parser_string_literal (parser,
2849 parser->translate_strings_p,
2850 true);
2852 case CPP_OPEN_PAREN:
2854 tree expr;
2855 bool saved_greater_than_is_operator_p;
2857 /* Consume the `('. */
2858 cp_lexer_consume_token (parser->lexer);
2859 /* Within a parenthesized expression, a `>' token is always
2860 the greater-than operator. */
2861 saved_greater_than_is_operator_p
2862 = parser->greater_than_is_operator_p;
2863 parser->greater_than_is_operator_p = true;
2864 /* If we see `( { ' then we are looking at the beginning of
2865 a GNU statement-expression. */
2866 if (cp_parser_allow_gnu_extensions_p (parser)
2867 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2869 /* Statement-expressions are not allowed by the standard. */
2870 if (pedantic)
2871 pedwarn ("ISO C++ forbids braced-groups within expressions");
2873 /* And they're not allowed outside of a function-body; you
2874 cannot, for example, write:
2876 int i = ({ int j = 3; j + 1; });
2878 at class or namespace scope. */
2879 if (!at_function_scope_p ())
2880 error ("statement-expressions are allowed only inside functions");
2881 /* Start the statement-expression. */
2882 expr = begin_stmt_expr ();
2883 /* Parse the compound-statement. */
2884 cp_parser_compound_statement (parser, expr, false);
2885 /* Finish up. */
2886 expr = finish_stmt_expr (expr, false);
2888 else
2890 /* Parse the parenthesized expression. */
2891 expr = cp_parser_expression (parser, cast_p);
2892 /* Let the front end know that this expression was
2893 enclosed in parentheses. This matters in case, for
2894 example, the expression is of the form `A::B', since
2895 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2896 not. */
2897 finish_parenthesized_expr (expr);
2899 /* The `>' token might be the end of a template-id or
2900 template-parameter-list now. */
2901 parser->greater_than_is_operator_p
2902 = saved_greater_than_is_operator_p;
2903 /* Consume the `)'. */
2904 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2905 cp_parser_skip_to_end_of_statement (parser);
2907 return expr;
2910 case CPP_KEYWORD:
2911 switch (token->keyword)
2913 /* These two are the boolean literals. */
2914 case RID_TRUE:
2915 cp_lexer_consume_token (parser->lexer);
2916 return boolean_true_node;
2917 case RID_FALSE:
2918 cp_lexer_consume_token (parser->lexer);
2919 return boolean_false_node;
2921 /* The `__null' literal. */
2922 case RID_NULL:
2923 cp_lexer_consume_token (parser->lexer);
2924 return null_node;
2926 /* Recognize the `this' keyword. */
2927 case RID_THIS:
2928 cp_lexer_consume_token (parser->lexer);
2929 if (parser->local_variables_forbidden_p)
2931 error ("%<this%> may not be used in this context");
2932 return error_mark_node;
2934 /* Pointers cannot appear in constant-expressions. */
2935 if (cp_parser_non_integral_constant_expression (parser,
2936 "`this'"))
2937 return error_mark_node;
2938 return finish_this_expr ();
2940 /* The `operator' keyword can be the beginning of an
2941 id-expression. */
2942 case RID_OPERATOR:
2943 goto id_expression;
2945 case RID_FUNCTION_NAME:
2946 case RID_PRETTY_FUNCTION_NAME:
2947 case RID_C99_FUNCTION_NAME:
2948 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2949 __func__ are the names of variables -- but they are
2950 treated specially. Therefore, they are handled here,
2951 rather than relying on the generic id-expression logic
2952 below. Grammatically, these names are id-expressions.
2954 Consume the token. */
2955 token = cp_lexer_consume_token (parser->lexer);
2956 /* Look up the name. */
2957 return finish_fname (token->value);
2959 case RID_VA_ARG:
2961 tree expression;
2962 tree type;
2964 /* The `__builtin_va_arg' construct is used to handle
2965 `va_arg'. Consume the `__builtin_va_arg' token. */
2966 cp_lexer_consume_token (parser->lexer);
2967 /* Look for the opening `('. */
2968 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2969 /* Now, parse the assignment-expression. */
2970 expression = cp_parser_assignment_expression (parser,
2971 /*cast_p=*/false);
2972 /* Look for the `,'. */
2973 cp_parser_require (parser, CPP_COMMA, "`,'");
2974 /* Parse the type-id. */
2975 type = cp_parser_type_id (parser);
2976 /* Look for the closing `)'. */
2977 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2978 /* Using `va_arg' in a constant-expression is not
2979 allowed. */
2980 if (cp_parser_non_integral_constant_expression (parser,
2981 "`va_arg'"))
2982 return error_mark_node;
2983 return build_x_va_arg (expression, type);
2986 case RID_OFFSETOF:
2987 return cp_parser_builtin_offsetof (parser);
2989 /* Objective-C++ expressions. */
2990 case RID_AT_ENCODE:
2991 case RID_AT_PROTOCOL:
2992 case RID_AT_SELECTOR:
2993 return cp_parser_objc_expression (parser);
2995 default:
2996 cp_parser_error (parser, "expected primary-expression");
2997 return error_mark_node;
3000 /* An id-expression can start with either an identifier, a
3001 `::' as the beginning of a qualified-id, or the "operator"
3002 keyword. */
3003 case CPP_NAME:
3004 case CPP_SCOPE:
3005 case CPP_TEMPLATE_ID:
3006 case CPP_NESTED_NAME_SPECIFIER:
3008 tree id_expression;
3009 tree decl;
3010 const char *error_msg;
3011 bool template_p;
3012 bool done;
3014 id_expression:
3015 /* Parse the id-expression. */
3016 id_expression
3017 = cp_parser_id_expression (parser,
3018 /*template_keyword_p=*/false,
3019 /*check_dependency_p=*/true,
3020 &template_p,
3021 /*declarator_p=*/false);
3022 if (id_expression == error_mark_node)
3023 return error_mark_node;
3024 token = cp_lexer_peek_token (parser->lexer);
3025 done = (token->type != CPP_OPEN_SQUARE
3026 && token->type != CPP_OPEN_PAREN
3027 && token->type != CPP_DOT
3028 && token->type != CPP_DEREF
3029 && token->type != CPP_PLUS_PLUS
3030 && token->type != CPP_MINUS_MINUS);
3031 /* If we have a template-id, then no further lookup is
3032 required. If the template-id was for a template-class, we
3033 will sometimes have a TYPE_DECL at this point. */
3034 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3035 || TREE_CODE (id_expression) == TYPE_DECL)
3036 decl = id_expression;
3037 /* Look up the name. */
3038 else
3040 tree ambiguous_decls;
3042 decl = cp_parser_lookup_name (parser, id_expression,
3043 none_type,
3044 template_p,
3045 /*is_namespace=*/false,
3046 /*check_dependency=*/true,
3047 &ambiguous_decls);
3048 /* If the lookup was ambiguous, an error will already have
3049 been issued. */
3050 if (ambiguous_decls)
3051 return error_mark_node;
3053 /* In Objective-C++, an instance variable (ivar) may be preferred
3054 to whatever cp_parser_lookup_name() found. */
3055 decl = objc_lookup_ivar (decl, id_expression);
3057 /* If name lookup gives us a SCOPE_REF, then the
3058 qualifying scope was dependent. */
3059 if (TREE_CODE (decl) == SCOPE_REF)
3060 return decl;
3061 /* Check to see if DECL is a local variable in a context
3062 where that is forbidden. */
3063 if (parser->local_variables_forbidden_p
3064 && local_variable_p (decl))
3066 /* It might be that we only found DECL because we are
3067 trying to be generous with pre-ISO scoping rules.
3068 For example, consider:
3070 int i;
3071 void g() {
3072 for (int i = 0; i < 10; ++i) {}
3073 extern void f(int j = i);
3076 Here, name look up will originally find the out
3077 of scope `i'. We need to issue a warning message,
3078 but then use the global `i'. */
3079 decl = check_for_out_of_scope_variable (decl);
3080 if (local_variable_p (decl))
3082 error ("local variable %qD may not appear in this context",
3083 decl);
3084 return error_mark_node;
3089 decl = (finish_id_expression
3090 (id_expression, decl, parser->scope,
3091 idk,
3092 parser->integral_constant_expression_p,
3093 parser->allow_non_integral_constant_expression_p,
3094 &parser->non_integral_constant_expression_p,
3095 template_p, done, address_p,
3096 template_arg_p,
3097 &error_msg));
3098 if (error_msg)
3099 cp_parser_error (parser, error_msg);
3100 return decl;
3103 /* Anything else is an error. */
3104 default:
3105 /* ...unless we have an Objective-C++ message or string literal, that is. */
3106 if (c_dialect_objc ()
3107 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3108 return cp_parser_objc_expression (parser);
3110 cp_parser_error (parser, "expected primary-expression");
3111 return error_mark_node;
3115 /* Parse an id-expression.
3117 id-expression:
3118 unqualified-id
3119 qualified-id
3121 qualified-id:
3122 :: [opt] nested-name-specifier template [opt] unqualified-id
3123 :: identifier
3124 :: operator-function-id
3125 :: template-id
3127 Return a representation of the unqualified portion of the
3128 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3129 a `::' or nested-name-specifier.
3131 Often, if the id-expression was a qualified-id, the caller will
3132 want to make a SCOPE_REF to represent the qualified-id. This
3133 function does not do this in order to avoid wastefully creating
3134 SCOPE_REFs when they are not required.
3136 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3137 `template' keyword.
3139 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3140 uninstantiated templates.
3142 If *TEMPLATE_P is non-NULL, it is set to true iff the
3143 `template' keyword is used to explicitly indicate that the entity
3144 named is a template.
3146 If DECLARATOR_P is true, the id-expression is appearing as part of
3147 a declarator, rather than as part of an expression. */
3149 static tree
3150 cp_parser_id_expression (cp_parser *parser,
3151 bool template_keyword_p,
3152 bool check_dependency_p,
3153 bool *template_p,
3154 bool declarator_p)
3156 bool global_scope_p;
3157 bool nested_name_specifier_p;
3159 /* Assume the `template' keyword was not used. */
3160 if (template_p)
3161 *template_p = template_keyword_p;
3163 /* Look for the optional `::' operator. */
3164 global_scope_p
3165 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3166 != NULL_TREE);
3167 /* Look for the optional nested-name-specifier. */
3168 nested_name_specifier_p
3169 = (cp_parser_nested_name_specifier_opt (parser,
3170 /*typename_keyword_p=*/false,
3171 check_dependency_p,
3172 /*type_p=*/false,
3173 declarator_p)
3174 != NULL_TREE);
3175 /* If there is a nested-name-specifier, then we are looking at
3176 the first qualified-id production. */
3177 if (nested_name_specifier_p)
3179 tree saved_scope;
3180 tree saved_object_scope;
3181 tree saved_qualifying_scope;
3182 tree unqualified_id;
3183 bool is_template;
3185 /* See if the next token is the `template' keyword. */
3186 if (!template_p)
3187 template_p = &is_template;
3188 *template_p = cp_parser_optional_template_keyword (parser);
3189 /* Name lookup we do during the processing of the
3190 unqualified-id might obliterate SCOPE. */
3191 saved_scope = parser->scope;
3192 saved_object_scope = parser->object_scope;
3193 saved_qualifying_scope = parser->qualifying_scope;
3194 /* Process the final unqualified-id. */
3195 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3196 check_dependency_p,
3197 declarator_p);
3198 /* Restore the SAVED_SCOPE for our caller. */
3199 parser->scope = saved_scope;
3200 parser->object_scope = saved_object_scope;
3201 parser->qualifying_scope = saved_qualifying_scope;
3203 return unqualified_id;
3205 /* Otherwise, if we are in global scope, then we are looking at one
3206 of the other qualified-id productions. */
3207 else if (global_scope_p)
3209 cp_token *token;
3210 tree id;
3212 /* Peek at the next token. */
3213 token = cp_lexer_peek_token (parser->lexer);
3215 /* If it's an identifier, and the next token is not a "<", then
3216 we can avoid the template-id case. This is an optimization
3217 for this common case. */
3218 if (token->type == CPP_NAME
3219 && !cp_parser_nth_token_starts_template_argument_list_p
3220 (parser, 2))
3221 return cp_parser_identifier (parser);
3223 cp_parser_parse_tentatively (parser);
3224 /* Try a template-id. */
3225 id = cp_parser_template_id (parser,
3226 /*template_keyword_p=*/false,
3227 /*check_dependency_p=*/true,
3228 declarator_p);
3229 /* If that worked, we're done. */
3230 if (cp_parser_parse_definitely (parser))
3231 return id;
3233 /* Peek at the next token. (Changes in the token buffer may
3234 have invalidated the pointer obtained above.) */
3235 token = cp_lexer_peek_token (parser->lexer);
3237 switch (token->type)
3239 case CPP_NAME:
3240 return cp_parser_identifier (parser);
3242 case CPP_KEYWORD:
3243 if (token->keyword == RID_OPERATOR)
3244 return cp_parser_operator_function_id (parser);
3245 /* Fall through. */
3247 default:
3248 cp_parser_error (parser, "expected id-expression");
3249 return error_mark_node;
3252 else
3253 return cp_parser_unqualified_id (parser, template_keyword_p,
3254 /*check_dependency_p=*/true,
3255 declarator_p);
3258 /* Parse an unqualified-id.
3260 unqualified-id:
3261 identifier
3262 operator-function-id
3263 conversion-function-id
3264 ~ class-name
3265 template-id
3267 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3268 keyword, in a construct like `A::template ...'.
3270 Returns a representation of unqualified-id. For the `identifier'
3271 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3272 production a BIT_NOT_EXPR is returned; the operand of the
3273 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3274 other productions, see the documentation accompanying the
3275 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3276 names are looked up in uninstantiated templates. If DECLARATOR_P
3277 is true, the unqualified-id is appearing as part of a declarator,
3278 rather than as part of an expression. */
3280 static tree
3281 cp_parser_unqualified_id (cp_parser* parser,
3282 bool template_keyword_p,
3283 bool check_dependency_p,
3284 bool declarator_p)
3286 cp_token *token;
3288 /* Peek at the next token. */
3289 token = cp_lexer_peek_token (parser->lexer);
3291 switch (token->type)
3293 case CPP_NAME:
3295 tree id;
3297 /* We don't know yet whether or not this will be a
3298 template-id. */
3299 cp_parser_parse_tentatively (parser);
3300 /* Try a template-id. */
3301 id = cp_parser_template_id (parser, template_keyword_p,
3302 check_dependency_p,
3303 declarator_p);
3304 /* If it worked, we're done. */
3305 if (cp_parser_parse_definitely (parser))
3306 return id;
3307 /* Otherwise, it's an ordinary identifier. */
3308 return cp_parser_identifier (parser);
3311 case CPP_TEMPLATE_ID:
3312 return cp_parser_template_id (parser, template_keyword_p,
3313 check_dependency_p,
3314 declarator_p);
3316 case CPP_COMPL:
3318 tree type_decl;
3319 tree qualifying_scope;
3320 tree object_scope;
3321 tree scope;
3322 bool done;
3324 /* Consume the `~' token. */
3325 cp_lexer_consume_token (parser->lexer);
3326 /* Parse the class-name. The standard, as written, seems to
3327 say that:
3329 template <typename T> struct S { ~S (); };
3330 template <typename T> S<T>::~S() {}
3332 is invalid, since `~' must be followed by a class-name, but
3333 `S<T>' is dependent, and so not known to be a class.
3334 That's not right; we need to look in uninstantiated
3335 templates. A further complication arises from:
3337 template <typename T> void f(T t) {
3338 t.T::~T();
3341 Here, it is not possible to look up `T' in the scope of `T'
3342 itself. We must look in both the current scope, and the
3343 scope of the containing complete expression.
3345 Yet another issue is:
3347 struct S {
3348 int S;
3349 ~S();
3352 S::~S() {}
3354 The standard does not seem to say that the `S' in `~S'
3355 should refer to the type `S' and not the data member
3356 `S::S'. */
3358 /* DR 244 says that we look up the name after the "~" in the
3359 same scope as we looked up the qualifying name. That idea
3360 isn't fully worked out; it's more complicated than that. */
3361 scope = parser->scope;
3362 object_scope = parser->object_scope;
3363 qualifying_scope = parser->qualifying_scope;
3365 /* If the name is of the form "X::~X" it's OK. */
3366 if (scope && TYPE_P (scope)
3367 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3368 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3369 == CPP_OPEN_PAREN)
3370 && (cp_lexer_peek_token (parser->lexer)->value
3371 == TYPE_IDENTIFIER (scope)))
3373 cp_lexer_consume_token (parser->lexer);
3374 return build_nt (BIT_NOT_EXPR, scope);
3377 /* If there was an explicit qualification (S::~T), first look
3378 in the scope given by the qualification (i.e., S). */
3379 done = false;
3380 type_decl = NULL_TREE;
3381 if (scope)
3383 cp_parser_parse_tentatively (parser);
3384 type_decl = cp_parser_class_name (parser,
3385 /*typename_keyword_p=*/false,
3386 /*template_keyword_p=*/false,
3387 none_type,
3388 /*check_dependency=*/false,
3389 /*class_head_p=*/false,
3390 declarator_p);
3391 if (cp_parser_parse_definitely (parser))
3392 done = true;
3394 /* In "N::S::~S", look in "N" as well. */
3395 if (!done && scope && qualifying_scope)
3397 cp_parser_parse_tentatively (parser);
3398 parser->scope = qualifying_scope;
3399 parser->object_scope = NULL_TREE;
3400 parser->qualifying_scope = NULL_TREE;
3401 type_decl
3402 = cp_parser_class_name (parser,
3403 /*typename_keyword_p=*/false,
3404 /*template_keyword_p=*/false,
3405 none_type,
3406 /*check_dependency=*/false,
3407 /*class_head_p=*/false,
3408 declarator_p);
3409 if (cp_parser_parse_definitely (parser))
3410 done = true;
3412 /* In "p->S::~T", look in the scope given by "*p" as well. */
3413 else if (!done && object_scope)
3415 cp_parser_parse_tentatively (parser);
3416 parser->scope = object_scope;
3417 parser->object_scope = NULL_TREE;
3418 parser->qualifying_scope = NULL_TREE;
3419 type_decl
3420 = cp_parser_class_name (parser,
3421 /*typename_keyword_p=*/false,
3422 /*template_keyword_p=*/false,
3423 none_type,
3424 /*check_dependency=*/false,
3425 /*class_head_p=*/false,
3426 declarator_p);
3427 if (cp_parser_parse_definitely (parser))
3428 done = true;
3430 /* Look in the surrounding context. */
3431 if (!done)
3433 parser->scope = NULL_TREE;
3434 parser->object_scope = NULL_TREE;
3435 parser->qualifying_scope = NULL_TREE;
3436 type_decl
3437 = cp_parser_class_name (parser,
3438 /*typename_keyword_p=*/false,
3439 /*template_keyword_p=*/false,
3440 none_type,
3441 /*check_dependency=*/false,
3442 /*class_head_p=*/false,
3443 declarator_p);
3445 /* If an error occurred, assume that the name of the
3446 destructor is the same as the name of the qualifying
3447 class. That allows us to keep parsing after running
3448 into ill-formed destructor names. */
3449 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3450 return build_nt (BIT_NOT_EXPR, scope);
3451 else if (type_decl == error_mark_node)
3452 return error_mark_node;
3454 /* Check that destructor name and scope match. */
3455 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3457 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3458 error ("declaration of %<~%T%> as member of %qT",
3459 type_decl, scope);
3460 return error_mark_node;
3463 /* [class.dtor]
3465 A typedef-name that names a class shall not be used as the
3466 identifier in the declarator for a destructor declaration. */
3467 if (declarator_p
3468 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3469 && !DECL_SELF_REFERENCE_P (type_decl)
3470 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3471 error ("typedef-name %qD used as destructor declarator",
3472 type_decl);
3474 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3477 case CPP_KEYWORD:
3478 if (token->keyword == RID_OPERATOR)
3480 tree id;
3482 /* This could be a template-id, so we try that first. */
3483 cp_parser_parse_tentatively (parser);
3484 /* Try a template-id. */
3485 id = cp_parser_template_id (parser, template_keyword_p,
3486 /*check_dependency_p=*/true,
3487 declarator_p);
3488 /* If that worked, we're done. */
3489 if (cp_parser_parse_definitely (parser))
3490 return id;
3491 /* We still don't know whether we're looking at an
3492 operator-function-id or a conversion-function-id. */
3493 cp_parser_parse_tentatively (parser);
3494 /* Try an operator-function-id. */
3495 id = cp_parser_operator_function_id (parser);
3496 /* If that didn't work, try a conversion-function-id. */
3497 if (!cp_parser_parse_definitely (parser))
3498 id = cp_parser_conversion_function_id (parser);
3500 return id;
3502 /* Fall through. */
3504 default:
3505 cp_parser_error (parser, "expected unqualified-id");
3506 return error_mark_node;
3510 /* Parse an (optional) nested-name-specifier.
3512 nested-name-specifier:
3513 class-or-namespace-name :: nested-name-specifier [opt]
3514 class-or-namespace-name :: template nested-name-specifier [opt]
3516 PARSER->SCOPE should be set appropriately before this function is
3517 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3518 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3519 in name lookups.
3521 Sets PARSER->SCOPE to the class (TYPE) or namespace
3522 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3523 it unchanged if there is no nested-name-specifier. Returns the new
3524 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3526 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3527 part of a declaration and/or decl-specifier. */
3529 static tree
3530 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3531 bool typename_keyword_p,
3532 bool check_dependency_p,
3533 bool type_p,
3534 bool is_declaration)
3536 bool success = false;
3537 cp_token_position start = 0;
3538 cp_token *token;
3540 /* If the next token corresponds to a nested name specifier, there
3541 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3542 false, it may have been true before, in which case something
3543 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3544 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3545 CHECK_DEPENDENCY_P is false, we have to fall through into the
3546 main loop. */
3547 if (check_dependency_p
3548 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3550 cp_parser_pre_parsed_nested_name_specifier (parser);
3551 return parser->scope;
3554 /* Remember where the nested-name-specifier starts. */
3555 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3557 start = cp_lexer_token_position (parser->lexer, false);
3558 push_deferring_access_checks (dk_deferred);
3561 while (true)
3563 tree new_scope;
3564 tree old_scope;
3565 tree saved_qualifying_scope;
3566 bool template_keyword_p;
3568 /* Spot cases that cannot be the beginning of a
3569 nested-name-specifier. */
3570 token = cp_lexer_peek_token (parser->lexer);
3572 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3573 the already parsed nested-name-specifier. */
3574 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3576 /* Grab the nested-name-specifier and continue the loop. */
3577 cp_parser_pre_parsed_nested_name_specifier (parser);
3578 success = true;
3579 continue;
3582 /* Spot cases that cannot be the beginning of a
3583 nested-name-specifier. On the second and subsequent times
3584 through the loop, we look for the `template' keyword. */
3585 if (success && token->keyword == RID_TEMPLATE)
3587 /* A template-id can start a nested-name-specifier. */
3588 else if (token->type == CPP_TEMPLATE_ID)
3590 else
3592 /* If the next token is not an identifier, then it is
3593 definitely not a class-or-namespace-name. */
3594 if (token->type != CPP_NAME)
3595 break;
3596 /* If the following token is neither a `<' (to begin a
3597 template-id), nor a `::', then we are not looking at a
3598 nested-name-specifier. */
3599 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3600 if (token->type != CPP_SCOPE
3601 && !cp_parser_nth_token_starts_template_argument_list_p
3602 (parser, 2))
3603 break;
3606 /* The nested-name-specifier is optional, so we parse
3607 tentatively. */
3608 cp_parser_parse_tentatively (parser);
3610 /* Look for the optional `template' keyword, if this isn't the
3611 first time through the loop. */
3612 if (success)
3613 template_keyword_p = cp_parser_optional_template_keyword (parser);
3614 else
3615 template_keyword_p = false;
3617 /* Save the old scope since the name lookup we are about to do
3618 might destroy it. */
3619 old_scope = parser->scope;
3620 saved_qualifying_scope = parser->qualifying_scope;
3621 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3622 look up names in "X<T>::I" in order to determine that "Y" is
3623 a template. So, if we have a typename at this point, we make
3624 an effort to look through it. */
3625 if (is_declaration
3626 && !typename_keyword_p
3627 && parser->scope
3628 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3629 parser->scope = resolve_typename_type (parser->scope,
3630 /*only_current_p=*/false);
3631 /* Parse the qualifying entity. */
3632 new_scope
3633 = cp_parser_class_or_namespace_name (parser,
3634 typename_keyword_p,
3635 template_keyword_p,
3636 check_dependency_p,
3637 type_p,
3638 is_declaration);
3639 /* Look for the `::' token. */
3640 cp_parser_require (parser, CPP_SCOPE, "`::'");
3642 /* If we found what we wanted, we keep going; otherwise, we're
3643 done. */
3644 if (!cp_parser_parse_definitely (parser))
3646 bool error_p = false;
3648 /* Restore the OLD_SCOPE since it was valid before the
3649 failed attempt at finding the last
3650 class-or-namespace-name. */
3651 parser->scope = old_scope;
3652 parser->qualifying_scope = saved_qualifying_scope;
3653 /* If the next token is an identifier, and the one after
3654 that is a `::', then any valid interpretation would have
3655 found a class-or-namespace-name. */
3656 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3657 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3658 == CPP_SCOPE)
3659 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3660 != CPP_COMPL))
3662 token = cp_lexer_consume_token (parser->lexer);
3663 if (!error_p)
3665 if (!token->ambiguous_p)
3667 tree decl;
3668 tree ambiguous_decls;
3670 decl = cp_parser_lookup_name (parser, token->value,
3671 none_type,
3672 /*is_template=*/false,
3673 /*is_namespace=*/false,
3674 /*check_dependency=*/true,
3675 &ambiguous_decls);
3676 if (TREE_CODE (decl) == TEMPLATE_DECL)
3677 error ("%qD used without template parameters", decl);
3678 else if (ambiguous_decls)
3680 error ("reference to %qD is ambiguous",
3681 token->value);
3682 print_candidates (ambiguous_decls);
3683 decl = error_mark_node;
3685 else
3686 cp_parser_name_lookup_error
3687 (parser, token->value, decl,
3688 "is not a class or namespace");
3690 parser->scope = error_mark_node;
3691 error_p = true;
3692 /* Treat this as a successful nested-name-specifier
3693 due to:
3695 [basic.lookup.qual]
3697 If the name found is not a class-name (clause
3698 _class_) or namespace-name (_namespace.def_), the
3699 program is ill-formed. */
3700 success = true;
3702 cp_lexer_consume_token (parser->lexer);
3704 break;
3706 /* We've found one valid nested-name-specifier. */
3707 success = true;
3708 /* Name lookup always gives us a DECL. */
3709 if (TREE_CODE (new_scope) == TYPE_DECL)
3710 new_scope = TREE_TYPE (new_scope);
3711 /* Uses of "template" must be followed by actual templates. */
3712 if (template_keyword_p
3713 && !(CLASS_TYPE_P (new_scope)
3714 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3715 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3716 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3717 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3718 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3719 == TEMPLATE_ID_EXPR)))
3720 pedwarn (TYPE_P (new_scope)
3721 ? "%qT is not a template"
3722 : "%qD is not a template",
3723 new_scope);
3724 /* If it is a class scope, try to complete it; we are about to
3725 be looking up names inside the class. */
3726 if (TYPE_P (new_scope)
3727 /* Since checking types for dependency can be expensive,
3728 avoid doing it if the type is already complete. */
3729 && !COMPLETE_TYPE_P (new_scope)
3730 /* Do not try to complete dependent types. */
3731 && !dependent_type_p (new_scope))
3732 new_scope = complete_type (new_scope);
3733 /* Make sure we look in the right scope the next time through
3734 the loop. */
3735 parser->scope = new_scope;
3738 /* If parsing tentatively, replace the sequence of tokens that makes
3739 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3740 token. That way, should we re-parse the token stream, we will
3741 not have to repeat the effort required to do the parse, nor will
3742 we issue duplicate error messages. */
3743 if (success && start)
3745 cp_token *token;
3746 tree access_checks;
3748 token = cp_lexer_token_at (parser->lexer, start);
3749 /* Reset the contents of the START token. */
3750 token->type = CPP_NESTED_NAME_SPECIFIER;
3751 /* Retrieve any deferred checks. Do not pop this access checks yet
3752 so the memory will not be reclaimed during token replacing below. */
3753 access_checks = get_deferred_access_checks ();
3754 token->value = build_tree_list (copy_list (access_checks),
3755 parser->scope);
3756 TREE_TYPE (token->value) = parser->qualifying_scope;
3757 token->keyword = RID_MAX;
3759 /* Purge all subsequent tokens. */
3760 cp_lexer_purge_tokens_after (parser->lexer, start);
3763 if (start)
3764 pop_to_parent_deferring_access_checks ();
3766 return success ? parser->scope : NULL_TREE;
3769 /* Parse a nested-name-specifier. See
3770 cp_parser_nested_name_specifier_opt for details. This function
3771 behaves identically, except that it will an issue an error if no
3772 nested-name-specifier is present. */
3774 static tree
3775 cp_parser_nested_name_specifier (cp_parser *parser,
3776 bool typename_keyword_p,
3777 bool check_dependency_p,
3778 bool type_p,
3779 bool is_declaration)
3781 tree scope;
3783 /* Look for the nested-name-specifier. */
3784 scope = cp_parser_nested_name_specifier_opt (parser,
3785 typename_keyword_p,
3786 check_dependency_p,
3787 type_p,
3788 is_declaration);
3789 /* If it was not present, issue an error message. */
3790 if (!scope)
3792 cp_parser_error (parser, "expected nested-name-specifier");
3793 parser->scope = NULL_TREE;
3796 return scope;
3799 /* Parse a class-or-namespace-name.
3801 class-or-namespace-name:
3802 class-name
3803 namespace-name
3805 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3806 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3807 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3808 TYPE_P is TRUE iff the next name should be taken as a class-name,
3809 even the same name is declared to be another entity in the same
3810 scope.
3812 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3813 specified by the class-or-namespace-name. If neither is found the
3814 ERROR_MARK_NODE is returned. */
3816 static tree
3817 cp_parser_class_or_namespace_name (cp_parser *parser,
3818 bool typename_keyword_p,
3819 bool template_keyword_p,
3820 bool check_dependency_p,
3821 bool type_p,
3822 bool is_declaration)
3824 tree saved_scope;
3825 tree saved_qualifying_scope;
3826 tree saved_object_scope;
3827 tree scope;
3828 bool only_class_p;
3830 /* Before we try to parse the class-name, we must save away the
3831 current PARSER->SCOPE since cp_parser_class_name will destroy
3832 it. */
3833 saved_scope = parser->scope;
3834 saved_qualifying_scope = parser->qualifying_scope;
3835 saved_object_scope = parser->object_scope;
3836 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3837 there is no need to look for a namespace-name. */
3838 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3839 if (!only_class_p)
3840 cp_parser_parse_tentatively (parser);
3841 scope = cp_parser_class_name (parser,
3842 typename_keyword_p,
3843 template_keyword_p,
3844 type_p ? class_type : none_type,
3845 check_dependency_p,
3846 /*class_head_p=*/false,
3847 is_declaration);
3848 /* If that didn't work, try for a namespace-name. */
3849 if (!only_class_p && !cp_parser_parse_definitely (parser))
3851 /* Restore the saved scope. */
3852 parser->scope = saved_scope;
3853 parser->qualifying_scope = saved_qualifying_scope;
3854 parser->object_scope = saved_object_scope;
3855 /* If we are not looking at an identifier followed by the scope
3856 resolution operator, then this is not part of a
3857 nested-name-specifier. (Note that this function is only used
3858 to parse the components of a nested-name-specifier.) */
3859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3860 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3861 return error_mark_node;
3862 scope = cp_parser_namespace_name (parser);
3865 return scope;
3868 /* Parse a postfix-expression.
3870 postfix-expression:
3871 primary-expression
3872 postfix-expression [ expression ]
3873 postfix-expression ( expression-list [opt] )
3874 simple-type-specifier ( expression-list [opt] )
3875 typename :: [opt] nested-name-specifier identifier
3876 ( expression-list [opt] )
3877 typename :: [opt] nested-name-specifier template [opt] template-id
3878 ( expression-list [opt] )
3879 postfix-expression . template [opt] id-expression
3880 postfix-expression -> template [opt] id-expression
3881 postfix-expression . pseudo-destructor-name
3882 postfix-expression -> pseudo-destructor-name
3883 postfix-expression ++
3884 postfix-expression --
3885 dynamic_cast < type-id > ( expression )
3886 static_cast < type-id > ( expression )
3887 reinterpret_cast < type-id > ( expression )
3888 const_cast < type-id > ( expression )
3889 typeid ( expression )
3890 typeid ( type-id )
3892 GNU Extension:
3894 postfix-expression:
3895 ( type-id ) { initializer-list , [opt] }
3897 This extension is a GNU version of the C99 compound-literal
3898 construct. (The C99 grammar uses `type-name' instead of `type-id',
3899 but they are essentially the same concept.)
3901 If ADDRESS_P is true, the postfix expression is the operand of the
3902 `&' operator. CAST_P is true if this expression is the target of a
3903 cast.
3905 Returns a representation of the expression. */
3907 static tree
3908 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3910 cp_token *token;
3911 enum rid keyword;
3912 cp_id_kind idk = CP_ID_KIND_NONE;
3913 tree postfix_expression = NULL_TREE;
3915 /* Peek at the next token. */
3916 token = cp_lexer_peek_token (parser->lexer);
3917 /* Some of the productions are determined by keywords. */
3918 keyword = token->keyword;
3919 switch (keyword)
3921 case RID_DYNCAST:
3922 case RID_STATCAST:
3923 case RID_REINTCAST:
3924 case RID_CONSTCAST:
3926 tree type;
3927 tree expression;
3928 const char *saved_message;
3930 /* All of these can be handled in the same way from the point
3931 of view of parsing. Begin by consuming the token
3932 identifying the cast. */
3933 cp_lexer_consume_token (parser->lexer);
3935 /* New types cannot be defined in the cast. */
3936 saved_message = parser->type_definition_forbidden_message;
3937 parser->type_definition_forbidden_message
3938 = "types may not be defined in casts";
3940 /* Look for the opening `<'. */
3941 cp_parser_require (parser, CPP_LESS, "`<'");
3942 /* Parse the type to which we are casting. */
3943 type = cp_parser_type_id (parser);
3944 /* Look for the closing `>'. */
3945 cp_parser_require (parser, CPP_GREATER, "`>'");
3946 /* Restore the old message. */
3947 parser->type_definition_forbidden_message = saved_message;
3949 /* And the expression which is being cast. */
3950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3951 expression = cp_parser_expression (parser, /*cast_p=*/true);
3952 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3954 /* Only type conversions to integral or enumeration types
3955 can be used in constant-expressions. */
3956 if (parser->integral_constant_expression_p
3957 && !dependent_type_p (type)
3958 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3959 && (cp_parser_non_integral_constant_expression
3960 (parser,
3961 "a cast to a type other than an integral or "
3962 "enumeration type")))
3963 return error_mark_node;
3965 switch (keyword)
3967 case RID_DYNCAST:
3968 postfix_expression
3969 = build_dynamic_cast (type, expression);
3970 break;
3971 case RID_STATCAST:
3972 postfix_expression
3973 = build_static_cast (type, expression);
3974 break;
3975 case RID_REINTCAST:
3976 postfix_expression
3977 = build_reinterpret_cast (type, expression);
3978 break;
3979 case RID_CONSTCAST:
3980 postfix_expression
3981 = build_const_cast (type, expression);
3982 break;
3983 default:
3984 gcc_unreachable ();
3987 break;
3989 case RID_TYPEID:
3991 tree type;
3992 const char *saved_message;
3993 bool saved_in_type_id_in_expr_p;
3995 /* Consume the `typeid' token. */
3996 cp_lexer_consume_token (parser->lexer);
3997 /* Look for the `(' token. */
3998 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3999 /* Types cannot be defined in a `typeid' expression. */
4000 saved_message = parser->type_definition_forbidden_message;
4001 parser->type_definition_forbidden_message
4002 = "types may not be defined in a `typeid\' expression";
4003 /* We can't be sure yet whether we're looking at a type-id or an
4004 expression. */
4005 cp_parser_parse_tentatively (parser);
4006 /* Try a type-id first. */
4007 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4008 parser->in_type_id_in_expr_p = true;
4009 type = cp_parser_type_id (parser);
4010 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4011 /* Look for the `)' token. Otherwise, we can't be sure that
4012 we're not looking at an expression: consider `typeid (int
4013 (3))', for example. */
4014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4015 /* If all went well, simply lookup the type-id. */
4016 if (cp_parser_parse_definitely (parser))
4017 postfix_expression = get_typeid (type);
4018 /* Otherwise, fall back to the expression variant. */
4019 else
4021 tree expression;
4023 /* Look for an expression. */
4024 expression = cp_parser_expression (parser, /*cast_p=*/false);
4025 /* Compute its typeid. */
4026 postfix_expression = build_typeid (expression);
4027 /* Look for the `)' token. */
4028 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4030 /* `typeid' may not appear in an integral constant expression. */
4031 if (cp_parser_non_integral_constant_expression(parser,
4032 "`typeid' operator"))
4033 return error_mark_node;
4034 /* Restore the saved message. */
4035 parser->type_definition_forbidden_message = saved_message;
4037 break;
4039 case RID_TYPENAME:
4041 tree type;
4042 /* The syntax permitted here is the same permitted for an
4043 elaborated-type-specifier. */
4044 type = cp_parser_elaborated_type_specifier (parser,
4045 /*is_friend=*/false,
4046 /*is_declaration=*/false);
4047 postfix_expression = cp_parser_functional_cast (parser, type);
4049 break;
4051 default:
4053 tree type;
4055 /* If the next thing is a simple-type-specifier, we may be
4056 looking at a functional cast. We could also be looking at
4057 an id-expression. So, we try the functional cast, and if
4058 that doesn't work we fall back to the primary-expression. */
4059 cp_parser_parse_tentatively (parser);
4060 /* Look for the simple-type-specifier. */
4061 type = cp_parser_simple_type_specifier (parser,
4062 /*decl_specs=*/NULL,
4063 CP_PARSER_FLAGS_NONE);
4064 /* Parse the cast itself. */
4065 if (!cp_parser_error_occurred (parser))
4066 postfix_expression
4067 = cp_parser_functional_cast (parser, type);
4068 /* If that worked, we're done. */
4069 if (cp_parser_parse_definitely (parser))
4070 break;
4072 /* If the functional-cast didn't work out, try a
4073 compound-literal. */
4074 if (cp_parser_allow_gnu_extensions_p (parser)
4075 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4077 VEC(constructor_elt,gc) *initializer_list = NULL;
4078 bool saved_in_type_id_in_expr_p;
4080 cp_parser_parse_tentatively (parser);
4081 /* Consume the `('. */
4082 cp_lexer_consume_token (parser->lexer);
4083 /* Parse the type. */
4084 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4085 parser->in_type_id_in_expr_p = true;
4086 type = cp_parser_type_id (parser);
4087 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4088 /* Look for the `)'. */
4089 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4090 /* Look for the `{'. */
4091 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4092 /* If things aren't going well, there's no need to
4093 keep going. */
4094 if (!cp_parser_error_occurred (parser))
4096 bool non_constant_p;
4097 /* Parse the initializer-list. */
4098 initializer_list
4099 = cp_parser_initializer_list (parser, &non_constant_p);
4100 /* Allow a trailing `,'. */
4101 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4102 cp_lexer_consume_token (parser->lexer);
4103 /* Look for the final `}'. */
4104 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4106 /* If that worked, we're definitely looking at a
4107 compound-literal expression. */
4108 if (cp_parser_parse_definitely (parser))
4110 /* Warn the user that a compound literal is not
4111 allowed in standard C++. */
4112 if (pedantic)
4113 pedwarn ("ISO C++ forbids compound-literals");
4114 /* Form the representation of the compound-literal. */
4115 postfix_expression
4116 = finish_compound_literal (type, initializer_list);
4117 break;
4121 /* It must be a primary-expression. */
4122 postfix_expression
4123 = cp_parser_primary_expression (parser, address_p, cast_p,
4124 /*template_arg_p=*/false,
4125 &idk);
4127 break;
4130 /* Keep looping until the postfix-expression is complete. */
4131 while (true)
4133 if (idk == CP_ID_KIND_UNQUALIFIED
4134 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4135 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4136 /* It is not a Koenig lookup function call. */
4137 postfix_expression
4138 = unqualified_name_lookup_error (postfix_expression);
4140 /* Peek at the next token. */
4141 token = cp_lexer_peek_token (parser->lexer);
4143 switch (token->type)
4145 case CPP_OPEN_SQUARE:
4146 postfix_expression
4147 = cp_parser_postfix_open_square_expression (parser,
4148 postfix_expression,
4149 false);
4150 idk = CP_ID_KIND_NONE;
4151 break;
4153 case CPP_OPEN_PAREN:
4154 /* postfix-expression ( expression-list [opt] ) */
4156 bool koenig_p;
4157 bool is_builtin_constant_p;
4158 bool saved_integral_constant_expression_p = false;
4159 bool saved_non_integral_constant_expression_p = false;
4160 tree args;
4162 is_builtin_constant_p
4163 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4164 if (is_builtin_constant_p)
4166 /* The whole point of __builtin_constant_p is to allow
4167 non-constant expressions to appear as arguments. */
4168 saved_integral_constant_expression_p
4169 = parser->integral_constant_expression_p;
4170 saved_non_integral_constant_expression_p
4171 = parser->non_integral_constant_expression_p;
4172 parser->integral_constant_expression_p = false;
4174 args = (cp_parser_parenthesized_expression_list
4175 (parser, /*is_attribute_list=*/false,
4176 /*cast_p=*/false,
4177 /*non_constant_p=*/NULL));
4178 if (is_builtin_constant_p)
4180 parser->integral_constant_expression_p
4181 = saved_integral_constant_expression_p;
4182 parser->non_integral_constant_expression_p
4183 = saved_non_integral_constant_expression_p;
4186 if (args == error_mark_node)
4188 postfix_expression = error_mark_node;
4189 break;
4192 /* Function calls are not permitted in
4193 constant-expressions. */
4194 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4195 && cp_parser_non_integral_constant_expression (parser,
4196 "a function call"))
4198 postfix_expression = error_mark_node;
4199 break;
4202 koenig_p = false;
4203 if (idk == CP_ID_KIND_UNQUALIFIED)
4205 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4207 if (args)
4209 koenig_p = true;
4210 postfix_expression
4211 = perform_koenig_lookup (postfix_expression, args);
4213 else
4214 postfix_expression
4215 = unqualified_fn_lookup_error (postfix_expression);
4217 /* We do not perform argument-dependent lookup if
4218 normal lookup finds a non-function, in accordance
4219 with the expected resolution of DR 218. */
4220 else if (args && is_overloaded_fn (postfix_expression))
4222 tree fn = get_first_fn (postfix_expression);
4224 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4225 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4227 /* Only do argument dependent lookup if regular
4228 lookup does not find a set of member functions.
4229 [basic.lookup.koenig]/2a */
4230 if (!DECL_FUNCTION_MEMBER_P (fn))
4232 koenig_p = true;
4233 postfix_expression
4234 = perform_koenig_lookup (postfix_expression, args);
4239 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4241 tree instance = TREE_OPERAND (postfix_expression, 0);
4242 tree fn = TREE_OPERAND (postfix_expression, 1);
4244 if (processing_template_decl
4245 && (type_dependent_expression_p (instance)
4246 || (!BASELINK_P (fn)
4247 && TREE_CODE (fn) != FIELD_DECL)
4248 || type_dependent_expression_p (fn)
4249 || any_type_dependent_arguments_p (args)))
4251 postfix_expression
4252 = build_min_nt (CALL_EXPR, postfix_expression,
4253 args, NULL_TREE);
4254 break;
4257 if (BASELINK_P (fn))
4258 postfix_expression
4259 = (build_new_method_call
4260 (instance, fn, args, NULL_TREE,
4261 (idk == CP_ID_KIND_QUALIFIED
4262 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4263 else
4264 postfix_expression
4265 = finish_call_expr (postfix_expression, args,
4266 /*disallow_virtual=*/false,
4267 /*koenig_p=*/false);
4269 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4270 || TREE_CODE (postfix_expression) == MEMBER_REF
4271 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4272 postfix_expression = (build_offset_ref_call_from_tree
4273 (postfix_expression, args));
4274 else if (idk == CP_ID_KIND_QUALIFIED)
4275 /* A call to a static class member, or a namespace-scope
4276 function. */
4277 postfix_expression
4278 = finish_call_expr (postfix_expression, args,
4279 /*disallow_virtual=*/true,
4280 koenig_p);
4281 else
4282 /* All other function calls. */
4283 postfix_expression
4284 = finish_call_expr (postfix_expression, args,
4285 /*disallow_virtual=*/false,
4286 koenig_p);
4288 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4289 idk = CP_ID_KIND_NONE;
4291 break;
4293 case CPP_DOT:
4294 case CPP_DEREF:
4295 /* postfix-expression . template [opt] id-expression
4296 postfix-expression . pseudo-destructor-name
4297 postfix-expression -> template [opt] id-expression
4298 postfix-expression -> pseudo-destructor-name */
4300 /* Consume the `.' or `->' operator. */
4301 cp_lexer_consume_token (parser->lexer);
4303 postfix_expression
4304 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4305 postfix_expression,
4306 false, &idk);
4307 break;
4309 case CPP_PLUS_PLUS:
4310 /* postfix-expression ++ */
4311 /* Consume the `++' token. */
4312 cp_lexer_consume_token (parser->lexer);
4313 /* Generate a representation for the complete expression. */
4314 postfix_expression
4315 = finish_increment_expr (postfix_expression,
4316 POSTINCREMENT_EXPR);
4317 /* Increments may not appear in constant-expressions. */
4318 if (cp_parser_non_integral_constant_expression (parser,
4319 "an increment"))
4320 postfix_expression = error_mark_node;
4321 idk = CP_ID_KIND_NONE;
4322 break;
4324 case CPP_MINUS_MINUS:
4325 /* postfix-expression -- */
4326 /* Consume the `--' token. */
4327 cp_lexer_consume_token (parser->lexer);
4328 /* Generate a representation for the complete expression. */
4329 postfix_expression
4330 = finish_increment_expr (postfix_expression,
4331 POSTDECREMENT_EXPR);
4332 /* Decrements may not appear in constant-expressions. */
4333 if (cp_parser_non_integral_constant_expression (parser,
4334 "a decrement"))
4335 postfix_expression = error_mark_node;
4336 idk = CP_ID_KIND_NONE;
4337 break;
4339 default:
4340 return postfix_expression;
4344 /* We should never get here. */
4345 gcc_unreachable ();
4346 return error_mark_node;
4349 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4350 by cp_parser_builtin_offsetof. We're looking for
4352 postfix-expression [ expression ]
4354 FOR_OFFSETOF is set if we're being called in that context, which
4355 changes how we deal with integer constant expressions. */
4357 static tree
4358 cp_parser_postfix_open_square_expression (cp_parser *parser,
4359 tree postfix_expression,
4360 bool for_offsetof)
4362 tree index;
4364 /* Consume the `[' token. */
4365 cp_lexer_consume_token (parser->lexer);
4367 /* Parse the index expression. */
4368 /* ??? For offsetof, there is a question of what to allow here. If
4369 offsetof is not being used in an integral constant expression context,
4370 then we *could* get the right answer by computing the value at runtime.
4371 If we are in an integral constant expression context, then we might
4372 could accept any constant expression; hard to say without analysis.
4373 Rather than open the barn door too wide right away, allow only integer
4374 constant expressions here. */
4375 if (for_offsetof)
4376 index = cp_parser_constant_expression (parser, false, NULL);
4377 else
4378 index = cp_parser_expression (parser, /*cast_p=*/false);
4380 /* Look for the closing `]'. */
4381 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4383 /* Build the ARRAY_REF. */
4384 postfix_expression = grok_array_decl (postfix_expression, index);
4386 /* When not doing offsetof, array references are not permitted in
4387 constant-expressions. */
4388 if (!for_offsetof
4389 && (cp_parser_non_integral_constant_expression
4390 (parser, "an array reference")))
4391 postfix_expression = error_mark_node;
4393 return postfix_expression;
4396 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4397 by cp_parser_builtin_offsetof. We're looking for
4399 postfix-expression . template [opt] id-expression
4400 postfix-expression . pseudo-destructor-name
4401 postfix-expression -> template [opt] id-expression
4402 postfix-expression -> pseudo-destructor-name
4404 FOR_OFFSETOF is set if we're being called in that context. That sorta
4405 limits what of the above we'll actually accept, but nevermind.
4406 TOKEN_TYPE is the "." or "->" token, which will already have been
4407 removed from the stream. */
4409 static tree
4410 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4411 enum cpp_ttype token_type,
4412 tree postfix_expression,
4413 bool for_offsetof, cp_id_kind *idk)
4415 tree name;
4416 bool dependent_p;
4417 bool pseudo_destructor_p;
4418 tree scope = NULL_TREE;
4420 /* If this is a `->' operator, dereference the pointer. */
4421 if (token_type == CPP_DEREF)
4422 postfix_expression = build_x_arrow (postfix_expression);
4423 /* Check to see whether or not the expression is type-dependent. */
4424 dependent_p = type_dependent_expression_p (postfix_expression);
4425 /* The identifier following the `->' or `.' is not qualified. */
4426 parser->scope = NULL_TREE;
4427 parser->qualifying_scope = NULL_TREE;
4428 parser->object_scope = NULL_TREE;
4429 *idk = CP_ID_KIND_NONE;
4430 /* Enter the scope corresponding to the type of the object
4431 given by the POSTFIX_EXPRESSION. */
4432 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4434 scope = TREE_TYPE (postfix_expression);
4435 /* According to the standard, no expression should ever have
4436 reference type. Unfortunately, we do not currently match
4437 the standard in this respect in that our internal representation
4438 of an expression may have reference type even when the standard
4439 says it does not. Therefore, we have to manually obtain the
4440 underlying type here. */
4441 scope = non_reference (scope);
4442 /* The type of the POSTFIX_EXPRESSION must be complete. */
4443 if (scope == unknown_type_node)
4445 error ("%qE does not have class type", postfix_expression);
4446 scope = NULL_TREE;
4448 else
4449 scope = complete_type_or_else (scope, NULL_TREE);
4450 /* Let the name lookup machinery know that we are processing a
4451 class member access expression. */
4452 parser->context->object_type = scope;
4453 /* If something went wrong, we want to be able to discern that case,
4454 as opposed to the case where there was no SCOPE due to the type
4455 of expression being dependent. */
4456 if (!scope)
4457 scope = error_mark_node;
4458 /* If the SCOPE was erroneous, make the various semantic analysis
4459 functions exit quickly -- and without issuing additional error
4460 messages. */
4461 if (scope == error_mark_node)
4462 postfix_expression = error_mark_node;
4465 /* Assume this expression is not a pseudo-destructor access. */
4466 pseudo_destructor_p = false;
4468 /* If the SCOPE is a scalar type, then, if this is a valid program,
4469 we must be looking at a pseudo-destructor-name. */
4470 if (scope && SCALAR_TYPE_P (scope))
4472 tree s;
4473 tree type;
4475 cp_parser_parse_tentatively (parser);
4476 /* Parse the pseudo-destructor-name. */
4477 s = NULL_TREE;
4478 cp_parser_pseudo_destructor_name (parser, &s, &type);
4479 if (cp_parser_parse_definitely (parser))
4481 pseudo_destructor_p = true;
4482 postfix_expression
4483 = finish_pseudo_destructor_expr (postfix_expression,
4484 s, TREE_TYPE (type));
4488 if (!pseudo_destructor_p)
4490 /* If the SCOPE is not a scalar type, we are looking at an
4491 ordinary class member access expression, rather than a
4492 pseudo-destructor-name. */
4493 bool template_p;
4494 /* Parse the id-expression. */
4495 name = (cp_parser_id_expression
4496 (parser,
4497 cp_parser_optional_template_keyword (parser),
4498 /*check_dependency_p=*/true,
4499 &template_p,
4500 /*declarator_p=*/false));
4501 /* In general, build a SCOPE_REF if the member name is qualified.
4502 However, if the name was not dependent and has already been
4503 resolved; there is no need to build the SCOPE_REF. For example;
4505 struct X { void f(); };
4506 template <typename T> void f(T* t) { t->X::f(); }
4508 Even though "t" is dependent, "X::f" is not and has been resolved
4509 to a BASELINK; there is no need to include scope information. */
4511 /* But we do need to remember that there was an explicit scope for
4512 virtual function calls. */
4513 if (parser->scope)
4514 *idk = CP_ID_KIND_QUALIFIED;
4516 /* If the name is a template-id that names a type, we will get a
4517 TYPE_DECL here. That is invalid code. */
4518 if (TREE_CODE (name) == TYPE_DECL)
4520 error ("invalid use of %qD", name);
4521 postfix_expression = error_mark_node;
4523 else
4525 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4527 name = build_qualified_name (/*type=*/NULL_TREE,
4528 parser->scope,
4529 name,
4530 template_p);
4531 parser->scope = NULL_TREE;
4532 parser->qualifying_scope = NULL_TREE;
4533 parser->object_scope = NULL_TREE;
4535 if (scope && name && BASELINK_P (name))
4536 adjust_result_of_qualified_name_lookup
4537 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4538 postfix_expression
4539 = finish_class_member_access_expr (postfix_expression, name,
4540 template_p);
4544 /* We no longer need to look up names in the scope of the object on
4545 the left-hand side of the `.' or `->' operator. */
4546 parser->context->object_type = NULL_TREE;
4548 /* Outside of offsetof, these operators may not appear in
4549 constant-expressions. */
4550 if (!for_offsetof
4551 && (cp_parser_non_integral_constant_expression
4552 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4553 postfix_expression = error_mark_node;
4555 return postfix_expression;
4558 /* Parse a parenthesized expression-list.
4560 expression-list:
4561 assignment-expression
4562 expression-list, assignment-expression
4564 attribute-list:
4565 expression-list
4566 identifier
4567 identifier, expression-list
4569 CAST_P is true if this expression is the target of a cast.
4571 Returns a TREE_LIST. The TREE_VALUE of each node is a
4572 representation of an assignment-expression. Note that a TREE_LIST
4573 is returned even if there is only a single expression in the list.
4574 error_mark_node is returned if the ( and or ) are
4575 missing. NULL_TREE is returned on no expressions. The parentheses
4576 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4577 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4578 indicates whether or not all of the expressions in the list were
4579 constant. */
4581 static tree
4582 cp_parser_parenthesized_expression_list (cp_parser* parser,
4583 bool is_attribute_list,
4584 bool cast_p,
4585 bool *non_constant_p)
4587 tree expression_list = NULL_TREE;
4588 bool fold_expr_p = is_attribute_list;
4589 tree identifier = NULL_TREE;
4591 /* Assume all the expressions will be constant. */
4592 if (non_constant_p)
4593 *non_constant_p = false;
4595 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4596 return error_mark_node;
4598 /* Consume expressions until there are no more. */
4599 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4600 while (true)
4602 tree expr;
4604 /* At the beginning of attribute lists, check to see if the
4605 next token is an identifier. */
4606 if (is_attribute_list
4607 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4609 cp_token *token;
4611 /* Consume the identifier. */
4612 token = cp_lexer_consume_token (parser->lexer);
4613 /* Save the identifier. */
4614 identifier = token->value;
4616 else
4618 /* Parse the next assignment-expression. */
4619 if (non_constant_p)
4621 bool expr_non_constant_p;
4622 expr = (cp_parser_constant_expression
4623 (parser, /*allow_non_constant_p=*/true,
4624 &expr_non_constant_p));
4625 if (expr_non_constant_p)
4626 *non_constant_p = true;
4628 else
4629 expr = cp_parser_assignment_expression (parser, cast_p);
4631 if (fold_expr_p)
4632 expr = fold_non_dependent_expr (expr);
4634 /* Add it to the list. We add error_mark_node
4635 expressions to the list, so that we can still tell if
4636 the correct form for a parenthesized expression-list
4637 is found. That gives better errors. */
4638 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4640 if (expr == error_mark_node)
4641 goto skip_comma;
4644 /* After the first item, attribute lists look the same as
4645 expression lists. */
4646 is_attribute_list = false;
4648 get_comma:;
4649 /* If the next token isn't a `,', then we are done. */
4650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4651 break;
4653 /* Otherwise, consume the `,' and keep going. */
4654 cp_lexer_consume_token (parser->lexer);
4657 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4659 int ending;
4661 skip_comma:;
4662 /* We try and resync to an unnested comma, as that will give the
4663 user better diagnostics. */
4664 ending = cp_parser_skip_to_closing_parenthesis (parser,
4665 /*recovering=*/true,
4666 /*or_comma=*/true,
4667 /*consume_paren=*/true);
4668 if (ending < 0)
4669 goto get_comma;
4670 if (!ending)
4671 return error_mark_node;
4674 /* We built up the list in reverse order so we must reverse it now. */
4675 expression_list = nreverse (expression_list);
4676 if (identifier)
4677 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4679 return expression_list;
4682 /* Parse a pseudo-destructor-name.
4684 pseudo-destructor-name:
4685 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4686 :: [opt] nested-name-specifier template template-id :: ~ type-name
4687 :: [opt] nested-name-specifier [opt] ~ type-name
4689 If either of the first two productions is used, sets *SCOPE to the
4690 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4691 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4692 or ERROR_MARK_NODE if the parse fails. */
4694 static void
4695 cp_parser_pseudo_destructor_name (cp_parser* parser,
4696 tree* scope,
4697 tree* type)
4699 bool nested_name_specifier_p;
4701 /* Assume that things will not work out. */
4702 *type = error_mark_node;
4704 /* Look for the optional `::' operator. */
4705 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4706 /* Look for the optional nested-name-specifier. */
4707 nested_name_specifier_p
4708 = (cp_parser_nested_name_specifier_opt (parser,
4709 /*typename_keyword_p=*/false,
4710 /*check_dependency_p=*/true,
4711 /*type_p=*/false,
4712 /*is_declaration=*/true)
4713 != NULL_TREE);
4714 /* Now, if we saw a nested-name-specifier, we might be doing the
4715 second production. */
4716 if (nested_name_specifier_p
4717 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4719 /* Consume the `template' keyword. */
4720 cp_lexer_consume_token (parser->lexer);
4721 /* Parse the template-id. */
4722 cp_parser_template_id (parser,
4723 /*template_keyword_p=*/true,
4724 /*check_dependency_p=*/false,
4725 /*is_declaration=*/true);
4726 /* Look for the `::' token. */
4727 cp_parser_require (parser, CPP_SCOPE, "`::'");
4729 /* If the next token is not a `~', then there might be some
4730 additional qualification. */
4731 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4733 /* Look for the type-name. */
4734 *scope = TREE_TYPE (cp_parser_type_name (parser));
4736 if (*scope == error_mark_node)
4737 return;
4739 /* If we don't have ::~, then something has gone wrong. Since
4740 the only caller of this function is looking for something
4741 after `.' or `->' after a scalar type, most likely the
4742 program is trying to get a member of a non-aggregate
4743 type. */
4744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4745 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4747 cp_parser_error (parser, "request for member of non-aggregate type");
4748 return;
4751 /* Look for the `::' token. */
4752 cp_parser_require (parser, CPP_SCOPE, "`::'");
4754 else
4755 *scope = NULL_TREE;
4757 /* Look for the `~'. */
4758 cp_parser_require (parser, CPP_COMPL, "`~'");
4759 /* Look for the type-name again. We are not responsible for
4760 checking that it matches the first type-name. */
4761 *type = cp_parser_type_name (parser);
4764 /* Parse a unary-expression.
4766 unary-expression:
4767 postfix-expression
4768 ++ cast-expression
4769 -- cast-expression
4770 unary-operator cast-expression
4771 sizeof unary-expression
4772 sizeof ( type-id )
4773 new-expression
4774 delete-expression
4776 GNU Extensions:
4778 unary-expression:
4779 __extension__ cast-expression
4780 __alignof__ unary-expression
4781 __alignof__ ( type-id )
4782 __real__ cast-expression
4783 __imag__ cast-expression
4784 && identifier
4786 ADDRESS_P is true iff the unary-expression is appearing as the
4787 operand of the `&' operator. CAST_P is true if this expression is
4788 the target of a cast.
4790 Returns a representation of the expression. */
4792 static tree
4793 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4795 cp_token *token;
4796 enum tree_code unary_operator;
4798 /* Peek at the next token. */
4799 token = cp_lexer_peek_token (parser->lexer);
4800 /* Some keywords give away the kind of expression. */
4801 if (token->type == CPP_KEYWORD)
4803 enum rid keyword = token->keyword;
4805 switch (keyword)
4807 case RID_ALIGNOF:
4808 case RID_SIZEOF:
4810 tree operand;
4811 enum tree_code op;
4813 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4814 /* Consume the token. */
4815 cp_lexer_consume_token (parser->lexer);
4816 /* Parse the operand. */
4817 operand = cp_parser_sizeof_operand (parser, keyword);
4819 if (TYPE_P (operand))
4820 return cxx_sizeof_or_alignof_type (operand, op, true);
4821 else
4822 return cxx_sizeof_or_alignof_expr (operand, op);
4825 case RID_NEW:
4826 return cp_parser_new_expression (parser);
4828 case RID_DELETE:
4829 return cp_parser_delete_expression (parser);
4831 case RID_EXTENSION:
4833 /* The saved value of the PEDANTIC flag. */
4834 int saved_pedantic;
4835 tree expr;
4837 /* Save away the PEDANTIC flag. */
4838 cp_parser_extension_opt (parser, &saved_pedantic);
4839 /* Parse the cast-expression. */
4840 expr = cp_parser_simple_cast_expression (parser);
4841 /* Restore the PEDANTIC flag. */
4842 pedantic = saved_pedantic;
4844 return expr;
4847 case RID_REALPART:
4848 case RID_IMAGPART:
4850 tree expression;
4852 /* Consume the `__real__' or `__imag__' token. */
4853 cp_lexer_consume_token (parser->lexer);
4854 /* Parse the cast-expression. */
4855 expression = cp_parser_simple_cast_expression (parser);
4856 /* Create the complete representation. */
4857 return build_x_unary_op ((keyword == RID_REALPART
4858 ? REALPART_EXPR : IMAGPART_EXPR),
4859 expression);
4861 break;
4863 default:
4864 break;
4868 /* Look for the `:: new' and `:: delete', which also signal the
4869 beginning of a new-expression, or delete-expression,
4870 respectively. If the next token is `::', then it might be one of
4871 these. */
4872 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4874 enum rid keyword;
4876 /* See if the token after the `::' is one of the keywords in
4877 which we're interested. */
4878 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4879 /* If it's `new', we have a new-expression. */
4880 if (keyword == RID_NEW)
4881 return cp_parser_new_expression (parser);
4882 /* Similarly, for `delete'. */
4883 else if (keyword == RID_DELETE)
4884 return cp_parser_delete_expression (parser);
4887 /* Look for a unary operator. */
4888 unary_operator = cp_parser_unary_operator (token);
4889 /* The `++' and `--' operators can be handled similarly, even though
4890 they are not technically unary-operators in the grammar. */
4891 if (unary_operator == ERROR_MARK)
4893 if (token->type == CPP_PLUS_PLUS)
4894 unary_operator = PREINCREMENT_EXPR;
4895 else if (token->type == CPP_MINUS_MINUS)
4896 unary_operator = PREDECREMENT_EXPR;
4897 /* Handle the GNU address-of-label extension. */
4898 else if (cp_parser_allow_gnu_extensions_p (parser)
4899 && token->type == CPP_AND_AND)
4901 tree identifier;
4903 /* Consume the '&&' token. */
4904 cp_lexer_consume_token (parser->lexer);
4905 /* Look for the identifier. */
4906 identifier = cp_parser_identifier (parser);
4907 /* Create an expression representing the address. */
4908 return finish_label_address_expr (identifier);
4911 if (unary_operator != ERROR_MARK)
4913 tree cast_expression;
4914 tree expression = error_mark_node;
4915 const char *non_constant_p = NULL;
4917 /* Consume the operator token. */
4918 token = cp_lexer_consume_token (parser->lexer);
4919 /* Parse the cast-expression. */
4920 cast_expression
4921 = cp_parser_cast_expression (parser,
4922 unary_operator == ADDR_EXPR,
4923 /*cast_p=*/false);
4924 /* Now, build an appropriate representation. */
4925 switch (unary_operator)
4927 case INDIRECT_REF:
4928 non_constant_p = "`*'";
4929 expression = build_x_indirect_ref (cast_expression, "unary *");
4930 break;
4932 case ADDR_EXPR:
4933 non_constant_p = "`&'";
4934 /* Fall through. */
4935 case BIT_NOT_EXPR:
4936 expression = build_x_unary_op (unary_operator, cast_expression);
4937 break;
4939 case PREINCREMENT_EXPR:
4940 case PREDECREMENT_EXPR:
4941 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4942 ? "`++'" : "`--'");
4943 /* Fall through. */
4944 case UNARY_PLUS_EXPR:
4945 case NEGATE_EXPR:
4946 case TRUTH_NOT_EXPR:
4947 expression = finish_unary_op_expr (unary_operator, cast_expression);
4948 break;
4950 default:
4951 gcc_unreachable ();
4954 if (non_constant_p
4955 && cp_parser_non_integral_constant_expression (parser,
4956 non_constant_p))
4957 expression = error_mark_node;
4959 return expression;
4962 return cp_parser_postfix_expression (parser, address_p, cast_p);
4965 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4966 unary-operator, the corresponding tree code is returned. */
4968 static enum tree_code
4969 cp_parser_unary_operator (cp_token* token)
4971 switch (token->type)
4973 case CPP_MULT:
4974 return INDIRECT_REF;
4976 case CPP_AND:
4977 return ADDR_EXPR;
4979 case CPP_PLUS:
4980 return UNARY_PLUS_EXPR;
4982 case CPP_MINUS:
4983 return NEGATE_EXPR;
4985 case CPP_NOT:
4986 return TRUTH_NOT_EXPR;
4988 case CPP_COMPL:
4989 return BIT_NOT_EXPR;
4991 default:
4992 return ERROR_MARK;
4996 /* Parse a new-expression.
4998 new-expression:
4999 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5000 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5002 Returns a representation of the expression. */
5004 static tree
5005 cp_parser_new_expression (cp_parser* parser)
5007 bool global_scope_p;
5008 tree placement;
5009 tree type;
5010 tree initializer;
5011 tree nelts;
5013 /* Look for the optional `::' operator. */
5014 global_scope_p
5015 = (cp_parser_global_scope_opt (parser,
5016 /*current_scope_valid_p=*/false)
5017 != NULL_TREE);
5018 /* Look for the `new' operator. */
5019 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5020 /* There's no easy way to tell a new-placement from the
5021 `( type-id )' construct. */
5022 cp_parser_parse_tentatively (parser);
5023 /* Look for a new-placement. */
5024 placement = cp_parser_new_placement (parser);
5025 /* If that didn't work out, there's no new-placement. */
5026 if (!cp_parser_parse_definitely (parser))
5027 placement = NULL_TREE;
5029 /* If the next token is a `(', then we have a parenthesized
5030 type-id. */
5031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5033 /* Consume the `('. */
5034 cp_lexer_consume_token (parser->lexer);
5035 /* Parse the type-id. */
5036 type = cp_parser_type_id (parser);
5037 /* Look for the closing `)'. */
5038 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5039 /* There should not be a direct-new-declarator in this production,
5040 but GCC used to allowed this, so we check and emit a sensible error
5041 message for this case. */
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5044 error ("array bound forbidden after parenthesized type-id");
5045 inform ("try removing the parentheses around the type-id");
5046 cp_parser_direct_new_declarator (parser);
5048 nelts = NULL_TREE;
5050 /* Otherwise, there must be a new-type-id. */
5051 else
5052 type = cp_parser_new_type_id (parser, &nelts);
5054 /* If the next token is a `(', then we have a new-initializer. */
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5056 initializer = cp_parser_new_initializer (parser);
5057 else
5058 initializer = NULL_TREE;
5060 /* A new-expression may not appear in an integral constant
5061 expression. */
5062 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5063 return error_mark_node;
5065 /* Create a representation of the new-expression. */
5066 return build_new (placement, type, nelts, initializer, global_scope_p);
5069 /* Parse a new-placement.
5071 new-placement:
5072 ( expression-list )
5074 Returns the same representation as for an expression-list. */
5076 static tree
5077 cp_parser_new_placement (cp_parser* parser)
5079 tree expression_list;
5081 /* Parse the expression-list. */
5082 expression_list = (cp_parser_parenthesized_expression_list
5083 (parser, false, /*cast_p=*/false,
5084 /*non_constant_p=*/NULL));
5086 return expression_list;
5089 /* Parse a new-type-id.
5091 new-type-id:
5092 type-specifier-seq new-declarator [opt]
5094 Returns the TYPE allocated. If the new-type-id indicates an array
5095 type, *NELTS is set to the number of elements in the last array
5096 bound; the TYPE will not include the last array bound. */
5098 static tree
5099 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5101 cp_decl_specifier_seq type_specifier_seq;
5102 cp_declarator *new_declarator;
5103 cp_declarator *declarator;
5104 cp_declarator *outer_declarator;
5105 const char *saved_message;
5106 tree type;
5108 /* The type-specifier sequence must not contain type definitions.
5109 (It cannot contain declarations of new types either, but if they
5110 are not definitions we will catch that because they are not
5111 complete.) */
5112 saved_message = parser->type_definition_forbidden_message;
5113 parser->type_definition_forbidden_message
5114 = "types may not be defined in a new-type-id";
5115 /* Parse the type-specifier-seq. */
5116 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5117 &type_specifier_seq);
5118 /* Restore the old message. */
5119 parser->type_definition_forbidden_message = saved_message;
5120 /* Parse the new-declarator. */
5121 new_declarator = cp_parser_new_declarator_opt (parser);
5123 /* Determine the number of elements in the last array dimension, if
5124 any. */
5125 *nelts = NULL_TREE;
5126 /* Skip down to the last array dimension. */
5127 declarator = new_declarator;
5128 outer_declarator = NULL;
5129 while (declarator && (declarator->kind == cdk_pointer
5130 || declarator->kind == cdk_ptrmem))
5132 outer_declarator = declarator;
5133 declarator = declarator->declarator;
5135 while (declarator
5136 && declarator->kind == cdk_array
5137 && declarator->declarator
5138 && declarator->declarator->kind == cdk_array)
5140 outer_declarator = declarator;
5141 declarator = declarator->declarator;
5144 if (declarator && declarator->kind == cdk_array)
5146 *nelts = declarator->u.array.bounds;
5147 if (*nelts == error_mark_node)
5148 *nelts = integer_one_node;
5150 if (outer_declarator)
5151 outer_declarator->declarator = declarator->declarator;
5152 else
5153 new_declarator = NULL;
5156 type = groktypename (&type_specifier_seq, new_declarator);
5157 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5159 *nelts = array_type_nelts_top (type);
5160 type = TREE_TYPE (type);
5162 return type;
5165 /* Parse an (optional) new-declarator.
5167 new-declarator:
5168 ptr-operator new-declarator [opt]
5169 direct-new-declarator
5171 Returns the declarator. */
5173 static cp_declarator *
5174 cp_parser_new_declarator_opt (cp_parser* parser)
5176 enum tree_code code;
5177 tree type;
5178 cp_cv_quals cv_quals;
5180 /* We don't know if there's a ptr-operator next, or not. */
5181 cp_parser_parse_tentatively (parser);
5182 /* Look for a ptr-operator. */
5183 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5184 /* If that worked, look for more new-declarators. */
5185 if (cp_parser_parse_definitely (parser))
5187 cp_declarator *declarator;
5189 /* Parse another optional declarator. */
5190 declarator = cp_parser_new_declarator_opt (parser);
5192 /* Create the representation of the declarator. */
5193 if (type)
5194 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5195 else if (code == INDIRECT_REF)
5196 declarator = make_pointer_declarator (cv_quals, declarator);
5197 else
5198 declarator = make_reference_declarator (cv_quals, declarator);
5200 return declarator;
5203 /* If the next token is a `[', there is a direct-new-declarator. */
5204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5205 return cp_parser_direct_new_declarator (parser);
5207 return NULL;
5210 /* Parse a direct-new-declarator.
5212 direct-new-declarator:
5213 [ expression ]
5214 direct-new-declarator [constant-expression]
5218 static cp_declarator *
5219 cp_parser_direct_new_declarator (cp_parser* parser)
5221 cp_declarator *declarator = NULL;
5223 while (true)
5225 tree expression;
5227 /* Look for the opening `['. */
5228 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5229 /* The first expression is not required to be constant. */
5230 if (!declarator)
5232 expression = cp_parser_expression (parser, /*cast_p=*/false);
5233 /* The standard requires that the expression have integral
5234 type. DR 74 adds enumeration types. We believe that the
5235 real intent is that these expressions be handled like the
5236 expression in a `switch' condition, which also allows
5237 classes with a single conversion to integral or
5238 enumeration type. */
5239 if (!processing_template_decl)
5241 expression
5242 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5243 expression,
5244 /*complain=*/true);
5245 if (!expression)
5247 error ("expression in new-declarator must have integral "
5248 "or enumeration type");
5249 expression = error_mark_node;
5253 /* But all the other expressions must be. */
5254 else
5255 expression
5256 = cp_parser_constant_expression (parser,
5257 /*allow_non_constant=*/false,
5258 NULL);
5259 /* Look for the closing `]'. */
5260 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5262 /* Add this bound to the declarator. */
5263 declarator = make_array_declarator (declarator, expression);
5265 /* If the next token is not a `[', then there are no more
5266 bounds. */
5267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5268 break;
5271 return declarator;
5274 /* Parse a new-initializer.
5276 new-initializer:
5277 ( expression-list [opt] )
5279 Returns a representation of the expression-list. If there is no
5280 expression-list, VOID_ZERO_NODE is returned. */
5282 static tree
5283 cp_parser_new_initializer (cp_parser* parser)
5285 tree expression_list;
5287 expression_list = (cp_parser_parenthesized_expression_list
5288 (parser, false, /*cast_p=*/false,
5289 /*non_constant_p=*/NULL));
5290 if (!expression_list)
5291 expression_list = void_zero_node;
5293 return expression_list;
5296 /* Parse a delete-expression.
5298 delete-expression:
5299 :: [opt] delete cast-expression
5300 :: [opt] delete [ ] cast-expression
5302 Returns a representation of the expression. */
5304 static tree
5305 cp_parser_delete_expression (cp_parser* parser)
5307 bool global_scope_p;
5308 bool array_p;
5309 tree expression;
5311 /* Look for the optional `::' operator. */
5312 global_scope_p
5313 = (cp_parser_global_scope_opt (parser,
5314 /*current_scope_valid_p=*/false)
5315 != NULL_TREE);
5316 /* Look for the `delete' keyword. */
5317 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5318 /* See if the array syntax is in use. */
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5321 /* Consume the `[' token. */
5322 cp_lexer_consume_token (parser->lexer);
5323 /* Look for the `]' token. */
5324 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5325 /* Remember that this is the `[]' construct. */
5326 array_p = true;
5328 else
5329 array_p = false;
5331 /* Parse the cast-expression. */
5332 expression = cp_parser_simple_cast_expression (parser);
5334 /* A delete-expression may not appear in an integral constant
5335 expression. */
5336 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5337 return error_mark_node;
5339 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5342 /* Parse a cast-expression.
5344 cast-expression:
5345 unary-expression
5346 ( type-id ) cast-expression
5348 ADDRESS_P is true iff the unary-expression is appearing as the
5349 operand of the `&' operator. CAST_P is true if this expression is
5350 the target of a cast.
5352 Returns a representation of the expression. */
5354 static tree
5355 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5357 /* If it's a `(', then we might be looking at a cast. */
5358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5360 tree type = NULL_TREE;
5361 tree expr = NULL_TREE;
5362 bool compound_literal_p;
5363 const char *saved_message;
5365 /* There's no way to know yet whether or not this is a cast.
5366 For example, `(int (3))' is a unary-expression, while `(int)
5367 3' is a cast. So, we resort to parsing tentatively. */
5368 cp_parser_parse_tentatively (parser);
5369 /* Types may not be defined in a cast. */
5370 saved_message = parser->type_definition_forbidden_message;
5371 parser->type_definition_forbidden_message
5372 = "types may not be defined in casts";
5373 /* Consume the `('. */
5374 cp_lexer_consume_token (parser->lexer);
5375 /* A very tricky bit is that `(struct S) { 3 }' is a
5376 compound-literal (which we permit in C++ as an extension).
5377 But, that construct is not a cast-expression -- it is a
5378 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5379 is legal; if the compound-literal were a cast-expression,
5380 you'd need an extra set of parentheses.) But, if we parse
5381 the type-id, and it happens to be a class-specifier, then we
5382 will commit to the parse at that point, because we cannot
5383 undo the action that is done when creating a new class. So,
5384 then we cannot back up and do a postfix-expression.
5386 Therefore, we scan ahead to the closing `)', and check to see
5387 if the token after the `)' is a `{'. If so, we are not
5388 looking at a cast-expression.
5390 Save tokens so that we can put them back. */
5391 cp_lexer_save_tokens (parser->lexer);
5392 /* Skip tokens until the next token is a closing parenthesis.
5393 If we find the closing `)', and the next token is a `{', then
5394 we are looking at a compound-literal. */
5395 compound_literal_p
5396 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5397 /*consume_paren=*/true)
5398 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5399 /* Roll back the tokens we skipped. */
5400 cp_lexer_rollback_tokens (parser->lexer);
5401 /* If we were looking at a compound-literal, simulate an error
5402 so that the call to cp_parser_parse_definitely below will
5403 fail. */
5404 if (compound_literal_p)
5405 cp_parser_simulate_error (parser);
5406 else
5408 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5409 parser->in_type_id_in_expr_p = true;
5410 /* Look for the type-id. */
5411 type = cp_parser_type_id (parser);
5412 /* Look for the closing `)'. */
5413 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5414 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5417 /* Restore the saved message. */
5418 parser->type_definition_forbidden_message = saved_message;
5420 /* If ok so far, parse the dependent expression. We cannot be
5421 sure it is a cast. Consider `(T ())'. It is a parenthesized
5422 ctor of T, but looks like a cast to function returning T
5423 without a dependent expression. */
5424 if (!cp_parser_error_occurred (parser))
5425 expr = cp_parser_cast_expression (parser,
5426 /*address_p=*/false,
5427 /*cast_p=*/true);
5429 if (cp_parser_parse_definitely (parser))
5431 /* Warn about old-style casts, if so requested. */
5432 if (warn_old_style_cast
5433 && !in_system_header
5434 && !VOID_TYPE_P (type)
5435 && current_lang_name != lang_name_c)
5436 warning (OPT_Wold_style_cast, "use of old-style cast");
5438 /* Only type conversions to integral or enumeration types
5439 can be used in constant-expressions. */
5440 if (parser->integral_constant_expression_p
5441 && !dependent_type_p (type)
5442 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5443 && (cp_parser_non_integral_constant_expression
5444 (parser,
5445 "a cast to a type other than an integral or "
5446 "enumeration type")))
5447 return error_mark_node;
5449 /* Perform the cast. */
5450 expr = build_c_cast (type, expr);
5451 return expr;
5455 /* If we get here, then it's not a cast, so it must be a
5456 unary-expression. */
5457 return cp_parser_unary_expression (parser, address_p, cast_p);
5460 /* Parse a binary expression of the general form:
5462 pm-expression:
5463 cast-expression
5464 pm-expression .* cast-expression
5465 pm-expression ->* cast-expression
5467 multiplicative-expression:
5468 pm-expression
5469 multiplicative-expression * pm-expression
5470 multiplicative-expression / pm-expression
5471 multiplicative-expression % pm-expression
5473 additive-expression:
5474 multiplicative-expression
5475 additive-expression + multiplicative-expression
5476 additive-expression - multiplicative-expression
5478 shift-expression:
5479 additive-expression
5480 shift-expression << additive-expression
5481 shift-expression >> additive-expression
5483 relational-expression:
5484 shift-expression
5485 relational-expression < shift-expression
5486 relational-expression > shift-expression
5487 relational-expression <= shift-expression
5488 relational-expression >= shift-expression
5490 GNU Extension:
5492 relational-expression:
5493 relational-expression <? shift-expression
5494 relational-expression >? shift-expression
5496 equality-expression:
5497 relational-expression
5498 equality-expression == relational-expression
5499 equality-expression != relational-expression
5501 and-expression:
5502 equality-expression
5503 and-expression & equality-expression
5505 exclusive-or-expression:
5506 and-expression
5507 exclusive-or-expression ^ and-expression
5509 inclusive-or-expression:
5510 exclusive-or-expression
5511 inclusive-or-expression | exclusive-or-expression
5513 logical-and-expression:
5514 inclusive-or-expression
5515 logical-and-expression && inclusive-or-expression
5517 logical-or-expression:
5518 logical-and-expression
5519 logical-or-expression || logical-and-expression
5521 All these are implemented with a single function like:
5523 binary-expression:
5524 simple-cast-expression
5525 binary-expression <token> binary-expression
5527 CAST_P is true if this expression is the target of a cast.
5529 The binops_by_token map is used to get the tree codes for each <token> type.
5530 binary-expressions are associated according to a precedence table. */
5532 #define TOKEN_PRECEDENCE(token) \
5533 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5534 ? PREC_NOT_OPERATOR \
5535 : binops_by_token[token->type].prec)
5537 static tree
5538 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5540 cp_parser_expression_stack stack;
5541 cp_parser_expression_stack_entry *sp = &stack[0];
5542 tree lhs, rhs;
5543 cp_token *token;
5544 enum tree_code tree_type;
5545 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5546 bool overloaded_p;
5548 /* Parse the first expression. */
5549 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5551 for (;;)
5553 /* Get an operator token. */
5554 token = cp_lexer_peek_token (parser->lexer);
5555 if (token->type == CPP_MIN || token->type == CPP_MAX)
5556 cp_parser_warn_min_max ();
5558 new_prec = TOKEN_PRECEDENCE (token);
5560 /* Popping an entry off the stack means we completed a subexpression:
5561 - either we found a token which is not an operator (`>' where it is not
5562 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5563 will happen repeatedly;
5564 - or, we found an operator which has lower priority. This is the case
5565 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5566 parsing `3 * 4'. */
5567 if (new_prec <= prec)
5569 if (sp == stack)
5570 break;
5571 else
5572 goto pop;
5575 get_rhs:
5576 tree_type = binops_by_token[token->type].tree_type;
5578 /* We used the operator token. */
5579 cp_lexer_consume_token (parser->lexer);
5581 /* Extract another operand. It may be the RHS of this expression
5582 or the LHS of a new, higher priority expression. */
5583 rhs = cp_parser_simple_cast_expression (parser);
5585 /* Get another operator token. Look up its precedence to avoid
5586 building a useless (immediately popped) stack entry for common
5587 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5588 token = cp_lexer_peek_token (parser->lexer);
5589 lookahead_prec = TOKEN_PRECEDENCE (token);
5590 if (lookahead_prec > new_prec)
5592 /* ... and prepare to parse the RHS of the new, higher priority
5593 expression. Since precedence levels on the stack are
5594 monotonically increasing, we do not have to care about
5595 stack overflows. */
5596 sp->prec = prec;
5597 sp->tree_type = tree_type;
5598 sp->lhs = lhs;
5599 sp++;
5600 lhs = rhs;
5601 prec = new_prec;
5602 new_prec = lookahead_prec;
5603 goto get_rhs;
5605 pop:
5606 /* If the stack is not empty, we have parsed into LHS the right side
5607 (`4' in the example above) of an expression we had suspended.
5608 We can use the information on the stack to recover the LHS (`3')
5609 from the stack together with the tree code (`MULT_EXPR'), and
5610 the precedence of the higher level subexpression
5611 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5612 which will be used to actually build the additive expression. */
5613 --sp;
5614 prec = sp->prec;
5615 tree_type = sp->tree_type;
5616 rhs = lhs;
5617 lhs = sp->lhs;
5620 overloaded_p = false;
5621 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5623 /* If the binary operator required the use of an overloaded operator,
5624 then this expression cannot be an integral constant-expression.
5625 An overloaded operator can be used even if both operands are
5626 otherwise permissible in an integral constant-expression if at
5627 least one of the operands is of enumeration type. */
5629 if (overloaded_p
5630 && (cp_parser_non_integral_constant_expression
5631 (parser, "calls to overloaded operators")))
5632 return error_mark_node;
5635 return lhs;
5639 /* Parse the `? expression : assignment-expression' part of a
5640 conditional-expression. The LOGICAL_OR_EXPR is the
5641 logical-or-expression that started the conditional-expression.
5642 Returns a representation of the entire conditional-expression.
5644 This routine is used by cp_parser_assignment_expression.
5646 ? expression : assignment-expression
5648 GNU Extensions:
5650 ? : assignment-expression */
5652 static tree
5653 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5655 tree expr;
5656 tree assignment_expr;
5658 /* Consume the `?' token. */
5659 cp_lexer_consume_token (parser->lexer);
5660 if (cp_parser_allow_gnu_extensions_p (parser)
5661 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5662 /* Implicit true clause. */
5663 expr = NULL_TREE;
5664 else
5665 /* Parse the expression. */
5666 expr = cp_parser_expression (parser, /*cast_p=*/false);
5668 /* The next token should be a `:'. */
5669 cp_parser_require (parser, CPP_COLON, "`:'");
5670 /* Parse the assignment-expression. */
5671 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5673 /* Build the conditional-expression. */
5674 return build_x_conditional_expr (logical_or_expr,
5675 expr,
5676 assignment_expr);
5679 /* Parse an assignment-expression.
5681 assignment-expression:
5682 conditional-expression
5683 logical-or-expression assignment-operator assignment_expression
5684 throw-expression
5686 CAST_P is true if this expression is the target of a cast.
5688 Returns a representation for the expression. */
5690 static tree
5691 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5693 tree expr;
5695 /* If the next token is the `throw' keyword, then we're looking at
5696 a throw-expression. */
5697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5698 expr = cp_parser_throw_expression (parser);
5699 /* Otherwise, it must be that we are looking at a
5700 logical-or-expression. */
5701 else
5703 /* Parse the binary expressions (logical-or-expression). */
5704 expr = cp_parser_binary_expression (parser, cast_p);
5705 /* If the next token is a `?' then we're actually looking at a
5706 conditional-expression. */
5707 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5708 return cp_parser_question_colon_clause (parser, expr);
5709 else
5711 enum tree_code assignment_operator;
5713 /* If it's an assignment-operator, we're using the second
5714 production. */
5715 assignment_operator
5716 = cp_parser_assignment_operator_opt (parser);
5717 if (assignment_operator != ERROR_MARK)
5719 tree rhs;
5721 /* Parse the right-hand side of the assignment. */
5722 rhs = cp_parser_assignment_expression (parser, cast_p);
5723 /* An assignment may not appear in a
5724 constant-expression. */
5725 if (cp_parser_non_integral_constant_expression (parser,
5726 "an assignment"))
5727 return error_mark_node;
5728 /* Build the assignment expression. */
5729 expr = build_x_modify_expr (expr,
5730 assignment_operator,
5731 rhs);
5736 return expr;
5739 /* Parse an (optional) assignment-operator.
5741 assignment-operator: one of
5742 = *= /= %= += -= >>= <<= &= ^= |=
5744 GNU Extension:
5746 assignment-operator: one of
5747 <?= >?=
5749 If the next token is an assignment operator, the corresponding tree
5750 code is returned, and the token is consumed. For example, for
5751 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5752 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5753 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5754 operator, ERROR_MARK is returned. */
5756 static enum tree_code
5757 cp_parser_assignment_operator_opt (cp_parser* parser)
5759 enum tree_code op;
5760 cp_token *token;
5762 /* Peek at the next toen. */
5763 token = cp_lexer_peek_token (parser->lexer);
5765 switch (token->type)
5767 case CPP_EQ:
5768 op = NOP_EXPR;
5769 break;
5771 case CPP_MULT_EQ:
5772 op = MULT_EXPR;
5773 break;
5775 case CPP_DIV_EQ:
5776 op = TRUNC_DIV_EXPR;
5777 break;
5779 case CPP_MOD_EQ:
5780 op = TRUNC_MOD_EXPR;
5781 break;
5783 case CPP_PLUS_EQ:
5784 op = PLUS_EXPR;
5785 break;
5787 case CPP_MINUS_EQ:
5788 op = MINUS_EXPR;
5789 break;
5791 case CPP_RSHIFT_EQ:
5792 op = RSHIFT_EXPR;
5793 break;
5795 case CPP_LSHIFT_EQ:
5796 op = LSHIFT_EXPR;
5797 break;
5799 case CPP_AND_EQ:
5800 op = BIT_AND_EXPR;
5801 break;
5803 case CPP_XOR_EQ:
5804 op = BIT_XOR_EXPR;
5805 break;
5807 case CPP_OR_EQ:
5808 op = BIT_IOR_EXPR;
5809 break;
5811 case CPP_MIN_EQ:
5812 op = MIN_EXPR;
5813 cp_parser_warn_min_max ();
5814 break;
5816 case CPP_MAX_EQ:
5817 op = MAX_EXPR;
5818 cp_parser_warn_min_max ();
5819 break;
5821 default:
5822 /* Nothing else is an assignment operator. */
5823 op = ERROR_MARK;
5826 /* If it was an assignment operator, consume it. */
5827 if (op != ERROR_MARK)
5828 cp_lexer_consume_token (parser->lexer);
5830 return op;
5833 /* Parse an expression.
5835 expression:
5836 assignment-expression
5837 expression , assignment-expression
5839 CAST_P is true if this expression is the target of a cast.
5841 Returns a representation of the expression. */
5843 static tree
5844 cp_parser_expression (cp_parser* parser, bool cast_p)
5846 tree expression = NULL_TREE;
5848 while (true)
5850 tree assignment_expression;
5852 /* Parse the next assignment-expression. */
5853 assignment_expression
5854 = cp_parser_assignment_expression (parser, cast_p);
5855 /* If this is the first assignment-expression, we can just
5856 save it away. */
5857 if (!expression)
5858 expression = assignment_expression;
5859 else
5860 expression = build_x_compound_expr (expression,
5861 assignment_expression);
5862 /* If the next token is not a comma, then we are done with the
5863 expression. */
5864 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5865 break;
5866 /* Consume the `,'. */
5867 cp_lexer_consume_token (parser->lexer);
5868 /* A comma operator cannot appear in a constant-expression. */
5869 if (cp_parser_non_integral_constant_expression (parser,
5870 "a comma operator"))
5871 expression = error_mark_node;
5874 return expression;
5877 /* Parse a constant-expression.
5879 constant-expression:
5880 conditional-expression
5882 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5883 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5884 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5885 is false, NON_CONSTANT_P should be NULL. */
5887 static tree
5888 cp_parser_constant_expression (cp_parser* parser,
5889 bool allow_non_constant_p,
5890 bool *non_constant_p)
5892 bool saved_integral_constant_expression_p;
5893 bool saved_allow_non_integral_constant_expression_p;
5894 bool saved_non_integral_constant_expression_p;
5895 tree expression;
5897 /* It might seem that we could simply parse the
5898 conditional-expression, and then check to see if it were
5899 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5900 one that the compiler can figure out is constant, possibly after
5901 doing some simplifications or optimizations. The standard has a
5902 precise definition of constant-expression, and we must honor
5903 that, even though it is somewhat more restrictive.
5905 For example:
5907 int i[(2, 3)];
5909 is not a legal declaration, because `(2, 3)' is not a
5910 constant-expression. The `,' operator is forbidden in a
5911 constant-expression. However, GCC's constant-folding machinery
5912 will fold this operation to an INTEGER_CST for `3'. */
5914 /* Save the old settings. */
5915 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5916 saved_allow_non_integral_constant_expression_p
5917 = parser->allow_non_integral_constant_expression_p;
5918 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5919 /* We are now parsing a constant-expression. */
5920 parser->integral_constant_expression_p = true;
5921 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5922 parser->non_integral_constant_expression_p = false;
5923 /* Although the grammar says "conditional-expression", we parse an
5924 "assignment-expression", which also permits "throw-expression"
5925 and the use of assignment operators. In the case that
5926 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5927 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5928 actually essential that we look for an assignment-expression.
5929 For example, cp_parser_initializer_clauses uses this function to
5930 determine whether a particular assignment-expression is in fact
5931 constant. */
5932 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5933 /* Restore the old settings. */
5934 parser->integral_constant_expression_p
5935 = saved_integral_constant_expression_p;
5936 parser->allow_non_integral_constant_expression_p
5937 = saved_allow_non_integral_constant_expression_p;
5938 if (allow_non_constant_p)
5939 *non_constant_p = parser->non_integral_constant_expression_p;
5940 else if (parser->non_integral_constant_expression_p)
5941 expression = error_mark_node;
5942 parser->non_integral_constant_expression_p
5943 = saved_non_integral_constant_expression_p;
5945 return expression;
5948 /* Parse __builtin_offsetof.
5950 offsetof-expression:
5951 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5953 offsetof-member-designator:
5954 id-expression
5955 | offsetof-member-designator "." id-expression
5956 | offsetof-member-designator "[" expression "]"
5959 static tree
5960 cp_parser_builtin_offsetof (cp_parser *parser)
5962 int save_ice_p, save_non_ice_p;
5963 tree type, expr;
5964 cp_id_kind dummy;
5966 /* We're about to accept non-integral-constant things, but will
5967 definitely yield an integral constant expression. Save and
5968 restore these values around our local parsing. */
5969 save_ice_p = parser->integral_constant_expression_p;
5970 save_non_ice_p = parser->non_integral_constant_expression_p;
5972 /* Consume the "__builtin_offsetof" token. */
5973 cp_lexer_consume_token (parser->lexer);
5974 /* Consume the opening `('. */
5975 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5976 /* Parse the type-id. */
5977 type = cp_parser_type_id (parser);
5978 /* Look for the `,'. */
5979 cp_parser_require (parser, CPP_COMMA, "`,'");
5981 /* Build the (type *)null that begins the traditional offsetof macro. */
5982 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5984 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5985 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5986 true, &dummy);
5987 while (true)
5989 cp_token *token = cp_lexer_peek_token (parser->lexer);
5990 switch (token->type)
5992 case CPP_OPEN_SQUARE:
5993 /* offsetof-member-designator "[" expression "]" */
5994 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5995 break;
5997 case CPP_DOT:
5998 /* offsetof-member-designator "." identifier */
5999 cp_lexer_consume_token (parser->lexer);
6000 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6001 true, &dummy);
6002 break;
6004 case CPP_CLOSE_PAREN:
6005 /* Consume the ")" token. */
6006 cp_lexer_consume_token (parser->lexer);
6007 goto success;
6009 default:
6010 /* Error. We know the following require will fail, but
6011 that gives the proper error message. */
6012 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6013 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6014 expr = error_mark_node;
6015 goto failure;
6019 success:
6020 /* If we're processing a template, we can't finish the semantics yet.
6021 Otherwise we can fold the entire expression now. */
6022 if (processing_template_decl)
6023 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6024 else
6025 expr = fold_offsetof (expr);
6027 failure:
6028 parser->integral_constant_expression_p = save_ice_p;
6029 parser->non_integral_constant_expression_p = save_non_ice_p;
6031 return expr;
6034 /* Statements [gram.stmt.stmt] */
6036 /* Parse a statement.
6038 statement:
6039 labeled-statement
6040 expression-statement
6041 compound-statement
6042 selection-statement
6043 iteration-statement
6044 jump-statement
6045 declaration-statement
6046 try-block
6048 IN_COMPOUND is true when the statement is nested inside a
6049 cp_parser_compound_statement; this matters for certain pragmas. */
6051 static void
6052 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6053 bool in_compound)
6055 tree statement;
6056 cp_token *token;
6057 location_t statement_location;
6059 restart:
6060 /* There is no statement yet. */
6061 statement = NULL_TREE;
6062 /* Peek at the next token. */
6063 token = cp_lexer_peek_token (parser->lexer);
6064 /* Remember the location of the first token in the statement. */
6065 statement_location = token->location;
6066 /* If this is a keyword, then that will often determine what kind of
6067 statement we have. */
6068 if (token->type == CPP_KEYWORD)
6070 enum rid keyword = token->keyword;
6072 switch (keyword)
6074 case RID_CASE:
6075 case RID_DEFAULT:
6076 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6077 in_compound);
6078 break;
6080 case RID_IF:
6081 case RID_SWITCH:
6082 statement = cp_parser_selection_statement (parser);
6083 break;
6085 case RID_WHILE:
6086 case RID_DO:
6087 case RID_FOR:
6088 statement = cp_parser_iteration_statement (parser);
6089 break;
6091 case RID_BREAK:
6092 case RID_CONTINUE:
6093 case RID_RETURN:
6094 case RID_GOTO:
6095 statement = cp_parser_jump_statement (parser);
6096 break;
6098 /* Objective-C++ exception-handling constructs. */
6099 case RID_AT_TRY:
6100 case RID_AT_CATCH:
6101 case RID_AT_FINALLY:
6102 case RID_AT_SYNCHRONIZED:
6103 case RID_AT_THROW:
6104 statement = cp_parser_objc_statement (parser);
6105 break;
6107 case RID_TRY:
6108 statement = cp_parser_try_block (parser);
6109 break;
6111 default:
6112 /* It might be a keyword like `int' that can start a
6113 declaration-statement. */
6114 break;
6117 else if (token->type == CPP_NAME)
6119 /* If the next token is a `:', then we are looking at a
6120 labeled-statement. */
6121 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6122 if (token->type == CPP_COLON)
6123 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6124 in_compound);
6126 /* Anything that starts with a `{' must be a compound-statement. */
6127 else if (token->type == CPP_OPEN_BRACE)
6128 statement = cp_parser_compound_statement (parser, NULL, false);
6129 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6130 a statement all its own. */
6131 else if (token->type == CPP_PRAGMA)
6133 /* Only certain OpenMP pragmas are attached to statements, and thus
6134 are considered statements themselves. All others are not. In
6135 the context of a compound, accept the pragma as a "statement" and
6136 return so that we can check for a close brace. Otherwise we
6137 require a real statement and must go back and read one. */
6138 if (in_compound)
6139 cp_parser_pragma (parser, pragma_compound);
6140 else if (!cp_parser_pragma (parser, pragma_stmt))
6141 goto restart;
6142 return;
6144 else if (token->type == CPP_EOF)
6146 cp_parser_error (parser, "expected statement");
6147 return;
6150 /* Everything else must be a declaration-statement or an
6151 expression-statement. Try for the declaration-statement
6152 first, unless we are looking at a `;', in which case we know that
6153 we have an expression-statement. */
6154 if (!statement)
6156 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6158 cp_parser_parse_tentatively (parser);
6159 /* Try to parse the declaration-statement. */
6160 cp_parser_declaration_statement (parser);
6161 /* If that worked, we're done. */
6162 if (cp_parser_parse_definitely (parser))
6163 return;
6165 /* Look for an expression-statement instead. */
6166 statement = cp_parser_expression_statement (parser, in_statement_expr);
6169 /* Set the line number for the statement. */
6170 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6171 SET_EXPR_LOCATION (statement, statement_location);
6174 /* Parse a labeled-statement.
6176 labeled-statement:
6177 identifier : statement
6178 case constant-expression : statement
6179 default : statement
6181 GNU Extension:
6183 labeled-statement:
6184 case constant-expression ... constant-expression : statement
6186 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6187 For an ordinary label, returns a LABEL_EXPR.
6189 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6190 inside a compound. */
6192 static tree
6193 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6194 bool in_compound)
6196 cp_token *token;
6197 tree statement = error_mark_node;
6199 /* The next token should be an identifier. */
6200 token = cp_lexer_peek_token (parser->lexer);
6201 if (token->type != CPP_NAME
6202 && token->type != CPP_KEYWORD)
6204 cp_parser_error (parser, "expected labeled-statement");
6205 return error_mark_node;
6208 switch (token->keyword)
6210 case RID_CASE:
6212 tree expr, expr_hi;
6213 cp_token *ellipsis;
6215 /* Consume the `case' token. */
6216 cp_lexer_consume_token (parser->lexer);
6217 /* Parse the constant-expression. */
6218 expr = cp_parser_constant_expression (parser,
6219 /*allow_non_constant_p=*/false,
6220 NULL);
6222 ellipsis = cp_lexer_peek_token (parser->lexer);
6223 if (ellipsis->type == CPP_ELLIPSIS)
6225 /* Consume the `...' token. */
6226 cp_lexer_consume_token (parser->lexer);
6227 expr_hi =
6228 cp_parser_constant_expression (parser,
6229 /*allow_non_constant_p=*/false,
6230 NULL);
6231 /* We don't need to emit warnings here, as the common code
6232 will do this for us. */
6234 else
6235 expr_hi = NULL_TREE;
6237 if (parser->in_switch_statement_p)
6238 statement = finish_case_label (expr, expr_hi);
6239 else
6240 error ("case label %qE not within a switch statement", expr);
6242 break;
6244 case RID_DEFAULT:
6245 /* Consume the `default' token. */
6246 cp_lexer_consume_token (parser->lexer);
6248 if (parser->in_switch_statement_p)
6249 statement = finish_case_label (NULL_TREE, NULL_TREE);
6250 else
6251 error ("case label not within a switch statement");
6252 break;
6254 default:
6255 /* Anything else must be an ordinary label. */
6256 statement = finish_label_stmt (cp_parser_identifier (parser));
6257 break;
6260 /* Require the `:' token. */
6261 cp_parser_require (parser, CPP_COLON, "`:'");
6262 /* Parse the labeled statement. */
6263 cp_parser_statement (parser, in_statement_expr, in_compound);
6265 /* Return the label, in the case of a `case' or `default' label. */
6266 return statement;
6269 /* Parse an expression-statement.
6271 expression-statement:
6272 expression [opt] ;
6274 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6275 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6276 indicates whether this expression-statement is part of an
6277 expression statement. */
6279 static tree
6280 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6282 tree statement = NULL_TREE;
6284 /* If the next token is a ';', then there is no expression
6285 statement. */
6286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6287 statement = cp_parser_expression (parser, /*cast_p=*/false);
6289 /* Consume the final `;'. */
6290 cp_parser_consume_semicolon_at_end_of_statement (parser);
6292 if (in_statement_expr
6293 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6294 /* This is the final expression statement of a statement
6295 expression. */
6296 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6297 else if (statement)
6298 statement = finish_expr_stmt (statement);
6299 else
6300 finish_stmt ();
6302 return statement;
6305 /* Parse a compound-statement.
6307 compound-statement:
6308 { statement-seq [opt] }
6310 Returns a tree representing the statement. */
6312 static tree
6313 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6314 bool in_try)
6316 tree compound_stmt;
6318 /* Consume the `{'. */
6319 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6320 return error_mark_node;
6321 /* Begin the compound-statement. */
6322 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6323 /* Parse an (optional) statement-seq. */
6324 cp_parser_statement_seq_opt (parser, in_statement_expr);
6325 /* Finish the compound-statement. */
6326 finish_compound_stmt (compound_stmt);
6327 /* Consume the `}'. */
6328 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6330 return compound_stmt;
6333 /* Parse an (optional) statement-seq.
6335 statement-seq:
6336 statement
6337 statement-seq [opt] statement */
6339 static void
6340 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6342 /* Scan statements until there aren't any more. */
6343 while (true)
6345 cp_token *token = cp_lexer_peek_token (parser->lexer);
6347 /* If we're looking at a `}', then we've run out of statements. */
6348 if (token->type == CPP_CLOSE_BRACE
6349 || token->type == CPP_EOF
6350 || token->type == CPP_PRAGMA_EOL)
6351 break;
6353 /* Parse the statement. */
6354 cp_parser_statement (parser, in_statement_expr, true);
6358 /* Parse a selection-statement.
6360 selection-statement:
6361 if ( condition ) statement
6362 if ( condition ) statement else statement
6363 switch ( condition ) statement
6365 Returns the new IF_STMT or SWITCH_STMT. */
6367 static tree
6368 cp_parser_selection_statement (cp_parser* parser)
6370 cp_token *token;
6371 enum rid keyword;
6373 /* Peek at the next token. */
6374 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6376 /* See what kind of keyword it is. */
6377 keyword = token->keyword;
6378 switch (keyword)
6380 case RID_IF:
6381 case RID_SWITCH:
6383 tree statement;
6384 tree condition;
6386 /* Look for the `('. */
6387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6389 cp_parser_skip_to_end_of_statement (parser);
6390 return error_mark_node;
6393 /* Begin the selection-statement. */
6394 if (keyword == RID_IF)
6395 statement = begin_if_stmt ();
6396 else
6397 statement = begin_switch_stmt ();
6399 /* Parse the condition. */
6400 condition = cp_parser_condition (parser);
6401 /* Look for the `)'. */
6402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6403 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6404 /*consume_paren=*/true);
6406 if (keyword == RID_IF)
6408 /* Add the condition. */
6409 finish_if_stmt_cond (condition, statement);
6411 /* Parse the then-clause. */
6412 cp_parser_implicitly_scoped_statement (parser);
6413 finish_then_clause (statement);
6415 /* If the next token is `else', parse the else-clause. */
6416 if (cp_lexer_next_token_is_keyword (parser->lexer,
6417 RID_ELSE))
6419 /* Consume the `else' keyword. */
6420 cp_lexer_consume_token (parser->lexer);
6421 begin_else_clause (statement);
6422 /* Parse the else-clause. */
6423 cp_parser_implicitly_scoped_statement (parser);
6424 finish_else_clause (statement);
6427 /* Now we're all done with the if-statement. */
6428 finish_if_stmt (statement);
6430 else
6432 bool in_switch_statement_p;
6433 unsigned char in_statement;
6435 /* Add the condition. */
6436 finish_switch_cond (condition, statement);
6438 /* Parse the body of the switch-statement. */
6439 in_switch_statement_p = parser->in_switch_statement_p;
6440 in_statement = parser->in_statement;
6441 parser->in_switch_statement_p = true;
6442 parser->in_statement |= IN_SWITCH_STMT;
6443 cp_parser_implicitly_scoped_statement (parser);
6444 parser->in_switch_statement_p = in_switch_statement_p;
6445 parser->in_statement = in_statement;
6447 /* Now we're all done with the switch-statement. */
6448 finish_switch_stmt (statement);
6451 return statement;
6453 break;
6455 default:
6456 cp_parser_error (parser, "expected selection-statement");
6457 return error_mark_node;
6461 /* Parse a condition.
6463 condition:
6464 expression
6465 type-specifier-seq declarator = assignment-expression
6467 GNU Extension:
6469 condition:
6470 type-specifier-seq declarator asm-specification [opt]
6471 attributes [opt] = assignment-expression
6473 Returns the expression that should be tested. */
6475 static tree
6476 cp_parser_condition (cp_parser* parser)
6478 cp_decl_specifier_seq type_specifiers;
6479 const char *saved_message;
6481 /* Try the declaration first. */
6482 cp_parser_parse_tentatively (parser);
6483 /* New types are not allowed in the type-specifier-seq for a
6484 condition. */
6485 saved_message = parser->type_definition_forbidden_message;
6486 parser->type_definition_forbidden_message
6487 = "types may not be defined in conditions";
6488 /* Parse the type-specifier-seq. */
6489 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6490 &type_specifiers);
6491 /* Restore the saved message. */
6492 parser->type_definition_forbidden_message = saved_message;
6493 /* If all is well, we might be looking at a declaration. */
6494 if (!cp_parser_error_occurred (parser))
6496 tree decl;
6497 tree asm_specification;
6498 tree attributes;
6499 cp_declarator *declarator;
6500 tree initializer = NULL_TREE;
6502 /* Parse the declarator. */
6503 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6504 /*ctor_dtor_or_conv_p=*/NULL,
6505 /*parenthesized_p=*/NULL,
6506 /*member_p=*/false);
6507 /* Parse the attributes. */
6508 attributes = cp_parser_attributes_opt (parser);
6509 /* Parse the asm-specification. */
6510 asm_specification = cp_parser_asm_specification_opt (parser);
6511 /* If the next token is not an `=', then we might still be
6512 looking at an expression. For example:
6514 if (A(a).x)
6516 looks like a decl-specifier-seq and a declarator -- but then
6517 there is no `=', so this is an expression. */
6518 cp_parser_require (parser, CPP_EQ, "`='");
6519 /* If we did see an `=', then we are looking at a declaration
6520 for sure. */
6521 if (cp_parser_parse_definitely (parser))
6523 tree pushed_scope;
6525 /* Create the declaration. */
6526 decl = start_decl (declarator, &type_specifiers,
6527 /*initialized_p=*/true,
6528 attributes, /*prefix_attributes=*/NULL_TREE,
6529 &pushed_scope);
6530 /* Parse the assignment-expression. */
6531 initializer = cp_parser_assignment_expression (parser,
6532 /*cast_p=*/false);
6534 /* Process the initializer. */
6535 cp_finish_decl (decl,
6536 initializer,
6537 asm_specification,
6538 LOOKUP_ONLYCONVERTING);
6540 if (pushed_scope)
6541 pop_scope (pushed_scope);
6543 return convert_from_reference (decl);
6546 /* If we didn't even get past the declarator successfully, we are
6547 definitely not looking at a declaration. */
6548 else
6549 cp_parser_abort_tentative_parse (parser);
6551 /* Otherwise, we are looking at an expression. */
6552 return cp_parser_expression (parser, /*cast_p=*/false);
6555 /* Parse an iteration-statement.
6557 iteration-statement:
6558 while ( condition ) statement
6559 do statement while ( expression ) ;
6560 for ( for-init-statement condition [opt] ; expression [opt] )
6561 statement
6563 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6565 static tree
6566 cp_parser_iteration_statement (cp_parser* parser)
6568 cp_token *token;
6569 enum rid keyword;
6570 tree statement;
6571 unsigned char in_statement;
6573 /* Peek at the next token. */
6574 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6575 if (!token)
6576 return error_mark_node;
6578 /* Remember whether or not we are already within an iteration
6579 statement. */
6580 in_statement = parser->in_statement;
6582 /* See what kind of keyword it is. */
6583 keyword = token->keyword;
6584 switch (keyword)
6586 case RID_WHILE:
6588 tree condition;
6590 /* Begin the while-statement. */
6591 statement = begin_while_stmt ();
6592 /* Look for the `('. */
6593 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6594 /* Parse the condition. */
6595 condition = cp_parser_condition (parser);
6596 finish_while_stmt_cond (condition, statement);
6597 /* Look for the `)'. */
6598 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6599 /* Parse the dependent statement. */
6600 parser->in_statement = IN_ITERATION_STMT;
6601 cp_parser_already_scoped_statement (parser);
6602 parser->in_statement = in_statement;
6603 /* We're done with the while-statement. */
6604 finish_while_stmt (statement);
6606 break;
6608 case RID_DO:
6610 tree expression;
6612 /* Begin the do-statement. */
6613 statement = begin_do_stmt ();
6614 /* Parse the body of the do-statement. */
6615 parser->in_statement = IN_ITERATION_STMT;
6616 cp_parser_implicitly_scoped_statement (parser);
6617 parser->in_statement = in_statement;
6618 finish_do_body (statement);
6619 /* Look for the `while' keyword. */
6620 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6621 /* Look for the `('. */
6622 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6623 /* Parse the expression. */
6624 expression = cp_parser_expression (parser, /*cast_p=*/false);
6625 /* We're done with the do-statement. */
6626 finish_do_stmt (expression, statement);
6627 /* Look for the `)'. */
6628 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6629 /* Look for the `;'. */
6630 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6632 break;
6634 case RID_FOR:
6636 tree condition = NULL_TREE;
6637 tree expression = NULL_TREE;
6639 /* Begin the for-statement. */
6640 statement = begin_for_stmt ();
6641 /* Look for the `('. */
6642 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6643 /* Parse the initialization. */
6644 cp_parser_for_init_statement (parser);
6645 finish_for_init_stmt (statement);
6647 /* If there's a condition, process it. */
6648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6649 condition = cp_parser_condition (parser);
6650 finish_for_cond (condition, statement);
6651 /* Look for the `;'. */
6652 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6654 /* If there's an expression, process it. */
6655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6656 expression = cp_parser_expression (parser, /*cast_p=*/false);
6657 finish_for_expr (expression, statement);
6658 /* Look for the `)'. */
6659 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6661 /* Parse the body of the for-statement. */
6662 parser->in_statement = IN_ITERATION_STMT;
6663 cp_parser_already_scoped_statement (parser);
6664 parser->in_statement = in_statement;
6666 /* We're done with the for-statement. */
6667 finish_for_stmt (statement);
6669 break;
6671 default:
6672 cp_parser_error (parser, "expected iteration-statement");
6673 statement = error_mark_node;
6674 break;
6677 return statement;
6680 /* Parse a for-init-statement.
6682 for-init-statement:
6683 expression-statement
6684 simple-declaration */
6686 static void
6687 cp_parser_for_init_statement (cp_parser* parser)
6689 /* If the next token is a `;', then we have an empty
6690 expression-statement. Grammatically, this is also a
6691 simple-declaration, but an invalid one, because it does not
6692 declare anything. Therefore, if we did not handle this case
6693 specially, we would issue an error message about an invalid
6694 declaration. */
6695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6697 /* We're going to speculatively look for a declaration, falling back
6698 to an expression, if necessary. */
6699 cp_parser_parse_tentatively (parser);
6700 /* Parse the declaration. */
6701 cp_parser_simple_declaration (parser,
6702 /*function_definition_allowed_p=*/false);
6703 /* If the tentative parse failed, then we shall need to look for an
6704 expression-statement. */
6705 if (cp_parser_parse_definitely (parser))
6706 return;
6709 cp_parser_expression_statement (parser, false);
6712 /* Parse a jump-statement.
6714 jump-statement:
6715 break ;
6716 continue ;
6717 return expression [opt] ;
6718 goto identifier ;
6720 GNU extension:
6722 jump-statement:
6723 goto * expression ;
6725 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6727 static tree
6728 cp_parser_jump_statement (cp_parser* parser)
6730 tree statement = error_mark_node;
6731 cp_token *token;
6732 enum rid keyword;
6734 /* Peek at the next token. */
6735 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6736 if (!token)
6737 return error_mark_node;
6739 /* See what kind of keyword it is. */
6740 keyword = token->keyword;
6741 switch (keyword)
6743 case RID_BREAK:
6744 switch (parser->in_statement)
6746 case 0:
6747 error ("break statement not within loop or switch");
6748 break;
6749 default:
6750 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6751 || parser->in_statement == IN_ITERATION_STMT);
6752 statement = finish_break_stmt ();
6753 break;
6754 case IN_OMP_BLOCK:
6755 error ("invalid exit from OpenMP structured block");
6756 break;
6757 case IN_OMP_FOR:
6758 error ("break statement used with OpenMP for loop");
6759 break;
6761 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6762 break;
6764 case RID_CONTINUE:
6765 switch (parser->in_statement & ~IN_SWITCH_STMT)
6767 case 0:
6768 error ("continue statement not within a loop");
6769 break;
6770 case IN_ITERATION_STMT:
6771 case IN_OMP_FOR:
6772 statement = finish_continue_stmt ();
6773 break;
6774 case IN_OMP_BLOCK:
6775 error ("invalid exit from OpenMP structured block");
6776 break;
6777 default:
6778 gcc_unreachable ();
6780 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6781 break;
6783 case RID_RETURN:
6785 tree expr;
6787 /* If the next token is a `;', then there is no
6788 expression. */
6789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6790 expr = cp_parser_expression (parser, /*cast_p=*/false);
6791 else
6792 expr = NULL_TREE;
6793 /* Build the return-statement. */
6794 statement = finish_return_stmt (expr);
6795 /* Look for the final `;'. */
6796 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6798 break;
6800 case RID_GOTO:
6801 /* Create the goto-statement. */
6802 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6804 /* Issue a warning about this use of a GNU extension. */
6805 if (pedantic)
6806 pedwarn ("ISO C++ forbids computed gotos");
6807 /* Consume the '*' token. */
6808 cp_lexer_consume_token (parser->lexer);
6809 /* Parse the dependent expression. */
6810 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6812 else
6813 finish_goto_stmt (cp_parser_identifier (parser));
6814 /* Look for the final `;'. */
6815 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6816 break;
6818 default:
6819 cp_parser_error (parser, "expected jump-statement");
6820 break;
6823 return statement;
6826 /* Parse a declaration-statement.
6828 declaration-statement:
6829 block-declaration */
6831 static void
6832 cp_parser_declaration_statement (cp_parser* parser)
6834 void *p;
6836 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6837 p = obstack_alloc (&declarator_obstack, 0);
6839 /* Parse the block-declaration. */
6840 cp_parser_block_declaration (parser, /*statement_p=*/true);
6842 /* Free any declarators allocated. */
6843 obstack_free (&declarator_obstack, p);
6845 /* Finish off the statement. */
6846 finish_stmt ();
6849 /* Some dependent statements (like `if (cond) statement'), are
6850 implicitly in their own scope. In other words, if the statement is
6851 a single statement (as opposed to a compound-statement), it is
6852 none-the-less treated as if it were enclosed in braces. Any
6853 declarations appearing in the dependent statement are out of scope
6854 after control passes that point. This function parses a statement,
6855 but ensures that is in its own scope, even if it is not a
6856 compound-statement.
6858 Returns the new statement. */
6860 static tree
6861 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6863 tree statement;
6865 /* Mark if () ; with a special NOP_EXPR. */
6866 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6868 cp_lexer_consume_token (parser->lexer);
6869 statement = add_stmt (build_empty_stmt ());
6871 /* if a compound is opened, we simply parse the statement directly. */
6872 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6873 statement = cp_parser_compound_statement (parser, NULL, false);
6874 /* If the token is not a `{', then we must take special action. */
6875 else
6877 /* Create a compound-statement. */
6878 statement = begin_compound_stmt (0);
6879 /* Parse the dependent-statement. */
6880 cp_parser_statement (parser, NULL_TREE, false);
6881 /* Finish the dummy compound-statement. */
6882 finish_compound_stmt (statement);
6885 /* Return the statement. */
6886 return statement;
6889 /* For some dependent statements (like `while (cond) statement'), we
6890 have already created a scope. Therefore, even if the dependent
6891 statement is a compound-statement, we do not want to create another
6892 scope. */
6894 static void
6895 cp_parser_already_scoped_statement (cp_parser* parser)
6897 /* If the token is a `{', then we must take special action. */
6898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6899 cp_parser_statement (parser, NULL_TREE, false);
6900 else
6902 /* Avoid calling cp_parser_compound_statement, so that we
6903 don't create a new scope. Do everything else by hand. */
6904 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6905 cp_parser_statement_seq_opt (parser, NULL_TREE);
6906 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6910 /* Declarations [gram.dcl.dcl] */
6912 /* Parse an optional declaration-sequence.
6914 declaration-seq:
6915 declaration
6916 declaration-seq declaration */
6918 static void
6919 cp_parser_declaration_seq_opt (cp_parser* parser)
6921 while (true)
6923 cp_token *token;
6925 token = cp_lexer_peek_token (parser->lexer);
6927 if (token->type == CPP_CLOSE_BRACE
6928 || token->type == CPP_EOF
6929 || token->type == CPP_PRAGMA_EOL)
6930 break;
6932 if (token->type == CPP_SEMICOLON)
6934 /* A declaration consisting of a single semicolon is
6935 invalid. Allow it unless we're being pedantic. */
6936 cp_lexer_consume_token (parser->lexer);
6937 if (pedantic && !in_system_header)
6938 pedwarn ("extra %<;%>");
6939 continue;
6942 /* If we're entering or exiting a region that's implicitly
6943 extern "C", modify the lang context appropriately. */
6944 if (!parser->implicit_extern_c && token->implicit_extern_c)
6946 push_lang_context (lang_name_c);
6947 parser->implicit_extern_c = true;
6949 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6951 pop_lang_context ();
6952 parser->implicit_extern_c = false;
6955 if (token->type == CPP_PRAGMA)
6957 /* A top-level declaration can consist solely of a #pragma.
6958 A nested declaration cannot, so this is done here and not
6959 in cp_parser_declaration. (A #pragma at block scope is
6960 handled in cp_parser_statement.) */
6961 cp_parser_pragma (parser, pragma_external);
6962 continue;
6965 /* Parse the declaration itself. */
6966 cp_parser_declaration (parser);
6970 /* Parse a declaration.
6972 declaration:
6973 block-declaration
6974 function-definition
6975 template-declaration
6976 explicit-instantiation
6977 explicit-specialization
6978 linkage-specification
6979 namespace-definition
6981 GNU extension:
6983 declaration:
6984 __extension__ declaration */
6986 static void
6987 cp_parser_declaration (cp_parser* parser)
6989 cp_token token1;
6990 cp_token token2;
6991 int saved_pedantic;
6992 void *p;
6994 /* Check for the `__extension__' keyword. */
6995 if (cp_parser_extension_opt (parser, &saved_pedantic))
6997 /* Parse the qualified declaration. */
6998 cp_parser_declaration (parser);
6999 /* Restore the PEDANTIC flag. */
7000 pedantic = saved_pedantic;
7002 return;
7005 /* Try to figure out what kind of declaration is present. */
7006 token1 = *cp_lexer_peek_token (parser->lexer);
7008 if (token1.type != CPP_EOF)
7009 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7010 else
7012 token2.type = CPP_EOF;
7013 token2.keyword = RID_MAX;
7016 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7017 p = obstack_alloc (&declarator_obstack, 0);
7019 /* If the next token is `extern' and the following token is a string
7020 literal, then we have a linkage specification. */
7021 if (token1.keyword == RID_EXTERN
7022 && cp_parser_is_string_literal (&token2))
7023 cp_parser_linkage_specification (parser);
7024 /* If the next token is `template', then we have either a template
7025 declaration, an explicit instantiation, or an explicit
7026 specialization. */
7027 else if (token1.keyword == RID_TEMPLATE)
7029 /* `template <>' indicates a template specialization. */
7030 if (token2.type == CPP_LESS
7031 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7032 cp_parser_explicit_specialization (parser);
7033 /* `template <' indicates a template declaration. */
7034 else if (token2.type == CPP_LESS)
7035 cp_parser_template_declaration (parser, /*member_p=*/false);
7036 /* Anything else must be an explicit instantiation. */
7037 else
7038 cp_parser_explicit_instantiation (parser);
7040 /* If the next token is `export', then we have a template
7041 declaration. */
7042 else if (token1.keyword == RID_EXPORT)
7043 cp_parser_template_declaration (parser, /*member_p=*/false);
7044 /* If the next token is `extern', 'static' or 'inline' and the one
7045 after that is `template', we have a GNU extended explicit
7046 instantiation directive. */
7047 else if (cp_parser_allow_gnu_extensions_p (parser)
7048 && (token1.keyword == RID_EXTERN
7049 || token1.keyword == RID_STATIC
7050 || token1.keyword == RID_INLINE)
7051 && token2.keyword == RID_TEMPLATE)
7052 cp_parser_explicit_instantiation (parser);
7053 /* If the next token is `namespace', check for a named or unnamed
7054 namespace definition. */
7055 else if (token1.keyword == RID_NAMESPACE
7056 && (/* A named namespace definition. */
7057 (token2.type == CPP_NAME
7058 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7059 == CPP_OPEN_BRACE))
7060 /* An unnamed namespace definition. */
7061 || token2.type == CPP_OPEN_BRACE))
7062 cp_parser_namespace_definition (parser);
7063 /* Objective-C++ declaration/definition. */
7064 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7065 cp_parser_objc_declaration (parser);
7066 /* We must have either a block declaration or a function
7067 definition. */
7068 else
7069 /* Try to parse a block-declaration, or a function-definition. */
7070 cp_parser_block_declaration (parser, /*statement_p=*/false);
7072 /* Free any declarators allocated. */
7073 obstack_free (&declarator_obstack, p);
7076 /* Parse a block-declaration.
7078 block-declaration:
7079 simple-declaration
7080 asm-definition
7081 namespace-alias-definition
7082 using-declaration
7083 using-directive
7085 GNU Extension:
7087 block-declaration:
7088 __extension__ block-declaration
7089 label-declaration
7091 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7092 part of a declaration-statement. */
7094 static void
7095 cp_parser_block_declaration (cp_parser *parser,
7096 bool statement_p)
7098 cp_token *token1;
7099 int saved_pedantic;
7101 /* Check for the `__extension__' keyword. */
7102 if (cp_parser_extension_opt (parser, &saved_pedantic))
7104 /* Parse the qualified declaration. */
7105 cp_parser_block_declaration (parser, statement_p);
7106 /* Restore the PEDANTIC flag. */
7107 pedantic = saved_pedantic;
7109 return;
7112 /* Peek at the next token to figure out which kind of declaration is
7113 present. */
7114 token1 = cp_lexer_peek_token (parser->lexer);
7116 /* If the next keyword is `asm', we have an asm-definition. */
7117 if (token1->keyword == RID_ASM)
7119 if (statement_p)
7120 cp_parser_commit_to_tentative_parse (parser);
7121 cp_parser_asm_definition (parser);
7123 /* If the next keyword is `namespace', we have a
7124 namespace-alias-definition. */
7125 else if (token1->keyword == RID_NAMESPACE)
7126 cp_parser_namespace_alias_definition (parser);
7127 /* If the next keyword is `using', we have either a
7128 using-declaration or a using-directive. */
7129 else if (token1->keyword == RID_USING)
7131 cp_token *token2;
7133 if (statement_p)
7134 cp_parser_commit_to_tentative_parse (parser);
7135 /* If the token after `using' is `namespace', then we have a
7136 using-directive. */
7137 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7138 if (token2->keyword == RID_NAMESPACE)
7139 cp_parser_using_directive (parser);
7140 /* Otherwise, it's a using-declaration. */
7141 else
7142 cp_parser_using_declaration (parser);
7144 /* If the next keyword is `__label__' we have a label declaration. */
7145 else if (token1->keyword == RID_LABEL)
7147 if (statement_p)
7148 cp_parser_commit_to_tentative_parse (parser);
7149 cp_parser_label_declaration (parser);
7151 /* Anything else must be a simple-declaration. */
7152 else
7153 cp_parser_simple_declaration (parser, !statement_p);
7156 /* Parse a simple-declaration.
7158 simple-declaration:
7159 decl-specifier-seq [opt] init-declarator-list [opt] ;
7161 init-declarator-list:
7162 init-declarator
7163 init-declarator-list , init-declarator
7165 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7166 function-definition as a simple-declaration. */
7168 static void
7169 cp_parser_simple_declaration (cp_parser* parser,
7170 bool function_definition_allowed_p)
7172 cp_decl_specifier_seq decl_specifiers;
7173 int declares_class_or_enum;
7174 bool saw_declarator;
7176 /* Defer access checks until we know what is being declared; the
7177 checks for names appearing in the decl-specifier-seq should be
7178 done as if we were in the scope of the thing being declared. */
7179 push_deferring_access_checks (dk_deferred);
7181 /* Parse the decl-specifier-seq. We have to keep track of whether
7182 or not the decl-specifier-seq declares a named class or
7183 enumeration type, since that is the only case in which the
7184 init-declarator-list is allowed to be empty.
7186 [dcl.dcl]
7188 In a simple-declaration, the optional init-declarator-list can be
7189 omitted only when declaring a class or enumeration, that is when
7190 the decl-specifier-seq contains either a class-specifier, an
7191 elaborated-type-specifier, or an enum-specifier. */
7192 cp_parser_decl_specifier_seq (parser,
7193 CP_PARSER_FLAGS_OPTIONAL,
7194 &decl_specifiers,
7195 &declares_class_or_enum);
7196 /* We no longer need to defer access checks. */
7197 stop_deferring_access_checks ();
7199 /* In a block scope, a valid declaration must always have a
7200 decl-specifier-seq. By not trying to parse declarators, we can
7201 resolve the declaration/expression ambiguity more quickly. */
7202 if (!function_definition_allowed_p
7203 && !decl_specifiers.any_specifiers_p)
7205 cp_parser_error (parser, "expected declaration");
7206 goto done;
7209 /* If the next two tokens are both identifiers, the code is
7210 erroneous. The usual cause of this situation is code like:
7212 T t;
7214 where "T" should name a type -- but does not. */
7215 if (!decl_specifiers.type
7216 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7218 /* If parsing tentatively, we should commit; we really are
7219 looking at a declaration. */
7220 cp_parser_commit_to_tentative_parse (parser);
7221 /* Give up. */
7222 goto done;
7225 /* If we have seen at least one decl-specifier, and the next token
7226 is not a parenthesis, then we must be looking at a declaration.
7227 (After "int (" we might be looking at a functional cast.) */
7228 if (decl_specifiers.any_specifiers_p
7229 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7230 cp_parser_commit_to_tentative_parse (parser);
7232 /* Keep going until we hit the `;' at the end of the simple
7233 declaration. */
7234 saw_declarator = false;
7235 while (cp_lexer_next_token_is_not (parser->lexer,
7236 CPP_SEMICOLON))
7238 cp_token *token;
7239 bool function_definition_p;
7240 tree decl;
7242 if (saw_declarator)
7244 /* If we are processing next declarator, coma is expected */
7245 token = cp_lexer_peek_token (parser->lexer);
7246 gcc_assert (token->type == CPP_COMMA);
7247 cp_lexer_consume_token (parser->lexer);
7249 else
7250 saw_declarator = true;
7252 /* Parse the init-declarator. */
7253 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7254 function_definition_allowed_p,
7255 /*member_p=*/false,
7256 declares_class_or_enum,
7257 &function_definition_p);
7258 /* If an error occurred while parsing tentatively, exit quickly.
7259 (That usually happens when in the body of a function; each
7260 statement is treated as a declaration-statement until proven
7261 otherwise.) */
7262 if (cp_parser_error_occurred (parser))
7263 goto done;
7264 /* Handle function definitions specially. */
7265 if (function_definition_p)
7267 /* If the next token is a `,', then we are probably
7268 processing something like:
7270 void f() {}, *p;
7272 which is erroneous. */
7273 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7274 error ("mixing declarations and function-definitions is forbidden");
7275 /* Otherwise, we're done with the list of declarators. */
7276 else
7278 pop_deferring_access_checks ();
7279 return;
7282 /* The next token should be either a `,' or a `;'. */
7283 token = cp_lexer_peek_token (parser->lexer);
7284 /* If it's a `,', there are more declarators to come. */
7285 if (token->type == CPP_COMMA)
7286 /* will be consumed next time around */;
7287 /* If it's a `;', we are done. */
7288 else if (token->type == CPP_SEMICOLON)
7289 break;
7290 /* Anything else is an error. */
7291 else
7293 /* If we have already issued an error message we don't need
7294 to issue another one. */
7295 if (decl != error_mark_node
7296 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7297 cp_parser_error (parser, "expected %<,%> or %<;%>");
7298 /* Skip tokens until we reach the end of the statement. */
7299 cp_parser_skip_to_end_of_statement (parser);
7300 /* If the next token is now a `;', consume it. */
7301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7302 cp_lexer_consume_token (parser->lexer);
7303 goto done;
7305 /* After the first time around, a function-definition is not
7306 allowed -- even if it was OK at first. For example:
7308 int i, f() {}
7310 is not valid. */
7311 function_definition_allowed_p = false;
7314 /* Issue an error message if no declarators are present, and the
7315 decl-specifier-seq does not itself declare a class or
7316 enumeration. */
7317 if (!saw_declarator)
7319 if (cp_parser_declares_only_class_p (parser))
7320 shadow_tag (&decl_specifiers);
7321 /* Perform any deferred access checks. */
7322 perform_deferred_access_checks ();
7325 /* Consume the `;'. */
7326 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7328 done:
7329 pop_deferring_access_checks ();
7332 /* Parse a decl-specifier-seq.
7334 decl-specifier-seq:
7335 decl-specifier-seq [opt] decl-specifier
7337 decl-specifier:
7338 storage-class-specifier
7339 type-specifier
7340 function-specifier
7341 friend
7342 typedef
7344 GNU Extension:
7346 decl-specifier:
7347 attributes
7349 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7351 The parser flags FLAGS is used to control type-specifier parsing.
7353 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7354 flags:
7356 1: one of the decl-specifiers is an elaborated-type-specifier
7357 (i.e., a type declaration)
7358 2: one of the decl-specifiers is an enum-specifier or a
7359 class-specifier (i.e., a type definition)
7363 static void
7364 cp_parser_decl_specifier_seq (cp_parser* parser,
7365 cp_parser_flags flags,
7366 cp_decl_specifier_seq *decl_specs,
7367 int* declares_class_or_enum)
7369 bool constructor_possible_p = !parser->in_declarator_p;
7370 cp_decl_spec ds;
7372 /* Clear DECL_SPECS. */
7373 clear_decl_specs (decl_specs);
7375 /* Assume no class or enumeration type is declared. */
7376 *declares_class_or_enum = 0;
7378 /* Keep reading specifiers until there are no more to read. */
7379 while (true)
7381 bool constructor_p;
7382 bool found_decl_spec;
7383 cp_token *token;
7385 /* Peek at the next token. */
7386 token = cp_lexer_peek_token (parser->lexer);
7387 /* Handle attributes. */
7388 if (token->keyword == RID_ATTRIBUTE)
7390 /* Parse the attributes. */
7391 decl_specs->attributes
7392 = chainon (decl_specs->attributes,
7393 cp_parser_attributes_opt (parser));
7394 continue;
7396 /* Assume we will find a decl-specifier keyword. */
7397 found_decl_spec = true;
7398 /* If the next token is an appropriate keyword, we can simply
7399 add it to the list. */
7400 switch (token->keyword)
7402 /* decl-specifier:
7403 friend */
7404 case RID_FRIEND:
7405 ++decl_specs->specs[(int) ds_friend];
7406 /* Consume the token. */
7407 cp_lexer_consume_token (parser->lexer);
7408 break;
7410 /* function-specifier:
7411 inline
7412 virtual
7413 explicit */
7414 case RID_INLINE:
7415 case RID_VIRTUAL:
7416 case RID_EXPLICIT:
7417 cp_parser_function_specifier_opt (parser, decl_specs);
7418 break;
7420 /* decl-specifier:
7421 typedef */
7422 case RID_TYPEDEF:
7423 ++decl_specs->specs[(int) ds_typedef];
7424 /* Consume the token. */
7425 cp_lexer_consume_token (parser->lexer);
7426 /* A constructor declarator cannot appear in a typedef. */
7427 constructor_possible_p = false;
7428 /* The "typedef" keyword can only occur in a declaration; we
7429 may as well commit at this point. */
7430 cp_parser_commit_to_tentative_parse (parser);
7431 break;
7433 /* storage-class-specifier:
7434 auto
7435 register
7436 static
7437 extern
7438 mutable
7440 GNU Extension:
7441 thread */
7442 case RID_AUTO:
7443 /* Consume the token. */
7444 cp_lexer_consume_token (parser->lexer);
7445 cp_parser_set_storage_class (decl_specs, sc_auto);
7446 break;
7447 case RID_REGISTER:
7448 /* Consume the token. */
7449 cp_lexer_consume_token (parser->lexer);
7450 cp_parser_set_storage_class (decl_specs, sc_register);
7451 break;
7452 case RID_STATIC:
7453 /* Consume the token. */
7454 cp_lexer_consume_token (parser->lexer);
7455 if (decl_specs->specs[(int) ds_thread])
7457 error ("%<__thread%> before %<static%>");
7458 decl_specs->specs[(int) ds_thread] = 0;
7460 cp_parser_set_storage_class (decl_specs, sc_static);
7461 break;
7462 case RID_EXTERN:
7463 /* Consume the token. */
7464 cp_lexer_consume_token (parser->lexer);
7465 if (decl_specs->specs[(int) ds_thread])
7467 error ("%<__thread%> before %<extern%>");
7468 decl_specs->specs[(int) ds_thread] = 0;
7470 cp_parser_set_storage_class (decl_specs, sc_extern);
7471 break;
7472 case RID_MUTABLE:
7473 /* Consume the token. */
7474 cp_lexer_consume_token (parser->lexer);
7475 cp_parser_set_storage_class (decl_specs, sc_mutable);
7476 break;
7477 case RID_THREAD:
7478 /* Consume the token. */
7479 cp_lexer_consume_token (parser->lexer);
7480 ++decl_specs->specs[(int) ds_thread];
7481 break;
7483 default:
7484 /* We did not yet find a decl-specifier yet. */
7485 found_decl_spec = false;
7486 break;
7489 /* Constructors are a special case. The `S' in `S()' is not a
7490 decl-specifier; it is the beginning of the declarator. */
7491 constructor_p
7492 = (!found_decl_spec
7493 && constructor_possible_p
7494 && (cp_parser_constructor_declarator_p
7495 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7497 /* If we don't have a DECL_SPEC yet, then we must be looking at
7498 a type-specifier. */
7499 if (!found_decl_spec && !constructor_p)
7501 int decl_spec_declares_class_or_enum;
7502 bool is_cv_qualifier;
7503 tree type_spec;
7505 type_spec
7506 = cp_parser_type_specifier (parser, flags,
7507 decl_specs,
7508 /*is_declaration=*/true,
7509 &decl_spec_declares_class_or_enum,
7510 &is_cv_qualifier);
7512 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7514 /* If this type-specifier referenced a user-defined type
7515 (a typedef, class-name, etc.), then we can't allow any
7516 more such type-specifiers henceforth.
7518 [dcl.spec]
7520 The longest sequence of decl-specifiers that could
7521 possibly be a type name is taken as the
7522 decl-specifier-seq of a declaration. The sequence shall
7523 be self-consistent as described below.
7525 [dcl.type]
7527 As a general rule, at most one type-specifier is allowed
7528 in the complete decl-specifier-seq of a declaration. The
7529 only exceptions are the following:
7531 -- const or volatile can be combined with any other
7532 type-specifier.
7534 -- signed or unsigned can be combined with char, long,
7535 short, or int.
7537 -- ..
7539 Example:
7541 typedef char* Pc;
7542 void g (const int Pc);
7544 Here, Pc is *not* part of the decl-specifier seq; it's
7545 the declarator. Therefore, once we see a type-specifier
7546 (other than a cv-qualifier), we forbid any additional
7547 user-defined types. We *do* still allow things like `int
7548 int' to be considered a decl-specifier-seq, and issue the
7549 error message later. */
7550 if (type_spec && !is_cv_qualifier)
7551 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7552 /* A constructor declarator cannot follow a type-specifier. */
7553 if (type_spec)
7555 constructor_possible_p = false;
7556 found_decl_spec = true;
7560 /* If we still do not have a DECL_SPEC, then there are no more
7561 decl-specifiers. */
7562 if (!found_decl_spec)
7563 break;
7565 decl_specs->any_specifiers_p = true;
7566 /* After we see one decl-specifier, further decl-specifiers are
7567 always optional. */
7568 flags |= CP_PARSER_FLAGS_OPTIONAL;
7571 /* Check for repeated decl-specifiers. */
7572 for (ds = ds_first; ds != ds_last; ++ds)
7574 unsigned count = decl_specs->specs[(int)ds];
7575 if (count < 2)
7576 continue;
7577 /* The "long" specifier is a special case because of "long long". */
7578 if (ds == ds_long)
7580 if (count > 2)
7581 error ("%<long long long%> is too long for GCC");
7582 else if (pedantic && !in_system_header && warn_long_long)
7583 pedwarn ("ISO C++ does not support %<long long%>");
7585 else if (count > 1)
7587 static const char *const decl_spec_names[] = {
7588 "signed",
7589 "unsigned",
7590 "short",
7591 "long",
7592 "const",
7593 "volatile",
7594 "restrict",
7595 "inline",
7596 "virtual",
7597 "explicit",
7598 "friend",
7599 "typedef",
7600 "__complex",
7601 "__thread"
7603 error ("duplicate %qs", decl_spec_names[(int)ds]);
7607 /* Don't allow a friend specifier with a class definition. */
7608 if (decl_specs->specs[(int) ds_friend] != 0
7609 && (*declares_class_or_enum & 2))
7610 error ("class definition may not be declared a friend");
7613 /* Parse an (optional) storage-class-specifier.
7615 storage-class-specifier:
7616 auto
7617 register
7618 static
7619 extern
7620 mutable
7622 GNU Extension:
7624 storage-class-specifier:
7625 thread
7627 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7629 static tree
7630 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7632 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7634 case RID_AUTO:
7635 case RID_REGISTER:
7636 case RID_STATIC:
7637 case RID_EXTERN:
7638 case RID_MUTABLE:
7639 case RID_THREAD:
7640 /* Consume the token. */
7641 return cp_lexer_consume_token (parser->lexer)->value;
7643 default:
7644 return NULL_TREE;
7648 /* Parse an (optional) function-specifier.
7650 function-specifier:
7651 inline
7652 virtual
7653 explicit
7655 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7656 Updates DECL_SPECS, if it is non-NULL. */
7658 static tree
7659 cp_parser_function_specifier_opt (cp_parser* parser,
7660 cp_decl_specifier_seq *decl_specs)
7662 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7664 case RID_INLINE:
7665 if (decl_specs)
7666 ++decl_specs->specs[(int) ds_inline];
7667 break;
7669 case RID_VIRTUAL:
7670 if (decl_specs)
7671 ++decl_specs->specs[(int) ds_virtual];
7672 break;
7674 case RID_EXPLICIT:
7675 if (decl_specs)
7676 ++decl_specs->specs[(int) ds_explicit];
7677 break;
7679 default:
7680 return NULL_TREE;
7683 /* Consume the token. */
7684 return cp_lexer_consume_token (parser->lexer)->value;
7687 /* Parse a linkage-specification.
7689 linkage-specification:
7690 extern string-literal { declaration-seq [opt] }
7691 extern string-literal declaration */
7693 static void
7694 cp_parser_linkage_specification (cp_parser* parser)
7696 tree linkage;
7698 /* Look for the `extern' keyword. */
7699 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7701 /* Look for the string-literal. */
7702 linkage = cp_parser_string_literal (parser, false, false);
7704 /* Transform the literal into an identifier. If the literal is a
7705 wide-character string, or contains embedded NULs, then we can't
7706 handle it as the user wants. */
7707 if (strlen (TREE_STRING_POINTER (linkage))
7708 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7710 cp_parser_error (parser, "invalid linkage-specification");
7711 /* Assume C++ linkage. */
7712 linkage = lang_name_cplusplus;
7714 else
7715 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7717 /* We're now using the new linkage. */
7718 push_lang_context (linkage);
7720 /* If the next token is a `{', then we're using the first
7721 production. */
7722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7724 /* Consume the `{' token. */
7725 cp_lexer_consume_token (parser->lexer);
7726 /* Parse the declarations. */
7727 cp_parser_declaration_seq_opt (parser);
7728 /* Look for the closing `}'. */
7729 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7731 /* Otherwise, there's just one declaration. */
7732 else
7734 bool saved_in_unbraced_linkage_specification_p;
7736 saved_in_unbraced_linkage_specification_p
7737 = parser->in_unbraced_linkage_specification_p;
7738 parser->in_unbraced_linkage_specification_p = true;
7739 have_extern_spec = true;
7740 cp_parser_declaration (parser);
7741 have_extern_spec = false;
7742 parser->in_unbraced_linkage_specification_p
7743 = saved_in_unbraced_linkage_specification_p;
7746 /* We're done with the linkage-specification. */
7747 pop_lang_context ();
7750 /* Special member functions [gram.special] */
7752 /* Parse a conversion-function-id.
7754 conversion-function-id:
7755 operator conversion-type-id
7757 Returns an IDENTIFIER_NODE representing the operator. */
7759 static tree
7760 cp_parser_conversion_function_id (cp_parser* parser)
7762 tree type;
7763 tree saved_scope;
7764 tree saved_qualifying_scope;
7765 tree saved_object_scope;
7766 tree pushed_scope = NULL_TREE;
7768 /* Look for the `operator' token. */
7769 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7770 return error_mark_node;
7771 /* When we parse the conversion-type-id, the current scope will be
7772 reset. However, we need that information in able to look up the
7773 conversion function later, so we save it here. */
7774 saved_scope = parser->scope;
7775 saved_qualifying_scope = parser->qualifying_scope;
7776 saved_object_scope = parser->object_scope;
7777 /* We must enter the scope of the class so that the names of
7778 entities declared within the class are available in the
7779 conversion-type-id. For example, consider:
7781 struct S {
7782 typedef int I;
7783 operator I();
7786 S::operator I() { ... }
7788 In order to see that `I' is a type-name in the definition, we
7789 must be in the scope of `S'. */
7790 if (saved_scope)
7791 pushed_scope = push_scope (saved_scope);
7792 /* Parse the conversion-type-id. */
7793 type = cp_parser_conversion_type_id (parser);
7794 /* Leave the scope of the class, if any. */
7795 if (pushed_scope)
7796 pop_scope (pushed_scope);
7797 /* Restore the saved scope. */
7798 parser->scope = saved_scope;
7799 parser->qualifying_scope = saved_qualifying_scope;
7800 parser->object_scope = saved_object_scope;
7801 /* If the TYPE is invalid, indicate failure. */
7802 if (type == error_mark_node)
7803 return error_mark_node;
7804 return mangle_conv_op_name_for_type (type);
7807 /* Parse a conversion-type-id:
7809 conversion-type-id:
7810 type-specifier-seq conversion-declarator [opt]
7812 Returns the TYPE specified. */
7814 static tree
7815 cp_parser_conversion_type_id (cp_parser* parser)
7817 tree attributes;
7818 cp_decl_specifier_seq type_specifiers;
7819 cp_declarator *declarator;
7820 tree type_specified;
7822 /* Parse the attributes. */
7823 attributes = cp_parser_attributes_opt (parser);
7824 /* Parse the type-specifiers. */
7825 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7826 &type_specifiers);
7827 /* If that didn't work, stop. */
7828 if (type_specifiers.type == error_mark_node)
7829 return error_mark_node;
7830 /* Parse the conversion-declarator. */
7831 declarator = cp_parser_conversion_declarator_opt (parser);
7833 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7834 /*initialized=*/0, &attributes);
7835 if (attributes)
7836 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7837 return type_specified;
7840 /* Parse an (optional) conversion-declarator.
7842 conversion-declarator:
7843 ptr-operator conversion-declarator [opt]
7847 static cp_declarator *
7848 cp_parser_conversion_declarator_opt (cp_parser* parser)
7850 enum tree_code code;
7851 tree class_type;
7852 cp_cv_quals cv_quals;
7854 /* We don't know if there's a ptr-operator next, or not. */
7855 cp_parser_parse_tentatively (parser);
7856 /* Try the ptr-operator. */
7857 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7858 /* If it worked, look for more conversion-declarators. */
7859 if (cp_parser_parse_definitely (parser))
7861 cp_declarator *declarator;
7863 /* Parse another optional declarator. */
7864 declarator = cp_parser_conversion_declarator_opt (parser);
7866 /* Create the representation of the declarator. */
7867 if (class_type)
7868 declarator = make_ptrmem_declarator (cv_quals, class_type,
7869 declarator);
7870 else if (code == INDIRECT_REF)
7871 declarator = make_pointer_declarator (cv_quals, declarator);
7872 else
7873 declarator = make_reference_declarator (cv_quals, declarator);
7875 return declarator;
7878 return NULL;
7881 /* Parse an (optional) ctor-initializer.
7883 ctor-initializer:
7884 : mem-initializer-list
7886 Returns TRUE iff the ctor-initializer was actually present. */
7888 static bool
7889 cp_parser_ctor_initializer_opt (cp_parser* parser)
7891 /* If the next token is not a `:', then there is no
7892 ctor-initializer. */
7893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7895 /* Do default initialization of any bases and members. */
7896 if (DECL_CONSTRUCTOR_P (current_function_decl))
7897 finish_mem_initializers (NULL_TREE);
7899 return false;
7902 /* Consume the `:' token. */
7903 cp_lexer_consume_token (parser->lexer);
7904 /* And the mem-initializer-list. */
7905 cp_parser_mem_initializer_list (parser);
7907 return true;
7910 /* Parse a mem-initializer-list.
7912 mem-initializer-list:
7913 mem-initializer
7914 mem-initializer , mem-initializer-list */
7916 static void
7917 cp_parser_mem_initializer_list (cp_parser* parser)
7919 tree mem_initializer_list = NULL_TREE;
7921 /* Let the semantic analysis code know that we are starting the
7922 mem-initializer-list. */
7923 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7924 error ("only constructors take base initializers");
7926 /* Loop through the list. */
7927 while (true)
7929 tree mem_initializer;
7931 /* Parse the mem-initializer. */
7932 mem_initializer = cp_parser_mem_initializer (parser);
7933 /* Add it to the list, unless it was erroneous. */
7934 if (mem_initializer != error_mark_node)
7936 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7937 mem_initializer_list = mem_initializer;
7939 /* If the next token is not a `,', we're done. */
7940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7941 break;
7942 /* Consume the `,' token. */
7943 cp_lexer_consume_token (parser->lexer);
7946 /* Perform semantic analysis. */
7947 if (DECL_CONSTRUCTOR_P (current_function_decl))
7948 finish_mem_initializers (mem_initializer_list);
7951 /* Parse a mem-initializer.
7953 mem-initializer:
7954 mem-initializer-id ( expression-list [opt] )
7956 GNU extension:
7958 mem-initializer:
7959 ( expression-list [opt] )
7961 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7962 class) or FIELD_DECL (for a non-static data member) to initialize;
7963 the TREE_VALUE is the expression-list. An empty initialization
7964 list is represented by void_list_node. */
7966 static tree
7967 cp_parser_mem_initializer (cp_parser* parser)
7969 tree mem_initializer_id;
7970 tree expression_list;
7971 tree member;
7973 /* Find out what is being initialized. */
7974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7976 pedwarn ("anachronistic old-style base class initializer");
7977 mem_initializer_id = NULL_TREE;
7979 else
7980 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7981 member = expand_member_init (mem_initializer_id);
7982 if (member && !DECL_P (member))
7983 in_base_initializer = 1;
7985 expression_list
7986 = cp_parser_parenthesized_expression_list (parser, false,
7987 /*cast_p=*/false,
7988 /*non_constant_p=*/NULL);
7989 if (expression_list == error_mark_node)
7990 return error_mark_node;
7991 if (!expression_list)
7992 expression_list = void_type_node;
7994 in_base_initializer = 0;
7996 return member ? build_tree_list (member, expression_list) : error_mark_node;
7999 /* Parse a mem-initializer-id.
8001 mem-initializer-id:
8002 :: [opt] nested-name-specifier [opt] class-name
8003 identifier
8005 Returns a TYPE indicating the class to be initializer for the first
8006 production. Returns an IDENTIFIER_NODE indicating the data member
8007 to be initialized for the second production. */
8009 static tree
8010 cp_parser_mem_initializer_id (cp_parser* parser)
8012 bool global_scope_p;
8013 bool nested_name_specifier_p;
8014 bool template_p = false;
8015 tree id;
8017 /* `typename' is not allowed in this context ([temp.res]). */
8018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8020 error ("keyword %<typename%> not allowed in this context (a qualified "
8021 "member initializer is implicitly a type)");
8022 cp_lexer_consume_token (parser->lexer);
8024 /* Look for the optional `::' operator. */
8025 global_scope_p
8026 = (cp_parser_global_scope_opt (parser,
8027 /*current_scope_valid_p=*/false)
8028 != NULL_TREE);
8029 /* Look for the optional nested-name-specifier. The simplest way to
8030 implement:
8032 [temp.res]
8034 The keyword `typename' is not permitted in a base-specifier or
8035 mem-initializer; in these contexts a qualified name that
8036 depends on a template-parameter is implicitly assumed to be a
8037 type name.
8039 is to assume that we have seen the `typename' keyword at this
8040 point. */
8041 nested_name_specifier_p
8042 = (cp_parser_nested_name_specifier_opt (parser,
8043 /*typename_keyword_p=*/true,
8044 /*check_dependency_p=*/true,
8045 /*type_p=*/true,
8046 /*is_declaration=*/true)
8047 != NULL_TREE);
8048 if (nested_name_specifier_p)
8049 template_p = cp_parser_optional_template_keyword (parser);
8050 /* If there is a `::' operator or a nested-name-specifier, then we
8051 are definitely looking for a class-name. */
8052 if (global_scope_p || nested_name_specifier_p)
8053 return cp_parser_class_name (parser,
8054 /*typename_keyword_p=*/true,
8055 /*template_keyword_p=*/template_p,
8056 none_type,
8057 /*check_dependency_p=*/true,
8058 /*class_head_p=*/false,
8059 /*is_declaration=*/true);
8060 /* Otherwise, we could also be looking for an ordinary identifier. */
8061 cp_parser_parse_tentatively (parser);
8062 /* Try a class-name. */
8063 id = cp_parser_class_name (parser,
8064 /*typename_keyword_p=*/true,
8065 /*template_keyword_p=*/false,
8066 none_type,
8067 /*check_dependency_p=*/true,
8068 /*class_head_p=*/false,
8069 /*is_declaration=*/true);
8070 /* If we found one, we're done. */
8071 if (cp_parser_parse_definitely (parser))
8072 return id;
8073 /* Otherwise, look for an ordinary identifier. */
8074 return cp_parser_identifier (parser);
8077 /* Overloading [gram.over] */
8079 /* Parse an operator-function-id.
8081 operator-function-id:
8082 operator operator
8084 Returns an IDENTIFIER_NODE for the operator which is a
8085 human-readable spelling of the identifier, e.g., `operator +'. */
8087 static tree
8088 cp_parser_operator_function_id (cp_parser* parser)
8090 /* Look for the `operator' keyword. */
8091 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8092 return error_mark_node;
8093 /* And then the name of the operator itself. */
8094 return cp_parser_operator (parser);
8097 /* Parse an operator.
8099 operator:
8100 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8101 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8102 || ++ -- , ->* -> () []
8104 GNU Extensions:
8106 operator:
8107 <? >? <?= >?=
8109 Returns an IDENTIFIER_NODE for the operator which is a
8110 human-readable spelling of the identifier, e.g., `operator +'. */
8112 static tree
8113 cp_parser_operator (cp_parser* parser)
8115 tree id = NULL_TREE;
8116 cp_token *token;
8118 /* Peek at the next token. */
8119 token = cp_lexer_peek_token (parser->lexer);
8120 /* Figure out which operator we have. */
8121 switch (token->type)
8123 case CPP_KEYWORD:
8125 enum tree_code op;
8127 /* The keyword should be either `new' or `delete'. */
8128 if (token->keyword == RID_NEW)
8129 op = NEW_EXPR;
8130 else if (token->keyword == RID_DELETE)
8131 op = DELETE_EXPR;
8132 else
8133 break;
8135 /* Consume the `new' or `delete' token. */
8136 cp_lexer_consume_token (parser->lexer);
8138 /* Peek at the next token. */
8139 token = cp_lexer_peek_token (parser->lexer);
8140 /* If it's a `[' token then this is the array variant of the
8141 operator. */
8142 if (token->type == CPP_OPEN_SQUARE)
8144 /* Consume the `[' token. */
8145 cp_lexer_consume_token (parser->lexer);
8146 /* Look for the `]' token. */
8147 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8148 id = ansi_opname (op == NEW_EXPR
8149 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8151 /* Otherwise, we have the non-array variant. */
8152 else
8153 id = ansi_opname (op);
8155 return id;
8158 case CPP_PLUS:
8159 id = ansi_opname (PLUS_EXPR);
8160 break;
8162 case CPP_MINUS:
8163 id = ansi_opname (MINUS_EXPR);
8164 break;
8166 case CPP_MULT:
8167 id = ansi_opname (MULT_EXPR);
8168 break;
8170 case CPP_DIV:
8171 id = ansi_opname (TRUNC_DIV_EXPR);
8172 break;
8174 case CPP_MOD:
8175 id = ansi_opname (TRUNC_MOD_EXPR);
8176 break;
8178 case CPP_XOR:
8179 id = ansi_opname (BIT_XOR_EXPR);
8180 break;
8182 case CPP_AND:
8183 id = ansi_opname (BIT_AND_EXPR);
8184 break;
8186 case CPP_OR:
8187 id = ansi_opname (BIT_IOR_EXPR);
8188 break;
8190 case CPP_COMPL:
8191 id = ansi_opname (BIT_NOT_EXPR);
8192 break;
8194 case CPP_NOT:
8195 id = ansi_opname (TRUTH_NOT_EXPR);
8196 break;
8198 case CPP_EQ:
8199 id = ansi_assopname (NOP_EXPR);
8200 break;
8202 case CPP_LESS:
8203 id = ansi_opname (LT_EXPR);
8204 break;
8206 case CPP_GREATER:
8207 id = ansi_opname (GT_EXPR);
8208 break;
8210 case CPP_PLUS_EQ:
8211 id = ansi_assopname (PLUS_EXPR);
8212 break;
8214 case CPP_MINUS_EQ:
8215 id = ansi_assopname (MINUS_EXPR);
8216 break;
8218 case CPP_MULT_EQ:
8219 id = ansi_assopname (MULT_EXPR);
8220 break;
8222 case CPP_DIV_EQ:
8223 id = ansi_assopname (TRUNC_DIV_EXPR);
8224 break;
8226 case CPP_MOD_EQ:
8227 id = ansi_assopname (TRUNC_MOD_EXPR);
8228 break;
8230 case CPP_XOR_EQ:
8231 id = ansi_assopname (BIT_XOR_EXPR);
8232 break;
8234 case CPP_AND_EQ:
8235 id = ansi_assopname (BIT_AND_EXPR);
8236 break;
8238 case CPP_OR_EQ:
8239 id = ansi_assopname (BIT_IOR_EXPR);
8240 break;
8242 case CPP_LSHIFT:
8243 id = ansi_opname (LSHIFT_EXPR);
8244 break;
8246 case CPP_RSHIFT:
8247 id = ansi_opname (RSHIFT_EXPR);
8248 break;
8250 case CPP_LSHIFT_EQ:
8251 id = ansi_assopname (LSHIFT_EXPR);
8252 break;
8254 case CPP_RSHIFT_EQ:
8255 id = ansi_assopname (RSHIFT_EXPR);
8256 break;
8258 case CPP_EQ_EQ:
8259 id = ansi_opname (EQ_EXPR);
8260 break;
8262 case CPP_NOT_EQ:
8263 id = ansi_opname (NE_EXPR);
8264 break;
8266 case CPP_LESS_EQ:
8267 id = ansi_opname (LE_EXPR);
8268 break;
8270 case CPP_GREATER_EQ:
8271 id = ansi_opname (GE_EXPR);
8272 break;
8274 case CPP_AND_AND:
8275 id = ansi_opname (TRUTH_ANDIF_EXPR);
8276 break;
8278 case CPP_OR_OR:
8279 id = ansi_opname (TRUTH_ORIF_EXPR);
8280 break;
8282 case CPP_PLUS_PLUS:
8283 id = ansi_opname (POSTINCREMENT_EXPR);
8284 break;
8286 case CPP_MINUS_MINUS:
8287 id = ansi_opname (PREDECREMENT_EXPR);
8288 break;
8290 case CPP_COMMA:
8291 id = ansi_opname (COMPOUND_EXPR);
8292 break;
8294 case CPP_DEREF_STAR:
8295 id = ansi_opname (MEMBER_REF);
8296 break;
8298 case CPP_DEREF:
8299 id = ansi_opname (COMPONENT_REF);
8300 break;
8302 case CPP_OPEN_PAREN:
8303 /* Consume the `('. */
8304 cp_lexer_consume_token (parser->lexer);
8305 /* Look for the matching `)'. */
8306 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8307 return ansi_opname (CALL_EXPR);
8309 case CPP_OPEN_SQUARE:
8310 /* Consume the `['. */
8311 cp_lexer_consume_token (parser->lexer);
8312 /* Look for the matching `]'. */
8313 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8314 return ansi_opname (ARRAY_REF);
8316 /* Extensions. */
8317 case CPP_MIN:
8318 id = ansi_opname (MIN_EXPR);
8319 cp_parser_warn_min_max ();
8320 break;
8322 case CPP_MAX:
8323 id = ansi_opname (MAX_EXPR);
8324 cp_parser_warn_min_max ();
8325 break;
8327 case CPP_MIN_EQ:
8328 id = ansi_assopname (MIN_EXPR);
8329 cp_parser_warn_min_max ();
8330 break;
8332 case CPP_MAX_EQ:
8333 id = ansi_assopname (MAX_EXPR);
8334 cp_parser_warn_min_max ();
8335 break;
8337 default:
8338 /* Anything else is an error. */
8339 break;
8342 /* If we have selected an identifier, we need to consume the
8343 operator token. */
8344 if (id)
8345 cp_lexer_consume_token (parser->lexer);
8346 /* Otherwise, no valid operator name was present. */
8347 else
8349 cp_parser_error (parser, "expected operator");
8350 id = error_mark_node;
8353 return id;
8356 /* Parse a template-declaration.
8358 template-declaration:
8359 export [opt] template < template-parameter-list > declaration
8361 If MEMBER_P is TRUE, this template-declaration occurs within a
8362 class-specifier.
8364 The grammar rule given by the standard isn't correct. What
8365 is really meant is:
8367 template-declaration:
8368 export [opt] template-parameter-list-seq
8369 decl-specifier-seq [opt] init-declarator [opt] ;
8370 export [opt] template-parameter-list-seq
8371 function-definition
8373 template-parameter-list-seq:
8374 template-parameter-list-seq [opt]
8375 template < template-parameter-list > */
8377 static void
8378 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8380 /* Check for `export'. */
8381 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8383 /* Consume the `export' token. */
8384 cp_lexer_consume_token (parser->lexer);
8385 /* Warn that we do not support `export'. */
8386 warning (0, "keyword %<export%> not implemented, and will be ignored");
8389 cp_parser_template_declaration_after_export (parser, member_p);
8392 /* Parse a template-parameter-list.
8394 template-parameter-list:
8395 template-parameter
8396 template-parameter-list , template-parameter
8398 Returns a TREE_LIST. Each node represents a template parameter.
8399 The nodes are connected via their TREE_CHAINs. */
8401 static tree
8402 cp_parser_template_parameter_list (cp_parser* parser)
8404 tree parameter_list = NULL_TREE;
8406 begin_template_parm_list ();
8407 while (true)
8409 tree parameter;
8410 cp_token *token;
8411 bool is_non_type;
8413 /* Parse the template-parameter. */
8414 parameter = cp_parser_template_parameter (parser, &is_non_type);
8415 /* Add it to the list. */
8416 if (parameter != error_mark_node)
8417 parameter_list = process_template_parm (parameter_list,
8418 parameter,
8419 is_non_type);
8420 /* Peek at the next token. */
8421 token = cp_lexer_peek_token (parser->lexer);
8422 /* If it's not a `,', we're done. */
8423 if (token->type != CPP_COMMA)
8424 break;
8425 /* Otherwise, consume the `,' token. */
8426 cp_lexer_consume_token (parser->lexer);
8429 return end_template_parm_list (parameter_list);
8432 /* Parse a template-parameter.
8434 template-parameter:
8435 type-parameter
8436 parameter-declaration
8438 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8439 the parameter. The TREE_PURPOSE is the default value, if any.
8440 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8441 iff this parameter is a non-type parameter. */
8443 static tree
8444 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8446 cp_token *token;
8447 cp_parameter_declarator *parameter_declarator;
8448 tree parm;
8450 /* Assume it is a type parameter or a template parameter. */
8451 *is_non_type = false;
8452 /* Peek at the next token. */
8453 token = cp_lexer_peek_token (parser->lexer);
8454 /* If it is `class' or `template', we have a type-parameter. */
8455 if (token->keyword == RID_TEMPLATE)
8456 return cp_parser_type_parameter (parser);
8457 /* If it is `class' or `typename' we do not know yet whether it is a
8458 type parameter or a non-type parameter. Consider:
8460 template <typename T, typename T::X X> ...
8464 template <class C, class D*> ...
8466 Here, the first parameter is a type parameter, and the second is
8467 a non-type parameter. We can tell by looking at the token after
8468 the identifier -- if it is a `,', `=', or `>' then we have a type
8469 parameter. */
8470 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8472 /* Peek at the token after `class' or `typename'. */
8473 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8474 /* If it's an identifier, skip it. */
8475 if (token->type == CPP_NAME)
8476 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8477 /* Now, see if the token looks like the end of a template
8478 parameter. */
8479 if (token->type == CPP_COMMA
8480 || token->type == CPP_EQ
8481 || token->type == CPP_GREATER)
8482 return cp_parser_type_parameter (parser);
8485 /* Otherwise, it is a non-type parameter.
8487 [temp.param]
8489 When parsing a default template-argument for a non-type
8490 template-parameter, the first non-nested `>' is taken as the end
8491 of the template parameter-list rather than a greater-than
8492 operator. */
8493 *is_non_type = true;
8494 parameter_declarator
8495 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8496 /*parenthesized_p=*/NULL);
8497 parm = grokdeclarator (parameter_declarator->declarator,
8498 &parameter_declarator->decl_specifiers,
8499 PARM, /*initialized=*/0,
8500 /*attrlist=*/NULL);
8501 if (parm == error_mark_node)
8502 return error_mark_node;
8503 return build_tree_list (parameter_declarator->default_argument, parm);
8506 /* Parse a type-parameter.
8508 type-parameter:
8509 class identifier [opt]
8510 class identifier [opt] = type-id
8511 typename identifier [opt]
8512 typename identifier [opt] = type-id
8513 template < template-parameter-list > class identifier [opt]
8514 template < template-parameter-list > class identifier [opt]
8515 = id-expression
8517 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8518 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8519 the declaration of the parameter. */
8521 static tree
8522 cp_parser_type_parameter (cp_parser* parser)
8524 cp_token *token;
8525 tree parameter;
8527 /* Look for a keyword to tell us what kind of parameter this is. */
8528 token = cp_parser_require (parser, CPP_KEYWORD,
8529 "`class', `typename', or `template'");
8530 if (!token)
8531 return error_mark_node;
8533 switch (token->keyword)
8535 case RID_CLASS:
8536 case RID_TYPENAME:
8538 tree identifier;
8539 tree default_argument;
8541 /* If the next token is an identifier, then it names the
8542 parameter. */
8543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8544 identifier = cp_parser_identifier (parser);
8545 else
8546 identifier = NULL_TREE;
8548 /* Create the parameter. */
8549 parameter = finish_template_type_parm (class_type_node, identifier);
8551 /* If the next token is an `=', we have a default argument. */
8552 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8554 /* Consume the `=' token. */
8555 cp_lexer_consume_token (parser->lexer);
8556 /* Parse the default-argument. */
8557 default_argument = cp_parser_type_id (parser);
8559 else
8560 default_argument = NULL_TREE;
8562 /* Create the combined representation of the parameter and the
8563 default argument. */
8564 parameter = build_tree_list (default_argument, parameter);
8566 break;
8568 case RID_TEMPLATE:
8570 tree parameter_list;
8571 tree identifier;
8572 tree default_argument;
8574 /* Look for the `<'. */
8575 cp_parser_require (parser, CPP_LESS, "`<'");
8576 /* Parse the template-parameter-list. */
8577 parameter_list = cp_parser_template_parameter_list (parser);
8578 /* Look for the `>'. */
8579 cp_parser_require (parser, CPP_GREATER, "`>'");
8580 /* Look for the `class' keyword. */
8581 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8582 /* If the next token is an `=', then there is a
8583 default-argument. If the next token is a `>', we are at
8584 the end of the parameter-list. If the next token is a `,',
8585 then we are at the end of this parameter. */
8586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8587 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8588 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8590 identifier = cp_parser_identifier (parser);
8591 /* Treat invalid names as if the parameter were nameless. */
8592 if (identifier == error_mark_node)
8593 identifier = NULL_TREE;
8595 else
8596 identifier = NULL_TREE;
8598 /* Create the template parameter. */
8599 parameter = finish_template_template_parm (class_type_node,
8600 identifier);
8602 /* If the next token is an `=', then there is a
8603 default-argument. */
8604 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8606 bool is_template;
8608 /* Consume the `='. */
8609 cp_lexer_consume_token (parser->lexer);
8610 /* Parse the id-expression. */
8611 default_argument
8612 = cp_parser_id_expression (parser,
8613 /*template_keyword_p=*/false,
8614 /*check_dependency_p=*/true,
8615 /*template_p=*/&is_template,
8616 /*declarator_p=*/false);
8617 if (TREE_CODE (default_argument) == TYPE_DECL)
8618 /* If the id-expression was a template-id that refers to
8619 a template-class, we already have the declaration here,
8620 so no further lookup is needed. */
8622 else
8623 /* Look up the name. */
8624 default_argument
8625 = cp_parser_lookup_name (parser, default_argument,
8626 none_type,
8627 /*is_template=*/is_template,
8628 /*is_namespace=*/false,
8629 /*check_dependency=*/true,
8630 /*ambiguous_decls=*/NULL);
8631 /* See if the default argument is valid. */
8632 default_argument
8633 = check_template_template_default_arg (default_argument);
8635 else
8636 default_argument = NULL_TREE;
8638 /* Create the combined representation of the parameter and the
8639 default argument. */
8640 parameter = build_tree_list (default_argument, parameter);
8642 break;
8644 default:
8645 gcc_unreachable ();
8646 break;
8649 return parameter;
8652 /* Parse a template-id.
8654 template-id:
8655 template-name < template-argument-list [opt] >
8657 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8658 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8659 returned. Otherwise, if the template-name names a function, or set
8660 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8661 names a class, returns a TYPE_DECL for the specialization.
8663 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8664 uninstantiated templates. */
8666 static tree
8667 cp_parser_template_id (cp_parser *parser,
8668 bool template_keyword_p,
8669 bool check_dependency_p,
8670 bool is_declaration)
8672 tree template;
8673 tree arguments;
8674 tree template_id;
8675 cp_token_position start_of_id = 0;
8676 tree access_check = NULL_TREE;
8677 cp_token *next_token, *next_token_2;
8678 bool is_identifier;
8680 /* If the next token corresponds to a template-id, there is no need
8681 to reparse it. */
8682 next_token = cp_lexer_peek_token (parser->lexer);
8683 if (next_token->type == CPP_TEMPLATE_ID)
8685 tree value;
8686 tree check;
8688 /* Get the stored value. */
8689 value = cp_lexer_consume_token (parser->lexer)->value;
8690 /* Perform any access checks that were deferred. */
8691 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8692 perform_or_defer_access_check (TREE_PURPOSE (check),
8693 TREE_VALUE (check));
8694 /* Return the stored value. */
8695 return TREE_VALUE (value);
8698 /* Avoid performing name lookup if there is no possibility of
8699 finding a template-id. */
8700 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8701 || (next_token->type == CPP_NAME
8702 && !cp_parser_nth_token_starts_template_argument_list_p
8703 (parser, 2)))
8705 cp_parser_error (parser, "expected template-id");
8706 return error_mark_node;
8709 /* Remember where the template-id starts. */
8710 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8711 start_of_id = cp_lexer_token_position (parser->lexer, false);
8713 push_deferring_access_checks (dk_deferred);
8715 /* Parse the template-name. */
8716 is_identifier = false;
8717 template = cp_parser_template_name (parser, template_keyword_p,
8718 check_dependency_p,
8719 is_declaration,
8720 &is_identifier);
8721 if (template == error_mark_node || is_identifier)
8723 pop_deferring_access_checks ();
8724 return template;
8727 /* If we find the sequence `[:' after a template-name, it's probably
8728 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8729 parse correctly the argument list. */
8730 next_token = cp_lexer_peek_token (parser->lexer);
8731 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8732 if (next_token->type == CPP_OPEN_SQUARE
8733 && next_token->flags & DIGRAPH
8734 && next_token_2->type == CPP_COLON
8735 && !(next_token_2->flags & PREV_WHITE))
8737 cp_parser_parse_tentatively (parser);
8738 /* Change `:' into `::'. */
8739 next_token_2->type = CPP_SCOPE;
8740 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8741 CPP_LESS. */
8742 cp_lexer_consume_token (parser->lexer);
8743 /* Parse the arguments. */
8744 arguments = cp_parser_enclosed_template_argument_list (parser);
8745 if (!cp_parser_parse_definitely (parser))
8747 /* If we couldn't parse an argument list, then we revert our changes
8748 and return simply an error. Maybe this is not a template-id
8749 after all. */
8750 next_token_2->type = CPP_COLON;
8751 cp_parser_error (parser, "expected %<<%>");
8752 pop_deferring_access_checks ();
8753 return error_mark_node;
8755 /* Otherwise, emit an error about the invalid digraph, but continue
8756 parsing because we got our argument list. */
8757 pedwarn ("%<<::%> cannot begin a template-argument list");
8758 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8759 "between %<<%> and %<::%>");
8760 if (!flag_permissive)
8762 static bool hint;
8763 if (!hint)
8765 inform ("(if you use -fpermissive G++ will accept your code)");
8766 hint = true;
8770 else
8772 /* Look for the `<' that starts the template-argument-list. */
8773 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8775 pop_deferring_access_checks ();
8776 return error_mark_node;
8778 /* Parse the arguments. */
8779 arguments = cp_parser_enclosed_template_argument_list (parser);
8782 /* Build a representation of the specialization. */
8783 if (TREE_CODE (template) == IDENTIFIER_NODE)
8784 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8785 else if (DECL_CLASS_TEMPLATE_P (template)
8786 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8787 template_id
8788 = finish_template_type (template, arguments,
8789 cp_lexer_next_token_is (parser->lexer,
8790 CPP_SCOPE));
8791 else
8793 /* If it's not a class-template or a template-template, it should be
8794 a function-template. */
8795 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8796 || TREE_CODE (template) == OVERLOAD
8797 || BASELINK_P (template)));
8799 template_id = lookup_template_function (template, arguments);
8802 /* Retrieve any deferred checks. Do not pop this access checks yet
8803 so the memory will not be reclaimed during token replacing below. */
8804 access_check = get_deferred_access_checks ();
8806 /* If parsing tentatively, replace the sequence of tokens that makes
8807 up the template-id with a CPP_TEMPLATE_ID token. That way,
8808 should we re-parse the token stream, we will not have to repeat
8809 the effort required to do the parse, nor will we issue duplicate
8810 error messages about problems during instantiation of the
8811 template. */
8812 if (start_of_id)
8814 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8816 /* Reset the contents of the START_OF_ID token. */
8817 token->type = CPP_TEMPLATE_ID;
8818 token->value = build_tree_list (access_check, template_id);
8819 token->keyword = RID_MAX;
8821 /* Purge all subsequent tokens. */
8822 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8824 /* ??? Can we actually assume that, if template_id ==
8825 error_mark_node, we will have issued a diagnostic to the
8826 user, as opposed to simply marking the tentative parse as
8827 failed? */
8828 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8829 error ("parse error in template argument list");
8832 pop_deferring_access_checks ();
8833 return template_id;
8836 /* Parse a template-name.
8838 template-name:
8839 identifier
8841 The standard should actually say:
8843 template-name:
8844 identifier
8845 operator-function-id
8847 A defect report has been filed about this issue.
8849 A conversion-function-id cannot be a template name because they cannot
8850 be part of a template-id. In fact, looking at this code:
8852 a.operator K<int>()
8854 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8855 It is impossible to call a templated conversion-function-id with an
8856 explicit argument list, since the only allowed template parameter is
8857 the type to which it is converting.
8859 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8860 `template' keyword, in a construction like:
8862 T::template f<3>()
8864 In that case `f' is taken to be a template-name, even though there
8865 is no way of knowing for sure.
8867 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8868 name refers to a set of overloaded functions, at least one of which
8869 is a template, or an IDENTIFIER_NODE with the name of the template,
8870 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8871 names are looked up inside uninstantiated templates. */
8873 static tree
8874 cp_parser_template_name (cp_parser* parser,
8875 bool template_keyword_p,
8876 bool check_dependency_p,
8877 bool is_declaration,
8878 bool *is_identifier)
8880 tree identifier;
8881 tree decl;
8882 tree fns;
8884 /* If the next token is `operator', then we have either an
8885 operator-function-id or a conversion-function-id. */
8886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8888 /* We don't know whether we're looking at an
8889 operator-function-id or a conversion-function-id. */
8890 cp_parser_parse_tentatively (parser);
8891 /* Try an operator-function-id. */
8892 identifier = cp_parser_operator_function_id (parser);
8893 /* If that didn't work, try a conversion-function-id. */
8894 if (!cp_parser_parse_definitely (parser))
8896 cp_parser_error (parser, "expected template-name");
8897 return error_mark_node;
8900 /* Look for the identifier. */
8901 else
8902 identifier = cp_parser_identifier (parser);
8904 /* If we didn't find an identifier, we don't have a template-id. */
8905 if (identifier == error_mark_node)
8906 return error_mark_node;
8908 /* If the name immediately followed the `template' keyword, then it
8909 is a template-name. However, if the next token is not `<', then
8910 we do not treat it as a template-name, since it is not being used
8911 as part of a template-id. This enables us to handle constructs
8912 like:
8914 template <typename T> struct S { S(); };
8915 template <typename T> S<T>::S();
8917 correctly. We would treat `S' as a template -- if it were `S<T>'
8918 -- but we do not if there is no `<'. */
8920 if (processing_template_decl
8921 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8923 /* In a declaration, in a dependent context, we pretend that the
8924 "template" keyword was present in order to improve error
8925 recovery. For example, given:
8927 template <typename T> void f(T::X<int>);
8929 we want to treat "X<int>" as a template-id. */
8930 if (is_declaration
8931 && !template_keyword_p
8932 && parser->scope && TYPE_P (parser->scope)
8933 && check_dependency_p
8934 && dependent_type_p (parser->scope)
8935 /* Do not do this for dtors (or ctors), since they never
8936 need the template keyword before their name. */
8937 && !constructor_name_p (identifier, parser->scope))
8939 cp_token_position start = 0;
8941 /* Explain what went wrong. */
8942 error ("non-template %qD used as template", identifier);
8943 inform ("use %<%T::template %D%> to indicate that it is a template",
8944 parser->scope, identifier);
8945 /* If parsing tentatively, find the location of the "<" token. */
8946 if (cp_parser_simulate_error (parser))
8947 start = cp_lexer_token_position (parser->lexer, true);
8948 /* Parse the template arguments so that we can issue error
8949 messages about them. */
8950 cp_lexer_consume_token (parser->lexer);
8951 cp_parser_enclosed_template_argument_list (parser);
8952 /* Skip tokens until we find a good place from which to
8953 continue parsing. */
8954 cp_parser_skip_to_closing_parenthesis (parser,
8955 /*recovering=*/true,
8956 /*or_comma=*/true,
8957 /*consume_paren=*/false);
8958 /* If parsing tentatively, permanently remove the
8959 template argument list. That will prevent duplicate
8960 error messages from being issued about the missing
8961 "template" keyword. */
8962 if (start)
8963 cp_lexer_purge_tokens_after (parser->lexer, start);
8964 if (is_identifier)
8965 *is_identifier = true;
8966 return identifier;
8969 /* If the "template" keyword is present, then there is generally
8970 no point in doing name-lookup, so we just return IDENTIFIER.
8971 But, if the qualifying scope is non-dependent then we can
8972 (and must) do name-lookup normally. */
8973 if (template_keyword_p
8974 && (!parser->scope
8975 || (TYPE_P (parser->scope)
8976 && dependent_type_p (parser->scope))))
8977 return identifier;
8980 /* Look up the name. */
8981 decl = cp_parser_lookup_name (parser, identifier,
8982 none_type,
8983 /*is_template=*/false,
8984 /*is_namespace=*/false,
8985 check_dependency_p,
8986 /*ambiguous_decls=*/NULL);
8987 decl = maybe_get_template_decl_from_type_decl (decl);
8989 /* If DECL is a template, then the name was a template-name. */
8990 if (TREE_CODE (decl) == TEMPLATE_DECL)
8992 else
8994 tree fn = NULL_TREE;
8996 /* The standard does not explicitly indicate whether a name that
8997 names a set of overloaded declarations, some of which are
8998 templates, is a template-name. However, such a name should
8999 be a template-name; otherwise, there is no way to form a
9000 template-id for the overloaded templates. */
9001 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9002 if (TREE_CODE (fns) == OVERLOAD)
9003 for (fn = fns; fn; fn = OVL_NEXT (fn))
9004 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9005 break;
9007 if (!fn)
9009 /* The name does not name a template. */
9010 cp_parser_error (parser, "expected template-name");
9011 return error_mark_node;
9015 /* If DECL is dependent, and refers to a function, then just return
9016 its name; we will look it up again during template instantiation. */
9017 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9019 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9020 if (TYPE_P (scope) && dependent_type_p (scope))
9021 return identifier;
9024 return decl;
9027 /* Parse a template-argument-list.
9029 template-argument-list:
9030 template-argument
9031 template-argument-list , template-argument
9033 Returns a TREE_VEC containing the arguments. */
9035 static tree
9036 cp_parser_template_argument_list (cp_parser* parser)
9038 tree fixed_args[10];
9039 unsigned n_args = 0;
9040 unsigned alloced = 10;
9041 tree *arg_ary = fixed_args;
9042 tree vec;
9043 bool saved_in_template_argument_list_p;
9044 bool saved_ice_p;
9045 bool saved_non_ice_p;
9047 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9048 parser->in_template_argument_list_p = true;
9049 /* Even if the template-id appears in an integral
9050 constant-expression, the contents of the argument list do
9051 not. */
9052 saved_ice_p = parser->integral_constant_expression_p;
9053 parser->integral_constant_expression_p = false;
9054 saved_non_ice_p = parser->non_integral_constant_expression_p;
9055 parser->non_integral_constant_expression_p = false;
9056 /* Parse the arguments. */
9059 tree argument;
9061 if (n_args)
9062 /* Consume the comma. */
9063 cp_lexer_consume_token (parser->lexer);
9065 /* Parse the template-argument. */
9066 argument = cp_parser_template_argument (parser);
9067 if (n_args == alloced)
9069 alloced *= 2;
9071 if (arg_ary == fixed_args)
9073 arg_ary = XNEWVEC (tree, alloced);
9074 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9076 else
9077 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9079 arg_ary[n_args++] = argument;
9081 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9083 vec = make_tree_vec (n_args);
9085 while (n_args--)
9086 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9088 if (arg_ary != fixed_args)
9089 free (arg_ary);
9090 parser->non_integral_constant_expression_p = saved_non_ice_p;
9091 parser->integral_constant_expression_p = saved_ice_p;
9092 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9093 return vec;
9096 /* Parse a template-argument.
9098 template-argument:
9099 assignment-expression
9100 type-id
9101 id-expression
9103 The representation is that of an assignment-expression, type-id, or
9104 id-expression -- except that the qualified id-expression is
9105 evaluated, so that the value returned is either a DECL or an
9106 OVERLOAD.
9108 Although the standard says "assignment-expression", it forbids
9109 throw-expressions or assignments in the template argument.
9110 Therefore, we use "conditional-expression" instead. */
9112 static tree
9113 cp_parser_template_argument (cp_parser* parser)
9115 tree argument;
9116 bool template_p;
9117 bool address_p;
9118 bool maybe_type_id = false;
9119 cp_token *token;
9120 cp_id_kind idk;
9122 /* There's really no way to know what we're looking at, so we just
9123 try each alternative in order.
9125 [temp.arg]
9127 In a template-argument, an ambiguity between a type-id and an
9128 expression is resolved to a type-id, regardless of the form of
9129 the corresponding template-parameter.
9131 Therefore, we try a type-id first. */
9132 cp_parser_parse_tentatively (parser);
9133 argument = cp_parser_type_id (parser);
9134 /* If there was no error parsing the type-id but the next token is a '>>',
9135 we probably found a typo for '> >'. But there are type-id which are
9136 also valid expressions. For instance:
9138 struct X { int operator >> (int); };
9139 template <int V> struct Foo {};
9140 Foo<X () >> 5> r;
9142 Here 'X()' is a valid type-id of a function type, but the user just
9143 wanted to write the expression "X() >> 5". Thus, we remember that we
9144 found a valid type-id, but we still try to parse the argument as an
9145 expression to see what happens. */
9146 if (!cp_parser_error_occurred (parser)
9147 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9149 maybe_type_id = true;
9150 cp_parser_abort_tentative_parse (parser);
9152 else
9154 /* If the next token isn't a `,' or a `>', then this argument wasn't
9155 really finished. This means that the argument is not a valid
9156 type-id. */
9157 if (!cp_parser_next_token_ends_template_argument_p (parser))
9158 cp_parser_error (parser, "expected template-argument");
9159 /* If that worked, we're done. */
9160 if (cp_parser_parse_definitely (parser))
9161 return argument;
9163 /* We're still not sure what the argument will be. */
9164 cp_parser_parse_tentatively (parser);
9165 /* Try a template. */
9166 argument = cp_parser_id_expression (parser,
9167 /*template_keyword_p=*/false,
9168 /*check_dependency_p=*/true,
9169 &template_p,
9170 /*declarator_p=*/false);
9171 /* If the next token isn't a `,' or a `>', then this argument wasn't
9172 really finished. */
9173 if (!cp_parser_next_token_ends_template_argument_p (parser))
9174 cp_parser_error (parser, "expected template-argument");
9175 if (!cp_parser_error_occurred (parser))
9177 /* Figure out what is being referred to. If the id-expression
9178 was for a class template specialization, then we will have a
9179 TYPE_DECL at this point. There is no need to do name lookup
9180 at this point in that case. */
9181 if (TREE_CODE (argument) != TYPE_DECL)
9182 argument = cp_parser_lookup_name (parser, argument,
9183 none_type,
9184 /*is_template=*/template_p,
9185 /*is_namespace=*/false,
9186 /*check_dependency=*/true,
9187 /*ambiguous_decls=*/NULL);
9188 if (TREE_CODE (argument) != TEMPLATE_DECL
9189 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9190 cp_parser_error (parser, "expected template-name");
9192 if (cp_parser_parse_definitely (parser))
9193 return argument;
9194 /* It must be a non-type argument. There permitted cases are given
9195 in [temp.arg.nontype]:
9197 -- an integral constant-expression of integral or enumeration
9198 type; or
9200 -- the name of a non-type template-parameter; or
9202 -- the name of an object or function with external linkage...
9204 -- the address of an object or function with external linkage...
9206 -- a pointer to member... */
9207 /* Look for a non-type template parameter. */
9208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9210 cp_parser_parse_tentatively (parser);
9211 argument = cp_parser_primary_expression (parser,
9212 /*adress_p=*/false,
9213 /*cast_p=*/false,
9214 /*template_arg_p=*/true,
9215 &idk);
9216 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9217 || !cp_parser_next_token_ends_template_argument_p (parser))
9218 cp_parser_simulate_error (parser);
9219 if (cp_parser_parse_definitely (parser))
9220 return argument;
9223 /* If the next token is "&", the argument must be the address of an
9224 object or function with external linkage. */
9225 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9226 if (address_p)
9227 cp_lexer_consume_token (parser->lexer);
9228 /* See if we might have an id-expression. */
9229 token = cp_lexer_peek_token (parser->lexer);
9230 if (token->type == CPP_NAME
9231 || token->keyword == RID_OPERATOR
9232 || token->type == CPP_SCOPE
9233 || token->type == CPP_TEMPLATE_ID
9234 || token->type == CPP_NESTED_NAME_SPECIFIER)
9236 cp_parser_parse_tentatively (parser);
9237 argument = cp_parser_primary_expression (parser,
9238 address_p,
9239 /*cast_p=*/false,
9240 /*template_arg_p=*/true,
9241 &idk);
9242 if (cp_parser_error_occurred (parser)
9243 || !cp_parser_next_token_ends_template_argument_p (parser))
9244 cp_parser_abort_tentative_parse (parser);
9245 else
9247 if (TREE_CODE (argument) == INDIRECT_REF)
9249 gcc_assert (REFERENCE_REF_P (argument));
9250 argument = TREE_OPERAND (argument, 0);
9253 if (TREE_CODE (argument) == BASELINK)
9254 /* We don't need the information about what class was used
9255 to name the overloaded functions. */
9256 argument = BASELINK_FUNCTIONS (argument);
9258 if (TREE_CODE (argument) == VAR_DECL)
9260 /* A variable without external linkage might still be a
9261 valid constant-expression, so no error is issued here
9262 if the external-linkage check fails. */
9263 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9264 cp_parser_simulate_error (parser);
9266 else if (is_overloaded_fn (argument))
9267 /* All overloaded functions are allowed; if the external
9268 linkage test does not pass, an error will be issued
9269 later. */
9271 else if (address_p
9272 && (TREE_CODE (argument) == OFFSET_REF
9273 || TREE_CODE (argument) == SCOPE_REF))
9274 /* A pointer-to-member. */
9276 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9278 else
9279 cp_parser_simulate_error (parser);
9281 if (cp_parser_parse_definitely (parser))
9283 if (address_p)
9284 argument = build_x_unary_op (ADDR_EXPR, argument);
9285 return argument;
9289 /* If the argument started with "&", there are no other valid
9290 alternatives at this point. */
9291 if (address_p)
9293 cp_parser_error (parser, "invalid non-type template argument");
9294 return error_mark_node;
9297 /* If the argument wasn't successfully parsed as a type-id followed
9298 by '>>', the argument can only be a constant expression now.
9299 Otherwise, we try parsing the constant-expression tentatively,
9300 because the argument could really be a type-id. */
9301 if (maybe_type_id)
9302 cp_parser_parse_tentatively (parser);
9303 argument = cp_parser_constant_expression (parser,
9304 /*allow_non_constant_p=*/false,
9305 /*non_constant_p=*/NULL);
9306 argument = fold_non_dependent_expr (argument);
9307 if (!maybe_type_id)
9308 return argument;
9309 if (!cp_parser_next_token_ends_template_argument_p (parser))
9310 cp_parser_error (parser, "expected template-argument");
9311 if (cp_parser_parse_definitely (parser))
9312 return argument;
9313 /* We did our best to parse the argument as a non type-id, but that
9314 was the only alternative that matched (albeit with a '>' after
9315 it). We can assume it's just a typo from the user, and a
9316 diagnostic will then be issued. */
9317 return cp_parser_type_id (parser);
9320 /* Parse an explicit-instantiation.
9322 explicit-instantiation:
9323 template declaration
9325 Although the standard says `declaration', what it really means is:
9327 explicit-instantiation:
9328 template decl-specifier-seq [opt] declarator [opt] ;
9330 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9331 supposed to be allowed. A defect report has been filed about this
9332 issue.
9334 GNU Extension:
9336 explicit-instantiation:
9337 storage-class-specifier template
9338 decl-specifier-seq [opt] declarator [opt] ;
9339 function-specifier template
9340 decl-specifier-seq [opt] declarator [opt] ; */
9342 static void
9343 cp_parser_explicit_instantiation (cp_parser* parser)
9345 int declares_class_or_enum;
9346 cp_decl_specifier_seq decl_specifiers;
9347 tree extension_specifier = NULL_TREE;
9349 /* Look for an (optional) storage-class-specifier or
9350 function-specifier. */
9351 if (cp_parser_allow_gnu_extensions_p (parser))
9353 extension_specifier
9354 = cp_parser_storage_class_specifier_opt (parser);
9355 if (!extension_specifier)
9356 extension_specifier
9357 = cp_parser_function_specifier_opt (parser,
9358 /*decl_specs=*/NULL);
9361 /* Look for the `template' keyword. */
9362 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9363 /* Let the front end know that we are processing an explicit
9364 instantiation. */
9365 begin_explicit_instantiation ();
9366 /* [temp.explicit] says that we are supposed to ignore access
9367 control while processing explicit instantiation directives. */
9368 push_deferring_access_checks (dk_no_check);
9369 /* Parse a decl-specifier-seq. */
9370 cp_parser_decl_specifier_seq (parser,
9371 CP_PARSER_FLAGS_OPTIONAL,
9372 &decl_specifiers,
9373 &declares_class_or_enum);
9374 /* If there was exactly one decl-specifier, and it declared a class,
9375 and there's no declarator, then we have an explicit type
9376 instantiation. */
9377 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9379 tree type;
9381 type = check_tag_decl (&decl_specifiers);
9382 /* Turn access control back on for names used during
9383 template instantiation. */
9384 pop_deferring_access_checks ();
9385 if (type)
9386 do_type_instantiation (type, extension_specifier,
9387 /*complain=*/tf_error);
9389 else
9391 cp_declarator *declarator;
9392 tree decl;
9394 /* Parse the declarator. */
9395 declarator
9396 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9397 /*ctor_dtor_or_conv_p=*/NULL,
9398 /*parenthesized_p=*/NULL,
9399 /*member_p=*/false);
9400 if (declares_class_or_enum & 2)
9401 cp_parser_check_for_definition_in_return_type (declarator,
9402 decl_specifiers.type);
9403 if (declarator != cp_error_declarator)
9405 decl = grokdeclarator (declarator, &decl_specifiers,
9406 NORMAL, 0, NULL);
9407 /* Turn access control back on for names used during
9408 template instantiation. */
9409 pop_deferring_access_checks ();
9410 /* Do the explicit instantiation. */
9411 do_decl_instantiation (decl, extension_specifier);
9413 else
9415 pop_deferring_access_checks ();
9416 /* Skip the body of the explicit instantiation. */
9417 cp_parser_skip_to_end_of_statement (parser);
9420 /* We're done with the instantiation. */
9421 end_explicit_instantiation ();
9423 cp_parser_consume_semicolon_at_end_of_statement (parser);
9426 /* Parse an explicit-specialization.
9428 explicit-specialization:
9429 template < > declaration
9431 Although the standard says `declaration', what it really means is:
9433 explicit-specialization:
9434 template <> decl-specifier [opt] init-declarator [opt] ;
9435 template <> function-definition
9436 template <> explicit-specialization
9437 template <> template-declaration */
9439 static void
9440 cp_parser_explicit_specialization (cp_parser* parser)
9442 bool need_lang_pop;
9443 /* Look for the `template' keyword. */
9444 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9445 /* Look for the `<'. */
9446 cp_parser_require (parser, CPP_LESS, "`<'");
9447 /* Look for the `>'. */
9448 cp_parser_require (parser, CPP_GREATER, "`>'");
9449 /* We have processed another parameter list. */
9450 ++parser->num_template_parameter_lists;
9451 /* [temp]
9453 A template ... explicit specialization ... shall not have C
9454 linkage. */
9455 if (current_lang_name == lang_name_c)
9457 error ("template specialization with C linkage");
9458 /* Give it C++ linkage to avoid confusing other parts of the
9459 front end. */
9460 push_lang_context (lang_name_cplusplus);
9461 need_lang_pop = true;
9463 else
9464 need_lang_pop = false;
9465 /* Let the front end know that we are beginning a specialization. */
9466 begin_specialization ();
9467 /* If the next keyword is `template', we need to figure out whether
9468 or not we're looking a template-declaration. */
9469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9471 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9472 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9473 cp_parser_template_declaration_after_export (parser,
9474 /*member_p=*/false);
9475 else
9476 cp_parser_explicit_specialization (parser);
9478 else
9479 /* Parse the dependent declaration. */
9480 cp_parser_single_declaration (parser,
9481 /*member_p=*/false,
9482 /*friend_p=*/NULL);
9483 /* We're done with the specialization. */
9484 end_specialization ();
9485 /* For the erroneous case of a template with C linkage, we pushed an
9486 implicit C++ linkage scope; exit that scope now. */
9487 if (need_lang_pop)
9488 pop_lang_context ();
9489 /* We're done with this parameter list. */
9490 --parser->num_template_parameter_lists;
9493 /* Parse a type-specifier.
9495 type-specifier:
9496 simple-type-specifier
9497 class-specifier
9498 enum-specifier
9499 elaborated-type-specifier
9500 cv-qualifier
9502 GNU Extension:
9504 type-specifier:
9505 __complex__
9507 Returns a representation of the type-specifier. For a
9508 class-specifier, enum-specifier, or elaborated-type-specifier, a
9509 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9511 The parser flags FLAGS is used to control type-specifier parsing.
9513 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9514 in a decl-specifier-seq.
9516 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9517 class-specifier, enum-specifier, or elaborated-type-specifier, then
9518 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9519 if a type is declared; 2 if it is defined. Otherwise, it is set to
9520 zero.
9522 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9523 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9524 is set to FALSE. */
9526 static tree
9527 cp_parser_type_specifier (cp_parser* parser,
9528 cp_parser_flags flags,
9529 cp_decl_specifier_seq *decl_specs,
9530 bool is_declaration,
9531 int* declares_class_or_enum,
9532 bool* is_cv_qualifier)
9534 tree type_spec = NULL_TREE;
9535 cp_token *token;
9536 enum rid keyword;
9537 cp_decl_spec ds = ds_last;
9539 /* Assume this type-specifier does not declare a new type. */
9540 if (declares_class_or_enum)
9541 *declares_class_or_enum = 0;
9542 /* And that it does not specify a cv-qualifier. */
9543 if (is_cv_qualifier)
9544 *is_cv_qualifier = false;
9545 /* Peek at the next token. */
9546 token = cp_lexer_peek_token (parser->lexer);
9548 /* If we're looking at a keyword, we can use that to guide the
9549 production we choose. */
9550 keyword = token->keyword;
9551 switch (keyword)
9553 case RID_ENUM:
9554 /* 'enum' [identifier] '{' introduces an enum-specifier;
9555 'enum' <anything else> introduces an elaborated-type-specifier. */
9556 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9557 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9558 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9559 == CPP_OPEN_BRACE))
9561 if (parser->num_template_parameter_lists)
9563 error ("template declaration of %qs", "enum");
9564 cp_parser_skip_to_end_of_block_or_statement (parser);
9565 type_spec = error_mark_node;
9567 else
9568 type_spec = cp_parser_enum_specifier (parser);
9570 if (declares_class_or_enum)
9571 *declares_class_or_enum = 2;
9572 if (decl_specs)
9573 cp_parser_set_decl_spec_type (decl_specs,
9574 type_spec,
9575 /*user_defined_p=*/true);
9576 return type_spec;
9578 else
9579 goto elaborated_type_specifier;
9581 /* Any of these indicate either a class-specifier, or an
9582 elaborated-type-specifier. */
9583 case RID_CLASS:
9584 case RID_STRUCT:
9585 case RID_UNION:
9586 /* Parse tentatively so that we can back up if we don't find a
9587 class-specifier. */
9588 cp_parser_parse_tentatively (parser);
9589 /* Look for the class-specifier. */
9590 type_spec = cp_parser_class_specifier (parser);
9591 /* If that worked, we're done. */
9592 if (cp_parser_parse_definitely (parser))
9594 if (declares_class_or_enum)
9595 *declares_class_or_enum = 2;
9596 if (decl_specs)
9597 cp_parser_set_decl_spec_type (decl_specs,
9598 type_spec,
9599 /*user_defined_p=*/true);
9600 return type_spec;
9603 /* Fall through. */
9604 elaborated_type_specifier:
9605 /* We're declaring (not defining) a class or enum. */
9606 if (declares_class_or_enum)
9607 *declares_class_or_enum = 1;
9609 /* Fall through. */
9610 case RID_TYPENAME:
9611 /* Look for an elaborated-type-specifier. */
9612 type_spec
9613 = (cp_parser_elaborated_type_specifier
9614 (parser,
9615 decl_specs && decl_specs->specs[(int) ds_friend],
9616 is_declaration));
9617 if (decl_specs)
9618 cp_parser_set_decl_spec_type (decl_specs,
9619 type_spec,
9620 /*user_defined_p=*/true);
9621 return type_spec;
9623 case RID_CONST:
9624 ds = ds_const;
9625 if (is_cv_qualifier)
9626 *is_cv_qualifier = true;
9627 break;
9629 case RID_VOLATILE:
9630 ds = ds_volatile;
9631 if (is_cv_qualifier)
9632 *is_cv_qualifier = true;
9633 break;
9635 case RID_RESTRICT:
9636 ds = ds_restrict;
9637 if (is_cv_qualifier)
9638 *is_cv_qualifier = true;
9639 break;
9641 case RID_COMPLEX:
9642 /* The `__complex__' keyword is a GNU extension. */
9643 ds = ds_complex;
9644 break;
9646 default:
9647 break;
9650 /* Handle simple keywords. */
9651 if (ds != ds_last)
9653 if (decl_specs)
9655 ++decl_specs->specs[(int)ds];
9656 decl_specs->any_specifiers_p = true;
9658 return cp_lexer_consume_token (parser->lexer)->value;
9661 /* If we do not already have a type-specifier, assume we are looking
9662 at a simple-type-specifier. */
9663 type_spec = cp_parser_simple_type_specifier (parser,
9664 decl_specs,
9665 flags);
9667 /* If we didn't find a type-specifier, and a type-specifier was not
9668 optional in this context, issue an error message. */
9669 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9671 cp_parser_error (parser, "expected type specifier");
9672 return error_mark_node;
9675 return type_spec;
9678 /* Parse a simple-type-specifier.
9680 simple-type-specifier:
9681 :: [opt] nested-name-specifier [opt] type-name
9682 :: [opt] nested-name-specifier template template-id
9683 char
9684 wchar_t
9685 bool
9686 short
9688 long
9689 signed
9690 unsigned
9691 float
9692 double
9693 void
9695 GNU Extension:
9697 simple-type-specifier:
9698 __typeof__ unary-expression
9699 __typeof__ ( type-id )
9701 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9702 appropriately updated. */
9704 static tree
9705 cp_parser_simple_type_specifier (cp_parser* parser,
9706 cp_decl_specifier_seq *decl_specs,
9707 cp_parser_flags flags)
9709 tree type = NULL_TREE;
9710 cp_token *token;
9712 /* Peek at the next token. */
9713 token = cp_lexer_peek_token (parser->lexer);
9715 /* If we're looking at a keyword, things are easy. */
9716 switch (token->keyword)
9718 case RID_CHAR:
9719 if (decl_specs)
9720 decl_specs->explicit_char_p = true;
9721 type = char_type_node;
9722 break;
9723 case RID_WCHAR:
9724 type = wchar_type_node;
9725 break;
9726 case RID_BOOL:
9727 type = boolean_type_node;
9728 break;
9729 case RID_SHORT:
9730 if (decl_specs)
9731 ++decl_specs->specs[(int) ds_short];
9732 type = short_integer_type_node;
9733 break;
9734 case RID_INT:
9735 if (decl_specs)
9736 decl_specs->explicit_int_p = true;
9737 type = integer_type_node;
9738 break;
9739 case RID_LONG:
9740 if (decl_specs)
9741 ++decl_specs->specs[(int) ds_long];
9742 type = long_integer_type_node;
9743 break;
9744 case RID_SIGNED:
9745 if (decl_specs)
9746 ++decl_specs->specs[(int) ds_signed];
9747 type = integer_type_node;
9748 break;
9749 case RID_UNSIGNED:
9750 if (decl_specs)
9751 ++decl_specs->specs[(int) ds_unsigned];
9752 type = unsigned_type_node;
9753 break;
9754 case RID_FLOAT:
9755 type = float_type_node;
9756 break;
9757 case RID_DOUBLE:
9758 type = double_type_node;
9759 break;
9760 case RID_VOID:
9761 type = void_type_node;
9762 break;
9764 case RID_TYPEOF:
9765 /* Consume the `typeof' token. */
9766 cp_lexer_consume_token (parser->lexer);
9767 /* Parse the operand to `typeof'. */
9768 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9769 /* If it is not already a TYPE, take its type. */
9770 if (!TYPE_P (type))
9771 type = finish_typeof (type);
9773 if (decl_specs)
9774 cp_parser_set_decl_spec_type (decl_specs, type,
9775 /*user_defined_p=*/true);
9777 return type;
9779 default:
9780 break;
9783 /* If the type-specifier was for a built-in type, we're done. */
9784 if (type)
9786 tree id;
9788 /* Record the type. */
9789 if (decl_specs
9790 && (token->keyword != RID_SIGNED
9791 && token->keyword != RID_UNSIGNED
9792 && token->keyword != RID_SHORT
9793 && token->keyword != RID_LONG))
9794 cp_parser_set_decl_spec_type (decl_specs,
9795 type,
9796 /*user_defined=*/false);
9797 if (decl_specs)
9798 decl_specs->any_specifiers_p = true;
9800 /* Consume the token. */
9801 id = cp_lexer_consume_token (parser->lexer)->value;
9803 /* There is no valid C++ program where a non-template type is
9804 followed by a "<". That usually indicates that the user thought
9805 that the type was a template. */
9806 cp_parser_check_for_invalid_template_id (parser, type);
9808 return TYPE_NAME (type);
9811 /* The type-specifier must be a user-defined type. */
9812 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9814 bool qualified_p;
9815 bool global_p;
9817 /* Don't gobble tokens or issue error messages if this is an
9818 optional type-specifier. */
9819 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9820 cp_parser_parse_tentatively (parser);
9822 /* Look for the optional `::' operator. */
9823 global_p
9824 = (cp_parser_global_scope_opt (parser,
9825 /*current_scope_valid_p=*/false)
9826 != NULL_TREE);
9827 /* Look for the nested-name specifier. */
9828 qualified_p
9829 = (cp_parser_nested_name_specifier_opt (parser,
9830 /*typename_keyword_p=*/false,
9831 /*check_dependency_p=*/true,
9832 /*type_p=*/false,
9833 /*is_declaration=*/false)
9834 != NULL_TREE);
9835 /* If we have seen a nested-name-specifier, and the next token
9836 is `template', then we are using the template-id production. */
9837 if (parser->scope
9838 && cp_parser_optional_template_keyword (parser))
9840 /* Look for the template-id. */
9841 type = cp_parser_template_id (parser,
9842 /*template_keyword_p=*/true,
9843 /*check_dependency_p=*/true,
9844 /*is_declaration=*/false);
9845 /* If the template-id did not name a type, we are out of
9846 luck. */
9847 if (TREE_CODE (type) != TYPE_DECL)
9849 cp_parser_error (parser, "expected template-id for type");
9850 type = NULL_TREE;
9853 /* Otherwise, look for a type-name. */
9854 else
9855 type = cp_parser_type_name (parser);
9856 /* Keep track of all name-lookups performed in class scopes. */
9857 if (type
9858 && !global_p
9859 && !qualified_p
9860 && TREE_CODE (type) == TYPE_DECL
9861 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9862 maybe_note_name_used_in_class (DECL_NAME (type), type);
9863 /* If it didn't work out, we don't have a TYPE. */
9864 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9865 && !cp_parser_parse_definitely (parser))
9866 type = NULL_TREE;
9867 if (type && decl_specs)
9868 cp_parser_set_decl_spec_type (decl_specs, type,
9869 /*user_defined=*/true);
9872 /* If we didn't get a type-name, issue an error message. */
9873 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9875 cp_parser_error (parser, "expected type-name");
9876 return error_mark_node;
9879 /* There is no valid C++ program where a non-template type is
9880 followed by a "<". That usually indicates that the user thought
9881 that the type was a template. */
9882 if (type && type != error_mark_node)
9884 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9885 If it is, then the '<'...'>' enclose protocol names rather than
9886 template arguments, and so everything is fine. */
9887 if (c_dialect_objc ()
9888 && (objc_is_id (type) || objc_is_class_name (type)))
9890 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9891 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9893 /* Clobber the "unqualified" type previously entered into
9894 DECL_SPECS with the new, improved protocol-qualified version. */
9895 if (decl_specs)
9896 decl_specs->type = qual_type;
9898 return qual_type;
9901 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9904 return type;
9907 /* Parse a type-name.
9909 type-name:
9910 class-name
9911 enum-name
9912 typedef-name
9914 enum-name:
9915 identifier
9917 typedef-name:
9918 identifier
9920 Returns a TYPE_DECL for the type. */
9922 static tree
9923 cp_parser_type_name (cp_parser* parser)
9925 tree type_decl;
9926 tree identifier;
9928 /* We can't know yet whether it is a class-name or not. */
9929 cp_parser_parse_tentatively (parser);
9930 /* Try a class-name. */
9931 type_decl = cp_parser_class_name (parser,
9932 /*typename_keyword_p=*/false,
9933 /*template_keyword_p=*/false,
9934 none_type,
9935 /*check_dependency_p=*/true,
9936 /*class_head_p=*/false,
9937 /*is_declaration=*/false);
9938 /* If it's not a class-name, keep looking. */
9939 if (!cp_parser_parse_definitely (parser))
9941 /* It must be a typedef-name or an enum-name. */
9942 identifier = cp_parser_identifier (parser);
9943 if (identifier == error_mark_node)
9944 return error_mark_node;
9946 /* Look up the type-name. */
9947 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9949 if (TREE_CODE (type_decl) != TYPE_DECL
9950 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9952 /* See if this is an Objective-C type. */
9953 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9954 tree type = objc_get_protocol_qualified_type (identifier, protos);
9955 if (type)
9956 type_decl = TYPE_NAME (type);
9959 /* Issue an error if we did not find a type-name. */
9960 if (TREE_CODE (type_decl) != TYPE_DECL)
9962 if (!cp_parser_simulate_error (parser))
9963 cp_parser_name_lookup_error (parser, identifier, type_decl,
9964 "is not a type");
9965 type_decl = error_mark_node;
9967 /* Remember that the name was used in the definition of the
9968 current class so that we can check later to see if the
9969 meaning would have been different after the class was
9970 entirely defined. */
9971 else if (type_decl != error_mark_node
9972 && !parser->scope)
9973 maybe_note_name_used_in_class (identifier, type_decl);
9976 return type_decl;
9980 /* Parse an elaborated-type-specifier. Note that the grammar given
9981 here incorporates the resolution to DR68.
9983 elaborated-type-specifier:
9984 class-key :: [opt] nested-name-specifier [opt] identifier
9985 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9986 enum :: [opt] nested-name-specifier [opt] identifier
9987 typename :: [opt] nested-name-specifier identifier
9988 typename :: [opt] nested-name-specifier template [opt]
9989 template-id
9991 GNU extension:
9993 elaborated-type-specifier:
9994 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9995 class-key attributes :: [opt] nested-name-specifier [opt]
9996 template [opt] template-id
9997 enum attributes :: [opt] nested-name-specifier [opt] identifier
9999 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10000 declared `friend'. If IS_DECLARATION is TRUE, then this
10001 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10002 something is being declared.
10004 Returns the TYPE specified. */
10006 static tree
10007 cp_parser_elaborated_type_specifier (cp_parser* parser,
10008 bool is_friend,
10009 bool is_declaration)
10011 enum tag_types tag_type;
10012 tree identifier;
10013 tree type = NULL_TREE;
10014 tree attributes = NULL_TREE;
10016 /* See if we're looking at the `enum' keyword. */
10017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10019 /* Consume the `enum' token. */
10020 cp_lexer_consume_token (parser->lexer);
10021 /* Remember that it's an enumeration type. */
10022 tag_type = enum_type;
10023 /* Parse the attributes. */
10024 attributes = cp_parser_attributes_opt (parser);
10026 /* Or, it might be `typename'. */
10027 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10028 RID_TYPENAME))
10030 /* Consume the `typename' token. */
10031 cp_lexer_consume_token (parser->lexer);
10032 /* Remember that it's a `typename' type. */
10033 tag_type = typename_type;
10034 /* The `typename' keyword is only allowed in templates. */
10035 if (!processing_template_decl)
10036 pedwarn ("using %<typename%> outside of template");
10038 /* Otherwise it must be a class-key. */
10039 else
10041 tag_type = cp_parser_class_key (parser);
10042 if (tag_type == none_type)
10043 return error_mark_node;
10044 /* Parse the attributes. */
10045 attributes = cp_parser_attributes_opt (parser);
10048 /* Look for the `::' operator. */
10049 cp_parser_global_scope_opt (parser,
10050 /*current_scope_valid_p=*/false);
10051 /* Look for the nested-name-specifier. */
10052 if (tag_type == typename_type)
10054 if (!cp_parser_nested_name_specifier (parser,
10055 /*typename_keyword_p=*/true,
10056 /*check_dependency_p=*/true,
10057 /*type_p=*/true,
10058 is_declaration))
10059 return error_mark_node;
10061 else
10062 /* Even though `typename' is not present, the proposed resolution
10063 to Core Issue 180 says that in `class A<T>::B', `B' should be
10064 considered a type-name, even if `A<T>' is dependent. */
10065 cp_parser_nested_name_specifier_opt (parser,
10066 /*typename_keyword_p=*/true,
10067 /*check_dependency_p=*/true,
10068 /*type_p=*/true,
10069 is_declaration);
10070 /* For everything but enumeration types, consider a template-id. */
10071 if (tag_type != enum_type)
10073 bool template_p = false;
10074 tree decl;
10076 /* Allow the `template' keyword. */
10077 template_p = cp_parser_optional_template_keyword (parser);
10078 /* If we didn't see `template', we don't know if there's a
10079 template-id or not. */
10080 if (!template_p)
10081 cp_parser_parse_tentatively (parser);
10082 /* Parse the template-id. */
10083 decl = cp_parser_template_id (parser, template_p,
10084 /*check_dependency_p=*/true,
10085 is_declaration);
10086 /* If we didn't find a template-id, look for an ordinary
10087 identifier. */
10088 if (!template_p && !cp_parser_parse_definitely (parser))
10090 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10091 in effect, then we must assume that, upon instantiation, the
10092 template will correspond to a class. */
10093 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10094 && tag_type == typename_type)
10095 type = make_typename_type (parser->scope, decl,
10096 typename_type,
10097 /*complain=*/tf_error);
10098 else
10099 type = TREE_TYPE (decl);
10102 /* For an enumeration type, consider only a plain identifier. */
10103 if (!type)
10105 identifier = cp_parser_identifier (parser);
10107 if (identifier == error_mark_node)
10109 parser->scope = NULL_TREE;
10110 return error_mark_node;
10113 /* For a `typename', we needn't call xref_tag. */
10114 if (tag_type == typename_type
10115 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10116 return cp_parser_make_typename_type (parser, parser->scope,
10117 identifier);
10118 /* Look up a qualified name in the usual way. */
10119 if (parser->scope)
10121 tree decl;
10123 decl = cp_parser_lookup_name (parser, identifier,
10124 tag_type,
10125 /*is_template=*/false,
10126 /*is_namespace=*/false,
10127 /*check_dependency=*/true,
10128 /*ambiguous_decls=*/NULL);
10130 /* If we are parsing friend declaration, DECL may be a
10131 TEMPLATE_DECL tree node here. However, we need to check
10132 whether this TEMPLATE_DECL results in valid code. Consider
10133 the following example:
10135 namespace N {
10136 template <class T> class C {};
10138 class X {
10139 template <class T> friend class N::C; // #1, valid code
10141 template <class T> class Y {
10142 friend class N::C; // #2, invalid code
10145 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10146 name lookup of `N::C'. We see that friend declaration must
10147 be template for the code to be valid. Note that
10148 processing_template_decl does not work here since it is
10149 always 1 for the above two cases. */
10151 decl = (cp_parser_maybe_treat_template_as_class
10152 (decl, /*tag_name_p=*/is_friend
10153 && parser->num_template_parameter_lists));
10155 if (TREE_CODE (decl) != TYPE_DECL)
10157 cp_parser_diagnose_invalid_type_name (parser,
10158 parser->scope,
10159 identifier);
10160 return error_mark_node;
10163 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10164 check_elaborated_type_specifier
10165 (tag_type, decl,
10166 (parser->num_template_parameter_lists
10167 || DECL_SELF_REFERENCE_P (decl)));
10169 type = TREE_TYPE (decl);
10171 else
10173 /* An elaborated-type-specifier sometimes introduces a new type and
10174 sometimes names an existing type. Normally, the rule is that it
10175 introduces a new type only if there is not an existing type of
10176 the same name already in scope. For example, given:
10178 struct S {};
10179 void f() { struct S s; }
10181 the `struct S' in the body of `f' is the same `struct S' as in
10182 the global scope; the existing definition is used. However, if
10183 there were no global declaration, this would introduce a new
10184 local class named `S'.
10186 An exception to this rule applies to the following code:
10188 namespace N { struct S; }
10190 Here, the elaborated-type-specifier names a new type
10191 unconditionally; even if there is already an `S' in the
10192 containing scope this declaration names a new type.
10193 This exception only applies if the elaborated-type-specifier
10194 forms the complete declaration:
10196 [class.name]
10198 A declaration consisting solely of `class-key identifier ;' is
10199 either a redeclaration of the name in the current scope or a
10200 forward declaration of the identifier as a class name. It
10201 introduces the name into the current scope.
10203 We are in this situation precisely when the next token is a `;'.
10205 An exception to the exception is that a `friend' declaration does
10206 *not* name a new type; i.e., given:
10208 struct S { friend struct T; };
10210 `T' is not a new type in the scope of `S'.
10212 Also, `new struct S' or `sizeof (struct S)' never results in the
10213 definition of a new type; a new type can only be declared in a
10214 declaration context. */
10216 tag_scope ts;
10217 bool template_p;
10219 if (is_friend)
10220 /* Friends have special name lookup rules. */
10221 ts = ts_within_enclosing_non_class;
10222 else if (is_declaration
10223 && cp_lexer_next_token_is (parser->lexer,
10224 CPP_SEMICOLON))
10225 /* This is a `class-key identifier ;' */
10226 ts = ts_current;
10227 else
10228 ts = ts_global;
10230 /* Warn about attributes. They are ignored. */
10231 if (attributes)
10232 warning (OPT_Wattributes,
10233 "type attributes are honored only at type definition");
10235 template_p =
10236 (parser->num_template_parameter_lists
10237 && (cp_parser_next_token_starts_class_definition_p (parser)
10238 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10239 /* An unqualified name was used to reference this type, so
10240 there were no qualifying templates. */
10241 if (!cp_parser_check_template_parameters (parser,
10242 /*num_templates=*/0))
10243 return error_mark_node;
10244 type = xref_tag (tag_type, identifier, ts, template_p);
10247 if (tag_type != enum_type)
10248 cp_parser_check_class_key (tag_type, type);
10250 /* A "<" cannot follow an elaborated type specifier. If that
10251 happens, the user was probably trying to form a template-id. */
10252 cp_parser_check_for_invalid_template_id (parser, type);
10254 return type;
10257 /* Parse an enum-specifier.
10259 enum-specifier:
10260 enum identifier [opt] { enumerator-list [opt] }
10262 GNU Extensions:
10263 enum identifier [opt] { enumerator-list [opt] } attributes
10265 Returns an ENUM_TYPE representing the enumeration. */
10267 static tree
10268 cp_parser_enum_specifier (cp_parser* parser)
10270 tree identifier;
10271 tree type;
10273 /* Caller guarantees that the current token is 'enum', an identifier
10274 possibly follows, and the token after that is an opening brace.
10275 If we don't have an identifier, fabricate an anonymous name for
10276 the enumeration being defined. */
10277 cp_lexer_consume_token (parser->lexer);
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10280 identifier = cp_parser_identifier (parser);
10281 else
10282 identifier = make_anon_name ();
10284 /* Issue an error message if type-definitions are forbidden here. */
10285 cp_parser_check_type_definition (parser);
10287 /* Create the new type. We do this before consuming the opening brace
10288 so the enum will be recorded as being on the line of its tag (or the
10289 'enum' keyword, if there is no tag). */
10290 type = start_enum (identifier);
10292 /* Consume the opening brace. */
10293 cp_lexer_consume_token (parser->lexer);
10295 /* If the next token is not '}', then there are some enumerators. */
10296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10297 cp_parser_enumerator_list (parser, type);
10299 /* Consume the final '}'. */
10300 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10302 /* Look for trailing attributes to apply to this enumeration, and
10303 apply them if appropriate. */
10304 if (cp_parser_allow_gnu_extensions_p (parser))
10306 tree trailing_attr = cp_parser_attributes_opt (parser);
10307 cplus_decl_attributes (&type,
10308 trailing_attr,
10309 (int) ATTR_FLAG_TYPE_IN_PLACE);
10312 /* Finish up the enumeration. */
10313 finish_enum (type);
10315 return type;
10318 /* Parse an enumerator-list. The enumerators all have the indicated
10319 TYPE.
10321 enumerator-list:
10322 enumerator-definition
10323 enumerator-list , enumerator-definition */
10325 static void
10326 cp_parser_enumerator_list (cp_parser* parser, tree type)
10328 while (true)
10330 /* Parse an enumerator-definition. */
10331 cp_parser_enumerator_definition (parser, type);
10333 /* If the next token is not a ',', we've reached the end of
10334 the list. */
10335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10336 break;
10337 /* Otherwise, consume the `,' and keep going. */
10338 cp_lexer_consume_token (parser->lexer);
10339 /* If the next token is a `}', there is a trailing comma. */
10340 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10342 if (pedantic && !in_system_header)
10343 pedwarn ("comma at end of enumerator list");
10344 break;
10349 /* Parse an enumerator-definition. The enumerator has the indicated
10350 TYPE.
10352 enumerator-definition:
10353 enumerator
10354 enumerator = constant-expression
10356 enumerator:
10357 identifier */
10359 static void
10360 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10362 tree identifier;
10363 tree value;
10365 /* Look for the identifier. */
10366 identifier = cp_parser_identifier (parser);
10367 if (identifier == error_mark_node)
10368 return;
10370 /* If the next token is an '=', then there is an explicit value. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10373 /* Consume the `=' token. */
10374 cp_lexer_consume_token (parser->lexer);
10375 /* Parse the value. */
10376 value = cp_parser_constant_expression (parser,
10377 /*allow_non_constant_p=*/false,
10378 NULL);
10380 else
10381 value = NULL_TREE;
10383 /* Create the enumerator. */
10384 build_enumerator (identifier, value, type);
10387 /* Parse a namespace-name.
10389 namespace-name:
10390 original-namespace-name
10391 namespace-alias
10393 Returns the NAMESPACE_DECL for the namespace. */
10395 static tree
10396 cp_parser_namespace_name (cp_parser* parser)
10398 tree identifier;
10399 tree namespace_decl;
10401 /* Get the name of the namespace. */
10402 identifier = cp_parser_identifier (parser);
10403 if (identifier == error_mark_node)
10404 return error_mark_node;
10406 /* Look up the identifier in the currently active scope. Look only
10407 for namespaces, due to:
10409 [basic.lookup.udir]
10411 When looking up a namespace-name in a using-directive or alias
10412 definition, only namespace names are considered.
10414 And:
10416 [basic.lookup.qual]
10418 During the lookup of a name preceding the :: scope resolution
10419 operator, object, function, and enumerator names are ignored.
10421 (Note that cp_parser_class_or_namespace_name only calls this
10422 function if the token after the name is the scope resolution
10423 operator.) */
10424 namespace_decl = cp_parser_lookup_name (parser, identifier,
10425 none_type,
10426 /*is_template=*/false,
10427 /*is_namespace=*/true,
10428 /*check_dependency=*/true,
10429 /*ambiguous_decls=*/NULL);
10430 /* If it's not a namespace, issue an error. */
10431 if (namespace_decl == error_mark_node
10432 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10434 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10435 error ("%qD is not a namespace-name", identifier);
10436 cp_parser_error (parser, "expected namespace-name");
10437 namespace_decl = error_mark_node;
10440 return namespace_decl;
10443 /* Parse a namespace-definition.
10445 namespace-definition:
10446 named-namespace-definition
10447 unnamed-namespace-definition
10449 named-namespace-definition:
10450 original-namespace-definition
10451 extension-namespace-definition
10453 original-namespace-definition:
10454 namespace identifier { namespace-body }
10456 extension-namespace-definition:
10457 namespace original-namespace-name { namespace-body }
10459 unnamed-namespace-definition:
10460 namespace { namespace-body } */
10462 static void
10463 cp_parser_namespace_definition (cp_parser* parser)
10465 tree identifier;
10467 /* Look for the `namespace' keyword. */
10468 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10470 /* Get the name of the namespace. We do not attempt to distinguish
10471 between an original-namespace-definition and an
10472 extension-namespace-definition at this point. The semantic
10473 analysis routines are responsible for that. */
10474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10475 identifier = cp_parser_identifier (parser);
10476 else
10477 identifier = NULL_TREE;
10479 /* Look for the `{' to start the namespace. */
10480 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10481 /* Start the namespace. */
10482 push_namespace (identifier);
10483 /* Parse the body of the namespace. */
10484 cp_parser_namespace_body (parser);
10485 /* Finish the namespace. */
10486 pop_namespace ();
10487 /* Look for the final `}'. */
10488 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10491 /* Parse a namespace-body.
10493 namespace-body:
10494 declaration-seq [opt] */
10496 static void
10497 cp_parser_namespace_body (cp_parser* parser)
10499 cp_parser_declaration_seq_opt (parser);
10502 /* Parse a namespace-alias-definition.
10504 namespace-alias-definition:
10505 namespace identifier = qualified-namespace-specifier ; */
10507 static void
10508 cp_parser_namespace_alias_definition (cp_parser* parser)
10510 tree identifier;
10511 tree namespace_specifier;
10513 /* Look for the `namespace' keyword. */
10514 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10515 /* Look for the identifier. */
10516 identifier = cp_parser_identifier (parser);
10517 if (identifier == error_mark_node)
10518 return;
10519 /* Look for the `=' token. */
10520 cp_parser_require (parser, CPP_EQ, "`='");
10521 /* Look for the qualified-namespace-specifier. */
10522 namespace_specifier
10523 = cp_parser_qualified_namespace_specifier (parser);
10524 /* Look for the `;' token. */
10525 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10527 /* Register the alias in the symbol table. */
10528 do_namespace_alias (identifier, namespace_specifier);
10531 /* Parse a qualified-namespace-specifier.
10533 qualified-namespace-specifier:
10534 :: [opt] nested-name-specifier [opt] namespace-name
10536 Returns a NAMESPACE_DECL corresponding to the specified
10537 namespace. */
10539 static tree
10540 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10542 /* Look for the optional `::'. */
10543 cp_parser_global_scope_opt (parser,
10544 /*current_scope_valid_p=*/false);
10546 /* Look for the optional nested-name-specifier. */
10547 cp_parser_nested_name_specifier_opt (parser,
10548 /*typename_keyword_p=*/false,
10549 /*check_dependency_p=*/true,
10550 /*type_p=*/false,
10551 /*is_declaration=*/true);
10553 return cp_parser_namespace_name (parser);
10556 /* Parse a using-declaration.
10558 using-declaration:
10559 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10560 using :: unqualified-id ; */
10562 static void
10563 cp_parser_using_declaration (cp_parser* parser)
10565 cp_token *token;
10566 bool typename_p = false;
10567 bool global_scope_p;
10568 tree decl;
10569 tree identifier;
10570 tree qscope;
10572 /* Look for the `using' keyword. */
10573 cp_parser_require_keyword (parser, RID_USING, "`using'");
10575 /* Peek at the next token. */
10576 token = cp_lexer_peek_token (parser->lexer);
10577 /* See if it's `typename'. */
10578 if (token->keyword == RID_TYPENAME)
10580 /* Remember that we've seen it. */
10581 typename_p = true;
10582 /* Consume the `typename' token. */
10583 cp_lexer_consume_token (parser->lexer);
10586 /* Look for the optional global scope qualification. */
10587 global_scope_p
10588 = (cp_parser_global_scope_opt (parser,
10589 /*current_scope_valid_p=*/false)
10590 != NULL_TREE);
10592 /* If we saw `typename', or didn't see `::', then there must be a
10593 nested-name-specifier present. */
10594 if (typename_p || !global_scope_p)
10595 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10596 /*check_dependency_p=*/true,
10597 /*type_p=*/false,
10598 /*is_declaration=*/true);
10599 /* Otherwise, we could be in either of the two productions. In that
10600 case, treat the nested-name-specifier as optional. */
10601 else
10602 qscope = cp_parser_nested_name_specifier_opt (parser,
10603 /*typename_keyword_p=*/false,
10604 /*check_dependency_p=*/true,
10605 /*type_p=*/false,
10606 /*is_declaration=*/true);
10607 if (!qscope)
10608 qscope = global_namespace;
10610 /* Parse the unqualified-id. */
10611 identifier = cp_parser_unqualified_id (parser,
10612 /*template_keyword_p=*/false,
10613 /*check_dependency_p=*/true,
10614 /*declarator_p=*/true);
10616 /* The function we call to handle a using-declaration is different
10617 depending on what scope we are in. */
10618 if (qscope == error_mark_node || identifier == error_mark_node)
10620 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10621 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10622 /* [namespace.udecl]
10624 A using declaration shall not name a template-id. */
10625 error ("a template-id may not appear in a using-declaration");
10626 else
10628 if (at_class_scope_p ())
10630 /* Create the USING_DECL. */
10631 decl = do_class_using_decl (parser->scope, identifier);
10632 /* Add it to the list of members in this class. */
10633 finish_member_declaration (decl);
10635 else
10637 decl = cp_parser_lookup_name_simple (parser, identifier);
10638 if (decl == error_mark_node)
10639 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10640 else if (!at_namespace_scope_p ())
10641 do_local_using_decl (decl, qscope, identifier);
10642 else
10643 do_toplevel_using_decl (decl, qscope, identifier);
10647 /* Look for the final `;'. */
10648 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10651 /* Parse a using-directive.
10653 using-directive:
10654 using namespace :: [opt] nested-name-specifier [opt]
10655 namespace-name ; */
10657 static void
10658 cp_parser_using_directive (cp_parser* parser)
10660 tree namespace_decl;
10661 tree attribs;
10663 /* Look for the `using' keyword. */
10664 cp_parser_require_keyword (parser, RID_USING, "`using'");
10665 /* And the `namespace' keyword. */
10666 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10667 /* Look for the optional `::' operator. */
10668 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10669 /* And the optional nested-name-specifier. */
10670 cp_parser_nested_name_specifier_opt (parser,
10671 /*typename_keyword_p=*/false,
10672 /*check_dependency_p=*/true,
10673 /*type_p=*/false,
10674 /*is_declaration=*/true);
10675 /* Get the namespace being used. */
10676 namespace_decl = cp_parser_namespace_name (parser);
10677 /* And any specified attributes. */
10678 attribs = cp_parser_attributes_opt (parser);
10679 /* Update the symbol table. */
10680 parse_using_directive (namespace_decl, attribs);
10681 /* Look for the final `;'. */
10682 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10685 /* Parse an asm-definition.
10687 asm-definition:
10688 asm ( string-literal ) ;
10690 GNU Extension:
10692 asm-definition:
10693 asm volatile [opt] ( string-literal ) ;
10694 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10695 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10696 : asm-operand-list [opt] ) ;
10697 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10698 : asm-operand-list [opt]
10699 : asm-operand-list [opt] ) ; */
10701 static void
10702 cp_parser_asm_definition (cp_parser* parser)
10704 tree string;
10705 tree outputs = NULL_TREE;
10706 tree inputs = NULL_TREE;
10707 tree clobbers = NULL_TREE;
10708 tree asm_stmt;
10709 bool volatile_p = false;
10710 bool extended_p = false;
10712 /* Look for the `asm' keyword. */
10713 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10714 /* See if the next token is `volatile'. */
10715 if (cp_parser_allow_gnu_extensions_p (parser)
10716 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10718 /* Remember that we saw the `volatile' keyword. */
10719 volatile_p = true;
10720 /* Consume the token. */
10721 cp_lexer_consume_token (parser->lexer);
10723 /* Look for the opening `('. */
10724 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10725 return;
10726 /* Look for the string. */
10727 string = cp_parser_string_literal (parser, false, false);
10728 if (string == error_mark_node)
10730 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10731 /*consume_paren=*/true);
10732 return;
10735 /* If we're allowing GNU extensions, check for the extended assembly
10736 syntax. Unfortunately, the `:' tokens need not be separated by
10737 a space in C, and so, for compatibility, we tolerate that here
10738 too. Doing that means that we have to treat the `::' operator as
10739 two `:' tokens. */
10740 if (cp_parser_allow_gnu_extensions_p (parser)
10741 && at_function_scope_p ()
10742 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10743 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10745 bool inputs_p = false;
10746 bool clobbers_p = false;
10748 /* The extended syntax was used. */
10749 extended_p = true;
10751 /* Look for outputs. */
10752 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10754 /* Consume the `:'. */
10755 cp_lexer_consume_token (parser->lexer);
10756 /* Parse the output-operands. */
10757 if (cp_lexer_next_token_is_not (parser->lexer,
10758 CPP_COLON)
10759 && cp_lexer_next_token_is_not (parser->lexer,
10760 CPP_SCOPE)
10761 && cp_lexer_next_token_is_not (parser->lexer,
10762 CPP_CLOSE_PAREN))
10763 outputs = cp_parser_asm_operand_list (parser);
10765 /* If the next token is `::', there are no outputs, and the
10766 next token is the beginning of the inputs. */
10767 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10768 /* The inputs are coming next. */
10769 inputs_p = true;
10771 /* Look for inputs. */
10772 if (inputs_p
10773 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10775 /* Consume the `:' or `::'. */
10776 cp_lexer_consume_token (parser->lexer);
10777 /* Parse the output-operands. */
10778 if (cp_lexer_next_token_is_not (parser->lexer,
10779 CPP_COLON)
10780 && cp_lexer_next_token_is_not (parser->lexer,
10781 CPP_CLOSE_PAREN))
10782 inputs = cp_parser_asm_operand_list (parser);
10784 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10785 /* The clobbers are coming next. */
10786 clobbers_p = true;
10788 /* Look for clobbers. */
10789 if (clobbers_p
10790 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10792 /* Consume the `:' or `::'. */
10793 cp_lexer_consume_token (parser->lexer);
10794 /* Parse the clobbers. */
10795 if (cp_lexer_next_token_is_not (parser->lexer,
10796 CPP_CLOSE_PAREN))
10797 clobbers = cp_parser_asm_clobber_list (parser);
10800 /* Look for the closing `)'. */
10801 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10802 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10803 /*consume_paren=*/true);
10804 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10806 /* Create the ASM_EXPR. */
10807 if (at_function_scope_p ())
10809 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10810 inputs, clobbers);
10811 /* If the extended syntax was not used, mark the ASM_EXPR. */
10812 if (!extended_p)
10814 tree temp = asm_stmt;
10815 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10816 temp = TREE_OPERAND (temp, 0);
10818 ASM_INPUT_P (temp) = 1;
10821 else
10822 cgraph_add_asm_node (string);
10825 /* Declarators [gram.dcl.decl] */
10827 /* Parse an init-declarator.
10829 init-declarator:
10830 declarator initializer [opt]
10832 GNU Extension:
10834 init-declarator:
10835 declarator asm-specification [opt] attributes [opt] initializer [opt]
10837 function-definition:
10838 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10839 function-body
10840 decl-specifier-seq [opt] declarator function-try-block
10842 GNU Extension:
10844 function-definition:
10845 __extension__ function-definition
10847 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10848 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10849 then this declarator appears in a class scope. The new DECL created
10850 by this declarator is returned.
10852 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10853 for a function-definition here as well. If the declarator is a
10854 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10855 be TRUE upon return. By that point, the function-definition will
10856 have been completely parsed.
10858 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10859 is FALSE. */
10861 static tree
10862 cp_parser_init_declarator (cp_parser* parser,
10863 cp_decl_specifier_seq *decl_specifiers,
10864 bool function_definition_allowed_p,
10865 bool member_p,
10866 int declares_class_or_enum,
10867 bool* function_definition_p)
10869 cp_token *token;
10870 cp_declarator *declarator;
10871 tree prefix_attributes;
10872 tree attributes;
10873 tree asm_specification;
10874 tree initializer;
10875 tree decl = NULL_TREE;
10876 tree scope;
10877 bool is_initialized;
10878 bool is_parenthesized_init;
10879 bool is_non_constant_init;
10880 int ctor_dtor_or_conv_p;
10881 bool friend_p;
10882 tree pushed_scope = NULL;
10884 /* Gather the attributes that were provided with the
10885 decl-specifiers. */
10886 prefix_attributes = decl_specifiers->attributes;
10888 /* Assume that this is not the declarator for a function
10889 definition. */
10890 if (function_definition_p)
10891 *function_definition_p = false;
10893 /* Defer access checks while parsing the declarator; we cannot know
10894 what names are accessible until we know what is being
10895 declared. */
10896 resume_deferring_access_checks ();
10898 /* Parse the declarator. */
10899 declarator
10900 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10901 &ctor_dtor_or_conv_p,
10902 /*parenthesized_p=*/NULL,
10903 /*member_p=*/false);
10904 /* Gather up the deferred checks. */
10905 stop_deferring_access_checks ();
10907 /* If the DECLARATOR was erroneous, there's no need to go
10908 further. */
10909 if (declarator == cp_error_declarator)
10910 return error_mark_node;
10912 if (declares_class_or_enum & 2)
10913 cp_parser_check_for_definition_in_return_type (declarator,
10914 decl_specifiers->type);
10916 /* Figure out what scope the entity declared by the DECLARATOR is
10917 located in. `grokdeclarator' sometimes changes the scope, so
10918 we compute it now. */
10919 scope = get_scope_of_declarator (declarator);
10921 /* If we're allowing GNU extensions, look for an asm-specification
10922 and attributes. */
10923 if (cp_parser_allow_gnu_extensions_p (parser))
10925 /* Look for an asm-specification. */
10926 asm_specification = cp_parser_asm_specification_opt (parser);
10927 /* And attributes. */
10928 attributes = cp_parser_attributes_opt (parser);
10930 else
10932 asm_specification = NULL_TREE;
10933 attributes = NULL_TREE;
10936 /* Peek at the next token. */
10937 token = cp_lexer_peek_token (parser->lexer);
10938 /* Check to see if the token indicates the start of a
10939 function-definition. */
10940 if (cp_parser_token_starts_function_definition_p (token))
10942 if (!function_definition_allowed_p)
10944 /* If a function-definition should not appear here, issue an
10945 error message. */
10946 cp_parser_error (parser,
10947 "a function-definition is not allowed here");
10948 return error_mark_node;
10950 else
10952 /* Neither attributes nor an asm-specification are allowed
10953 on a function-definition. */
10954 if (asm_specification)
10955 error ("an asm-specification is not allowed on a function-definition");
10956 if (attributes)
10957 error ("attributes are not allowed on a function-definition");
10958 /* This is a function-definition. */
10959 *function_definition_p = true;
10961 /* Parse the function definition. */
10962 if (member_p)
10963 decl = cp_parser_save_member_function_body (parser,
10964 decl_specifiers,
10965 declarator,
10966 prefix_attributes);
10967 else
10968 decl
10969 = (cp_parser_function_definition_from_specifiers_and_declarator
10970 (parser, decl_specifiers, prefix_attributes, declarator));
10972 return decl;
10976 /* [dcl.dcl]
10978 Only in function declarations for constructors, destructors, and
10979 type conversions can the decl-specifier-seq be omitted.
10981 We explicitly postpone this check past the point where we handle
10982 function-definitions because we tolerate function-definitions
10983 that are missing their return types in some modes. */
10984 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10986 cp_parser_error (parser,
10987 "expected constructor, destructor, or type conversion");
10988 return error_mark_node;
10991 /* An `=' or an `(' indicates an initializer. */
10992 is_initialized = (token->type == CPP_EQ
10993 || token->type == CPP_OPEN_PAREN);
10994 /* If the init-declarator isn't initialized and isn't followed by a
10995 `,' or `;', it's not a valid init-declarator. */
10996 if (!is_initialized
10997 && token->type != CPP_COMMA
10998 && token->type != CPP_SEMICOLON)
11000 cp_parser_error (parser, "expected initializer");
11001 return error_mark_node;
11004 /* Because start_decl has side-effects, we should only call it if we
11005 know we're going ahead. By this point, we know that we cannot
11006 possibly be looking at any other construct. */
11007 cp_parser_commit_to_tentative_parse (parser);
11009 /* If the decl specifiers were bad, issue an error now that we're
11010 sure this was intended to be a declarator. Then continue
11011 declaring the variable(s), as int, to try to cut down on further
11012 errors. */
11013 if (decl_specifiers->any_specifiers_p
11014 && decl_specifiers->type == error_mark_node)
11016 cp_parser_error (parser, "invalid type in declaration");
11017 decl_specifiers->type = integer_type_node;
11020 /* Check to see whether or not this declaration is a friend. */
11021 friend_p = cp_parser_friend_p (decl_specifiers);
11023 /* Check that the number of template-parameter-lists is OK. */
11024 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11025 return error_mark_node;
11027 /* Enter the newly declared entry in the symbol table. If we're
11028 processing a declaration in a class-specifier, we wait until
11029 after processing the initializer. */
11030 if (!member_p)
11032 if (parser->in_unbraced_linkage_specification_p)
11034 decl_specifiers->storage_class = sc_extern;
11035 have_extern_spec = false;
11037 decl = start_decl (declarator, decl_specifiers,
11038 is_initialized, attributes, prefix_attributes,
11039 &pushed_scope);
11041 else if (scope)
11042 /* Enter the SCOPE. That way unqualified names appearing in the
11043 initializer will be looked up in SCOPE. */
11044 pushed_scope = push_scope (scope);
11046 /* Perform deferred access control checks, now that we know in which
11047 SCOPE the declared entity resides. */
11048 if (!member_p && decl)
11050 tree saved_current_function_decl = NULL_TREE;
11052 /* If the entity being declared is a function, pretend that we
11053 are in its scope. If it is a `friend', it may have access to
11054 things that would not otherwise be accessible. */
11055 if (TREE_CODE (decl) == FUNCTION_DECL)
11057 saved_current_function_decl = current_function_decl;
11058 current_function_decl = decl;
11061 /* Perform the access control checks for the declarator and the
11062 the decl-specifiers. */
11063 perform_deferred_access_checks ();
11065 /* Restore the saved value. */
11066 if (TREE_CODE (decl) == FUNCTION_DECL)
11067 current_function_decl = saved_current_function_decl;
11070 /* Parse the initializer. */
11071 if (is_initialized)
11072 initializer = cp_parser_initializer (parser,
11073 &is_parenthesized_init,
11074 &is_non_constant_init);
11075 else
11077 initializer = NULL_TREE;
11078 is_parenthesized_init = false;
11079 is_non_constant_init = true;
11082 /* The old parser allows attributes to appear after a parenthesized
11083 initializer. Mark Mitchell proposed removing this functionality
11084 on the GCC mailing lists on 2002-08-13. This parser accepts the
11085 attributes -- but ignores them. */
11086 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11087 if (cp_parser_attributes_opt (parser))
11088 warning (OPT_Wattributes,
11089 "attributes after parenthesized initializer ignored");
11091 /* For an in-class declaration, use `grokfield' to create the
11092 declaration. */
11093 if (member_p)
11095 if (pushed_scope)
11097 pop_scope (pushed_scope);
11098 pushed_scope = false;
11100 decl = grokfield (declarator, decl_specifiers,
11101 initializer, /*asmspec=*/NULL_TREE,
11102 prefix_attributes);
11103 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11104 cp_parser_save_default_args (parser, decl);
11107 /* Finish processing the declaration. But, skip friend
11108 declarations. */
11109 if (!friend_p && decl && decl != error_mark_node)
11111 cp_finish_decl (decl,
11112 initializer,
11113 asm_specification,
11114 /* If the initializer is in parentheses, then this is
11115 a direct-initialization, which means that an
11116 `explicit' constructor is OK. Otherwise, an
11117 `explicit' constructor cannot be used. */
11118 ((is_parenthesized_init || !is_initialized)
11119 ? 0 : LOOKUP_ONLYCONVERTING));
11121 if (!friend_p && pushed_scope)
11122 pop_scope (pushed_scope);
11124 /* Remember whether or not variables were initialized by
11125 constant-expressions. */
11126 if (decl && TREE_CODE (decl) == VAR_DECL
11127 && is_initialized && !is_non_constant_init)
11128 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11130 return decl;
11133 /* Parse a declarator.
11135 declarator:
11136 direct-declarator
11137 ptr-operator declarator
11139 abstract-declarator:
11140 ptr-operator abstract-declarator [opt]
11141 direct-abstract-declarator
11143 GNU Extensions:
11145 declarator:
11146 attributes [opt] direct-declarator
11147 attributes [opt] ptr-operator declarator
11149 abstract-declarator:
11150 attributes [opt] ptr-operator abstract-declarator [opt]
11151 attributes [opt] direct-abstract-declarator
11153 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11154 detect constructor, destructor or conversion operators. It is set
11155 to -1 if the declarator is a name, and +1 if it is a
11156 function. Otherwise it is set to zero. Usually you just want to
11157 test for >0, but internally the negative value is used.
11159 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11160 a decl-specifier-seq unless it declares a constructor, destructor,
11161 or conversion. It might seem that we could check this condition in
11162 semantic analysis, rather than parsing, but that makes it difficult
11163 to handle something like `f()'. We want to notice that there are
11164 no decl-specifiers, and therefore realize that this is an
11165 expression, not a declaration.)
11167 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11168 the declarator is a direct-declarator of the form "(...)".
11170 MEMBER_P is true iff this declarator is a member-declarator. */
11172 static cp_declarator *
11173 cp_parser_declarator (cp_parser* parser,
11174 cp_parser_declarator_kind dcl_kind,
11175 int* ctor_dtor_or_conv_p,
11176 bool* parenthesized_p,
11177 bool member_p)
11179 cp_token *token;
11180 cp_declarator *declarator;
11181 enum tree_code code;
11182 cp_cv_quals cv_quals;
11183 tree class_type;
11184 tree attributes = NULL_TREE;
11186 /* Assume this is not a constructor, destructor, or type-conversion
11187 operator. */
11188 if (ctor_dtor_or_conv_p)
11189 *ctor_dtor_or_conv_p = 0;
11191 if (cp_parser_allow_gnu_extensions_p (parser))
11192 attributes = cp_parser_attributes_opt (parser);
11194 /* Peek at the next token. */
11195 token = cp_lexer_peek_token (parser->lexer);
11197 /* Check for the ptr-operator production. */
11198 cp_parser_parse_tentatively (parser);
11199 /* Parse the ptr-operator. */
11200 code = cp_parser_ptr_operator (parser,
11201 &class_type,
11202 &cv_quals);
11203 /* If that worked, then we have a ptr-operator. */
11204 if (cp_parser_parse_definitely (parser))
11206 /* If a ptr-operator was found, then this declarator was not
11207 parenthesized. */
11208 if (parenthesized_p)
11209 *parenthesized_p = true;
11210 /* The dependent declarator is optional if we are parsing an
11211 abstract-declarator. */
11212 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11213 cp_parser_parse_tentatively (parser);
11215 /* Parse the dependent declarator. */
11216 declarator = cp_parser_declarator (parser, dcl_kind,
11217 /*ctor_dtor_or_conv_p=*/NULL,
11218 /*parenthesized_p=*/NULL,
11219 /*member_p=*/false);
11221 /* If we are parsing an abstract-declarator, we must handle the
11222 case where the dependent declarator is absent. */
11223 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11224 && !cp_parser_parse_definitely (parser))
11225 declarator = NULL;
11227 /* Build the representation of the ptr-operator. */
11228 if (class_type)
11229 declarator = make_ptrmem_declarator (cv_quals,
11230 class_type,
11231 declarator);
11232 else if (code == INDIRECT_REF)
11233 declarator = make_pointer_declarator (cv_quals, declarator);
11234 else
11235 declarator = make_reference_declarator (cv_quals, declarator);
11237 /* Everything else is a direct-declarator. */
11238 else
11240 if (parenthesized_p)
11241 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11242 CPP_OPEN_PAREN);
11243 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11244 ctor_dtor_or_conv_p,
11245 member_p);
11248 if (attributes && declarator != cp_error_declarator)
11249 declarator->attributes = attributes;
11251 return declarator;
11254 /* Parse a direct-declarator or direct-abstract-declarator.
11256 direct-declarator:
11257 declarator-id
11258 direct-declarator ( parameter-declaration-clause )
11259 cv-qualifier-seq [opt]
11260 exception-specification [opt]
11261 direct-declarator [ constant-expression [opt] ]
11262 ( declarator )
11264 direct-abstract-declarator:
11265 direct-abstract-declarator [opt]
11266 ( parameter-declaration-clause )
11267 cv-qualifier-seq [opt]
11268 exception-specification [opt]
11269 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11270 ( abstract-declarator )
11272 Returns a representation of the declarator. DCL_KIND is
11273 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11274 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11275 we are parsing a direct-declarator. It is
11276 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11277 of ambiguity we prefer an abstract declarator, as per
11278 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11279 cp_parser_declarator. */
11281 static cp_declarator *
11282 cp_parser_direct_declarator (cp_parser* parser,
11283 cp_parser_declarator_kind dcl_kind,
11284 int* ctor_dtor_or_conv_p,
11285 bool member_p)
11287 cp_token *token;
11288 cp_declarator *declarator = NULL;
11289 tree scope = NULL_TREE;
11290 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11291 bool saved_in_declarator_p = parser->in_declarator_p;
11292 bool first = true;
11293 tree pushed_scope = NULL_TREE;
11295 while (true)
11297 /* Peek at the next token. */
11298 token = cp_lexer_peek_token (parser->lexer);
11299 if (token->type == CPP_OPEN_PAREN)
11301 /* This is either a parameter-declaration-clause, or a
11302 parenthesized declarator. When we know we are parsing a
11303 named declarator, it must be a parenthesized declarator
11304 if FIRST is true. For instance, `(int)' is a
11305 parameter-declaration-clause, with an omitted
11306 direct-abstract-declarator. But `((*))', is a
11307 parenthesized abstract declarator. Finally, when T is a
11308 template parameter `(T)' is a
11309 parameter-declaration-clause, and not a parenthesized
11310 named declarator.
11312 We first try and parse a parameter-declaration-clause,
11313 and then try a nested declarator (if FIRST is true).
11315 It is not an error for it not to be a
11316 parameter-declaration-clause, even when FIRST is
11317 false. Consider,
11319 int i (int);
11320 int i (3);
11322 The first is the declaration of a function while the
11323 second is a the definition of a variable, including its
11324 initializer.
11326 Having seen only the parenthesis, we cannot know which of
11327 these two alternatives should be selected. Even more
11328 complex are examples like:
11330 int i (int (a));
11331 int i (int (3));
11333 The former is a function-declaration; the latter is a
11334 variable initialization.
11336 Thus again, we try a parameter-declaration-clause, and if
11337 that fails, we back out and return. */
11339 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11341 cp_parameter_declarator *params;
11342 unsigned saved_num_template_parameter_lists;
11344 /* In a member-declarator, the only valid interpretation
11345 of a parenthesis is the start of a
11346 parameter-declaration-clause. (It is invalid to
11347 initialize a static data member with a parenthesized
11348 initializer; only the "=" form of initialization is
11349 permitted.) */
11350 if (!member_p)
11351 cp_parser_parse_tentatively (parser);
11353 /* Consume the `('. */
11354 cp_lexer_consume_token (parser->lexer);
11355 if (first)
11357 /* If this is going to be an abstract declarator, we're
11358 in a declarator and we can't have default args. */
11359 parser->default_arg_ok_p = false;
11360 parser->in_declarator_p = true;
11363 /* Inside the function parameter list, surrounding
11364 template-parameter-lists do not apply. */
11365 saved_num_template_parameter_lists
11366 = parser->num_template_parameter_lists;
11367 parser->num_template_parameter_lists = 0;
11369 /* Parse the parameter-declaration-clause. */
11370 params = cp_parser_parameter_declaration_clause (parser);
11372 parser->num_template_parameter_lists
11373 = saved_num_template_parameter_lists;
11375 /* If all went well, parse the cv-qualifier-seq and the
11376 exception-specification. */
11377 if (member_p || cp_parser_parse_definitely (parser))
11379 cp_cv_quals cv_quals;
11380 tree exception_specification;
11382 if (ctor_dtor_or_conv_p)
11383 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11384 first = false;
11385 /* Consume the `)'. */
11386 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11388 /* Parse the cv-qualifier-seq. */
11389 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11390 /* And the exception-specification. */
11391 exception_specification
11392 = cp_parser_exception_specification_opt (parser);
11394 /* Create the function-declarator. */
11395 declarator = make_call_declarator (declarator,
11396 params,
11397 cv_quals,
11398 exception_specification);
11399 /* Any subsequent parameter lists are to do with
11400 return type, so are not those of the declared
11401 function. */
11402 parser->default_arg_ok_p = false;
11404 /* Repeat the main loop. */
11405 continue;
11409 /* If this is the first, we can try a parenthesized
11410 declarator. */
11411 if (first)
11413 bool saved_in_type_id_in_expr_p;
11415 parser->default_arg_ok_p = saved_default_arg_ok_p;
11416 parser->in_declarator_p = saved_in_declarator_p;
11418 /* Consume the `('. */
11419 cp_lexer_consume_token (parser->lexer);
11420 /* Parse the nested declarator. */
11421 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11422 parser->in_type_id_in_expr_p = true;
11423 declarator
11424 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11425 /*parenthesized_p=*/NULL,
11426 member_p);
11427 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11428 first = false;
11429 /* Expect a `)'. */
11430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11431 declarator = cp_error_declarator;
11432 if (declarator == cp_error_declarator)
11433 break;
11435 goto handle_declarator;
11437 /* Otherwise, we must be done. */
11438 else
11439 break;
11441 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11442 && token->type == CPP_OPEN_SQUARE)
11444 /* Parse an array-declarator. */
11445 tree bounds;
11447 if (ctor_dtor_or_conv_p)
11448 *ctor_dtor_or_conv_p = 0;
11450 first = false;
11451 parser->default_arg_ok_p = false;
11452 parser->in_declarator_p = true;
11453 /* Consume the `['. */
11454 cp_lexer_consume_token (parser->lexer);
11455 /* Peek at the next token. */
11456 token = cp_lexer_peek_token (parser->lexer);
11457 /* If the next token is `]', then there is no
11458 constant-expression. */
11459 if (token->type != CPP_CLOSE_SQUARE)
11461 bool non_constant_p;
11463 bounds
11464 = cp_parser_constant_expression (parser,
11465 /*allow_non_constant=*/true,
11466 &non_constant_p);
11467 if (!non_constant_p)
11468 bounds = fold_non_dependent_expr (bounds);
11469 /* Normally, the array bound must be an integral constant
11470 expression. However, as an extension, we allow VLAs
11471 in function scopes. */
11472 else if (!at_function_scope_p ())
11474 error ("array bound is not an integer constant");
11475 bounds = error_mark_node;
11478 else
11479 bounds = NULL_TREE;
11480 /* Look for the closing `]'. */
11481 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11483 declarator = cp_error_declarator;
11484 break;
11487 declarator = make_array_declarator (declarator, bounds);
11489 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11491 tree qualifying_scope;
11492 tree unqualified_name;
11493 special_function_kind sfk;
11495 /* Parse a declarator-id */
11496 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11497 cp_parser_parse_tentatively (parser);
11498 unqualified_name = cp_parser_declarator_id (parser);
11499 qualifying_scope = parser->scope;
11500 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11502 if (!cp_parser_parse_definitely (parser))
11503 unqualified_name = error_mark_node;
11504 else if (qualifying_scope
11505 || (TREE_CODE (unqualified_name)
11506 != IDENTIFIER_NODE))
11508 cp_parser_error (parser, "expected unqualified-id");
11509 unqualified_name = error_mark_node;
11513 if (unqualified_name == error_mark_node)
11515 declarator = cp_error_declarator;
11516 break;
11519 if (qualifying_scope && at_namespace_scope_p ()
11520 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11522 /* In the declaration of a member of a template class
11523 outside of the class itself, the SCOPE will sometimes
11524 be a TYPENAME_TYPE. For example, given:
11526 template <typename T>
11527 int S<T>::R::i = 3;
11529 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11530 this context, we must resolve S<T>::R to an ordinary
11531 type, rather than a typename type.
11533 The reason we normally avoid resolving TYPENAME_TYPEs
11534 is that a specialization of `S' might render
11535 `S<T>::R' not a type. However, if `S' is
11536 specialized, then this `i' will not be used, so there
11537 is no harm in resolving the types here. */
11538 tree type;
11540 /* Resolve the TYPENAME_TYPE. */
11541 type = resolve_typename_type (qualifying_scope,
11542 /*only_current_p=*/false);
11543 /* If that failed, the declarator is invalid. */
11544 if (type == error_mark_node)
11545 error ("%<%T::%D%> is not a type",
11546 TYPE_CONTEXT (qualifying_scope),
11547 TYPE_IDENTIFIER (qualifying_scope));
11548 qualifying_scope = type;
11551 sfk = sfk_none;
11552 if (unqualified_name)
11554 tree class_type;
11556 if (qualifying_scope
11557 && CLASS_TYPE_P (qualifying_scope))
11558 class_type = qualifying_scope;
11559 else
11560 class_type = current_class_type;
11562 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11564 tree name_type = TREE_TYPE (unqualified_name);
11565 if (class_type && same_type_p (name_type, class_type))
11567 if (qualifying_scope
11568 && CLASSTYPE_USE_TEMPLATE (name_type))
11570 error ("invalid use of constructor as a template");
11571 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11572 "name the constructor in a qualified name",
11573 class_type,
11574 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11575 class_type, name_type);
11576 declarator = cp_error_declarator;
11577 break;
11579 else
11580 unqualified_name = constructor_name (class_type);
11582 else
11584 /* We do not attempt to print the declarator
11585 here because we do not have enough
11586 information about its original syntactic
11587 form. */
11588 cp_parser_error (parser, "invalid declarator");
11589 declarator = cp_error_declarator;
11590 break;
11594 if (class_type)
11596 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11597 sfk = sfk_destructor;
11598 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11599 sfk = sfk_conversion;
11600 else if (/* There's no way to declare a constructor
11601 for an anonymous type, even if the type
11602 got a name for linkage purposes. */
11603 !TYPE_WAS_ANONYMOUS (class_type)
11604 && constructor_name_p (unqualified_name,
11605 class_type))
11607 unqualified_name = constructor_name (class_type);
11608 sfk = sfk_constructor;
11611 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11612 *ctor_dtor_or_conv_p = -1;
11615 declarator = make_id_declarator (qualifying_scope,
11616 unqualified_name,
11617 sfk);
11618 declarator->id_loc = token->location;
11620 handle_declarator:;
11621 scope = get_scope_of_declarator (declarator);
11622 if (scope)
11623 /* Any names that appear after the declarator-id for a
11624 member are looked up in the containing scope. */
11625 pushed_scope = push_scope (scope);
11626 parser->in_declarator_p = true;
11627 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11628 || (declarator && declarator->kind == cdk_id))
11629 /* Default args are only allowed on function
11630 declarations. */
11631 parser->default_arg_ok_p = saved_default_arg_ok_p;
11632 else
11633 parser->default_arg_ok_p = false;
11635 first = false;
11637 /* We're done. */
11638 else
11639 break;
11642 /* For an abstract declarator, we might wind up with nothing at this
11643 point. That's an error; the declarator is not optional. */
11644 if (!declarator)
11645 cp_parser_error (parser, "expected declarator");
11647 /* If we entered a scope, we must exit it now. */
11648 if (pushed_scope)
11649 pop_scope (pushed_scope);
11651 parser->default_arg_ok_p = saved_default_arg_ok_p;
11652 parser->in_declarator_p = saved_in_declarator_p;
11654 return declarator;
11657 /* Parse a ptr-operator.
11659 ptr-operator:
11660 * cv-qualifier-seq [opt]
11662 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11664 GNU Extension:
11666 ptr-operator:
11667 & cv-qualifier-seq [opt]
11669 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11670 Returns ADDR_EXPR if a reference was used. In the case of a
11671 pointer-to-member, *TYPE is filled in with the TYPE containing the
11672 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11673 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11674 ERROR_MARK if an error occurred. */
11676 static enum tree_code
11677 cp_parser_ptr_operator (cp_parser* parser,
11678 tree* type,
11679 cp_cv_quals *cv_quals)
11681 enum tree_code code = ERROR_MARK;
11682 cp_token *token;
11684 /* Assume that it's not a pointer-to-member. */
11685 *type = NULL_TREE;
11686 /* And that there are no cv-qualifiers. */
11687 *cv_quals = TYPE_UNQUALIFIED;
11689 /* Peek at the next token. */
11690 token = cp_lexer_peek_token (parser->lexer);
11691 /* If it's a `*' or `&' we have a pointer or reference. */
11692 if (token->type == CPP_MULT || token->type == CPP_AND)
11694 /* Remember which ptr-operator we were processing. */
11695 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11697 /* Consume the `*' or `&'. */
11698 cp_lexer_consume_token (parser->lexer);
11700 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11701 `&', if we are allowing GNU extensions. (The only qualifier
11702 that can legally appear after `&' is `restrict', but that is
11703 enforced during semantic analysis. */
11704 if (code == INDIRECT_REF
11705 || cp_parser_allow_gnu_extensions_p (parser))
11706 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11708 else
11710 /* Try the pointer-to-member case. */
11711 cp_parser_parse_tentatively (parser);
11712 /* Look for the optional `::' operator. */
11713 cp_parser_global_scope_opt (parser,
11714 /*current_scope_valid_p=*/false);
11715 /* Look for the nested-name specifier. */
11716 cp_parser_nested_name_specifier (parser,
11717 /*typename_keyword_p=*/false,
11718 /*check_dependency_p=*/true,
11719 /*type_p=*/false,
11720 /*is_declaration=*/false);
11721 /* If we found it, and the next token is a `*', then we are
11722 indeed looking at a pointer-to-member operator. */
11723 if (!cp_parser_error_occurred (parser)
11724 && cp_parser_require (parser, CPP_MULT, "`*'"))
11726 /* The type of which the member is a member is given by the
11727 current SCOPE. */
11728 *type = parser->scope;
11729 /* The next name will not be qualified. */
11730 parser->scope = NULL_TREE;
11731 parser->qualifying_scope = NULL_TREE;
11732 parser->object_scope = NULL_TREE;
11733 /* Indicate that the `*' operator was used. */
11734 code = INDIRECT_REF;
11735 /* Look for the optional cv-qualifier-seq. */
11736 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11738 /* If that didn't work we don't have a ptr-operator. */
11739 if (!cp_parser_parse_definitely (parser))
11740 cp_parser_error (parser, "expected ptr-operator");
11743 return code;
11746 /* Parse an (optional) cv-qualifier-seq.
11748 cv-qualifier-seq:
11749 cv-qualifier cv-qualifier-seq [opt]
11751 cv-qualifier:
11752 const
11753 volatile
11755 GNU Extension:
11757 cv-qualifier:
11758 __restrict__
11760 Returns a bitmask representing the cv-qualifiers. */
11762 static cp_cv_quals
11763 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11765 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11767 while (true)
11769 cp_token *token;
11770 cp_cv_quals cv_qualifier;
11772 /* Peek at the next token. */
11773 token = cp_lexer_peek_token (parser->lexer);
11774 /* See if it's a cv-qualifier. */
11775 switch (token->keyword)
11777 case RID_CONST:
11778 cv_qualifier = TYPE_QUAL_CONST;
11779 break;
11781 case RID_VOLATILE:
11782 cv_qualifier = TYPE_QUAL_VOLATILE;
11783 break;
11785 case RID_RESTRICT:
11786 cv_qualifier = TYPE_QUAL_RESTRICT;
11787 break;
11789 default:
11790 cv_qualifier = TYPE_UNQUALIFIED;
11791 break;
11794 if (!cv_qualifier)
11795 break;
11797 if (cv_quals & cv_qualifier)
11799 error ("duplicate cv-qualifier");
11800 cp_lexer_purge_token (parser->lexer);
11802 else
11804 cp_lexer_consume_token (parser->lexer);
11805 cv_quals |= cv_qualifier;
11809 return cv_quals;
11812 /* Parse a declarator-id.
11814 declarator-id:
11815 id-expression
11816 :: [opt] nested-name-specifier [opt] type-name
11818 In the `id-expression' case, the value returned is as for
11819 cp_parser_id_expression if the id-expression was an unqualified-id.
11820 If the id-expression was a qualified-id, then a SCOPE_REF is
11821 returned. The first operand is the scope (either a NAMESPACE_DECL
11822 or TREE_TYPE), but the second is still just a representation of an
11823 unqualified-id. */
11825 static tree
11826 cp_parser_declarator_id (cp_parser* parser)
11828 tree id;
11829 /* The expression must be an id-expression. Assume that qualified
11830 names are the names of types so that:
11832 template <class T>
11833 int S<T>::R::i = 3;
11835 will work; we must treat `S<T>::R' as the name of a type.
11836 Similarly, assume that qualified names are templates, where
11837 required, so that:
11839 template <class T>
11840 int S<T>::R<T>::i = 3;
11842 will work, too. */
11843 id = cp_parser_id_expression (parser,
11844 /*template_keyword_p=*/false,
11845 /*check_dependency_p=*/false,
11846 /*template_p=*/NULL,
11847 /*declarator_p=*/true);
11848 if (BASELINK_P (id))
11849 id = BASELINK_FUNCTIONS (id);
11850 return id;
11853 /* Parse a type-id.
11855 type-id:
11856 type-specifier-seq abstract-declarator [opt]
11858 Returns the TYPE specified. */
11860 static tree
11861 cp_parser_type_id (cp_parser* parser)
11863 cp_decl_specifier_seq type_specifier_seq;
11864 cp_declarator *abstract_declarator;
11866 /* Parse the type-specifier-seq. */
11867 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11868 &type_specifier_seq);
11869 if (type_specifier_seq.type == error_mark_node)
11870 return error_mark_node;
11872 /* There might or might not be an abstract declarator. */
11873 cp_parser_parse_tentatively (parser);
11874 /* Look for the declarator. */
11875 abstract_declarator
11876 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11877 /*parenthesized_p=*/NULL,
11878 /*member_p=*/false);
11879 /* Check to see if there really was a declarator. */
11880 if (!cp_parser_parse_definitely (parser))
11881 abstract_declarator = NULL;
11883 return groktypename (&type_specifier_seq, abstract_declarator);
11886 /* Parse a type-specifier-seq.
11888 type-specifier-seq:
11889 type-specifier type-specifier-seq [opt]
11891 GNU extension:
11893 type-specifier-seq:
11894 attributes type-specifier-seq [opt]
11896 If IS_CONDITION is true, we are at the start of a "condition",
11897 e.g., we've just seen "if (".
11899 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11901 static void
11902 cp_parser_type_specifier_seq (cp_parser* parser,
11903 bool is_condition,
11904 cp_decl_specifier_seq *type_specifier_seq)
11906 bool seen_type_specifier = false;
11907 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11909 /* Clear the TYPE_SPECIFIER_SEQ. */
11910 clear_decl_specs (type_specifier_seq);
11912 /* Parse the type-specifiers and attributes. */
11913 while (true)
11915 tree type_specifier;
11916 bool is_cv_qualifier;
11918 /* Check for attributes first. */
11919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11921 type_specifier_seq->attributes =
11922 chainon (type_specifier_seq->attributes,
11923 cp_parser_attributes_opt (parser));
11924 continue;
11927 /* Look for the type-specifier. */
11928 type_specifier = cp_parser_type_specifier (parser,
11929 flags,
11930 type_specifier_seq,
11931 /*is_declaration=*/false,
11932 NULL,
11933 &is_cv_qualifier);
11934 if (!type_specifier)
11936 /* If the first type-specifier could not be found, this is not a
11937 type-specifier-seq at all. */
11938 if (!seen_type_specifier)
11940 cp_parser_error (parser, "expected type-specifier");
11941 type_specifier_seq->type = error_mark_node;
11942 return;
11944 /* If subsequent type-specifiers could not be found, the
11945 type-specifier-seq is complete. */
11946 break;
11949 seen_type_specifier = true;
11950 /* The standard says that a condition can be:
11952 type-specifier-seq declarator = assignment-expression
11954 However, given:
11956 struct S {};
11957 if (int S = ...)
11959 we should treat the "S" as a declarator, not as a
11960 type-specifier. The standard doesn't say that explicitly for
11961 type-specifier-seq, but it does say that for
11962 decl-specifier-seq in an ordinary declaration. Perhaps it
11963 would be clearer just to allow a decl-specifier-seq here, and
11964 then add a semantic restriction that if any decl-specifiers
11965 that are not type-specifiers appear, the program is invalid. */
11966 if (is_condition && !is_cv_qualifier)
11967 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11971 /* Parse a parameter-declaration-clause.
11973 parameter-declaration-clause:
11974 parameter-declaration-list [opt] ... [opt]
11975 parameter-declaration-list , ...
11977 Returns a representation for the parameter declarations. A return
11978 value of NULL indicates a parameter-declaration-clause consisting
11979 only of an ellipsis. */
11981 static cp_parameter_declarator *
11982 cp_parser_parameter_declaration_clause (cp_parser* parser)
11984 cp_parameter_declarator *parameters;
11985 cp_token *token;
11986 bool ellipsis_p;
11987 bool is_error;
11989 /* Peek at the next token. */
11990 token = cp_lexer_peek_token (parser->lexer);
11991 /* Check for trivial parameter-declaration-clauses. */
11992 if (token->type == CPP_ELLIPSIS)
11994 /* Consume the `...' token. */
11995 cp_lexer_consume_token (parser->lexer);
11996 return NULL;
11998 else if (token->type == CPP_CLOSE_PAREN)
11999 /* There are no parameters. */
12001 #ifndef NO_IMPLICIT_EXTERN_C
12002 if (in_system_header && current_class_type == NULL
12003 && current_lang_name == lang_name_c)
12004 return NULL;
12005 else
12006 #endif
12007 return no_parameters;
12009 /* Check for `(void)', too, which is a special case. */
12010 else if (token->keyword == RID_VOID
12011 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12012 == CPP_CLOSE_PAREN))
12014 /* Consume the `void' token. */
12015 cp_lexer_consume_token (parser->lexer);
12016 /* There are no parameters. */
12017 return no_parameters;
12020 /* Parse the parameter-declaration-list. */
12021 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12022 /* If a parse error occurred while parsing the
12023 parameter-declaration-list, then the entire
12024 parameter-declaration-clause is erroneous. */
12025 if (is_error)
12026 return NULL;
12028 /* Peek at the next token. */
12029 token = cp_lexer_peek_token (parser->lexer);
12030 /* If it's a `,', the clause should terminate with an ellipsis. */
12031 if (token->type == CPP_COMMA)
12033 /* Consume the `,'. */
12034 cp_lexer_consume_token (parser->lexer);
12035 /* Expect an ellipsis. */
12036 ellipsis_p
12037 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12039 /* It might also be `...' if the optional trailing `,' was
12040 omitted. */
12041 else if (token->type == CPP_ELLIPSIS)
12043 /* Consume the `...' token. */
12044 cp_lexer_consume_token (parser->lexer);
12045 /* And remember that we saw it. */
12046 ellipsis_p = true;
12048 else
12049 ellipsis_p = false;
12051 /* Finish the parameter list. */
12052 if (parameters && ellipsis_p)
12053 parameters->ellipsis_p = true;
12055 return parameters;
12058 /* Parse a parameter-declaration-list.
12060 parameter-declaration-list:
12061 parameter-declaration
12062 parameter-declaration-list , parameter-declaration
12064 Returns a representation of the parameter-declaration-list, as for
12065 cp_parser_parameter_declaration_clause. However, the
12066 `void_list_node' is never appended to the list. Upon return,
12067 *IS_ERROR will be true iff an error occurred. */
12069 static cp_parameter_declarator *
12070 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12072 cp_parameter_declarator *parameters = NULL;
12073 cp_parameter_declarator **tail = &parameters;
12075 /* Assume all will go well. */
12076 *is_error = false;
12078 /* Look for more parameters. */
12079 while (true)
12081 cp_parameter_declarator *parameter;
12082 bool parenthesized_p;
12083 /* Parse the parameter. */
12084 parameter
12085 = cp_parser_parameter_declaration (parser,
12086 /*template_parm_p=*/false,
12087 &parenthesized_p);
12089 /* If a parse error occurred parsing the parameter declaration,
12090 then the entire parameter-declaration-list is erroneous. */
12091 if (!parameter)
12093 *is_error = true;
12094 parameters = NULL;
12095 break;
12097 /* Add the new parameter to the list. */
12098 *tail = parameter;
12099 tail = &parameter->next;
12101 /* Peek at the next token. */
12102 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12103 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12104 /* These are for Objective-C++ */
12105 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12106 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12107 /* The parameter-declaration-list is complete. */
12108 break;
12109 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12111 cp_token *token;
12113 /* Peek at the next token. */
12114 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12115 /* If it's an ellipsis, then the list is complete. */
12116 if (token->type == CPP_ELLIPSIS)
12117 break;
12118 /* Otherwise, there must be more parameters. Consume the
12119 `,'. */
12120 cp_lexer_consume_token (parser->lexer);
12121 /* When parsing something like:
12123 int i(float f, double d)
12125 we can tell after seeing the declaration for "f" that we
12126 are not looking at an initialization of a variable "i",
12127 but rather at the declaration of a function "i".
12129 Due to the fact that the parsing of template arguments
12130 (as specified to a template-id) requires backtracking we
12131 cannot use this technique when inside a template argument
12132 list. */
12133 if (!parser->in_template_argument_list_p
12134 && !parser->in_type_id_in_expr_p
12135 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12136 /* However, a parameter-declaration of the form
12137 "foat(f)" (which is a valid declaration of a
12138 parameter "f") can also be interpreted as an
12139 expression (the conversion of "f" to "float"). */
12140 && !parenthesized_p)
12141 cp_parser_commit_to_tentative_parse (parser);
12143 else
12145 cp_parser_error (parser, "expected %<,%> or %<...%>");
12146 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12147 cp_parser_skip_to_closing_parenthesis (parser,
12148 /*recovering=*/true,
12149 /*or_comma=*/false,
12150 /*consume_paren=*/false);
12151 break;
12155 return parameters;
12158 /* Parse a parameter declaration.
12160 parameter-declaration:
12161 decl-specifier-seq declarator
12162 decl-specifier-seq declarator = assignment-expression
12163 decl-specifier-seq abstract-declarator [opt]
12164 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12166 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12167 declares a template parameter. (In that case, a non-nested `>'
12168 token encountered during the parsing of the assignment-expression
12169 is not interpreted as a greater-than operator.)
12171 Returns a representation of the parameter, or NULL if an error
12172 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12173 true iff the declarator is of the form "(p)". */
12175 static cp_parameter_declarator *
12176 cp_parser_parameter_declaration (cp_parser *parser,
12177 bool template_parm_p,
12178 bool *parenthesized_p)
12180 int declares_class_or_enum;
12181 bool greater_than_is_operator_p;
12182 cp_decl_specifier_seq decl_specifiers;
12183 cp_declarator *declarator;
12184 tree default_argument;
12185 cp_token *token;
12186 const char *saved_message;
12188 /* In a template parameter, `>' is not an operator.
12190 [temp.param]
12192 When parsing a default template-argument for a non-type
12193 template-parameter, the first non-nested `>' is taken as the end
12194 of the template parameter-list rather than a greater-than
12195 operator. */
12196 greater_than_is_operator_p = !template_parm_p;
12198 /* Type definitions may not appear in parameter types. */
12199 saved_message = parser->type_definition_forbidden_message;
12200 parser->type_definition_forbidden_message
12201 = "types may not be defined in parameter types";
12203 /* Parse the declaration-specifiers. */
12204 cp_parser_decl_specifier_seq (parser,
12205 CP_PARSER_FLAGS_NONE,
12206 &decl_specifiers,
12207 &declares_class_or_enum);
12208 /* If an error occurred, there's no reason to attempt to parse the
12209 rest of the declaration. */
12210 if (cp_parser_error_occurred (parser))
12212 parser->type_definition_forbidden_message = saved_message;
12213 return NULL;
12216 /* Peek at the next token. */
12217 token = cp_lexer_peek_token (parser->lexer);
12218 /* If the next token is a `)', `,', `=', `>', or `...', then there
12219 is no declarator. */
12220 if (token->type == CPP_CLOSE_PAREN
12221 || token->type == CPP_COMMA
12222 || token->type == CPP_EQ
12223 || token->type == CPP_ELLIPSIS
12224 || token->type == CPP_GREATER)
12226 declarator = NULL;
12227 if (parenthesized_p)
12228 *parenthesized_p = false;
12230 /* Otherwise, there should be a declarator. */
12231 else
12233 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12234 parser->default_arg_ok_p = false;
12236 /* After seeing a decl-specifier-seq, if the next token is not a
12237 "(", there is no possibility that the code is a valid
12238 expression. Therefore, if parsing tentatively, we commit at
12239 this point. */
12240 if (!parser->in_template_argument_list_p
12241 /* In an expression context, having seen:
12243 (int((char ...
12245 we cannot be sure whether we are looking at a
12246 function-type (taking a "char" as a parameter) or a cast
12247 of some object of type "char" to "int". */
12248 && !parser->in_type_id_in_expr_p
12249 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12250 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12251 cp_parser_commit_to_tentative_parse (parser);
12252 /* Parse the declarator. */
12253 declarator = cp_parser_declarator (parser,
12254 CP_PARSER_DECLARATOR_EITHER,
12255 /*ctor_dtor_or_conv_p=*/NULL,
12256 parenthesized_p,
12257 /*member_p=*/false);
12258 parser->default_arg_ok_p = saved_default_arg_ok_p;
12259 /* After the declarator, allow more attributes. */
12260 decl_specifiers.attributes
12261 = chainon (decl_specifiers.attributes,
12262 cp_parser_attributes_opt (parser));
12265 /* The restriction on defining new types applies only to the type
12266 of the parameter, not to the default argument. */
12267 parser->type_definition_forbidden_message = saved_message;
12269 /* If the next token is `=', then process a default argument. */
12270 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12272 bool saved_greater_than_is_operator_p;
12273 /* Consume the `='. */
12274 cp_lexer_consume_token (parser->lexer);
12276 /* If we are defining a class, then the tokens that make up the
12277 default argument must be saved and processed later. */
12278 if (!template_parm_p && at_class_scope_p ()
12279 && TYPE_BEING_DEFINED (current_class_type))
12281 unsigned depth = 0;
12282 cp_token *first_token;
12283 cp_token *token;
12285 /* Add tokens until we have processed the entire default
12286 argument. We add the range [first_token, token). */
12287 first_token = cp_lexer_peek_token (parser->lexer);
12288 while (true)
12290 bool done = false;
12292 /* Peek at the next token. */
12293 token = cp_lexer_peek_token (parser->lexer);
12294 /* What we do depends on what token we have. */
12295 switch (token->type)
12297 /* In valid code, a default argument must be
12298 immediately followed by a `,' `)', or `...'. */
12299 case CPP_COMMA:
12300 case CPP_CLOSE_PAREN:
12301 case CPP_ELLIPSIS:
12302 /* If we run into a non-nested `;', `}', or `]',
12303 then the code is invalid -- but the default
12304 argument is certainly over. */
12305 case CPP_SEMICOLON:
12306 case CPP_CLOSE_BRACE:
12307 case CPP_CLOSE_SQUARE:
12308 if (depth == 0)
12309 done = true;
12310 /* Update DEPTH, if necessary. */
12311 else if (token->type == CPP_CLOSE_PAREN
12312 || token->type == CPP_CLOSE_BRACE
12313 || token->type == CPP_CLOSE_SQUARE)
12314 --depth;
12315 break;
12317 case CPP_OPEN_PAREN:
12318 case CPP_OPEN_SQUARE:
12319 case CPP_OPEN_BRACE:
12320 ++depth;
12321 break;
12323 case CPP_GREATER:
12324 /* If we see a non-nested `>', and `>' is not an
12325 operator, then it marks the end of the default
12326 argument. */
12327 if (!depth && !greater_than_is_operator_p)
12328 done = true;
12329 break;
12331 /* If we run out of tokens, issue an error message. */
12332 case CPP_EOF:
12333 case CPP_PRAGMA_EOL:
12334 error ("file ends in default argument");
12335 done = true;
12336 break;
12338 case CPP_NAME:
12339 case CPP_SCOPE:
12340 /* In these cases, we should look for template-ids.
12341 For example, if the default argument is
12342 `X<int, double>()', we need to do name lookup to
12343 figure out whether or not `X' is a template; if
12344 so, the `,' does not end the default argument.
12346 That is not yet done. */
12347 break;
12349 default:
12350 break;
12353 /* If we've reached the end, stop. */
12354 if (done)
12355 break;
12357 /* Add the token to the token block. */
12358 token = cp_lexer_consume_token (parser->lexer);
12361 /* Create a DEFAULT_ARG to represented the unparsed default
12362 argument. */
12363 default_argument = make_node (DEFAULT_ARG);
12364 DEFARG_TOKENS (default_argument)
12365 = cp_token_cache_new (first_token, token);
12366 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12368 /* Outside of a class definition, we can just parse the
12369 assignment-expression. */
12370 else
12372 bool saved_local_variables_forbidden_p;
12374 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12375 set correctly. */
12376 saved_greater_than_is_operator_p
12377 = parser->greater_than_is_operator_p;
12378 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12379 /* Local variable names (and the `this' keyword) may not
12380 appear in a default argument. */
12381 saved_local_variables_forbidden_p
12382 = parser->local_variables_forbidden_p;
12383 parser->local_variables_forbidden_p = true;
12384 /* Parse the assignment-expression. */
12385 default_argument
12386 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12387 /* Restore saved state. */
12388 parser->greater_than_is_operator_p
12389 = saved_greater_than_is_operator_p;
12390 parser->local_variables_forbidden_p
12391 = saved_local_variables_forbidden_p;
12393 if (!parser->default_arg_ok_p)
12395 if (!flag_pedantic_errors)
12396 warning (0, "deprecated use of default argument for parameter of non-function");
12397 else
12399 error ("default arguments are only permitted for function parameters");
12400 default_argument = NULL_TREE;
12404 else
12405 default_argument = NULL_TREE;
12407 return make_parameter_declarator (&decl_specifiers,
12408 declarator,
12409 default_argument);
12412 /* Parse a function-body.
12414 function-body:
12415 compound_statement */
12417 static void
12418 cp_parser_function_body (cp_parser *parser)
12420 cp_parser_compound_statement (parser, NULL, false);
12423 /* Parse a ctor-initializer-opt followed by a function-body. Return
12424 true if a ctor-initializer was present. */
12426 static bool
12427 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12429 tree body;
12430 bool ctor_initializer_p;
12432 /* Begin the function body. */
12433 body = begin_function_body ();
12434 /* Parse the optional ctor-initializer. */
12435 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12436 /* Parse the function-body. */
12437 cp_parser_function_body (parser);
12438 /* Finish the function body. */
12439 finish_function_body (body);
12441 return ctor_initializer_p;
12444 /* Parse an initializer.
12446 initializer:
12447 = initializer-clause
12448 ( expression-list )
12450 Returns an expression representing the initializer. If no
12451 initializer is present, NULL_TREE is returned.
12453 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12454 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12455 set to FALSE if there is no initializer present. If there is an
12456 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12457 is set to true; otherwise it is set to false. */
12459 static tree
12460 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12461 bool* non_constant_p)
12463 cp_token *token;
12464 tree init;
12466 /* Peek at the next token. */
12467 token = cp_lexer_peek_token (parser->lexer);
12469 /* Let our caller know whether or not this initializer was
12470 parenthesized. */
12471 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12472 /* Assume that the initializer is constant. */
12473 *non_constant_p = false;
12475 if (token->type == CPP_EQ)
12477 /* Consume the `='. */
12478 cp_lexer_consume_token (parser->lexer);
12479 /* Parse the initializer-clause. */
12480 init = cp_parser_initializer_clause (parser, non_constant_p);
12482 else if (token->type == CPP_OPEN_PAREN)
12483 init = cp_parser_parenthesized_expression_list (parser, false,
12484 /*cast_p=*/false,
12485 non_constant_p);
12486 else
12488 /* Anything else is an error. */
12489 cp_parser_error (parser, "expected initializer");
12490 init = error_mark_node;
12493 return init;
12496 /* Parse an initializer-clause.
12498 initializer-clause:
12499 assignment-expression
12500 { initializer-list , [opt] }
12503 Returns an expression representing the initializer.
12505 If the `assignment-expression' production is used the value
12506 returned is simply a representation for the expression.
12508 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12509 the elements of the initializer-list (or NULL, if the last
12510 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12511 NULL_TREE. There is no way to detect whether or not the optional
12512 trailing `,' was provided. NON_CONSTANT_P is as for
12513 cp_parser_initializer. */
12515 static tree
12516 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12518 tree initializer;
12520 /* Assume the expression is constant. */
12521 *non_constant_p = false;
12523 /* If it is not a `{', then we are looking at an
12524 assignment-expression. */
12525 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12527 initializer
12528 = cp_parser_constant_expression (parser,
12529 /*allow_non_constant_p=*/true,
12530 non_constant_p);
12531 if (!*non_constant_p)
12532 initializer = fold_non_dependent_expr (initializer);
12534 else
12536 /* Consume the `{' token. */
12537 cp_lexer_consume_token (parser->lexer);
12538 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12539 initializer = make_node (CONSTRUCTOR);
12540 /* If it's not a `}', then there is a non-trivial initializer. */
12541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12543 /* Parse the initializer list. */
12544 CONSTRUCTOR_ELTS (initializer)
12545 = cp_parser_initializer_list (parser, non_constant_p);
12546 /* A trailing `,' token is allowed. */
12547 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12548 cp_lexer_consume_token (parser->lexer);
12550 /* Now, there should be a trailing `}'. */
12551 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12554 return initializer;
12557 /* Parse an initializer-list.
12559 initializer-list:
12560 initializer-clause
12561 initializer-list , initializer-clause
12563 GNU Extension:
12565 initializer-list:
12566 identifier : initializer-clause
12567 initializer-list, identifier : initializer-clause
12569 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12570 for the initializer. If the INDEX of the elt is non-NULL, it is the
12571 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12572 as for cp_parser_initializer. */
12574 static VEC(constructor_elt,gc) *
12575 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12577 VEC(constructor_elt,gc) *v = NULL;
12579 /* Assume all of the expressions are constant. */
12580 *non_constant_p = false;
12582 /* Parse the rest of the list. */
12583 while (true)
12585 cp_token *token;
12586 tree identifier;
12587 tree initializer;
12588 bool clause_non_constant_p;
12590 /* If the next token is an identifier and the following one is a
12591 colon, we are looking at the GNU designated-initializer
12592 syntax. */
12593 if (cp_parser_allow_gnu_extensions_p (parser)
12594 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12595 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12597 /* Consume the identifier. */
12598 identifier = cp_lexer_consume_token (parser->lexer)->value;
12599 /* Consume the `:'. */
12600 cp_lexer_consume_token (parser->lexer);
12602 else
12603 identifier = NULL_TREE;
12605 /* Parse the initializer. */
12606 initializer = cp_parser_initializer_clause (parser,
12607 &clause_non_constant_p);
12608 /* If any clause is non-constant, so is the entire initializer. */
12609 if (clause_non_constant_p)
12610 *non_constant_p = true;
12612 /* Add it to the vector. */
12613 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12615 /* If the next token is not a comma, we have reached the end of
12616 the list. */
12617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12618 break;
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12622 /* If the next token is a `}', then we're still done. An
12623 initializer-clause can have a trailing `,' after the
12624 initializer-list and before the closing `}'. */
12625 if (token->type == CPP_CLOSE_BRACE)
12626 break;
12628 /* Consume the `,' token. */
12629 cp_lexer_consume_token (parser->lexer);
12632 return v;
12635 /* Classes [gram.class] */
12637 /* Parse a class-name.
12639 class-name:
12640 identifier
12641 template-id
12643 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12644 to indicate that names looked up in dependent types should be
12645 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12646 keyword has been used to indicate that the name that appears next
12647 is a template. TAG_TYPE indicates the explicit tag given before
12648 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12649 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12650 is the class being defined in a class-head.
12652 Returns the TYPE_DECL representing the class. */
12654 static tree
12655 cp_parser_class_name (cp_parser *parser,
12656 bool typename_keyword_p,
12657 bool template_keyword_p,
12658 enum tag_types tag_type,
12659 bool check_dependency_p,
12660 bool class_head_p,
12661 bool is_declaration)
12663 tree decl;
12664 tree scope;
12665 bool typename_p;
12666 cp_token *token;
12668 /* All class-names start with an identifier. */
12669 token = cp_lexer_peek_token (parser->lexer);
12670 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12672 cp_parser_error (parser, "expected class-name");
12673 return error_mark_node;
12676 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12677 to a template-id, so we save it here. */
12678 scope = parser->scope;
12679 if (scope == error_mark_node)
12680 return error_mark_node;
12682 /* Any name names a type if we're following the `typename' keyword
12683 in a qualified name where the enclosing scope is type-dependent. */
12684 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12685 && dependent_type_p (scope));
12686 /* Handle the common case (an identifier, but not a template-id)
12687 efficiently. */
12688 if (token->type == CPP_NAME
12689 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12691 cp_token *identifier_token;
12692 tree identifier;
12693 bool ambiguous_p;
12695 /* Look for the identifier. */
12696 identifier_token = cp_lexer_peek_token (parser->lexer);
12697 ambiguous_p = identifier_token->ambiguous_p;
12698 identifier = cp_parser_identifier (parser);
12699 /* If the next token isn't an identifier, we are certainly not
12700 looking at a class-name. */
12701 if (identifier == error_mark_node)
12702 decl = error_mark_node;
12703 /* If we know this is a type-name, there's no need to look it
12704 up. */
12705 else if (typename_p)
12706 decl = identifier;
12707 else
12709 tree ambiguous_decls;
12710 /* If we already know that this lookup is ambiguous, then
12711 we've already issued an error message; there's no reason
12712 to check again. */
12713 if (ambiguous_p)
12715 cp_parser_simulate_error (parser);
12716 return error_mark_node;
12718 /* If the next token is a `::', then the name must be a type
12719 name.
12721 [basic.lookup.qual]
12723 During the lookup for a name preceding the :: scope
12724 resolution operator, object, function, and enumerator
12725 names are ignored. */
12726 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12727 tag_type = typename_type;
12728 /* Look up the name. */
12729 decl = cp_parser_lookup_name (parser, identifier,
12730 tag_type,
12731 /*is_template=*/false,
12732 /*is_namespace=*/false,
12733 check_dependency_p,
12734 &ambiguous_decls);
12735 if (ambiguous_decls)
12737 error ("reference to %qD is ambiguous", identifier);
12738 print_candidates (ambiguous_decls);
12739 if (cp_parser_parsing_tentatively (parser))
12741 identifier_token->ambiguous_p = true;
12742 cp_parser_simulate_error (parser);
12744 return error_mark_node;
12748 else
12750 /* Try a template-id. */
12751 decl = cp_parser_template_id (parser, template_keyword_p,
12752 check_dependency_p,
12753 is_declaration);
12754 if (decl == error_mark_node)
12755 return error_mark_node;
12758 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12760 /* If this is a typename, create a TYPENAME_TYPE. */
12761 if (typename_p && decl != error_mark_node)
12763 decl = make_typename_type (scope, decl, typename_type,
12764 /*complain=*/tf_error);
12765 if (decl != error_mark_node)
12766 decl = TYPE_NAME (decl);
12769 /* Check to see that it is really the name of a class. */
12770 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12771 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12772 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12773 /* Situations like this:
12775 template <typename T> struct A {
12776 typename T::template X<int>::I i;
12779 are problematic. Is `T::template X<int>' a class-name? The
12780 standard does not seem to be definitive, but there is no other
12781 valid interpretation of the following `::'. Therefore, those
12782 names are considered class-names. */
12783 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12784 else if (decl == error_mark_node
12785 || TREE_CODE (decl) != TYPE_DECL
12786 || TREE_TYPE (decl) == error_mark_node
12787 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12789 cp_parser_error (parser, "expected class-name");
12790 return error_mark_node;
12793 return decl;
12796 /* Parse a class-specifier.
12798 class-specifier:
12799 class-head { member-specification [opt] }
12801 Returns the TREE_TYPE representing the class. */
12803 static tree
12804 cp_parser_class_specifier (cp_parser* parser)
12806 cp_token *token;
12807 tree type;
12808 tree attributes = NULL_TREE;
12809 int has_trailing_semicolon;
12810 bool nested_name_specifier_p;
12811 unsigned saved_num_template_parameter_lists;
12812 tree old_scope = NULL_TREE;
12813 tree scope = NULL_TREE;
12815 push_deferring_access_checks (dk_no_deferred);
12817 /* Parse the class-head. */
12818 type = cp_parser_class_head (parser,
12819 &nested_name_specifier_p,
12820 &attributes);
12821 /* If the class-head was a semantic disaster, skip the entire body
12822 of the class. */
12823 if (!type)
12825 cp_parser_skip_to_end_of_block_or_statement (parser);
12826 pop_deferring_access_checks ();
12827 return error_mark_node;
12830 /* Look for the `{'. */
12831 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12833 pop_deferring_access_checks ();
12834 return error_mark_node;
12837 /* Issue an error message if type-definitions are forbidden here. */
12838 cp_parser_check_type_definition (parser);
12839 /* Remember that we are defining one more class. */
12840 ++parser->num_classes_being_defined;
12841 /* Inside the class, surrounding template-parameter-lists do not
12842 apply. */
12843 saved_num_template_parameter_lists
12844 = parser->num_template_parameter_lists;
12845 parser->num_template_parameter_lists = 0;
12847 /* Start the class. */
12848 if (nested_name_specifier_p)
12850 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12851 old_scope = push_inner_scope (scope);
12853 type = begin_class_definition (type);
12855 if (type == error_mark_node)
12856 /* If the type is erroneous, skip the entire body of the class. */
12857 cp_parser_skip_to_closing_brace (parser);
12858 else
12859 /* Parse the member-specification. */
12860 cp_parser_member_specification_opt (parser);
12862 /* Look for the trailing `}'. */
12863 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12864 /* We get better error messages by noticing a common problem: a
12865 missing trailing `;'. */
12866 token = cp_lexer_peek_token (parser->lexer);
12867 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12868 /* Look for trailing attributes to apply to this class. */
12869 if (cp_parser_allow_gnu_extensions_p (parser))
12871 tree sub_attr = cp_parser_attributes_opt (parser);
12872 attributes = chainon (attributes, sub_attr);
12874 if (type != error_mark_node)
12875 type = finish_struct (type, attributes);
12876 if (nested_name_specifier_p)
12877 pop_inner_scope (old_scope, scope);
12878 /* If this class is not itself within the scope of another class,
12879 then we need to parse the bodies of all of the queued function
12880 definitions. Note that the queued functions defined in a class
12881 are not always processed immediately following the
12882 class-specifier for that class. Consider:
12884 struct A {
12885 struct B { void f() { sizeof (A); } };
12888 If `f' were processed before the processing of `A' were
12889 completed, there would be no way to compute the size of `A'.
12890 Note that the nesting we are interested in here is lexical --
12891 not the semantic nesting given by TYPE_CONTEXT. In particular,
12892 for:
12894 struct A { struct B; };
12895 struct A::B { void f() { } };
12897 there is no need to delay the parsing of `A::B::f'. */
12898 if (--parser->num_classes_being_defined == 0)
12900 tree queue_entry;
12901 tree fn;
12902 tree class_type = NULL_TREE;
12903 tree pushed_scope = NULL_TREE;
12905 /* In a first pass, parse default arguments to the functions.
12906 Then, in a second pass, parse the bodies of the functions.
12907 This two-phased approach handles cases like:
12909 struct S {
12910 void f() { g(); }
12911 void g(int i = 3);
12915 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12916 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12917 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12918 TREE_PURPOSE (parser->unparsed_functions_queues)
12919 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12921 fn = TREE_VALUE (queue_entry);
12922 /* If there are default arguments that have not yet been processed,
12923 take care of them now. */
12924 if (class_type != TREE_PURPOSE (queue_entry))
12926 if (pushed_scope)
12927 pop_scope (pushed_scope);
12928 class_type = TREE_PURPOSE (queue_entry);
12929 pushed_scope = push_scope (class_type);
12931 /* Make sure that any template parameters are in scope. */
12932 maybe_begin_member_template_processing (fn);
12933 /* Parse the default argument expressions. */
12934 cp_parser_late_parsing_default_args (parser, fn);
12935 /* Remove any template parameters from the symbol table. */
12936 maybe_end_member_template_processing ();
12938 if (pushed_scope)
12939 pop_scope (pushed_scope);
12940 /* Now parse the body of the functions. */
12941 for (TREE_VALUE (parser->unparsed_functions_queues)
12942 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12943 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12944 TREE_VALUE (parser->unparsed_functions_queues)
12945 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12947 /* Figure out which function we need to process. */
12948 fn = TREE_VALUE (queue_entry);
12949 /* Parse the function. */
12950 cp_parser_late_parsing_for_member (parser, fn);
12954 /* Put back any saved access checks. */
12955 pop_deferring_access_checks ();
12957 /* Restore the count of active template-parameter-lists. */
12958 parser->num_template_parameter_lists
12959 = saved_num_template_parameter_lists;
12961 return type;
12964 /* Parse a class-head.
12966 class-head:
12967 class-key identifier [opt] base-clause [opt]
12968 class-key nested-name-specifier identifier base-clause [opt]
12969 class-key nested-name-specifier [opt] template-id
12970 base-clause [opt]
12972 GNU Extensions:
12973 class-key attributes identifier [opt] base-clause [opt]
12974 class-key attributes nested-name-specifier identifier base-clause [opt]
12975 class-key attributes nested-name-specifier [opt] template-id
12976 base-clause [opt]
12978 Returns the TYPE of the indicated class. Sets
12979 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12980 involving a nested-name-specifier was used, and FALSE otherwise.
12982 Returns error_mark_node if this is not a class-head.
12984 Returns NULL_TREE if the class-head is syntactically valid, but
12985 semantically invalid in a way that means we should skip the entire
12986 body of the class. */
12988 static tree
12989 cp_parser_class_head (cp_parser* parser,
12990 bool* nested_name_specifier_p,
12991 tree *attributes_p)
12993 tree nested_name_specifier;
12994 enum tag_types class_key;
12995 tree id = NULL_TREE;
12996 tree type = NULL_TREE;
12997 tree attributes;
12998 bool template_id_p = false;
12999 bool qualified_p = false;
13000 bool invalid_nested_name_p = false;
13001 bool invalid_explicit_specialization_p = false;
13002 tree pushed_scope = NULL_TREE;
13003 unsigned num_templates;
13004 tree bases;
13006 /* Assume no nested-name-specifier will be present. */
13007 *nested_name_specifier_p = false;
13008 /* Assume no template parameter lists will be used in defining the
13009 type. */
13010 num_templates = 0;
13012 /* Look for the class-key. */
13013 class_key = cp_parser_class_key (parser);
13014 if (class_key == none_type)
13015 return error_mark_node;
13017 /* Parse the attributes. */
13018 attributes = cp_parser_attributes_opt (parser);
13020 /* If the next token is `::', that is invalid -- but sometimes
13021 people do try to write:
13023 struct ::S {};
13025 Handle this gracefully by accepting the extra qualifier, and then
13026 issuing an error about it later if this really is a
13027 class-head. If it turns out just to be an elaborated type
13028 specifier, remain silent. */
13029 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13030 qualified_p = true;
13032 push_deferring_access_checks (dk_no_check);
13034 /* Determine the name of the class. Begin by looking for an
13035 optional nested-name-specifier. */
13036 nested_name_specifier
13037 = cp_parser_nested_name_specifier_opt (parser,
13038 /*typename_keyword_p=*/false,
13039 /*check_dependency_p=*/false,
13040 /*type_p=*/false,
13041 /*is_declaration=*/false);
13042 /* If there was a nested-name-specifier, then there *must* be an
13043 identifier. */
13044 if (nested_name_specifier)
13046 /* Although the grammar says `identifier', it really means
13047 `class-name' or `template-name'. You are only allowed to
13048 define a class that has already been declared with this
13049 syntax.
13051 The proposed resolution for Core Issue 180 says that whever
13052 you see `class T::X' you should treat `X' as a type-name.
13054 It is OK to define an inaccessible class; for example:
13056 class A { class B; };
13057 class A::B {};
13059 We do not know if we will see a class-name, or a
13060 template-name. We look for a class-name first, in case the
13061 class-name is a template-id; if we looked for the
13062 template-name first we would stop after the template-name. */
13063 cp_parser_parse_tentatively (parser);
13064 type = cp_parser_class_name (parser,
13065 /*typename_keyword_p=*/false,
13066 /*template_keyword_p=*/false,
13067 class_type,
13068 /*check_dependency_p=*/false,
13069 /*class_head_p=*/true,
13070 /*is_declaration=*/false);
13071 /* If that didn't work, ignore the nested-name-specifier. */
13072 if (!cp_parser_parse_definitely (parser))
13074 invalid_nested_name_p = true;
13075 id = cp_parser_identifier (parser);
13076 if (id == error_mark_node)
13077 id = NULL_TREE;
13079 /* If we could not find a corresponding TYPE, treat this
13080 declaration like an unqualified declaration. */
13081 if (type == error_mark_node)
13082 nested_name_specifier = NULL_TREE;
13083 /* Otherwise, count the number of templates used in TYPE and its
13084 containing scopes. */
13085 else
13087 tree scope;
13089 for (scope = TREE_TYPE (type);
13090 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13091 scope = (TYPE_P (scope)
13092 ? TYPE_CONTEXT (scope)
13093 : DECL_CONTEXT (scope)))
13094 if (TYPE_P (scope)
13095 && CLASS_TYPE_P (scope)
13096 && CLASSTYPE_TEMPLATE_INFO (scope)
13097 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13098 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13099 ++num_templates;
13102 /* Otherwise, the identifier is optional. */
13103 else
13105 /* We don't know whether what comes next is a template-id,
13106 an identifier, or nothing at all. */
13107 cp_parser_parse_tentatively (parser);
13108 /* Check for a template-id. */
13109 id = cp_parser_template_id (parser,
13110 /*template_keyword_p=*/false,
13111 /*check_dependency_p=*/true,
13112 /*is_declaration=*/true);
13113 /* If that didn't work, it could still be an identifier. */
13114 if (!cp_parser_parse_definitely (parser))
13116 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13117 id = cp_parser_identifier (parser);
13118 else
13119 id = NULL_TREE;
13121 else
13123 template_id_p = true;
13124 ++num_templates;
13128 pop_deferring_access_checks ();
13130 if (id)
13131 cp_parser_check_for_invalid_template_id (parser, id);
13133 /* If it's not a `:' or a `{' then we can't really be looking at a
13134 class-head, since a class-head only appears as part of a
13135 class-specifier. We have to detect this situation before calling
13136 xref_tag, since that has irreversible side-effects. */
13137 if (!cp_parser_next_token_starts_class_definition_p (parser))
13139 cp_parser_error (parser, "expected %<{%> or %<:%>");
13140 return error_mark_node;
13143 /* At this point, we're going ahead with the class-specifier, even
13144 if some other problem occurs. */
13145 cp_parser_commit_to_tentative_parse (parser);
13146 /* Issue the error about the overly-qualified name now. */
13147 if (qualified_p)
13148 cp_parser_error (parser,
13149 "global qualification of class name is invalid");
13150 else if (invalid_nested_name_p)
13151 cp_parser_error (parser,
13152 "qualified name does not name a class");
13153 else if (nested_name_specifier)
13155 tree scope;
13157 /* Reject typedef-names in class heads. */
13158 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13160 error ("invalid class name in declaration of %qD", type);
13161 type = NULL_TREE;
13162 goto done;
13165 /* Figure out in what scope the declaration is being placed. */
13166 scope = current_scope ();
13167 /* If that scope does not contain the scope in which the
13168 class was originally declared, the program is invalid. */
13169 if (scope && !is_ancestor (scope, nested_name_specifier))
13171 error ("declaration of %qD in %qD which does not enclose %qD",
13172 type, scope, nested_name_specifier);
13173 type = NULL_TREE;
13174 goto done;
13176 /* [dcl.meaning]
13178 A declarator-id shall not be qualified exception of the
13179 definition of a ... nested class outside of its class
13180 ... [or] a the definition or explicit instantiation of a
13181 class member of a namespace outside of its namespace. */
13182 if (scope == nested_name_specifier)
13184 pedwarn ("extra qualification ignored");
13185 nested_name_specifier = NULL_TREE;
13186 num_templates = 0;
13189 /* An explicit-specialization must be preceded by "template <>". If
13190 it is not, try to recover gracefully. */
13191 if (at_namespace_scope_p ()
13192 && parser->num_template_parameter_lists == 0
13193 && template_id_p)
13195 error ("an explicit specialization must be preceded by %<template <>%>");
13196 invalid_explicit_specialization_p = true;
13197 /* Take the same action that would have been taken by
13198 cp_parser_explicit_specialization. */
13199 ++parser->num_template_parameter_lists;
13200 begin_specialization ();
13202 /* There must be no "return" statements between this point and the
13203 end of this function; set "type "to the correct return value and
13204 use "goto done;" to return. */
13205 /* Make sure that the right number of template parameters were
13206 present. */
13207 if (!cp_parser_check_template_parameters (parser, num_templates))
13209 /* If something went wrong, there is no point in even trying to
13210 process the class-definition. */
13211 type = NULL_TREE;
13212 goto done;
13215 /* Look up the type. */
13216 if (template_id_p)
13218 type = TREE_TYPE (id);
13219 maybe_process_partial_specialization (type);
13220 if (nested_name_specifier)
13221 pushed_scope = push_scope (nested_name_specifier);
13223 else if (nested_name_specifier)
13225 tree class_type;
13227 /* Given:
13229 template <typename T> struct S { struct T };
13230 template <typename T> struct S<T>::T { };
13232 we will get a TYPENAME_TYPE when processing the definition of
13233 `S::T'. We need to resolve it to the actual type before we
13234 try to define it. */
13235 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13237 class_type = resolve_typename_type (TREE_TYPE (type),
13238 /*only_current_p=*/false);
13239 if (class_type != error_mark_node)
13240 type = TYPE_NAME (class_type);
13241 else
13243 cp_parser_error (parser, "could not resolve typename type");
13244 type = error_mark_node;
13248 maybe_process_partial_specialization (TREE_TYPE (type));
13249 class_type = current_class_type;
13250 /* Enter the scope indicated by the nested-name-specifier. */
13251 pushed_scope = push_scope (nested_name_specifier);
13252 /* Get the canonical version of this type. */
13253 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13254 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13255 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13257 type = push_template_decl (type);
13258 if (type == error_mark_node)
13260 type = NULL_TREE;
13261 goto done;
13265 type = TREE_TYPE (type);
13266 *nested_name_specifier_p = true;
13268 else /* The name is not a nested name. */
13270 /* If the class was unnamed, create a dummy name. */
13271 if (!id)
13272 id = make_anon_name ();
13273 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13274 parser->num_template_parameter_lists);
13277 /* Indicate whether this class was declared as a `class' or as a
13278 `struct'. */
13279 if (TREE_CODE (type) == RECORD_TYPE)
13280 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13281 cp_parser_check_class_key (class_key, type);
13283 /* If this type was already complete, and we see another definition,
13284 that's an error. */
13285 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13287 error ("redefinition of %q#T", type);
13288 error ("previous definition of %q+#T", type);
13289 type = NULL_TREE;
13290 goto done;
13293 /* We will have entered the scope containing the class; the names of
13294 base classes should be looked up in that context. For example:
13296 struct A { struct B {}; struct C; };
13297 struct A::C : B {};
13299 is valid. */
13300 bases = NULL_TREE;
13302 /* Get the list of base-classes, if there is one. */
13303 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13304 bases = cp_parser_base_clause (parser);
13306 /* Process the base classes. */
13307 xref_basetypes (type, bases);
13309 done:
13310 /* Leave the scope given by the nested-name-specifier. We will
13311 enter the class scope itself while processing the members. */
13312 if (pushed_scope)
13313 pop_scope (pushed_scope);
13315 if (invalid_explicit_specialization_p)
13317 end_specialization ();
13318 --parser->num_template_parameter_lists;
13320 *attributes_p = attributes;
13321 return type;
13324 /* Parse a class-key.
13326 class-key:
13327 class
13328 struct
13329 union
13331 Returns the kind of class-key specified, or none_type to indicate
13332 error. */
13334 static enum tag_types
13335 cp_parser_class_key (cp_parser* parser)
13337 cp_token *token;
13338 enum tag_types tag_type;
13340 /* Look for the class-key. */
13341 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13342 if (!token)
13343 return none_type;
13345 /* Check to see if the TOKEN is a class-key. */
13346 tag_type = cp_parser_token_is_class_key (token);
13347 if (!tag_type)
13348 cp_parser_error (parser, "expected class-key");
13349 return tag_type;
13352 /* Parse an (optional) member-specification.
13354 member-specification:
13355 member-declaration member-specification [opt]
13356 access-specifier : member-specification [opt] */
13358 static void
13359 cp_parser_member_specification_opt (cp_parser* parser)
13361 while (true)
13363 cp_token *token;
13364 enum rid keyword;
13366 /* Peek at the next token. */
13367 token = cp_lexer_peek_token (parser->lexer);
13368 /* If it's a `}', or EOF then we've seen all the members. */
13369 if (token->type == CPP_CLOSE_BRACE
13370 || token->type == CPP_EOF
13371 || token->type == CPP_PRAGMA_EOL)
13372 break;
13374 /* See if this token is a keyword. */
13375 keyword = token->keyword;
13376 switch (keyword)
13378 case RID_PUBLIC:
13379 case RID_PROTECTED:
13380 case RID_PRIVATE:
13381 /* Consume the access-specifier. */
13382 cp_lexer_consume_token (parser->lexer);
13383 /* Remember which access-specifier is active. */
13384 current_access_specifier = token->value;
13385 /* Look for the `:'. */
13386 cp_parser_require (parser, CPP_COLON, "`:'");
13387 break;
13389 default:
13390 /* Accept #pragmas at class scope. */
13391 if (token->type == CPP_PRAGMA)
13393 cp_parser_pragma (parser, pragma_external);
13394 break;
13397 /* Otherwise, the next construction must be a
13398 member-declaration. */
13399 cp_parser_member_declaration (parser);
13404 /* Parse a member-declaration.
13406 member-declaration:
13407 decl-specifier-seq [opt] member-declarator-list [opt] ;
13408 function-definition ; [opt]
13409 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13410 using-declaration
13411 template-declaration
13413 member-declarator-list:
13414 member-declarator
13415 member-declarator-list , member-declarator
13417 member-declarator:
13418 declarator pure-specifier [opt]
13419 declarator constant-initializer [opt]
13420 identifier [opt] : constant-expression
13422 GNU Extensions:
13424 member-declaration:
13425 __extension__ member-declaration
13427 member-declarator:
13428 declarator attributes [opt] pure-specifier [opt]
13429 declarator attributes [opt] constant-initializer [opt]
13430 identifier [opt] attributes [opt] : constant-expression */
13432 static void
13433 cp_parser_member_declaration (cp_parser* parser)
13435 cp_decl_specifier_seq decl_specifiers;
13436 tree prefix_attributes;
13437 tree decl;
13438 int declares_class_or_enum;
13439 bool friend_p;
13440 cp_token *token;
13441 int saved_pedantic;
13443 /* Check for the `__extension__' keyword. */
13444 if (cp_parser_extension_opt (parser, &saved_pedantic))
13446 /* Recurse. */
13447 cp_parser_member_declaration (parser);
13448 /* Restore the old value of the PEDANTIC flag. */
13449 pedantic = saved_pedantic;
13451 return;
13454 /* Check for a template-declaration. */
13455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13457 /* An explicit specialization here is an error condition, and we
13458 expect the specialization handler to detect and report this. */
13459 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13460 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13461 cp_parser_explicit_specialization (parser);
13462 else
13463 cp_parser_template_declaration (parser, /*member_p=*/true);
13465 return;
13468 /* Check for a using-declaration. */
13469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13471 /* Parse the using-declaration. */
13472 cp_parser_using_declaration (parser);
13474 return;
13477 /* Check for @defs. */
13478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13480 tree ivar, member;
13481 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13482 ivar = ivar_chains;
13483 while (ivar)
13485 member = ivar;
13486 ivar = TREE_CHAIN (member);
13487 TREE_CHAIN (member) = NULL_TREE;
13488 finish_member_declaration (member);
13490 return;
13493 /* Parse the decl-specifier-seq. */
13494 cp_parser_decl_specifier_seq (parser,
13495 CP_PARSER_FLAGS_OPTIONAL,
13496 &decl_specifiers,
13497 &declares_class_or_enum);
13498 prefix_attributes = decl_specifiers.attributes;
13499 decl_specifiers.attributes = NULL_TREE;
13500 /* Check for an invalid type-name. */
13501 if (!decl_specifiers.type
13502 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13503 return;
13504 /* If there is no declarator, then the decl-specifier-seq should
13505 specify a type. */
13506 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13508 /* If there was no decl-specifier-seq, and the next token is a
13509 `;', then we have something like:
13511 struct S { ; };
13513 [class.mem]
13515 Each member-declaration shall declare at least one member
13516 name of the class. */
13517 if (!decl_specifiers.any_specifiers_p)
13519 cp_token *token = cp_lexer_peek_token (parser->lexer);
13520 if (pedantic && !token->in_system_header)
13521 pedwarn ("%Hextra %<;%>", &token->location);
13523 else
13525 tree type;
13527 /* See if this declaration is a friend. */
13528 friend_p = cp_parser_friend_p (&decl_specifiers);
13529 /* If there were decl-specifiers, check to see if there was
13530 a class-declaration. */
13531 type = check_tag_decl (&decl_specifiers);
13532 /* Nested classes have already been added to the class, but
13533 a `friend' needs to be explicitly registered. */
13534 if (friend_p)
13536 /* If the `friend' keyword was present, the friend must
13537 be introduced with a class-key. */
13538 if (!declares_class_or_enum)
13539 error ("a class-key must be used when declaring a friend");
13540 /* In this case:
13542 template <typename T> struct A {
13543 friend struct A<T>::B;
13546 A<T>::B will be represented by a TYPENAME_TYPE, and
13547 therefore not recognized by check_tag_decl. */
13548 if (!type
13549 && decl_specifiers.type
13550 && TYPE_P (decl_specifiers.type))
13551 type = decl_specifiers.type;
13552 if (!type || !TYPE_P (type))
13553 error ("friend declaration does not name a class or "
13554 "function");
13555 else
13556 make_friend_class (current_class_type, type,
13557 /*complain=*/true);
13559 /* If there is no TYPE, an error message will already have
13560 been issued. */
13561 else if (!type || type == error_mark_node)
13563 /* An anonymous aggregate has to be handled specially; such
13564 a declaration really declares a data member (with a
13565 particular type), as opposed to a nested class. */
13566 else if (ANON_AGGR_TYPE_P (type))
13568 /* Remove constructors and such from TYPE, now that we
13569 know it is an anonymous aggregate. */
13570 fixup_anonymous_aggr (type);
13571 /* And make the corresponding data member. */
13572 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13573 /* Add it to the class. */
13574 finish_member_declaration (decl);
13576 else
13577 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13580 else
13582 /* See if these declarations will be friends. */
13583 friend_p = cp_parser_friend_p (&decl_specifiers);
13585 /* Keep going until we hit the `;' at the end of the
13586 declaration. */
13587 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13589 tree attributes = NULL_TREE;
13590 tree first_attribute;
13592 /* Peek at the next token. */
13593 token = cp_lexer_peek_token (parser->lexer);
13595 /* Check for a bitfield declaration. */
13596 if (token->type == CPP_COLON
13597 || (token->type == CPP_NAME
13598 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13599 == CPP_COLON))
13601 tree identifier;
13602 tree width;
13604 /* Get the name of the bitfield. Note that we cannot just
13605 check TOKEN here because it may have been invalidated by
13606 the call to cp_lexer_peek_nth_token above. */
13607 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13608 identifier = cp_parser_identifier (parser);
13609 else
13610 identifier = NULL_TREE;
13612 /* Consume the `:' token. */
13613 cp_lexer_consume_token (parser->lexer);
13614 /* Get the width of the bitfield. */
13615 width
13616 = cp_parser_constant_expression (parser,
13617 /*allow_non_constant=*/false,
13618 NULL);
13620 /* Look for attributes that apply to the bitfield. */
13621 attributes = cp_parser_attributes_opt (parser);
13622 /* Remember which attributes are prefix attributes and
13623 which are not. */
13624 first_attribute = attributes;
13625 /* Combine the attributes. */
13626 attributes = chainon (prefix_attributes, attributes);
13628 /* Create the bitfield declaration. */
13629 decl = grokbitfield (identifier
13630 ? make_id_declarator (NULL_TREE,
13631 identifier,
13632 sfk_none)
13633 : NULL,
13634 &decl_specifiers,
13635 width);
13636 /* Apply the attributes. */
13637 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13639 else
13641 cp_declarator *declarator;
13642 tree initializer;
13643 tree asm_specification;
13644 int ctor_dtor_or_conv_p;
13646 /* Parse the declarator. */
13647 declarator
13648 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13649 &ctor_dtor_or_conv_p,
13650 /*parenthesized_p=*/NULL,
13651 /*member_p=*/true);
13653 /* If something went wrong parsing the declarator, make sure
13654 that we at least consume some tokens. */
13655 if (declarator == cp_error_declarator)
13657 /* Skip to the end of the statement. */
13658 cp_parser_skip_to_end_of_statement (parser);
13659 /* If the next token is not a semicolon, that is
13660 probably because we just skipped over the body of
13661 a function. So, we consume a semicolon if
13662 present, but do not issue an error message if it
13663 is not present. */
13664 if (cp_lexer_next_token_is (parser->lexer,
13665 CPP_SEMICOLON))
13666 cp_lexer_consume_token (parser->lexer);
13667 return;
13670 if (declares_class_or_enum & 2)
13671 cp_parser_check_for_definition_in_return_type
13672 (declarator, decl_specifiers.type);
13674 /* Look for an asm-specification. */
13675 asm_specification = cp_parser_asm_specification_opt (parser);
13676 /* Look for attributes that apply to the declaration. */
13677 attributes = cp_parser_attributes_opt (parser);
13678 /* Remember which attributes are prefix attributes and
13679 which are not. */
13680 first_attribute = attributes;
13681 /* Combine the attributes. */
13682 attributes = chainon (prefix_attributes, attributes);
13684 /* If it's an `=', then we have a constant-initializer or a
13685 pure-specifier. It is not correct to parse the
13686 initializer before registering the member declaration
13687 since the member declaration should be in scope while
13688 its initializer is processed. However, the rest of the
13689 front end does not yet provide an interface that allows
13690 us to handle this correctly. */
13691 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13693 /* In [class.mem]:
13695 A pure-specifier shall be used only in the declaration of
13696 a virtual function.
13698 A member-declarator can contain a constant-initializer
13699 only if it declares a static member of integral or
13700 enumeration type.
13702 Therefore, if the DECLARATOR is for a function, we look
13703 for a pure-specifier; otherwise, we look for a
13704 constant-initializer. When we call `grokfield', it will
13705 perform more stringent semantics checks. */
13706 if (declarator->kind == cdk_function)
13707 initializer = cp_parser_pure_specifier (parser);
13708 else
13709 /* Parse the initializer. */
13710 initializer = cp_parser_constant_initializer (parser);
13712 /* Otherwise, there is no initializer. */
13713 else
13714 initializer = NULL_TREE;
13716 /* See if we are probably looking at a function
13717 definition. We are certainly not looking at a
13718 member-declarator. Calling `grokfield' has
13719 side-effects, so we must not do it unless we are sure
13720 that we are looking at a member-declarator. */
13721 if (cp_parser_token_starts_function_definition_p
13722 (cp_lexer_peek_token (parser->lexer)))
13724 /* The grammar does not allow a pure-specifier to be
13725 used when a member function is defined. (It is
13726 possible that this fact is an oversight in the
13727 standard, since a pure function may be defined
13728 outside of the class-specifier. */
13729 if (initializer)
13730 error ("pure-specifier on function-definition");
13731 decl = cp_parser_save_member_function_body (parser,
13732 &decl_specifiers,
13733 declarator,
13734 attributes);
13735 /* If the member was not a friend, declare it here. */
13736 if (!friend_p)
13737 finish_member_declaration (decl);
13738 /* Peek at the next token. */
13739 token = cp_lexer_peek_token (parser->lexer);
13740 /* If the next token is a semicolon, consume it. */
13741 if (token->type == CPP_SEMICOLON)
13742 cp_lexer_consume_token (parser->lexer);
13743 return;
13745 else
13747 /* Create the declaration. */
13748 decl = grokfield (declarator, &decl_specifiers,
13749 initializer, asm_specification,
13750 attributes);
13751 /* Any initialization must have been from a
13752 constant-expression. */
13753 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13754 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13758 /* Reset PREFIX_ATTRIBUTES. */
13759 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13760 attributes = TREE_CHAIN (attributes);
13761 if (attributes)
13762 TREE_CHAIN (attributes) = NULL_TREE;
13764 /* If there is any qualification still in effect, clear it
13765 now; we will be starting fresh with the next declarator. */
13766 parser->scope = NULL_TREE;
13767 parser->qualifying_scope = NULL_TREE;
13768 parser->object_scope = NULL_TREE;
13769 /* If it's a `,', then there are more declarators. */
13770 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13771 cp_lexer_consume_token (parser->lexer);
13772 /* If the next token isn't a `;', then we have a parse error. */
13773 else if (cp_lexer_next_token_is_not (parser->lexer,
13774 CPP_SEMICOLON))
13776 cp_parser_error (parser, "expected %<;%>");
13777 /* Skip tokens until we find a `;'. */
13778 cp_parser_skip_to_end_of_statement (parser);
13780 break;
13783 if (decl)
13785 /* Add DECL to the list of members. */
13786 if (!friend_p)
13787 finish_member_declaration (decl);
13789 if (TREE_CODE (decl) == FUNCTION_DECL)
13790 cp_parser_save_default_args (parser, decl);
13795 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13798 /* Parse a pure-specifier.
13800 pure-specifier:
13803 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13804 Otherwise, ERROR_MARK_NODE is returned. */
13806 static tree
13807 cp_parser_pure_specifier (cp_parser* parser)
13809 cp_token *token;
13811 /* Look for the `=' token. */
13812 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13813 return error_mark_node;
13814 /* Look for the `0' token. */
13815 token = cp_lexer_consume_token (parser->lexer);
13816 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13817 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13818 return integer_zero_node;
13820 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13821 cp_parser_skip_to_end_of_statement (parser);
13822 return error_mark_node;
13825 /* Parse a constant-initializer.
13827 constant-initializer:
13828 = constant-expression
13830 Returns a representation of the constant-expression. */
13832 static tree
13833 cp_parser_constant_initializer (cp_parser* parser)
13835 /* Look for the `=' token. */
13836 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13837 return error_mark_node;
13839 /* It is invalid to write:
13841 struct S { static const int i = { 7 }; };
13844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13846 cp_parser_error (parser,
13847 "a brace-enclosed initializer is not allowed here");
13848 /* Consume the opening brace. */
13849 cp_lexer_consume_token (parser->lexer);
13850 /* Skip the initializer. */
13851 cp_parser_skip_to_closing_brace (parser);
13852 /* Look for the trailing `}'. */
13853 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13855 return error_mark_node;
13858 return cp_parser_constant_expression (parser,
13859 /*allow_non_constant=*/false,
13860 NULL);
13863 /* Derived classes [gram.class.derived] */
13865 /* Parse a base-clause.
13867 base-clause:
13868 : base-specifier-list
13870 base-specifier-list:
13871 base-specifier
13872 base-specifier-list , base-specifier
13874 Returns a TREE_LIST representing the base-classes, in the order in
13875 which they were declared. The representation of each node is as
13876 described by cp_parser_base_specifier.
13878 In the case that no bases are specified, this function will return
13879 NULL_TREE, not ERROR_MARK_NODE. */
13881 static tree
13882 cp_parser_base_clause (cp_parser* parser)
13884 tree bases = NULL_TREE;
13886 /* Look for the `:' that begins the list. */
13887 cp_parser_require (parser, CPP_COLON, "`:'");
13889 /* Scan the base-specifier-list. */
13890 while (true)
13892 cp_token *token;
13893 tree base;
13895 /* Look for the base-specifier. */
13896 base = cp_parser_base_specifier (parser);
13897 /* Add BASE to the front of the list. */
13898 if (base != error_mark_node)
13900 TREE_CHAIN (base) = bases;
13901 bases = base;
13903 /* Peek at the next token. */
13904 token = cp_lexer_peek_token (parser->lexer);
13905 /* If it's not a comma, then the list is complete. */
13906 if (token->type != CPP_COMMA)
13907 break;
13908 /* Consume the `,'. */
13909 cp_lexer_consume_token (parser->lexer);
13912 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13913 base class had a qualified name. However, the next name that
13914 appears is certainly not qualified. */
13915 parser->scope = NULL_TREE;
13916 parser->qualifying_scope = NULL_TREE;
13917 parser->object_scope = NULL_TREE;
13919 return nreverse (bases);
13922 /* Parse a base-specifier.
13924 base-specifier:
13925 :: [opt] nested-name-specifier [opt] class-name
13926 virtual access-specifier [opt] :: [opt] nested-name-specifier
13927 [opt] class-name
13928 access-specifier virtual [opt] :: [opt] nested-name-specifier
13929 [opt] class-name
13931 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13932 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13933 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13934 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13936 static tree
13937 cp_parser_base_specifier (cp_parser* parser)
13939 cp_token *token;
13940 bool done = false;
13941 bool virtual_p = false;
13942 bool duplicate_virtual_error_issued_p = false;
13943 bool duplicate_access_error_issued_p = false;
13944 bool class_scope_p, template_p;
13945 tree access = access_default_node;
13946 tree type;
13948 /* Process the optional `virtual' and `access-specifier'. */
13949 while (!done)
13951 /* Peek at the next token. */
13952 token = cp_lexer_peek_token (parser->lexer);
13953 /* Process `virtual'. */
13954 switch (token->keyword)
13956 case RID_VIRTUAL:
13957 /* If `virtual' appears more than once, issue an error. */
13958 if (virtual_p && !duplicate_virtual_error_issued_p)
13960 cp_parser_error (parser,
13961 "%<virtual%> specified more than once in base-specified");
13962 duplicate_virtual_error_issued_p = true;
13965 virtual_p = true;
13967 /* Consume the `virtual' token. */
13968 cp_lexer_consume_token (parser->lexer);
13970 break;
13972 case RID_PUBLIC:
13973 case RID_PROTECTED:
13974 case RID_PRIVATE:
13975 /* If more than one access specifier appears, issue an
13976 error. */
13977 if (access != access_default_node
13978 && !duplicate_access_error_issued_p)
13980 cp_parser_error (parser,
13981 "more than one access specifier in base-specified");
13982 duplicate_access_error_issued_p = true;
13985 access = ridpointers[(int) token->keyword];
13987 /* Consume the access-specifier. */
13988 cp_lexer_consume_token (parser->lexer);
13990 break;
13992 default:
13993 done = true;
13994 break;
13997 /* It is not uncommon to see programs mechanically, erroneously, use
13998 the 'typename' keyword to denote (dependent) qualified types
13999 as base classes. */
14000 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14002 if (!processing_template_decl)
14003 error ("keyword %<typename%> not allowed outside of templates");
14004 else
14005 error ("keyword %<typename%> not allowed in this context "
14006 "(the base class is implicitly a type)");
14007 cp_lexer_consume_token (parser->lexer);
14010 /* Look for the optional `::' operator. */
14011 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14012 /* Look for the nested-name-specifier. The simplest way to
14013 implement:
14015 [temp.res]
14017 The keyword `typename' is not permitted in a base-specifier or
14018 mem-initializer; in these contexts a qualified name that
14019 depends on a template-parameter is implicitly assumed to be a
14020 type name.
14022 is to pretend that we have seen the `typename' keyword at this
14023 point. */
14024 cp_parser_nested_name_specifier_opt (parser,
14025 /*typename_keyword_p=*/true,
14026 /*check_dependency_p=*/true,
14027 typename_type,
14028 /*is_declaration=*/true);
14029 /* If the base class is given by a qualified name, assume that names
14030 we see are type names or templates, as appropriate. */
14031 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14032 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14034 /* Finally, look for the class-name. */
14035 type = cp_parser_class_name (parser,
14036 class_scope_p,
14037 template_p,
14038 typename_type,
14039 /*check_dependency_p=*/true,
14040 /*class_head_p=*/false,
14041 /*is_declaration=*/true);
14043 if (type == error_mark_node)
14044 return error_mark_node;
14046 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14049 /* Exception handling [gram.exception] */
14051 /* Parse an (optional) exception-specification.
14053 exception-specification:
14054 throw ( type-id-list [opt] )
14056 Returns a TREE_LIST representing the exception-specification. The
14057 TREE_VALUE of each node is a type. */
14059 static tree
14060 cp_parser_exception_specification_opt (cp_parser* parser)
14062 cp_token *token;
14063 tree type_id_list;
14065 /* Peek at the next token. */
14066 token = cp_lexer_peek_token (parser->lexer);
14067 /* If it's not `throw', then there's no exception-specification. */
14068 if (!cp_parser_is_keyword (token, RID_THROW))
14069 return NULL_TREE;
14071 /* Consume the `throw'. */
14072 cp_lexer_consume_token (parser->lexer);
14074 /* Look for the `('. */
14075 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14077 /* Peek at the next token. */
14078 token = cp_lexer_peek_token (parser->lexer);
14079 /* If it's not a `)', then there is a type-id-list. */
14080 if (token->type != CPP_CLOSE_PAREN)
14082 const char *saved_message;
14084 /* Types may not be defined in an exception-specification. */
14085 saved_message = parser->type_definition_forbidden_message;
14086 parser->type_definition_forbidden_message
14087 = "types may not be defined in an exception-specification";
14088 /* Parse the type-id-list. */
14089 type_id_list = cp_parser_type_id_list (parser);
14090 /* Restore the saved message. */
14091 parser->type_definition_forbidden_message = saved_message;
14093 else
14094 type_id_list = empty_except_spec;
14096 /* Look for the `)'. */
14097 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14099 return type_id_list;
14102 /* Parse an (optional) type-id-list.
14104 type-id-list:
14105 type-id
14106 type-id-list , type-id
14108 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14109 in the order that the types were presented. */
14111 static tree
14112 cp_parser_type_id_list (cp_parser* parser)
14114 tree types = NULL_TREE;
14116 while (true)
14118 cp_token *token;
14119 tree type;
14121 /* Get the next type-id. */
14122 type = cp_parser_type_id (parser);
14123 /* Add it to the list. */
14124 types = add_exception_specifier (types, type, /*complain=*/1);
14125 /* Peek at the next token. */
14126 token = cp_lexer_peek_token (parser->lexer);
14127 /* If it is not a `,', we are done. */
14128 if (token->type != CPP_COMMA)
14129 break;
14130 /* Consume the `,'. */
14131 cp_lexer_consume_token (parser->lexer);
14134 return nreverse (types);
14137 /* Parse a try-block.
14139 try-block:
14140 try compound-statement handler-seq */
14142 static tree
14143 cp_parser_try_block (cp_parser* parser)
14145 tree try_block;
14147 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14148 try_block = begin_try_block ();
14149 cp_parser_compound_statement (parser, NULL, true);
14150 finish_try_block (try_block);
14151 cp_parser_handler_seq (parser);
14152 finish_handler_sequence (try_block);
14154 return try_block;
14157 /* Parse a function-try-block.
14159 function-try-block:
14160 try ctor-initializer [opt] function-body handler-seq */
14162 static bool
14163 cp_parser_function_try_block (cp_parser* parser)
14165 tree try_block;
14166 bool ctor_initializer_p;
14168 /* Look for the `try' keyword. */
14169 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14170 return false;
14171 /* Let the rest of the front-end know where we are. */
14172 try_block = begin_function_try_block ();
14173 /* Parse the function-body. */
14174 ctor_initializer_p
14175 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14176 /* We're done with the `try' part. */
14177 finish_function_try_block (try_block);
14178 /* Parse the handlers. */
14179 cp_parser_handler_seq (parser);
14180 /* We're done with the handlers. */
14181 finish_function_handler_sequence (try_block);
14183 return ctor_initializer_p;
14186 /* Parse a handler-seq.
14188 handler-seq:
14189 handler handler-seq [opt] */
14191 static void
14192 cp_parser_handler_seq (cp_parser* parser)
14194 while (true)
14196 cp_token *token;
14198 /* Parse the handler. */
14199 cp_parser_handler (parser);
14200 /* Peek at the next token. */
14201 token = cp_lexer_peek_token (parser->lexer);
14202 /* If it's not `catch' then there are no more handlers. */
14203 if (!cp_parser_is_keyword (token, RID_CATCH))
14204 break;
14208 /* Parse a handler.
14210 handler:
14211 catch ( exception-declaration ) compound-statement */
14213 static void
14214 cp_parser_handler (cp_parser* parser)
14216 tree handler;
14217 tree declaration;
14219 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14220 handler = begin_handler ();
14221 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14222 declaration = cp_parser_exception_declaration (parser);
14223 finish_handler_parms (declaration, handler);
14224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14225 cp_parser_compound_statement (parser, NULL, false);
14226 finish_handler (handler);
14229 /* Parse an exception-declaration.
14231 exception-declaration:
14232 type-specifier-seq declarator
14233 type-specifier-seq abstract-declarator
14234 type-specifier-seq
14237 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14238 ellipsis variant is used. */
14240 static tree
14241 cp_parser_exception_declaration (cp_parser* parser)
14243 tree decl;
14244 cp_decl_specifier_seq type_specifiers;
14245 cp_declarator *declarator;
14246 const char *saved_message;
14248 /* If it's an ellipsis, it's easy to handle. */
14249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14251 /* Consume the `...' token. */
14252 cp_lexer_consume_token (parser->lexer);
14253 return NULL_TREE;
14256 /* Types may not be defined in exception-declarations. */
14257 saved_message = parser->type_definition_forbidden_message;
14258 parser->type_definition_forbidden_message
14259 = "types may not be defined in exception-declarations";
14261 /* Parse the type-specifier-seq. */
14262 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14263 &type_specifiers);
14264 /* If it's a `)', then there is no declarator. */
14265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14266 declarator = NULL;
14267 else
14268 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14269 /*ctor_dtor_or_conv_p=*/NULL,
14270 /*parenthesized_p=*/NULL,
14271 /*member_p=*/false);
14273 /* Restore the saved message. */
14274 parser->type_definition_forbidden_message = saved_message;
14276 if (type_specifiers.any_specifiers_p)
14278 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14279 if (decl == NULL_TREE)
14280 error ("invalid catch parameter");
14282 else
14283 decl = NULL_TREE;
14285 return decl;
14288 /* Parse a throw-expression.
14290 throw-expression:
14291 throw assignment-expression [opt]
14293 Returns a THROW_EXPR representing the throw-expression. */
14295 static tree
14296 cp_parser_throw_expression (cp_parser* parser)
14298 tree expression;
14299 cp_token* token;
14301 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14302 token = cp_lexer_peek_token (parser->lexer);
14303 /* Figure out whether or not there is an assignment-expression
14304 following the "throw" keyword. */
14305 if (token->type == CPP_COMMA
14306 || token->type == CPP_SEMICOLON
14307 || token->type == CPP_CLOSE_PAREN
14308 || token->type == CPP_CLOSE_SQUARE
14309 || token->type == CPP_CLOSE_BRACE
14310 || token->type == CPP_COLON)
14311 expression = NULL_TREE;
14312 else
14313 expression = cp_parser_assignment_expression (parser,
14314 /*cast_p=*/false);
14316 return build_throw (expression);
14319 /* GNU Extensions */
14321 /* Parse an (optional) asm-specification.
14323 asm-specification:
14324 asm ( string-literal )
14326 If the asm-specification is present, returns a STRING_CST
14327 corresponding to the string-literal. Otherwise, returns
14328 NULL_TREE. */
14330 static tree
14331 cp_parser_asm_specification_opt (cp_parser* parser)
14333 cp_token *token;
14334 tree asm_specification;
14336 /* Peek at the next token. */
14337 token = cp_lexer_peek_token (parser->lexer);
14338 /* If the next token isn't the `asm' keyword, then there's no
14339 asm-specification. */
14340 if (!cp_parser_is_keyword (token, RID_ASM))
14341 return NULL_TREE;
14343 /* Consume the `asm' token. */
14344 cp_lexer_consume_token (parser->lexer);
14345 /* Look for the `('. */
14346 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14348 /* Look for the string-literal. */
14349 asm_specification = cp_parser_string_literal (parser, false, false);
14351 /* Look for the `)'. */
14352 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14354 return asm_specification;
14357 /* Parse an asm-operand-list.
14359 asm-operand-list:
14360 asm-operand
14361 asm-operand-list , asm-operand
14363 asm-operand:
14364 string-literal ( expression )
14365 [ string-literal ] string-literal ( expression )
14367 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14368 each node is the expression. The TREE_PURPOSE is itself a
14369 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14370 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14371 is a STRING_CST for the string literal before the parenthesis. */
14373 static tree
14374 cp_parser_asm_operand_list (cp_parser* parser)
14376 tree asm_operands = NULL_TREE;
14378 while (true)
14380 tree string_literal;
14381 tree expression;
14382 tree name;
14384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14386 /* Consume the `[' token. */
14387 cp_lexer_consume_token (parser->lexer);
14388 /* Read the operand name. */
14389 name = cp_parser_identifier (parser);
14390 if (name != error_mark_node)
14391 name = build_string (IDENTIFIER_LENGTH (name),
14392 IDENTIFIER_POINTER (name));
14393 /* Look for the closing `]'. */
14394 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14396 else
14397 name = NULL_TREE;
14398 /* Look for the string-literal. */
14399 string_literal = cp_parser_string_literal (parser, false, false);
14401 /* Look for the `('. */
14402 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14403 /* Parse the expression. */
14404 expression = cp_parser_expression (parser, /*cast_p=*/false);
14405 /* Look for the `)'. */
14406 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14408 /* Add this operand to the list. */
14409 asm_operands = tree_cons (build_tree_list (name, string_literal),
14410 expression,
14411 asm_operands);
14412 /* If the next token is not a `,', there are no more
14413 operands. */
14414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14415 break;
14416 /* Consume the `,'. */
14417 cp_lexer_consume_token (parser->lexer);
14420 return nreverse (asm_operands);
14423 /* Parse an asm-clobber-list.
14425 asm-clobber-list:
14426 string-literal
14427 asm-clobber-list , string-literal
14429 Returns a TREE_LIST, indicating the clobbers in the order that they
14430 appeared. The TREE_VALUE of each node is a STRING_CST. */
14432 static tree
14433 cp_parser_asm_clobber_list (cp_parser* parser)
14435 tree clobbers = NULL_TREE;
14437 while (true)
14439 tree string_literal;
14441 /* Look for the string literal. */
14442 string_literal = cp_parser_string_literal (parser, false, false);
14443 /* Add it to the list. */
14444 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14445 /* If the next token is not a `,', then the list is
14446 complete. */
14447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14448 break;
14449 /* Consume the `,' token. */
14450 cp_lexer_consume_token (parser->lexer);
14453 return clobbers;
14456 /* Parse an (optional) series of attributes.
14458 attributes:
14459 attributes attribute
14461 attribute:
14462 __attribute__ (( attribute-list [opt] ))
14464 The return value is as for cp_parser_attribute_list. */
14466 static tree
14467 cp_parser_attributes_opt (cp_parser* parser)
14469 tree attributes = NULL_TREE;
14471 while (true)
14473 cp_token *token;
14474 tree attribute_list;
14476 /* Peek at the next token. */
14477 token = cp_lexer_peek_token (parser->lexer);
14478 /* If it's not `__attribute__', then we're done. */
14479 if (token->keyword != RID_ATTRIBUTE)
14480 break;
14482 /* Consume the `__attribute__' keyword. */
14483 cp_lexer_consume_token (parser->lexer);
14484 /* Look for the two `(' tokens. */
14485 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14486 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14488 /* Peek at the next token. */
14489 token = cp_lexer_peek_token (parser->lexer);
14490 if (token->type != CPP_CLOSE_PAREN)
14491 /* Parse the attribute-list. */
14492 attribute_list = cp_parser_attribute_list (parser);
14493 else
14494 /* If the next token is a `)', then there is no attribute
14495 list. */
14496 attribute_list = NULL;
14498 /* Look for the two `)' tokens. */
14499 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14502 /* Add these new attributes to the list. */
14503 attributes = chainon (attributes, attribute_list);
14506 return attributes;
14509 /* Parse an attribute-list.
14511 attribute-list:
14512 attribute
14513 attribute-list , attribute
14515 attribute:
14516 identifier
14517 identifier ( identifier )
14518 identifier ( identifier , expression-list )
14519 identifier ( expression-list )
14521 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14522 to an attribute. The TREE_PURPOSE of each node is the identifier
14523 indicating which attribute is in use. The TREE_VALUE represents
14524 the arguments, if any. */
14526 static tree
14527 cp_parser_attribute_list (cp_parser* parser)
14529 tree attribute_list = NULL_TREE;
14530 bool save_translate_strings_p = parser->translate_strings_p;
14532 parser->translate_strings_p = false;
14533 while (true)
14535 cp_token *token;
14536 tree identifier;
14537 tree attribute;
14539 /* Look for the identifier. We also allow keywords here; for
14540 example `__attribute__ ((const))' is legal. */
14541 token = cp_lexer_peek_token (parser->lexer);
14542 if (token->type == CPP_NAME
14543 || token->type == CPP_KEYWORD)
14545 /* Consume the token. */
14546 token = cp_lexer_consume_token (parser->lexer);
14548 /* Save away the identifier that indicates which attribute
14549 this is. */
14550 identifier = token->value;
14551 attribute = build_tree_list (identifier, NULL_TREE);
14553 /* Peek at the next token. */
14554 token = cp_lexer_peek_token (parser->lexer);
14555 /* If it's an `(', then parse the attribute arguments. */
14556 if (token->type == CPP_OPEN_PAREN)
14558 tree arguments;
14560 arguments = (cp_parser_parenthesized_expression_list
14561 (parser, true, /*cast_p=*/false,
14562 /*non_constant_p=*/NULL));
14563 /* Save the identifier and arguments away. */
14564 TREE_VALUE (attribute) = arguments;
14567 /* Add this attribute to the list. */
14568 TREE_CHAIN (attribute) = attribute_list;
14569 attribute_list = attribute;
14571 token = cp_lexer_peek_token (parser->lexer);
14573 /* Now, look for more attributes. If the next token isn't a
14574 `,', we're done. */
14575 if (token->type != CPP_COMMA)
14576 break;
14578 /* Consume the comma and keep going. */
14579 cp_lexer_consume_token (parser->lexer);
14581 parser->translate_strings_p = save_translate_strings_p;
14583 /* We built up the list in reverse order. */
14584 return nreverse (attribute_list);
14587 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14588 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14589 current value of the PEDANTIC flag, regardless of whether or not
14590 the `__extension__' keyword is present. The caller is responsible
14591 for restoring the value of the PEDANTIC flag. */
14593 static bool
14594 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14596 /* Save the old value of the PEDANTIC flag. */
14597 *saved_pedantic = pedantic;
14599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14601 /* Consume the `__extension__' token. */
14602 cp_lexer_consume_token (parser->lexer);
14603 /* We're not being pedantic while the `__extension__' keyword is
14604 in effect. */
14605 pedantic = 0;
14607 return true;
14610 return false;
14613 /* Parse a label declaration.
14615 label-declaration:
14616 __label__ label-declarator-seq ;
14618 label-declarator-seq:
14619 identifier , label-declarator-seq
14620 identifier */
14622 static void
14623 cp_parser_label_declaration (cp_parser* parser)
14625 /* Look for the `__label__' keyword. */
14626 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14628 while (true)
14630 tree identifier;
14632 /* Look for an identifier. */
14633 identifier = cp_parser_identifier (parser);
14634 /* If we failed, stop. */
14635 if (identifier == error_mark_node)
14636 break;
14637 /* Declare it as a label. */
14638 finish_label_decl (identifier);
14639 /* If the next token is a `;', stop. */
14640 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14641 break;
14642 /* Look for the `,' separating the label declarations. */
14643 cp_parser_require (parser, CPP_COMMA, "`,'");
14646 /* Look for the final `;'. */
14647 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14650 /* Support Functions */
14652 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14653 NAME should have one of the representations used for an
14654 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14655 is returned. If PARSER->SCOPE is a dependent type, then a
14656 SCOPE_REF is returned.
14658 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14659 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14660 was formed. Abstractly, such entities should not be passed to this
14661 function, because they do not need to be looked up, but it is
14662 simpler to check for this special case here, rather than at the
14663 call-sites.
14665 In cases not explicitly covered above, this function returns a
14666 DECL, OVERLOAD, or baselink representing the result of the lookup.
14667 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14668 is returned.
14670 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14671 (e.g., "struct") that was used. In that case bindings that do not
14672 refer to types are ignored.
14674 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14675 ignored.
14677 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14678 are ignored.
14680 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14681 types.
14683 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14684 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14685 NULL_TREE otherwise. */
14687 static tree
14688 cp_parser_lookup_name (cp_parser *parser, tree name,
14689 enum tag_types tag_type,
14690 bool is_template,
14691 bool is_namespace,
14692 bool check_dependency,
14693 tree *ambiguous_decls)
14695 int flags = 0;
14696 tree decl;
14697 tree object_type = parser->context->object_type;
14699 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14700 flags |= LOOKUP_COMPLAIN;
14702 /* Assume that the lookup will be unambiguous. */
14703 if (ambiguous_decls)
14704 *ambiguous_decls = NULL_TREE;
14706 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14707 no longer valid. Note that if we are parsing tentatively, and
14708 the parse fails, OBJECT_TYPE will be automatically restored. */
14709 parser->context->object_type = NULL_TREE;
14711 if (name == error_mark_node)
14712 return error_mark_node;
14714 /* A template-id has already been resolved; there is no lookup to
14715 do. */
14716 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14717 return name;
14718 if (BASELINK_P (name))
14720 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14721 == TEMPLATE_ID_EXPR);
14722 return name;
14725 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14726 it should already have been checked to make sure that the name
14727 used matches the type being destroyed. */
14728 if (TREE_CODE (name) == BIT_NOT_EXPR)
14730 tree type;
14732 /* Figure out to which type this destructor applies. */
14733 if (parser->scope)
14734 type = parser->scope;
14735 else if (object_type)
14736 type = object_type;
14737 else
14738 type = current_class_type;
14739 /* If that's not a class type, there is no destructor. */
14740 if (!type || !CLASS_TYPE_P (type))
14741 return error_mark_node;
14742 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14743 lazily_declare_fn (sfk_destructor, type);
14744 if (!CLASSTYPE_DESTRUCTORS (type))
14745 return error_mark_node;
14746 /* If it was a class type, return the destructor. */
14747 return CLASSTYPE_DESTRUCTORS (type);
14750 /* By this point, the NAME should be an ordinary identifier. If
14751 the id-expression was a qualified name, the qualifying scope is
14752 stored in PARSER->SCOPE at this point. */
14753 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14755 /* Perform the lookup. */
14756 if (parser->scope)
14758 bool dependent_p;
14760 if (parser->scope == error_mark_node)
14761 return error_mark_node;
14763 /* If the SCOPE is dependent, the lookup must be deferred until
14764 the template is instantiated -- unless we are explicitly
14765 looking up names in uninstantiated templates. Even then, we
14766 cannot look up the name if the scope is not a class type; it
14767 might, for example, be a template type parameter. */
14768 dependent_p = (TYPE_P (parser->scope)
14769 && !(parser->in_declarator_p
14770 && currently_open_class (parser->scope))
14771 && dependent_type_p (parser->scope));
14772 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14773 && dependent_p)
14775 if (tag_type)
14777 tree type;
14779 /* The resolution to Core Issue 180 says that `struct
14780 A::B' should be considered a type-name, even if `A'
14781 is dependent. */
14782 type = make_typename_type (parser->scope, name, tag_type,
14783 /*complain=*/tf_error);
14784 decl = TYPE_NAME (type);
14786 else if (is_template
14787 && (cp_parser_next_token_ends_template_argument_p (parser)
14788 || cp_lexer_next_token_is (parser->lexer,
14789 CPP_CLOSE_PAREN)))
14790 decl = make_unbound_class_template (parser->scope,
14791 name, NULL_TREE,
14792 /*complain=*/tf_error);
14793 else
14794 decl = build_qualified_name (/*type=*/NULL_TREE,
14795 parser->scope, name,
14796 is_template);
14798 else
14800 tree pushed_scope = NULL_TREE;
14802 /* If PARSER->SCOPE is a dependent type, then it must be a
14803 class type, and we must not be checking dependencies;
14804 otherwise, we would have processed this lookup above. So
14805 that PARSER->SCOPE is not considered a dependent base by
14806 lookup_member, we must enter the scope here. */
14807 if (dependent_p)
14808 pushed_scope = push_scope (parser->scope);
14809 /* If the PARSER->SCOPE is a template specialization, it
14810 may be instantiated during name lookup. In that case,
14811 errors may be issued. Even if we rollback the current
14812 tentative parse, those errors are valid. */
14813 decl = lookup_qualified_name (parser->scope, name,
14814 tag_type != none_type,
14815 /*complain=*/true);
14816 if (pushed_scope)
14817 pop_scope (pushed_scope);
14819 parser->qualifying_scope = parser->scope;
14820 parser->object_scope = NULL_TREE;
14822 else if (object_type)
14824 tree object_decl = NULL_TREE;
14825 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14826 OBJECT_TYPE is not a class. */
14827 if (CLASS_TYPE_P (object_type))
14828 /* If the OBJECT_TYPE is a template specialization, it may
14829 be instantiated during name lookup. In that case, errors
14830 may be issued. Even if we rollback the current tentative
14831 parse, those errors are valid. */
14832 object_decl = lookup_member (object_type,
14833 name,
14834 /*protect=*/0,
14835 tag_type != none_type);
14836 /* Look it up in the enclosing context, too. */
14837 decl = lookup_name_real (name, tag_type != none_type,
14838 /*nonclass=*/0,
14839 /*block_p=*/true, is_namespace, flags);
14840 parser->object_scope = object_type;
14841 parser->qualifying_scope = NULL_TREE;
14842 if (object_decl)
14843 decl = object_decl;
14845 else
14847 decl = lookup_name_real (name, tag_type != none_type,
14848 /*nonclass=*/0,
14849 /*block_p=*/true, is_namespace, flags);
14850 parser->qualifying_scope = NULL_TREE;
14851 parser->object_scope = NULL_TREE;
14854 /* If the lookup failed, let our caller know. */
14855 if (!decl || decl == error_mark_node)
14856 return error_mark_node;
14858 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14859 if (TREE_CODE (decl) == TREE_LIST)
14861 if (ambiguous_decls)
14862 *ambiguous_decls = decl;
14863 /* The error message we have to print is too complicated for
14864 cp_parser_error, so we incorporate its actions directly. */
14865 if (!cp_parser_simulate_error (parser))
14867 error ("reference to %qD is ambiguous", name);
14868 print_candidates (decl);
14870 return error_mark_node;
14873 gcc_assert (DECL_P (decl)
14874 || TREE_CODE (decl) == OVERLOAD
14875 || TREE_CODE (decl) == SCOPE_REF
14876 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14877 || BASELINK_P (decl));
14879 /* If we have resolved the name of a member declaration, check to
14880 see if the declaration is accessible. When the name resolves to
14881 set of overloaded functions, accessibility is checked when
14882 overload resolution is done.
14884 During an explicit instantiation, access is not checked at all,
14885 as per [temp.explicit]. */
14886 if (DECL_P (decl))
14887 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14889 return decl;
14892 /* Like cp_parser_lookup_name, but for use in the typical case where
14893 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14894 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14896 static tree
14897 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14899 return cp_parser_lookup_name (parser, name,
14900 none_type,
14901 /*is_template=*/false,
14902 /*is_namespace=*/false,
14903 /*check_dependency=*/true,
14904 /*ambiguous_decls=*/NULL);
14907 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14908 the current context, return the TYPE_DECL. If TAG_NAME_P is
14909 true, the DECL indicates the class being defined in a class-head,
14910 or declared in an elaborated-type-specifier.
14912 Otherwise, return DECL. */
14914 static tree
14915 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14917 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14918 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14920 struct A {
14921 template <typename T> struct B;
14924 template <typename T> struct A::B {};
14926 Similarly, in an elaborated-type-specifier:
14928 namespace N { struct X{}; }
14930 struct A {
14931 template <typename T> friend struct N::X;
14934 However, if the DECL refers to a class type, and we are in
14935 the scope of the class, then the name lookup automatically
14936 finds the TYPE_DECL created by build_self_reference rather
14937 than a TEMPLATE_DECL. For example, in:
14939 template <class T> struct S {
14940 S s;
14943 there is no need to handle such case. */
14945 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14946 return DECL_TEMPLATE_RESULT (decl);
14948 return decl;
14951 /* If too many, or too few, template-parameter lists apply to the
14952 declarator, issue an error message. Returns TRUE if all went well,
14953 and FALSE otherwise. */
14955 static bool
14956 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14957 cp_declarator *declarator)
14959 unsigned num_templates;
14961 /* We haven't seen any classes that involve template parameters yet. */
14962 num_templates = 0;
14964 switch (declarator->kind)
14966 case cdk_id:
14967 if (declarator->u.id.qualifying_scope)
14969 tree scope;
14970 tree member;
14972 scope = declarator->u.id.qualifying_scope;
14973 member = declarator->u.id.unqualified_name;
14975 while (scope && CLASS_TYPE_P (scope))
14977 /* You're supposed to have one `template <...>'
14978 for every template class, but you don't need one
14979 for a full specialization. For example:
14981 template <class T> struct S{};
14982 template <> struct S<int> { void f(); };
14983 void S<int>::f () {}
14985 is correct; there shouldn't be a `template <>' for
14986 the definition of `S<int>::f'. */
14987 if (CLASSTYPE_TEMPLATE_INFO (scope)
14988 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14989 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14990 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14991 ++num_templates;
14993 scope = TYPE_CONTEXT (scope);
14996 else if (TREE_CODE (declarator->u.id.unqualified_name)
14997 == TEMPLATE_ID_EXPR)
14998 /* If the DECLARATOR has the form `X<y>' then it uses one
14999 additional level of template parameters. */
15000 ++num_templates;
15002 return cp_parser_check_template_parameters (parser,
15003 num_templates);
15005 case cdk_function:
15006 case cdk_array:
15007 case cdk_pointer:
15008 case cdk_reference:
15009 case cdk_ptrmem:
15010 return (cp_parser_check_declarator_template_parameters
15011 (parser, declarator->declarator));
15013 case cdk_error:
15014 return true;
15016 default:
15017 gcc_unreachable ();
15019 return false;
15022 /* NUM_TEMPLATES were used in the current declaration. If that is
15023 invalid, return FALSE and issue an error messages. Otherwise,
15024 return TRUE. */
15026 static bool
15027 cp_parser_check_template_parameters (cp_parser* parser,
15028 unsigned num_templates)
15030 /* If there are more template classes than parameter lists, we have
15031 something like:
15033 template <class T> void S<T>::R<T>::f (); */
15034 if (parser->num_template_parameter_lists < num_templates)
15036 error ("too few template-parameter-lists");
15037 return false;
15039 /* If there are the same number of template classes and parameter
15040 lists, that's OK. */
15041 if (parser->num_template_parameter_lists == num_templates)
15042 return true;
15043 /* If there are more, but only one more, then we are referring to a
15044 member template. That's OK too. */
15045 if (parser->num_template_parameter_lists == num_templates + 1)
15046 return true;
15047 /* Otherwise, there are too many template parameter lists. We have
15048 something like:
15050 template <class T> template <class U> void S::f(); */
15051 error ("too many template-parameter-lists");
15052 return false;
15055 /* Parse an optional `::' token indicating that the following name is
15056 from the global namespace. If so, PARSER->SCOPE is set to the
15057 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15058 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15059 Returns the new value of PARSER->SCOPE, if the `::' token is
15060 present, and NULL_TREE otherwise. */
15062 static tree
15063 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15065 cp_token *token;
15067 /* Peek at the next token. */
15068 token = cp_lexer_peek_token (parser->lexer);
15069 /* If we're looking at a `::' token then we're starting from the
15070 global namespace, not our current location. */
15071 if (token->type == CPP_SCOPE)
15073 /* Consume the `::' token. */
15074 cp_lexer_consume_token (parser->lexer);
15075 /* Set the SCOPE so that we know where to start the lookup. */
15076 parser->scope = global_namespace;
15077 parser->qualifying_scope = global_namespace;
15078 parser->object_scope = NULL_TREE;
15080 return parser->scope;
15082 else if (!current_scope_valid_p)
15084 parser->scope = NULL_TREE;
15085 parser->qualifying_scope = NULL_TREE;
15086 parser->object_scope = NULL_TREE;
15089 return NULL_TREE;
15092 /* Returns TRUE if the upcoming token sequence is the start of a
15093 constructor declarator. If FRIEND_P is true, the declarator is
15094 preceded by the `friend' specifier. */
15096 static bool
15097 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15099 bool constructor_p;
15100 tree type_decl = NULL_TREE;
15101 bool nested_name_p;
15102 cp_token *next_token;
15104 /* The common case is that this is not a constructor declarator, so
15105 try to avoid doing lots of work if at all possible. It's not
15106 valid declare a constructor at function scope. */
15107 if (at_function_scope_p ())
15108 return false;
15109 /* And only certain tokens can begin a constructor declarator. */
15110 next_token = cp_lexer_peek_token (parser->lexer);
15111 if (next_token->type != CPP_NAME
15112 && next_token->type != CPP_SCOPE
15113 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15114 && next_token->type != CPP_TEMPLATE_ID)
15115 return false;
15117 /* Parse tentatively; we are going to roll back all of the tokens
15118 consumed here. */
15119 cp_parser_parse_tentatively (parser);
15120 /* Assume that we are looking at a constructor declarator. */
15121 constructor_p = true;
15123 /* Look for the optional `::' operator. */
15124 cp_parser_global_scope_opt (parser,
15125 /*current_scope_valid_p=*/false);
15126 /* Look for the nested-name-specifier. */
15127 nested_name_p
15128 = (cp_parser_nested_name_specifier_opt (parser,
15129 /*typename_keyword_p=*/false,
15130 /*check_dependency_p=*/false,
15131 /*type_p=*/false,
15132 /*is_declaration=*/false)
15133 != NULL_TREE);
15134 /* Outside of a class-specifier, there must be a
15135 nested-name-specifier. */
15136 if (!nested_name_p &&
15137 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15138 || friend_p))
15139 constructor_p = false;
15140 /* If we still think that this might be a constructor-declarator,
15141 look for a class-name. */
15142 if (constructor_p)
15144 /* If we have:
15146 template <typename T> struct S { S(); };
15147 template <typename T> S<T>::S ();
15149 we must recognize that the nested `S' names a class.
15150 Similarly, for:
15152 template <typename T> S<T>::S<T> ();
15154 we must recognize that the nested `S' names a template. */
15155 type_decl = cp_parser_class_name (parser,
15156 /*typename_keyword_p=*/false,
15157 /*template_keyword_p=*/false,
15158 none_type,
15159 /*check_dependency_p=*/false,
15160 /*class_head_p=*/false,
15161 /*is_declaration=*/false);
15162 /* If there was no class-name, then this is not a constructor. */
15163 constructor_p = !cp_parser_error_occurred (parser);
15166 /* If we're still considering a constructor, we have to see a `(',
15167 to begin the parameter-declaration-clause, followed by either a
15168 `)', an `...', or a decl-specifier. We need to check for a
15169 type-specifier to avoid being fooled into thinking that:
15171 S::S (f) (int);
15173 is a constructor. (It is actually a function named `f' that
15174 takes one parameter (of type `int') and returns a value of type
15175 `S::S'. */
15176 if (constructor_p
15177 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15180 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15181 /* A parameter declaration begins with a decl-specifier,
15182 which is either the "attribute" keyword, a storage class
15183 specifier, or (usually) a type-specifier. */
15184 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15185 && !cp_parser_storage_class_specifier_opt (parser))
15187 tree type;
15188 tree pushed_scope = NULL_TREE;
15189 unsigned saved_num_template_parameter_lists;
15191 /* Names appearing in the type-specifier should be looked up
15192 in the scope of the class. */
15193 if (current_class_type)
15194 type = NULL_TREE;
15195 else
15197 type = TREE_TYPE (type_decl);
15198 if (TREE_CODE (type) == TYPENAME_TYPE)
15200 type = resolve_typename_type (type,
15201 /*only_current_p=*/false);
15202 if (type == error_mark_node)
15204 cp_parser_abort_tentative_parse (parser);
15205 return false;
15208 pushed_scope = push_scope (type);
15211 /* Inside the constructor parameter list, surrounding
15212 template-parameter-lists do not apply. */
15213 saved_num_template_parameter_lists
15214 = parser->num_template_parameter_lists;
15215 parser->num_template_parameter_lists = 0;
15217 /* Look for the type-specifier. */
15218 cp_parser_type_specifier (parser,
15219 CP_PARSER_FLAGS_NONE,
15220 /*decl_specs=*/NULL,
15221 /*is_declarator=*/true,
15222 /*declares_class_or_enum=*/NULL,
15223 /*is_cv_qualifier=*/NULL);
15225 parser->num_template_parameter_lists
15226 = saved_num_template_parameter_lists;
15228 /* Leave the scope of the class. */
15229 if (pushed_scope)
15230 pop_scope (pushed_scope);
15232 constructor_p = !cp_parser_error_occurred (parser);
15235 else
15236 constructor_p = false;
15237 /* We did not really want to consume any tokens. */
15238 cp_parser_abort_tentative_parse (parser);
15240 return constructor_p;
15243 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15244 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15245 they must be performed once we are in the scope of the function.
15247 Returns the function defined. */
15249 static tree
15250 cp_parser_function_definition_from_specifiers_and_declarator
15251 (cp_parser* parser,
15252 cp_decl_specifier_seq *decl_specifiers,
15253 tree attributes,
15254 const cp_declarator *declarator)
15256 tree fn;
15257 bool success_p;
15259 /* Begin the function-definition. */
15260 success_p = start_function (decl_specifiers, declarator, attributes);
15262 /* The things we're about to see are not directly qualified by any
15263 template headers we've seen thus far. */
15264 reset_specialization ();
15266 /* If there were names looked up in the decl-specifier-seq that we
15267 did not check, check them now. We must wait until we are in the
15268 scope of the function to perform the checks, since the function
15269 might be a friend. */
15270 perform_deferred_access_checks ();
15272 if (!success_p)
15274 /* Skip the entire function. */
15275 error ("invalid function declaration");
15276 cp_parser_skip_to_end_of_block_or_statement (parser);
15277 fn = error_mark_node;
15279 else
15280 fn = cp_parser_function_definition_after_declarator (parser,
15281 /*inline_p=*/false);
15283 return fn;
15286 /* Parse the part of a function-definition that follows the
15287 declarator. INLINE_P is TRUE iff this function is an inline
15288 function defined with a class-specifier.
15290 Returns the function defined. */
15292 static tree
15293 cp_parser_function_definition_after_declarator (cp_parser* parser,
15294 bool inline_p)
15296 tree fn;
15297 bool ctor_initializer_p = false;
15298 bool saved_in_unbraced_linkage_specification_p;
15299 unsigned saved_num_template_parameter_lists;
15301 /* If the next token is `return', then the code may be trying to
15302 make use of the "named return value" extension that G++ used to
15303 support. */
15304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15306 /* Consume the `return' keyword. */
15307 cp_lexer_consume_token (parser->lexer);
15308 /* Look for the identifier that indicates what value is to be
15309 returned. */
15310 cp_parser_identifier (parser);
15311 /* Issue an error message. */
15312 error ("named return values are no longer supported");
15313 /* Skip tokens until we reach the start of the function body. */
15314 while (true)
15316 cp_token *token = cp_lexer_peek_token (parser->lexer);
15317 if (token->type == CPP_OPEN_BRACE
15318 || token->type == CPP_EOF
15319 || token->type == CPP_PRAGMA_EOL)
15320 break;
15321 cp_lexer_consume_token (parser->lexer);
15324 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15325 anything declared inside `f'. */
15326 saved_in_unbraced_linkage_specification_p
15327 = parser->in_unbraced_linkage_specification_p;
15328 parser->in_unbraced_linkage_specification_p = false;
15329 /* Inside the function, surrounding template-parameter-lists do not
15330 apply. */
15331 saved_num_template_parameter_lists
15332 = parser->num_template_parameter_lists;
15333 parser->num_template_parameter_lists = 0;
15334 /* If the next token is `try', then we are looking at a
15335 function-try-block. */
15336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15337 ctor_initializer_p = cp_parser_function_try_block (parser);
15338 /* A function-try-block includes the function-body, so we only do
15339 this next part if we're not processing a function-try-block. */
15340 else
15341 ctor_initializer_p
15342 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15344 /* Finish the function. */
15345 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15346 (inline_p ? 2 : 0));
15347 /* Generate code for it, if necessary. */
15348 expand_or_defer_fn (fn);
15349 /* Restore the saved values. */
15350 parser->in_unbraced_linkage_specification_p
15351 = saved_in_unbraced_linkage_specification_p;
15352 parser->num_template_parameter_lists
15353 = saved_num_template_parameter_lists;
15355 return fn;
15358 /* Parse a template-declaration, assuming that the `export' (and
15359 `extern') keywords, if present, has already been scanned. MEMBER_P
15360 is as for cp_parser_template_declaration. */
15362 static void
15363 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15365 tree decl = NULL_TREE;
15366 tree parameter_list;
15367 bool friend_p = false;
15368 bool need_lang_pop;
15370 /* Look for the `template' keyword. */
15371 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15372 return;
15374 /* And the `<'. */
15375 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15376 return;
15377 /* [temp]
15379 A template ... shall not have C linkage. */
15380 if (current_lang_name == lang_name_c)
15382 error ("template with C linkage");
15383 /* Give it C++ linkage to avoid confusing other parts of the
15384 front end. */
15385 push_lang_context (lang_name_cplusplus);
15386 need_lang_pop = true;
15388 else
15389 need_lang_pop = false;
15390 /* If the next token is `>', then we have an invalid
15391 specialization. Rather than complain about an invalid template
15392 parameter, issue an error message here. */
15393 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15395 cp_parser_error (parser, "invalid explicit specialization");
15396 begin_specialization ();
15397 parameter_list = NULL_TREE;
15399 else
15400 /* Parse the template parameters. */
15401 parameter_list = cp_parser_template_parameter_list (parser);
15403 /* Look for the `>'. */
15404 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15405 /* We just processed one more parameter list. */
15406 ++parser->num_template_parameter_lists;
15407 /* If the next token is `template', there are more template
15408 parameters. */
15409 if (cp_lexer_next_token_is_keyword (parser->lexer,
15410 RID_TEMPLATE))
15411 cp_parser_template_declaration_after_export (parser, member_p);
15412 else
15414 /* There are no access checks when parsing a template, as we do not
15415 know if a specialization will be a friend. */
15416 push_deferring_access_checks (dk_no_check);
15418 decl = cp_parser_single_declaration (parser,
15419 member_p,
15420 &friend_p);
15422 pop_deferring_access_checks ();
15424 /* If this is a member template declaration, let the front
15425 end know. */
15426 if (member_p && !friend_p && decl)
15428 if (TREE_CODE (decl) == TYPE_DECL)
15429 cp_parser_check_access_in_redeclaration (decl);
15431 decl = finish_member_template_decl (decl);
15433 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15434 make_friend_class (current_class_type, TREE_TYPE (decl),
15435 /*complain=*/true);
15437 /* We are done with the current parameter list. */
15438 --parser->num_template_parameter_lists;
15440 /* Finish up. */
15441 finish_template_decl (parameter_list);
15443 /* Register member declarations. */
15444 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15445 finish_member_declaration (decl);
15446 /* For the erroneous case of a template with C linkage, we pushed an
15447 implicit C++ linkage scope; exit that scope now. */
15448 if (need_lang_pop)
15449 pop_lang_context ();
15450 /* If DECL is a function template, we must return to parse it later.
15451 (Even though there is no definition, there might be default
15452 arguments that need handling.) */
15453 if (member_p && decl
15454 && (TREE_CODE (decl) == FUNCTION_DECL
15455 || DECL_FUNCTION_TEMPLATE_P (decl)))
15456 TREE_VALUE (parser->unparsed_functions_queues)
15457 = tree_cons (NULL_TREE, decl,
15458 TREE_VALUE (parser->unparsed_functions_queues));
15461 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15462 `function-definition' sequence. MEMBER_P is true, this declaration
15463 appears in a class scope.
15465 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15466 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15468 static tree
15469 cp_parser_single_declaration (cp_parser* parser,
15470 bool member_p,
15471 bool* friend_p)
15473 int declares_class_or_enum;
15474 tree decl = NULL_TREE;
15475 cp_decl_specifier_seq decl_specifiers;
15476 bool function_definition_p = false;
15478 /* This function is only used when processing a template
15479 declaration. */
15480 gcc_assert (innermost_scope_kind () == sk_template_parms
15481 || innermost_scope_kind () == sk_template_spec);
15483 /* Defer access checks until we know what is being declared. */
15484 push_deferring_access_checks (dk_deferred);
15486 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15487 alternative. */
15488 cp_parser_decl_specifier_seq (parser,
15489 CP_PARSER_FLAGS_OPTIONAL,
15490 &decl_specifiers,
15491 &declares_class_or_enum);
15492 if (friend_p)
15493 *friend_p = cp_parser_friend_p (&decl_specifiers);
15495 /* There are no template typedefs. */
15496 if (decl_specifiers.specs[(int) ds_typedef])
15498 error ("template declaration of %qs", "typedef");
15499 decl = error_mark_node;
15502 /* Gather up the access checks that occurred the
15503 decl-specifier-seq. */
15504 stop_deferring_access_checks ();
15506 /* Check for the declaration of a template class. */
15507 if (declares_class_or_enum)
15509 if (cp_parser_declares_only_class_p (parser))
15511 decl = shadow_tag (&decl_specifiers);
15513 /* In this case:
15515 struct C {
15516 friend template <typename T> struct A<T>::B;
15519 A<T>::B will be represented by a TYPENAME_TYPE, and
15520 therefore not recognized by shadow_tag. */
15521 if (friend_p && *friend_p
15522 && !decl
15523 && decl_specifiers.type
15524 && TYPE_P (decl_specifiers.type))
15525 decl = decl_specifiers.type;
15527 if (decl && decl != error_mark_node)
15528 decl = TYPE_NAME (decl);
15529 else
15530 decl = error_mark_node;
15533 /* If it's not a template class, try for a template function. If
15534 the next token is a `;', then this declaration does not declare
15535 anything. But, if there were errors in the decl-specifiers, then
15536 the error might well have come from an attempted class-specifier.
15537 In that case, there's no need to warn about a missing declarator. */
15538 if (!decl
15539 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15540 || decl_specifiers.type != error_mark_node))
15541 decl = cp_parser_init_declarator (parser,
15542 &decl_specifiers,
15543 /*function_definition_allowed_p=*/true,
15544 member_p,
15545 declares_class_or_enum,
15546 &function_definition_p);
15548 pop_deferring_access_checks ();
15550 /* Clear any current qualification; whatever comes next is the start
15551 of something new. */
15552 parser->scope = NULL_TREE;
15553 parser->qualifying_scope = NULL_TREE;
15554 parser->object_scope = NULL_TREE;
15555 /* Look for a trailing `;' after the declaration. */
15556 if (!function_definition_p
15557 && (decl == error_mark_node
15558 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15559 cp_parser_skip_to_end_of_block_or_statement (parser);
15561 return decl;
15564 /* Parse a cast-expression that is not the operand of a unary "&". */
15566 static tree
15567 cp_parser_simple_cast_expression (cp_parser *parser)
15569 return cp_parser_cast_expression (parser, /*address_p=*/false,
15570 /*cast_p=*/false);
15573 /* Parse a functional cast to TYPE. Returns an expression
15574 representing the cast. */
15576 static tree
15577 cp_parser_functional_cast (cp_parser* parser, tree type)
15579 tree expression_list;
15580 tree cast;
15582 expression_list
15583 = cp_parser_parenthesized_expression_list (parser, false,
15584 /*cast_p=*/true,
15585 /*non_constant_p=*/NULL);
15587 cast = build_functional_cast (type, expression_list);
15588 /* [expr.const]/1: In an integral constant expression "only type
15589 conversions to integral or enumeration type can be used". */
15590 if (TREE_CODE (type) == TYPE_DECL)
15591 type = TREE_TYPE (type);
15592 if (cast != error_mark_node && !dependent_type_p (type)
15593 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15595 if (cp_parser_non_integral_constant_expression
15596 (parser, "a call to a constructor"))
15597 return error_mark_node;
15599 return cast;
15602 /* Save the tokens that make up the body of a member function defined
15603 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15604 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15605 specifiers applied to the declaration. Returns the FUNCTION_DECL
15606 for the member function. */
15608 static tree
15609 cp_parser_save_member_function_body (cp_parser* parser,
15610 cp_decl_specifier_seq *decl_specifiers,
15611 cp_declarator *declarator,
15612 tree attributes)
15614 cp_token *first;
15615 cp_token *last;
15616 tree fn;
15618 /* Create the function-declaration. */
15619 fn = start_method (decl_specifiers, declarator, attributes);
15620 /* If something went badly wrong, bail out now. */
15621 if (fn == error_mark_node)
15623 /* If there's a function-body, skip it. */
15624 if (cp_parser_token_starts_function_definition_p
15625 (cp_lexer_peek_token (parser->lexer)))
15626 cp_parser_skip_to_end_of_block_or_statement (parser);
15627 return error_mark_node;
15630 /* Remember it, if there default args to post process. */
15631 cp_parser_save_default_args (parser, fn);
15633 /* Save away the tokens that make up the body of the
15634 function. */
15635 first = parser->lexer->next_token;
15636 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15637 /* Handle function try blocks. */
15638 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15639 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15640 last = parser->lexer->next_token;
15642 /* Save away the inline definition; we will process it when the
15643 class is complete. */
15644 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15645 DECL_PENDING_INLINE_P (fn) = 1;
15647 /* We need to know that this was defined in the class, so that
15648 friend templates are handled correctly. */
15649 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15651 /* We're done with the inline definition. */
15652 finish_method (fn);
15654 /* Add FN to the queue of functions to be parsed later. */
15655 TREE_VALUE (parser->unparsed_functions_queues)
15656 = tree_cons (NULL_TREE, fn,
15657 TREE_VALUE (parser->unparsed_functions_queues));
15659 return fn;
15662 /* Parse a template-argument-list, as well as the trailing ">" (but
15663 not the opening ">"). See cp_parser_template_argument_list for the
15664 return value. */
15666 static tree
15667 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15669 tree arguments;
15670 tree saved_scope;
15671 tree saved_qualifying_scope;
15672 tree saved_object_scope;
15673 bool saved_greater_than_is_operator_p;
15674 bool saved_skip_evaluation;
15676 /* [temp.names]
15678 When parsing a template-id, the first non-nested `>' is taken as
15679 the end of the template-argument-list rather than a greater-than
15680 operator. */
15681 saved_greater_than_is_operator_p
15682 = parser->greater_than_is_operator_p;
15683 parser->greater_than_is_operator_p = false;
15684 /* Parsing the argument list may modify SCOPE, so we save it
15685 here. */
15686 saved_scope = parser->scope;
15687 saved_qualifying_scope = parser->qualifying_scope;
15688 saved_object_scope = parser->object_scope;
15689 /* We need to evaluate the template arguments, even though this
15690 template-id may be nested within a "sizeof". */
15691 saved_skip_evaluation = skip_evaluation;
15692 skip_evaluation = false;
15693 /* Parse the template-argument-list itself. */
15694 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15695 arguments = NULL_TREE;
15696 else
15697 arguments = cp_parser_template_argument_list (parser);
15698 /* Look for the `>' that ends the template-argument-list. If we find
15699 a '>>' instead, it's probably just a typo. */
15700 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15702 if (!saved_greater_than_is_operator_p)
15704 /* If we're in a nested template argument list, the '>>' has
15705 to be a typo for '> >'. We emit the error message, but we
15706 continue parsing and we push a '>' as next token, so that
15707 the argument list will be parsed correctly. Note that the
15708 global source location is still on the token before the
15709 '>>', so we need to say explicitly where we want it. */
15710 cp_token *token = cp_lexer_peek_token (parser->lexer);
15711 error ("%H%<>>%> should be %<> >%> "
15712 "within a nested template argument list",
15713 &token->location);
15715 /* ??? Proper recovery should terminate two levels of
15716 template argument list here. */
15717 token->type = CPP_GREATER;
15719 else
15721 /* If this is not a nested template argument list, the '>>'
15722 is a typo for '>'. Emit an error message and continue.
15723 Same deal about the token location, but here we can get it
15724 right by consuming the '>>' before issuing the diagnostic. */
15725 cp_lexer_consume_token (parser->lexer);
15726 error ("spurious %<>>%>, use %<>%> to terminate "
15727 "a template argument list");
15730 else
15731 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15732 /* The `>' token might be a greater-than operator again now. */
15733 parser->greater_than_is_operator_p
15734 = saved_greater_than_is_operator_p;
15735 /* Restore the SAVED_SCOPE. */
15736 parser->scope = saved_scope;
15737 parser->qualifying_scope = saved_qualifying_scope;
15738 parser->object_scope = saved_object_scope;
15739 skip_evaluation = saved_skip_evaluation;
15741 return arguments;
15744 /* MEMBER_FUNCTION is a member function, or a friend. If default
15745 arguments, or the body of the function have not yet been parsed,
15746 parse them now. */
15748 static void
15749 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15751 /* If this member is a template, get the underlying
15752 FUNCTION_DECL. */
15753 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15754 member_function = DECL_TEMPLATE_RESULT (member_function);
15756 /* There should not be any class definitions in progress at this
15757 point; the bodies of members are only parsed outside of all class
15758 definitions. */
15759 gcc_assert (parser->num_classes_being_defined == 0);
15760 /* While we're parsing the member functions we might encounter more
15761 classes. We want to handle them right away, but we don't want
15762 them getting mixed up with functions that are currently in the
15763 queue. */
15764 parser->unparsed_functions_queues
15765 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15767 /* Make sure that any template parameters are in scope. */
15768 maybe_begin_member_template_processing (member_function);
15770 /* If the body of the function has not yet been parsed, parse it
15771 now. */
15772 if (DECL_PENDING_INLINE_P (member_function))
15774 tree function_scope;
15775 cp_token_cache *tokens;
15777 /* The function is no longer pending; we are processing it. */
15778 tokens = DECL_PENDING_INLINE_INFO (member_function);
15779 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15780 DECL_PENDING_INLINE_P (member_function) = 0;
15782 /* If this is a local class, enter the scope of the containing
15783 function. */
15784 function_scope = current_function_decl;
15785 if (function_scope)
15786 push_function_context_to (function_scope);
15789 /* Push the body of the function onto the lexer stack. */
15790 cp_parser_push_lexer_for_tokens (parser, tokens);
15792 /* Let the front end know that we going to be defining this
15793 function. */
15794 start_preparsed_function (member_function, NULL_TREE,
15795 SF_PRE_PARSED | SF_INCLASS_INLINE);
15797 /* Don't do access checking if it is a templated function. */
15798 if (processing_template_decl)
15799 push_deferring_access_checks (dk_no_check);
15801 /* Now, parse the body of the function. */
15802 cp_parser_function_definition_after_declarator (parser,
15803 /*inline_p=*/true);
15805 if (processing_template_decl)
15806 pop_deferring_access_checks ();
15808 /* Leave the scope of the containing function. */
15809 if (function_scope)
15810 pop_function_context_from (function_scope);
15811 cp_parser_pop_lexer (parser);
15814 /* Remove any template parameters from the symbol table. */
15815 maybe_end_member_template_processing ();
15817 /* Restore the queue. */
15818 parser->unparsed_functions_queues
15819 = TREE_CHAIN (parser->unparsed_functions_queues);
15822 /* If DECL contains any default args, remember it on the unparsed
15823 functions queue. */
15825 static void
15826 cp_parser_save_default_args (cp_parser* parser, tree decl)
15828 tree probe;
15830 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15831 probe;
15832 probe = TREE_CHAIN (probe))
15833 if (TREE_PURPOSE (probe))
15835 TREE_PURPOSE (parser->unparsed_functions_queues)
15836 = tree_cons (current_class_type, decl,
15837 TREE_PURPOSE (parser->unparsed_functions_queues));
15838 break;
15842 /* FN is a FUNCTION_DECL which may contains a parameter with an
15843 unparsed DEFAULT_ARG. Parse the default args now. This function
15844 assumes that the current scope is the scope in which the default
15845 argument should be processed. */
15847 static void
15848 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15850 bool saved_local_variables_forbidden_p;
15851 tree parm;
15853 /* While we're parsing the default args, we might (due to the
15854 statement expression extension) encounter more classes. We want
15855 to handle them right away, but we don't want them getting mixed
15856 up with default args that are currently in the queue. */
15857 parser->unparsed_functions_queues
15858 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15860 /* Local variable names (and the `this' keyword) may not appear
15861 in a default argument. */
15862 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15863 parser->local_variables_forbidden_p = true;
15865 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15866 parm;
15867 parm = TREE_CHAIN (parm))
15869 cp_token_cache *tokens;
15870 tree default_arg = TREE_PURPOSE (parm);
15871 tree parsed_arg;
15872 VEC(tree,gc) *insts;
15873 tree copy;
15874 unsigned ix;
15876 if (!default_arg)
15877 continue;
15879 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15880 /* This can happen for a friend declaration for a function
15881 already declared with default arguments. */
15882 continue;
15884 /* Push the saved tokens for the default argument onto the parser's
15885 lexer stack. */
15886 tokens = DEFARG_TOKENS (default_arg);
15887 cp_parser_push_lexer_for_tokens (parser, tokens);
15889 /* Parse the assignment-expression. */
15890 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15892 if (!processing_template_decl)
15893 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15895 TREE_PURPOSE (parm) = parsed_arg;
15897 /* Update any instantiations we've already created. */
15898 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15899 VEC_iterate (tree, insts, ix, copy); ix++)
15900 TREE_PURPOSE (copy) = parsed_arg;
15902 /* If the token stream has not been completely used up, then
15903 there was extra junk after the end of the default
15904 argument. */
15905 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15906 cp_parser_error (parser, "expected %<,%>");
15908 /* Revert to the main lexer. */
15909 cp_parser_pop_lexer (parser);
15912 /* Make sure no default arg is missing. */
15913 check_default_args (fn);
15915 /* Restore the state of local_variables_forbidden_p. */
15916 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15918 /* Restore the queue. */
15919 parser->unparsed_functions_queues
15920 = TREE_CHAIN (parser->unparsed_functions_queues);
15923 /* Parse the operand of `sizeof' (or a similar operator). Returns
15924 either a TYPE or an expression, depending on the form of the
15925 input. The KEYWORD indicates which kind of expression we have
15926 encountered. */
15928 static tree
15929 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15931 static const char *format;
15932 tree expr = NULL_TREE;
15933 const char *saved_message;
15934 bool saved_integral_constant_expression_p;
15935 bool saved_non_integral_constant_expression_p;
15937 /* Initialize FORMAT the first time we get here. */
15938 if (!format)
15939 format = "types may not be defined in '%s' expressions";
15941 /* Types cannot be defined in a `sizeof' expression. Save away the
15942 old message. */
15943 saved_message = parser->type_definition_forbidden_message;
15944 /* And create the new one. */
15945 parser->type_definition_forbidden_message
15946 = XNEWVEC (const char, strlen (format)
15947 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15948 + 1 /* `\0' */);
15949 sprintf ((char *) parser->type_definition_forbidden_message,
15950 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15952 /* The restrictions on constant-expressions do not apply inside
15953 sizeof expressions. */
15954 saved_integral_constant_expression_p
15955 = parser->integral_constant_expression_p;
15956 saved_non_integral_constant_expression_p
15957 = parser->non_integral_constant_expression_p;
15958 parser->integral_constant_expression_p = false;
15960 /* Do not actually evaluate the expression. */
15961 ++skip_evaluation;
15962 /* If it's a `(', then we might be looking at the type-id
15963 construction. */
15964 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15966 tree type;
15967 bool saved_in_type_id_in_expr_p;
15969 /* We can't be sure yet whether we're looking at a type-id or an
15970 expression. */
15971 cp_parser_parse_tentatively (parser);
15972 /* Consume the `('. */
15973 cp_lexer_consume_token (parser->lexer);
15974 /* Parse the type-id. */
15975 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15976 parser->in_type_id_in_expr_p = true;
15977 type = cp_parser_type_id (parser);
15978 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15979 /* Now, look for the trailing `)'. */
15980 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15981 /* If all went well, then we're done. */
15982 if (cp_parser_parse_definitely (parser))
15984 cp_decl_specifier_seq decl_specs;
15986 /* Build a trivial decl-specifier-seq. */
15987 clear_decl_specs (&decl_specs);
15988 decl_specs.type = type;
15990 /* Call grokdeclarator to figure out what type this is. */
15991 expr = grokdeclarator (NULL,
15992 &decl_specs,
15993 TYPENAME,
15994 /*initialized=*/0,
15995 /*attrlist=*/NULL);
15999 /* If the type-id production did not work out, then we must be
16000 looking at the unary-expression production. */
16001 if (!expr)
16002 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16003 /*cast_p=*/false);
16004 /* Go back to evaluating expressions. */
16005 --skip_evaluation;
16007 /* Free the message we created. */
16008 free ((char *) parser->type_definition_forbidden_message);
16009 /* And restore the old one. */
16010 parser->type_definition_forbidden_message = saved_message;
16011 parser->integral_constant_expression_p
16012 = saved_integral_constant_expression_p;
16013 parser->non_integral_constant_expression_p
16014 = saved_non_integral_constant_expression_p;
16016 return expr;
16019 /* If the current declaration has no declarator, return true. */
16021 static bool
16022 cp_parser_declares_only_class_p (cp_parser *parser)
16024 /* If the next token is a `;' or a `,' then there is no
16025 declarator. */
16026 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16027 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16030 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
16032 static void
16033 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
16034 cp_storage_class storage_class)
16036 if (decl_specs->storage_class != sc_none)
16037 decl_specs->multiple_storage_classes_p = true;
16038 else
16039 decl_specs->storage_class = storage_class;
16042 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16043 is true, the type is a user-defined type; otherwise it is a
16044 built-in type specified by a keyword. */
16046 static void
16047 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16048 tree type_spec,
16049 bool user_defined_p)
16051 decl_specs->any_specifiers_p = true;
16053 /* If the user tries to redeclare bool or wchar_t (with, for
16054 example, in "typedef int wchar_t;") we remember that this is what
16055 happened. In system headers, we ignore these declarations so
16056 that G++ can work with system headers that are not C++-safe. */
16057 if (decl_specs->specs[(int) ds_typedef]
16058 && !user_defined_p
16059 && (type_spec == boolean_type_node
16060 || type_spec == wchar_type_node)
16061 && (decl_specs->type
16062 || decl_specs->specs[(int) ds_long]
16063 || decl_specs->specs[(int) ds_short]
16064 || decl_specs->specs[(int) ds_unsigned]
16065 || decl_specs->specs[(int) ds_signed]))
16067 decl_specs->redefined_builtin_type = type_spec;
16068 if (!decl_specs->type)
16070 decl_specs->type = type_spec;
16071 decl_specs->user_defined_type_p = false;
16074 else if (decl_specs->type)
16075 decl_specs->multiple_types_p = true;
16076 else
16078 decl_specs->type = type_spec;
16079 decl_specs->user_defined_type_p = user_defined_p;
16080 decl_specs->redefined_builtin_type = NULL_TREE;
16084 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16085 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16087 static bool
16088 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16090 return decl_specifiers->specs[(int) ds_friend] != 0;
16093 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16094 issue an error message indicating that TOKEN_DESC was expected.
16096 Returns the token consumed, if the token had the appropriate type.
16097 Otherwise, returns NULL. */
16099 static cp_token *
16100 cp_parser_require (cp_parser* parser,
16101 enum cpp_ttype type,
16102 const char* token_desc)
16104 if (cp_lexer_next_token_is (parser->lexer, type))
16105 return cp_lexer_consume_token (parser->lexer);
16106 else
16108 /* Output the MESSAGE -- unless we're parsing tentatively. */
16109 if (!cp_parser_simulate_error (parser))
16111 char *message = concat ("expected ", token_desc, NULL);
16112 cp_parser_error (parser, message);
16113 free (message);
16115 return NULL;
16119 /* Like cp_parser_require, except that tokens will be skipped until
16120 the desired token is found. An error message is still produced if
16121 the next token is not as expected. */
16123 static void
16124 cp_parser_skip_until_found (cp_parser* parser,
16125 enum cpp_ttype type,
16126 const char* token_desc)
16128 cp_token *token;
16129 unsigned nesting_depth = 0;
16131 if (cp_parser_require (parser, type, token_desc))
16132 return;
16134 /* Skip tokens until the desired token is found. */
16135 while (true)
16137 /* Peek at the next token. */
16138 token = cp_lexer_peek_token (parser->lexer);
16140 /* If we've reached the token we want, consume it and stop. */
16141 if (token->type == type && !nesting_depth)
16143 cp_lexer_consume_token (parser->lexer);
16144 return;
16147 switch (token->type)
16149 case CPP_EOF:
16150 case CPP_PRAGMA_EOL:
16151 /* If we've run out of tokens, stop. */
16152 return;
16154 case CPP_OPEN_BRACE:
16155 case CPP_OPEN_PAREN:
16156 case CPP_OPEN_SQUARE:
16157 ++nesting_depth;
16158 break;
16160 case CPP_CLOSE_BRACE:
16161 case CPP_CLOSE_PAREN:
16162 case CPP_CLOSE_SQUARE:
16163 if (nesting_depth-- == 0)
16164 return;
16165 break;
16167 default:
16168 break;
16171 /* Consume this token. */
16172 cp_lexer_consume_token (parser->lexer);
16176 /* If the next token is the indicated keyword, consume it. Otherwise,
16177 issue an error message indicating that TOKEN_DESC was expected.
16179 Returns the token consumed, if the token had the appropriate type.
16180 Otherwise, returns NULL. */
16182 static cp_token *
16183 cp_parser_require_keyword (cp_parser* parser,
16184 enum rid keyword,
16185 const char* token_desc)
16187 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16189 if (token && token->keyword != keyword)
16191 dyn_string_t error_msg;
16193 /* Format the error message. */
16194 error_msg = dyn_string_new (0);
16195 dyn_string_append_cstr (error_msg, "expected ");
16196 dyn_string_append_cstr (error_msg, token_desc);
16197 cp_parser_error (parser, error_msg->s);
16198 dyn_string_delete (error_msg);
16199 return NULL;
16202 return token;
16205 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16206 function-definition. */
16208 static bool
16209 cp_parser_token_starts_function_definition_p (cp_token* token)
16211 return (/* An ordinary function-body begins with an `{'. */
16212 token->type == CPP_OPEN_BRACE
16213 /* A ctor-initializer begins with a `:'. */
16214 || token->type == CPP_COLON
16215 /* A function-try-block begins with `try'. */
16216 || token->keyword == RID_TRY
16217 /* The named return value extension begins with `return'. */
16218 || token->keyword == RID_RETURN);
16221 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16222 definition. */
16224 static bool
16225 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16227 cp_token *token;
16229 token = cp_lexer_peek_token (parser->lexer);
16230 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16233 /* Returns TRUE iff the next token is the "," or ">" ending a
16234 template-argument. */
16236 static bool
16237 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16239 cp_token *token;
16241 token = cp_lexer_peek_token (parser->lexer);
16242 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16245 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16246 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16248 static bool
16249 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16250 size_t n)
16252 cp_token *token;
16254 token = cp_lexer_peek_nth_token (parser->lexer, n);
16255 if (token->type == CPP_LESS)
16256 return true;
16257 /* Check for the sequence `<::' in the original code. It would be lexed as
16258 `[:', where `[' is a digraph, and there is no whitespace before
16259 `:'. */
16260 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16262 cp_token *token2;
16263 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16264 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16265 return true;
16267 return false;
16270 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16271 or none_type otherwise. */
16273 static enum tag_types
16274 cp_parser_token_is_class_key (cp_token* token)
16276 switch (token->keyword)
16278 case RID_CLASS:
16279 return class_type;
16280 case RID_STRUCT:
16281 return record_type;
16282 case RID_UNION:
16283 return union_type;
16285 default:
16286 return none_type;
16290 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16292 static void
16293 cp_parser_check_class_key (enum tag_types class_key, tree type)
16295 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16296 pedwarn ("%qs tag used in naming %q#T",
16297 class_key == union_type ? "union"
16298 : class_key == record_type ? "struct" : "class",
16299 type);
16302 /* Issue an error message if DECL is redeclared with different
16303 access than its original declaration [class.access.spec/3].
16304 This applies to nested classes and nested class templates.
16305 [class.mem/1]. */
16307 static void
16308 cp_parser_check_access_in_redeclaration (tree decl)
16310 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16311 return;
16313 if ((TREE_PRIVATE (decl)
16314 != (current_access_specifier == access_private_node))
16315 || (TREE_PROTECTED (decl)
16316 != (current_access_specifier == access_protected_node)))
16317 error ("%qD redeclared with different access", decl);
16320 /* Look for the `template' keyword, as a syntactic disambiguator.
16321 Return TRUE iff it is present, in which case it will be
16322 consumed. */
16324 static bool
16325 cp_parser_optional_template_keyword (cp_parser *parser)
16327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16329 /* The `template' keyword can only be used within templates;
16330 outside templates the parser can always figure out what is a
16331 template and what is not. */
16332 if (!processing_template_decl)
16334 error ("%<template%> (as a disambiguator) is only allowed "
16335 "within templates");
16336 /* If this part of the token stream is rescanned, the same
16337 error message would be generated. So, we purge the token
16338 from the stream. */
16339 cp_lexer_purge_token (parser->lexer);
16340 return false;
16342 else
16344 /* Consume the `template' keyword. */
16345 cp_lexer_consume_token (parser->lexer);
16346 return true;
16350 return false;
16353 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16354 set PARSER->SCOPE, and perform other related actions. */
16356 static void
16357 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16359 tree value;
16360 tree check;
16362 /* Get the stored value. */
16363 value = cp_lexer_consume_token (parser->lexer)->value;
16364 /* Perform any access checks that were deferred. */
16365 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16366 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16367 /* Set the scope from the stored value. */
16368 parser->scope = TREE_VALUE (value);
16369 parser->qualifying_scope = TREE_TYPE (value);
16370 parser->object_scope = NULL_TREE;
16373 /* Consume tokens up through a non-nested END token. */
16375 static void
16376 cp_parser_cache_group (cp_parser *parser,
16377 enum cpp_ttype end,
16378 unsigned depth)
16380 while (true)
16382 cp_token *token;
16384 /* Abort a parenthesized expression if we encounter a brace. */
16385 if ((end == CPP_CLOSE_PAREN || depth == 0)
16386 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16387 return;
16388 /* If we've reached the end of the file, stop. */
16389 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16390 || (end != CPP_PRAGMA_EOL
16391 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16392 return;
16393 /* Consume the next token. */
16394 token = cp_lexer_consume_token (parser->lexer);
16395 /* See if it starts a new group. */
16396 if (token->type == CPP_OPEN_BRACE)
16398 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16399 if (depth == 0)
16400 return;
16402 else if (token->type == CPP_OPEN_PAREN)
16403 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16404 else if (token->type == CPP_PRAGMA)
16405 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16406 else if (token->type == end)
16407 return;
16411 /* Begin parsing tentatively. We always save tokens while parsing
16412 tentatively so that if the tentative parsing fails we can restore the
16413 tokens. */
16415 static void
16416 cp_parser_parse_tentatively (cp_parser* parser)
16418 /* Enter a new parsing context. */
16419 parser->context = cp_parser_context_new (parser->context);
16420 /* Begin saving tokens. */
16421 cp_lexer_save_tokens (parser->lexer);
16422 /* In order to avoid repetitive access control error messages,
16423 access checks are queued up until we are no longer parsing
16424 tentatively. */
16425 push_deferring_access_checks (dk_deferred);
16428 /* Commit to the currently active tentative parse. */
16430 static void
16431 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16433 cp_parser_context *context;
16434 cp_lexer *lexer;
16436 /* Mark all of the levels as committed. */
16437 lexer = parser->lexer;
16438 for (context = parser->context; context->next; context = context->next)
16440 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16441 break;
16442 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16443 while (!cp_lexer_saving_tokens (lexer))
16444 lexer = lexer->next;
16445 cp_lexer_commit_tokens (lexer);
16449 /* Abort the currently active tentative parse. All consumed tokens
16450 will be rolled back, and no diagnostics will be issued. */
16452 static void
16453 cp_parser_abort_tentative_parse (cp_parser* parser)
16455 cp_parser_simulate_error (parser);
16456 /* Now, pretend that we want to see if the construct was
16457 successfully parsed. */
16458 cp_parser_parse_definitely (parser);
16461 /* Stop parsing tentatively. If a parse error has occurred, restore the
16462 token stream. Otherwise, commit to the tokens we have consumed.
16463 Returns true if no error occurred; false otherwise. */
16465 static bool
16466 cp_parser_parse_definitely (cp_parser* parser)
16468 bool error_occurred;
16469 cp_parser_context *context;
16471 /* Remember whether or not an error occurred, since we are about to
16472 destroy that information. */
16473 error_occurred = cp_parser_error_occurred (parser);
16474 /* Remove the topmost context from the stack. */
16475 context = parser->context;
16476 parser->context = context->next;
16477 /* If no parse errors occurred, commit to the tentative parse. */
16478 if (!error_occurred)
16480 /* Commit to the tokens read tentatively, unless that was
16481 already done. */
16482 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16483 cp_lexer_commit_tokens (parser->lexer);
16485 pop_to_parent_deferring_access_checks ();
16487 /* Otherwise, if errors occurred, roll back our state so that things
16488 are just as they were before we began the tentative parse. */
16489 else
16491 cp_lexer_rollback_tokens (parser->lexer);
16492 pop_deferring_access_checks ();
16494 /* Add the context to the front of the free list. */
16495 context->next = cp_parser_context_free_list;
16496 cp_parser_context_free_list = context;
16498 return !error_occurred;
16501 /* Returns true if we are parsing tentatively and are not committed to
16502 this tentative parse. */
16504 static bool
16505 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16507 return (cp_parser_parsing_tentatively (parser)
16508 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16511 /* Returns nonzero iff an error has occurred during the most recent
16512 tentative parse. */
16514 static bool
16515 cp_parser_error_occurred (cp_parser* parser)
16517 return (cp_parser_parsing_tentatively (parser)
16518 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16521 /* Returns nonzero if GNU extensions are allowed. */
16523 static bool
16524 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16526 return parser->allow_gnu_extensions_p;
16529 /* Objective-C++ Productions */
16532 /* Parse an Objective-C expression, which feeds into a primary-expression
16533 above.
16535 objc-expression:
16536 objc-message-expression
16537 objc-string-literal
16538 objc-encode-expression
16539 objc-protocol-expression
16540 objc-selector-expression
16542 Returns a tree representation of the expression. */
16544 static tree
16545 cp_parser_objc_expression (cp_parser* parser)
16547 /* Try to figure out what kind of declaration is present. */
16548 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16550 switch (kwd->type)
16552 case CPP_OPEN_SQUARE:
16553 return cp_parser_objc_message_expression (parser);
16555 case CPP_OBJC_STRING:
16556 kwd = cp_lexer_consume_token (parser->lexer);
16557 return objc_build_string_object (kwd->value);
16559 case CPP_KEYWORD:
16560 switch (kwd->keyword)
16562 case RID_AT_ENCODE:
16563 return cp_parser_objc_encode_expression (parser);
16565 case RID_AT_PROTOCOL:
16566 return cp_parser_objc_protocol_expression (parser);
16568 case RID_AT_SELECTOR:
16569 return cp_parser_objc_selector_expression (parser);
16571 default:
16572 break;
16574 default:
16575 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16576 cp_parser_skip_to_end_of_block_or_statement (parser);
16579 return error_mark_node;
16582 /* Parse an Objective-C message expression.
16584 objc-message-expression:
16585 [ objc-message-receiver objc-message-args ]
16587 Returns a representation of an Objective-C message. */
16589 static tree
16590 cp_parser_objc_message_expression (cp_parser* parser)
16592 tree receiver, messageargs;
16594 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16595 receiver = cp_parser_objc_message_receiver (parser);
16596 messageargs = cp_parser_objc_message_args (parser);
16597 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16599 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16602 /* Parse an objc-message-receiver.
16604 objc-message-receiver:
16605 expression
16606 simple-type-specifier
16608 Returns a representation of the type or expression. */
16610 static tree
16611 cp_parser_objc_message_receiver (cp_parser* parser)
16613 tree rcv;
16615 /* An Objective-C message receiver may be either (1) a type
16616 or (2) an expression. */
16617 cp_parser_parse_tentatively (parser);
16618 rcv = cp_parser_expression (parser, false);
16620 if (cp_parser_parse_definitely (parser))
16621 return rcv;
16623 rcv = cp_parser_simple_type_specifier (parser,
16624 /*decl_specs=*/NULL,
16625 CP_PARSER_FLAGS_NONE);
16627 return objc_get_class_reference (rcv);
16630 /* Parse the arguments and selectors comprising an Objective-C message.
16632 objc-message-args:
16633 objc-selector
16634 objc-selector-args
16635 objc-selector-args , objc-comma-args
16637 objc-selector-args:
16638 objc-selector [opt] : assignment-expression
16639 objc-selector-args objc-selector [opt] : assignment-expression
16641 objc-comma-args:
16642 assignment-expression
16643 objc-comma-args , assignment-expression
16645 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16646 selector arguments and TREE_VALUE containing a list of comma
16647 arguments. */
16649 static tree
16650 cp_parser_objc_message_args (cp_parser* parser)
16652 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16653 bool maybe_unary_selector_p = true;
16654 cp_token *token = cp_lexer_peek_token (parser->lexer);
16656 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16658 tree selector = NULL_TREE, arg;
16660 if (token->type != CPP_COLON)
16661 selector = cp_parser_objc_selector (parser);
16663 /* Detect if we have a unary selector. */
16664 if (maybe_unary_selector_p
16665 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16666 return build_tree_list (selector, NULL_TREE);
16668 maybe_unary_selector_p = false;
16669 cp_parser_require (parser, CPP_COLON, "`:'");
16670 arg = cp_parser_assignment_expression (parser, false);
16672 sel_args
16673 = chainon (sel_args,
16674 build_tree_list (selector, arg));
16676 token = cp_lexer_peek_token (parser->lexer);
16679 /* Handle non-selector arguments, if any. */
16680 while (token->type == CPP_COMMA)
16682 tree arg;
16684 cp_lexer_consume_token (parser->lexer);
16685 arg = cp_parser_assignment_expression (parser, false);
16687 addl_args
16688 = chainon (addl_args,
16689 build_tree_list (NULL_TREE, arg));
16691 token = cp_lexer_peek_token (parser->lexer);
16694 return build_tree_list (sel_args, addl_args);
16697 /* Parse an Objective-C encode expression.
16699 objc-encode-expression:
16700 @encode objc-typename
16702 Returns an encoded representation of the type argument. */
16704 static tree
16705 cp_parser_objc_encode_expression (cp_parser* parser)
16707 tree type;
16709 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16710 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16711 type = complete_type (cp_parser_type_id (parser));
16712 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16714 if (!type)
16716 error ("%<@encode%> must specify a type as an argument");
16717 return error_mark_node;
16720 return objc_build_encode_expr (type);
16723 /* Parse an Objective-C @defs expression. */
16725 static tree
16726 cp_parser_objc_defs_expression (cp_parser *parser)
16728 tree name;
16730 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16731 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16732 name = cp_parser_identifier (parser);
16733 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16735 return objc_get_class_ivars (name);
16738 /* Parse an Objective-C protocol expression.
16740 objc-protocol-expression:
16741 @protocol ( identifier )
16743 Returns a representation of the protocol expression. */
16745 static tree
16746 cp_parser_objc_protocol_expression (cp_parser* parser)
16748 tree proto;
16750 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16751 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16752 proto = cp_parser_identifier (parser);
16753 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16755 return objc_build_protocol_expr (proto);
16758 /* Parse an Objective-C selector expression.
16760 objc-selector-expression:
16761 @selector ( objc-method-signature )
16763 objc-method-signature:
16764 objc-selector
16765 objc-selector-seq
16767 objc-selector-seq:
16768 objc-selector :
16769 objc-selector-seq objc-selector :
16771 Returns a representation of the method selector. */
16773 static tree
16774 cp_parser_objc_selector_expression (cp_parser* parser)
16776 tree sel_seq = NULL_TREE;
16777 bool maybe_unary_selector_p = true;
16778 cp_token *token;
16780 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16781 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16782 token = cp_lexer_peek_token (parser->lexer);
16784 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16785 || token->type == CPP_SCOPE)
16787 tree selector = NULL_TREE;
16789 if (token->type != CPP_COLON
16790 || token->type == CPP_SCOPE)
16791 selector = cp_parser_objc_selector (parser);
16793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16794 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16796 /* Detect if we have a unary selector. */
16797 if (maybe_unary_selector_p)
16799 sel_seq = selector;
16800 goto finish_selector;
16802 else
16804 cp_parser_error (parser, "expected %<:%>");
16807 maybe_unary_selector_p = false;
16808 token = cp_lexer_consume_token (parser->lexer);
16810 if (token->type == CPP_SCOPE)
16812 sel_seq
16813 = chainon (sel_seq,
16814 build_tree_list (selector, NULL_TREE));
16815 sel_seq
16816 = chainon (sel_seq,
16817 build_tree_list (NULL_TREE, NULL_TREE));
16819 else
16820 sel_seq
16821 = chainon (sel_seq,
16822 build_tree_list (selector, NULL_TREE));
16824 token = cp_lexer_peek_token (parser->lexer);
16827 finish_selector:
16828 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16830 return objc_build_selector_expr (sel_seq);
16833 /* Parse a list of identifiers.
16835 objc-identifier-list:
16836 identifier
16837 objc-identifier-list , identifier
16839 Returns a TREE_LIST of identifier nodes. */
16841 static tree
16842 cp_parser_objc_identifier_list (cp_parser* parser)
16844 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16845 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16847 while (sep->type == CPP_COMMA)
16849 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16850 list = chainon (list,
16851 build_tree_list (NULL_TREE,
16852 cp_parser_identifier (parser)));
16853 sep = cp_lexer_peek_token (parser->lexer);
16856 return list;
16859 /* Parse an Objective-C alias declaration.
16861 objc-alias-declaration:
16862 @compatibility_alias identifier identifier ;
16864 This function registers the alias mapping with the Objective-C front-end.
16865 It returns nothing. */
16867 static void
16868 cp_parser_objc_alias_declaration (cp_parser* parser)
16870 tree alias, orig;
16872 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16873 alias = cp_parser_identifier (parser);
16874 orig = cp_parser_identifier (parser);
16875 objc_declare_alias (alias, orig);
16876 cp_parser_consume_semicolon_at_end_of_statement (parser);
16879 /* Parse an Objective-C class forward-declaration.
16881 objc-class-declaration:
16882 @class objc-identifier-list ;
16884 The function registers the forward declarations with the Objective-C
16885 front-end. It returns nothing. */
16887 static void
16888 cp_parser_objc_class_declaration (cp_parser* parser)
16890 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16891 objc_declare_class (cp_parser_objc_identifier_list (parser));
16892 cp_parser_consume_semicolon_at_end_of_statement (parser);
16895 /* Parse a list of Objective-C protocol references.
16897 objc-protocol-refs-opt:
16898 objc-protocol-refs [opt]
16900 objc-protocol-refs:
16901 < objc-identifier-list >
16903 Returns a TREE_LIST of identifiers, if any. */
16905 static tree
16906 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16908 tree protorefs = NULL_TREE;
16910 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16912 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16913 protorefs = cp_parser_objc_identifier_list (parser);
16914 cp_parser_require (parser, CPP_GREATER, "`>'");
16917 return protorefs;
16920 /* Parse a Objective-C visibility specification. */
16922 static void
16923 cp_parser_objc_visibility_spec (cp_parser* parser)
16925 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16927 switch (vis->keyword)
16929 case RID_AT_PRIVATE:
16930 objc_set_visibility (2);
16931 break;
16932 case RID_AT_PROTECTED:
16933 objc_set_visibility (0);
16934 break;
16935 case RID_AT_PUBLIC:
16936 objc_set_visibility (1);
16937 break;
16938 default:
16939 return;
16942 /* Eat '@private'/'@protected'/'@public'. */
16943 cp_lexer_consume_token (parser->lexer);
16946 /* Parse an Objective-C method type. */
16948 static void
16949 cp_parser_objc_method_type (cp_parser* parser)
16951 objc_set_method_type
16952 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16953 ? PLUS_EXPR
16954 : MINUS_EXPR);
16957 /* Parse an Objective-C protocol qualifier. */
16959 static tree
16960 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16962 tree quals = NULL_TREE, node;
16963 cp_token *token = cp_lexer_peek_token (parser->lexer);
16965 node = token->value;
16967 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16968 && (node == ridpointers [(int) RID_IN]
16969 || node == ridpointers [(int) RID_OUT]
16970 || node == ridpointers [(int) RID_INOUT]
16971 || node == ridpointers [(int) RID_BYCOPY]
16972 || node == ridpointers [(int) RID_BYREF]
16973 || node == ridpointers [(int) RID_ONEWAY]))
16975 quals = tree_cons (NULL_TREE, node, quals);
16976 cp_lexer_consume_token (parser->lexer);
16977 token = cp_lexer_peek_token (parser->lexer);
16978 node = token->value;
16981 return quals;
16984 /* Parse an Objective-C typename. */
16986 static tree
16987 cp_parser_objc_typename (cp_parser* parser)
16989 tree typename = NULL_TREE;
16991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16993 tree proto_quals, cp_type = NULL_TREE;
16995 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16996 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16998 /* An ObjC type name may consist of just protocol qualifiers, in which
16999 case the type shall default to 'id'. */
17000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17001 cp_type = cp_parser_type_id (parser);
17003 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17004 typename = build_tree_list (proto_quals, cp_type);
17007 return typename;
17010 /* Check to see if TYPE refers to an Objective-C selector name. */
17012 static bool
17013 cp_parser_objc_selector_p (enum cpp_ttype type)
17015 return (type == CPP_NAME || type == CPP_KEYWORD
17016 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17017 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17018 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17019 || type == CPP_XOR || type == CPP_XOR_EQ);
17022 /* Parse an Objective-C selector. */
17024 static tree
17025 cp_parser_objc_selector (cp_parser* parser)
17027 cp_token *token = cp_lexer_consume_token (parser->lexer);
17029 if (!cp_parser_objc_selector_p (token->type))
17031 error ("invalid Objective-C++ selector name");
17032 return error_mark_node;
17035 /* C++ operator names are allowed to appear in ObjC selectors. */
17036 switch (token->type)
17038 case CPP_AND_AND: return get_identifier ("and");
17039 case CPP_AND_EQ: return get_identifier ("and_eq");
17040 case CPP_AND: return get_identifier ("bitand");
17041 case CPP_OR: return get_identifier ("bitor");
17042 case CPP_COMPL: return get_identifier ("compl");
17043 case CPP_NOT: return get_identifier ("not");
17044 case CPP_NOT_EQ: return get_identifier ("not_eq");
17045 case CPP_OR_OR: return get_identifier ("or");
17046 case CPP_OR_EQ: return get_identifier ("or_eq");
17047 case CPP_XOR: return get_identifier ("xor");
17048 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17049 default: return token->value;
17053 /* Parse an Objective-C params list. */
17055 static tree
17056 cp_parser_objc_method_keyword_params (cp_parser* parser)
17058 tree params = NULL_TREE;
17059 bool maybe_unary_selector_p = true;
17060 cp_token *token = cp_lexer_peek_token (parser->lexer);
17062 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17064 tree selector = NULL_TREE, typename, identifier;
17066 if (token->type != CPP_COLON)
17067 selector = cp_parser_objc_selector (parser);
17069 /* Detect if we have a unary selector. */
17070 if (maybe_unary_selector_p
17071 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17072 return selector;
17074 maybe_unary_selector_p = false;
17075 cp_parser_require (parser, CPP_COLON, "`:'");
17076 typename = cp_parser_objc_typename (parser);
17077 identifier = cp_parser_identifier (parser);
17079 params
17080 = chainon (params,
17081 objc_build_keyword_decl (selector,
17082 typename,
17083 identifier));
17085 token = cp_lexer_peek_token (parser->lexer);
17088 return params;
17091 /* Parse the non-keyword Objective-C params. */
17093 static tree
17094 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17096 tree params = make_node (TREE_LIST);
17097 cp_token *token = cp_lexer_peek_token (parser->lexer);
17098 *ellipsisp = false; /* Initially, assume no ellipsis. */
17100 while (token->type == CPP_COMMA)
17102 cp_parameter_declarator *parmdecl;
17103 tree parm;
17105 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17106 token = cp_lexer_peek_token (parser->lexer);
17108 if (token->type == CPP_ELLIPSIS)
17110 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17111 *ellipsisp = true;
17112 break;
17115 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17116 parm = grokdeclarator (parmdecl->declarator,
17117 &parmdecl->decl_specifiers,
17118 PARM, /*initialized=*/0,
17119 /*attrlist=*/NULL);
17121 chainon (params, build_tree_list (NULL_TREE, parm));
17122 token = cp_lexer_peek_token (parser->lexer);
17125 return params;
17128 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17130 static void
17131 cp_parser_objc_interstitial_code (cp_parser* parser)
17133 cp_token *token = cp_lexer_peek_token (parser->lexer);
17135 /* If the next token is `extern' and the following token is a string
17136 literal, then we have a linkage specification. */
17137 if (token->keyword == RID_EXTERN
17138 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17139 cp_parser_linkage_specification (parser);
17140 /* Handle #pragma, if any. */
17141 else if (token->type == CPP_PRAGMA)
17142 cp_parser_pragma (parser, pragma_external);
17143 /* Allow stray semicolons. */
17144 else if (token->type == CPP_SEMICOLON)
17145 cp_lexer_consume_token (parser->lexer);
17146 /* Finally, try to parse a block-declaration, or a function-definition. */
17147 else
17148 cp_parser_block_declaration (parser, /*statement_p=*/false);
17151 /* Parse a method signature. */
17153 static tree
17154 cp_parser_objc_method_signature (cp_parser* parser)
17156 tree rettype, kwdparms, optparms;
17157 bool ellipsis = false;
17159 cp_parser_objc_method_type (parser);
17160 rettype = cp_parser_objc_typename (parser);
17161 kwdparms = cp_parser_objc_method_keyword_params (parser);
17162 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17164 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17167 /* Pars an Objective-C method prototype list. */
17169 static void
17170 cp_parser_objc_method_prototype_list (cp_parser* parser)
17172 cp_token *token = cp_lexer_peek_token (parser->lexer);
17174 while (token->keyword != RID_AT_END)
17176 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17178 objc_add_method_declaration
17179 (cp_parser_objc_method_signature (parser));
17180 cp_parser_consume_semicolon_at_end_of_statement (parser);
17182 else
17183 /* Allow for interspersed non-ObjC++ code. */
17184 cp_parser_objc_interstitial_code (parser);
17186 token = cp_lexer_peek_token (parser->lexer);
17189 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17190 objc_finish_interface ();
17193 /* Parse an Objective-C method definition list. */
17195 static void
17196 cp_parser_objc_method_definition_list (cp_parser* parser)
17198 cp_token *token = cp_lexer_peek_token (parser->lexer);
17200 while (token->keyword != RID_AT_END)
17202 tree meth;
17204 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17206 push_deferring_access_checks (dk_deferred);
17207 objc_start_method_definition
17208 (cp_parser_objc_method_signature (parser));
17210 /* For historical reasons, we accept an optional semicolon. */
17211 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17212 cp_lexer_consume_token (parser->lexer);
17214 perform_deferred_access_checks ();
17215 stop_deferring_access_checks ();
17216 meth = cp_parser_function_definition_after_declarator (parser,
17217 false);
17218 pop_deferring_access_checks ();
17219 objc_finish_method_definition (meth);
17221 else
17222 /* Allow for interspersed non-ObjC++ code. */
17223 cp_parser_objc_interstitial_code (parser);
17225 token = cp_lexer_peek_token (parser->lexer);
17228 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17229 objc_finish_implementation ();
17232 /* Parse Objective-C ivars. */
17234 static void
17235 cp_parser_objc_class_ivars (cp_parser* parser)
17237 cp_token *token = cp_lexer_peek_token (parser->lexer);
17239 if (token->type != CPP_OPEN_BRACE)
17240 return; /* No ivars specified. */
17242 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17243 token = cp_lexer_peek_token (parser->lexer);
17245 while (token->type != CPP_CLOSE_BRACE)
17247 cp_decl_specifier_seq declspecs;
17248 int decl_class_or_enum_p;
17249 tree prefix_attributes;
17251 cp_parser_objc_visibility_spec (parser);
17253 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17254 break;
17256 cp_parser_decl_specifier_seq (parser,
17257 CP_PARSER_FLAGS_OPTIONAL,
17258 &declspecs,
17259 &decl_class_or_enum_p);
17260 prefix_attributes = declspecs.attributes;
17261 declspecs.attributes = NULL_TREE;
17263 /* Keep going until we hit the `;' at the end of the
17264 declaration. */
17265 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17267 tree width = NULL_TREE, attributes, first_attribute, decl;
17268 cp_declarator *declarator = NULL;
17269 int ctor_dtor_or_conv_p;
17271 /* Check for a (possibly unnamed) bitfield declaration. */
17272 token = cp_lexer_peek_token (parser->lexer);
17273 if (token->type == CPP_COLON)
17274 goto eat_colon;
17276 if (token->type == CPP_NAME
17277 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17278 == CPP_COLON))
17280 /* Get the name of the bitfield. */
17281 declarator = make_id_declarator (NULL_TREE,
17282 cp_parser_identifier (parser),
17283 sfk_none);
17285 eat_colon:
17286 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17287 /* Get the width of the bitfield. */
17288 width
17289 = cp_parser_constant_expression (parser,
17290 /*allow_non_constant=*/false,
17291 NULL);
17293 else
17295 /* Parse the declarator. */
17296 declarator
17297 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17298 &ctor_dtor_or_conv_p,
17299 /*parenthesized_p=*/NULL,
17300 /*member_p=*/false);
17303 /* Look for attributes that apply to the ivar. */
17304 attributes = cp_parser_attributes_opt (parser);
17305 /* Remember which attributes are prefix attributes and
17306 which are not. */
17307 first_attribute = attributes;
17308 /* Combine the attributes. */
17309 attributes = chainon (prefix_attributes, attributes);
17311 if (width)
17313 /* Create the bitfield declaration. */
17314 decl = grokbitfield (declarator, &declspecs, width);
17315 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17317 else
17318 decl = grokfield (declarator, &declspecs, NULL_TREE,
17319 NULL_TREE, attributes);
17321 /* Add the instance variable. */
17322 objc_add_instance_variable (decl);
17324 /* Reset PREFIX_ATTRIBUTES. */
17325 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17326 attributes = TREE_CHAIN (attributes);
17327 if (attributes)
17328 TREE_CHAIN (attributes) = NULL_TREE;
17330 token = cp_lexer_peek_token (parser->lexer);
17332 if (token->type == CPP_COMMA)
17334 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17335 continue;
17337 break;
17340 cp_parser_consume_semicolon_at_end_of_statement (parser);
17341 token = cp_lexer_peek_token (parser->lexer);
17344 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17345 /* For historical reasons, we accept an optional semicolon. */
17346 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17347 cp_lexer_consume_token (parser->lexer);
17350 /* Parse an Objective-C protocol declaration. */
17352 static void
17353 cp_parser_objc_protocol_declaration (cp_parser* parser)
17355 tree proto, protorefs;
17356 cp_token *tok;
17358 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17361 error ("identifier expected after %<@protocol%>");
17362 goto finish;
17365 /* See if we have a forward declaration or a definition. */
17366 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17368 /* Try a forward declaration first. */
17369 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17371 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17372 finish:
17373 cp_parser_consume_semicolon_at_end_of_statement (parser);
17376 /* Ok, we got a full-fledged definition (or at least should). */
17377 else
17379 proto = cp_parser_identifier (parser);
17380 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17381 objc_start_protocol (proto, protorefs);
17382 cp_parser_objc_method_prototype_list (parser);
17386 /* Parse an Objective-C superclass or category. */
17388 static void
17389 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17390 tree *categ)
17392 cp_token *next = cp_lexer_peek_token (parser->lexer);
17394 *super = *categ = NULL_TREE;
17395 if (next->type == CPP_COLON)
17397 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17398 *super = cp_parser_identifier (parser);
17400 else if (next->type == CPP_OPEN_PAREN)
17402 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17403 *categ = cp_parser_identifier (parser);
17404 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17408 /* Parse an Objective-C class interface. */
17410 static void
17411 cp_parser_objc_class_interface (cp_parser* parser)
17413 tree name, super, categ, protos;
17415 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17416 name = cp_parser_identifier (parser);
17417 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17418 protos = cp_parser_objc_protocol_refs_opt (parser);
17420 /* We have either a class or a category on our hands. */
17421 if (categ)
17422 objc_start_category_interface (name, categ, protos);
17423 else
17425 objc_start_class_interface (name, super, protos);
17426 /* Handle instance variable declarations, if any. */
17427 cp_parser_objc_class_ivars (parser);
17428 objc_continue_interface ();
17431 cp_parser_objc_method_prototype_list (parser);
17434 /* Parse an Objective-C class implementation. */
17436 static void
17437 cp_parser_objc_class_implementation (cp_parser* parser)
17439 tree name, super, categ;
17441 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17442 name = cp_parser_identifier (parser);
17443 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17445 /* We have either a class or a category on our hands. */
17446 if (categ)
17447 objc_start_category_implementation (name, categ);
17448 else
17450 objc_start_class_implementation (name, super);
17451 /* Handle instance variable declarations, if any. */
17452 cp_parser_objc_class_ivars (parser);
17453 objc_continue_implementation ();
17456 cp_parser_objc_method_definition_list (parser);
17459 /* Consume the @end token and finish off the implementation. */
17461 static void
17462 cp_parser_objc_end_implementation (cp_parser* parser)
17464 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17465 objc_finish_implementation ();
17468 /* Parse an Objective-C declaration. */
17470 static void
17471 cp_parser_objc_declaration (cp_parser* parser)
17473 /* Try to figure out what kind of declaration is present. */
17474 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17476 switch (kwd->keyword)
17478 case RID_AT_ALIAS:
17479 cp_parser_objc_alias_declaration (parser);
17480 break;
17481 case RID_AT_CLASS:
17482 cp_parser_objc_class_declaration (parser);
17483 break;
17484 case RID_AT_PROTOCOL:
17485 cp_parser_objc_protocol_declaration (parser);
17486 break;
17487 case RID_AT_INTERFACE:
17488 cp_parser_objc_class_interface (parser);
17489 break;
17490 case RID_AT_IMPLEMENTATION:
17491 cp_parser_objc_class_implementation (parser);
17492 break;
17493 case RID_AT_END:
17494 cp_parser_objc_end_implementation (parser);
17495 break;
17496 default:
17497 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17498 cp_parser_skip_to_end_of_block_or_statement (parser);
17502 /* Parse an Objective-C try-catch-finally statement.
17504 objc-try-catch-finally-stmt:
17505 @try compound-statement objc-catch-clause-seq [opt]
17506 objc-finally-clause [opt]
17508 objc-catch-clause-seq:
17509 objc-catch-clause objc-catch-clause-seq [opt]
17511 objc-catch-clause:
17512 @catch ( exception-declaration ) compound-statement
17514 objc-finally-clause
17515 @finally compound-statement
17517 Returns NULL_TREE. */
17519 static tree
17520 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17521 location_t location;
17522 tree stmt;
17524 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17525 location = cp_lexer_peek_token (parser->lexer)->location;
17526 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17527 node, lest it get absorbed into the surrounding block. */
17528 stmt = push_stmt_list ();
17529 cp_parser_compound_statement (parser, NULL, false);
17530 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17532 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17534 cp_parameter_declarator *parmdecl;
17535 tree parm;
17537 cp_lexer_consume_token (parser->lexer);
17538 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17539 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17540 parm = grokdeclarator (parmdecl->declarator,
17541 &parmdecl->decl_specifiers,
17542 PARM, /*initialized=*/0,
17543 /*attrlist=*/NULL);
17544 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17545 objc_begin_catch_clause (parm);
17546 cp_parser_compound_statement (parser, NULL, false);
17547 objc_finish_catch_clause ();
17550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17552 cp_lexer_consume_token (parser->lexer);
17553 location = cp_lexer_peek_token (parser->lexer)->location;
17554 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17555 node, lest it get absorbed into the surrounding block. */
17556 stmt = push_stmt_list ();
17557 cp_parser_compound_statement (parser, NULL, false);
17558 objc_build_finally_clause (location, pop_stmt_list (stmt));
17561 return objc_finish_try_stmt ();
17564 /* Parse an Objective-C synchronized statement.
17566 objc-synchronized-stmt:
17567 @synchronized ( expression ) compound-statement
17569 Returns NULL_TREE. */
17571 static tree
17572 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17573 location_t location;
17574 tree lock, stmt;
17576 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17578 location = cp_lexer_peek_token (parser->lexer)->location;
17579 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17580 lock = cp_parser_expression (parser, false);
17581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17583 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17584 node, lest it get absorbed into the surrounding block. */
17585 stmt = push_stmt_list ();
17586 cp_parser_compound_statement (parser, NULL, false);
17588 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17591 /* Parse an Objective-C throw statement.
17593 objc-throw-stmt:
17594 @throw assignment-expression [opt] ;
17596 Returns a constructed '@throw' statement. */
17598 static tree
17599 cp_parser_objc_throw_statement (cp_parser *parser) {
17600 tree expr = NULL_TREE;
17602 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17605 expr = cp_parser_assignment_expression (parser, false);
17607 cp_parser_consume_semicolon_at_end_of_statement (parser);
17609 return objc_build_throw_stmt (expr);
17612 /* Parse an Objective-C statement. */
17614 static tree
17615 cp_parser_objc_statement (cp_parser * parser) {
17616 /* Try to figure out what kind of declaration is present. */
17617 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17619 switch (kwd->keyword)
17621 case RID_AT_TRY:
17622 return cp_parser_objc_try_catch_finally_statement (parser);
17623 case RID_AT_SYNCHRONIZED:
17624 return cp_parser_objc_synchronized_statement (parser);
17625 case RID_AT_THROW:
17626 return cp_parser_objc_throw_statement (parser);
17627 default:
17628 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17629 cp_parser_skip_to_end_of_block_or_statement (parser);
17632 return error_mark_node;
17635 /* OpenMP 2.5 parsing routines. */
17637 /* All OpenMP clauses. OpenMP 2.5. */
17638 typedef enum pragma_omp_clause {
17639 PRAGMA_OMP_CLAUSE_NONE = 0,
17641 PRAGMA_OMP_CLAUSE_COPYIN,
17642 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17643 PRAGMA_OMP_CLAUSE_DEFAULT,
17644 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17645 PRAGMA_OMP_CLAUSE_IF,
17646 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17647 PRAGMA_OMP_CLAUSE_NOWAIT,
17648 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17649 PRAGMA_OMP_CLAUSE_ORDERED,
17650 PRAGMA_OMP_CLAUSE_PRIVATE,
17651 PRAGMA_OMP_CLAUSE_REDUCTION,
17652 PRAGMA_OMP_CLAUSE_SCHEDULE,
17653 PRAGMA_OMP_CLAUSE_SHARED
17654 } pragma_omp_clause;
17656 /* Returns name of the next clause.
17657 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17658 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17659 returned and the token is consumed. */
17661 static pragma_omp_clause
17662 cp_parser_omp_clause_name (cp_parser *parser)
17664 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17667 result = PRAGMA_OMP_CLAUSE_IF;
17668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17669 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17670 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17671 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17672 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17674 tree id = cp_lexer_peek_token (parser->lexer)->value;
17675 const char *p = IDENTIFIER_POINTER (id);
17677 switch (p[0])
17679 case 'c':
17680 if (!strcmp ("copyin", p))
17681 result = PRAGMA_OMP_CLAUSE_COPYIN;
17682 else if (!strcmp ("copyprivate", p))
17683 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17684 break;
17685 case 'f':
17686 if (!strcmp ("firstprivate", p))
17687 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17688 break;
17689 case 'l':
17690 if (!strcmp ("lastprivate", p))
17691 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17692 break;
17693 case 'n':
17694 if (!strcmp ("nowait", p))
17695 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17696 else if (!strcmp ("num_threads", p))
17697 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17698 break;
17699 case 'o':
17700 if (!strcmp ("ordered", p))
17701 result = PRAGMA_OMP_CLAUSE_ORDERED;
17702 break;
17703 case 'r':
17704 if (!strcmp ("reduction", p))
17705 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17706 break;
17707 case 's':
17708 if (!strcmp ("schedule", p))
17709 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17710 else if (!strcmp ("shared", p))
17711 result = PRAGMA_OMP_CLAUSE_SHARED;
17712 break;
17716 if (result != PRAGMA_OMP_CLAUSE_NONE)
17717 cp_lexer_consume_token (parser->lexer);
17719 return result;
17722 /* Validate that a clause of the given type does not already exist. */
17724 static void
17725 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17727 tree c;
17729 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17730 if (OMP_CLAUSE_CODE (c) == code)
17732 error ("too many %qs clauses", name);
17733 break;
17737 /* OpenMP 2.5:
17738 variable-list:
17739 identifier
17740 variable-list , identifier
17742 In addition, we match a closing parenthesis. An opening parenthesis
17743 will have been consumed by the caller.
17745 If KIND is nonzero, create the appropriate node and install the decl
17746 in OMP_CLAUSE_DECL and add the node to the head of the list.
17748 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17749 return the list created. */
17751 static tree
17752 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17753 tree list)
17755 while (1)
17757 tree name, decl;
17759 name = cp_parser_id_expression (parser, /*template_p=*/false,
17760 /*check_dependency_p=*/true,
17761 /*template_p=*/NULL,
17762 /*declarator_p=*/false);
17763 if (name == error_mark_node)
17764 goto skip_comma;
17766 decl = cp_parser_lookup_name_simple (parser, name);
17767 if (decl == error_mark_node)
17768 cp_parser_name_lookup_error (parser, name, decl, NULL);
17769 else if (kind != 0)
17771 tree u = build_omp_clause (kind);
17772 OMP_CLAUSE_DECL (u) = decl;
17773 OMP_CLAUSE_CHAIN (u) = list;
17774 list = u;
17776 else
17777 list = tree_cons (decl, NULL_TREE, list);
17779 get_comma:
17780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17781 break;
17782 cp_lexer_consume_token (parser->lexer);
17785 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17787 int ending;
17789 /* Try to resync to an unnested comma. Copied from
17790 cp_parser_parenthesized_expression_list. */
17791 skip_comma:
17792 ending = cp_parser_skip_to_closing_parenthesis (parser,
17793 /*recovering=*/true,
17794 /*or_comma=*/true,
17795 /*consume_paren=*/true);
17796 if (ending < 0)
17797 goto get_comma;
17800 return list;
17803 /* Similarly, but expect leading and trailing parenthesis. This is a very
17804 common case for omp clauses. */
17806 static tree
17807 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17809 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17810 return cp_parser_omp_var_list_no_open (parser, kind, list);
17811 return list;
17814 /* OpenMP 2.5:
17815 default ( shared | none ) */
17817 static tree
17818 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17820 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17821 tree c;
17823 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17824 return list;
17825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17827 tree id = cp_lexer_peek_token (parser->lexer)->value;
17828 const char *p = IDENTIFIER_POINTER (id);
17830 switch (p[0])
17832 case 'n':
17833 if (strcmp ("none", p) != 0)
17834 goto invalid_kind;
17835 kind = OMP_CLAUSE_DEFAULT_NONE;
17836 break;
17838 case 's':
17839 if (strcmp ("shared", p) != 0)
17840 goto invalid_kind;
17841 kind = OMP_CLAUSE_DEFAULT_SHARED;
17842 break;
17844 default:
17845 goto invalid_kind;
17848 cp_lexer_consume_token (parser->lexer);
17850 else
17852 invalid_kind:
17853 cp_parser_error (parser, "expected %<none%> or %<shared%>");
17856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17858 /*or_comma=*/false,
17859 /*consume_paren=*/true);
17861 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17862 return list;
17864 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
17865 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
17866 OMP_CLAUSE_CHAIN (c) = list;
17867 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
17869 return c;
17872 /* OpenMP 2.5:
17873 if ( expression ) */
17875 static tree
17876 cp_parser_omp_clause_if (cp_parser *parser, tree list)
17878 tree t, c;
17880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17881 return list;
17883 t = cp_parser_condition (parser);
17885 if (t == error_mark_node
17886 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17888 /*or_comma=*/false,
17889 /*consume_paren=*/true);
17891 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
17893 c = build_omp_clause (OMP_CLAUSE_IF);
17894 OMP_CLAUSE_IF_EXPR (c) = t;
17895 OMP_CLAUSE_CHAIN (c) = list;
17897 return c;
17900 /* OpenMP 2.5:
17901 nowait */
17903 static tree
17904 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17906 tree c;
17908 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
17910 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
17911 OMP_CLAUSE_CHAIN (c) = list;
17912 return c;
17915 /* OpenMP 2.5:
17916 num_threads ( expression ) */
17918 static tree
17919 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
17921 tree t, c;
17923 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17924 return list;
17926 t = cp_parser_expression (parser, false);
17928 if (t == error_mark_node
17929 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17931 /*or_comma=*/false,
17932 /*consume_paren=*/true);
17934 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
17936 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
17937 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
17938 OMP_CLAUSE_CHAIN (c) = list;
17940 return c;
17943 /* OpenMP 2.5:
17944 ordered */
17946 static tree
17947 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17949 tree c;
17951 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
17953 c = build_omp_clause (OMP_CLAUSE_ORDERED);
17954 OMP_CLAUSE_CHAIN (c) = list;
17955 return c;
17958 /* OpenMP 2.5:
17959 reduction ( reduction-operator : variable-list )
17961 reduction-operator:
17962 One of: + * - & ^ | && || */
17964 static tree
17965 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
17967 enum tree_code code;
17968 tree nlist, c;
17970 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17971 return list;
17973 switch (cp_lexer_peek_token (parser->lexer)->type)
17975 case CPP_PLUS:
17976 code = PLUS_EXPR;
17977 break;
17978 case CPP_MULT:
17979 code = MULT_EXPR;
17980 break;
17981 case CPP_MINUS:
17982 code = MINUS_EXPR;
17983 break;
17984 case CPP_AND:
17985 code = BIT_AND_EXPR;
17986 break;
17987 case CPP_XOR:
17988 code = BIT_XOR_EXPR;
17989 break;
17990 case CPP_OR:
17991 code = BIT_IOR_EXPR;
17992 break;
17993 case CPP_AND_AND:
17994 code = TRUTH_ANDIF_EXPR;
17995 break;
17996 case CPP_OR_OR:
17997 code = TRUTH_ORIF_EXPR;
17998 break;
17999 default:
18000 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18001 resync_fail:
18002 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18003 /*or_comma=*/false,
18004 /*consume_paren=*/true);
18005 return list;
18007 cp_lexer_consume_token (parser->lexer);
18009 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18010 goto resync_fail;
18012 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18013 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18014 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18016 return nlist;
18019 /* OpenMP 2.5:
18020 schedule ( schedule-kind )
18021 schedule ( schedule-kind , expression )
18023 schedule-kind:
18024 static | dynamic | guided | runtime
18027 static tree
18028 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18030 tree c, t;
18032 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18033 return list;
18035 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18039 tree id = cp_lexer_peek_token (parser->lexer)->value;
18040 const char *p = IDENTIFIER_POINTER (id);
18042 switch (p[0])
18044 case 'd':
18045 if (strcmp ("dynamic", p) != 0)
18046 goto invalid_kind;
18047 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18048 break;
18050 case 'g':
18051 if (strcmp ("guided", p) != 0)
18052 goto invalid_kind;
18053 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18054 break;
18056 case 'r':
18057 if (strcmp ("runtime", p) != 0)
18058 goto invalid_kind;
18059 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18060 break;
18062 default:
18063 goto invalid_kind;
18066 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18067 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18068 else
18069 goto invalid_kind;
18070 cp_lexer_consume_token (parser->lexer);
18072 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18074 cp_lexer_consume_token (parser->lexer);
18076 t = cp_parser_assignment_expression (parser, false);
18078 if (t == error_mark_node)
18079 goto resync_fail;
18080 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18081 error ("schedule %<runtime%> does not take "
18082 "a %<chunk_size%> parameter");
18083 else
18084 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18086 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18087 goto resync_fail;
18089 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18090 goto resync_fail;
18092 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18093 OMP_CLAUSE_CHAIN (c) = list;
18094 return c;
18096 invalid_kind:
18097 cp_parser_error (parser, "invalid schedule kind");
18098 resync_fail:
18099 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18100 /*or_comma=*/false,
18101 /*consume_paren=*/true);
18102 return list;
18105 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18106 is a bitmask in MASK. Return the list of clauses found; the result
18107 of clause default goes in *pdefault. */
18109 static tree
18110 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18111 const char *where, cp_token *pragma_tok)
18113 tree clauses = NULL;
18115 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18117 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18118 const char *c_name;
18119 tree prev = clauses;
18121 switch (c_kind)
18123 case PRAGMA_OMP_CLAUSE_COPYIN:
18124 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18125 c_name = "copyin";
18126 break;
18127 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18128 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18129 clauses);
18130 c_name = "copyprivate";
18131 break;
18132 case PRAGMA_OMP_CLAUSE_DEFAULT:
18133 clauses = cp_parser_omp_clause_default (parser, clauses);
18134 c_name = "default";
18135 break;
18136 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18137 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18138 clauses);
18139 c_name = "firstprivate";
18140 break;
18141 case PRAGMA_OMP_CLAUSE_IF:
18142 clauses = cp_parser_omp_clause_if (parser, clauses);
18143 c_name = "if";
18144 break;
18145 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18146 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18147 clauses);
18148 c_name = "lastprivate";
18149 break;
18150 case PRAGMA_OMP_CLAUSE_NOWAIT:
18151 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18152 c_name = "nowait";
18153 break;
18154 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18155 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18156 c_name = "num_threads";
18157 break;
18158 case PRAGMA_OMP_CLAUSE_ORDERED:
18159 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18160 c_name = "ordered";
18161 break;
18162 case PRAGMA_OMP_CLAUSE_PRIVATE:
18163 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18164 clauses);
18165 c_name = "private";
18166 break;
18167 case PRAGMA_OMP_CLAUSE_REDUCTION:
18168 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18169 c_name = "reduction";
18170 break;
18171 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18172 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18173 c_name = "schedule";
18174 break;
18175 case PRAGMA_OMP_CLAUSE_SHARED:
18176 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18177 clauses);
18178 c_name = "shared";
18179 break;
18180 default:
18181 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18182 goto saw_error;
18185 if (((mask >> c_kind) & 1) == 0)
18187 /* Remove the invalid clause(s) from the list to avoid
18188 confusing the rest of the compiler. */
18189 clauses = prev;
18190 error ("%qs is not valid for %qs", c_name, where);
18193 saw_error:
18194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18195 return finish_omp_clauses (clauses);
18198 /* OpenMP 2.5:
18199 structured-block:
18200 statement
18202 In practice, we're also interested in adding the statement to an
18203 outer node. So it is convenient if we work around the fact that
18204 cp_parser_statement calls add_stmt. */
18206 static unsigned
18207 cp_parser_begin_omp_structured_block (cp_parser *parser)
18209 unsigned save = parser->in_statement;
18211 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18212 This preserves the "not within loop or switch" style error messages
18213 for nonsense cases like
18214 void foo() {
18215 #pragma omp single
18216 break;
18219 if (parser->in_statement)
18220 parser->in_statement = IN_OMP_BLOCK;
18222 return save;
18225 static void
18226 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18228 parser->in_statement = save;
18231 static tree
18232 cp_parser_omp_structured_block (cp_parser *parser)
18234 tree stmt = begin_omp_structured_block ();
18235 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18237 cp_parser_statement (parser, NULL_TREE, false);
18239 cp_parser_end_omp_structured_block (parser, save);
18240 return finish_omp_structured_block (stmt);
18243 /* OpenMP 2.5:
18244 # pragma omp atomic new-line
18245 expression-stmt
18247 expression-stmt:
18248 x binop= expr | x++ | ++x | x-- | --x
18249 binop:
18250 +, *, -, /, &, ^, |, <<, >>
18252 where x is an lvalue expression with scalar type. */
18254 static void
18255 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18257 tree lhs, rhs;
18258 enum tree_code code;
18260 cp_parser_require_pragma_eol (parser, pragma_tok);
18262 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18263 /*cast_p=*/false);
18264 switch (TREE_CODE (lhs))
18266 case ERROR_MARK:
18267 goto saw_error;
18269 case PREINCREMENT_EXPR:
18270 case POSTINCREMENT_EXPR:
18271 lhs = TREE_OPERAND (lhs, 0);
18272 code = PLUS_EXPR;
18273 rhs = integer_one_node;
18274 break;
18276 case PREDECREMENT_EXPR:
18277 case POSTDECREMENT_EXPR:
18278 lhs = TREE_OPERAND (lhs, 0);
18279 code = MINUS_EXPR;
18280 rhs = integer_one_node;
18281 break;
18283 default:
18284 switch (cp_lexer_peek_token (parser->lexer)->type)
18286 case CPP_MULT_EQ:
18287 code = MULT_EXPR;
18288 break;
18289 case CPP_DIV_EQ:
18290 code = TRUNC_DIV_EXPR;
18291 break;
18292 case CPP_PLUS_EQ:
18293 code = PLUS_EXPR;
18294 break;
18295 case CPP_MINUS_EQ:
18296 code = MINUS_EXPR;
18297 break;
18298 case CPP_LSHIFT_EQ:
18299 code = LSHIFT_EXPR;
18300 break;
18301 case CPP_RSHIFT_EQ:
18302 code = RSHIFT_EXPR;
18303 break;
18304 case CPP_AND_EQ:
18305 code = BIT_AND_EXPR;
18306 break;
18307 case CPP_OR_EQ:
18308 code = BIT_IOR_EXPR;
18309 break;
18310 case CPP_XOR_EQ:
18311 code = BIT_XOR_EXPR;
18312 break;
18313 default:
18314 cp_parser_error (parser,
18315 "invalid operator for %<#pragma omp atomic%>");
18316 goto saw_error;
18318 cp_lexer_consume_token (parser->lexer);
18320 rhs = cp_parser_expression (parser, false);
18321 if (rhs == error_mark_node)
18322 goto saw_error;
18323 break;
18325 finish_omp_atomic (code, lhs, rhs);
18326 cp_parser_consume_semicolon_at_end_of_statement (parser);
18327 return;
18329 saw_error:
18330 cp_parser_skip_to_end_of_block_or_statement (parser);
18334 /* OpenMP 2.5:
18335 # pragma omp barrier new-line
18338 static void
18339 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18341 cp_parser_require_pragma_eol (parser, pragma_tok);
18342 finish_omp_barrier ();
18345 /* OpenMP 2.5:
18346 # pragma omp critical [(name)] new-line
18347 structured-block
18350 static tree
18351 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18353 tree stmt, name = NULL;
18355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18357 cp_lexer_consume_token (parser->lexer);
18359 name = cp_parser_identifier (parser);
18361 if (name == error_mark_node
18362 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18364 /*or_comma=*/false,
18365 /*consume_paren=*/true);
18366 if (name == error_mark_node)
18367 name = NULL;
18369 cp_parser_require_pragma_eol (parser, pragma_tok);
18371 stmt = cp_parser_omp_structured_block (parser);
18372 return c_finish_omp_critical (stmt, name);
18375 /* OpenMP 2.5:
18376 # pragma omp flush flush-vars[opt] new-line
18378 flush-vars:
18379 ( variable-list ) */
18381 static void
18382 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18385 (void) cp_parser_omp_var_list (parser, 0, NULL);
18386 cp_parser_require_pragma_eol (parser, pragma_tok);
18388 finish_omp_flush ();
18391 /* Parse the restricted form of the for statment allowed by OpenMP. */
18393 static tree
18394 cp_parser_omp_for_loop (cp_parser *parser)
18396 tree init, cond, incr, body, decl, pre_body;
18397 location_t loc;
18399 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18401 cp_parser_error (parser, "for statement expected");
18402 return NULL;
18404 loc = cp_lexer_consume_token (parser->lexer)->location;
18405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18406 return NULL;
18408 init = decl = NULL;
18409 pre_body = push_stmt_list ();
18410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18412 cp_decl_specifier_seq type_specifiers;
18414 /* First, try to parse as an initialized declaration. See
18415 cp_parser_condition, from whence the bulk of this is copied. */
18417 cp_parser_parse_tentatively (parser);
18418 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18419 &type_specifiers);
18420 if (!cp_parser_error_occurred (parser))
18422 tree asm_specification, attributes;
18423 cp_declarator *declarator;
18425 declarator = cp_parser_declarator (parser,
18426 CP_PARSER_DECLARATOR_NAMED,
18427 /*ctor_dtor_or_conv_p=*/NULL,
18428 /*parenthesized_p=*/NULL,
18429 /*member_p=*/false);
18430 attributes = cp_parser_attributes_opt (parser);
18431 asm_specification = cp_parser_asm_specification_opt (parser);
18433 cp_parser_require (parser, CPP_EQ, "`='");
18434 if (cp_parser_parse_definitely (parser))
18436 tree pushed_scope;
18438 decl = start_decl (declarator, &type_specifiers,
18439 /*initialized_p=*/false, attributes,
18440 /*prefix_attributes=*/NULL_TREE,
18441 &pushed_scope);
18443 init = cp_parser_assignment_expression (parser, false);
18445 cp_finish_decl (decl, NULL_TREE, asm_specification,
18446 LOOKUP_ONLYCONVERTING);
18448 if (pushed_scope)
18449 pop_scope (pushed_scope);
18453 /* If parsing as an initialized declaration failed, try again as
18454 a simple expression. */
18455 if (decl == NULL)
18457 cp_parser_abort_tentative_parse (parser);
18458 init = cp_parser_expression (parser, false);
18461 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18462 pre_body = pop_stmt_list (pre_body);
18464 cond = NULL;
18465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18466 cond = cp_parser_condition (parser);
18467 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18469 incr = NULL;
18470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18471 incr = cp_parser_expression (parser, false);
18473 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18475 /*or_comma=*/false,
18476 /*consume_paren=*/true);
18478 /* Note that we saved the original contents of this flag when we entered
18479 the structured block, and so we don't need to re-save it here. */
18480 parser->in_statement = IN_OMP_FOR;
18482 /* Note that the grammar doesn't call for a structured block here,
18483 though the loop as a whole is a structured block. */
18484 body = push_stmt_list ();
18485 cp_parser_statement (parser, NULL_TREE, false);
18486 body = pop_stmt_list (body);
18488 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18491 /* OpenMP 2.5:
18492 #pragma omp for for-clause[optseq] new-line
18493 for-loop
18496 #define OMP_FOR_CLAUSE_MASK \
18497 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18498 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18499 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18500 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18501 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18502 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18503 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18505 static tree
18506 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18508 tree clauses, sb, ret;
18509 unsigned int save;
18511 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18512 "#pragma omp for", pragma_tok);
18514 sb = begin_omp_structured_block ();
18515 save = cp_parser_begin_omp_structured_block (parser);
18517 ret = cp_parser_omp_for_loop (parser);
18518 if (ret)
18519 OMP_FOR_CLAUSES (ret) = clauses;
18521 cp_parser_end_omp_structured_block (parser, save);
18522 add_stmt (finish_omp_structured_block (sb));
18524 return ret;
18527 /* OpenMP 2.5:
18528 # pragma omp master new-line
18529 structured-block
18532 static tree
18533 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18535 cp_parser_require_pragma_eol (parser, pragma_tok);
18536 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18539 /* OpenMP 2.5:
18540 # pragma omp ordered new-line
18541 structured-block
18544 static tree
18545 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18547 cp_parser_require_pragma_eol (parser, pragma_tok);
18548 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18551 /* OpenMP 2.5:
18553 section-scope:
18554 { section-sequence }
18556 section-sequence:
18557 section-directive[opt] structured-block
18558 section-sequence section-directive structured-block */
18560 static tree
18561 cp_parser_omp_sections_scope (cp_parser *parser)
18563 tree stmt, substmt;
18564 bool error_suppress = false;
18565 cp_token *tok;
18567 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18568 return NULL_TREE;
18570 stmt = push_stmt_list ();
18572 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18574 unsigned save;
18576 substmt = begin_omp_structured_block ();
18577 save = cp_parser_begin_omp_structured_block (parser);
18579 while (1)
18581 cp_parser_statement (parser, NULL_TREE, false);
18583 tok = cp_lexer_peek_token (parser->lexer);
18584 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18585 break;
18586 if (tok->type == CPP_CLOSE_BRACE)
18587 break;
18588 if (tok->type == CPP_EOF)
18589 break;
18592 cp_parser_end_omp_structured_block (parser, save);
18593 substmt = finish_omp_structured_block (substmt);
18594 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18595 add_stmt (substmt);
18598 while (1)
18600 tok = cp_lexer_peek_token (parser->lexer);
18601 if (tok->type == CPP_CLOSE_BRACE)
18602 break;
18603 if (tok->type == CPP_EOF)
18604 break;
18606 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18608 cp_lexer_consume_token (parser->lexer);
18609 cp_parser_require_pragma_eol (parser, tok);
18610 error_suppress = false;
18612 else if (!error_suppress)
18614 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18615 error_suppress = true;
18618 substmt = cp_parser_omp_structured_block (parser);
18619 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18620 add_stmt (substmt);
18622 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18624 substmt = pop_stmt_list (stmt);
18626 stmt = make_node (OMP_SECTIONS);
18627 TREE_TYPE (stmt) = void_type_node;
18628 OMP_SECTIONS_BODY (stmt) = substmt;
18630 add_stmt (stmt);
18631 return stmt;
18634 /* OpenMP 2.5:
18635 # pragma omp sections sections-clause[optseq] newline
18636 sections-scope
18639 #define OMP_SECTIONS_CLAUSE_MASK \
18640 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18641 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18642 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18643 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18644 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18646 static tree
18647 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18649 tree clauses, ret;
18651 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18652 "#pragma omp sections", pragma_tok);
18654 ret = cp_parser_omp_sections_scope (parser);
18655 if (ret)
18656 OMP_SECTIONS_CLAUSES (ret) = clauses;
18658 return ret;
18661 /* OpenMP 2.5:
18662 # pragma parallel parallel-clause new-line
18663 # pragma parallel for parallel-for-clause new-line
18664 # pragma parallel sections parallel-sections-clause new-line
18667 #define OMP_PARALLEL_CLAUSE_MASK \
18668 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18669 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18670 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18671 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18672 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18673 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18674 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18675 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18677 static tree
18678 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18680 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18681 const char *p_name = "#pragma omp parallel";
18682 tree stmt, clauses, par_clause, ws_clause, block;
18683 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18684 unsigned int save;
18686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18688 cp_lexer_consume_token (parser->lexer);
18689 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18690 p_name = "#pragma omp parallel for";
18691 mask |= OMP_FOR_CLAUSE_MASK;
18692 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18694 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18696 tree id = cp_lexer_peek_token (parser->lexer)->value;
18697 const char *p = IDENTIFIER_POINTER (id);
18698 if (strcmp (p, "sections") == 0)
18700 cp_lexer_consume_token (parser->lexer);
18701 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18702 p_name = "#pragma omp parallel sections";
18703 mask |= OMP_SECTIONS_CLAUSE_MASK;
18704 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18708 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18709 block = begin_omp_parallel ();
18710 save = cp_parser_begin_omp_structured_block (parser);
18712 switch (p_kind)
18714 case PRAGMA_OMP_PARALLEL:
18715 cp_parser_already_scoped_statement (parser);
18716 par_clause = clauses;
18717 break;
18719 case PRAGMA_OMP_PARALLEL_FOR:
18720 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18721 stmt = cp_parser_omp_for_loop (parser);
18722 if (stmt)
18723 OMP_FOR_CLAUSES (stmt) = ws_clause;
18724 break;
18726 case PRAGMA_OMP_PARALLEL_SECTIONS:
18727 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18728 stmt = cp_parser_omp_sections_scope (parser);
18729 if (stmt)
18730 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18731 break;
18733 default:
18734 gcc_unreachable ();
18737 cp_parser_end_omp_structured_block (parser, save);
18738 return finish_omp_parallel (par_clause, block);
18741 /* OpenMP 2.5:
18742 # pragma omp single single-clause[optseq] new-line
18743 structured-block
18746 #define OMP_SINGLE_CLAUSE_MASK \
18747 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18748 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18749 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18750 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18752 static tree
18753 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18755 tree stmt = make_node (OMP_SINGLE);
18756 TREE_TYPE (stmt) = void_type_node;
18758 OMP_SINGLE_CLAUSES (stmt)
18759 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18760 "#pragma omp single", pragma_tok);
18761 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18763 return add_stmt (stmt);
18766 /* OpenMP 2.5:
18767 # pragma omp threadprivate (variable-list) */
18769 static void
18770 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18772 tree vars;
18774 vars = cp_parser_omp_var_list (parser, 0, NULL);
18775 cp_parser_require_pragma_eol (parser, pragma_tok);
18777 if (!targetm.have_tls)
18778 sorry ("threadprivate variables not supported in this target");
18780 finish_omp_threadprivate (vars);
18783 /* Main entry point to OpenMP statement pragmas. */
18785 static void
18786 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18788 tree stmt;
18790 switch (pragma_tok->pragma_kind)
18792 case PRAGMA_OMP_ATOMIC:
18793 cp_parser_omp_atomic (parser, pragma_tok);
18794 return;
18795 case PRAGMA_OMP_CRITICAL:
18796 stmt = cp_parser_omp_critical (parser, pragma_tok);
18797 break;
18798 case PRAGMA_OMP_FOR:
18799 stmt = cp_parser_omp_for (parser, pragma_tok);
18800 break;
18801 case PRAGMA_OMP_MASTER:
18802 stmt = cp_parser_omp_master (parser, pragma_tok);
18803 break;
18804 case PRAGMA_OMP_ORDERED:
18805 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18806 break;
18807 case PRAGMA_OMP_PARALLEL:
18808 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18809 break;
18810 case PRAGMA_OMP_SECTIONS:
18811 stmt = cp_parser_omp_sections (parser, pragma_tok);
18812 break;
18813 case PRAGMA_OMP_SINGLE:
18814 stmt = cp_parser_omp_single (parser, pragma_tok);
18815 break;
18816 default:
18817 gcc_unreachable ();
18820 if (stmt)
18821 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18824 /* The parser. */
18826 static GTY (()) cp_parser *the_parser;
18829 /* Special handling for the first token or line in the file. The first
18830 thing in the file might be #pragma GCC pch_preprocess, which loads a
18831 PCH file, which is a GC collection point. So we need to handle this
18832 first pragma without benefit of an existing lexer structure.
18834 Always returns one token to the caller in *FIRST_TOKEN. This is
18835 either the true first token of the file, or the first token after
18836 the initial pragma. */
18838 static void
18839 cp_parser_initial_pragma (cp_token *first_token)
18841 tree name = NULL;
18843 cp_lexer_get_preprocessor_token (NULL, first_token);
18844 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18845 return;
18847 cp_lexer_get_preprocessor_token (NULL, first_token);
18848 if (first_token->type == CPP_STRING)
18850 name = first_token->value;
18852 cp_lexer_get_preprocessor_token (NULL, first_token);
18853 if (first_token->type != CPP_PRAGMA_EOL)
18854 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18856 else
18857 error ("expected string literal");
18859 /* Skip to the end of the pragma. */
18860 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18861 cp_lexer_get_preprocessor_token (NULL, first_token);
18863 /* Read one more token to return to our caller. */
18864 cp_lexer_get_preprocessor_token (NULL, first_token);
18866 /* Now actually load the PCH file. */
18867 if (name)
18868 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
18871 /* Normal parsing of a pragma token. Here we can (and must) use the
18872 regular lexer. */
18874 static bool
18875 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
18877 cp_token *pragma_tok;
18878 unsigned int id;
18880 pragma_tok = cp_lexer_consume_token (parser->lexer);
18881 gcc_assert (pragma_tok->type == CPP_PRAGMA);
18882 parser->lexer->in_pragma = true;
18884 id = pragma_tok->pragma_kind;
18885 switch (id)
18887 case PRAGMA_GCC_PCH_PREPROCESS:
18888 error ("%<#pragma GCC pch_preprocess%> must be first");
18889 break;
18891 case PRAGMA_OMP_BARRIER:
18892 switch (context)
18894 case pragma_compound:
18895 cp_parser_omp_barrier (parser, pragma_tok);
18896 return false;
18897 case pragma_stmt:
18898 error ("%<#pragma omp barrier%> may only be "
18899 "used in compound statements");
18900 break;
18901 default:
18902 goto bad_stmt;
18904 break;
18906 case PRAGMA_OMP_FLUSH:
18907 switch (context)
18909 case pragma_compound:
18910 cp_parser_omp_flush (parser, pragma_tok);
18911 return false;
18912 case pragma_stmt:
18913 error ("%<#pragma omp flush%> may only be "
18914 "used in compound statements");
18915 break;
18916 default:
18917 goto bad_stmt;
18919 break;
18921 case PRAGMA_OMP_THREADPRIVATE:
18922 cp_parser_omp_threadprivate (parser, pragma_tok);
18923 return false;
18925 case PRAGMA_OMP_ATOMIC:
18926 case PRAGMA_OMP_CRITICAL:
18927 case PRAGMA_OMP_FOR:
18928 case PRAGMA_OMP_MASTER:
18929 case PRAGMA_OMP_ORDERED:
18930 case PRAGMA_OMP_PARALLEL:
18931 case PRAGMA_OMP_SECTIONS:
18932 case PRAGMA_OMP_SINGLE:
18933 if (context == pragma_external)
18934 goto bad_stmt;
18935 cp_parser_omp_construct (parser, pragma_tok);
18936 return true;
18938 case PRAGMA_OMP_SECTION:
18939 error ("%<#pragma omp section%> may only be used in "
18940 "%<#pragma omp sections%> construct");
18941 break;
18943 default:
18944 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
18945 c_invoke_pragma_handler (id);
18946 break;
18948 bad_stmt:
18949 cp_parser_error (parser, "expected declaration specifiers");
18950 break;
18953 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18954 return false;
18957 /* The interface the pragma parsers have to the lexer. */
18959 enum cpp_ttype
18960 pragma_lex (tree *value)
18962 cp_token *tok;
18963 enum cpp_ttype ret;
18965 tok = cp_lexer_peek_token (the_parser->lexer);
18967 ret = tok->type;
18968 *value = tok->value;
18970 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
18971 ret = CPP_EOF;
18972 else if (ret == CPP_STRING)
18973 *value = cp_parser_string_literal (the_parser, false, false);
18974 else
18976 cp_lexer_consume_token (the_parser->lexer);
18977 if (ret == CPP_KEYWORD)
18978 ret = CPP_NAME;
18981 return ret;
18985 /* External interface. */
18987 /* Parse one entire translation unit. */
18989 void
18990 c_parse_file (void)
18992 bool error_occurred;
18993 static bool already_called = false;
18995 if (already_called)
18997 sorry ("inter-module optimizations not implemented for C++");
18998 return;
19000 already_called = true;
19002 the_parser = cp_parser_new ();
19003 push_deferring_access_checks (flag_access_control
19004 ? dk_no_deferred : dk_no_check);
19005 error_occurred = cp_parser_translation_unit (the_parser);
19006 the_parser = NULL;
19009 /* This variable must be provided by every front end. */
19011 int yydebug;
19013 #include "gt-cp-parser.h"