Merge C++ from gomp-20050608-branch.
[official-gcc.git] / gcc / cp / parser.c
blobb5c7fc011a5bae964715623865db1ffeb4142e76
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 Note that this doesn't quite overlap with in_statement above.
1309 The difference relates to giving the right sets of error messages:
1310 "case not in switch" vs "break statement used with OpenMP...". */
1311 bool in_switch_statement_p;
1313 /* TRUE if we are parsing a type-id in an expression context. In
1314 such a situation, both "type (expr)" and "type (type)" are valid
1315 alternatives. */
1316 bool in_type_id_in_expr_p;
1318 /* TRUE if we are currently in a header file where declarations are
1319 implicitly extern "C". */
1320 bool implicit_extern_c;
1322 /* TRUE if strings in expressions should be translated to the execution
1323 character set. */
1324 bool translate_strings_p;
1326 /* If non-NULL, then we are parsing a construct where new type
1327 definitions are not permitted. The string stored here will be
1328 issued as an error message if a type is defined. */
1329 const char *type_definition_forbidden_message;
1331 /* A list of lists. The outer list is a stack, used for member
1332 functions of local classes. At each level there are two sub-list,
1333 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1334 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1335 TREE_VALUE's. The functions are chained in reverse declaration
1336 order.
1338 The TREE_PURPOSE sublist contains those functions with default
1339 arguments that need post processing, and the TREE_VALUE sublist
1340 contains those functions with definitions that need post
1341 processing.
1343 These lists can only be processed once the outermost class being
1344 defined is complete. */
1345 tree unparsed_functions_queues;
1347 /* The number of classes whose definitions are currently in
1348 progress. */
1349 unsigned num_classes_being_defined;
1351 /* The number of template parameter lists that apply directly to the
1352 current declaration. */
1353 unsigned num_template_parameter_lists;
1354 } cp_parser;
1356 /* Prototypes. */
1358 /* Constructors and destructors. */
1360 static cp_parser *cp_parser_new
1361 (void);
1363 /* Routines to parse various constructs.
1365 Those that return `tree' will return the error_mark_node (rather
1366 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1367 Sometimes, they will return an ordinary node if error-recovery was
1368 attempted, even though a parse error occurred. So, to check
1369 whether or not a parse error occurred, you should always use
1370 cp_parser_error_occurred. If the construct is optional (indicated
1371 either by an `_opt' in the name of the function that does the
1372 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1373 the construct is not present. */
1375 /* Lexical conventions [gram.lex] */
1377 static tree cp_parser_identifier
1378 (cp_parser *);
1379 static tree cp_parser_string_literal
1380 (cp_parser *, bool, bool);
1382 /* Basic concepts [gram.basic] */
1384 static bool cp_parser_translation_unit
1385 (cp_parser *);
1387 /* Expressions [gram.expr] */
1389 static tree cp_parser_primary_expression
1390 (cp_parser *, bool, bool, bool, cp_id_kind *);
1391 static tree cp_parser_id_expression
1392 (cp_parser *, bool, bool, bool *, bool);
1393 static tree cp_parser_unqualified_id
1394 (cp_parser *, bool, bool, bool);
1395 static tree cp_parser_nested_name_specifier_opt
1396 (cp_parser *, bool, bool, bool, bool);
1397 static tree cp_parser_nested_name_specifier
1398 (cp_parser *, bool, bool, bool, bool);
1399 static tree cp_parser_class_or_namespace_name
1400 (cp_parser *, bool, bool, bool, bool, bool);
1401 static tree cp_parser_postfix_expression
1402 (cp_parser *, bool, bool);
1403 static tree cp_parser_postfix_open_square_expression
1404 (cp_parser *, tree, bool);
1405 static tree cp_parser_postfix_dot_deref_expression
1406 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1407 static tree cp_parser_parenthesized_expression_list
1408 (cp_parser *, bool, bool, bool *);
1409 static void cp_parser_pseudo_destructor_name
1410 (cp_parser *, tree *, tree *);
1411 static tree cp_parser_unary_expression
1412 (cp_parser *, bool, bool);
1413 static enum tree_code cp_parser_unary_operator
1414 (cp_token *);
1415 static tree cp_parser_new_expression
1416 (cp_parser *);
1417 static tree cp_parser_new_placement
1418 (cp_parser *);
1419 static tree cp_parser_new_type_id
1420 (cp_parser *, tree *);
1421 static cp_declarator *cp_parser_new_declarator_opt
1422 (cp_parser *);
1423 static cp_declarator *cp_parser_direct_new_declarator
1424 (cp_parser *);
1425 static tree cp_parser_new_initializer
1426 (cp_parser *);
1427 static tree cp_parser_delete_expression
1428 (cp_parser *);
1429 static tree cp_parser_cast_expression
1430 (cp_parser *, bool, bool);
1431 static tree cp_parser_binary_expression
1432 (cp_parser *, bool);
1433 static tree cp_parser_question_colon_clause
1434 (cp_parser *, tree);
1435 static tree cp_parser_assignment_expression
1436 (cp_parser *, bool);
1437 static enum tree_code cp_parser_assignment_operator_opt
1438 (cp_parser *);
1439 static tree cp_parser_expression
1440 (cp_parser *, bool);
1441 static tree cp_parser_constant_expression
1442 (cp_parser *, bool, bool *);
1443 static tree cp_parser_builtin_offsetof
1444 (cp_parser *);
1446 /* Statements [gram.stmt.stmt] */
1448 static void cp_parser_statement
1449 (cp_parser *, tree, bool);
1450 static tree cp_parser_labeled_statement
1451 (cp_parser *, tree, bool);
1452 static tree cp_parser_expression_statement
1453 (cp_parser *, tree);
1454 static tree cp_parser_compound_statement
1455 (cp_parser *, tree, bool);
1456 static void cp_parser_statement_seq_opt
1457 (cp_parser *, tree);
1458 static tree cp_parser_selection_statement
1459 (cp_parser *);
1460 static tree cp_parser_condition
1461 (cp_parser *);
1462 static tree cp_parser_iteration_statement
1463 (cp_parser *);
1464 static void cp_parser_for_init_statement
1465 (cp_parser *);
1466 static tree cp_parser_jump_statement
1467 (cp_parser *);
1468 static void cp_parser_declaration_statement
1469 (cp_parser *);
1471 static tree cp_parser_implicitly_scoped_statement
1472 (cp_parser *);
1473 static void cp_parser_already_scoped_statement
1474 (cp_parser *);
1476 /* Declarations [gram.dcl.dcl] */
1478 static void cp_parser_declaration_seq_opt
1479 (cp_parser *);
1480 static void cp_parser_declaration
1481 (cp_parser *);
1482 static void cp_parser_block_declaration
1483 (cp_parser *, bool);
1484 static void cp_parser_simple_declaration
1485 (cp_parser *, bool);
1486 static void cp_parser_decl_specifier_seq
1487 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1488 static tree cp_parser_storage_class_specifier_opt
1489 (cp_parser *);
1490 static tree cp_parser_function_specifier_opt
1491 (cp_parser *, cp_decl_specifier_seq *);
1492 static tree cp_parser_type_specifier
1493 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1494 int *, bool *);
1495 static tree cp_parser_simple_type_specifier
1496 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1497 static tree cp_parser_type_name
1498 (cp_parser *);
1499 static tree cp_parser_elaborated_type_specifier
1500 (cp_parser *, bool, bool);
1501 static tree cp_parser_enum_specifier
1502 (cp_parser *);
1503 static void cp_parser_enumerator_list
1504 (cp_parser *, tree);
1505 static void cp_parser_enumerator_definition
1506 (cp_parser *, tree);
1507 static tree cp_parser_namespace_name
1508 (cp_parser *);
1509 static void cp_parser_namespace_definition
1510 (cp_parser *);
1511 static void cp_parser_namespace_body
1512 (cp_parser *);
1513 static tree cp_parser_qualified_namespace_specifier
1514 (cp_parser *);
1515 static void cp_parser_namespace_alias_definition
1516 (cp_parser *);
1517 static void cp_parser_using_declaration
1518 (cp_parser *);
1519 static void cp_parser_using_directive
1520 (cp_parser *);
1521 static void cp_parser_asm_definition
1522 (cp_parser *);
1523 static void cp_parser_linkage_specification
1524 (cp_parser *);
1526 /* Declarators [gram.dcl.decl] */
1528 static tree cp_parser_init_declarator
1529 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1530 static cp_declarator *cp_parser_declarator
1531 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1532 static cp_declarator *cp_parser_direct_declarator
1533 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1534 static enum tree_code cp_parser_ptr_operator
1535 (cp_parser *, tree *, cp_cv_quals *);
1536 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1537 (cp_parser *);
1538 static tree cp_parser_declarator_id
1539 (cp_parser *);
1540 static tree cp_parser_type_id
1541 (cp_parser *);
1542 static void cp_parser_type_specifier_seq
1543 (cp_parser *, bool, cp_decl_specifier_seq *);
1544 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1545 (cp_parser *);
1546 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1547 (cp_parser *, bool *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration
1549 (cp_parser *, bool, bool *);
1550 static void cp_parser_function_body
1551 (cp_parser *);
1552 static tree cp_parser_initializer
1553 (cp_parser *, bool *, bool *);
1554 static tree cp_parser_initializer_clause
1555 (cp_parser *, bool *);
1556 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1557 (cp_parser *, bool *);
1559 static bool cp_parser_ctor_initializer_opt_and_function_body
1560 (cp_parser *);
1562 /* Classes [gram.class] */
1564 static tree cp_parser_class_name
1565 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1566 static tree cp_parser_class_specifier
1567 (cp_parser *);
1568 static tree cp_parser_class_head
1569 (cp_parser *, bool *, tree *);
1570 static enum tag_types cp_parser_class_key
1571 (cp_parser *);
1572 static void cp_parser_member_specification_opt
1573 (cp_parser *);
1574 static void cp_parser_member_declaration
1575 (cp_parser *);
1576 static tree cp_parser_pure_specifier
1577 (cp_parser *);
1578 static tree cp_parser_constant_initializer
1579 (cp_parser *);
1581 /* Derived classes [gram.class.derived] */
1583 static tree cp_parser_base_clause
1584 (cp_parser *);
1585 static tree cp_parser_base_specifier
1586 (cp_parser *);
1588 /* Special member functions [gram.special] */
1590 static tree cp_parser_conversion_function_id
1591 (cp_parser *);
1592 static tree cp_parser_conversion_type_id
1593 (cp_parser *);
1594 static cp_declarator *cp_parser_conversion_declarator_opt
1595 (cp_parser *);
1596 static bool cp_parser_ctor_initializer_opt
1597 (cp_parser *);
1598 static void cp_parser_mem_initializer_list
1599 (cp_parser *);
1600 static tree cp_parser_mem_initializer
1601 (cp_parser *);
1602 static tree cp_parser_mem_initializer_id
1603 (cp_parser *);
1605 /* Overloading [gram.over] */
1607 static tree cp_parser_operator_function_id
1608 (cp_parser *);
1609 static tree cp_parser_operator
1610 (cp_parser *);
1612 /* Templates [gram.temp] */
1614 static void cp_parser_template_declaration
1615 (cp_parser *, bool);
1616 static tree cp_parser_template_parameter_list
1617 (cp_parser *);
1618 static tree cp_parser_template_parameter
1619 (cp_parser *, bool *);
1620 static tree cp_parser_type_parameter
1621 (cp_parser *);
1622 static tree cp_parser_template_id
1623 (cp_parser *, bool, bool, bool);
1624 static tree cp_parser_template_name
1625 (cp_parser *, bool, bool, bool, bool *);
1626 static tree cp_parser_template_argument_list
1627 (cp_parser *);
1628 static tree cp_parser_template_argument
1629 (cp_parser *);
1630 static void cp_parser_explicit_instantiation
1631 (cp_parser *);
1632 static void cp_parser_explicit_specialization
1633 (cp_parser *);
1635 /* Exception handling [gram.exception] */
1637 static tree cp_parser_try_block
1638 (cp_parser *);
1639 static bool cp_parser_function_try_block
1640 (cp_parser *);
1641 static void cp_parser_handler_seq
1642 (cp_parser *);
1643 static void cp_parser_handler
1644 (cp_parser *);
1645 static tree cp_parser_exception_declaration
1646 (cp_parser *);
1647 static tree cp_parser_throw_expression
1648 (cp_parser *);
1649 static tree cp_parser_exception_specification_opt
1650 (cp_parser *);
1651 static tree cp_parser_type_id_list
1652 (cp_parser *);
1654 /* GNU Extensions */
1656 static tree cp_parser_asm_specification_opt
1657 (cp_parser *);
1658 static tree cp_parser_asm_operand_list
1659 (cp_parser *);
1660 static tree cp_parser_asm_clobber_list
1661 (cp_parser *);
1662 static tree cp_parser_attributes_opt
1663 (cp_parser *);
1664 static tree cp_parser_attribute_list
1665 (cp_parser *);
1666 static bool cp_parser_extension_opt
1667 (cp_parser *, int *);
1668 static void cp_parser_label_declaration
1669 (cp_parser *);
1671 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1672 static bool cp_parser_pragma
1673 (cp_parser *, enum pragma_context);
1675 /* Objective-C++ Productions */
1677 static tree cp_parser_objc_message_receiver
1678 (cp_parser *);
1679 static tree cp_parser_objc_message_args
1680 (cp_parser *);
1681 static tree cp_parser_objc_message_expression
1682 (cp_parser *);
1683 static tree cp_parser_objc_encode_expression
1684 (cp_parser *);
1685 static tree cp_parser_objc_defs_expression
1686 (cp_parser *);
1687 static tree cp_parser_objc_protocol_expression
1688 (cp_parser *);
1689 static tree cp_parser_objc_selector_expression
1690 (cp_parser *);
1691 static tree cp_parser_objc_expression
1692 (cp_parser *);
1693 static bool cp_parser_objc_selector_p
1694 (enum cpp_ttype);
1695 static tree cp_parser_objc_selector
1696 (cp_parser *);
1697 static tree cp_parser_objc_protocol_refs_opt
1698 (cp_parser *);
1699 static void cp_parser_objc_declaration
1700 (cp_parser *);
1701 static tree cp_parser_objc_statement
1702 (cp_parser *);
1704 /* Utility Routines */
1706 static tree cp_parser_lookup_name
1707 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1708 static tree cp_parser_lookup_name_simple
1709 (cp_parser *, tree);
1710 static tree cp_parser_maybe_treat_template_as_class
1711 (tree, bool);
1712 static bool cp_parser_check_declarator_template_parameters
1713 (cp_parser *, cp_declarator *);
1714 static bool cp_parser_check_template_parameters
1715 (cp_parser *, unsigned);
1716 static tree cp_parser_simple_cast_expression
1717 (cp_parser *);
1718 static tree cp_parser_global_scope_opt
1719 (cp_parser *, bool);
1720 static bool cp_parser_constructor_declarator_p
1721 (cp_parser *, bool);
1722 static tree cp_parser_function_definition_from_specifiers_and_declarator
1723 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1724 static tree cp_parser_function_definition_after_declarator
1725 (cp_parser *, bool);
1726 static void cp_parser_template_declaration_after_export
1727 (cp_parser *, bool);
1728 static tree cp_parser_single_declaration
1729 (cp_parser *, bool, bool *);
1730 static tree cp_parser_functional_cast
1731 (cp_parser *, tree);
1732 static tree cp_parser_save_member_function_body
1733 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1734 static tree cp_parser_enclosed_template_argument_list
1735 (cp_parser *);
1736 static void cp_parser_save_default_args
1737 (cp_parser *, tree);
1738 static void cp_parser_late_parsing_for_member
1739 (cp_parser *, tree);
1740 static void cp_parser_late_parsing_default_args
1741 (cp_parser *, tree);
1742 static tree cp_parser_sizeof_operand
1743 (cp_parser *, enum rid);
1744 static bool cp_parser_declares_only_class_p
1745 (cp_parser *);
1746 static void cp_parser_set_storage_class
1747 (cp_decl_specifier_seq *, cp_storage_class);
1748 static void cp_parser_set_decl_spec_type
1749 (cp_decl_specifier_seq *, tree, bool);
1750 static bool cp_parser_friend_p
1751 (const cp_decl_specifier_seq *);
1752 static cp_token *cp_parser_require
1753 (cp_parser *, enum cpp_ttype, const char *);
1754 static cp_token *cp_parser_require_keyword
1755 (cp_parser *, enum rid, const char *);
1756 static bool cp_parser_token_starts_function_definition_p
1757 (cp_token *);
1758 static bool cp_parser_next_token_starts_class_definition_p
1759 (cp_parser *);
1760 static bool cp_parser_next_token_ends_template_argument_p
1761 (cp_parser *);
1762 static bool cp_parser_nth_token_starts_template_argument_list_p
1763 (cp_parser *, size_t);
1764 static enum tag_types cp_parser_token_is_class_key
1765 (cp_token *);
1766 static void cp_parser_check_class_key
1767 (enum tag_types, tree type);
1768 static void cp_parser_check_access_in_redeclaration
1769 (tree type);
1770 static bool cp_parser_optional_template_keyword
1771 (cp_parser *);
1772 static void cp_parser_pre_parsed_nested_name_specifier
1773 (cp_parser *);
1774 static void cp_parser_cache_group
1775 (cp_parser *, enum cpp_ttype, unsigned);
1776 static void cp_parser_parse_tentatively
1777 (cp_parser *);
1778 static void cp_parser_commit_to_tentative_parse
1779 (cp_parser *);
1780 static void cp_parser_abort_tentative_parse
1781 (cp_parser *);
1782 static bool cp_parser_parse_definitely
1783 (cp_parser *);
1784 static inline bool cp_parser_parsing_tentatively
1785 (cp_parser *);
1786 static bool cp_parser_uncommitted_to_tentative_parse_p
1787 (cp_parser *);
1788 static void cp_parser_error
1789 (cp_parser *, const char *);
1790 static void cp_parser_name_lookup_error
1791 (cp_parser *, tree, tree, const char *);
1792 static bool cp_parser_simulate_error
1793 (cp_parser *);
1794 static void cp_parser_check_type_definition
1795 (cp_parser *);
1796 static void cp_parser_check_for_definition_in_return_type
1797 (cp_declarator *, tree);
1798 static void cp_parser_check_for_invalid_template_id
1799 (cp_parser *, tree);
1800 static bool cp_parser_non_integral_constant_expression
1801 (cp_parser *, const char *);
1802 static void cp_parser_diagnose_invalid_type_name
1803 (cp_parser *, tree, tree);
1804 static bool cp_parser_parse_and_diagnose_invalid_type_name
1805 (cp_parser *);
1806 static int cp_parser_skip_to_closing_parenthesis
1807 (cp_parser *, bool, bool, bool);
1808 static void cp_parser_skip_to_end_of_statement
1809 (cp_parser *);
1810 static void cp_parser_consume_semicolon_at_end_of_statement
1811 (cp_parser *);
1812 static void cp_parser_skip_to_end_of_block_or_statement
1813 (cp_parser *);
1814 static void cp_parser_skip_to_closing_brace
1815 (cp_parser *);
1816 static void cp_parser_skip_until_found
1817 (cp_parser *, enum cpp_ttype, const char *);
1818 static void cp_parser_skip_to_pragma_eol
1819 (cp_parser*, cp_token *);
1820 static bool cp_parser_error_occurred
1821 (cp_parser *);
1822 static bool cp_parser_allow_gnu_extensions_p
1823 (cp_parser *);
1824 static bool cp_parser_is_string_literal
1825 (cp_token *);
1826 static bool cp_parser_is_keyword
1827 (cp_token *, enum rid);
1828 static tree cp_parser_make_typename_type
1829 (cp_parser *, tree, tree);
1831 /* Returns nonzero if we are parsing tentatively. */
1833 static inline bool
1834 cp_parser_parsing_tentatively (cp_parser* parser)
1836 return parser->context->next != NULL;
1839 /* Returns nonzero if TOKEN is a string literal. */
1841 static bool
1842 cp_parser_is_string_literal (cp_token* token)
1844 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1847 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1849 static bool
1850 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1852 return token->keyword == keyword;
1855 /* A minimum or maximum operator has been seen. As these are
1856 deprecated, issue a warning. */
1858 static inline void
1859 cp_parser_warn_min_max (void)
1861 if (warn_deprecated && !in_system_header)
1862 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1865 /* If not parsing tentatively, issue a diagnostic of the form
1866 FILE:LINE: MESSAGE before TOKEN
1867 where TOKEN is the next token in the input stream. MESSAGE
1868 (specified by the caller) is usually of the form "expected
1869 OTHER-TOKEN". */
1871 static void
1872 cp_parser_error (cp_parser* parser, const char* message)
1874 if (!cp_parser_simulate_error (parser))
1876 cp_token *token = cp_lexer_peek_token (parser->lexer);
1877 /* This diagnostic makes more sense if it is tagged to the line
1878 of the token we just peeked at. */
1879 cp_lexer_set_source_position_from_token (token);
1881 if (token->type == CPP_PRAGMA)
1883 error ("%<#pragma%> is not allowed here");
1884 cp_parser_skip_to_pragma_eol (parser, token);
1885 return;
1888 c_parse_error (message,
1889 /* Because c_parser_error does not understand
1890 CPP_KEYWORD, keywords are treated like
1891 identifiers. */
1892 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1893 token->value);
1897 /* Issue an error about name-lookup failing. NAME is the
1898 IDENTIFIER_NODE DECL is the result of
1899 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1900 the thing that we hoped to find. */
1902 static void
1903 cp_parser_name_lookup_error (cp_parser* parser,
1904 tree name,
1905 tree decl,
1906 const char* desired)
1908 /* If name lookup completely failed, tell the user that NAME was not
1909 declared. */
1910 if (decl == error_mark_node)
1912 if (parser->scope && parser->scope != global_namespace)
1913 error ("%<%D::%D%> has not been declared",
1914 parser->scope, name);
1915 else if (parser->scope == global_namespace)
1916 error ("%<::%D%> has not been declared", name);
1917 else if (parser->object_scope
1918 && !CLASS_TYPE_P (parser->object_scope))
1919 error ("request for member %qD in non-class type %qT",
1920 name, parser->object_scope);
1921 else if (parser->object_scope)
1922 error ("%<%T::%D%> has not been declared",
1923 parser->object_scope, name);
1924 else
1925 error ("%qD has not been declared", name);
1927 else if (parser->scope && parser->scope != global_namespace)
1928 error ("%<%D::%D%> %s", parser->scope, name, desired);
1929 else if (parser->scope == global_namespace)
1930 error ("%<::%D%> %s", name, desired);
1931 else
1932 error ("%qD %s", name, desired);
1935 /* If we are parsing tentatively, remember that an error has occurred
1936 during this tentative parse. Returns true if the error was
1937 simulated; false if a message should be issued by the caller. */
1939 static bool
1940 cp_parser_simulate_error (cp_parser* parser)
1942 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1945 return true;
1947 return false;
1950 /* This function is called when a type is defined. If type
1951 definitions are forbidden at this point, an error message is
1952 issued. */
1954 static void
1955 cp_parser_check_type_definition (cp_parser* parser)
1957 /* If types are forbidden here, issue a message. */
1958 if (parser->type_definition_forbidden_message)
1959 /* Use `%s' to print the string in case there are any escape
1960 characters in the message. */
1961 error ("%s", parser->type_definition_forbidden_message);
1964 /* This function is called when the DECLARATOR is processed. The TYPE
1965 was a type defined in the decl-specifiers. If it is invalid to
1966 define a type in the decl-specifiers for DECLARATOR, an error is
1967 issued. */
1969 static void
1970 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1971 tree type)
1973 /* [dcl.fct] forbids type definitions in return types.
1974 Unfortunately, it's not easy to know whether or not we are
1975 processing a return type until after the fact. */
1976 while (declarator
1977 && (declarator->kind == cdk_pointer
1978 || declarator->kind == cdk_reference
1979 || declarator->kind == cdk_ptrmem))
1980 declarator = declarator->declarator;
1981 if (declarator
1982 && declarator->kind == cdk_function)
1984 error ("new types may not be defined in a return type");
1985 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1986 type);
1990 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1991 "<" in any valid C++ program. If the next token is indeed "<",
1992 issue a message warning the user about what appears to be an
1993 invalid attempt to form a template-id. */
1995 static void
1996 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1997 tree type)
1999 cp_token_position start = 0;
2001 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2003 if (TYPE_P (type))
2004 error ("%qT is not a template", type);
2005 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2006 error ("%qE is not a template", type);
2007 else
2008 error ("invalid template-id");
2009 /* Remember the location of the invalid "<". */
2010 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2011 start = cp_lexer_token_position (parser->lexer, true);
2012 /* Consume the "<". */
2013 cp_lexer_consume_token (parser->lexer);
2014 /* Parse the template arguments. */
2015 cp_parser_enclosed_template_argument_list (parser);
2016 /* Permanently remove the invalid template arguments so that
2017 this error message is not issued again. */
2018 if (start)
2019 cp_lexer_purge_tokens_after (parser->lexer, start);
2023 /* If parsing an integral constant-expression, issue an error message
2024 about the fact that THING appeared and return true. Otherwise,
2025 return false. In either case, set
2026 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2028 static bool
2029 cp_parser_non_integral_constant_expression (cp_parser *parser,
2030 const char *thing)
2032 parser->non_integral_constant_expression_p = true;
2033 if (parser->integral_constant_expression_p)
2035 if (!parser->allow_non_integral_constant_expression_p)
2037 error ("%s cannot appear in a constant-expression", thing);
2038 return true;
2041 return false;
2044 /* Emit a diagnostic for an invalid type name. SCOPE is the
2045 qualifying scope (or NULL, if none) for ID. This function commits
2046 to the current active tentative parse, if any. (Otherwise, the
2047 problematic construct might be encountered again later, resulting
2048 in duplicate error messages.) */
2050 static void
2051 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2053 tree decl, old_scope;
2054 /* Try to lookup the identifier. */
2055 old_scope = parser->scope;
2056 parser->scope = scope;
2057 decl = cp_parser_lookup_name_simple (parser, id);
2058 parser->scope = old_scope;
2059 /* If the lookup found a template-name, it means that the user forgot
2060 to specify an argument list. Emit a useful error message. */
2061 if (TREE_CODE (decl) == TEMPLATE_DECL)
2062 error ("invalid use of template-name %qE without an argument list",
2063 decl);
2064 else if (!parser->scope)
2066 /* Issue an error message. */
2067 error ("%qE does not name a type", id);
2068 /* If we're in a template class, it's possible that the user was
2069 referring to a type from a base class. For example:
2071 template <typename T> struct A { typedef T X; };
2072 template <typename T> struct B : public A<T> { X x; };
2074 The user should have said "typename A<T>::X". */
2075 if (processing_template_decl && current_class_type
2076 && TYPE_BINFO (current_class_type))
2078 tree b;
2080 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2082 b = TREE_CHAIN (b))
2084 tree base_type = BINFO_TYPE (b);
2085 if (CLASS_TYPE_P (base_type)
2086 && dependent_type_p (base_type))
2088 tree field;
2089 /* Go from a particular instantiation of the
2090 template (which will have an empty TYPE_FIELDs),
2091 to the main version. */
2092 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2093 for (field = TYPE_FIELDS (base_type);
2094 field;
2095 field = TREE_CHAIN (field))
2096 if (TREE_CODE (field) == TYPE_DECL
2097 && DECL_NAME (field) == id)
2099 inform ("(perhaps %<typename %T::%E%> was intended)",
2100 BINFO_TYPE (b), id);
2101 break;
2103 if (field)
2104 break;
2109 /* Here we diagnose qualified-ids where the scope is actually correct,
2110 but the identifier does not resolve to a valid type name. */
2111 else if (parser->scope != error_mark_node)
2113 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2114 error ("%qE in namespace %qE does not name a type",
2115 id, parser->scope);
2116 else if (TYPE_P (parser->scope))
2117 error ("%qE in class %qT does not name a type", id, parser->scope);
2118 else
2119 gcc_unreachable ();
2121 cp_parser_commit_to_tentative_parse (parser);
2124 /* Check for a common situation where a type-name should be present,
2125 but is not, and issue a sensible error message. Returns true if an
2126 invalid type-name was detected.
2128 The situation handled by this function are variable declarations of the
2129 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2130 Usually, `ID' should name a type, but if we got here it means that it
2131 does not. We try to emit the best possible error message depending on
2132 how exactly the id-expression looks like.
2135 static bool
2136 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2138 tree id;
2140 cp_parser_parse_tentatively (parser);
2141 id = cp_parser_id_expression (parser,
2142 /*template_keyword_p=*/false,
2143 /*check_dependency_p=*/true,
2144 /*template_p=*/NULL,
2145 /*declarator_p=*/true);
2146 /* After the id-expression, there should be a plain identifier,
2147 otherwise this is not a simple variable declaration. Also, if
2148 the scope is dependent, we cannot do much. */
2149 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2150 || (parser->scope && TYPE_P (parser->scope)
2151 && dependent_type_p (parser->scope)))
2153 cp_parser_abort_tentative_parse (parser);
2154 return false;
2156 if (!cp_parser_parse_definitely (parser)
2157 || TREE_CODE (id) != IDENTIFIER_NODE)
2158 return false;
2160 /* Emit a diagnostic for the invalid type. */
2161 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2162 /* Skip to the end of the declaration; there's no point in
2163 trying to process it. */
2164 cp_parser_skip_to_end_of_block_or_statement (parser);
2165 return true;
2168 /* Consume tokens up to, and including, the next non-nested closing `)'.
2169 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2170 are doing error recovery. Returns -1 if OR_COMMA is true and we
2171 found an unnested comma. */
2173 static int
2174 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2175 bool recovering,
2176 bool or_comma,
2177 bool consume_paren)
2179 unsigned paren_depth = 0;
2180 unsigned brace_depth = 0;
2182 if (recovering && !or_comma
2183 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2184 return 0;
2186 while (true)
2188 cp_token * token = cp_lexer_peek_token (parser->lexer);
2190 switch (token->type)
2192 case CPP_EOF:
2193 case CPP_PRAGMA_EOL:
2194 /* If we've run out of tokens, then there is no closing `)'. */
2195 return 0;
2197 case CPP_SEMICOLON:
2198 /* This matches the processing in skip_to_end_of_statement. */
2199 if (!brace_depth)
2200 return 0;
2201 break;
2203 case CPP_OPEN_BRACE:
2204 ++brace_depth;
2205 break;
2206 case CPP_CLOSE_BRACE:
2207 if (!brace_depth--)
2208 return 0;
2209 break;
2211 case CPP_COMMA:
2212 if (recovering && or_comma && !brace_depth && !paren_depth)
2213 return -1;
2214 break;
2216 case CPP_OPEN_PAREN:
2217 if (!brace_depth)
2218 ++paren_depth;
2219 break;
2221 case CPP_CLOSE_PAREN:
2222 if (!brace_depth && !paren_depth--)
2224 if (consume_paren)
2225 cp_lexer_consume_token (parser->lexer);
2226 return 1;
2228 break;
2230 default:
2231 break;
2234 /* Consume the token. */
2235 cp_lexer_consume_token (parser->lexer);
2239 /* Consume tokens until we reach the end of the current statement.
2240 Normally, that will be just before consuming a `;'. However, if a
2241 non-nested `}' comes first, then we stop before consuming that. */
2243 static void
2244 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2246 unsigned nesting_depth = 0;
2248 while (true)
2250 cp_token *token = cp_lexer_peek_token (parser->lexer);
2252 switch (token->type)
2254 case CPP_EOF:
2255 case CPP_PRAGMA_EOL:
2256 /* If we've run out of tokens, stop. */
2257 return;
2259 case CPP_SEMICOLON:
2260 /* If the next token is a `;', we have reached the end of the
2261 statement. */
2262 if (!nesting_depth)
2263 return;
2264 break;
2266 case CPP_CLOSE_BRACE:
2267 /* If this is a non-nested '}', stop before consuming it.
2268 That way, when confronted with something like:
2270 { 3 + }
2272 we stop before consuming the closing '}', even though we
2273 have not yet reached a `;'. */
2274 if (nesting_depth == 0)
2275 return;
2277 /* If it is the closing '}' for a block that we have
2278 scanned, stop -- but only after consuming the token.
2279 That way given:
2281 void f g () { ... }
2282 typedef int I;
2284 we will stop after the body of the erroneously declared
2285 function, but before consuming the following `typedef'
2286 declaration. */
2287 if (--nesting_depth == 0)
2289 cp_lexer_consume_token (parser->lexer);
2290 return;
2293 case CPP_OPEN_BRACE:
2294 ++nesting_depth;
2295 break;
2297 default:
2298 break;
2301 /* Consume the token. */
2302 cp_lexer_consume_token (parser->lexer);
2306 /* This function is called at the end of a statement or declaration.
2307 If the next token is a semicolon, it is consumed; otherwise, error
2308 recovery is attempted. */
2310 static void
2311 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2313 /* Look for the trailing `;'. */
2314 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2316 /* If there is additional (erroneous) input, skip to the end of
2317 the statement. */
2318 cp_parser_skip_to_end_of_statement (parser);
2319 /* If the next token is now a `;', consume it. */
2320 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2321 cp_lexer_consume_token (parser->lexer);
2325 /* Skip tokens until we have consumed an entire block, or until we
2326 have consumed a non-nested `;'. */
2328 static void
2329 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2331 int nesting_depth = 0;
2333 while (nesting_depth >= 0)
2335 cp_token *token = cp_lexer_peek_token (parser->lexer);
2337 switch (token->type)
2339 case CPP_EOF:
2340 case CPP_PRAGMA_EOL:
2341 /* If we've run out of tokens, stop. */
2342 return;
2344 case CPP_SEMICOLON:
2345 /* Stop if this is an unnested ';'. */
2346 if (!nesting_depth)
2347 nesting_depth = -1;
2348 break;
2350 case CPP_CLOSE_BRACE:
2351 /* Stop if this is an unnested '}', or closes the outermost
2352 nesting level. */
2353 nesting_depth--;
2354 if (!nesting_depth)
2355 nesting_depth = -1;
2356 break;
2358 case CPP_OPEN_BRACE:
2359 /* Nest. */
2360 nesting_depth++;
2361 break;
2363 default:
2364 break;
2367 /* Consume the token. */
2368 cp_lexer_consume_token (parser->lexer);
2372 /* Skip tokens until a non-nested closing curly brace is the next
2373 token. */
2375 static void
2376 cp_parser_skip_to_closing_brace (cp_parser *parser)
2378 unsigned nesting_depth = 0;
2380 while (true)
2382 cp_token *token = cp_lexer_peek_token (parser->lexer);
2384 switch (token->type)
2386 case CPP_EOF:
2387 case CPP_PRAGMA_EOL:
2388 /* If we've run out of tokens, stop. */
2389 return;
2391 case CPP_CLOSE_BRACE:
2392 /* If the next token is a non-nested `}', then we have reached
2393 the end of the current block. */
2394 if (nesting_depth-- == 0)
2395 return;
2396 break;
2398 case CPP_OPEN_BRACE:
2399 /* If it the next token is a `{', then we are entering a new
2400 block. Consume the entire block. */
2401 ++nesting_depth;
2402 break;
2404 default:
2405 break;
2408 /* Consume the token. */
2409 cp_lexer_consume_token (parser->lexer);
2413 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2414 parameter is the PRAGMA token, allowing us to purge the entire pragma
2415 sequence. */
2417 static void
2418 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2420 cp_token *token;
2422 parser->lexer->in_pragma = false;
2425 token = cp_lexer_consume_token (parser->lexer);
2426 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2428 /* Ensure that the pragma is not parsed again. */
2429 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2432 /* Require pragma end of line, resyncing with it as necessary. The
2433 arguments are as for cp_parser_skip_to_pragma_eol. */
2435 static void
2436 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2438 parser->lexer->in_pragma = false;
2439 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2443 /* This is a simple wrapper around make_typename_type. When the id is
2444 an unresolved identifier node, we can provide a superior diagnostic
2445 using cp_parser_diagnose_invalid_type_name. */
2447 static tree
2448 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2450 tree result;
2451 if (TREE_CODE (id) == IDENTIFIER_NODE)
2453 result = make_typename_type (scope, id, typename_type,
2454 /*complain=*/tf_none);
2455 if (result == error_mark_node)
2456 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2457 return result;
2459 return make_typename_type (scope, id, typename_type, tf_error);
2463 /* Create a new C++ parser. */
2465 static cp_parser *
2466 cp_parser_new (void)
2468 cp_parser *parser;
2469 cp_lexer *lexer;
2470 unsigned i;
2472 /* cp_lexer_new_main is called before calling ggc_alloc because
2473 cp_lexer_new_main might load a PCH file. */
2474 lexer = cp_lexer_new_main ();
2476 /* Initialize the binops_by_token so that we can get the tree
2477 directly from the token. */
2478 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2479 binops_by_token[binops[i].token_type] = binops[i];
2481 parser = GGC_CNEW (cp_parser);
2482 parser->lexer = lexer;
2483 parser->context = cp_parser_context_new (NULL);
2485 /* For now, we always accept GNU extensions. */
2486 parser->allow_gnu_extensions_p = 1;
2488 /* The `>' token is a greater-than operator, not the end of a
2489 template-id. */
2490 parser->greater_than_is_operator_p = true;
2492 parser->default_arg_ok_p = true;
2494 /* We are not parsing a constant-expression. */
2495 parser->integral_constant_expression_p = false;
2496 parser->allow_non_integral_constant_expression_p = false;
2497 parser->non_integral_constant_expression_p = false;
2499 /* Local variable names are not forbidden. */
2500 parser->local_variables_forbidden_p = false;
2502 /* We are not processing an `extern "C"' declaration. */
2503 parser->in_unbraced_linkage_specification_p = false;
2505 /* We are not processing a declarator. */
2506 parser->in_declarator_p = false;
2508 /* We are not processing a template-argument-list. */
2509 parser->in_template_argument_list_p = false;
2511 /* We are not in an iteration statement. */
2512 parser->in_statement = 0;
2514 /* We are not in a switch statement. */
2515 parser->in_switch_statement_p = false;
2517 /* We are not parsing a type-id inside an expression. */
2518 parser->in_type_id_in_expr_p = false;
2520 /* Declarations aren't implicitly extern "C". */
2521 parser->implicit_extern_c = false;
2523 /* String literals should be translated to the execution character set. */
2524 parser->translate_strings_p = true;
2526 /* The unparsed function queue is empty. */
2527 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2529 /* There are no classes being defined. */
2530 parser->num_classes_being_defined = 0;
2532 /* No template parameters apply. */
2533 parser->num_template_parameter_lists = 0;
2535 return parser;
2538 /* Create a cp_lexer structure which will emit the tokens in CACHE
2539 and push it onto the parser's lexer stack. This is used for delayed
2540 parsing of in-class method bodies and default arguments, and should
2541 not be confused with tentative parsing. */
2542 static void
2543 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2545 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2546 lexer->next = parser->lexer;
2547 parser->lexer = lexer;
2549 /* Move the current source position to that of the first token in the
2550 new lexer. */
2551 cp_lexer_set_source_position_from_token (lexer->next_token);
2554 /* Pop the top lexer off the parser stack. This is never used for the
2555 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2556 static void
2557 cp_parser_pop_lexer (cp_parser *parser)
2559 cp_lexer *lexer = parser->lexer;
2560 parser->lexer = lexer->next;
2561 cp_lexer_destroy (lexer);
2563 /* Put the current source position back where it was before this
2564 lexer was pushed. */
2565 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2568 /* Lexical conventions [gram.lex] */
2570 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2571 identifier. */
2573 static tree
2574 cp_parser_identifier (cp_parser* parser)
2576 cp_token *token;
2578 /* Look for the identifier. */
2579 token = cp_parser_require (parser, CPP_NAME, "identifier");
2580 /* Return the value. */
2581 return token ? token->value : error_mark_node;
2584 /* Parse a sequence of adjacent string constants. Returns a
2585 TREE_STRING representing the combined, nul-terminated string
2586 constant. If TRANSLATE is true, translate the string to the
2587 execution character set. If WIDE_OK is true, a wide string is
2588 invalid here.
2590 C++98 [lex.string] says that if a narrow string literal token is
2591 adjacent to a wide string literal token, the behavior is undefined.
2592 However, C99 6.4.5p4 says that this results in a wide string literal.
2593 We follow C99 here, for consistency with the C front end.
2595 This code is largely lifted from lex_string() in c-lex.c.
2597 FUTURE: ObjC++ will need to handle @-strings here. */
2598 static tree
2599 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2601 tree value;
2602 bool wide = false;
2603 size_t count;
2604 struct obstack str_ob;
2605 cpp_string str, istr, *strs;
2606 cp_token *tok;
2608 tok = cp_lexer_peek_token (parser->lexer);
2609 if (!cp_parser_is_string_literal (tok))
2611 cp_parser_error (parser, "expected string-literal");
2612 return error_mark_node;
2615 /* Try to avoid the overhead of creating and destroying an obstack
2616 for the common case of just one string. */
2617 if (!cp_parser_is_string_literal
2618 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2620 cp_lexer_consume_token (parser->lexer);
2622 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2623 str.len = TREE_STRING_LENGTH (tok->value);
2624 count = 1;
2625 if (tok->type == CPP_WSTRING)
2626 wide = true;
2628 strs = &str;
2630 else
2632 gcc_obstack_init (&str_ob);
2633 count = 0;
2637 cp_lexer_consume_token (parser->lexer);
2638 count++;
2639 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2640 str.len = TREE_STRING_LENGTH (tok->value);
2641 if (tok->type == CPP_WSTRING)
2642 wide = true;
2644 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2646 tok = cp_lexer_peek_token (parser->lexer);
2648 while (cp_parser_is_string_literal (tok));
2650 strs = (cpp_string *) obstack_finish (&str_ob);
2653 if (wide && !wide_ok)
2655 cp_parser_error (parser, "a wide string is invalid in this context");
2656 wide = false;
2659 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2660 (parse_in, strs, count, &istr, wide))
2662 value = build_string (istr.len, (char *)istr.text);
2663 free ((void *)istr.text);
2665 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2666 value = fix_string_type (value);
2668 else
2669 /* cpp_interpret_string has issued an error. */
2670 value = error_mark_node;
2672 if (count > 1)
2673 obstack_free (&str_ob, 0);
2675 return value;
2679 /* Basic concepts [gram.basic] */
2681 /* Parse a translation-unit.
2683 translation-unit:
2684 declaration-seq [opt]
2686 Returns TRUE if all went well. */
2688 static bool
2689 cp_parser_translation_unit (cp_parser* parser)
2691 /* The address of the first non-permanent object on the declarator
2692 obstack. */
2693 static void *declarator_obstack_base;
2695 bool success;
2697 /* Create the declarator obstack, if necessary. */
2698 if (!cp_error_declarator)
2700 gcc_obstack_init (&declarator_obstack);
2701 /* Create the error declarator. */
2702 cp_error_declarator = make_declarator (cdk_error);
2703 /* Create the empty parameter list. */
2704 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2705 /* Remember where the base of the declarator obstack lies. */
2706 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2709 cp_parser_declaration_seq_opt (parser);
2711 /* If there are no tokens left then all went well. */
2712 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2714 /* Get rid of the token array; we don't need it any more. */
2715 cp_lexer_destroy (parser->lexer);
2716 parser->lexer = NULL;
2718 /* This file might have been a context that's implicitly extern
2719 "C". If so, pop the lang context. (Only relevant for PCH.) */
2720 if (parser->implicit_extern_c)
2722 pop_lang_context ();
2723 parser->implicit_extern_c = false;
2726 /* Finish up. */
2727 finish_translation_unit ();
2729 success = true;
2731 else
2733 cp_parser_error (parser, "expected declaration");
2734 success = false;
2737 /* Make sure the declarator obstack was fully cleaned up. */
2738 gcc_assert (obstack_next_free (&declarator_obstack)
2739 == declarator_obstack_base);
2741 /* All went well. */
2742 return success;
2745 /* Expressions [gram.expr] */
2747 /* Parse a primary-expression.
2749 primary-expression:
2750 literal
2751 this
2752 ( expression )
2753 id-expression
2755 GNU Extensions:
2757 primary-expression:
2758 ( compound-statement )
2759 __builtin_va_arg ( assignment-expression , type-id )
2760 __builtin_offsetof ( type-id , offsetof-expression )
2762 Objective-C++ Extension:
2764 primary-expression:
2765 objc-expression
2767 literal:
2768 __null
2770 ADDRESS_P is true iff this expression was immediately preceded by
2771 "&" and therefore might denote a pointer-to-member. CAST_P is true
2772 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2773 true iff this expression is a template argument.
2775 Returns a representation of the expression. Upon return, *IDK
2776 indicates what kind of id-expression (if any) was present. */
2778 static tree
2779 cp_parser_primary_expression (cp_parser *parser,
2780 bool address_p,
2781 bool cast_p,
2782 bool template_arg_p,
2783 cp_id_kind *idk)
2785 cp_token *token;
2787 /* Assume the primary expression is not an id-expression. */
2788 *idk = CP_ID_KIND_NONE;
2790 /* Peek at the next token. */
2791 token = cp_lexer_peek_token (parser->lexer);
2792 switch (token->type)
2794 /* literal:
2795 integer-literal
2796 character-literal
2797 floating-literal
2798 string-literal
2799 boolean-literal */
2800 case CPP_CHAR:
2801 case CPP_WCHAR:
2802 case CPP_NUMBER:
2803 token = cp_lexer_consume_token (parser->lexer);
2804 /* Floating-point literals are only allowed in an integral
2805 constant expression if they are cast to an integral or
2806 enumeration type. */
2807 if (TREE_CODE (token->value) == REAL_CST
2808 && parser->integral_constant_expression_p
2809 && pedantic)
2811 /* CAST_P will be set even in invalid code like "int(2.7 +
2812 ...)". Therefore, we have to check that the next token
2813 is sure to end the cast. */
2814 if (cast_p)
2816 cp_token *next_token;
2818 next_token = cp_lexer_peek_token (parser->lexer);
2819 if (/* The comma at the end of an
2820 enumerator-definition. */
2821 next_token->type != CPP_COMMA
2822 /* The curly brace at the end of an enum-specifier. */
2823 && next_token->type != CPP_CLOSE_BRACE
2824 /* The end of a statement. */
2825 && next_token->type != CPP_SEMICOLON
2826 /* The end of the cast-expression. */
2827 && next_token->type != CPP_CLOSE_PAREN
2828 /* The end of an array bound. */
2829 && next_token->type != CPP_CLOSE_SQUARE
2830 /* The closing ">" in a template-argument-list. */
2831 && (next_token->type != CPP_GREATER
2832 || parser->greater_than_is_operator_p))
2833 cast_p = false;
2836 /* If we are within a cast, then the constraint that the
2837 cast is to an integral or enumeration type will be
2838 checked at that point. If we are not within a cast, then
2839 this code is invalid. */
2840 if (!cast_p)
2841 cp_parser_non_integral_constant_expression
2842 (parser, "floating-point literal");
2844 return token->value;
2846 case CPP_STRING:
2847 case CPP_WSTRING:
2848 /* ??? Should wide strings be allowed when parser->translate_strings_p
2849 is false (i.e. in attributes)? If not, we can kill the third
2850 argument to cp_parser_string_literal. */
2851 return cp_parser_string_literal (parser,
2852 parser->translate_strings_p,
2853 true);
2855 case CPP_OPEN_PAREN:
2857 tree expr;
2858 bool saved_greater_than_is_operator_p;
2860 /* Consume the `('. */
2861 cp_lexer_consume_token (parser->lexer);
2862 /* Within a parenthesized expression, a `>' token is always
2863 the greater-than operator. */
2864 saved_greater_than_is_operator_p
2865 = parser->greater_than_is_operator_p;
2866 parser->greater_than_is_operator_p = true;
2867 /* If we see `( { ' then we are looking at the beginning of
2868 a GNU statement-expression. */
2869 if (cp_parser_allow_gnu_extensions_p (parser)
2870 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2872 /* Statement-expressions are not allowed by the standard. */
2873 if (pedantic)
2874 pedwarn ("ISO C++ forbids braced-groups within expressions");
2876 /* And they're not allowed outside of a function-body; you
2877 cannot, for example, write:
2879 int i = ({ int j = 3; j + 1; });
2881 at class or namespace scope. */
2882 if (!at_function_scope_p ())
2883 error ("statement-expressions are allowed only inside functions");
2884 /* Start the statement-expression. */
2885 expr = begin_stmt_expr ();
2886 /* Parse the compound-statement. */
2887 cp_parser_compound_statement (parser, expr, false);
2888 /* Finish up. */
2889 expr = finish_stmt_expr (expr, false);
2891 else
2893 /* Parse the parenthesized expression. */
2894 expr = cp_parser_expression (parser, cast_p);
2895 /* Let the front end know that this expression was
2896 enclosed in parentheses. This matters in case, for
2897 example, the expression is of the form `A::B', since
2898 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2899 not. */
2900 finish_parenthesized_expr (expr);
2902 /* The `>' token might be the end of a template-id or
2903 template-parameter-list now. */
2904 parser->greater_than_is_operator_p
2905 = saved_greater_than_is_operator_p;
2906 /* Consume the `)'. */
2907 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2908 cp_parser_skip_to_end_of_statement (parser);
2910 return expr;
2913 case CPP_KEYWORD:
2914 switch (token->keyword)
2916 /* These two are the boolean literals. */
2917 case RID_TRUE:
2918 cp_lexer_consume_token (parser->lexer);
2919 return boolean_true_node;
2920 case RID_FALSE:
2921 cp_lexer_consume_token (parser->lexer);
2922 return boolean_false_node;
2924 /* The `__null' literal. */
2925 case RID_NULL:
2926 cp_lexer_consume_token (parser->lexer);
2927 return null_node;
2929 /* Recognize the `this' keyword. */
2930 case RID_THIS:
2931 cp_lexer_consume_token (parser->lexer);
2932 if (parser->local_variables_forbidden_p)
2934 error ("%<this%> may not be used in this context");
2935 return error_mark_node;
2937 /* Pointers cannot appear in constant-expressions. */
2938 if (cp_parser_non_integral_constant_expression (parser,
2939 "`this'"))
2940 return error_mark_node;
2941 return finish_this_expr ();
2943 /* The `operator' keyword can be the beginning of an
2944 id-expression. */
2945 case RID_OPERATOR:
2946 goto id_expression;
2948 case RID_FUNCTION_NAME:
2949 case RID_PRETTY_FUNCTION_NAME:
2950 case RID_C99_FUNCTION_NAME:
2951 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2952 __func__ are the names of variables -- but they are
2953 treated specially. Therefore, they are handled here,
2954 rather than relying on the generic id-expression logic
2955 below. Grammatically, these names are id-expressions.
2957 Consume the token. */
2958 token = cp_lexer_consume_token (parser->lexer);
2959 /* Look up the name. */
2960 return finish_fname (token->value);
2962 case RID_VA_ARG:
2964 tree expression;
2965 tree type;
2967 /* The `__builtin_va_arg' construct is used to handle
2968 `va_arg'. Consume the `__builtin_va_arg' token. */
2969 cp_lexer_consume_token (parser->lexer);
2970 /* Look for the opening `('. */
2971 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2972 /* Now, parse the assignment-expression. */
2973 expression = cp_parser_assignment_expression (parser,
2974 /*cast_p=*/false);
2975 /* Look for the `,'. */
2976 cp_parser_require (parser, CPP_COMMA, "`,'");
2977 /* Parse the type-id. */
2978 type = cp_parser_type_id (parser);
2979 /* Look for the closing `)'. */
2980 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2981 /* Using `va_arg' in a constant-expression is not
2982 allowed. */
2983 if (cp_parser_non_integral_constant_expression (parser,
2984 "`va_arg'"))
2985 return error_mark_node;
2986 return build_x_va_arg (expression, type);
2989 case RID_OFFSETOF:
2990 return cp_parser_builtin_offsetof (parser);
2992 /* Objective-C++ expressions. */
2993 case RID_AT_ENCODE:
2994 case RID_AT_PROTOCOL:
2995 case RID_AT_SELECTOR:
2996 return cp_parser_objc_expression (parser);
2998 default:
2999 cp_parser_error (parser, "expected primary-expression");
3000 return error_mark_node;
3003 /* An id-expression can start with either an identifier, a
3004 `::' as the beginning of a qualified-id, or the "operator"
3005 keyword. */
3006 case CPP_NAME:
3007 case CPP_SCOPE:
3008 case CPP_TEMPLATE_ID:
3009 case CPP_NESTED_NAME_SPECIFIER:
3011 tree id_expression;
3012 tree decl;
3013 const char *error_msg;
3014 bool template_p;
3015 bool done;
3017 id_expression:
3018 /* Parse the id-expression. */
3019 id_expression
3020 = cp_parser_id_expression (parser,
3021 /*template_keyword_p=*/false,
3022 /*check_dependency_p=*/true,
3023 &template_p,
3024 /*declarator_p=*/false);
3025 if (id_expression == error_mark_node)
3026 return error_mark_node;
3027 token = cp_lexer_peek_token (parser->lexer);
3028 done = (token->type != CPP_OPEN_SQUARE
3029 && token->type != CPP_OPEN_PAREN
3030 && token->type != CPP_DOT
3031 && token->type != CPP_DEREF
3032 && token->type != CPP_PLUS_PLUS
3033 && token->type != CPP_MINUS_MINUS);
3034 /* If we have a template-id, then no further lookup is
3035 required. If the template-id was for a template-class, we
3036 will sometimes have a TYPE_DECL at this point. */
3037 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3038 || TREE_CODE (id_expression) == TYPE_DECL)
3039 decl = id_expression;
3040 /* Look up the name. */
3041 else
3043 tree ambiguous_decls;
3045 decl = cp_parser_lookup_name (parser, id_expression,
3046 none_type,
3047 template_p,
3048 /*is_namespace=*/false,
3049 /*check_dependency=*/true,
3050 &ambiguous_decls);
3051 /* If the lookup was ambiguous, an error will already have
3052 been issued. */
3053 if (ambiguous_decls)
3054 return error_mark_node;
3056 /* In Objective-C++, an instance variable (ivar) may be preferred
3057 to whatever cp_parser_lookup_name() found. */
3058 decl = objc_lookup_ivar (decl, id_expression);
3060 /* If name lookup gives us a SCOPE_REF, then the
3061 qualifying scope was dependent. */
3062 if (TREE_CODE (decl) == SCOPE_REF)
3063 return decl;
3064 /* Check to see if DECL is a local variable in a context
3065 where that is forbidden. */
3066 if (parser->local_variables_forbidden_p
3067 && local_variable_p (decl))
3069 /* It might be that we only found DECL because we are
3070 trying to be generous with pre-ISO scoping rules.
3071 For example, consider:
3073 int i;
3074 void g() {
3075 for (int i = 0; i < 10; ++i) {}
3076 extern void f(int j = i);
3079 Here, name look up will originally find the out
3080 of scope `i'. We need to issue a warning message,
3081 but then use the global `i'. */
3082 decl = check_for_out_of_scope_variable (decl);
3083 if (local_variable_p (decl))
3085 error ("local variable %qD may not appear in this context",
3086 decl);
3087 return error_mark_node;
3092 decl = (finish_id_expression
3093 (id_expression, decl, parser->scope,
3094 idk,
3095 parser->integral_constant_expression_p,
3096 parser->allow_non_integral_constant_expression_p,
3097 &parser->non_integral_constant_expression_p,
3098 template_p, done, address_p,
3099 template_arg_p,
3100 &error_msg));
3101 if (error_msg)
3102 cp_parser_error (parser, error_msg);
3103 return decl;
3106 /* Anything else is an error. */
3107 default:
3108 /* ...unless we have an Objective-C++ message or string literal, that is. */
3109 if (c_dialect_objc ()
3110 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3111 return cp_parser_objc_expression (parser);
3113 cp_parser_error (parser, "expected primary-expression");
3114 return error_mark_node;
3118 /* Parse an id-expression.
3120 id-expression:
3121 unqualified-id
3122 qualified-id
3124 qualified-id:
3125 :: [opt] nested-name-specifier template [opt] unqualified-id
3126 :: identifier
3127 :: operator-function-id
3128 :: template-id
3130 Return a representation of the unqualified portion of the
3131 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3132 a `::' or nested-name-specifier.
3134 Often, if the id-expression was a qualified-id, the caller will
3135 want to make a SCOPE_REF to represent the qualified-id. This
3136 function does not do this in order to avoid wastefully creating
3137 SCOPE_REFs when they are not required.
3139 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3140 `template' keyword.
3142 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3143 uninstantiated templates.
3145 If *TEMPLATE_P is non-NULL, it is set to true iff the
3146 `template' keyword is used to explicitly indicate that the entity
3147 named is a template.
3149 If DECLARATOR_P is true, the id-expression is appearing as part of
3150 a declarator, rather than as part of an expression. */
3152 static tree
3153 cp_parser_id_expression (cp_parser *parser,
3154 bool template_keyword_p,
3155 bool check_dependency_p,
3156 bool *template_p,
3157 bool declarator_p)
3159 bool global_scope_p;
3160 bool nested_name_specifier_p;
3162 /* Assume the `template' keyword was not used. */
3163 if (template_p)
3164 *template_p = template_keyword_p;
3166 /* Look for the optional `::' operator. */
3167 global_scope_p
3168 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3169 != NULL_TREE);
3170 /* Look for the optional nested-name-specifier. */
3171 nested_name_specifier_p
3172 = (cp_parser_nested_name_specifier_opt (parser,
3173 /*typename_keyword_p=*/false,
3174 check_dependency_p,
3175 /*type_p=*/false,
3176 declarator_p)
3177 != NULL_TREE);
3178 /* If there is a nested-name-specifier, then we are looking at
3179 the first qualified-id production. */
3180 if (nested_name_specifier_p)
3182 tree saved_scope;
3183 tree saved_object_scope;
3184 tree saved_qualifying_scope;
3185 tree unqualified_id;
3186 bool is_template;
3188 /* See if the next token is the `template' keyword. */
3189 if (!template_p)
3190 template_p = &is_template;
3191 *template_p = cp_parser_optional_template_keyword (parser);
3192 /* Name lookup we do during the processing of the
3193 unqualified-id might obliterate SCOPE. */
3194 saved_scope = parser->scope;
3195 saved_object_scope = parser->object_scope;
3196 saved_qualifying_scope = parser->qualifying_scope;
3197 /* Process the final unqualified-id. */
3198 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3199 check_dependency_p,
3200 declarator_p);
3201 /* Restore the SAVED_SCOPE for our caller. */
3202 parser->scope = saved_scope;
3203 parser->object_scope = saved_object_scope;
3204 parser->qualifying_scope = saved_qualifying_scope;
3206 return unqualified_id;
3208 /* Otherwise, if we are in global scope, then we are looking at one
3209 of the other qualified-id productions. */
3210 else if (global_scope_p)
3212 cp_token *token;
3213 tree id;
3215 /* Peek at the next token. */
3216 token = cp_lexer_peek_token (parser->lexer);
3218 /* If it's an identifier, and the next token is not a "<", then
3219 we can avoid the template-id case. This is an optimization
3220 for this common case. */
3221 if (token->type == CPP_NAME
3222 && !cp_parser_nth_token_starts_template_argument_list_p
3223 (parser, 2))
3224 return cp_parser_identifier (parser);
3226 cp_parser_parse_tentatively (parser);
3227 /* Try a template-id. */
3228 id = cp_parser_template_id (parser,
3229 /*template_keyword_p=*/false,
3230 /*check_dependency_p=*/true,
3231 declarator_p);
3232 /* If that worked, we're done. */
3233 if (cp_parser_parse_definitely (parser))
3234 return id;
3236 /* Peek at the next token. (Changes in the token buffer may
3237 have invalidated the pointer obtained above.) */
3238 token = cp_lexer_peek_token (parser->lexer);
3240 switch (token->type)
3242 case CPP_NAME:
3243 return cp_parser_identifier (parser);
3245 case CPP_KEYWORD:
3246 if (token->keyword == RID_OPERATOR)
3247 return cp_parser_operator_function_id (parser);
3248 /* Fall through. */
3250 default:
3251 cp_parser_error (parser, "expected id-expression");
3252 return error_mark_node;
3255 else
3256 return cp_parser_unqualified_id (parser, template_keyword_p,
3257 /*check_dependency_p=*/true,
3258 declarator_p);
3261 /* Parse an unqualified-id.
3263 unqualified-id:
3264 identifier
3265 operator-function-id
3266 conversion-function-id
3267 ~ class-name
3268 template-id
3270 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3271 keyword, in a construct like `A::template ...'.
3273 Returns a representation of unqualified-id. For the `identifier'
3274 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3275 production a BIT_NOT_EXPR is returned; the operand of the
3276 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3277 other productions, see the documentation accompanying the
3278 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3279 names are looked up in uninstantiated templates. If DECLARATOR_P
3280 is true, the unqualified-id is appearing as part of a declarator,
3281 rather than as part of an expression. */
3283 static tree
3284 cp_parser_unqualified_id (cp_parser* parser,
3285 bool template_keyword_p,
3286 bool check_dependency_p,
3287 bool declarator_p)
3289 cp_token *token;
3291 /* Peek at the next token. */
3292 token = cp_lexer_peek_token (parser->lexer);
3294 switch (token->type)
3296 case CPP_NAME:
3298 tree id;
3300 /* We don't know yet whether or not this will be a
3301 template-id. */
3302 cp_parser_parse_tentatively (parser);
3303 /* Try a template-id. */
3304 id = cp_parser_template_id (parser, template_keyword_p,
3305 check_dependency_p,
3306 declarator_p);
3307 /* If it worked, we're done. */
3308 if (cp_parser_parse_definitely (parser))
3309 return id;
3310 /* Otherwise, it's an ordinary identifier. */
3311 return cp_parser_identifier (parser);
3314 case CPP_TEMPLATE_ID:
3315 return cp_parser_template_id (parser, template_keyword_p,
3316 check_dependency_p,
3317 declarator_p);
3319 case CPP_COMPL:
3321 tree type_decl;
3322 tree qualifying_scope;
3323 tree object_scope;
3324 tree scope;
3325 bool done;
3327 /* Consume the `~' token. */
3328 cp_lexer_consume_token (parser->lexer);
3329 /* Parse the class-name. The standard, as written, seems to
3330 say that:
3332 template <typename T> struct S { ~S (); };
3333 template <typename T> S<T>::~S() {}
3335 is invalid, since `~' must be followed by a class-name, but
3336 `S<T>' is dependent, and so not known to be a class.
3337 That's not right; we need to look in uninstantiated
3338 templates. A further complication arises from:
3340 template <typename T> void f(T t) {
3341 t.T::~T();
3344 Here, it is not possible to look up `T' in the scope of `T'
3345 itself. We must look in both the current scope, and the
3346 scope of the containing complete expression.
3348 Yet another issue is:
3350 struct S {
3351 int S;
3352 ~S();
3355 S::~S() {}
3357 The standard does not seem to say that the `S' in `~S'
3358 should refer to the type `S' and not the data member
3359 `S::S'. */
3361 /* DR 244 says that we look up the name after the "~" in the
3362 same scope as we looked up the qualifying name. That idea
3363 isn't fully worked out; it's more complicated than that. */
3364 scope = parser->scope;
3365 object_scope = parser->object_scope;
3366 qualifying_scope = parser->qualifying_scope;
3368 /* If the name is of the form "X::~X" it's OK. */
3369 if (scope && TYPE_P (scope)
3370 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3371 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3372 == CPP_OPEN_PAREN)
3373 && (cp_lexer_peek_token (parser->lexer)->value
3374 == TYPE_IDENTIFIER (scope)))
3376 cp_lexer_consume_token (parser->lexer);
3377 return build_nt (BIT_NOT_EXPR, scope);
3380 /* If there was an explicit qualification (S::~T), first look
3381 in the scope given by the qualification (i.e., S). */
3382 done = false;
3383 type_decl = NULL_TREE;
3384 if (scope)
3386 cp_parser_parse_tentatively (parser);
3387 type_decl = cp_parser_class_name (parser,
3388 /*typename_keyword_p=*/false,
3389 /*template_keyword_p=*/false,
3390 none_type,
3391 /*check_dependency=*/false,
3392 /*class_head_p=*/false,
3393 declarator_p);
3394 if (cp_parser_parse_definitely (parser))
3395 done = true;
3397 /* In "N::S::~S", look in "N" as well. */
3398 if (!done && scope && qualifying_scope)
3400 cp_parser_parse_tentatively (parser);
3401 parser->scope = qualifying_scope;
3402 parser->object_scope = NULL_TREE;
3403 parser->qualifying_scope = NULL_TREE;
3404 type_decl
3405 = cp_parser_class_name (parser,
3406 /*typename_keyword_p=*/false,
3407 /*template_keyword_p=*/false,
3408 none_type,
3409 /*check_dependency=*/false,
3410 /*class_head_p=*/false,
3411 declarator_p);
3412 if (cp_parser_parse_definitely (parser))
3413 done = true;
3415 /* In "p->S::~T", look in the scope given by "*p" as well. */
3416 else if (!done && object_scope)
3418 cp_parser_parse_tentatively (parser);
3419 parser->scope = object_scope;
3420 parser->object_scope = NULL_TREE;
3421 parser->qualifying_scope = NULL_TREE;
3422 type_decl
3423 = cp_parser_class_name (parser,
3424 /*typename_keyword_p=*/false,
3425 /*template_keyword_p=*/false,
3426 none_type,
3427 /*check_dependency=*/false,
3428 /*class_head_p=*/false,
3429 declarator_p);
3430 if (cp_parser_parse_definitely (parser))
3431 done = true;
3433 /* Look in the surrounding context. */
3434 if (!done)
3436 parser->scope = NULL_TREE;
3437 parser->object_scope = NULL_TREE;
3438 parser->qualifying_scope = NULL_TREE;
3439 type_decl
3440 = cp_parser_class_name (parser,
3441 /*typename_keyword_p=*/false,
3442 /*template_keyword_p=*/false,
3443 none_type,
3444 /*check_dependency=*/false,
3445 /*class_head_p=*/false,
3446 declarator_p);
3448 /* If an error occurred, assume that the name of the
3449 destructor is the same as the name of the qualifying
3450 class. That allows us to keep parsing after running
3451 into ill-formed destructor names. */
3452 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3453 return build_nt (BIT_NOT_EXPR, scope);
3454 else if (type_decl == error_mark_node)
3455 return error_mark_node;
3457 /* Check that destructor name and scope match. */
3458 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3460 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3461 error ("declaration of %<~%T%> as member of %qT",
3462 type_decl, scope);
3463 return error_mark_node;
3466 /* [class.dtor]
3468 A typedef-name that names a class shall not be used as the
3469 identifier in the declarator for a destructor declaration. */
3470 if (declarator_p
3471 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3472 && !DECL_SELF_REFERENCE_P (type_decl)
3473 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3474 error ("typedef-name %qD used as destructor declarator",
3475 type_decl);
3477 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3480 case CPP_KEYWORD:
3481 if (token->keyword == RID_OPERATOR)
3483 tree id;
3485 /* This could be a template-id, so we try that first. */
3486 cp_parser_parse_tentatively (parser);
3487 /* Try a template-id. */
3488 id = cp_parser_template_id (parser, template_keyword_p,
3489 /*check_dependency_p=*/true,
3490 declarator_p);
3491 /* If that worked, we're done. */
3492 if (cp_parser_parse_definitely (parser))
3493 return id;
3494 /* We still don't know whether we're looking at an
3495 operator-function-id or a conversion-function-id. */
3496 cp_parser_parse_tentatively (parser);
3497 /* Try an operator-function-id. */
3498 id = cp_parser_operator_function_id (parser);
3499 /* If that didn't work, try a conversion-function-id. */
3500 if (!cp_parser_parse_definitely (parser))
3501 id = cp_parser_conversion_function_id (parser);
3503 return id;
3505 /* Fall through. */
3507 default:
3508 cp_parser_error (parser, "expected unqualified-id");
3509 return error_mark_node;
3513 /* Parse an (optional) nested-name-specifier.
3515 nested-name-specifier:
3516 class-or-namespace-name :: nested-name-specifier [opt]
3517 class-or-namespace-name :: template nested-name-specifier [opt]
3519 PARSER->SCOPE should be set appropriately before this function is
3520 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3521 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3522 in name lookups.
3524 Sets PARSER->SCOPE to the class (TYPE) or namespace
3525 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3526 it unchanged if there is no nested-name-specifier. Returns the new
3527 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3529 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3530 part of a declaration and/or decl-specifier. */
3532 static tree
3533 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3534 bool typename_keyword_p,
3535 bool check_dependency_p,
3536 bool type_p,
3537 bool is_declaration)
3539 bool success = false;
3540 cp_token_position start = 0;
3541 cp_token *token;
3543 /* If the next token corresponds to a nested name specifier, there
3544 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3545 false, it may have been true before, in which case something
3546 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3547 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3548 CHECK_DEPENDENCY_P is false, we have to fall through into the
3549 main loop. */
3550 if (check_dependency_p
3551 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3553 cp_parser_pre_parsed_nested_name_specifier (parser);
3554 return parser->scope;
3557 /* Remember where the nested-name-specifier starts. */
3558 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3560 start = cp_lexer_token_position (parser->lexer, false);
3561 push_deferring_access_checks (dk_deferred);
3564 while (true)
3566 tree new_scope;
3567 tree old_scope;
3568 tree saved_qualifying_scope;
3569 bool template_keyword_p;
3571 /* Spot cases that cannot be the beginning of a
3572 nested-name-specifier. */
3573 token = cp_lexer_peek_token (parser->lexer);
3575 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3576 the already parsed nested-name-specifier. */
3577 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3579 /* Grab the nested-name-specifier and continue the loop. */
3580 cp_parser_pre_parsed_nested_name_specifier (parser);
3581 success = true;
3582 continue;
3585 /* Spot cases that cannot be the beginning of a
3586 nested-name-specifier. On the second and subsequent times
3587 through the loop, we look for the `template' keyword. */
3588 if (success && token->keyword == RID_TEMPLATE)
3590 /* A template-id can start a nested-name-specifier. */
3591 else if (token->type == CPP_TEMPLATE_ID)
3593 else
3595 /* If the next token is not an identifier, then it is
3596 definitely not a class-or-namespace-name. */
3597 if (token->type != CPP_NAME)
3598 break;
3599 /* If the following token is neither a `<' (to begin a
3600 template-id), nor a `::', then we are not looking at a
3601 nested-name-specifier. */
3602 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3603 if (token->type != CPP_SCOPE
3604 && !cp_parser_nth_token_starts_template_argument_list_p
3605 (parser, 2))
3606 break;
3609 /* The nested-name-specifier is optional, so we parse
3610 tentatively. */
3611 cp_parser_parse_tentatively (parser);
3613 /* Look for the optional `template' keyword, if this isn't the
3614 first time through the loop. */
3615 if (success)
3616 template_keyword_p = cp_parser_optional_template_keyword (parser);
3617 else
3618 template_keyword_p = false;
3620 /* Save the old scope since the name lookup we are about to do
3621 might destroy it. */
3622 old_scope = parser->scope;
3623 saved_qualifying_scope = parser->qualifying_scope;
3624 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3625 look up names in "X<T>::I" in order to determine that "Y" is
3626 a template. So, if we have a typename at this point, we make
3627 an effort to look through it. */
3628 if (is_declaration
3629 && !typename_keyword_p
3630 && parser->scope
3631 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3632 parser->scope = resolve_typename_type (parser->scope,
3633 /*only_current_p=*/false);
3634 /* Parse the qualifying entity. */
3635 new_scope
3636 = cp_parser_class_or_namespace_name (parser,
3637 typename_keyword_p,
3638 template_keyword_p,
3639 check_dependency_p,
3640 type_p,
3641 is_declaration);
3642 /* Look for the `::' token. */
3643 cp_parser_require (parser, CPP_SCOPE, "`::'");
3645 /* If we found what we wanted, we keep going; otherwise, we're
3646 done. */
3647 if (!cp_parser_parse_definitely (parser))
3649 bool error_p = false;
3651 /* Restore the OLD_SCOPE since it was valid before the
3652 failed attempt at finding the last
3653 class-or-namespace-name. */
3654 parser->scope = old_scope;
3655 parser->qualifying_scope = saved_qualifying_scope;
3656 /* If the next token is an identifier, and the one after
3657 that is a `::', then any valid interpretation would have
3658 found a class-or-namespace-name. */
3659 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3660 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3661 == CPP_SCOPE)
3662 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3663 != CPP_COMPL))
3665 token = cp_lexer_consume_token (parser->lexer);
3666 if (!error_p)
3668 if (!token->ambiguous_p)
3670 tree decl;
3671 tree ambiguous_decls;
3673 decl = cp_parser_lookup_name (parser, token->value,
3674 none_type,
3675 /*is_template=*/false,
3676 /*is_namespace=*/false,
3677 /*check_dependency=*/true,
3678 &ambiguous_decls);
3679 if (TREE_CODE (decl) == TEMPLATE_DECL)
3680 error ("%qD used without template parameters", decl);
3681 else if (ambiguous_decls)
3683 error ("reference to %qD is ambiguous",
3684 token->value);
3685 print_candidates (ambiguous_decls);
3686 decl = error_mark_node;
3688 else
3689 cp_parser_name_lookup_error
3690 (parser, token->value, decl,
3691 "is not a class or namespace");
3693 parser->scope = error_mark_node;
3694 error_p = true;
3695 /* Treat this as a successful nested-name-specifier
3696 due to:
3698 [basic.lookup.qual]
3700 If the name found is not a class-name (clause
3701 _class_) or namespace-name (_namespace.def_), the
3702 program is ill-formed. */
3703 success = true;
3705 cp_lexer_consume_token (parser->lexer);
3707 break;
3709 /* We've found one valid nested-name-specifier. */
3710 success = true;
3711 /* Name lookup always gives us a DECL. */
3712 if (TREE_CODE (new_scope) == TYPE_DECL)
3713 new_scope = TREE_TYPE (new_scope);
3714 /* Uses of "template" must be followed by actual templates. */
3715 if (template_keyword_p
3716 && !(CLASS_TYPE_P (new_scope)
3717 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3718 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3719 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3720 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3721 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3722 == TEMPLATE_ID_EXPR)))
3723 pedwarn (TYPE_P (new_scope)
3724 ? "%qT is not a template"
3725 : "%qD is not a template",
3726 new_scope);
3727 /* If it is a class scope, try to complete it; we are about to
3728 be looking up names inside the class. */
3729 if (TYPE_P (new_scope)
3730 /* Since checking types for dependency can be expensive,
3731 avoid doing it if the type is already complete. */
3732 && !COMPLETE_TYPE_P (new_scope)
3733 /* Do not try to complete dependent types. */
3734 && !dependent_type_p (new_scope))
3735 new_scope = complete_type (new_scope);
3736 /* Make sure we look in the right scope the next time through
3737 the loop. */
3738 parser->scope = new_scope;
3741 /* If parsing tentatively, replace the sequence of tokens that makes
3742 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3743 token. That way, should we re-parse the token stream, we will
3744 not have to repeat the effort required to do the parse, nor will
3745 we issue duplicate error messages. */
3746 if (success && start)
3748 cp_token *token;
3749 tree access_checks;
3751 token = cp_lexer_token_at (parser->lexer, start);
3752 /* Reset the contents of the START token. */
3753 token->type = CPP_NESTED_NAME_SPECIFIER;
3754 /* Retrieve any deferred checks. Do not pop this access checks yet
3755 so the memory will not be reclaimed during token replacing below. */
3756 access_checks = get_deferred_access_checks ();
3757 token->value = build_tree_list (copy_list (access_checks),
3758 parser->scope);
3759 TREE_TYPE (token->value) = parser->qualifying_scope;
3760 token->keyword = RID_MAX;
3762 /* Purge all subsequent tokens. */
3763 cp_lexer_purge_tokens_after (parser->lexer, start);
3766 if (start)
3767 pop_to_parent_deferring_access_checks ();
3769 return success ? parser->scope : NULL_TREE;
3772 /* Parse a nested-name-specifier. See
3773 cp_parser_nested_name_specifier_opt for details. This function
3774 behaves identically, except that it will an issue an error if no
3775 nested-name-specifier is present. */
3777 static tree
3778 cp_parser_nested_name_specifier (cp_parser *parser,
3779 bool typename_keyword_p,
3780 bool check_dependency_p,
3781 bool type_p,
3782 bool is_declaration)
3784 tree scope;
3786 /* Look for the nested-name-specifier. */
3787 scope = cp_parser_nested_name_specifier_opt (parser,
3788 typename_keyword_p,
3789 check_dependency_p,
3790 type_p,
3791 is_declaration);
3792 /* If it was not present, issue an error message. */
3793 if (!scope)
3795 cp_parser_error (parser, "expected nested-name-specifier");
3796 parser->scope = NULL_TREE;
3799 return scope;
3802 /* Parse a class-or-namespace-name.
3804 class-or-namespace-name:
3805 class-name
3806 namespace-name
3808 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3809 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3810 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3811 TYPE_P is TRUE iff the next name should be taken as a class-name,
3812 even the same name is declared to be another entity in the same
3813 scope.
3815 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3816 specified by the class-or-namespace-name. If neither is found the
3817 ERROR_MARK_NODE is returned. */
3819 static tree
3820 cp_parser_class_or_namespace_name (cp_parser *parser,
3821 bool typename_keyword_p,
3822 bool template_keyword_p,
3823 bool check_dependency_p,
3824 bool type_p,
3825 bool is_declaration)
3827 tree saved_scope;
3828 tree saved_qualifying_scope;
3829 tree saved_object_scope;
3830 tree scope;
3831 bool only_class_p;
3833 /* Before we try to parse the class-name, we must save away the
3834 current PARSER->SCOPE since cp_parser_class_name will destroy
3835 it. */
3836 saved_scope = parser->scope;
3837 saved_qualifying_scope = parser->qualifying_scope;
3838 saved_object_scope = parser->object_scope;
3839 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3840 there is no need to look for a namespace-name. */
3841 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3842 if (!only_class_p)
3843 cp_parser_parse_tentatively (parser);
3844 scope = cp_parser_class_name (parser,
3845 typename_keyword_p,
3846 template_keyword_p,
3847 type_p ? class_type : none_type,
3848 check_dependency_p,
3849 /*class_head_p=*/false,
3850 is_declaration);
3851 /* If that didn't work, try for a namespace-name. */
3852 if (!only_class_p && !cp_parser_parse_definitely (parser))
3854 /* Restore the saved scope. */
3855 parser->scope = saved_scope;
3856 parser->qualifying_scope = saved_qualifying_scope;
3857 parser->object_scope = saved_object_scope;
3858 /* If we are not looking at an identifier followed by the scope
3859 resolution operator, then this is not part of a
3860 nested-name-specifier. (Note that this function is only used
3861 to parse the components of a nested-name-specifier.) */
3862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3863 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3864 return error_mark_node;
3865 scope = cp_parser_namespace_name (parser);
3868 return scope;
3871 /* Parse a postfix-expression.
3873 postfix-expression:
3874 primary-expression
3875 postfix-expression [ expression ]
3876 postfix-expression ( expression-list [opt] )
3877 simple-type-specifier ( expression-list [opt] )
3878 typename :: [opt] nested-name-specifier identifier
3879 ( expression-list [opt] )
3880 typename :: [opt] nested-name-specifier template [opt] template-id
3881 ( expression-list [opt] )
3882 postfix-expression . template [opt] id-expression
3883 postfix-expression -> template [opt] id-expression
3884 postfix-expression . pseudo-destructor-name
3885 postfix-expression -> pseudo-destructor-name
3886 postfix-expression ++
3887 postfix-expression --
3888 dynamic_cast < type-id > ( expression )
3889 static_cast < type-id > ( expression )
3890 reinterpret_cast < type-id > ( expression )
3891 const_cast < type-id > ( expression )
3892 typeid ( expression )
3893 typeid ( type-id )
3895 GNU Extension:
3897 postfix-expression:
3898 ( type-id ) { initializer-list , [opt] }
3900 This extension is a GNU version of the C99 compound-literal
3901 construct. (The C99 grammar uses `type-name' instead of `type-id',
3902 but they are essentially the same concept.)
3904 If ADDRESS_P is true, the postfix expression is the operand of the
3905 `&' operator. CAST_P is true if this expression is the target of a
3906 cast.
3908 Returns a representation of the expression. */
3910 static tree
3911 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3913 cp_token *token;
3914 enum rid keyword;
3915 cp_id_kind idk = CP_ID_KIND_NONE;
3916 tree postfix_expression = NULL_TREE;
3918 /* Peek at the next token. */
3919 token = cp_lexer_peek_token (parser->lexer);
3920 /* Some of the productions are determined by keywords. */
3921 keyword = token->keyword;
3922 switch (keyword)
3924 case RID_DYNCAST:
3925 case RID_STATCAST:
3926 case RID_REINTCAST:
3927 case RID_CONSTCAST:
3929 tree type;
3930 tree expression;
3931 const char *saved_message;
3933 /* All of these can be handled in the same way from the point
3934 of view of parsing. Begin by consuming the token
3935 identifying the cast. */
3936 cp_lexer_consume_token (parser->lexer);
3938 /* New types cannot be defined in the cast. */
3939 saved_message = parser->type_definition_forbidden_message;
3940 parser->type_definition_forbidden_message
3941 = "types may not be defined in casts";
3943 /* Look for the opening `<'. */
3944 cp_parser_require (parser, CPP_LESS, "`<'");
3945 /* Parse the type to which we are casting. */
3946 type = cp_parser_type_id (parser);
3947 /* Look for the closing `>'. */
3948 cp_parser_require (parser, CPP_GREATER, "`>'");
3949 /* Restore the old message. */
3950 parser->type_definition_forbidden_message = saved_message;
3952 /* And the expression which is being cast. */
3953 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3954 expression = cp_parser_expression (parser, /*cast_p=*/true);
3955 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3957 /* Only type conversions to integral or enumeration types
3958 can be used in constant-expressions. */
3959 if (parser->integral_constant_expression_p
3960 && !dependent_type_p (type)
3961 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3962 && (cp_parser_non_integral_constant_expression
3963 (parser,
3964 "a cast to a type other than an integral or "
3965 "enumeration type")))
3966 return error_mark_node;
3968 switch (keyword)
3970 case RID_DYNCAST:
3971 postfix_expression
3972 = build_dynamic_cast (type, expression);
3973 break;
3974 case RID_STATCAST:
3975 postfix_expression
3976 = build_static_cast (type, expression);
3977 break;
3978 case RID_REINTCAST:
3979 postfix_expression
3980 = build_reinterpret_cast (type, expression);
3981 break;
3982 case RID_CONSTCAST:
3983 postfix_expression
3984 = build_const_cast (type, expression);
3985 break;
3986 default:
3987 gcc_unreachable ();
3990 break;
3992 case RID_TYPEID:
3994 tree type;
3995 const char *saved_message;
3996 bool saved_in_type_id_in_expr_p;
3998 /* Consume the `typeid' token. */
3999 cp_lexer_consume_token (parser->lexer);
4000 /* Look for the `(' token. */
4001 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4002 /* Types cannot be defined in a `typeid' expression. */
4003 saved_message = parser->type_definition_forbidden_message;
4004 parser->type_definition_forbidden_message
4005 = "types may not be defined in a `typeid\' expression";
4006 /* We can't be sure yet whether we're looking at a type-id or an
4007 expression. */
4008 cp_parser_parse_tentatively (parser);
4009 /* Try a type-id first. */
4010 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4011 parser->in_type_id_in_expr_p = true;
4012 type = cp_parser_type_id (parser);
4013 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4014 /* Look for the `)' token. Otherwise, we can't be sure that
4015 we're not looking at an expression: consider `typeid (int
4016 (3))', for example. */
4017 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4018 /* If all went well, simply lookup the type-id. */
4019 if (cp_parser_parse_definitely (parser))
4020 postfix_expression = get_typeid (type);
4021 /* Otherwise, fall back to the expression variant. */
4022 else
4024 tree expression;
4026 /* Look for an expression. */
4027 expression = cp_parser_expression (parser, /*cast_p=*/false);
4028 /* Compute its typeid. */
4029 postfix_expression = build_typeid (expression);
4030 /* Look for the `)' token. */
4031 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4033 /* `typeid' may not appear in an integral constant expression. */
4034 if (cp_parser_non_integral_constant_expression(parser,
4035 "`typeid' operator"))
4036 return error_mark_node;
4037 /* Restore the saved message. */
4038 parser->type_definition_forbidden_message = saved_message;
4040 break;
4042 case RID_TYPENAME:
4044 tree type;
4045 /* The syntax permitted here is the same permitted for an
4046 elaborated-type-specifier. */
4047 type = cp_parser_elaborated_type_specifier (parser,
4048 /*is_friend=*/false,
4049 /*is_declaration=*/false);
4050 postfix_expression = cp_parser_functional_cast (parser, type);
4052 break;
4054 default:
4056 tree type;
4058 /* If the next thing is a simple-type-specifier, we may be
4059 looking at a functional cast. We could also be looking at
4060 an id-expression. So, we try the functional cast, and if
4061 that doesn't work we fall back to the primary-expression. */
4062 cp_parser_parse_tentatively (parser);
4063 /* Look for the simple-type-specifier. */
4064 type = cp_parser_simple_type_specifier (parser,
4065 /*decl_specs=*/NULL,
4066 CP_PARSER_FLAGS_NONE);
4067 /* Parse the cast itself. */
4068 if (!cp_parser_error_occurred (parser))
4069 postfix_expression
4070 = cp_parser_functional_cast (parser, type);
4071 /* If that worked, we're done. */
4072 if (cp_parser_parse_definitely (parser))
4073 break;
4075 /* If the functional-cast didn't work out, try a
4076 compound-literal. */
4077 if (cp_parser_allow_gnu_extensions_p (parser)
4078 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4080 VEC(constructor_elt,gc) *initializer_list = NULL;
4081 bool saved_in_type_id_in_expr_p;
4083 cp_parser_parse_tentatively (parser);
4084 /* Consume the `('. */
4085 cp_lexer_consume_token (parser->lexer);
4086 /* Parse the type. */
4087 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4088 parser->in_type_id_in_expr_p = true;
4089 type = cp_parser_type_id (parser);
4090 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4091 /* Look for the `)'. */
4092 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4093 /* Look for the `{'. */
4094 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4095 /* If things aren't going well, there's no need to
4096 keep going. */
4097 if (!cp_parser_error_occurred (parser))
4099 bool non_constant_p;
4100 /* Parse the initializer-list. */
4101 initializer_list
4102 = cp_parser_initializer_list (parser, &non_constant_p);
4103 /* Allow a trailing `,'. */
4104 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4105 cp_lexer_consume_token (parser->lexer);
4106 /* Look for the final `}'. */
4107 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4109 /* If that worked, we're definitely looking at a
4110 compound-literal expression. */
4111 if (cp_parser_parse_definitely (parser))
4113 /* Warn the user that a compound literal is not
4114 allowed in standard C++. */
4115 if (pedantic)
4116 pedwarn ("ISO C++ forbids compound-literals");
4117 /* Form the representation of the compound-literal. */
4118 postfix_expression
4119 = finish_compound_literal (type, initializer_list);
4120 break;
4124 /* It must be a primary-expression. */
4125 postfix_expression
4126 = cp_parser_primary_expression (parser, address_p, cast_p,
4127 /*template_arg_p=*/false,
4128 &idk);
4130 break;
4133 /* Keep looping until the postfix-expression is complete. */
4134 while (true)
4136 if (idk == CP_ID_KIND_UNQUALIFIED
4137 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4138 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4139 /* It is not a Koenig lookup function call. */
4140 postfix_expression
4141 = unqualified_name_lookup_error (postfix_expression);
4143 /* Peek at the next token. */
4144 token = cp_lexer_peek_token (parser->lexer);
4146 switch (token->type)
4148 case CPP_OPEN_SQUARE:
4149 postfix_expression
4150 = cp_parser_postfix_open_square_expression (parser,
4151 postfix_expression,
4152 false);
4153 idk = CP_ID_KIND_NONE;
4154 break;
4156 case CPP_OPEN_PAREN:
4157 /* postfix-expression ( expression-list [opt] ) */
4159 bool koenig_p;
4160 bool is_builtin_constant_p;
4161 bool saved_integral_constant_expression_p = false;
4162 bool saved_non_integral_constant_expression_p = false;
4163 tree args;
4165 is_builtin_constant_p
4166 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4167 if (is_builtin_constant_p)
4169 /* The whole point of __builtin_constant_p is to allow
4170 non-constant expressions to appear as arguments. */
4171 saved_integral_constant_expression_p
4172 = parser->integral_constant_expression_p;
4173 saved_non_integral_constant_expression_p
4174 = parser->non_integral_constant_expression_p;
4175 parser->integral_constant_expression_p = false;
4177 args = (cp_parser_parenthesized_expression_list
4178 (parser, /*is_attribute_list=*/false,
4179 /*cast_p=*/false,
4180 /*non_constant_p=*/NULL));
4181 if (is_builtin_constant_p)
4183 parser->integral_constant_expression_p
4184 = saved_integral_constant_expression_p;
4185 parser->non_integral_constant_expression_p
4186 = saved_non_integral_constant_expression_p;
4189 if (args == error_mark_node)
4191 postfix_expression = error_mark_node;
4192 break;
4195 /* Function calls are not permitted in
4196 constant-expressions. */
4197 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4198 && cp_parser_non_integral_constant_expression (parser,
4199 "a function call"))
4201 postfix_expression = error_mark_node;
4202 break;
4205 koenig_p = false;
4206 if (idk == CP_ID_KIND_UNQUALIFIED)
4208 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4210 if (args)
4212 koenig_p = true;
4213 postfix_expression
4214 = perform_koenig_lookup (postfix_expression, args);
4216 else
4217 postfix_expression
4218 = unqualified_fn_lookup_error (postfix_expression);
4220 /* We do not perform argument-dependent lookup if
4221 normal lookup finds a non-function, in accordance
4222 with the expected resolution of DR 218. */
4223 else if (args && is_overloaded_fn (postfix_expression))
4225 tree fn = get_first_fn (postfix_expression);
4227 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4228 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4230 /* Only do argument dependent lookup if regular
4231 lookup does not find a set of member functions.
4232 [basic.lookup.koenig]/2a */
4233 if (!DECL_FUNCTION_MEMBER_P (fn))
4235 koenig_p = true;
4236 postfix_expression
4237 = perform_koenig_lookup (postfix_expression, args);
4242 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4244 tree instance = TREE_OPERAND (postfix_expression, 0);
4245 tree fn = TREE_OPERAND (postfix_expression, 1);
4247 if (processing_template_decl
4248 && (type_dependent_expression_p (instance)
4249 || (!BASELINK_P (fn)
4250 && TREE_CODE (fn) != FIELD_DECL)
4251 || type_dependent_expression_p (fn)
4252 || any_type_dependent_arguments_p (args)))
4254 postfix_expression
4255 = build_min_nt (CALL_EXPR, postfix_expression,
4256 args, NULL_TREE);
4257 break;
4260 if (BASELINK_P (fn))
4261 postfix_expression
4262 = (build_new_method_call
4263 (instance, fn, args, NULL_TREE,
4264 (idk == CP_ID_KIND_QUALIFIED
4265 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4266 else
4267 postfix_expression
4268 = finish_call_expr (postfix_expression, args,
4269 /*disallow_virtual=*/false,
4270 /*koenig_p=*/false);
4272 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4273 || TREE_CODE (postfix_expression) == MEMBER_REF
4274 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4275 postfix_expression = (build_offset_ref_call_from_tree
4276 (postfix_expression, args));
4277 else if (idk == CP_ID_KIND_QUALIFIED)
4278 /* A call to a static class member, or a namespace-scope
4279 function. */
4280 postfix_expression
4281 = finish_call_expr (postfix_expression, args,
4282 /*disallow_virtual=*/true,
4283 koenig_p);
4284 else
4285 /* All other function calls. */
4286 postfix_expression
4287 = finish_call_expr (postfix_expression, args,
4288 /*disallow_virtual=*/false,
4289 koenig_p);
4291 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4292 idk = CP_ID_KIND_NONE;
4294 break;
4296 case CPP_DOT:
4297 case CPP_DEREF:
4298 /* postfix-expression . template [opt] id-expression
4299 postfix-expression . pseudo-destructor-name
4300 postfix-expression -> template [opt] id-expression
4301 postfix-expression -> pseudo-destructor-name */
4303 /* Consume the `.' or `->' operator. */
4304 cp_lexer_consume_token (parser->lexer);
4306 postfix_expression
4307 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4308 postfix_expression,
4309 false, &idk);
4310 break;
4312 case CPP_PLUS_PLUS:
4313 /* postfix-expression ++ */
4314 /* Consume the `++' token. */
4315 cp_lexer_consume_token (parser->lexer);
4316 /* Generate a representation for the complete expression. */
4317 postfix_expression
4318 = finish_increment_expr (postfix_expression,
4319 POSTINCREMENT_EXPR);
4320 /* Increments may not appear in constant-expressions. */
4321 if (cp_parser_non_integral_constant_expression (parser,
4322 "an increment"))
4323 postfix_expression = error_mark_node;
4324 idk = CP_ID_KIND_NONE;
4325 break;
4327 case CPP_MINUS_MINUS:
4328 /* postfix-expression -- */
4329 /* Consume the `--' token. */
4330 cp_lexer_consume_token (parser->lexer);
4331 /* Generate a representation for the complete expression. */
4332 postfix_expression
4333 = finish_increment_expr (postfix_expression,
4334 POSTDECREMENT_EXPR);
4335 /* Decrements may not appear in constant-expressions. */
4336 if (cp_parser_non_integral_constant_expression (parser,
4337 "a decrement"))
4338 postfix_expression = error_mark_node;
4339 idk = CP_ID_KIND_NONE;
4340 break;
4342 default:
4343 return postfix_expression;
4347 /* We should never get here. */
4348 gcc_unreachable ();
4349 return error_mark_node;
4352 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4353 by cp_parser_builtin_offsetof. We're looking for
4355 postfix-expression [ expression ]
4357 FOR_OFFSETOF is set if we're being called in that context, which
4358 changes how we deal with integer constant expressions. */
4360 static tree
4361 cp_parser_postfix_open_square_expression (cp_parser *parser,
4362 tree postfix_expression,
4363 bool for_offsetof)
4365 tree index;
4367 /* Consume the `[' token. */
4368 cp_lexer_consume_token (parser->lexer);
4370 /* Parse the index expression. */
4371 /* ??? For offsetof, there is a question of what to allow here. If
4372 offsetof is not being used in an integral constant expression context,
4373 then we *could* get the right answer by computing the value at runtime.
4374 If we are in an integral constant expression context, then we might
4375 could accept any constant expression; hard to say without analysis.
4376 Rather than open the barn door too wide right away, allow only integer
4377 constant expressions here. */
4378 if (for_offsetof)
4379 index = cp_parser_constant_expression (parser, false, NULL);
4380 else
4381 index = cp_parser_expression (parser, /*cast_p=*/false);
4383 /* Look for the closing `]'. */
4384 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4386 /* Build the ARRAY_REF. */
4387 postfix_expression = grok_array_decl (postfix_expression, index);
4389 /* When not doing offsetof, array references are not permitted in
4390 constant-expressions. */
4391 if (!for_offsetof
4392 && (cp_parser_non_integral_constant_expression
4393 (parser, "an array reference")))
4394 postfix_expression = error_mark_node;
4396 return postfix_expression;
4399 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4400 by cp_parser_builtin_offsetof. We're looking for
4402 postfix-expression . template [opt] id-expression
4403 postfix-expression . pseudo-destructor-name
4404 postfix-expression -> template [opt] id-expression
4405 postfix-expression -> pseudo-destructor-name
4407 FOR_OFFSETOF is set if we're being called in that context. That sorta
4408 limits what of the above we'll actually accept, but nevermind.
4409 TOKEN_TYPE is the "." or "->" token, which will already have been
4410 removed from the stream. */
4412 static tree
4413 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4414 enum cpp_ttype token_type,
4415 tree postfix_expression,
4416 bool for_offsetof, cp_id_kind *idk)
4418 tree name;
4419 bool dependent_p;
4420 bool pseudo_destructor_p;
4421 tree scope = NULL_TREE;
4423 /* If this is a `->' operator, dereference the pointer. */
4424 if (token_type == CPP_DEREF)
4425 postfix_expression = build_x_arrow (postfix_expression);
4426 /* Check to see whether or not the expression is type-dependent. */
4427 dependent_p = type_dependent_expression_p (postfix_expression);
4428 /* The identifier following the `->' or `.' is not qualified. */
4429 parser->scope = NULL_TREE;
4430 parser->qualifying_scope = NULL_TREE;
4431 parser->object_scope = NULL_TREE;
4432 *idk = CP_ID_KIND_NONE;
4433 /* Enter the scope corresponding to the type of the object
4434 given by the POSTFIX_EXPRESSION. */
4435 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4437 scope = TREE_TYPE (postfix_expression);
4438 /* According to the standard, no expression should ever have
4439 reference type. Unfortunately, we do not currently match
4440 the standard in this respect in that our internal representation
4441 of an expression may have reference type even when the standard
4442 says it does not. Therefore, we have to manually obtain the
4443 underlying type here. */
4444 scope = non_reference (scope);
4445 /* The type of the POSTFIX_EXPRESSION must be complete. */
4446 if (scope == unknown_type_node)
4448 error ("%qE does not have class type", postfix_expression);
4449 scope = NULL_TREE;
4451 else
4452 scope = complete_type_or_else (scope, NULL_TREE);
4453 /* Let the name lookup machinery know that we are processing a
4454 class member access expression. */
4455 parser->context->object_type = scope;
4456 /* If something went wrong, we want to be able to discern that case,
4457 as opposed to the case where there was no SCOPE due to the type
4458 of expression being dependent. */
4459 if (!scope)
4460 scope = error_mark_node;
4461 /* If the SCOPE was erroneous, make the various semantic analysis
4462 functions exit quickly -- and without issuing additional error
4463 messages. */
4464 if (scope == error_mark_node)
4465 postfix_expression = error_mark_node;
4468 /* Assume this expression is not a pseudo-destructor access. */
4469 pseudo_destructor_p = false;
4471 /* If the SCOPE is a scalar type, then, if this is a valid program,
4472 we must be looking at a pseudo-destructor-name. */
4473 if (scope && SCALAR_TYPE_P (scope))
4475 tree s;
4476 tree type;
4478 cp_parser_parse_tentatively (parser);
4479 /* Parse the pseudo-destructor-name. */
4480 s = NULL_TREE;
4481 cp_parser_pseudo_destructor_name (parser, &s, &type);
4482 if (cp_parser_parse_definitely (parser))
4484 pseudo_destructor_p = true;
4485 postfix_expression
4486 = finish_pseudo_destructor_expr (postfix_expression,
4487 s, TREE_TYPE (type));
4491 if (!pseudo_destructor_p)
4493 /* If the SCOPE is not a scalar type, we are looking at an
4494 ordinary class member access expression, rather than a
4495 pseudo-destructor-name. */
4496 bool template_p;
4497 /* Parse the id-expression. */
4498 name = (cp_parser_id_expression
4499 (parser,
4500 cp_parser_optional_template_keyword (parser),
4501 /*check_dependency_p=*/true,
4502 &template_p,
4503 /*declarator_p=*/false));
4504 /* In general, build a SCOPE_REF if the member name is qualified.
4505 However, if the name was not dependent and has already been
4506 resolved; there is no need to build the SCOPE_REF. For example;
4508 struct X { void f(); };
4509 template <typename T> void f(T* t) { t->X::f(); }
4511 Even though "t" is dependent, "X::f" is not and has been resolved
4512 to a BASELINK; there is no need to include scope information. */
4514 /* But we do need to remember that there was an explicit scope for
4515 virtual function calls. */
4516 if (parser->scope)
4517 *idk = CP_ID_KIND_QUALIFIED;
4519 /* If the name is a template-id that names a type, we will get a
4520 TYPE_DECL here. That is invalid code. */
4521 if (TREE_CODE (name) == TYPE_DECL)
4523 error ("invalid use of %qD", name);
4524 postfix_expression = error_mark_node;
4526 else
4528 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4530 name = build_qualified_name (/*type=*/NULL_TREE,
4531 parser->scope,
4532 name,
4533 template_p);
4534 parser->scope = NULL_TREE;
4535 parser->qualifying_scope = NULL_TREE;
4536 parser->object_scope = NULL_TREE;
4538 if (scope && name && BASELINK_P (name))
4539 adjust_result_of_qualified_name_lookup
4540 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4541 postfix_expression
4542 = finish_class_member_access_expr (postfix_expression, name,
4543 template_p);
4547 /* We no longer need to look up names in the scope of the object on
4548 the left-hand side of the `.' or `->' operator. */
4549 parser->context->object_type = NULL_TREE;
4551 /* Outside of offsetof, these operators may not appear in
4552 constant-expressions. */
4553 if (!for_offsetof
4554 && (cp_parser_non_integral_constant_expression
4555 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4556 postfix_expression = error_mark_node;
4558 return postfix_expression;
4561 /* Parse a parenthesized expression-list.
4563 expression-list:
4564 assignment-expression
4565 expression-list, assignment-expression
4567 attribute-list:
4568 expression-list
4569 identifier
4570 identifier, expression-list
4572 CAST_P is true if this expression is the target of a cast.
4574 Returns a TREE_LIST. The TREE_VALUE of each node is a
4575 representation of an assignment-expression. Note that a TREE_LIST
4576 is returned even if there is only a single expression in the list.
4577 error_mark_node is returned if the ( and or ) are
4578 missing. NULL_TREE is returned on no expressions. The parentheses
4579 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4580 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4581 indicates whether or not all of the expressions in the list were
4582 constant. */
4584 static tree
4585 cp_parser_parenthesized_expression_list (cp_parser* parser,
4586 bool is_attribute_list,
4587 bool cast_p,
4588 bool *non_constant_p)
4590 tree expression_list = NULL_TREE;
4591 bool fold_expr_p = is_attribute_list;
4592 tree identifier = NULL_TREE;
4594 /* Assume all the expressions will be constant. */
4595 if (non_constant_p)
4596 *non_constant_p = false;
4598 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4599 return error_mark_node;
4601 /* Consume expressions until there are no more. */
4602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4603 while (true)
4605 tree expr;
4607 /* At the beginning of attribute lists, check to see if the
4608 next token is an identifier. */
4609 if (is_attribute_list
4610 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4612 cp_token *token;
4614 /* Consume the identifier. */
4615 token = cp_lexer_consume_token (parser->lexer);
4616 /* Save the identifier. */
4617 identifier = token->value;
4619 else
4621 /* Parse the next assignment-expression. */
4622 if (non_constant_p)
4624 bool expr_non_constant_p;
4625 expr = (cp_parser_constant_expression
4626 (parser, /*allow_non_constant_p=*/true,
4627 &expr_non_constant_p));
4628 if (expr_non_constant_p)
4629 *non_constant_p = true;
4631 else
4632 expr = cp_parser_assignment_expression (parser, cast_p);
4634 if (fold_expr_p)
4635 expr = fold_non_dependent_expr (expr);
4637 /* Add it to the list. We add error_mark_node
4638 expressions to the list, so that we can still tell if
4639 the correct form for a parenthesized expression-list
4640 is found. That gives better errors. */
4641 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4643 if (expr == error_mark_node)
4644 goto skip_comma;
4647 /* After the first item, attribute lists look the same as
4648 expression lists. */
4649 is_attribute_list = false;
4651 get_comma:;
4652 /* If the next token isn't a `,', then we are done. */
4653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4654 break;
4656 /* Otherwise, consume the `,' and keep going. */
4657 cp_lexer_consume_token (parser->lexer);
4660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4662 int ending;
4664 skip_comma:;
4665 /* We try and resync to an unnested comma, as that will give the
4666 user better diagnostics. */
4667 ending = cp_parser_skip_to_closing_parenthesis (parser,
4668 /*recovering=*/true,
4669 /*or_comma=*/true,
4670 /*consume_paren=*/true);
4671 if (ending < 0)
4672 goto get_comma;
4673 if (!ending)
4674 return error_mark_node;
4677 /* We built up the list in reverse order so we must reverse it now. */
4678 expression_list = nreverse (expression_list);
4679 if (identifier)
4680 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4682 return expression_list;
4685 /* Parse a pseudo-destructor-name.
4687 pseudo-destructor-name:
4688 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4689 :: [opt] nested-name-specifier template template-id :: ~ type-name
4690 :: [opt] nested-name-specifier [opt] ~ type-name
4692 If either of the first two productions is used, sets *SCOPE to the
4693 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4694 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4695 or ERROR_MARK_NODE if the parse fails. */
4697 static void
4698 cp_parser_pseudo_destructor_name (cp_parser* parser,
4699 tree* scope,
4700 tree* type)
4702 bool nested_name_specifier_p;
4704 /* Assume that things will not work out. */
4705 *type = error_mark_node;
4707 /* Look for the optional `::' operator. */
4708 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4709 /* Look for the optional nested-name-specifier. */
4710 nested_name_specifier_p
4711 = (cp_parser_nested_name_specifier_opt (parser,
4712 /*typename_keyword_p=*/false,
4713 /*check_dependency_p=*/true,
4714 /*type_p=*/false,
4715 /*is_declaration=*/true)
4716 != NULL_TREE);
4717 /* Now, if we saw a nested-name-specifier, we might be doing the
4718 second production. */
4719 if (nested_name_specifier_p
4720 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4722 /* Consume the `template' keyword. */
4723 cp_lexer_consume_token (parser->lexer);
4724 /* Parse the template-id. */
4725 cp_parser_template_id (parser,
4726 /*template_keyword_p=*/true,
4727 /*check_dependency_p=*/false,
4728 /*is_declaration=*/true);
4729 /* Look for the `::' token. */
4730 cp_parser_require (parser, CPP_SCOPE, "`::'");
4732 /* If the next token is not a `~', then there might be some
4733 additional qualification. */
4734 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4736 /* Look for the type-name. */
4737 *scope = TREE_TYPE (cp_parser_type_name (parser));
4739 if (*scope == error_mark_node)
4740 return;
4742 /* If we don't have ::~, then something has gone wrong. Since
4743 the only caller of this function is looking for something
4744 after `.' or `->' after a scalar type, most likely the
4745 program is trying to get a member of a non-aggregate
4746 type. */
4747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4748 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4750 cp_parser_error (parser, "request for member of non-aggregate type");
4751 return;
4754 /* Look for the `::' token. */
4755 cp_parser_require (parser, CPP_SCOPE, "`::'");
4757 else
4758 *scope = NULL_TREE;
4760 /* Look for the `~'. */
4761 cp_parser_require (parser, CPP_COMPL, "`~'");
4762 /* Look for the type-name again. We are not responsible for
4763 checking that it matches the first type-name. */
4764 *type = cp_parser_type_name (parser);
4767 /* Parse a unary-expression.
4769 unary-expression:
4770 postfix-expression
4771 ++ cast-expression
4772 -- cast-expression
4773 unary-operator cast-expression
4774 sizeof unary-expression
4775 sizeof ( type-id )
4776 new-expression
4777 delete-expression
4779 GNU Extensions:
4781 unary-expression:
4782 __extension__ cast-expression
4783 __alignof__ unary-expression
4784 __alignof__ ( type-id )
4785 __real__ cast-expression
4786 __imag__ cast-expression
4787 && identifier
4789 ADDRESS_P is true iff the unary-expression is appearing as the
4790 operand of the `&' operator. CAST_P is true if this expression is
4791 the target of a cast.
4793 Returns a representation of the expression. */
4795 static tree
4796 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4798 cp_token *token;
4799 enum tree_code unary_operator;
4801 /* Peek at the next token. */
4802 token = cp_lexer_peek_token (parser->lexer);
4803 /* Some keywords give away the kind of expression. */
4804 if (token->type == CPP_KEYWORD)
4806 enum rid keyword = token->keyword;
4808 switch (keyword)
4810 case RID_ALIGNOF:
4811 case RID_SIZEOF:
4813 tree operand;
4814 enum tree_code op;
4816 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4817 /* Consume the token. */
4818 cp_lexer_consume_token (parser->lexer);
4819 /* Parse the operand. */
4820 operand = cp_parser_sizeof_operand (parser, keyword);
4822 if (TYPE_P (operand))
4823 return cxx_sizeof_or_alignof_type (operand, op, true);
4824 else
4825 return cxx_sizeof_or_alignof_expr (operand, op);
4828 case RID_NEW:
4829 return cp_parser_new_expression (parser);
4831 case RID_DELETE:
4832 return cp_parser_delete_expression (parser);
4834 case RID_EXTENSION:
4836 /* The saved value of the PEDANTIC flag. */
4837 int saved_pedantic;
4838 tree expr;
4840 /* Save away the PEDANTIC flag. */
4841 cp_parser_extension_opt (parser, &saved_pedantic);
4842 /* Parse the cast-expression. */
4843 expr = cp_parser_simple_cast_expression (parser);
4844 /* Restore the PEDANTIC flag. */
4845 pedantic = saved_pedantic;
4847 return expr;
4850 case RID_REALPART:
4851 case RID_IMAGPART:
4853 tree expression;
4855 /* Consume the `__real__' or `__imag__' token. */
4856 cp_lexer_consume_token (parser->lexer);
4857 /* Parse the cast-expression. */
4858 expression = cp_parser_simple_cast_expression (parser);
4859 /* Create the complete representation. */
4860 return build_x_unary_op ((keyword == RID_REALPART
4861 ? REALPART_EXPR : IMAGPART_EXPR),
4862 expression);
4864 break;
4866 default:
4867 break;
4871 /* Look for the `:: new' and `:: delete', which also signal the
4872 beginning of a new-expression, or delete-expression,
4873 respectively. If the next token is `::', then it might be one of
4874 these. */
4875 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4877 enum rid keyword;
4879 /* See if the token after the `::' is one of the keywords in
4880 which we're interested. */
4881 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4882 /* If it's `new', we have a new-expression. */
4883 if (keyword == RID_NEW)
4884 return cp_parser_new_expression (parser);
4885 /* Similarly, for `delete'. */
4886 else if (keyword == RID_DELETE)
4887 return cp_parser_delete_expression (parser);
4890 /* Look for a unary operator. */
4891 unary_operator = cp_parser_unary_operator (token);
4892 /* The `++' and `--' operators can be handled similarly, even though
4893 they are not technically unary-operators in the grammar. */
4894 if (unary_operator == ERROR_MARK)
4896 if (token->type == CPP_PLUS_PLUS)
4897 unary_operator = PREINCREMENT_EXPR;
4898 else if (token->type == CPP_MINUS_MINUS)
4899 unary_operator = PREDECREMENT_EXPR;
4900 /* Handle the GNU address-of-label extension. */
4901 else if (cp_parser_allow_gnu_extensions_p (parser)
4902 && token->type == CPP_AND_AND)
4904 tree identifier;
4906 /* Consume the '&&' token. */
4907 cp_lexer_consume_token (parser->lexer);
4908 /* Look for the identifier. */
4909 identifier = cp_parser_identifier (parser);
4910 /* Create an expression representing the address. */
4911 return finish_label_address_expr (identifier);
4914 if (unary_operator != ERROR_MARK)
4916 tree cast_expression;
4917 tree expression = error_mark_node;
4918 const char *non_constant_p = NULL;
4920 /* Consume the operator token. */
4921 token = cp_lexer_consume_token (parser->lexer);
4922 /* Parse the cast-expression. */
4923 cast_expression
4924 = cp_parser_cast_expression (parser,
4925 unary_operator == ADDR_EXPR,
4926 /*cast_p=*/false);
4927 /* Now, build an appropriate representation. */
4928 switch (unary_operator)
4930 case INDIRECT_REF:
4931 non_constant_p = "`*'";
4932 expression = build_x_indirect_ref (cast_expression, "unary *");
4933 break;
4935 case ADDR_EXPR:
4936 non_constant_p = "`&'";
4937 /* Fall through. */
4938 case BIT_NOT_EXPR:
4939 expression = build_x_unary_op (unary_operator, cast_expression);
4940 break;
4942 case PREINCREMENT_EXPR:
4943 case PREDECREMENT_EXPR:
4944 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4945 ? "`++'" : "`--'");
4946 /* Fall through. */
4947 case UNARY_PLUS_EXPR:
4948 case NEGATE_EXPR:
4949 case TRUTH_NOT_EXPR:
4950 expression = finish_unary_op_expr (unary_operator, cast_expression);
4951 break;
4953 default:
4954 gcc_unreachable ();
4957 if (non_constant_p
4958 && cp_parser_non_integral_constant_expression (parser,
4959 non_constant_p))
4960 expression = error_mark_node;
4962 return expression;
4965 return cp_parser_postfix_expression (parser, address_p, cast_p);
4968 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4969 unary-operator, the corresponding tree code is returned. */
4971 static enum tree_code
4972 cp_parser_unary_operator (cp_token* token)
4974 switch (token->type)
4976 case CPP_MULT:
4977 return INDIRECT_REF;
4979 case CPP_AND:
4980 return ADDR_EXPR;
4982 case CPP_PLUS:
4983 return UNARY_PLUS_EXPR;
4985 case CPP_MINUS:
4986 return NEGATE_EXPR;
4988 case CPP_NOT:
4989 return TRUTH_NOT_EXPR;
4991 case CPP_COMPL:
4992 return BIT_NOT_EXPR;
4994 default:
4995 return ERROR_MARK;
4999 /* Parse a new-expression.
5001 new-expression:
5002 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5003 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5005 Returns a representation of the expression. */
5007 static tree
5008 cp_parser_new_expression (cp_parser* parser)
5010 bool global_scope_p;
5011 tree placement;
5012 tree type;
5013 tree initializer;
5014 tree nelts;
5016 /* Look for the optional `::' operator. */
5017 global_scope_p
5018 = (cp_parser_global_scope_opt (parser,
5019 /*current_scope_valid_p=*/false)
5020 != NULL_TREE);
5021 /* Look for the `new' operator. */
5022 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5023 /* There's no easy way to tell a new-placement from the
5024 `( type-id )' construct. */
5025 cp_parser_parse_tentatively (parser);
5026 /* Look for a new-placement. */
5027 placement = cp_parser_new_placement (parser);
5028 /* If that didn't work out, there's no new-placement. */
5029 if (!cp_parser_parse_definitely (parser))
5030 placement = NULL_TREE;
5032 /* If the next token is a `(', then we have a parenthesized
5033 type-id. */
5034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5036 /* Consume the `('. */
5037 cp_lexer_consume_token (parser->lexer);
5038 /* Parse the type-id. */
5039 type = cp_parser_type_id (parser);
5040 /* Look for the closing `)'. */
5041 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5042 /* There should not be a direct-new-declarator in this production,
5043 but GCC used to allowed this, so we check and emit a sensible error
5044 message for this case. */
5045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5047 error ("array bound forbidden after parenthesized type-id");
5048 inform ("try removing the parentheses around the type-id");
5049 cp_parser_direct_new_declarator (parser);
5051 nelts = NULL_TREE;
5053 /* Otherwise, there must be a new-type-id. */
5054 else
5055 type = cp_parser_new_type_id (parser, &nelts);
5057 /* If the next token is a `(', then we have a new-initializer. */
5058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5059 initializer = cp_parser_new_initializer (parser);
5060 else
5061 initializer = NULL_TREE;
5063 /* A new-expression may not appear in an integral constant
5064 expression. */
5065 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5066 return error_mark_node;
5068 /* Create a representation of the new-expression. */
5069 return build_new (placement, type, nelts, initializer, global_scope_p);
5072 /* Parse a new-placement.
5074 new-placement:
5075 ( expression-list )
5077 Returns the same representation as for an expression-list. */
5079 static tree
5080 cp_parser_new_placement (cp_parser* parser)
5082 tree expression_list;
5084 /* Parse the expression-list. */
5085 expression_list = (cp_parser_parenthesized_expression_list
5086 (parser, false, /*cast_p=*/false,
5087 /*non_constant_p=*/NULL));
5089 return expression_list;
5092 /* Parse a new-type-id.
5094 new-type-id:
5095 type-specifier-seq new-declarator [opt]
5097 Returns the TYPE allocated. If the new-type-id indicates an array
5098 type, *NELTS is set to the number of elements in the last array
5099 bound; the TYPE will not include the last array bound. */
5101 static tree
5102 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5104 cp_decl_specifier_seq type_specifier_seq;
5105 cp_declarator *new_declarator;
5106 cp_declarator *declarator;
5107 cp_declarator *outer_declarator;
5108 const char *saved_message;
5109 tree type;
5111 /* The type-specifier sequence must not contain type definitions.
5112 (It cannot contain declarations of new types either, but if they
5113 are not definitions we will catch that because they are not
5114 complete.) */
5115 saved_message = parser->type_definition_forbidden_message;
5116 parser->type_definition_forbidden_message
5117 = "types may not be defined in a new-type-id";
5118 /* Parse the type-specifier-seq. */
5119 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5120 &type_specifier_seq);
5121 /* Restore the old message. */
5122 parser->type_definition_forbidden_message = saved_message;
5123 /* Parse the new-declarator. */
5124 new_declarator = cp_parser_new_declarator_opt (parser);
5126 /* Determine the number of elements in the last array dimension, if
5127 any. */
5128 *nelts = NULL_TREE;
5129 /* Skip down to the last array dimension. */
5130 declarator = new_declarator;
5131 outer_declarator = NULL;
5132 while (declarator && (declarator->kind == cdk_pointer
5133 || declarator->kind == cdk_ptrmem))
5135 outer_declarator = declarator;
5136 declarator = declarator->declarator;
5138 while (declarator
5139 && declarator->kind == cdk_array
5140 && declarator->declarator
5141 && declarator->declarator->kind == cdk_array)
5143 outer_declarator = declarator;
5144 declarator = declarator->declarator;
5147 if (declarator && declarator->kind == cdk_array)
5149 *nelts = declarator->u.array.bounds;
5150 if (*nelts == error_mark_node)
5151 *nelts = integer_one_node;
5153 if (outer_declarator)
5154 outer_declarator->declarator = declarator->declarator;
5155 else
5156 new_declarator = NULL;
5159 type = groktypename (&type_specifier_seq, new_declarator);
5160 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5162 *nelts = array_type_nelts_top (type);
5163 type = TREE_TYPE (type);
5165 return type;
5168 /* Parse an (optional) new-declarator.
5170 new-declarator:
5171 ptr-operator new-declarator [opt]
5172 direct-new-declarator
5174 Returns the declarator. */
5176 static cp_declarator *
5177 cp_parser_new_declarator_opt (cp_parser* parser)
5179 enum tree_code code;
5180 tree type;
5181 cp_cv_quals cv_quals;
5183 /* We don't know if there's a ptr-operator next, or not. */
5184 cp_parser_parse_tentatively (parser);
5185 /* Look for a ptr-operator. */
5186 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5187 /* If that worked, look for more new-declarators. */
5188 if (cp_parser_parse_definitely (parser))
5190 cp_declarator *declarator;
5192 /* Parse another optional declarator. */
5193 declarator = cp_parser_new_declarator_opt (parser);
5195 /* Create the representation of the declarator. */
5196 if (type)
5197 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5198 else if (code == INDIRECT_REF)
5199 declarator = make_pointer_declarator (cv_quals, declarator);
5200 else
5201 declarator = make_reference_declarator (cv_quals, declarator);
5203 return declarator;
5206 /* If the next token is a `[', there is a direct-new-declarator. */
5207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5208 return cp_parser_direct_new_declarator (parser);
5210 return NULL;
5213 /* Parse a direct-new-declarator.
5215 direct-new-declarator:
5216 [ expression ]
5217 direct-new-declarator [constant-expression]
5221 static cp_declarator *
5222 cp_parser_direct_new_declarator (cp_parser* parser)
5224 cp_declarator *declarator = NULL;
5226 while (true)
5228 tree expression;
5230 /* Look for the opening `['. */
5231 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5232 /* The first expression is not required to be constant. */
5233 if (!declarator)
5235 expression = cp_parser_expression (parser, /*cast_p=*/false);
5236 /* The standard requires that the expression have integral
5237 type. DR 74 adds enumeration types. We believe that the
5238 real intent is that these expressions be handled like the
5239 expression in a `switch' condition, which also allows
5240 classes with a single conversion to integral or
5241 enumeration type. */
5242 if (!processing_template_decl)
5244 expression
5245 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5246 expression,
5247 /*complain=*/true);
5248 if (!expression)
5250 error ("expression in new-declarator must have integral "
5251 "or enumeration type");
5252 expression = error_mark_node;
5256 /* But all the other expressions must be. */
5257 else
5258 expression
5259 = cp_parser_constant_expression (parser,
5260 /*allow_non_constant=*/false,
5261 NULL);
5262 /* Look for the closing `]'. */
5263 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5265 /* Add this bound to the declarator. */
5266 declarator = make_array_declarator (declarator, expression);
5268 /* If the next token is not a `[', then there are no more
5269 bounds. */
5270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5271 break;
5274 return declarator;
5277 /* Parse a new-initializer.
5279 new-initializer:
5280 ( expression-list [opt] )
5282 Returns a representation of the expression-list. If there is no
5283 expression-list, VOID_ZERO_NODE is returned. */
5285 static tree
5286 cp_parser_new_initializer (cp_parser* parser)
5288 tree expression_list;
5290 expression_list = (cp_parser_parenthesized_expression_list
5291 (parser, false, /*cast_p=*/false,
5292 /*non_constant_p=*/NULL));
5293 if (!expression_list)
5294 expression_list = void_zero_node;
5296 return expression_list;
5299 /* Parse a delete-expression.
5301 delete-expression:
5302 :: [opt] delete cast-expression
5303 :: [opt] delete [ ] cast-expression
5305 Returns a representation of the expression. */
5307 static tree
5308 cp_parser_delete_expression (cp_parser* parser)
5310 bool global_scope_p;
5311 bool array_p;
5312 tree expression;
5314 /* Look for the optional `::' operator. */
5315 global_scope_p
5316 = (cp_parser_global_scope_opt (parser,
5317 /*current_scope_valid_p=*/false)
5318 != NULL_TREE);
5319 /* Look for the `delete' keyword. */
5320 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5321 /* See if the array syntax is in use. */
5322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5324 /* Consume the `[' token. */
5325 cp_lexer_consume_token (parser->lexer);
5326 /* Look for the `]' token. */
5327 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5328 /* Remember that this is the `[]' construct. */
5329 array_p = true;
5331 else
5332 array_p = false;
5334 /* Parse the cast-expression. */
5335 expression = cp_parser_simple_cast_expression (parser);
5337 /* A delete-expression may not appear in an integral constant
5338 expression. */
5339 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5340 return error_mark_node;
5342 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5345 /* Parse a cast-expression.
5347 cast-expression:
5348 unary-expression
5349 ( type-id ) cast-expression
5351 ADDRESS_P is true iff the unary-expression is appearing as the
5352 operand of the `&' operator. CAST_P is true if this expression is
5353 the target of a cast.
5355 Returns a representation of the expression. */
5357 static tree
5358 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5360 /* If it's a `(', then we might be looking at a cast. */
5361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5363 tree type = NULL_TREE;
5364 tree expr = NULL_TREE;
5365 bool compound_literal_p;
5366 const char *saved_message;
5368 /* There's no way to know yet whether or not this is a cast.
5369 For example, `(int (3))' is a unary-expression, while `(int)
5370 3' is a cast. So, we resort to parsing tentatively. */
5371 cp_parser_parse_tentatively (parser);
5372 /* Types may not be defined in a cast. */
5373 saved_message = parser->type_definition_forbidden_message;
5374 parser->type_definition_forbidden_message
5375 = "types may not be defined in casts";
5376 /* Consume the `('. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* A very tricky bit is that `(struct S) { 3 }' is a
5379 compound-literal (which we permit in C++ as an extension).
5380 But, that construct is not a cast-expression -- it is a
5381 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5382 is legal; if the compound-literal were a cast-expression,
5383 you'd need an extra set of parentheses.) But, if we parse
5384 the type-id, and it happens to be a class-specifier, then we
5385 will commit to the parse at that point, because we cannot
5386 undo the action that is done when creating a new class. So,
5387 then we cannot back up and do a postfix-expression.
5389 Therefore, we scan ahead to the closing `)', and check to see
5390 if the token after the `)' is a `{'. If so, we are not
5391 looking at a cast-expression.
5393 Save tokens so that we can put them back. */
5394 cp_lexer_save_tokens (parser->lexer);
5395 /* Skip tokens until the next token is a closing parenthesis.
5396 If we find the closing `)', and the next token is a `{', then
5397 we are looking at a compound-literal. */
5398 compound_literal_p
5399 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5400 /*consume_paren=*/true)
5401 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5402 /* Roll back the tokens we skipped. */
5403 cp_lexer_rollback_tokens (parser->lexer);
5404 /* If we were looking at a compound-literal, simulate an error
5405 so that the call to cp_parser_parse_definitely below will
5406 fail. */
5407 if (compound_literal_p)
5408 cp_parser_simulate_error (parser);
5409 else
5411 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5412 parser->in_type_id_in_expr_p = true;
5413 /* Look for the type-id. */
5414 type = cp_parser_type_id (parser);
5415 /* Look for the closing `)'. */
5416 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5417 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5420 /* Restore the saved message. */
5421 parser->type_definition_forbidden_message = saved_message;
5423 /* If ok so far, parse the dependent expression. We cannot be
5424 sure it is a cast. Consider `(T ())'. It is a parenthesized
5425 ctor of T, but looks like a cast to function returning T
5426 without a dependent expression. */
5427 if (!cp_parser_error_occurred (parser))
5428 expr = cp_parser_cast_expression (parser,
5429 /*address_p=*/false,
5430 /*cast_p=*/true);
5432 if (cp_parser_parse_definitely (parser))
5434 /* Warn about old-style casts, if so requested. */
5435 if (warn_old_style_cast
5436 && !in_system_header
5437 && !VOID_TYPE_P (type)
5438 && current_lang_name != lang_name_c)
5439 warning (OPT_Wold_style_cast, "use of old-style cast");
5441 /* Only type conversions to integral or enumeration types
5442 can be used in constant-expressions. */
5443 if (parser->integral_constant_expression_p
5444 && !dependent_type_p (type)
5445 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5446 && (cp_parser_non_integral_constant_expression
5447 (parser,
5448 "a cast to a type other than an integral or "
5449 "enumeration type")))
5450 return error_mark_node;
5452 /* Perform the cast. */
5453 expr = build_c_cast (type, expr);
5454 return expr;
5458 /* If we get here, then it's not a cast, so it must be a
5459 unary-expression. */
5460 return cp_parser_unary_expression (parser, address_p, cast_p);
5463 /* Parse a binary expression of the general form:
5465 pm-expression:
5466 cast-expression
5467 pm-expression .* cast-expression
5468 pm-expression ->* cast-expression
5470 multiplicative-expression:
5471 pm-expression
5472 multiplicative-expression * pm-expression
5473 multiplicative-expression / pm-expression
5474 multiplicative-expression % pm-expression
5476 additive-expression:
5477 multiplicative-expression
5478 additive-expression + multiplicative-expression
5479 additive-expression - multiplicative-expression
5481 shift-expression:
5482 additive-expression
5483 shift-expression << additive-expression
5484 shift-expression >> additive-expression
5486 relational-expression:
5487 shift-expression
5488 relational-expression < shift-expression
5489 relational-expression > shift-expression
5490 relational-expression <= shift-expression
5491 relational-expression >= shift-expression
5493 GNU Extension:
5495 relational-expression:
5496 relational-expression <? shift-expression
5497 relational-expression >? shift-expression
5499 equality-expression:
5500 relational-expression
5501 equality-expression == relational-expression
5502 equality-expression != relational-expression
5504 and-expression:
5505 equality-expression
5506 and-expression & equality-expression
5508 exclusive-or-expression:
5509 and-expression
5510 exclusive-or-expression ^ and-expression
5512 inclusive-or-expression:
5513 exclusive-or-expression
5514 inclusive-or-expression | exclusive-or-expression
5516 logical-and-expression:
5517 inclusive-or-expression
5518 logical-and-expression && inclusive-or-expression
5520 logical-or-expression:
5521 logical-and-expression
5522 logical-or-expression || logical-and-expression
5524 All these are implemented with a single function like:
5526 binary-expression:
5527 simple-cast-expression
5528 binary-expression <token> binary-expression
5530 CAST_P is true if this expression is the target of a cast.
5532 The binops_by_token map is used to get the tree codes for each <token> type.
5533 binary-expressions are associated according to a precedence table. */
5535 #define TOKEN_PRECEDENCE(token) \
5536 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5537 ? PREC_NOT_OPERATOR \
5538 : binops_by_token[token->type].prec)
5540 static tree
5541 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5543 cp_parser_expression_stack stack;
5544 cp_parser_expression_stack_entry *sp = &stack[0];
5545 tree lhs, rhs;
5546 cp_token *token;
5547 enum tree_code tree_type;
5548 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5549 bool overloaded_p;
5551 /* Parse the first expression. */
5552 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5554 for (;;)
5556 /* Get an operator token. */
5557 token = cp_lexer_peek_token (parser->lexer);
5558 if (token->type == CPP_MIN || token->type == CPP_MAX)
5559 cp_parser_warn_min_max ();
5561 new_prec = TOKEN_PRECEDENCE (token);
5563 /* Popping an entry off the stack means we completed a subexpression:
5564 - either we found a token which is not an operator (`>' where it is not
5565 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5566 will happen repeatedly;
5567 - or, we found an operator which has lower priority. This is the case
5568 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5569 parsing `3 * 4'. */
5570 if (new_prec <= prec)
5572 if (sp == stack)
5573 break;
5574 else
5575 goto pop;
5578 get_rhs:
5579 tree_type = binops_by_token[token->type].tree_type;
5581 /* We used the operator token. */
5582 cp_lexer_consume_token (parser->lexer);
5584 /* Extract another operand. It may be the RHS of this expression
5585 or the LHS of a new, higher priority expression. */
5586 rhs = cp_parser_simple_cast_expression (parser);
5588 /* Get another operator token. Look up its precedence to avoid
5589 building a useless (immediately popped) stack entry for common
5590 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5591 token = cp_lexer_peek_token (parser->lexer);
5592 lookahead_prec = TOKEN_PRECEDENCE (token);
5593 if (lookahead_prec > new_prec)
5595 /* ... and prepare to parse the RHS of the new, higher priority
5596 expression. Since precedence levels on the stack are
5597 monotonically increasing, we do not have to care about
5598 stack overflows. */
5599 sp->prec = prec;
5600 sp->tree_type = tree_type;
5601 sp->lhs = lhs;
5602 sp++;
5603 lhs = rhs;
5604 prec = new_prec;
5605 new_prec = lookahead_prec;
5606 goto get_rhs;
5608 pop:
5609 /* If the stack is not empty, we have parsed into LHS the right side
5610 (`4' in the example above) of an expression we had suspended.
5611 We can use the information on the stack to recover the LHS (`3')
5612 from the stack together with the tree code (`MULT_EXPR'), and
5613 the precedence of the higher level subexpression
5614 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5615 which will be used to actually build the additive expression. */
5616 --sp;
5617 prec = sp->prec;
5618 tree_type = sp->tree_type;
5619 rhs = lhs;
5620 lhs = sp->lhs;
5623 overloaded_p = false;
5624 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5626 /* If the binary operator required the use of an overloaded operator,
5627 then this expression cannot be an integral constant-expression.
5628 An overloaded operator can be used even if both operands are
5629 otherwise permissible in an integral constant-expression if at
5630 least one of the operands is of enumeration type. */
5632 if (overloaded_p
5633 && (cp_parser_non_integral_constant_expression
5634 (parser, "calls to overloaded operators")))
5635 return error_mark_node;
5638 return lhs;
5642 /* Parse the `? expression : assignment-expression' part of a
5643 conditional-expression. The LOGICAL_OR_EXPR is the
5644 logical-or-expression that started the conditional-expression.
5645 Returns a representation of the entire conditional-expression.
5647 This routine is used by cp_parser_assignment_expression.
5649 ? expression : assignment-expression
5651 GNU Extensions:
5653 ? : assignment-expression */
5655 static tree
5656 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5658 tree expr;
5659 tree assignment_expr;
5661 /* Consume the `?' token. */
5662 cp_lexer_consume_token (parser->lexer);
5663 if (cp_parser_allow_gnu_extensions_p (parser)
5664 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5665 /* Implicit true clause. */
5666 expr = NULL_TREE;
5667 else
5668 /* Parse the expression. */
5669 expr = cp_parser_expression (parser, /*cast_p=*/false);
5671 /* The next token should be a `:'. */
5672 cp_parser_require (parser, CPP_COLON, "`:'");
5673 /* Parse the assignment-expression. */
5674 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5676 /* Build the conditional-expression. */
5677 return build_x_conditional_expr (logical_or_expr,
5678 expr,
5679 assignment_expr);
5682 /* Parse an assignment-expression.
5684 assignment-expression:
5685 conditional-expression
5686 logical-or-expression assignment-operator assignment_expression
5687 throw-expression
5689 CAST_P is true if this expression is the target of a cast.
5691 Returns a representation for the expression. */
5693 static tree
5694 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5696 tree expr;
5698 /* If the next token is the `throw' keyword, then we're looking at
5699 a throw-expression. */
5700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5701 expr = cp_parser_throw_expression (parser);
5702 /* Otherwise, it must be that we are looking at a
5703 logical-or-expression. */
5704 else
5706 /* Parse the binary expressions (logical-or-expression). */
5707 expr = cp_parser_binary_expression (parser, cast_p);
5708 /* If the next token is a `?' then we're actually looking at a
5709 conditional-expression. */
5710 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5711 return cp_parser_question_colon_clause (parser, expr);
5712 else
5714 enum tree_code assignment_operator;
5716 /* If it's an assignment-operator, we're using the second
5717 production. */
5718 assignment_operator
5719 = cp_parser_assignment_operator_opt (parser);
5720 if (assignment_operator != ERROR_MARK)
5722 tree rhs;
5724 /* Parse the right-hand side of the assignment. */
5725 rhs = cp_parser_assignment_expression (parser, cast_p);
5726 /* An assignment may not appear in a
5727 constant-expression. */
5728 if (cp_parser_non_integral_constant_expression (parser,
5729 "an assignment"))
5730 return error_mark_node;
5731 /* Build the assignment expression. */
5732 expr = build_x_modify_expr (expr,
5733 assignment_operator,
5734 rhs);
5739 return expr;
5742 /* Parse an (optional) assignment-operator.
5744 assignment-operator: one of
5745 = *= /= %= += -= >>= <<= &= ^= |=
5747 GNU Extension:
5749 assignment-operator: one of
5750 <?= >?=
5752 If the next token is an assignment operator, the corresponding tree
5753 code is returned, and the token is consumed. For example, for
5754 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5755 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5756 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5757 operator, ERROR_MARK is returned. */
5759 static enum tree_code
5760 cp_parser_assignment_operator_opt (cp_parser* parser)
5762 enum tree_code op;
5763 cp_token *token;
5765 /* Peek at the next toen. */
5766 token = cp_lexer_peek_token (parser->lexer);
5768 switch (token->type)
5770 case CPP_EQ:
5771 op = NOP_EXPR;
5772 break;
5774 case CPP_MULT_EQ:
5775 op = MULT_EXPR;
5776 break;
5778 case CPP_DIV_EQ:
5779 op = TRUNC_DIV_EXPR;
5780 break;
5782 case CPP_MOD_EQ:
5783 op = TRUNC_MOD_EXPR;
5784 break;
5786 case CPP_PLUS_EQ:
5787 op = PLUS_EXPR;
5788 break;
5790 case CPP_MINUS_EQ:
5791 op = MINUS_EXPR;
5792 break;
5794 case CPP_RSHIFT_EQ:
5795 op = RSHIFT_EXPR;
5796 break;
5798 case CPP_LSHIFT_EQ:
5799 op = LSHIFT_EXPR;
5800 break;
5802 case CPP_AND_EQ:
5803 op = BIT_AND_EXPR;
5804 break;
5806 case CPP_XOR_EQ:
5807 op = BIT_XOR_EXPR;
5808 break;
5810 case CPP_OR_EQ:
5811 op = BIT_IOR_EXPR;
5812 break;
5814 case CPP_MIN_EQ:
5815 op = MIN_EXPR;
5816 cp_parser_warn_min_max ();
5817 break;
5819 case CPP_MAX_EQ:
5820 op = MAX_EXPR;
5821 cp_parser_warn_min_max ();
5822 break;
5824 default:
5825 /* Nothing else is an assignment operator. */
5826 op = ERROR_MARK;
5829 /* If it was an assignment operator, consume it. */
5830 if (op != ERROR_MARK)
5831 cp_lexer_consume_token (parser->lexer);
5833 return op;
5836 /* Parse an expression.
5838 expression:
5839 assignment-expression
5840 expression , assignment-expression
5842 CAST_P is true if this expression is the target of a cast.
5844 Returns a representation of the expression. */
5846 static tree
5847 cp_parser_expression (cp_parser* parser, bool cast_p)
5849 tree expression = NULL_TREE;
5851 while (true)
5853 tree assignment_expression;
5855 /* Parse the next assignment-expression. */
5856 assignment_expression
5857 = cp_parser_assignment_expression (parser, cast_p);
5858 /* If this is the first assignment-expression, we can just
5859 save it away. */
5860 if (!expression)
5861 expression = assignment_expression;
5862 else
5863 expression = build_x_compound_expr (expression,
5864 assignment_expression);
5865 /* If the next token is not a comma, then we are done with the
5866 expression. */
5867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5868 break;
5869 /* Consume the `,'. */
5870 cp_lexer_consume_token (parser->lexer);
5871 /* A comma operator cannot appear in a constant-expression. */
5872 if (cp_parser_non_integral_constant_expression (parser,
5873 "a comma operator"))
5874 expression = error_mark_node;
5877 return expression;
5880 /* Parse a constant-expression.
5882 constant-expression:
5883 conditional-expression
5885 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5886 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5887 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5888 is false, NON_CONSTANT_P should be NULL. */
5890 static tree
5891 cp_parser_constant_expression (cp_parser* parser,
5892 bool allow_non_constant_p,
5893 bool *non_constant_p)
5895 bool saved_integral_constant_expression_p;
5896 bool saved_allow_non_integral_constant_expression_p;
5897 bool saved_non_integral_constant_expression_p;
5898 tree expression;
5900 /* It might seem that we could simply parse the
5901 conditional-expression, and then check to see if it were
5902 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5903 one that the compiler can figure out is constant, possibly after
5904 doing some simplifications or optimizations. The standard has a
5905 precise definition of constant-expression, and we must honor
5906 that, even though it is somewhat more restrictive.
5908 For example:
5910 int i[(2, 3)];
5912 is not a legal declaration, because `(2, 3)' is not a
5913 constant-expression. The `,' operator is forbidden in a
5914 constant-expression. However, GCC's constant-folding machinery
5915 will fold this operation to an INTEGER_CST for `3'. */
5917 /* Save the old settings. */
5918 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5919 saved_allow_non_integral_constant_expression_p
5920 = parser->allow_non_integral_constant_expression_p;
5921 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5922 /* We are now parsing a constant-expression. */
5923 parser->integral_constant_expression_p = true;
5924 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5925 parser->non_integral_constant_expression_p = false;
5926 /* Although the grammar says "conditional-expression", we parse an
5927 "assignment-expression", which also permits "throw-expression"
5928 and the use of assignment operators. In the case that
5929 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5930 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5931 actually essential that we look for an assignment-expression.
5932 For example, cp_parser_initializer_clauses uses this function to
5933 determine whether a particular assignment-expression is in fact
5934 constant. */
5935 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5936 /* Restore the old settings. */
5937 parser->integral_constant_expression_p
5938 = saved_integral_constant_expression_p;
5939 parser->allow_non_integral_constant_expression_p
5940 = saved_allow_non_integral_constant_expression_p;
5941 if (allow_non_constant_p)
5942 *non_constant_p = parser->non_integral_constant_expression_p;
5943 else if (parser->non_integral_constant_expression_p)
5944 expression = error_mark_node;
5945 parser->non_integral_constant_expression_p
5946 = saved_non_integral_constant_expression_p;
5948 return expression;
5951 /* Parse __builtin_offsetof.
5953 offsetof-expression:
5954 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5956 offsetof-member-designator:
5957 id-expression
5958 | offsetof-member-designator "." id-expression
5959 | offsetof-member-designator "[" expression "]"
5962 static tree
5963 cp_parser_builtin_offsetof (cp_parser *parser)
5965 int save_ice_p, save_non_ice_p;
5966 tree type, expr;
5967 cp_id_kind dummy;
5969 /* We're about to accept non-integral-constant things, but will
5970 definitely yield an integral constant expression. Save and
5971 restore these values around our local parsing. */
5972 save_ice_p = parser->integral_constant_expression_p;
5973 save_non_ice_p = parser->non_integral_constant_expression_p;
5975 /* Consume the "__builtin_offsetof" token. */
5976 cp_lexer_consume_token (parser->lexer);
5977 /* Consume the opening `('. */
5978 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5979 /* Parse the type-id. */
5980 type = cp_parser_type_id (parser);
5981 /* Look for the `,'. */
5982 cp_parser_require (parser, CPP_COMMA, "`,'");
5984 /* Build the (type *)null that begins the traditional offsetof macro. */
5985 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5987 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5988 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5989 true, &dummy);
5990 while (true)
5992 cp_token *token = cp_lexer_peek_token (parser->lexer);
5993 switch (token->type)
5995 case CPP_OPEN_SQUARE:
5996 /* offsetof-member-designator "[" expression "]" */
5997 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5998 break;
6000 case CPP_DOT:
6001 /* offsetof-member-designator "." identifier */
6002 cp_lexer_consume_token (parser->lexer);
6003 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6004 true, &dummy);
6005 break;
6007 case CPP_CLOSE_PAREN:
6008 /* Consume the ")" token. */
6009 cp_lexer_consume_token (parser->lexer);
6010 goto success;
6012 default:
6013 /* Error. We know the following require will fail, but
6014 that gives the proper error message. */
6015 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6016 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6017 expr = error_mark_node;
6018 goto failure;
6022 success:
6023 /* If we're processing a template, we can't finish the semantics yet.
6024 Otherwise we can fold the entire expression now. */
6025 if (processing_template_decl)
6026 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6027 else
6028 expr = fold_offsetof (expr);
6030 failure:
6031 parser->integral_constant_expression_p = save_ice_p;
6032 parser->non_integral_constant_expression_p = save_non_ice_p;
6034 return expr;
6037 /* Statements [gram.stmt.stmt] */
6039 /* Parse a statement.
6041 statement:
6042 labeled-statement
6043 expression-statement
6044 compound-statement
6045 selection-statement
6046 iteration-statement
6047 jump-statement
6048 declaration-statement
6049 try-block
6051 IN_COMPOUND is true when the statement is nested inside a
6052 cp_parser_compound_statement; this matters for certain pragmas. */
6054 static void
6055 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6056 bool in_compound)
6058 tree statement;
6059 cp_token *token;
6060 location_t statement_location;
6062 restart:
6063 /* There is no statement yet. */
6064 statement = NULL_TREE;
6065 /* Peek at the next token. */
6066 token = cp_lexer_peek_token (parser->lexer);
6067 /* Remember the location of the first token in the statement. */
6068 statement_location = token->location;
6069 /* If this is a keyword, then that will often determine what kind of
6070 statement we have. */
6071 if (token->type == CPP_KEYWORD)
6073 enum rid keyword = token->keyword;
6075 switch (keyword)
6077 case RID_CASE:
6078 case RID_DEFAULT:
6079 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6080 in_compound);
6081 break;
6083 case RID_IF:
6084 case RID_SWITCH:
6085 statement = cp_parser_selection_statement (parser);
6086 break;
6088 case RID_WHILE:
6089 case RID_DO:
6090 case RID_FOR:
6091 statement = cp_parser_iteration_statement (parser);
6092 break;
6094 case RID_BREAK:
6095 case RID_CONTINUE:
6096 case RID_RETURN:
6097 case RID_GOTO:
6098 statement = cp_parser_jump_statement (parser);
6099 break;
6101 /* Objective-C++ exception-handling constructs. */
6102 case RID_AT_TRY:
6103 case RID_AT_CATCH:
6104 case RID_AT_FINALLY:
6105 case RID_AT_SYNCHRONIZED:
6106 case RID_AT_THROW:
6107 statement = cp_parser_objc_statement (parser);
6108 break;
6110 case RID_TRY:
6111 statement = cp_parser_try_block (parser);
6112 break;
6114 default:
6115 /* It might be a keyword like `int' that can start a
6116 declaration-statement. */
6117 break;
6120 else if (token->type == CPP_NAME)
6122 /* If the next token is a `:', then we are looking at a
6123 labeled-statement. */
6124 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6125 if (token->type == CPP_COLON)
6126 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6127 in_compound);
6129 /* Anything that starts with a `{' must be a compound-statement. */
6130 else if (token->type == CPP_OPEN_BRACE)
6131 statement = cp_parser_compound_statement (parser, NULL, false);
6132 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6133 a statement all its own. */
6134 else if (token->type == CPP_PRAGMA)
6136 /* Only certain OpenMP pragmas are attached to statements, and thus
6137 are considered statements themselves. All others are not. In
6138 the context of a compound, accept the pragma as a "statement" and
6139 return so that we can check for a close brace. Otherwise we
6140 require a real statement and must go back and read one. */
6141 if (in_compound)
6142 cp_parser_pragma (parser, pragma_compound);
6143 else if (!cp_parser_pragma (parser, pragma_stmt))
6144 goto restart;
6145 return;
6147 else if (token->type == CPP_EOF)
6149 cp_parser_error (parser, "expected statement");
6150 return;
6153 /* Everything else must be a declaration-statement or an
6154 expression-statement. Try for the declaration-statement
6155 first, unless we are looking at a `;', in which case we know that
6156 we have an expression-statement. */
6157 if (!statement)
6159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6161 cp_parser_parse_tentatively (parser);
6162 /* Try to parse the declaration-statement. */
6163 cp_parser_declaration_statement (parser);
6164 /* If that worked, we're done. */
6165 if (cp_parser_parse_definitely (parser))
6166 return;
6168 /* Look for an expression-statement instead. */
6169 statement = cp_parser_expression_statement (parser, in_statement_expr);
6172 /* Set the line number for the statement. */
6173 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6174 SET_EXPR_LOCATION (statement, statement_location);
6177 /* Parse a labeled-statement.
6179 labeled-statement:
6180 identifier : statement
6181 case constant-expression : statement
6182 default : statement
6184 GNU Extension:
6186 labeled-statement:
6187 case constant-expression ... constant-expression : statement
6189 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6190 For an ordinary label, returns a LABEL_EXPR.
6192 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6193 inside a compound. */
6195 static tree
6196 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6197 bool in_compound)
6199 cp_token *token;
6200 tree statement = error_mark_node;
6202 /* The next token should be an identifier. */
6203 token = cp_lexer_peek_token (parser->lexer);
6204 if (token->type != CPP_NAME
6205 && token->type != CPP_KEYWORD)
6207 cp_parser_error (parser, "expected labeled-statement");
6208 return error_mark_node;
6211 switch (token->keyword)
6213 case RID_CASE:
6215 tree expr, expr_hi;
6216 cp_token *ellipsis;
6218 /* Consume the `case' token. */
6219 cp_lexer_consume_token (parser->lexer);
6220 /* Parse the constant-expression. */
6221 expr = cp_parser_constant_expression (parser,
6222 /*allow_non_constant_p=*/false,
6223 NULL);
6225 ellipsis = cp_lexer_peek_token (parser->lexer);
6226 if (ellipsis->type == CPP_ELLIPSIS)
6228 /* Consume the `...' token. */
6229 cp_lexer_consume_token (parser->lexer);
6230 expr_hi =
6231 cp_parser_constant_expression (parser,
6232 /*allow_non_constant_p=*/false,
6233 NULL);
6234 /* We don't need to emit warnings here, as the common code
6235 will do this for us. */
6237 else
6238 expr_hi = NULL_TREE;
6240 if (parser->in_switch_statement_p)
6241 statement = finish_case_label (expr, expr_hi);
6242 else
6243 error ("case label %qE not within a switch statement", expr);
6245 break;
6247 case RID_DEFAULT:
6248 /* Consume the `default' token. */
6249 cp_lexer_consume_token (parser->lexer);
6251 if (parser->in_switch_statement_p)
6252 statement = finish_case_label (NULL_TREE, NULL_TREE);
6253 else
6254 error ("case label not within a switch statement");
6255 break;
6257 default:
6258 /* Anything else must be an ordinary label. */
6259 statement = finish_label_stmt (cp_parser_identifier (parser));
6260 break;
6263 /* Require the `:' token. */
6264 cp_parser_require (parser, CPP_COLON, "`:'");
6265 /* Parse the labeled statement. */
6266 cp_parser_statement (parser, in_statement_expr, in_compound);
6268 /* Return the label, in the case of a `case' or `default' label. */
6269 return statement;
6272 /* Parse an expression-statement.
6274 expression-statement:
6275 expression [opt] ;
6277 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6278 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6279 indicates whether this expression-statement is part of an
6280 expression statement. */
6282 static tree
6283 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6285 tree statement = NULL_TREE;
6287 /* If the next token is a ';', then there is no expression
6288 statement. */
6289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6290 statement = cp_parser_expression (parser, /*cast_p=*/false);
6292 /* Consume the final `;'. */
6293 cp_parser_consume_semicolon_at_end_of_statement (parser);
6295 if (in_statement_expr
6296 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6297 /* This is the final expression statement of a statement
6298 expression. */
6299 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6300 else if (statement)
6301 statement = finish_expr_stmt (statement);
6302 else
6303 finish_stmt ();
6305 return statement;
6308 /* Parse a compound-statement.
6310 compound-statement:
6311 { statement-seq [opt] }
6313 Returns a tree representing the statement. */
6315 static tree
6316 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6317 bool in_try)
6319 tree compound_stmt;
6321 /* Consume the `{'. */
6322 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6323 return error_mark_node;
6324 /* Begin the compound-statement. */
6325 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6326 /* Parse an (optional) statement-seq. */
6327 cp_parser_statement_seq_opt (parser, in_statement_expr);
6328 /* Finish the compound-statement. */
6329 finish_compound_stmt (compound_stmt);
6330 /* Consume the `}'. */
6331 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6333 return compound_stmt;
6336 /* Parse an (optional) statement-seq.
6338 statement-seq:
6339 statement
6340 statement-seq [opt] statement */
6342 static void
6343 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6345 /* Scan statements until there aren't any more. */
6346 while (true)
6348 cp_token *token = cp_lexer_peek_token (parser->lexer);
6350 /* If we're looking at a `}', then we've run out of statements. */
6351 if (token->type == CPP_CLOSE_BRACE
6352 || token->type == CPP_EOF
6353 || token->type == CPP_PRAGMA_EOL)
6354 break;
6356 /* Parse the statement. */
6357 cp_parser_statement (parser, in_statement_expr, true);
6361 /* Parse a selection-statement.
6363 selection-statement:
6364 if ( condition ) statement
6365 if ( condition ) statement else statement
6366 switch ( condition ) statement
6368 Returns the new IF_STMT or SWITCH_STMT. */
6370 static tree
6371 cp_parser_selection_statement (cp_parser* parser)
6373 cp_token *token;
6374 enum rid keyword;
6376 /* Peek at the next token. */
6377 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6379 /* See what kind of keyword it is. */
6380 keyword = token->keyword;
6381 switch (keyword)
6383 case RID_IF:
6384 case RID_SWITCH:
6386 tree statement;
6387 tree condition;
6389 /* Look for the `('. */
6390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6392 cp_parser_skip_to_end_of_statement (parser);
6393 return error_mark_node;
6396 /* Begin the selection-statement. */
6397 if (keyword == RID_IF)
6398 statement = begin_if_stmt ();
6399 else
6400 statement = begin_switch_stmt ();
6402 /* Parse the condition. */
6403 condition = cp_parser_condition (parser);
6404 /* Look for the `)'. */
6405 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6406 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6407 /*consume_paren=*/true);
6409 if (keyword == RID_IF)
6411 /* Add the condition. */
6412 finish_if_stmt_cond (condition, statement);
6414 /* Parse the then-clause. */
6415 cp_parser_implicitly_scoped_statement (parser);
6416 finish_then_clause (statement);
6418 /* If the next token is `else', parse the else-clause. */
6419 if (cp_lexer_next_token_is_keyword (parser->lexer,
6420 RID_ELSE))
6422 /* Consume the `else' keyword. */
6423 cp_lexer_consume_token (parser->lexer);
6424 begin_else_clause (statement);
6425 /* Parse the else-clause. */
6426 cp_parser_implicitly_scoped_statement (parser);
6427 finish_else_clause (statement);
6430 /* Now we're all done with the if-statement. */
6431 finish_if_stmt (statement);
6433 else
6435 bool in_switch_statement_p;
6436 unsigned char in_statement;
6438 /* Add the condition. */
6439 finish_switch_cond (condition, statement);
6441 /* Parse the body of the switch-statement. */
6442 in_switch_statement_p = parser->in_switch_statement_p;
6443 in_statement = parser->in_statement;
6444 parser->in_switch_statement_p = true;
6445 parser->in_statement |= IN_SWITCH_STMT;
6446 cp_parser_implicitly_scoped_statement (parser);
6447 parser->in_switch_statement_p = in_switch_statement_p;
6448 parser->in_statement = in_statement;
6450 /* Now we're all done with the switch-statement. */
6451 finish_switch_stmt (statement);
6454 return statement;
6456 break;
6458 default:
6459 cp_parser_error (parser, "expected selection-statement");
6460 return error_mark_node;
6464 /* Parse a condition.
6466 condition:
6467 expression
6468 type-specifier-seq declarator = assignment-expression
6470 GNU Extension:
6472 condition:
6473 type-specifier-seq declarator asm-specification [opt]
6474 attributes [opt] = assignment-expression
6476 Returns the expression that should be tested. */
6478 static tree
6479 cp_parser_condition (cp_parser* parser)
6481 cp_decl_specifier_seq type_specifiers;
6482 const char *saved_message;
6484 /* Try the declaration first. */
6485 cp_parser_parse_tentatively (parser);
6486 /* New types are not allowed in the type-specifier-seq for a
6487 condition. */
6488 saved_message = parser->type_definition_forbidden_message;
6489 parser->type_definition_forbidden_message
6490 = "types may not be defined in conditions";
6491 /* Parse the type-specifier-seq. */
6492 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6493 &type_specifiers);
6494 /* Restore the saved message. */
6495 parser->type_definition_forbidden_message = saved_message;
6496 /* If all is well, we might be looking at a declaration. */
6497 if (!cp_parser_error_occurred (parser))
6499 tree decl;
6500 tree asm_specification;
6501 tree attributes;
6502 cp_declarator *declarator;
6503 tree initializer = NULL_TREE;
6505 /* Parse the declarator. */
6506 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6507 /*ctor_dtor_or_conv_p=*/NULL,
6508 /*parenthesized_p=*/NULL,
6509 /*member_p=*/false);
6510 /* Parse the attributes. */
6511 attributes = cp_parser_attributes_opt (parser);
6512 /* Parse the asm-specification. */
6513 asm_specification = cp_parser_asm_specification_opt (parser);
6514 /* If the next token is not an `=', then we might still be
6515 looking at an expression. For example:
6517 if (A(a).x)
6519 looks like a decl-specifier-seq and a declarator -- but then
6520 there is no `=', so this is an expression. */
6521 cp_parser_require (parser, CPP_EQ, "`='");
6522 /* If we did see an `=', then we are looking at a declaration
6523 for sure. */
6524 if (cp_parser_parse_definitely (parser))
6526 tree pushed_scope;
6527 bool non_constant_p;
6529 /* Create the declaration. */
6530 decl = start_decl (declarator, &type_specifiers,
6531 /*initialized_p=*/true,
6532 attributes, /*prefix_attributes=*/NULL_TREE,
6533 &pushed_scope);
6534 /* Parse the assignment-expression. */
6535 initializer
6536 = cp_parser_constant_expression (parser,
6537 /*allow_non_constant_p=*/true,
6538 &non_constant_p);
6539 if (!non_constant_p)
6540 initializer = fold_non_dependent_expr (initializer);
6542 /* Process the initializer. */
6543 cp_finish_decl (decl,
6544 initializer, !non_constant_p,
6545 asm_specification,
6546 LOOKUP_ONLYCONVERTING);
6548 if (pushed_scope)
6549 pop_scope (pushed_scope);
6551 return convert_from_reference (decl);
6554 /* If we didn't even get past the declarator successfully, we are
6555 definitely not looking at a declaration. */
6556 else
6557 cp_parser_abort_tentative_parse (parser);
6559 /* Otherwise, we are looking at an expression. */
6560 return cp_parser_expression (parser, /*cast_p=*/false);
6563 /* Parse an iteration-statement.
6565 iteration-statement:
6566 while ( condition ) statement
6567 do statement while ( expression ) ;
6568 for ( for-init-statement condition [opt] ; expression [opt] )
6569 statement
6571 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6573 static tree
6574 cp_parser_iteration_statement (cp_parser* parser)
6576 cp_token *token;
6577 enum rid keyword;
6578 tree statement;
6579 unsigned char in_statement;
6581 /* Peek at the next token. */
6582 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6583 if (!token)
6584 return error_mark_node;
6586 /* Remember whether or not we are already within an iteration
6587 statement. */
6588 in_statement = parser->in_statement;
6590 /* See what kind of keyword it is. */
6591 keyword = token->keyword;
6592 switch (keyword)
6594 case RID_WHILE:
6596 tree condition;
6598 /* Begin the while-statement. */
6599 statement = begin_while_stmt ();
6600 /* Look for the `('. */
6601 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6602 /* Parse the condition. */
6603 condition = cp_parser_condition (parser);
6604 finish_while_stmt_cond (condition, statement);
6605 /* Look for the `)'. */
6606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6607 /* Parse the dependent statement. */
6608 parser->in_statement = IN_ITERATION_STMT;
6609 cp_parser_already_scoped_statement (parser);
6610 parser->in_statement = in_statement;
6611 /* We're done with the while-statement. */
6612 finish_while_stmt (statement);
6614 break;
6616 case RID_DO:
6618 tree expression;
6620 /* Begin the do-statement. */
6621 statement = begin_do_stmt ();
6622 /* Parse the body of the do-statement. */
6623 parser->in_statement = IN_ITERATION_STMT;
6624 cp_parser_implicitly_scoped_statement (parser);
6625 parser->in_statement = in_statement;
6626 finish_do_body (statement);
6627 /* Look for the `while' keyword. */
6628 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6629 /* Look for the `('. */
6630 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6631 /* Parse the expression. */
6632 expression = cp_parser_expression (parser, /*cast_p=*/false);
6633 /* We're done with the do-statement. */
6634 finish_do_stmt (expression, statement);
6635 /* Look for the `)'. */
6636 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6637 /* Look for the `;'. */
6638 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6640 break;
6642 case RID_FOR:
6644 tree condition = NULL_TREE;
6645 tree expression = NULL_TREE;
6647 /* Begin the for-statement. */
6648 statement = begin_for_stmt ();
6649 /* Look for the `('. */
6650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6651 /* Parse the initialization. */
6652 cp_parser_for_init_statement (parser);
6653 finish_for_init_stmt (statement);
6655 /* If there's a condition, process it. */
6656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6657 condition = cp_parser_condition (parser);
6658 finish_for_cond (condition, statement);
6659 /* Look for the `;'. */
6660 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6662 /* If there's an expression, process it. */
6663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6664 expression = cp_parser_expression (parser, /*cast_p=*/false);
6665 finish_for_expr (expression, statement);
6666 /* Look for the `)'. */
6667 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6669 /* Parse the body of the for-statement. */
6670 parser->in_statement = IN_ITERATION_STMT;
6671 cp_parser_already_scoped_statement (parser);
6672 parser->in_statement = in_statement;
6674 /* We're done with the for-statement. */
6675 finish_for_stmt (statement);
6677 break;
6679 default:
6680 cp_parser_error (parser, "expected iteration-statement");
6681 statement = error_mark_node;
6682 break;
6685 return statement;
6688 /* Parse a for-init-statement.
6690 for-init-statement:
6691 expression-statement
6692 simple-declaration */
6694 static void
6695 cp_parser_for_init_statement (cp_parser* parser)
6697 /* If the next token is a `;', then we have an empty
6698 expression-statement. Grammatically, this is also a
6699 simple-declaration, but an invalid one, because it does not
6700 declare anything. Therefore, if we did not handle this case
6701 specially, we would issue an error message about an invalid
6702 declaration. */
6703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6705 /* We're going to speculatively look for a declaration, falling back
6706 to an expression, if necessary. */
6707 cp_parser_parse_tentatively (parser);
6708 /* Parse the declaration. */
6709 cp_parser_simple_declaration (parser,
6710 /*function_definition_allowed_p=*/false);
6711 /* If the tentative parse failed, then we shall need to look for an
6712 expression-statement. */
6713 if (cp_parser_parse_definitely (parser))
6714 return;
6717 cp_parser_expression_statement (parser, false);
6720 /* Parse a jump-statement.
6722 jump-statement:
6723 break ;
6724 continue ;
6725 return expression [opt] ;
6726 goto identifier ;
6728 GNU extension:
6730 jump-statement:
6731 goto * expression ;
6733 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6735 static tree
6736 cp_parser_jump_statement (cp_parser* parser)
6738 tree statement = error_mark_node;
6739 cp_token *token;
6740 enum rid keyword;
6742 /* Peek at the next token. */
6743 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6744 if (!token)
6745 return error_mark_node;
6747 /* See what kind of keyword it is. */
6748 keyword = token->keyword;
6749 switch (keyword)
6751 case RID_BREAK:
6752 switch (parser->in_statement)
6754 case 0:
6755 error ("break statement not within loop or switch");
6756 break;
6757 default:
6758 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6759 || parser->in_statement == IN_ITERATION_STMT);
6760 statement = finish_break_stmt ();
6761 break;
6762 case IN_OMP_BLOCK:
6763 error ("invalid exit from OpenMP structured block");
6764 break;
6765 case IN_OMP_FOR:
6766 error ("break statement used with OpenMP for loop");
6767 break;
6769 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6770 break;
6772 case RID_CONTINUE:
6773 switch (parser->in_statement & ~IN_SWITCH_STMT)
6775 case 0:
6776 error ("continue statement not within a loop");
6777 break;
6778 case IN_ITERATION_STMT:
6779 case IN_OMP_FOR:
6780 statement = finish_continue_stmt ();
6781 break;
6782 case IN_OMP_BLOCK:
6783 error ("invalid exit from OpenMP structured block");
6784 break;
6785 default:
6786 gcc_unreachable ();
6788 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6789 break;
6791 case RID_RETURN:
6793 tree expr;
6795 /* If the next token is a `;', then there is no
6796 expression. */
6797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6798 expr = cp_parser_expression (parser, /*cast_p=*/false);
6799 else
6800 expr = NULL_TREE;
6801 /* Build the return-statement. */
6802 statement = finish_return_stmt (expr);
6803 /* Look for the final `;'. */
6804 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6806 break;
6808 case RID_GOTO:
6809 /* Create the goto-statement. */
6810 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6812 /* Issue a warning about this use of a GNU extension. */
6813 if (pedantic)
6814 pedwarn ("ISO C++ forbids computed gotos");
6815 /* Consume the '*' token. */
6816 cp_lexer_consume_token (parser->lexer);
6817 /* Parse the dependent expression. */
6818 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6820 else
6821 finish_goto_stmt (cp_parser_identifier (parser));
6822 /* Look for the final `;'. */
6823 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6824 break;
6826 default:
6827 cp_parser_error (parser, "expected jump-statement");
6828 break;
6831 return statement;
6834 /* Parse a declaration-statement.
6836 declaration-statement:
6837 block-declaration */
6839 static void
6840 cp_parser_declaration_statement (cp_parser* parser)
6842 void *p;
6844 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6845 p = obstack_alloc (&declarator_obstack, 0);
6847 /* Parse the block-declaration. */
6848 cp_parser_block_declaration (parser, /*statement_p=*/true);
6850 /* Free any declarators allocated. */
6851 obstack_free (&declarator_obstack, p);
6853 /* Finish off the statement. */
6854 finish_stmt ();
6857 /* Some dependent statements (like `if (cond) statement'), are
6858 implicitly in their own scope. In other words, if the statement is
6859 a single statement (as opposed to a compound-statement), it is
6860 none-the-less treated as if it were enclosed in braces. Any
6861 declarations appearing in the dependent statement are out of scope
6862 after control passes that point. This function parses a statement,
6863 but ensures that is in its own scope, even if it is not a
6864 compound-statement.
6866 Returns the new statement. */
6868 static tree
6869 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6871 tree statement;
6873 /* Mark if () ; with a special NOP_EXPR. */
6874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6876 cp_lexer_consume_token (parser->lexer);
6877 statement = add_stmt (build_empty_stmt ());
6879 /* if a compound is opened, we simply parse the statement directly. */
6880 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6881 statement = cp_parser_compound_statement (parser, NULL, false);
6882 /* If the token is not a `{', then we must take special action. */
6883 else
6885 /* Create a compound-statement. */
6886 statement = begin_compound_stmt (0);
6887 /* Parse the dependent-statement. */
6888 cp_parser_statement (parser, NULL_TREE, false);
6889 /* Finish the dummy compound-statement. */
6890 finish_compound_stmt (statement);
6893 /* Return the statement. */
6894 return statement;
6897 /* For some dependent statements (like `while (cond) statement'), we
6898 have already created a scope. Therefore, even if the dependent
6899 statement is a compound-statement, we do not want to create another
6900 scope. */
6902 static void
6903 cp_parser_already_scoped_statement (cp_parser* parser)
6905 /* If the token is a `{', then we must take special action. */
6906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6907 cp_parser_statement (parser, NULL_TREE, false);
6908 else
6910 /* Avoid calling cp_parser_compound_statement, so that we
6911 don't create a new scope. Do everything else by hand. */
6912 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6913 cp_parser_statement_seq_opt (parser, NULL_TREE);
6914 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6918 /* Declarations [gram.dcl.dcl] */
6920 /* Parse an optional declaration-sequence.
6922 declaration-seq:
6923 declaration
6924 declaration-seq declaration */
6926 static void
6927 cp_parser_declaration_seq_opt (cp_parser* parser)
6929 while (true)
6931 cp_token *token;
6933 token = cp_lexer_peek_token (parser->lexer);
6935 if (token->type == CPP_CLOSE_BRACE
6936 || token->type == CPP_EOF
6937 || token->type == CPP_PRAGMA_EOL)
6938 break;
6940 if (token->type == CPP_SEMICOLON)
6942 /* A declaration consisting of a single semicolon is
6943 invalid. Allow it unless we're being pedantic. */
6944 cp_lexer_consume_token (parser->lexer);
6945 if (pedantic && !in_system_header)
6946 pedwarn ("extra %<;%>");
6947 continue;
6950 /* If we're entering or exiting a region that's implicitly
6951 extern "C", modify the lang context appropriately. */
6952 if (!parser->implicit_extern_c && token->implicit_extern_c)
6954 push_lang_context (lang_name_c);
6955 parser->implicit_extern_c = true;
6957 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6959 pop_lang_context ();
6960 parser->implicit_extern_c = false;
6963 if (token->type == CPP_PRAGMA)
6965 /* A top-level declaration can consist solely of a #pragma.
6966 A nested declaration cannot, so this is done here and not
6967 in cp_parser_declaration. (A #pragma at block scope is
6968 handled in cp_parser_statement.) */
6969 cp_parser_pragma (parser, pragma_external);
6970 continue;
6973 /* Parse the declaration itself. */
6974 cp_parser_declaration (parser);
6978 /* Parse a declaration.
6980 declaration:
6981 block-declaration
6982 function-definition
6983 template-declaration
6984 explicit-instantiation
6985 explicit-specialization
6986 linkage-specification
6987 namespace-definition
6989 GNU extension:
6991 declaration:
6992 __extension__ declaration */
6994 static void
6995 cp_parser_declaration (cp_parser* parser)
6997 cp_token token1;
6998 cp_token token2;
6999 int saved_pedantic;
7000 void *p;
7002 /* Check for the `__extension__' keyword. */
7003 if (cp_parser_extension_opt (parser, &saved_pedantic))
7005 /* Parse the qualified declaration. */
7006 cp_parser_declaration (parser);
7007 /* Restore the PEDANTIC flag. */
7008 pedantic = saved_pedantic;
7010 return;
7013 /* Try to figure out what kind of declaration is present. */
7014 token1 = *cp_lexer_peek_token (parser->lexer);
7016 if (token1.type != CPP_EOF)
7017 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7018 else
7020 token2.type = CPP_EOF;
7021 token2.keyword = RID_MAX;
7024 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7025 p = obstack_alloc (&declarator_obstack, 0);
7027 /* If the next token is `extern' and the following token is a string
7028 literal, then we have a linkage specification. */
7029 if (token1.keyword == RID_EXTERN
7030 && cp_parser_is_string_literal (&token2))
7031 cp_parser_linkage_specification (parser);
7032 /* If the next token is `template', then we have either a template
7033 declaration, an explicit instantiation, or an explicit
7034 specialization. */
7035 else if (token1.keyword == RID_TEMPLATE)
7037 /* `template <>' indicates a template specialization. */
7038 if (token2.type == CPP_LESS
7039 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7040 cp_parser_explicit_specialization (parser);
7041 /* `template <' indicates a template declaration. */
7042 else if (token2.type == CPP_LESS)
7043 cp_parser_template_declaration (parser, /*member_p=*/false);
7044 /* Anything else must be an explicit instantiation. */
7045 else
7046 cp_parser_explicit_instantiation (parser);
7048 /* If the next token is `export', then we have a template
7049 declaration. */
7050 else if (token1.keyword == RID_EXPORT)
7051 cp_parser_template_declaration (parser, /*member_p=*/false);
7052 /* If the next token is `extern', 'static' or 'inline' and the one
7053 after that is `template', we have a GNU extended explicit
7054 instantiation directive. */
7055 else if (cp_parser_allow_gnu_extensions_p (parser)
7056 && (token1.keyword == RID_EXTERN
7057 || token1.keyword == RID_STATIC
7058 || token1.keyword == RID_INLINE)
7059 && token2.keyword == RID_TEMPLATE)
7060 cp_parser_explicit_instantiation (parser);
7061 /* If the next token is `namespace', check for a named or unnamed
7062 namespace definition. */
7063 else if (token1.keyword == RID_NAMESPACE
7064 && (/* A named namespace definition. */
7065 (token2.type == CPP_NAME
7066 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7067 == CPP_OPEN_BRACE))
7068 /* An unnamed namespace definition. */
7069 || token2.type == CPP_OPEN_BRACE))
7070 cp_parser_namespace_definition (parser);
7071 /* Objective-C++ declaration/definition. */
7072 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7073 cp_parser_objc_declaration (parser);
7074 /* We must have either a block declaration or a function
7075 definition. */
7076 else
7077 /* Try to parse a block-declaration, or a function-definition. */
7078 cp_parser_block_declaration (parser, /*statement_p=*/false);
7080 /* Free any declarators allocated. */
7081 obstack_free (&declarator_obstack, p);
7084 /* Parse a block-declaration.
7086 block-declaration:
7087 simple-declaration
7088 asm-definition
7089 namespace-alias-definition
7090 using-declaration
7091 using-directive
7093 GNU Extension:
7095 block-declaration:
7096 __extension__ block-declaration
7097 label-declaration
7099 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7100 part of a declaration-statement. */
7102 static void
7103 cp_parser_block_declaration (cp_parser *parser,
7104 bool statement_p)
7106 cp_token *token1;
7107 int saved_pedantic;
7109 /* Check for the `__extension__' keyword. */
7110 if (cp_parser_extension_opt (parser, &saved_pedantic))
7112 /* Parse the qualified declaration. */
7113 cp_parser_block_declaration (parser, statement_p);
7114 /* Restore the PEDANTIC flag. */
7115 pedantic = saved_pedantic;
7117 return;
7120 /* Peek at the next token to figure out which kind of declaration is
7121 present. */
7122 token1 = cp_lexer_peek_token (parser->lexer);
7124 /* If the next keyword is `asm', we have an asm-definition. */
7125 if (token1->keyword == RID_ASM)
7127 if (statement_p)
7128 cp_parser_commit_to_tentative_parse (parser);
7129 cp_parser_asm_definition (parser);
7131 /* If the next keyword is `namespace', we have a
7132 namespace-alias-definition. */
7133 else if (token1->keyword == RID_NAMESPACE)
7134 cp_parser_namespace_alias_definition (parser);
7135 /* If the next keyword is `using', we have either a
7136 using-declaration or a using-directive. */
7137 else if (token1->keyword == RID_USING)
7139 cp_token *token2;
7141 if (statement_p)
7142 cp_parser_commit_to_tentative_parse (parser);
7143 /* If the token after `using' is `namespace', then we have a
7144 using-directive. */
7145 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7146 if (token2->keyword == RID_NAMESPACE)
7147 cp_parser_using_directive (parser);
7148 /* Otherwise, it's a using-declaration. */
7149 else
7150 cp_parser_using_declaration (parser);
7152 /* If the next keyword is `__label__' we have a label declaration. */
7153 else if (token1->keyword == RID_LABEL)
7155 if (statement_p)
7156 cp_parser_commit_to_tentative_parse (parser);
7157 cp_parser_label_declaration (parser);
7159 /* Anything else must be a simple-declaration. */
7160 else
7161 cp_parser_simple_declaration (parser, !statement_p);
7164 /* Parse a simple-declaration.
7166 simple-declaration:
7167 decl-specifier-seq [opt] init-declarator-list [opt] ;
7169 init-declarator-list:
7170 init-declarator
7171 init-declarator-list , init-declarator
7173 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7174 function-definition as a simple-declaration. */
7176 static void
7177 cp_parser_simple_declaration (cp_parser* parser,
7178 bool function_definition_allowed_p)
7180 cp_decl_specifier_seq decl_specifiers;
7181 int declares_class_or_enum;
7182 bool saw_declarator;
7184 /* Defer access checks until we know what is being declared; the
7185 checks for names appearing in the decl-specifier-seq should be
7186 done as if we were in the scope of the thing being declared. */
7187 push_deferring_access_checks (dk_deferred);
7189 /* Parse the decl-specifier-seq. We have to keep track of whether
7190 or not the decl-specifier-seq declares a named class or
7191 enumeration type, since that is the only case in which the
7192 init-declarator-list is allowed to be empty.
7194 [dcl.dcl]
7196 In a simple-declaration, the optional init-declarator-list can be
7197 omitted only when declaring a class or enumeration, that is when
7198 the decl-specifier-seq contains either a class-specifier, an
7199 elaborated-type-specifier, or an enum-specifier. */
7200 cp_parser_decl_specifier_seq (parser,
7201 CP_PARSER_FLAGS_OPTIONAL,
7202 &decl_specifiers,
7203 &declares_class_or_enum);
7204 /* We no longer need to defer access checks. */
7205 stop_deferring_access_checks ();
7207 /* In a block scope, a valid declaration must always have a
7208 decl-specifier-seq. By not trying to parse declarators, we can
7209 resolve the declaration/expression ambiguity more quickly. */
7210 if (!function_definition_allowed_p
7211 && !decl_specifiers.any_specifiers_p)
7213 cp_parser_error (parser, "expected declaration");
7214 goto done;
7217 /* If the next two tokens are both identifiers, the code is
7218 erroneous. The usual cause of this situation is code like:
7220 T t;
7222 where "T" should name a type -- but does not. */
7223 if (!decl_specifiers.type
7224 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7226 /* If parsing tentatively, we should commit; we really are
7227 looking at a declaration. */
7228 cp_parser_commit_to_tentative_parse (parser);
7229 /* Give up. */
7230 goto done;
7233 /* If we have seen at least one decl-specifier, and the next token
7234 is not a parenthesis, then we must be looking at a declaration.
7235 (After "int (" we might be looking at a functional cast.) */
7236 if (decl_specifiers.any_specifiers_p
7237 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7238 cp_parser_commit_to_tentative_parse (parser);
7240 /* Keep going until we hit the `;' at the end of the simple
7241 declaration. */
7242 saw_declarator = false;
7243 while (cp_lexer_next_token_is_not (parser->lexer,
7244 CPP_SEMICOLON))
7246 cp_token *token;
7247 bool function_definition_p;
7248 tree decl;
7250 if (saw_declarator)
7252 /* If we are processing next declarator, coma is expected */
7253 token = cp_lexer_peek_token (parser->lexer);
7254 gcc_assert (token->type == CPP_COMMA);
7255 cp_lexer_consume_token (parser->lexer);
7257 else
7258 saw_declarator = true;
7260 /* Parse the init-declarator. */
7261 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7262 function_definition_allowed_p,
7263 /*member_p=*/false,
7264 declares_class_or_enum,
7265 &function_definition_p);
7266 /* If an error occurred while parsing tentatively, exit quickly.
7267 (That usually happens when in the body of a function; each
7268 statement is treated as a declaration-statement until proven
7269 otherwise.) */
7270 if (cp_parser_error_occurred (parser))
7271 goto done;
7272 /* Handle function definitions specially. */
7273 if (function_definition_p)
7275 /* If the next token is a `,', then we are probably
7276 processing something like:
7278 void f() {}, *p;
7280 which is erroneous. */
7281 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7282 error ("mixing declarations and function-definitions is forbidden");
7283 /* Otherwise, we're done with the list of declarators. */
7284 else
7286 pop_deferring_access_checks ();
7287 return;
7290 /* The next token should be either a `,' or a `;'. */
7291 token = cp_lexer_peek_token (parser->lexer);
7292 /* If it's a `,', there are more declarators to come. */
7293 if (token->type == CPP_COMMA)
7294 /* will be consumed next time around */;
7295 /* If it's a `;', we are done. */
7296 else if (token->type == CPP_SEMICOLON)
7297 break;
7298 /* Anything else is an error. */
7299 else
7301 /* If we have already issued an error message we don't need
7302 to issue another one. */
7303 if (decl != error_mark_node
7304 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7305 cp_parser_error (parser, "expected %<,%> or %<;%>");
7306 /* Skip tokens until we reach the end of the statement. */
7307 cp_parser_skip_to_end_of_statement (parser);
7308 /* If the next token is now a `;', consume it. */
7309 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7310 cp_lexer_consume_token (parser->lexer);
7311 goto done;
7313 /* After the first time around, a function-definition is not
7314 allowed -- even if it was OK at first. For example:
7316 int i, f() {}
7318 is not valid. */
7319 function_definition_allowed_p = false;
7322 /* Issue an error message if no declarators are present, and the
7323 decl-specifier-seq does not itself declare a class or
7324 enumeration. */
7325 if (!saw_declarator)
7327 if (cp_parser_declares_only_class_p (parser))
7328 shadow_tag (&decl_specifiers);
7329 /* Perform any deferred access checks. */
7330 perform_deferred_access_checks ();
7333 /* Consume the `;'. */
7334 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7336 done:
7337 pop_deferring_access_checks ();
7340 /* Parse a decl-specifier-seq.
7342 decl-specifier-seq:
7343 decl-specifier-seq [opt] decl-specifier
7345 decl-specifier:
7346 storage-class-specifier
7347 type-specifier
7348 function-specifier
7349 friend
7350 typedef
7352 GNU Extension:
7354 decl-specifier:
7355 attributes
7357 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7359 The parser flags FLAGS is used to control type-specifier parsing.
7361 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7362 flags:
7364 1: one of the decl-specifiers is an elaborated-type-specifier
7365 (i.e., a type declaration)
7366 2: one of the decl-specifiers is an enum-specifier or a
7367 class-specifier (i.e., a type definition)
7371 static void
7372 cp_parser_decl_specifier_seq (cp_parser* parser,
7373 cp_parser_flags flags,
7374 cp_decl_specifier_seq *decl_specs,
7375 int* declares_class_or_enum)
7377 bool constructor_possible_p = !parser->in_declarator_p;
7378 cp_decl_spec ds;
7380 /* Clear DECL_SPECS. */
7381 clear_decl_specs (decl_specs);
7383 /* Assume no class or enumeration type is declared. */
7384 *declares_class_or_enum = 0;
7386 /* Keep reading specifiers until there are no more to read. */
7387 while (true)
7389 bool constructor_p;
7390 bool found_decl_spec;
7391 cp_token *token;
7393 /* Peek at the next token. */
7394 token = cp_lexer_peek_token (parser->lexer);
7395 /* Handle attributes. */
7396 if (token->keyword == RID_ATTRIBUTE)
7398 /* Parse the attributes. */
7399 decl_specs->attributes
7400 = chainon (decl_specs->attributes,
7401 cp_parser_attributes_opt (parser));
7402 continue;
7404 /* Assume we will find a decl-specifier keyword. */
7405 found_decl_spec = true;
7406 /* If the next token is an appropriate keyword, we can simply
7407 add it to the list. */
7408 switch (token->keyword)
7410 /* decl-specifier:
7411 friend */
7412 case RID_FRIEND:
7413 ++decl_specs->specs[(int) ds_friend];
7414 /* Consume the token. */
7415 cp_lexer_consume_token (parser->lexer);
7416 break;
7418 /* function-specifier:
7419 inline
7420 virtual
7421 explicit */
7422 case RID_INLINE:
7423 case RID_VIRTUAL:
7424 case RID_EXPLICIT:
7425 cp_parser_function_specifier_opt (parser, decl_specs);
7426 break;
7428 /* decl-specifier:
7429 typedef */
7430 case RID_TYPEDEF:
7431 ++decl_specs->specs[(int) ds_typedef];
7432 /* Consume the token. */
7433 cp_lexer_consume_token (parser->lexer);
7434 /* A constructor declarator cannot appear in a typedef. */
7435 constructor_possible_p = false;
7436 /* The "typedef" keyword can only occur in a declaration; we
7437 may as well commit at this point. */
7438 cp_parser_commit_to_tentative_parse (parser);
7439 break;
7441 /* storage-class-specifier:
7442 auto
7443 register
7444 static
7445 extern
7446 mutable
7448 GNU Extension:
7449 thread */
7450 case RID_AUTO:
7451 /* Consume the token. */
7452 cp_lexer_consume_token (parser->lexer);
7453 cp_parser_set_storage_class (decl_specs, sc_auto);
7454 break;
7455 case RID_REGISTER:
7456 /* Consume the token. */
7457 cp_lexer_consume_token (parser->lexer);
7458 cp_parser_set_storage_class (decl_specs, sc_register);
7459 break;
7460 case RID_STATIC:
7461 /* Consume the token. */
7462 cp_lexer_consume_token (parser->lexer);
7463 if (decl_specs->specs[(int) ds_thread])
7465 error ("%<__thread%> before %<static%>");
7466 decl_specs->specs[(int) ds_thread] = 0;
7468 cp_parser_set_storage_class (decl_specs, sc_static);
7469 break;
7470 case RID_EXTERN:
7471 /* Consume the token. */
7472 cp_lexer_consume_token (parser->lexer);
7473 if (decl_specs->specs[(int) ds_thread])
7475 error ("%<__thread%> before %<extern%>");
7476 decl_specs->specs[(int) ds_thread] = 0;
7478 cp_parser_set_storage_class (decl_specs, sc_extern);
7479 break;
7480 case RID_MUTABLE:
7481 /* Consume the token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 cp_parser_set_storage_class (decl_specs, sc_mutable);
7484 break;
7485 case RID_THREAD:
7486 /* Consume the token. */
7487 cp_lexer_consume_token (parser->lexer);
7488 ++decl_specs->specs[(int) ds_thread];
7489 break;
7491 default:
7492 /* We did not yet find a decl-specifier yet. */
7493 found_decl_spec = false;
7494 break;
7497 /* Constructors are a special case. The `S' in `S()' is not a
7498 decl-specifier; it is the beginning of the declarator. */
7499 constructor_p
7500 = (!found_decl_spec
7501 && constructor_possible_p
7502 && (cp_parser_constructor_declarator_p
7503 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7505 /* If we don't have a DECL_SPEC yet, then we must be looking at
7506 a type-specifier. */
7507 if (!found_decl_spec && !constructor_p)
7509 int decl_spec_declares_class_or_enum;
7510 bool is_cv_qualifier;
7511 tree type_spec;
7513 type_spec
7514 = cp_parser_type_specifier (parser, flags,
7515 decl_specs,
7516 /*is_declaration=*/true,
7517 &decl_spec_declares_class_or_enum,
7518 &is_cv_qualifier);
7520 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7522 /* If this type-specifier referenced a user-defined type
7523 (a typedef, class-name, etc.), then we can't allow any
7524 more such type-specifiers henceforth.
7526 [dcl.spec]
7528 The longest sequence of decl-specifiers that could
7529 possibly be a type name is taken as the
7530 decl-specifier-seq of a declaration. The sequence shall
7531 be self-consistent as described below.
7533 [dcl.type]
7535 As a general rule, at most one type-specifier is allowed
7536 in the complete decl-specifier-seq of a declaration. The
7537 only exceptions are the following:
7539 -- const or volatile can be combined with any other
7540 type-specifier.
7542 -- signed or unsigned can be combined with char, long,
7543 short, or int.
7545 -- ..
7547 Example:
7549 typedef char* Pc;
7550 void g (const int Pc);
7552 Here, Pc is *not* part of the decl-specifier seq; it's
7553 the declarator. Therefore, once we see a type-specifier
7554 (other than a cv-qualifier), we forbid any additional
7555 user-defined types. We *do* still allow things like `int
7556 int' to be considered a decl-specifier-seq, and issue the
7557 error message later. */
7558 if (type_spec && !is_cv_qualifier)
7559 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7560 /* A constructor declarator cannot follow a type-specifier. */
7561 if (type_spec)
7563 constructor_possible_p = false;
7564 found_decl_spec = true;
7568 /* If we still do not have a DECL_SPEC, then there are no more
7569 decl-specifiers. */
7570 if (!found_decl_spec)
7571 break;
7573 decl_specs->any_specifiers_p = true;
7574 /* After we see one decl-specifier, further decl-specifiers are
7575 always optional. */
7576 flags |= CP_PARSER_FLAGS_OPTIONAL;
7579 /* Check for repeated decl-specifiers. */
7580 for (ds = ds_first; ds != ds_last; ++ds)
7582 unsigned count = decl_specs->specs[(int)ds];
7583 if (count < 2)
7584 continue;
7585 /* The "long" specifier is a special case because of "long long". */
7586 if (ds == ds_long)
7588 if (count > 2)
7589 error ("%<long long long%> is too long for GCC");
7590 else if (pedantic && !in_system_header && warn_long_long)
7591 pedwarn ("ISO C++ does not support %<long long%>");
7593 else if (count > 1)
7595 static const char *const decl_spec_names[] = {
7596 "signed",
7597 "unsigned",
7598 "short",
7599 "long",
7600 "const",
7601 "volatile",
7602 "restrict",
7603 "inline",
7604 "virtual",
7605 "explicit",
7606 "friend",
7607 "typedef",
7608 "__complex",
7609 "__thread"
7611 error ("duplicate %qs", decl_spec_names[(int)ds]);
7615 /* Don't allow a friend specifier with a class definition. */
7616 if (decl_specs->specs[(int) ds_friend] != 0
7617 && (*declares_class_or_enum & 2))
7618 error ("class definition may not be declared a friend");
7621 /* Parse an (optional) storage-class-specifier.
7623 storage-class-specifier:
7624 auto
7625 register
7626 static
7627 extern
7628 mutable
7630 GNU Extension:
7632 storage-class-specifier:
7633 thread
7635 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7637 static tree
7638 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7640 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7642 case RID_AUTO:
7643 case RID_REGISTER:
7644 case RID_STATIC:
7645 case RID_EXTERN:
7646 case RID_MUTABLE:
7647 case RID_THREAD:
7648 /* Consume the token. */
7649 return cp_lexer_consume_token (parser->lexer)->value;
7651 default:
7652 return NULL_TREE;
7656 /* Parse an (optional) function-specifier.
7658 function-specifier:
7659 inline
7660 virtual
7661 explicit
7663 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7664 Updates DECL_SPECS, if it is non-NULL. */
7666 static tree
7667 cp_parser_function_specifier_opt (cp_parser* parser,
7668 cp_decl_specifier_seq *decl_specs)
7670 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7672 case RID_INLINE:
7673 if (decl_specs)
7674 ++decl_specs->specs[(int) ds_inline];
7675 break;
7677 case RID_VIRTUAL:
7678 if (decl_specs)
7679 ++decl_specs->specs[(int) ds_virtual];
7680 break;
7682 case RID_EXPLICIT:
7683 if (decl_specs)
7684 ++decl_specs->specs[(int) ds_explicit];
7685 break;
7687 default:
7688 return NULL_TREE;
7691 /* Consume the token. */
7692 return cp_lexer_consume_token (parser->lexer)->value;
7695 /* Parse a linkage-specification.
7697 linkage-specification:
7698 extern string-literal { declaration-seq [opt] }
7699 extern string-literal declaration */
7701 static void
7702 cp_parser_linkage_specification (cp_parser* parser)
7704 tree linkage;
7706 /* Look for the `extern' keyword. */
7707 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7709 /* Look for the string-literal. */
7710 linkage = cp_parser_string_literal (parser, false, false);
7712 /* Transform the literal into an identifier. If the literal is a
7713 wide-character string, or contains embedded NULs, then we can't
7714 handle it as the user wants. */
7715 if (strlen (TREE_STRING_POINTER (linkage))
7716 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7718 cp_parser_error (parser, "invalid linkage-specification");
7719 /* Assume C++ linkage. */
7720 linkage = lang_name_cplusplus;
7722 else
7723 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7725 /* We're now using the new linkage. */
7726 push_lang_context (linkage);
7728 /* If the next token is a `{', then we're using the first
7729 production. */
7730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7732 /* Consume the `{' token. */
7733 cp_lexer_consume_token (parser->lexer);
7734 /* Parse the declarations. */
7735 cp_parser_declaration_seq_opt (parser);
7736 /* Look for the closing `}'. */
7737 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7739 /* Otherwise, there's just one declaration. */
7740 else
7742 bool saved_in_unbraced_linkage_specification_p;
7744 saved_in_unbraced_linkage_specification_p
7745 = parser->in_unbraced_linkage_specification_p;
7746 parser->in_unbraced_linkage_specification_p = true;
7747 have_extern_spec = true;
7748 cp_parser_declaration (parser);
7749 have_extern_spec = false;
7750 parser->in_unbraced_linkage_specification_p
7751 = saved_in_unbraced_linkage_specification_p;
7754 /* We're done with the linkage-specification. */
7755 pop_lang_context ();
7758 /* Special member functions [gram.special] */
7760 /* Parse a conversion-function-id.
7762 conversion-function-id:
7763 operator conversion-type-id
7765 Returns an IDENTIFIER_NODE representing the operator. */
7767 static tree
7768 cp_parser_conversion_function_id (cp_parser* parser)
7770 tree type;
7771 tree saved_scope;
7772 tree saved_qualifying_scope;
7773 tree saved_object_scope;
7774 tree pushed_scope = NULL_TREE;
7776 /* Look for the `operator' token. */
7777 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7778 return error_mark_node;
7779 /* When we parse the conversion-type-id, the current scope will be
7780 reset. However, we need that information in able to look up the
7781 conversion function later, so we save it here. */
7782 saved_scope = parser->scope;
7783 saved_qualifying_scope = parser->qualifying_scope;
7784 saved_object_scope = parser->object_scope;
7785 /* We must enter the scope of the class so that the names of
7786 entities declared within the class are available in the
7787 conversion-type-id. For example, consider:
7789 struct S {
7790 typedef int I;
7791 operator I();
7794 S::operator I() { ... }
7796 In order to see that `I' is a type-name in the definition, we
7797 must be in the scope of `S'. */
7798 if (saved_scope)
7799 pushed_scope = push_scope (saved_scope);
7800 /* Parse the conversion-type-id. */
7801 type = cp_parser_conversion_type_id (parser);
7802 /* Leave the scope of the class, if any. */
7803 if (pushed_scope)
7804 pop_scope (pushed_scope);
7805 /* Restore the saved scope. */
7806 parser->scope = saved_scope;
7807 parser->qualifying_scope = saved_qualifying_scope;
7808 parser->object_scope = saved_object_scope;
7809 /* If the TYPE is invalid, indicate failure. */
7810 if (type == error_mark_node)
7811 return error_mark_node;
7812 return mangle_conv_op_name_for_type (type);
7815 /* Parse a conversion-type-id:
7817 conversion-type-id:
7818 type-specifier-seq conversion-declarator [opt]
7820 Returns the TYPE specified. */
7822 static tree
7823 cp_parser_conversion_type_id (cp_parser* parser)
7825 tree attributes;
7826 cp_decl_specifier_seq type_specifiers;
7827 cp_declarator *declarator;
7828 tree type_specified;
7830 /* Parse the attributes. */
7831 attributes = cp_parser_attributes_opt (parser);
7832 /* Parse the type-specifiers. */
7833 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7834 &type_specifiers);
7835 /* If that didn't work, stop. */
7836 if (type_specifiers.type == error_mark_node)
7837 return error_mark_node;
7838 /* Parse the conversion-declarator. */
7839 declarator = cp_parser_conversion_declarator_opt (parser);
7841 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7842 /*initialized=*/0, &attributes);
7843 if (attributes)
7844 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7845 return type_specified;
7848 /* Parse an (optional) conversion-declarator.
7850 conversion-declarator:
7851 ptr-operator conversion-declarator [opt]
7855 static cp_declarator *
7856 cp_parser_conversion_declarator_opt (cp_parser* parser)
7858 enum tree_code code;
7859 tree class_type;
7860 cp_cv_quals cv_quals;
7862 /* We don't know if there's a ptr-operator next, or not. */
7863 cp_parser_parse_tentatively (parser);
7864 /* Try the ptr-operator. */
7865 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7866 /* If it worked, look for more conversion-declarators. */
7867 if (cp_parser_parse_definitely (parser))
7869 cp_declarator *declarator;
7871 /* Parse another optional declarator. */
7872 declarator = cp_parser_conversion_declarator_opt (parser);
7874 /* Create the representation of the declarator. */
7875 if (class_type)
7876 declarator = make_ptrmem_declarator (cv_quals, class_type,
7877 declarator);
7878 else if (code == INDIRECT_REF)
7879 declarator = make_pointer_declarator (cv_quals, declarator);
7880 else
7881 declarator = make_reference_declarator (cv_quals, declarator);
7883 return declarator;
7886 return NULL;
7889 /* Parse an (optional) ctor-initializer.
7891 ctor-initializer:
7892 : mem-initializer-list
7894 Returns TRUE iff the ctor-initializer was actually present. */
7896 static bool
7897 cp_parser_ctor_initializer_opt (cp_parser* parser)
7899 /* If the next token is not a `:', then there is no
7900 ctor-initializer. */
7901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7903 /* Do default initialization of any bases and members. */
7904 if (DECL_CONSTRUCTOR_P (current_function_decl))
7905 finish_mem_initializers (NULL_TREE);
7907 return false;
7910 /* Consume the `:' token. */
7911 cp_lexer_consume_token (parser->lexer);
7912 /* And the mem-initializer-list. */
7913 cp_parser_mem_initializer_list (parser);
7915 return true;
7918 /* Parse a mem-initializer-list.
7920 mem-initializer-list:
7921 mem-initializer
7922 mem-initializer , mem-initializer-list */
7924 static void
7925 cp_parser_mem_initializer_list (cp_parser* parser)
7927 tree mem_initializer_list = NULL_TREE;
7929 /* Let the semantic analysis code know that we are starting the
7930 mem-initializer-list. */
7931 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7932 error ("only constructors take base initializers");
7934 /* Loop through the list. */
7935 while (true)
7937 tree mem_initializer;
7939 /* Parse the mem-initializer. */
7940 mem_initializer = cp_parser_mem_initializer (parser);
7941 /* Add it to the list, unless it was erroneous. */
7942 if (mem_initializer != error_mark_node)
7944 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7945 mem_initializer_list = mem_initializer;
7947 /* If the next token is not a `,', we're done. */
7948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7949 break;
7950 /* Consume the `,' token. */
7951 cp_lexer_consume_token (parser->lexer);
7954 /* Perform semantic analysis. */
7955 if (DECL_CONSTRUCTOR_P (current_function_decl))
7956 finish_mem_initializers (mem_initializer_list);
7959 /* Parse a mem-initializer.
7961 mem-initializer:
7962 mem-initializer-id ( expression-list [opt] )
7964 GNU extension:
7966 mem-initializer:
7967 ( expression-list [opt] )
7969 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7970 class) or FIELD_DECL (for a non-static data member) to initialize;
7971 the TREE_VALUE is the expression-list. An empty initialization
7972 list is represented by void_list_node. */
7974 static tree
7975 cp_parser_mem_initializer (cp_parser* parser)
7977 tree mem_initializer_id;
7978 tree expression_list;
7979 tree member;
7981 /* Find out what is being initialized. */
7982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7984 pedwarn ("anachronistic old-style base class initializer");
7985 mem_initializer_id = NULL_TREE;
7987 else
7988 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7989 member = expand_member_init (mem_initializer_id);
7990 if (member && !DECL_P (member))
7991 in_base_initializer = 1;
7993 expression_list
7994 = cp_parser_parenthesized_expression_list (parser, false,
7995 /*cast_p=*/false,
7996 /*non_constant_p=*/NULL);
7997 if (expression_list == error_mark_node)
7998 return error_mark_node;
7999 if (!expression_list)
8000 expression_list = void_type_node;
8002 in_base_initializer = 0;
8004 return member ? build_tree_list (member, expression_list) : error_mark_node;
8007 /* Parse a mem-initializer-id.
8009 mem-initializer-id:
8010 :: [opt] nested-name-specifier [opt] class-name
8011 identifier
8013 Returns a TYPE indicating the class to be initializer for the first
8014 production. Returns an IDENTIFIER_NODE indicating the data member
8015 to be initialized for the second production. */
8017 static tree
8018 cp_parser_mem_initializer_id (cp_parser* parser)
8020 bool global_scope_p;
8021 bool nested_name_specifier_p;
8022 bool template_p = false;
8023 tree id;
8025 /* `typename' is not allowed in this context ([temp.res]). */
8026 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8028 error ("keyword %<typename%> not allowed in this context (a qualified "
8029 "member initializer is implicitly a type)");
8030 cp_lexer_consume_token (parser->lexer);
8032 /* Look for the optional `::' operator. */
8033 global_scope_p
8034 = (cp_parser_global_scope_opt (parser,
8035 /*current_scope_valid_p=*/false)
8036 != NULL_TREE);
8037 /* Look for the optional nested-name-specifier. The simplest way to
8038 implement:
8040 [temp.res]
8042 The keyword `typename' is not permitted in a base-specifier or
8043 mem-initializer; in these contexts a qualified name that
8044 depends on a template-parameter is implicitly assumed to be a
8045 type name.
8047 is to assume that we have seen the `typename' keyword at this
8048 point. */
8049 nested_name_specifier_p
8050 = (cp_parser_nested_name_specifier_opt (parser,
8051 /*typename_keyword_p=*/true,
8052 /*check_dependency_p=*/true,
8053 /*type_p=*/true,
8054 /*is_declaration=*/true)
8055 != NULL_TREE);
8056 if (nested_name_specifier_p)
8057 template_p = cp_parser_optional_template_keyword (parser);
8058 /* If there is a `::' operator or a nested-name-specifier, then we
8059 are definitely looking for a class-name. */
8060 if (global_scope_p || nested_name_specifier_p)
8061 return cp_parser_class_name (parser,
8062 /*typename_keyword_p=*/true,
8063 /*template_keyword_p=*/template_p,
8064 none_type,
8065 /*check_dependency_p=*/true,
8066 /*class_head_p=*/false,
8067 /*is_declaration=*/true);
8068 /* Otherwise, we could also be looking for an ordinary identifier. */
8069 cp_parser_parse_tentatively (parser);
8070 /* Try a class-name. */
8071 id = cp_parser_class_name (parser,
8072 /*typename_keyword_p=*/true,
8073 /*template_keyword_p=*/false,
8074 none_type,
8075 /*check_dependency_p=*/true,
8076 /*class_head_p=*/false,
8077 /*is_declaration=*/true);
8078 /* If we found one, we're done. */
8079 if (cp_parser_parse_definitely (parser))
8080 return id;
8081 /* Otherwise, look for an ordinary identifier. */
8082 return cp_parser_identifier (parser);
8085 /* Overloading [gram.over] */
8087 /* Parse an operator-function-id.
8089 operator-function-id:
8090 operator operator
8092 Returns an IDENTIFIER_NODE for the operator which is a
8093 human-readable spelling of the identifier, e.g., `operator +'. */
8095 static tree
8096 cp_parser_operator_function_id (cp_parser* parser)
8098 /* Look for the `operator' keyword. */
8099 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8100 return error_mark_node;
8101 /* And then the name of the operator itself. */
8102 return cp_parser_operator (parser);
8105 /* Parse an operator.
8107 operator:
8108 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8109 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8110 || ++ -- , ->* -> () []
8112 GNU Extensions:
8114 operator:
8115 <? >? <?= >?=
8117 Returns an IDENTIFIER_NODE for the operator which is a
8118 human-readable spelling of the identifier, e.g., `operator +'. */
8120 static tree
8121 cp_parser_operator (cp_parser* parser)
8123 tree id = NULL_TREE;
8124 cp_token *token;
8126 /* Peek at the next token. */
8127 token = cp_lexer_peek_token (parser->lexer);
8128 /* Figure out which operator we have. */
8129 switch (token->type)
8131 case CPP_KEYWORD:
8133 enum tree_code op;
8135 /* The keyword should be either `new' or `delete'. */
8136 if (token->keyword == RID_NEW)
8137 op = NEW_EXPR;
8138 else if (token->keyword == RID_DELETE)
8139 op = DELETE_EXPR;
8140 else
8141 break;
8143 /* Consume the `new' or `delete' token. */
8144 cp_lexer_consume_token (parser->lexer);
8146 /* Peek at the next token. */
8147 token = cp_lexer_peek_token (parser->lexer);
8148 /* If it's a `[' token then this is the array variant of the
8149 operator. */
8150 if (token->type == CPP_OPEN_SQUARE)
8152 /* Consume the `[' token. */
8153 cp_lexer_consume_token (parser->lexer);
8154 /* Look for the `]' token. */
8155 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8156 id = ansi_opname (op == NEW_EXPR
8157 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8159 /* Otherwise, we have the non-array variant. */
8160 else
8161 id = ansi_opname (op);
8163 return id;
8166 case CPP_PLUS:
8167 id = ansi_opname (PLUS_EXPR);
8168 break;
8170 case CPP_MINUS:
8171 id = ansi_opname (MINUS_EXPR);
8172 break;
8174 case CPP_MULT:
8175 id = ansi_opname (MULT_EXPR);
8176 break;
8178 case CPP_DIV:
8179 id = ansi_opname (TRUNC_DIV_EXPR);
8180 break;
8182 case CPP_MOD:
8183 id = ansi_opname (TRUNC_MOD_EXPR);
8184 break;
8186 case CPP_XOR:
8187 id = ansi_opname (BIT_XOR_EXPR);
8188 break;
8190 case CPP_AND:
8191 id = ansi_opname (BIT_AND_EXPR);
8192 break;
8194 case CPP_OR:
8195 id = ansi_opname (BIT_IOR_EXPR);
8196 break;
8198 case CPP_COMPL:
8199 id = ansi_opname (BIT_NOT_EXPR);
8200 break;
8202 case CPP_NOT:
8203 id = ansi_opname (TRUTH_NOT_EXPR);
8204 break;
8206 case CPP_EQ:
8207 id = ansi_assopname (NOP_EXPR);
8208 break;
8210 case CPP_LESS:
8211 id = ansi_opname (LT_EXPR);
8212 break;
8214 case CPP_GREATER:
8215 id = ansi_opname (GT_EXPR);
8216 break;
8218 case CPP_PLUS_EQ:
8219 id = ansi_assopname (PLUS_EXPR);
8220 break;
8222 case CPP_MINUS_EQ:
8223 id = ansi_assopname (MINUS_EXPR);
8224 break;
8226 case CPP_MULT_EQ:
8227 id = ansi_assopname (MULT_EXPR);
8228 break;
8230 case CPP_DIV_EQ:
8231 id = ansi_assopname (TRUNC_DIV_EXPR);
8232 break;
8234 case CPP_MOD_EQ:
8235 id = ansi_assopname (TRUNC_MOD_EXPR);
8236 break;
8238 case CPP_XOR_EQ:
8239 id = ansi_assopname (BIT_XOR_EXPR);
8240 break;
8242 case CPP_AND_EQ:
8243 id = ansi_assopname (BIT_AND_EXPR);
8244 break;
8246 case CPP_OR_EQ:
8247 id = ansi_assopname (BIT_IOR_EXPR);
8248 break;
8250 case CPP_LSHIFT:
8251 id = ansi_opname (LSHIFT_EXPR);
8252 break;
8254 case CPP_RSHIFT:
8255 id = ansi_opname (RSHIFT_EXPR);
8256 break;
8258 case CPP_LSHIFT_EQ:
8259 id = ansi_assopname (LSHIFT_EXPR);
8260 break;
8262 case CPP_RSHIFT_EQ:
8263 id = ansi_assopname (RSHIFT_EXPR);
8264 break;
8266 case CPP_EQ_EQ:
8267 id = ansi_opname (EQ_EXPR);
8268 break;
8270 case CPP_NOT_EQ:
8271 id = ansi_opname (NE_EXPR);
8272 break;
8274 case CPP_LESS_EQ:
8275 id = ansi_opname (LE_EXPR);
8276 break;
8278 case CPP_GREATER_EQ:
8279 id = ansi_opname (GE_EXPR);
8280 break;
8282 case CPP_AND_AND:
8283 id = ansi_opname (TRUTH_ANDIF_EXPR);
8284 break;
8286 case CPP_OR_OR:
8287 id = ansi_opname (TRUTH_ORIF_EXPR);
8288 break;
8290 case CPP_PLUS_PLUS:
8291 id = ansi_opname (POSTINCREMENT_EXPR);
8292 break;
8294 case CPP_MINUS_MINUS:
8295 id = ansi_opname (PREDECREMENT_EXPR);
8296 break;
8298 case CPP_COMMA:
8299 id = ansi_opname (COMPOUND_EXPR);
8300 break;
8302 case CPP_DEREF_STAR:
8303 id = ansi_opname (MEMBER_REF);
8304 break;
8306 case CPP_DEREF:
8307 id = ansi_opname (COMPONENT_REF);
8308 break;
8310 case CPP_OPEN_PAREN:
8311 /* Consume the `('. */
8312 cp_lexer_consume_token (parser->lexer);
8313 /* Look for the matching `)'. */
8314 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8315 return ansi_opname (CALL_EXPR);
8317 case CPP_OPEN_SQUARE:
8318 /* Consume the `['. */
8319 cp_lexer_consume_token (parser->lexer);
8320 /* Look for the matching `]'. */
8321 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8322 return ansi_opname (ARRAY_REF);
8324 /* Extensions. */
8325 case CPP_MIN:
8326 id = ansi_opname (MIN_EXPR);
8327 cp_parser_warn_min_max ();
8328 break;
8330 case CPP_MAX:
8331 id = ansi_opname (MAX_EXPR);
8332 cp_parser_warn_min_max ();
8333 break;
8335 case CPP_MIN_EQ:
8336 id = ansi_assopname (MIN_EXPR);
8337 cp_parser_warn_min_max ();
8338 break;
8340 case CPP_MAX_EQ:
8341 id = ansi_assopname (MAX_EXPR);
8342 cp_parser_warn_min_max ();
8343 break;
8345 default:
8346 /* Anything else is an error. */
8347 break;
8350 /* If we have selected an identifier, we need to consume the
8351 operator token. */
8352 if (id)
8353 cp_lexer_consume_token (parser->lexer);
8354 /* Otherwise, no valid operator name was present. */
8355 else
8357 cp_parser_error (parser, "expected operator");
8358 id = error_mark_node;
8361 return id;
8364 /* Parse a template-declaration.
8366 template-declaration:
8367 export [opt] template < template-parameter-list > declaration
8369 If MEMBER_P is TRUE, this template-declaration occurs within a
8370 class-specifier.
8372 The grammar rule given by the standard isn't correct. What
8373 is really meant is:
8375 template-declaration:
8376 export [opt] template-parameter-list-seq
8377 decl-specifier-seq [opt] init-declarator [opt] ;
8378 export [opt] template-parameter-list-seq
8379 function-definition
8381 template-parameter-list-seq:
8382 template-parameter-list-seq [opt]
8383 template < template-parameter-list > */
8385 static void
8386 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8388 /* Check for `export'. */
8389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8391 /* Consume the `export' token. */
8392 cp_lexer_consume_token (parser->lexer);
8393 /* Warn that we do not support `export'. */
8394 warning (0, "keyword %<export%> not implemented, and will be ignored");
8397 cp_parser_template_declaration_after_export (parser, member_p);
8400 /* Parse a template-parameter-list.
8402 template-parameter-list:
8403 template-parameter
8404 template-parameter-list , template-parameter
8406 Returns a TREE_LIST. Each node represents a template parameter.
8407 The nodes are connected via their TREE_CHAINs. */
8409 static tree
8410 cp_parser_template_parameter_list (cp_parser* parser)
8412 tree parameter_list = NULL_TREE;
8414 begin_template_parm_list ();
8415 while (true)
8417 tree parameter;
8418 cp_token *token;
8419 bool is_non_type;
8421 /* Parse the template-parameter. */
8422 parameter = cp_parser_template_parameter (parser, &is_non_type);
8423 /* Add it to the list. */
8424 if (parameter != error_mark_node)
8425 parameter_list = process_template_parm (parameter_list,
8426 parameter,
8427 is_non_type);
8428 /* Peek at the next token. */
8429 token = cp_lexer_peek_token (parser->lexer);
8430 /* If it's not a `,', we're done. */
8431 if (token->type != CPP_COMMA)
8432 break;
8433 /* Otherwise, consume the `,' token. */
8434 cp_lexer_consume_token (parser->lexer);
8437 return end_template_parm_list (parameter_list);
8440 /* Parse a template-parameter.
8442 template-parameter:
8443 type-parameter
8444 parameter-declaration
8446 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8447 the parameter. The TREE_PURPOSE is the default value, if any.
8448 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8449 iff this parameter is a non-type parameter. */
8451 static tree
8452 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8454 cp_token *token;
8455 cp_parameter_declarator *parameter_declarator;
8456 tree parm;
8458 /* Assume it is a type parameter or a template parameter. */
8459 *is_non_type = false;
8460 /* Peek at the next token. */
8461 token = cp_lexer_peek_token (parser->lexer);
8462 /* If it is `class' or `template', we have a type-parameter. */
8463 if (token->keyword == RID_TEMPLATE)
8464 return cp_parser_type_parameter (parser);
8465 /* If it is `class' or `typename' we do not know yet whether it is a
8466 type parameter or a non-type parameter. Consider:
8468 template <typename T, typename T::X X> ...
8472 template <class C, class D*> ...
8474 Here, the first parameter is a type parameter, and the second is
8475 a non-type parameter. We can tell by looking at the token after
8476 the identifier -- if it is a `,', `=', or `>' then we have a type
8477 parameter. */
8478 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8480 /* Peek at the token after `class' or `typename'. */
8481 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8482 /* If it's an identifier, skip it. */
8483 if (token->type == CPP_NAME)
8484 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8485 /* Now, see if the token looks like the end of a template
8486 parameter. */
8487 if (token->type == CPP_COMMA
8488 || token->type == CPP_EQ
8489 || token->type == CPP_GREATER)
8490 return cp_parser_type_parameter (parser);
8493 /* Otherwise, it is a non-type parameter.
8495 [temp.param]
8497 When parsing a default template-argument for a non-type
8498 template-parameter, the first non-nested `>' is taken as the end
8499 of the template parameter-list rather than a greater-than
8500 operator. */
8501 *is_non_type = true;
8502 parameter_declarator
8503 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8504 /*parenthesized_p=*/NULL);
8505 parm = grokdeclarator (parameter_declarator->declarator,
8506 &parameter_declarator->decl_specifiers,
8507 PARM, /*initialized=*/0,
8508 /*attrlist=*/NULL);
8509 if (parm == error_mark_node)
8510 return error_mark_node;
8511 return build_tree_list (parameter_declarator->default_argument, parm);
8514 /* Parse a type-parameter.
8516 type-parameter:
8517 class identifier [opt]
8518 class identifier [opt] = type-id
8519 typename identifier [opt]
8520 typename identifier [opt] = type-id
8521 template < template-parameter-list > class identifier [opt]
8522 template < template-parameter-list > class identifier [opt]
8523 = id-expression
8525 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8526 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8527 the declaration of the parameter. */
8529 static tree
8530 cp_parser_type_parameter (cp_parser* parser)
8532 cp_token *token;
8533 tree parameter;
8535 /* Look for a keyword to tell us what kind of parameter this is. */
8536 token = cp_parser_require (parser, CPP_KEYWORD,
8537 "`class', `typename', or `template'");
8538 if (!token)
8539 return error_mark_node;
8541 switch (token->keyword)
8543 case RID_CLASS:
8544 case RID_TYPENAME:
8546 tree identifier;
8547 tree default_argument;
8549 /* If the next token is an identifier, then it names the
8550 parameter. */
8551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8552 identifier = cp_parser_identifier (parser);
8553 else
8554 identifier = NULL_TREE;
8556 /* Create the parameter. */
8557 parameter = finish_template_type_parm (class_type_node, identifier);
8559 /* If the next token is an `=', we have a default argument. */
8560 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8562 /* Consume the `=' token. */
8563 cp_lexer_consume_token (parser->lexer);
8564 /* Parse the default-argument. */
8565 default_argument = cp_parser_type_id (parser);
8567 else
8568 default_argument = NULL_TREE;
8570 /* Create the combined representation of the parameter and the
8571 default argument. */
8572 parameter = build_tree_list (default_argument, parameter);
8574 break;
8576 case RID_TEMPLATE:
8578 tree parameter_list;
8579 tree identifier;
8580 tree default_argument;
8582 /* Look for the `<'. */
8583 cp_parser_require (parser, CPP_LESS, "`<'");
8584 /* Parse the template-parameter-list. */
8585 parameter_list = cp_parser_template_parameter_list (parser);
8586 /* Look for the `>'. */
8587 cp_parser_require (parser, CPP_GREATER, "`>'");
8588 /* Look for the `class' keyword. */
8589 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8590 /* If the next token is an `=', then there is a
8591 default-argument. If the next token is a `>', we are at
8592 the end of the parameter-list. If the next token is a `,',
8593 then we are at the end of this parameter. */
8594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8595 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8596 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8598 identifier = cp_parser_identifier (parser);
8599 /* Treat invalid names as if the parameter were nameless. */
8600 if (identifier == error_mark_node)
8601 identifier = NULL_TREE;
8603 else
8604 identifier = NULL_TREE;
8606 /* Create the template parameter. */
8607 parameter = finish_template_template_parm (class_type_node,
8608 identifier);
8610 /* If the next token is an `=', then there is a
8611 default-argument. */
8612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8614 bool is_template;
8616 /* Consume the `='. */
8617 cp_lexer_consume_token (parser->lexer);
8618 /* Parse the id-expression. */
8619 default_argument
8620 = cp_parser_id_expression (parser,
8621 /*template_keyword_p=*/false,
8622 /*check_dependency_p=*/true,
8623 /*template_p=*/&is_template,
8624 /*declarator_p=*/false);
8625 if (TREE_CODE (default_argument) == TYPE_DECL)
8626 /* If the id-expression was a template-id that refers to
8627 a template-class, we already have the declaration here,
8628 so no further lookup is needed. */
8630 else
8631 /* Look up the name. */
8632 default_argument
8633 = cp_parser_lookup_name (parser, default_argument,
8634 none_type,
8635 /*is_template=*/is_template,
8636 /*is_namespace=*/false,
8637 /*check_dependency=*/true,
8638 /*ambiguous_decls=*/NULL);
8639 /* See if the default argument is valid. */
8640 default_argument
8641 = check_template_template_default_arg (default_argument);
8643 else
8644 default_argument = NULL_TREE;
8646 /* Create the combined representation of the parameter and the
8647 default argument. */
8648 parameter = build_tree_list (default_argument, parameter);
8650 break;
8652 default:
8653 gcc_unreachable ();
8654 break;
8657 return parameter;
8660 /* Parse a template-id.
8662 template-id:
8663 template-name < template-argument-list [opt] >
8665 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8666 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8667 returned. Otherwise, if the template-name names a function, or set
8668 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8669 names a class, returns a TYPE_DECL for the specialization.
8671 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8672 uninstantiated templates. */
8674 static tree
8675 cp_parser_template_id (cp_parser *parser,
8676 bool template_keyword_p,
8677 bool check_dependency_p,
8678 bool is_declaration)
8680 tree template;
8681 tree arguments;
8682 tree template_id;
8683 cp_token_position start_of_id = 0;
8684 tree access_check = NULL_TREE;
8685 cp_token *next_token, *next_token_2;
8686 bool is_identifier;
8688 /* If the next token corresponds to a template-id, there is no need
8689 to reparse it. */
8690 next_token = cp_lexer_peek_token (parser->lexer);
8691 if (next_token->type == CPP_TEMPLATE_ID)
8693 tree value;
8694 tree check;
8696 /* Get the stored value. */
8697 value = cp_lexer_consume_token (parser->lexer)->value;
8698 /* Perform any access checks that were deferred. */
8699 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8700 perform_or_defer_access_check (TREE_PURPOSE (check),
8701 TREE_VALUE (check));
8702 /* Return the stored value. */
8703 return TREE_VALUE (value);
8706 /* Avoid performing name lookup if there is no possibility of
8707 finding a template-id. */
8708 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8709 || (next_token->type == CPP_NAME
8710 && !cp_parser_nth_token_starts_template_argument_list_p
8711 (parser, 2)))
8713 cp_parser_error (parser, "expected template-id");
8714 return error_mark_node;
8717 /* Remember where the template-id starts. */
8718 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8719 start_of_id = cp_lexer_token_position (parser->lexer, false);
8721 push_deferring_access_checks (dk_deferred);
8723 /* Parse the template-name. */
8724 is_identifier = false;
8725 template = cp_parser_template_name (parser, template_keyword_p,
8726 check_dependency_p,
8727 is_declaration,
8728 &is_identifier);
8729 if (template == error_mark_node || is_identifier)
8731 pop_deferring_access_checks ();
8732 return template;
8735 /* If we find the sequence `[:' after a template-name, it's probably
8736 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8737 parse correctly the argument list. */
8738 next_token = cp_lexer_peek_token (parser->lexer);
8739 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8740 if (next_token->type == CPP_OPEN_SQUARE
8741 && next_token->flags & DIGRAPH
8742 && next_token_2->type == CPP_COLON
8743 && !(next_token_2->flags & PREV_WHITE))
8745 cp_parser_parse_tentatively (parser);
8746 /* Change `:' into `::'. */
8747 next_token_2->type = CPP_SCOPE;
8748 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8749 CPP_LESS. */
8750 cp_lexer_consume_token (parser->lexer);
8751 /* Parse the arguments. */
8752 arguments = cp_parser_enclosed_template_argument_list (parser);
8753 if (!cp_parser_parse_definitely (parser))
8755 /* If we couldn't parse an argument list, then we revert our changes
8756 and return simply an error. Maybe this is not a template-id
8757 after all. */
8758 next_token_2->type = CPP_COLON;
8759 cp_parser_error (parser, "expected %<<%>");
8760 pop_deferring_access_checks ();
8761 return error_mark_node;
8763 /* Otherwise, emit an error about the invalid digraph, but continue
8764 parsing because we got our argument list. */
8765 pedwarn ("%<<::%> cannot begin a template-argument list");
8766 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8767 "between %<<%> and %<::%>");
8768 if (!flag_permissive)
8770 static bool hint;
8771 if (!hint)
8773 inform ("(if you use -fpermissive G++ will accept your code)");
8774 hint = true;
8778 else
8780 /* Look for the `<' that starts the template-argument-list. */
8781 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8783 pop_deferring_access_checks ();
8784 return error_mark_node;
8786 /* Parse the arguments. */
8787 arguments = cp_parser_enclosed_template_argument_list (parser);
8790 /* Build a representation of the specialization. */
8791 if (TREE_CODE (template) == IDENTIFIER_NODE)
8792 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8793 else if (DECL_CLASS_TEMPLATE_P (template)
8794 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8795 template_id
8796 = finish_template_type (template, arguments,
8797 cp_lexer_next_token_is (parser->lexer,
8798 CPP_SCOPE));
8799 else
8801 /* If it's not a class-template or a template-template, it should be
8802 a function-template. */
8803 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8804 || TREE_CODE (template) == OVERLOAD
8805 || BASELINK_P (template)));
8807 template_id = lookup_template_function (template, arguments);
8810 /* Retrieve any deferred checks. Do not pop this access checks yet
8811 so the memory will not be reclaimed during token replacing below. */
8812 access_check = get_deferred_access_checks ();
8814 /* If parsing tentatively, replace the sequence of tokens that makes
8815 up the template-id with a CPP_TEMPLATE_ID token. That way,
8816 should we re-parse the token stream, we will not have to repeat
8817 the effort required to do the parse, nor will we issue duplicate
8818 error messages about problems during instantiation of the
8819 template. */
8820 if (start_of_id)
8822 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8824 /* Reset the contents of the START_OF_ID token. */
8825 token->type = CPP_TEMPLATE_ID;
8826 token->value = build_tree_list (access_check, template_id);
8827 token->keyword = RID_MAX;
8829 /* Purge all subsequent tokens. */
8830 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8832 /* ??? Can we actually assume that, if template_id ==
8833 error_mark_node, we will have issued a diagnostic to the
8834 user, as opposed to simply marking the tentative parse as
8835 failed? */
8836 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8837 error ("parse error in template argument list");
8840 pop_deferring_access_checks ();
8841 return template_id;
8844 /* Parse a template-name.
8846 template-name:
8847 identifier
8849 The standard should actually say:
8851 template-name:
8852 identifier
8853 operator-function-id
8855 A defect report has been filed about this issue.
8857 A conversion-function-id cannot be a template name because they cannot
8858 be part of a template-id. In fact, looking at this code:
8860 a.operator K<int>()
8862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8863 It is impossible to call a templated conversion-function-id with an
8864 explicit argument list, since the only allowed template parameter is
8865 the type to which it is converting.
8867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8868 `template' keyword, in a construction like:
8870 T::template f<3>()
8872 In that case `f' is taken to be a template-name, even though there
8873 is no way of knowing for sure.
8875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8876 name refers to a set of overloaded functions, at least one of which
8877 is a template, or an IDENTIFIER_NODE with the name of the template,
8878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8879 names are looked up inside uninstantiated templates. */
8881 static tree
8882 cp_parser_template_name (cp_parser* parser,
8883 bool template_keyword_p,
8884 bool check_dependency_p,
8885 bool is_declaration,
8886 bool *is_identifier)
8888 tree identifier;
8889 tree decl;
8890 tree fns;
8892 /* If the next token is `operator', then we have either an
8893 operator-function-id or a conversion-function-id. */
8894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8896 /* We don't know whether we're looking at an
8897 operator-function-id or a conversion-function-id. */
8898 cp_parser_parse_tentatively (parser);
8899 /* Try an operator-function-id. */
8900 identifier = cp_parser_operator_function_id (parser);
8901 /* If that didn't work, try a conversion-function-id. */
8902 if (!cp_parser_parse_definitely (parser))
8904 cp_parser_error (parser, "expected template-name");
8905 return error_mark_node;
8908 /* Look for the identifier. */
8909 else
8910 identifier = cp_parser_identifier (parser);
8912 /* If we didn't find an identifier, we don't have a template-id. */
8913 if (identifier == error_mark_node)
8914 return error_mark_node;
8916 /* If the name immediately followed the `template' keyword, then it
8917 is a template-name. However, if the next token is not `<', then
8918 we do not treat it as a template-name, since it is not being used
8919 as part of a template-id. This enables us to handle constructs
8920 like:
8922 template <typename T> struct S { S(); };
8923 template <typename T> S<T>::S();
8925 correctly. We would treat `S' as a template -- if it were `S<T>'
8926 -- but we do not if there is no `<'. */
8928 if (processing_template_decl
8929 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8931 /* In a declaration, in a dependent context, we pretend that the
8932 "template" keyword was present in order to improve error
8933 recovery. For example, given:
8935 template <typename T> void f(T::X<int>);
8937 we want to treat "X<int>" as a template-id. */
8938 if (is_declaration
8939 && !template_keyword_p
8940 && parser->scope && TYPE_P (parser->scope)
8941 && check_dependency_p
8942 && dependent_type_p (parser->scope)
8943 /* Do not do this for dtors (or ctors), since they never
8944 need the template keyword before their name. */
8945 && !constructor_name_p (identifier, parser->scope))
8947 cp_token_position start = 0;
8949 /* Explain what went wrong. */
8950 error ("non-template %qD used as template", identifier);
8951 inform ("use %<%T::template %D%> to indicate that it is a template",
8952 parser->scope, identifier);
8953 /* If parsing tentatively, find the location of the "<" token. */
8954 if (cp_parser_simulate_error (parser))
8955 start = cp_lexer_token_position (parser->lexer, true);
8956 /* Parse the template arguments so that we can issue error
8957 messages about them. */
8958 cp_lexer_consume_token (parser->lexer);
8959 cp_parser_enclosed_template_argument_list (parser);
8960 /* Skip tokens until we find a good place from which to
8961 continue parsing. */
8962 cp_parser_skip_to_closing_parenthesis (parser,
8963 /*recovering=*/true,
8964 /*or_comma=*/true,
8965 /*consume_paren=*/false);
8966 /* If parsing tentatively, permanently remove the
8967 template argument list. That will prevent duplicate
8968 error messages from being issued about the missing
8969 "template" keyword. */
8970 if (start)
8971 cp_lexer_purge_tokens_after (parser->lexer, start);
8972 if (is_identifier)
8973 *is_identifier = true;
8974 return identifier;
8977 /* If the "template" keyword is present, then there is generally
8978 no point in doing name-lookup, so we just return IDENTIFIER.
8979 But, if the qualifying scope is non-dependent then we can
8980 (and must) do name-lookup normally. */
8981 if (template_keyword_p
8982 && (!parser->scope
8983 || (TYPE_P (parser->scope)
8984 && dependent_type_p (parser->scope))))
8985 return identifier;
8988 /* Look up the name. */
8989 decl = cp_parser_lookup_name (parser, identifier,
8990 none_type,
8991 /*is_template=*/false,
8992 /*is_namespace=*/false,
8993 check_dependency_p,
8994 /*ambiguous_decls=*/NULL);
8995 decl = maybe_get_template_decl_from_type_decl (decl);
8997 /* If DECL is a template, then the name was a template-name. */
8998 if (TREE_CODE (decl) == TEMPLATE_DECL)
9000 else
9002 tree fn = NULL_TREE;
9004 /* The standard does not explicitly indicate whether a name that
9005 names a set of overloaded declarations, some of which are
9006 templates, is a template-name. However, such a name should
9007 be a template-name; otherwise, there is no way to form a
9008 template-id for the overloaded templates. */
9009 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9010 if (TREE_CODE (fns) == OVERLOAD)
9011 for (fn = fns; fn; fn = OVL_NEXT (fn))
9012 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9013 break;
9015 if (!fn)
9017 /* The name does not name a template. */
9018 cp_parser_error (parser, "expected template-name");
9019 return error_mark_node;
9023 /* If DECL is dependent, and refers to a function, then just return
9024 its name; we will look it up again during template instantiation. */
9025 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9027 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9028 if (TYPE_P (scope) && dependent_type_p (scope))
9029 return identifier;
9032 return decl;
9035 /* Parse a template-argument-list.
9037 template-argument-list:
9038 template-argument
9039 template-argument-list , template-argument
9041 Returns a TREE_VEC containing the arguments. */
9043 static tree
9044 cp_parser_template_argument_list (cp_parser* parser)
9046 tree fixed_args[10];
9047 unsigned n_args = 0;
9048 unsigned alloced = 10;
9049 tree *arg_ary = fixed_args;
9050 tree vec;
9051 bool saved_in_template_argument_list_p;
9052 bool saved_ice_p;
9053 bool saved_non_ice_p;
9055 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9056 parser->in_template_argument_list_p = true;
9057 /* Even if the template-id appears in an integral
9058 constant-expression, the contents of the argument list do
9059 not. */
9060 saved_ice_p = parser->integral_constant_expression_p;
9061 parser->integral_constant_expression_p = false;
9062 saved_non_ice_p = parser->non_integral_constant_expression_p;
9063 parser->non_integral_constant_expression_p = false;
9064 /* Parse the arguments. */
9067 tree argument;
9069 if (n_args)
9070 /* Consume the comma. */
9071 cp_lexer_consume_token (parser->lexer);
9073 /* Parse the template-argument. */
9074 argument = cp_parser_template_argument (parser);
9075 if (n_args == alloced)
9077 alloced *= 2;
9079 if (arg_ary == fixed_args)
9081 arg_ary = XNEWVEC (tree, alloced);
9082 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9084 else
9085 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9087 arg_ary[n_args++] = argument;
9089 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9091 vec = make_tree_vec (n_args);
9093 while (n_args--)
9094 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9096 if (arg_ary != fixed_args)
9097 free (arg_ary);
9098 parser->non_integral_constant_expression_p = saved_non_ice_p;
9099 parser->integral_constant_expression_p = saved_ice_p;
9100 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9101 return vec;
9104 /* Parse a template-argument.
9106 template-argument:
9107 assignment-expression
9108 type-id
9109 id-expression
9111 The representation is that of an assignment-expression, type-id, or
9112 id-expression -- except that the qualified id-expression is
9113 evaluated, so that the value returned is either a DECL or an
9114 OVERLOAD.
9116 Although the standard says "assignment-expression", it forbids
9117 throw-expressions or assignments in the template argument.
9118 Therefore, we use "conditional-expression" instead. */
9120 static tree
9121 cp_parser_template_argument (cp_parser* parser)
9123 tree argument;
9124 bool template_p;
9125 bool address_p;
9126 bool maybe_type_id = false;
9127 cp_token *token;
9128 cp_id_kind idk;
9130 /* There's really no way to know what we're looking at, so we just
9131 try each alternative in order.
9133 [temp.arg]
9135 In a template-argument, an ambiguity between a type-id and an
9136 expression is resolved to a type-id, regardless of the form of
9137 the corresponding template-parameter.
9139 Therefore, we try a type-id first. */
9140 cp_parser_parse_tentatively (parser);
9141 argument = cp_parser_type_id (parser);
9142 /* If there was no error parsing the type-id but the next token is a '>>',
9143 we probably found a typo for '> >'. But there are type-id which are
9144 also valid expressions. For instance:
9146 struct X { int operator >> (int); };
9147 template <int V> struct Foo {};
9148 Foo<X () >> 5> r;
9150 Here 'X()' is a valid type-id of a function type, but the user just
9151 wanted to write the expression "X() >> 5". Thus, we remember that we
9152 found a valid type-id, but we still try to parse the argument as an
9153 expression to see what happens. */
9154 if (!cp_parser_error_occurred (parser)
9155 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9157 maybe_type_id = true;
9158 cp_parser_abort_tentative_parse (parser);
9160 else
9162 /* If the next token isn't a `,' or a `>', then this argument wasn't
9163 really finished. This means that the argument is not a valid
9164 type-id. */
9165 if (!cp_parser_next_token_ends_template_argument_p (parser))
9166 cp_parser_error (parser, "expected template-argument");
9167 /* If that worked, we're done. */
9168 if (cp_parser_parse_definitely (parser))
9169 return argument;
9171 /* We're still not sure what the argument will be. */
9172 cp_parser_parse_tentatively (parser);
9173 /* Try a template. */
9174 argument = cp_parser_id_expression (parser,
9175 /*template_keyword_p=*/false,
9176 /*check_dependency_p=*/true,
9177 &template_p,
9178 /*declarator_p=*/false);
9179 /* If the next token isn't a `,' or a `>', then this argument wasn't
9180 really finished. */
9181 if (!cp_parser_next_token_ends_template_argument_p (parser))
9182 cp_parser_error (parser, "expected template-argument");
9183 if (!cp_parser_error_occurred (parser))
9185 /* Figure out what is being referred to. If the id-expression
9186 was for a class template specialization, then we will have a
9187 TYPE_DECL at this point. There is no need to do name lookup
9188 at this point in that case. */
9189 if (TREE_CODE (argument) != TYPE_DECL)
9190 argument = cp_parser_lookup_name (parser, argument,
9191 none_type,
9192 /*is_template=*/template_p,
9193 /*is_namespace=*/false,
9194 /*check_dependency=*/true,
9195 /*ambiguous_decls=*/NULL);
9196 if (TREE_CODE (argument) != TEMPLATE_DECL
9197 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9198 cp_parser_error (parser, "expected template-name");
9200 if (cp_parser_parse_definitely (parser))
9201 return argument;
9202 /* It must be a non-type argument. There permitted cases are given
9203 in [temp.arg.nontype]:
9205 -- an integral constant-expression of integral or enumeration
9206 type; or
9208 -- the name of a non-type template-parameter; or
9210 -- the name of an object or function with external linkage...
9212 -- the address of an object or function with external linkage...
9214 -- a pointer to member... */
9215 /* Look for a non-type template parameter. */
9216 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9218 cp_parser_parse_tentatively (parser);
9219 argument = cp_parser_primary_expression (parser,
9220 /*adress_p=*/false,
9221 /*cast_p=*/false,
9222 /*template_arg_p=*/true,
9223 &idk);
9224 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9225 || !cp_parser_next_token_ends_template_argument_p (parser))
9226 cp_parser_simulate_error (parser);
9227 if (cp_parser_parse_definitely (parser))
9228 return argument;
9231 /* If the next token is "&", the argument must be the address of an
9232 object or function with external linkage. */
9233 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9234 if (address_p)
9235 cp_lexer_consume_token (parser->lexer);
9236 /* See if we might have an id-expression. */
9237 token = cp_lexer_peek_token (parser->lexer);
9238 if (token->type == CPP_NAME
9239 || token->keyword == RID_OPERATOR
9240 || token->type == CPP_SCOPE
9241 || token->type == CPP_TEMPLATE_ID
9242 || token->type == CPP_NESTED_NAME_SPECIFIER)
9244 cp_parser_parse_tentatively (parser);
9245 argument = cp_parser_primary_expression (parser,
9246 address_p,
9247 /*cast_p=*/false,
9248 /*template_arg_p=*/true,
9249 &idk);
9250 if (cp_parser_error_occurred (parser)
9251 || !cp_parser_next_token_ends_template_argument_p (parser))
9252 cp_parser_abort_tentative_parse (parser);
9253 else
9255 if (TREE_CODE (argument) == INDIRECT_REF)
9257 gcc_assert (REFERENCE_REF_P (argument));
9258 argument = TREE_OPERAND (argument, 0);
9261 if (TREE_CODE (argument) == BASELINK)
9262 /* We don't need the information about what class was used
9263 to name the overloaded functions. */
9264 argument = BASELINK_FUNCTIONS (argument);
9266 if (TREE_CODE (argument) == VAR_DECL)
9268 /* A variable without external linkage might still be a
9269 valid constant-expression, so no error is issued here
9270 if the external-linkage check fails. */
9271 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9272 cp_parser_simulate_error (parser);
9274 else if (is_overloaded_fn (argument))
9275 /* All overloaded functions are allowed; if the external
9276 linkage test does not pass, an error will be issued
9277 later. */
9279 else if (address_p
9280 && (TREE_CODE (argument) == OFFSET_REF
9281 || TREE_CODE (argument) == SCOPE_REF))
9282 /* A pointer-to-member. */
9284 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9286 else
9287 cp_parser_simulate_error (parser);
9289 if (cp_parser_parse_definitely (parser))
9291 if (address_p)
9292 argument = build_x_unary_op (ADDR_EXPR, argument);
9293 return argument;
9297 /* If the argument started with "&", there are no other valid
9298 alternatives at this point. */
9299 if (address_p)
9301 cp_parser_error (parser, "invalid non-type template argument");
9302 return error_mark_node;
9305 /* If the argument wasn't successfully parsed as a type-id followed
9306 by '>>', the argument can only be a constant expression now.
9307 Otherwise, we try parsing the constant-expression tentatively,
9308 because the argument could really be a type-id. */
9309 if (maybe_type_id)
9310 cp_parser_parse_tentatively (parser);
9311 argument = cp_parser_constant_expression (parser,
9312 /*allow_non_constant_p=*/false,
9313 /*non_constant_p=*/NULL);
9314 argument = fold_non_dependent_expr (argument);
9315 if (!maybe_type_id)
9316 return argument;
9317 if (!cp_parser_next_token_ends_template_argument_p (parser))
9318 cp_parser_error (parser, "expected template-argument");
9319 if (cp_parser_parse_definitely (parser))
9320 return argument;
9321 /* We did our best to parse the argument as a non type-id, but that
9322 was the only alternative that matched (albeit with a '>' after
9323 it). We can assume it's just a typo from the user, and a
9324 diagnostic will then be issued. */
9325 return cp_parser_type_id (parser);
9328 /* Parse an explicit-instantiation.
9330 explicit-instantiation:
9331 template declaration
9333 Although the standard says `declaration', what it really means is:
9335 explicit-instantiation:
9336 template decl-specifier-seq [opt] declarator [opt] ;
9338 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9339 supposed to be allowed. A defect report has been filed about this
9340 issue.
9342 GNU Extension:
9344 explicit-instantiation:
9345 storage-class-specifier template
9346 decl-specifier-seq [opt] declarator [opt] ;
9347 function-specifier template
9348 decl-specifier-seq [opt] declarator [opt] ; */
9350 static void
9351 cp_parser_explicit_instantiation (cp_parser* parser)
9353 int declares_class_or_enum;
9354 cp_decl_specifier_seq decl_specifiers;
9355 tree extension_specifier = NULL_TREE;
9357 /* Look for an (optional) storage-class-specifier or
9358 function-specifier. */
9359 if (cp_parser_allow_gnu_extensions_p (parser))
9361 extension_specifier
9362 = cp_parser_storage_class_specifier_opt (parser);
9363 if (!extension_specifier)
9364 extension_specifier
9365 = cp_parser_function_specifier_opt (parser,
9366 /*decl_specs=*/NULL);
9369 /* Look for the `template' keyword. */
9370 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9371 /* Let the front end know that we are processing an explicit
9372 instantiation. */
9373 begin_explicit_instantiation ();
9374 /* [temp.explicit] says that we are supposed to ignore access
9375 control while processing explicit instantiation directives. */
9376 push_deferring_access_checks (dk_no_check);
9377 /* Parse a decl-specifier-seq. */
9378 cp_parser_decl_specifier_seq (parser,
9379 CP_PARSER_FLAGS_OPTIONAL,
9380 &decl_specifiers,
9381 &declares_class_or_enum);
9382 /* If there was exactly one decl-specifier, and it declared a class,
9383 and there's no declarator, then we have an explicit type
9384 instantiation. */
9385 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9387 tree type;
9389 type = check_tag_decl (&decl_specifiers);
9390 /* Turn access control back on for names used during
9391 template instantiation. */
9392 pop_deferring_access_checks ();
9393 if (type)
9394 do_type_instantiation (type, extension_specifier,
9395 /*complain=*/tf_error);
9397 else
9399 cp_declarator *declarator;
9400 tree decl;
9402 /* Parse the declarator. */
9403 declarator
9404 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9405 /*ctor_dtor_or_conv_p=*/NULL,
9406 /*parenthesized_p=*/NULL,
9407 /*member_p=*/false);
9408 if (declares_class_or_enum & 2)
9409 cp_parser_check_for_definition_in_return_type (declarator,
9410 decl_specifiers.type);
9411 if (declarator != cp_error_declarator)
9413 decl = grokdeclarator (declarator, &decl_specifiers,
9414 NORMAL, 0, NULL);
9415 /* Turn access control back on for names used during
9416 template instantiation. */
9417 pop_deferring_access_checks ();
9418 /* Do the explicit instantiation. */
9419 do_decl_instantiation (decl, extension_specifier);
9421 else
9423 pop_deferring_access_checks ();
9424 /* Skip the body of the explicit instantiation. */
9425 cp_parser_skip_to_end_of_statement (parser);
9428 /* We're done with the instantiation. */
9429 end_explicit_instantiation ();
9431 cp_parser_consume_semicolon_at_end_of_statement (parser);
9434 /* Parse an explicit-specialization.
9436 explicit-specialization:
9437 template < > declaration
9439 Although the standard says `declaration', what it really means is:
9441 explicit-specialization:
9442 template <> decl-specifier [opt] init-declarator [opt] ;
9443 template <> function-definition
9444 template <> explicit-specialization
9445 template <> template-declaration */
9447 static void
9448 cp_parser_explicit_specialization (cp_parser* parser)
9450 bool need_lang_pop;
9451 /* Look for the `template' keyword. */
9452 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9453 /* Look for the `<'. */
9454 cp_parser_require (parser, CPP_LESS, "`<'");
9455 /* Look for the `>'. */
9456 cp_parser_require (parser, CPP_GREATER, "`>'");
9457 /* We have processed another parameter list. */
9458 ++parser->num_template_parameter_lists;
9459 /* [temp]
9461 A template ... explicit specialization ... shall not have C
9462 linkage. */
9463 if (current_lang_name == lang_name_c)
9465 error ("template specialization with C linkage");
9466 /* Give it C++ linkage to avoid confusing other parts of the
9467 front end. */
9468 push_lang_context (lang_name_cplusplus);
9469 need_lang_pop = true;
9471 else
9472 need_lang_pop = false;
9473 /* Let the front end know that we are beginning a specialization. */
9474 begin_specialization ();
9475 /* If the next keyword is `template', we need to figure out whether
9476 or not we're looking a template-declaration. */
9477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9479 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9480 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9481 cp_parser_template_declaration_after_export (parser,
9482 /*member_p=*/false);
9483 else
9484 cp_parser_explicit_specialization (parser);
9486 else
9487 /* Parse the dependent declaration. */
9488 cp_parser_single_declaration (parser,
9489 /*member_p=*/false,
9490 /*friend_p=*/NULL);
9491 /* We're done with the specialization. */
9492 end_specialization ();
9493 /* For the erroneous case of a template with C linkage, we pushed an
9494 implicit C++ linkage scope; exit that scope now. */
9495 if (need_lang_pop)
9496 pop_lang_context ();
9497 /* We're done with this parameter list. */
9498 --parser->num_template_parameter_lists;
9501 /* Parse a type-specifier.
9503 type-specifier:
9504 simple-type-specifier
9505 class-specifier
9506 enum-specifier
9507 elaborated-type-specifier
9508 cv-qualifier
9510 GNU Extension:
9512 type-specifier:
9513 __complex__
9515 Returns a representation of the type-specifier. For a
9516 class-specifier, enum-specifier, or elaborated-type-specifier, a
9517 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9519 The parser flags FLAGS is used to control type-specifier parsing.
9521 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9522 in a decl-specifier-seq.
9524 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9525 class-specifier, enum-specifier, or elaborated-type-specifier, then
9526 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9527 if a type is declared; 2 if it is defined. Otherwise, it is set to
9528 zero.
9530 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9531 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9532 is set to FALSE. */
9534 static tree
9535 cp_parser_type_specifier (cp_parser* parser,
9536 cp_parser_flags flags,
9537 cp_decl_specifier_seq *decl_specs,
9538 bool is_declaration,
9539 int* declares_class_or_enum,
9540 bool* is_cv_qualifier)
9542 tree type_spec = NULL_TREE;
9543 cp_token *token;
9544 enum rid keyword;
9545 cp_decl_spec ds = ds_last;
9547 /* Assume this type-specifier does not declare a new type. */
9548 if (declares_class_or_enum)
9549 *declares_class_or_enum = 0;
9550 /* And that it does not specify a cv-qualifier. */
9551 if (is_cv_qualifier)
9552 *is_cv_qualifier = false;
9553 /* Peek at the next token. */
9554 token = cp_lexer_peek_token (parser->lexer);
9556 /* If we're looking at a keyword, we can use that to guide the
9557 production we choose. */
9558 keyword = token->keyword;
9559 switch (keyword)
9561 case RID_ENUM:
9562 /* 'enum' [identifier] '{' introduces an enum-specifier;
9563 'enum' <anything else> introduces an elaborated-type-specifier. */
9564 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9565 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9566 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9567 == CPP_OPEN_BRACE))
9569 if (parser->num_template_parameter_lists)
9571 error ("template declaration of %qs", "enum");
9572 cp_parser_skip_to_end_of_block_or_statement (parser);
9573 type_spec = error_mark_node;
9575 else
9576 type_spec = cp_parser_enum_specifier (parser);
9578 if (declares_class_or_enum)
9579 *declares_class_or_enum = 2;
9580 if (decl_specs)
9581 cp_parser_set_decl_spec_type (decl_specs,
9582 type_spec,
9583 /*user_defined_p=*/true);
9584 return type_spec;
9586 else
9587 goto elaborated_type_specifier;
9589 /* Any of these indicate either a class-specifier, or an
9590 elaborated-type-specifier. */
9591 case RID_CLASS:
9592 case RID_STRUCT:
9593 case RID_UNION:
9594 /* Parse tentatively so that we can back up if we don't find a
9595 class-specifier. */
9596 cp_parser_parse_tentatively (parser);
9597 /* Look for the class-specifier. */
9598 type_spec = cp_parser_class_specifier (parser);
9599 /* If that worked, we're done. */
9600 if (cp_parser_parse_definitely (parser))
9602 if (declares_class_or_enum)
9603 *declares_class_or_enum = 2;
9604 if (decl_specs)
9605 cp_parser_set_decl_spec_type (decl_specs,
9606 type_spec,
9607 /*user_defined_p=*/true);
9608 return type_spec;
9611 /* Fall through. */
9612 elaborated_type_specifier:
9613 /* We're declaring (not defining) a class or enum. */
9614 if (declares_class_or_enum)
9615 *declares_class_or_enum = 1;
9617 /* Fall through. */
9618 case RID_TYPENAME:
9619 /* Look for an elaborated-type-specifier. */
9620 type_spec
9621 = (cp_parser_elaborated_type_specifier
9622 (parser,
9623 decl_specs && decl_specs->specs[(int) ds_friend],
9624 is_declaration));
9625 if (decl_specs)
9626 cp_parser_set_decl_spec_type (decl_specs,
9627 type_spec,
9628 /*user_defined_p=*/true);
9629 return type_spec;
9631 case RID_CONST:
9632 ds = ds_const;
9633 if (is_cv_qualifier)
9634 *is_cv_qualifier = true;
9635 break;
9637 case RID_VOLATILE:
9638 ds = ds_volatile;
9639 if (is_cv_qualifier)
9640 *is_cv_qualifier = true;
9641 break;
9643 case RID_RESTRICT:
9644 ds = ds_restrict;
9645 if (is_cv_qualifier)
9646 *is_cv_qualifier = true;
9647 break;
9649 case RID_COMPLEX:
9650 /* The `__complex__' keyword is a GNU extension. */
9651 ds = ds_complex;
9652 break;
9654 default:
9655 break;
9658 /* Handle simple keywords. */
9659 if (ds != ds_last)
9661 if (decl_specs)
9663 ++decl_specs->specs[(int)ds];
9664 decl_specs->any_specifiers_p = true;
9666 return cp_lexer_consume_token (parser->lexer)->value;
9669 /* If we do not already have a type-specifier, assume we are looking
9670 at a simple-type-specifier. */
9671 type_spec = cp_parser_simple_type_specifier (parser,
9672 decl_specs,
9673 flags);
9675 /* If we didn't find a type-specifier, and a type-specifier was not
9676 optional in this context, issue an error message. */
9677 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9679 cp_parser_error (parser, "expected type specifier");
9680 return error_mark_node;
9683 return type_spec;
9686 /* Parse a simple-type-specifier.
9688 simple-type-specifier:
9689 :: [opt] nested-name-specifier [opt] type-name
9690 :: [opt] nested-name-specifier template template-id
9691 char
9692 wchar_t
9693 bool
9694 short
9696 long
9697 signed
9698 unsigned
9699 float
9700 double
9701 void
9703 GNU Extension:
9705 simple-type-specifier:
9706 __typeof__ unary-expression
9707 __typeof__ ( type-id )
9709 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9710 appropriately updated. */
9712 static tree
9713 cp_parser_simple_type_specifier (cp_parser* parser,
9714 cp_decl_specifier_seq *decl_specs,
9715 cp_parser_flags flags)
9717 tree type = NULL_TREE;
9718 cp_token *token;
9720 /* Peek at the next token. */
9721 token = cp_lexer_peek_token (parser->lexer);
9723 /* If we're looking at a keyword, things are easy. */
9724 switch (token->keyword)
9726 case RID_CHAR:
9727 if (decl_specs)
9728 decl_specs->explicit_char_p = true;
9729 type = char_type_node;
9730 break;
9731 case RID_WCHAR:
9732 type = wchar_type_node;
9733 break;
9734 case RID_BOOL:
9735 type = boolean_type_node;
9736 break;
9737 case RID_SHORT:
9738 if (decl_specs)
9739 ++decl_specs->specs[(int) ds_short];
9740 type = short_integer_type_node;
9741 break;
9742 case RID_INT:
9743 if (decl_specs)
9744 decl_specs->explicit_int_p = true;
9745 type = integer_type_node;
9746 break;
9747 case RID_LONG:
9748 if (decl_specs)
9749 ++decl_specs->specs[(int) ds_long];
9750 type = long_integer_type_node;
9751 break;
9752 case RID_SIGNED:
9753 if (decl_specs)
9754 ++decl_specs->specs[(int) ds_signed];
9755 type = integer_type_node;
9756 break;
9757 case RID_UNSIGNED:
9758 if (decl_specs)
9759 ++decl_specs->specs[(int) ds_unsigned];
9760 type = unsigned_type_node;
9761 break;
9762 case RID_FLOAT:
9763 type = float_type_node;
9764 break;
9765 case RID_DOUBLE:
9766 type = double_type_node;
9767 break;
9768 case RID_VOID:
9769 type = void_type_node;
9770 break;
9772 case RID_TYPEOF:
9773 /* Consume the `typeof' token. */
9774 cp_lexer_consume_token (parser->lexer);
9775 /* Parse the operand to `typeof'. */
9776 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9777 /* If it is not already a TYPE, take its type. */
9778 if (!TYPE_P (type))
9779 type = finish_typeof (type);
9781 if (decl_specs)
9782 cp_parser_set_decl_spec_type (decl_specs, type,
9783 /*user_defined_p=*/true);
9785 return type;
9787 default:
9788 break;
9791 /* If the type-specifier was for a built-in type, we're done. */
9792 if (type)
9794 tree id;
9796 /* Record the type. */
9797 if (decl_specs
9798 && (token->keyword != RID_SIGNED
9799 && token->keyword != RID_UNSIGNED
9800 && token->keyword != RID_SHORT
9801 && token->keyword != RID_LONG))
9802 cp_parser_set_decl_spec_type (decl_specs,
9803 type,
9804 /*user_defined=*/false);
9805 if (decl_specs)
9806 decl_specs->any_specifiers_p = true;
9808 /* Consume the token. */
9809 id = cp_lexer_consume_token (parser->lexer)->value;
9811 /* There is no valid C++ program where a non-template type is
9812 followed by a "<". That usually indicates that the user thought
9813 that the type was a template. */
9814 cp_parser_check_for_invalid_template_id (parser, type);
9816 return TYPE_NAME (type);
9819 /* The type-specifier must be a user-defined type. */
9820 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9822 bool qualified_p;
9823 bool global_p;
9825 /* Don't gobble tokens or issue error messages if this is an
9826 optional type-specifier. */
9827 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9828 cp_parser_parse_tentatively (parser);
9830 /* Look for the optional `::' operator. */
9831 global_p
9832 = (cp_parser_global_scope_opt (parser,
9833 /*current_scope_valid_p=*/false)
9834 != NULL_TREE);
9835 /* Look for the nested-name specifier. */
9836 qualified_p
9837 = (cp_parser_nested_name_specifier_opt (parser,
9838 /*typename_keyword_p=*/false,
9839 /*check_dependency_p=*/true,
9840 /*type_p=*/false,
9841 /*is_declaration=*/false)
9842 != NULL_TREE);
9843 /* If we have seen a nested-name-specifier, and the next token
9844 is `template', then we are using the template-id production. */
9845 if (parser->scope
9846 && cp_parser_optional_template_keyword (parser))
9848 /* Look for the template-id. */
9849 type = cp_parser_template_id (parser,
9850 /*template_keyword_p=*/true,
9851 /*check_dependency_p=*/true,
9852 /*is_declaration=*/false);
9853 /* If the template-id did not name a type, we are out of
9854 luck. */
9855 if (TREE_CODE (type) != TYPE_DECL)
9857 cp_parser_error (parser, "expected template-id for type");
9858 type = NULL_TREE;
9861 /* Otherwise, look for a type-name. */
9862 else
9863 type = cp_parser_type_name (parser);
9864 /* Keep track of all name-lookups performed in class scopes. */
9865 if (type
9866 && !global_p
9867 && !qualified_p
9868 && TREE_CODE (type) == TYPE_DECL
9869 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9870 maybe_note_name_used_in_class (DECL_NAME (type), type);
9871 /* If it didn't work out, we don't have a TYPE. */
9872 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9873 && !cp_parser_parse_definitely (parser))
9874 type = NULL_TREE;
9875 if (type && decl_specs)
9876 cp_parser_set_decl_spec_type (decl_specs, type,
9877 /*user_defined=*/true);
9880 /* If we didn't get a type-name, issue an error message. */
9881 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9883 cp_parser_error (parser, "expected type-name");
9884 return error_mark_node;
9887 /* There is no valid C++ program where a non-template type is
9888 followed by a "<". That usually indicates that the user thought
9889 that the type was a template. */
9890 if (type && type != error_mark_node)
9892 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9893 If it is, then the '<'...'>' enclose protocol names rather than
9894 template arguments, and so everything is fine. */
9895 if (c_dialect_objc ()
9896 && (objc_is_id (type) || objc_is_class_name (type)))
9898 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9899 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9901 /* Clobber the "unqualified" type previously entered into
9902 DECL_SPECS with the new, improved protocol-qualified version. */
9903 if (decl_specs)
9904 decl_specs->type = qual_type;
9906 return qual_type;
9909 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9912 return type;
9915 /* Parse a type-name.
9917 type-name:
9918 class-name
9919 enum-name
9920 typedef-name
9922 enum-name:
9923 identifier
9925 typedef-name:
9926 identifier
9928 Returns a TYPE_DECL for the type. */
9930 static tree
9931 cp_parser_type_name (cp_parser* parser)
9933 tree type_decl;
9934 tree identifier;
9936 /* We can't know yet whether it is a class-name or not. */
9937 cp_parser_parse_tentatively (parser);
9938 /* Try a class-name. */
9939 type_decl = cp_parser_class_name (parser,
9940 /*typename_keyword_p=*/false,
9941 /*template_keyword_p=*/false,
9942 none_type,
9943 /*check_dependency_p=*/true,
9944 /*class_head_p=*/false,
9945 /*is_declaration=*/false);
9946 /* If it's not a class-name, keep looking. */
9947 if (!cp_parser_parse_definitely (parser))
9949 /* It must be a typedef-name or an enum-name. */
9950 identifier = cp_parser_identifier (parser);
9951 if (identifier == error_mark_node)
9952 return error_mark_node;
9954 /* Look up the type-name. */
9955 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9957 if (TREE_CODE (type_decl) != TYPE_DECL
9958 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9960 /* See if this is an Objective-C type. */
9961 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9962 tree type = objc_get_protocol_qualified_type (identifier, protos);
9963 if (type)
9964 type_decl = TYPE_NAME (type);
9967 /* Issue an error if we did not find a type-name. */
9968 if (TREE_CODE (type_decl) != TYPE_DECL)
9970 if (!cp_parser_simulate_error (parser))
9971 cp_parser_name_lookup_error (parser, identifier, type_decl,
9972 "is not a type");
9973 type_decl = error_mark_node;
9975 /* Remember that the name was used in the definition of the
9976 current class so that we can check later to see if the
9977 meaning would have been different after the class was
9978 entirely defined. */
9979 else if (type_decl != error_mark_node
9980 && !parser->scope)
9981 maybe_note_name_used_in_class (identifier, type_decl);
9984 return type_decl;
9988 /* Parse an elaborated-type-specifier. Note that the grammar given
9989 here incorporates the resolution to DR68.
9991 elaborated-type-specifier:
9992 class-key :: [opt] nested-name-specifier [opt] identifier
9993 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9994 enum :: [opt] nested-name-specifier [opt] identifier
9995 typename :: [opt] nested-name-specifier identifier
9996 typename :: [opt] nested-name-specifier template [opt]
9997 template-id
9999 GNU extension:
10001 elaborated-type-specifier:
10002 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10003 class-key attributes :: [opt] nested-name-specifier [opt]
10004 template [opt] template-id
10005 enum attributes :: [opt] nested-name-specifier [opt] identifier
10007 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10008 declared `friend'. If IS_DECLARATION is TRUE, then this
10009 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10010 something is being declared.
10012 Returns the TYPE specified. */
10014 static tree
10015 cp_parser_elaborated_type_specifier (cp_parser* parser,
10016 bool is_friend,
10017 bool is_declaration)
10019 enum tag_types tag_type;
10020 tree identifier;
10021 tree type = NULL_TREE;
10022 tree attributes = NULL_TREE;
10024 /* See if we're looking at the `enum' keyword. */
10025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10027 /* Consume the `enum' token. */
10028 cp_lexer_consume_token (parser->lexer);
10029 /* Remember that it's an enumeration type. */
10030 tag_type = enum_type;
10031 /* Parse the attributes. */
10032 attributes = cp_parser_attributes_opt (parser);
10034 /* Or, it might be `typename'. */
10035 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10036 RID_TYPENAME))
10038 /* Consume the `typename' token. */
10039 cp_lexer_consume_token (parser->lexer);
10040 /* Remember that it's a `typename' type. */
10041 tag_type = typename_type;
10042 /* The `typename' keyword is only allowed in templates. */
10043 if (!processing_template_decl)
10044 pedwarn ("using %<typename%> outside of template");
10046 /* Otherwise it must be a class-key. */
10047 else
10049 tag_type = cp_parser_class_key (parser);
10050 if (tag_type == none_type)
10051 return error_mark_node;
10052 /* Parse the attributes. */
10053 attributes = cp_parser_attributes_opt (parser);
10056 /* Look for the `::' operator. */
10057 cp_parser_global_scope_opt (parser,
10058 /*current_scope_valid_p=*/false);
10059 /* Look for the nested-name-specifier. */
10060 if (tag_type == typename_type)
10062 if (!cp_parser_nested_name_specifier (parser,
10063 /*typename_keyword_p=*/true,
10064 /*check_dependency_p=*/true,
10065 /*type_p=*/true,
10066 is_declaration))
10067 return error_mark_node;
10069 else
10070 /* Even though `typename' is not present, the proposed resolution
10071 to Core Issue 180 says that in `class A<T>::B', `B' should be
10072 considered a type-name, even if `A<T>' is dependent. */
10073 cp_parser_nested_name_specifier_opt (parser,
10074 /*typename_keyword_p=*/true,
10075 /*check_dependency_p=*/true,
10076 /*type_p=*/true,
10077 is_declaration);
10078 /* For everything but enumeration types, consider a template-id. */
10079 if (tag_type != enum_type)
10081 bool template_p = false;
10082 tree decl;
10084 /* Allow the `template' keyword. */
10085 template_p = cp_parser_optional_template_keyword (parser);
10086 /* If we didn't see `template', we don't know if there's a
10087 template-id or not. */
10088 if (!template_p)
10089 cp_parser_parse_tentatively (parser);
10090 /* Parse the template-id. */
10091 decl = cp_parser_template_id (parser, template_p,
10092 /*check_dependency_p=*/true,
10093 is_declaration);
10094 /* If we didn't find a template-id, look for an ordinary
10095 identifier. */
10096 if (!template_p && !cp_parser_parse_definitely (parser))
10098 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10099 in effect, then we must assume that, upon instantiation, the
10100 template will correspond to a class. */
10101 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10102 && tag_type == typename_type)
10103 type = make_typename_type (parser->scope, decl,
10104 typename_type,
10105 /*complain=*/tf_error);
10106 else
10107 type = TREE_TYPE (decl);
10110 /* For an enumeration type, consider only a plain identifier. */
10111 if (!type)
10113 identifier = cp_parser_identifier (parser);
10115 if (identifier == error_mark_node)
10117 parser->scope = NULL_TREE;
10118 return error_mark_node;
10121 /* For a `typename', we needn't call xref_tag. */
10122 if (tag_type == typename_type
10123 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10124 return cp_parser_make_typename_type (parser, parser->scope,
10125 identifier);
10126 /* Look up a qualified name in the usual way. */
10127 if (parser->scope)
10129 tree decl;
10131 decl = cp_parser_lookup_name (parser, identifier,
10132 tag_type,
10133 /*is_template=*/false,
10134 /*is_namespace=*/false,
10135 /*check_dependency=*/true,
10136 /*ambiguous_decls=*/NULL);
10138 /* If we are parsing friend declaration, DECL may be a
10139 TEMPLATE_DECL tree node here. However, we need to check
10140 whether this TEMPLATE_DECL results in valid code. Consider
10141 the following example:
10143 namespace N {
10144 template <class T> class C {};
10146 class X {
10147 template <class T> friend class N::C; // #1, valid code
10149 template <class T> class Y {
10150 friend class N::C; // #2, invalid code
10153 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10154 name lookup of `N::C'. We see that friend declaration must
10155 be template for the code to be valid. Note that
10156 processing_template_decl does not work here since it is
10157 always 1 for the above two cases. */
10159 decl = (cp_parser_maybe_treat_template_as_class
10160 (decl, /*tag_name_p=*/is_friend
10161 && parser->num_template_parameter_lists));
10163 if (TREE_CODE (decl) != TYPE_DECL)
10165 cp_parser_diagnose_invalid_type_name (parser,
10166 parser->scope,
10167 identifier);
10168 return error_mark_node;
10171 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10172 check_elaborated_type_specifier
10173 (tag_type, decl,
10174 (parser->num_template_parameter_lists
10175 || DECL_SELF_REFERENCE_P (decl)));
10177 type = TREE_TYPE (decl);
10179 else
10181 /* An elaborated-type-specifier sometimes introduces a new type and
10182 sometimes names an existing type. Normally, the rule is that it
10183 introduces a new type only if there is not an existing type of
10184 the same name already in scope. For example, given:
10186 struct S {};
10187 void f() { struct S s; }
10189 the `struct S' in the body of `f' is the same `struct S' as in
10190 the global scope; the existing definition is used. However, if
10191 there were no global declaration, this would introduce a new
10192 local class named `S'.
10194 An exception to this rule applies to the following code:
10196 namespace N { struct S; }
10198 Here, the elaborated-type-specifier names a new type
10199 unconditionally; even if there is already an `S' in the
10200 containing scope this declaration names a new type.
10201 This exception only applies if the elaborated-type-specifier
10202 forms the complete declaration:
10204 [class.name]
10206 A declaration consisting solely of `class-key identifier ;' is
10207 either a redeclaration of the name in the current scope or a
10208 forward declaration of the identifier as a class name. It
10209 introduces the name into the current scope.
10211 We are in this situation precisely when the next token is a `;'.
10213 An exception to the exception is that a `friend' declaration does
10214 *not* name a new type; i.e., given:
10216 struct S { friend struct T; };
10218 `T' is not a new type in the scope of `S'.
10220 Also, `new struct S' or `sizeof (struct S)' never results in the
10221 definition of a new type; a new type can only be declared in a
10222 declaration context. */
10224 tag_scope ts;
10225 bool template_p;
10227 if (is_friend)
10228 /* Friends have special name lookup rules. */
10229 ts = ts_within_enclosing_non_class;
10230 else if (is_declaration
10231 && cp_lexer_next_token_is (parser->lexer,
10232 CPP_SEMICOLON))
10233 /* This is a `class-key identifier ;' */
10234 ts = ts_current;
10235 else
10236 ts = ts_global;
10238 /* Warn about attributes. They are ignored. */
10239 if (attributes)
10240 warning (OPT_Wattributes,
10241 "type attributes are honored only at type definition");
10243 template_p =
10244 (parser->num_template_parameter_lists
10245 && (cp_parser_next_token_starts_class_definition_p (parser)
10246 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10247 /* An unqualified name was used to reference this type, so
10248 there were no qualifying templates. */
10249 if (!cp_parser_check_template_parameters (parser,
10250 /*num_templates=*/0))
10251 return error_mark_node;
10252 type = xref_tag (tag_type, identifier, ts, template_p);
10255 if (tag_type != enum_type)
10256 cp_parser_check_class_key (tag_type, type);
10258 /* A "<" cannot follow an elaborated type specifier. If that
10259 happens, the user was probably trying to form a template-id. */
10260 cp_parser_check_for_invalid_template_id (parser, type);
10262 return type;
10265 /* Parse an enum-specifier.
10267 enum-specifier:
10268 enum identifier [opt] { enumerator-list [opt] }
10270 GNU Extensions:
10271 enum identifier [opt] { enumerator-list [opt] } attributes
10273 Returns an ENUM_TYPE representing the enumeration. */
10275 static tree
10276 cp_parser_enum_specifier (cp_parser* parser)
10278 tree identifier;
10279 tree type;
10281 /* Caller guarantees that the current token is 'enum', an identifier
10282 possibly follows, and the token after that is an opening brace.
10283 If we don't have an identifier, fabricate an anonymous name for
10284 the enumeration being defined. */
10285 cp_lexer_consume_token (parser->lexer);
10287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10288 identifier = cp_parser_identifier (parser);
10289 else
10290 identifier = make_anon_name ();
10292 /* Issue an error message if type-definitions are forbidden here. */
10293 cp_parser_check_type_definition (parser);
10295 /* Create the new type. We do this before consuming the opening brace
10296 so the enum will be recorded as being on the line of its tag (or the
10297 'enum' keyword, if there is no tag). */
10298 type = start_enum (identifier);
10300 /* Consume the opening brace. */
10301 cp_lexer_consume_token (parser->lexer);
10303 /* If the next token is not '}', then there are some enumerators. */
10304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10305 cp_parser_enumerator_list (parser, type);
10307 /* Consume the final '}'. */
10308 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10310 /* Look for trailing attributes to apply to this enumeration, and
10311 apply them if appropriate. */
10312 if (cp_parser_allow_gnu_extensions_p (parser))
10314 tree trailing_attr = cp_parser_attributes_opt (parser);
10315 cplus_decl_attributes (&type,
10316 trailing_attr,
10317 (int) ATTR_FLAG_TYPE_IN_PLACE);
10320 /* Finish up the enumeration. */
10321 finish_enum (type);
10323 return type;
10326 /* Parse an enumerator-list. The enumerators all have the indicated
10327 TYPE.
10329 enumerator-list:
10330 enumerator-definition
10331 enumerator-list , enumerator-definition */
10333 static void
10334 cp_parser_enumerator_list (cp_parser* parser, tree type)
10336 while (true)
10338 /* Parse an enumerator-definition. */
10339 cp_parser_enumerator_definition (parser, type);
10341 /* If the next token is not a ',', we've reached the end of
10342 the list. */
10343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10344 break;
10345 /* Otherwise, consume the `,' and keep going. */
10346 cp_lexer_consume_token (parser->lexer);
10347 /* If the next token is a `}', there is a trailing comma. */
10348 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10350 if (pedantic && !in_system_header)
10351 pedwarn ("comma at end of enumerator list");
10352 break;
10357 /* Parse an enumerator-definition. The enumerator has the indicated
10358 TYPE.
10360 enumerator-definition:
10361 enumerator
10362 enumerator = constant-expression
10364 enumerator:
10365 identifier */
10367 static void
10368 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10370 tree identifier;
10371 tree value;
10373 /* Look for the identifier. */
10374 identifier = cp_parser_identifier (parser);
10375 if (identifier == error_mark_node)
10376 return;
10378 /* If the next token is an '=', then there is an explicit value. */
10379 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10381 /* Consume the `=' token. */
10382 cp_lexer_consume_token (parser->lexer);
10383 /* Parse the value. */
10384 value = cp_parser_constant_expression (parser,
10385 /*allow_non_constant_p=*/false,
10386 NULL);
10388 else
10389 value = NULL_TREE;
10391 /* Create the enumerator. */
10392 build_enumerator (identifier, value, type);
10395 /* Parse a namespace-name.
10397 namespace-name:
10398 original-namespace-name
10399 namespace-alias
10401 Returns the NAMESPACE_DECL for the namespace. */
10403 static tree
10404 cp_parser_namespace_name (cp_parser* parser)
10406 tree identifier;
10407 tree namespace_decl;
10409 /* Get the name of the namespace. */
10410 identifier = cp_parser_identifier (parser);
10411 if (identifier == error_mark_node)
10412 return error_mark_node;
10414 /* Look up the identifier in the currently active scope. Look only
10415 for namespaces, due to:
10417 [basic.lookup.udir]
10419 When looking up a namespace-name in a using-directive or alias
10420 definition, only namespace names are considered.
10422 And:
10424 [basic.lookup.qual]
10426 During the lookup of a name preceding the :: scope resolution
10427 operator, object, function, and enumerator names are ignored.
10429 (Note that cp_parser_class_or_namespace_name only calls this
10430 function if the token after the name is the scope resolution
10431 operator.) */
10432 namespace_decl = cp_parser_lookup_name (parser, identifier,
10433 none_type,
10434 /*is_template=*/false,
10435 /*is_namespace=*/true,
10436 /*check_dependency=*/true,
10437 /*ambiguous_decls=*/NULL);
10438 /* If it's not a namespace, issue an error. */
10439 if (namespace_decl == error_mark_node
10440 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10443 error ("%qD is not a namespace-name", identifier);
10444 cp_parser_error (parser, "expected namespace-name");
10445 namespace_decl = error_mark_node;
10448 return namespace_decl;
10451 /* Parse a namespace-definition.
10453 namespace-definition:
10454 named-namespace-definition
10455 unnamed-namespace-definition
10457 named-namespace-definition:
10458 original-namespace-definition
10459 extension-namespace-definition
10461 original-namespace-definition:
10462 namespace identifier { namespace-body }
10464 extension-namespace-definition:
10465 namespace original-namespace-name { namespace-body }
10467 unnamed-namespace-definition:
10468 namespace { namespace-body } */
10470 static void
10471 cp_parser_namespace_definition (cp_parser* parser)
10473 tree identifier;
10475 /* Look for the `namespace' keyword. */
10476 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10478 /* Get the name of the namespace. We do not attempt to distinguish
10479 between an original-namespace-definition and an
10480 extension-namespace-definition at this point. The semantic
10481 analysis routines are responsible for that. */
10482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10483 identifier = cp_parser_identifier (parser);
10484 else
10485 identifier = NULL_TREE;
10487 /* Look for the `{' to start the namespace. */
10488 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10489 /* Start the namespace. */
10490 push_namespace (identifier);
10491 /* Parse the body of the namespace. */
10492 cp_parser_namespace_body (parser);
10493 /* Finish the namespace. */
10494 pop_namespace ();
10495 /* Look for the final `}'. */
10496 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10499 /* Parse a namespace-body.
10501 namespace-body:
10502 declaration-seq [opt] */
10504 static void
10505 cp_parser_namespace_body (cp_parser* parser)
10507 cp_parser_declaration_seq_opt (parser);
10510 /* Parse a namespace-alias-definition.
10512 namespace-alias-definition:
10513 namespace identifier = qualified-namespace-specifier ; */
10515 static void
10516 cp_parser_namespace_alias_definition (cp_parser* parser)
10518 tree identifier;
10519 tree namespace_specifier;
10521 /* Look for the `namespace' keyword. */
10522 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10523 /* Look for the identifier. */
10524 identifier = cp_parser_identifier (parser);
10525 if (identifier == error_mark_node)
10526 return;
10527 /* Look for the `=' token. */
10528 cp_parser_require (parser, CPP_EQ, "`='");
10529 /* Look for the qualified-namespace-specifier. */
10530 namespace_specifier
10531 = cp_parser_qualified_namespace_specifier (parser);
10532 /* Look for the `;' token. */
10533 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10535 /* Register the alias in the symbol table. */
10536 do_namespace_alias (identifier, namespace_specifier);
10539 /* Parse a qualified-namespace-specifier.
10541 qualified-namespace-specifier:
10542 :: [opt] nested-name-specifier [opt] namespace-name
10544 Returns a NAMESPACE_DECL corresponding to the specified
10545 namespace. */
10547 static tree
10548 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10550 /* Look for the optional `::'. */
10551 cp_parser_global_scope_opt (parser,
10552 /*current_scope_valid_p=*/false);
10554 /* Look for the optional nested-name-specifier. */
10555 cp_parser_nested_name_specifier_opt (parser,
10556 /*typename_keyword_p=*/false,
10557 /*check_dependency_p=*/true,
10558 /*type_p=*/false,
10559 /*is_declaration=*/true);
10561 return cp_parser_namespace_name (parser);
10564 /* Parse a using-declaration.
10566 using-declaration:
10567 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10568 using :: unqualified-id ; */
10570 static void
10571 cp_parser_using_declaration (cp_parser* parser)
10573 cp_token *token;
10574 bool typename_p = false;
10575 bool global_scope_p;
10576 tree decl;
10577 tree identifier;
10578 tree qscope;
10580 /* Look for the `using' keyword. */
10581 cp_parser_require_keyword (parser, RID_USING, "`using'");
10583 /* Peek at the next token. */
10584 token = cp_lexer_peek_token (parser->lexer);
10585 /* See if it's `typename'. */
10586 if (token->keyword == RID_TYPENAME)
10588 /* Remember that we've seen it. */
10589 typename_p = true;
10590 /* Consume the `typename' token. */
10591 cp_lexer_consume_token (parser->lexer);
10594 /* Look for the optional global scope qualification. */
10595 global_scope_p
10596 = (cp_parser_global_scope_opt (parser,
10597 /*current_scope_valid_p=*/false)
10598 != NULL_TREE);
10600 /* If we saw `typename', or didn't see `::', then there must be a
10601 nested-name-specifier present. */
10602 if (typename_p || !global_scope_p)
10603 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10604 /*check_dependency_p=*/true,
10605 /*type_p=*/false,
10606 /*is_declaration=*/true);
10607 /* Otherwise, we could be in either of the two productions. In that
10608 case, treat the nested-name-specifier as optional. */
10609 else
10610 qscope = cp_parser_nested_name_specifier_opt (parser,
10611 /*typename_keyword_p=*/false,
10612 /*check_dependency_p=*/true,
10613 /*type_p=*/false,
10614 /*is_declaration=*/true);
10615 if (!qscope)
10616 qscope = global_namespace;
10618 /* Parse the unqualified-id. */
10619 identifier = cp_parser_unqualified_id (parser,
10620 /*template_keyword_p=*/false,
10621 /*check_dependency_p=*/true,
10622 /*declarator_p=*/true);
10624 /* The function we call to handle a using-declaration is different
10625 depending on what scope we are in. */
10626 if (qscope == error_mark_node || identifier == error_mark_node)
10628 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10629 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10630 /* [namespace.udecl]
10632 A using declaration shall not name a template-id. */
10633 error ("a template-id may not appear in a using-declaration");
10634 else
10636 if (at_class_scope_p ())
10638 /* Create the USING_DECL. */
10639 decl = do_class_using_decl (parser->scope, identifier);
10640 /* Add it to the list of members in this class. */
10641 finish_member_declaration (decl);
10643 else
10645 decl = cp_parser_lookup_name_simple (parser, identifier);
10646 if (decl == error_mark_node)
10647 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10648 else if (!at_namespace_scope_p ())
10649 do_local_using_decl (decl, qscope, identifier);
10650 else
10651 do_toplevel_using_decl (decl, qscope, identifier);
10655 /* Look for the final `;'. */
10656 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10659 /* Parse a using-directive.
10661 using-directive:
10662 using namespace :: [opt] nested-name-specifier [opt]
10663 namespace-name ; */
10665 static void
10666 cp_parser_using_directive (cp_parser* parser)
10668 tree namespace_decl;
10669 tree attribs;
10671 /* Look for the `using' keyword. */
10672 cp_parser_require_keyword (parser, RID_USING, "`using'");
10673 /* And the `namespace' keyword. */
10674 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10675 /* Look for the optional `::' operator. */
10676 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10677 /* And the optional nested-name-specifier. */
10678 cp_parser_nested_name_specifier_opt (parser,
10679 /*typename_keyword_p=*/false,
10680 /*check_dependency_p=*/true,
10681 /*type_p=*/false,
10682 /*is_declaration=*/true);
10683 /* Get the namespace being used. */
10684 namespace_decl = cp_parser_namespace_name (parser);
10685 /* And any specified attributes. */
10686 attribs = cp_parser_attributes_opt (parser);
10687 /* Update the symbol table. */
10688 parse_using_directive (namespace_decl, attribs);
10689 /* Look for the final `;'. */
10690 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10693 /* Parse an asm-definition.
10695 asm-definition:
10696 asm ( string-literal ) ;
10698 GNU Extension:
10700 asm-definition:
10701 asm volatile [opt] ( string-literal ) ;
10702 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10703 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10704 : asm-operand-list [opt] ) ;
10705 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10706 : asm-operand-list [opt]
10707 : asm-operand-list [opt] ) ; */
10709 static void
10710 cp_parser_asm_definition (cp_parser* parser)
10712 tree string;
10713 tree outputs = NULL_TREE;
10714 tree inputs = NULL_TREE;
10715 tree clobbers = NULL_TREE;
10716 tree asm_stmt;
10717 bool volatile_p = false;
10718 bool extended_p = false;
10720 /* Look for the `asm' keyword. */
10721 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10722 /* See if the next token is `volatile'. */
10723 if (cp_parser_allow_gnu_extensions_p (parser)
10724 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10726 /* Remember that we saw the `volatile' keyword. */
10727 volatile_p = true;
10728 /* Consume the token. */
10729 cp_lexer_consume_token (parser->lexer);
10731 /* Look for the opening `('. */
10732 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10733 return;
10734 /* Look for the string. */
10735 string = cp_parser_string_literal (parser, false, false);
10736 if (string == error_mark_node)
10738 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10739 /*consume_paren=*/true);
10740 return;
10743 /* If we're allowing GNU extensions, check for the extended assembly
10744 syntax. Unfortunately, the `:' tokens need not be separated by
10745 a space in C, and so, for compatibility, we tolerate that here
10746 too. Doing that means that we have to treat the `::' operator as
10747 two `:' tokens. */
10748 if (cp_parser_allow_gnu_extensions_p (parser)
10749 && at_function_scope_p ()
10750 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10751 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10753 bool inputs_p = false;
10754 bool clobbers_p = false;
10756 /* The extended syntax was used. */
10757 extended_p = true;
10759 /* Look for outputs. */
10760 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10762 /* Consume the `:'. */
10763 cp_lexer_consume_token (parser->lexer);
10764 /* Parse the output-operands. */
10765 if (cp_lexer_next_token_is_not (parser->lexer,
10766 CPP_COLON)
10767 && cp_lexer_next_token_is_not (parser->lexer,
10768 CPP_SCOPE)
10769 && cp_lexer_next_token_is_not (parser->lexer,
10770 CPP_CLOSE_PAREN))
10771 outputs = cp_parser_asm_operand_list (parser);
10773 /* If the next token is `::', there are no outputs, and the
10774 next token is the beginning of the inputs. */
10775 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10776 /* The inputs are coming next. */
10777 inputs_p = true;
10779 /* Look for inputs. */
10780 if (inputs_p
10781 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10783 /* Consume the `:' or `::'. */
10784 cp_lexer_consume_token (parser->lexer);
10785 /* Parse the output-operands. */
10786 if (cp_lexer_next_token_is_not (parser->lexer,
10787 CPP_COLON)
10788 && cp_lexer_next_token_is_not (parser->lexer,
10789 CPP_CLOSE_PAREN))
10790 inputs = cp_parser_asm_operand_list (parser);
10792 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10793 /* The clobbers are coming next. */
10794 clobbers_p = true;
10796 /* Look for clobbers. */
10797 if (clobbers_p
10798 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10800 /* Consume the `:' or `::'. */
10801 cp_lexer_consume_token (parser->lexer);
10802 /* Parse the clobbers. */
10803 if (cp_lexer_next_token_is_not (parser->lexer,
10804 CPP_CLOSE_PAREN))
10805 clobbers = cp_parser_asm_clobber_list (parser);
10808 /* Look for the closing `)'. */
10809 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10810 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10811 /*consume_paren=*/true);
10812 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10814 /* Create the ASM_EXPR. */
10815 if (at_function_scope_p ())
10817 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10818 inputs, clobbers);
10819 /* If the extended syntax was not used, mark the ASM_EXPR. */
10820 if (!extended_p)
10822 tree temp = asm_stmt;
10823 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10824 temp = TREE_OPERAND (temp, 0);
10826 ASM_INPUT_P (temp) = 1;
10829 else
10830 cgraph_add_asm_node (string);
10833 /* Declarators [gram.dcl.decl] */
10835 /* Parse an init-declarator.
10837 init-declarator:
10838 declarator initializer [opt]
10840 GNU Extension:
10842 init-declarator:
10843 declarator asm-specification [opt] attributes [opt] initializer [opt]
10845 function-definition:
10846 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10847 function-body
10848 decl-specifier-seq [opt] declarator function-try-block
10850 GNU Extension:
10852 function-definition:
10853 __extension__ function-definition
10855 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10856 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10857 then this declarator appears in a class scope. The new DECL created
10858 by this declarator is returned.
10860 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10861 for a function-definition here as well. If the declarator is a
10862 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10863 be TRUE upon return. By that point, the function-definition will
10864 have been completely parsed.
10866 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10867 is FALSE. */
10869 static tree
10870 cp_parser_init_declarator (cp_parser* parser,
10871 cp_decl_specifier_seq *decl_specifiers,
10872 bool function_definition_allowed_p,
10873 bool member_p,
10874 int declares_class_or_enum,
10875 bool* function_definition_p)
10877 cp_token *token;
10878 cp_declarator *declarator;
10879 tree prefix_attributes;
10880 tree attributes;
10881 tree asm_specification;
10882 tree initializer;
10883 tree decl = NULL_TREE;
10884 tree scope;
10885 bool is_initialized;
10886 bool is_parenthesized_init;
10887 bool is_non_constant_init;
10888 int ctor_dtor_or_conv_p;
10889 bool friend_p;
10890 tree pushed_scope = NULL;
10892 /* Gather the attributes that were provided with the
10893 decl-specifiers. */
10894 prefix_attributes = decl_specifiers->attributes;
10896 /* Assume that this is not the declarator for a function
10897 definition. */
10898 if (function_definition_p)
10899 *function_definition_p = false;
10901 /* Defer access checks while parsing the declarator; we cannot know
10902 what names are accessible until we know what is being
10903 declared. */
10904 resume_deferring_access_checks ();
10906 /* Parse the declarator. */
10907 declarator
10908 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10909 &ctor_dtor_or_conv_p,
10910 /*parenthesized_p=*/NULL,
10911 /*member_p=*/false);
10912 /* Gather up the deferred checks. */
10913 stop_deferring_access_checks ();
10915 /* If the DECLARATOR was erroneous, there's no need to go
10916 further. */
10917 if (declarator == cp_error_declarator)
10918 return error_mark_node;
10920 if (declares_class_or_enum & 2)
10921 cp_parser_check_for_definition_in_return_type (declarator,
10922 decl_specifiers->type);
10924 /* Figure out what scope the entity declared by the DECLARATOR is
10925 located in. `grokdeclarator' sometimes changes the scope, so
10926 we compute it now. */
10927 scope = get_scope_of_declarator (declarator);
10929 /* If we're allowing GNU extensions, look for an asm-specification
10930 and attributes. */
10931 if (cp_parser_allow_gnu_extensions_p (parser))
10933 /* Look for an asm-specification. */
10934 asm_specification = cp_parser_asm_specification_opt (parser);
10935 /* And attributes. */
10936 attributes = cp_parser_attributes_opt (parser);
10938 else
10940 asm_specification = NULL_TREE;
10941 attributes = NULL_TREE;
10944 /* Peek at the next token. */
10945 token = cp_lexer_peek_token (parser->lexer);
10946 /* Check to see if the token indicates the start of a
10947 function-definition. */
10948 if (cp_parser_token_starts_function_definition_p (token))
10950 if (!function_definition_allowed_p)
10952 /* If a function-definition should not appear here, issue an
10953 error message. */
10954 cp_parser_error (parser,
10955 "a function-definition is not allowed here");
10956 return error_mark_node;
10958 else
10960 /* Neither attributes nor an asm-specification are allowed
10961 on a function-definition. */
10962 if (asm_specification)
10963 error ("an asm-specification is not allowed on a function-definition");
10964 if (attributes)
10965 error ("attributes are not allowed on a function-definition");
10966 /* This is a function-definition. */
10967 *function_definition_p = true;
10969 /* Parse the function definition. */
10970 if (member_p)
10971 decl = cp_parser_save_member_function_body (parser,
10972 decl_specifiers,
10973 declarator,
10974 prefix_attributes);
10975 else
10976 decl
10977 = (cp_parser_function_definition_from_specifiers_and_declarator
10978 (parser, decl_specifiers, prefix_attributes, declarator));
10980 return decl;
10984 /* [dcl.dcl]
10986 Only in function declarations for constructors, destructors, and
10987 type conversions can the decl-specifier-seq be omitted.
10989 We explicitly postpone this check past the point where we handle
10990 function-definitions because we tolerate function-definitions
10991 that are missing their return types in some modes. */
10992 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10994 cp_parser_error (parser,
10995 "expected constructor, destructor, or type conversion");
10996 return error_mark_node;
10999 /* An `=' or an `(' indicates an initializer. */
11000 is_initialized = (token->type == CPP_EQ
11001 || token->type == CPP_OPEN_PAREN);
11002 /* If the init-declarator isn't initialized and isn't followed by a
11003 `,' or `;', it's not a valid init-declarator. */
11004 if (!is_initialized
11005 && token->type != CPP_COMMA
11006 && token->type != CPP_SEMICOLON)
11008 cp_parser_error (parser, "expected initializer");
11009 return error_mark_node;
11012 /* Because start_decl has side-effects, we should only call it if we
11013 know we're going ahead. By this point, we know that we cannot
11014 possibly be looking at any other construct. */
11015 cp_parser_commit_to_tentative_parse (parser);
11017 /* If the decl specifiers were bad, issue an error now that we're
11018 sure this was intended to be a declarator. Then continue
11019 declaring the variable(s), as int, to try to cut down on further
11020 errors. */
11021 if (decl_specifiers->any_specifiers_p
11022 && decl_specifiers->type == error_mark_node)
11024 cp_parser_error (parser, "invalid type in declaration");
11025 decl_specifiers->type = integer_type_node;
11028 /* Check to see whether or not this declaration is a friend. */
11029 friend_p = cp_parser_friend_p (decl_specifiers);
11031 /* Check that the number of template-parameter-lists is OK. */
11032 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11033 return error_mark_node;
11035 /* Enter the newly declared entry in the symbol table. If we're
11036 processing a declaration in a class-specifier, we wait until
11037 after processing the initializer. */
11038 if (!member_p)
11040 if (parser->in_unbraced_linkage_specification_p)
11042 decl_specifiers->storage_class = sc_extern;
11043 have_extern_spec = false;
11045 decl = start_decl (declarator, decl_specifiers,
11046 is_initialized, attributes, prefix_attributes,
11047 &pushed_scope);
11049 else if (scope)
11050 /* Enter the SCOPE. That way unqualified names appearing in the
11051 initializer will be looked up in SCOPE. */
11052 pushed_scope = push_scope (scope);
11054 /* Perform deferred access control checks, now that we know in which
11055 SCOPE the declared entity resides. */
11056 if (!member_p && decl)
11058 tree saved_current_function_decl = NULL_TREE;
11060 /* If the entity being declared is a function, pretend that we
11061 are in its scope. If it is a `friend', it may have access to
11062 things that would not otherwise be accessible. */
11063 if (TREE_CODE (decl) == FUNCTION_DECL)
11065 saved_current_function_decl = current_function_decl;
11066 current_function_decl = decl;
11069 /* Perform the access control checks for the declarator and the
11070 the decl-specifiers. */
11071 perform_deferred_access_checks ();
11073 /* Restore the saved value. */
11074 if (TREE_CODE (decl) == FUNCTION_DECL)
11075 current_function_decl = saved_current_function_decl;
11078 /* Parse the initializer. */
11079 if (is_initialized)
11080 initializer = cp_parser_initializer (parser,
11081 &is_parenthesized_init,
11082 &is_non_constant_init);
11083 else
11085 initializer = NULL_TREE;
11086 is_parenthesized_init = false;
11087 is_non_constant_init = true;
11090 /* The old parser allows attributes to appear after a parenthesized
11091 initializer. Mark Mitchell proposed removing this functionality
11092 on the GCC mailing lists on 2002-08-13. This parser accepts the
11093 attributes -- but ignores them. */
11094 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11095 if (cp_parser_attributes_opt (parser))
11096 warning (OPT_Wattributes,
11097 "attributes after parenthesized initializer ignored");
11099 /* For an in-class declaration, use `grokfield' to create the
11100 declaration. */
11101 if (member_p)
11103 if (pushed_scope)
11105 pop_scope (pushed_scope);
11106 pushed_scope = false;
11108 decl = grokfield (declarator, decl_specifiers,
11109 initializer, !is_non_constant_init,
11110 /*asmspec=*/NULL_TREE,
11111 prefix_attributes);
11112 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11113 cp_parser_save_default_args (parser, decl);
11116 /* Finish processing the declaration. But, skip friend
11117 declarations. */
11118 if (!friend_p && decl && decl != error_mark_node)
11120 cp_finish_decl (decl,
11121 initializer, !is_non_constant_init,
11122 asm_specification,
11123 /* If the initializer is in parentheses, then this is
11124 a direct-initialization, which means that an
11125 `explicit' constructor is OK. Otherwise, an
11126 `explicit' constructor cannot be used. */
11127 ((is_parenthesized_init || !is_initialized)
11128 ? 0 : LOOKUP_ONLYCONVERTING));
11130 if (!friend_p && pushed_scope)
11131 pop_scope (pushed_scope);
11133 return decl;
11136 /* Parse a declarator.
11138 declarator:
11139 direct-declarator
11140 ptr-operator declarator
11142 abstract-declarator:
11143 ptr-operator abstract-declarator [opt]
11144 direct-abstract-declarator
11146 GNU Extensions:
11148 declarator:
11149 attributes [opt] direct-declarator
11150 attributes [opt] ptr-operator declarator
11152 abstract-declarator:
11153 attributes [opt] ptr-operator abstract-declarator [opt]
11154 attributes [opt] direct-abstract-declarator
11156 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11157 detect constructor, destructor or conversion operators. It is set
11158 to -1 if the declarator is a name, and +1 if it is a
11159 function. Otherwise it is set to zero. Usually you just want to
11160 test for >0, but internally the negative value is used.
11162 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11163 a decl-specifier-seq unless it declares a constructor, destructor,
11164 or conversion. It might seem that we could check this condition in
11165 semantic analysis, rather than parsing, but that makes it difficult
11166 to handle something like `f()'. We want to notice that there are
11167 no decl-specifiers, and therefore realize that this is an
11168 expression, not a declaration.)
11170 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11171 the declarator is a direct-declarator of the form "(...)".
11173 MEMBER_P is true iff this declarator is a member-declarator. */
11175 static cp_declarator *
11176 cp_parser_declarator (cp_parser* parser,
11177 cp_parser_declarator_kind dcl_kind,
11178 int* ctor_dtor_or_conv_p,
11179 bool* parenthesized_p,
11180 bool member_p)
11182 cp_token *token;
11183 cp_declarator *declarator;
11184 enum tree_code code;
11185 cp_cv_quals cv_quals;
11186 tree class_type;
11187 tree attributes = NULL_TREE;
11189 /* Assume this is not a constructor, destructor, or type-conversion
11190 operator. */
11191 if (ctor_dtor_or_conv_p)
11192 *ctor_dtor_or_conv_p = 0;
11194 if (cp_parser_allow_gnu_extensions_p (parser))
11195 attributes = cp_parser_attributes_opt (parser);
11197 /* Peek at the next token. */
11198 token = cp_lexer_peek_token (parser->lexer);
11200 /* Check for the ptr-operator production. */
11201 cp_parser_parse_tentatively (parser);
11202 /* Parse the ptr-operator. */
11203 code = cp_parser_ptr_operator (parser,
11204 &class_type,
11205 &cv_quals);
11206 /* If that worked, then we have a ptr-operator. */
11207 if (cp_parser_parse_definitely (parser))
11209 /* If a ptr-operator was found, then this declarator was not
11210 parenthesized. */
11211 if (parenthesized_p)
11212 *parenthesized_p = true;
11213 /* The dependent declarator is optional if we are parsing an
11214 abstract-declarator. */
11215 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11216 cp_parser_parse_tentatively (parser);
11218 /* Parse the dependent declarator. */
11219 declarator = cp_parser_declarator (parser, dcl_kind,
11220 /*ctor_dtor_or_conv_p=*/NULL,
11221 /*parenthesized_p=*/NULL,
11222 /*member_p=*/false);
11224 /* If we are parsing an abstract-declarator, we must handle the
11225 case where the dependent declarator is absent. */
11226 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11227 && !cp_parser_parse_definitely (parser))
11228 declarator = NULL;
11230 /* Build the representation of the ptr-operator. */
11231 if (class_type)
11232 declarator = make_ptrmem_declarator (cv_quals,
11233 class_type,
11234 declarator);
11235 else if (code == INDIRECT_REF)
11236 declarator = make_pointer_declarator (cv_quals, declarator);
11237 else
11238 declarator = make_reference_declarator (cv_quals, declarator);
11240 /* Everything else is a direct-declarator. */
11241 else
11243 if (parenthesized_p)
11244 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11245 CPP_OPEN_PAREN);
11246 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11247 ctor_dtor_or_conv_p,
11248 member_p);
11251 if (attributes && declarator != cp_error_declarator)
11252 declarator->attributes = attributes;
11254 return declarator;
11257 /* Parse a direct-declarator or direct-abstract-declarator.
11259 direct-declarator:
11260 declarator-id
11261 direct-declarator ( parameter-declaration-clause )
11262 cv-qualifier-seq [opt]
11263 exception-specification [opt]
11264 direct-declarator [ constant-expression [opt] ]
11265 ( declarator )
11267 direct-abstract-declarator:
11268 direct-abstract-declarator [opt]
11269 ( parameter-declaration-clause )
11270 cv-qualifier-seq [opt]
11271 exception-specification [opt]
11272 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11273 ( abstract-declarator )
11275 Returns a representation of the declarator. DCL_KIND is
11276 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11277 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11278 we are parsing a direct-declarator. It is
11279 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11280 of ambiguity we prefer an abstract declarator, as per
11281 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11282 cp_parser_declarator. */
11284 static cp_declarator *
11285 cp_parser_direct_declarator (cp_parser* parser,
11286 cp_parser_declarator_kind dcl_kind,
11287 int* ctor_dtor_or_conv_p,
11288 bool member_p)
11290 cp_token *token;
11291 cp_declarator *declarator = NULL;
11292 tree scope = NULL_TREE;
11293 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11294 bool saved_in_declarator_p = parser->in_declarator_p;
11295 bool first = true;
11296 tree pushed_scope = NULL_TREE;
11298 while (true)
11300 /* Peek at the next token. */
11301 token = cp_lexer_peek_token (parser->lexer);
11302 if (token->type == CPP_OPEN_PAREN)
11304 /* This is either a parameter-declaration-clause, or a
11305 parenthesized declarator. When we know we are parsing a
11306 named declarator, it must be a parenthesized declarator
11307 if FIRST is true. For instance, `(int)' is a
11308 parameter-declaration-clause, with an omitted
11309 direct-abstract-declarator. But `((*))', is a
11310 parenthesized abstract declarator. Finally, when T is a
11311 template parameter `(T)' is a
11312 parameter-declaration-clause, and not a parenthesized
11313 named declarator.
11315 We first try and parse a parameter-declaration-clause,
11316 and then try a nested declarator (if FIRST is true).
11318 It is not an error for it not to be a
11319 parameter-declaration-clause, even when FIRST is
11320 false. Consider,
11322 int i (int);
11323 int i (3);
11325 The first is the declaration of a function while the
11326 second is a the definition of a variable, including its
11327 initializer.
11329 Having seen only the parenthesis, we cannot know which of
11330 these two alternatives should be selected. Even more
11331 complex are examples like:
11333 int i (int (a));
11334 int i (int (3));
11336 The former is a function-declaration; the latter is a
11337 variable initialization.
11339 Thus again, we try a parameter-declaration-clause, and if
11340 that fails, we back out and return. */
11342 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11344 cp_parameter_declarator *params;
11345 unsigned saved_num_template_parameter_lists;
11347 /* In a member-declarator, the only valid interpretation
11348 of a parenthesis is the start of a
11349 parameter-declaration-clause. (It is invalid to
11350 initialize a static data member with a parenthesized
11351 initializer; only the "=" form of initialization is
11352 permitted.) */
11353 if (!member_p)
11354 cp_parser_parse_tentatively (parser);
11356 /* Consume the `('. */
11357 cp_lexer_consume_token (parser->lexer);
11358 if (first)
11360 /* If this is going to be an abstract declarator, we're
11361 in a declarator and we can't have default args. */
11362 parser->default_arg_ok_p = false;
11363 parser->in_declarator_p = true;
11366 /* Inside the function parameter list, surrounding
11367 template-parameter-lists do not apply. */
11368 saved_num_template_parameter_lists
11369 = parser->num_template_parameter_lists;
11370 parser->num_template_parameter_lists = 0;
11372 /* Parse the parameter-declaration-clause. */
11373 params = cp_parser_parameter_declaration_clause (parser);
11375 parser->num_template_parameter_lists
11376 = saved_num_template_parameter_lists;
11378 /* If all went well, parse the cv-qualifier-seq and the
11379 exception-specification. */
11380 if (member_p || cp_parser_parse_definitely (parser))
11382 cp_cv_quals cv_quals;
11383 tree exception_specification;
11385 if (ctor_dtor_or_conv_p)
11386 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11387 first = false;
11388 /* Consume the `)'. */
11389 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11391 /* Parse the cv-qualifier-seq. */
11392 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11393 /* And the exception-specification. */
11394 exception_specification
11395 = cp_parser_exception_specification_opt (parser);
11397 /* Create the function-declarator. */
11398 declarator = make_call_declarator (declarator,
11399 params,
11400 cv_quals,
11401 exception_specification);
11402 /* Any subsequent parameter lists are to do with
11403 return type, so are not those of the declared
11404 function. */
11405 parser->default_arg_ok_p = false;
11407 /* Repeat the main loop. */
11408 continue;
11412 /* If this is the first, we can try a parenthesized
11413 declarator. */
11414 if (first)
11416 bool saved_in_type_id_in_expr_p;
11418 parser->default_arg_ok_p = saved_default_arg_ok_p;
11419 parser->in_declarator_p = saved_in_declarator_p;
11421 /* Consume the `('. */
11422 cp_lexer_consume_token (parser->lexer);
11423 /* Parse the nested declarator. */
11424 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11425 parser->in_type_id_in_expr_p = true;
11426 declarator
11427 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11428 /*parenthesized_p=*/NULL,
11429 member_p);
11430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11431 first = false;
11432 /* Expect a `)'. */
11433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11434 declarator = cp_error_declarator;
11435 if (declarator == cp_error_declarator)
11436 break;
11438 goto handle_declarator;
11440 /* Otherwise, we must be done. */
11441 else
11442 break;
11444 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11445 && token->type == CPP_OPEN_SQUARE)
11447 /* Parse an array-declarator. */
11448 tree bounds;
11450 if (ctor_dtor_or_conv_p)
11451 *ctor_dtor_or_conv_p = 0;
11453 first = false;
11454 parser->default_arg_ok_p = false;
11455 parser->in_declarator_p = true;
11456 /* Consume the `['. */
11457 cp_lexer_consume_token (parser->lexer);
11458 /* Peek at the next token. */
11459 token = cp_lexer_peek_token (parser->lexer);
11460 /* If the next token is `]', then there is no
11461 constant-expression. */
11462 if (token->type != CPP_CLOSE_SQUARE)
11464 bool non_constant_p;
11466 bounds
11467 = cp_parser_constant_expression (parser,
11468 /*allow_non_constant=*/true,
11469 &non_constant_p);
11470 if (!non_constant_p)
11471 bounds = fold_non_dependent_expr (bounds);
11472 /* Normally, the array bound must be an integral constant
11473 expression. However, as an extension, we allow VLAs
11474 in function scopes. */
11475 else if (!at_function_scope_p ())
11477 error ("array bound is not an integer constant");
11478 bounds = error_mark_node;
11481 else
11482 bounds = NULL_TREE;
11483 /* Look for the closing `]'. */
11484 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11486 declarator = cp_error_declarator;
11487 break;
11490 declarator = make_array_declarator (declarator, bounds);
11492 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11494 tree qualifying_scope;
11495 tree unqualified_name;
11496 special_function_kind sfk;
11498 /* Parse a declarator-id */
11499 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11500 cp_parser_parse_tentatively (parser);
11501 unqualified_name = cp_parser_declarator_id (parser);
11502 qualifying_scope = parser->scope;
11503 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11505 if (!cp_parser_parse_definitely (parser))
11506 unqualified_name = error_mark_node;
11507 else if (qualifying_scope
11508 || (TREE_CODE (unqualified_name)
11509 != IDENTIFIER_NODE))
11511 cp_parser_error (parser, "expected unqualified-id");
11512 unqualified_name = error_mark_node;
11516 if (unqualified_name == error_mark_node)
11518 declarator = cp_error_declarator;
11519 break;
11522 if (qualifying_scope && at_namespace_scope_p ()
11523 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11525 /* In the declaration of a member of a template class
11526 outside of the class itself, the SCOPE will sometimes
11527 be a TYPENAME_TYPE. For example, given:
11529 template <typename T>
11530 int S<T>::R::i = 3;
11532 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11533 this context, we must resolve S<T>::R to an ordinary
11534 type, rather than a typename type.
11536 The reason we normally avoid resolving TYPENAME_TYPEs
11537 is that a specialization of `S' might render
11538 `S<T>::R' not a type. However, if `S' is
11539 specialized, then this `i' will not be used, so there
11540 is no harm in resolving the types here. */
11541 tree type;
11543 /* Resolve the TYPENAME_TYPE. */
11544 type = resolve_typename_type (qualifying_scope,
11545 /*only_current_p=*/false);
11546 /* If that failed, the declarator is invalid. */
11547 if (type == error_mark_node)
11548 error ("%<%T::%D%> is not a type",
11549 TYPE_CONTEXT (qualifying_scope),
11550 TYPE_IDENTIFIER (qualifying_scope));
11551 qualifying_scope = type;
11554 sfk = sfk_none;
11555 if (unqualified_name)
11557 tree class_type;
11559 if (qualifying_scope
11560 && CLASS_TYPE_P (qualifying_scope))
11561 class_type = qualifying_scope;
11562 else
11563 class_type = current_class_type;
11565 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11567 tree name_type = TREE_TYPE (unqualified_name);
11568 if (class_type && same_type_p (name_type, class_type))
11570 if (qualifying_scope
11571 && CLASSTYPE_USE_TEMPLATE (name_type))
11573 error ("invalid use of constructor as a template");
11574 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11575 "name the constructor in a qualified name",
11576 class_type,
11577 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11578 class_type, name_type);
11579 declarator = cp_error_declarator;
11580 break;
11582 else
11583 unqualified_name = constructor_name (class_type);
11585 else
11587 /* We do not attempt to print the declarator
11588 here because we do not have enough
11589 information about its original syntactic
11590 form. */
11591 cp_parser_error (parser, "invalid declarator");
11592 declarator = cp_error_declarator;
11593 break;
11597 if (class_type)
11599 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11600 sfk = sfk_destructor;
11601 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11602 sfk = sfk_conversion;
11603 else if (/* There's no way to declare a constructor
11604 for an anonymous type, even if the type
11605 got a name for linkage purposes. */
11606 !TYPE_WAS_ANONYMOUS (class_type)
11607 && constructor_name_p (unqualified_name,
11608 class_type))
11610 unqualified_name = constructor_name (class_type);
11611 sfk = sfk_constructor;
11614 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11615 *ctor_dtor_or_conv_p = -1;
11618 declarator = make_id_declarator (qualifying_scope,
11619 unqualified_name,
11620 sfk);
11621 declarator->id_loc = token->location;
11623 handle_declarator:;
11624 scope = get_scope_of_declarator (declarator);
11625 if (scope)
11626 /* Any names that appear after the declarator-id for a
11627 member are looked up in the containing scope. */
11628 pushed_scope = push_scope (scope);
11629 parser->in_declarator_p = true;
11630 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11631 || (declarator && declarator->kind == cdk_id))
11632 /* Default args are only allowed on function
11633 declarations. */
11634 parser->default_arg_ok_p = saved_default_arg_ok_p;
11635 else
11636 parser->default_arg_ok_p = false;
11638 first = false;
11640 /* We're done. */
11641 else
11642 break;
11645 /* For an abstract declarator, we might wind up with nothing at this
11646 point. That's an error; the declarator is not optional. */
11647 if (!declarator)
11648 cp_parser_error (parser, "expected declarator");
11650 /* If we entered a scope, we must exit it now. */
11651 if (pushed_scope)
11652 pop_scope (pushed_scope);
11654 parser->default_arg_ok_p = saved_default_arg_ok_p;
11655 parser->in_declarator_p = saved_in_declarator_p;
11657 return declarator;
11660 /* Parse a ptr-operator.
11662 ptr-operator:
11663 * cv-qualifier-seq [opt]
11665 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11667 GNU Extension:
11669 ptr-operator:
11670 & cv-qualifier-seq [opt]
11672 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11673 Returns ADDR_EXPR if a reference was used. In the case of a
11674 pointer-to-member, *TYPE is filled in with the TYPE containing the
11675 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11676 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11677 ERROR_MARK if an error occurred. */
11679 static enum tree_code
11680 cp_parser_ptr_operator (cp_parser* parser,
11681 tree* type,
11682 cp_cv_quals *cv_quals)
11684 enum tree_code code = ERROR_MARK;
11685 cp_token *token;
11687 /* Assume that it's not a pointer-to-member. */
11688 *type = NULL_TREE;
11689 /* And that there are no cv-qualifiers. */
11690 *cv_quals = TYPE_UNQUALIFIED;
11692 /* Peek at the next token. */
11693 token = cp_lexer_peek_token (parser->lexer);
11694 /* If it's a `*' or `&' we have a pointer or reference. */
11695 if (token->type == CPP_MULT || token->type == CPP_AND)
11697 /* Remember which ptr-operator we were processing. */
11698 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11700 /* Consume the `*' or `&'. */
11701 cp_lexer_consume_token (parser->lexer);
11703 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11704 `&', if we are allowing GNU extensions. (The only qualifier
11705 that can legally appear after `&' is `restrict', but that is
11706 enforced during semantic analysis. */
11707 if (code == INDIRECT_REF
11708 || cp_parser_allow_gnu_extensions_p (parser))
11709 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11711 else
11713 /* Try the pointer-to-member case. */
11714 cp_parser_parse_tentatively (parser);
11715 /* Look for the optional `::' operator. */
11716 cp_parser_global_scope_opt (parser,
11717 /*current_scope_valid_p=*/false);
11718 /* Look for the nested-name specifier. */
11719 cp_parser_nested_name_specifier (parser,
11720 /*typename_keyword_p=*/false,
11721 /*check_dependency_p=*/true,
11722 /*type_p=*/false,
11723 /*is_declaration=*/false);
11724 /* If we found it, and the next token is a `*', then we are
11725 indeed looking at a pointer-to-member operator. */
11726 if (!cp_parser_error_occurred (parser)
11727 && cp_parser_require (parser, CPP_MULT, "`*'"))
11729 /* The type of which the member is a member is given by the
11730 current SCOPE. */
11731 *type = parser->scope;
11732 /* The next name will not be qualified. */
11733 parser->scope = NULL_TREE;
11734 parser->qualifying_scope = NULL_TREE;
11735 parser->object_scope = NULL_TREE;
11736 /* Indicate that the `*' operator was used. */
11737 code = INDIRECT_REF;
11738 /* Look for the optional cv-qualifier-seq. */
11739 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11741 /* If that didn't work we don't have a ptr-operator. */
11742 if (!cp_parser_parse_definitely (parser))
11743 cp_parser_error (parser, "expected ptr-operator");
11746 return code;
11749 /* Parse an (optional) cv-qualifier-seq.
11751 cv-qualifier-seq:
11752 cv-qualifier cv-qualifier-seq [opt]
11754 cv-qualifier:
11755 const
11756 volatile
11758 GNU Extension:
11760 cv-qualifier:
11761 __restrict__
11763 Returns a bitmask representing the cv-qualifiers. */
11765 static cp_cv_quals
11766 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11768 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11770 while (true)
11772 cp_token *token;
11773 cp_cv_quals cv_qualifier;
11775 /* Peek at the next token. */
11776 token = cp_lexer_peek_token (parser->lexer);
11777 /* See if it's a cv-qualifier. */
11778 switch (token->keyword)
11780 case RID_CONST:
11781 cv_qualifier = TYPE_QUAL_CONST;
11782 break;
11784 case RID_VOLATILE:
11785 cv_qualifier = TYPE_QUAL_VOLATILE;
11786 break;
11788 case RID_RESTRICT:
11789 cv_qualifier = TYPE_QUAL_RESTRICT;
11790 break;
11792 default:
11793 cv_qualifier = TYPE_UNQUALIFIED;
11794 break;
11797 if (!cv_qualifier)
11798 break;
11800 if (cv_quals & cv_qualifier)
11802 error ("duplicate cv-qualifier");
11803 cp_lexer_purge_token (parser->lexer);
11805 else
11807 cp_lexer_consume_token (parser->lexer);
11808 cv_quals |= cv_qualifier;
11812 return cv_quals;
11815 /* Parse a declarator-id.
11817 declarator-id:
11818 id-expression
11819 :: [opt] nested-name-specifier [opt] type-name
11821 In the `id-expression' case, the value returned is as for
11822 cp_parser_id_expression if the id-expression was an unqualified-id.
11823 If the id-expression was a qualified-id, then a SCOPE_REF is
11824 returned. The first operand is the scope (either a NAMESPACE_DECL
11825 or TREE_TYPE), but the second is still just a representation of an
11826 unqualified-id. */
11828 static tree
11829 cp_parser_declarator_id (cp_parser* parser)
11831 tree id;
11832 /* The expression must be an id-expression. Assume that qualified
11833 names are the names of types so that:
11835 template <class T>
11836 int S<T>::R::i = 3;
11838 will work; we must treat `S<T>::R' as the name of a type.
11839 Similarly, assume that qualified names are templates, where
11840 required, so that:
11842 template <class T>
11843 int S<T>::R<T>::i = 3;
11845 will work, too. */
11846 id = cp_parser_id_expression (parser,
11847 /*template_keyword_p=*/false,
11848 /*check_dependency_p=*/false,
11849 /*template_p=*/NULL,
11850 /*declarator_p=*/true);
11851 if (BASELINK_P (id))
11852 id = BASELINK_FUNCTIONS (id);
11853 return id;
11856 /* Parse a type-id.
11858 type-id:
11859 type-specifier-seq abstract-declarator [opt]
11861 Returns the TYPE specified. */
11863 static tree
11864 cp_parser_type_id (cp_parser* parser)
11866 cp_decl_specifier_seq type_specifier_seq;
11867 cp_declarator *abstract_declarator;
11869 /* Parse the type-specifier-seq. */
11870 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11871 &type_specifier_seq);
11872 if (type_specifier_seq.type == error_mark_node)
11873 return error_mark_node;
11875 /* There might or might not be an abstract declarator. */
11876 cp_parser_parse_tentatively (parser);
11877 /* Look for the declarator. */
11878 abstract_declarator
11879 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11880 /*parenthesized_p=*/NULL,
11881 /*member_p=*/false);
11882 /* Check to see if there really was a declarator. */
11883 if (!cp_parser_parse_definitely (parser))
11884 abstract_declarator = NULL;
11886 return groktypename (&type_specifier_seq, abstract_declarator);
11889 /* Parse a type-specifier-seq.
11891 type-specifier-seq:
11892 type-specifier type-specifier-seq [opt]
11894 GNU extension:
11896 type-specifier-seq:
11897 attributes type-specifier-seq [opt]
11899 If IS_CONDITION is true, we are at the start of a "condition",
11900 e.g., we've just seen "if (".
11902 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11904 static void
11905 cp_parser_type_specifier_seq (cp_parser* parser,
11906 bool is_condition,
11907 cp_decl_specifier_seq *type_specifier_seq)
11909 bool seen_type_specifier = false;
11910 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11912 /* Clear the TYPE_SPECIFIER_SEQ. */
11913 clear_decl_specs (type_specifier_seq);
11915 /* Parse the type-specifiers and attributes. */
11916 while (true)
11918 tree type_specifier;
11919 bool is_cv_qualifier;
11921 /* Check for attributes first. */
11922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11924 type_specifier_seq->attributes =
11925 chainon (type_specifier_seq->attributes,
11926 cp_parser_attributes_opt (parser));
11927 continue;
11930 /* Look for the type-specifier. */
11931 type_specifier = cp_parser_type_specifier (parser,
11932 flags,
11933 type_specifier_seq,
11934 /*is_declaration=*/false,
11935 NULL,
11936 &is_cv_qualifier);
11937 if (!type_specifier)
11939 /* If the first type-specifier could not be found, this is not a
11940 type-specifier-seq at all. */
11941 if (!seen_type_specifier)
11943 cp_parser_error (parser, "expected type-specifier");
11944 type_specifier_seq->type = error_mark_node;
11945 return;
11947 /* If subsequent type-specifiers could not be found, the
11948 type-specifier-seq is complete. */
11949 break;
11952 seen_type_specifier = true;
11953 /* The standard says that a condition can be:
11955 type-specifier-seq declarator = assignment-expression
11957 However, given:
11959 struct S {};
11960 if (int S = ...)
11962 we should treat the "S" as a declarator, not as a
11963 type-specifier. The standard doesn't say that explicitly for
11964 type-specifier-seq, but it does say that for
11965 decl-specifier-seq in an ordinary declaration. Perhaps it
11966 would be clearer just to allow a decl-specifier-seq here, and
11967 then add a semantic restriction that if any decl-specifiers
11968 that are not type-specifiers appear, the program is invalid. */
11969 if (is_condition && !is_cv_qualifier)
11970 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11974 /* Parse a parameter-declaration-clause.
11976 parameter-declaration-clause:
11977 parameter-declaration-list [opt] ... [opt]
11978 parameter-declaration-list , ...
11980 Returns a representation for the parameter declarations. A return
11981 value of NULL indicates a parameter-declaration-clause consisting
11982 only of an ellipsis. */
11984 static cp_parameter_declarator *
11985 cp_parser_parameter_declaration_clause (cp_parser* parser)
11987 cp_parameter_declarator *parameters;
11988 cp_token *token;
11989 bool ellipsis_p;
11990 bool is_error;
11992 /* Peek at the next token. */
11993 token = cp_lexer_peek_token (parser->lexer);
11994 /* Check for trivial parameter-declaration-clauses. */
11995 if (token->type == CPP_ELLIPSIS)
11997 /* Consume the `...' token. */
11998 cp_lexer_consume_token (parser->lexer);
11999 return NULL;
12001 else if (token->type == CPP_CLOSE_PAREN)
12002 /* There are no parameters. */
12004 #ifndef NO_IMPLICIT_EXTERN_C
12005 if (in_system_header && current_class_type == NULL
12006 && current_lang_name == lang_name_c)
12007 return NULL;
12008 else
12009 #endif
12010 return no_parameters;
12012 /* Check for `(void)', too, which is a special case. */
12013 else if (token->keyword == RID_VOID
12014 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12015 == CPP_CLOSE_PAREN))
12017 /* Consume the `void' token. */
12018 cp_lexer_consume_token (parser->lexer);
12019 /* There are no parameters. */
12020 return no_parameters;
12023 /* Parse the parameter-declaration-list. */
12024 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12025 /* If a parse error occurred while parsing the
12026 parameter-declaration-list, then the entire
12027 parameter-declaration-clause is erroneous. */
12028 if (is_error)
12029 return NULL;
12031 /* Peek at the next token. */
12032 token = cp_lexer_peek_token (parser->lexer);
12033 /* If it's a `,', the clause should terminate with an ellipsis. */
12034 if (token->type == CPP_COMMA)
12036 /* Consume the `,'. */
12037 cp_lexer_consume_token (parser->lexer);
12038 /* Expect an ellipsis. */
12039 ellipsis_p
12040 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12042 /* It might also be `...' if the optional trailing `,' was
12043 omitted. */
12044 else if (token->type == CPP_ELLIPSIS)
12046 /* Consume the `...' token. */
12047 cp_lexer_consume_token (parser->lexer);
12048 /* And remember that we saw it. */
12049 ellipsis_p = true;
12051 else
12052 ellipsis_p = false;
12054 /* Finish the parameter list. */
12055 if (parameters && ellipsis_p)
12056 parameters->ellipsis_p = true;
12058 return parameters;
12061 /* Parse a parameter-declaration-list.
12063 parameter-declaration-list:
12064 parameter-declaration
12065 parameter-declaration-list , parameter-declaration
12067 Returns a representation of the parameter-declaration-list, as for
12068 cp_parser_parameter_declaration_clause. However, the
12069 `void_list_node' is never appended to the list. Upon return,
12070 *IS_ERROR will be true iff an error occurred. */
12072 static cp_parameter_declarator *
12073 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12075 cp_parameter_declarator *parameters = NULL;
12076 cp_parameter_declarator **tail = &parameters;
12078 /* Assume all will go well. */
12079 *is_error = false;
12081 /* Look for more parameters. */
12082 while (true)
12084 cp_parameter_declarator *parameter;
12085 bool parenthesized_p;
12086 /* Parse the parameter. */
12087 parameter
12088 = cp_parser_parameter_declaration (parser,
12089 /*template_parm_p=*/false,
12090 &parenthesized_p);
12092 /* If a parse error occurred parsing the parameter declaration,
12093 then the entire parameter-declaration-list is erroneous. */
12094 if (!parameter)
12096 *is_error = true;
12097 parameters = NULL;
12098 break;
12100 /* Add the new parameter to the list. */
12101 *tail = parameter;
12102 tail = &parameter->next;
12104 /* Peek at the next token. */
12105 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12106 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12107 /* These are for Objective-C++ */
12108 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12109 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12110 /* The parameter-declaration-list is complete. */
12111 break;
12112 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12114 cp_token *token;
12116 /* Peek at the next token. */
12117 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12118 /* If it's an ellipsis, then the list is complete. */
12119 if (token->type == CPP_ELLIPSIS)
12120 break;
12121 /* Otherwise, there must be more parameters. Consume the
12122 `,'. */
12123 cp_lexer_consume_token (parser->lexer);
12124 /* When parsing something like:
12126 int i(float f, double d)
12128 we can tell after seeing the declaration for "f" that we
12129 are not looking at an initialization of a variable "i",
12130 but rather at the declaration of a function "i".
12132 Due to the fact that the parsing of template arguments
12133 (as specified to a template-id) requires backtracking we
12134 cannot use this technique when inside a template argument
12135 list. */
12136 if (!parser->in_template_argument_list_p
12137 && !parser->in_type_id_in_expr_p
12138 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12139 /* However, a parameter-declaration of the form
12140 "foat(f)" (which is a valid declaration of a
12141 parameter "f") can also be interpreted as an
12142 expression (the conversion of "f" to "float"). */
12143 && !parenthesized_p)
12144 cp_parser_commit_to_tentative_parse (parser);
12146 else
12148 cp_parser_error (parser, "expected %<,%> or %<...%>");
12149 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12150 cp_parser_skip_to_closing_parenthesis (parser,
12151 /*recovering=*/true,
12152 /*or_comma=*/false,
12153 /*consume_paren=*/false);
12154 break;
12158 return parameters;
12161 /* Parse a parameter declaration.
12163 parameter-declaration:
12164 decl-specifier-seq declarator
12165 decl-specifier-seq declarator = assignment-expression
12166 decl-specifier-seq abstract-declarator [opt]
12167 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12169 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12170 declares a template parameter. (In that case, a non-nested `>'
12171 token encountered during the parsing of the assignment-expression
12172 is not interpreted as a greater-than operator.)
12174 Returns a representation of the parameter, or NULL if an error
12175 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12176 true iff the declarator is of the form "(p)". */
12178 static cp_parameter_declarator *
12179 cp_parser_parameter_declaration (cp_parser *parser,
12180 bool template_parm_p,
12181 bool *parenthesized_p)
12183 int declares_class_or_enum;
12184 bool greater_than_is_operator_p;
12185 cp_decl_specifier_seq decl_specifiers;
12186 cp_declarator *declarator;
12187 tree default_argument;
12188 cp_token *token;
12189 const char *saved_message;
12191 /* In a template parameter, `>' is not an operator.
12193 [temp.param]
12195 When parsing a default template-argument for a non-type
12196 template-parameter, the first non-nested `>' is taken as the end
12197 of the template parameter-list rather than a greater-than
12198 operator. */
12199 greater_than_is_operator_p = !template_parm_p;
12201 /* Type definitions may not appear in parameter types. */
12202 saved_message = parser->type_definition_forbidden_message;
12203 parser->type_definition_forbidden_message
12204 = "types may not be defined in parameter types";
12206 /* Parse the declaration-specifiers. */
12207 cp_parser_decl_specifier_seq (parser,
12208 CP_PARSER_FLAGS_NONE,
12209 &decl_specifiers,
12210 &declares_class_or_enum);
12211 /* If an error occurred, there's no reason to attempt to parse the
12212 rest of the declaration. */
12213 if (cp_parser_error_occurred (parser))
12215 parser->type_definition_forbidden_message = saved_message;
12216 return NULL;
12219 /* Peek at the next token. */
12220 token = cp_lexer_peek_token (parser->lexer);
12221 /* If the next token is a `)', `,', `=', `>', or `...', then there
12222 is no declarator. */
12223 if (token->type == CPP_CLOSE_PAREN
12224 || token->type == CPP_COMMA
12225 || token->type == CPP_EQ
12226 || token->type == CPP_ELLIPSIS
12227 || token->type == CPP_GREATER)
12229 declarator = NULL;
12230 if (parenthesized_p)
12231 *parenthesized_p = false;
12233 /* Otherwise, there should be a declarator. */
12234 else
12236 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12237 parser->default_arg_ok_p = false;
12239 /* After seeing a decl-specifier-seq, if the next token is not a
12240 "(", there is no possibility that the code is a valid
12241 expression. Therefore, if parsing tentatively, we commit at
12242 this point. */
12243 if (!parser->in_template_argument_list_p
12244 /* In an expression context, having seen:
12246 (int((char ...
12248 we cannot be sure whether we are looking at a
12249 function-type (taking a "char" as a parameter) or a cast
12250 of some object of type "char" to "int". */
12251 && !parser->in_type_id_in_expr_p
12252 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12253 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12254 cp_parser_commit_to_tentative_parse (parser);
12255 /* Parse the declarator. */
12256 declarator = cp_parser_declarator (parser,
12257 CP_PARSER_DECLARATOR_EITHER,
12258 /*ctor_dtor_or_conv_p=*/NULL,
12259 parenthesized_p,
12260 /*member_p=*/false);
12261 parser->default_arg_ok_p = saved_default_arg_ok_p;
12262 /* After the declarator, allow more attributes. */
12263 decl_specifiers.attributes
12264 = chainon (decl_specifiers.attributes,
12265 cp_parser_attributes_opt (parser));
12268 /* The restriction on defining new types applies only to the type
12269 of the parameter, not to the default argument. */
12270 parser->type_definition_forbidden_message = saved_message;
12272 /* If the next token is `=', then process a default argument. */
12273 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12275 bool saved_greater_than_is_operator_p;
12276 /* Consume the `='. */
12277 cp_lexer_consume_token (parser->lexer);
12279 /* If we are defining a class, then the tokens that make up the
12280 default argument must be saved and processed later. */
12281 if (!template_parm_p && at_class_scope_p ()
12282 && TYPE_BEING_DEFINED (current_class_type))
12284 unsigned depth = 0;
12285 cp_token *first_token;
12286 cp_token *token;
12288 /* Add tokens until we have processed the entire default
12289 argument. We add the range [first_token, token). */
12290 first_token = cp_lexer_peek_token (parser->lexer);
12291 while (true)
12293 bool done = false;
12295 /* Peek at the next token. */
12296 token = cp_lexer_peek_token (parser->lexer);
12297 /* What we do depends on what token we have. */
12298 switch (token->type)
12300 /* In valid code, a default argument must be
12301 immediately followed by a `,' `)', or `...'. */
12302 case CPP_COMMA:
12303 case CPP_CLOSE_PAREN:
12304 case CPP_ELLIPSIS:
12305 /* If we run into a non-nested `;', `}', or `]',
12306 then the code is invalid -- but the default
12307 argument is certainly over. */
12308 case CPP_SEMICOLON:
12309 case CPP_CLOSE_BRACE:
12310 case CPP_CLOSE_SQUARE:
12311 if (depth == 0)
12312 done = true;
12313 /* Update DEPTH, if necessary. */
12314 else if (token->type == CPP_CLOSE_PAREN
12315 || token->type == CPP_CLOSE_BRACE
12316 || token->type == CPP_CLOSE_SQUARE)
12317 --depth;
12318 break;
12320 case CPP_OPEN_PAREN:
12321 case CPP_OPEN_SQUARE:
12322 case CPP_OPEN_BRACE:
12323 ++depth;
12324 break;
12326 case CPP_GREATER:
12327 /* If we see a non-nested `>', and `>' is not an
12328 operator, then it marks the end of the default
12329 argument. */
12330 if (!depth && !greater_than_is_operator_p)
12331 done = true;
12332 break;
12334 /* If we run out of tokens, issue an error message. */
12335 case CPP_EOF:
12336 case CPP_PRAGMA_EOL:
12337 error ("file ends in default argument");
12338 done = true;
12339 break;
12341 case CPP_NAME:
12342 case CPP_SCOPE:
12343 /* In these cases, we should look for template-ids.
12344 For example, if the default argument is
12345 `X<int, double>()', we need to do name lookup to
12346 figure out whether or not `X' is a template; if
12347 so, the `,' does not end the default argument.
12349 That is not yet done. */
12350 break;
12352 default:
12353 break;
12356 /* If we've reached the end, stop. */
12357 if (done)
12358 break;
12360 /* Add the token to the token block. */
12361 token = cp_lexer_consume_token (parser->lexer);
12364 /* Create a DEFAULT_ARG to represented the unparsed default
12365 argument. */
12366 default_argument = make_node (DEFAULT_ARG);
12367 DEFARG_TOKENS (default_argument)
12368 = cp_token_cache_new (first_token, token);
12369 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12371 /* Outside of a class definition, we can just parse the
12372 assignment-expression. */
12373 else
12375 bool saved_local_variables_forbidden_p;
12377 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12378 set correctly. */
12379 saved_greater_than_is_operator_p
12380 = parser->greater_than_is_operator_p;
12381 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12382 /* Local variable names (and the `this' keyword) may not
12383 appear in a default argument. */
12384 saved_local_variables_forbidden_p
12385 = parser->local_variables_forbidden_p;
12386 parser->local_variables_forbidden_p = true;
12387 /* Parse the assignment-expression. */
12388 default_argument
12389 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12390 /* Restore saved state. */
12391 parser->greater_than_is_operator_p
12392 = saved_greater_than_is_operator_p;
12393 parser->local_variables_forbidden_p
12394 = saved_local_variables_forbidden_p;
12396 if (!parser->default_arg_ok_p)
12398 if (!flag_pedantic_errors)
12399 warning (0, "deprecated use of default argument for parameter of non-function");
12400 else
12402 error ("default arguments are only permitted for function parameters");
12403 default_argument = NULL_TREE;
12407 else
12408 default_argument = NULL_TREE;
12410 return make_parameter_declarator (&decl_specifiers,
12411 declarator,
12412 default_argument);
12415 /* Parse a function-body.
12417 function-body:
12418 compound_statement */
12420 static void
12421 cp_parser_function_body (cp_parser *parser)
12423 cp_parser_compound_statement (parser, NULL, false);
12426 /* Parse a ctor-initializer-opt followed by a function-body. Return
12427 true if a ctor-initializer was present. */
12429 static bool
12430 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12432 tree body;
12433 bool ctor_initializer_p;
12435 /* Begin the function body. */
12436 body = begin_function_body ();
12437 /* Parse the optional ctor-initializer. */
12438 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12439 /* Parse the function-body. */
12440 cp_parser_function_body (parser);
12441 /* Finish the function body. */
12442 finish_function_body (body);
12444 return ctor_initializer_p;
12447 /* Parse an initializer.
12449 initializer:
12450 = initializer-clause
12451 ( expression-list )
12453 Returns an expression representing the initializer. If no
12454 initializer is present, NULL_TREE is returned.
12456 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12457 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12458 set to FALSE if there is no initializer present. If there is an
12459 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12460 is set to true; otherwise it is set to false. */
12462 static tree
12463 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12464 bool* non_constant_p)
12466 cp_token *token;
12467 tree init;
12469 /* Peek at the next token. */
12470 token = cp_lexer_peek_token (parser->lexer);
12472 /* Let our caller know whether or not this initializer was
12473 parenthesized. */
12474 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12475 /* Assume that the initializer is constant. */
12476 *non_constant_p = false;
12478 if (token->type == CPP_EQ)
12480 /* Consume the `='. */
12481 cp_lexer_consume_token (parser->lexer);
12482 /* Parse the initializer-clause. */
12483 init = cp_parser_initializer_clause (parser, non_constant_p);
12485 else if (token->type == CPP_OPEN_PAREN)
12486 init = cp_parser_parenthesized_expression_list (parser, false,
12487 /*cast_p=*/false,
12488 non_constant_p);
12489 else
12491 /* Anything else is an error. */
12492 cp_parser_error (parser, "expected initializer");
12493 init = error_mark_node;
12496 return init;
12499 /* Parse an initializer-clause.
12501 initializer-clause:
12502 assignment-expression
12503 { initializer-list , [opt] }
12506 Returns an expression representing the initializer.
12508 If the `assignment-expression' production is used the value
12509 returned is simply a representation for the expression.
12511 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12512 the elements of the initializer-list (or NULL, if the last
12513 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12514 NULL_TREE. There is no way to detect whether or not the optional
12515 trailing `,' was provided. NON_CONSTANT_P is as for
12516 cp_parser_initializer. */
12518 static tree
12519 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12521 tree initializer;
12523 /* Assume the expression is constant. */
12524 *non_constant_p = false;
12526 /* If it is not a `{', then we are looking at an
12527 assignment-expression. */
12528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12530 initializer
12531 = cp_parser_constant_expression (parser,
12532 /*allow_non_constant_p=*/true,
12533 non_constant_p);
12534 if (!*non_constant_p)
12535 initializer = fold_non_dependent_expr (initializer);
12537 else
12539 /* Consume the `{' token. */
12540 cp_lexer_consume_token (parser->lexer);
12541 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12542 initializer = make_node (CONSTRUCTOR);
12543 /* If it's not a `}', then there is a non-trivial initializer. */
12544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12546 /* Parse the initializer list. */
12547 CONSTRUCTOR_ELTS (initializer)
12548 = cp_parser_initializer_list (parser, non_constant_p);
12549 /* A trailing `,' token is allowed. */
12550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12551 cp_lexer_consume_token (parser->lexer);
12553 /* Now, there should be a trailing `}'. */
12554 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12557 return initializer;
12560 /* Parse an initializer-list.
12562 initializer-list:
12563 initializer-clause
12564 initializer-list , initializer-clause
12566 GNU Extension:
12568 initializer-list:
12569 identifier : initializer-clause
12570 initializer-list, identifier : initializer-clause
12572 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12573 for the initializer. If the INDEX of the elt is non-NULL, it is the
12574 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12575 as for cp_parser_initializer. */
12577 static VEC(constructor_elt,gc) *
12578 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12580 VEC(constructor_elt,gc) *v = NULL;
12582 /* Assume all of the expressions are constant. */
12583 *non_constant_p = false;
12585 /* Parse the rest of the list. */
12586 while (true)
12588 cp_token *token;
12589 tree identifier;
12590 tree initializer;
12591 bool clause_non_constant_p;
12593 /* If the next token is an identifier and the following one is a
12594 colon, we are looking at the GNU designated-initializer
12595 syntax. */
12596 if (cp_parser_allow_gnu_extensions_p (parser)
12597 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12598 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12600 /* Consume the identifier. */
12601 identifier = cp_lexer_consume_token (parser->lexer)->value;
12602 /* Consume the `:'. */
12603 cp_lexer_consume_token (parser->lexer);
12605 else
12606 identifier = NULL_TREE;
12608 /* Parse the initializer. */
12609 initializer = cp_parser_initializer_clause (parser,
12610 &clause_non_constant_p);
12611 /* If any clause is non-constant, so is the entire initializer. */
12612 if (clause_non_constant_p)
12613 *non_constant_p = true;
12615 /* Add it to the vector. */
12616 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12618 /* If the next token is not a comma, we have reached the end of
12619 the list. */
12620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12621 break;
12623 /* Peek at the next token. */
12624 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12625 /* If the next token is a `}', then we're still done. An
12626 initializer-clause can have a trailing `,' after the
12627 initializer-list and before the closing `}'. */
12628 if (token->type == CPP_CLOSE_BRACE)
12629 break;
12631 /* Consume the `,' token. */
12632 cp_lexer_consume_token (parser->lexer);
12635 return v;
12638 /* Classes [gram.class] */
12640 /* Parse a class-name.
12642 class-name:
12643 identifier
12644 template-id
12646 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12647 to indicate that names looked up in dependent types should be
12648 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12649 keyword has been used to indicate that the name that appears next
12650 is a template. TAG_TYPE indicates the explicit tag given before
12651 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12652 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12653 is the class being defined in a class-head.
12655 Returns the TYPE_DECL representing the class. */
12657 static tree
12658 cp_parser_class_name (cp_parser *parser,
12659 bool typename_keyword_p,
12660 bool template_keyword_p,
12661 enum tag_types tag_type,
12662 bool check_dependency_p,
12663 bool class_head_p,
12664 bool is_declaration)
12666 tree decl;
12667 tree scope;
12668 bool typename_p;
12669 cp_token *token;
12671 /* All class-names start with an identifier. */
12672 token = cp_lexer_peek_token (parser->lexer);
12673 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12675 cp_parser_error (parser, "expected class-name");
12676 return error_mark_node;
12679 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12680 to a template-id, so we save it here. */
12681 scope = parser->scope;
12682 if (scope == error_mark_node)
12683 return error_mark_node;
12685 /* Any name names a type if we're following the `typename' keyword
12686 in a qualified name where the enclosing scope is type-dependent. */
12687 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12688 && dependent_type_p (scope));
12689 /* Handle the common case (an identifier, but not a template-id)
12690 efficiently. */
12691 if (token->type == CPP_NAME
12692 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12694 cp_token *identifier_token;
12695 tree identifier;
12696 bool ambiguous_p;
12698 /* Look for the identifier. */
12699 identifier_token = cp_lexer_peek_token (parser->lexer);
12700 ambiguous_p = identifier_token->ambiguous_p;
12701 identifier = cp_parser_identifier (parser);
12702 /* If the next token isn't an identifier, we are certainly not
12703 looking at a class-name. */
12704 if (identifier == error_mark_node)
12705 decl = error_mark_node;
12706 /* If we know this is a type-name, there's no need to look it
12707 up. */
12708 else if (typename_p)
12709 decl = identifier;
12710 else
12712 tree ambiguous_decls;
12713 /* If we already know that this lookup is ambiguous, then
12714 we've already issued an error message; there's no reason
12715 to check again. */
12716 if (ambiguous_p)
12718 cp_parser_simulate_error (parser);
12719 return error_mark_node;
12721 /* If the next token is a `::', then the name must be a type
12722 name.
12724 [basic.lookup.qual]
12726 During the lookup for a name preceding the :: scope
12727 resolution operator, object, function, and enumerator
12728 names are ignored. */
12729 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12730 tag_type = typename_type;
12731 /* Look up the name. */
12732 decl = cp_parser_lookup_name (parser, identifier,
12733 tag_type,
12734 /*is_template=*/false,
12735 /*is_namespace=*/false,
12736 check_dependency_p,
12737 &ambiguous_decls);
12738 if (ambiguous_decls)
12740 error ("reference to %qD is ambiguous", identifier);
12741 print_candidates (ambiguous_decls);
12742 if (cp_parser_parsing_tentatively (parser))
12744 identifier_token->ambiguous_p = true;
12745 cp_parser_simulate_error (parser);
12747 return error_mark_node;
12751 else
12753 /* Try a template-id. */
12754 decl = cp_parser_template_id (parser, template_keyword_p,
12755 check_dependency_p,
12756 is_declaration);
12757 if (decl == error_mark_node)
12758 return error_mark_node;
12761 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12763 /* If this is a typename, create a TYPENAME_TYPE. */
12764 if (typename_p && decl != error_mark_node)
12766 decl = make_typename_type (scope, decl, typename_type,
12767 /*complain=*/tf_error);
12768 if (decl != error_mark_node)
12769 decl = TYPE_NAME (decl);
12772 /* Check to see that it is really the name of a class. */
12773 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12774 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12775 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12776 /* Situations like this:
12778 template <typename T> struct A {
12779 typename T::template X<int>::I i;
12782 are problematic. Is `T::template X<int>' a class-name? The
12783 standard does not seem to be definitive, but there is no other
12784 valid interpretation of the following `::'. Therefore, those
12785 names are considered class-names. */
12786 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12787 else if (decl == error_mark_node
12788 || TREE_CODE (decl) != TYPE_DECL
12789 || TREE_TYPE (decl) == error_mark_node
12790 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12792 cp_parser_error (parser, "expected class-name");
12793 return error_mark_node;
12796 return decl;
12799 /* Parse a class-specifier.
12801 class-specifier:
12802 class-head { member-specification [opt] }
12804 Returns the TREE_TYPE representing the class. */
12806 static tree
12807 cp_parser_class_specifier (cp_parser* parser)
12809 cp_token *token;
12810 tree type;
12811 tree attributes = NULL_TREE;
12812 int has_trailing_semicolon;
12813 bool nested_name_specifier_p;
12814 unsigned saved_num_template_parameter_lists;
12815 tree old_scope = NULL_TREE;
12816 tree scope = NULL_TREE;
12818 push_deferring_access_checks (dk_no_deferred);
12820 /* Parse the class-head. */
12821 type = cp_parser_class_head (parser,
12822 &nested_name_specifier_p,
12823 &attributes);
12824 /* If the class-head was a semantic disaster, skip the entire body
12825 of the class. */
12826 if (!type)
12828 cp_parser_skip_to_end_of_block_or_statement (parser);
12829 pop_deferring_access_checks ();
12830 return error_mark_node;
12833 /* Look for the `{'. */
12834 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12836 pop_deferring_access_checks ();
12837 return error_mark_node;
12840 /* Issue an error message if type-definitions are forbidden here. */
12841 cp_parser_check_type_definition (parser);
12842 /* Remember that we are defining one more class. */
12843 ++parser->num_classes_being_defined;
12844 /* Inside the class, surrounding template-parameter-lists do not
12845 apply. */
12846 saved_num_template_parameter_lists
12847 = parser->num_template_parameter_lists;
12848 parser->num_template_parameter_lists = 0;
12850 /* Start the class. */
12851 if (nested_name_specifier_p)
12853 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12854 old_scope = push_inner_scope (scope);
12856 type = begin_class_definition (type);
12858 if (type == error_mark_node)
12859 /* If the type is erroneous, skip the entire body of the class. */
12860 cp_parser_skip_to_closing_brace (parser);
12861 else
12862 /* Parse the member-specification. */
12863 cp_parser_member_specification_opt (parser);
12865 /* Look for the trailing `}'. */
12866 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12867 /* We get better error messages by noticing a common problem: a
12868 missing trailing `;'. */
12869 token = cp_lexer_peek_token (parser->lexer);
12870 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12871 /* Look for trailing attributes to apply to this class. */
12872 if (cp_parser_allow_gnu_extensions_p (parser))
12874 tree sub_attr = cp_parser_attributes_opt (parser);
12875 attributes = chainon (attributes, sub_attr);
12877 if (type != error_mark_node)
12878 type = finish_struct (type, attributes);
12879 if (nested_name_specifier_p)
12880 pop_inner_scope (old_scope, scope);
12881 /* If this class is not itself within the scope of another class,
12882 then we need to parse the bodies of all of the queued function
12883 definitions. Note that the queued functions defined in a class
12884 are not always processed immediately following the
12885 class-specifier for that class. Consider:
12887 struct A {
12888 struct B { void f() { sizeof (A); } };
12891 If `f' were processed before the processing of `A' were
12892 completed, there would be no way to compute the size of `A'.
12893 Note that the nesting we are interested in here is lexical --
12894 not the semantic nesting given by TYPE_CONTEXT. In particular,
12895 for:
12897 struct A { struct B; };
12898 struct A::B { void f() { } };
12900 there is no need to delay the parsing of `A::B::f'. */
12901 if (--parser->num_classes_being_defined == 0)
12903 tree queue_entry;
12904 tree fn;
12905 tree class_type = NULL_TREE;
12906 tree pushed_scope = NULL_TREE;
12908 /* In a first pass, parse default arguments to the functions.
12909 Then, in a second pass, parse the bodies of the functions.
12910 This two-phased approach handles cases like:
12912 struct S {
12913 void f() { g(); }
12914 void g(int i = 3);
12918 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12919 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12920 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12921 TREE_PURPOSE (parser->unparsed_functions_queues)
12922 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12924 fn = TREE_VALUE (queue_entry);
12925 /* If there are default arguments that have not yet been processed,
12926 take care of them now. */
12927 if (class_type != TREE_PURPOSE (queue_entry))
12929 if (pushed_scope)
12930 pop_scope (pushed_scope);
12931 class_type = TREE_PURPOSE (queue_entry);
12932 pushed_scope = push_scope (class_type);
12934 /* Make sure that any template parameters are in scope. */
12935 maybe_begin_member_template_processing (fn);
12936 /* Parse the default argument expressions. */
12937 cp_parser_late_parsing_default_args (parser, fn);
12938 /* Remove any template parameters from the symbol table. */
12939 maybe_end_member_template_processing ();
12941 if (pushed_scope)
12942 pop_scope (pushed_scope);
12943 /* Now parse the body of the functions. */
12944 for (TREE_VALUE (parser->unparsed_functions_queues)
12945 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12946 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12947 TREE_VALUE (parser->unparsed_functions_queues)
12948 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12950 /* Figure out which function we need to process. */
12951 fn = TREE_VALUE (queue_entry);
12952 /* Parse the function. */
12953 cp_parser_late_parsing_for_member (parser, fn);
12957 /* Put back any saved access checks. */
12958 pop_deferring_access_checks ();
12960 /* Restore the count of active template-parameter-lists. */
12961 parser->num_template_parameter_lists
12962 = saved_num_template_parameter_lists;
12964 return type;
12967 /* Parse a class-head.
12969 class-head:
12970 class-key identifier [opt] base-clause [opt]
12971 class-key nested-name-specifier identifier base-clause [opt]
12972 class-key nested-name-specifier [opt] template-id
12973 base-clause [opt]
12975 GNU Extensions:
12976 class-key attributes identifier [opt] base-clause [opt]
12977 class-key attributes nested-name-specifier identifier base-clause [opt]
12978 class-key attributes nested-name-specifier [opt] template-id
12979 base-clause [opt]
12981 Returns the TYPE of the indicated class. Sets
12982 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12983 involving a nested-name-specifier was used, and FALSE otherwise.
12985 Returns error_mark_node if this is not a class-head.
12987 Returns NULL_TREE if the class-head is syntactically valid, but
12988 semantically invalid in a way that means we should skip the entire
12989 body of the class. */
12991 static tree
12992 cp_parser_class_head (cp_parser* parser,
12993 bool* nested_name_specifier_p,
12994 tree *attributes_p)
12996 tree nested_name_specifier;
12997 enum tag_types class_key;
12998 tree id = NULL_TREE;
12999 tree type = NULL_TREE;
13000 tree attributes;
13001 bool template_id_p = false;
13002 bool qualified_p = false;
13003 bool invalid_nested_name_p = false;
13004 bool invalid_explicit_specialization_p = false;
13005 tree pushed_scope = NULL_TREE;
13006 unsigned num_templates;
13007 tree bases;
13009 /* Assume no nested-name-specifier will be present. */
13010 *nested_name_specifier_p = false;
13011 /* Assume no template parameter lists will be used in defining the
13012 type. */
13013 num_templates = 0;
13015 /* Look for the class-key. */
13016 class_key = cp_parser_class_key (parser);
13017 if (class_key == none_type)
13018 return error_mark_node;
13020 /* Parse the attributes. */
13021 attributes = cp_parser_attributes_opt (parser);
13023 /* If the next token is `::', that is invalid -- but sometimes
13024 people do try to write:
13026 struct ::S {};
13028 Handle this gracefully by accepting the extra qualifier, and then
13029 issuing an error about it later if this really is a
13030 class-head. If it turns out just to be an elaborated type
13031 specifier, remain silent. */
13032 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13033 qualified_p = true;
13035 push_deferring_access_checks (dk_no_check);
13037 /* Determine the name of the class. Begin by looking for an
13038 optional nested-name-specifier. */
13039 nested_name_specifier
13040 = cp_parser_nested_name_specifier_opt (parser,
13041 /*typename_keyword_p=*/false,
13042 /*check_dependency_p=*/false,
13043 /*type_p=*/false,
13044 /*is_declaration=*/false);
13045 /* If there was a nested-name-specifier, then there *must* be an
13046 identifier. */
13047 if (nested_name_specifier)
13049 /* Although the grammar says `identifier', it really means
13050 `class-name' or `template-name'. You are only allowed to
13051 define a class that has already been declared with this
13052 syntax.
13054 The proposed resolution for Core Issue 180 says that whever
13055 you see `class T::X' you should treat `X' as a type-name.
13057 It is OK to define an inaccessible class; for example:
13059 class A { class B; };
13060 class A::B {};
13062 We do not know if we will see a class-name, or a
13063 template-name. We look for a class-name first, in case the
13064 class-name is a template-id; if we looked for the
13065 template-name first we would stop after the template-name. */
13066 cp_parser_parse_tentatively (parser);
13067 type = cp_parser_class_name (parser,
13068 /*typename_keyword_p=*/false,
13069 /*template_keyword_p=*/false,
13070 class_type,
13071 /*check_dependency_p=*/false,
13072 /*class_head_p=*/true,
13073 /*is_declaration=*/false);
13074 /* If that didn't work, ignore the nested-name-specifier. */
13075 if (!cp_parser_parse_definitely (parser))
13077 invalid_nested_name_p = true;
13078 id = cp_parser_identifier (parser);
13079 if (id == error_mark_node)
13080 id = NULL_TREE;
13082 /* If we could not find a corresponding TYPE, treat this
13083 declaration like an unqualified declaration. */
13084 if (type == error_mark_node)
13085 nested_name_specifier = NULL_TREE;
13086 /* Otherwise, count the number of templates used in TYPE and its
13087 containing scopes. */
13088 else
13090 tree scope;
13092 for (scope = TREE_TYPE (type);
13093 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13094 scope = (TYPE_P (scope)
13095 ? TYPE_CONTEXT (scope)
13096 : DECL_CONTEXT (scope)))
13097 if (TYPE_P (scope)
13098 && CLASS_TYPE_P (scope)
13099 && CLASSTYPE_TEMPLATE_INFO (scope)
13100 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13101 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13102 ++num_templates;
13105 /* Otherwise, the identifier is optional. */
13106 else
13108 /* We don't know whether what comes next is a template-id,
13109 an identifier, or nothing at all. */
13110 cp_parser_parse_tentatively (parser);
13111 /* Check for a template-id. */
13112 id = cp_parser_template_id (parser,
13113 /*template_keyword_p=*/false,
13114 /*check_dependency_p=*/true,
13115 /*is_declaration=*/true);
13116 /* If that didn't work, it could still be an identifier. */
13117 if (!cp_parser_parse_definitely (parser))
13119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13120 id = cp_parser_identifier (parser);
13121 else
13122 id = NULL_TREE;
13124 else
13126 template_id_p = true;
13127 ++num_templates;
13131 pop_deferring_access_checks ();
13133 if (id)
13134 cp_parser_check_for_invalid_template_id (parser, id);
13136 /* If it's not a `:' or a `{' then we can't really be looking at a
13137 class-head, since a class-head only appears as part of a
13138 class-specifier. We have to detect this situation before calling
13139 xref_tag, since that has irreversible side-effects. */
13140 if (!cp_parser_next_token_starts_class_definition_p (parser))
13142 cp_parser_error (parser, "expected %<{%> or %<:%>");
13143 return error_mark_node;
13146 /* At this point, we're going ahead with the class-specifier, even
13147 if some other problem occurs. */
13148 cp_parser_commit_to_tentative_parse (parser);
13149 /* Issue the error about the overly-qualified name now. */
13150 if (qualified_p)
13151 cp_parser_error (parser,
13152 "global qualification of class name is invalid");
13153 else if (invalid_nested_name_p)
13154 cp_parser_error (parser,
13155 "qualified name does not name a class");
13156 else if (nested_name_specifier)
13158 tree scope;
13160 /* Reject typedef-names in class heads. */
13161 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13163 error ("invalid class name in declaration of %qD", type);
13164 type = NULL_TREE;
13165 goto done;
13168 /* Figure out in what scope the declaration is being placed. */
13169 scope = current_scope ();
13170 /* If that scope does not contain the scope in which the
13171 class was originally declared, the program is invalid. */
13172 if (scope && !is_ancestor (scope, nested_name_specifier))
13174 error ("declaration of %qD in %qD which does not enclose %qD",
13175 type, scope, nested_name_specifier);
13176 type = NULL_TREE;
13177 goto done;
13179 /* [dcl.meaning]
13181 A declarator-id shall not be qualified exception of the
13182 definition of a ... nested class outside of its class
13183 ... [or] a the definition or explicit instantiation of a
13184 class member of a namespace outside of its namespace. */
13185 if (scope == nested_name_specifier)
13187 pedwarn ("extra qualification ignored");
13188 nested_name_specifier = NULL_TREE;
13189 num_templates = 0;
13192 /* An explicit-specialization must be preceded by "template <>". If
13193 it is not, try to recover gracefully. */
13194 if (at_namespace_scope_p ()
13195 && parser->num_template_parameter_lists == 0
13196 && template_id_p)
13198 error ("an explicit specialization must be preceded by %<template <>%>");
13199 invalid_explicit_specialization_p = true;
13200 /* Take the same action that would have been taken by
13201 cp_parser_explicit_specialization. */
13202 ++parser->num_template_parameter_lists;
13203 begin_specialization ();
13205 /* There must be no "return" statements between this point and the
13206 end of this function; set "type "to the correct return value and
13207 use "goto done;" to return. */
13208 /* Make sure that the right number of template parameters were
13209 present. */
13210 if (!cp_parser_check_template_parameters (parser, num_templates))
13212 /* If something went wrong, there is no point in even trying to
13213 process the class-definition. */
13214 type = NULL_TREE;
13215 goto done;
13218 /* Look up the type. */
13219 if (template_id_p)
13221 type = TREE_TYPE (id);
13222 maybe_process_partial_specialization (type);
13223 if (nested_name_specifier)
13224 pushed_scope = push_scope (nested_name_specifier);
13226 else if (nested_name_specifier)
13228 tree class_type;
13230 /* Given:
13232 template <typename T> struct S { struct T };
13233 template <typename T> struct S<T>::T { };
13235 we will get a TYPENAME_TYPE when processing the definition of
13236 `S::T'. We need to resolve it to the actual type before we
13237 try to define it. */
13238 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13240 class_type = resolve_typename_type (TREE_TYPE (type),
13241 /*only_current_p=*/false);
13242 if (class_type != error_mark_node)
13243 type = TYPE_NAME (class_type);
13244 else
13246 cp_parser_error (parser, "could not resolve typename type");
13247 type = error_mark_node;
13251 maybe_process_partial_specialization (TREE_TYPE (type));
13252 class_type = current_class_type;
13253 /* Enter the scope indicated by the nested-name-specifier. */
13254 pushed_scope = push_scope (nested_name_specifier);
13255 /* Get the canonical version of this type. */
13256 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13257 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13258 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13260 type = push_template_decl (type);
13261 if (type == error_mark_node)
13263 type = NULL_TREE;
13264 goto done;
13268 type = TREE_TYPE (type);
13269 *nested_name_specifier_p = true;
13271 else /* The name is not a nested name. */
13273 /* If the class was unnamed, create a dummy name. */
13274 if (!id)
13275 id = make_anon_name ();
13276 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13277 parser->num_template_parameter_lists);
13280 /* Indicate whether this class was declared as a `class' or as a
13281 `struct'. */
13282 if (TREE_CODE (type) == RECORD_TYPE)
13283 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13284 cp_parser_check_class_key (class_key, type);
13286 /* If this type was already complete, and we see another definition,
13287 that's an error. */
13288 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13290 error ("redefinition of %q#T", type);
13291 error ("previous definition of %q+#T", type);
13292 type = NULL_TREE;
13293 goto done;
13296 /* We will have entered the scope containing the class; the names of
13297 base classes should be looked up in that context. For example:
13299 struct A { struct B {}; struct C; };
13300 struct A::C : B {};
13302 is valid. */
13303 bases = NULL_TREE;
13305 /* Get the list of base-classes, if there is one. */
13306 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13307 bases = cp_parser_base_clause (parser);
13309 /* Process the base classes. */
13310 xref_basetypes (type, bases);
13312 done:
13313 /* Leave the scope given by the nested-name-specifier. We will
13314 enter the class scope itself while processing the members. */
13315 if (pushed_scope)
13316 pop_scope (pushed_scope);
13318 if (invalid_explicit_specialization_p)
13320 end_specialization ();
13321 --parser->num_template_parameter_lists;
13323 *attributes_p = attributes;
13324 return type;
13327 /* Parse a class-key.
13329 class-key:
13330 class
13331 struct
13332 union
13334 Returns the kind of class-key specified, or none_type to indicate
13335 error. */
13337 static enum tag_types
13338 cp_parser_class_key (cp_parser* parser)
13340 cp_token *token;
13341 enum tag_types tag_type;
13343 /* Look for the class-key. */
13344 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13345 if (!token)
13346 return none_type;
13348 /* Check to see if the TOKEN is a class-key. */
13349 tag_type = cp_parser_token_is_class_key (token);
13350 if (!tag_type)
13351 cp_parser_error (parser, "expected class-key");
13352 return tag_type;
13355 /* Parse an (optional) member-specification.
13357 member-specification:
13358 member-declaration member-specification [opt]
13359 access-specifier : member-specification [opt] */
13361 static void
13362 cp_parser_member_specification_opt (cp_parser* parser)
13364 while (true)
13366 cp_token *token;
13367 enum rid keyword;
13369 /* Peek at the next token. */
13370 token = cp_lexer_peek_token (parser->lexer);
13371 /* If it's a `}', or EOF then we've seen all the members. */
13372 if (token->type == CPP_CLOSE_BRACE
13373 || token->type == CPP_EOF
13374 || token->type == CPP_PRAGMA_EOL)
13375 break;
13377 /* See if this token is a keyword. */
13378 keyword = token->keyword;
13379 switch (keyword)
13381 case RID_PUBLIC:
13382 case RID_PROTECTED:
13383 case RID_PRIVATE:
13384 /* Consume the access-specifier. */
13385 cp_lexer_consume_token (parser->lexer);
13386 /* Remember which access-specifier is active. */
13387 current_access_specifier = token->value;
13388 /* Look for the `:'. */
13389 cp_parser_require (parser, CPP_COLON, "`:'");
13390 break;
13392 default:
13393 /* Accept #pragmas at class scope. */
13394 if (token->type == CPP_PRAGMA)
13396 cp_parser_pragma (parser, pragma_external);
13397 break;
13400 /* Otherwise, the next construction must be a
13401 member-declaration. */
13402 cp_parser_member_declaration (parser);
13407 /* Parse a member-declaration.
13409 member-declaration:
13410 decl-specifier-seq [opt] member-declarator-list [opt] ;
13411 function-definition ; [opt]
13412 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13413 using-declaration
13414 template-declaration
13416 member-declarator-list:
13417 member-declarator
13418 member-declarator-list , member-declarator
13420 member-declarator:
13421 declarator pure-specifier [opt]
13422 declarator constant-initializer [opt]
13423 identifier [opt] : constant-expression
13425 GNU Extensions:
13427 member-declaration:
13428 __extension__ member-declaration
13430 member-declarator:
13431 declarator attributes [opt] pure-specifier [opt]
13432 declarator attributes [opt] constant-initializer [opt]
13433 identifier [opt] attributes [opt] : constant-expression */
13435 static void
13436 cp_parser_member_declaration (cp_parser* parser)
13438 cp_decl_specifier_seq decl_specifiers;
13439 tree prefix_attributes;
13440 tree decl;
13441 int declares_class_or_enum;
13442 bool friend_p;
13443 cp_token *token;
13444 int saved_pedantic;
13446 /* Check for the `__extension__' keyword. */
13447 if (cp_parser_extension_opt (parser, &saved_pedantic))
13449 /* Recurse. */
13450 cp_parser_member_declaration (parser);
13451 /* Restore the old value of the PEDANTIC flag. */
13452 pedantic = saved_pedantic;
13454 return;
13457 /* Check for a template-declaration. */
13458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13460 /* An explicit specialization here is an error condition, and we
13461 expect the specialization handler to detect and report this. */
13462 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13464 cp_parser_explicit_specialization (parser);
13465 else
13466 cp_parser_template_declaration (parser, /*member_p=*/true);
13468 return;
13471 /* Check for a using-declaration. */
13472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13474 /* Parse the using-declaration. */
13475 cp_parser_using_declaration (parser);
13477 return;
13480 /* Check for @defs. */
13481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13483 tree ivar, member;
13484 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13485 ivar = ivar_chains;
13486 while (ivar)
13488 member = ivar;
13489 ivar = TREE_CHAIN (member);
13490 TREE_CHAIN (member) = NULL_TREE;
13491 finish_member_declaration (member);
13493 return;
13496 /* Parse the decl-specifier-seq. */
13497 cp_parser_decl_specifier_seq (parser,
13498 CP_PARSER_FLAGS_OPTIONAL,
13499 &decl_specifiers,
13500 &declares_class_or_enum);
13501 prefix_attributes = decl_specifiers.attributes;
13502 decl_specifiers.attributes = NULL_TREE;
13503 /* Check for an invalid type-name. */
13504 if (!decl_specifiers.type
13505 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13506 return;
13507 /* If there is no declarator, then the decl-specifier-seq should
13508 specify a type. */
13509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13511 /* If there was no decl-specifier-seq, and the next token is a
13512 `;', then we have something like:
13514 struct S { ; };
13516 [class.mem]
13518 Each member-declaration shall declare at least one member
13519 name of the class. */
13520 if (!decl_specifiers.any_specifiers_p)
13522 cp_token *token = cp_lexer_peek_token (parser->lexer);
13523 if (pedantic && !token->in_system_header)
13524 pedwarn ("%Hextra %<;%>", &token->location);
13526 else
13528 tree type;
13530 /* See if this declaration is a friend. */
13531 friend_p = cp_parser_friend_p (&decl_specifiers);
13532 /* If there were decl-specifiers, check to see if there was
13533 a class-declaration. */
13534 type = check_tag_decl (&decl_specifiers);
13535 /* Nested classes have already been added to the class, but
13536 a `friend' needs to be explicitly registered. */
13537 if (friend_p)
13539 /* If the `friend' keyword was present, the friend must
13540 be introduced with a class-key. */
13541 if (!declares_class_or_enum)
13542 error ("a class-key must be used when declaring a friend");
13543 /* In this case:
13545 template <typename T> struct A {
13546 friend struct A<T>::B;
13549 A<T>::B will be represented by a TYPENAME_TYPE, and
13550 therefore not recognized by check_tag_decl. */
13551 if (!type
13552 && decl_specifiers.type
13553 && TYPE_P (decl_specifiers.type))
13554 type = decl_specifiers.type;
13555 if (!type || !TYPE_P (type))
13556 error ("friend declaration does not name a class or "
13557 "function");
13558 else
13559 make_friend_class (current_class_type, type,
13560 /*complain=*/true);
13562 /* If there is no TYPE, an error message will already have
13563 been issued. */
13564 else if (!type || type == error_mark_node)
13566 /* An anonymous aggregate has to be handled specially; such
13567 a declaration really declares a data member (with a
13568 particular type), as opposed to a nested class. */
13569 else if (ANON_AGGR_TYPE_P (type))
13571 /* Remove constructors and such from TYPE, now that we
13572 know it is an anonymous aggregate. */
13573 fixup_anonymous_aggr (type);
13574 /* And make the corresponding data member. */
13575 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13576 /* Add it to the class. */
13577 finish_member_declaration (decl);
13579 else
13580 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13583 else
13585 /* See if these declarations will be friends. */
13586 friend_p = cp_parser_friend_p (&decl_specifiers);
13588 /* Keep going until we hit the `;' at the end of the
13589 declaration. */
13590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13592 tree attributes = NULL_TREE;
13593 tree first_attribute;
13595 /* Peek at the next token. */
13596 token = cp_lexer_peek_token (parser->lexer);
13598 /* Check for a bitfield declaration. */
13599 if (token->type == CPP_COLON
13600 || (token->type == CPP_NAME
13601 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13602 == CPP_COLON))
13604 tree identifier;
13605 tree width;
13607 /* Get the name of the bitfield. Note that we cannot just
13608 check TOKEN here because it may have been invalidated by
13609 the call to cp_lexer_peek_nth_token above. */
13610 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13611 identifier = cp_parser_identifier (parser);
13612 else
13613 identifier = NULL_TREE;
13615 /* Consume the `:' token. */
13616 cp_lexer_consume_token (parser->lexer);
13617 /* Get the width of the bitfield. */
13618 width
13619 = cp_parser_constant_expression (parser,
13620 /*allow_non_constant=*/false,
13621 NULL);
13623 /* Look for attributes that apply to the bitfield. */
13624 attributes = cp_parser_attributes_opt (parser);
13625 /* Remember which attributes are prefix attributes and
13626 which are not. */
13627 first_attribute = attributes;
13628 /* Combine the attributes. */
13629 attributes = chainon (prefix_attributes, attributes);
13631 /* Create the bitfield declaration. */
13632 decl = grokbitfield (identifier
13633 ? make_id_declarator (NULL_TREE,
13634 identifier,
13635 sfk_none)
13636 : NULL,
13637 &decl_specifiers,
13638 width);
13639 /* Apply the attributes. */
13640 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13642 else
13644 cp_declarator *declarator;
13645 tree initializer;
13646 tree asm_specification;
13647 int ctor_dtor_or_conv_p;
13649 /* Parse the declarator. */
13650 declarator
13651 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13652 &ctor_dtor_or_conv_p,
13653 /*parenthesized_p=*/NULL,
13654 /*member_p=*/true);
13656 /* If something went wrong parsing the declarator, make sure
13657 that we at least consume some tokens. */
13658 if (declarator == cp_error_declarator)
13660 /* Skip to the end of the statement. */
13661 cp_parser_skip_to_end_of_statement (parser);
13662 /* If the next token is not a semicolon, that is
13663 probably because we just skipped over the body of
13664 a function. So, we consume a semicolon if
13665 present, but do not issue an error message if it
13666 is not present. */
13667 if (cp_lexer_next_token_is (parser->lexer,
13668 CPP_SEMICOLON))
13669 cp_lexer_consume_token (parser->lexer);
13670 return;
13673 if (declares_class_or_enum & 2)
13674 cp_parser_check_for_definition_in_return_type
13675 (declarator, decl_specifiers.type);
13677 /* Look for an asm-specification. */
13678 asm_specification = cp_parser_asm_specification_opt (parser);
13679 /* Look for attributes that apply to the declaration. */
13680 attributes = cp_parser_attributes_opt (parser);
13681 /* Remember which attributes are prefix attributes and
13682 which are not. */
13683 first_attribute = attributes;
13684 /* Combine the attributes. */
13685 attributes = chainon (prefix_attributes, attributes);
13687 /* If it's an `=', then we have a constant-initializer or a
13688 pure-specifier. It is not correct to parse the
13689 initializer before registering the member declaration
13690 since the member declaration should be in scope while
13691 its initializer is processed. However, the rest of the
13692 front end does not yet provide an interface that allows
13693 us to handle this correctly. */
13694 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13696 /* In [class.mem]:
13698 A pure-specifier shall be used only in the declaration of
13699 a virtual function.
13701 A member-declarator can contain a constant-initializer
13702 only if it declares a static member of integral or
13703 enumeration type.
13705 Therefore, if the DECLARATOR is for a function, we look
13706 for a pure-specifier; otherwise, we look for a
13707 constant-initializer. When we call `grokfield', it will
13708 perform more stringent semantics checks. */
13709 if (declarator->kind == cdk_function)
13710 initializer = cp_parser_pure_specifier (parser);
13711 else
13712 /* Parse the initializer. */
13713 initializer = cp_parser_constant_initializer (parser);
13715 /* Otherwise, there is no initializer. */
13716 else
13717 initializer = NULL_TREE;
13719 /* See if we are probably looking at a function
13720 definition. We are certainly not looking at a
13721 member-declarator. Calling `grokfield' has
13722 side-effects, so we must not do it unless we are sure
13723 that we are looking at a member-declarator. */
13724 if (cp_parser_token_starts_function_definition_p
13725 (cp_lexer_peek_token (parser->lexer)))
13727 /* The grammar does not allow a pure-specifier to be
13728 used when a member function is defined. (It is
13729 possible that this fact is an oversight in the
13730 standard, since a pure function may be defined
13731 outside of the class-specifier. */
13732 if (initializer)
13733 error ("pure-specifier on function-definition");
13734 decl = cp_parser_save_member_function_body (parser,
13735 &decl_specifiers,
13736 declarator,
13737 attributes);
13738 /* If the member was not a friend, declare it here. */
13739 if (!friend_p)
13740 finish_member_declaration (decl);
13741 /* Peek at the next token. */
13742 token = cp_lexer_peek_token (parser->lexer);
13743 /* If the next token is a semicolon, consume it. */
13744 if (token->type == CPP_SEMICOLON)
13745 cp_lexer_consume_token (parser->lexer);
13746 return;
13748 else
13749 /* Create the declaration. */
13750 decl = grokfield (declarator, &decl_specifiers,
13751 initializer, /*init_const_expr_p=*/true,
13752 asm_specification,
13753 attributes);
13756 /* Reset PREFIX_ATTRIBUTES. */
13757 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13758 attributes = TREE_CHAIN (attributes);
13759 if (attributes)
13760 TREE_CHAIN (attributes) = NULL_TREE;
13762 /* If there is any qualification still in effect, clear it
13763 now; we will be starting fresh with the next declarator. */
13764 parser->scope = NULL_TREE;
13765 parser->qualifying_scope = NULL_TREE;
13766 parser->object_scope = NULL_TREE;
13767 /* If it's a `,', then there are more declarators. */
13768 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13769 cp_lexer_consume_token (parser->lexer);
13770 /* If the next token isn't a `;', then we have a parse error. */
13771 else if (cp_lexer_next_token_is_not (parser->lexer,
13772 CPP_SEMICOLON))
13774 cp_parser_error (parser, "expected %<;%>");
13775 /* Skip tokens until we find a `;'. */
13776 cp_parser_skip_to_end_of_statement (parser);
13778 break;
13781 if (decl)
13783 /* Add DECL to the list of members. */
13784 if (!friend_p)
13785 finish_member_declaration (decl);
13787 if (TREE_CODE (decl) == FUNCTION_DECL)
13788 cp_parser_save_default_args (parser, decl);
13793 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13796 /* Parse a pure-specifier.
13798 pure-specifier:
13801 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13802 Otherwise, ERROR_MARK_NODE is returned. */
13804 static tree
13805 cp_parser_pure_specifier (cp_parser* parser)
13807 cp_token *token;
13809 /* Look for the `=' token. */
13810 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13811 return error_mark_node;
13812 /* Look for the `0' token. */
13813 token = cp_lexer_consume_token (parser->lexer);
13814 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13815 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13816 return integer_zero_node;
13818 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13819 cp_parser_skip_to_end_of_statement (parser);
13820 return error_mark_node;
13823 /* Parse a constant-initializer.
13825 constant-initializer:
13826 = constant-expression
13828 Returns a representation of the constant-expression. */
13830 static tree
13831 cp_parser_constant_initializer (cp_parser* parser)
13833 /* Look for the `=' token. */
13834 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13835 return error_mark_node;
13837 /* It is invalid to write:
13839 struct S { static const int i = { 7 }; };
13842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13844 cp_parser_error (parser,
13845 "a brace-enclosed initializer is not allowed here");
13846 /* Consume the opening brace. */
13847 cp_lexer_consume_token (parser->lexer);
13848 /* Skip the initializer. */
13849 cp_parser_skip_to_closing_brace (parser);
13850 /* Look for the trailing `}'. */
13851 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13853 return error_mark_node;
13856 return cp_parser_constant_expression (parser,
13857 /*allow_non_constant=*/false,
13858 NULL);
13861 /* Derived classes [gram.class.derived] */
13863 /* Parse a base-clause.
13865 base-clause:
13866 : base-specifier-list
13868 base-specifier-list:
13869 base-specifier
13870 base-specifier-list , base-specifier
13872 Returns a TREE_LIST representing the base-classes, in the order in
13873 which they were declared. The representation of each node is as
13874 described by cp_parser_base_specifier.
13876 In the case that no bases are specified, this function will return
13877 NULL_TREE, not ERROR_MARK_NODE. */
13879 static tree
13880 cp_parser_base_clause (cp_parser* parser)
13882 tree bases = NULL_TREE;
13884 /* Look for the `:' that begins the list. */
13885 cp_parser_require (parser, CPP_COLON, "`:'");
13887 /* Scan the base-specifier-list. */
13888 while (true)
13890 cp_token *token;
13891 tree base;
13893 /* Look for the base-specifier. */
13894 base = cp_parser_base_specifier (parser);
13895 /* Add BASE to the front of the list. */
13896 if (base != error_mark_node)
13898 TREE_CHAIN (base) = bases;
13899 bases = base;
13901 /* Peek at the next token. */
13902 token = cp_lexer_peek_token (parser->lexer);
13903 /* If it's not a comma, then the list is complete. */
13904 if (token->type != CPP_COMMA)
13905 break;
13906 /* Consume the `,'. */
13907 cp_lexer_consume_token (parser->lexer);
13910 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13911 base class had a qualified name. However, the next name that
13912 appears is certainly not qualified. */
13913 parser->scope = NULL_TREE;
13914 parser->qualifying_scope = NULL_TREE;
13915 parser->object_scope = NULL_TREE;
13917 return nreverse (bases);
13920 /* Parse a base-specifier.
13922 base-specifier:
13923 :: [opt] nested-name-specifier [opt] class-name
13924 virtual access-specifier [opt] :: [opt] nested-name-specifier
13925 [opt] class-name
13926 access-specifier virtual [opt] :: [opt] nested-name-specifier
13927 [opt] class-name
13929 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13930 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13931 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13932 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13934 static tree
13935 cp_parser_base_specifier (cp_parser* parser)
13937 cp_token *token;
13938 bool done = false;
13939 bool virtual_p = false;
13940 bool duplicate_virtual_error_issued_p = false;
13941 bool duplicate_access_error_issued_p = false;
13942 bool class_scope_p, template_p;
13943 tree access = access_default_node;
13944 tree type;
13946 /* Process the optional `virtual' and `access-specifier'. */
13947 while (!done)
13949 /* Peek at the next token. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* Process `virtual'. */
13952 switch (token->keyword)
13954 case RID_VIRTUAL:
13955 /* If `virtual' appears more than once, issue an error. */
13956 if (virtual_p && !duplicate_virtual_error_issued_p)
13958 cp_parser_error (parser,
13959 "%<virtual%> specified more than once in base-specified");
13960 duplicate_virtual_error_issued_p = true;
13963 virtual_p = true;
13965 /* Consume the `virtual' token. */
13966 cp_lexer_consume_token (parser->lexer);
13968 break;
13970 case RID_PUBLIC:
13971 case RID_PROTECTED:
13972 case RID_PRIVATE:
13973 /* If more than one access specifier appears, issue an
13974 error. */
13975 if (access != access_default_node
13976 && !duplicate_access_error_issued_p)
13978 cp_parser_error (parser,
13979 "more than one access specifier in base-specified");
13980 duplicate_access_error_issued_p = true;
13983 access = ridpointers[(int) token->keyword];
13985 /* Consume the access-specifier. */
13986 cp_lexer_consume_token (parser->lexer);
13988 break;
13990 default:
13991 done = true;
13992 break;
13995 /* It is not uncommon to see programs mechanically, erroneously, use
13996 the 'typename' keyword to denote (dependent) qualified types
13997 as base classes. */
13998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14000 if (!processing_template_decl)
14001 error ("keyword %<typename%> not allowed outside of templates");
14002 else
14003 error ("keyword %<typename%> not allowed in this context "
14004 "(the base class is implicitly a type)");
14005 cp_lexer_consume_token (parser->lexer);
14008 /* Look for the optional `::' operator. */
14009 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14010 /* Look for the nested-name-specifier. The simplest way to
14011 implement:
14013 [temp.res]
14015 The keyword `typename' is not permitted in a base-specifier or
14016 mem-initializer; in these contexts a qualified name that
14017 depends on a template-parameter is implicitly assumed to be a
14018 type name.
14020 is to pretend that we have seen the `typename' keyword at this
14021 point. */
14022 cp_parser_nested_name_specifier_opt (parser,
14023 /*typename_keyword_p=*/true,
14024 /*check_dependency_p=*/true,
14025 typename_type,
14026 /*is_declaration=*/true);
14027 /* If the base class is given by a qualified name, assume that names
14028 we see are type names or templates, as appropriate. */
14029 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14030 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14032 /* Finally, look for the class-name. */
14033 type = cp_parser_class_name (parser,
14034 class_scope_p,
14035 template_p,
14036 typename_type,
14037 /*check_dependency_p=*/true,
14038 /*class_head_p=*/false,
14039 /*is_declaration=*/true);
14041 if (type == error_mark_node)
14042 return error_mark_node;
14044 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14047 /* Exception handling [gram.exception] */
14049 /* Parse an (optional) exception-specification.
14051 exception-specification:
14052 throw ( type-id-list [opt] )
14054 Returns a TREE_LIST representing the exception-specification. The
14055 TREE_VALUE of each node is a type. */
14057 static tree
14058 cp_parser_exception_specification_opt (cp_parser* parser)
14060 cp_token *token;
14061 tree type_id_list;
14063 /* Peek at the next token. */
14064 token = cp_lexer_peek_token (parser->lexer);
14065 /* If it's not `throw', then there's no exception-specification. */
14066 if (!cp_parser_is_keyword (token, RID_THROW))
14067 return NULL_TREE;
14069 /* Consume the `throw'. */
14070 cp_lexer_consume_token (parser->lexer);
14072 /* Look for the `('. */
14073 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14075 /* Peek at the next token. */
14076 token = cp_lexer_peek_token (parser->lexer);
14077 /* If it's not a `)', then there is a type-id-list. */
14078 if (token->type != CPP_CLOSE_PAREN)
14080 const char *saved_message;
14082 /* Types may not be defined in an exception-specification. */
14083 saved_message = parser->type_definition_forbidden_message;
14084 parser->type_definition_forbidden_message
14085 = "types may not be defined in an exception-specification";
14086 /* Parse the type-id-list. */
14087 type_id_list = cp_parser_type_id_list (parser);
14088 /* Restore the saved message. */
14089 parser->type_definition_forbidden_message = saved_message;
14091 else
14092 type_id_list = empty_except_spec;
14094 /* Look for the `)'. */
14095 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14097 return type_id_list;
14100 /* Parse an (optional) type-id-list.
14102 type-id-list:
14103 type-id
14104 type-id-list , type-id
14106 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14107 in the order that the types were presented. */
14109 static tree
14110 cp_parser_type_id_list (cp_parser* parser)
14112 tree types = NULL_TREE;
14114 while (true)
14116 cp_token *token;
14117 tree type;
14119 /* Get the next type-id. */
14120 type = cp_parser_type_id (parser);
14121 /* Add it to the list. */
14122 types = add_exception_specifier (types, type, /*complain=*/1);
14123 /* Peek at the next token. */
14124 token = cp_lexer_peek_token (parser->lexer);
14125 /* If it is not a `,', we are done. */
14126 if (token->type != CPP_COMMA)
14127 break;
14128 /* Consume the `,'. */
14129 cp_lexer_consume_token (parser->lexer);
14132 return nreverse (types);
14135 /* Parse a try-block.
14137 try-block:
14138 try compound-statement handler-seq */
14140 static tree
14141 cp_parser_try_block (cp_parser* parser)
14143 tree try_block;
14145 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14146 try_block = begin_try_block ();
14147 cp_parser_compound_statement (parser, NULL, true);
14148 finish_try_block (try_block);
14149 cp_parser_handler_seq (parser);
14150 finish_handler_sequence (try_block);
14152 return try_block;
14155 /* Parse a function-try-block.
14157 function-try-block:
14158 try ctor-initializer [opt] function-body handler-seq */
14160 static bool
14161 cp_parser_function_try_block (cp_parser* parser)
14163 tree try_block;
14164 bool ctor_initializer_p;
14166 /* Look for the `try' keyword. */
14167 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14168 return false;
14169 /* Let the rest of the front-end know where we are. */
14170 try_block = begin_function_try_block ();
14171 /* Parse the function-body. */
14172 ctor_initializer_p
14173 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14174 /* We're done with the `try' part. */
14175 finish_function_try_block (try_block);
14176 /* Parse the handlers. */
14177 cp_parser_handler_seq (parser);
14178 /* We're done with the handlers. */
14179 finish_function_handler_sequence (try_block);
14181 return ctor_initializer_p;
14184 /* Parse a handler-seq.
14186 handler-seq:
14187 handler handler-seq [opt] */
14189 static void
14190 cp_parser_handler_seq (cp_parser* parser)
14192 while (true)
14194 cp_token *token;
14196 /* Parse the handler. */
14197 cp_parser_handler (parser);
14198 /* Peek at the next token. */
14199 token = cp_lexer_peek_token (parser->lexer);
14200 /* If it's not `catch' then there are no more handlers. */
14201 if (!cp_parser_is_keyword (token, RID_CATCH))
14202 break;
14206 /* Parse a handler.
14208 handler:
14209 catch ( exception-declaration ) compound-statement */
14211 static void
14212 cp_parser_handler (cp_parser* parser)
14214 tree handler;
14215 tree declaration;
14217 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14218 handler = begin_handler ();
14219 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14220 declaration = cp_parser_exception_declaration (parser);
14221 finish_handler_parms (declaration, handler);
14222 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14223 cp_parser_compound_statement (parser, NULL, false);
14224 finish_handler (handler);
14227 /* Parse an exception-declaration.
14229 exception-declaration:
14230 type-specifier-seq declarator
14231 type-specifier-seq abstract-declarator
14232 type-specifier-seq
14235 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14236 ellipsis variant is used. */
14238 static tree
14239 cp_parser_exception_declaration (cp_parser* parser)
14241 tree decl;
14242 cp_decl_specifier_seq type_specifiers;
14243 cp_declarator *declarator;
14244 const char *saved_message;
14246 /* If it's an ellipsis, it's easy to handle. */
14247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14249 /* Consume the `...' token. */
14250 cp_lexer_consume_token (parser->lexer);
14251 return NULL_TREE;
14254 /* Types may not be defined in exception-declarations. */
14255 saved_message = parser->type_definition_forbidden_message;
14256 parser->type_definition_forbidden_message
14257 = "types may not be defined in exception-declarations";
14259 /* Parse the type-specifier-seq. */
14260 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14261 &type_specifiers);
14262 /* If it's a `)', then there is no declarator. */
14263 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14264 declarator = NULL;
14265 else
14266 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14267 /*ctor_dtor_or_conv_p=*/NULL,
14268 /*parenthesized_p=*/NULL,
14269 /*member_p=*/false);
14271 /* Restore the saved message. */
14272 parser->type_definition_forbidden_message = saved_message;
14274 if (type_specifiers.any_specifiers_p)
14276 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14277 if (decl == NULL_TREE)
14278 error ("invalid catch parameter");
14280 else
14281 decl = NULL_TREE;
14283 return decl;
14286 /* Parse a throw-expression.
14288 throw-expression:
14289 throw assignment-expression [opt]
14291 Returns a THROW_EXPR representing the throw-expression. */
14293 static tree
14294 cp_parser_throw_expression (cp_parser* parser)
14296 tree expression;
14297 cp_token* token;
14299 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14300 token = cp_lexer_peek_token (parser->lexer);
14301 /* Figure out whether or not there is an assignment-expression
14302 following the "throw" keyword. */
14303 if (token->type == CPP_COMMA
14304 || token->type == CPP_SEMICOLON
14305 || token->type == CPP_CLOSE_PAREN
14306 || token->type == CPP_CLOSE_SQUARE
14307 || token->type == CPP_CLOSE_BRACE
14308 || token->type == CPP_COLON)
14309 expression = NULL_TREE;
14310 else
14311 expression = cp_parser_assignment_expression (parser,
14312 /*cast_p=*/false);
14314 return build_throw (expression);
14317 /* GNU Extensions */
14319 /* Parse an (optional) asm-specification.
14321 asm-specification:
14322 asm ( string-literal )
14324 If the asm-specification is present, returns a STRING_CST
14325 corresponding to the string-literal. Otherwise, returns
14326 NULL_TREE. */
14328 static tree
14329 cp_parser_asm_specification_opt (cp_parser* parser)
14331 cp_token *token;
14332 tree asm_specification;
14334 /* Peek at the next token. */
14335 token = cp_lexer_peek_token (parser->lexer);
14336 /* If the next token isn't the `asm' keyword, then there's no
14337 asm-specification. */
14338 if (!cp_parser_is_keyword (token, RID_ASM))
14339 return NULL_TREE;
14341 /* Consume the `asm' token. */
14342 cp_lexer_consume_token (parser->lexer);
14343 /* Look for the `('. */
14344 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14346 /* Look for the string-literal. */
14347 asm_specification = cp_parser_string_literal (parser, false, false);
14349 /* Look for the `)'. */
14350 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14352 return asm_specification;
14355 /* Parse an asm-operand-list.
14357 asm-operand-list:
14358 asm-operand
14359 asm-operand-list , asm-operand
14361 asm-operand:
14362 string-literal ( expression )
14363 [ string-literal ] string-literal ( expression )
14365 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14366 each node is the expression. The TREE_PURPOSE is itself a
14367 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14368 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14369 is a STRING_CST for the string literal before the parenthesis. */
14371 static tree
14372 cp_parser_asm_operand_list (cp_parser* parser)
14374 tree asm_operands = NULL_TREE;
14376 while (true)
14378 tree string_literal;
14379 tree expression;
14380 tree name;
14382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14384 /* Consume the `[' token. */
14385 cp_lexer_consume_token (parser->lexer);
14386 /* Read the operand name. */
14387 name = cp_parser_identifier (parser);
14388 if (name != error_mark_node)
14389 name = build_string (IDENTIFIER_LENGTH (name),
14390 IDENTIFIER_POINTER (name));
14391 /* Look for the closing `]'. */
14392 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14394 else
14395 name = NULL_TREE;
14396 /* Look for the string-literal. */
14397 string_literal = cp_parser_string_literal (parser, false, false);
14399 /* Look for the `('. */
14400 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14401 /* Parse the expression. */
14402 expression = cp_parser_expression (parser, /*cast_p=*/false);
14403 /* Look for the `)'. */
14404 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14406 /* Add this operand to the list. */
14407 asm_operands = tree_cons (build_tree_list (name, string_literal),
14408 expression,
14409 asm_operands);
14410 /* If the next token is not a `,', there are no more
14411 operands. */
14412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14413 break;
14414 /* Consume the `,'. */
14415 cp_lexer_consume_token (parser->lexer);
14418 return nreverse (asm_operands);
14421 /* Parse an asm-clobber-list.
14423 asm-clobber-list:
14424 string-literal
14425 asm-clobber-list , string-literal
14427 Returns a TREE_LIST, indicating the clobbers in the order that they
14428 appeared. The TREE_VALUE of each node is a STRING_CST. */
14430 static tree
14431 cp_parser_asm_clobber_list (cp_parser* parser)
14433 tree clobbers = NULL_TREE;
14435 while (true)
14437 tree string_literal;
14439 /* Look for the string literal. */
14440 string_literal = cp_parser_string_literal (parser, false, false);
14441 /* Add it to the list. */
14442 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14443 /* If the next token is not a `,', then the list is
14444 complete. */
14445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14446 break;
14447 /* Consume the `,' token. */
14448 cp_lexer_consume_token (parser->lexer);
14451 return clobbers;
14454 /* Parse an (optional) series of attributes.
14456 attributes:
14457 attributes attribute
14459 attribute:
14460 __attribute__ (( attribute-list [opt] ))
14462 The return value is as for cp_parser_attribute_list. */
14464 static tree
14465 cp_parser_attributes_opt (cp_parser* parser)
14467 tree attributes = NULL_TREE;
14469 while (true)
14471 cp_token *token;
14472 tree attribute_list;
14474 /* Peek at the next token. */
14475 token = cp_lexer_peek_token (parser->lexer);
14476 /* If it's not `__attribute__', then we're done. */
14477 if (token->keyword != RID_ATTRIBUTE)
14478 break;
14480 /* Consume the `__attribute__' keyword. */
14481 cp_lexer_consume_token (parser->lexer);
14482 /* Look for the two `(' tokens. */
14483 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14484 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14486 /* Peek at the next token. */
14487 token = cp_lexer_peek_token (parser->lexer);
14488 if (token->type != CPP_CLOSE_PAREN)
14489 /* Parse the attribute-list. */
14490 attribute_list = cp_parser_attribute_list (parser);
14491 else
14492 /* If the next token is a `)', then there is no attribute
14493 list. */
14494 attribute_list = NULL;
14496 /* Look for the two `)' tokens. */
14497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14498 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14500 /* Add these new attributes to the list. */
14501 attributes = chainon (attributes, attribute_list);
14504 return attributes;
14507 /* Parse an attribute-list.
14509 attribute-list:
14510 attribute
14511 attribute-list , attribute
14513 attribute:
14514 identifier
14515 identifier ( identifier )
14516 identifier ( identifier , expression-list )
14517 identifier ( expression-list )
14519 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14520 to an attribute. The TREE_PURPOSE of each node is the identifier
14521 indicating which attribute is in use. The TREE_VALUE represents
14522 the arguments, if any. */
14524 static tree
14525 cp_parser_attribute_list (cp_parser* parser)
14527 tree attribute_list = NULL_TREE;
14528 bool save_translate_strings_p = parser->translate_strings_p;
14530 parser->translate_strings_p = false;
14531 while (true)
14533 cp_token *token;
14534 tree identifier;
14535 tree attribute;
14537 /* Look for the identifier. We also allow keywords here; for
14538 example `__attribute__ ((const))' is legal. */
14539 token = cp_lexer_peek_token (parser->lexer);
14540 if (token->type == CPP_NAME
14541 || token->type == CPP_KEYWORD)
14543 /* Consume the token. */
14544 token = cp_lexer_consume_token (parser->lexer);
14546 /* Save away the identifier that indicates which attribute
14547 this is. */
14548 identifier = token->value;
14549 attribute = build_tree_list (identifier, NULL_TREE);
14551 /* Peek at the next token. */
14552 token = cp_lexer_peek_token (parser->lexer);
14553 /* If it's an `(', then parse the attribute arguments. */
14554 if (token->type == CPP_OPEN_PAREN)
14556 tree arguments;
14558 arguments = (cp_parser_parenthesized_expression_list
14559 (parser, true, /*cast_p=*/false,
14560 /*non_constant_p=*/NULL));
14561 /* Save the identifier and arguments away. */
14562 TREE_VALUE (attribute) = arguments;
14565 /* Add this attribute to the list. */
14566 TREE_CHAIN (attribute) = attribute_list;
14567 attribute_list = attribute;
14569 token = cp_lexer_peek_token (parser->lexer);
14571 /* Now, look for more attributes. If the next token isn't a
14572 `,', we're done. */
14573 if (token->type != CPP_COMMA)
14574 break;
14576 /* Consume the comma and keep going. */
14577 cp_lexer_consume_token (parser->lexer);
14579 parser->translate_strings_p = save_translate_strings_p;
14581 /* We built up the list in reverse order. */
14582 return nreverse (attribute_list);
14585 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14586 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14587 current value of the PEDANTIC flag, regardless of whether or not
14588 the `__extension__' keyword is present. The caller is responsible
14589 for restoring the value of the PEDANTIC flag. */
14591 static bool
14592 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14594 /* Save the old value of the PEDANTIC flag. */
14595 *saved_pedantic = pedantic;
14597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14599 /* Consume the `__extension__' token. */
14600 cp_lexer_consume_token (parser->lexer);
14601 /* We're not being pedantic while the `__extension__' keyword is
14602 in effect. */
14603 pedantic = 0;
14605 return true;
14608 return false;
14611 /* Parse a label declaration.
14613 label-declaration:
14614 __label__ label-declarator-seq ;
14616 label-declarator-seq:
14617 identifier , label-declarator-seq
14618 identifier */
14620 static void
14621 cp_parser_label_declaration (cp_parser* parser)
14623 /* Look for the `__label__' keyword. */
14624 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14626 while (true)
14628 tree identifier;
14630 /* Look for an identifier. */
14631 identifier = cp_parser_identifier (parser);
14632 /* If we failed, stop. */
14633 if (identifier == error_mark_node)
14634 break;
14635 /* Declare it as a label. */
14636 finish_label_decl (identifier);
14637 /* If the next token is a `;', stop. */
14638 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14639 break;
14640 /* Look for the `,' separating the label declarations. */
14641 cp_parser_require (parser, CPP_COMMA, "`,'");
14644 /* Look for the final `;'. */
14645 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14648 /* Support Functions */
14650 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14651 NAME should have one of the representations used for an
14652 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14653 is returned. If PARSER->SCOPE is a dependent type, then a
14654 SCOPE_REF is returned.
14656 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14657 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14658 was formed. Abstractly, such entities should not be passed to this
14659 function, because they do not need to be looked up, but it is
14660 simpler to check for this special case here, rather than at the
14661 call-sites.
14663 In cases not explicitly covered above, this function returns a
14664 DECL, OVERLOAD, or baselink representing the result of the lookup.
14665 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14666 is returned.
14668 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14669 (e.g., "struct") that was used. In that case bindings that do not
14670 refer to types are ignored.
14672 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14673 ignored.
14675 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14676 are ignored.
14678 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14679 types.
14681 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14682 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14683 NULL_TREE otherwise. */
14685 static tree
14686 cp_parser_lookup_name (cp_parser *parser, tree name,
14687 enum tag_types tag_type,
14688 bool is_template,
14689 bool is_namespace,
14690 bool check_dependency,
14691 tree *ambiguous_decls)
14693 int flags = 0;
14694 tree decl;
14695 tree object_type = parser->context->object_type;
14697 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14698 flags |= LOOKUP_COMPLAIN;
14700 /* Assume that the lookup will be unambiguous. */
14701 if (ambiguous_decls)
14702 *ambiguous_decls = NULL_TREE;
14704 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14705 no longer valid. Note that if we are parsing tentatively, and
14706 the parse fails, OBJECT_TYPE will be automatically restored. */
14707 parser->context->object_type = NULL_TREE;
14709 if (name == error_mark_node)
14710 return error_mark_node;
14712 /* A template-id has already been resolved; there is no lookup to
14713 do. */
14714 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14715 return name;
14716 if (BASELINK_P (name))
14718 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14719 == TEMPLATE_ID_EXPR);
14720 return name;
14723 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14724 it should already have been checked to make sure that the name
14725 used matches the type being destroyed. */
14726 if (TREE_CODE (name) == BIT_NOT_EXPR)
14728 tree type;
14730 /* Figure out to which type this destructor applies. */
14731 if (parser->scope)
14732 type = parser->scope;
14733 else if (object_type)
14734 type = object_type;
14735 else
14736 type = current_class_type;
14737 /* If that's not a class type, there is no destructor. */
14738 if (!type || !CLASS_TYPE_P (type))
14739 return error_mark_node;
14740 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14741 lazily_declare_fn (sfk_destructor, type);
14742 if (!CLASSTYPE_DESTRUCTORS (type))
14743 return error_mark_node;
14744 /* If it was a class type, return the destructor. */
14745 return CLASSTYPE_DESTRUCTORS (type);
14748 /* By this point, the NAME should be an ordinary identifier. If
14749 the id-expression was a qualified name, the qualifying scope is
14750 stored in PARSER->SCOPE at this point. */
14751 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14753 /* Perform the lookup. */
14754 if (parser->scope)
14756 bool dependent_p;
14758 if (parser->scope == error_mark_node)
14759 return error_mark_node;
14761 /* If the SCOPE is dependent, the lookup must be deferred until
14762 the template is instantiated -- unless we are explicitly
14763 looking up names in uninstantiated templates. Even then, we
14764 cannot look up the name if the scope is not a class type; it
14765 might, for example, be a template type parameter. */
14766 dependent_p = (TYPE_P (parser->scope)
14767 && !(parser->in_declarator_p
14768 && currently_open_class (parser->scope))
14769 && dependent_type_p (parser->scope));
14770 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14771 && dependent_p)
14773 if (tag_type)
14775 tree type;
14777 /* The resolution to Core Issue 180 says that `struct
14778 A::B' should be considered a type-name, even if `A'
14779 is dependent. */
14780 type = make_typename_type (parser->scope, name, tag_type,
14781 /*complain=*/tf_error);
14782 decl = TYPE_NAME (type);
14784 else if (is_template
14785 && (cp_parser_next_token_ends_template_argument_p (parser)
14786 || cp_lexer_next_token_is (parser->lexer,
14787 CPP_CLOSE_PAREN)))
14788 decl = make_unbound_class_template (parser->scope,
14789 name, NULL_TREE,
14790 /*complain=*/tf_error);
14791 else
14792 decl = build_qualified_name (/*type=*/NULL_TREE,
14793 parser->scope, name,
14794 is_template);
14796 else
14798 tree pushed_scope = NULL_TREE;
14800 /* If PARSER->SCOPE is a dependent type, then it must be a
14801 class type, and we must not be checking dependencies;
14802 otherwise, we would have processed this lookup above. So
14803 that PARSER->SCOPE is not considered a dependent base by
14804 lookup_member, we must enter the scope here. */
14805 if (dependent_p)
14806 pushed_scope = push_scope (parser->scope);
14807 /* If the PARSER->SCOPE is a template specialization, it
14808 may be instantiated during name lookup. In that case,
14809 errors may be issued. Even if we rollback the current
14810 tentative parse, those errors are valid. */
14811 decl = lookup_qualified_name (parser->scope, name,
14812 tag_type != none_type,
14813 /*complain=*/true);
14814 if (pushed_scope)
14815 pop_scope (pushed_scope);
14817 parser->qualifying_scope = parser->scope;
14818 parser->object_scope = NULL_TREE;
14820 else if (object_type)
14822 tree object_decl = NULL_TREE;
14823 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14824 OBJECT_TYPE is not a class. */
14825 if (CLASS_TYPE_P (object_type))
14826 /* If the OBJECT_TYPE is a template specialization, it may
14827 be instantiated during name lookup. In that case, errors
14828 may be issued. Even if we rollback the current tentative
14829 parse, those errors are valid. */
14830 object_decl = lookup_member (object_type,
14831 name,
14832 /*protect=*/0,
14833 tag_type != none_type);
14834 /* Look it up in the enclosing context, too. */
14835 decl = lookup_name_real (name, tag_type != none_type,
14836 /*nonclass=*/0,
14837 /*block_p=*/true, is_namespace, flags);
14838 parser->object_scope = object_type;
14839 parser->qualifying_scope = NULL_TREE;
14840 if (object_decl)
14841 decl = object_decl;
14843 else
14845 decl = lookup_name_real (name, tag_type != none_type,
14846 /*nonclass=*/0,
14847 /*block_p=*/true, is_namespace, flags);
14848 parser->qualifying_scope = NULL_TREE;
14849 parser->object_scope = NULL_TREE;
14852 /* If the lookup failed, let our caller know. */
14853 if (!decl || decl == error_mark_node)
14854 return error_mark_node;
14856 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14857 if (TREE_CODE (decl) == TREE_LIST)
14859 if (ambiguous_decls)
14860 *ambiguous_decls = decl;
14861 /* The error message we have to print is too complicated for
14862 cp_parser_error, so we incorporate its actions directly. */
14863 if (!cp_parser_simulate_error (parser))
14865 error ("reference to %qD is ambiguous", name);
14866 print_candidates (decl);
14868 return error_mark_node;
14871 gcc_assert (DECL_P (decl)
14872 || TREE_CODE (decl) == OVERLOAD
14873 || TREE_CODE (decl) == SCOPE_REF
14874 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14875 || BASELINK_P (decl));
14877 /* If we have resolved the name of a member declaration, check to
14878 see if the declaration is accessible. When the name resolves to
14879 set of overloaded functions, accessibility is checked when
14880 overload resolution is done.
14882 During an explicit instantiation, access is not checked at all,
14883 as per [temp.explicit]. */
14884 if (DECL_P (decl))
14885 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14887 return decl;
14890 /* Like cp_parser_lookup_name, but for use in the typical case where
14891 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14892 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14894 static tree
14895 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14897 return cp_parser_lookup_name (parser, name,
14898 none_type,
14899 /*is_template=*/false,
14900 /*is_namespace=*/false,
14901 /*check_dependency=*/true,
14902 /*ambiguous_decls=*/NULL);
14905 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14906 the current context, return the TYPE_DECL. If TAG_NAME_P is
14907 true, the DECL indicates the class being defined in a class-head,
14908 or declared in an elaborated-type-specifier.
14910 Otherwise, return DECL. */
14912 static tree
14913 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14915 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14916 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14918 struct A {
14919 template <typename T> struct B;
14922 template <typename T> struct A::B {};
14924 Similarly, in an elaborated-type-specifier:
14926 namespace N { struct X{}; }
14928 struct A {
14929 template <typename T> friend struct N::X;
14932 However, if the DECL refers to a class type, and we are in
14933 the scope of the class, then the name lookup automatically
14934 finds the TYPE_DECL created by build_self_reference rather
14935 than a TEMPLATE_DECL. For example, in:
14937 template <class T> struct S {
14938 S s;
14941 there is no need to handle such case. */
14943 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14944 return DECL_TEMPLATE_RESULT (decl);
14946 return decl;
14949 /* If too many, or too few, template-parameter lists apply to the
14950 declarator, issue an error message. Returns TRUE if all went well,
14951 and FALSE otherwise. */
14953 static bool
14954 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14955 cp_declarator *declarator)
14957 unsigned num_templates;
14959 /* We haven't seen any classes that involve template parameters yet. */
14960 num_templates = 0;
14962 switch (declarator->kind)
14964 case cdk_id:
14965 if (declarator->u.id.qualifying_scope)
14967 tree scope;
14968 tree member;
14970 scope = declarator->u.id.qualifying_scope;
14971 member = declarator->u.id.unqualified_name;
14973 while (scope && CLASS_TYPE_P (scope))
14975 /* You're supposed to have one `template <...>'
14976 for every template class, but you don't need one
14977 for a full specialization. For example:
14979 template <class T> struct S{};
14980 template <> struct S<int> { void f(); };
14981 void S<int>::f () {}
14983 is correct; there shouldn't be a `template <>' for
14984 the definition of `S<int>::f'. */
14985 if (CLASSTYPE_TEMPLATE_INFO (scope)
14986 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14987 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14988 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14989 ++num_templates;
14991 scope = TYPE_CONTEXT (scope);
14994 else if (TREE_CODE (declarator->u.id.unqualified_name)
14995 == TEMPLATE_ID_EXPR)
14996 /* If the DECLARATOR has the form `X<y>' then it uses one
14997 additional level of template parameters. */
14998 ++num_templates;
15000 return cp_parser_check_template_parameters (parser,
15001 num_templates);
15003 case cdk_function:
15004 case cdk_array:
15005 case cdk_pointer:
15006 case cdk_reference:
15007 case cdk_ptrmem:
15008 return (cp_parser_check_declarator_template_parameters
15009 (parser, declarator->declarator));
15011 case cdk_error:
15012 return true;
15014 default:
15015 gcc_unreachable ();
15017 return false;
15020 /* NUM_TEMPLATES were used in the current declaration. If that is
15021 invalid, return FALSE and issue an error messages. Otherwise,
15022 return TRUE. */
15024 static bool
15025 cp_parser_check_template_parameters (cp_parser* parser,
15026 unsigned num_templates)
15028 /* If there are more template classes than parameter lists, we have
15029 something like:
15031 template <class T> void S<T>::R<T>::f (); */
15032 if (parser->num_template_parameter_lists < num_templates)
15034 error ("too few template-parameter-lists");
15035 return false;
15037 /* If there are the same number of template classes and parameter
15038 lists, that's OK. */
15039 if (parser->num_template_parameter_lists == num_templates)
15040 return true;
15041 /* If there are more, but only one more, then we are referring to a
15042 member template. That's OK too. */
15043 if (parser->num_template_parameter_lists == num_templates + 1)
15044 return true;
15045 /* Otherwise, there are too many template parameter lists. We have
15046 something like:
15048 template <class T> template <class U> void S::f(); */
15049 error ("too many template-parameter-lists");
15050 return false;
15053 /* Parse an optional `::' token indicating that the following name is
15054 from the global namespace. If so, PARSER->SCOPE is set to the
15055 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15056 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15057 Returns the new value of PARSER->SCOPE, if the `::' token is
15058 present, and NULL_TREE otherwise. */
15060 static tree
15061 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15063 cp_token *token;
15065 /* Peek at the next token. */
15066 token = cp_lexer_peek_token (parser->lexer);
15067 /* If we're looking at a `::' token then we're starting from the
15068 global namespace, not our current location. */
15069 if (token->type == CPP_SCOPE)
15071 /* Consume the `::' token. */
15072 cp_lexer_consume_token (parser->lexer);
15073 /* Set the SCOPE so that we know where to start the lookup. */
15074 parser->scope = global_namespace;
15075 parser->qualifying_scope = global_namespace;
15076 parser->object_scope = NULL_TREE;
15078 return parser->scope;
15080 else if (!current_scope_valid_p)
15082 parser->scope = NULL_TREE;
15083 parser->qualifying_scope = NULL_TREE;
15084 parser->object_scope = NULL_TREE;
15087 return NULL_TREE;
15090 /* Returns TRUE if the upcoming token sequence is the start of a
15091 constructor declarator. If FRIEND_P is true, the declarator is
15092 preceded by the `friend' specifier. */
15094 static bool
15095 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15097 bool constructor_p;
15098 tree type_decl = NULL_TREE;
15099 bool nested_name_p;
15100 cp_token *next_token;
15102 /* The common case is that this is not a constructor declarator, so
15103 try to avoid doing lots of work if at all possible. It's not
15104 valid declare a constructor at function scope. */
15105 if (at_function_scope_p ())
15106 return false;
15107 /* And only certain tokens can begin a constructor declarator. */
15108 next_token = cp_lexer_peek_token (parser->lexer);
15109 if (next_token->type != CPP_NAME
15110 && next_token->type != CPP_SCOPE
15111 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15112 && next_token->type != CPP_TEMPLATE_ID)
15113 return false;
15115 /* Parse tentatively; we are going to roll back all of the tokens
15116 consumed here. */
15117 cp_parser_parse_tentatively (parser);
15118 /* Assume that we are looking at a constructor declarator. */
15119 constructor_p = true;
15121 /* Look for the optional `::' operator. */
15122 cp_parser_global_scope_opt (parser,
15123 /*current_scope_valid_p=*/false);
15124 /* Look for the nested-name-specifier. */
15125 nested_name_p
15126 = (cp_parser_nested_name_specifier_opt (parser,
15127 /*typename_keyword_p=*/false,
15128 /*check_dependency_p=*/false,
15129 /*type_p=*/false,
15130 /*is_declaration=*/false)
15131 != NULL_TREE);
15132 /* Outside of a class-specifier, there must be a
15133 nested-name-specifier. */
15134 if (!nested_name_p &&
15135 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15136 || friend_p))
15137 constructor_p = false;
15138 /* If we still think that this might be a constructor-declarator,
15139 look for a class-name. */
15140 if (constructor_p)
15142 /* If we have:
15144 template <typename T> struct S { S(); };
15145 template <typename T> S<T>::S ();
15147 we must recognize that the nested `S' names a class.
15148 Similarly, for:
15150 template <typename T> S<T>::S<T> ();
15152 we must recognize that the nested `S' names a template. */
15153 type_decl = cp_parser_class_name (parser,
15154 /*typename_keyword_p=*/false,
15155 /*template_keyword_p=*/false,
15156 none_type,
15157 /*check_dependency_p=*/false,
15158 /*class_head_p=*/false,
15159 /*is_declaration=*/false);
15160 /* If there was no class-name, then this is not a constructor. */
15161 constructor_p = !cp_parser_error_occurred (parser);
15164 /* If we're still considering a constructor, we have to see a `(',
15165 to begin the parameter-declaration-clause, followed by either a
15166 `)', an `...', or a decl-specifier. We need to check for a
15167 type-specifier to avoid being fooled into thinking that:
15169 S::S (f) (int);
15171 is a constructor. (It is actually a function named `f' that
15172 takes one parameter (of type `int') and returns a value of type
15173 `S::S'. */
15174 if (constructor_p
15175 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15178 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15179 /* A parameter declaration begins with a decl-specifier,
15180 which is either the "attribute" keyword, a storage class
15181 specifier, or (usually) a type-specifier. */
15182 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15183 && !cp_parser_storage_class_specifier_opt (parser))
15185 tree type;
15186 tree pushed_scope = NULL_TREE;
15187 unsigned saved_num_template_parameter_lists;
15189 /* Names appearing in the type-specifier should be looked up
15190 in the scope of the class. */
15191 if (current_class_type)
15192 type = NULL_TREE;
15193 else
15195 type = TREE_TYPE (type_decl);
15196 if (TREE_CODE (type) == TYPENAME_TYPE)
15198 type = resolve_typename_type (type,
15199 /*only_current_p=*/false);
15200 if (type == error_mark_node)
15202 cp_parser_abort_tentative_parse (parser);
15203 return false;
15206 pushed_scope = push_scope (type);
15209 /* Inside the constructor parameter list, surrounding
15210 template-parameter-lists do not apply. */
15211 saved_num_template_parameter_lists
15212 = parser->num_template_parameter_lists;
15213 parser->num_template_parameter_lists = 0;
15215 /* Look for the type-specifier. */
15216 cp_parser_type_specifier (parser,
15217 CP_PARSER_FLAGS_NONE,
15218 /*decl_specs=*/NULL,
15219 /*is_declarator=*/true,
15220 /*declares_class_or_enum=*/NULL,
15221 /*is_cv_qualifier=*/NULL);
15223 parser->num_template_parameter_lists
15224 = saved_num_template_parameter_lists;
15226 /* Leave the scope of the class. */
15227 if (pushed_scope)
15228 pop_scope (pushed_scope);
15230 constructor_p = !cp_parser_error_occurred (parser);
15233 else
15234 constructor_p = false;
15235 /* We did not really want to consume any tokens. */
15236 cp_parser_abort_tentative_parse (parser);
15238 return constructor_p;
15241 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15242 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15243 they must be performed once we are in the scope of the function.
15245 Returns the function defined. */
15247 static tree
15248 cp_parser_function_definition_from_specifiers_and_declarator
15249 (cp_parser* parser,
15250 cp_decl_specifier_seq *decl_specifiers,
15251 tree attributes,
15252 const cp_declarator *declarator)
15254 tree fn;
15255 bool success_p;
15257 /* Begin the function-definition. */
15258 success_p = start_function (decl_specifiers, declarator, attributes);
15260 /* The things we're about to see are not directly qualified by any
15261 template headers we've seen thus far. */
15262 reset_specialization ();
15264 /* If there were names looked up in the decl-specifier-seq that we
15265 did not check, check them now. We must wait until we are in the
15266 scope of the function to perform the checks, since the function
15267 might be a friend. */
15268 perform_deferred_access_checks ();
15270 if (!success_p)
15272 /* Skip the entire function. */
15273 error ("invalid function declaration");
15274 cp_parser_skip_to_end_of_block_or_statement (parser);
15275 fn = error_mark_node;
15277 else
15278 fn = cp_parser_function_definition_after_declarator (parser,
15279 /*inline_p=*/false);
15281 return fn;
15284 /* Parse the part of a function-definition that follows the
15285 declarator. INLINE_P is TRUE iff this function is an inline
15286 function defined with a class-specifier.
15288 Returns the function defined. */
15290 static tree
15291 cp_parser_function_definition_after_declarator (cp_parser* parser,
15292 bool inline_p)
15294 tree fn;
15295 bool ctor_initializer_p = false;
15296 bool saved_in_unbraced_linkage_specification_p;
15297 unsigned saved_num_template_parameter_lists;
15299 /* If the next token is `return', then the code may be trying to
15300 make use of the "named return value" extension that G++ used to
15301 support. */
15302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15304 /* Consume the `return' keyword. */
15305 cp_lexer_consume_token (parser->lexer);
15306 /* Look for the identifier that indicates what value is to be
15307 returned. */
15308 cp_parser_identifier (parser);
15309 /* Issue an error message. */
15310 error ("named return values are no longer supported");
15311 /* Skip tokens until we reach the start of the function body. */
15312 while (true)
15314 cp_token *token = cp_lexer_peek_token (parser->lexer);
15315 if (token->type == CPP_OPEN_BRACE
15316 || token->type == CPP_EOF
15317 || token->type == CPP_PRAGMA_EOL)
15318 break;
15319 cp_lexer_consume_token (parser->lexer);
15322 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15323 anything declared inside `f'. */
15324 saved_in_unbraced_linkage_specification_p
15325 = parser->in_unbraced_linkage_specification_p;
15326 parser->in_unbraced_linkage_specification_p = false;
15327 /* Inside the function, surrounding template-parameter-lists do not
15328 apply. */
15329 saved_num_template_parameter_lists
15330 = parser->num_template_parameter_lists;
15331 parser->num_template_parameter_lists = 0;
15332 /* If the next token is `try', then we are looking at a
15333 function-try-block. */
15334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15335 ctor_initializer_p = cp_parser_function_try_block (parser);
15336 /* A function-try-block includes the function-body, so we only do
15337 this next part if we're not processing a function-try-block. */
15338 else
15339 ctor_initializer_p
15340 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15342 /* Finish the function. */
15343 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15344 (inline_p ? 2 : 0));
15345 /* Generate code for it, if necessary. */
15346 expand_or_defer_fn (fn);
15347 /* Restore the saved values. */
15348 parser->in_unbraced_linkage_specification_p
15349 = saved_in_unbraced_linkage_specification_p;
15350 parser->num_template_parameter_lists
15351 = saved_num_template_parameter_lists;
15353 return fn;
15356 /* Parse a template-declaration, assuming that the `export' (and
15357 `extern') keywords, if present, has already been scanned. MEMBER_P
15358 is as for cp_parser_template_declaration. */
15360 static void
15361 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15363 tree decl = NULL_TREE;
15364 tree parameter_list;
15365 bool friend_p = false;
15366 bool need_lang_pop;
15368 /* Look for the `template' keyword. */
15369 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15370 return;
15372 /* And the `<'. */
15373 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15374 return;
15375 /* [temp]
15377 A template ... shall not have C linkage. */
15378 if (current_lang_name == lang_name_c)
15380 error ("template with C linkage");
15381 /* Give it C++ linkage to avoid confusing other parts of the
15382 front end. */
15383 push_lang_context (lang_name_cplusplus);
15384 need_lang_pop = true;
15386 else
15387 need_lang_pop = false;
15388 /* If the next token is `>', then we have an invalid
15389 specialization. Rather than complain about an invalid template
15390 parameter, issue an error message here. */
15391 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15393 cp_parser_error (parser, "invalid explicit specialization");
15394 begin_specialization ();
15395 parameter_list = NULL_TREE;
15397 else
15398 /* Parse the template parameters. */
15399 parameter_list = cp_parser_template_parameter_list (parser);
15401 /* Look for the `>'. */
15402 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15403 /* We just processed one more parameter list. */
15404 ++parser->num_template_parameter_lists;
15405 /* If the next token is `template', there are more template
15406 parameters. */
15407 if (cp_lexer_next_token_is_keyword (parser->lexer,
15408 RID_TEMPLATE))
15409 cp_parser_template_declaration_after_export (parser, member_p);
15410 else
15412 /* There are no access checks when parsing a template, as we do not
15413 know if a specialization will be a friend. */
15414 push_deferring_access_checks (dk_no_check);
15416 decl = cp_parser_single_declaration (parser,
15417 member_p,
15418 &friend_p);
15420 pop_deferring_access_checks ();
15422 /* If this is a member template declaration, let the front
15423 end know. */
15424 if (member_p && !friend_p && decl)
15426 if (TREE_CODE (decl) == TYPE_DECL)
15427 cp_parser_check_access_in_redeclaration (decl);
15429 decl = finish_member_template_decl (decl);
15431 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15432 make_friend_class (current_class_type, TREE_TYPE (decl),
15433 /*complain=*/true);
15435 /* We are done with the current parameter list. */
15436 --parser->num_template_parameter_lists;
15438 /* Finish up. */
15439 finish_template_decl (parameter_list);
15441 /* Register member declarations. */
15442 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15443 finish_member_declaration (decl);
15444 /* For the erroneous case of a template with C linkage, we pushed an
15445 implicit C++ linkage scope; exit that scope now. */
15446 if (need_lang_pop)
15447 pop_lang_context ();
15448 /* If DECL is a function template, we must return to parse it later.
15449 (Even though there is no definition, there might be default
15450 arguments that need handling.) */
15451 if (member_p && decl
15452 && (TREE_CODE (decl) == FUNCTION_DECL
15453 || DECL_FUNCTION_TEMPLATE_P (decl)))
15454 TREE_VALUE (parser->unparsed_functions_queues)
15455 = tree_cons (NULL_TREE, decl,
15456 TREE_VALUE (parser->unparsed_functions_queues));
15459 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15460 `function-definition' sequence. MEMBER_P is true, this declaration
15461 appears in a class scope.
15463 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15464 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15466 static tree
15467 cp_parser_single_declaration (cp_parser* parser,
15468 bool member_p,
15469 bool* friend_p)
15471 int declares_class_or_enum;
15472 tree decl = NULL_TREE;
15473 cp_decl_specifier_seq decl_specifiers;
15474 bool function_definition_p = false;
15476 /* This function is only used when processing a template
15477 declaration. */
15478 gcc_assert (innermost_scope_kind () == sk_template_parms
15479 || innermost_scope_kind () == sk_template_spec);
15481 /* Defer access checks until we know what is being declared. */
15482 push_deferring_access_checks (dk_deferred);
15484 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15485 alternative. */
15486 cp_parser_decl_specifier_seq (parser,
15487 CP_PARSER_FLAGS_OPTIONAL,
15488 &decl_specifiers,
15489 &declares_class_or_enum);
15490 if (friend_p)
15491 *friend_p = cp_parser_friend_p (&decl_specifiers);
15493 /* There are no template typedefs. */
15494 if (decl_specifiers.specs[(int) ds_typedef])
15496 error ("template declaration of %qs", "typedef");
15497 decl = error_mark_node;
15500 /* Gather up the access checks that occurred the
15501 decl-specifier-seq. */
15502 stop_deferring_access_checks ();
15504 /* Check for the declaration of a template class. */
15505 if (declares_class_or_enum)
15507 if (cp_parser_declares_only_class_p (parser))
15509 decl = shadow_tag (&decl_specifiers);
15511 /* In this case:
15513 struct C {
15514 friend template <typename T> struct A<T>::B;
15517 A<T>::B will be represented by a TYPENAME_TYPE, and
15518 therefore not recognized by shadow_tag. */
15519 if (friend_p && *friend_p
15520 && !decl
15521 && decl_specifiers.type
15522 && TYPE_P (decl_specifiers.type))
15523 decl = decl_specifiers.type;
15525 if (decl && decl != error_mark_node)
15526 decl = TYPE_NAME (decl);
15527 else
15528 decl = error_mark_node;
15531 /* If it's not a template class, try for a template function. If
15532 the next token is a `;', then this declaration does not declare
15533 anything. But, if there were errors in the decl-specifiers, then
15534 the error might well have come from an attempted class-specifier.
15535 In that case, there's no need to warn about a missing declarator. */
15536 if (!decl
15537 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15538 || decl_specifiers.type != error_mark_node))
15539 decl = cp_parser_init_declarator (parser,
15540 &decl_specifiers,
15541 /*function_definition_allowed_p=*/true,
15542 member_p,
15543 declares_class_or_enum,
15544 &function_definition_p);
15546 pop_deferring_access_checks ();
15548 /* Clear any current qualification; whatever comes next is the start
15549 of something new. */
15550 parser->scope = NULL_TREE;
15551 parser->qualifying_scope = NULL_TREE;
15552 parser->object_scope = NULL_TREE;
15553 /* Look for a trailing `;' after the declaration. */
15554 if (!function_definition_p
15555 && (decl == error_mark_node
15556 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15557 cp_parser_skip_to_end_of_block_or_statement (parser);
15559 return decl;
15562 /* Parse a cast-expression that is not the operand of a unary "&". */
15564 static tree
15565 cp_parser_simple_cast_expression (cp_parser *parser)
15567 return cp_parser_cast_expression (parser, /*address_p=*/false,
15568 /*cast_p=*/false);
15571 /* Parse a functional cast to TYPE. Returns an expression
15572 representing the cast. */
15574 static tree
15575 cp_parser_functional_cast (cp_parser* parser, tree type)
15577 tree expression_list;
15578 tree cast;
15580 expression_list
15581 = cp_parser_parenthesized_expression_list (parser, false,
15582 /*cast_p=*/true,
15583 /*non_constant_p=*/NULL);
15585 cast = build_functional_cast (type, expression_list);
15586 /* [expr.const]/1: In an integral constant expression "only type
15587 conversions to integral or enumeration type can be used". */
15588 if (TREE_CODE (type) == TYPE_DECL)
15589 type = TREE_TYPE (type);
15590 if (cast != error_mark_node && !dependent_type_p (type)
15591 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15593 if (cp_parser_non_integral_constant_expression
15594 (parser, "a call to a constructor"))
15595 return error_mark_node;
15597 return cast;
15600 /* Save the tokens that make up the body of a member function defined
15601 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15602 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15603 specifiers applied to the declaration. Returns the FUNCTION_DECL
15604 for the member function. */
15606 static tree
15607 cp_parser_save_member_function_body (cp_parser* parser,
15608 cp_decl_specifier_seq *decl_specifiers,
15609 cp_declarator *declarator,
15610 tree attributes)
15612 cp_token *first;
15613 cp_token *last;
15614 tree fn;
15616 /* Create the function-declaration. */
15617 fn = start_method (decl_specifiers, declarator, attributes);
15618 /* If something went badly wrong, bail out now. */
15619 if (fn == error_mark_node)
15621 /* If there's a function-body, skip it. */
15622 if (cp_parser_token_starts_function_definition_p
15623 (cp_lexer_peek_token (parser->lexer)))
15624 cp_parser_skip_to_end_of_block_or_statement (parser);
15625 return error_mark_node;
15628 /* Remember it, if there default args to post process. */
15629 cp_parser_save_default_args (parser, fn);
15631 /* Save away the tokens that make up the body of the
15632 function. */
15633 first = parser->lexer->next_token;
15634 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15635 /* Handle function try blocks. */
15636 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15637 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15638 last = parser->lexer->next_token;
15640 /* Save away the inline definition; we will process it when the
15641 class is complete. */
15642 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15643 DECL_PENDING_INLINE_P (fn) = 1;
15645 /* We need to know that this was defined in the class, so that
15646 friend templates are handled correctly. */
15647 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15649 /* We're done with the inline definition. */
15650 finish_method (fn);
15652 /* Add FN to the queue of functions to be parsed later. */
15653 TREE_VALUE (parser->unparsed_functions_queues)
15654 = tree_cons (NULL_TREE, fn,
15655 TREE_VALUE (parser->unparsed_functions_queues));
15657 return fn;
15660 /* Parse a template-argument-list, as well as the trailing ">" (but
15661 not the opening ">"). See cp_parser_template_argument_list for the
15662 return value. */
15664 static tree
15665 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15667 tree arguments;
15668 tree saved_scope;
15669 tree saved_qualifying_scope;
15670 tree saved_object_scope;
15671 bool saved_greater_than_is_operator_p;
15672 bool saved_skip_evaluation;
15674 /* [temp.names]
15676 When parsing a template-id, the first non-nested `>' is taken as
15677 the end of the template-argument-list rather than a greater-than
15678 operator. */
15679 saved_greater_than_is_operator_p
15680 = parser->greater_than_is_operator_p;
15681 parser->greater_than_is_operator_p = false;
15682 /* Parsing the argument list may modify SCOPE, so we save it
15683 here. */
15684 saved_scope = parser->scope;
15685 saved_qualifying_scope = parser->qualifying_scope;
15686 saved_object_scope = parser->object_scope;
15687 /* We need to evaluate the template arguments, even though this
15688 template-id may be nested within a "sizeof". */
15689 saved_skip_evaluation = skip_evaluation;
15690 skip_evaluation = false;
15691 /* Parse the template-argument-list itself. */
15692 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15693 arguments = NULL_TREE;
15694 else
15695 arguments = cp_parser_template_argument_list (parser);
15696 /* Look for the `>' that ends the template-argument-list. If we find
15697 a '>>' instead, it's probably just a typo. */
15698 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15700 if (!saved_greater_than_is_operator_p)
15702 /* If we're in a nested template argument list, the '>>' has
15703 to be a typo for '> >'. We emit the error message, but we
15704 continue parsing and we push a '>' as next token, so that
15705 the argument list will be parsed correctly. Note that the
15706 global source location is still on the token before the
15707 '>>', so we need to say explicitly where we want it. */
15708 cp_token *token = cp_lexer_peek_token (parser->lexer);
15709 error ("%H%<>>%> should be %<> >%> "
15710 "within a nested template argument list",
15711 &token->location);
15713 /* ??? Proper recovery should terminate two levels of
15714 template argument list here. */
15715 token->type = CPP_GREATER;
15717 else
15719 /* If this is not a nested template argument list, the '>>'
15720 is a typo for '>'. Emit an error message and continue.
15721 Same deal about the token location, but here we can get it
15722 right by consuming the '>>' before issuing the diagnostic. */
15723 cp_lexer_consume_token (parser->lexer);
15724 error ("spurious %<>>%>, use %<>%> to terminate "
15725 "a template argument list");
15728 else
15729 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15730 /* The `>' token might be a greater-than operator again now. */
15731 parser->greater_than_is_operator_p
15732 = saved_greater_than_is_operator_p;
15733 /* Restore the SAVED_SCOPE. */
15734 parser->scope = saved_scope;
15735 parser->qualifying_scope = saved_qualifying_scope;
15736 parser->object_scope = saved_object_scope;
15737 skip_evaluation = saved_skip_evaluation;
15739 return arguments;
15742 /* MEMBER_FUNCTION is a member function, or a friend. If default
15743 arguments, or the body of the function have not yet been parsed,
15744 parse them now. */
15746 static void
15747 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15749 /* If this member is a template, get the underlying
15750 FUNCTION_DECL. */
15751 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15752 member_function = DECL_TEMPLATE_RESULT (member_function);
15754 /* There should not be any class definitions in progress at this
15755 point; the bodies of members are only parsed outside of all class
15756 definitions. */
15757 gcc_assert (parser->num_classes_being_defined == 0);
15758 /* While we're parsing the member functions we might encounter more
15759 classes. We want to handle them right away, but we don't want
15760 them getting mixed up with functions that are currently in the
15761 queue. */
15762 parser->unparsed_functions_queues
15763 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15765 /* Make sure that any template parameters are in scope. */
15766 maybe_begin_member_template_processing (member_function);
15768 /* If the body of the function has not yet been parsed, parse it
15769 now. */
15770 if (DECL_PENDING_INLINE_P (member_function))
15772 tree function_scope;
15773 cp_token_cache *tokens;
15775 /* The function is no longer pending; we are processing it. */
15776 tokens = DECL_PENDING_INLINE_INFO (member_function);
15777 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15778 DECL_PENDING_INLINE_P (member_function) = 0;
15780 /* If this is a local class, enter the scope of the containing
15781 function. */
15782 function_scope = current_function_decl;
15783 if (function_scope)
15784 push_function_context_to (function_scope);
15787 /* Push the body of the function onto the lexer stack. */
15788 cp_parser_push_lexer_for_tokens (parser, tokens);
15790 /* Let the front end know that we going to be defining this
15791 function. */
15792 start_preparsed_function (member_function, NULL_TREE,
15793 SF_PRE_PARSED | SF_INCLASS_INLINE);
15795 /* Don't do access checking if it is a templated function. */
15796 if (processing_template_decl)
15797 push_deferring_access_checks (dk_no_check);
15799 /* Now, parse the body of the function. */
15800 cp_parser_function_definition_after_declarator (parser,
15801 /*inline_p=*/true);
15803 if (processing_template_decl)
15804 pop_deferring_access_checks ();
15806 /* Leave the scope of the containing function. */
15807 if (function_scope)
15808 pop_function_context_from (function_scope);
15809 cp_parser_pop_lexer (parser);
15812 /* Remove any template parameters from the symbol table. */
15813 maybe_end_member_template_processing ();
15815 /* Restore the queue. */
15816 parser->unparsed_functions_queues
15817 = TREE_CHAIN (parser->unparsed_functions_queues);
15820 /* If DECL contains any default args, remember it on the unparsed
15821 functions queue. */
15823 static void
15824 cp_parser_save_default_args (cp_parser* parser, tree decl)
15826 tree probe;
15828 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15829 probe;
15830 probe = TREE_CHAIN (probe))
15831 if (TREE_PURPOSE (probe))
15833 TREE_PURPOSE (parser->unparsed_functions_queues)
15834 = tree_cons (current_class_type, decl,
15835 TREE_PURPOSE (parser->unparsed_functions_queues));
15836 break;
15840 /* FN is a FUNCTION_DECL which may contains a parameter with an
15841 unparsed DEFAULT_ARG. Parse the default args now. This function
15842 assumes that the current scope is the scope in which the default
15843 argument should be processed. */
15845 static void
15846 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15848 bool saved_local_variables_forbidden_p;
15849 tree parm;
15851 /* While we're parsing the default args, we might (due to the
15852 statement expression extension) encounter more classes. We want
15853 to handle them right away, but we don't want them getting mixed
15854 up with default args that are currently in the queue. */
15855 parser->unparsed_functions_queues
15856 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15858 /* Local variable names (and the `this' keyword) may not appear
15859 in a default argument. */
15860 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15861 parser->local_variables_forbidden_p = true;
15863 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15864 parm;
15865 parm = TREE_CHAIN (parm))
15867 cp_token_cache *tokens;
15868 tree default_arg = TREE_PURPOSE (parm);
15869 tree parsed_arg;
15870 VEC(tree,gc) *insts;
15871 tree copy;
15872 unsigned ix;
15874 if (!default_arg)
15875 continue;
15877 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15878 /* This can happen for a friend declaration for a function
15879 already declared with default arguments. */
15880 continue;
15882 /* Push the saved tokens for the default argument onto the parser's
15883 lexer stack. */
15884 tokens = DEFARG_TOKENS (default_arg);
15885 cp_parser_push_lexer_for_tokens (parser, tokens);
15887 /* Parse the assignment-expression. */
15888 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15890 if (!processing_template_decl)
15891 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15893 TREE_PURPOSE (parm) = parsed_arg;
15895 /* Update any instantiations we've already created. */
15896 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15897 VEC_iterate (tree, insts, ix, copy); ix++)
15898 TREE_PURPOSE (copy) = parsed_arg;
15900 /* If the token stream has not been completely used up, then
15901 there was extra junk after the end of the default
15902 argument. */
15903 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15904 cp_parser_error (parser, "expected %<,%>");
15906 /* Revert to the main lexer. */
15907 cp_parser_pop_lexer (parser);
15910 /* Make sure no default arg is missing. */
15911 check_default_args (fn);
15913 /* Restore the state of local_variables_forbidden_p. */
15914 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15916 /* Restore the queue. */
15917 parser->unparsed_functions_queues
15918 = TREE_CHAIN (parser->unparsed_functions_queues);
15921 /* Parse the operand of `sizeof' (or a similar operator). Returns
15922 either a TYPE or an expression, depending on the form of the
15923 input. The KEYWORD indicates which kind of expression we have
15924 encountered. */
15926 static tree
15927 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15929 static const char *format;
15930 tree expr = NULL_TREE;
15931 const char *saved_message;
15932 bool saved_integral_constant_expression_p;
15933 bool saved_non_integral_constant_expression_p;
15935 /* Initialize FORMAT the first time we get here. */
15936 if (!format)
15937 format = "types may not be defined in '%s' expressions";
15939 /* Types cannot be defined in a `sizeof' expression. Save away the
15940 old message. */
15941 saved_message = parser->type_definition_forbidden_message;
15942 /* And create the new one. */
15943 parser->type_definition_forbidden_message
15944 = XNEWVEC (const char, strlen (format)
15945 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15946 + 1 /* `\0' */);
15947 sprintf ((char *) parser->type_definition_forbidden_message,
15948 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15950 /* The restrictions on constant-expressions do not apply inside
15951 sizeof expressions. */
15952 saved_integral_constant_expression_p
15953 = parser->integral_constant_expression_p;
15954 saved_non_integral_constant_expression_p
15955 = parser->non_integral_constant_expression_p;
15956 parser->integral_constant_expression_p = false;
15958 /* Do not actually evaluate the expression. */
15959 ++skip_evaluation;
15960 /* If it's a `(', then we might be looking at the type-id
15961 construction. */
15962 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15964 tree type;
15965 bool saved_in_type_id_in_expr_p;
15967 /* We can't be sure yet whether we're looking at a type-id or an
15968 expression. */
15969 cp_parser_parse_tentatively (parser);
15970 /* Consume the `('. */
15971 cp_lexer_consume_token (parser->lexer);
15972 /* Parse the type-id. */
15973 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15974 parser->in_type_id_in_expr_p = true;
15975 type = cp_parser_type_id (parser);
15976 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15977 /* Now, look for the trailing `)'. */
15978 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15979 /* If all went well, then we're done. */
15980 if (cp_parser_parse_definitely (parser))
15982 cp_decl_specifier_seq decl_specs;
15984 /* Build a trivial decl-specifier-seq. */
15985 clear_decl_specs (&decl_specs);
15986 decl_specs.type = type;
15988 /* Call grokdeclarator to figure out what type this is. */
15989 expr = grokdeclarator (NULL,
15990 &decl_specs,
15991 TYPENAME,
15992 /*initialized=*/0,
15993 /*attrlist=*/NULL);
15997 /* If the type-id production did not work out, then we must be
15998 looking at the unary-expression production. */
15999 if (!expr)
16000 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16001 /*cast_p=*/false);
16002 /* Go back to evaluating expressions. */
16003 --skip_evaluation;
16005 /* Free the message we created. */
16006 free ((char *) parser->type_definition_forbidden_message);
16007 /* And restore the old one. */
16008 parser->type_definition_forbidden_message = saved_message;
16009 parser->integral_constant_expression_p
16010 = saved_integral_constant_expression_p;
16011 parser->non_integral_constant_expression_p
16012 = saved_non_integral_constant_expression_p;
16014 return expr;
16017 /* If the current declaration has no declarator, return true. */
16019 static bool
16020 cp_parser_declares_only_class_p (cp_parser *parser)
16022 /* If the next token is a `;' or a `,' then there is no
16023 declarator. */
16024 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16025 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16028 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
16030 static void
16031 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
16032 cp_storage_class storage_class)
16034 if (decl_specs->storage_class != sc_none)
16035 decl_specs->multiple_storage_classes_p = true;
16036 else
16037 decl_specs->storage_class = storage_class;
16040 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16041 is true, the type is a user-defined type; otherwise it is a
16042 built-in type specified by a keyword. */
16044 static void
16045 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16046 tree type_spec,
16047 bool user_defined_p)
16049 decl_specs->any_specifiers_p = true;
16051 /* If the user tries to redeclare bool or wchar_t (with, for
16052 example, in "typedef int wchar_t;") we remember that this is what
16053 happened. In system headers, we ignore these declarations so
16054 that G++ can work with system headers that are not C++-safe. */
16055 if (decl_specs->specs[(int) ds_typedef]
16056 && !user_defined_p
16057 && (type_spec == boolean_type_node
16058 || type_spec == wchar_type_node)
16059 && (decl_specs->type
16060 || decl_specs->specs[(int) ds_long]
16061 || decl_specs->specs[(int) ds_short]
16062 || decl_specs->specs[(int) ds_unsigned]
16063 || decl_specs->specs[(int) ds_signed]))
16065 decl_specs->redefined_builtin_type = type_spec;
16066 if (!decl_specs->type)
16068 decl_specs->type = type_spec;
16069 decl_specs->user_defined_type_p = false;
16072 else if (decl_specs->type)
16073 decl_specs->multiple_types_p = true;
16074 else
16076 decl_specs->type = type_spec;
16077 decl_specs->user_defined_type_p = user_defined_p;
16078 decl_specs->redefined_builtin_type = NULL_TREE;
16082 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16083 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16085 static bool
16086 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16088 return decl_specifiers->specs[(int) ds_friend] != 0;
16091 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16092 issue an error message indicating that TOKEN_DESC was expected.
16094 Returns the token consumed, if the token had the appropriate type.
16095 Otherwise, returns NULL. */
16097 static cp_token *
16098 cp_parser_require (cp_parser* parser,
16099 enum cpp_ttype type,
16100 const char* token_desc)
16102 if (cp_lexer_next_token_is (parser->lexer, type))
16103 return cp_lexer_consume_token (parser->lexer);
16104 else
16106 /* Output the MESSAGE -- unless we're parsing tentatively. */
16107 if (!cp_parser_simulate_error (parser))
16109 char *message = concat ("expected ", token_desc, NULL);
16110 cp_parser_error (parser, message);
16111 free (message);
16113 return NULL;
16117 /* Like cp_parser_require, except that tokens will be skipped until
16118 the desired token is found. An error message is still produced if
16119 the next token is not as expected. */
16121 static void
16122 cp_parser_skip_until_found (cp_parser* parser,
16123 enum cpp_ttype type,
16124 const char* token_desc)
16126 cp_token *token;
16127 unsigned nesting_depth = 0;
16129 if (cp_parser_require (parser, type, token_desc))
16130 return;
16132 /* Skip tokens until the desired token is found. */
16133 while (true)
16135 /* Peek at the next token. */
16136 token = cp_lexer_peek_token (parser->lexer);
16138 /* If we've reached the token we want, consume it and stop. */
16139 if (token->type == type && !nesting_depth)
16141 cp_lexer_consume_token (parser->lexer);
16142 return;
16145 switch (token->type)
16147 case CPP_EOF:
16148 case CPP_PRAGMA_EOL:
16149 /* If we've run out of tokens, stop. */
16150 return;
16152 case CPP_OPEN_BRACE:
16153 case CPP_OPEN_PAREN:
16154 case CPP_OPEN_SQUARE:
16155 ++nesting_depth;
16156 break;
16158 case CPP_CLOSE_BRACE:
16159 case CPP_CLOSE_PAREN:
16160 case CPP_CLOSE_SQUARE:
16161 if (nesting_depth-- == 0)
16162 return;
16163 break;
16165 default:
16166 break;
16169 /* Consume this token. */
16170 cp_lexer_consume_token (parser->lexer);
16174 /* If the next token is the indicated keyword, consume it. Otherwise,
16175 issue an error message indicating that TOKEN_DESC was expected.
16177 Returns the token consumed, if the token had the appropriate type.
16178 Otherwise, returns NULL. */
16180 static cp_token *
16181 cp_parser_require_keyword (cp_parser* parser,
16182 enum rid keyword,
16183 const char* token_desc)
16185 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16187 if (token && token->keyword != keyword)
16189 dyn_string_t error_msg;
16191 /* Format the error message. */
16192 error_msg = dyn_string_new (0);
16193 dyn_string_append_cstr (error_msg, "expected ");
16194 dyn_string_append_cstr (error_msg, token_desc);
16195 cp_parser_error (parser, error_msg->s);
16196 dyn_string_delete (error_msg);
16197 return NULL;
16200 return token;
16203 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16204 function-definition. */
16206 static bool
16207 cp_parser_token_starts_function_definition_p (cp_token* token)
16209 return (/* An ordinary function-body begins with an `{'. */
16210 token->type == CPP_OPEN_BRACE
16211 /* A ctor-initializer begins with a `:'. */
16212 || token->type == CPP_COLON
16213 /* A function-try-block begins with `try'. */
16214 || token->keyword == RID_TRY
16215 /* The named return value extension begins with `return'. */
16216 || token->keyword == RID_RETURN);
16219 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16220 definition. */
16222 static bool
16223 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16225 cp_token *token;
16227 token = cp_lexer_peek_token (parser->lexer);
16228 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16231 /* Returns TRUE iff the next token is the "," or ">" ending a
16232 template-argument. */
16234 static bool
16235 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16237 cp_token *token;
16239 token = cp_lexer_peek_token (parser->lexer);
16240 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16243 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16244 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16246 static bool
16247 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16248 size_t n)
16250 cp_token *token;
16252 token = cp_lexer_peek_nth_token (parser->lexer, n);
16253 if (token->type == CPP_LESS)
16254 return true;
16255 /* Check for the sequence `<::' in the original code. It would be lexed as
16256 `[:', where `[' is a digraph, and there is no whitespace before
16257 `:'. */
16258 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16260 cp_token *token2;
16261 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16262 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16263 return true;
16265 return false;
16268 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16269 or none_type otherwise. */
16271 static enum tag_types
16272 cp_parser_token_is_class_key (cp_token* token)
16274 switch (token->keyword)
16276 case RID_CLASS:
16277 return class_type;
16278 case RID_STRUCT:
16279 return record_type;
16280 case RID_UNION:
16281 return union_type;
16283 default:
16284 return none_type;
16288 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16290 static void
16291 cp_parser_check_class_key (enum tag_types class_key, tree type)
16293 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16294 pedwarn ("%qs tag used in naming %q#T",
16295 class_key == union_type ? "union"
16296 : class_key == record_type ? "struct" : "class",
16297 type);
16300 /* Issue an error message if DECL is redeclared with different
16301 access than its original declaration [class.access.spec/3].
16302 This applies to nested classes and nested class templates.
16303 [class.mem/1]. */
16305 static void
16306 cp_parser_check_access_in_redeclaration (tree decl)
16308 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16309 return;
16311 if ((TREE_PRIVATE (decl)
16312 != (current_access_specifier == access_private_node))
16313 || (TREE_PROTECTED (decl)
16314 != (current_access_specifier == access_protected_node)))
16315 error ("%qD redeclared with different access", decl);
16318 /* Look for the `template' keyword, as a syntactic disambiguator.
16319 Return TRUE iff it is present, in which case it will be
16320 consumed. */
16322 static bool
16323 cp_parser_optional_template_keyword (cp_parser *parser)
16325 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16327 /* The `template' keyword can only be used within templates;
16328 outside templates the parser can always figure out what is a
16329 template and what is not. */
16330 if (!processing_template_decl)
16332 error ("%<template%> (as a disambiguator) is only allowed "
16333 "within templates");
16334 /* If this part of the token stream is rescanned, the same
16335 error message would be generated. So, we purge the token
16336 from the stream. */
16337 cp_lexer_purge_token (parser->lexer);
16338 return false;
16340 else
16342 /* Consume the `template' keyword. */
16343 cp_lexer_consume_token (parser->lexer);
16344 return true;
16348 return false;
16351 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16352 set PARSER->SCOPE, and perform other related actions. */
16354 static void
16355 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16357 tree value;
16358 tree check;
16360 /* Get the stored value. */
16361 value = cp_lexer_consume_token (parser->lexer)->value;
16362 /* Perform any access checks that were deferred. */
16363 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16364 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16365 /* Set the scope from the stored value. */
16366 parser->scope = TREE_VALUE (value);
16367 parser->qualifying_scope = TREE_TYPE (value);
16368 parser->object_scope = NULL_TREE;
16371 /* Consume tokens up through a non-nested END token. */
16373 static void
16374 cp_parser_cache_group (cp_parser *parser,
16375 enum cpp_ttype end,
16376 unsigned depth)
16378 while (true)
16380 cp_token *token;
16382 /* Abort a parenthesized expression if we encounter a brace. */
16383 if ((end == CPP_CLOSE_PAREN || depth == 0)
16384 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16385 return;
16386 /* If we've reached the end of the file, stop. */
16387 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16388 || (end != CPP_PRAGMA_EOL
16389 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16390 return;
16391 /* Consume the next token. */
16392 token = cp_lexer_consume_token (parser->lexer);
16393 /* See if it starts a new group. */
16394 if (token->type == CPP_OPEN_BRACE)
16396 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16397 if (depth == 0)
16398 return;
16400 else if (token->type == CPP_OPEN_PAREN)
16401 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16402 else if (token->type == CPP_PRAGMA)
16403 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16404 else if (token->type == end)
16405 return;
16409 /* Begin parsing tentatively. We always save tokens while parsing
16410 tentatively so that if the tentative parsing fails we can restore the
16411 tokens. */
16413 static void
16414 cp_parser_parse_tentatively (cp_parser* parser)
16416 /* Enter a new parsing context. */
16417 parser->context = cp_parser_context_new (parser->context);
16418 /* Begin saving tokens. */
16419 cp_lexer_save_tokens (parser->lexer);
16420 /* In order to avoid repetitive access control error messages,
16421 access checks are queued up until we are no longer parsing
16422 tentatively. */
16423 push_deferring_access_checks (dk_deferred);
16426 /* Commit to the currently active tentative parse. */
16428 static void
16429 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16431 cp_parser_context *context;
16432 cp_lexer *lexer;
16434 /* Mark all of the levels as committed. */
16435 lexer = parser->lexer;
16436 for (context = parser->context; context->next; context = context->next)
16438 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16439 break;
16440 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16441 while (!cp_lexer_saving_tokens (lexer))
16442 lexer = lexer->next;
16443 cp_lexer_commit_tokens (lexer);
16447 /* Abort the currently active tentative parse. All consumed tokens
16448 will be rolled back, and no diagnostics will be issued. */
16450 static void
16451 cp_parser_abort_tentative_parse (cp_parser* parser)
16453 cp_parser_simulate_error (parser);
16454 /* Now, pretend that we want to see if the construct was
16455 successfully parsed. */
16456 cp_parser_parse_definitely (parser);
16459 /* Stop parsing tentatively. If a parse error has occurred, restore the
16460 token stream. Otherwise, commit to the tokens we have consumed.
16461 Returns true if no error occurred; false otherwise. */
16463 static bool
16464 cp_parser_parse_definitely (cp_parser* parser)
16466 bool error_occurred;
16467 cp_parser_context *context;
16469 /* Remember whether or not an error occurred, since we are about to
16470 destroy that information. */
16471 error_occurred = cp_parser_error_occurred (parser);
16472 /* Remove the topmost context from the stack. */
16473 context = parser->context;
16474 parser->context = context->next;
16475 /* If no parse errors occurred, commit to the tentative parse. */
16476 if (!error_occurred)
16478 /* Commit to the tokens read tentatively, unless that was
16479 already done. */
16480 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16481 cp_lexer_commit_tokens (parser->lexer);
16483 pop_to_parent_deferring_access_checks ();
16485 /* Otherwise, if errors occurred, roll back our state so that things
16486 are just as they were before we began the tentative parse. */
16487 else
16489 cp_lexer_rollback_tokens (parser->lexer);
16490 pop_deferring_access_checks ();
16492 /* Add the context to the front of the free list. */
16493 context->next = cp_parser_context_free_list;
16494 cp_parser_context_free_list = context;
16496 return !error_occurred;
16499 /* Returns true if we are parsing tentatively and are not committed to
16500 this tentative parse. */
16502 static bool
16503 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16505 return (cp_parser_parsing_tentatively (parser)
16506 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16509 /* Returns nonzero iff an error has occurred during the most recent
16510 tentative parse. */
16512 static bool
16513 cp_parser_error_occurred (cp_parser* parser)
16515 return (cp_parser_parsing_tentatively (parser)
16516 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16519 /* Returns nonzero if GNU extensions are allowed. */
16521 static bool
16522 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16524 return parser->allow_gnu_extensions_p;
16527 /* Objective-C++ Productions */
16530 /* Parse an Objective-C expression, which feeds into a primary-expression
16531 above.
16533 objc-expression:
16534 objc-message-expression
16535 objc-string-literal
16536 objc-encode-expression
16537 objc-protocol-expression
16538 objc-selector-expression
16540 Returns a tree representation of the expression. */
16542 static tree
16543 cp_parser_objc_expression (cp_parser* parser)
16545 /* Try to figure out what kind of declaration is present. */
16546 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16548 switch (kwd->type)
16550 case CPP_OPEN_SQUARE:
16551 return cp_parser_objc_message_expression (parser);
16553 case CPP_OBJC_STRING:
16554 kwd = cp_lexer_consume_token (parser->lexer);
16555 return objc_build_string_object (kwd->value);
16557 case CPP_KEYWORD:
16558 switch (kwd->keyword)
16560 case RID_AT_ENCODE:
16561 return cp_parser_objc_encode_expression (parser);
16563 case RID_AT_PROTOCOL:
16564 return cp_parser_objc_protocol_expression (parser);
16566 case RID_AT_SELECTOR:
16567 return cp_parser_objc_selector_expression (parser);
16569 default:
16570 break;
16572 default:
16573 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16574 cp_parser_skip_to_end_of_block_or_statement (parser);
16577 return error_mark_node;
16580 /* Parse an Objective-C message expression.
16582 objc-message-expression:
16583 [ objc-message-receiver objc-message-args ]
16585 Returns a representation of an Objective-C message. */
16587 static tree
16588 cp_parser_objc_message_expression (cp_parser* parser)
16590 tree receiver, messageargs;
16592 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16593 receiver = cp_parser_objc_message_receiver (parser);
16594 messageargs = cp_parser_objc_message_args (parser);
16595 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16597 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16600 /* Parse an objc-message-receiver.
16602 objc-message-receiver:
16603 expression
16604 simple-type-specifier
16606 Returns a representation of the type or expression. */
16608 static tree
16609 cp_parser_objc_message_receiver (cp_parser* parser)
16611 tree rcv;
16613 /* An Objective-C message receiver may be either (1) a type
16614 or (2) an expression. */
16615 cp_parser_parse_tentatively (parser);
16616 rcv = cp_parser_expression (parser, false);
16618 if (cp_parser_parse_definitely (parser))
16619 return rcv;
16621 rcv = cp_parser_simple_type_specifier (parser,
16622 /*decl_specs=*/NULL,
16623 CP_PARSER_FLAGS_NONE);
16625 return objc_get_class_reference (rcv);
16628 /* Parse the arguments and selectors comprising an Objective-C message.
16630 objc-message-args:
16631 objc-selector
16632 objc-selector-args
16633 objc-selector-args , objc-comma-args
16635 objc-selector-args:
16636 objc-selector [opt] : assignment-expression
16637 objc-selector-args objc-selector [opt] : assignment-expression
16639 objc-comma-args:
16640 assignment-expression
16641 objc-comma-args , assignment-expression
16643 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16644 selector arguments and TREE_VALUE containing a list of comma
16645 arguments. */
16647 static tree
16648 cp_parser_objc_message_args (cp_parser* parser)
16650 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16651 bool maybe_unary_selector_p = true;
16652 cp_token *token = cp_lexer_peek_token (parser->lexer);
16654 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16656 tree selector = NULL_TREE, arg;
16658 if (token->type != CPP_COLON)
16659 selector = cp_parser_objc_selector (parser);
16661 /* Detect if we have a unary selector. */
16662 if (maybe_unary_selector_p
16663 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16664 return build_tree_list (selector, NULL_TREE);
16666 maybe_unary_selector_p = false;
16667 cp_parser_require (parser, CPP_COLON, "`:'");
16668 arg = cp_parser_assignment_expression (parser, false);
16670 sel_args
16671 = chainon (sel_args,
16672 build_tree_list (selector, arg));
16674 token = cp_lexer_peek_token (parser->lexer);
16677 /* Handle non-selector arguments, if any. */
16678 while (token->type == CPP_COMMA)
16680 tree arg;
16682 cp_lexer_consume_token (parser->lexer);
16683 arg = cp_parser_assignment_expression (parser, false);
16685 addl_args
16686 = chainon (addl_args,
16687 build_tree_list (NULL_TREE, arg));
16689 token = cp_lexer_peek_token (parser->lexer);
16692 return build_tree_list (sel_args, addl_args);
16695 /* Parse an Objective-C encode expression.
16697 objc-encode-expression:
16698 @encode objc-typename
16700 Returns an encoded representation of the type argument. */
16702 static tree
16703 cp_parser_objc_encode_expression (cp_parser* parser)
16705 tree type;
16707 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16708 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16709 type = complete_type (cp_parser_type_id (parser));
16710 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16712 if (!type)
16714 error ("%<@encode%> must specify a type as an argument");
16715 return error_mark_node;
16718 return objc_build_encode_expr (type);
16721 /* Parse an Objective-C @defs expression. */
16723 static tree
16724 cp_parser_objc_defs_expression (cp_parser *parser)
16726 tree name;
16728 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16730 name = cp_parser_identifier (parser);
16731 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16733 return objc_get_class_ivars (name);
16736 /* Parse an Objective-C protocol expression.
16738 objc-protocol-expression:
16739 @protocol ( identifier )
16741 Returns a representation of the protocol expression. */
16743 static tree
16744 cp_parser_objc_protocol_expression (cp_parser* parser)
16746 tree proto;
16748 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16749 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16750 proto = cp_parser_identifier (parser);
16751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16753 return objc_build_protocol_expr (proto);
16756 /* Parse an Objective-C selector expression.
16758 objc-selector-expression:
16759 @selector ( objc-method-signature )
16761 objc-method-signature:
16762 objc-selector
16763 objc-selector-seq
16765 objc-selector-seq:
16766 objc-selector :
16767 objc-selector-seq objc-selector :
16769 Returns a representation of the method selector. */
16771 static tree
16772 cp_parser_objc_selector_expression (cp_parser* parser)
16774 tree sel_seq = NULL_TREE;
16775 bool maybe_unary_selector_p = true;
16776 cp_token *token;
16778 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16779 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16780 token = cp_lexer_peek_token (parser->lexer);
16782 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16783 || token->type == CPP_SCOPE)
16785 tree selector = NULL_TREE;
16787 if (token->type != CPP_COLON
16788 || token->type == CPP_SCOPE)
16789 selector = cp_parser_objc_selector (parser);
16791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16792 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16794 /* Detect if we have a unary selector. */
16795 if (maybe_unary_selector_p)
16797 sel_seq = selector;
16798 goto finish_selector;
16800 else
16802 cp_parser_error (parser, "expected %<:%>");
16805 maybe_unary_selector_p = false;
16806 token = cp_lexer_consume_token (parser->lexer);
16808 if (token->type == CPP_SCOPE)
16810 sel_seq
16811 = chainon (sel_seq,
16812 build_tree_list (selector, NULL_TREE));
16813 sel_seq
16814 = chainon (sel_seq,
16815 build_tree_list (NULL_TREE, NULL_TREE));
16817 else
16818 sel_seq
16819 = chainon (sel_seq,
16820 build_tree_list (selector, NULL_TREE));
16822 token = cp_lexer_peek_token (parser->lexer);
16825 finish_selector:
16826 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16828 return objc_build_selector_expr (sel_seq);
16831 /* Parse a list of identifiers.
16833 objc-identifier-list:
16834 identifier
16835 objc-identifier-list , identifier
16837 Returns a TREE_LIST of identifier nodes. */
16839 static tree
16840 cp_parser_objc_identifier_list (cp_parser* parser)
16842 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16843 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16845 while (sep->type == CPP_COMMA)
16847 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16848 list = chainon (list,
16849 build_tree_list (NULL_TREE,
16850 cp_parser_identifier (parser)));
16851 sep = cp_lexer_peek_token (parser->lexer);
16854 return list;
16857 /* Parse an Objective-C alias declaration.
16859 objc-alias-declaration:
16860 @compatibility_alias identifier identifier ;
16862 This function registers the alias mapping with the Objective-C front-end.
16863 It returns nothing. */
16865 static void
16866 cp_parser_objc_alias_declaration (cp_parser* parser)
16868 tree alias, orig;
16870 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16871 alias = cp_parser_identifier (parser);
16872 orig = cp_parser_identifier (parser);
16873 objc_declare_alias (alias, orig);
16874 cp_parser_consume_semicolon_at_end_of_statement (parser);
16877 /* Parse an Objective-C class forward-declaration.
16879 objc-class-declaration:
16880 @class objc-identifier-list ;
16882 The function registers the forward declarations with the Objective-C
16883 front-end. It returns nothing. */
16885 static void
16886 cp_parser_objc_class_declaration (cp_parser* parser)
16888 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16889 objc_declare_class (cp_parser_objc_identifier_list (parser));
16890 cp_parser_consume_semicolon_at_end_of_statement (parser);
16893 /* Parse a list of Objective-C protocol references.
16895 objc-protocol-refs-opt:
16896 objc-protocol-refs [opt]
16898 objc-protocol-refs:
16899 < objc-identifier-list >
16901 Returns a TREE_LIST of identifiers, if any. */
16903 static tree
16904 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16906 tree protorefs = NULL_TREE;
16908 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16910 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16911 protorefs = cp_parser_objc_identifier_list (parser);
16912 cp_parser_require (parser, CPP_GREATER, "`>'");
16915 return protorefs;
16918 /* Parse a Objective-C visibility specification. */
16920 static void
16921 cp_parser_objc_visibility_spec (cp_parser* parser)
16923 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16925 switch (vis->keyword)
16927 case RID_AT_PRIVATE:
16928 objc_set_visibility (2);
16929 break;
16930 case RID_AT_PROTECTED:
16931 objc_set_visibility (0);
16932 break;
16933 case RID_AT_PUBLIC:
16934 objc_set_visibility (1);
16935 break;
16936 default:
16937 return;
16940 /* Eat '@private'/'@protected'/'@public'. */
16941 cp_lexer_consume_token (parser->lexer);
16944 /* Parse an Objective-C method type. */
16946 static void
16947 cp_parser_objc_method_type (cp_parser* parser)
16949 objc_set_method_type
16950 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16951 ? PLUS_EXPR
16952 : MINUS_EXPR);
16955 /* Parse an Objective-C protocol qualifier. */
16957 static tree
16958 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16960 tree quals = NULL_TREE, node;
16961 cp_token *token = cp_lexer_peek_token (parser->lexer);
16963 node = token->value;
16965 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16966 && (node == ridpointers [(int) RID_IN]
16967 || node == ridpointers [(int) RID_OUT]
16968 || node == ridpointers [(int) RID_INOUT]
16969 || node == ridpointers [(int) RID_BYCOPY]
16970 || node == ridpointers [(int) RID_BYREF]
16971 || node == ridpointers [(int) RID_ONEWAY]))
16973 quals = tree_cons (NULL_TREE, node, quals);
16974 cp_lexer_consume_token (parser->lexer);
16975 token = cp_lexer_peek_token (parser->lexer);
16976 node = token->value;
16979 return quals;
16982 /* Parse an Objective-C typename. */
16984 static tree
16985 cp_parser_objc_typename (cp_parser* parser)
16987 tree typename = NULL_TREE;
16989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16991 tree proto_quals, cp_type = NULL_TREE;
16993 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16994 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16996 /* An ObjC type name may consist of just protocol qualifiers, in which
16997 case the type shall default to 'id'. */
16998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16999 cp_type = cp_parser_type_id (parser);
17001 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17002 typename = build_tree_list (proto_quals, cp_type);
17005 return typename;
17008 /* Check to see if TYPE refers to an Objective-C selector name. */
17010 static bool
17011 cp_parser_objc_selector_p (enum cpp_ttype type)
17013 return (type == CPP_NAME || type == CPP_KEYWORD
17014 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17015 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17016 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17017 || type == CPP_XOR || type == CPP_XOR_EQ);
17020 /* Parse an Objective-C selector. */
17022 static tree
17023 cp_parser_objc_selector (cp_parser* parser)
17025 cp_token *token = cp_lexer_consume_token (parser->lexer);
17027 if (!cp_parser_objc_selector_p (token->type))
17029 error ("invalid Objective-C++ selector name");
17030 return error_mark_node;
17033 /* C++ operator names are allowed to appear in ObjC selectors. */
17034 switch (token->type)
17036 case CPP_AND_AND: return get_identifier ("and");
17037 case CPP_AND_EQ: return get_identifier ("and_eq");
17038 case CPP_AND: return get_identifier ("bitand");
17039 case CPP_OR: return get_identifier ("bitor");
17040 case CPP_COMPL: return get_identifier ("compl");
17041 case CPP_NOT: return get_identifier ("not");
17042 case CPP_NOT_EQ: return get_identifier ("not_eq");
17043 case CPP_OR_OR: return get_identifier ("or");
17044 case CPP_OR_EQ: return get_identifier ("or_eq");
17045 case CPP_XOR: return get_identifier ("xor");
17046 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17047 default: return token->value;
17051 /* Parse an Objective-C params list. */
17053 static tree
17054 cp_parser_objc_method_keyword_params (cp_parser* parser)
17056 tree params = NULL_TREE;
17057 bool maybe_unary_selector_p = true;
17058 cp_token *token = cp_lexer_peek_token (parser->lexer);
17060 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17062 tree selector = NULL_TREE, typename, identifier;
17064 if (token->type != CPP_COLON)
17065 selector = cp_parser_objc_selector (parser);
17067 /* Detect if we have a unary selector. */
17068 if (maybe_unary_selector_p
17069 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17070 return selector;
17072 maybe_unary_selector_p = false;
17073 cp_parser_require (parser, CPP_COLON, "`:'");
17074 typename = cp_parser_objc_typename (parser);
17075 identifier = cp_parser_identifier (parser);
17077 params
17078 = chainon (params,
17079 objc_build_keyword_decl (selector,
17080 typename,
17081 identifier));
17083 token = cp_lexer_peek_token (parser->lexer);
17086 return params;
17089 /* Parse the non-keyword Objective-C params. */
17091 static tree
17092 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17094 tree params = make_node (TREE_LIST);
17095 cp_token *token = cp_lexer_peek_token (parser->lexer);
17096 *ellipsisp = false; /* Initially, assume no ellipsis. */
17098 while (token->type == CPP_COMMA)
17100 cp_parameter_declarator *parmdecl;
17101 tree parm;
17103 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17104 token = cp_lexer_peek_token (parser->lexer);
17106 if (token->type == CPP_ELLIPSIS)
17108 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17109 *ellipsisp = true;
17110 break;
17113 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17114 parm = grokdeclarator (parmdecl->declarator,
17115 &parmdecl->decl_specifiers,
17116 PARM, /*initialized=*/0,
17117 /*attrlist=*/NULL);
17119 chainon (params, build_tree_list (NULL_TREE, parm));
17120 token = cp_lexer_peek_token (parser->lexer);
17123 return params;
17126 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17128 static void
17129 cp_parser_objc_interstitial_code (cp_parser* parser)
17131 cp_token *token = cp_lexer_peek_token (parser->lexer);
17133 /* If the next token is `extern' and the following token is a string
17134 literal, then we have a linkage specification. */
17135 if (token->keyword == RID_EXTERN
17136 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17137 cp_parser_linkage_specification (parser);
17138 /* Handle #pragma, if any. */
17139 else if (token->type == CPP_PRAGMA)
17140 cp_parser_pragma (parser, pragma_external);
17141 /* Allow stray semicolons. */
17142 else if (token->type == CPP_SEMICOLON)
17143 cp_lexer_consume_token (parser->lexer);
17144 /* Finally, try to parse a block-declaration, or a function-definition. */
17145 else
17146 cp_parser_block_declaration (parser, /*statement_p=*/false);
17149 /* Parse a method signature. */
17151 static tree
17152 cp_parser_objc_method_signature (cp_parser* parser)
17154 tree rettype, kwdparms, optparms;
17155 bool ellipsis = false;
17157 cp_parser_objc_method_type (parser);
17158 rettype = cp_parser_objc_typename (parser);
17159 kwdparms = cp_parser_objc_method_keyword_params (parser);
17160 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17162 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17165 /* Pars an Objective-C method prototype list. */
17167 static void
17168 cp_parser_objc_method_prototype_list (cp_parser* parser)
17170 cp_token *token = cp_lexer_peek_token (parser->lexer);
17172 while (token->keyword != RID_AT_END)
17174 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17176 objc_add_method_declaration
17177 (cp_parser_objc_method_signature (parser));
17178 cp_parser_consume_semicolon_at_end_of_statement (parser);
17180 else
17181 /* Allow for interspersed non-ObjC++ code. */
17182 cp_parser_objc_interstitial_code (parser);
17184 token = cp_lexer_peek_token (parser->lexer);
17187 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17188 objc_finish_interface ();
17191 /* Parse an Objective-C method definition list. */
17193 static void
17194 cp_parser_objc_method_definition_list (cp_parser* parser)
17196 cp_token *token = cp_lexer_peek_token (parser->lexer);
17198 while (token->keyword != RID_AT_END)
17200 tree meth;
17202 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17204 push_deferring_access_checks (dk_deferred);
17205 objc_start_method_definition
17206 (cp_parser_objc_method_signature (parser));
17208 /* For historical reasons, we accept an optional semicolon. */
17209 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17210 cp_lexer_consume_token (parser->lexer);
17212 perform_deferred_access_checks ();
17213 stop_deferring_access_checks ();
17214 meth = cp_parser_function_definition_after_declarator (parser,
17215 false);
17216 pop_deferring_access_checks ();
17217 objc_finish_method_definition (meth);
17219 else
17220 /* Allow for interspersed non-ObjC++ code. */
17221 cp_parser_objc_interstitial_code (parser);
17223 token = cp_lexer_peek_token (parser->lexer);
17226 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17227 objc_finish_implementation ();
17230 /* Parse Objective-C ivars. */
17232 static void
17233 cp_parser_objc_class_ivars (cp_parser* parser)
17235 cp_token *token = cp_lexer_peek_token (parser->lexer);
17237 if (token->type != CPP_OPEN_BRACE)
17238 return; /* No ivars specified. */
17240 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17241 token = cp_lexer_peek_token (parser->lexer);
17243 while (token->type != CPP_CLOSE_BRACE)
17245 cp_decl_specifier_seq declspecs;
17246 int decl_class_or_enum_p;
17247 tree prefix_attributes;
17249 cp_parser_objc_visibility_spec (parser);
17251 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17252 break;
17254 cp_parser_decl_specifier_seq (parser,
17255 CP_PARSER_FLAGS_OPTIONAL,
17256 &declspecs,
17257 &decl_class_or_enum_p);
17258 prefix_attributes = declspecs.attributes;
17259 declspecs.attributes = NULL_TREE;
17261 /* Keep going until we hit the `;' at the end of the
17262 declaration. */
17263 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17265 tree width = NULL_TREE, attributes, first_attribute, decl;
17266 cp_declarator *declarator = NULL;
17267 int ctor_dtor_or_conv_p;
17269 /* Check for a (possibly unnamed) bitfield declaration. */
17270 token = cp_lexer_peek_token (parser->lexer);
17271 if (token->type == CPP_COLON)
17272 goto eat_colon;
17274 if (token->type == CPP_NAME
17275 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17276 == CPP_COLON))
17278 /* Get the name of the bitfield. */
17279 declarator = make_id_declarator (NULL_TREE,
17280 cp_parser_identifier (parser),
17281 sfk_none);
17283 eat_colon:
17284 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17285 /* Get the width of the bitfield. */
17286 width
17287 = cp_parser_constant_expression (parser,
17288 /*allow_non_constant=*/false,
17289 NULL);
17291 else
17293 /* Parse the declarator. */
17294 declarator
17295 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17296 &ctor_dtor_or_conv_p,
17297 /*parenthesized_p=*/NULL,
17298 /*member_p=*/false);
17301 /* Look for attributes that apply to the ivar. */
17302 attributes = cp_parser_attributes_opt (parser);
17303 /* Remember which attributes are prefix attributes and
17304 which are not. */
17305 first_attribute = attributes;
17306 /* Combine the attributes. */
17307 attributes = chainon (prefix_attributes, attributes);
17309 if (width)
17311 /* Create the bitfield declaration. */
17312 decl = grokbitfield (declarator, &declspecs, width);
17313 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17315 else
17316 decl = grokfield (declarator, &declspecs,
17317 NULL_TREE, /*init_const_expr_p=*/false,
17318 NULL_TREE, attributes);
17320 /* Add the instance variable. */
17321 objc_add_instance_variable (decl);
17323 /* Reset PREFIX_ATTRIBUTES. */
17324 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17325 attributes = TREE_CHAIN (attributes);
17326 if (attributes)
17327 TREE_CHAIN (attributes) = NULL_TREE;
17329 token = cp_lexer_peek_token (parser->lexer);
17331 if (token->type == CPP_COMMA)
17333 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17334 continue;
17336 break;
17339 cp_parser_consume_semicolon_at_end_of_statement (parser);
17340 token = cp_lexer_peek_token (parser->lexer);
17343 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17344 /* For historical reasons, we accept an optional semicolon. */
17345 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17346 cp_lexer_consume_token (parser->lexer);
17349 /* Parse an Objective-C protocol declaration. */
17351 static void
17352 cp_parser_objc_protocol_declaration (cp_parser* parser)
17354 tree proto, protorefs;
17355 cp_token *tok;
17357 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17360 error ("identifier expected after %<@protocol%>");
17361 goto finish;
17364 /* See if we have a forward declaration or a definition. */
17365 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17367 /* Try a forward declaration first. */
17368 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17370 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17371 finish:
17372 cp_parser_consume_semicolon_at_end_of_statement (parser);
17375 /* Ok, we got a full-fledged definition (or at least should). */
17376 else
17378 proto = cp_parser_identifier (parser);
17379 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17380 objc_start_protocol (proto, protorefs);
17381 cp_parser_objc_method_prototype_list (parser);
17385 /* Parse an Objective-C superclass or category. */
17387 static void
17388 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17389 tree *categ)
17391 cp_token *next = cp_lexer_peek_token (parser->lexer);
17393 *super = *categ = NULL_TREE;
17394 if (next->type == CPP_COLON)
17396 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17397 *super = cp_parser_identifier (parser);
17399 else if (next->type == CPP_OPEN_PAREN)
17401 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17402 *categ = cp_parser_identifier (parser);
17403 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17407 /* Parse an Objective-C class interface. */
17409 static void
17410 cp_parser_objc_class_interface (cp_parser* parser)
17412 tree name, super, categ, protos;
17414 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17415 name = cp_parser_identifier (parser);
17416 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17417 protos = cp_parser_objc_protocol_refs_opt (parser);
17419 /* We have either a class or a category on our hands. */
17420 if (categ)
17421 objc_start_category_interface (name, categ, protos);
17422 else
17424 objc_start_class_interface (name, super, protos);
17425 /* Handle instance variable declarations, if any. */
17426 cp_parser_objc_class_ivars (parser);
17427 objc_continue_interface ();
17430 cp_parser_objc_method_prototype_list (parser);
17433 /* Parse an Objective-C class implementation. */
17435 static void
17436 cp_parser_objc_class_implementation (cp_parser* parser)
17438 tree name, super, categ;
17440 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17441 name = cp_parser_identifier (parser);
17442 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17444 /* We have either a class or a category on our hands. */
17445 if (categ)
17446 objc_start_category_implementation (name, categ);
17447 else
17449 objc_start_class_implementation (name, super);
17450 /* Handle instance variable declarations, if any. */
17451 cp_parser_objc_class_ivars (parser);
17452 objc_continue_implementation ();
17455 cp_parser_objc_method_definition_list (parser);
17458 /* Consume the @end token and finish off the implementation. */
17460 static void
17461 cp_parser_objc_end_implementation (cp_parser* parser)
17463 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17464 objc_finish_implementation ();
17467 /* Parse an Objective-C declaration. */
17469 static void
17470 cp_parser_objc_declaration (cp_parser* parser)
17472 /* Try to figure out what kind of declaration is present. */
17473 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17475 switch (kwd->keyword)
17477 case RID_AT_ALIAS:
17478 cp_parser_objc_alias_declaration (parser);
17479 break;
17480 case RID_AT_CLASS:
17481 cp_parser_objc_class_declaration (parser);
17482 break;
17483 case RID_AT_PROTOCOL:
17484 cp_parser_objc_protocol_declaration (parser);
17485 break;
17486 case RID_AT_INTERFACE:
17487 cp_parser_objc_class_interface (parser);
17488 break;
17489 case RID_AT_IMPLEMENTATION:
17490 cp_parser_objc_class_implementation (parser);
17491 break;
17492 case RID_AT_END:
17493 cp_parser_objc_end_implementation (parser);
17494 break;
17495 default:
17496 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17497 cp_parser_skip_to_end_of_block_or_statement (parser);
17501 /* Parse an Objective-C try-catch-finally statement.
17503 objc-try-catch-finally-stmt:
17504 @try compound-statement objc-catch-clause-seq [opt]
17505 objc-finally-clause [opt]
17507 objc-catch-clause-seq:
17508 objc-catch-clause objc-catch-clause-seq [opt]
17510 objc-catch-clause:
17511 @catch ( exception-declaration ) compound-statement
17513 objc-finally-clause
17514 @finally compound-statement
17516 Returns NULL_TREE. */
17518 static tree
17519 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17520 location_t location;
17521 tree stmt;
17523 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17524 location = cp_lexer_peek_token (parser->lexer)->location;
17525 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17526 node, lest it get absorbed into the surrounding block. */
17527 stmt = push_stmt_list ();
17528 cp_parser_compound_statement (parser, NULL, false);
17529 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17531 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17533 cp_parameter_declarator *parmdecl;
17534 tree parm;
17536 cp_lexer_consume_token (parser->lexer);
17537 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17538 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17539 parm = grokdeclarator (parmdecl->declarator,
17540 &parmdecl->decl_specifiers,
17541 PARM, /*initialized=*/0,
17542 /*attrlist=*/NULL);
17543 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17544 objc_begin_catch_clause (parm);
17545 cp_parser_compound_statement (parser, NULL, false);
17546 objc_finish_catch_clause ();
17549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17551 cp_lexer_consume_token (parser->lexer);
17552 location = cp_lexer_peek_token (parser->lexer)->location;
17553 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17554 node, lest it get absorbed into the surrounding block. */
17555 stmt = push_stmt_list ();
17556 cp_parser_compound_statement (parser, NULL, false);
17557 objc_build_finally_clause (location, pop_stmt_list (stmt));
17560 return objc_finish_try_stmt ();
17563 /* Parse an Objective-C synchronized statement.
17565 objc-synchronized-stmt:
17566 @synchronized ( expression ) compound-statement
17568 Returns NULL_TREE. */
17570 static tree
17571 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17572 location_t location;
17573 tree lock, stmt;
17575 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17577 location = cp_lexer_peek_token (parser->lexer)->location;
17578 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17579 lock = cp_parser_expression (parser, false);
17580 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17582 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17583 node, lest it get absorbed into the surrounding block. */
17584 stmt = push_stmt_list ();
17585 cp_parser_compound_statement (parser, NULL, false);
17587 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17590 /* Parse an Objective-C throw statement.
17592 objc-throw-stmt:
17593 @throw assignment-expression [opt] ;
17595 Returns a constructed '@throw' statement. */
17597 static tree
17598 cp_parser_objc_throw_statement (cp_parser *parser) {
17599 tree expr = NULL_TREE;
17601 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17604 expr = cp_parser_assignment_expression (parser, false);
17606 cp_parser_consume_semicolon_at_end_of_statement (parser);
17608 return objc_build_throw_stmt (expr);
17611 /* Parse an Objective-C statement. */
17613 static tree
17614 cp_parser_objc_statement (cp_parser * parser) {
17615 /* Try to figure out what kind of declaration is present. */
17616 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17618 switch (kwd->keyword)
17620 case RID_AT_TRY:
17621 return cp_parser_objc_try_catch_finally_statement (parser);
17622 case RID_AT_SYNCHRONIZED:
17623 return cp_parser_objc_synchronized_statement (parser);
17624 case RID_AT_THROW:
17625 return cp_parser_objc_throw_statement (parser);
17626 default:
17627 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17628 cp_parser_skip_to_end_of_block_or_statement (parser);
17631 return error_mark_node;
17634 /* OpenMP 2.5 parsing routines. */
17636 /* All OpenMP clauses. OpenMP 2.5. */
17637 typedef enum pragma_omp_clause {
17638 PRAGMA_OMP_CLAUSE_NONE = 0,
17640 PRAGMA_OMP_CLAUSE_COPYIN,
17641 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17642 PRAGMA_OMP_CLAUSE_DEFAULT,
17643 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17644 PRAGMA_OMP_CLAUSE_IF,
17645 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17646 PRAGMA_OMP_CLAUSE_NOWAIT,
17647 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17648 PRAGMA_OMP_CLAUSE_ORDERED,
17649 PRAGMA_OMP_CLAUSE_PRIVATE,
17650 PRAGMA_OMP_CLAUSE_REDUCTION,
17651 PRAGMA_OMP_CLAUSE_SCHEDULE,
17652 PRAGMA_OMP_CLAUSE_SHARED
17653 } pragma_omp_clause;
17655 /* Returns name of the next clause.
17656 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17657 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17658 returned and the token is consumed. */
17660 static pragma_omp_clause
17661 cp_parser_omp_clause_name (cp_parser *parser)
17663 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17666 result = PRAGMA_OMP_CLAUSE_IF;
17667 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17668 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17669 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17670 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17671 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17673 tree id = cp_lexer_peek_token (parser->lexer)->value;
17674 const char *p = IDENTIFIER_POINTER (id);
17676 switch (p[0])
17678 case 'c':
17679 if (!strcmp ("copyin", p))
17680 result = PRAGMA_OMP_CLAUSE_COPYIN;
17681 else if (!strcmp ("copyprivate", p))
17682 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17683 break;
17684 case 'f':
17685 if (!strcmp ("firstprivate", p))
17686 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17687 break;
17688 case 'l':
17689 if (!strcmp ("lastprivate", p))
17690 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17691 break;
17692 case 'n':
17693 if (!strcmp ("nowait", p))
17694 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17695 else if (!strcmp ("num_threads", p))
17696 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17697 break;
17698 case 'o':
17699 if (!strcmp ("ordered", p))
17700 result = PRAGMA_OMP_CLAUSE_ORDERED;
17701 break;
17702 case 'r':
17703 if (!strcmp ("reduction", p))
17704 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17705 break;
17706 case 's':
17707 if (!strcmp ("schedule", p))
17708 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17709 else if (!strcmp ("shared", p))
17710 result = PRAGMA_OMP_CLAUSE_SHARED;
17711 break;
17715 if (result != PRAGMA_OMP_CLAUSE_NONE)
17716 cp_lexer_consume_token (parser->lexer);
17718 return result;
17721 /* Validate that a clause of the given type does not already exist. */
17723 static void
17724 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17726 tree c;
17728 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17729 if (OMP_CLAUSE_CODE (c) == code)
17731 error ("too many %qs clauses", name);
17732 break;
17736 /* OpenMP 2.5:
17737 variable-list:
17738 identifier
17739 variable-list , identifier
17741 In addition, we match a closing parenthesis. An opening parenthesis
17742 will have been consumed by the caller.
17744 If KIND is nonzero, create the appropriate node and install the decl
17745 in OMP_CLAUSE_DECL and add the node to the head of the list.
17747 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17748 return the list created. */
17750 static tree
17751 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17752 tree list)
17754 while (1)
17756 tree name, decl;
17758 name = cp_parser_id_expression (parser, /*template_p=*/false,
17759 /*check_dependency_p=*/true,
17760 /*template_p=*/NULL,
17761 /*declarator_p=*/false);
17762 if (name == error_mark_node)
17763 goto skip_comma;
17765 decl = cp_parser_lookup_name_simple (parser, name);
17766 if (decl == error_mark_node)
17767 cp_parser_name_lookup_error (parser, name, decl, NULL);
17768 else if (kind != 0)
17770 tree u = build_omp_clause (kind);
17771 OMP_CLAUSE_DECL (u) = decl;
17772 OMP_CLAUSE_CHAIN (u) = list;
17773 list = u;
17775 else
17776 list = tree_cons (decl, NULL_TREE, list);
17778 get_comma:
17779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17780 break;
17781 cp_lexer_consume_token (parser->lexer);
17784 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17786 int ending;
17788 /* Try to resync to an unnested comma. Copied from
17789 cp_parser_parenthesized_expression_list. */
17790 skip_comma:
17791 ending = cp_parser_skip_to_closing_parenthesis (parser,
17792 /*recovering=*/true,
17793 /*or_comma=*/true,
17794 /*consume_paren=*/true);
17795 if (ending < 0)
17796 goto get_comma;
17799 return list;
17802 /* Similarly, but expect leading and trailing parenthesis. This is a very
17803 common case for omp clauses. */
17805 static tree
17806 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17808 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17809 return cp_parser_omp_var_list_no_open (parser, kind, list);
17810 return list;
17813 /* OpenMP 2.5:
17814 default ( shared | none ) */
17816 static tree
17817 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17819 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17820 tree c;
17822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17823 return list;
17824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17826 tree id = cp_lexer_peek_token (parser->lexer)->value;
17827 const char *p = IDENTIFIER_POINTER (id);
17829 switch (p[0])
17831 case 'n':
17832 if (strcmp ("none", p) != 0)
17833 goto invalid_kind;
17834 kind = OMP_CLAUSE_DEFAULT_NONE;
17835 break;
17837 case 's':
17838 if (strcmp ("shared", p) != 0)
17839 goto invalid_kind;
17840 kind = OMP_CLAUSE_DEFAULT_SHARED;
17841 break;
17843 default:
17844 goto invalid_kind;
17847 cp_lexer_consume_token (parser->lexer);
17849 else
17851 invalid_kind:
17852 cp_parser_error (parser, "expected %<none%> or %<shared%>");
17855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17857 /*or_comma=*/false,
17858 /*consume_paren=*/true);
17860 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17861 return list;
17863 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
17864 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
17865 OMP_CLAUSE_CHAIN (c) = list;
17866 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
17868 return c;
17871 /* OpenMP 2.5:
17872 if ( expression ) */
17874 static tree
17875 cp_parser_omp_clause_if (cp_parser *parser, tree list)
17877 tree t, c;
17879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17880 return list;
17882 t = cp_parser_condition (parser);
17884 if (t == error_mark_node
17885 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17887 /*or_comma=*/false,
17888 /*consume_paren=*/true);
17890 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
17892 c = build_omp_clause (OMP_CLAUSE_IF);
17893 OMP_CLAUSE_IF_EXPR (c) = t;
17894 OMP_CLAUSE_CHAIN (c) = list;
17896 return c;
17899 /* OpenMP 2.5:
17900 nowait */
17902 static tree
17903 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17905 tree c;
17907 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
17909 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
17910 OMP_CLAUSE_CHAIN (c) = list;
17911 return c;
17914 /* OpenMP 2.5:
17915 num_threads ( expression ) */
17917 static tree
17918 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
17920 tree t, c;
17922 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17923 return list;
17925 t = cp_parser_expression (parser, false);
17927 if (t == error_mark_node
17928 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17929 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17930 /*or_comma=*/false,
17931 /*consume_paren=*/true);
17933 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
17935 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
17936 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
17937 OMP_CLAUSE_CHAIN (c) = list;
17939 return c;
17942 /* OpenMP 2.5:
17943 ordered */
17945 static tree
17946 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17948 tree c;
17950 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
17952 c = build_omp_clause (OMP_CLAUSE_ORDERED);
17953 OMP_CLAUSE_CHAIN (c) = list;
17954 return c;
17957 /* OpenMP 2.5:
17958 reduction ( reduction-operator : variable-list )
17960 reduction-operator:
17961 One of: + * - & ^ | && || */
17963 static tree
17964 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
17966 enum tree_code code;
17967 tree nlist, c;
17969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17970 return list;
17972 switch (cp_lexer_peek_token (parser->lexer)->type)
17974 case CPP_PLUS:
17975 code = PLUS_EXPR;
17976 break;
17977 case CPP_MULT:
17978 code = MULT_EXPR;
17979 break;
17980 case CPP_MINUS:
17981 code = MINUS_EXPR;
17982 break;
17983 case CPP_AND:
17984 code = BIT_AND_EXPR;
17985 break;
17986 case CPP_XOR:
17987 code = BIT_XOR_EXPR;
17988 break;
17989 case CPP_OR:
17990 code = BIT_IOR_EXPR;
17991 break;
17992 case CPP_AND_AND:
17993 code = TRUTH_ANDIF_EXPR;
17994 break;
17995 case CPP_OR_OR:
17996 code = TRUTH_ORIF_EXPR;
17997 break;
17998 default:
17999 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18000 resync_fail:
18001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18002 /*or_comma=*/false,
18003 /*consume_paren=*/true);
18004 return list;
18006 cp_lexer_consume_token (parser->lexer);
18008 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18009 goto resync_fail;
18011 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18012 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18013 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18015 return nlist;
18018 /* OpenMP 2.5:
18019 schedule ( schedule-kind )
18020 schedule ( schedule-kind , expression )
18022 schedule-kind:
18023 static | dynamic | guided | runtime
18026 static tree
18027 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18029 tree c, t;
18031 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18032 return list;
18034 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18038 tree id = cp_lexer_peek_token (parser->lexer)->value;
18039 const char *p = IDENTIFIER_POINTER (id);
18041 switch (p[0])
18043 case 'd':
18044 if (strcmp ("dynamic", p) != 0)
18045 goto invalid_kind;
18046 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18047 break;
18049 case 'g':
18050 if (strcmp ("guided", p) != 0)
18051 goto invalid_kind;
18052 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18053 break;
18055 case 'r':
18056 if (strcmp ("runtime", p) != 0)
18057 goto invalid_kind;
18058 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18059 break;
18061 default:
18062 goto invalid_kind;
18065 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18066 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18067 else
18068 goto invalid_kind;
18069 cp_lexer_consume_token (parser->lexer);
18071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18073 cp_lexer_consume_token (parser->lexer);
18075 t = cp_parser_assignment_expression (parser, false);
18077 if (t == error_mark_node)
18078 goto resync_fail;
18079 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18080 error ("schedule %<runtime%> does not take "
18081 "a %<chunk_size%> parameter");
18082 else
18083 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18085 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18086 goto resync_fail;
18088 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18089 goto resync_fail;
18091 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18092 OMP_CLAUSE_CHAIN (c) = list;
18093 return c;
18095 invalid_kind:
18096 cp_parser_error (parser, "invalid schedule kind");
18097 resync_fail:
18098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18099 /*or_comma=*/false,
18100 /*consume_paren=*/true);
18101 return list;
18104 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18105 is a bitmask in MASK. Return the list of clauses found; the result
18106 of clause default goes in *pdefault. */
18108 static tree
18109 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18110 const char *where, cp_token *pragma_tok)
18112 tree clauses = NULL;
18114 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18116 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18117 const char *c_name;
18118 tree prev = clauses;
18120 switch (c_kind)
18122 case PRAGMA_OMP_CLAUSE_COPYIN:
18123 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18124 c_name = "copyin";
18125 break;
18126 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18128 clauses);
18129 c_name = "copyprivate";
18130 break;
18131 case PRAGMA_OMP_CLAUSE_DEFAULT:
18132 clauses = cp_parser_omp_clause_default (parser, clauses);
18133 c_name = "default";
18134 break;
18135 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18136 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18137 clauses);
18138 c_name = "firstprivate";
18139 break;
18140 case PRAGMA_OMP_CLAUSE_IF:
18141 clauses = cp_parser_omp_clause_if (parser, clauses);
18142 c_name = "if";
18143 break;
18144 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18145 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18146 clauses);
18147 c_name = "lastprivate";
18148 break;
18149 case PRAGMA_OMP_CLAUSE_NOWAIT:
18150 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18151 c_name = "nowait";
18152 break;
18153 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18154 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18155 c_name = "num_threads";
18156 break;
18157 case PRAGMA_OMP_CLAUSE_ORDERED:
18158 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18159 c_name = "ordered";
18160 break;
18161 case PRAGMA_OMP_CLAUSE_PRIVATE:
18162 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18163 clauses);
18164 c_name = "private";
18165 break;
18166 case PRAGMA_OMP_CLAUSE_REDUCTION:
18167 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18168 c_name = "reduction";
18169 break;
18170 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18171 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18172 c_name = "schedule";
18173 break;
18174 case PRAGMA_OMP_CLAUSE_SHARED:
18175 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18176 clauses);
18177 c_name = "shared";
18178 break;
18179 default:
18180 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18181 goto saw_error;
18184 if (((mask >> c_kind) & 1) == 0)
18186 /* Remove the invalid clause(s) from the list to avoid
18187 confusing the rest of the compiler. */
18188 clauses = prev;
18189 error ("%qs is not valid for %qs", c_name, where);
18192 saw_error:
18193 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18194 return finish_omp_clauses (clauses);
18197 /* OpenMP 2.5:
18198 structured-block:
18199 statement
18201 In practice, we're also interested in adding the statement to an
18202 outer node. So it is convenient if we work around the fact that
18203 cp_parser_statement calls add_stmt. */
18205 static unsigned
18206 cp_parser_begin_omp_structured_block (cp_parser *parser)
18208 unsigned save = parser->in_statement;
18210 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18211 This preserves the "not within loop or switch" style error messages
18212 for nonsense cases like
18213 void foo() {
18214 #pragma omp single
18215 break;
18218 if (parser->in_statement)
18219 parser->in_statement = IN_OMP_BLOCK;
18221 return save;
18224 static void
18225 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18227 parser->in_statement = save;
18230 static tree
18231 cp_parser_omp_structured_block (cp_parser *parser)
18233 tree stmt = begin_omp_structured_block ();
18234 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18236 cp_parser_statement (parser, NULL_TREE, false);
18238 cp_parser_end_omp_structured_block (parser, save);
18239 return finish_omp_structured_block (stmt);
18242 /* OpenMP 2.5:
18243 # pragma omp atomic new-line
18244 expression-stmt
18246 expression-stmt:
18247 x binop= expr | x++ | ++x | x-- | --x
18248 binop:
18249 +, *, -, /, &, ^, |, <<, >>
18251 where x is an lvalue expression with scalar type. */
18253 static void
18254 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18256 tree lhs, rhs;
18257 enum tree_code code;
18259 cp_parser_require_pragma_eol (parser, pragma_tok);
18261 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18262 /*cast_p=*/false);
18263 switch (TREE_CODE (lhs))
18265 case ERROR_MARK:
18266 goto saw_error;
18268 case PREINCREMENT_EXPR:
18269 case POSTINCREMENT_EXPR:
18270 lhs = TREE_OPERAND (lhs, 0);
18271 code = PLUS_EXPR;
18272 rhs = integer_one_node;
18273 break;
18275 case PREDECREMENT_EXPR:
18276 case POSTDECREMENT_EXPR:
18277 lhs = TREE_OPERAND (lhs, 0);
18278 code = MINUS_EXPR;
18279 rhs = integer_one_node;
18280 break;
18282 default:
18283 switch (cp_lexer_peek_token (parser->lexer)->type)
18285 case CPP_MULT_EQ:
18286 code = MULT_EXPR;
18287 break;
18288 case CPP_DIV_EQ:
18289 code = TRUNC_DIV_EXPR;
18290 break;
18291 case CPP_PLUS_EQ:
18292 code = PLUS_EXPR;
18293 break;
18294 case CPP_MINUS_EQ:
18295 code = MINUS_EXPR;
18296 break;
18297 case CPP_LSHIFT_EQ:
18298 code = LSHIFT_EXPR;
18299 break;
18300 case CPP_RSHIFT_EQ:
18301 code = RSHIFT_EXPR;
18302 break;
18303 case CPP_AND_EQ:
18304 code = BIT_AND_EXPR;
18305 break;
18306 case CPP_OR_EQ:
18307 code = BIT_IOR_EXPR;
18308 break;
18309 case CPP_XOR_EQ:
18310 code = BIT_XOR_EXPR;
18311 break;
18312 default:
18313 cp_parser_error (parser,
18314 "invalid operator for %<#pragma omp atomic%>");
18315 goto saw_error;
18317 cp_lexer_consume_token (parser->lexer);
18319 rhs = cp_parser_expression (parser, false);
18320 if (rhs == error_mark_node)
18321 goto saw_error;
18322 break;
18324 finish_omp_atomic (code, lhs, rhs);
18325 cp_parser_consume_semicolon_at_end_of_statement (parser);
18326 return;
18328 saw_error:
18329 cp_parser_skip_to_end_of_block_or_statement (parser);
18333 /* OpenMP 2.5:
18334 # pragma omp barrier new-line
18337 static void
18338 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18340 cp_parser_require_pragma_eol (parser, pragma_tok);
18341 finish_omp_barrier ();
18344 /* OpenMP 2.5:
18345 # pragma omp critical [(name)] new-line
18346 structured-block
18349 static tree
18350 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18352 tree stmt, name = NULL;
18354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18356 cp_lexer_consume_token (parser->lexer);
18358 name = cp_parser_identifier (parser);
18360 if (name == error_mark_node
18361 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18363 /*or_comma=*/false,
18364 /*consume_paren=*/true);
18365 if (name == error_mark_node)
18366 name = NULL;
18368 cp_parser_require_pragma_eol (parser, pragma_tok);
18370 stmt = cp_parser_omp_structured_block (parser);
18371 return c_finish_omp_critical (stmt, name);
18374 /* OpenMP 2.5:
18375 # pragma omp flush flush-vars[opt] new-line
18377 flush-vars:
18378 ( variable-list ) */
18380 static void
18381 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18384 (void) cp_parser_omp_var_list (parser, 0, NULL);
18385 cp_parser_require_pragma_eol (parser, pragma_tok);
18387 finish_omp_flush ();
18390 /* Parse the restricted form of the for statment allowed by OpenMP. */
18392 static tree
18393 cp_parser_omp_for_loop (cp_parser *parser)
18395 tree init, cond, incr, body, decl, pre_body;
18396 location_t loc;
18398 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18400 cp_parser_error (parser, "for statement expected");
18401 return NULL;
18403 loc = cp_lexer_consume_token (parser->lexer)->location;
18404 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18405 return NULL;
18407 init = decl = NULL;
18408 pre_body = push_stmt_list ();
18409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18411 cp_decl_specifier_seq type_specifiers;
18413 /* First, try to parse as an initialized declaration. See
18414 cp_parser_condition, from whence the bulk of this is copied. */
18416 cp_parser_parse_tentatively (parser);
18417 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18418 &type_specifiers);
18419 if (!cp_parser_error_occurred (parser))
18421 tree asm_specification, attributes;
18422 cp_declarator *declarator;
18424 declarator = cp_parser_declarator (parser,
18425 CP_PARSER_DECLARATOR_NAMED,
18426 /*ctor_dtor_or_conv_p=*/NULL,
18427 /*parenthesized_p=*/NULL,
18428 /*member_p=*/false);
18429 attributes = cp_parser_attributes_opt (parser);
18430 asm_specification = cp_parser_asm_specification_opt (parser);
18432 cp_parser_require (parser, CPP_EQ, "`='");
18433 if (cp_parser_parse_definitely (parser))
18435 tree pushed_scope;
18437 decl = start_decl (declarator, &type_specifiers,
18438 /*initialized_p=*/false, attributes,
18439 /*prefix_attributes=*/NULL_TREE,
18440 &pushed_scope);
18442 init = cp_parser_assignment_expression (parser, false);
18444 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18445 asm_specification, LOOKUP_ONLYCONVERTING);
18447 if (pushed_scope)
18448 pop_scope (pushed_scope);
18452 /* If parsing as an initialized declaration failed, try again as
18453 a simple expression. */
18454 if (decl == NULL)
18456 cp_parser_abort_tentative_parse (parser);
18457 init = cp_parser_expression (parser, false);
18460 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18461 pre_body = pop_stmt_list (pre_body);
18463 cond = NULL;
18464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18465 cond = cp_parser_condition (parser);
18466 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18468 incr = NULL;
18469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18470 incr = cp_parser_expression (parser, false);
18472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18474 /*or_comma=*/false,
18475 /*consume_paren=*/true);
18477 /* Note that we saved the original contents of this flag when we entered
18478 the structured block, and so we don't need to re-save it here. */
18479 parser->in_statement = IN_OMP_FOR;
18481 /* Note that the grammar doesn't call for a structured block here,
18482 though the loop as a whole is a structured block. */
18483 body = push_stmt_list ();
18484 cp_parser_statement (parser, NULL_TREE, false);
18485 body = pop_stmt_list (body);
18487 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18490 /* OpenMP 2.5:
18491 #pragma omp for for-clause[optseq] new-line
18492 for-loop
18495 #define OMP_FOR_CLAUSE_MASK \
18496 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18497 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18498 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18499 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18500 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18501 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18502 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18504 static tree
18505 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18507 tree clauses, sb, ret;
18508 unsigned int save;
18510 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18511 "#pragma omp for", pragma_tok);
18513 sb = begin_omp_structured_block ();
18514 save = cp_parser_begin_omp_structured_block (parser);
18516 ret = cp_parser_omp_for_loop (parser);
18517 if (ret)
18518 OMP_FOR_CLAUSES (ret) = clauses;
18520 cp_parser_end_omp_structured_block (parser, save);
18521 add_stmt (finish_omp_structured_block (sb));
18523 return ret;
18526 /* OpenMP 2.5:
18527 # pragma omp master new-line
18528 structured-block
18531 static tree
18532 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18534 cp_parser_require_pragma_eol (parser, pragma_tok);
18535 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18538 /* OpenMP 2.5:
18539 # pragma omp ordered new-line
18540 structured-block
18543 static tree
18544 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18546 cp_parser_require_pragma_eol (parser, pragma_tok);
18547 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18550 /* OpenMP 2.5:
18552 section-scope:
18553 { section-sequence }
18555 section-sequence:
18556 section-directive[opt] structured-block
18557 section-sequence section-directive structured-block */
18559 static tree
18560 cp_parser_omp_sections_scope (cp_parser *parser)
18562 tree stmt, substmt;
18563 bool error_suppress = false;
18564 cp_token *tok;
18566 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18567 return NULL_TREE;
18569 stmt = push_stmt_list ();
18571 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18573 unsigned save;
18575 substmt = begin_omp_structured_block ();
18576 save = cp_parser_begin_omp_structured_block (parser);
18578 while (1)
18580 cp_parser_statement (parser, NULL_TREE, false);
18582 tok = cp_lexer_peek_token (parser->lexer);
18583 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18584 break;
18585 if (tok->type == CPP_CLOSE_BRACE)
18586 break;
18587 if (tok->type == CPP_EOF)
18588 break;
18591 cp_parser_end_omp_structured_block (parser, save);
18592 substmt = finish_omp_structured_block (substmt);
18593 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18594 add_stmt (substmt);
18597 while (1)
18599 tok = cp_lexer_peek_token (parser->lexer);
18600 if (tok->type == CPP_CLOSE_BRACE)
18601 break;
18602 if (tok->type == CPP_EOF)
18603 break;
18605 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18607 cp_lexer_consume_token (parser->lexer);
18608 cp_parser_require_pragma_eol (parser, tok);
18609 error_suppress = false;
18611 else if (!error_suppress)
18613 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18614 error_suppress = true;
18617 substmt = cp_parser_omp_structured_block (parser);
18618 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18619 add_stmt (substmt);
18621 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18623 substmt = pop_stmt_list (stmt);
18625 stmt = make_node (OMP_SECTIONS);
18626 TREE_TYPE (stmt) = void_type_node;
18627 OMP_SECTIONS_BODY (stmt) = substmt;
18629 add_stmt (stmt);
18630 return stmt;
18633 /* OpenMP 2.5:
18634 # pragma omp sections sections-clause[optseq] newline
18635 sections-scope
18638 #define OMP_SECTIONS_CLAUSE_MASK \
18639 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18640 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18641 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18642 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18643 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18645 static tree
18646 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18648 tree clauses, ret;
18650 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18651 "#pragma omp sections", pragma_tok);
18653 ret = cp_parser_omp_sections_scope (parser);
18654 if (ret)
18655 OMP_SECTIONS_CLAUSES (ret) = clauses;
18657 return ret;
18660 /* OpenMP 2.5:
18661 # pragma parallel parallel-clause new-line
18662 # pragma parallel for parallel-for-clause new-line
18663 # pragma parallel sections parallel-sections-clause new-line
18666 #define OMP_PARALLEL_CLAUSE_MASK \
18667 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18668 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18669 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18670 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18671 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18672 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18673 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18674 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18676 static tree
18677 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18679 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18680 const char *p_name = "#pragma omp parallel";
18681 tree stmt, clauses, par_clause, ws_clause, block;
18682 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18683 unsigned int save;
18685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18687 cp_lexer_consume_token (parser->lexer);
18688 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18689 p_name = "#pragma omp parallel for";
18690 mask |= OMP_FOR_CLAUSE_MASK;
18691 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18693 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18695 tree id = cp_lexer_peek_token (parser->lexer)->value;
18696 const char *p = IDENTIFIER_POINTER (id);
18697 if (strcmp (p, "sections") == 0)
18699 cp_lexer_consume_token (parser->lexer);
18700 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18701 p_name = "#pragma omp parallel sections";
18702 mask |= OMP_SECTIONS_CLAUSE_MASK;
18703 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18707 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18708 block = begin_omp_parallel ();
18709 save = cp_parser_begin_omp_structured_block (parser);
18711 switch (p_kind)
18713 case PRAGMA_OMP_PARALLEL:
18714 cp_parser_already_scoped_statement (parser);
18715 par_clause = clauses;
18716 break;
18718 case PRAGMA_OMP_PARALLEL_FOR:
18719 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18720 stmt = cp_parser_omp_for_loop (parser);
18721 if (stmt)
18722 OMP_FOR_CLAUSES (stmt) = ws_clause;
18723 break;
18725 case PRAGMA_OMP_PARALLEL_SECTIONS:
18726 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18727 stmt = cp_parser_omp_sections_scope (parser);
18728 if (stmt)
18729 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18730 break;
18732 default:
18733 gcc_unreachable ();
18736 cp_parser_end_omp_structured_block (parser, save);
18737 return finish_omp_parallel (par_clause, block);
18740 /* OpenMP 2.5:
18741 # pragma omp single single-clause[optseq] new-line
18742 structured-block
18745 #define OMP_SINGLE_CLAUSE_MASK \
18746 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18747 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18748 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18749 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18751 static tree
18752 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18754 tree stmt = make_node (OMP_SINGLE);
18755 TREE_TYPE (stmt) = void_type_node;
18757 OMP_SINGLE_CLAUSES (stmt)
18758 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18759 "#pragma omp single", pragma_tok);
18760 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18762 return add_stmt (stmt);
18765 /* OpenMP 2.5:
18766 # pragma omp threadprivate (variable-list) */
18768 static void
18769 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18771 tree vars;
18773 vars = cp_parser_omp_var_list (parser, 0, NULL);
18774 cp_parser_require_pragma_eol (parser, pragma_tok);
18776 if (!targetm.have_tls)
18777 sorry ("threadprivate variables not supported in this target");
18779 finish_omp_threadprivate (vars);
18782 /* Main entry point to OpenMP statement pragmas. */
18784 static void
18785 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18787 tree stmt;
18789 switch (pragma_tok->pragma_kind)
18791 case PRAGMA_OMP_ATOMIC:
18792 cp_parser_omp_atomic (parser, pragma_tok);
18793 return;
18794 case PRAGMA_OMP_CRITICAL:
18795 stmt = cp_parser_omp_critical (parser, pragma_tok);
18796 break;
18797 case PRAGMA_OMP_FOR:
18798 stmt = cp_parser_omp_for (parser, pragma_tok);
18799 break;
18800 case PRAGMA_OMP_MASTER:
18801 stmt = cp_parser_omp_master (parser, pragma_tok);
18802 break;
18803 case PRAGMA_OMP_ORDERED:
18804 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18805 break;
18806 case PRAGMA_OMP_PARALLEL:
18807 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18808 break;
18809 case PRAGMA_OMP_SECTIONS:
18810 stmt = cp_parser_omp_sections (parser, pragma_tok);
18811 break;
18812 case PRAGMA_OMP_SINGLE:
18813 stmt = cp_parser_omp_single (parser, pragma_tok);
18814 break;
18815 default:
18816 gcc_unreachable ();
18819 if (stmt)
18820 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18823 /* The parser. */
18825 static GTY (()) cp_parser *the_parser;
18828 /* Special handling for the first token or line in the file. The first
18829 thing in the file might be #pragma GCC pch_preprocess, which loads a
18830 PCH file, which is a GC collection point. So we need to handle this
18831 first pragma without benefit of an existing lexer structure.
18833 Always returns one token to the caller in *FIRST_TOKEN. This is
18834 either the true first token of the file, or the first token after
18835 the initial pragma. */
18837 static void
18838 cp_parser_initial_pragma (cp_token *first_token)
18840 tree name = NULL;
18842 cp_lexer_get_preprocessor_token (NULL, first_token);
18843 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18844 return;
18846 cp_lexer_get_preprocessor_token (NULL, first_token);
18847 if (first_token->type == CPP_STRING)
18849 name = first_token->value;
18851 cp_lexer_get_preprocessor_token (NULL, first_token);
18852 if (first_token->type != CPP_PRAGMA_EOL)
18853 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18855 else
18856 error ("expected string literal");
18858 /* Skip to the end of the pragma. */
18859 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18860 cp_lexer_get_preprocessor_token (NULL, first_token);
18862 /* Read one more token to return to our caller. */
18863 cp_lexer_get_preprocessor_token (NULL, first_token);
18865 /* Now actually load the PCH file. */
18866 if (name)
18867 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
18870 /* Normal parsing of a pragma token. Here we can (and must) use the
18871 regular lexer. */
18873 static bool
18874 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
18876 cp_token *pragma_tok;
18877 unsigned int id;
18879 pragma_tok = cp_lexer_consume_token (parser->lexer);
18880 gcc_assert (pragma_tok->type == CPP_PRAGMA);
18881 parser->lexer->in_pragma = true;
18883 id = pragma_tok->pragma_kind;
18884 switch (id)
18886 case PRAGMA_GCC_PCH_PREPROCESS:
18887 error ("%<#pragma GCC pch_preprocess%> must be first");
18888 break;
18890 case PRAGMA_OMP_BARRIER:
18891 switch (context)
18893 case pragma_compound:
18894 cp_parser_omp_barrier (parser, pragma_tok);
18895 return false;
18896 case pragma_stmt:
18897 error ("%<#pragma omp barrier%> may only be "
18898 "used in compound statements");
18899 break;
18900 default:
18901 goto bad_stmt;
18903 break;
18905 case PRAGMA_OMP_FLUSH:
18906 switch (context)
18908 case pragma_compound:
18909 cp_parser_omp_flush (parser, pragma_tok);
18910 return false;
18911 case pragma_stmt:
18912 error ("%<#pragma omp flush%> may only be "
18913 "used in compound statements");
18914 break;
18915 default:
18916 goto bad_stmt;
18918 break;
18920 case PRAGMA_OMP_THREADPRIVATE:
18921 cp_parser_omp_threadprivate (parser, pragma_tok);
18922 return false;
18924 case PRAGMA_OMP_ATOMIC:
18925 case PRAGMA_OMP_CRITICAL:
18926 case PRAGMA_OMP_FOR:
18927 case PRAGMA_OMP_MASTER:
18928 case PRAGMA_OMP_ORDERED:
18929 case PRAGMA_OMP_PARALLEL:
18930 case PRAGMA_OMP_SECTIONS:
18931 case PRAGMA_OMP_SINGLE:
18932 if (context == pragma_external)
18933 goto bad_stmt;
18934 cp_parser_omp_construct (parser, pragma_tok);
18935 return true;
18937 case PRAGMA_OMP_SECTION:
18938 error ("%<#pragma omp section%> may only be used in "
18939 "%<#pragma omp sections%> construct");
18940 break;
18942 default:
18943 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
18944 c_invoke_pragma_handler (id);
18945 break;
18947 bad_stmt:
18948 cp_parser_error (parser, "expected declaration specifiers");
18949 break;
18952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18953 return false;
18956 /* The interface the pragma parsers have to the lexer. */
18958 enum cpp_ttype
18959 pragma_lex (tree *value)
18961 cp_token *tok;
18962 enum cpp_ttype ret;
18964 tok = cp_lexer_peek_token (the_parser->lexer);
18966 ret = tok->type;
18967 *value = tok->value;
18969 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
18970 ret = CPP_EOF;
18971 else if (ret == CPP_STRING)
18972 *value = cp_parser_string_literal (the_parser, false, false);
18973 else
18975 cp_lexer_consume_token (the_parser->lexer);
18976 if (ret == CPP_KEYWORD)
18977 ret = CPP_NAME;
18980 return ret;
18984 /* External interface. */
18986 /* Parse one entire translation unit. */
18988 void
18989 c_parse_file (void)
18991 bool error_occurred;
18992 static bool already_called = false;
18994 if (already_called)
18996 sorry ("inter-module optimizations not implemented for C++");
18997 return;
18999 already_called = true;
19001 the_parser = cp_parser_new ();
19002 push_deferring_access_checks (flag_access_control
19003 ? dk_no_deferred : dk_no_check);
19004 error_occurred = cp_parser_translation_unit (the_parser);
19005 the_parser = NULL;
19008 /* This variable must be provided by every front end. */
19010 int yydebug;
19012 #include "gt-cp-parser.h"