2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
35 #include "diagnostic.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
64 typedef struct GTY (()) cp_token {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
155 static cp_lexer *cp_lexer_new_main
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
161 static int cp_lexer_saving_tokens
163 static cp_token_position cp_lexer_token_position
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
181 static void cp_lexer_purge_token
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
187 static void cp_lexer_commit_tokens
189 static void cp_lexer_rollback_tokens
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
259 cp_lexer_new_main (void)
261 cp_token first_token;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
544 /* Storage classes. */
550 /* Elaborated type specifiers. */
556 /* Simple type specifiers. */
570 /* GNU extensions. */
573 /* C++0x extensions. */
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
593 /* N is 1-based, not zero-based. */
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
606 if (token == lexer->last_token)
612 if (token->type != CPP_PURGED)
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
679 if (tok == lexer->last_token)
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
767 /* C++ parser token types - see "Manifest constants", above. */
770 "NESTED_NAME_SPECIFIER",
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
787 /* else fall through */
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
805 /* Start emitting debugging information. */
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
893 declarator->id_loc = UNKNOWN_LOCATION;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
950 declarator->parameter_pack_p = target->parameter_pack_p;
951 target->parameter_pack_p = false;
954 declarator->parameter_pack_p = false;
959 /* Like make_pointer_declarator -- but for references. */
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.reference.qualifiers = cv_qualifiers;
970 declarator->u.reference.rvalue_ref = rvalue_ref;
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
977 declarator->parameter_pack_p = false;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1002 declarator->parameter_pack_p = false;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1013 make_call_declarator (cp_declarator *target,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification,
1017 tree late_return_type)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 declarator->u.function.late_return_type = late_return_type;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1083 declarator = declarator->declarator;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1109 clear_decl_specs (¶meter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1120 function_declarator_p (const cp_declarator *declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1130 declarator = declarator->declarator;
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, treat user-defined type-names
1201 as non-type identifiers. */
1202 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203 /* When parsing a type-specifier, do not try to parse a class-specifier
1204 or enum-specifier. */
1205 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1208 /* This type is used for parameters and variables which hold
1209 combinations of the above flags. */
1210 typedef int cp_parser_flags;
1212 /* The different kinds of declarators we want to parse. */
1214 typedef enum cp_parser_declarator_kind
1216 /* We want an abstract declarator. */
1217 CP_PARSER_DECLARATOR_ABSTRACT,
1218 /* We want a named declarator. */
1219 CP_PARSER_DECLARATOR_NAMED,
1220 /* We don't mind, but the name must be an unqualified-id. */
1221 CP_PARSER_DECLARATOR_EITHER
1222 } cp_parser_declarator_kind;
1224 /* The precedence values used to parse binary expressions. The minimum value
1225 of PREC must be 1, because zero is reserved to quickly discriminate
1226 binary operators from other tokens. */
1231 PREC_LOGICAL_OR_EXPRESSION,
1232 PREC_LOGICAL_AND_EXPRESSION,
1233 PREC_INCLUSIVE_OR_EXPRESSION,
1234 PREC_EXCLUSIVE_OR_EXPRESSION,
1235 PREC_AND_EXPRESSION,
1236 PREC_EQUALITY_EXPRESSION,
1237 PREC_RELATIONAL_EXPRESSION,
1238 PREC_SHIFT_EXPRESSION,
1239 PREC_ADDITIVE_EXPRESSION,
1240 PREC_MULTIPLICATIVE_EXPRESSION,
1242 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1245 /* A mapping from a token type to a corresponding tree node type, with a
1246 precedence value. */
1248 typedef struct cp_parser_binary_operations_map_node
1250 /* The token type. */
1251 enum cpp_ttype token_type;
1252 /* The corresponding tree code. */
1253 enum tree_code tree_type;
1254 /* The precedence of this operator. */
1255 enum cp_parser_prec prec;
1256 } cp_parser_binary_operations_map_node;
1258 /* The status of a tentative parse. */
1260 typedef enum cp_parser_status_kind
1262 /* No errors have occurred. */
1263 CP_PARSER_STATUS_KIND_NO_ERROR,
1264 /* An error has occurred. */
1265 CP_PARSER_STATUS_KIND_ERROR,
1266 /* We are committed to this tentative parse, whether or not an error
1268 CP_PARSER_STATUS_KIND_COMMITTED
1269 } cp_parser_status_kind;
1271 typedef struct cp_parser_expression_stack_entry
1273 /* Left hand side of the binary operation we are currently
1276 /* Original tree code for left hand side, if it was a binary
1277 expression itself (used for -Wparentheses). */
1278 enum tree_code lhs_type;
1279 /* Tree code for the binary operation we are parsing. */
1280 enum tree_code tree_type;
1281 /* Precedence of the binary operation we are parsing. */
1282 enum cp_parser_prec prec;
1283 } cp_parser_expression_stack_entry;
1285 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1286 entries because precedence levels on the stack are monotonically
1288 typedef struct cp_parser_expression_stack_entry
1289 cp_parser_expression_stack[NUM_PREC_VALUES];
1291 /* Context that is saved and restored when parsing tentatively. */
1292 typedef struct GTY (()) cp_parser_context {
1293 /* If this is a tentative parsing context, the status of the
1295 enum cp_parser_status_kind status;
1296 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1297 that are looked up in this context must be looked up both in the
1298 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299 the context of the containing expression. */
1302 /* The next parsing context in the stack. */
1303 struct cp_parser_context *next;
1304 } cp_parser_context;
1308 /* Constructors and destructors. */
1310 static cp_parser_context *cp_parser_context_new
1311 (cp_parser_context *);
1313 /* Class variables. */
1315 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1317 /* The operator-precedence table used by cp_parser_binary_expression.
1318 Transformed into an associative array (binops_by_token) by
1321 static const cp_parser_binary_operations_map_node binops[] = {
1322 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1325 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1329 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1332 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1335 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1340 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1343 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1345 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1347 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1349 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1351 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1354 /* The same as binops, but initialized by cp_parser_new so that
1355 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1357 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1359 /* Constructors and destructors. */
1361 /* Construct a new context. The context below this one on the stack
1362 is given by NEXT. */
1364 static cp_parser_context *
1365 cp_parser_context_new (cp_parser_context* next)
1367 cp_parser_context *context;
1369 /* Allocate the storage. */
1370 if (cp_parser_context_free_list != NULL)
1372 /* Pull the first entry from the free list. */
1373 context = cp_parser_context_free_list;
1374 cp_parser_context_free_list = context->next;
1375 memset (context, 0, sizeof (*context));
1378 context = GGC_CNEW (cp_parser_context);
1380 /* No errors have occurred yet in this context. */
1381 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382 /* If this is not the bottommost context, copy information that we
1383 need from the previous context. */
1386 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387 expression, then we are parsing one in this context, too. */
1388 context->object_type = next->object_type;
1389 /* Thread the stack. */
1390 context->next = next;
1396 /* The cp_parser structure represents the C++ parser. */
1398 typedef struct GTY(()) cp_parser {
1399 /* The lexer from which we are obtaining tokens. */
1402 /* The scope in which names should be looked up. If NULL_TREE, then
1403 we look up names in the scope that is currently open in the
1404 source program. If non-NULL, this is either a TYPE or
1405 NAMESPACE_DECL for the scope in which we should look. It can
1406 also be ERROR_MARK, when we've parsed a bogus scope.
1408 This value is not cleared automatically after a name is looked
1409 up, so we must be careful to clear it before starting a new look
1410 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1411 will look up `Z' in the scope of `X', rather than the current
1412 scope.) Unfortunately, it is difficult to tell when name lookup
1413 is complete, because we sometimes peek at a token, look it up,
1414 and then decide not to consume it. */
1417 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418 last lookup took place. OBJECT_SCOPE is used if an expression
1419 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420 respectively. QUALIFYING_SCOPE is used for an expression of the
1421 form "X::Y"; it refers to X. */
1423 tree qualifying_scope;
1425 /* A stack of parsing contexts. All but the bottom entry on the
1426 stack will be tentative contexts.
1428 We parse tentatively in order to determine which construct is in
1429 use in some situations. For example, in order to determine
1430 whether a statement is an expression-statement or a
1431 declaration-statement we parse it tentatively as a
1432 declaration-statement. If that fails, we then reparse the same
1433 token stream as an expression-statement. */
1434 cp_parser_context *context;
1436 /* True if we are parsing GNU C++. If this flag is not set, then
1437 GNU extensions are not recognized. */
1438 bool allow_gnu_extensions_p;
1440 /* TRUE if the `>' token should be interpreted as the greater-than
1441 operator. FALSE if it is the end of a template-id or
1442 template-parameter-list. In C++0x mode, this flag also applies to
1443 `>>' tokens, which are viewed as two consecutive `>' tokens when
1444 this flag is FALSE. */
1445 bool greater_than_is_operator_p;
1447 /* TRUE if default arguments are allowed within a parameter list
1448 that starts at this point. FALSE if only a gnu extension makes
1449 them permissible. */
1450 bool default_arg_ok_p;
1452 /* TRUE if we are parsing an integral constant-expression. See
1453 [expr.const] for a precise definition. */
1454 bool integral_constant_expression_p;
1456 /* TRUE if we are parsing an integral constant-expression -- but a
1457 non-constant expression should be permitted as well. This flag
1458 is used when parsing an array bound so that GNU variable-length
1459 arrays are tolerated. */
1460 bool allow_non_integral_constant_expression_p;
1462 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463 been seen that makes the expression non-constant. */
1464 bool non_integral_constant_expression_p;
1466 /* TRUE if local variable names and `this' are forbidden in the
1468 bool local_variables_forbidden_p;
1470 /* TRUE if the declaration we are parsing is part of a
1471 linkage-specification of the form `extern string-literal
1473 bool in_unbraced_linkage_specification_p;
1475 /* TRUE if we are presently parsing a declarator, after the
1476 direct-declarator. */
1477 bool in_declarator_p;
1479 /* TRUE if we are presently parsing a template-argument-list. */
1480 bool in_template_argument_list_p;
1482 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483 to IN_OMP_BLOCK if parsing OpenMP structured block and
1484 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1485 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486 iteration-statement, OpenMP block or loop within that switch. */
1487 #define IN_SWITCH_STMT 1
1488 #define IN_ITERATION_STMT 2
1489 #define IN_OMP_BLOCK 4
1490 #define IN_OMP_FOR 8
1491 #define IN_IF_STMT 16
1492 unsigned char in_statement;
1494 /* TRUE if we are presently parsing the body of a switch statement.
1495 Note that this doesn't quite overlap with in_statement above.
1496 The difference relates to giving the right sets of error messages:
1497 "case not in switch" vs "break statement used with OpenMP...". */
1498 bool in_switch_statement_p;
1500 /* TRUE if we are parsing a type-id in an expression context. In
1501 such a situation, both "type (expr)" and "type (type)" are valid
1503 bool in_type_id_in_expr_p;
1505 /* TRUE if we are currently in a header file where declarations are
1506 implicitly extern "C". */
1507 bool implicit_extern_c;
1509 /* TRUE if strings in expressions should be translated to the execution
1511 bool translate_strings_p;
1513 /* TRUE if we are presently parsing the body of a function, but not
1515 bool in_function_body;
1517 /* If non-NULL, then we are parsing a construct where new type
1518 definitions are not permitted. The string stored here will be
1519 issued as an error message if a type is defined. */
1520 const char *type_definition_forbidden_message;
1522 /* A list of lists. The outer list is a stack, used for member
1523 functions of local classes. At each level there are two sub-list,
1524 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526 TREE_VALUE's. The functions are chained in reverse declaration
1529 The TREE_PURPOSE sublist contains those functions with default
1530 arguments that need post processing, and the TREE_VALUE sublist
1531 contains those functions with definitions that need post
1534 These lists can only be processed once the outermost class being
1535 defined is complete. */
1536 tree unparsed_functions_queues;
1538 /* The number of classes whose definitions are currently in
1540 unsigned num_classes_being_defined;
1542 /* The number of template parameter lists that apply directly to the
1543 current declaration. */
1544 unsigned num_template_parameter_lists;
1549 /* Constructors and destructors. */
1551 static cp_parser *cp_parser_new
1554 /* Routines to parse various constructs.
1556 Those that return `tree' will return the error_mark_node (rather
1557 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558 Sometimes, they will return an ordinary node if error-recovery was
1559 attempted, even though a parse error occurred. So, to check
1560 whether or not a parse error occurred, you should always use
1561 cp_parser_error_occurred. If the construct is optional (indicated
1562 either by an `_opt' in the name of the function that does the
1563 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564 the construct is not present. */
1566 /* Lexical conventions [gram.lex] */
1568 static tree cp_parser_identifier
1570 static tree cp_parser_string_literal
1571 (cp_parser *, bool, bool);
1573 /* Basic concepts [gram.basic] */
1575 static bool cp_parser_translation_unit
1578 /* Expressions [gram.expr] */
1580 static tree cp_parser_primary_expression
1581 (cp_parser *, bool, bool, bool, cp_id_kind *);
1582 static tree cp_parser_id_expression
1583 (cp_parser *, bool, bool, bool *, bool, bool);
1584 static tree cp_parser_unqualified_id
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier_opt
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_nested_name_specifier
1589 (cp_parser *, bool, bool, bool, bool);
1590 static tree cp_parser_qualifying_entity
1591 (cp_parser *, bool, bool, bool, bool, bool);
1592 static tree cp_parser_postfix_expression
1593 (cp_parser *, bool, bool, bool, cp_id_kind *);
1594 static tree cp_parser_postfix_open_square_expression
1595 (cp_parser *, tree, bool);
1596 static tree cp_parser_postfix_dot_deref_expression
1597 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599 (cp_parser *, bool, bool, bool, bool *);
1600 static void cp_parser_pseudo_destructor_name
1601 (cp_parser *, tree *, tree *);
1602 static tree cp_parser_unary_expression
1603 (cp_parser *, bool, bool, cp_id_kind *);
1604 static enum tree_code cp_parser_unary_operator
1606 static tree cp_parser_new_expression
1608 static VEC(tree,gc) *cp_parser_new_placement
1610 static tree cp_parser_new_type_id
1611 (cp_parser *, tree *);
1612 static cp_declarator *cp_parser_new_declarator_opt
1614 static cp_declarator *cp_parser_direct_new_declarator
1616 static VEC(tree,gc) *cp_parser_new_initializer
1618 static tree cp_parser_delete_expression
1620 static tree cp_parser_cast_expression
1621 (cp_parser *, bool, bool, cp_id_kind *);
1622 static tree cp_parser_binary_expression
1623 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624 static tree cp_parser_question_colon_clause
1625 (cp_parser *, tree);
1626 static tree cp_parser_assignment_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static enum tree_code cp_parser_assignment_operator_opt
1630 static tree cp_parser_expression
1631 (cp_parser *, bool, cp_id_kind *);
1632 static tree cp_parser_constant_expression
1633 (cp_parser *, bool, bool *);
1634 static tree cp_parser_builtin_offsetof
1636 static tree cp_parser_lambda_expression
1638 static void cp_parser_lambda_introducer
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_declarator_opt
1641 (cp_parser *, tree);
1642 static void cp_parser_lambda_body
1643 (cp_parser *, tree);
1645 /* Statements [gram.stmt.stmt] */
1647 static void cp_parser_statement
1648 (cp_parser *, tree, bool, bool *);
1649 static void cp_parser_label_for_labeled_statement
1651 static tree cp_parser_expression_statement
1652 (cp_parser *, tree);
1653 static tree cp_parser_compound_statement
1654 (cp_parser *, tree, bool);
1655 static void cp_parser_statement_seq_opt
1656 (cp_parser *, tree);
1657 static tree cp_parser_selection_statement
1658 (cp_parser *, bool *);
1659 static tree cp_parser_condition
1661 static tree cp_parser_iteration_statement
1663 static void cp_parser_for_init_statement
1665 static tree cp_parser_jump_statement
1667 static void cp_parser_declaration_statement
1670 static tree cp_parser_implicitly_scoped_statement
1671 (cp_parser *, bool *);
1672 static void cp_parser_already_scoped_statement
1675 /* Declarations [gram.dcl.dcl] */
1677 static void cp_parser_declaration_seq_opt
1679 static void cp_parser_declaration
1681 static void cp_parser_block_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_simple_declaration
1684 (cp_parser *, bool);
1685 static void cp_parser_decl_specifier_seq
1686 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687 static tree cp_parser_storage_class_specifier_opt
1689 static tree cp_parser_function_specifier_opt
1690 (cp_parser *, cp_decl_specifier_seq *);
1691 static tree cp_parser_type_specifier
1692 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1694 static tree cp_parser_simple_type_specifier
1695 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696 static tree cp_parser_type_name
1698 static tree cp_parser_nonclass_name
1699 (cp_parser* parser);
1700 static tree cp_parser_elaborated_type_specifier
1701 (cp_parser *, bool, bool);
1702 static tree cp_parser_enum_specifier
1704 static void cp_parser_enumerator_list
1705 (cp_parser *, tree);
1706 static void cp_parser_enumerator_definition
1707 (cp_parser *, tree);
1708 static tree cp_parser_namespace_name
1710 static void cp_parser_namespace_definition
1712 static void cp_parser_namespace_body
1714 static tree cp_parser_qualified_namespace_specifier
1716 static void cp_parser_namespace_alias_definition
1718 static bool cp_parser_using_declaration
1719 (cp_parser *, bool);
1720 static void cp_parser_using_directive
1722 static void cp_parser_asm_definition
1724 static void cp_parser_linkage_specification
1726 static void cp_parser_static_assert
1727 (cp_parser *, bool);
1728 static tree cp_parser_decltype
1731 /* Declarators [gram.dcl.decl] */
1733 static tree cp_parser_init_declarator
1734 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735 static cp_declarator *cp_parser_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737 static cp_declarator *cp_parser_direct_declarator
1738 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739 static enum tree_code cp_parser_ptr_operator
1740 (cp_parser *, tree *, cp_cv_quals *);
1741 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1743 static tree cp_parser_late_return_type_opt
1745 static tree cp_parser_declarator_id
1746 (cp_parser *, bool);
1747 static tree cp_parser_type_id
1749 static tree cp_parser_template_type_arg
1751 static tree cp_parser_trailing_type_id (cp_parser *);
1752 static tree cp_parser_type_id_1
1753 (cp_parser *, bool, bool);
1754 static void cp_parser_type_specifier_seq
1755 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756 static tree cp_parser_parameter_declaration_clause
1758 static tree cp_parser_parameter_declaration_list
1759 (cp_parser *, bool *);
1760 static cp_parameter_declarator *cp_parser_parameter_declaration
1761 (cp_parser *, bool, bool *);
1762 static tree cp_parser_default_argument
1763 (cp_parser *, bool);
1764 static void cp_parser_function_body
1766 static tree cp_parser_initializer
1767 (cp_parser *, bool *, bool *);
1768 static tree cp_parser_initializer_clause
1769 (cp_parser *, bool *);
1770 static tree cp_parser_braced_list
1771 (cp_parser*, bool*);
1772 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773 (cp_parser *, bool *);
1775 static bool cp_parser_ctor_initializer_opt_and_function_body
1778 /* Classes [gram.class] */
1780 static tree cp_parser_class_name
1781 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782 static tree cp_parser_class_specifier
1784 static tree cp_parser_class_head
1785 (cp_parser *, bool *, tree *, tree *);
1786 static enum tag_types cp_parser_class_key
1788 static void cp_parser_member_specification_opt
1790 static void cp_parser_member_declaration
1792 static tree cp_parser_pure_specifier
1794 static tree cp_parser_constant_initializer
1797 /* Derived classes [gram.class.derived] */
1799 static tree cp_parser_base_clause
1801 static tree cp_parser_base_specifier
1804 /* Special member functions [gram.special] */
1806 static tree cp_parser_conversion_function_id
1808 static tree cp_parser_conversion_type_id
1810 static cp_declarator *cp_parser_conversion_declarator_opt
1812 static bool cp_parser_ctor_initializer_opt
1814 static void cp_parser_mem_initializer_list
1816 static tree cp_parser_mem_initializer
1818 static tree cp_parser_mem_initializer_id
1821 /* Overloading [gram.over] */
1823 static tree cp_parser_operator_function_id
1825 static tree cp_parser_operator
1828 /* Templates [gram.temp] */
1830 static void cp_parser_template_declaration
1831 (cp_parser *, bool);
1832 static tree cp_parser_template_parameter_list
1834 static tree cp_parser_template_parameter
1835 (cp_parser *, bool *, bool *);
1836 static tree cp_parser_type_parameter
1837 (cp_parser *, bool *);
1838 static tree cp_parser_template_id
1839 (cp_parser *, bool, bool, bool);
1840 static tree cp_parser_template_name
1841 (cp_parser *, bool, bool, bool, bool *);
1842 static tree cp_parser_template_argument_list
1844 static tree cp_parser_template_argument
1846 static void cp_parser_explicit_instantiation
1848 static void cp_parser_explicit_specialization
1851 /* Exception handling [gram.exception] */
1853 static tree cp_parser_try_block
1855 static bool cp_parser_function_try_block
1857 static void cp_parser_handler_seq
1859 static void cp_parser_handler
1861 static tree cp_parser_exception_declaration
1863 static tree cp_parser_throw_expression
1865 static tree cp_parser_exception_specification_opt
1867 static tree cp_parser_type_id_list
1870 /* GNU Extensions */
1872 static tree cp_parser_asm_specification_opt
1874 static tree cp_parser_asm_operand_list
1876 static tree cp_parser_asm_clobber_list
1878 static tree cp_parser_asm_label_list
1880 static tree cp_parser_attributes_opt
1882 static tree cp_parser_attribute_list
1884 static bool cp_parser_extension_opt
1885 (cp_parser *, int *);
1886 static void cp_parser_label_declaration
1889 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890 static bool cp_parser_pragma
1891 (cp_parser *, enum pragma_context);
1893 /* Objective-C++ Productions */
1895 static tree cp_parser_objc_message_receiver
1897 static tree cp_parser_objc_message_args
1899 static tree cp_parser_objc_message_expression
1901 static tree cp_parser_objc_encode_expression
1903 static tree cp_parser_objc_defs_expression
1905 static tree cp_parser_objc_protocol_expression
1907 static tree cp_parser_objc_selector_expression
1909 static tree cp_parser_objc_expression
1911 static bool cp_parser_objc_selector_p
1913 static tree cp_parser_objc_selector
1915 static tree cp_parser_objc_protocol_refs_opt
1917 static void cp_parser_objc_declaration
1919 static tree cp_parser_objc_statement
1922 /* Utility Routines */
1924 static tree cp_parser_lookup_name
1925 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926 static tree cp_parser_lookup_name_simple
1927 (cp_parser *, tree, location_t);
1928 static tree cp_parser_maybe_treat_template_as_class
1930 static bool cp_parser_check_declarator_template_parameters
1931 (cp_parser *, cp_declarator *, location_t);
1932 static bool cp_parser_check_template_parameters
1933 (cp_parser *, unsigned, location_t, cp_declarator *);
1934 static tree cp_parser_simple_cast_expression
1936 static tree cp_parser_global_scope_opt
1937 (cp_parser *, bool);
1938 static bool cp_parser_constructor_declarator_p
1939 (cp_parser *, bool);
1940 static tree cp_parser_function_definition_from_specifiers_and_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942 static tree cp_parser_function_definition_after_declarator
1943 (cp_parser *, bool);
1944 static void cp_parser_template_declaration_after_export
1945 (cp_parser *, bool);
1946 static void cp_parser_perform_template_parameter_access_checks
1947 (VEC (deferred_access_check,gc)*);
1948 static tree cp_parser_single_declaration
1949 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950 static tree cp_parser_functional_cast
1951 (cp_parser *, tree);
1952 static tree cp_parser_save_member_function_body
1953 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954 static tree cp_parser_enclosed_template_argument_list
1956 static void cp_parser_save_default_args
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_for_member
1959 (cp_parser *, tree);
1960 static void cp_parser_late_parsing_default_args
1961 (cp_parser *, tree);
1962 static tree cp_parser_sizeof_operand
1963 (cp_parser *, enum rid);
1964 static tree cp_parser_trait_expr
1965 (cp_parser *, enum rid);
1966 static bool cp_parser_declares_only_class_p
1968 static void cp_parser_set_storage_class
1969 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970 static void cp_parser_set_decl_spec_type
1971 (cp_decl_specifier_seq *, tree, location_t, bool);
1972 static bool cp_parser_friend_p
1973 (const cp_decl_specifier_seq *);
1974 static cp_token *cp_parser_require
1975 (cp_parser *, enum cpp_ttype, const char *);
1976 static cp_token *cp_parser_require_keyword
1977 (cp_parser *, enum rid, const char *);
1978 static bool cp_parser_token_starts_function_definition_p
1980 static bool cp_parser_next_token_starts_class_definition_p
1982 static bool cp_parser_next_token_ends_template_argument_p
1984 static bool cp_parser_nth_token_starts_template_argument_list_p
1985 (cp_parser *, size_t);
1986 static enum tag_types cp_parser_token_is_class_key
1988 static void cp_parser_check_class_key
1989 (enum tag_types, tree type);
1990 static void cp_parser_check_access_in_redeclaration
1991 (tree type, location_t location);
1992 static bool cp_parser_optional_template_keyword
1994 static void cp_parser_pre_parsed_nested_name_specifier
1996 static bool cp_parser_cache_group
1997 (cp_parser *, enum cpp_ttype, unsigned);
1998 static void cp_parser_parse_tentatively
2000 static void cp_parser_commit_to_tentative_parse
2002 static void cp_parser_abort_tentative_parse
2004 static bool cp_parser_parse_definitely
2006 static inline bool cp_parser_parsing_tentatively
2008 static bool cp_parser_uncommitted_to_tentative_parse_p
2010 static void cp_parser_error
2011 (cp_parser *, const char *);
2012 static void cp_parser_name_lookup_error
2013 (cp_parser *, tree, tree, const char *, location_t);
2014 static bool cp_parser_simulate_error
2016 static bool cp_parser_check_type_definition
2018 static void cp_parser_check_for_definition_in_return_type
2019 (cp_declarator *, tree, location_t type_location);
2020 static void cp_parser_check_for_invalid_template_id
2021 (cp_parser *, tree, location_t location);
2022 static bool cp_parser_non_integral_constant_expression
2023 (cp_parser *, const char *);
2024 static void cp_parser_diagnose_invalid_type_name
2025 (cp_parser *, tree, tree, location_t);
2026 static bool cp_parser_parse_and_diagnose_invalid_type_name
2028 static int cp_parser_skip_to_closing_parenthesis
2029 (cp_parser *, bool, bool, bool);
2030 static void cp_parser_skip_to_end_of_statement
2032 static void cp_parser_consume_semicolon_at_end_of_statement
2034 static void cp_parser_skip_to_end_of_block_or_statement
2036 static bool cp_parser_skip_to_closing_brace
2038 static void cp_parser_skip_to_end_of_template_parameter_list
2040 static void cp_parser_skip_to_pragma_eol
2041 (cp_parser*, cp_token *);
2042 static bool cp_parser_error_occurred
2044 static bool cp_parser_allow_gnu_extensions_p
2046 static bool cp_parser_is_string_literal
2048 static bool cp_parser_is_keyword
2049 (cp_token *, enum rid);
2050 static tree cp_parser_make_typename_type
2051 (cp_parser *, tree, tree, location_t location);
2052 static cp_declarator * cp_parser_make_indirect_declarator
2053 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2055 /* Returns nonzero if we are parsing tentatively. */
2058 cp_parser_parsing_tentatively (cp_parser* parser)
2060 return parser->context->next != NULL;
2063 /* Returns nonzero if TOKEN is a string literal. */
2066 cp_parser_is_string_literal (cp_token* token)
2068 return (token->type == CPP_STRING ||
2069 token->type == CPP_STRING16 ||
2070 token->type == CPP_STRING32 ||
2071 token->type == CPP_WSTRING ||
2072 token->type == CPP_UTF8STRING);
2075 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2078 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2080 return token->keyword == keyword;
2083 /* If not parsing tentatively, issue a diagnostic of the form
2084 FILE:LINE: MESSAGE before TOKEN
2085 where TOKEN is the next token in the input stream. MESSAGE
2086 (specified by the caller) is usually of the form "expected
2090 cp_parser_error (cp_parser* parser, const char* message)
2092 if (!cp_parser_simulate_error (parser))
2094 cp_token *token = cp_lexer_peek_token (parser->lexer);
2095 /* This diagnostic makes more sense if it is tagged to the line
2096 of the token we just peeked at. */
2097 cp_lexer_set_source_position_from_token (token);
2099 if (token->type == CPP_PRAGMA)
2101 error_at (token->location,
2102 "%<#pragma%> is not allowed here");
2103 cp_parser_skip_to_pragma_eol (parser, token);
2107 c_parse_error (message,
2108 /* Because c_parser_error does not understand
2109 CPP_KEYWORD, keywords are treated like
2111 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112 token->u.value, token->flags);
2116 /* Issue an error about name-lookup failing. NAME is the
2117 IDENTIFIER_NODE DECL is the result of
2118 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2119 the thing that we hoped to find. */
2122 cp_parser_name_lookup_error (cp_parser* parser,
2125 const char* desired,
2126 location_t location)
2128 /* If name lookup completely failed, tell the user that NAME was not
2130 if (decl == error_mark_node)
2132 if (parser->scope && parser->scope != global_namespace)
2133 error_at (location, "%<%E::%E%> has not been declared",
2134 parser->scope, name);
2135 else if (parser->scope == global_namespace)
2136 error_at (location, "%<::%E%> has not been declared", name);
2137 else if (parser->object_scope
2138 && !CLASS_TYPE_P (parser->object_scope))
2139 error_at (location, "request for member %qE in non-class type %qT",
2140 name, parser->object_scope);
2141 else if (parser->object_scope)
2142 error_at (location, "%<%T::%E%> has not been declared",
2143 parser->object_scope, name);
2145 error_at (location, "%qE has not been declared", name);
2147 else if (parser->scope && parser->scope != global_namespace)
2148 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149 else if (parser->scope == global_namespace)
2150 error_at (location, "%<::%E%> %s", name, desired);
2152 error_at (location, "%qE %s", name, desired);
2155 /* If we are parsing tentatively, remember that an error has occurred
2156 during this tentative parse. Returns true if the error was
2157 simulated; false if a message should be issued by the caller. */
2160 cp_parser_simulate_error (cp_parser* parser)
2162 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2164 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2170 /* Check for repeated decl-specifiers. */
2173 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174 location_t location)
2178 for (ds = ds_first; ds != ds_last; ++ds)
2180 unsigned count = decl_specs->specs[ds];
2183 /* The "long" specifier is a special case because of "long long". */
2187 error_at (location, "%<long long long%> is too long for GCC");
2189 pedwarn_cxx98 (location, OPT_Wlong_long,
2190 "ISO C++ 1998 does not support %<long long%>");
2194 static const char *const decl_spec_names[] = {
2211 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2216 /* This function is called when a type is defined. If type
2217 definitions are forbidden at this point, an error message is
2221 cp_parser_check_type_definition (cp_parser* parser)
2223 /* If types are forbidden here, issue a message. */
2224 if (parser->type_definition_forbidden_message)
2226 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227 in the message need to be interpreted. */
2228 error (parser->type_definition_forbidden_message);
2234 /* This function is called when the DECLARATOR is processed. The TYPE
2235 was a type defined in the decl-specifiers. If it is invalid to
2236 define a type in the decl-specifiers for DECLARATOR, an error is
2237 issued. TYPE_LOCATION is the location of TYPE and is used
2238 for error reporting. */
2241 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242 tree type, location_t type_location)
2244 /* [dcl.fct] forbids type definitions in return types.
2245 Unfortunately, it's not easy to know whether or not we are
2246 processing a return type until after the fact. */
2248 && (declarator->kind == cdk_pointer
2249 || declarator->kind == cdk_reference
2250 || declarator->kind == cdk_ptrmem))
2251 declarator = declarator->declarator;
2253 && declarator->kind == cdk_function)
2255 error_at (type_location,
2256 "new types may not be defined in a return type");
2257 inform (type_location,
2258 "(perhaps a semicolon is missing after the definition of %qT)",
2263 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2264 "<" in any valid C++ program. If the next token is indeed "<",
2265 issue a message warning the user about what appears to be an
2266 invalid attempt to form a template-id. LOCATION is the location
2267 of the type-specifier (TYPE) */
2270 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271 tree type, location_t location)
2273 cp_token_position start = 0;
2275 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2278 error_at (location, "%qT is not a template", type);
2279 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280 error_at (location, "%qE is not a template", type);
2282 error_at (location, "invalid template-id");
2283 /* Remember the location of the invalid "<". */
2284 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285 start = cp_lexer_token_position (parser->lexer, true);
2286 /* Consume the "<". */
2287 cp_lexer_consume_token (parser->lexer);
2288 /* Parse the template arguments. */
2289 cp_parser_enclosed_template_argument_list (parser);
2290 /* Permanently remove the invalid template arguments so that
2291 this error message is not issued again. */
2293 cp_lexer_purge_tokens_after (parser->lexer, start);
2297 /* If parsing an integral constant-expression, issue an error message
2298 about the fact that THING appeared and return true. Otherwise,
2299 return false. In either case, set
2300 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2303 cp_parser_non_integral_constant_expression (cp_parser *parser,
2306 parser->non_integral_constant_expression_p = true;
2307 if (parser->integral_constant_expression_p)
2309 if (!parser->allow_non_integral_constant_expression_p)
2311 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312 in the message need to be interpreted. */
2313 char *message = concat (thing,
2314 " cannot appear in a constant-expression",
2324 /* Emit a diagnostic for an invalid type name. SCOPE is the
2325 qualifying scope (or NULL, if none) for ID. This function commits
2326 to the current active tentative parse, if any. (Otherwise, the
2327 problematic construct might be encountered again later, resulting
2328 in duplicate error messages.) LOCATION is the location of ID. */
2331 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332 tree scope, tree id,
2333 location_t location)
2335 tree decl, old_scope;
2336 /* Try to lookup the identifier. */
2337 old_scope = parser->scope;
2338 parser->scope = scope;
2339 decl = cp_parser_lookup_name_simple (parser, id, location);
2340 parser->scope = old_scope;
2341 /* If the lookup found a template-name, it means that the user forgot
2342 to specify an argument list. Emit a useful error message. */
2343 if (TREE_CODE (decl) == TEMPLATE_DECL)
2345 "invalid use of template-name %qE without an argument list",
2347 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348 error_at (location, "invalid use of destructor %qD as a type", id);
2349 else if (TREE_CODE (decl) == TYPE_DECL)
2350 /* Something like 'unsigned A a;' */
2351 error_at (location, "invalid combination of multiple type-specifiers");
2352 else if (!parser->scope)
2354 /* Issue an error message. */
2355 error_at (location, "%qE does not name a type", id);
2356 /* If we're in a template class, it's possible that the user was
2357 referring to a type from a base class. For example:
2359 template <typename T> struct A { typedef T X; };
2360 template <typename T> struct B : public A<T> { X x; };
2362 The user should have said "typename A<T>::X". */
2363 if (processing_template_decl && current_class_type
2364 && TYPE_BINFO (current_class_type))
2368 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2372 tree base_type = BINFO_TYPE (b);
2373 if (CLASS_TYPE_P (base_type)
2374 && dependent_type_p (base_type))
2377 /* Go from a particular instantiation of the
2378 template (which will have an empty TYPE_FIELDs),
2379 to the main version. */
2380 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381 for (field = TYPE_FIELDS (base_type);
2383 field = TREE_CHAIN (field))
2384 if (TREE_CODE (field) == TYPE_DECL
2385 && DECL_NAME (field) == id)
2388 "(perhaps %<typename %T::%E%> was intended)",
2389 BINFO_TYPE (b), id);
2398 /* Here we diagnose qualified-ids where the scope is actually correct,
2399 but the identifier does not resolve to a valid type name. */
2400 else if (parser->scope != error_mark_node)
2402 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403 error_at (location, "%qE in namespace %qE does not name a type",
2405 else if (CLASS_TYPE_P (parser->scope)
2406 && constructor_name_p (id, parser->scope))
2409 error_at (location, "%<%T::%E%> names the constructor, not"
2410 " the type", parser->scope, id);
2411 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412 error_at (location, "and %qT has no template constructors",
2415 else if (TYPE_P (parser->scope)
2416 && dependent_scope_p (parser->scope))
2417 error_at (location, "need %<typename%> before %<%T::%E%> because "
2418 "%qT is a dependent scope",
2419 parser->scope, id, parser->scope);
2420 else if (TYPE_P (parser->scope))
2421 error_at (location, "%qE in class %qT does not name a type",
2426 cp_parser_commit_to_tentative_parse (parser);
2429 /* Check for a common situation where a type-name should be present,
2430 but is not, and issue a sensible error message. Returns true if an
2431 invalid type-name was detected.
2433 The situation handled by this function are variable declarations of the
2434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435 Usually, `ID' should name a type, but if we got here it means that it
2436 does not. We try to emit the best possible error message depending on
2437 how exactly the id-expression looks like. */
2440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2443 cp_token *token = cp_lexer_peek_token (parser->lexer);
2445 /* Avoid duplicate error about ambiguous lookup. */
2446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449 if (next->type == CPP_NAME && next->ambiguous_p)
2453 cp_parser_parse_tentatively (parser);
2454 id = cp_parser_id_expression (parser,
2455 /*template_keyword_p=*/false,
2456 /*check_dependency_p=*/true,
2457 /*template_p=*/NULL,
2458 /*declarator_p=*/true,
2459 /*optional_p=*/false);
2460 /* If the next token is a (, this is a function with no explicit return
2461 type, i.e. constructor, destructor or conversion op. */
2462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463 || TREE_CODE (id) == TYPE_DECL)
2465 cp_parser_abort_tentative_parse (parser);
2468 if (!cp_parser_parse_definitely (parser))
2471 /* Emit a diagnostic for the invalid type. */
2472 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473 id, token->location);
2475 /* If we aren't in the middle of a declarator (i.e. in a
2476 parameter-declaration-clause), skip to the end of the declaration;
2477 there's no point in trying to process it. */
2478 if (!parser->in_declarator_p)
2479 cp_parser_skip_to_end_of_block_or_statement (parser);
2483 /* Consume tokens up to, and including, the next non-nested closing `)'.
2484 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2485 are doing error recovery. Returns -1 if OR_COMMA is true and we
2486 found an unnested comma. */
2489 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2494 unsigned paren_depth = 0;
2495 unsigned brace_depth = 0;
2496 unsigned square_depth = 0;
2498 if (recovering && !or_comma
2499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2504 cp_token * token = cp_lexer_peek_token (parser->lexer);
2506 switch (token->type)
2509 case CPP_PRAGMA_EOL:
2510 /* If we've run out of tokens, then there is no closing `)'. */
2513 /* This is good for lambda expression capture-lists. */
2514 case CPP_OPEN_SQUARE:
2517 case CPP_CLOSE_SQUARE:
2518 if (!square_depth--)
2523 /* This matches the processing in skip_to_end_of_statement. */
2528 case CPP_OPEN_BRACE:
2531 case CPP_CLOSE_BRACE:
2537 if (recovering && or_comma && !brace_depth && !paren_depth
2542 case CPP_OPEN_PAREN:
2547 case CPP_CLOSE_PAREN:
2548 if (!brace_depth && !paren_depth--)
2551 cp_lexer_consume_token (parser->lexer);
2560 /* Consume the token. */
2561 cp_lexer_consume_token (parser->lexer);
2565 /* Consume tokens until we reach the end of the current statement.
2566 Normally, that will be just before consuming a `;'. However, if a
2567 non-nested `}' comes first, then we stop before consuming that. */
2570 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2572 unsigned nesting_depth = 0;
2576 cp_token *token = cp_lexer_peek_token (parser->lexer);
2578 switch (token->type)
2581 case CPP_PRAGMA_EOL:
2582 /* If we've run out of tokens, stop. */
2586 /* If the next token is a `;', we have reached the end of the
2592 case CPP_CLOSE_BRACE:
2593 /* If this is a non-nested '}', stop before consuming it.
2594 That way, when confronted with something like:
2598 we stop before consuming the closing '}', even though we
2599 have not yet reached a `;'. */
2600 if (nesting_depth == 0)
2603 /* If it is the closing '}' for a block that we have
2604 scanned, stop -- but only after consuming the token.
2610 we will stop after the body of the erroneously declared
2611 function, but before consuming the following `typedef'
2613 if (--nesting_depth == 0)
2615 cp_lexer_consume_token (parser->lexer);
2619 case CPP_OPEN_BRACE:
2627 /* Consume the token. */
2628 cp_lexer_consume_token (parser->lexer);
2632 /* This function is called at the end of a statement or declaration.
2633 If the next token is a semicolon, it is consumed; otherwise, error
2634 recovery is attempted. */
2637 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2639 /* Look for the trailing `;'. */
2640 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2642 /* If there is additional (erroneous) input, skip to the end of
2644 cp_parser_skip_to_end_of_statement (parser);
2645 /* If the next token is now a `;', consume it. */
2646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647 cp_lexer_consume_token (parser->lexer);
2651 /* Skip tokens until we have consumed an entire block, or until we
2652 have consumed a non-nested `;'. */
2655 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2657 int nesting_depth = 0;
2659 while (nesting_depth >= 0)
2661 cp_token *token = cp_lexer_peek_token (parser->lexer);
2663 switch (token->type)
2666 case CPP_PRAGMA_EOL:
2667 /* If we've run out of tokens, stop. */
2671 /* Stop if this is an unnested ';'. */
2676 case CPP_CLOSE_BRACE:
2677 /* Stop if this is an unnested '}', or closes the outermost
2680 if (nesting_depth < 0)
2686 case CPP_OPEN_BRACE:
2695 /* Consume the token. */
2696 cp_lexer_consume_token (parser->lexer);
2700 /* Skip tokens until a non-nested closing curly brace is the next
2701 token, or there are no more tokens. Return true in the first case,
2705 cp_parser_skip_to_closing_brace (cp_parser *parser)
2707 unsigned nesting_depth = 0;
2711 cp_token *token = cp_lexer_peek_token (parser->lexer);
2713 switch (token->type)
2716 case CPP_PRAGMA_EOL:
2717 /* If we've run out of tokens, stop. */
2720 case CPP_CLOSE_BRACE:
2721 /* If the next token is a non-nested `}', then we have reached
2722 the end of the current block. */
2723 if (nesting_depth-- == 0)
2727 case CPP_OPEN_BRACE:
2728 /* If it the next token is a `{', then we are entering a new
2729 block. Consume the entire block. */
2737 /* Consume the token. */
2738 cp_lexer_consume_token (parser->lexer);
2742 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2743 parameter is the PRAGMA token, allowing us to purge the entire pragma
2747 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2751 parser->lexer->in_pragma = false;
2754 token = cp_lexer_consume_token (parser->lexer);
2755 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2757 /* Ensure that the pragma is not parsed again. */
2758 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2761 /* Require pragma end of line, resyncing with it as necessary. The
2762 arguments are as for cp_parser_skip_to_pragma_eol. */
2765 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2767 parser->lexer->in_pragma = false;
2768 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2772 /* This is a simple wrapper around make_typename_type. When the id is
2773 an unresolved identifier node, we can provide a superior diagnostic
2774 using cp_parser_diagnose_invalid_type_name. */
2777 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778 tree id, location_t id_location)
2781 if (TREE_CODE (id) == IDENTIFIER_NODE)
2783 result = make_typename_type (scope, id, typename_type,
2784 /*complain=*/tf_none);
2785 if (result == error_mark_node)
2786 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2789 return make_typename_type (scope, id, typename_type, tf_error);
2792 /* This is a wrapper around the
2793 make_{pointer,ptrmem,reference}_declarator functions that decides
2794 which one to call based on the CODE and CLASS_TYPE arguments. The
2795 CODE argument should be one of the values returned by
2796 cp_parser_ptr_operator. */
2797 static cp_declarator *
2798 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799 cp_cv_quals cv_qualifiers,
2800 cp_declarator *target)
2802 if (code == ERROR_MARK)
2803 return cp_error_declarator;
2805 if (code == INDIRECT_REF)
2806 if (class_type == NULL_TREE)
2807 return make_pointer_declarator (cv_qualifiers, target);
2809 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811 return make_reference_declarator (cv_qualifiers, target, false);
2812 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813 return make_reference_declarator (cv_qualifiers, target, true);
2817 /* Create a new C++ parser. */
2820 cp_parser_new (void)
2826 /* cp_lexer_new_main is called before calling ggc_alloc because
2827 cp_lexer_new_main might load a PCH file. */
2828 lexer = cp_lexer_new_main ();
2830 /* Initialize the binops_by_token so that we can get the tree
2831 directly from the token. */
2832 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833 binops_by_token[binops[i].token_type] = binops[i];
2835 parser = GGC_CNEW (cp_parser);
2836 parser->lexer = lexer;
2837 parser->context = cp_parser_context_new (NULL);
2839 /* For now, we always accept GNU extensions. */
2840 parser->allow_gnu_extensions_p = 1;
2842 /* The `>' token is a greater-than operator, not the end of a
2844 parser->greater_than_is_operator_p = true;
2846 parser->default_arg_ok_p = true;
2848 /* We are not parsing a constant-expression. */
2849 parser->integral_constant_expression_p = false;
2850 parser->allow_non_integral_constant_expression_p = false;
2851 parser->non_integral_constant_expression_p = false;
2853 /* Local variable names are not forbidden. */
2854 parser->local_variables_forbidden_p = false;
2856 /* We are not processing an `extern "C"' declaration. */
2857 parser->in_unbraced_linkage_specification_p = false;
2859 /* We are not processing a declarator. */
2860 parser->in_declarator_p = false;
2862 /* We are not processing a template-argument-list. */
2863 parser->in_template_argument_list_p = false;
2865 /* We are not in an iteration statement. */
2866 parser->in_statement = 0;
2868 /* We are not in a switch statement. */
2869 parser->in_switch_statement_p = false;
2871 /* We are not parsing a type-id inside an expression. */
2872 parser->in_type_id_in_expr_p = false;
2874 /* Declarations aren't implicitly extern "C". */
2875 parser->implicit_extern_c = false;
2877 /* String literals should be translated to the execution character set. */
2878 parser->translate_strings_p = true;
2880 /* We are not parsing a function body. */
2881 parser->in_function_body = false;
2883 /* The unparsed function queue is empty. */
2884 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2886 /* There are no classes being defined. */
2887 parser->num_classes_being_defined = 0;
2889 /* No template parameters apply. */
2890 parser->num_template_parameter_lists = 0;
2895 /* Create a cp_lexer structure which will emit the tokens in CACHE
2896 and push it onto the parser's lexer stack. This is used for delayed
2897 parsing of in-class method bodies and default arguments, and should
2898 not be confused with tentative parsing. */
2900 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2902 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903 lexer->next = parser->lexer;
2904 parser->lexer = lexer;
2906 /* Move the current source position to that of the first token in the
2908 cp_lexer_set_source_position_from_token (lexer->next_token);
2911 /* Pop the top lexer off the parser stack. This is never used for the
2912 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2914 cp_parser_pop_lexer (cp_parser *parser)
2916 cp_lexer *lexer = parser->lexer;
2917 parser->lexer = lexer->next;
2918 cp_lexer_destroy (lexer);
2920 /* Put the current source position back where it was before this
2921 lexer was pushed. */
2922 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2925 /* Lexical conventions [gram.lex] */
2927 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2931 cp_parser_identifier (cp_parser* parser)
2935 /* Look for the identifier. */
2936 token = cp_parser_require (parser, CPP_NAME, "identifier");
2937 /* Return the value. */
2938 return token ? token->u.value : error_mark_node;
2941 /* Parse a sequence of adjacent string constants. Returns a
2942 TREE_STRING representing the combined, nul-terminated string
2943 constant. If TRANSLATE is true, translate the string to the
2944 execution character set. If WIDE_OK is true, a wide string is
2947 C++98 [lex.string] says that if a narrow string literal token is
2948 adjacent to a wide string literal token, the behavior is undefined.
2949 However, C99 6.4.5p4 says that this results in a wide string literal.
2950 We follow C99 here, for consistency with the C front end.
2952 This code is largely lifted from lex_string() in c-lex.c.
2954 FUTURE: ObjC++ will need to handle @-strings here. */
2956 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2960 struct obstack str_ob;
2961 cpp_string str, istr, *strs;
2963 enum cpp_ttype type;
2965 tok = cp_lexer_peek_token (parser->lexer);
2966 if (!cp_parser_is_string_literal (tok))
2968 cp_parser_error (parser, "expected string-literal");
2969 return error_mark_node;
2974 /* Try to avoid the overhead of creating and destroying an obstack
2975 for the common case of just one string. */
2976 if (!cp_parser_is_string_literal
2977 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2979 cp_lexer_consume_token (parser->lexer);
2981 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982 str.len = TREE_STRING_LENGTH (tok->u.value);
2989 gcc_obstack_init (&str_ob);
2994 cp_lexer_consume_token (parser->lexer);
2996 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997 str.len = TREE_STRING_LENGTH (tok->u.value);
2999 if (type != tok->type)
3001 if (type == CPP_STRING)
3003 else if (tok->type != CPP_STRING)
3004 error_at (tok->location,
3005 "unsupported non-standard concatenation "
3006 "of string literals");
3009 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3011 tok = cp_lexer_peek_token (parser->lexer);
3013 while (cp_parser_is_string_literal (tok));
3015 strs = (cpp_string *) obstack_finish (&str_ob);
3018 if (type != CPP_STRING && !wide_ok)
3020 cp_parser_error (parser, "a wide string is invalid in this context");
3024 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025 (parse_in, strs, count, &istr, type))
3027 value = build_string (istr.len, (const char *)istr.text);
3028 free (CONST_CAST (unsigned char *, istr.text));
3034 case CPP_UTF8STRING:
3035 TREE_TYPE (value) = char_array_type_node;
3038 TREE_TYPE (value) = char16_array_type_node;
3041 TREE_TYPE (value) = char32_array_type_node;
3044 TREE_TYPE (value) = wchar_array_type_node;
3048 value = fix_string_type (value);
3051 /* cpp_interpret_string has issued an error. */
3052 value = error_mark_node;
3055 obstack_free (&str_ob, 0);
3061 /* Basic concepts [gram.basic] */
3063 /* Parse a translation-unit.
3066 declaration-seq [opt]
3068 Returns TRUE if all went well. */
3071 cp_parser_translation_unit (cp_parser* parser)
3073 /* The address of the first non-permanent object on the declarator
3075 static void *declarator_obstack_base;
3079 /* Create the declarator obstack, if necessary. */
3080 if (!cp_error_declarator)
3082 gcc_obstack_init (&declarator_obstack);
3083 /* Create the error declarator. */
3084 cp_error_declarator = make_declarator (cdk_error);
3085 /* Create the empty parameter list. */
3086 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087 /* Remember where the base of the declarator obstack lies. */
3088 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3091 cp_parser_declaration_seq_opt (parser);
3093 /* If there are no tokens left then all went well. */
3094 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3096 /* Get rid of the token array; we don't need it any more. */
3097 cp_lexer_destroy (parser->lexer);
3098 parser->lexer = NULL;
3100 /* This file might have been a context that's implicitly extern
3101 "C". If so, pop the lang context. (Only relevant for PCH.) */
3102 if (parser->implicit_extern_c)
3104 pop_lang_context ();
3105 parser->implicit_extern_c = false;
3109 finish_translation_unit ();
3115 cp_parser_error (parser, "expected declaration");
3119 /* Make sure the declarator obstack was fully cleaned up. */
3120 gcc_assert (obstack_next_free (&declarator_obstack)
3121 == declarator_obstack_base);
3123 /* All went well. */
3127 /* Expressions [gram.expr] */
3129 /* Parse a primary-expression.
3140 ( compound-statement )
3141 __builtin_va_arg ( assignment-expression , type-id )
3142 __builtin_offsetof ( type-id , offsetof-expression )
3145 __has_nothrow_assign ( type-id )
3146 __has_nothrow_constructor ( type-id )
3147 __has_nothrow_copy ( type-id )
3148 __has_trivial_assign ( type-id )
3149 __has_trivial_constructor ( type-id )
3150 __has_trivial_copy ( type-id )
3151 __has_trivial_destructor ( type-id )
3152 __has_virtual_destructor ( type-id )
3153 __is_abstract ( type-id )
3154 __is_base_of ( type-id , type-id )
3155 __is_class ( type-id )
3156 __is_convertible_to ( type-id , type-id )
3157 __is_empty ( type-id )
3158 __is_enum ( type-id )
3159 __is_pod ( type-id )
3160 __is_polymorphic ( type-id )
3161 __is_union ( type-id )
3163 Objective-C++ Extension:
3171 ADDRESS_P is true iff this expression was immediately preceded by
3172 "&" and therefore might denote a pointer-to-member. CAST_P is true
3173 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3174 true iff this expression is a template argument.
3176 Returns a representation of the expression. Upon return, *IDK
3177 indicates what kind of id-expression (if any) was present. */
3180 cp_parser_primary_expression (cp_parser *parser,
3183 bool template_arg_p,
3186 cp_token *token = NULL;
3188 /* Assume the primary expression is not an id-expression. */
3189 *idk = CP_ID_KIND_NONE;
3191 /* Peek at the next token. */
3192 token = cp_lexer_peek_token (parser->lexer);
3193 switch (token->type)
3206 token = cp_lexer_consume_token (parser->lexer);
3207 if (TREE_CODE (token->u.value) == FIXED_CST)
3209 error_at (token->location,
3210 "fixed-point types not supported in C++");
3211 return error_mark_node;
3213 /* Floating-point literals are only allowed in an integral
3214 constant expression if they are cast to an integral or
3215 enumeration type. */
3216 if (TREE_CODE (token->u.value) == REAL_CST
3217 && parser->integral_constant_expression_p
3220 /* CAST_P will be set even in invalid code like "int(2.7 +
3221 ...)". Therefore, we have to check that the next token
3222 is sure to end the cast. */
3225 cp_token *next_token;
3227 next_token = cp_lexer_peek_token (parser->lexer);
3228 if (/* The comma at the end of an
3229 enumerator-definition. */
3230 next_token->type != CPP_COMMA
3231 /* The curly brace at the end of an enum-specifier. */
3232 && next_token->type != CPP_CLOSE_BRACE
3233 /* The end of a statement. */
3234 && next_token->type != CPP_SEMICOLON
3235 /* The end of the cast-expression. */
3236 && next_token->type != CPP_CLOSE_PAREN
3237 /* The end of an array bound. */
3238 && next_token->type != CPP_CLOSE_SQUARE
3239 /* The closing ">" in a template-argument-list. */
3240 && (next_token->type != CPP_GREATER
3241 || parser->greater_than_is_operator_p)
3242 /* C++0x only: A ">>" treated like two ">" tokens,
3243 in a template-argument-list. */
3244 && (next_token->type != CPP_RSHIFT
3245 || (cxx_dialect == cxx98)
3246 || parser->greater_than_is_operator_p))
3250 /* If we are within a cast, then the constraint that the
3251 cast is to an integral or enumeration type will be
3252 checked at that point. If we are not within a cast, then
3253 this code is invalid. */
3255 cp_parser_non_integral_constant_expression
3256 (parser, "floating-point literal");
3258 return token->u.value;
3264 case CPP_UTF8STRING:
3265 /* ??? Should wide strings be allowed when parser->translate_strings_p
3266 is false (i.e. in attributes)? If not, we can kill the third
3267 argument to cp_parser_string_literal. */
3268 return cp_parser_string_literal (parser,
3269 parser->translate_strings_p,
3272 case CPP_OPEN_PAREN:
3275 bool saved_greater_than_is_operator_p;
3277 /* Consume the `('. */
3278 cp_lexer_consume_token (parser->lexer);
3279 /* Within a parenthesized expression, a `>' token is always
3280 the greater-than operator. */
3281 saved_greater_than_is_operator_p
3282 = parser->greater_than_is_operator_p;
3283 parser->greater_than_is_operator_p = true;
3284 /* If we see `( { ' then we are looking at the beginning of
3285 a GNU statement-expression. */
3286 if (cp_parser_allow_gnu_extensions_p (parser)
3287 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3289 /* Statement-expressions are not allowed by the standard. */
3290 pedwarn (token->location, OPT_pedantic,
3291 "ISO C++ forbids braced-groups within expressions");
3293 /* And they're not allowed outside of a function-body; you
3294 cannot, for example, write:
3296 int i = ({ int j = 3; j + 1; });
3298 at class or namespace scope. */
3299 if (!parser->in_function_body
3300 || parser->in_template_argument_list_p)
3302 error_at (token->location,
3303 "statement-expressions are not allowed outside "
3304 "functions nor in template-argument lists");
3305 cp_parser_skip_to_end_of_block_or_statement (parser);
3306 expr = error_mark_node;
3310 /* Start the statement-expression. */
3311 expr = begin_stmt_expr ();
3312 /* Parse the compound-statement. */
3313 cp_parser_compound_statement (parser, expr, false);
3315 expr = finish_stmt_expr (expr, false);
3320 /* Parse the parenthesized expression. */
3321 expr = cp_parser_expression (parser, cast_p, idk);
3322 /* Let the front end know that this expression was
3323 enclosed in parentheses. This matters in case, for
3324 example, the expression is of the form `A::B', since
3325 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3327 finish_parenthesized_expr (expr);
3329 /* The `>' token might be the end of a template-id or
3330 template-parameter-list now. */
3331 parser->greater_than_is_operator_p
3332 = saved_greater_than_is_operator_p;
3333 /* Consume the `)'. */
3334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3335 cp_parser_skip_to_end_of_statement (parser);
3340 case CPP_OPEN_SQUARE:
3341 if (c_dialect_objc ())
3342 /* We have an Objective-C++ message. */
3343 return cp_parser_objc_expression (parser);
3344 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3345 return cp_parser_lambda_expression (parser);
3347 case CPP_OBJC_STRING:
3348 if (c_dialect_objc ())
3349 /* We have an Objective-C++ string literal. */
3350 return cp_parser_objc_expression (parser);
3351 cp_parser_error (parser, "expected primary-expression");
3352 return error_mark_node;
3355 switch (token->keyword)
3357 /* These two are the boolean literals. */
3359 cp_lexer_consume_token (parser->lexer);
3360 return boolean_true_node;
3362 cp_lexer_consume_token (parser->lexer);
3363 return boolean_false_node;
3365 /* The `__null' literal. */
3367 cp_lexer_consume_token (parser->lexer);
3370 /* Recognize the `this' keyword. */
3372 cp_lexer_consume_token (parser->lexer);
3373 if (parser->local_variables_forbidden_p)
3375 error_at (token->location,
3376 "%<this%> may not be used in this context");
3377 return error_mark_node;
3379 /* Pointers cannot appear in constant-expressions. */
3380 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3381 return error_mark_node;
3382 return finish_this_expr ();
3384 /* The `operator' keyword can be the beginning of an
3389 case RID_FUNCTION_NAME:
3390 case RID_PRETTY_FUNCTION_NAME:
3391 case RID_C99_FUNCTION_NAME:
3395 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3396 __func__ are the names of variables -- but they are
3397 treated specially. Therefore, they are handled here,
3398 rather than relying on the generic id-expression logic
3399 below. Grammatically, these names are id-expressions.
3401 Consume the token. */
3402 token = cp_lexer_consume_token (parser->lexer);
3404 switch (token->keyword)
3406 case RID_FUNCTION_NAME:
3407 name = "%<__FUNCTION__%>";
3409 case RID_PRETTY_FUNCTION_NAME:
3410 name = "%<__PRETTY_FUNCTION__%>";
3412 case RID_C99_FUNCTION_NAME:
3413 name = "%<__func__%>";
3419 if (cp_parser_non_integral_constant_expression (parser, name))
3420 return error_mark_node;
3422 /* Look up the name. */
3423 return finish_fname (token->u.value);
3431 /* The `__builtin_va_arg' construct is used to handle
3432 `va_arg'. Consume the `__builtin_va_arg' token. */
3433 cp_lexer_consume_token (parser->lexer);
3434 /* Look for the opening `('. */
3435 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3436 /* Now, parse the assignment-expression. */
3437 expression = cp_parser_assignment_expression (parser,
3438 /*cast_p=*/false, NULL);
3439 /* Look for the `,'. */
3440 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3441 /* Parse the type-id. */
3442 type = cp_parser_type_id (parser);
3443 /* Look for the closing `)'. */
3444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3445 /* Using `va_arg' in a constant-expression is not
3447 if (cp_parser_non_integral_constant_expression (parser,
3449 return error_mark_node;
3450 return build_x_va_arg (expression, type);
3454 return cp_parser_builtin_offsetof (parser);
3456 case RID_HAS_NOTHROW_ASSIGN:
3457 case RID_HAS_NOTHROW_CONSTRUCTOR:
3458 case RID_HAS_NOTHROW_COPY:
3459 case RID_HAS_TRIVIAL_ASSIGN:
3460 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3461 case RID_HAS_TRIVIAL_COPY:
3462 case RID_HAS_TRIVIAL_DESTRUCTOR:
3463 case RID_HAS_VIRTUAL_DESTRUCTOR:
3464 case RID_IS_ABSTRACT:
3465 case RID_IS_BASE_OF:
3467 case RID_IS_CONVERTIBLE_TO:
3471 case RID_IS_POLYMORPHIC:
3472 case RID_IS_STD_LAYOUT:
3473 case RID_IS_TRIVIAL:
3475 return cp_parser_trait_expr (parser, token->keyword);
3477 /* Objective-C++ expressions. */
3479 case RID_AT_PROTOCOL:
3480 case RID_AT_SELECTOR:
3481 return cp_parser_objc_expression (parser);
3484 cp_parser_error (parser, "expected primary-expression");
3485 return error_mark_node;
3488 /* An id-expression can start with either an identifier, a
3489 `::' as the beginning of a qualified-id, or the "operator"
3493 case CPP_TEMPLATE_ID:
3494 case CPP_NESTED_NAME_SPECIFIER:
3498 const char *error_msg;
3501 cp_token *id_expr_token;
3504 /* Parse the id-expression. */
3506 = cp_parser_id_expression (parser,
3507 /*template_keyword_p=*/false,
3508 /*check_dependency_p=*/true,
3510 /*declarator_p=*/false,
3511 /*optional_p=*/false);
3512 if (id_expression == error_mark_node)
3513 return error_mark_node;
3514 id_expr_token = token;
3515 token = cp_lexer_peek_token (parser->lexer);
3516 done = (token->type != CPP_OPEN_SQUARE
3517 && token->type != CPP_OPEN_PAREN
3518 && token->type != CPP_DOT
3519 && token->type != CPP_DEREF
3520 && token->type != CPP_PLUS_PLUS
3521 && token->type != CPP_MINUS_MINUS);
3522 /* If we have a template-id, then no further lookup is
3523 required. If the template-id was for a template-class, we
3524 will sometimes have a TYPE_DECL at this point. */
3525 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3526 || TREE_CODE (id_expression) == TYPE_DECL)
3527 decl = id_expression;
3528 /* Look up the name. */
3531 tree ambiguous_decls;
3533 /* If we already know that this lookup is ambiguous, then
3534 we've already issued an error message; there's no reason
3536 if (id_expr_token->type == CPP_NAME
3537 && id_expr_token->ambiguous_p)
3539 cp_parser_simulate_error (parser);
3540 return error_mark_node;
3543 decl = cp_parser_lookup_name (parser, id_expression,
3546 /*is_namespace=*/false,
3547 /*check_dependency=*/true,
3549 id_expr_token->location);
3550 /* If the lookup was ambiguous, an error will already have
3552 if (ambiguous_decls)
3553 return error_mark_node;
3555 /* In Objective-C++, an instance variable (ivar) may be preferred
3556 to whatever cp_parser_lookup_name() found. */
3557 decl = objc_lookup_ivar (decl, id_expression);
3559 /* If name lookup gives us a SCOPE_REF, then the
3560 qualifying scope was dependent. */
3561 if (TREE_CODE (decl) == SCOPE_REF)
3563 /* At this point, we do not know if DECL is a valid
3564 integral constant expression. We assume that it is
3565 in fact such an expression, so that code like:
3567 template <int N> struct A {
3571 is accepted. At template-instantiation time, we
3572 will check that B<N>::i is actually a constant. */
3575 /* Check to see if DECL is a local variable in a context
3576 where that is forbidden. */
3577 if (parser->local_variables_forbidden_p
3578 && local_variable_p (decl))
3580 /* It might be that we only found DECL because we are
3581 trying to be generous with pre-ISO scoping rules.
3582 For example, consider:
3586 for (int i = 0; i < 10; ++i) {}
3587 extern void f(int j = i);
3590 Here, name look up will originally find the out
3591 of scope `i'. We need to issue a warning message,
3592 but then use the global `i'. */
3593 decl = check_for_out_of_scope_variable (decl);
3594 if (local_variable_p (decl))
3596 error_at (id_expr_token->location,
3597 "local variable %qD may not appear in this context",
3599 return error_mark_node;
3604 decl = (finish_id_expression
3605 (id_expression, decl, parser->scope,
3607 parser->integral_constant_expression_p,
3608 parser->allow_non_integral_constant_expression_p,
3609 &parser->non_integral_constant_expression_p,
3610 template_p, done, address_p,